Add opt-in automatic background server startup (#46117)

## What changed

- Add `features.daemon_auto_start`, disabled by default and available through `/experimental`, to start the shared local server for eligible new, resumed, and forked sessions. Changes take effect on the next launch, and disabling the feature persists an explicit `false`.
- Require successful daemon startup and connection when auto-start applies. On failure, show guidance to rerun the same command with `--no-daemon` instead of silently falling back to an embedded server.
- Preserve embedded mode for excluded launches, including Bedrock sign-in, and carry exclusion warnings through resume and fork pickers. Honor `--no-daemon` without an exclusion warning.
- Update `/import` guidance to recommend restarting with `codex --no-daemon`.

## Testing

Add CLI and TUI coverage for automatic daemon attachment, startup and connection failures, `--no-daemon`, picker warning persistence, and Bedrock onboarding with and without a running daemon. Add snapshots and configuration-write assertions for the experimental toggle.

GitOrigin-RevId: 2e8eb163bc7ddd268a86c7546c05a85356c1e9ee
This commit is contained in:
Eric Traut authored and copyberry committed 2026-09-17 04:53:51 +00:00
1 parent 4b0f19d6f9
commit 70e8fe1be3
37 files changed
+513 -17

No files matched your search

+55
View File
@@ -402,6 +402,12 @@ fn no_daemon_skips_startup_and_discovery() -> Result<()> {
let workspace = tempfile::tempdir()?;
let home = tempfile::tempdir()?;
write_test_config(home.path(), workspace.path())?;
let config = home.path().join("config.toml");
let contents = std::fs::read_to_string(&config)?;
std::fs::write(
config,
format!("features.daemon_auto_start = true\n{contents}"),
)?;
let socket_path = codex_app_server_client::app_server_control_socket_path(home.path())?;
std::fs::create_dir_all(socket_path.as_path().parent().unwrap())?;
let listener = if running {
@@ -431,6 +437,55 @@ fn no_daemon_skips_startup_and_discovery() -> Result<()> {
std::io::ErrorKind::WouldBlock
);
}
ensure!(
!String::from_utf8_lossy(&terminal.output)
.contains("Running without the shared background server")
);
}
Ok(())
}
#[test]
fn auto_daemon_start_failure_exits_with_manual_fallback_hint() -> Result<()> {
let workspace = tempfile::tempdir()?;
let home = tempfile::tempdir()?;
write_test_config(home.path(), workspace.path())?;
let config = home.path().join("config.toml");
let contents = std::fs::read_to_string(&config)?;
std::fs::write(
config,
format!("features.daemon_auto_start = true\n{contents}"),
)?;
// An incomplete selected package must fail without installing a replacement.
std::fs::create_dir_all(home.path().join("packages/app-server-daemon/current"))?;
let mut terminal = PtyCodex::start(workspace.path(), home, &[])?;
let deadline = Instant::now() + STARTUP_TIMEOUT;
while Instant::now() < deadline {
terminal.read_output(Duration::from_millis(/*millis*/ 50))?;
terminal.answer_startup_queries()?;
if let Some(status) = terminal.child.try_wait()? {
ensure!(!status.success());
let output = String::from_utf8_lossy(&terminal.output);
let failure = &output[output.find("Error:").context("missing fatal error")?..];
let failure = codex_ansi_escape::ansi_escape(failure)
.to_string()
.replace(
terminal
._codex_home
.path()
.canonicalize()?
.to_string_lossy()
.as_ref(),
"[CODEX_HOME]",
)
.replace(
terminal._codex_home.path().to_string_lossy().as_ref(),
"[CODEX_HOME]",
)
.replace('\r', "");
insta::assert_snapshot!("daemon_auto_start_failure", failure.trim());
return Ok(());
}
}
bail!("auto-start did not fail: {}", terminal.screen_contents())
}
@@ -0,0 +1,7 @@
---
source: tui/tests/suite/focus_palette.rs
assertion_line: 484
expression: failure.trim()
---
Error: daemon executable not found at [CODEX_HOME]/packages/app-server-daemon/current/bin/codex; repair the existing installation, or run `codex app-server daemon start` to install a missing daemon
To work without the background server, rerun the same command with --no-daemon (including resume or fork and its arguments).