use anyhow::Context;
pub(crate) mod active_models;
pub(crate) mod analyze;
pub(crate) mod automations;
pub mod bun;
pub mod chrome;
pub mod chrome_daemon;
pub(crate) mod chrome_groups;
pub mod chrome_release;
pub(crate) mod chrome_tab_ledger;
pub(crate) mod chrome_tabs;
pub mod computer;
pub(crate) mod custom;
pub(crate) mod delegation;
pub(crate) mod document;
pub(crate) mod edit;
pub(crate) mod files;
pub(crate) mod grants;
pub(crate) mod hyperframes;
pub(crate) mod image_gen;
pub(crate) mod implement;
pub(crate) mod listing;
pub(crate) mod mahbot_config;
pub(crate) mod mahbot_debug;
pub mod media_catalog;
pub(crate) mod path;
pub(crate) mod rasterize;
pub(crate) mod rasterize_html;
pub(crate) mod read;
pub(crate) mod read_document;
pub(crate) mod report_gap;
pub(crate) mod research;
pub(crate) mod say;
pub(crate) mod search;
pub(crate) mod search_archived_tickets;
pub(crate) mod send;
pub(crate) mod shell;
pub(crate) mod sleep;
pub(crate) mod ticket;
pub(crate) mod video_gen;
pub(crate) mod web_search;
pub(crate) mod workspace;
pub(crate) const MAX_FILE_SIZE_BYTES: u64 = 10 * 1024 * 1024;
const MAX_REFERENCE_IMAGE_BYTES: u64 = 1_500_000;
const MAX_REQUEST_BODY_BYTES: usize = 2_000_000;
pub(crate) const MAX_REFERENCE_IMAGES_PER_REQUEST: usize = 16;
#[expect(clippy::cast_possible_wrap)] #[must_use]
pub(crate) fn effective_reference_max(catalog_max: i64) -> i64 {
catalog_max.min(MAX_REFERENCE_IMAGES_PER_REQUEST as i64)
}
const PATH_ALIAS_KEYS: &[&str] = &["file", "filename"];
fn check_size_within(meta: &std::fs::Metadata, limit: u64, label: &str) -> anyhow::Result<()> {
if meta.len() > limit {
anyhow::bail!("{label}: {} bytes (limit: {limit} bytes)", meta.len());
}
Ok(())
}
fn reference_json(references: &[crate::util::ReferenceImage]) -> serde_json::Value {
serde_json::json!(
references
.iter()
.map(|r| serde_json::json!({
"type": "image_url",
"image_url": { "url": r.data_uri() }
}))
.collect::<Vec<_>>()
)
}
const INPUT_REFERENCES_KEY: &str = "input_references";
pub(crate) fn fit_request_body_budget(
body: &mut serde_json::Value,
references: &mut [crate::util::ReferenceImage],
max_body_bytes: usize,
) -> anyhow::Result<()> {
if !references.is_empty() {
body[INPUT_REFERENCES_KEY] = reference_json(references);
}
loop {
if serde_json::to_vec(body)?.len() <= max_body_bytes {
for r in references {
r.release_source_bytes();
}
return Ok(());
}
let Some(idx) = references
.iter()
.enumerate()
.filter(|(_, r)| r.has_compression_left())
.max_by_key(|(_, r)| r.data_uri().len())
.map(|(i, _)| i)
else {
break;
};
references[idx].compress_more()?;
body[INPUT_REFERENCES_KEY] = reference_json(references);
}
let body_len = serde_json::to_vec(body)?.len();
if references.is_empty() {
anyhow::bail!(
"Generation request body is too large ({body_len} bytes; limit {max_body_bytes} bytes). \
Shorten the prompt.",
);
}
let advice = if references.len() > 1 {
"Reduce the reference image size, the number of references, or the prompt length."
} else {
"Reduce the reference image size or shorten the prompt."
};
anyhow::bail!(
"Generation request body is too large ({body_len} bytes after compression; \
limit {max_body_bytes} bytes). {advice}",
);
}
pub(crate) use analyze::{AnalyzeDepth, AnalyzeTool, DispatchMode};
pub(crate) use automations::{AddAutomationTool, ListAutomationsTool, RemoveAutomationTool};
pub(crate) use chrome::ChromeTool;
pub(crate) use computer::ComputerTool;
pub(crate) use custom::CustomTool;
pub(crate) use document::DocumentTool;
pub(crate) use edit::EditTool;
pub(crate) use files::FilesTool;
pub(crate) use hyperframes::HyperframesTool;
pub(crate) use image_gen::ImageGenTool;
pub(crate) use mahbot_config::MahbotConfigTool;
pub(crate) use mahbot_debug::MahbotDebugTool;
pub(crate) use rasterize::RasterizeTool;
pub(crate) use read::ReadTool;
pub(crate) use report_gap::ReportGapTool;
pub(crate) use say::SayTool;
pub(crate) use search::SearchTool;
pub(crate) use search_archived_tickets::SearchArchivedTicketsTool;
pub(crate) use send::{Addressee, SendTool};
pub(crate) use shell::{ShellMode, ShellTool};
pub(crate) use sleep::SleepTool;
pub(crate) use ticket::{
AddCommentTool, CreateTicketTool, GetTicketTool, ListTicketsTool, UpdateTicketTool,
};
pub(crate) use video_gen::VideoGenTool;
pub(crate) use web_search::WebSearchTool;
pub(crate) use workspace::WorkspaceTool;
use crate::agent::message_router::{AgentJob, MessageKind};
use crate::{Tool, Workspace};
use futures_util::FutureExt;
use std::borrow::Cow;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use crate::util::json::{
get_bool, get_object, get_opt_bool, get_opt_i64, get_opt_named_str, get_opt_str,
get_opt_str_strict, get_opt_u64, get_str, get_str_array, get_usize, wrong_type,
};
pub(crate) fn get_dispatch_message(args: &serde_json::Value) -> anyhow::Result<&str> {
let message = get_str(args, "message")?;
anyhow::ensure!(
!message.trim().is_empty(),
"usage: the message must not be empty"
);
Ok(message)
}
pub(crate) fn log_normalization_note(tool: &str, note: &str) {
tracing::debug!(tool, note, "tool arguments were normalized");
}
pub(crate) fn with_note(text: &str, note: &str) -> String {
let mut out = text.trim_end().to_string();
if !out.is_empty() {
out.push_str("\n\n");
}
out.push_str(note);
out
}
pub(crate) fn refuse_choice(argument: &str, value: &str, allowed: &[&str]) -> anyhow::Error {
anyhow::anyhow!(
"usage: `{argument}` must be one of {} — got \"{value}\"",
allowed.join(", ")
)
}
pub(crate) fn chosen_kind<T: Copy + PartialEq>(
args: &serde_json::Value,
argument: &str,
held: &[T],
spelling: impl Fn(T) -> &'static str,
parse: impl Fn(&str) -> Option<T>,
) -> anyhow::Result<T> {
debug_assert!(!held.is_empty(), "a holder always has at least one kind");
match args.get(argument) {
None | Some(serde_json::Value::Null) => Ok(held[0]),
Some(serde_json::Value::String(value)) => parse(value)
.filter(|kind| held.contains(kind))
.ok_or_else(|| {
refuse_choice(
argument,
value,
&held.iter().map(|kind| spelling(*kind)).collect::<Vec<_>>(),
)
}),
Some(other) => Err(wrong_type(argument, "a string", other)),
}
}
#[must_use]
pub(crate) fn tool_params_schema(
properties: &serde_json::Value,
required: &[&str],
) -> serde_json::Value {
let mut schema = serde_json::json!({
"type": "object",
"properties": properties,
});
if !required.is_empty() {
schema["required"] = serde_json::json!(required);
}
schema
}
#[must_use]
pub(crate) fn action_entry_schema(
name: &str,
description: &str,
required: &[&str],
properties: &serde_json::Value,
) -> serde_json::Value {
let mut inner = tool_params_schema(properties, required);
inner["additionalProperties"] = serde_json::json!(false);
serde_json::json!({
"type": "object",
"properties": {
(name): inner
},
"required": [name],
"additionalProperties": false,
"description": description
})
}
pub(crate) const TOOL_FAILURE_MARKER: &str = "Tool call failed.";
const TOOL_FAILURE_ARGS_PREVIEW_BYTES: usize = 1_000;
const TOOL_FAILURE_REASON_PREFIX: &str = "Error executing ";
#[must_use]
pub(crate) fn tool_failure_reason(tool_name: &str, fault: &str) -> String {
format!("{TOOL_FAILURE_REASON_PREFIX}{tool_name}: {fault}")
}
pub(crate) const fn tool_failure_envelope_bytes(tool_name_len: usize) -> usize {
TOOL_FAILURE_REASON_PREFIX.len()
+ tool_name_len
+ ": ".len()
+ TOOL_FAILURE_MARKER.len()
+ "\ntool: \narguments: \nreason:\n".len()
+ tool_name_len
+ TOOL_FAILURE_ARGS_PREVIEW_BYTES
}
#[must_use]
pub(crate) fn format_tool_failure_feedback(
tool_name: &str,
tool_args: &serde_json::Value,
reason: &str,
) -> String {
let args = tool_args.to_string();
let args_preview = crate::util::cut_bytes(&args, TOOL_FAILURE_ARGS_PREVIEW_BYTES);
format!(
"{TOOL_FAILURE_MARKER}\n\
tool: {tool_name}\n\
arguments: {args_preview}\n\
reason:\n{reason}"
)
}
#[must_use]
pub(crate) fn internal_fault(cause: &str) -> anyhow::Error {
anyhow::anyhow!(
"internal: {cause} (mahbot-side fault) — \
hint: no model action can recover this; a user or engineer must \
investigate the mahbot service"
)
}
pub(crate) async fn caught_round<T>(
label: &str,
round: impl std::future::Future<Output = T>,
) -> Result<T, String> {
let round = Box::pin(round);
match std::panic::AssertUnwindSafe(round).catch_unwind().await {
Ok(value) => Ok(value),
Err(panic) => {
let panic = crate::util::panic_message(&*panic);
tracing::error!(panic = %panic, "{label} panicked");
Err(panic)
}
}
}
struct ExecEngineGuard<'a>(&'a crate::Workspace);
impl Drop for ExecEngineGuard<'_> {
fn drop(&mut self) {
crate::tools::delegation::release_exec_engine(self.0);
}
}
#[derive(Debug)]
pub(crate) struct CallSuspended;
impl std::fmt::Display for CallSuspended {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"tool call suspended by shutdown/drain — durable work left launched for deterministic resume"
)
}
}
impl std::error::Error for CallSuspended {}
pub(crate) struct CoreJobArgs<'a> {
pub job_id: &'a str,
pub caller_role: crate::Role,
pub user_name: &'a str,
pub channel: &'a str,
pub spawn: Option<SpawnRecord<'a>>,
pub caller_agent_id: Option<&'a str>,
pub fail_on_checkpoint_error: bool,
}
pub(crate) struct SpawnRecord<'a> {
pub delivery_name: &'a str,
pub exec_dir: Option<&'a str>,
pub custom_launch: Option<&'a crate::Launch>,
}
pub(crate) enum SyncCoreOutcome {
Terminal(Result<String, anyhow::Error>),
DrainCut,
Cancelled,
}
#[derive(Copy, Clone)]
pub(crate) enum SyncDurableCore {
Analyze,
Worker,
}
impl SyncDurableCore {
#[must_use]
pub(crate) fn label(self) -> &'static str {
match self {
Self::Analyze => "Analyze",
Self::Worker => "Worker",
}
}
pub(crate) fn kind_str(self) -> &'static str {
match self {
Self::Analyze => crate::jobs::SpawnChild::Analyze.kind_str(),
Self::Worker => crate::jobs::SpawnChild::Implement.kind_str(),
}
}
#[must_use]
pub(crate) fn from_call(name: &str, args: &serde_json::Value) -> Option<Self> {
if name == "analyze"
&& args
.get(crate::tools::analyze::ACTION_ARG)
.is_some_and(|v| !v.is_null())
{
return None;
}
let (core, argument, own_choice) = match name {
"analyze" => (
Self::Analyze,
AnalyzeDepth::ARG,
AnalyzeDepth::Quick.as_str(),
),
"send" => (Self::Worker, Addressee::ARG, Addressee::Worker.as_str()),
_ => return None,
};
match args.get(argument) {
None | Some(serde_json::Value::Null) => {}
Some(serde_json::Value::String(value)) if value == own_choice => {}
Some(_) => return None,
}
Some(core)
}
pub(crate) async fn run(
self,
ws: &crate::Workspace,
task: &str,
args: CoreJobArgs<'_>,
) -> anyhow::Result<SyncCoreOutcome> {
match self {
Self::Analyze => crate::tools::analyze::run_analyze_with_job(ws, task, args).await,
Self::Worker => crate::tools::implement::run_implement_with_job(ws, task, args).await,
}
}
async fn abandon_failed_dispatch(conn: &crate::db::Connection, job_id: &str, label: &str) {
if let Err(e) = crate::jobs::terminalize_job(conn, job_id).await {
tracing::warn!(
job = %job_id,
error = %e,
"Failed to abandon the failed {label} dispatch's row",
);
}
}
fn cancel_guard(
self,
job_id: &str,
caller: Option<&str>,
) -> Option<crate::round_cancel::CancelGuard> {
match self {
Self::Analyze => Some(crate::round_cancel::register(job_id, caller)),
Self::Worker => None,
}
}
fn stopped_by_cancel(self, job_id: &str) -> bool {
if !crate::round_cancel::is_cancelled(job_id) {
return false;
}
tracing::info!(job = %job_id, "{} round stopped by cancel — nothing delivered", self.label());
true
}
pub(crate) async fn run_sync_dispatch(
self,
ws: &crate::Workspace,
task: &str,
caller_role: crate::Role,
) -> anyhow::Result<String> {
let caller_agent_id = crate::agent::CURRENT_TOOL_AGENT_ID
.try_with(Clone::clone)
.unwrap_or(None);
let user_name = crate::agent::tool_user_name();
let channel = crate::agent::tool_channel();
let conn = &crate::session::store().conn;
let job_id = crate::generate_id();
let custom = crate::agent::tool_custom_launch();
let args = CoreJobArgs {
job_id: &job_id,
caller_role: caller_role.for_launch(custom.as_ref()),
user_name: &user_name,
channel: &channel,
spawn: Some(SpawnRecord {
delivery_name: &ws.name,
exec_dir: None,
custom_launch: custom.as_ref(),
}),
caller_agent_id: caller_agent_id.as_deref(),
fail_on_checkpoint_error: caller_agent_id.is_some(),
};
let outcome = match self.run(ws, task, args).await {
Ok(outcome) => outcome,
Err(e) => {
Self::abandon_failed_dispatch(conn, &job_id, self.label()).await;
return Err(e);
}
};
match outcome {
SyncCoreOutcome::Terminal(Ok(text)) => {
crate::jobs::terminalize_job(conn, &job_id).await?;
Ok(text)
}
SyncCoreOutcome::Terminal(Err(e)) => {
Self::abandon_failed_dispatch(conn, &job_id, self.label()).await;
Err(e)
}
SyncCoreOutcome::DrainCut => {
tracing::info!(
job = %job_id,
"{} round cut short by drain — job stays launched for deterministic resume",
self.label(),
);
Err(anyhow::Error::new(crate::tools::CallSuspended))
}
SyncCoreOutcome::Cancelled => Err(crate::tools::internal_fault(
"a sync round observed a cancel signal it never registered",
)),
}
}
pub(crate) async fn resume_sync_core(
self,
ws: &crate::Workspace,
job_id: &str,
sync_resume: bool,
) -> anyhow::Result<crate::jobs::SyncResumeOutcome> {
let label = self.label();
let (caller, caller_role) = match crate::jobs::resume_job_preamble_discrete(
&crate::session::store().conn,
job_id,
&format!("{label} resume"),
&format!("{label} resume"),
)
.await
{
crate::jobs::ResumePreamble::Proceed(caller, caller_role) => (caller, caller_role),
crate::jobs::ResumePreamble::DrainCut => {
return Ok(crate::jobs::SyncResumeOutcome::DrainCut);
}
crate::jobs::ResumePreamble::Gone => {
return Ok(crate::jobs::SyncResumeOutcome::Gone);
}
crate::jobs::ResumePreamble::Unreadable(e) => {
return Err(e.context(format!("{label} resume: job row unreadable")));
}
};
let args = CoreJobArgs {
job_id,
caller_role,
user_name: &caller.user_name,
channel: &caller.channel,
spawn: None,
caller_agent_id: None,
fail_on_checkpoint_error: sync_resume,
};
let run = self.run(ws, &caller.task, args).await;
match run {
Ok(SyncCoreOutcome::Terminal(result)) => Ok(crate::jobs::SyncResumeOutcome::Terminal(
caller_role,
caller,
result,
)),
Ok(SyncCoreOutcome::DrainCut) => {
tracing::info!(
job = %job_id,
"{label} resume drain-cut — outcome checkpointed, job stays for next boot",
);
Ok(crate::jobs::SyncResumeOutcome::DrainCut)
}
Ok(SyncCoreOutcome::Cancelled) => Ok(crate::jobs::SyncResumeOutcome::Gone),
Err(e) => Err(e),
}
}
#[must_use]
pub(crate) fn message_kind(self) -> MessageKind {
match self {
Self::Analyze => MessageKind::AnalyzeToolResult,
Self::Worker => MessageKind::ImplementResult,
}
}
#[must_use]
pub(crate) fn build_async_message(
self,
job_id: &str,
result: &anyhow::Result<String>,
exec_dir: Option<&str>,
delivery: &crate::Workspace,
) -> String {
match self {
Self::Analyze => {
build_async_result_envelope(job_id, result, "analyze-tool-result", exec_dir)
}
Self::Worker => build_worker_envelope(
"worker_report.md",
exec_dir.unwrap_or(&delivery.path),
None,
&worker_report_text(result),
),
}
}
#[expect(clippy::too_many_arguments)]
pub(crate) async fn dispatch_durable(
self,
delivery: &crate::Workspace,
place: Option<&crate::Workspace>,
task: &str,
caller_role: crate::Role,
user_name: &str,
channel: &str,
job_id: &str,
custom: Option<crate::Launch>,
) -> Option<AgentJob> {
let exec = place.unwrap_or(delivery);
let exec_dir = place.map(|p| p.path.as_str());
let caller_role = caller_role.for_launch(custom.as_ref());
let result = match self
.run(
exec,
task,
CoreJobArgs {
job_id,
caller_role,
user_name,
channel,
spawn: Some(SpawnRecord {
delivery_name: &delivery.name,
exec_dir,
custom_launch: custom.as_ref(),
}),
caller_agent_id: None,
fail_on_checkpoint_error: false,
},
)
.await
{
Ok(SyncCoreOutcome::DrainCut) => {
tracing::info!(
job = %job_id,
"{} round cut short by drain — job stays launched for boot resume",
self.label(),
);
return None;
}
Ok(SyncCoreOutcome::Cancelled) => {
tracing::info!(
job = %job_id,
"{} round stopped by cancel — not completed, not routed",
self.label(),
);
if let Some(place) = place {
crate::tools::delegation::release_exec_engine(place);
}
return None;
}
Ok(SyncCoreOutcome::Terminal(result)) => result,
Err(e) => Err(e),
};
if let Some(place) = place {
crate::tools::delegation::release_exec_engine(place);
}
Some(
crate::jobs::complete_durable_job(
job_id,
self.build_async_message(job_id, &result, exec_dir, delivery),
self.message_kind(),
caller_role,
user_name,
channel,
&delivery.name,
custom,
)
.await,
)
}
pub(crate) async fn resume_durable_round(
self,
job_id: &str,
delivery: &crate::Workspace,
exec_dir: Option<&str>,
) {
let label = self.label();
let _cancel_guard = self.cancel_guard(job_id, None);
if self.stopped_by_cancel(job_id) {
return;
}
let resumed = caught_round(&format!("{label} resume"), async {
match exec_dir {
None => self.resume_sync_core(delivery, job_id, false).await,
Some(dir) => match crate::tools::delegation::workspace_at_dir(dir).await {
Ok(exec) => {
let _engine = ExecEngineGuard(&exec);
self.resume_sync_core(&exec, job_id, false).await
}
Err(e) => Err(e),
},
}
})
.await
.unwrap_or_else(|panic| Err(anyhow::anyhow!("the round panicked: {panic}")));
let (caller_role, caller, custom, result) = match resumed {
Ok(crate::jobs::SyncResumeOutcome::Terminal(caller_role, caller, result)) => {
let custom = caller.custom_launch.as_deref().cloned();
(caller_role, caller, custom, result)
}
Ok(crate::jobs::SyncResumeOutcome::DrainCut | crate::jobs::SyncResumeOutcome::Gone) => {
return;
}
Err(e) => {
let Some((caller, caller_role)) = crate::jobs::resume_job_preamble(
&crate::session::store().conn,
job_id,
&format!("{label} resume"),
&format!("{label} resume"),
)
.await
else {
return;
};
let custom = caller.custom_launch.as_deref().cloned();
let envelope = crate::jobs::complete_durable_job(
job_id,
self.build_async_message(job_id, &Err(e), exec_dir, delivery),
self.message_kind(),
caller_role,
&caller.user_name,
&caller.channel,
&delivery.name,
custom,
)
.await;
if self.stopped_by_cancel(job_id) {
return;
}
crate::agent::message_router::route(
&crate::jobs::envelope_target(&envelope),
envelope,
);
return;
}
};
if crate::shutdown::aborting() {
tracing::info!(
job = %job_id,
"{label} resume aborted after the round completed — job stays for next boot",
);
return;
}
let envelope = crate::jobs::complete_durable_job(
job_id,
self.build_async_message(job_id, &result, exec_dir, delivery),
self.message_kind(),
caller_role,
&caller.user_name,
&caller.channel,
&delivery.name,
custom,
)
.await;
if self.stopped_by_cancel(job_id) {
return;
}
crate::agent::message_router::route(&crate::jobs::envelope_target(&envelope), envelope);
}
pub(crate) fn spawn_dispatch(
self,
delivery: &crate::Workspace,
place: Option<&crate::Workspace>,
task: &str,
caller_role: crate::Role,
job_id: String,
) {
let user_name = crate::agent::tool_user_name();
let channel = crate::agent::tool_channel();
let custom = crate::agent::tool_custom_launch();
let caller = crate::agent::CURRENT_TOOL_AGENT_ID
.try_with(Clone::clone)
.unwrap_or(None);
let cancel_guard = self.cancel_guard(&job_id, caller.as_deref());
let (delivery, task) = (delivery.clone(), task.to_string());
let place = place.cloned();
let ws_name = delivery.name.clone();
tokio::spawn(async move {
let _cancel_guard = cancel_guard;
let exec_dir = place.as_ref().map(|p| p.path.as_str());
let round = caught_round(&format!("{} round dispatch", self.label()), async {
self.dispatch_durable(
&delivery,
place.as_ref(),
&task,
caller_role,
&user_name,
&channel,
&job_id,
custom.clone(),
)
.await
})
.await;
let envelope = match round {
Ok(Some(envelope)) => envelope,
Ok(None) => {
return;
}
Err(panic) => {
if let Some(exec) = place.as_ref() {
crate::tools::delegation::release_exec_engine(exec);
}
let role = caller_role.for_launch(custom.as_ref());
AgentJob {
content: self.build_async_message(
&job_id,
&Err(anyhow::anyhow!("dispatch panicked: {panic}")),
exec_dir,
&delivery,
),
workspace_name: ws_name,
user_name,
channel,
kind: self.message_kind(),
role,
reply_target: None,
pending_job_id: Some(job_id.clone()),
originating_workspace: None,
custom_role: custom,
reply_to_custom: None,
urgent: false,
}
}
};
if self.stopped_by_cancel(&job_id) {
return;
}
if crate::shutdown::aborting() {
return;
}
crate::agent::message_router::route(&crate::jobs::envelope_target(&envelope), envelope);
});
}
}
pub(crate) fn build_async_result_envelope(
job_id: &str,
result: &anyhow::Result<String>,
tag: &str,
exec_dir: Option<&str>,
) -> String {
let dir_line = exec_dir.map_or_else(String::new, |dir| {
format!(
"\n\nDirectory: {}",
dir.trim_end_matches(std::path::MAIN_SEPARATOR)
)
});
match result {
Ok(text) => format!("<{tag}>\n\nJob: {job_id}{dir_line}\n\n{text}</{tag}>"),
Err(e) => {
tracing::debug!(error = %e, %tag, "async tool result failed");
format!("<{tag}>\n\nJob: {job_id}{dir_line}\n\nAn error occurred: {e}</{tag}>")
}
}
}
fn worker_report_text(result: &anyhow::Result<String>) -> String {
match result {
Ok(text) => text.clone(),
Err(e) => {
tracing::debug!(error = %e, "worker report failed");
format!("An error occurred: {e}")
}
}
}
pub(crate) fn build_worker_envelope(
asset: &str,
folder: &str,
branch: Option<&str>,
message: &str,
) -> String {
crate::prompt::substitute(
&crate::prompt::load_prompt(asset),
&[
(
"{{folder}}",
envelope_attr(folder.trim_end_matches(std::path::MAIN_SEPARATOR)).as_str(),
),
("{{branch_attr}}", branch_attr(branch).as_str()),
("{{message}}", message),
],
)
}
pub(crate) fn branch_attr(identifier: Option<&str>) -> String {
identifier.map_or_else(String::new, |id| {
format!(" branch=\"{}\"", envelope_attr(id))
})
}
pub(crate) fn envelope_attr(value: &str) -> String {
value
.replace('"', """)
.replace('<', "<")
.replace('>', ">")
}
#[derive(Debug, Default)]
pub(crate) struct ToolOutput {
pub text: String,
pub image_payloads: Vec<ImagePayload>,
pub text_is_content: bool,
pub preserve_text: bool,
}
pub(crate) async fn with_image_payload<T: Tool + ?Sized>(
tool: &T,
text: String,
ws: &Workspace,
args: &serde_json::Value,
) -> ToolOutput {
ToolOutput {
text,
image_payloads: tool.image_payload(ws, args).await.into_iter().collect(),
text_is_content: false,
preserve_text: false,
}
}
#[expect(clippy::struct_excessive_bools)] #[derive(Debug)]
pub(crate) struct ToolExecutionOutcome {
pub output: String,
pub success: bool,
pub image_payloads: Vec<ImagePayload>,
pub text_is_content: bool,
pub preserve_text: bool,
pub suspended: bool,
pub ends_turn: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ImagePayloadSource {
Read,
Generated,
Chrome,
Computer,
Grid,
}
impl ImagePayloadSource {
#[must_use]
pub(crate) fn opener(self) -> &'static str {
match self {
Self::Read => "Read image file",
Self::Generated => "Generated image file",
Self::Chrome => "Chrome screenshot",
Self::Computer => "Computer screenshot",
Self::Grid => "Video frame grid",
}
}
}
#[derive(Debug, Clone)]
pub(crate) struct ImagePayload {
pub path: String,
pub data_uri: String,
pub picture_size: (u32, u32),
pub copy_size: (u32, u32),
pub format: String,
pub recovery_note: Option<String>,
pub note: Option<String>,
pub source: ImagePayloadSource,
}
#[must_use]
pub(crate) fn with_recovery_note(note: Option<&str>, body: String) -> String {
match note {
Some(note) => format!("{note}\n{body}"),
None => body,
}
}
impl ImagePayload {
#[must_use]
pub(crate) fn attached_annotation(&self) -> String {
self.annotation("Image content attached to the conversation as a native image.")
}
#[must_use]
pub(crate) fn already_attached_annotation(&self) -> String {
self.annotation(
"Image content is already in the conversation as a native image and is not attached \
again.",
)
}
#[must_use]
fn annotation(&self, sentence: &str) -> String {
let (width, height) = self.picture_size;
let mut lines = vec![format!(
"{} {} ({width}x{height}, {}). {sentence}",
self.source.opener(),
self.path,
self.format,
)];
if self.copy_size != self.picture_size {
let (copy_width, copy_height) = self.copy_size;
lines.push(format!("The model's copy is {copy_width}x{copy_height}."));
}
if let Some(note) = &self.note {
lines.push(note.clone());
}
with_recovery_note(self.recovery_note.as_deref(), lines.join("\n"))
}
#[must_use]
pub(crate) fn from_compressed_meta(
path: &std::path::Path,
meta: crate::util::CompressedImageMeta,
recovery_note: Option<String>,
source: ImagePayloadSource,
) -> Self {
let crate::util::CompressedImageMeta {
data_uri,
sizes,
format,
} = meta;
Self {
path: path.display().to_string(),
data_uri,
picture_size: sizes.file_size,
copy_size: sizes.copy_size,
format,
recovery_note,
note: None,
source,
}
}
}
#[must_use]
pub(crate) fn normalize_tool_call(
name: &str,
mut args: serde_json::Value,
) -> (String, serde_json::Value) {
if name == "glob"
&& let Some(obj) = args.as_object_mut()
&& !obj.contains_key("mode")
{
obj.insert("mode".to_string(), serde_json::json!("files"));
}
let normalized_name = normalize_tool_name(name).to_string();
normalize_tool_arguments(&normalized_name, &mut args);
(normalized_name, args)
}
pub(crate) fn normalize_tool_name(name: &str) -> &str {
match name {
"bash" | "run_terminal_cmd" => "shell",
"grep" | "rg" | "grep_search" | "glob" => "search",
"read_file" => "read",
"str_replace" => "edit",
"browser" => "chrome",
_ => name,
}
}
fn normalize_tool_arguments(name: &str, args: &mut serde_json::Value) {
let Some(obj) = args.as_object_mut() else {
return;
};
if name != "document" && name != "computer" {
for &alias in PATH_ALIAS_KEYS {
remap_arg_key(obj, alias, "path");
}
}
match name {
"edit" => {
remap_arg_key(obj, "old_str", "old_string");
remap_arg_key(obj, "new_str", "new_string");
}
"shell" => {
remap_arg_key(obj, "cmd", "command");
remap_arg_key(obj, "script", "command");
}
"get_ticket" | "update_ticket" | "add_comment" | "workspace" => {
remap_arg_key(obj, "id", "ticket_id");
remap_arg_key(obj, "ticket", "ticket_id");
}
_ => {}
}
}
fn remap_arg_key(obj: &mut serde_json::Map<String, serde_json::Value>, from: &str, to: &str) {
if !obj.contains_key(to)
&& let Some(v) = obj.remove(from)
{
obj.insert(to.to_string(), v);
}
}
pub(crate) struct ToolAnswer {
pub text: String,
pub partial: bool,
}
impl ToolAnswer {
pub(crate) fn done(text: impl Into<String>) -> Self {
Self {
text: text.into(),
partial: false,
}
}
pub(crate) fn partial(text: impl Into<String>) -> Self {
Self {
text: text.into(),
partial: true,
}
}
}
#[must_use]
pub(crate) fn find_tool<'a>(tools: &'a [Box<dyn Tool>], name: &str) -> Option<&'a dyn Tool> {
let normalized = normalize_tool_name(name);
tools
.iter()
.find(|t| t.name() == normalized)
.map(Box::as_ref)
}
pub(crate) const GENERATED_DIR: &str = "generated";
pub(crate) fn output_directory(ws: &Workspace, fallback: &str) -> PathBuf {
crate::agent::tool_output_dir().unwrap_or_else(|| ws.as_path().join(fallback))
}
pub(crate) async fn resolve_local_media_path(
input: &str,
ws: &Workspace,
kind: &str,
) -> anyhow::Result<PathBuf> {
let root = crate::stored_workspace_path(ws.as_path());
let spelled = Path::new(input);
let candidate = if spelled.is_absolute() {
spelled.to_path_buf()
} else {
root.join(spelled)
};
let canonical = crate::util::strip_verbatim_prefix(
&tokio::fs::canonicalize(&candidate)
.await
.with_context(|| format!("Local {kind} not found: {input}"))?,
);
let meta = tokio::fs::metadata(&canonical)
.await
.with_context(|| format!("Local {kind} not found: {input}"))?;
if !meta.is_file() {
anyhow::bail!(
"Local {kind} must be a regular file — read with no timeout of its own, a pipe or a \
device would hold the operation forever, got: {}",
canonical.display()
);
}
if crate::agent::tool_command_line() {
return Ok(canonical);
}
if !path::is_path_under_roots(&canonical, std::slice::from_ref(&root)) {
tracing::warn!(
path = %canonical.display(),
"Local {kind} rejected: outside the caller's workspace"
);
anyhow::bail!(
"forbidden: a local {kind} must be a file inside your own workspace, got: {} — hint: \
copy the file into your workspace first",
canonical.display()
);
}
if let Some(area) = canonical
.strip_prefix(&root)
.ok()
.and_then(path::closed_area)
{
tracing::warn!(
path = %canonical.display(),
"Local {kind} rejected: inside the closed `{area}` area"
);
anyhow::bail!(
"forbidden: a local {kind} must be a file inside your own workspace, got: {} — \
`{area}/` is closed to every tool",
canonical.display()
);
}
Ok(canonical)
}
async fn save_generated_file(
ws: &Workspace,
bytes: &[u8],
prefix: &str,
ext: &str,
) -> anyhow::Result<PathBuf> {
let generated_dir = output_directory(ws, GENERATED_DIR);
tokio::fs::create_dir_all(&generated_dir)
.await
.with_context(|| {
format!(
"Failed to create generated directory at {}",
generated_dir.display()
)
})?;
let timestamp = crate::util::unix_millis();
let stripped = crate::util::provenance::strip_image_provenance(bytes);
let output_path = crate::util::write_unique_file(
&generated_dir,
&format!("{prefix}_{timestamp}.{ext}"),
stripped.as_ref(),
)
.await
.with_context(|| {
format!(
"Failed to write a generated file into {}",
generated_dir.display()
)
})?;
if matches!(stripped, Cow::Owned(_)) {
tracing::debug!(
path = %output_path.display(),
"Stripped AI-provenance metadata from a generated image artifact"
);
}
Ok(output_path)
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum TimelinePictures {
Attached,
TextOnly,
}
pub(crate) async fn timeline_output(
reading: crate::media::Reading,
pictures: TimelinePictures,
) -> (String, Vec<ImagePayload>) {
match pictures {
TimelinePictures::Attached => {
let composed = reading.pictures().await;
let count = composed.len();
let payloads = composed
.into_iter()
.enumerate()
.map(|(index, picture)| {
let note = format!(
"picture {} of {count}: {}",
index + 1,
picture.frames_text()
);
ImagePayload {
path: reading.path.display().to_string(),
data_uri: picture.data_uri,
picture_size: picture.size,
copy_size: picture.size,
format: "JPEG".to_string(),
recovery_note: None,
note: Some(note),
source: ImagePayloadSource::Grid,
}
})
.collect();
(reading.text, payloads)
}
TimelinePictures::TextOnly => (reading.text, Vec::new()),
}
}
pub(crate) async fn video_output(
prefix: String,
path: &std::path::Path,
pictures: TimelinePictures,
) -> ToolOutput {
let (text, image_payloads) =
match crate::media::read(path, crate::media::Window::Overview).await {
Ok(reading) => timeline_output(reading, pictures).await,
Err(error) => (error.unreadable(), Vec::new()),
};
ToolOutput {
text: format!("{prefix}\n\n{text}"),
image_payloads,
text_is_content: true,
..ToolOutput::default()
}
}
const VIDEO_JOB_TIMEOUT: Duration = Duration::from_hours(1);
const VIDEO_POLL_INTERVAL: Duration = Duration::from_secs(30);
struct VideoJobLabels {
label: &'static str,
gerund: &'static str,
download: &'static str,
}
impl VideoJobLabels {
const GENERATION: Self = Self {
label: "Video generation",
gerund: "generation",
download: "Video download",
};
const EDIT: Self = Self {
label: "Video edit",
gerund: "editing",
download: "Video edit download",
};
}
#[expect(clippy::too_many_lines)]
async fn fetch_async_video(
api_base: &str,
body: &serde_json::Value,
labels: VideoJobLabels,
) -> anyhow::Result<Vec<u8>> {
let submit_url = format!("{api_base}/videos");
let submit_body: serde_json::Value = match crate::util::http::post_json_to_provider(
&submit_url,
body,
&format!("{} submission", labels.label),
)
.await
{
Ok(v) => v,
Err(e) => {
if e.downcast_ref::<crate::util::error::HttpError>()
.map(|e| e.status)
== Some(402)
{
anyhow::bail!(
"Insufficient OpenRouter credits for video {} (HTTP 402). \
Please add credits to your OpenRouter account and try again.",
labels.gerund,
);
}
return Err(e);
}
};
let job_id = match submit_body.get("id").and_then(|v| v.as_str()) {
Some(id) => id.to_string(),
None => {
anyhow::bail!("No job ID in submission response: {submit_body}");
}
};
let polling_url = match submit_body.get("polling_url").and_then(|v| v.as_str()) {
Some(url) => url.to_string(),
None => format!("{api_base}/videos/{job_id}"),
};
tracing::info!(%job_id, "{} job submitted", labels.label);
let deadline = Instant::now() + VIDEO_JOB_TIMEOUT;
let mut result_url: Option<String> = None;
let mut attempt: u32 = 0;
while Instant::now() < deadline {
attempt += 1;
let remaining = deadline.saturating_duration_since(Instant::now());
tokio::time::sleep(VIDEO_POLL_INTERVAL.min(remaining)).await;
if Instant::now() >= deadline {
break;
}
let remaining = deadline.saturating_duration_since(Instant::now());
let poll_body = match tokio::time::timeout(
remaining,
crate::util::http::get_json_from_provider(
&polling_url,
&format!("{} poll", labels.label),
),
)
.await
{
Ok(Ok(v)) => v,
Ok(Err(e)) => {
tracing::debug!(%job_id, attempt, error = %e, "Poll failed");
continue;
}
Err(_) => break,
};
let status = poll_body
.get("status")
.and_then(|v| v.as_str())
.unwrap_or("unknown");
tracing::debug!(%job_id, %status, attempt, "{} poll", labels.label);
if status == "completed" {
result_url = poll_body
.get("unsigned_urls")
.and_then(|v| v.as_array())
.and_then(|arr| arr.first())
.and_then(|v| v.as_str())
.map(String::from)
.or_else(|| Some(format!("{api_base}/videos/{job_id}/content?index=0")));
break;
}
if status == "failed" || status == "cancelled" || status == "expired" {
let err_msg = poll_body
.get("error")
.and_then(|v| v.as_str())
.unwrap_or("Unknown error");
anyhow::bail!("{} failed: {err_msg}", labels.label);
}
}
let Some(download_url) = result_url else {
anyhow::bail!(
"{} did not complete within the 1-hour timeout period",
labels.label
);
};
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
anyhow::bail!(
"{} did not complete within the 1-hour timeout period",
labels.label
);
}
let video_bytes =
crate::util::http::get_bytes_from_provider(&download_url, labels.download, remaining)
.await?;
if video_bytes.len() <= 100_000 || &video_bytes[4..8] != b"ftyp" {
anyhow::bail!(
"{} returned an invalid file ({} bytes, no ftyp header)",
labels.download,
video_bytes.len(),
);
}
Ok(video_bytes)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Tool;
use crate::workspace::test_ws_named;
use tempfile::TempDir;
#[test]
fn failure_feedback_bounds_the_argument_preview_it_shows() {
let tool = "image_gen";
let fault = "Image generation failed: no configured image model served the request.";
let args = serde_json::json!({ "prompt": "生成".repeat(600) });
let args_str = args.to_string();
assert!(args_str.len() > TOOL_FAILURE_ARGS_PREVIEW_BYTES);
let reason = tool_failure_reason(tool, fault);
let feedback = format_tool_failure_feedback(tool, &args, &reason);
let preview = feedback
.lines()
.find_map(|line| line.strip_prefix("arguments: "))
.expect("the preview is shown");
assert!(
preview.ends_with('…'),
"a cut preview says so: {}",
preview.len()
);
assert!(
preview.len() <= TOOL_FAILURE_ARGS_PREVIEW_BYTES,
"{preview}"
);
assert!(
args_str.starts_with(preview.trim_end_matches('…')),
"the preview is the head of the arguments"
);
assert!(
feedback.len() <= tool_failure_envelope_bytes(tool.len()) + fault.len(),
"{} bytes for a fault text of {}",
feedback.len(),
fault.len()
);
assert_eq!(crate::util::truncate_tool_output(&feedback), feedback);
}
#[test]
fn image_payload_generated_verb_annotation() {
let payload = ImagePayload {
path: "/gen/img.png".into(),
data_uri: "data:image/jpeg;base64,aaa".into(),
picture_size: (4, 4),
copy_size: (4, 4),
format: "PNG".into(),
recovery_note: None,
note: None,
source: ImagePayloadSource::Generated,
};
let fresh = payload.attached_annotation();
assert!(
fresh.starts_with("Generated image file /gen/img.png"),
"fresh: {fresh}"
);
assert!(
!fresh.starts_with("Read image file"),
"must not be Read: {fresh}"
);
assert!(
!fresh.contains('\n'),
"note-free annotation must stay one sentence: {fresh}"
);
let dup = payload.already_attached_annotation();
assert!(
dup.starts_with("Generated image file /gen/img.png"),
"dup: {dup}"
);
assert!(dup.contains("already in the conversation"), "dup: {dup}");
let mut noted = payload.clone();
noted.note = Some("Served by fallback model fallback-1".into());
let fresh_noted = noted.attached_annotation();
assert!(
fresh_noted.starts_with(&fresh)
&& fresh_noted.ends_with("\nServed by fallback model fallback-1"),
"note must be appended as a new line: {fresh_noted}"
);
assert!(
noted
.already_attached_annotation()
.ends_with("\nServed by fallback model fallback-1"),
"note also rides on the already-attached annotation"
);
}
#[tokio::test]
async fn a_downscaled_copy_is_labelled_apart_from_the_file_size() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("big.png");
let (width, height) = (1536, 2048);
tokio::fs::write(&path, crate::util::test::solid_png(width, height))
.await
.unwrap();
let meta = crate::util::local_image_to_compressed_data_uri_with_meta(&path)
.await
.unwrap();
let payload =
ImagePayload::from_compressed_meta(&path, meta, None, ImagePayloadSource::Generated);
assert_eq!(
payload.picture_size,
(width, height),
"the file's own pixels"
);
assert_eq!(
payload.copy_size.1,
crate::util::INBOUND_IMAGE_MAX_SIDE,
"the copy is bounded by the attachment cap"
);
assert!(
payload.copy_size.0 < width,
"the copy is smaller than the file"
);
let annotation = payload.attached_annotation();
assert!(
annotation.starts_with(&format!(
"Generated image file {} ({width}x{height}, PNG). Image content attached to the \
conversation as a native image.",
path.display()
)),
"the naming sentence states the file's size: {annotation}"
);
let (copy_width, copy_height) = payload.copy_size;
assert!(
annotation.ends_with(&format!(
"\nThe model's copy is {copy_width}x{copy_height}."
)),
"the copy's size is its own labelled fact: {annotation}"
);
assert!(
payload.already_attached_annotation().ends_with(&format!(
"\nThe model's copy is {copy_width}x{copy_height}."
)),
"a deduped repeat states the copy's size too"
);
assert!(
!annotation.contains(&format!("({copy_width}x{copy_height}, PNG)")),
"the copy's figure is never printed as the file's: {annotation}"
);
}
#[tokio::test]
async fn a_picture_within_the_cap_owes_no_copy_clause() {
let dir = TempDir::new().unwrap();
let path = dir.path().join("small.png");
let (width, height) = (crate::util::INBOUND_IMAGE_MAX_SIDE, 768);
tokio::fs::write(&path, crate::util::test::solid_png(width, height))
.await
.unwrap();
let meta = crate::util::local_image_to_compressed_data_uri_with_meta(&path)
.await
.unwrap();
let payload =
ImagePayload::from_compressed_meta(&path, meta, None, ImagePayloadSource::Generated);
assert_eq!(payload.picture_size, (width, height));
assert_eq!(payload.copy_size, (width, height));
assert_eq!(
payload.attached_annotation(),
format!(
"Generated image file {} ({width}x{height}, PNG). Image content attached to the \
conversation as a native image.",
path.display()
)
);
}
#[test]
fn a_frame_grid_states_its_own_size_and_owes_no_copy_clause() {
let payload = ImagePayload {
path: "/clip.mp4".into(),
data_uri: "data:image/jpeg;base64,grid".into(),
picture_size: (320, 240),
copy_size: (320, 240),
format: "JPEG".into(),
recovery_note: None,
note: Some("picture 1 of 2: frames 1-4".into()),
source: ImagePayloadSource::Grid,
};
assert_eq!(
payload.attached_annotation(),
"Video frame grid /clip.mp4 (320x240, JPEG). Image content attached to the conversation \
as a native image.\npicture 1 of 2: frames 1-4"
);
}
struct NormalizeCallCase {
name: &'static str,
tool: &'static str,
input: serde_json::Value,
routed: &'static str,
fields: &'static [(&'static str, &'static str)],
kept: Option<&'static str>,
}
#[expect(clippy::too_many_lines)]
#[test]
fn normalize_tool_call_cases() {
for case in [
NormalizeCallCase {
name: "a shell alias sends the command under its own key",
tool: "bash",
input: serde_json::json!({"cmd": "echo hi"}),
routed: "shell",
fields: &[("command", "echo hi")],
kept: None,
},
NormalizeCallCase {
name: "a search alias becomes a file listing",
tool: "glob",
input: serde_json::json!({"query": "main.rs"}),
routed: "search",
fields: &[("mode", "files")],
kept: None,
},
NormalizeCallCase {
name: "an id argument is the ticket to read",
tool: "get_ticket",
input: serde_json::json!({"id": "mahbot-1"}),
routed: "get_ticket",
fields: &[("ticket_id", "mahbot-1")],
kept: None,
},
NormalizeCallCase {
name: "an id argument is the workspace to add",
tool: "workspace",
input: serde_json::json!({"id": "ws-1"}),
routed: "workspace",
fields: &[("ticket_id", "ws-1")],
kept: None,
},
NormalizeCallCase {
name: "a path alias becomes the path to read",
tool: "read",
input: serde_json::json!({"file": "src/main.rs"}),
routed: "read",
fields: &[("path", "src/main.rs")],
kept: None,
},
NormalizeCallCase {
name: "a read alias is routed to the reader with its own key",
tool: "read_file",
input: serde_json::json!({"filename": "lib.rs"}),
routed: "read",
fields: &[("path", "lib.rs")],
kept: None,
},
NormalizeCallCase {
name: "the editor's path and both halves of its replacement are repaired",
tool: "edit",
input: serde_json::json!({"file": "main.rs", "old_str": "foo", "new_str": "bar"}),
routed: "edit",
fields: &[
("path", "main.rs"),
("old_string", "foo"),
("new_string", "bar"),
],
kept: None,
},
NormalizeCallCase {
name: "a canonical path is never overwritten by an alias",
tool: "read",
input: serde_json::json!({"path": "canonical.rs", "file": "alias.rs"}),
routed: "read",
fields: &[("path", "canonical.rs")],
kept: Some("file"),
},
] {
let (name, args) = normalize_tool_call(case.tool, case.input);
assert_eq!(name, case.routed, "[{}] routed tool", case.name);
for (key, value) in case.fields {
assert_eq!(args[key], *value, "[{}] argument {key}", case.name);
}
if let Some(key) = case.kept {
assert!(
args.as_object().unwrap().contains_key(key),
"[{}] the caller's own key {key} must survive: {args}",
case.name
);
}
}
for &alias in PATH_ALIAS_KEYS {
for (tool_name, extra) in &[
("read", serde_json::json!({})),
("shell", serde_json::json!({"cmd": "ls"})),
] {
let mut input = serde_json::json!({});
input[alias] = serde_json::json!("src/main.rs");
if let Some(obj) = extra.as_object() {
for (k, v) in obj {
input[k] = v.clone();
}
}
let (name, args) = normalize_tool_call(tool_name, input);
assert_eq!(
name, *tool_name,
"tool name should not change for {tool_name} with alias {alias}"
);
assert_eq!(
args["path"], "src/main.rs",
"alias {alias} should be remapped to 'path' for tool {tool_name}"
);
assert!(
!args.as_object().unwrap().contains_key(alias),
"alias key {alias} should be removed after normalization for {tool_name}"
);
if *tool_name == "shell" {
assert_eq!(args["command"], "ls");
}
}
}
for tool_name in ["document", "computer"] {
let (_, args) = normalize_tool_call(tool_name, serde_json::json!({ "file": "a.pdf" }));
assert_eq!(
args["file"], "a.pdf",
"{tool_name} must keep the caller's key"
);
assert!(
args.get("path").is_none(),
"{tool_name} must not invent 'path'"
);
}
for (name, args) in [
("an absent address", serde_json::json!({"message": "m"})),
(
"an explicit null address",
serde_json::json!({"message": "m", "addressee": null}),
),
(
"the worker address",
serde_json::json!({"message": "m", "addressee": "worker"}),
),
] {
assert!(
matches!(
SyncDurableCore::from_call("send", &args),
Some(SyncDurableCore::Worker)
),
"{name} still resumes the sync worker"
);
}
assert!(
SyncDurableCore::from_call(
"send",
&serde_json::json!({"message": "m", "addressee": "manager"})
)
.is_none(),
"another address is re-executed, never resolved to this core"
);
}
#[test]
fn all_media_tools_implement_media_marker() {
let tools: [(&str, Box<dyn Tool>); 5] = [
("ImageGenTool", Box::new(ImageGenTool)),
("VideoGenTool", Box::new(VideoGenTool)),
(
"DocumentTool",
Box::new(DocumentTool::new(path::PathAccess::Workspace)),
),
(
"RasterizeTool",
Box::new(RasterizeTool::new(path::PathAccess::Workspace)),
),
(
"HyperframesTool",
Box::new(HyperframesTool::new(path::PathAccess::Workspace)),
),
];
for (name, tool) in &tools {
let marker = tool.media_marker();
assert!(
marker.is_some(),
"{name} should return Some from media_marker()"
);
let marker = marker.unwrap();
assert!(
marker.starts_with('['),
"{name} marker {marker:?} should start with '['"
);
assert!(
marker.ends_with(':'),
"{name} marker {marker:?} should end with ':'"
);
let kind = &marker[1..marker.len() - 1]; assert!(
!kind.is_empty() && kind.chars().all(char::is_uppercase),
"{name} marker kind {kind:?} should be non-empty uppercase letters"
);
let full_marker = format!("{marker}/some/path]");
assert!(
crate::util::MEDIA_MARKER_RE.is_match(&full_marker),
"{name} marker + path should match MEDIA_MARKER_RE, got: {full_marker:?}"
);
}
}
#[tokio::test]
async fn resolve_local_media_path_takes_any_file_for_a_command_line_call() {
let tmp = TempDir::new().expect("tempdir");
let ws = crate::Workspace::from_path(tmp.path());
let write = |name: &str| {
let path = tmp.path().join(name);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, b"media").unwrap();
path
};
let inside = write("generated/clip.mp4");
let elsewhere = TempDir::new().expect("tempdir");
let outside = elsewhere.path().join("picture.png");
std::fs::write(&outside, b"media").unwrap();
assert!(
resolve_local_media_path(&outside.to_string_lossy(), &ws, "clip")
.await
.is_err(),
"an agent's call must stay confined to the workspace"
);
assert!(
resolve_local_media_path(&inside.to_string_lossy(), &ws, "clip")
.await
.is_ok()
);
let taken = crate::agent::CURRENT_TOOL_COMMAND_LINE
.scope(true, async {
resolve_local_media_path(&outside.to_string_lossy(), &ws, "clip").await
})
.await
.expect("a command-line call takes the file the caller named");
assert_eq!(
taken,
crate::util::strip_verbatim_prefix(&outside.canonicalize().unwrap()),
"the resolved path is the caller's own file"
);
#[cfg(unix)]
{
let fifo = tmp.path().join("generated/pipe.mp4");
let path = std::ffi::CString::new(fifo.to_string_lossy().as_bytes())
.expect("a path without a NUL");
if unsafe { libc::mkfifo(path.as_ptr(), 0o600) } == 0 {
let err = resolve_local_media_path(&fifo.to_string_lossy(), &ws, "clip")
.await
.expect_err("a pipe is no media input");
assert!(err.to_string().contains("regular file"), "{err}");
let err = crate::agent::CURRENT_TOOL_COMMAND_LINE
.scope(true, async {
resolve_local_media_path(&fifo.to_string_lossy(), &ws, "clip").await
})
.await
.expect_err("a pipe is no media input for the command line either");
assert!(err.to_string().contains("regular file"), "{err}");
}
}
}
#[tokio::test]
async fn resolve_local_media_path_confines_to_the_callers_workspace() {
let tmp = TempDir::new().expect("tempdir");
let ws = crate::Workspace::from_path(tmp.path());
let write = |name: &str| {
let path = tmp.path().join(name);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, b"media").unwrap();
path.to_string_lossy().to_string()
};
for accepted in [
"generated/image_1.png",
"uploads/clip.mp4",
"album/photo.png",
"album/2026/clip.mp4",
"at-the-top.png",
] {
assert!(
resolve_local_media_path(&write(accepted), &ws, "clip")
.await
.is_ok(),
"{accepted} is inside the workspace and must be taken"
);
}
assert!(
resolve_local_media_path("album/photo.png", &ws, "clip")
.await
.is_ok()
);
for closed in ["shared/tool.ts", ".git/photo.png", ".hyperframes/state.png"] {
let err = resolve_local_media_path(&write(closed), &ws, "clip")
.await
.expect_err("a closed area is never a reference");
assert!(err.to_string().contains("closed to every tool"), "{err}");
}
let elsewhere = TempDir::new().expect("tempdir");
let outside = elsewhere.path().join("clip.mp4");
std::fs::write(&outside, b"media").unwrap();
let err = resolve_local_media_path(&outside.to_string_lossy(), &ws, "clip")
.await
.expect_err("a file outside the workspace must be refused");
assert!(
err.to_string()
.contains("must be a file inside your own workspace"),
"unexpected error: {err}"
);
}
struct SaveGeneratedCase {
name: &'static str,
nested: bool,
prefix: &'static str,
ext: &'static str,
data: &'static [u8],
}
#[tokio::test]
async fn save_generated_file_cases() {
for case in [
SaveGeneratedCase {
name: "a file is written under its prefix and extension",
nested: false,
prefix: "img",
ext: "png",
data: b"hello world",
},
SaveGeneratedCase {
name: "a missing workspace directory is created",
nested: true,
prefix: "vid",
ext: "mp4",
data: b"test content",
},
] {
let tmp = TempDir::new().expect("tempdir");
let root = if case.nested {
tmp.path().join("nested")
} else {
tmp.path().to_path_buf()
};
let ws = test_ws_named(&root.to_string_lossy(), "test");
let path = save_generated_file(&ws, case.data, case.prefix, case.ext)
.await
.unwrap_or_else(|err| panic!("[{}] save_generated_file: {err}", case.name));
assert!(
path.exists(),
"[{}] file should exist: {}",
case.name,
path.display()
);
assert!(
path.starts_with(&root),
"[{}] file should be inside the workspace",
case.name
);
let file_name = path.file_name().unwrap().to_str().unwrap();
assert!(
file_name.starts_with(&format!("{}_", case.prefix)),
"[{}] filename should start with the prefix: {file_name}",
case.name
);
assert!(
std::path::Path::new(file_name)
.extension()
.is_some_and(|ext| ext.eq_ignore_ascii_case(case.ext)),
"[{}] filename should end with .{}: {file_name}",
case.name,
case.ext
);
if !case.nested {
let content = tokio::fs::read_to_string(&path).await.unwrap();
assert_eq!(
content,
std::str::from_utf8(case.data).unwrap(),
"[{}] the bytes are written as given",
case.name
);
}
let _ = tokio::fs::remove_dir_all(tmp.path()).await;
}
}
enum BudgetScenario {
AggregateLadder,
FallsThroughToSmallerReference,
}
struct BodyBudgetCase {
name: &'static str,
scenario: BudgetScenario,
}
#[tokio::test]
async fn reference_body_budget_cases() {
for case in [
BodyBudgetCase {
name: "a body past the budget compresses its reference",
scenario: BudgetScenario::AggregateLadder,
},
BodyBudgetCase {
name: "an exhausted ladder falls through to the next reference",
scenario: BudgetScenario::FallsThroughToSmallerReference,
},
] {
match case.scenario {
BudgetScenario::AggregateLadder => {
const BUDGET: usize = 150_000;
let tmp = TempDir::new().unwrap();
let path = tmp.path().join("ref.png");
std::fs::write(&path, crate::util::test::noisy_png(384, 384)).unwrap();
let mut refs = vec![
crate::util::load_reference_image(&path, MAX_REFERENCE_IMAGE_BYTES)
.await
.unwrap(),
];
let original = refs[0].data_uri().to_string();
let mut body = serde_json::json!({
"model": "test",
"prompt": "test",
"input_references": reference_json(&refs),
});
assert!(
serde_json::to_vec(&body).unwrap().len() > BUDGET,
"[{}] the body starts past the budget",
case.name
);
fit_request_body_budget(&mut body, &mut refs, BUDGET).unwrap();
assert!(
serde_json::to_vec(&body).unwrap().len() <= BUDGET,
"[{}] the body fits the budget",
case.name
);
assert_ne!(
refs[0].data_uri(),
original,
"[{}] reference should be compressed",
case.name
);
}
BudgetScenario::FallsThroughToSmallerReference => {
let tmp = TempDir::new().unwrap();
let big = tmp.path().join("big.png");
let small = tmp.path().join("small.png");
std::fs::write(&big, crate::util::test::noisy_png(512, 512)).unwrap();
std::fs::write(&small, crate::util::test::noisy_png(128, 128)).unwrap();
let mut refs = vec![
crate::util::load_reference_image(&big, MAX_REFERENCE_IMAGE_BYTES)
.await
.unwrap(),
crate::util::load_reference_image(&small, MAX_REFERENCE_IMAGE_BYTES)
.await
.unwrap(),
];
let small_original = refs[1].data_uri().to_string();
let mut body = serde_json::json!({
"model": "test",
"prompt": "test",
"input_references": reference_json(&refs),
});
while refs[0].has_compression_left() {
refs[0].compress_more().unwrap();
}
body[INPUT_REFERENCES_KEY] = reference_json(&refs);
let raw_body = serde_json::to_vec(&body).unwrap().len();
fit_request_body_budget(&mut body, &mut refs, raw_body - 1).unwrap();
assert!(
serde_json::to_vec(&body).unwrap().len() < raw_body,
"[{}] the body shrinks",
case.name
);
assert_ne!(
refs[1].data_uri(),
small_original,
"[{}] smaller reference should have been compressed",
case.name
);
}
}
}
}
}