use std::io::Write;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
pub const RESEARCH_FOLD_BUDGET: usize = 180_000;
#[derive(Debug, Default, Clone, Copy)]
pub struct Progress {
pub turns: u32,
pub gate_attempts: u32,
pub in_gate: bool,
pub gate_wait_ms: u64,
pub usage: blyte_provider::Usage,
}
#[derive(Debug, Clone)]
pub struct TaskConfig {
pub prompt: String,
pub gate: Option<String>,
pub context_strategy: String,
pub model: Option<String>,
pub container: Option<String>,
pub max_turns: usize,
pub max_gates: usize,
pub rollback: bool,
pub root: PathBuf,
pub container_workdir: String,
pub max_seconds: Option<u64>,
pub max_cost_usd: Option<f64>,
pub report: Option<PathBuf>,
pub fold_budget: Option<usize>,
pub sandbox: String,
pub network: String,
pub allow_read: Vec<PathBuf>,
pub pass_env: Vec<String>,
pub research: bool,
pub claim_check: bool,
pub base_check: bool,
pub orchestrate: bool,
pub dispatch_cmd: Option<String>,
}
impl From<&crate::cli::Cli> for TaskConfig {
fn from(cli: &crate::cli::Cli) -> Self {
blyte_agent::tools::set_read_roots(cli.read_roots.clone());
let root = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
Self {
prompt: cli.prompt.clone().unwrap_or_default(),
gate: cli.gate.clone(),
context_strategy: cli.context.clone(),
model: cli.model.clone(),
container: cli.container.clone(),
max_turns: cli.max_turns,
max_gates: cli.max_gates,
rollback: cli.rollback,
root,
container_workdir: cli.container_workdir.clone(),
max_seconds: cli.max_seconds,
max_cost_usd: cli.max_cost_usd,
report: cli.report.clone(),
fold_budget: cli.fold_budget,
sandbox: if cli.orchestrate {
"off".into()
} else {
cli.sandbox.clone()
},
network: cli.network.clone(),
allow_read: cli.allow_read.clone(),
pass_env: cli.pass_env.clone(),
research: cli.research,
claim_check: cli.claim_check,
base_check: cli.base_check,
orchestrate: cli.orchestrate,
dispatch_cmd: cli.dispatch_cmd.clone(),
}
}
}
pub fn check_network_mode(config: &TaskConfig) -> Result<(), String> {
if config.network == "off" {
if config.container.is_some() {
return Err(
"--network=off is not supported with --container; set the container's network instead"
.into(),
);
}
if config.sandbox != "on" {
return Err("--network=off needs the sandbox".into());
}
#[cfg(target_os = "linux")]
if blyte_agent::sandbox::landlock_abi() < 4 {
return Err("--network=off on Linux needs Linux 6.7 or later".into());
}
if !blyte_agent::sandbox::available() {
return Err("--network=off needs the sandbox, but the sandbox is unavailable".into());
}
}
Ok(())
}
pub fn run_task(
config: TaskConfig,
) -> Result<(blyte_agent::run::Outcome, blyte_agent::run::RunReport), String> {
let started = std::time::Instant::now();
let progress = Arc::new(Mutex::new(Progress::default()));
if let Some(s) = config.max_seconds {
let grace_secs: u64 = std::env::var("BLYTE_WATCHDOG_GRACE_SECS")
.ok()
.and_then(|v| v.trim().parse().ok())
.unwrap_or(30);
let watchdog_config = config.clone();
let watchdog_progress = Arc::clone(&progress);
std::thread::spawn(move || {
loop {
let wait_ms = match watchdog_progress.lock() {
Ok(g) => g.gate_wait_ms,
Err(e) => e.into_inner().gate_wait_ms,
};
let deadline = started
+ std::time::Duration::from_secs(s.saturating_add(grace_secs))
+ std::time::Duration::from_millis(wait_ms);
match deadline.checked_duration_since(std::time::Instant::now()) {
Some(remaining) if !remaining.is_zero() => {
std::thread::sleep(remaining.min(std::time::Duration::from_millis(500)));
}
_ => break,
}
}
let p = match watchdog_progress.lock() {
Ok(g) => *g,
Err(e) => *e.into_inner(),
};
let msg = if p.in_gate {
format!(
"stopped {grace_secs} s past --max-seconds {s}: gate attempt {} was still running",
p.gate_attempts
)
} else {
format!(
"stopped {grace_secs} s past --max-seconds {s} during turn {}: a model call or command never returned",
p.turns
)
};
if let Some(path) = &watchdog_config.report {
let mut record =
crate::report::RunRecord::from_error(&watchdog_config, &msg, started.elapsed());
record.turns = p.turns;
record.gate_attempts = p.gate_attempts;
record.usage = crate::report::UsageRecord {
input: p.usage.input,
cached: p.usage.cached,
output: p.usage.output,
thinking: p.usage.thinking,
};
let model_id = blyte_provider::resolve_provider(watchdog_config.model.as_deref())
.map(|pr| pr.model_id())
.ok()
.or_else(|| watchdog_config.model.clone())
.unwrap_or_default();
record.cost_usd = blyte_provider::pricing(&model_id).map(|pr| pr.cost(&p.usage));
let _ = crate::report::write(path, &record);
}
eprintln!("blyte: {msg}");
std::process::exit(1);
});
}
let verification_cmd = config.gate.as_deref().unwrap_or("");
let mut state = blyte_agent::state::State::new(&config.prompt, verification_cmd);
state.orchestrator.max_cost_usd = config.max_cost_usd;
if config.orchestrate {
let rules_path = blyte_agent::orchestrator::user_rules_path();
let loaded_rules = rules_path
.as_deref()
.and_then(|p| blyte_agent::orchestrator::load_user_rules(p).map(|text| (p, text)));
let rules_ref = loaded_rules.as_ref().map(|(p, t)| (*p, t.as_str()));
state.orchestrator.instructions =
Some(blyte_agent::orchestrator::prompt_sections_with_dispatch(
crate::skill::SKILL_MD,
rules_ref,
config.dispatch_cmd.is_some(),
));
}
let mut stdout = std::io::stdout();
let result =
run_task_with_state_writer_and_progress(&config, &mut state, &mut stdout, progress);
if let (Err(e), Some(path)) = (&result, &config.report) {
crate::report::write(
path,
&crate::report::RunRecord::from_error(&config, e, started.elapsed()),
)?;
}
result
}
pub fn run_task_with_state_and_writer<W: Write>(
config: &TaskConfig,
state: &mut blyte_agent::state::State,
writer: &mut W,
) -> Result<(blyte_agent::run::Outcome, blyte_agent::run::RunReport), String> {
let progress = Arc::new(Mutex::new(Progress::default()));
run_task_with_state_writer_and_progress(config, state, writer, progress)
}
pub fn run_task_with_state_writer_and_progress<W: Write>(
config: &TaskConfig,
state: &mut blyte_agent::state::State,
writer: &mut W,
progress: Arc<Mutex<Progress>>,
) -> Result<(blyte_agent::run::Outcome, blyte_agent::run::RunReport), String> {
check_network_mode(config)?;
let started = std::time::Instant::now();
if config.orchestrate {
if std::env::var("BLYTE_SESSION")
.map(|v| v.is_empty())
.unwrap_or(true)
{
let now_secs = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
let id = crate::repl::new_session_id(now_secs, std::process::id());
std::env::set_var("BLYTE_SESSION", &id);
}
if state.orchestrator.instructions.is_none() {
let rules_path = blyte_agent::orchestrator::user_rules_path();
let loaded_rules = rules_path
.as_deref()
.and_then(|p| blyte_agent::orchestrator::load_user_rules(p).map(|text| (p, text)));
let rules_ref = loaded_rules.as_ref().map(|(p, t)| (*p, t.as_str()));
state.orchestrator.instructions =
Some(blyte_agent::orchestrator::prompt_sections_with_dispatch(
crate::skill::SKILL_MD,
rules_ref,
config.dispatch_cmd.is_some(),
));
}
}
blyte_agent::tools::set_dispatch_cmd(config.dispatch_cmd.clone());
let provider = match blyte_provider::resolve_provider(config.model.as_deref()) {
Ok(p) => p,
Err(blyte_provider::ProviderError::MissingKey(key)) => {
return Err(format!(
"Provider error: no API key: set {key} in environment"
));
}
Err(e) => return Err(format!("Provider error: {e}")),
};
let model_id = provider.model_id();
if config.max_cost_usd.is_some() && blyte_provider::pricing(&model_id).is_none() {
return Err(format!(
"--max-cost needs a known price for {model_id}; none is known"
));
}
state.orchestrator.max_cost_usd = config.max_cost_usd;
let folded = blyte_agent::context::Folded {
budget_tokens: config.fold_budget.unwrap_or(if config.research {
RESEARCH_FOLD_BUDGET
} else {
blyte_agent::context::Folded::default().budget_tokens
}),
};
let conversational = blyte_agent::context::Conversational;
let context_strategy: &dyn blyte_agent::context::ContextStrategy =
match config.context_strategy.as_str() {
"conversational" => &conversational,
_ => &folded,
};
let local_fs = blyte_agent::fs::LocalFs;
let (persistent, session_fs, root) = match &config.container {
Some(cid) => (
blyte_agent::shell::PersistentShell::container(
cid.clone(),
PathBuf::from(&config.container_workdir),
),
Some(blyte_agent::fs::SessionFs::container(
cid.clone(),
PathBuf::from(&config.container_workdir),
)),
PathBuf::from(&config.container_workdir),
),
None => {
blyte_agent::shell::set_pass_env(config.pass_env.clone());
if let Some(r) = blyte_agent::rules::load(&config.root) {
eprintln!("blyte: repository rules from {}", r.source.display());
}
let withheld = blyte_agent::shell::withheld_env();
if !withheld.is_empty() {
eprintln!(
"blyte: withholding from commands: {} (keep one with --pass-env NAME)",
withheld.join(", ")
);
}
let shell = if config.sandbox == "on" {
if blyte_agent::sandbox::available() {
let profile = blyte_agent::sandbox::default_profile_net(
&config.root,
&config.allow_read,
config.network == "on",
);
if matches!(profile, blyte_agent::sandbox::Profile::Landlock(_)) {
eprintln!(
"blyte: on Linux the sandbox restricts writes only; credential reads are not blocked"
);
}
if matches!(profile, blyte_agent::sandbox::Profile::LandlockOffline(_)) {
eprintln!(
"blyte: on Linux --network=off blocks TCP connections only (localhost included); UDP and the Docker socket are not blocked"
);
}
if let blyte_agent::sandbox::Profile::LandlockConfined { block_tcp, .. } =
&profile
{
eprintln!(
"blyte: on Linux, commands can read only system directories, the workspace, tool caches and --allow-read paths"
);
if *block_tcp {
eprintln!(
"blyte: on Linux --network=off blocks TCP connections only (localhost included); UDP and the Docker socket are not blocked"
);
}
}
blyte_agent::shell::PersistentShell::local_sandboxed(
config.root.clone(),
profile,
)
} else {
eprintln!("warning: sandbox unavailable; running unsandboxed");
blyte_agent::shell::PersistentShell::local(config.root.clone())
}
} else {
blyte_agent::shell::PersistentShell::local(config.root.clone())
};
(shell, None, config.root.clone())
}
};
let shell: &dyn blyte_agent::shell::Shell = &persistent;
let fs: &dyn blyte_agent::fs::Fs = match &session_fs {
Some(f) => f,
None => &local_fs,
};
let writer_mutex = Mutex::new(writer);
let on_event = |event: blyte_agent::run::Event<'_>| {
{
let mut p = match progress.lock() {
Ok(g) => g,
Err(e) => e.into_inner(),
};
match &event {
blyte_agent::run::Event::Turn { n, usage, .. } => {
p.turns = *n;
p.usage.input += usage.input;
p.usage.cached += usage.cached;
p.usage.output += usage.output;
p.usage.thinking += usage.thinking;
p.in_gate = false;
}
blyte_agent::run::Event::GateStart { attempt } => {
p.gate_attempts = *attempt;
p.in_gate = true;
}
blyte_agent::run::Event::GateSlotWait { ms } => {
p.gate_wait_ms += ms;
}
blyte_agent::run::Event::Gate { .. } => {
p.in_gate = false;
}
_ => {}
}
}
if let Ok(mut w) = writer_mutex.lock() {
let _ = writeln!(w, "{}", crate::repl::format_event(&event));
}
};
let runner = blyte_agent::run::Runner {
provider: provider.as_ref(),
shell,
fs,
context: context_strategy,
root,
budget: blyte_agent::run::Budget {
max_turns: config.max_turns as u32,
max_gate_attempts: config.max_gates as u32,
max_duration: config.max_seconds.map(std::time::Duration::from_secs),
},
rollback: config.rollback,
on_event: Some(&on_event),
research: config.research,
claim_check: config.claim_check,
base_check: config.base_check,
};
let (outcome, report) = runner.run(state);
let model_id = provider.model_id();
let entry = crate::usage_log::Entry {
ts: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
repo: crate::usage_log::repo_of(&config.root),
session: crate::usage_log::session_from(
std::env::var("BLYTE_SESSION").ok(),
std::env::var("CLAUDE_CODE_SESSION_ID").ok(),
),
dispatch: std::env::var("BLYTE_DISPATCH_ID")
.ok()
.filter(|s| !s.is_empty()),
research: config.research,
model: model_id.clone(),
outcome: format!("{outcome:?}").to_lowercase(),
turns: report.outcome_turns,
input: report.usage.input,
cached: report.usage.cached,
output: report.usage.output,
thinking: report.usage.thinking,
cost_usd: blyte_provider::pricing(&model_id).map(|p| p.cost(&report.usage)),
};
if let Some(path) = crate::usage_log::default_path() {
if let Err(err) = crate::usage_log::append(&path, &entry) {
eprintln!("warning: usage log: {err}");
}
}
if let Some(path) = &config.report {
let mut record = crate::report::RunRecord::from_run(
config,
&model_id,
&outcome,
&report,
state,
started.elapsed(),
);
record.effort = provider.effort();
crate::report::write(path, &record)?;
}
Ok((outcome, report))
}