Skip to content

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

fix(threads): handle missing turn state in mirror observer

Steven Enamakelbd84a242d217
diff --git a/crates/openhuman-core/src/integrations/composio/tools.rs b/crates/openhuman-core/src/integrations/composio/tools.rs
index 2081fe9e21..dbae37627f 100644
--- a/crates/openhuman-core/src/integrations/composio/tools.rs
+++ b/crates/openhuman-core/src/integrations/composio/tools.rs
@@ -26,11 +26,41 @@
mod direct;
+mod authorize;
+mod connect;
+mod execute;
+mod list_connections;
+mod list_toolkits;
+mod list_tools;
+mod registry;
+mod visibility;
+
// ── Tests ───────────────────────────────────────────────────────────
#[cfg(test)]
#[path = "tools_tests.rs"]
mod tests;
-include!("tools_part_01.rs");
-include!("tools_part_02.rs");
-include!("tools_part_03.rs");
+
+pub use direct::{ComposioAction, ComposioConnectedAccount, ComposioTool};
+pub use registry::all_composio_agent_tools;
+
+// Brought into this module's own namespace (private `use`, not `pub use`)
+// so `tools_tests.rs` — declared as a direct child module of `tools` above
+// — can still reach these via a plain `use super::*;`, exactly as when
+// this was one un-split file. See each item's `pub(super)` in its owning
+// submodule.
+pub(super) use visibility::{action_mutates_external_state, resolve_action_scope};
+use authorize::ComposioAuthorizeTool;
+use connect::{
+ canonicalize_toolkit_slug, composio_connect_timeout, connection_is_active,
+ parse_composio_connect_timeout, ComposioConnectTool, DEFAULT_COMPOSIO_CONNECT_TIMEOUT_SECS,
+};
+use execute::ComposioExecuteTool;
+use list_connections::ComposioListConnectionsTool;
+use list_tools::ComposioListToolsTool;
+use list_toolkits::ComposioListToolkitsTool;
+use visibility::{
+ empty_uncurated_toolkits_message, evaluate_tool_visibility, normalized_scope_toolkits,
+ render_tools_markdown, retain_connected_tools, scope_error_message, uncatalogued_toolkits,
+ ToolDecision,
+};
diff --git a/crates/openhuman-core/src/threads/turn_state/mirror.rs b/crates/openhuman-core/src/threads/turn_state/mirror.rs
index 1641c0115e..6eec593d37 100644
--- a/crates/openhuman-core/src/threads/turn_state/mirror.rs
+++ b/crates/openhuman-core/src/threads/turn_state/mirror.rs
@@ -17,5 +17,10 @@
#[cfg(test)]
#[path = "mirror_tests.rs"]
mod tests;
-include!("mirror_part_01.rs");
-include!("mirror_part_02.rs");
+
+mod caps;
+mod lifecycle;
+mod observe;
+mod state;
+
+pub use state::TurnStateMirror;
diff --git a/crates/openhuman-core/src/threads/turn_state/mirror/observe.rs b/crates/openhuman-core/src/threads/turn_state/mirror/observe.rs
new file mode 100644
index 0000000000..e9a330f4ce
--- /dev/null
+++ b/crates/openhuman-core/src/threads/turn_state/mirror/observe.rs
@@ -0,0 +1,567 @@
+//! Translating one [`AgentProgress`] event into a [`TurnState`] mutation,
+//! plus the transcript/tool-timeline bookkeeping helpers `observe` relies on.
+
+use crate::agent::progress::AgentProgress;
+
+use super::caps::{append_capped_transcript_text, cap_persisted_args, cap_persisted_output};
+use super::state::TurnStateMirror;
+use crate::threads::turn_state::types::{
+ PersistedToolFailure, SubagentActivity, SubagentToolCall, SubagentTranscriptItem,
+ ToolTimelineEntry, ToolTimelineStatus, TranscriptItem, TurnLifecycle, TurnPhase,
+};
+
+impl TurnStateMirror {
+ /// Apply one progress event to the in-memory snapshot. Returns `true`
+ /// if the event triggered a disk flush.
+ pub fn observe(&mut self, event: &AgentProgress) -> bool {
+ self.state.updated_at = chrono::Utc::now().to_rfc3339();
+ match event {
+ AgentProgress::TurnStarted => {
+ self.state.lifecycle = TurnLifecycle::Started;
+ self.flush();
+ true
+ }
+ AgentProgress::IterationStarted {
+ iteration,
+ max_iterations,
+ } => {
+ self.state.iteration = *iteration;
+ self.state.max_iterations = *max_iterations;
+ self.state.phase = Some(TurnPhase::Thinking);
+ self.state.lifecycle = TurnLifecycle::Streaming;
+ self.state.active_tool = None;
+ self.flush();
+ true
+ }
+ AgentProgress::ToolCallStarted {
+ call_id,
+ tool_name,
+ iteration,
+ display_label,
+ display_detail,
+ ..
+ } => {
+ self.state.lifecycle = TurnLifecycle::Streaming;
+ self.state.phase = Some(TurnPhase::ToolUse);
+ self.state.active_tool = Some(tool_name.clone());
+ // Record the tool row in the ordered transcript so the
+ // processing panel can interleave it between narration /
+ // thinking at the position it actually occurred.
+ self.push_transcript_tool(*iteration, call_id);
+ // `ToolCallArgsDelta` may have already created a
+ // synthetic placeholder for this `call_id` before the
+ // start event arrived. Reuse it (filling in `name` /
+ // `round`) so the timeline doesn't end up with two
+ // rows for one tool call.
+ if let Some(existing) = self
+ .state
+ .tool_timeline
+ .iter_mut()
+ .rev()
+ .find(|e| e.id == *call_id)
+ {
+ existing.name = tool_name.clone();
+ existing.round = *iteration;
+ existing.status = ToolTimelineStatus::Running;
+ // Only overwrite with a present server value so an
+ // args-delta placeholder's fields aren't clobbered to None.
+ if display_label.is_some() {
+ existing.display_name = display_label.clone();
+ }
+ if display_detail.is_some() {
+ existing.detail = display_detail.clone();
+ }
+ } else {
+ let seq = self.next_tool_seq();
+ self.state.tool_timeline.push(ToolTimelineEntry {
+ id: call_id.clone(),
+ name: tool_name.clone(),
+ round: *iteration,
+ status: ToolTimelineStatus::Running,
+ args_buffer: None,
+ display_name: display_label.clone(),
+ detail: display_detail.clone(),
+ source_tool_name: None,
+ subagent: None,
+ failure: None,
+ output: None,
+ seq: Some(seq),
+ });
+ }
+ self.flush();
+ true
+ }
+ AgentProgress::ToolCallCompleted {
+ call_id,
+ success,
+ failure,
+ output,
+ ..
+ } => {
+ if let Some(entry) = self
+ .state
+ .tool_timeline
+ .iter_mut()
+ .rev()
+ .find(|e| e.id == *call_id)
+ {
+ entry.status = if *success {
+ ToolTimelineStatus::Success
+ } else {
+ ToolTimelineStatus::Error
+ };
+ // Persist the plain-language failure so the explanation
+ // survives a thread switch / cold boot (#4459). Clear it on
+ // a (re-)success so a retried row doesn't keep stale copy.
+ entry.failure = failure.as_ref().map(PersistedToolFailure::from);
+ // Persist the (capped) result text so the rehydrated
+ // timeline can show what the tool returned, matching the
+ // live `tool_result` socket payload.
+ entry.output = cap_persisted_output(output);
+ }
+ if self.state.active_tool.is_some() {
+ self.state.active_tool = None;
+ }
+ self.state.phase = Some(TurnPhase::Thinking);
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentSpawned {
+ agent_id,
+ task_id,
+ mode,
+ dedicated_thread,
+ worker_thread_id,
+ display_name,
+ ..
+ } => {
+ self.state.phase = Some(TurnPhase::Subagent);
+ self.state.active_subagent = Some(agent_id.clone());
+ let seq = self.next_tool_seq();
+ self.state.tool_timeline.push(ToolTimelineEntry {
+ id: format!("subagent:{task_id}"),
+ name: format!("subagent:{agent_id}"),
+ round: self.state.iteration,
+ status: ToolTimelineStatus::Running,
+ args_buffer: None,
+ display_name: display_name.clone().or_else(|| Some(agent_id.clone())),
+ detail: None,
+ source_tool_name: Some("spawn_subagent".to_string()),
+ subagent: Some(SubagentActivity {
+ task_id: task_id.clone(),
+ agent_id: agent_id.clone(),
+ status: None,
+ mode: Some(mode.clone()),
+ dedicated_thread: Some(*dedicated_thread),
+ child_iteration: None,
+ child_max_iterations: None,
+ iterations: None,
+ elapsed_ms: None,
+ output_chars: None,
+ worker_thread_id: worker_thread_id.clone(),
+ tool_calls: Vec::new(),
+ transcript: Vec::new(),
+ }),
+ failure: None,
+ output: None,
+ seq: Some(seq),
+ });
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentCompleted {
+ task_id,
+ elapsed_ms,
+ iterations,
+ output_chars,
+ ..
+ } => {
+ if let Some(entry) = self.find_subagent_entry_mut(task_id) {
+ entry.status = ToolTimelineStatus::Success;
+ if let Some(activity) = entry.subagent.as_mut() {
+ activity.elapsed_ms = Some(*elapsed_ms);
+ activity.iterations = Some(*iterations);
+ activity.output_chars = Some(*output_chars);
+ }
+ }
+ self.state.active_subagent = None;
+ self.state.phase = Some(TurnPhase::Thinking);
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentFailed { task_id, .. } => {
+ if let Some(entry) = self.find_subagent_entry_mut(task_id) {
+ entry.status = ToolTimelineStatus::Error;
+ }
+ self.state.active_subagent = None;
+ self.state.phase = Some(TurnPhase::Thinking);
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentAwaitingUser { task_id, .. } => {
+ if let Some(entry) = self.find_subagent_entry_mut(task_id) {
+ if let Some(activity) = entry.subagent.as_mut() {
+ activity.status = Some("awaiting_user".to_string());
+ }
+ }
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentIterationStarted {
+ task_id,
+ iteration,
+ max_iterations,
+ ..
+ } => {
+ if let Some(entry) = self.find_subagent_entry_mut(task_id) {
+ if let Some(activity) = entry.subagent.as_mut() {
+ activity.child_iteration = Some(*iteration);
+ activity.child_max_iterations = Some(*max_iterations);
+ }
+ }
+ false
+ }
+ AgentProgress::SubagentToolCallStarted {
+ task_id,
+ call_id,
+ tool_name,
+ arguments,
+ iteration,
+ display_label,
+ display_detail,
+ ..
+ } => {
+ if let Some(entry) = self.find_subagent_entry_mut(task_id) {
+ if let Some(activity) = entry.subagent.as_mut() {
+ activity.tool_calls.push(SubagentToolCall {
+ call_id: call_id.clone(),
+ tool_name: tool_name.clone(),
+ status: ToolTimelineStatus::Running,
+ iteration: Some(*iteration),
+ elapsed_ms: None,
+ output_chars: None,
+ display_name: display_label.clone(),
+ detail: display_detail.clone(),
+ // The live socket event already carries these; the
+ // snapshot has to as well or a reloaded child row
+ // comes back without its input (#5987).
+ args: cap_persisted_args(arguments),
+ failure: None,
+ output: None,
+ });
+ // Mirror the call into the ordered transcript so the
+ // rehydrated thoughts interleave it at the right spot.
+ activity.transcript.push(SubagentTranscriptItem::Tool {
+ iteration: Some(*iteration),
+ call_id: call_id.clone(),
+ tool_name: tool_name.clone(),
+ status: ToolTimelineStatus::Running,
+ elapsed_ms: None,
+ output_chars: None,
+ display_name: display_label.clone(),
+ detail: display_detail.clone(),
+ });
+ }
+ }
+ // Flush at sub-agent tool boundaries so prose streamed since the
+ // last boundary reaches disk (the parent is blocked on the
+ // spawn tool, so its own flushes don't fire mid sub-agent run).
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentToolCallCompleted {
+ task_id,
+ call_id,
+ success,
+ output,
+ output_chars,
+ arguments,
+ elapsed_ms,
+ failure,
+ ..
+ } => {
+ if let Some(entry) = self.find_subagent_entry_mut(task_id) {
+ if let Some(activity) = entry.subagent.as_mut() {
+ let status = if *success {
+ ToolTimelineStatus::Success
+ } else {
+ ToolTimelineStatus::Error
+ };
+ let persisted_failure = failure.as_ref().map(PersistedToolFailure::from);
+ if let Some(call) = activity
+ .tool_calls
+ .iter_mut()
+ .rev()
+ .find(|c| c.call_id == *call_id)
+ {
+ call.status = status;
+ call.elapsed_ms = Some(*elapsed_ms);
+ call.output_chars = Some(*output_chars);
+ // Carry the child failure so a failed sub-agent row
+ // keeps its explanation across a round-trip (#4459).
+ call.failure = persisted_failure;
+ call.output = cap_persisted_output(output);
+ // Backfill the input on the tinyagents path, where
+ // the *started* event carries `Value::Null` and the
+ // captured arguments only reach us here (see
+ // `SubagentToolCallCompleted::arguments`). Without
+ // this the ordinary sub-agent tool — the common
+ // case — still rehydrates with no input (#5987).
+ // Only fills a gap: a start event that already
+ // supplied arguments stays authoritative.
+ if call.args.is_none() {
+ if let Some(arguments) = arguments {
+ call.args = cap_persisted_args(arguments);
+ }
+ }
+ }
+ // Keep the transcript's Tool item in lockstep so the
+ // rehydrated row shows the terminal status + timing.
+ if let Some(SubagentTranscriptItem::Tool {
+ status: tx_status,
+ elapsed_ms: tx_elapsed,
+ output_chars: tx_output,
+ ..
+ }) = activity
+ .transcript
+ .iter_mut()
+ .rev()
+ .find(|item| matches!(item, SubagentTranscriptItem::Tool { call_id: c, .. } if c == call_id))
+ {
+ *tx_status = status;
+ *tx_elapsed = Some(*elapsed_ms);
+ *tx_output = Some(*output_chars);
+ }
+ }
+ }
+ self.flush();
+ true
+ }
+ AgentProgress::SubagentTextDelta {
+ task_id,
+ delta,
+ iteration,
+ ..
+ } => {
+ self.push_subagent_prose(task_id, *iteration, delta, false);
+ false
+ }
+ AgentProgress::SubagentThinkingDelta {
+ task_id,
+ delta,
+ iteration,
+ ..
+ } => {
+ self.push_subagent_prose(task_id, *iteration, delta, true);
+ false
+ }
+ AgentProgress::TaskBoardUpdated { board } => {
+ self.state.task_board = Some(board.clone());
+ self.flush();
+ true
+ }
+ AgentProgress::TextDelta { delta, iteration } => {
+ self.state.streaming_text.push_str(delta);
+ self.push_transcript_narration(*iteration, delta);
+ false
+ }
+ AgentProgress::ThinkingDelta { delta, iteration } => {
+ self.state.thinking.push_str(delta);
+ self.push_transcript_thinking(*iteration, delta);
+ false
+ }
+ AgentProgress::ToolCallArgsDelta {
+ call_id,
+ tool_name,
+ delta,
+ ..
+ } => {
+ if let Some(entry) = self
+ .state
+ .tool_timeline
+ .iter_mut()
+ .rev()
+ .find(|e| e.id == *call_id)
+ {
+ let buffer = entry.args_buffer.get_or_insert_with(String::new);
+ buffer.push_str(delta);
+ } else {
+ // No matching entry yet — `ToolCallArgsDelta` may
+ // arrive before `ToolCallStarted` so synthesise a
+ // placeholder we can update once the start event lands.
+ let seq = self.next_tool_seq();
+ self.state.tool_timeline.push(ToolTimelineEntry {
+ id: call_id.clone(),
+ name: tool_name.clone(),
+ round: self.state.iteration,
+ status: ToolTimelineStatus::Running,
+ args_buffer: Some(delta.clone()),
+ display_name: None,
+ detail: None,
+ source_tool_name: None,
+ subagent: None,
+ failure: None,
+ output: None,
+ seq: Some(seq),
+ });
+ }
+ false
+ }
+ AgentProgress::TurnCompleted { .. } => {
+ self.turn_completed = true;
+ // Keep the snapshot (don't delete) so a reloaded / cold-booted
+ // client can replay this turn's processing transcript via
+ // `getTurnState`. Mark it `Completed` and quiesce the live
+ // fields so the UI renders it settled (no spinner / retry),
+ // and startup interrupted-marking leaves it alone.
+ self.state.lifecycle = TurnLifecycle::Completed;
+ self.state.phase = None;
+ self.state.active_tool = None;
+ self.state.active_subagent = None;
+ self.flush();
+ true
+ }
+ AgentProgress::TurnCostUpdated { .. } | AgentProgress::ModelCallCompleted { .. } => {
+ // Cost/usage updates don't change the turn-state snapshot
+ // shape (lifecycle / phase / active tool / etc.), so
+ // we just acknowledge them without flushing. Surfacing
+ // cost in the persisted snapshot would force a disk
+ // flush per LLM call — not worth it for telemetry.
+ false
+ }
+ AgentProgress::TurnContent { .. } => {
+ // Prompt/reply content is consumed by the tracing exporter, not
+ // the turn-state snapshot; nothing to mirror, no flush.
+ false
+ }
+ }
+ }
+
+ /// Append a visible-narration delta to the transcript, coalescing into
+ /// the trailing [`TranscriptItem::Narration`] when it's the most recent
+ /// item and from the same round — so a streamed paragraph stays one item
+ /// instead of one-per-token. A new round (or any intervening thinking /
+ /// tool item) starts a fresh narration block.
+ fn push_transcript_narration(&mut self, round: u32, delta: &str) {
+ if let Some(TranscriptItem::Narration { round: r, text, .. }) =
+ self.state.transcript.last_mut()
+ {
+ if *r == round {
+ append_capped_transcript_text(text, delta);
+ return;
+ }
+ }
+ let seq = self.next_seq();
+ let mut text = String::new();
+ append_capped_transcript_text(&mut text, delta);
+ self.state
+ .transcript
+ .push(TranscriptItem::Narration { round, seq, text });
+ }
+
+ /// Append a hidden-reasoning delta to the transcript, with the same
+ /// coalescing rule as [`Self::push_transcript_narration`].
+ fn push_transcript_thinking(&mut self, round: u32, delta: &str) {
+ if let Some(TranscriptItem::Thinking { round: r, text, .. }) =
+ self.state.transcript.last_mut()
+ {
+ if *r == round {
+ append_capped_transcript_text(text, delta);
+ return;
+ }
+ }
+ let seq = self.next_seq();
+ let mut text = String::new();
+ append_capped_transcript_text(&mut text, delta);
+ self.state
+ .transcript
+ .push(TranscriptItem::Thinking { round, seq, text });
+ }
+
+ /// Record a tool call in the transcript at the point it occurred, as a
+ /// pointer into [`TurnState::tool_timeline`] (the row's status/label live
+ /// there). Skips a duplicate if the same `call_id` was already recorded
+ /// (e.g. a start event after an args-delta placeholder).
+ fn push_transcript_tool(&mut self, round: u32, call_id: &str) {
+ let already = self.state.transcript.iter().any(
+ |item| matches!(item, TranscriptItem::ToolCall { call_id: c, .. } if c == call_id),
+ );
+ if already {
+ return;
+ }
+ let seq = self.next_seq();
+ self.state.transcript.push(TranscriptItem::ToolCall {
+ round,
+ seq,
+ call_id: call_id.to_string(),
+ });
+ }
+
+ /// Return the next monotonic transcript ordering key and advance it.
+ fn next_seq(&mut self) -> u32 {
+ let seq = self.next_seq;
+ self.next_seq = self.next_seq.saturating_add(1);
+ seq
+ }
+
+ /// Return the next monotonic tool-timeline ordering key and advance it.
+ fn next_tool_seq(&mut self) -> u64 {
+ let seq = self.next_tool_seq;
+ self.next_tool_seq = self.next_tool_seq.saturating_add(1);
+ seq
+ }
+
+ fn find_subagent_entry_mut(&mut self, task_id: &str) -> Option<&mut ToolTimelineEntry> {
+ let needle = format!("subagent:{task_id}");
+ self.state
+ .tool_timeline
+ .iter_mut()
+ .rev()
+ .find(|entry| entry.id == needle)
+ }
+
+ /// Append a sub-agent prose delta (narration when `is_thinking == false`,
+ /// reasoning otherwise) to that sub-agent's transcript, coalescing into the
+ /// trailing same-kind, same-iteration item so a streamed paragraph stays
+ /// one entry (mirrors the frontend `appendSubagentStreamDelta`). Mutate-
+ /// only (no flush) — high-frequency like the parent's `TextDelta`; the
+ /// accumulated prose is persisted at the next sub-agent tool boundary.
+ fn push_subagent_prose(&mut self, task_id: &str, iteration: u32, delta: &str, is_thinking: bool) {
+ let Some(entry) = self.find_subagent_entry_mut(task_id) else {
+ return;
+ };
+ let Some(activity) = entry.subagent.as_mut() else {
+ return;
+ };
+ match activity.transcript.last_mut() {
+ Some(SubagentTranscriptItem::Thinking {
+ iteration: it,
+ text,
+ }) if is_thinking && *it == Some(iteration) => {
+ append_capped_transcript_text(text, delta);
+ return;
+ }
+ Some(SubagentTranscriptItem::Text {
+ iteration: it,
+ text,
+ }) if !is_thinking && *it == Some(iteration) => {
+ append_capped_transcript_text(text, delta);
+ return;
+ }
+ _ => {}
+ }
+ let mut text = String::new();
+ append_capped_transcript_text(&mut text, delta);
+ activity.transcript.push(if is_thinking {
+ SubagentTranscriptItem::Thinking {
+ iteration: Some(iteration),
+ text,
+ }
+ } else {
+ SubagentTranscriptItem::Text {
+ iteration: Some(iteration),
+ text,
+ }
+ });
+ }
+}
diff --git a/crates/openhuman-core/src/web3/wallet/execution/types.rs b/crates/openhuman-core/src/web3/wallet/execution/types.rs
new file mode 100644
index 0000000000..ab24892307
--- /dev/null
+++ b/crates/openhuman-core/src/web3/wallet/execution/types.rs
@@ -0,0 +1,228 @@
+//! Wire types for the wallet execution surface: chain/asset/balance
+//! snapshots, the prepare→execute quote lifecycle, transaction lookups, and
+//! the RPC param shapes.
+
+use serde::{Deserialize, Serialize};
+
+use super::super::defaults::EvmNetwork;
+use super::super::ops::WalletChain;
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ChainStatus {
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub configured: bool,
+ pub provider_status: ProviderStatus,
+ pub rpc_url: String,
+}
+
+#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+pub enum ProviderStatus {
+ Ready,
+ Missing,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SupportedAsset {
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub symbol: String,
+ pub name: String,
+ pub native: bool,
+ pub decimals: u8,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub contract_address: Option<String>,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct BalanceInfo {
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub address: String,
+ pub asset_symbol: String,
+ pub decimals: u8,
+ pub raw: String,
+ pub formatted: String,
+ pub provider_status: ProviderStatus,
+}
+
+#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+pub enum PreparedKind {
+ NativeTransfer,
+ TokenTransfer,
+}
+
+#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+pub enum PreparedStatus {
+ AwaitingConfirmation,
+ Broadcasted,
+ Consumed,
+}
+
+/// Identity of the chat thread that prepared a quote.
+///
+/// The wallet executes prepare/execute as a two-step flow keyed by `quote_id`.
+/// `quote_id`s are visible in the shared chat broadcast (the prepared-tx
+/// summary that gets sent back into the channel), so a co-channel caller can
+/// read another caller's `quote_id` and try to drive its execute from their
+/// own (now per-sender-isolated, post-#2331) agent session. Binding the
+/// quote to the originating chat thread closes that gap: execute is only
+/// allowed when the caller's `current_owner()` equals the prepare-time owner.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(crate) struct QuoteOwner {
+ pub(crate) thread_id: String,
+ pub(crate) client_id: String,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PreparedTransaction {
+ pub quote_id: String,
+ pub kind: PreparedKind,
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub from_address: String,
+ pub to_address: String,
+ pub asset_symbol: String,
+ pub amount_raw: String,
+ pub amount_formatted: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub receive_symbol: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub min_receive_raw: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub calldata: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub token_address: Option<String>,
+ pub estimated_fee_raw: String,
+ pub status: PreparedStatus,
+ pub created_at_ms: u64,
+ pub expires_at_ms: u64,
+ pub notes: Vec<String>,
+ /// Chat-thread owner stamped at prepare time. Present when the quote
+ /// was prepared from inside an interactive chat turn (web channel sets
+ /// `APPROVAL_CHAT_CONTEXT`); `None` for CLI / direct-RPC / background
+ /// callers. Internal gate data — never serialized over the wire.
+ #[serde(skip_serializing)]
+ pub(crate) owner: Option<QuoteOwner>,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ExecutionResult {
+ pub quote_id: String,
+ pub status: PreparedStatus,
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub transaction_hash: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub explorer_url: Option<String>,
+ pub transaction: PreparedTransaction,
+}
+
+/// Result of a low-level "sign this unsigned transaction and broadcast it"
+/// primitive. Unlike [`ExecutionResult`], this carries no `PreparedTransaction`
+/// — it is the minimal output the `web3` layer needs after handing the wallet
+/// an externally-built (e.g. deBridge) unsigned transaction to sign+broadcast.
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub(crate) struct RawBroadcastResult {
+ pub transaction_hash: String,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub explorer_url: Option<String>,
+ /// Simulated fee in the chain's smallest unit. `None` when the fee is not
+ /// known at broadcast time (e.g. Solana's dynamic base+priority fee, which
+ /// must be read back from the confirmed transaction).
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub fee_raw: Option<String>,
+}
+
+/// Normalized lifecycle state of a broadcast transaction.
+#[derive(Debug, Clone, Copy, Serialize, PartialEq, Eq)]
+#[serde(rename_all = "snake_case")]
+pub enum TxState {
+ /// Seen by the node but not yet included in a block.
+ Pending,
+ /// Included in a block and succeeded.
+ Confirmed,
+ /// Included in a block but reverted/failed.
+ Failed,
+ /// The node has no record of this hash.
+ NotFound,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct TxStatusInfo {
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub hash: String,
+ pub state: TxState,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub confirmations: Option<u64>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub block_number: Option<u64>,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct TxReceiptInfo {
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub hash: String,
+ pub found: bool,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub success: Option<bool>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub block_number: Option<u64>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub gas_used: Option<String>,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub fee_raw: Option<String>,
+ /// Raw provider receipt payload, passed through unchanged.
+ pub raw: serde_json::Value,
+}
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(rename_all = "camelCase")]
+pub struct TxLookupInfo {
+ pub chain: WalletChain,
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub evm_network: Option<EvmNetwork>,
+ pub hash: String,
+ pub found: bool,
+ /// Raw provider transaction payload, passed through unchanged.
+ pub raw: serde_json::Value,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct PrepareTransferParams {
+ pub chain: WalletChain,
+ pub to_address: String,
+ pub amount_raw: String,
+ #[serde(default)]
+ pub asset_symbol: Option<String>,
+ #[serde(default)]
+ pub evm_network: Option<EvmNetwork>,
+}
+
+#[derive(Debug, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct ExecutePreparedParams {
+ pub quote_id: String,
+ pub confirmed: bool,
+}