mirror of
https://github.com/openai/codex.git
synced 2026-12-02 00:31:03 +00:00
Repair app-server daemon packages with update --from-cli (#65046)
## Why A daemon with a dangling package selection or a missing executable can fail to start, and leftover logs can make an absent installation look like an existing one. ## What changed - Let `codex app-server daemon update --from-cli` install or repair and pin the daemon from a complete CLI package. Point missing-executable errors to that command. Repair checks that the daemon and updater are stopped and that the package selection has stayed unchanged. - Let `codex app-server daemon start` bootstrap a missing installation even when old daemon logs remain. Use legacy logs to infer a stopped standalone installation only when its package selection still exists and dedicated daemon logs are absent. - Allow repair to replace a dangling Windows junction whose target is directly under the daemon's releases directory. ## Testing Extend coverage for startup with leftover logs, package repair after losing the selection or executable, repair blocked by a starting daemon or updater, and replacement of a dangling Windows junction. GitOrigin-RevId: 679266cc22b7ff7a34bbd41c2bf0659d3068eb75
This commit is contained in:
@@ -1120,7 +1120,7 @@ impl Daemon {
|
||||
|
||||
let managed_codex_path = self.managed_codex_bin.display();
|
||||
Err(anyhow!(
|
||||
"daemon executable not found at {managed_codex_path}; repair the existing installation, or run `codex app-server daemon start` to install a missing daemon"
|
||||
"daemon executable not found at {managed_codex_path}; run `codex app-server daemon update --from-cli` from a complete CLI package to repair and pin the daemon"
|
||||
))
|
||||
}
|
||||
|
||||
|
||||
@@ -15,42 +15,27 @@ use tokio::process::Command;
|
||||
use tokio::time::timeout;
|
||||
|
||||
/// New daemons own their packages, regardless of how the calling CLI was installed.
|
||||
/// Preserve legacy launch state, including logs left after a daemon is stopped;
|
||||
/// settings, installer selections, and lock files alone do not prove a prior launch.
|
||||
/// Legacy logs identify a stopped standalone daemon only while its selection exists.
|
||||
pub(crate) fn package_root(codex_home: &Path) -> PathBuf {
|
||||
let dedicated = codex_home.join("packages/app-server-daemon");
|
||||
if !matches!(dedicated.join("current").symlink_metadata(),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound)
|
||||
let present = |path: PathBuf| !matches!(path.symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound);
|
||||
let state = codex_home.join("app-server-daemon");
|
||||
if present(dedicated.join("current"))
|
||||
|| present(state.join(crate::DAEMON_PID_FILE_NAME))
|
||||
|| present(state.join(crate::DAEMON_UPDATE_PID_FILE_NAME))
|
||||
{
|
||||
return dedicated;
|
||||
}
|
||||
let state = codex_home.join("app-server-daemon");
|
||||
for (package, artifacts) in [
|
||||
(
|
||||
"app-server-daemon",
|
||||
[
|
||||
crate::DAEMON_PID_FILE_NAME,
|
||||
"daemon.stderr.log",
|
||||
crate::DAEMON_UPDATE_PID_FILE_NAME,
|
||||
"daemon-updater.stderr.log",
|
||||
],
|
||||
),
|
||||
(
|
||||
"standalone",
|
||||
[
|
||||
crate::LEGACY_PID_FILE_NAME,
|
||||
"app-server.stderr.log",
|
||||
crate::LEGACY_UPDATE_PID_FILE_NAME,
|
||||
"app-server-updater.stderr.log",
|
||||
],
|
||||
),
|
||||
] {
|
||||
if artifacts.iter().any(|name| {
|
||||
!matches!(state.join(name).symlink_metadata(),
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound)
|
||||
}) {
|
||||
return codex_home.join("packages").join(package);
|
||||
}
|
||||
let standalone = codex_home.join("packages/standalone");
|
||||
if present(state.join(crate::LEGACY_PID_FILE_NAME))
|
||||
|| present(state.join(crate::LEGACY_UPDATE_PID_FILE_NAME))
|
||||
|| (present(standalone.join("current"))
|
||||
&& !present(state.join("daemon.stderr.log"))
|
||||
&& !present(state.join("daemon-updater.stderr.log"))
|
||||
&& (present(state.join("app-server.stderr.log"))
|
||||
|| present(state.join("app-server-updater.stderr.log"))))
|
||||
{
|
||||
return standalone;
|
||||
}
|
||||
dedicated
|
||||
}
|
||||
|
||||
@@ -30,10 +30,17 @@ fn discovers_package_and_legacy_installs() {
|
||||
std::fs::write(&packaged, b"packaged").expect("packaged executable");
|
||||
assert_eq!(super::managed_codex_bin(home.path()), packaged);
|
||||
|
||||
// After migration, losing the dedicated selection must not reactivate the old CLI.
|
||||
std::fs::write(state.join("daemon.stderr.log"), b"").unwrap();
|
||||
assert_eq!(
|
||||
super::package_root(home.path()),
|
||||
home.path().join("packages/app-server-daemon")
|
||||
);
|
||||
std::fs::remove_file(state.join("daemon.stderr.log")).unwrap();
|
||||
std::fs::remove_dir_all(home.path().join("packages/standalone")).unwrap();
|
||||
assert_eq!(
|
||||
super::package_root(home.path()),
|
||||
home.path().join("packages/standalone")
|
||||
home.path().join("packages/app-server-daemon")
|
||||
);
|
||||
std::fs::remove_file(state.join("app-server.stderr.log")).unwrap();
|
||||
std::fs::write(state.join("app-server.pid"), b"running daemon").unwrap();
|
||||
@@ -51,7 +58,7 @@ fn discovers_package_and_legacy_installs() {
|
||||
std::fs::write(state.join("daemon.stderr.log"), b"").unwrap();
|
||||
assert_eq!(
|
||||
super::package_root(home.path()),
|
||||
home.path().join("packages/app-server-daemon")
|
||||
home.path().join("packages/standalone")
|
||||
);
|
||||
|
||||
#[cfg(unix)]
|
||||
|
||||
@@ -112,17 +112,10 @@ async fn prepare_from_package(
|
||||
return Ok(true);
|
||||
}
|
||||
if !matches!(root.join("current").symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound)
|
||||
|| ["daemon.pid", "daemon.stderr.log", "daemon-updater.pid", "daemon-updater.stderr.log"]
|
||||
.iter().any(|name| !matches!(home.join("app-server-daemon").join(name).symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound))
|
||||
{
|
||||
daemon.ensure_managed_codex_bin()?;
|
||||
return Ok(true);
|
||||
}
|
||||
} else {
|
||||
anyhow::ensure!(
|
||||
previous_root.join("current").symlink_metadata().is_ok(),
|
||||
"no daemon package is selected; run `codex app-server daemon start` first"
|
||||
);
|
||||
}
|
||||
std::fs::create_dir_all(&root)?;
|
||||
anyhow::ensure!(
|
||||
@@ -135,17 +128,33 @@ async fn prepare_from_package(
|
||||
"app server is running but is not managed by codex app-server daemon"
|
||||
);
|
||||
let selected = managed_install::managed_codex_bin(home);
|
||||
let previous_release = previous_root.join("current").canonicalize().ok();
|
||||
if mode == InstallMode::Missing {
|
||||
if selected.is_file() {
|
||||
return Ok(true);
|
||||
}
|
||||
let current = previous_root.join("current");
|
||||
let previous_release = current.canonicalize().ok();
|
||||
// Two dangling selections can have different targets even though neither resolves.
|
||||
let previous_link = std::fs::read_link(¤t).ok();
|
||||
let installed = selected.is_file();
|
||||
let selection_unchanged = || {
|
||||
current.canonicalize().ok() == previous_release
|
||||
&& std::fs::read_link(¤t).ok() == previous_link
|
||||
&& managed_install::managed_codex_bin(home).is_file() == installed
|
||||
};
|
||||
if !installed {
|
||||
anyhow::ensure!(
|
||||
backend.is_none()
|
||||
&& matches!(root.join("current").symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound),
|
||||
"the selected daemon package is incomplete; repair its installation before starting"
|
||||
backend.is_none(),
|
||||
"stop the daemon with `codex app-server daemon stop` before repairing its package"
|
||||
);
|
||||
if previous_root == root && previous_link.is_none() {
|
||||
anyhow::ensure!(
|
||||
matches!(current.symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound),
|
||||
"cannot replace {}; move the incomplete selection aside before retrying",
|
||||
current.display()
|
||||
);
|
||||
}
|
||||
}
|
||||
anyhow::ensure!(
|
||||
mode == InstallMode::Replace || (previous_link.is_none() && !installed),
|
||||
"daemon selection changed; retry the command"
|
||||
);
|
||||
let source = source.context(
|
||||
"this CLI has no complete local package; install a packaged Codex CLI or use the standalone installer",
|
||||
)?;
|
||||
@@ -219,7 +228,7 @@ async fn prepare_from_package(
|
||||
return Ok(true);
|
||||
}
|
||||
anyhow::ensure!(
|
||||
previous_root.join("current").canonicalize().ok() == previous_release,
|
||||
selection_unchanged(),
|
||||
"daemon selection changed while awaiting confirmation; retry the command"
|
||||
);
|
||||
let current_backend = daemon.running_backend_instance(settings).await?;
|
||||
@@ -298,12 +307,67 @@ async fn prepare_from_package(
|
||||
!stable || version == binary_version,
|
||||
"the CLI package version does not match its executable"
|
||||
);
|
||||
let name = if stable && mode == InstallMode::Missing {
|
||||
let mut name = if stable && mode == InstallMode::Missing {
|
||||
format!("{version}-{target}")
|
||||
} else {
|
||||
format!("local-{digest}-{target}")
|
||||
};
|
||||
let release = releases.join(&name);
|
||||
let mut release = releases.join(&name);
|
||||
// A pinned copy of this very CLI may have lost its executable. Keep that
|
||||
// directory intact and use a distinct, reusable local release name.
|
||||
while mode == InstallMode::Replace
|
||||
&& release.is_dir()
|
||||
&& !release.join(entrypoint).try_exists()?
|
||||
{
|
||||
let alternate = blake3::hash(name.as_bytes());
|
||||
name = format!("local-{alternate}-{target}");
|
||||
release = releases.join(&name);
|
||||
}
|
||||
if !installed {
|
||||
if mode == InstallMode::Replace {
|
||||
crate::update_policy::ensure_allowed(daemon).await?;
|
||||
}
|
||||
// Check both PID namespaces under the locks before publishing: even
|
||||
// creating the release can make a dangling selection live again.
|
||||
for (pid, updater) in [
|
||||
(
|
||||
crate::DAEMON_PID_FILE_NAME,
|
||||
crate::DAEMON_UPDATE_PID_FILE_NAME,
|
||||
),
|
||||
(
|
||||
crate::LEGACY_PID_FILE_NAME,
|
||||
crate::LEGACY_UPDATE_PID_FILE_NAME,
|
||||
),
|
||||
] {
|
||||
let mut paths = daemon.backend_paths(settings);
|
||||
paths.pid_file.set_file_name(pid);
|
||||
paths.update_pid_file.set_file_name(updater);
|
||||
for path in [&paths.pid_file, &paths.update_pid_file] {
|
||||
anyhow::ensure!(
|
||||
!path.is_symlink(),
|
||||
"cannot verify daemon PID file {}",
|
||||
path.display()
|
||||
);
|
||||
}
|
||||
anyhow::ensure!(
|
||||
!crate::backend::pid_backend(paths.clone())
|
||||
.is_starting_or_running()
|
||||
.await?
|
||||
&& !crate::backend::pid_update_loop_backend(paths)
|
||||
.is_starting_or_running()
|
||||
.await?,
|
||||
"the daemon or updater is starting or running; both must be stopped before repairing the package"
|
||||
);
|
||||
}
|
||||
anyhow::ensure!(
|
||||
managed_install::package_root(home) == previous_root
|
||||
&& selection_unchanged()
|
||||
&& crate::client::probe(&daemon.socket_path).await.is_err(),
|
||||
"daemon selection or socket ownership changed while preparing its package; retry the command"
|
||||
);
|
||||
}
|
||||
#[cfg(windows)]
|
||||
windows::validate_selection(&root)?;
|
||||
if release.try_exists()? {
|
||||
anyhow::ensure!(
|
||||
!release.symlink_metadata()?.file_type().is_symlink()
|
||||
@@ -340,17 +404,14 @@ async fn prepare_from_package(
|
||||
if backend.is_some() {
|
||||
crate::backend::windows::ensure_detached_launch(&release.join(entrypoint))?;
|
||||
}
|
||||
#[cfg(windows)]
|
||||
windows::validate_selection(&root)?;
|
||||
if mode == InstallMode::Replace {
|
||||
if mode == InstallMode::Replace && installed {
|
||||
crate::update_policy::ensure_allowed(daemon).await?;
|
||||
let stopped = async {
|
||||
crate::backend::pid_update_loop_backend(daemon.backend_paths(settings))
|
||||
.stop()
|
||||
.await?;
|
||||
anyhow::ensure!(
|
||||
managed_install::package_root(home) == previous_root
|
||||
&& previous_root.join("current").canonicalize().ok() == previous_release,
|
||||
managed_install::package_root(home) == previous_root && selection_unchanged(),
|
||||
"daemon selection changed while preparing its package; retry the command"
|
||||
);
|
||||
if let Some(backend) = &backend {
|
||||
|
||||
@@ -444,12 +444,12 @@ async fn explicit_selection_requires_unchanged_cli_and_pins_all_versions() {
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn broken_selection_is_not_a_missing_installation() {
|
||||
async fn broken_selection_requires_explicit_repair() {
|
||||
let temp = tempfile::TempDir::new().unwrap();
|
||||
let home = temp.path().join("home");
|
||||
let source = temp.path().join("source");
|
||||
let bin = package(&source, "0.152.0");
|
||||
let daemon = daemon(&home);
|
||||
let daemon = daemon(&home).current_installation().unwrap();
|
||||
let current = home.join("packages/app-server-daemon/current");
|
||||
std::fs::create_dir_all(current.parent().unwrap()).unwrap();
|
||||
std::os::unix::fs::symlink("missing-release", ¤t).unwrap();
|
||||
@@ -466,12 +466,54 @@ async fn broken_selection_is_not_a_missing_installation() {
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("repair the existing installation")
|
||||
.contains("codex app-server daemon update --from-cli")
|
||||
);
|
||||
let start_error = daemon.start(&BTreeMap::new()).await.unwrap_err();
|
||||
assert_eq!(start_error.failure_reason(), "install");
|
||||
assert_eq!(
|
||||
std::fs::read_link(current).unwrap(),
|
||||
std::fs::read_link(¤t).unwrap(),
|
||||
PathBuf::from("missing-release")
|
||||
);
|
||||
|
||||
let settings = DaemonSettings::default();
|
||||
let repair = || {
|
||||
prepare_from_package(
|
||||
&daemon,
|
||||
&settings,
|
||||
InstallMode::Replace,
|
||||
Some(&source),
|
||||
&bin,
|
||||
|_| Ok(true),
|
||||
)
|
||||
};
|
||||
std::fs::create_dir_all(home.join("app-server-daemon")).unwrap();
|
||||
let updater = daemon.update_pid_file.clone();
|
||||
std::fs::write(&updater, b"ambiguous").unwrap();
|
||||
repair().await.expect_err("cannot verify updater");
|
||||
assert_eq!(std::fs::read(&updater).unwrap(), b"ambiguous");
|
||||
std::fs::remove_file(&updater).unwrap();
|
||||
for pid in [&daemon.pid_file, &updater] {
|
||||
let reservation = std::fs::File::create(pid.with_extension("pid.lock")).unwrap();
|
||||
reservation.lock().unwrap();
|
||||
repair().await.expect_err("process is starting");
|
||||
assert_eq!(
|
||||
std::fs::read_link(¤t).unwrap(),
|
||||
PathBuf::from("missing-release")
|
||||
);
|
||||
}
|
||||
|
||||
assert!(repair().await.unwrap());
|
||||
let pinned = current.canonicalize().unwrap();
|
||||
std::fs::remove_file(pinned.join("bin/codex")).unwrap();
|
||||
assert!(repair().await.unwrap());
|
||||
assert!(pinned.is_dir());
|
||||
assert_eq!(
|
||||
std::fs::read(current.join("bin/codex")).unwrap(),
|
||||
std::fs::read(&bin).unwrap()
|
||||
);
|
||||
let repaired = current.canonicalize().unwrap();
|
||||
std::fs::remove_file(¤t).unwrap();
|
||||
assert!(repair().await.unwrap());
|
||||
assert_eq!(current.canonicalize().unwrap(), repaired);
|
||||
assert!(!current.with_file_name("auto-update-version").exists());
|
||||
}
|
||||
|
||||
@@ -200,8 +200,24 @@ pub(super) fn validate_selection(root: &Path) -> Result<()> {
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
let selected = match current.canonicalize() {
|
||||
Ok(selected) => selected,
|
||||
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
|
||||
// A dangling junction must still point directly into our releases.
|
||||
let target = root.join(std::fs::read_link(¤t)?);
|
||||
anyhow::ensure!(
|
||||
matches!(target.symlink_metadata(), Err(error) if error.kind() == std::io::ErrorKind::NotFound),
|
||||
"refusing to replace an unresolvable daemon release"
|
||||
);
|
||||
let parent = target.parent().context("missing junction target parent")?;
|
||||
parent
|
||||
.canonicalize()?
|
||||
.join(target.file_name().context("missing release name")?)
|
||||
}
|
||||
Err(error) => return Err(error.into()),
|
||||
};
|
||||
anyhow::ensure!(
|
||||
current.canonicalize()?.parent() == Some(root.join("releases").canonicalize()?.as_path()),
|
||||
selected.parent() == Some(root.join("releases").canonicalize()?.as_path()),
|
||||
"refusing to replace a daemon selection outside its releases directory"
|
||||
);
|
||||
Ok(())
|
||||
|
||||
@@ -39,7 +39,12 @@ fn daemon_junction_can_be_created_retargeted_and_replaced_without_cli_links() {
|
||||
let root = home
|
||||
.path()
|
||||
.join("home & 100% ready!/packages/app-server-daemon");
|
||||
for (version, native_denied) in [("first", true), ("second", false), ("third", true)] {
|
||||
for (version, native_denied) in [
|
||||
("first", true),
|
||||
("second", false),
|
||||
("third", true),
|
||||
("repaired", false),
|
||||
] {
|
||||
let release = root.join("releases").join(version);
|
||||
std::fs::create_dir_all(&release).unwrap();
|
||||
if native_denied {
|
||||
@@ -55,6 +60,9 @@ fn daemon_junction_can_be_created_retargeted_and_replaced_without_cli_links() {
|
||||
root.join("current").canonicalize().unwrap(),
|
||||
release.canonicalize().unwrap()
|
||||
);
|
||||
if version == "third" {
|
||||
std::fs::remove_dir(&release).unwrap();
|
||||
}
|
||||
}
|
||||
assert!(!root.with_file_name("standalone").exists());
|
||||
}
|
||||
|
||||
@@ -69,6 +69,11 @@ fn packaged_daemon_launch(action: &str, initial: InitialDaemon) -> Result<()> {
|
||||
let state = daemon.home.path().join("app-server-daemon");
|
||||
if initial == InitialDaemon::Missing {
|
||||
std::fs::remove_file(state.join("app-server.stderr.log"))?;
|
||||
if action == "start" {
|
||||
for log in ["daemon.stderr.log", "daemon-updater.stderr.log"] {
|
||||
std::fs::write(state.join(log), b"previous launch")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::fs::write(
|
||||
state.join("settings.json"),
|
||||
|
||||
+1
-1
@@ -3,5 +3,5 @@ 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
|
||||
Error: daemon executable not found at [CODEX_HOME]/packages/app-server-daemon/current/bin/codex; run `codex app-server daemon update --from-cli` from a complete CLI package to repair and pin the daemon
|
||||
To work without the background server, rerun the same command with --no-daemon (including resume or fork and its arguments).
|
||||
|
||||
Reference in New Issue
Block a user