Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

fix(opencode): give the version probe room for a cold start

Adityaa04f3d3794fe
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 82e19d3e..0a81bd8e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,14 @@ version in [`docs/changelog/`](docs/changelog/):
### Fixed
+- The OpenCode version probe now waits up to 10 seconds instead of 2.
+ `setup`, `install-hooks`, `install-mcp` and `ai-memory run` run
+ `opencode --version` to choose which major to wire, and a cold first start
+ of the executable (macOS scans a fresh binary, and a loaded machine slows
+ every launch) could pass the old limit, so the command failed with "version
+ probe timed out" although `opencode` was fine. A genuinely hung `opencode` is
+ still reported, after 10 seconds (#1718).
+
- Codex's own memory-writing agent no longer appears as captured sessions.
Codex runs a background "memory writing agent" from `$CODEX_HOME/memories`
(the default `~/.codex/memories`) to consolidate its memory; the hooks
diff --git a/crates/ai-memory-cli/src/commands/opencode_dialect.rs b/crates/ai-memory-cli/src/commands/opencode_dialect.rs
index 12aad931..4e59f635 100644
--- a/crates/ai-memory-cli/src/commands/opencode_dialect.rs
+++ b/crates/ai-memory-cli/src/commands/opencode_dialect.rs
@@ -13,7 +13,11 @@ use anyhow::{Context as _, Result, anyhow, bail};
use crate::cli::{AgentChoice, McpClient};
use crate::commands::run::EffectiveChildEnv;
-const PROBE_TIMEOUT: Duration = Duration::from_secs(2);
+/// Budget for `opencode --version`. A cold first run of an executable can take
+/// seconds (macOS scans a fresh binary, and a busy machine slows every exec), so
+/// a tight limit turns a slow start into a hard failure. Every caller is a
+/// one-shot command, so only a hung `opencode` pays the full wait.
+const PROBE_TIMEOUT: Duration = Duration::from_secs(10);
const MAX_VERSION_OUTPUT: u64 = 64 * 1024;
#[derive(Debug)]
@@ -45,6 +49,14 @@ pub(crate) fn resolve_client(client: McpClient, env: &EffectiveChildEnv) -> Resu
pub(crate) fn resolve_generic(
executable: Option<&OsStr>,
env: &EffectiveChildEnv,
+) -> Result<ResolvedOpenCode> {
+ resolve_generic_with_timeout(executable, env, PROBE_TIMEOUT)
+}
+
+fn resolve_generic_with_timeout(
+ executable: Option<&OsStr>,
+ env: &EffectiveChildEnv,
+ timeout: Duration,
) -> Result<ResolvedOpenCode> {
let program = executable.unwrap_or_else(|| OsStr::new("opencode"));
let resolved = super::run::resolve_program(program, env.executable_search()).ok_or_else(|| {
@@ -72,21 +84,21 @@ pub(crate) fn resolve_generic(
{
break status;
}
- if started.elapsed() >= PROBE_TIMEOUT {
+ if started.elapsed() >= timeout {
let _ = child.kill();
let _ = child.wait();
- return Err(timeout_error(&resolved));
+ return Err(timeout_error(&resolved, timeout));
}
thread::sleep(Duration::from_millis(10));
};
- let remaining = PROBE_TIMEOUT.saturating_sub(started.elapsed());
+ let remaining = timeout.saturating_sub(started.elapsed());
let stdout = stdout
.recv_timeout(remaining)
- .map_err(|_| timeout_error(&resolved))??;
- let remaining = PROBE_TIMEOUT.saturating_sub(started.elapsed());
+ .map_err(|_| timeout_error(&resolved, timeout))??;
+ let remaining = timeout.saturating_sub(started.elapsed());
let stderr = stderr
.recv_timeout(remaining)
- .map_err(|_| timeout_error(&resolved))??;
+ .map_err(|_| timeout_error(&resolved, timeout))??;
if stdout.len() > MAX_VERSION_OUTPUT as usize || stderr.len() > MAX_VERSION_OUTPUT as usize {
bail!("OpenCode version probe returned more than {MAX_VERSION_OUTPUT} bytes");
}
@@ -119,10 +131,10 @@ pub(crate) fn resolve_generic(
})
}
-fn timeout_error(program: &std::path::Path) -> anyhow::Error {
+fn timeout_error(program: &std::path::Path, timeout: Duration) -> anyhow::Error {
anyhow!(
"OpenCode version probe timed out after {} seconds for {}; run `{} --version` and verify it exits promptly",
- PROBE_TIMEOUT.as_secs(),
+ timeout.as_secs_f64(),
program.display(),
program.display()
)
@@ -259,15 +271,38 @@ mod tests {
permissions.set_mode(0o755);
std::fs::set_permissions(&executable, permissions).unwrap();
- let error = resolve_generic(
+ let error = resolve_generic_with_timeout(
Some(executable.as_os_str()),
&EffectiveChildEnv::from_runtime(&crate::config::RuntimeEnv::default()),
+ Duration::from_millis(300),
)
.unwrap_err()
.to_string();
assert!(error.contains("version probe timed out"), "{error}");
}
+ #[cfg(unix)]
+ #[test]
+ fn version_probe_tolerates_a_start_slower_than_two_seconds() {
+ use std::os::unix::fs::PermissionsExt as _;
+
+ let temp = tempfile::tempdir().unwrap();
+ let executable = temp.path().join("opencode");
+ std::fs::write(
+ &executable,
+ "#!/bin/sh\nsleep 3\nprintf '%s\\n' '1.18.34'\n",
+ )
+ .unwrap();
+ std::fs::set_permissions(&executable, std::fs::Permissions::from_mode(0o755)).unwrap();
+
+ let resolved = resolve_generic(
+ Some(executable.as_os_str()),
+ &EffectiveChildEnv::from_runtime(&crate::config::RuntimeEnv::default()),
+ )
+ .unwrap();
+ assert_eq!(resolved.dialect, OpenCodeDialect::V1);
+ }
+
#[cfg(windows)]
#[test]
fn windows_explicit_extensionless_path_resolves_the_cmd_launcher() {