1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Self-Update Module
//!
//! Handles building, testing, and hot-restarting OpenCrabs.
//! The running binary is in memory — modifying source on disk is safe.
//! After a successful build, `exec()` replaces the current process with the new binary.
//!
//! If the binary was downloaded (no source tree), `auto_detect()` automatically
//! clones the repo into `~/.opencrabs/source/` so `/rebuild` works everywhere.
use anyhow::Result;
use std::path::PathBuf;
use uuid::Uuid;
/// GitHub repo URL for auto-cloning when source is not available locally.
const REPO_URL: &str = "https://github.com/adolfousier/opencrabs.git";
/// Handles building, testing, and restarting OpenCrabs from source.
pub struct SelfUpdater {
/// Root of the OpenCrabs project (where Cargo.toml lives)
project_root: PathBuf,
/// Path to the compiled binary
binary_path: PathBuf,
}
impl SelfUpdater {
/// Create a new SelfUpdater.
///
/// `project_root` — directory containing Cargo.toml
/// `binary_path` — where the release binary will be after build
pub fn new(project_root: PathBuf, binary_path: PathBuf) -> Self {
Self {
project_root,
binary_path,
}
}
/// Auto-detect project root and binary path from the current executable.
///
/// **Source tree found** (Cargo.toml walks up from exe): uses
/// `<project_root>/target/release/opencrabs` as the binary path.
/// This is the `/rebuild` path.
///
/// **Pre-built binary** (no Cargo.toml): uses the current executable
/// path (`std::env::current_exe()`) as the binary path. This is the
/// `/evolve` path: the download already replaced the binary at this
/// location, so restart just exec's it. Source is lazily cloned into
/// `~/.opencrabs/source/` only when `/rebuild` is invoked.
///
/// Before this fix, auto_detect() cloned unconditionally then pointed
/// binary_path at a target/ dir that was never built, causing
/// "exec() failed: No such file or directory" on restart (#179).
pub fn auto_detect() -> Result<Self> {
let exe = std::env::current_exe()?;
let source_dir = crate::config::opencrabs_home().join("source");
let (project_root, binary_path) = Self::resolve_paths(&exe, source_dir)?;
tracing::info!(
"SelfUpdater: project_root={}, binary={}",
project_root.display(),
binary_path.display()
);
Ok(Self {
project_root,
binary_path,
})
}
/// Pure path-resolution for `auto_detect`, factored out so it can be
/// tested without depending on `current_exe()`.
///
/// Walking up from `exe`, the FIRST directory containing `Cargo.toml` is
/// a source tree → `(root, root/target/release/opencrabs)` (the
/// `/rebuild` build output). If no `Cargo.toml` is found, this is a
/// pre-built install → `(source_dir, exe)`: the binary path is the
/// running exe itself (which `/evolve` replaces in place), and
/// `source_dir` is only used as the lazy-clone target for `/rebuild`.
///
/// The pre-built branch returning `exe` (not a never-built
/// `source_dir/target/release/opencrabs`) is the #179 fix: restart after
/// auto-update must exec the real binary, not a path that was never built.
pub(crate) fn resolve_paths(
exe: &std::path::Path,
source_dir: std::path::PathBuf,
) -> Result<(std::path::PathBuf, std::path::PathBuf)> {
let mut search_dir = exe
.parent()
.ok_or_else(|| anyhow::anyhow!("Cannot determine executable parent directory"))?
.to_path_buf();
loop {
if search_dir.join("Cargo.toml").exists() {
let binary_path = search_dir.join("target").join("release").join("opencrabs");
return Ok((search_dir, binary_path));
}
if !search_dir.pop() {
break;
}
}
// Pre-built binary: no source tree. binary_path = the running exe.
Ok((source_dir, exe.to_path_buf()))
}
/// Ensure the source tree exists at project_root (lazy clone).
/// Called by build() when the user invokes /rebuild on a pre-built binary.
fn ensure_source_tree(&self) -> Result<()> {
if self.project_root.join("Cargo.toml").exists() {
// Already have source. Pull latest.
tracing::info!("Updating source at {}", self.project_root.display());
let _ = std::process::Command::new("git")
.args(["pull", "--ff-only"])
.current_dir(&self.project_root)
.output();
return Ok(());
}
// Clone the repo
tracing::info!(
"Cloning OpenCrabs source to {}",
self.project_root.display()
);
let output = std::process::Command::new("git")
.args([
"clone",
"--depth",
"1",
REPO_URL,
&self.project_root.to_string_lossy(),
])
.output()
.map_err(|e| anyhow::anyhow!("Failed to clone source (is git installed?): {}", e))?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return Err(anyhow::anyhow!("git clone failed: {}", stderr));
}
Ok(())
}
/// Build the project with `cargo build --release`.
///
/// Returns `Ok(binary_path)` on success or `Err(compiler_output)` on failure.
pub async fn build(&self) -> Result<PathBuf, String> {
self.build_streaming(|_| {}).await
}
/// Build with streaming progress — calls `on_line` for each compiler output line.
///
/// Returns `Ok(binary_path)` on success or `Err(compiler_output)` on failure.
pub async fn build_streaming<F>(&self, on_line: F) -> Result<PathBuf, String>
where
F: Fn(String) + Send + 'static,
{
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
// Lazy clone source tree if needed (pre-built binary + /rebuild).
if let Err(e) = self.ensure_source_tree() {
return Err(format!("Failed to prepare source tree: {e}"));
}
tracing::info!("Building OpenCrabs at {}", self.project_root.display());
let mut child = Command::new("cargo")
.args(["build", "--release"])
.env("RUSTFLAGS", "-C target-cpu=native")
.current_dir(&self.project_root)
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.map_err(|e| format!("Failed to spawn cargo build: {}", e))?;
// Stream stderr (where cargo writes progress) line by line
if let Some(stderr) = child.stderr.take() {
let mut lines = BufReader::new(stderr).lines();
while let Ok(Some(line)) = lines.next_line().await {
on_line(line);
}
}
let status = child
.wait()
.await
.map_err(|e| format!("Build process error: {}", e))?;
if status.success() {
// For pre-built binaries, binary_path points at the exe path
// (which /evolve replaced). After /rebuild, the binary is at
// <project_root>/target/release/opencrabs. Use whichever exists.
let built_path = self
.project_root
.join("target")
.join("release")
.join("opencrabs");
let result_path = if built_path.exists() {
built_path
} else {
self.binary_path.clone()
};
tracing::info!("Build succeeded: {}", result_path.display());
Ok(result_path)
} else {
Err("Build failed — see output above".to_string())
}
}
/// Run tests with `cargo test`.
///
/// Returns `Ok(())` on success or `Err(test_output)` on failure.
pub async fn test(&self) -> Result<(), String> {
tracing::info!("Running tests at {}", self.project_root.display());
let output = tokio::process::Command::new("cargo")
.arg("test")
.current_dir(&self.project_root)
.output()
.await
.map_err(|e| format!("Failed to spawn cargo test: {}", e))?;
if output.status.success() {
tracing::info!("Tests passed");
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr).to_string();
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
tracing::warn!("Tests failed:\n{}\n{}", stderr, stdout);
Err(format!("{}\n{}", stderr, stdout))
}
}
/// Replace the running process with the new binary via Unix exec().
///
/// Passes `chat --session <session_id>` to resume the same session.
/// This function only returns on error — on success, the process is replaced.
#[cfg(unix)]
pub fn restart(&self, session_id: Uuid) -> Result<()> {
Self::restart_into(&self.binary_path, session_id)
}
/// On non-Unix platforms, restart is not supported via exec().
#[cfg(not(unix))]
pub fn restart(&self, session_id: Uuid) -> Result<()> {
Self::restart_into(&self.binary_path, session_id)
}
/// Exec-restart into a SPECIFIC binary path. Used by the RestartReady
/// handler to launch the exact binary that was just produced (e.g.
/// `/rebuild` returns a freshly-built binary that is NOT the running
/// exe on a pre-built install). Resolving the path via `auto_detect()`
/// instead would pick the stale running exe and restart into the old
/// version (#179 follow-up). Passes `chat --session <id>` to resume the
/// same session. Only returns on error — on success the process is
/// replaced.
#[cfg(unix)]
pub fn restart_into(binary_path: &std::path::Path, session_id: Uuid) -> Result<()> {
use std::os::unix::process::CommandExt;
tracing::info!(
"Restarting OpenCrabs: {} chat --session {}",
binary_path.display(),
session_id
);
let err = std::process::Command::new(binary_path)
.args(["chat", "--session", &session_id.to_string()])
.env("OPENCRABS_EVOLVED_FROM", crate::VERSION)
.exec(); // Replaces the process — only returns on error
Err(anyhow::anyhow!("exec() failed: {}", err))
}
#[cfg(not(unix))]
pub fn restart_into(_binary_path: &std::path::Path, _session_id: Uuid) -> Result<()> {
Err(anyhow::anyhow!(
"Hot restart via exec() is only supported on Unix platforms"
))
}
/// Get the project root path.
pub fn project_root(&self) -> &std::path::Path {
&self.project_root
}
/// Get the binary path.
pub fn binary_path(&self) -> &std::path::Path {
&self.binary_path
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let updater = SelfUpdater::new(
PathBuf::from("/tmp/project"),
PathBuf::from("/tmp/project/target/release/opencrabs"),
);
assert_eq!(updater.project_root(), std::path::Path::new("/tmp/project"));
assert_eq!(
updater.binary_path(),
std::path::Path::new("/tmp/project/target/release/opencrabs")
);
}
}