Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(web_chat): centralize subagent ledger writes in progress ledger

Steven Enamakel0f94b9462240
diff --git a/crates/openhuman-core/src/web_chat/progress.rs b/crates/openhuman-core/src/web_chat/progress.rs
index 3fe118407c..3d58f4103f 100644
--- a/crates/openhuman-core/src/web_chat/progress.rs
+++ b/crates/openhuman-core/src/web_chat/progress.rs
@@ -11,7 +11,6 @@
use std::time::Instant;
-use serde_json::json;
use tinyagents_session::port::TurnStates;
use tinyagents_session::turn_state::TurnStateMirror;
use tinychannels_bus::web::{
@@ -78,7 +77,6 @@ pub(crate) struct ProgressProjection {
pending_narration: String,
timing: TurnTimingState,
cost_throttle: TurnCostThrottle,
- events_seen: u64,
ledger: Ledger,
turn_state: Option<TurnStateMirror>,
span_collector: Option<SpanCollector>,
@@ -115,6 +113,12 @@ impl ProgressProjection {
&ids.request_id,
)
.unzip();
+ let ledger = for_turn(
+ &config.workspace_dir,
+ &ids.client_id,
+ &ids.thread_id,
+ &ids.request_id,
+ );
Self {
surface: Surface::new(ctx, ids.clone(), config.clone()).await,
turn_state: Some(TurnStateMirror::new(
@@ -132,12 +136,7 @@ impl ProgressProjection {
pending_narration: String::new(),
timing: TurnTimingState::default(),
cost_throttle: TurnCostThrottle::default(),
- ledger: for_turn(
- &config.workspace_dir,
- &ids.client_id,
- &ids.thread_id,
- &ids.request_id,
- ),
+ ledger,
span_collector,
journal_trace_ctx,
timing_snapshot: None,
@@ -189,13 +188,23 @@ impl ProgressProjection {
/// Project one event of the turn, in order.
pub(crate) async fn on_event(&mut self, event: AgentProgress) {
- let ledger_context = self.ledger.observe(&event).await;
if let Some(mirror) = self.turn_state.as_mut() {
mirror.observe(&event);
}
if let Some(collector) = self.span_collector.as_mut() {
collector.record(&event, unix_epoch_ms());
}
+ if let AgentProgress::ToolCallStarted { iteration, .. } = &event {
+ if self.timing.tool_call(self.elapsed_ms()) {
+ log::info!(
+ "[web_channel][bridge] time-to-first-visible kind=tool_call first_tool_ms={} round={iteration} request_id={}",
+ self.elapsed_ms(),
+ self.ids.request_id
+ );
+ }
+ self.flush_interim_narration(*iteration).await;
+ }
+ let ledger_context = self.ledger.observe(&event).await;
match event {
AgentProgress::TurnStarted => {
self.turn_active = true;
@@ -219,17 +228,6 @@ impl ProgressProjection {
display_label,
display_detail,
} => {
- if self.timing.tool_call(self.elapsed_ms()) {
- log::info!(
- "[web_channel][bridge] time-to-first-visible kind=tool_call first_tool_ms={} round={iteration} tool={tool_name} request_id={}",
- self.elapsed_ms(),
- self.ids.request_id
- );
- }
- // The parent's leading narration for this round is complete
- // once it calls a tool: persist it interleaved with the tool
- // activity.
- self.flush_interim_narration(iteration).await;
log::debug!(
"[web_channel][bridge] tool_call round={iteration} tool={tool_name} call_id={call_id} request_id={}",
self.ids.request_id
diff --git a/crates/openhuman-core/src/web_chat/progress_subagent.rs b/crates/openhuman-core/src/web_chat/progress_subagent.rs
index 5a94f90895..018aae1575 100644
--- a/crates/openhuman-core/src/web_chat/progress_subagent.rs
+++ b/crates/openhuman-core/src/web_chat/progress_subagent.rs
@@ -1,16 +1,18 @@
//! The sub-agent arms of [`ProgressProjection::on_event`]: lifecycle, child
//! tool calls and child deltas, each mirrored into the run ledger.
-use serde_json::json;
-
use tinyagents_harness::progress::AgentProgress;
-use super::super::progress_ledger::ChildDone;
+use super::super::progress_ledger::ProgressLedgerContext;
use super::super::progress_map as map;
use super::ProgressProjection;
impl ProgressProjection {
- pub(super) async fn on_subagent_event(&mut self, event: AgentProgress) {
+ pub(super) async fn on_subagent_event(
+ &mut self,
+ event: AgentProgress,
+ ledger_context: ProgressLedgerContext,
+ ) {
let round = self.round;
match event {
AgentProgress::SubagentSpawned {
@@ -21,25 +23,9 @@ impl ProgressProjection {
prompt_chars,
worker_thread_id,
display_name,
- parent_call_id,
+ parent_call_id: _,
..
} => {
- self.subagent_parent_call_ids
- .insert(task_id.clone(), parent_call_id.clone());
- if let Some(ledger) = self.ledger() {
- ledger
- .child_spawned(
- &agent_id,
- &task_id,
- &mode,
- dedicated_thread,
- prompt_chars,
- &worker_thread_id,
- &display_name,
- &parent_call_id,
- )
- .await;
- }
self.surface
.output(map::sub_spawned(
&agent_id,
@@ -50,7 +36,7 @@ impl ProgressProjection {
prompt_chars,
worker_thread_id,
display_name,
- parent_call_id,
+ ledger_context.parent_call_id,
))
.await;
}
@@ -67,23 +53,6 @@ impl ProgressProjection {
dirty_status,
..
} => {
- let parent_call_id = self.subagent_parent_call_ids.remove(&task_id).flatten();
- if let Some(ledger) = self.ledger() {
- ledger
- .child_completed(&ChildDone {
- agent_id: &agent_id,
- task_id: &task_id,
- elapsed_ms,
- iterations,
- output_chars,
- worktree_path: &worktree_path,
- changed_files: &changed_files,
- dirty_status,
- parent_call_id: &parent_call_id,
- tool_count: self.child_tool_counts.get(&task_id).copied(),
- })
- .await;
- }
self.surface
.output(map::sub_completed(
&agent_id,
@@ -96,7 +65,7 @@ impl ProgressProjection {
worktree_path,
changed_files,
dirty_status,
- parent_call_id,
+ ledger_context.parent_call_id,
))
.await;
}
@@ -111,25 +80,13 @@ impl ProgressProjection {
self.ids.thread_id,
self.ids.request_id
);
- let parent_call_id = self.subagent_parent_call_ids.remove(&task_id).flatten();
- if let Some(ledger) = self.ledger() {
- ledger
- .child_failed(
- &agent_id,
- &task_id,
- &error,
- &parent_call_id,
- self.child_tool_counts.get(&task_id).copied(),
- )
- .await;
- }
self.surface
.output(map::sub_failed(
&agent_id,
&task_id,
round,
error,
- parent_call_id,
+ ledger_context.parent_call_id,
))
.await;
}
@@ -140,23 +97,6 @@ impl ProgressProjection {
worker_thread_id,
checkpoint_path,
} => {
- let parent_call_id = self
- .subagent_parent_call_ids
- .get(&task_id)
- .cloned()
- .flatten();
- if let Some(ledger) = self.ledger() {
- ledger
- .child_awaiting_user(
- &agent_id,
- &task_id,
- &question,
- &worker_thread_id,
- &checkpoint_path,
- &parent_call_id,
- )
- .await;
- }
self.surface
.output(map::sub_awaiting_user(
&agent_id,
@@ -164,7 +104,7 @@ impl ProgressProjection {
round,
question,
worker_thread_id,
- parent_call_id,
+ ledger_context.parent_call_id,
))
.await;
}
@@ -195,16 +135,6 @@ impl ProgressProjection {
display_label,
display_detail,
} => {
- let count = self.child_tool_counts.entry(task_id.clone()).or_insert(0);
- *count += 1;
- let count = *count;
- if let Some(ledger) = self.ledger() {
- ledger
- .child_tool_started(
- &agent_id, &task_id, &call_id, &tool_name, iteration, count,
- )
- .await;
- }
self.surface
.output(map::sub_tool_started(
&agent_id,
@@ -219,37 +149,6 @@ impl ProgressProjection {
.await;
}
AgentProgress::SubagentToolCallCompleted { .. } => {
- if let AgentProgress::SubagentToolCallCompleted {
- agent_id,
- task_id,
- call_id,
- tool_name,
- success,
- output_chars,
- elapsed_ms,
- iteration,
- failure,
- ..
- } = &event
- {
- if let Some(ledger) = self.ledger() {
- ledger
- .child_tool_completed(
- task_id,
- json!({
- "agentId": agent_id,
- "callId": call_id,
- "toolName": tool_name,
- "success": success,
- "outputChars": output_chars,
- "elapsedMs": elapsed_ms,
- "iteration": iteration,
- "failure": failure.as_ref().and_then(|f| serde_json::to_value(f).ok()),
- }),
- )
- .await;
- }
- }
if let Some(output) = map::sub_tool_completed(event) {
self.surface.output(output).await;
}