use std::sync::{Arc, LazyLock, OnceLock};
use durable::{
ConversationId, ConversationOwnership, EntryDraft, EntryId, SubmissionId, SubmissionStatus, TaskDefinition, TaskId,
TaskOptions, TaskOutcome, TaskOutcomeError, TaskOwnership, TaskRecord, TaskState, TaskStatus, Tx,
};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::submissions::{Submissions, place_initial_input};
use super::util::{from_value, to_value};
use super::{AgentChange, ModelRef, Services, configure, create_documents};
use crate::scheduler::{Cancel, Ctx, HarnessError, NextState, Result, Task, TaskRuntime};
pub const CHILD_KIND: &str = "ultron.child";
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChildInput {
pub name: String,
pub prompt: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<ModelRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub timeout_ms: Option<u64>,
#[serde(default)]
pub keep: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub thinking: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cwd: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub env: Vec<(String, String)>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub agent: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub usage_root: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fork: Option<ChildFork>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub history: Vec<Value>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ChildFork {
pub conversation: ConversationId,
pub entry: EntryId,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "phase", rename_all = "camelCase", rename_all_fields = "camelCase")]
pub(crate) enum ChildCheckpoint {
Start,
Wait {
conversation: ConversationId,
submission: SubmissionId,
#[serde(default, skip_serializing_if = "Option::is_none")]
until: Option<u64>,
},
Idle {
conversation: ConversationId,
result: Value,
},
}
static DEFINITION: LazyLock<TaskDefinition> =
LazyLock::new(|| TaskDefinition::new(CHILD_KIND, 1, |_| to_value(&ChildCheckpoint::Start)));
#[derive(Default)]
pub(crate) struct ChildServices {
pub(crate) submissions: OnceLock<Arc<Submissions>>,
}
fn result(status: &str, value: Option<Value>, error: Option<String>) -> Value {
let mut result = json!({ "status": status, "verification": "unverified" });
if let Some(value) = value {
result["value"] = value;
}
if let Some(error) = error {
result["error"] = json!(error);
}
result
}
fn checkpoint_of(task: &TaskRecord) -> Result<ChildCheckpoint> {
match &task.state {
TaskState::Pending { checkpoint }
| TaskState::Running { checkpoint }
| TaskState::Waiting { checkpoint, .. } => from_value(checkpoint.clone(), "child checkpoint"),
_ => Err(HarnessError::msg("A terminal child has no checkpoint")),
}
}
pub(crate) fn child_task(services: Arc<Services>) -> Task {
let (start, wait) = (services.clone(), services);
Task::define(CHILD_KIND, 1, |input| (DEFINITION.initial)(input))
.phase("start", move |task, runtime| start_phase(&start, task, runtime))
.phase("wait", move |task, runtime| wait_phase(&wait, task, runtime))
.phase("idle", |_, runtime| Err(runtime.signal().wait()))
.abort(|task, runtime| {
let conversation = match checkpoint_of(task)? {
ChildCheckpoint::Wait { conversation, .. } | ChildCheckpoint::Idle { conversation, .. } => {
Some(conversation.0)
}
ChildCheckpoint::Start => None,
};
let reason = if task.abort_reason.is_some() {
"Interrupted by a restart"
} else {
"Cancelled"
};
runtime.commit(&runtime.ctx(), |_, _| {
Ok(Some(NextState::Terminal {
outcome: TaskOutcome::Aborted {
reason: None,
result: Some(json!({
"result": result("cancelled", None, Some(reason.into())),
"conversation": conversation,
})),
},
}))
})
})
.build()
}
fn start_phase(_services: &Services, task: &TaskRecord, runtime: &TaskRuntime) -> Result<()> {
let input: ChildInput = from_value(task.input.clone(), "child input")?;
let model = match input.model.clone() {
Some(model) => Some(model),
None => super::generation::read_agent(runtime)?.model,
};
let task_id = runtime.task_id();
let now = runtime.now()?;
runtime.commit(&runtime.ctx(), |tx, _| {
let conversation = match input.fork {
Some(fork) => {
let id = tx
.fork_conversation(fork.conversation, fork.entry, ConversationOwnership::Task(task_id))?
.id;
create_documents(tx, id, true)?;
id
}
None => {
let id = tx.create_conversation(ConversationOwnership::Task(task_id))?.id;
create_documents(tx, id, false)?;
id
}
};
if let Some(model) = &model {
configure(tx, conversation, &AgentChange::model(model.clone()))?;
}
if let Some(level) = &input.thinking {
let change = AgentChange {
model: None,
thinking_level: Some(Some(level.clone())),
};
configure(tx, conversation, &change)?;
}
for message in &input.history {
let kind = match message["role"].as_str() {
Some("assistant") => super::entries::ASSISTANT,
Some("toolResult") => super::entries::TOOL_RESULT,
_ => super::entries::USER,
};
tx.append_entry(
conversation,
EntryDraft {
model: Some(vec![message.clone()]),
..EntryDraft::new(kind)
},
)?;
}
let submission = place_initial_input(tx, conversation, &json!(input.prompt), now)?;
let mut brief = input.prompt.clone();
if brief.chars().count() > 200 {
brief = brief.chars().take(200).collect::<String>() + "โฆ";
}
let work = serde_json::Map::from_iter([("work".to_owned(), json!(brief))]);
super::swarm::register(
tx,
&input.name,
Some(conversation),
Some(task_id.0.to_string()),
&work,
now,
)?;
Ok(Some(NextState::Running {
checkpoint: to_value(&ChildCheckpoint::Wait {
conversation,
submission,
until: input.timeout_ms.map(|timeout| now + timeout),
}),
}))
})
}
fn answer_text(tx: &mut Tx<'_>, answer: durable::EntryId) -> Result<Value> {
let Some(entry) = tx.entry(answer)? else {
return Ok(Value::Null);
};
let message = entry
.model
.and_then(|model| model.into_iter().next())
.unwrap_or(Value::Null);
let text: Vec<String> = message["content"]
.as_array()
.into_iter()
.flatten()
.filter(|block| block["type"] == "text")
.filter_map(|block| block["text"].as_str().map(str::to_owned))
.collect();
Ok(json!(text.join("\n")))
}
fn wait_phase(services: &Services, task: &TaskRecord, runtime: &TaskRuntime) -> Result<()> {
let ChildCheckpoint::Wait {
conversation,
submission,
until,
} = checkpoint_of(task)?
else {
return Err(HarnessError::msg("wait phase without a submission"));
};
let input: ChildInput = from_value(task.input.clone(), "child input")?;
let submissions = services
.children
.submissions
.get()
.cloned()
.ok_or_else(|| HarnessError::msg("Child tasks need an open ChatHarness"))?;
let deadline = Cancel::new();
if let Some(until) = until {
let (timer, sleeper, done) = (deadline.clone(), runtime.clone(), runtime.signal().clone());
std::thread::spawn(move || {
if sleeper.sleep(until, &Ctx::with_signal(&done)).is_ok() {
timer.cancel(HarnessError::msg("deadline"));
}
});
}
let linked = Cancel::any(&[runtime.signal(), &deadline]);
let ctx = Ctx::with_signal(linked.signal());
let mut settled = submissions.wait(submission, &ctx);
let mut waited = submission;
while let Ok(record) = &settled
&& record.status == SubmissionStatus::Done
{
let live = runtime.snapshot(&super::docs::LIVE, super::util::owner(conversation))?;
let next = live
.as_ref()
.and_then(|live| live["run"]["inputs"].as_array()?.last()?.as_u64())
.map(SubmissionId)
.filter(|next| *next != waited);
let Some(next) = next else { break };
waited = next;
settled = submissions.wait(next, &ctx);
}
runtime.signal().check()?;
let settled = match settled {
Ok(settled) => settled,
Err(_) if deadline.is_cancelled() => {
let timeout = input.timeout_ms.unwrap_or_default();
let error = format!("Subagent {} timed out after {timeout} ms", input.name);
return runtime.commit(&runtime.ctx(), |_, _| {
Ok(Some(NextState::Terminal {
outcome: TaskOutcome::Failed {
error: TaskOutcomeError {
message: error.clone(),
detail: Some(json!({ "reason": "timeout" })),
},
result: Some(json!({
"result": result("cancelled", None, Some(error.clone())),
"conversation": conversation.0,
})),
},
}))
});
}
Err(error) => return Err(error),
};
runtime.commit(&runtime.ctx(), |tx, _| {
let child_result = match (settled.status, settled.answer) {
(SubmissionStatus::Done, Some(answer)) => result("succeeded", Some(answer_text(tx, answer)?), None),
_ => result(
"failed",
None,
Some(format!(
"Subagent {} ended without an answer: {}",
input.name,
settled
.detail
.as_ref()
.and_then(Value::as_str)
.or(settled.reason.as_deref())
.unwrap_or("unanswered")
)),
),
};
if input.keep {
return Ok(Some(NextState::Running {
checkpoint: to_value(&ChildCheckpoint::Idle {
conversation,
result: child_result,
}),
}));
}
let receipt = json!({ "result": child_result, "conversation": conversation.0 });
Ok(Some(NextState::Terminal {
outcome: TaskOutcome::Completed { result: receipt },
}))
})
}
pub fn spawn_child(
tx: &mut Tx<'_>,
conversation: ConversationId,
cell: Option<TaskId>,
input: &ChildInput,
) -> Result<TaskId> {
let options = match (input.keep, cell) {
(false, Some(cell)) => TaskOptions {
ownership: TaskOwnership::Task(cell),
..TaskOptions::default()
},
_ => TaskOptions {
ownership: TaskOwnership::Conversation,
conversation_id: Some(conversation),
background: true,
..TaskOptions::default()
},
};
Ok(tx.create_task(&DEFINITION, to_value(input), options)?)
}
pub fn child_report(record: &TaskRecord) -> Option<Value> {
let id = record.id.0.to_string();
let receipt = match &record.state {
TaskState::Terminal { outcome } => match outcome {
TaskOutcome::Completed { result } => result.clone(),
TaskOutcome::Failed {
result: Some(result), ..
}
| TaskOutcome::Aborted {
result: Some(result), ..
} => result.clone(),
TaskOutcome::Failed { error, .. } => {
json!({ "result": result("failed", None, Some(error.message.clone())) })
}
TaskOutcome::Aborted { .. } => json!({ "result": result("cancelled", None, None) }),
TaskOutcome::Faulted { error } => {
json!({ "result": result("failed", None, Some(error.message.clone())) })
}
TaskOutcome::Orphaned { reason } => json!({ "result": result("interrupted", None, Some(reason.clone())) }),
},
TaskState::Running { checkpoint } | TaskState::Pending { checkpoint } if checkpoint["phase"] == "idle" => {
json!({ "result": checkpoint["result"], "conversation": checkpoint["conversation"] })
}
_ => return None,
};
let mut report = json!({ "id": id, "result": receipt["result"] });
if !receipt["conversation"].is_null() {
report["conversation"] = receipt["conversation"].clone();
}
Some(report)
}
pub fn child_done(record: &TaskRecord) -> bool {
record.state.status() == TaskStatus::Terminal || child_report(record).is_some()
}