Skip to content

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

chore: files changed crates/openhuman-core/src/channels/bus_part_01.rs,crates/openhuman-core/src/cha

Steven Enamakel0dfcc8fcdd79
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs
new file mode 100644
index 0000000000..ebf89e06c5
--- /dev/null
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/steering.rs
@@ -0,0 +1,275 @@
+//! Injecting messages and crate-native control-flow directives into a running
+//! sub-agent: the `RunQueue` compatibility lane, and the TinyAgents
+//! `SteeringHandle` fast path for runs that have registered one.
+
+use crate::agent::harness::run_queue::{QueueMode, QueuedMessage};
+use crate::agent::tinyagents::orchestration::{
+ DetachedTaskRegistryError, SteeringCommand, SteeringCommandKind,
+};
+use tinyagents_harness::ids::TaskId;
+use tinyinference::message::Message as TaMessage;
+
+use super::cancel::now_ms;
+use super::registry::registry;
+
+/// Why a steer could not be delivered.
+#[derive(Debug, PartialEq, Eq)]
+pub enum SteerError {
+ /// No such sub-agent — never existed, or already finished and pruned.
+ Unknown,
+ /// The caller's `parent_session` does not own this sub-agent.
+ NotOwned,
+ /// The sub-agent already reached a terminal status.
+ AlreadyDone,
+}
+
+pub(crate) fn steer_error_from_registry(error: DetachedTaskRegistryError) -> SteerError {
+ match error {
+ DetachedTaskRegistryError::NotOwned => SteerError::NotOwned,
+ DetachedTaskRegistryError::AlreadyDone => SteerError::AlreadyDone,
+ _ => SteerError::Unknown,
+ }
+}
+
+fn steering_command_for_mode(mode: QueueMode, text: String) -> Option<SteeringCommand> {
+ match mode {
+ QueueMode::Steer => Some(SteeringCommand::InjectMessage(TaMessage::user(format!(
+ "[User steering message]: {text}"
+ )))),
+ QueueMode::Collect => Some(SteeringCommand::InjectMessage(TaMessage::user(format!(
+ "[Additional context from user]: {text}"
+ )))),
+ QueueMode::Interrupt | QueueMode::Followup | QueueMode::Parallel => None,
+ }
+}
+
+fn send_registered_steering(
+ handle: &tinyagents_harness::steering::SteeringHandle,
+ text: String,
+ mode: QueueMode,
+) -> bool {
+ let Some(command) = steering_command_for_mode(mode, text) else {
+ return false;
+ };
+ handle.send(command);
+ true
+}
+
+/// Crate-native steering directives beyond the `InjectMessage`/collect lanes.
+///
+/// These map 1:1 onto the tinyagents [`SteeringCommand`] control variants that
+/// the crate exposes (`Redirect`, `Pause`, `Resume`, `Cancel`). They are
+/// delivered **only** through a registered `SteeringHandle` and therefore land
+/// only at a safe loop boundary (the crate drains before each model call) —
+/// never mid-stream, and never through the `RunQueue` fallback (which has no
+/// equivalent lane). Approval/security is never bypassed: `Redirect` lowers to a
+/// system instruction the normal approval-gated loop still governs, and
+/// `Pause`/`Resume`/`Cancel` are pure control-flow.
+///
+/// The crate's `SetMetadata` command is intentionally *not* mapped here: no
+/// OpenHuman control surface owns run-metadata mutation yet.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub(crate) enum SteeringDirective {
+ /// Redirect the run toward a new instruction (`SteeringCommand::Redirect`).
+ Redirect(String),
+ /// Cooperatively pause at the next checkpoint (`SteeringCommand::Pause`).
+ Pause,
+ /// Clear a pending pause (`SteeringCommand::Resume`).
+ Resume,
+ /// Cooperatively terminate at the next checkpoint (`SteeringCommand::Cancel`) —
+ /// a graceful, safe-boundary alternative to the hard `AbortHandle` cancel.
+ Cancel,
+}
+
+impl SteeringDirective {
+ fn kind(&self) -> SteeringCommandKind {
+ match self {
+ SteeringDirective::Redirect(_) => SteeringCommandKind::Redirect,
+ SteeringDirective::Pause => SteeringCommandKind::Pause,
+ SteeringDirective::Resume => SteeringCommandKind::Resume,
+ SteeringDirective::Cancel => SteeringCommandKind::Cancel,
+ }
+ }
+
+ fn into_command(self) -> SteeringCommand {
+ match self {
+ SteeringDirective::Redirect(instruction) => SteeringCommand::Redirect { instruction },
+ SteeringDirective::Pause => SteeringCommand::Pause,
+ SteeringDirective::Resume => SteeringCommand::Resume,
+ SteeringDirective::Cancel => SteeringCommand::Cancel,
+ }
+ }
+}
+
+/// Why a crate-native steering directive could not be delivered.
+#[derive(Debug, PartialEq, Eq)]
+pub(crate) enum SteerDirectiveError {
+ /// No such sub-agent — never existed, or already finished and pruned.
+ Unknown,
+ /// The caller's `parent_session` does not own this sub-agent.
+ NotOwned,
+ /// The sub-agent already reached a terminal status.
+ AlreadyDone,
+ /// The sub-agent has no live crate-native `SteeringHandle` registered
+ /// (e.g. a legacy `RunQueue`-only run), so control-flow steering that has no
+ /// `RunQueue` lane cannot be delivered.
+ NoRegisteredHandle,
+ /// The run's `SteeringPolicy` does not permit this directive's command
+ /// kind. Enqueuing it anyway would abort the run with
+ /// `TinyAgentsError::Steering`, so we refuse up front.
+ PolicyRejected,
+}
+
+pub(crate) fn steer_directive_error_from_registry(
+ error: DetachedTaskRegistryError,
+) -> SteerDirectiveError {
+ match error {
+ DetachedTaskRegistryError::NotOwned => SteerDirectiveError::NotOwned,
+ DetachedTaskRegistryError::AlreadyDone => SteerDirectiveError::AlreadyDone,
+ DetachedTaskRegistryError::NoSteeringHandle => SteerDirectiveError::NoRegisteredHandle,
+ _ => SteerDirectiveError::Unknown,
+ }
+}
+
+/// Deliver a crate-native control-flow [`SteeringDirective`] to a running
+/// sub-agent through its registered TinyAgents `SteeringHandle`.
+///
+/// Unlike [`steer`], this has **no** `RunQueue` fallback: the crate control
+/// variants (`Redirect`/`Pause`/`Resume`/`Cancel`) have no OpenHuman queue lane,
+/// so a run must have a live registered handle to receive them. The directive's
+/// command kind is checked against the run's own `SteeringPolicy` *before*
+/// enqueue — a disallowed command would otherwise abort the run — so this can
+/// never smuggle a control kind past a policy that a tighter run class installed.
+pub(crate) fn steer_directive(
+ task_id: &str,
+ parent_session: &str,
+ directive: SteeringDirective,
+) -> Result<(), SteerDirectiveError> {
+ let handle = registry()
+ .steering_handle(&TaskId::new(task_id), parent_session)
+ .map_err(steer_directive_error_from_registry)?;
+ let kind = directive.kind();
+ if !handle.policy().is_allowed(kind) {
+ log::warn!(
+ "[running_subagents] directive rejected by run policy task_id={} kind={}",
+ task_id,
+ kind.as_str()
+ );
+ return Err(SteerDirectiveError::PolicyRejected);
+ }
+ handle.send(directive.into_command());
+ log::info!(
+ "[running_subagents] steered task_id={} directive={} via=tinyagents_registry",
+ task_id,
+ kind.as_str()
+ );
+ Ok(())
+}
+
+/// Inject a message into a running sub-agent. Prefer the crate-native
+/// TinyAgents steering registry when the child run has registered its live
+/// handle, and fall back to the OpenHuman `RunQueue` compatibility path.
+pub async fn steer(
+ task_id: &str,
+ parent_session: &str,
+ text: String,
+ mode: QueueMode,
+) -> Result<(), SteerError> {
+ let task_id_key = TaskId::new(task_id);
+ let snapshot = registry()
+ .snapshot(&task_id_key, parent_session)
+ .map_err(steer_error_from_registry)?;
+ if snapshot.status.is_terminal() {
+ return Err(SteerError::AlreadyDone);
+ }
+
+ let steered_via_registry = registry()
+ .steering_handle(&task_id_key, parent_session)
+ .map(|handle| send_registered_steering(&handle, text.clone(), mode))
+ .unwrap_or(false);
+ if steered_via_registry {
+ log::info!(
+ "[running_subagents] steered task_id={} mode={} via=tinyagents_registry",
+ task_id,
+ mode
+ );
+ return Ok(());
+ }
+
+ snapshot
+ .metadata
+ .run_queue
+ .push(QueuedMessage {
+ text,
+ mode,
+ client_id: "steer_subagent".to_string(),
+ thread_id: task_id.to_string(),
+ queued_at_ms: now_ms(),
+ model_override: None,
+ temperature: None,
+ profile_id: None,
+ locale: None,
+ })
+ .await;
+ log::info!(
+ "[running_subagents] steered task_id={} mode={}",
+ task_id,
+ mode
+ );
+ Ok(())
+}
+
+/// Trusted-control variant used by JSON-RPC sub-agent controls.
+///
+/// This intentionally does not require the caller to provide `parent_session`:
+/// the RPC layer is already bearer-protected and mirrors the existing
+/// `subagent_cancel` control surface, which can abort a task by id. The function
+/// still refuses unknown or terminal tasks and never logs the steered text.
+pub(crate) async fn steer_control(
+ task_id: &str,
+ text: String,
+ mode: QueueMode,
+) -> Result<(), SteerError> {
+ let task_id_key = TaskId::new(task_id);
+ let snapshot = registry()
+ .snapshot_trusted(&task_id_key)
+ .map_err(steer_error_from_registry)?;
+ if snapshot.status.is_terminal() {
+ return Err(SteerError::AlreadyDone);
+ }
+
+ let steered_via_registry = registry()
+ .steering_handle_trusted(&task_id_key)
+ .map(|handle| send_registered_steering(&handle, text.clone(), mode))
+ .unwrap_or(false);
+ if steered_via_registry {
+ log::info!(
+ "[running_subagents] control_steered task_id={} mode={} via=tinyagents_registry",
+ task_id,
+ mode
+ );
+ return Ok(());
+ }
+
+ snapshot
+ .metadata
+ .run_queue
+ .push(QueuedMessage {
+ text,
+ mode,
+ client_id: "subagent_control_rpc".to_string(),
+ thread_id: task_id.to_string(),
+ queued_at_ms: now_ms(),
+ model_override: None,
+ temperature: None,
+ profile_id: None,
+ locale: None,
+ })
+ .await;
+ log::info!(
+ "[running_subagents] control_steered task_id={} mode={}",
+ task_id,
+ mode
+ );
+ Ok(())
+}
diff --git a/crates/openhuman-core/src/channels/bus_part_01.rs b/crates/openhuman-core/src/channels/bus_part_01.rs
deleted file mode 100644
index 2da163b862..0000000000
--- a/crates/openhuman-core/src/channels/bus_part_01.rs
+++ /dev/null
@@ -1,583 +0,0 @@
-use crate::core::events::DomainEvent;
-use async_trait::async_trait;
-use serde_json::{json, Value};
-use tinybus::EventHandler;
-
-/// Subscribes to `ChannelInboundMessage` events and runs the agent loop,
-/// sending replies back to the originating channel via the backend REST API.
-pub struct ChannelInboundSubscriber;
-
-impl Default for ChannelInboundSubscriber {
- fn default() -> Self {
- Self::new()
- }
-}
-
-fn channel_message_body_with_idempotency(channel: &str, body: Value) -> Value {
- let intent = tinychannels::outbound_intent_from_legacy_message(channel, body);
- tinychannels::legacy_message_value_from_outbound_intent(&intent)
-}
-
-impl ChannelInboundSubscriber {
- pub fn new() -> Self {
- Self
- }
-}
-
-#[async_trait]
-impl EventHandler<DomainEvent> for ChannelInboundSubscriber {
- fn name(&self) -> &str {
- "channel::inbound_handler"
- }
-
- fn domains(&self) -> Option<&[&str]> {
- Some(&["channel"])
- }
-
- async fn handle(&self, event: &DomainEvent) {
- let DomainEvent::ChannelInboundMessage {
- event_name: _,
- channel,
- message,
- sender,
- reply_target,
- thread_ts,
- raw_data: _,
- } = event
- else {
- return;
- };
-
- tracing::info!(
- "[channel-inbound] received message from channel='{}' sender={} len={}",
- channel,
- sender.as_deref().unwrap_or("<unknown>"),
- message.len()
- );
-
- // Mirror `channels::context::conversation_history_key`: the inbound
- // path must key on `(channel, sender, reply_target, thread_ts)` —
- // not channel alone — or distinct participants in a shared
- // Discord / Slack channel get collapsed into one cached agent
- // session, and the second sender resumes the first's in-flight
- // state (including any prepared wallet quote).
- //
- // Legacy publishers that don't fill in `sender` fall back to the
- // old channel-only key so existing single-DM flows keep working.
- let thread_id = derive_inbound_thread_id(
- channel,
- sender.as_deref(),
- reply_target.as_deref(),
- thread_ts.as_deref(),
- );
- // Per-sender client_id so the `AGENT_TURN_ORIGIN.WebChat.client_id`
- // and the wallet `QuoteOwner.client_id` paired with it differ across
- // distinct senders in the same shared channel. The thread_id is
- // already per-sender via `derive_inbound_thread_id`, and the
- // wallet/approval gates compare both halves of the (thread_id,
- // client_id) owner pair for equality — but a single shared
- // `client_id="inbound"` collapses the surface for any downstream
- // consumer that keys on client_id alone (audit logs, future
- // session-scoped caches, etc.). Build a stable per-sender label
- // here so the surface stays segregated end-to-end.
- let client_id = derive_inbound_client_id(channel, sender.as_deref());
-
- let mut event_rx = crate::web_chat::subscribe_web_channel_events();
-
- let request_id = match crate::web_chat::start_chat(
- &client_id,
- &thread_id,
- message,
- None,
- None,
- None,
- None,
- None,
- crate::web_chat::ChatRequestMetadata {
- // Tag inbound provider messages so traces classify as
- // run:channel_inbound instead of interactive chat.
- source: Some("channel_inbound".to_string()),
- ..Default::default()
- },
- )
- .await
- {
- Ok(rid) => {
- tracing::debug!(
- "[channel-inbound] agent started request_id={} thread={}",
- rid,
- thread_id
- );
- rid
- }
- Err(err) => {
- tracing::error!("[channel-inbound] start_chat failed: {}", err);
- send_channel_reply(
- channel,
- &format!("Sorry, I couldn't process your message: {err}"),
- )
- .await;
- return;
- }
- };
-
- let timeout = tokio::time::Duration::from_secs(180);
- let deadline = tokio::time::Instant::now() + timeout;
-
- // ── Progressive-edit streaming state ──────────────────────────
- // We buffer text/tool deltas and flush them as edits on a
- // timer. If the first edit fails (e.g. the backend doesn't
- // implement the PATCH endpoint for this channel) we latch into
- // `edit_disabled` and fall back to atomic-final delivery.
- let mut streaming_state = StreamingState::default();
- let mut edit_timer = tokio::time::interval(EDIT_FLUSH_INTERVAL);
- edit_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
- // Don't fire immediately; wait for the first tick.
- edit_timer.tick().await;
-
- // ── Typing indicator state ────────────────────────────────────
- // Telegram's `sendChatAction` keeps the "typing…" UI alive for
- // ~5s, so we re-send every 4s while the turn is in flight. The
- // first call fires immediately; on repeated failures we latch
- // `typing_disabled` to stop hitting a backend that doesn't
- // support it.
- let mut typing_state = TypingState::default();
- let mut typing_timer = tokio::time::interval(TYPING_REFRESH_INTERVAL);
- typing_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
- // Fire immediately on first tick so the indicator shows up as
- // soon as the inbound message is received.
- send_typing_indicator(channel, &mut typing_state).await;
- typing_timer.tick().await; // consume the immediate tick
-
- // ── Filler messages ──────────────────────────────────────────
- // Once progressive edits + thinking streams go quiet (backend
- // doesn't support PATCH, reasoning has finished, etc.) the user
- // can wait 30–90 s seeing no fresh activity. Post a short filler
- // every FILLER_INTERVAL so the chat keeps moving. All filler ids
- // are tracked in `StreamingState.filler_message_ids` and deleted
- // in `finalize_channel_reply` once the real response is on screen.
- let mut filler_timer = tokio::time::interval(FILLER_INTERVAL);
- filler_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
- filler_timer.tick().await; // consume the immediate tick — first filler fires after FILLER_INTERVAL
-
- loop {
- tokio::select! {
- event = event_rx.recv() => {
- match event {
- Ok(ev) if ev.request_id == request_id => {
- match ev.event.as_str() {
- "text_delta" => {
- if let Some(delta) = ev.delta.as_ref() {
- streaming_state.content.push_str(delta);
- streaming_state.dirty = true;
- }
- }
- "tool_call" => {
- if let Some(ref name) = ev.tool_name {
- streaming_state.last_tool = Some(format!("🔧 {name}…"));
- streaming_state.dirty = true;
- }
- }
- "tool_result" => {
- if let Some(ref name) = ev.tool_name {
- let ok = ev.success.unwrap_or(true);
- streaming_state.last_tool = Some(if ok {
- format!("🔧 {name} ✓")
- } else {
- format!("🔧 {name} ✗")
- });
- streaming_state.dirty = true;
- }
- }
- "thinking_delta" => {
- if let Some(delta) = ev.delta.as_ref() {
- streaming_state.thinking_accumulator.push_str(delta);
- streaming_state.thinking_dirty = true;
- }
- }
- "chat_done" | "chat:done" => {
- let reply = ev.full_response.unwrap_or_default();
- // Even when the agent produced no visible
- // text, we must close out any draft we
- // already posted — otherwise the user is
- // left staring at a stale "_working…_"
- // message indefinitely.
- let reply_text = if reply.trim().is_empty() {
- tracing::warn!(
- "[channel-inbound] agent returned empty response — finalizing draft with fallback",
- );
- "(No response from agent.)"
- } else {
- reply.as_str()
- };
- tracing::info!(
- "[channel-inbound] agent done, replying to channel='{}' len={} streamed_msg_id={:?}",
- channel,
- reply_text.len(),
- streaming_state.message_id,
- );
- // If we've been streaming progressive edits, replace
- // the outbound message with the final canonical text.
- // Otherwise send a fresh message atomically.
- finalize_channel_reply(
- channel,
- &mut streaming_state,
- reply_text,
- )
- .await;
- return;
- }
- "chat_error" | "chat:error" => {
- let err_msg = ev.message.unwrap_or_else(|| "unknown error".to_string());
- tracing::error!("[channel-inbound] agent error: {}", err_msg);
- let reply = format!("Sorry, I encountered an error: {err_msg}");
- finalize_channel_reply(channel, &mut streaming_state, &reply)
- .await;
- return;
- }
- _ => {}
- }
- }
- Ok(_) => {}
- Err(tokio::sync::broadcast::error::RecvError::Lagged(n)) => {
- tracing::warn!("[channel-inbound] event bus lagged, skipped {} events", n);
- }
- Err(tokio::sync::broadcast::error::RecvError::Closed) => {
- tracing::error!("[channel-inbound] event bus closed unexpectedly");
- return;
- }
- }
- }
- _ = edit_timer.tick() => {
- // Progressive draft/thinking bubbles require edit+delete
- // support; skip them on channels that lack it (Discord) so
- // they don't leave un-cleanable placeholder messages.
- if channel_supports_progressive_ui(channel) {
- if streaming_state.thinking_dirty && !streaming_state.thinking_edit_disabled {
- flush_thinking_message(channel, &mut streaming_state).await;
- }
- if streaming_state.dirty && !streaming_state.edit_disabled {
- flush_streaming_edit(channel, &mut streaming_state).await;
- }
- }
- }
- _ = typing_timer.tick() => {
- if !typing_state.disabled {
- send_typing_indicator(channel, &mut typing_state).await;
- }
- }
- _ = filler_timer.tick() => {
- // Fillers ("💭 Still working on it…") are ephemeral and
- // deleted on finalize — only post them where cleanup works.
- if channel_supports_progressive_ui(channel) && !streaming_state.filler_disabled {
- send_filler_message(channel, &mut streaming_state).await;
- }
- }
- _ = tokio::time::sleep_until(deadline) => {
- tracing::error!("[channel-inbound] agent timed out after {}s", timeout.as_secs());
- let reply = "Sorry, the request timed out.";
- finalize_channel_reply(channel, &mut streaming_state, reply).await;
- return;
- }
- }
- }
- }
-}
-
-/// Minimum interval between progressive edits of the outbound channel
-/// message. Tuned to stay comfortably below Telegram's ~1 edit/sec cap
-/// per chat. Slack has a similar soft limit.
-const EDIT_FLUSH_INTERVAL: tokio::time::Duration = tokio::time::Duration::from_millis(1000);
-
-/// Maximum consecutive edit failures tolerated before giving up on
-/// progressive streaming and falling back to atomic-final delivery.
-const MAX_EDIT_FAILURES: u32 = 2;
-
-/// How often to re-send the "typing…" indicator while a turn is in
-/// flight. Telegram's `sendChatAction` keeps the UI alive for about
-/// 5 seconds per call, so we refresh every 4 s to ensure continuity.
-const TYPING_REFRESH_INTERVAL: tokio::time::Duration = tokio::time::Duration::from_secs(4);
-
-/// Maximum consecutive typing-indicator failures before we stop
-/// trying. One failure is usually "endpoint doesn't exist"; two is
-/// enough to conclude the backend doesn't support it on this channel.
-const MAX_TYPING_FAILURES: u32 = 2;
-
-/// How often to post a filler "still working" message to the channel
-/// so the user keeps seeing activity during long agent turns. Deleted
-/// on finalization alongside the ephemeral thinking bubble.
-const FILLER_INTERVAL: tokio::time::Duration = tokio::time::Duration::from_secs(13);
-
-/// Whether a channel supports the progressive-UI placeholders — the
-/// evolving draft bubble, the rotating "💭" fillers, and the ephemeral
-/// "thinking" bubble. All three rely on the backend supporting **both**
-/// message *edit* and *delete*: edit keeps a single bubble evolving in
-/// place, delete removes it once the final reply lands. Telegram supports
-/// both. Discord's adapter supports **neither** (edits 404, delete is a
-/// hard `Delete not supported` stub), so every placeholder becomes a
-/// permanent, un-editable, un-deletable message — the channel fills with
-/// "💭 Still working on it…" bubbles.
-///
-/// This is an **allowlist**, not a denylist: only channels confirmed to
-/// support edit+delete opt in. A new/unknown adapter therefore fails *safe*
-/// (placeholders suppressed) rather than silently re-introducing the spam bug
-/// this gate was added to fix.
-fn channel_supports_progressive_ui(channel: &str) -> bool {
- // Inbound channels arrive provider-prefixed from the socket layer
- // (e.g. `discord:<guild>`, `tg:<chat>`), so compare the provider prefix,
- // not the whole id — mirroring `channel_is_telegram`.
- let provider = channel.split(':').next().unwrap_or(channel);
- matches!(provider, "telegram" | "tg")
-}
-
-/// Why an edit of an already-posted channel message failed.
-///
-/// These three causes need three different recoveries, and conflating the
-/// first two is what broke the live "💭 Thinking:" bubble (#5230).
-#[derive(Debug, PartialEq, Eq)]
-enum EditFailure {
- /// The backend serves no message-edit route at all, so the edit could
- /// never have succeeded. The message itself is untouched and we still own
- /// it: keep its id so it can still be deleted (or finally edited once the
- /// backend ships the route) and just stop attempting edits.
- RouteUnsupported,
- /// The message really is gone on the provider side (user deleted it, or
- /// the backend GC'd the relay row). The id is worthless — drop it.
- MessageGone,
- /// Anything else (transient 5xx, transport error, rate limit). Counts
- /// against the per-turn failure budget and may be retried.
- Transient,
-}
-
-/// Classify an edit failure by typed error rather than by message text, so the
-/// recovery a call site picks cannot drift with `#[error(...)]` wording.
-fn classify_edit_failure(err: &anyhow::Error) -> EditFailure {
- match err.downcast_ref::<crate::api::rest::BackendApiError>() {
- Some(crate::api::rest::BackendApiError::ChannelEditUnsupported { .. }) => {
- EditFailure::RouteUnsupported
- }
- Some(crate::api::rest::BackendApiError::MessageNotFound { .. }) => EditFailure::MessageGone,
- _ => EditFailure::Transient,
- }
-}
-
-/// Providers whose backend answered "no edit route" at least once this
-/// process. Whether the route exists is a property of the deployed backend,
-/// not of a turn, so re-probing it on every turn only buys a guaranteed-404
-/// round-trip per turn forever. Latching it here keeps that to one attempt per
-/// provider per process — and it self-heals on the next core start once the
-/// backend ships the route (#5230).
-static EDIT_UNSUPPORTED_PROVIDERS: std::sync::OnceLock<
- std::sync::Mutex<std::collections::HashSet<String>>,
-> = std::sync::OnceLock::new();
-
-/// Provider key for the edit-capability latch. Inbound channels arrive
-/// provider-prefixed (`telegram:<chat>`), and the route's existence is a
-/// per-provider fact, so latch on the prefix — same reasoning as
-/// [`channel_supports_progressive_ui`].
-fn edit_capability_key(channel: &str) -> String {
- channel.split(':').next().unwrap_or(channel).to_string()
-}
-
-/// `true` once this process has learned the backend serves no edit route for
-/// `channel`'s provider. Callers should skip the request entirely.
-fn channel_edits_unsupported(channel: &str) -> bool {
- EDIT_UNSUPPORTED_PROVIDERS
- .get_or_init(Default::default)
- .lock()
- .map(|set| set.contains(&edit_capability_key(channel)))
- .unwrap_or(false)
-}
-
-/// Latch `channel`'s provider as having no message-edit route.
-fn mark_channel_edits_unsupported(channel: &str) {
- let key = edit_capability_key(channel);
- if let Ok(mut set) = EDIT_UNSUPPORTED_PROVIDERS
- .get_or_init(Default::default)
- .lock()
- {
- if set.insert(key.clone()) {
- tracing::warn!(
- "[channel-inbound][edit] backend serves no message-edit route for provider='{}' — \
- progressive edits disabled for this process; placeholders will be posted once and \
- cleaned up by delete instead (#5230)",
- key,
- );
- }
- }
-}
-
-/// Maximum consecutive filler-send failures before we stop trying.
-/// Same rationale as the thinking/typing latches.
-const MAX_FILLER_FAILURES: u32 = 2;
-
-/// Maximum number of Unicode scalars to include in a dynamic filler
-/// derived from the thinking accumulator. Keeps each bubble compact.
-const MAX_FILLER_CHARS: usize = 200;
-
-/// Fallback rotating pool used when the thinking stream has produced
-/// nothing new since the previous filler (or nothing at all). Index in
-/// `StreamingState.filler_index` advances only when this branch is hit.
-const STATIC_FILLERS: &[&str] = &[
- "💭 Still working on it…",
- "💭 Just a moment…",
- "💭 Almost there…",
-];
-
-/// Per-turn progressive-edit buffer. `dirty=true` means there's new
-/// content to flush; `edit_disabled=true` means the backend doesn't
-/// support editing for this channel and we should finalize atomically.
-#[derive(Default)]
-struct StreamingState {
- /// Accumulated visible assistant text from `text_delta` events.
- content: String,
- /// Most recent tool status line (prepended to the message body).
- last_tool: Option<String>,
- /// Backend-assigned message id returned from the initial
- /// `send_channel_message`; subsequent edits target this id.
- message_id: Option<String>,
- /// `true` once a draft message has been posted to the channel,
- /// even when the backend response didn't include an id to target
- /// for future edits. Decouples "a draft exists" from "we can edit
- /// it" so `finalize_channel_reply` won't post a duplicate bubble
- /// when the id was lost.
- draft_sent: bool,
- /// New content has arrived since the last edit flush.
- dirty: bool,
- /// Consecutive edit failures. Reset to zero on every success.
- edit_failures: u32,
- /// Latched when the backend doesn't support edits for this channel
- /// — we stop trying and rely on the final atomic send.
- edit_disabled: bool,
- /// Accumulated LLM reasoning from `thinking_delta` events. Shown
- /// to the user as an ephemeral "💭 Thinking…" message that is
- /// **deleted** once the final response is ready (#600).
- thinking_accumulator: String,
- /// Backend-assigned id of the ephemeral thinking message. Used to
- /// delete it at finalization so the user sees only the clean reply.
- thinking_message_id: Option<String>,
- /// `true` once a thinking message has been posted to the channel.
- thinking_sent: bool,
- /// New thinking content has arrived since the last thinking flush.
- thinking_dirty: bool,
- /// Latched when the first thinking POST succeeded with 200 but the
- /// backend didn't return an id we can edit. Without this latch,
- /// every subsequent `thinking_dirty` tick re-enters the "send new
- /// message" branch and the user sees one italic bubble per
- /// accumulated snippet instead of a single evolving one (#600).
- thinking_edit_disabled: bool,
- /// Ids of ephemeral filler messages posted during long turns, in
- /// send order. Deleted in `finalize_channel_reply` after the
- /// canonical response is on screen.
- filler_message_ids: Vec<String>,
- /// Next entry in `STATIC_FILLERS` to send when we fall back to the
- /// rotating pool (no fresh thinking content to surface). Wraps
- /// modulo pool size.
- filler_index: usize,
- /// Consecutive filler-send failures. Reset to zero on success.
- filler_failures: u32,
- /// Latched when the backend rejects filler sends — stops hitting
- /// a broken endpoint every 13 s.
- filler_disabled: bool,
- /// Last dynamic snippet we posted as a filler. Used to skip a
- /// duplicate post when the thinking accumulator hasn't advanced
- /// enough to produce a new tail slice — we fall through to the
- /// static pool instead so the chat still sees movement.
- last_filler_snippet: Option<String>,
-}
-
-/// Typing-indicator bookkeeping. One per in-flight turn. Latches
-/// `disabled` after repeated failures so channels without typing
-/// support stop getting hit every 4 seconds.
-#[derive(Default)]
-struct TypingState {
- failures: u32,
- disabled: bool,
-}
-
-/// Fire a single "typing…" indicator at the channel. Silently
-/// latches `disabled` on repeated failure so callers can keep calling
-/// this from a timer without accumulating warnings.
-async fn send_typing_indicator(channel: &str, state: &mut TypingState) {
- if state.disabled {
- return;
- }
- let Some((client, jwt)) = build_channel_client().await else {
- return;
- };
- match client.send_channel_typing(channel, &jwt).await {
- Ok(_) => {
- if state.failures > 0 {
- tracing::debug!(
- "[channel-inbound][typing] recovered channel='{}' after {} failure(s)",
- channel,
- state.failures,
- );
- }
- state.failures = 0;
- }
- Err(err) => {
- state.failures += 1;
- tracing::debug!(
- "[channel-inbound][typing] indicator failed channel='{}' err={} (failures={}/{})",
- channel,
- err,
- state.failures,
- MAX_TYPING_FAILURES,
- );
- if state.failures >= MAX_TYPING_FAILURES {
- tracing::info!(
- "[channel-inbound][typing] disabling typing indicator for channel='{}' — backend unsupported",
- channel,
- );
- state.disabled = true;
- }
- }
- }
-}
-
-impl StreamingState {
- /// The backend has no edit route: stop attempting edits but **keep**
- /// `message_id`. The draft is still on the user's screen and we still own
- /// it, so finalization needs the id to delete it before posting the
- /// canonical reply. Dropping the id here is what orphaned the draft and
- /// left a stale "_working…_" bubble (#5230).
- fn latch_draft_edits_unsupported(&mut self) {
- self.edit_disabled = true;
- }
-
- /// The draft really is gone provider-side: the id is worthless, so forget
- /// it as well as disabling edits.
- fn forget_draft(&mut self) {
- self.message_id = None;
- self.edit_disabled = true;
- }
-
- /// Thinking-bubble counterpart of [`Self::latch_draft_edits_unsupported`].
- /// Keeping `thinking_message_id` is what lets finalization delete the
- /// ephemeral "💭 Thinking:" bubble instead of leaving it in the chat (#5230).
- fn latch_thinking_edits_unsupported(&mut self) {
- self.thinking_edit_disabled = true;
- }
-
- /// Thinking-bubble counterpart of [`Self::forget_draft`].
- fn forget_thinking(&mut self) {
- self.thinking_message_id = None;
- self.thinking_edit_disabled = true;
- }
-
- fn compose_draft(&self) -> String {
- let trimmed = self.content.trim_end();
- if trimmed.is_empty() {
- // No visible text yet — show a placeholder. Tool indicators
- // (🔧 …) are intentionally omitted so the draft only ever
- // contains content that is a clean prefix of the final
- // response. If the draft persists after finalization the
- // user sees benign placeholder text instead of stale tool
- // status lines (#600).
- "_working…_".to_string()
- } else {
- trimmed.to_string()
- }
- }
-}
diff --git a/crates/openhuman-core/src/channels/bus_part_02.rs b/crates/openhuman-core/src/channels/bus_part_02.rs
deleted file mode 100644
index bde9940cd7..0000000000
--- a/crates/openhuman-core/src/channels/bus_part_02.rs
+++ /dev/null
@@ -1,678 +0,0 @@
-
-/// Post or edit a draft message carrying the latest buffered text +
-/// tool status. On the first call, sends a new message and records its
-/// id; on subsequent calls, edits the existing message.
-async fn flush_streaming_edit(channel: &str, state: &mut StreamingState) {
- let draft = state.compose_draft();
- if draft.is_empty() {
- return;
- }
- state.dirty = false;
-
- let Some((client, jwt)) = build_channel_client().await else {
- return;
- };
-
- if let Some(ref message_id) = state.message_id {
- // Known-missing edit route: skip the guaranteed 404 and let
- // finalization replace the draft (delete + fresh atomic reply).
- if channel_edits_unsupported(channel) {
- tracing::debug!(
- "[channel-inbound][stream] skipping edit channel='{}' msg_id={} — no edit route on this backend, draft stays as-is until finalize",
- channel,
- message_id,
- );
- state.latch_draft_edits_unsupported();
- return;
- }
- let body = json!({ "text": draft });
- match client
- .send_channel_edit(channel, message_id, &jwt, body)
- .await
- {
- Ok(_) => {
- tracing::debug!(
- "[channel-inbound][stream] edit ok channel='{}' msg_id={} chars={}",
- channel,
- message_id,
- draft.len(),
- );
- state.edit_failures = 0;
- }
- Err(err) => {
- match classify_edit_failure(&err) {
- EditFailure::RouteUnsupported => {
- // Keep `message_id`: the draft is still on screen and
- // finalization must be able to delete it before posting
- // the canonical reply. Clearing it here (as the old
- // `MessageNotFound` branch did) orphaned the draft and
- // produced a stale "_working…_" bubble (#5230).
- tracing::info!(
- "[channel-inbound][stream] edit channel='{}' msg_id={} — backend has no edit route, keeping id for finalize cleanup and disabling progressive edits",
- channel,
- message_id,
- );
- mark_channel_edits_unsupported(channel);
- state.latch_draft_edits_unsupported();
- return;
- }
- EditFailure::MessageGone => {
- tracing::info!(
- "[channel-inbound][stream] edit channel='{}' msg_id={} — message gone provider-side (404), clearing stale id and disabling further edits",
- channel,
- message_id,
- );
- state.forget_draft();
- return;
- }
- EditFailure::Transient => {}
- }
- state.edit_failures += 1;
- tracing::warn!(
- "[channel-inbound][stream] edit failed channel='{}' msg_id={} err={} (failures={}/{})",
- channel,
- message_id,
- err,
- state.edit_failures,
- MAX_EDIT_FAILURES,
- );
- if state.edit_failures >= MAX_EDIT_FAILURES {
- tracing::info!(
- "[channel-inbound][stream] giving up on progressive edits for channel='{}', falling back to atomic delivery",
- channel,
- );
- state.edit_disabled = true;
- }
- }
- }
- } else {
- let body = channel_message_body_with_idempotency(channel, json!({ "text": draft }));
- match client.send_channel_message(channel, &jwt, body).await {
- Ok(resp) => {
- // A message was posted to the user — record that fact
- // *before* checking for an id. Even if we can't extract
- // one (and thus can't edit it further), we must never
- // later fall back to sending a second atomic message.
- state.draft_sent = true;
- let id = extract_message_id(&resp);
- if let Some(id) = id {
- tracing::debug!(
- "[channel-inbound][stream] initial draft sent channel='{}' msg_id={}",
- channel,
- id,
- );
- state.message_id = Some(id);
- } else {
- tracing::warn!(
- "[channel-inbound][stream] initial draft sent but response lacked id — disabling progressive edits (finalize will skip sending a duplicate) channel='{}' resp={}",
- channel,
- resp,
- );
- state.edit_disabled = true;
- }
- }
- Err(err) => {
- state.edit_failures += 1;
- tracing::warn!(
- "[channel-inbound][stream] initial send failed channel='{}' err={} (failures={})",
- channel,
- err,
- state.edit_failures,
- );
- if state.edit_failures >= MAX_EDIT_FAILURES {
- state.edit_disabled = true;
- }
- }
- }
- }
-}
-
-/// Extract a message id from a backend `send_channel_message` response.
-/// The backend has used at least three shapes: `{"id":"..."}`,
-/// `{"data":{"id":"..."}}`, and `{"messageId":1456,"success":true}` —
-/// the last one returns the id as a JSON number, not a string, so
-/// `as_str()` alone misses it (#600).
-fn extract_message_id(resp: &serde_json::Value) -> Option<String> {
- let candidate = resp
- .get("id")
- .or_else(|| resp.get("messageId"))
- .or_else(|| resp.get("data").and_then(|d| d.get("id")))
- .or_else(|| resp.get("data").and_then(|d| d.get("messageId")))?;
- if let Some(s) = candidate.as_str() {
- return Some(s.to_string());
- }
- if let Some(n) = candidate.as_i64() {
- return Some(n.to_string());
- }
- if let Some(n) = candidate.as_u64() {
- return Some(n.to_string());
- }
- None
-}
-
-/// Maximum length of the thinking snippet shown in the ephemeral
-/// channel message. Longer reasoning is truncated with "…" to avoid
-/// overwhelming the chat.
-const MAX_THINKING_DISPLAY_CHARS: usize = 500;
-
-/// Send or edit the ephemeral "💭 Thinking…" message on the channel.
-/// This message is deleted when the final response is ready.
-async fn flush_thinking_message(channel: &str, state: &mut StreamingState) {
- state.thinking_dirty = false;
-
- if state.thinking_accumulator.trim().is_empty() {
- return;
- }
-
- let mut snippet = state.thinking_accumulator.trim().to_string();
- if snippet.len() > MAX_THINKING_DISPLAY_CHARS {
- snippet.truncate(MAX_THINKING_DISPLAY_CHARS);
- snippet.push('…');
- }
- let text = format!("💭 Thinking:\n_{snippet}_");
-
- let Some((client, jwt)) = build_channel_client().await else {
- return;
- };
-
- if let Some(msg_id) = state.thinking_message_id.clone() {
- // Known-missing edit route: the bubble stays as first posted and is
- // deleted at finalization. Skip the guaranteed 404.
- if channel_edits_unsupported(channel) {
- tracing::debug!(
- "[channel-inbound][thinking] skipping edit channel='{}' msg_id={} — no edit route on this backend, bubble stays until finalize deletes it",
- channel,
- msg_id,
- );
- state.latch_thinking_edits_unsupported();
- return;
- }
- // Edit existing thinking message with updated content.
- let body = json!({ "text": text });
- if let Err(err) = client.send_channel_edit(channel, &msg_id, &jwt, body).await {
- match classify_edit_failure(&err) {
- EditFailure::RouteUnsupported => {
- // Keep `thinking_message_id`. Clearing it (as the old
- // `MessageNotFound` branch did) meant finalization had
- // nothing to delete, so the ephemeral "💭 Thinking:"
- // bubble stayed in the chat forever (#5230).
- tracing::info!(
- "[channel-inbound][thinking] edit channel='{}' msg_id={} — backend has no edit route, keeping id so finalize still deletes the bubble",
- channel,
- msg_id,
- );
- mark_channel_edits_unsupported(channel);
- state.latch_thinking_edits_unsupported();
- }
- EditFailure::MessageGone => {
- tracing::info!(
- "[channel-inbound][thinking] edit channel='{}' msg_id={} — thinking msg gone provider-side (404), clearing id and disabling further thinking edits",
- channel,
- msg_id,
- );
- state.forget_thinking();
- }
- EditFailure::Transient => {
- tracing::debug!(
- "[channel-inbound][thinking] edit failed channel='{}' msg_id={} err={}",
- channel,
- msg_id,
- err,
- );
- }
- }
- }
- } else {
- // Send initial thinking message.
- let body = channel_message_body_with_idempotency(channel, json!({ "text": text }));
- match client.send_channel_message(channel, &jwt, body).await {
- Ok(resp) => {
- state.thinking_sent = true;
- let id = extract_message_id(&resp);
- if let Some(id) = id {
- tracing::debug!(
- "[channel-inbound][thinking] thinking msg sent channel='{}' msg_id={}",
- channel,
- id,
- );
- state.thinking_message_id = Some(id);
- } else {
- tracing::warn!(
- "[channel-inbound][thinking] thinking msg sent but response lacked id — disabling further thinking flushes (message won't be deletable) channel='{}' resp={}",
- channel,
- resp,
- );
- state.thinking_edit_disabled = true;
- }
- }
- Err(err) => {
- tracing::warn!(
- "[channel-inbound][thinking] failed to send thinking msg channel='{}' err={} — disabling further thinking flushes",
- channel,
- err,
- );
- state.thinking_edit_disabled = true;
- }
- }
- }
-}
-
-/// Pull the most recent `MAX_FILLER_CHARS` Unicode scalars out of the
-/// thinking accumulator so we can surface a live snapshot of the agent's
-/// reasoning as a filler. Returns `None` when there's nothing to show
-/// yet. Trims any partial leading word so the snippet reads cleanly.
-fn latest_thinking_snippet(state: &StreamingState) -> Option<String> {
- let acc = state.thinking_accumulator.trim();
- if acc.is_empty() {
- return None;
- }
- let total = acc.chars().count();
- let snippet: String = if total <= MAX_FILLER_CHARS {
- acc.to_string()
- } else {
- acc.chars().skip(total - MAX_FILLER_CHARS).collect()
- };
- let trimmed = snippet
- .trim_start_matches(|c: char| !c.is_whitespace())
- .trim_start()
- .to_string();
- if trimmed.is_empty() {
- None
- } else {
- Some(trimmed)
- }
-}
-
-/// Post a fresh filler message to the channel and record its id so
-/// `finalize_channel_reply` can delete it once the real response is on
-/// screen. Prefers a live snippet of the agent's latest reasoning
-/// (`thinking_accumulator`); falls back to the rotating `STATIC_FILLERS`
-/// pool when there's no new thinking to show.
-async fn send_filler_message(channel: &str, state: &mut StreamingState) {
- let text = match latest_thinking_snippet(state) {
- Some(snippet) if state.last_filler_snippet.as_deref() != Some(snippet.as_str()) => {
- state.last_filler_snippet = Some(snippet.clone());
- format!("💭 _{snippet}…_")
- }
- _ => {
- let pool = STATIC_FILLERS;
- let idx = state.filler_index % pool.len();
- state.filler_index = state.filler_index.wrapping_add(1);
- pool[idx].to_string()
- }
- };
-
- let Some((client, jwt)) = build_channel_client().await else {
- return;
- };
- let body = channel_message_body_with_idempotency(channel, json!({ "text": text }));
- match client.send_channel_message(channel, &jwt, body).await {
- Ok(resp) => {
- state.filler_failures = 0;
- if let Some(id) = extract_message_id(&resp) {
- tracing::debug!(
- "[channel-inbound][filler] sent channel='{}' len={} msg_id={}",
- channel,
- text.len(),
- id,
- );
- state.filler_message_ids.push(id);
- } else {
- tracing::warn!(
- "[channel-inbound][filler] sent but response lacked id — cannot clean up on finalize channel='{}' resp={}",
- channel,
- resp,
- );
- }
- }
- Err(err) => {
- state.filler_failures = state.filler_failures.saturating_add(1);
- tracing::warn!(
- "[channel-inbound][filler] send failed channel='{}' err={} (failures={}/{})",
- channel,
- err,
- state.filler_failures,
- MAX_FILLER_FAILURES,
- );
- if state.filler_failures >= MAX_FILLER_FAILURES {
- tracing::info!(
- "[channel-inbound][filler] disabling filler messages for channel='{}' — backend unsupported",
- channel,
- );
- state.filler_disabled = true;
- }
- }
- }
-}
-
-/// Delete a previously sent message from the channel. Used to clean
-/// up ephemeral thinking messages once the final response is ready.
-async fn delete_channel_message(channel: &str, message_id: &str) {
- let Some((client, jwt)) = build_channel_client().await else {
- return;
- };
- match client.send_channel_delete(channel, message_id, &jwt).await {
- Ok(_) => {
- tracing::info!(
- "[channel-inbound] deleted ephemeral msg channel='{}' msg_id={}",
- channel,
- message_id,
- );
- }
- Err(err) => {
- if let Some(crate::api::rest::BackendApiError::MessageNotFound { .. }) =
- err.downcast_ref::<crate::api::rest::BackendApiError>()
- {
- tracing::info!(
- "[channel-inbound] delete channel='{}' msg_id={} — message already gone provider-side (404), nothing to clean up",
- channel,
- message_id,
- );
- } else {
- tracing::warn!(
- "[channel-inbound] failed to delete ephemeral msg channel='{}' msg_id={} err={}",
- channel,
- message_id,
- err,
- );
- }
- }
- }
-}
-
-/// Deliver the final canonical reply.
-///
-/// **Invariant**: if a draft message has already been posted to the
-/// channel (`state.draft_sent == true`), we MUST NOT post a second
-/// message — that would duplicate the visible bubble on the user's
-/// side. When we have an id we attempt one last edit; when the id was
-/// lost we leave the draft in place silently. The only path that
-/// creates a fresh outbound message is when no draft has been posted
-/// at all.
-async fn finalize_channel_reply(channel: &str, state: &mut StreamingState, final_text: &str) {
- // Deliver the canonical reply FIRST, then clean up the ephemeral
- // "💭 Thinking:" bubble. Deleting before the reply would leave the
- // chat empty for a beat; this order keeps something visible at all
- // times (#600).
- 'send: {
- if let Some(ref message_id) = state.message_id {
- // Once this process knows the backend serves no edit route, the
- // "one last edit" below is a guaranteed 404 — go straight to
- // replacing the draft. Deleting it first matters: it is still on
- // screen showing partial text, so leaving it would strand a stale
- // "_working…_" bubble next to the real answer (#5230).
- if channel_edits_unsupported(channel) {
- tracing::info!(
- "[channel-inbound] final edit skipped channel='{}' msg_id={} — no edit route on this backend, replacing the draft with a fresh atomic reply",
- channel,
- message_id,
- );
- let orphan = message_id.clone();
- delete_channel_message(channel, &orphan).await;
- send_channel_reply(channel, final_text).await;
- break 'send;
- }
- // We committed to a draft earlier in the turn. Always attempt
- // to edit it with the canonical reply, even when we'd
- // previously latched `edit_disabled` during the streaming
- // phase — the user is already looking at that message, so a
- // late edit attempt is still the right call. If the edit
- // fails, delete the orphan draft and send the final reply
- // as a fresh atomic message so the user always sees it.
- if let Some((client, jwt)) = build_channel_client().await {
- let body = json!({ "text": final_text });
- match client
- .send_channel_edit(channel, message_id, &jwt, body)
- .await
- {
- Ok(_) => {
- tracing::info!(
- "[channel-inbound] final edit ok channel='{}' msg_id={} chars={}",
- channel,
- message_id,
- final_text.len(),
- );
- }
- Err(err) => match classify_edit_failure(&err) {
- EditFailure::MessageGone => {
- tracing::info!(
- "[channel-inbound] final edit channel='{}' msg_id={} — draft already gone provider-side (404), sending fresh atomic reply",
- channel,
- message_id,
- );
- send_channel_reply(channel, final_text).await;
- }
- // Route absent (or any other failure): the draft is
- // still on screen showing partial text, so it has to be
- // deleted before the canonical reply lands — otherwise
- // the user keeps a stale "_working…_" bubble alongside
- // the real answer (#5230).
- EditFailure::RouteUnsupported => {
- tracing::warn!(
- "[channel-inbound] final edit channel='{}' msg_id={} — backend has no edit route, deleting the draft and sending a fresh atomic reply",
- channel,
- message_id,
- );
- mark_channel_edits_unsupported(channel);
- let orphan = message_id.clone();
- delete_channel_message(channel, &orphan).await;
- send_channel_reply(channel, final_text).await;
- }
- EditFailure::Transient => {
- tracing::warn!(
- "[channel-inbound] final edit failed channel='{}' msg_id={} err={} — deleting orphan draft and sending fresh atomic reply so user still sees the canonical response",
- channel,
- message_id,
- err,
- );
- let orphan = message_id.clone();
- delete_channel_message(channel, &orphan).await;
- send_channel_reply(channel, final_text).await;
- }
- },
- }
- } else {
- tracing::warn!(
- "[channel-inbound] cannot finalize channel='{}' msg_id={} — backend client unavailable, draft left in place",
- channel,
- message_id,
- );
- }
- break 'send;
- }
- if state.draft_sent {
- // A draft was posted but the backend didn't return an id, so
- // we have nothing to edit. Since the draft only contains a
- // clean text prefix (or "_working…_" placeholder), sending the
- // final response as a second bubble is acceptable — leaving
- // the user without the canonical reply is worse (#600).
- tracing::warn!(
- "[channel-inbound] sending fresh reply on channel='{}' — id-less draft exists but user needs the final response",
- channel,
- );
- send_channel_reply(channel, final_text).await;
- break 'send;
- }
- // No draft exists — this is the first (and only) message for the
- // turn. Safe to send atomically.
- send_channel_reply(channel, final_text).await;
- }
-
- // ── Clean up ephemeral filler + thinking messages ───────────
- // Delete after the canonical reply is already on screen so the
- // chat is never momentarily empty between the two operations.
- // Fillers first (more of them, oldest-first), then the thinking
- // bubble — purely cosmetic ordering.
- let fillers = std::mem::take(&mut state.filler_message_ids);
- for id in fillers {
- delete_channel_message(channel, &id).await;
- }
- if let Some(thinking_id) = state.thinking_message_id.take() {
- delete_channel_message(channel, &thinking_id).await;
- }
-}
-
-/// Construct the REST client + session JWT shared by every outbound
-/// channel call on this turn. Returns `None` and logs if either is
-/// unavailable so the caller can bail quietly.
-async fn build_channel_client() -> Option<(crate::api::rest::BackendOAuthClient, String)> {
- let config = match crate::config::rpc::load_config_with_timeout().await {
- Ok(c) => c,
- Err(e) => {
- tracing::error!("[channel-inbound] failed to load config: {}", e);
- return None;
- }
- };
- let api_url = crate::api::config::effective_backend_api_url(&config.api_url);
- let jwt = match crate::api::jwt::get_session_token(&config) {
- Ok(Some(t)) => t,
- Ok(None) => {
- tracing::error!("[channel-inbound] no session JWT — cannot send");
- return None;
- }
- Err(e) => {
- tracing::error!("[channel-inbound] failed to get session token: {}", e);
- return None;
- }
- };
- match crate::api::rest::BackendOAuthClient::new(&api_url) {
- Ok(c) => Some((c, jwt)),
- Err(e) => {
- tracing::error!("[channel-inbound] failed to create API client: {}", e);
- None
- }
- }
-}
-
-/// Send a text reply back to a channel via the backend REST API.
-async fn send_channel_reply(channel: &str, text: &str) {
- let config = match crate::config::rpc::load_config_with_timeout().await {
- Ok(c) => c,
- Err(e) => {
- tracing::error!("[channel-inbound] failed to load config: {}", e);
- return;
- }
- };
-
- let api_url = crate::api::config::effective_backend_api_url(&config.api_url);
- let jwt = match crate::api::jwt::get_session_token(&config) {
- Ok(Some(t)) => t,
- Ok(None) => {
- tracing::error!("[channel-inbound] no session JWT — cannot reply");
- return;
- }
- Err(e) => {
- tracing::error!("[channel-inbound] failed to get session token: {}", e);
- return;
- }
- };
-
- let client = match crate::api::rest::BackendOAuthClient::new(&api_url) {
- Ok(c) => c,
- Err(e) => {
- tracing::error!("[channel-inbound] failed to create API client: {}", e);
- return;
- }
- };
-
- let body = channel_message_body_with_idempotency(channel, json!({ "text": text }));
- match client.send_channel_message(channel, &jwt, body).await {
- Ok(resp) => {
- tracing::info!(
- "[channel-inbound] reply sent to channel='{}' response={:?}",
- channel,
- resp
- );
- }
- Err(e) => {
- tracing::error!(
- "[channel-inbound] failed to send reply to channel='{}': {}",
- channel,
- e
- );
- }
- }
-}
-
-/// Per-sender thread-id derivation for inbound channel messages.
-///
-/// Matches the shape `channels::context::conversation_history_key` builds
-/// for the canonical channel paths so the inbound bus handler does not
-/// re-introduce a session-collapse where distinct participants in a
-/// shared channel share a cached agent session.
-///
-/// Layout: `channel:<channel>[/<sender>][/<reply_target>][#thread:<ts>]`.
-/// Each optional segment is appended only when the publisher surfaced
-/// that field; legacy callers that pass only `channel` fall back to the
-/// historical `channel:<channel>` key so single-DM flows keep working.
-pub(crate) fn derive_inbound_thread_id(
- channel: &str,
- sender: Option<&str>,
- reply_target: Option<&str>,
- thread_ts: Option<&str>,
-) -> String {
- let mut key = format!("channel:{channel}");
- let clean = |s: &str| -> Option<String> {
- let t = s.trim();
- if t.is_empty() {
- None
- } else {
- Some(t.to_string())
- }
- };
- if let Some(s) = sender.and_then(clean) {
- key.push('/');
- key.push_str(&s);
- }
- if let Some(r) = reply_target.and_then(clean) {
- key.push('/');
- key.push_str(&r);
- }
- // Telegram threads its messages by `thread_ts` for transport routing
- // but should not split memory/history per message — match the
- // `conversation_history_key` carve-out and skip the thread suffix
- // there. The socket layer addresses Telegram with raw channel ids
- // like `tg:123` as well as the literal `telegram` slug, so the
- // carve-out keys off whichever provider prefix the channel string
- // exposes, not the full id.
- if !channel_is_telegram(channel) {
- if let Some(t) = thread_ts.and_then(clean) {
- key.push_str("#thread:");
- key.push_str(&t);
- }
- }
- key
-}
-
-/// Build the per-turn `client_id` for an inbound socket message. Inbound
-/// messages do not have a Socket.IO client id of their own — they arrive
-/// from the channel transport layer rather than from a connected web
-/// browser. Mint a stable label so downstream consumers that key on
-/// `client_id` (the agent-turn origin, approval-chat-context, wallet
-/// QuoteOwner pair, future audit-log keys) see distinct values for
-/// distinct senders sharing a single Discord / Slack channel.
-///
-/// `None` (legacy publisher that didn't fill `sender`) maps to the bare
-/// `"inbound"` literal that the path used historically, preserving
-/// behavior for single-DM flows where no co-channel attacker exists.
-pub(crate) fn derive_inbound_client_id(channel: &str, sender: Option<&str>) -> String {
- let trimmed_channel = channel.trim();
- let trimmed = sender.map(|s| s.trim()).filter(|s| !s.is_empty());
- match trimmed {
- Some(s) if !trimmed_channel.is_empty() => format!("inbound:{trimmed_channel}:{s}"),
- Some(s) => format!("inbound:{s}"),
- None => "inbound".to_string(),
- }
-}
-
-/// True for any inbound channel string that addresses Telegram, whether
-/// the publisher uses the canonical slug (`"telegram"`) or the raw
-/// provider-prefixed form the socket layer emits (`"tg:<chat_id>"`,
-/// `"telegram:<chat_id>"`).
-fn channel_is_telegram(channel: &str) -> bool {
- if channel == "telegram" || channel == "tg" {
- return true;
- }
- let provider = channel.split(':').next().unwrap_or("");
- matches!(provider, "telegram" | "tg")
-}