Skip to content

Conversation

@aibrahim-oai
Copy link
Collaborator

@aibrahim-oai aibrahim-oai commented Sep 17, 2025

This PR introduces the ability to give agents a git work-tree. Thus, we can have multiple agents working on the same repo at the same time.

This feature is disabled by default behind enable_git_worktree config.

When enabled:

  • We check if the cwd is a git repo
  • We initialize a detached git worktree
  • We change the cwd to the worktree

Caveat:

  • resuming conversation is tricky specially if the worktree is deleted.

UX:

  • I imagine this feature will be used by power users who are capable of dealing with git worktrees. I don't have plans to support a more sophisticated UI during the next 2-3 weeks.
  • This feature will help us implement the BoN and through more compute at a problem to solve it.
  • Long Term: this can be a building stone for multi-agent management UX (not in the current scope).

Lifecycle:

  • I am exposing a cleanup function to clean the gitwork tree. In case of BoN this will be automatically used. In case of power users, we will expose a slash command to clean up when they want.

@aibrahim-oai aibrahim-oai changed the title worktree Experimental: Introduce git worktrees Sep 17, 2025

impl WorktreeHandle {
/// Create (or reuse) a worktree rooted at
/// `<repo_root>/codex/<conversation_id>`.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about <repo_root>/codex-worktree/<conversation_id> or <repo_root>/.codex/worktree/<convseration_id>?

@aibrahim-oai aibrahim-oai marked this pull request as ready for review September 17, 2025 22:39
Copy link
Contributor

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review: Here are some suggestions.

Reply with @codex fix comments to fix any unresolved comments.

About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".

Comment on lines 45 to 56
let path = codex_dir.join(conversation_id.to_string());
let is_registered = worktree_registered(&repo_root, &path).await?;

if is_registered {
if let Err(err) = ensure_codex_excluded(&repo_root).await {
warn!("failed to add codex worktree path to git exclude: {err:#}");
}
info!(
worktree = %path.display(),
"reusing existing git worktree for conversation"
);
return Ok(Self { repo_root, path });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P1] Avoid reusing deleted worktree directories

When worktree_registered returns true the code immediately returns a WorktreeHandle without verifying that the directory at codex/<conversation> actually exists. If a user deletes the worktree directory but forgets to run git worktree prune (which keeps the registration entry), subsequent sessions will reuse a non‑existent path and all commands will fail with No such file or directory. Consider checking path.exists() in the reuse branch and recreating or reporting an error so the session never operates in a missing cwd.

Useful? React with 👍 / 👎.

}

async fn ensure_codex_excluded(repo_root: &Path) -> Result<()> {
const PATTERN: &str = "/codex/";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe only /codex/worktree/ should be excluded now?

@maan2003
Copy link

you can also just write codex/worktree/.gitignore:

# managed by codex automatically
*

this avoids messing with files outside codex/

Introduce WorktreeHandle to create/reuse/remove linked checkouts under <cwd>/codex/<conversation>; add config flag enable_git_worktree (and trust logic for worktrees); plumb protocol: RemoveWorktree, WorktreeRemoved, SessionConfigured.worktree_path; update exec/TUI integration; docs: experimental config section.

# Conflicts:
#	codex-rs/core/src/codex.rs
#	codex-rs/tui/src/chatwidget.rs
@etraut-openai etraut-openai added the oai-pr PRs posted by Codex team members label Oct 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

oai-pr PRs posted by Codex team members

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants