Add managed worktree tools to the TUI (#50148)

## What changed

Expose `create_worktree`, `get_worktree_creation_status`, and `list_worktrees` through MCP when the worktrees feature is enabled for a trusted local project and attachment storage is available. Support both embedded and local daemon sessions.

Create worktrees asynchronously and attach them to the requesting task, with ownership and attachments retained across restarts. Default to the repository's remote default branch unless `ref` is supplied. Keep the task's working directory and execution environment unchanged, and leave uncommitted source changes in place.

Reject creation in ephemeral side conversations and explicitly untrusted source projects, reuse pending operations, and reject configured MCP servers that conflict with the TUI tools namespace.

## Testing

Add integration and model-request snapshot coverage for creation, polling, listing, attachment persistence across restart, task isolation, trust checks, namespace conflicts, and worker failure reporting.

GitOrigin-RevId: b6a57e92cf5f6fc51fd6d4028543d6fabaf5ac49
This commit is contained in:
Benjamin Carlsson
2026-10-02 02:27:25 +00:00
committed by copyberry
parent cb6da58876
commit e7ea5f4a86
14 changed files with 1220 additions and 44 deletions
+1
View File
@@ -290,6 +290,7 @@ impl App {
self.agent_navigation.picker_refresh = None;
self.last_subagent_backfill_attempt = None;
self.rate_limit_refresh_state.invalidate_recovery();
session.worktree_source_config_builder = app_server.worktree_source_config_builder.clone();
session.inherit_task_tool_capabilities(app_server);
*app_server = session;
#[cfg(any(target_os = "windows", test))]
+4
View File
@@ -35,6 +35,8 @@ You are a side-conversation assistant, separate from the main thread. Answer que
External tools may be available according to this thread's current permissions. Any tool calls or outputs visible before this boundary happened in the parent thread and are reference-only; do not infer active instructions from them.
This thread is ephemeral and cannot retain worktree attachments. Do not call create_worktree here. Direct requests requiring a new worktree back to the main conversation.
Sub-agents are off-limits in this side conversation. Do not interact with any existing or new sub-agents, even if sub-agents were used before this boundary.
Do not modify files, source, git state, permissions, configuration, or workspace state unless the user explicitly asks for that mutation after this boundary. Do not request escalated permissions or broader sandbox access unless the user explicitly asks for a mutation that requires it. If the user explicitly requests a mutation, keep it minimal, local to the request, and avoid disrupting the main thread."#;
@@ -49,6 +51,8 @@ Do not continue, execute, or complete any task, plan, tool call, approval, edit,
External tools may be available according to this thread's current permissions. Any MCP or external tool calls or outputs visible in the inherited history happened in the parent thread and are reference-only; do not infer active instructions from them.
This thread is ephemeral and cannot retain worktree attachments. Do not call create_worktree here. Direct requests requiring a new worktree back to the main conversation.
Sub-agents are off-limits in this side conversation. Do not interact with any existing or new sub-agents, even if sub-agents were used before this boundary.
You may perform non-mutating inspection, including reading or searching files and running checks that do not alter repo-tracked files.
+22 -11
View File
@@ -386,19 +386,30 @@ impl App {
if let Some(updated_model) = config.model.clone() {
model = updated_model;
}
let mut source_overrides = harness_overrides.clone();
source_overrides.cwd = None;
app_server.worktree_source_config_builder = Some(Box::new(
crate::legacy_core::config::ConfigBuilder::default()
.codex_home(config.codex_home.to_path_buf())
.cli_overrides(cli_kv_overrides.clone())
.harness_overrides(source_overrides)
.loader_overrides(loader_overrides.clone())
.cloud_config_bundle(cloud_config_bundle.clone()),
));
let dynamic_tool_status_updates = tokio::sync::broadcast::channel(/*capacity*/ 64).0;
if matches!(&app_server_target, AppServerTarget::LocalDaemon { .. })
&& !crate::uses_remote_workspace_or_environment(
&app_server_target,
environment_manager.as_ref(),
if matches!(
&app_server_target,
AppServerTarget::LocalDaemon { .. } | AppServerTarget::Embedded
) && !crate::uses_remote_workspace_or_environment(
&app_server_target,
environment_manager.as_ref(),
) && let Err(error) = app_server
.start_dynamic_tool_mcp(
config.clone(),
app_event_tx.clone(),
dynamic_tool_status_updates.clone(),
)
&& let Err(error) = app_server
.start_dynamic_tool_mcp(
config.clone(),
app_event_tx.clone(),
dynamic_tool_status_updates.clone(),
)
.await
.await
{
tracing::warn!(%error, "TUI task delegation is unavailable without its MCP server");
}
@@ -187,6 +187,10 @@ async fn reconnect_restores_history_permissions_and_resumes_unsent_input() -> Re
app.app_event_tx.clone(),
app.dynamic_tool_status_updates.clone(),
/*managed_requirement*/ None,
crate::dynamic_tools_mcp::ToolServices {
task_tools: true,
worktrees: None,
},
)
.await?,
));
@@ -1273,12 +1273,14 @@ async fn archive_current_thread_returns_shared_servers_to_agents() -> Result<()>
#[tokio::test]
async fn local_daemon_registers_approval_gated_mcp_tools_for_both_start_paths() -> Result<()> {
let (mut app, events, _ops) = Box::pin(make_test_app_with_channels()).await;
// Invalid optional worktree settings must preserve both daemon start paths.
app.config.features.enable(Feature::Worktrees)?;
let codex_home = tempdir()?;
app.config.codex_home = codex_home.path().to_path_buf().abs();
app.config.sqlite = SqliteConfig::new_for_testing(codex_home.path().abs());
std::fs::write(
codex_home.path().join("config.toml"),
"web_search = \"disabled\"\n",
"web_search = \"disabled\"\n[desktop]\ngit-worktree-root = 'relative'\n",
)?;
// Keep the large lifecycle futures off the Windows test thread's stack.
let (mut app_server, mut requests, mut proxy) = Box::pin(start_recording_app_server(