Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

One helper keeps a waiting tincan copy current, for both updaters

Lars Talian22cb65efcc9d
diff --git a/src/daemon/update.rs b/src/daemon/update.rs
index 6521887..1708a27 100644
--- a/src/daemon/update.rs
+++ b/src/daemon/update.rs
@@ -39,11 +39,8 @@ async fn update() -> Result<Option<String>> {
return Ok(None);
}
let dir = crate::util::current_exe()?.parent().map(|d| d.to_path_buf()).unwrap_or_default();
- // A tincan copy still waiting to be moved over (see `migrate`) is a copy of this binary: it stays one.
let exe = dir.join(format!("{}{}", crate::AGENT_NAME, std::env::consts::EXE_SUFFIX));
- let copy = crate::setup::legacy_exe(&exe);
- let waiting = std::fs::symlink_metadata(&copy).is_ok_and(|m| m.is_file())
- && std::fs::read(&copy).is_ok_and(|b| std::fs::read(&exe).is_ok_and(|o| o == b));
+ let waiting = crate::migrate::waiting(&exe);
let mut cmd = tokio::process::Command::from(crate::installer(&dir));
cmd.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
@@ -70,11 +67,8 @@ async fn update() -> Result<Option<String>> {
if !out.status.success() {
bail!("install failed: {}", String::from_utf8_lossy(&out.stderr).trim());
}
- // A new file renamed over it: never a moment without tincan, nor a half-written one. (A tincan.exe that's running
- // can't be replaced: it stays as it was, still handing over to us.)
- let new = copy.with_extension("new");
- if waiting && std::fs::copy(&exe, &new).is_ok() && std::fs::rename(&new, &copy).is_err() {
- let _ = std::fs::remove_file(&new);
+ if waiting {
+ crate::migrate::keep_waiting_copy(&exe);
}
// Restart only on a binary that really is newer (a release tagged without bumping its version must not loop).
let installed = tokio::process::Command::new(&exe).arg("--version").kill_on_drop(true).output();
diff --git a/src/main.rs b/src/main.rs
index 1a321c2..f68300e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -540,9 +540,14 @@ async fn cli(cmd: Cmd) -> Result<()> {
bail!("the network engine updates with the reason CLI: update reason, then run `{CLI} setup`");
}
let dir = util::current_exe()?.parent().map(|d| d.to_path_buf()).unwrap_or_default();
+ let exe = dir.join(format!("{AGENT_NAME}{}", std::env::consts::EXE_SUFFIX));
+ let waiting = migrate::waiting(&exe);
if !installer(&dir).status()?.success() {
bail!("upgrade failed");
}
+ if waiting {
+ migrate::keep_waiting_copy(&exe);
+ }
stop_daemon().await;
println!("The daemon restarted on the new version. Restart agent sessions to update their tools.");
}
diff --git a/src/migrate.rs b/src/migrate.rs
index 97a0e25..460075d 100644
--- a/src/migrate.rs
+++ b/src/migrate.rs
@@ -22,6 +22,24 @@ pub fn as_ours() {
}
}
+/// Whether a tincan copy beside `exe` is still waiting to be moved over: a copy of this very binary.
+pub fn waiting(exe: &std::path::Path) -> bool {
+ let old = setup::legacy_exe(exe);
+ std::fs::symlink_metadata(&old).is_ok_and(|m| m.is_file())
+ && std::fs::read(&old).is_ok_and(|b| std::fs::read(exe).is_ok_and(|o| o == b))
+}
+
+/// After an update replaced `exe`, keeps a copy that was waiting a copy of the new binary, so the move-over can still
+/// finish. A new file renamed over it: never a moment without tincan, nor a half-written one. (A tincan.exe that's
+/// running can't be replaced: it stays as it was, still handing over to us.)
+pub fn keep_waiting_copy(exe: &std::path::Path) {
+ let old = setup::legacy_exe(exe);
+ let new = old.with_extension("new");
+ if std::fs::copy(exe, &new).is_err() || std::fs::rename(&new, &old).is_err() {
+ let _ = std::fs::remove_file(&new);
+ }
+}
+
/// Moves this machine's agents and login service over from the tincan beside us, if that hasn't happened yet.
/// Lines go to `log`.
pub async fn run(log: fn(&str)) {
@@ -29,8 +47,7 @@ pub async fn run(log: fn(&str)) {
let old = setup::legacy_exe(&exe);
// Only tincan's last update leaves this: a copy of this very binary under the old name. Anything else called tincan
// (a tincan of its own, a script) isn't ours to move. Once moved, it's a link to us, or gone.
- let copy = std::fs::symlink_metadata(&old).is_ok_and(|m| m.is_file())
- && std::fs::read(&old).is_ok_and(|b| std::fs::read(&exe).is_ok_and(|o| o == b));
+ let copy = waiting(&exe);
// Where the Reason CLI's engine is installed too, it owns the agents and the service.
let reason_cli = paths::user_home().join(".reason").join("bin").join(exe.file_name().unwrap_or_default()).exists();
if util::from_source() || crate::managed() || reason_cli || !copy {