Skip to content

Commit 3d273ec

Browse files
committed
refactor(desktop): satisfy source size gates
Signed-off-by: Reinhold <[email protected]>
1 parent bbd545a commit 3d273ec

9 files changed

Lines changed: 317 additions & 310 deletions

File tree

‎desktop/src-tauri/src/commands/agent_discovery.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ pub async fn save_custom_harness(
138138
// so concurrent saves never produce a stale registry snapshot (B-6).
139139
custom_harnesses::save_and_warm(&custom_dir, &definition, rename_old_id.as_deref())?;
140140

141-
// Resolve availability for the returned catalog entry.
142141
let (availability, command_opt, binary_path) =
143142
match crate::managed_agents::find_command(&definition.command) {
144143
Some(path) => (
@@ -148,7 +147,6 @@ pub async fn save_custom_harness(
148147
),
149148
None => (AcpAvailabilityStatus::NotInstalled, None, None),
150149
};
151-
152150
let default_args =
153151
crate::managed_agents::normalize_agent_args(&definition.command, definition.args.clone());
154152

‎desktop/src-tauri/src/managed_agents/discovery.rs‎

Lines changed: 7 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,192 +1152,16 @@ pub(crate) fn classify_runtime(
11521152
}
11531153
}
11541154

1155-
/// The oldest `codex-acp` version supported by Buzz managed agents.
1156-
///
1157-
/// Older 1.x adapters are detected successfully, but can still bundle a Codex runtime
1158-
/// that does not reliably give `buzz` CLI subprocesses outbound relay access.
1159-
///
1160-
/// Bump policy: raise this only when a newer adapter fixes a defect that breaks managed
1161-
/// agents, and only to a version already published on npm — every user below the floor is
1162-
/// offered a reinstall on their next discovery pass.
1163-
pub(crate) const MIN_CODEX_ACP_VERSION: (u64, u64, u64) = (1, 1, 7);
1164-
1165-
/// Probe the full version of a `codex-acp` binary by running `--version`.
1166-
///
1167-
/// The 1.x adapter (`@agentclientprotocol/codex-acp`) outputs
1168-
/// `@agentclientprotocol/codex-acp <major>.<minor>.<patch>` on stdout and exits 0.
1169-
/// The old 0.16.x adapter (`@zed-industries/codex-acp`) is a Rust binary that does
1170-
/// not recognise `--version` and exits non-zero.
1171-
///
1172-
/// Returns the `(major, minor, patch)` triple on success, `None` on any failure
1173-
/// (non-zero exit, unparseable output, timeout, or missing binary).
1174-
///
1175-
/// The parse is deliberately strict: exactly three numeric dot-separated components.
1176-
/// Partial versions (`1.2`) and prerelease tags (`1.2.0-rc1`) return `None` and so
1177-
/// classify as [`AcpAvailabilityStatus::AdapterOutdated`] — failing closed offers a
1178-
/// reinstall rather than running an adapter whose version cannot be compared.
1179-
///
1180-
/// The probe is bounded by a 5-second deadline. The child is polled with
1181-
/// [`std::process::Child::try_wait`] (the repo's standard deadline pattern) and
1182-
/// killed if it does not exit in time.
1183-
///
1184-
/// Stdout is redirected to a temporary file rather than a pipe, so forked
1185-
/// descendants cannot hold EOF open. Reads from a regular file return EOF at its
1186-
/// current write position regardless of inherited file descriptors, cross-platform.
1187-
pub(crate) fn probe_codex_acp_version(binary_path: &Path) -> Option<(u64, u64, u64)> {
1188-
probe_codex_acp_version_with_path(
1189-
binary_path,
1190-
crate::managed_agents::readiness::cli_probe::augmented_path().as_deref(),
1191-
)
1192-
}
1193-
pub(crate) fn probe_codex_acp_version_with_path(
1194-
binary_path: &Path,
1195-
augmented_path: Option<&str>,
1196-
) -> Option<(u64, u64, u64)> {
1197-
probe_codex_acp_version_inner(binary_path, augmented_path, None)
1198-
}
1199-
1200-
fn probe_codex_acp_version_with_plan(
1201-
binary_path: &Path,
1202-
plan: &super::runtime_plan::RuntimeExecutionPlan,
1203-
) -> Option<(u64, u64, u64)> {
1204-
probe_codex_acp_version_inner(
1205-
binary_path,
1206-
plan.generated_environment("PATH"),
1207-
Some(plan),
1208-
)
1209-
}
1210-
1211-
fn probe_codex_acp_version_inner(
1212-
binary_path: &Path,
1213-
augmented_path: Option<&str>,
1214-
runtime_plan: Option<&super::runtime_plan::RuntimeExecutionPlan>,
1215-
) -> Option<(u64, u64, u64)> {
1216-
use std::io::{Read as _, Seek as _, SeekFrom};
1217-
use std::time::{Duration, Instant};
1218-
const VERSION_PROBE_TIMEOUT: Duration = Duration::from_secs(5);
1219-
1220-
// A regular file returns EOF at its current size even when a descendant
1221-
// inherits its descriptor, bounding the post-exit read cross-platform.
1222-
let mut tmp = tempfile::tempfile().ok()?;
1223-
1224-
let mut command = Command::new(binary_path);
1225-
command.arg("--version");
1226-
if let Some(path) = augmented_path {
1227-
command.env("PATH", path);
1228-
}
1229-
if let Some(plan) = runtime_plan {
1230-
plan.apply_environment(&mut command);
1231-
}
1232-
crate::util::configure_no_window(&mut command);
1233-
let mut child = command
1234-
.stdout(tmp.try_clone().ok()?)
1235-
.stderr(std::process::Stdio::null())
1236-
.spawn()
1237-
.ok()?;
1238-
1239-
// Poll until the deadline rather than blocking on stdout EOF.
1240-
let deadline = Instant::now() + VERSION_PROBE_TIMEOUT;
1241-
let exit_status = loop {
1242-
match child.try_wait() {
1243-
Ok(Some(status)) => break status,
1244-
Ok(None) => {
1245-
if Instant::now() >= deadline {
1246-
let _ = child.kill();
1247-
let _ = child.wait();
1248-
return None;
1249-
}
1250-
std::thread::sleep(Duration::from_millis(50));
1251-
}
1252-
Err(_) => {
1253-
let _ = child.kill();
1254-
let _ = child.wait();
1255-
return None;
1256-
}
1257-
}
1258-
};
1259-
1260-
if !exit_status.success() {
1261-
return None;
1262-
}
1263-
1264-
// Read at most 4 KiB from the regular file without blocking.
1265-
tmp.seek(SeekFrom::Start(0)).ok()?;
1266-
let mut buf = Vec::with_capacity(128);
1267-
let _ = (&mut tmp as &mut dyn std::io::Read)
1268-
.take(4096)
1269-
.read_to_end(&mut buf);
1270-
1271-
let stdout = String::from_utf8_lossy(&buf);
1272-
// Output format: "<package-name> <major>.<minor>.<patch>"
1273-
let version_str = stdout.split_whitespace().last()?;
1274-
let mut components = version_str.split('.');
1275-
let major = components.next()?.parse::<u64>().ok()?;
1276-
let minor = components.next()?.parse::<u64>().ok()?;
1277-
let patch = components.next()?.parse::<u64>().ok()?;
1278-
if components.next().is_some() {
1279-
return None;
1280-
}
1281-
Some((major, minor, patch))
1282-
}
1283-
1284-
/// Classifies a resolved codex-acp binary path as [`AcpAvailabilityStatus::Available`]
1285-
/// or [`AcpAvailabilityStatus::AdapterOutdated`].
1286-
///
1287-
/// The 0.16.x adapter (`@zed-industries/codex-acp`) does not recognise `--version`
1288-
/// and exits non-zero — that probe failure yields `AdapterOutdated`. An adapter is
1289-
/// available only when its version is at least [`MIN_CODEX_ACP_VERSION`].
1290-
///
1291-
/// Used by `discover_acp_runtimes`, `cli_login_requirements`, and
1292-
/// `install_acp_runtime_blocking` so the version-gate logic is not duplicated.
1293-
pub(crate) fn codex_adapter_availability(path: &Path) -> AcpAvailabilityStatus {
1294-
match probe_codex_acp_version(path) {
1295-
Some(version) if version >= MIN_CODEX_ACP_VERSION => AcpAvailabilityStatus::Available,
1296-
_ => AcpAvailabilityStatus::AdapterOutdated,
1297-
}
1298-
}
1299-
1300-
pub(crate) fn codex_adapter_availability_with_plan(
1301-
plan: &super::runtime_plan::RuntimeExecutionPlan,
1302-
) -> AcpAvailabilityStatus {
1303-
let version = plan
1304-
.harness_path()
1305-
.ok()
1306-
.and_then(|path| {
1307-
plan.verify()
1308-
.ok()
1309-
.and_then(|()| probe_codex_acp_version_with_plan(path, plan))
1310-
});
1311-
match version {
1312-
Some(version) if version >= MIN_CODEX_ACP_VERSION => AcpAvailabilityStatus::Available,
1313-
_ => AcpAvailabilityStatus::AdapterOutdated,
1314-
}
1315-
}
1155+
mod codex_version;
13161156

1317-
/// Returns `true` when the codex-acp binary at `path` is below
1318-
/// [`MIN_CODEX_ACP_VERSION`] or cannot be probed using `augmented_path`. Thin wrapper
1319-
/// around [`codex_adapter_is_outdated_with_path`].
1157+
pub(crate) use codex_version::{
1158+
codex_adapter_availability, codex_adapter_availability_with_plan,
1159+
codex_adapter_is_outdated_with_path, probe_codex_acp_version,
1160+
probe_codex_acp_version_with_path, MIN_CODEX_ACP_VERSION,
1161+
};
13201162
#[cfg(test)]
1321-
pub(crate) fn codex_adapter_is_outdated(path: &Path) -> bool {
1322-
codex_adapter_is_outdated_with_path(
1323-
path,
1324-
crate::managed_agents::readiness::cli_probe::augmented_path().as_deref(),
1325-
)
1326-
}
1327-
1328-
/// Returns `true` when the codex-acp binary at `path` is below
1329-
/// [`MIN_CODEX_ACP_VERSION`] or cannot be probed with the supplied PATH.
1330-
pub(crate) fn codex_adapter_is_outdated_with_path(
1331-
path: &Path,
1332-
augmented_path: Option<&str>,
1333-
) -> bool {
1334-
!matches!(
1335-
probe_codex_acp_version_with_path(path, augmented_path),
1336-
Some(version) if version >= MIN_CODEX_ACP_VERSION
1337-
)
1338-
}
1163+
pub(crate) use codex_version::codex_adapter_is_outdated;
13391164

1340-
/// Intermediate struct built before the (potentially slow) auth probe phase.
13411165
struct PartialEntry {
13421166
runtime: &'static KnownAcpRuntime,
13431167
entry: AcpRuntimeCatalogEntry,
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
use std::{path::Path, process::Command};
2+
3+
use super::AcpAvailabilityStatus;
4+
5+
/// The oldest `codex-acp` version supported by Buzz managed agents.
6+
///
7+
/// Raise only for a published adapter that fixes a managed-agent defect.
8+
pub(crate) const MIN_CODEX_ACP_VERSION: (u64, u64, u64) = (1, 1, 7);
9+
10+
/// Probe the full version of a `codex-acp` binary by running `--version`.
11+
///
12+
/// The 1.x adapter (`@agentclientprotocol/codex-acp`) outputs
13+
/// `@agentclientprotocol/codex-acp <major>.<minor>.<patch>` on stdout and exits 0.
14+
/// The old 0.16.x adapter (`@zed-industries/codex-acp`) is a Rust binary that does
15+
/// not recognise `--version` and exits non-zero.
16+
///
17+
/// Returns the `(major, minor, patch)` triple on success, `None` on any failure
18+
/// (non-zero exit, unparseable output, timeout, or missing binary).
19+
///
20+
/// The parse is deliberately strict: exactly three numeric dot-separated components.
21+
/// Partial versions (`1.2`) and prerelease tags (`1.2.0-rc1`) return `None` and so
22+
/// classify as [`AcpAvailabilityStatus::AdapterOutdated`] — failing closed offers a
23+
/// reinstall rather than running an adapter whose version cannot be compared.
24+
///
25+
/// The probe is bounded by a 5-second deadline. The child is polled with
26+
/// [`std::process::Child::try_wait`] (the repo's standard deadline pattern) and
27+
/// killed if it does not exit in time.
28+
///
29+
/// Stdout is redirected to a temporary file rather than a pipe, so forked
30+
/// descendants cannot hold EOF open. Reads from a regular file return EOF at its
31+
/// current write position regardless of inherited file descriptors, cross-platform.
32+
pub(crate) fn probe_codex_acp_version(binary_path: &Path) -> Option<(u64, u64, u64)> {
33+
probe_codex_acp_version_with_path(
34+
binary_path,
35+
crate::managed_agents::readiness::cli_probe::augmented_path().as_deref(),
36+
)
37+
}
38+
pub(crate) fn probe_codex_acp_version_with_path(
39+
binary_path: &Path,
40+
augmented_path: Option<&str>,
41+
) -> Option<(u64, u64, u64)> {
42+
probe_codex_acp_version_inner(binary_path, augmented_path, None)
43+
}
44+
45+
fn probe_codex_acp_version_with_plan(
46+
binary_path: &Path,
47+
plan: &crate::managed_agents::runtime_plan::RuntimeExecutionPlan,
48+
) -> Option<(u64, u64, u64)> {
49+
probe_codex_acp_version_inner(
50+
binary_path,
51+
plan.generated_environment("PATH"),
52+
Some(plan),
53+
)
54+
}
55+
56+
fn probe_codex_acp_version_inner(
57+
binary_path: &Path,
58+
augmented_path: Option<&str>,
59+
runtime_plan: Option<&crate::managed_agents::runtime_plan::RuntimeExecutionPlan>,
60+
) -> Option<(u64, u64, u64)> {
61+
use std::io::{Read as _, Seek as _, SeekFrom};
62+
use std::time::{Duration, Instant};
63+
const VERSION_PROBE_TIMEOUT: Duration = Duration::from_secs(5);
64+
65+
// A regular file returns EOF at its current size even when a descendant
66+
// inherits its descriptor, bounding the post-exit read cross-platform.
67+
let mut tmp = tempfile::tempfile().ok()?;
68+
69+
let mut command = Command::new(binary_path);
70+
command.arg("--version");
71+
if let Some(path) = augmented_path {
72+
command.env("PATH", path);
73+
}
74+
if let Some(plan) = runtime_plan {
75+
plan.apply_environment(&mut command);
76+
}
77+
crate::util::configure_no_window(&mut command);
78+
let mut child = command
79+
.stdout(tmp.try_clone().ok()?)
80+
.stderr(std::process::Stdio::null())
81+
.spawn()
82+
.ok()?;
83+
84+
// Poll until the deadline rather than blocking on stdout EOF.
85+
let deadline = Instant::now() + VERSION_PROBE_TIMEOUT;
86+
let exit_status = loop {
87+
match child.try_wait() {
88+
Ok(Some(status)) => break status,
89+
Ok(None) => {
90+
if Instant::now() >= deadline {
91+
let _ = child.kill();
92+
let _ = child.wait();
93+
return None;
94+
}
95+
std::thread::sleep(Duration::from_millis(50));
96+
}
97+
Err(_) => {
98+
let _ = child.kill();
99+
let _ = child.wait();
100+
return None;
101+
}
102+
}
103+
};
104+
105+
if !exit_status.success() {
106+
return None;
107+
}
108+
109+
// Read at most 4 KiB from the regular file without blocking.
110+
tmp.seek(SeekFrom::Start(0)).ok()?;
111+
let mut buf = Vec::with_capacity(128);
112+
let _ = (&mut tmp as &mut dyn std::io::Read)
113+
.take(4096)
114+
.read_to_end(&mut buf);
115+
116+
let stdout = String::from_utf8_lossy(&buf);
117+
// Output format: "<package-name> <major>.<minor>.<patch>"
118+
let version_str = stdout.split_whitespace().last()?;
119+
let mut components = version_str.split('.');
120+
let major = components.next()?.parse::<u64>().ok()?;
121+
let minor = components.next()?.parse::<u64>().ok()?;
122+
let patch = components.next()?.parse::<u64>().ok()?;
123+
if components.next().is_some() {
124+
return None;
125+
}
126+
Some((major, minor, patch))
127+
}
128+
129+
/// Classifies a resolved codex-acp binary path as [`AcpAvailabilityStatus::Available`]
130+
/// or [`AcpAvailabilityStatus::AdapterOutdated`].
131+
///
132+
/// The 0.16.x adapter (`@zed-industries/codex-acp`) does not recognise `--version`
133+
/// and exits non-zero — that probe failure yields `AdapterOutdated`. An adapter is
134+
/// available only when its version is at least [`MIN_CODEX_ACP_VERSION`].
135+
///
136+
/// Used by `discover_acp_runtimes`, `cli_login_requirements`, and
137+
/// `install_acp_runtime_blocking` so the version-gate logic is not duplicated.
138+
pub(crate) fn codex_adapter_availability(path: &Path) -> AcpAvailabilityStatus {
139+
match probe_codex_acp_version(path) {
140+
Some(version) if version >= MIN_CODEX_ACP_VERSION => AcpAvailabilityStatus::Available,
141+
_ => AcpAvailabilityStatus::AdapterOutdated,
142+
}
143+
}
144+
145+
pub(crate) fn codex_adapter_availability_with_plan(
146+
plan: &crate::managed_agents::runtime_plan::RuntimeExecutionPlan,
147+
) -> AcpAvailabilityStatus {
148+
let version = plan
149+
.harness_path()
150+
.ok()
151+
.and_then(|path| {
152+
plan.verify()
153+
.ok()
154+
.and_then(|()| probe_codex_acp_version_with_plan(path, plan))
155+
});
156+
match version {
157+
Some(version) if version >= MIN_CODEX_ACP_VERSION => AcpAvailabilityStatus::Available,
158+
_ => AcpAvailabilityStatus::AdapterOutdated,
159+
}
160+
}
161+
162+
/// Returns `true` when the codex-acp binary at `path` is below
163+
/// [`MIN_CODEX_ACP_VERSION`] or cannot be probed using `augmented_path`. Thin wrapper
164+
/// around [`codex_adapter_is_outdated_with_path`].
165+
#[cfg(test)]
166+
pub(crate) fn codex_adapter_is_outdated(path: &Path) -> bool {
167+
codex_adapter_is_outdated_with_path(
168+
path,
169+
crate::managed_agents::readiness::cli_probe::augmented_path().as_deref(),
170+
)
171+
}
172+
173+
/// Returns `true` when the codex-acp binary at `path` is below
174+
/// [`MIN_CODEX_ACP_VERSION`] or cannot be probed with the supplied PATH.
175+
pub(crate) fn codex_adapter_is_outdated_with_path(
176+
path: &Path,
177+
augmented_path: Option<&str>,
178+
) -> bool {
179+
!matches!(
180+
probe_codex_acp_version_with_path(path, augmented_path),
181+
Some(version) if version >= MIN_CODEX_ACP_VERSION
182+
)
183+
}

0 commit comments

Comments
 (0)