Skip to content

Commit 3d949e3

Browse files
author
Star Lord
committed
Merge main into composer Markdown repair
Preserve typed Markdown conversion alongside upstream caret and mention selection fixes. Remove a duplicated Dialog prop inherited from main so the integrated tree typechecks. Signed-off-by: Star Lord <b89298dbe87c6b3fd8a425b535d9d161f23c88d555a47925ae0b04dfe02b201e@buzz.block.builderlab.xyz>
2 parents 5685363 + bc542f2 commit 3d949e3

215 files changed

Lines changed: 15938 additions & 2068 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎crates/agent-controller/src/pi.rs‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ use crate::{
55
};
66
use std::{
77
collections::BTreeMap,
8+
io::{Read, Seek, SeekFrom},
89
path::{Path, PathBuf},
10+
process::{Command, Stdio},
11+
time::{Duration, Instant},
912
};
1013

1114
/// Native only: contains local environment values, never serialized over IPC.
15+
#[derive(PartialEq)]
1216
pub struct PiContext {
1317
pub command: PathBuf,
1418
pub workspace: PathBuf,
@@ -92,6 +96,39 @@ impl PiContext {
9296
})
9397
}
9498

99+
/// A prompt-free probe; callers own its bounded lifetime and cancellation.
100+
pub fn version_command(&self) -> Command {
101+
let mut command = version_command(&self.command, &self.path);
102+
command.current_dir(&self.workspace);
103+
command
104+
}
105+
pub fn version_error(&self, reason: &str) -> String {
106+
format!(
107+
"{reason}. Update the Pi CLI at {} to version 0.99.0 or later, then retry",
108+
self.command.display()
109+
)
110+
}
111+
pub fn accept_version(self, output: &str) -> Result<VerifiedPiContext> {
112+
let version = output.trim();
113+
let parts = version
114+
.split('.')
115+
.map(str::parse::<u64>)
116+
.collect::<std::result::Result<Vec<_>, _>>()
117+
.unwrap_or_default();
118+
if parts.len() != 3 {
119+
return Err(self.version_error("Could not verify the Pi CLI version"));
120+
}
121+
if (parts[0], parts[1], parts[2]) < (0, 99, 0) {
122+
return Err(self.version_error(&format!("Pi CLI {version} is too old")));
123+
}
124+
Ok(VerifiedPiContext(self))
125+
}
126+
fn verify(self) -> Result<VerifiedPiContext> {
127+
let output =
128+
version_output(self.version_command()).map_err(|error| self.version_error(&error))?;
129+
self.accept_version(&output)
130+
}
131+
95132
pub fn catalog_args(&self) -> Result<Vec<String>> {
96133
let mut result = Vec::new();
97134
let mut args = self.args.iter();
@@ -168,6 +205,112 @@ impl PiContext {
168205
}
169206
}
170207

208+
/// Verification stays native and cannot be serialized or supplied over IPC.
209+
pub struct VerifiedPiContext(PiContext);
210+
impl VerifiedPiContext {
211+
pub fn into_context(self) -> PiContext {
212+
self.0
213+
}
214+
}
215+
/// A captured launch check, including the absence of Pi. Launch re-resolves the
216+
/// effective settings and rejects changes made while this check was pending.
217+
pub struct LaunchPreflight(Option<PiContext>);
218+
impl LaunchPreflight {
219+
pub fn new(pi: Option<VerifiedPiContext>) -> Self {
220+
Self(pi.map(|pi| pi.0))
221+
}
222+
pub(crate) fn check(&self, pi: &Option<PiContext>) -> Result<()> {
223+
if &self.0 != pi {
224+
return Err("Pi launch settings changed; retry Start".into());
225+
}
226+
Ok(())
227+
}
228+
}
229+
pub(crate) fn verify_launch(
230+
pi: Option<PiContext>,
231+
preflight: Option<&LaunchPreflight>,
232+
) -> Result<Option<PiContext>> {
233+
if let Some(preflight) = preflight {
234+
preflight.check(&pi)?;
235+
return Ok(pi);
236+
}
237+
pi.map(|pi| pi.verify().map(|pi| pi.0)).transpose()
238+
}
239+
fn version_command(command: &Path, path: &std::ffi::OsStr) -> Command {
240+
let mut probe = Command::new(command);
241+
probe
242+
.arg("--version")
243+
.env_clear()
244+
.env("PATH", path)
245+
.stdin(Stdio::null())
246+
.stdout(Stdio::piped())
247+
.stderr(Stdio::null());
248+
for name in [
249+
"HOME",
250+
"TMPDIR",
251+
"USER",
252+
"LOGNAME",
253+
"LANG",
254+
"SSL_CERT_FILE",
255+
"SSL_CERT_DIR",
256+
] {
257+
if let Some(value) = std::env::var_os(name) {
258+
probe.env(name, value);
259+
}
260+
}
261+
#[cfg(unix)]
262+
{
263+
use std::os::unix::process::CommandExt;
264+
probe.process_group(0);
265+
}
266+
probe
267+
}
268+
269+
// No pipe reader can outlive the probe, even if a wrapper's helper keeps stdout
270+
// open. The tiny file is private and output is never included in an error.
271+
fn version_output(mut probe: Command) -> Result<String> {
272+
const FAILED: &str = "Could not verify the Pi CLI version";
273+
let mut output = tempfile::tempfile().map_err(|_| FAILED)?;
274+
probe.stdout(output.try_clone().map_err(|_| FAILED)?);
275+
let mut child = probe.spawn().map_err(|_| FAILED)?;
276+
let deadline = Instant::now() + Duration::from_secs(5);
277+
let result = loop {
278+
if output.metadata().map_or(true, |meta| meta.len() > 128) {
279+
break Err(FAILED);
280+
}
281+
match child.try_wait() {
282+
Ok(Some(status)) => {
283+
break if status.success() {
284+
Ok(())
285+
} else {
286+
Err(FAILED)
287+
}
288+
}
289+
Err(_) => break Err(FAILED),
290+
Ok(None) if Instant::now() >= deadline => break Err("Pi CLI version check timed out"),
291+
Ok(None) => std::thread::sleep(Duration::from_millis(25)),
292+
}
293+
};
294+
// Retire helpers on success too; --version has no continuing work.
295+
#[cfg(unix)]
296+
unsafe {
297+
libc::kill(-(child.id() as i32), libc::SIGKILL);
298+
}
299+
let _ = child.kill();
300+
child.wait().map_err(|_| FAILED)?;
301+
result?;
302+
output.seek(SeekFrom::Start(0)).map_err(|_| FAILED)?;
303+
let mut version = String::new();
304+
output
305+
.take(129)
306+
.read_to_string(&mut version)
307+
.map_err(|_| FAILED)?;
308+
if version.len() > 128 {
309+
return Err(FAILED.into());
310+
}
311+
Ok(version)
312+
}
313+
171314
/// Selection constraints shared by catalog discovery and ACP launch.
172315
/// Empty selectors retain Pi defaults; model IDs may contain namespace slashes.
173316
pub fn validate_selection(provider: &str, model: &str) -> Result<()> {

‎crates/agent-controller/src/runtime.rs‎

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ impl RuntimeBundle {
1717
agent: &Agent,
1818
key: &crate::Secret,
1919
defaults: &crate::BuildDefaults,
20+
) -> Result<Command> {
21+
self.command_checked(agent, key, defaults, None)
22+
}
23+
fn command_checked(
24+
&self,
25+
agent: &Agent,
26+
key: &crate::Secret,
27+
defaults: &crate::BuildDefaults,
28+
preflight: Option<&crate::pi::LaunchPreflight>,
2029
) -> Result<Command> {
2130
agent.validate()?;
2231
let harness = defaults.resolve(&agent.harness, &agent.environment);
@@ -118,6 +127,7 @@ impl RuntimeBundle {
118127
crate::pi::PiContext::new(&agent.harness, &agent.workspace, &agent.environment)
119128
})
120129
.transpose()?;
130+
let pi = crate::pi::verify_launch(pi, preflight)?;
121131
let (args, environment, tools_path) = if let Some(pi) = &pi {
122132
(
123133
pi.adapter_args(&agent.harness)?,
@@ -497,6 +507,30 @@ impl Controller {
497507
let agent = self.edited_agent(id, revision, edit)?;
498508
crate::pi::PiContext::new(&agent.harness, &agent.workspace, &agent.environment)
499509
}
510+
pub fn pi_launch_context(
511+
&self,
512+
id: &str,
513+
revision: u64,
514+
) -> Result<Option<crate::pi::PiContext>> {
515+
let agent = self
516+
.store
517+
.agents()?
518+
.into_iter()
519+
.find(|agent| agent.id == id)
520+
.ok_or("Agent no longer exists")?;
521+
if agent.revision != revision {
522+
return Err("Saved settings changed; retry Start".into());
523+
}
524+
let agent = crate::agent_defaults::effective(&agent, &self.store.defaults()?);
525+
if Path::new(&agent.harness.command)
526+
.file_name()
527+
.and_then(|name| name.to_str())
528+
!= Some("buzz-pi-acp")
529+
{
530+
return Ok(None);
531+
}
532+
crate::pi::PiContext::new(&agent.harness, &agent.workspace, &agent.environment).map(Some)
533+
}
500534
/// Resolve unsaved Create drafts against the same native defaults as a saved start.
501535
pub fn effective_draft(&self, mut edit: AgentEdit) -> Result<AgentEdit> {
502536
let mut environment = draft_environment(edit.environment);
@@ -759,18 +793,43 @@ impl Controller {
759793
revision: u64,
760794
key: &crate::Secret,
761795
replay_floor: Option<u64>,
796+
) -> Result<()> {
797+
self.action_checked(id, action, revision, key, replay_floor, None)
798+
}
799+
pub fn action_with_preflight(
800+
&mut self,
801+
id: &str,
802+
action: Action,
803+
revision: u64,
804+
key: &crate::Secret,
805+
replay_floor: Option<u64>,
806+
preflight: &crate::pi::LaunchPreflight,
807+
) -> Result<()> {
808+
self.action_checked(id, action, revision, key, replay_floor, Some(preflight))
809+
}
810+
fn action_checked(
811+
&mut self,
812+
id: &str,
813+
action: Action,
814+
revision: u64,
815+
key: &crate::Secret,
816+
replay_floor: Option<u64>,
817+
preflight: Option<&crate::pi::LaunchPreflight>,
762818
) -> Result<()> {
763819
if self.credential_request(id)?.2 != revision {
764820
return Err("Saved settings changed while opening credentials; retry Start".into());
765821
}
822+
if let Some(preflight) = preflight {
823+
preflight.check(&self.pi_launch_context(id, revision)?)?;
824+
}
766825
self.store.enabled(id, true)?;
767826
if matches!(action, Action::Restart) {
768827
if let Err(error) = self.stop(id) {
769828
self.errors.insert(id.into(), error);
770829
return Ok(());
771830
}
772831
}
773-
match self.start_with_key(id, Some(key), replay_floor) {
832+
match self.start_with_key(id, Some(key), replay_floor, preflight) {
774833
Ok(()) => {
775834
self.errors.remove(id);
776835
}
@@ -793,13 +852,14 @@ impl Controller {
793852
.collect())
794853
}
795854
fn start(&mut self, id: &str) -> Result<()> {
796-
self.start_with_key(id, None, None)
855+
self.start_with_key(id, None, None, None)
797856
}
798857
fn start_with_key(
799858
&mut self,
800859
id: &str,
801860
supplied: Option<&crate::Secret>,
802861
replay_floor: Option<u64>,
862+
preflight: Option<&crate::pi::LaunchPreflight>,
803863
) -> Result<()> {
804864
if let Some(run) = self.running.get_mut(id) {
805865
if run.process.alive()? {
@@ -842,7 +902,11 @@ impl Controller {
842902
.prefix("agent-")
843903
.tempdir_in(&runs)
844904
.map_err(|_| "Could not create private runtime directory")?;
845-
let mut command = bundle.command(&agent, key)?;
905+
let mut command = if let Some(preflight) = preflight {
906+
bundle.command_checked(&agent, key, &crate::build_defaults(), Some(preflight))?
907+
} else {
908+
bundle.command(&agent, key)?
909+
};
846910
// Per-send startup input, never saved configuration or inherited environment.
847911
if let Some(floor) = replay_floor {
848912
command.env("BUZZ_ACP_REPLAY_FLOOR", floor.to_string());

0 commit comments

Comments
 (0)