Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(agents): finish external chat replies in their threads (#8398)

gbirmana9456d9f8caf
diff --git a/.sqlx/query-25208ebed599177a660882160bff7be6d5b824eb5105d011ae5022fd13e7bc2e.json b/.sqlx/query-25208ebed599177a660882160bff7be6d5b824eb5105d011ae5022fd13e7bc2e.json
new file mode 100644
index 0000000000..b0a2eddc33
--- /dev/null
+++ b/.sqlx/query-25208ebed599177a660882160bff7be6d5b824eb5105d011ae5022fd13e7bc2e.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO agent_external_replies (session_id, action_id, completion) VALUES ($1, $2, $3) ON CONFLICT (session_id, action_id) DO UPDATE SET completion = COALESCE(agent_external_replies.completion, EXCLUDED.completion), updated_at = now()",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Jsonb"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "25208ebed599177a660882160bff7be6d5b824eb5105d011ae5022fd13e7bc2e"
+}
diff --git a/.sqlx/query-33967c4adbd4740a8727a3b9e9bf9868bec30abd9d7167405ab8538a22061786.json b/.sqlx/query-33967c4adbd4740a8727a3b9e9bf9868bec30abd9d7167405ab8538a22061786.json
new file mode 100644
index 0000000000..ecd32de268
--- /dev/null
+++ b/.sqlx/query-33967c4adbd4740a8727a3b9e9bf9868bec30abd9d7167405ab8538a22061786.json
@@ -0,0 +1,16 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "INSERT INTO agent_external_replies (session_id, action_id, target) VALUES ($1, $2, $3) ON CONFLICT (session_id, action_id) DO UPDATE SET target = COALESCE(agent_external_replies.target, EXCLUDED.target), updated_at = now()",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid",
+ "Jsonb"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "33967c4adbd4740a8727a3b9e9bf9868bec30abd9d7167405ab8538a22061786"
+}
diff --git a/.sqlx/query-3c40a776fab0a69c58a9e678f4105de20ab0211e16ebee133c525038756a8955.json b/.sqlx/query-3c40a776fab0a69c58a9e678f4105de20ab0211e16ebee133c525038756a8955.json
new file mode 100644
index 0000000000..ad4dcd95ab
--- /dev/null
+++ b/.sqlx/query-3c40a776fab0a69c58a9e678f4105de20ab0211e16ebee133c525038756a8955.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE agent_external_replies SET updated_at = now() WHERE session_id = $1 AND action_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "3c40a776fab0a69c58a9e678f4105de20ab0211e16ebee133c525038756a8955"
+}
diff --git a/.sqlx/query-4617fc1eaba9af17740995099cef925249e1588ac5cdb06f923d7ebe8857fdc5.json b/.sqlx/query-4617fc1eaba9af17740995099cef925249e1588ac5cdb06f923d7ebe8857fdc5.json
new file mode 100644
index 0000000000..8189da4504
--- /dev/null
+++ b/.sqlx/query-4617fc1eaba9af17740995099cef925249e1588ac5cdb06f923d7ebe8857fdc5.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE agent_external_replies SET announced = TRUE WHERE session_id = $1 AND action_id = $2",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "4617fc1eaba9af17740995099cef925249e1588ac5cdb06f923d7ebe8857fdc5"
+}
diff --git a/.sqlx/query-a5663ccacceeaf52b7cacdaf1b1c4bf6f8ed6f388431936b4acd4a4d7a0b5ef2.json b/.sqlx/query-a5663ccacceeaf52b7cacdaf1b1c4bf6f8ed6f388431936b4acd4a4d7a0b5ef2.json
new file mode 100644
index 0000000000..e4143b653a
--- /dev/null
+++ b/.sqlx/query-a5663ccacceeaf52b7cacdaf1b1c4bf6f8ed6f388431936b4acd4a4d7a0b5ef2.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT pg_try_advisory_xact_lock(hashtextextended($1, 48)) AS \"claimed!\"",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "claimed!",
+ "type_info": "Bool"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Text"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "a5663ccacceeaf52b7cacdaf1b1c4bf6f8ed6f388431936b4acd4a4d7a0b5ef2"
+}
diff --git a/.sqlx/query-a9c50843be9eb3599ca16eb6ff151bd53a1d93d00f410b9149bd1a739a506072.json b/.sqlx/query-a9c50843be9eb3599ca16eb6ff151bd53a1d93d00f410b9149bd1a739a506072.json
new file mode 100644
index 0000000000..0257ee94d3
--- /dev/null
+++ b/.sqlx/query-a9c50843be9eb3599ca16eb6ff151bd53a1d93d00f410b9149bd1a739a506072.json
@@ -0,0 +1,22 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT to_jsonb(reply) AS \"value!\" FROM agent_external_replies reply WHERE target IS NOT NULL AND NOT finalized AND (completion IS NOT NULL OR created_at > now() - interval '24 hours' OR updated_at <= now() - interval '1 hour') ORDER BY updated_at, session_id, action_id LIMIT $1",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "value!",
+ "type_info": "Jsonb"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Int8"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "a9c50843be9eb3599ca16eb6ff151bd53a1d93d00f410b9149bd1a739a506072"
+}
diff --git a/.sqlx/query-b2eedf26966f407965c2e7d622ef45fe238ac01a5839f03a351c166f14df282d.json b/.sqlx/query-b2eedf26966f407965c2e7d622ef45fe238ac01a5839f03a351c166f14df282d.json
new file mode 100644
index 0000000000..209d92dcac
--- /dev/null
+++ b/.sqlx/query-b2eedf26966f407965c2e7d622ef45fe238ac01a5839f03a351c166f14df282d.json
@@ -0,0 +1,53 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n SELECT id, agent_session_id, user_id AS \"user_id: MacroUserIdStr\",\n direction, content, created_at\n FROM agent_session_log\n WHERE agent_session_id = $1\n AND EXISTS (\n SELECT 1 FROM agent_session_log AS prompt\n WHERE prompt.agent_session_id = $1\n AND prompt.direction = 'to_runtime'\n AND prompt.content->>'method' = 'session/prompt'\n AND prompt.content->>'id' = $2\n )\n ORDER BY created_at, id\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "agent_session_id",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 2,
+ "name": "user_id: MacroUserIdStr",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 3,
+ "name": "direction",
+ "type_info": "Text"
+ },
+ {
+ "ordinal": 4,
+ "name": "content",
+ "type_info": "Jsonb"
+ },
+ {
+ "ordinal": 5,
+ "name": "created_at",
+ "type_info": "Timestamptz"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Text"
+ ]
+ },
+ "nullable": [
+ false,
+ false,
+ true,
+ false,
+ false,
+ false
+ ]
+ },
+ "hash": "b2eedf26966f407965c2e7d622ef45fe238ac01a5839f03a351c166f14df282d"
+}
diff --git a/.sqlx/query-e6b8e45854aa72c1ce7680892effad427be91805e1363ed0baf1f199f5f74418.json b/.sqlx/query-e6b8e45854aa72c1ce7680892effad427be91805e1363ed0baf1f199f5f74418.json
new file mode 100644
index 0000000000..ed46c2b129
--- /dev/null
+++ b/.sqlx/query-e6b8e45854aa72c1ce7680892effad427be91805e1363ed0baf1f199f5f74418.json
@@ -0,0 +1,23 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "SELECT to_jsonb(reply) AS \"value!\" FROM agent_external_replies reply WHERE session_id = $1 AND action_id = $2",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "value!",
+ "type_info": "Jsonb"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ null
+ ]
+ },
+ "hash": "e6b8e45854aa72c1ce7680892effad427be91805e1363ed0baf1f199f5f74418"
+}
diff --git a/.sqlx/query-f6086ce0947015c7b6bc60e3989d0e47dd16be52a456c8891998cb65180cab02.json b/.sqlx/query-f6086ce0947015c7b6bc60e3989d0e47dd16be52a456c8891998cb65180cab02.json
new file mode 100644
index 0000000000..88921af4ff
--- /dev/null
+++ b/.sqlx/query-f6086ce0947015c7b6bc60e3989d0e47dd16be52a456c8891998cb65180cab02.json
@@ -0,0 +1,15 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "UPDATE agent_external_replies SET finalized = TRUE, updated_at = now() WHERE session_id = $1 AND action_id = $2 AND target IS NOT NULL",
+ "describe": {
+ "columns": [],
+ "parameters": {
+ "Left": [
+ "Uuid",
+ "Uuid"
+ ]
+ },
+ "nullable": []
+ },
+ "hash": "f6086ce0947015c7b6bc60e3989d0e47dd16be52a456c8891998cb65180cab02"
+}
diff --git a/crates/agent_harness/AGENTS.md b/crates/agent_harness/AGENTS.md
index 3646d2346c..ee413d9518 100644
--- a/crates/agent_harness/AGENTS.md
+++ b/crates/agent_harness/AGENTS.md
@@ -45,6 +45,17 @@ External runtimes receive the session's configured MCP selection when binding,
including enabled owner connections and custom servers under `OwnerConnections`.
Use `external_mcp_servers` to preserve the host-reachable egress URL override.
+External chat replies use `domain/service/external_replies.rs` and the
+`agent_external_replies` journal. Macrod sends the triggering message ID as the
+control action ID; the harness joins that completion to its authorized reply
+origin, regardless of arrival order. Recovery reads terminal results from the
+original durable action history when an observer callback was lost, including
+turns hidden by a later session load, and retries message updates
+without re-prompting. Keep deduplication inside the serialized command worker.
+Remember successful announcements. After 24 hours, recover replies without a
+persisted completion at most hourly, preserving late answers after lost callbacks.
+Persisted completions bypass this backoff; direct completion handling is immediate.
+
## Checks
- `cargo test -p agent_harness` (`SQLX_OFFLINE` unset).
diff --git a/crates/agent_harness/src/domain/external_replies.rs b/crates/agent_harness/src/domain/external_replies.rs
new file mode 100644
index 0000000000..f4f83dbfda
--- /dev/null
+++ b/crates/agent_harness/src/domain/external_replies.rs
@@ -0,0 +1,88 @@
+//! Join an external runtime's turn with its independently delivered thread reply.
+
+use agent_runtime_protocol::domain::action::AgentActionId;
+use agent_session::domain::{error::Result, model::AgentSessionId};
+use macro_user_id::user_id::MacroUserIdStr;
+use macro_uuid::Uuid;
+
+use super::model::{AnnounceOrigin, ReplyOutcome};
+
+/// The authorized destination of one external chat reply.
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+pub struct ExternalReplyTarget {
+ /// The observed message's origin, never inferred from the current turn.
+ pub origin: AnnounceOrigin,
+ /// The user who caused the reply; current access is checked before posting.
+ pub actor: MacroUserIdStr<'static>,
+ /// Reserved before posting so a retry addresses the same message.
+ pub message_id: Uuid,
+}
+
+/// The terminal result, retained even if the announcement has not arrived yet.
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
+pub struct ExternalReplyCompletion {
+ /// Turn position reported by the session fold.
+ pub turn: agent_fold::domain::model::TurnId,
+ /// The answer or terminal failure to show in the thread.
+ pub outcome: ReplyOutcome,
+}
+
+/// A join of the announcement and completion of one externally delivered prompt.
+#[derive(Debug, Clone, serde::Deserialize)]
+pub struct ExternalReply {
+ /// Session whose runtime produced the response.
+ pub session_id: AgentSessionId,
+ /// The triggering message ID, also sent as the runtime action ID.
+ pub action_id: AgentActionId,
+ /// Absent if the runtime finished before the broker delivered the announcement.
+ pub target: Option<ExternalReplyTarget>,
+ /// Absent while the runtime is still working.
+ pub completion: Option<ExternalReplyCompletion>,
+ /// First reservation/result time; retries never reset the recovery backoff.
+ pub created_at: chrono::DateTime<chrono::Utc>,
+ /// Whether the pending message was successfully posted.
+ pub announced: bool,
+ /// Whether the reply was shown or its destination permanently disappeared.
+ pub finalized: bool,
+}
+
+/// Held while reconciling a reply; releasing it must also work on cancellation.
+pub trait ExternalReplyLease: Send {}
+
+/// Durable reply bookkeeping. Authorization and posting policy stay in the harness.
+/// Object safe because the host supplies this optional persistence capability.
+#[async_trait::async_trait]
+pub trait ExternalReplyStore: Send + Sync + 'static {
+ /// Keep the first authorized destination and reserved reply ID.
+ async fn reserve(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ target: &ExternalReplyTarget,
+ ) -> Result<()>;
+ /// Keep the first terminal result, including when it precedes the destination.
+ async fn complete(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ completion: &ExternalReplyCompletion,
+ ) -> Result<()>;
+ /// Read one reply after obtaining its reconciliation lease.
+ async fn get(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> Result<Option<ExternalReply>>;
+ /// Serialize posting across replicas without holding a row lock during HTTP calls.
+ async fn claim(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> Result<Option<Box<dyn ExternalReplyLease>>>;
+ /// Remember a successful post; a lost response still retries the reserved ID.
+ async fn mark_announced(&self, session: AgentSessionId, action: AgentActionId) -> Result<()>;
+ /// Finish after posting succeeds or the origin is permanently invalid.
+ async fn finalize(&self, session: AgentSessionId, action: AgentActionId) -> Result<()>;
+ /// Bounded recovery of unfinished replies, including missing completion callbacks.
+ async fn pending(&self, limit: u16) -> Result<Vec<ExternalReply>>;
+}
diff --git a/crates/agent_harness/src/domain/mod.rs b/crates/agent_harness/src/domain/mod.rs
index 11adf610a8..31a42fc133 100644
--- a/crates/agent_harness/src/domain/mod.rs
+++ b/crates/agent_harness/src/domain/mod.rs
@@ -4,6 +4,7 @@ pub mod conversation_turns;
/// Agent conversations in channels: who may prompt them, and their sessions.
pub mod conversations;
pub mod error;
+pub mod external_replies;
pub mod model;
/// Which lifecycle facts become notifications for people, and for whom.
pub mod notifications;
diff --git a/crates/agent_harness/src/domain/service.rs b/crates/agent_harness/src/domain/service.rs
index e8e359cb0e..3343615ef5 100644
--- a/crates/agent_harness/src/domain/service.rs
+++ b/crates/agent_harness/src/domain/service.rs
@@ -18,6 +18,7 @@ mod test;
mod admission;
mod deliver;
+mod external_replies;
mod lifecycle;
mod lifecycle_events;
mod open;
@@ -141,6 +142,7 @@ struct AgentHarnessInner<
coding_agents: Box<dyn ErasedCodingAgentSource>,
conversations: Option<Arc<dyn crate::domain::conversations::ConversationPolicy>>,
conversation_turns: Option<Arc<dyn crate::domain::conversation_turns::ConversationTurnStore>>,
+ external_replies: Option<Arc<dyn crate::domain::external_replies::ExternalReplyStore>>,
defaults: HarnessDefaults,
/// Turn-occupying actions waiting for their session's running turn to
/// end. In-memory working copy; the session store is the durable source.
@@ -360,6 +362,7 @@ where
coding_agents: Box::new(coding_agents),
conversations,
conversation_turns: None,
+ external_replies: None,
defaults: defaults.into(),
queues: SessionQueues::new(),
hydrated: DashSet::new(),
@@ -600,6 +603,12 @@ where
}
let persona = self.inner.reply_persona(&session).await?;
+ if !persona.is_coding && self.inner.external_replies.is_some() {
+ return self
+ .inner
+ .announce_external_reply(session_id, prompt.origin, prompt.sender)
+ .await;
+ }
self.inner
.announcer
.announce(SessionAnnouncement {
diff --git a/crates/agent_harness/src/domain/service/deliver.rs b/crates/agent_harness/src/domain/service/deliver.rs
index 7f9a810190..583753fdc9 100644
--- a/crates/agent_harness/src/domain/service/deliver.rs
+++ b/crates/agent_harness/src/domain/service/deliver.rs
@@ -301,6 +301,10 @@ where
| ReplyOutcome::AwaitingApproval(_)
| ReplyOutcome::Resumed
);
+ if terminal && turn.announce.is_none() {
+ self.complete_external_reply(session_id, turn.action_id, turn.turn, outcome.clone())
+ .await;
+ }
let in_segments = turn.announce.as_ref().is_some_and(|origin| {
origin.reply_placement.voice_style() == crate::domain::model::VoiceStyle::Segments
});
diff --git a/crates/agent_harness/src/domain/service/external_replies.rs b/crates/agent_harness/src/domain/service/external_replies.rs
new file mode 100644
index 0000000000..679172af5d
--- /dev/null
+++ b/crates/agent_harness/src/domain/service/external_replies.rs
@@ -0,0 +1,269 @@
+//! Reconcile an external turn with the reply independently announced by its trigger.
+
+use super::*;
+use crate::domain::external_replies::{
+ ExternalReplyCompletion, ExternalReplyStore, ExternalReplyTarget,
+};
+
+impl<
+ Sessions,
+ Containers,
+ Announcer,
+ Runtimes,
+ PromptContext,
+ PromptComposer,
+ Egress,
+ Lifecycle,
+ Mentions,
+ Notifier,
+>
+ AgentHarnessService<
+ Sessions,
+ Containers,
+ Announcer,
+ Runtimes,
+ PromptContext,
+ PromptComposer,
+ Egress,
+ Lifecycle,
+ Mentions,
+ Notifier,
+ >
+where
+ Sessions: AgentSessionService,
+ Containers: ContainerManager,
+ Announcer: SessionAnnouncer,
+ Runtimes: RuntimeConnections,
+ PromptContext: MessagePromptContext,
+ PromptComposer: AgentPromptComposer,
+ Egress: SandboxEgressProvisioner,
+ Lifecycle: AgentSessionLifecyclePublisher,
+ Mentions: PromptMentions,
+ Notifier: AgentSessionNotifier,
+{
+ /// Enable durable external replies before sharing this service.
+ #[must_use]
+ pub fn with_external_replies(mut self, store: Arc<dyn ExternalReplyStore>) -> Self {
+ Arc::get_mut(&mut self.inner)
+ .expect("configure external replies before cloning the harness")
+ .external_replies = Some(store);
+ self
+ }
+
+ /// Retry channel updates only; recovery never prompts the runtime again.
+ #[tracing::instrument(skip_all, fields(external_reply.pending = tracing::field::Empty), err)]
+ pub async fn recover_external_replies(&self) -> Result<()> {
+ let Some(store) = &self.inner.external_replies else {
+ return Ok(());
+ };
+ let pending = store.pending(100).await?;
+ tracing::Span::current().record("external_reply.pending", pending.len());
+ for reply in pending {
+ if let Err(error) = self
+ .inner
+ .reconcile_external_reply(reply.session_id, reply.action_id)
+ .await
+ {
+ tracing::warn!(?error, session_id = %reply.session_id, action_id = %reply.action_id, "external reply reconciliation failed; will retry");
+ }
+ }
+ Ok(())
+ }
+}
+
+impl<
+ Sessions,
+ Containers,
+ Announcer,
+ Runtimes,
+ PromptContext,
+ PromptComposer,
+ Egress,
+ Lifecycle,
+ Mentions,
+ Notifier,
+>
+ AgentHarnessInner<
+ Sessions,
+ Containers,
+ Announcer,
+ Runtimes,
+ PromptContext,
+ PromptComposer,
+ Egress,
+ Lifecycle,
+ Mentions,
+ Notifier,
+ >
+where
+ Sessions: AgentSessionService,
+ Containers: ContainerManager,
+ Announcer: SessionAnnouncer,
+ Runtimes: RuntimeConnections,
+ PromptContext: MessagePromptContext,
+ PromptComposer: AgentPromptComposer,
+ Egress: SandboxEgressProvisioner,
+ Lifecycle: AgentSessionLifecyclePublisher,
+ Mentions: PromptMentions,
+ Notifier: AgentSessionNotifier,
+{
+ /// Reserve the reply before posting. Both sides use the source message's ID.
+ #[tracing::instrument(skip(self, origin, actor), fields(%session_id, message_id = %origin.message_id), err)]
+ pub(super) async fn announce_external_reply(
+ &self,
+ session_id: AgentSessionId,
+ origin: AnnounceOrigin,
+ actor: MacroUserIdStr<'static>,
+ ) -> Result<()> {
+ let Some(store) = &self.external_replies else {
+ return Ok(());
+ };
+ self.prompt_context
+ .authorize_origin(&actor, &origin)
+ .await?;
+ let action_id = AgentActionId::from_uuid(origin.message_id);
+ store
+ .reserve(
+ session_id,
+ action_id,
+ &ExternalReplyTarget {
+ origin,
+ actor,
+ message_id: macro_uuid::generate_uuid_v7(),
+ },
+ )
+ .await?;
+ if let Err(error) = self.reconcile_external_reply(session_id, action_id).await {
+ tracing::warn!(?error, %session_id, %action_id, "external reply reserved; posting will retry");
+ }
+ Ok(())
+ }
+
+ /// Keep the terminal result before touching the channel, including when
+ /// the observed trigger has not reached this replica yet.
+ #[tracing::instrument(skip_all, fields(%session_id, %action_id, ?turn))]
+ pub(super) async fn complete_external_reply(
+ &self,
+ session_id: AgentSessionId,
+ action_id: AgentActionId,
+ turn: agent_fold::domain::model::TurnId,
+ outcome: ReplyOutcome,
+ ) {
+ let Some(store) = &self.external_replies else {
+ return;
+ };
+ let result = async {
+ let session = self.sessions.get_session(session_id).await?;
+ if AgentKind::for_session(session.bot_id, &session.harness) != AgentKind::External {
+ return Ok::<_, HarnessError>(());
+ }
+ store
+ .complete(
+ session_id,
+ action_id,
+ &ExternalReplyCompletion { turn, outcome },
+ )
+ .await?;
+ self.reconcile_external_reply(session_id, action_id).await
+ }
+ .await;
+ if let Err(error) = result {
+ tracing::error!(?error, %session_id, %action_id, "could not reconcile an external turn's reply");
+ }
+ }
+
+ #[tracing::instrument(skip(self), fields(%session_id, %action_id, external_reply.claimed = false), err)]
+ async fn reconcile_external_reply(
+ &self,
+ session_id: AgentSessionId,
+ action_id: AgentActionId,
+ ) -> Result<()> {
+ let Some(store) = &self.external_replies else {
+ return Ok(());
+ };
+ let Some(_lease) = store.claim(session_id, action_id).await? else {
+ return Ok(());
+ };
+ tracing::Span::current().record("external_reply.claimed", true);
+ let Some(mut reply) = store.get(session_id, action_id).await? else {
+ return Ok(());
+ };
+ if reply.finalized {
+ return Ok(());
+ }
+ let Some(target) = reply.target else {
+ return Ok(());
+ };
+ if let Err(error) = self
+ .prompt_context
+ .authorize_origin(&target.actor, &target.origin)
+ .await
+ {
+ if matches!(error, HarnessError::InvalidOrigin(_)) {
+ store.finalize(session_id, action_id).await?;
+ }
+ return Err(error);
+ }
+ if reply.completion.is_none() {
+ use agent_session::domain::action_history::RecordedAction;
+ let completion = match self.sessions.recorded_action(session_id, action_id).await? {
+ Some(RecordedAction::Finished {
+ turn,
+ stop,
+ last_text,
+ }) => Some(ExternalReplyCompletion {
+ turn,
+ outcome: ReplyOutcome::of_turn(&stop, last_text),
+ }),
+ Some(RecordedAction::Interrupted { turn }) => Some(ExternalReplyCompletion {
+ turn,
+ outcome: ReplyOutcome::Failed,
+ }),
+ Some(RecordedAction::Running { .. }) | None => None,
+ };
+ if let Some(completion) = completion {
+ store.complete(session_id, action_id, &completion).await?;
+ reply.completion = Some(completion);
+ }
+ }
+ let session = self.sessions.get_session(session_id).await?;
+ // Posting a reserved ID is idempotent and never overwrites a completed
+ // or deleted message. A lost post response can therefore be retried.
+ if !reply.announced {
+ self.announcer
+ .announce(SessionAnnouncement {
+ reply_message_id: Some(target.message_id),
+ reply_placement: target.origin.reply_placement,
+ reuse_origin_message: false,
+ session_id,
+ bot_id: session.bot_id,
+ is_coding: false,
+ origin_parent: target.origin.parent.clone(),
+ origin_thread_id: target.origin.thread_id,
+ origin_message_id: target.origin.message_id,
+ prompted_message_id: MessageId::first(AuthorKind::User),
+ prompted_content: String::new(),
+ triggered_by: target.actor.clone(),
+ })
+ .await?;
+ store.mark_announced(session_id, action_id).await?;
+ }
+ if let Some(completion) = reply.completion {
+ self.announcer
+ .resolve(ResolvedReply {
+ session_id,
+ bot_id: session.bot_id,
+ is_coding: false,
+ message_id: target.message_id,
+ origin_parent: target.origin.parent,
+ triggered_by: target.actor,
+ outcome: completion.outcome,
+ turn: completion.turn,
+ segments: Vec::new(),
+ })
+ .await?;
+ store.finalize(session_id, action_id).await?;
+ }
+ Ok(())
+ }
+}
diff --git a/crates/agent_harness/src/domain/service/open.rs b/crates/agent_harness/src/domain/service/open.rs
index 12341d407d..b798916352 100644
--- a/crates/agent_harness/src/domain/service/open.rs
+++ b/crates/agent_harness/src/domain/service/open.rs
@@ -106,7 +106,7 @@ where
request.instructions.or(profile_instructions),
)
.await?;
- let session = self
+ let created = self
.inner
.sessions
.create_session(CreateAgentSessionParams {
@@ -130,12 +130,82 @@ where
// The thread linkage is the caller's claim, not an observed
// mention; it must not grant the channel anything.
})
- .await?;
- self.inner.publish_opened(&session).await;
+ .await;
+ let (session, existing) = match created {
+ Ok(session) => (session, false),
+ Err(AgentSessionError::SessionIdTaken(id))
+ if self.inner.external_replies.is_some() && request.thread.is_some() =>
+ {
+ let session = self.inner.sessions.get_session(id).await?;
+ if session.bot_id != request.bot_id
+ || session.thread_id != request.thread.as_ref().map(|thread| thread.thread_id)
+ || session.owner_user()? != &owner_user
+ {
+ return Err(AgentSessionError::SessionIdTaken(id));
+ }
+ (session, true)
+ }
+ Err(AgentSessionError::ThreadSessionExists)
+ if self.inner.external_replies.is_some() =>
+ {
+ let thread = request
+ .thread
+ .as_ref()
+ .ok_or(AgentSessionError::ThreadSessionExists)?;
+ match self
+ .inner
+ .sessions
+ .find_for_thread(Some(thread.thread_id), Some(request.bot_id))
+ .await?
+ {
+ agent_session::domain::model::ThreadSession::CreatedFromThread(session) => {
+ (session, true)
+ }
+ agent_session::domain::model::ThreadSession::None => {
+ return Err(AgentSessionError::ThreadSessionExists);
+ }
+ }
+ }
+ Err(error) => return Err(error),
+ };
+ if !existing {
+ self.inner.publish_opened(&session).await;
+ }
if let Some(thread) = request.thread {
+ let persona = self
+ .inner
+ .reply_persona(&session)
+ .await
+ .map_err(into_session_error)?;
+ if !persona.is_coding
+ && !thread.reuse_origin_message
+ && self.inner.external_replies.is_some()
+ {
+ self.inner
+ .announce_external_reply(
+ session.id,
+ AnnounceOrigin {
+ reply_placement: Default::default(),
+ reuse_origin_message: false,
+ parent: thread.parent,
+ thread_id: thread.thread_id,
+ message_id: thread.message_id,
+ },
+ owner_user,
+ )
+ .await
+ .map_err(into_session_error)?;
+ return if existing {
+ Err(AgentSessionError::ThreadSessionExists)
+ } else {
+ Ok(session)
+ };
+ }
+ if existing {
+ return Err(AgentSessionError::ThreadSessionExists);
+ }
let announce = async {
- let persona = self.inner.reply_persona(&session).await?;
let announcement = SessionAnnouncement {
reply_message_id: None,
reply_placement: Default::default(),
@@ -150,7 +220,7 @@ where
prompted_content: thread.content,
triggered_by: owner_user,
};
- self.inner.announcer.announce(announcement).await
+ self.inner.announcer.announce(announcement).await.map(drop)
};
if let Err(error) = announce.await {
tracing::warn!(
@@ -161,6 +231,9 @@ where
}
}
+ if existing {
+ return Err(AgentSessionError::ThreadSessionExists);
+ }
Ok(session)
}
diff --git a/crates/agent_harness/src/domain/service/queue.rs b/crates/agent_harness/src/domain/service/queue.rs
index efda4d3c25..93f48ddbd1 100644
--- a/crates/agent_harness/src/domain/service/queue.rs
+++ b/crates/agent_harness/src/domain/service/queue.rs
@@ -500,8 +500,18 @@ where
stop,
last_text,
action_id: fold_action_id,
+ turn,
..
}) => {
+ if let Some(action) = fold_action_id {
+ self.complete_external_reply(
+ session_id,
+ action,
+ turn,
+ ReplyOutcome::of_turn(&stop, last_text.clone()),
+ )
+ .await;
+ }
let mut ended = self.busy.turn(session_id);
if let (Some(turn), Some(action)) = (&ended, fold_action_id)
&& turn.action_id != action
@@ -804,10 +814,10 @@ where
/// An action whose id this session already holds is the same action
/// arriving twice - a caller retrying a control request under the id it
/// named. It reports what became of the first copy instead of queueing a
- /// second, so a retry cannot double-prompt. Only what this replica still
- /// holds is checked: an id whose action has already finished its turn is
- /// no longer anywhere to be seen, and accepting it again is indistinguishable
- /// from asking for the same thing twice on purpose.
+ /// second, so a retry cannot double-prompt. External sessions with a reply
+ /// store also check durable completion and original prompt history, so
+ /// finished actions remain deduplicated across reconnects and restarts.
+ /// Other sessions check only this replica's queued and in-flight actions.
///
/// A channel follow-up (`announce` set) that lands on a running turn
/// steers: it goes to the front of the queue, a stop cancels the current
@@ -820,6 +830,14 @@ where
) -> Result<CommandOutcome> {
self.authorize_action(&command).await?;
let action_id = command.id;
+ if let Some(store) = &self.external_replies
+ && store
+ .get(session_id, action_id)
+ .await?
+ .is_some_and(|reply| reply.completion.is_some())
+ {
+ return Ok(CommandOutcome::Completed);
+ }
if self.queues.contains(session_id, action_id) {
if command.announce.as_ref().is_some_and(|origin| {
origin.reply_placement == crate::domain::model::ReplyPlacement::Timeline
@@ -841,6 +859,18 @@ where
return Ok(CommandOutcome::Completed);
}
let session = self.sessions.get_session(session_id).await?;
+ // A persisted prompt proves delivery even if this replica has not yet
+ // recovered its completion callback. Never execute its effects again.
+ if self.external_replies.is_some()
+ && AgentKind::for_session(session.bot_id, &session.harness) == AgentKind::External
+ && self
+ .sessions
+ .recorded_action(session_id, action_id)
+ .await?
+ .is_some()
+ {
+ return Ok(CommandOutcome::Completed);
+ }
if let Err(error) = self.admit_session(&session).await {
// Forwarding acknowledges bus acceptance before this worker runs.
// Publish even an ingress refusal so the submitting replica hears it.
diff --git a/crates/agent_harness/src/domain/service/test.rs b/crates/agent_harness/src/domain/service/test.rs
index d5407ff36e..4746206036 100644
--- a/crates/agent_harness/src/domain/service/test.rs
+++ b/crates/agent_harness/src/domain/service/test.rs
@@ -5,6 +5,7 @@
mod chat_reply;
mod coding_preferences;
mod external_mcp;
+mod external_replies;
mod owned_purge;
mod presentation;
mod quota;
diff --git a/crates/agent_harness/src/domain/service/test/external_replies.rs b/crates/agent_harness/src/domain/service/test/external_replies.rs
new file mode 100644
index 0000000000..65577467ad
--- /dev/null
+++ b/crates/agent_harness/src/domain/service/test/external_replies.rs
@@ -0,0 +1,698 @@
+use super::*;
+use crate::domain::external_replies::*;
+use crate::domain::model::{AnnouncePrompt, ReplyOutcome};
+use agent_fold::domain::model::{StopReason, TurnId};
+use std::collections::HashMap;
+
+type Key = (AgentSessionId, AgentActionId);
+#[derive(Default)]
+struct ReplyStore {
+ rows: Mutex<HashMap<Key, ExternalReply>>,
+ lock: Arc<tokio::sync::Mutex<()>>,
+ fail_reserve: std::sync::atomic::AtomicBool,
+}
+impl ExternalReplyLease for tokio::sync::OwnedMutexGuard<()> {}
+#[async_trait::async_trait]
+impl ExternalReplyStore for ReplyStore {
+ async fn reserve(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ target: &ExternalReplyTarget,
+ ) -> agent_session::domain::error::Result<()> {
+ if self
+ .fail_reserve
+ .swap(false, std::sync::atomic::Ordering::SeqCst)
+ {
+ return Err(rootcause::report!("reservation temporarily unavailable").into());
+ }
+ self.rows
+ .lock()
+ .unwrap()
+ .entry((session, action))
+ .or_insert(ExternalReply {
+ session_id: session,
+ action_id: action,
+ target: None,
+ completion: None,
+ created_at: chrono::Utc::now(),
+ announced: false,
+ finalized: false,
+ })
+ .target
+ .get_or_insert(target.clone());
+ Ok(())
+ }
+ async fn complete(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ completion: &ExternalReplyCompletion,
+ ) -> agent_session::domain::error::Result<()> {
+ let mut rows = self.rows.lock().unwrap();
+ let row = rows.entry((session, action)).or_insert(ExternalReply {
+ session_id: session,
+ action_id: action,
+ target: None,
+ completion: None,
+ created_at: chrono::Utc::now(),
+ announced: false,
+ finalized: false,
+ });
+ if row.completion.is_none() {
+ row.completion = Some(completion.clone());
+ }
+ Ok(())
+ }
+ async fn get(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> agent_session::domain::error::Result<Option<ExternalReply>> {
+ Ok(self.rows.lock().unwrap().get(&(session, action)).cloned())
+ }
+ async fn claim(
+ &self,
+ _: AgentSessionId,
+ _: AgentActionId,
+ ) -> agent_session::domain::error::Result<Option<Box<dyn ExternalReplyLease>>> {
+ Ok(self
+ .lock
+ .clone()
+ .try_lock_owned()
+ .ok()
+ .map(|lease| Box::new(lease) as Box<dyn ExternalReplyLease>))
+ }
+ async fn mark_announced(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> agent_session::domain::error::Result<()> {
+ self.rows
+ .lock()
+ .unwrap()
+ .get_mut(&(session, action))
+ .unwrap()
+ .announced = true;
+ Ok(())
+ }
+ async fn finalize(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> agent_session::domain::error::Result<()> {
+ self.rows
+ .lock()
+ .unwrap()
+ .get_mut(&(session, action))
+ .unwrap()
+ .finalized = true;
+ Ok(())
+ }
+ async fn pending(
+ &self,
+ limit: u16,
+ ) -> agent_session::domain::error::Result<Vec<ExternalReply>> {
+ Ok(self
+ .rows
+ .lock()
+ .unwrap()
+ .values()
+ .filter(|row| row.target.is_some() && !row.finalized)
+ .take(limit.into())
+ .cloned()
+ .collect())
+ }
+}
+
+#[tokio::test]
+async fn an_external_mention_resolves_and_a_followup_can_finish_before_its_announcement() {
+ let store = Arc::new(ReplyStore::default());
+ let mut service =
+ harness_sharing_repo(InMemoryAgentSessionRepo::new()).with_external_replies(store.clone());
+ Arc::get_mut(&mut service.inner).unwrap().coding_agents = Box::new(ChosenCodingAgents(false));
+ let source = macro_uuid::generate_uuid_v7();
+ let mut request = open_external_request("/workspace");
+ request.thread = Some(agent_session::domain::ports::SessionThread {
+ reuse_origin_message: false,
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: source,
+ content: "hello @ehayes".into(),
+ });
+ let session = service.open_external_session(request).await.unwrap();
+ let announced = service.inner.announcer.announced_messages();
+ assert_eq!(announced.len(), 1);
+ service
+ .execute_here(
+ session.id,
+ HarnessCommand::Turn(TurnSignal::TurnEnded {
+ turn: TurnId(0),
+ action_id: Some(AgentActionId::from_uuid(source)),
+ stop: StopReason::EndTurn,
+ last_text: Some("I am Eric Hayes.".into()),
+ }),
+ )
+ .await
+ .unwrap();
+ let replies = service.inner.announcer.resolved();
+ assert_eq!(replies.len(), 1);
+ assert_eq!(replies[0].message_id, announced[0].message_id);
+ assert_eq!(
+ replies[0].outcome,
+ ReplyOutcome::Answered("I am Eric Hayes.".into())
+ );
+
+ let followup = macro_uuid::generate_uuid_v7();
+ service
+ .execute_here(
+ session.id,
+ HarnessCommand::Turn(TurnSignal::TurnEnded {
+ turn: TurnId(1),
+ action_id: Some(AgentActionId::from_uuid(followup)),
+ stop: StopReason::EndTurn,
+ last_text: Some("I am Eric Hayes, again.".into()),
+ }),
+ )
+ .await
+ .unwrap();
+ assert_eq!(service.inner.announcer.resolved().len(), 1);
+ let prompt = AnnouncePrompt {
+ bot_id: session.bot_id,
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: followup,
+ },
+ content: "again".into(),
+ sender: sender(),
+ };
+ service
+ .announce_external_prompt(session.id, prompt.clone())
+ .await
+ .unwrap();
+ service
+ .announce_external_prompt(session.id, prompt)
+ .await
+ .unwrap();
+ assert_eq!(service.inner.announcer.announced_messages().len(), 2);
+ let replies = service.inner.announcer.resolved();
+ assert_eq!(replies.len(), 2);
+ assert_ne!(replies[0].message_id, replies[1].message_id);
+ assert_eq!(
+ replies[1].outcome,
+ ReplyOutcome::Answered("I am Eric Hayes, again.".into())
+ );
+ assert!(store.pending(100).await.unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn failed_channel_updates_retry_after_restart_without_reprompting() {
+ let repo = InMemoryAgentSessionRepo::new();
+ let store = Arc::new(ReplyStore::default());
+ let mut service = harness_sharing_repo(repo.clone()).with_external_replies(store.clone());
+ Arc::get_mut(&mut service.inner).unwrap().coding_agents = Box::new(ChosenCodingAgents(false));
+ let session = service
+ .open_external_session(open_external_request("/workspace"))
+ .await
+ .unwrap();
+ let source = macro_uuid::generate_uuid_v7();
+ service
+ .announce_external_prompt(
+ session.id,
+ AnnouncePrompt {
+ bot_id: session.bot_id,
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: source,
+ },
+ content: "hello".into(),
+ sender: sender(),
+ },
+ )
+ .await
+ .unwrap();
+ let original = service.inner.announcer.announced_messages()[0].message_id;
+ service.inner.announcer.fails("message service unavailable");
+ service
+ .execute_here(
+ session.id,
+ HarnessCommand::Turn(TurnSignal::TurnEnded {
+ turn: TurnId(0),
+ action_id: Some(AgentActionId::from_uuid(source)),
+ stop: StopReason::Cancelled,
+ last_text: None,
+ }),
+ )
+ .await
+ .unwrap();
+ assert_eq!(store.pending(100).await.unwrap().len(), 1);
+ let restarted = harness_sharing_repo(repo).with_external_replies(store.clone());
+ restarted.recover_external_replies().await.unwrap();
+ let replies = restarted.inner.announcer.resolved();
+ assert_eq!(replies.len(), 1);
+ assert_eq!(replies[0].message_id, original);
+ assert_eq!(replies[0].outcome, ReplyOutcome::Cancelled);
+ let duplicate = restarted
+ .execute_here(
+ session.id,
+ HarnessCommand::Deliver(DeliverAction::control(ControlEvent {
+ action_id: Some(AgentActionId::from_uuid(source)),
+ action: AgentAction::prompt("retry"),
+ actor: Some(sender()),
+ context: None,
+ })),
+ )
+ .await
+ .unwrap();
+ assert!(matches!(duplicate, CommandOutcome::Completed));
+ assert!(restarted.inner.queues.list(session.id).is_empty());
+ assert!(store.pending(100).await.unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn recovery_rebuilds_a_missed_completion_from_the_durable_protocol_log() {
+ let repo = InMemoryAgentSessionRepo::new();
+ let store = Arc::new(ReplyStore::default());
+ let mut service = harness_sharing_repo(repo.clone()).with_external_replies(store.clone());
+ Arc::get_mut(&mut service.inner).unwrap().coding_agents = Box::new(ChosenCodingAgents(false));
+ let session = service
+ .open_external_session(open_external_request("/workspace"))
+ .await
+ .unwrap();
+ let source = macro_uuid::generate_uuid_v7();
+ service
+ .announce_external_prompt(
+ session.id,
+ AnnouncePrompt {
+ bot_id: session.bot_id,
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: source,
+ },
+ content: "hello".into(),
+ sender: sender(),
+ },
+ )
+ .await
+ .unwrap();
+ let original = service.inner.announcer.announced_messages()[0].message_id;
+ // The actor committed the answer, but the process died before TurnEnded
+ // reached the harness. A new harness has only the log and reply journal.
+ repo.extend_log(agent_fold::testing::parse_log_as(
+ session.id,
+ &agent_fold::testing::TURN.replace("prompt-1", &source.to_string()),
+ ));
+ store
+ .rows
+ .lock()
+ .unwrap()
+ .get_mut(&(session.id, AgentActionId::from_uuid(source)))
+ .unwrap()
+ .created_at -= chrono::TimeDelta::hours(25);
+ let restarted = harness_sharing_repo(repo).with_external_replies(store.clone());
+ // A retry can beat the periodic recovery worker; the durable prompt alone
+ // must prevent re-executing it.
+ let duplicate = restarted
+ .execute_here(
+ session.id,
+ HarnessCommand::Deliver(DeliverAction::control(ControlEvent {
+ action_id: Some(AgentActionId::from_uuid(source)),
+ action: AgentAction::prompt("retry"),
+ actor: Some(sender()),
+ context: None,
+ })),
+ )
+ .await
+ .unwrap();
+ assert!(matches!(duplicate, CommandOutcome::Completed));
+ assert!(restarted.inner.queues.list(session.id).is_empty());
+ restarted.recover_external_replies().await.unwrap();
+ let replies = restarted.inner.announcer.resolved();
+ assert_eq!(replies.len(), 1);
+ assert_eq!(replies[0].message_id, original);
+ assert_eq!(replies[0].outcome, ReplyOutcome::Answered("Done.".into()));
+ assert!(store.pending(100).await.unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn successful_announcements_are_not_repeated_after_restart() {
+ let repo = InMemoryAgentSessionRepo::new();
+ let store = Arc::new(ReplyStore::default());
+ let mut service = harness_sharing_repo(repo.clone()).with_external_replies(store.clone());
+ Arc::get_mut(&mut service.inner).unwrap().coding_agents = Box::new(ChosenCodingAgents(false));
+ let session = service
+ .open_external_session(open_external_request("/workspace"))
+ .await
+ .unwrap();
+ let source = macro_uuid::generate_uuid_v7();
+ let action = AgentActionId::from_uuid(source);
+ service
+ .announce_external_prompt(
+ session.id,
+ AnnouncePrompt {
+ bot_id: session.bot_id,
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: source,
+ },
+ content: "hello".into(),
+ sender: sender(),
+ },
+ )
+ .await
+ .unwrap();
+ let original = service.inner.announcer.announced_messages()[0].message_id;
+ let restarted = harness_sharing_repo(repo).with_external_replies(store.clone());
+ restarted.recover_external_replies().await.unwrap();
+ assert!(restarted.inner.announcer.announced().is_empty());
+ assert!(restarted.inner.announcer.resolved().is_empty());
+ store
+ .rows
+ .lock()
+ .unwrap()
+ .get_mut(&(session.id, action))
+ .unwrap()
+ .created_at -= chrono::TimeDelta::hours(25);
+ restarted.recover_external_replies().await.unwrap();
+ assert_eq!(store.pending(100).await.unwrap().len(), 1);
+ assert!(
+ store
+ .get(session.id, action)
+ .await
+ .unwrap()
+ .unwrap()
+ .completion
+ .is_none()
+ );
+ assert!(restarted.inner.announcer.resolved().is_empty());
+ restarted
+ .inner
+ .complete_external_reply(
+ session.id,
+ action,
+ TurnId(0),
+ ReplyOutcome::Answered("Late answer".into()),
+ )
+ .await;
+ let replies = restarted.inner.announcer.resolved();
+ assert_eq!(replies.len(), 1);
+ assert_eq!(replies[0].message_id, original);
+ assert_eq!(
+ replies[0].outcome,
+ ReplyOutcome::Answered("Late answer".into())
+ );
+ assert!(restarted.inner.announcer.announced().is_empty());
+ assert!(store.pending(100).await.unwrap().is_empty());
+}
+
+#[tokio::test]
+async fn an_unauthorized_destination_never_reserves_or_posts_a_reply() {
+ let store = Arc::new(ReplyStore::default());
+ let mut service =
+ harness_sharing_repo(InMemoryAgentSessionRepo::new()).with_external_replies(store.clone());
+ let inner = Arc::get_mut(&mut service.inner).unwrap();
+ inner.coding_agents = Box::new(ChosenCodingAgents(false));
+ inner.prompt_context = PromptContextMock::unauthorized("access revoked");
+ let session = service
+ .open_external_session(open_external_request("/workspace"))
+ .await
+ .unwrap();
+ let source = macro_uuid::generate_uuid_v7();
+ let result = service
+ .announce_external_prompt(
+ session.id,
+ AnnouncePrompt {
+ bot_id: session.bot_id,
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: source,
+ },
+ content: "hello".into(),
+ sender: sender(),
+ },
+ )
+ .await;
+ assert!(matches!(result, Err(HarnessError::PromptContext(_))));
+ assert!(store.rows.lock().unwrap().is_empty());
+ assert!(service.inner.announcer.announced().is_empty());
+}
+
+#[sqlx::test(migrator = "macro_db_migrator::MACRO_DB_MIGRATIONS")]
+async fn creation_retries_repair_reservation_for_both_primary_key_and_thread_conflicts(
+ pool: sqlx::PgPool,
+) {
+ let macro_user = macro_uuid::generate_uuid_v7();
+ sqlx::query!("INSERT INTO macro_user (id, username, email, stripe_customer_id) VALUES ($1, '[email protected]', '[email protected]', 'external-reply-test')", macro_user).execute(&pool).await.unwrap();
+ sqlx::query!(r#"INSERT INTO "User" (id, email, macro_user_id) VALUES ('macro|[email protected]', '[email protected]', $1)"#, macro_user).execute(&pool).await.unwrap();
+ use bots::domain::models::{BotOwner, CreateBotRequest};
+ use bots::domain::ports::BotRepo;
+ let bot = bots::outbound::pg_bots_repo::PgBotsRepo::new(pool.clone())
+ .create_owned_bot(
+ BotOwner::User {
+ user_id: sender().to_string(),
+ },
+ sender(),
+ CreateBotRequest {
+ team_id: None,
+ name: "External chat agent".into(),
+ handle: format!("external-{}", macro_uuid::generate_uuid_v7()),
+ description: None,
+ avatar_url: None,
+ has_agent: None,
+ },
+ )
+ .await
+ .unwrap()
+ .id;
+ let repo = crate::testing::postgres_sessions(pool.clone());
+ let store = Arc::new(ReplyStore::default());
+ let runtimes = RuntimeRegistry::new();
+ let service = AgentHarnessService::new(
+ None,
+ AgentSessionServiceImpl::new(
+ repo.clone(),
+ FoldedMessageService::new(repo.clone()),
+ NoOpRealtime,
+ NoOpAgentSessionNameGenerator,
+ Arc::new(NoOpTurnObserver),
+ Arc::new(RecordingLifecyclePublisher::new()),
+ ReplicaId::mint(),
+ ),
+ MockContainerManager::new(),
+ AnnouncerMock::new(),
+ TestConnections::new(MirrorBindings, Arc::clone(&runtimes)),
+ PromptContextMock::default(),
+ PromptComposerMock::default(),
+ EgressProvisionerMock::new(),
+ NoPeers,
+ KindDefaultPolicies,
+ ChosenCodingAgents(false),
+ HarnessDefaults::new(SessionDefaults {
+ bot_id: BotId::TEST_A,
+ model: "fixed".into(),
+ harness: "macrod".into(),
+ repo_url: None,
+ }),
+ RecordingLifecyclePublisher::new(),
+ crate::domain::pending::PendingCommands::new(),
+ PromptMentionsMock::new(),
+ NotifierMock::new(),
+ )
+ .with_external_replies(store.clone());
+ let id = AgentSessionId::new();
+ let source = macro_uuid::generate_uuid_v7();
+ use channels::domain::agent_dm::AgentDmRepo;
+ let channel = channels::outbound::pg_channels_repo::PgChannelsRepo::new(pool.clone())
+ .ensure(sender(), bot)
+ .await
+ .unwrap()
+ .dm
+ .channel_id;
+ sqlx::query!("INSERT INTO comms_messages (id, sender_id, content, parent_entity_type, parent_entity_id) VALUES ($1, 'macro|[email protected]', 'hello', 'channel', $2)", source, channel.to_string()).execute(&pool).await.unwrap();
+ let mut request = open_external_request("/workspace");
+ request.id = Some(id);
+ request.bot_id = bot;
+ request.thread = Some(agent_session::domain::ports::SessionThread {
+ reuse_origin_message: false,
+ parent: MessageParent::Channel(channel),
+ thread_id: source,
+ message_id: source,
+ content: "hello".into(),
+ });
+ store
+ .fail_reserve
+ .store(true, std::sync::atomic::Ordering::SeqCst);
+ assert!(
+ service
+ .open_external_session(request.clone())
+ .await
+ .is_err()
+ );
+ assert_eq!(repo.get(id).await.unwrap().id, id);
+ assert!(store.rows.lock().unwrap().is_empty());
+ assert!(matches!(
+ service.open_external_session(request.clone()).await,
+ Err(AgentSessionError::ThreadSessionExists)
+ ));
+ let action = AgentActionId::from_uuid(source);
+ let reply_id = store
+ .get(id, action)
+ .await
+ .unwrap()
+ .unwrap()
+ .target
+ .unwrap()
+ .message_id;
+ request.id = Some(AgentSessionId::new());
+ assert!(matches!(
+ service.open_external_session(request.clone()).await,
+ Err(AgentSessionError::ThreadSessionExists)
+ ));
+ assert_eq!(
+ store
+ .get(id, action)
+ .await
+ .unwrap()
+ .unwrap()
+ .target
+ .unwrap()
+ .message_id,
+ reply_id
+ );
+ assert_eq!(service.inner.announcer.announced_messages().len(), 1);
+ // The runtime commits its answer and later reconnects with session/load,
+ // but the observer callback never reached this harness.
+ for entry in agent_fold::testing::parse_log_as(
+ id,
+ &agent_fold::testing::TURN.replace("prompt-1", &source.to_string()),
+ ) {
+ agent_session::domain::ports::AgentSessionLogRepo::create(&repo, entry)
+ .await
+ .unwrap();
+ }
+ service.inner.sessions.mark_disconnected(id).await.unwrap();
+ let reloaded = include_str!("external_replies/load.jsonl");
+ let mut boundary = None;
+ for entry in agent_fold::testing::parse_log_as(id, reloaded) {
+ let stored = agent_session::domain::ports::AgentSessionLogRepo::create(&repo, entry)
+ .await
+ .unwrap();
+ boundary.get_or_insert(stored.id);
+ }
+ sqlx::query!(
+ "UPDATE agent_session SET history_start_log_id = $2 WHERE id = $1",
+ id.as_uuid(),
+ boundary
+ )
+ .execute(&pool)
+ .await
+ .unwrap();
+ let visible = service.inner.sessions.folded_session_log(id).await.unwrap();
+ assert!(!visible.messages.is_empty());
+ assert!(
+ visible
+ .messages
+ .iter()
+ .all(|message| message.request_id != Some(action))
+ );
+ let duplicate = service
+ .execute_here(
+ id,
+ HarnessCommand::Deliver(DeliverAction::control(ControlEvent {
+ action_id: Some(action),
+ action: AgentAction::prompt("retry"),
+ actor: Some(sender()),
+ context: None,
+ })),
+ )
+ .await
+ .unwrap();
+ assert!(matches!(duplicate, CommandOutcome::Completed));
+ service.recover_external_replies().await.unwrap();
+ let replies = service.inner.announcer.resolved();
+ assert_eq!(replies.len(), 1);
+ assert_eq!(replies[0].message_id, reply_id);
+ assert_eq!(replies[0].outcome, ReplyOutcome::Answered("Done.".into()));
+ assert!(store.pending(100).await.unwrap().is_empty());
+
+ request.bot_id = BotId::TEST_A;
+ request.id = Some(id);
+ assert!(
+ matches!(service.open_external_session(request).await, Err(AgentSessionError::SessionIdTaken(taken)) if taken == id)
+ );
+}
+
+#[tokio::test]
+async fn recovery_finishes_a_turn_interrupted_before_an_acp_response() {
+ let repo = InMemoryAgentSessionRepo::new();
+ let store = Arc::new(ReplyStore::default());
+ let mut service = harness_sharing_repo(repo.clone()).with_external_replies(store.clone());
+ Arc::get_mut(&mut service.inner).unwrap().coding_agents = Box::new(ChosenCodingAgents(false));
+ let session = service
+ .open_external_session(open_external_request("/workspace"))
+ .await
+ .unwrap();
+ let source = macro_uuid::generate_uuid_v7();
+ service
+ .announce_external_prompt(
+ session.id,
+ AnnouncePrompt {
+ bot_id: session.bot_id,
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: MessageParent::Channel(Uuid::from_u128(100)),
+ thread_id: source,
+ message_id: source,
+ },
+ content: "hello".into(),
+ sender: sender(),
+ },
+ )
+ .await
+ .unwrap();
+ let interrupted = agent_fold::testing::TURN
+ .lines()
+ .take(6)
+ .collect::<Vec<_>>()
+ .join("\n")
+ .replace("prompt-1", &source.to_string());
+ repo.extend_log(agent_fold::testing::parse_log_as(session.id, &interrupted));
+ store
+ .rows
+ .lock()
+ .unwrap()
+ .get_mut(&(session.id, AgentActionId::from_uuid(source)))
+ .unwrap()
+ .created_at -= chrono::TimeDelta::hours(25);
+ service.recover_external_replies().await.unwrap();
+ assert!(service.inner.announcer.resolved().is_empty());
+ assert_eq!(store.pending(100).await.unwrap().len(), 1);
+ service
+ .inner
+ .sessions
+ .mark_disconnected(session.id)
+ .await
+ .unwrap();
+ let restarted = harness_sharing_repo(repo).with_external_replies(store.clone());
+ restarted.recover_external_replies().await.unwrap();
+ let replies = restarted.inner.announcer.resolved();
+ assert_eq!(replies.len(), 1);
+ assert_eq!(replies[0].outcome, ReplyOutcome::Failed);
+ assert!(store.pending(100).await.unwrap().is_empty());
+}
diff --git a/crates/agent_harness/src/domain/service/test/external_replies/load.jsonl b/crates/agent_harness/src/domain/service/test/external_replies/load.jsonl
new file mode 100644
index 0000000000..84e46f7b36
--- /dev/null
+++ b/crates/agent_harness/src/domain/service/test/external_replies/load.jsonl
@@ -0,0 +1,7 @@
+{"direction":"to_server","content":{"type":"event","event":"acp_ready"}}
+{"direction":"to_runtime","content":{"type":"acp","jsonrpc":"2.0","id":"init-2","method":"initialize","params":{"protocolVersion":1}}}
+{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","id":"init-2","result":{"protocolVersion":1,"agentCapabilities":{}}}}
+{"direction":"to_runtime","content":{"type":"acp","jsonrpc":"2.0","id":"load-2","method":"session/load","params":{"sessionId":"s1","cwd":"/workspace","mcpServers":[]}}}
+{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"user_message_chunk","content":{"type":"text","text":"replayed prompt"}}}}}
+{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","method":"session/update","params":{"sessionId":"s1","update":{"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":"replayed answer"}}}}}
+{"direction":"to_server","content":{"type":"acp","jsonrpc":"2.0","id":"load-2","result":{}}}
diff --git a/crates/agent_harness/src/outbound/channel_announcer.rs b/crates/agent_harness/src/outbound/channel_announcer.rs
index 035f51aa9b..c98523c70e 100644
--- a/crates/agent_harness/src/outbound/channel_announcer.rs
+++ b/crates/agent_harness/src/outbound/channel_announcer.rs
@@ -398,31 +398,29 @@ impl<Access: EntityAccessService> SessionAnnouncer for MessageAnnouncer<Access>
message_id: announcement.origin_message_id,
});
}
- let posted = self
- .messages
- .post(
- access,
- PostMessage {
- id: announcement.reply_message_id,
- attribution: MessageAttribution::ActingUser,
- // Neither shape is news yet. The chip is a pointer: the
- // thread hears about the session when it finishes or
- // asks, through the lifecycle notifications. The
- // pending reply notifies when it is resolved into the
- // answer, as the answer.
- notification_policy: PostMessageNotificationPolicy::Silent,
- content,
- thread_id: announcement
- .reply_placement
- .thread_id(announcement.origin_thread_id),
- anchor: None,
- mentions: Vec::new(),
- attachments: Vec::new(),
- nonce: None,
- },
- )
- .await
- .map_err(|error| HarnessError::Announce(rootcause::report!(error).into()))?;
+ let input = PostMessage {
+ id: announcement.reply_message_id,
+ attribution: MessageAttribution::ActingUser,
+ // Neither shape is news yet. The chip is a pointer: the
+ // thread hears about the session when it finishes or
+ // asks, through the lifecycle notifications. The
+ // pending reply notifies when it is resolved into the
+ // answer, as the answer.
+ notification_policy: PostMessageNotificationPolicy::Silent,
+ content,
+ thread_id: announcement
+ .reply_placement
+ .thread_id(announcement.origin_thread_id),
+ anchor: None,
+ mentions: Vec::new(),
+ attachments: Vec::new(),
+ nonce: None,
+ };
+ let posted = match announcement.reply_message_id {
+ Some(id) => self.messages.post_from_event(access, id, input).await,
+ None => self.messages.post(access, input).await,
+ }
+ .map_err(|error| HarnessError::Announce(rootcause::report!(error).into()))?;
Ok(AnnouncedMessage {
message_id: posted.id,
})
diff --git a/crates/agent_harness/src/outbound/external_replies.rs b/crates/agent_harness/src/outbound/external_replies.rs
new file mode 100644
index 0000000000..72e59fb787
--- /dev/null
+++ b/crates/agent_harness/src/outbound/external_replies.rs
@@ -0,0 +1,131 @@
+//! PostgreSQL persistence for external-runtime thread replies.
+
+use std::sync::Arc;
+
+use agent_runtime_protocol::domain::action::AgentActionId;
+use agent_session::domain::{error::Result, model::AgentSessionId};
+use sqlx::PgPool;
+
+use crate::domain::external_replies::{
+ ExternalReply, ExternalReplyCompletion, ExternalReplyLease, ExternalReplyStore,
+ ExternalReplyTarget,
+};
+
+/// Durable join of independently delivered announcements and turn completions.
+pub struct PgExternalReplyStore {
+ pool: PgPool,
+ slots: Arc<tokio::sync::Semaphore>,
+}
+
+impl PgExternalReplyStore {
+ /// Leave pool capacity available for the message service while holding leases.
+ pub fn new(pool: PgPool) -> Self {
+ let slots = (pool.options().get_max_connections() as usize / 4).clamp(1, 16);
+ Self {
+ pool,
+ slots: Arc::new(tokio::sync::Semaphore::new(slots)),
+ }
+ }
+}
+
+struct ReplyLease {
+ _transaction: sqlx::Transaction<'static, sqlx::Postgres>,
+ _slot: tokio::sync::OwnedSemaphorePermit,
+}
+impl ExternalReplyLease for ReplyLease {}
+
+fn decode(value: serde_json::Value) -> Result<ExternalReply> {
+ serde_json::from_value(value)
+ .map_err(anyhow::Error::from)
+ .map_err(Into::into)
+}
+
+#[async_trait::async_trait]
+impl ExternalReplyStore for PgExternalReplyStore {
+ #[tracing::instrument(skip(self, target), err)]
+ async fn reserve(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ target: &ExternalReplyTarget,
+ ) -> Result<()> {
+ let target = serde_json::to_value(target).map_err(anyhow::Error::from)?;
+ sqlx::query!("INSERT INTO agent_external_replies (session_id, action_id, target) VALUES ($1, $2, $3) ON CONFLICT (session_id, action_id) DO UPDATE SET target = COALESCE(agent_external_replies.target, EXCLUDED.target), updated_at = now()", session.as_uuid(), action.as_uuid(), target)
+ .execute(&self.pool).await.map_err(anyhow::Error::from)?;
+ Ok(())
+ }
+
+ #[tracing::instrument(skip(self, completion), err)]
+ async fn complete(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ completion: &ExternalReplyCompletion,
+ ) -> Result<()> {
+ let completion = serde_json::to_value(completion).map_err(anyhow::Error::from)?;
+ sqlx::query!("INSERT INTO agent_external_replies (session_id, action_id, completion) VALUES ($1, $2, $3) ON CONFLICT (session_id, action_id) DO UPDATE SET completion = COALESCE(agent_external_replies.completion, EXCLUDED.completion), updated_at = now()", session.as_uuid(), action.as_uuid(), completion)
+ .execute(&self.pool).await.map_err(anyhow::Error::from)?;
+ Ok(())
+ }
+
+ async fn get(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> Result<Option<ExternalReply>> {
+ sqlx::query_scalar!(r#"SELECT to_jsonb(reply) AS "value!" FROM agent_external_replies reply WHERE session_id = $1 AND action_id = $2"#, session.as_uuid(), action.as_uuid())
+ .fetch_optional(&self.pool).await.map_err(anyhow::Error::from)?.map(decode).transpose()
+ }
+
+ async fn claim(
+ &self,
+ session: AgentSessionId,
+ action: AgentActionId,
+ ) -> Result<Option<Box<dyn ExternalReplyLease>>> {
+ let Ok(slot) = self.slots.clone().try_acquire_owned() else {
+ return Ok(None);
+ };
+ let mut tx = self.pool.begin().await.map_err(anyhow::Error::from)?;
+ let key = format!("{session}:{action}");
+ let claimed = sqlx::query_scalar!(
+ r#"SELECT pg_try_advisory_xact_lock(hashtextextended($1, 48)) AS "claimed!""#,
+ key
+ )
+ .fetch_one(&mut *tx)
+ .await
+ .map_err(anyhow::Error::from)?;
+ if !claimed {
+ return Ok(None);
+ }
+ // Rotate every attempt, including failures, so a bad origin cannot
+ // monopolize the bounded recovery batch.
+ sqlx::query!("UPDATE agent_external_replies SET updated_at = now() WHERE session_id = $1 AND action_id = $2", session.as_uuid(), action.as_uuid())
+ .execute(&self.pool).await.map_err(anyhow::Error::from)?;
+ Ok(Some(Box::new(ReplyLease {
+ _transaction: tx,
+ _slot: slot,
+ })))
+ }
+
+ async fn mark_announced(&self, session: AgentSessionId, action: AgentActionId) -> Result<()> {
+ sqlx::query!("UPDATE agent_external_replies SET announced = TRUE WHERE session_id = $1 AND action_id = $2", session.as_uuid(), action.as_uuid())
+ .execute(&self.pool).await.map_err(anyhow::Error::from)?;
+ Ok(())
+ }
+
+ async fn finalize(&self, session: AgentSessionId, action: AgentActionId) -> Result<()> {
+ sqlx::query!("UPDATE agent_external_replies SET finalized = TRUE, updated_at = now() WHERE session_id = $1 AND action_id = $2 AND target IS NOT NULL", session.as_uuid(), action.as_uuid())
+ .execute(&self.pool).await.map_err(anyhow::Error::from)?;
+ Ok(())
+ }
+
+ async fn pending(&self, limit: u16) -> Result<Vec<ExternalReply>> {
+ // Keep old unmatched replies recoverable, but don't rescan their logs
+ // every tick. A newly persisted completion bypasses this backoff.
+ sqlx::query_scalar!(r#"SELECT to_jsonb(reply) AS "value!" FROM agent_external_replies reply WHERE target IS NOT NULL AND NOT finalized AND (completion IS NOT NULL OR created_at > now() - interval '24 hours' OR updated_at <= now() - interval '1 hour') ORDER BY updated_at, session_id, action_id LIMIT $1"#, i64::from(limit))
+ .fetch_all(&self.pool).await.map_err(anyhow::Error::from)?.into_iter().map(decode).collect()
+ }
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/agent_harness/src/outbound/external_replies/test.rs b/crates/agent_harness/src/outbound/external_replies/test.rs
new file mode 100644
index 0000000000..cb5d9c15e0
--- /dev/null
+++ b/crates/agent_harness/src/outbound/external_replies/test.rs
@@ -0,0 +1,211 @@
+use super::*;
+use crate::domain::model::{AnnounceOrigin, ReplyOutcome};
+use agent_fold::domain::model::TurnId;
+use agent_session::domain::agent_conversation::{AgentConversation, AgentConversationRepo};
+use channels::{domain::agent_dm::AgentDmRepo, outbound::pg_channels_repo::PgChannelsRepo};
+use macro_db_migrator::MACRO_DB_MIGRATIONS;
+use macro_user_id::user_id::MacroUserIdStr;
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn completion_before_target_survives_reconnect_and_keeps_the_original_reply(pool: PgPool) {
+ let user = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let channel = PgChannelsRepo::new(pool.clone())
+ .ensure(user.clone(), bot_id::MACRO_NEW_BOT_ID)
+ .await
+ .unwrap()
+ .dm
+ .channel_id;
+ let session = crate::testing::postgres_sessions(pool.clone())
+ .current_or_create(AgentConversation {
+ channel_id: channel,
+ bot_id: bot_id::MACRO_NEW_BOT_ID,
+ })
+ .await
+ .unwrap();
+ let action = AgentActionId::from_uuid(macro_uuid::generate_uuid_v7());
+ sqlx::query!("INSERT INTO agent_session (id, owner_id, bot_id, model, harness, workspace, for_warming) VALUES ($1, $2, $3, 'fixed', 'macrod', '/workspace', false)", session.as_uuid(), user.as_ref(), bot_id::MACRO_NEW_BOT_ID.as_uuid()).execute(&pool).await.unwrap();
+ let store = PgExternalReplyStore::new(pool.clone());
+ store
+ .complete(
+ session,
+ action,
+ &ExternalReplyCompletion {
+ turn: TurnId(0),
+ outcome: ReplyOutcome::Answered("I am Eric Hayes.".into()),
+ },
+ )
+ .await
+ .unwrap();
+ assert!(store.pending(10).await.unwrap().is_empty());
+ let target = ExternalReplyTarget {
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: messages::domain::models::MessageParent::Channel(channel),
+ thread_id: action.as_uuid(),
+ message_id: action.as_uuid(),
+ },
+ actor: user,
+ message_id: macro_uuid::generate_uuid_v7(),
+ };
+ store.reserve(session, action, &target).await.unwrap();
+ let reconnected = PgExternalReplyStore::new(pool);
+ reconnected
+ .reserve(
+ session,
+ action,
+ &ExternalReplyTarget {
+ message_id: macro_uuid::generate_uuid_v7(),
+ ..target.clone()
+ },
+ )
+ .await
+ .unwrap();
+ reconnected
+ .complete(
+ session,
+ action,
+ &ExternalReplyCompletion {
+ turn: TurnId(1),
+ outcome: ReplyOutcome::Failed,
+ },
+ )
+ .await
+ .unwrap();
+ let record = reconnected.get(session, action).await.unwrap().unwrap();
+ assert_eq!(record.target.unwrap().message_id, target.message_id);
+ assert_eq!(
+ record.completion.unwrap().outcome,
+ ReplyOutcome::Answered("I am Eric Hayes.".into())
+ );
+ let lease = store.claim(session, action).await.unwrap().unwrap();
+ assert!(reconnected.claim(session, action).await.unwrap().is_none());
+ drop(lease);
+ reconnected.finalize(session, action).await.unwrap();
+ assert!(reconnected.pending(10).await.unwrap().is_empty());
+}
+
+#[sqlx::test(migrator = "MACRO_DB_MIGRATIONS")]
+async fn unfinished_and_failed_replies_do_not_starve_later_recovery(pool: PgPool) {
+ let user = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let channel = PgChannelsRepo::new(pool.clone())
+ .ensure(user.clone(), bot_id::MACRO_NEW_BOT_ID)
+ .await
+ .unwrap()
+ .dm
+ .channel_id;
+ let session = crate::testing::postgres_sessions(pool.clone())
+ .current_or_create(AgentConversation {
+ channel_id: channel,
+ bot_id: bot_id::MACRO_NEW_BOT_ID,
+ })
+ .await
+ .unwrap();
+ sqlx::query!("INSERT INTO agent_session (id, owner_id, bot_id, model, harness, workspace, for_warming) VALUES ($1, $2, $3, 'fixed', 'macrod', '/workspace', false)", session.as_uuid(), user.as_ref(), bot_id::MACRO_NEW_BOT_ID.as_uuid()).execute(&pool).await.unwrap();
+ let store = PgExternalReplyStore::new(pool.clone());
+ let target = ExternalReplyTarget {
+ origin: AnnounceOrigin {
+ reuse_origin_message: false,
+ reply_placement: Default::default(),
+ parent: messages::domain::models::MessageParent::Channel(channel),
+ thread_id: macro_uuid::generate_uuid_v7(),
+ message_id: macro_uuid::generate_uuid_v7(),
+ },
+ actor: user,
+ message_id: macro_uuid::generate_uuid_v7(),
+ };
+ for _ in 0..101 {
+ store
+ .reserve(
+ session,
+ AgentActionId::from_uuid(macro_uuid::generate_uuid_v7()),
+ &target,
+ )
+ .await
+ .unwrap();
+ }
+ let first = store.pending(100).await.unwrap();
+ assert_eq!(first.len(), 100);
+ for reply in &first {
+ let _lease = store
+ .claim(session, reply.action_id)
+ .await
+ .unwrap()
+ .unwrap();
+ // A failed HTTP call drops the lease without finalizing.
+ }
+ let next = store.pending(1).await.unwrap();
+ assert!(
+ !first
+ .iter()
+ .any(|reply| reply.action_id == next[0].action_id)
+ );
+ store.finalize(session, next[0].action_id).await.unwrap();
+ assert!(
+ store
+ .get(session, next[0].action_id)
+ .await
+ .unwrap()
+ .unwrap()
+ .finalized
+ );
+
+ // Old unmatched replies back off but remain eligible for late answers.
+ let action = first[0].action_id;
+ sqlx::query!("UPDATE agent_external_replies SET created_at = now() - interval '25 hours', updated_at = now() WHERE session_id = $1 AND action_id = $2", session.as_uuid(), action.as_uuid()).execute(&pool).await.unwrap();
+ assert!(
+ !store
+ .pending(200)
+ .await
+ .unwrap()
+ .iter()
+ .any(|row| row.action_id == action)
+ );
+ sqlx::query!("UPDATE agent_external_replies SET updated_at = now() - interval '2 hours' WHERE session_id = $1 AND action_id = $2", session.as_uuid(), action.as_uuid()).execute(&pool).await.unwrap();
+ assert!(
+ store
+ .pending(200)
+ .await
+ .unwrap()
+ .iter()
+ .any(|row| row.action_id == action)
+ );
+ drop(store.claim(session, action).await.unwrap().unwrap());
+ assert!(
+ !store
+ .pending(200)
+ .await
+ .unwrap()
+ .iter()
+ .any(|row| row.action_id == action)
+ );
+ store.mark_announced(session, action).await.unwrap();
+ let created_at = store
+ .get(session, action)
+ .await
+ .unwrap()
+ .unwrap()
+ .created_at;
+ let completion = ExternalReplyCompletion {
+ turn: TurnId(0),
+ outcome: ReplyOutcome::Answered("Late answer".into()),
+ };
+ store.complete(session, action, &completion).await.unwrap();
+ assert!(
+ store
+ .pending(200)
+ .await
+ .unwrap()
+ .iter()
+ .any(|row| row.action_id == action)
+ );
+ store.reserve(session, action, &target).await.unwrap();
+ let row = store.get(session, action).await.unwrap().unwrap();
+ assert!(!row.finalized);
+ assert!(row.announced);
+ assert_eq!(row.created_at, created_at);
+ assert_eq!(row.completion.unwrap().outcome, completion.outcome);
+ store.finalize(session, action).await.unwrap();
+ store.complete(session, action, &completion).await.unwrap();
+ assert!(store.get(session, action).await.unwrap().unwrap().finalized);
+}
diff --git a/crates/agent_harness/src/outbound/mod.rs b/crates/agent_harness/src/outbound/mod.rs
index 5f31324fa9..7e25d12f75 100644
--- a/crates/agent_harness/src/outbound/mod.rs
+++ b/crates/agent_harness/src/outbound/mod.rs
@@ -10,6 +10,7 @@ pub mod conversation_turns;
pub mod cursor;
pub mod daytona;
pub mod egress;
+pub mod external_replies;
pub mod forward;
pub mod github_branches;
pub mod github_repositories;
diff --git a/crates/agent_harness/src/testing/helpers/announcer.rs b/crates/agent_harness/src/testing/helpers/announcer.rs
index 8e889794e3..dc44a604e2 100644
--- a/crates/agent_harness/src/testing/helpers/announcer.rs
+++ b/crates/agent_harness/src/testing/helpers/announcer.rs
@@ -134,13 +134,21 @@ impl SessionAnnouncer for AnnouncerMock {
message_id: if announcement.reuse_origin_message {
announcement.origin_message_id
} else {
- macro_uuid::generate_uuid_v7()
+ announcement
+ .reply_message_id
+ .unwrap_or_else(macro_uuid::generate_uuid_v7)
},
};
- self.announced
+ let mut announced = self
+ .announced
.lock()
- .expect("announcer mock lock should not be poisoned")
- .push((announcement, message));
+ .expect("announcer mock lock should not be poisoned");
+ if !announced
+ .iter()
+ .any(|(_, existing)| existing.message_id == message.message_id)
+ {
+ announced.push((announcement, message));
+ }
Ok(message)
}
diff --git a/crates/agent_session/AGENTS.md b/crates/agent_session/AGENTS.md
index 2383b4add9..82228857bc 100644
--- a/crates/agent_session/AGENTS.md
+++ b/crates/agent_session/AGENTS.md
@@ -52,6 +52,10 @@ consumer talks to.
- `src/inbound/toolset/`: session reads, creation, lifecycle actions, and sharing. Mutations preserve acting-user identity and existing owner/edit receipts; internal controls accept only prompt/stop, never human approvals.
+Internal action recovery uses `AgentSessionService::recorded_action`, which reads
+one durable log snapshot including pre-load frames. Keep this separate from the
+effective history served to transcript readers: replayed prompts lose action IDs.
+
## Checks
- `cargo test -p agent_session` (`SQLX_OFFLINE` unset).
diff --git a/crates/agent_session/src/domain/action_history.rs b/crates/agent_session/src/domain/action_history.rs
new file mode 100644
index 0000000000..ff4dbb3b97
--- /dev/null
+++ b/crates/agent_session/src/domain/action_history.rs
@@ -0,0 +1,86 @@
+//! Recover action facts independently of the currently displayed replay history.
+
+use agent_fold::domain::lifecycle::LifecycleFold;
+use agent_fold::domain::model::{StopReason, TurnId, TurnSignal};
+use agent_runtime_protocol::domain::action::AgentActionId;
+use agent_runtime_protocol::domain::schema::v0::{
+ AcpMessage, SystemEvent, ToRuntimeMessage, ToServerMessage,
+};
+
+use super::model::{AgentSessionLog, Message, StoredAgentSessionLog};
+
+/// Durable evidence that an action reached its session, and how it ended.
+#[derive(Debug, PartialEq)]
+pub enum RecordedAction {
+ /// A prompt was recorded, without a terminal response or disconnect yet.
+ Running { turn: TurnId },
+ /// The original ACP response, even if a later load replaced its transcript.
+ Finished {
+ turn: TurnId,
+ stop: StopReason,
+ last_text: Option<String>,
+ },
+ /// A disconnect was recorded after the prompt and before any response.
+ Interrupted { turn: TurnId },
+}
+
+pub(super) fn recorded_action(
+ entries: Vec<StoredAgentSessionLog>,
+ action: AgentActionId,
+) -> Option<RecordedAction> {
+ let mut fold = LifecycleFold::new();
+ let mut prompt = None;
+ let mut disconnected = false;
+ for stored in entries {
+ let mut entry = stored.entry;
+ entry.logged_at = Some(stored.created_at);
+ disconnected |= prompt.is_some()
+ && matches!(
+ entry.content,
+ Message::ToServer(ToServerMessage::Event {
+ event: SystemEvent::Disconnected
+ })
+ );
+ if is_prompt(&entry, action) {
+ prompt = Some(fold.inner().next_turn_id());
+ }
+ let answers_action = matches!(&entry.content,
+ Message::ToServer(ToServerMessage::Acp(AcpMessage(frame)))
+ if frame.response_id().and_then(AgentActionId::from_request_id) == Some(action));
+ let pushed = fold.push(entry);
+ for signal in pushed.signals {
+ if let TurnSignal::TurnEnded {
+ turn,
+ action_id,
+ stop,
+ last_text,
+ } = signal
+ && (action_id == Some(action) || (action_id.is_none() && answers_action))
+ {
+ // A completed turn wins over any later disconnect or replay.
+ return Some(RecordedAction::Finished {
+ turn,
+ stop,
+ last_text,
+ });
+ }
+ }
+ }
+ prompt.map(|turn| {
+ if disconnected {
+ RecordedAction::Interrupted { turn }
+ } else {
+ RecordedAction::Running { turn }
+ }
+ })
+}
+
+/// Whether a durable outgoing prompt carries the action, even if it renders no text.
+pub(crate) fn is_prompt(entry: &AgentSessionLog, action: AgentActionId) -> bool {
+ matches!(&entry.content,
+ Message::ToRuntime(ToRuntimeMessage::Acp(AcpMessage(agent_client_protocol::RawJsonRpcMessage::Request(request))))
+ if request.method.as_ref() == "session/prompt" && AgentActionId::from_request_id(&request.id) == Some(action))
+}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/agent_session/src/domain/action_history/test.rs b/crates/agent_session/src/domain/action_history/test.rs
new file mode 100644
index 0000000000..d012ba4a85
--- /dev/null
+++ b/crates/agent_session/src/domain/action_history/test.rs
@@ -0,0 +1,48 @@
+use super::*;
+use crate::domain::model::AgentSessionId;
+
+#[test]
+fn an_empty_prompt_still_proves_delivery() {
+ let session = AgentSessionId::new();
+ let action = AgentActionId::mint();
+ let log = format!(
+ r#"{{"direction":"to_runtime","content":{{"type":"acp","jsonrpc":"2.0","id":"{action}","method":"session/prompt","params":{{"sessionId":"s1","prompt":[]}}}}}}"#
+ );
+ let entries = agent_fold::testing::parse_log_as(session, &log)
+ .into_iter()
+ .map(|entry| StoredAgentSessionLog {
+ id: macro_uuid::generate_uuid_v7(),
+ created_at: chrono::Utc::now(),
+ entry,
+ })
+ .collect();
+ assert_eq!(
+ recorded_action(entries, action),
+ Some(RecordedAction::Running { turn: TurnId(0) })
+ );
+}
+
+#[test]
+fn an_empty_prompt_recovers_its_completed_answer() {
+ let session = AgentSessionId::new();
+ let action = AgentActionId::mint();
+ let log = agent_fold::testing::TURN
+ .replace("prompt-1", &action.to_string())
+ .replace("list the examples and write a file", "");
+ let entries = agent_fold::testing::parse_log_as(session, &log)
+ .into_iter()
+ .map(|entry| StoredAgentSessionLog {
+ id: macro_uuid::generate_uuid_v7(),
+ created_at: chrono::Utc::now(),
+ entry,
+ })
+ .collect();
+ assert_eq!(
+ recorded_action(entries, action),
+ Some(RecordedAction::Finished {
+ turn: TurnId(0),
+ stop: StopReason::EndTurn,
+ last_text: Some("Done.".into()),
+ })
+ );
+}
diff --git a/crates/agent_session/src/domain/mod.rs b/crates/agent_session/src/domain/mod.rs
index 2910fd8d64..21c15eb54e 100644
--- a/crates/agent_session/src/domain/mod.rs
+++ b/crates/agent_session/src/domain/mod.rs
@@ -1,3 +1,4 @@
+pub mod action_history;
pub mod agent_conversation;
/// Current authorization for agent session streams.
pub mod audience;
diff --git a/crates/agent_session/src/domain/ports.rs b/crates/agent_session/src/domain/ports.rs
index ed778935c4..0753fbf5e6 100644
--- a/crates/agent_session/src/domain/ports.rs
+++ b/crates/agent_session/src/domain/ports.rs
@@ -768,6 +768,16 @@ pub trait AgentSessionLogRepo: Send + Sync + 'static {
agent_session_id: AgentSessionId,
) -> impl Future<Output = Result<Vec<StoredAgentSessionLog>>> + Send;
+ /// A single snapshot of durable history when the action was prompted,
+ /// or an empty snapshot when absent. Includes frames
+ /// before a successful load. Internal action recovery needs their original
+ /// request IDs, which replayed display history does not preserve.
+ fn list_action_history(
+ &self,
+ agent_session_id: AgentSessionId,
+ action: agent_runtime_protocol::domain::action::AgentActionId,
+ ) -> impl Future<Output = Result<Vec<StoredAgentSessionLog>>> + Send;
+
/// Every user the log has attributed a frame to: whoever prompted,
/// answered, or otherwise drove the session. Distinct, unordered; the
/// owner appears only if they acted. Spans the whole log, resumes
diff --git a/crates/agent_session/src/domain/service.rs b/crates/agent_session/src/domain/service.rs
index 78b69618c5..dd0701f264 100644
--- a/crates/agent_session/src/domain/service.rs
+++ b/crates/agent_session/src/domain/service.rs
@@ -350,6 +350,14 @@ pub trait AgentSessionService: Send + Sync + 'static {
/// implementation. See [`SessionLog`] and [`Self::folded_session_log`].
fn session_log(&self, id: AgentSessionId) -> impl Future<Output = Result<SessionLog>> + Send;
+ /// Recover one action from a consistent durable snapshot, including history
+ /// replaced by session/load. No observer callback or live actor is required.
+ fn recorded_action(
+ &self,
+ id: AgentSessionId,
+ action: AgentActionId,
+ ) -> impl Future<Output = Result<Option<super::action_history::RecordedAction>>> + Send;
+
/// The same history as [`Self::session_log`], folded server-side into
/// renderable messages for readers that do not run the fold themselves,
/// such as SDK programs and agents reading other sessions.
@@ -1175,6 +1183,16 @@ where
})
}
+ #[tracing::instrument(err, skip(self))]
+ async fn recorded_action(
+ &self,
+ id: AgentSessionId,
+ action: AgentActionId,
+ ) -> Result<Option<super::action_history::RecordedAction>> {
+ let entries = self.repo.list_action_history(id, action).await?;
+ Ok(super::action_history::recorded_action(entries, action))
+ }
+
#[tracing::instrument(err, skip(self))]
async fn folded_session_log(&self, id: AgentSessionId) -> Result<FoldedSessionLog> {
let session = self.repo.get(id).await?;
diff --git a/crates/agent_session/src/domain/service/test.rs b/crates/agent_session/src/domain/service/test.rs
index ea1bda2fea..9eb1e48f0b 100644
--- a/crates/agent_session/src/domain/service/test.rs
+++ b/crates/agent_session/src/domain/service/test.rs
@@ -774,6 +774,16 @@ impl SessionOwnership for BlockingPromptLogs {
}
impl AgentSessionLogRepo for BlockingPromptLogs {
+ async fn list_action_history(
+ &self,
+ agent_session_id: AgentSessionId,
+ action: agent_runtime_protocol::domain::action::AgentActionId,
+ ) -> Result<Vec<StoredAgentSessionLog>> {
+ self.repo
+ .list_action_history(agent_session_id, action)
+ .await
+ }
+
async fn create_fenced_with_boundary(
&self,
log: AgentSessionLog,
diff --git a/crates/agent_session/src/outbound/postgres.rs b/crates/agent_session/src/outbound/postgres.rs
index 93a6866847..53ec7fe274 100644
--- a/crates/agent_session/src/outbound/postgres.rs
+++ b/crates/agent_session/src/outbound/postgres.rs
@@ -1657,6 +1657,39 @@ impl<B: BotFacts + 'static> AgentSessionLogRepo for PgAgentSessionRepo<B> {
.collect::<anyhow::Result<Vec<_>>>()?)
}
+ async fn list_action_history(
+ &self,
+ agent_session_id: AgentSessionId,
+ action: agent_runtime_protocol::domain::action::AgentActionId,
+ ) -> Result<Vec<StoredAgentSessionLog>> {
+ let rows = sqlx::query_as!(
+ AgentSessionLogRow,
+ r#"
+ SELECT id, agent_session_id, user_id AS "user_id: MacroUserIdStr",
+ direction, content, created_at
+ FROM agent_session_log
+ WHERE agent_session_id = $1
+ AND EXISTS (
+ SELECT 1 FROM agent_session_log AS prompt
+ WHERE prompt.agent_session_id = $1
+ AND prompt.direction = 'to_runtime'
+ AND prompt.content->>'method' = 'session/prompt'
+ AND prompt.content->>'id' = $2
+ )
+ ORDER BY created_at, id
+ "#,
+ agent_session_id.as_uuid(),
+ action.to_string(),
+ )
+ .fetch_all(&self.pool)
+ .await
+ .context("failed to read durable action history")?;
+ Ok(rows
+ .into_iter()
+ .map(TryInto::try_into)
+ .collect::<anyhow::Result<Vec<_>>>()?)
+ }
+
async fn participants(
&self,
agent_session_id: AgentSessionId,
diff --git a/crates/agent_session/src/testing.rs b/crates/agent_session/src/testing.rs
index e20941ce88..33ff39a0b4 100644
--- a/crates/agent_session/src/testing.rs
+++ b/crates/agent_session/src/testing.rs
@@ -792,6 +792,29 @@ impl crate::domain::turn_state::SessionTurnProjectionRepo for InMemoryAgentSessi
}
impl AgentSessionLogRepo for InMemoryAgentSessionRepo {
+ async fn list_action_history(
+ &self,
+ agent_session_id: AgentSessionId,
+ action: agent_runtime_protocol::domain::action::AgentActionId,
+ ) -> Result<Vec<StoredAgentSessionLog>> {
+ let _transaction = self.log_transaction.lock().unwrap();
+ let mut rows = self
+ .logs
+ .lock()
+ .unwrap()
+ .get(&agent_session_id)
+ .cloned()
+ .unwrap_or_default();
+ if !rows
+ .iter()
+ .any(|row| crate::domain::action_history::is_prompt(&row.entry, action))
+ {
+ return Ok(Vec::new());
+ }
+ rows.sort_unstable_by_key(|row| (row.created_at, row.id));
+ Ok(rows)
+ }
+
async fn create(&self, log: AgentSessionLog) -> Result<StoredAgentSessionLog> {
self.create_projected(log, None, None, None).await
}
diff --git a/crates/coding_agent_worker/AGENTS.md b/crates/coding_agent_worker/AGENTS.md
index 2f96df8b3c..1a837fbd2c 100644
--- a/crates/coding_agent_worker/AGENTS.md
+++ b/crates/coding_agent_worker/AGENTS.md
@@ -39,13 +39,19 @@ the local ACP process (Claude Code, Codex, ...), and drives native
- Add/change a native herdr control (command, model picker): `src/herdr/controls/`
→ `README.md` (native controls section) → `src/herdr/acp_agent/`.
+Channel/document trigger prompts retain their source message ID through
+`TriggerWork` and send it as `ControlRequest.action_id`. Preserve this correlation
+for initial mentions, existing-session follow-ups, and conflicted-create retries;
+external chat replies depend on it. Direct session controls still mint IDs.
+
## Checks
- `cargo test -p coding_agent_worker` (`SQLX_OFFLINE` unset).
- `just run` (from this directory) to run the daemon against `macrod.toml` at
the repo root.
-- `just build-static` to build and smoke-test the release `macrod` the way
+- `OPENSSL_NO_VENDOR=0 just build-static` to build and smoke-test the release `macrod` the way
the tag workflow does (needs the Nix devshell: cargo-zigbuild, zig, cmake).
+ The override enables bundled OpenSSL for the musl target in the devshell.
## Gotchas
diff --git a/crates/coding_agent_worker/README.md b/crates/coding_agent_worker/README.md
index dd1904ee0d..78de9c1205 100644
--- a/crates/coding_agent_worker/README.md
+++ b/crates/coding_agent_worker/README.md
@@ -108,3 +108,22 @@ The Config tab adds **Model** (empty uses the native default), **Extra args**
previous agent's model and extra arguments. Storage and focus preferences remain.
Session metadata stays in the instance directory even with custom worktree storage.
Choose a repository in Macro's composer; local sessions always start from `main`.
+
+## Replies to chat-agent mentions
+
+Trigger prompts use the source channel/document message UUID as the control
+action ID, scoped to their session. Initial mentions, follow-ups, and retries
+after an existing-thread conflict all retain that ID. The backend joins it to
+the authorized reply destination, so a completed external chat turn replaces
+its pending thread reply. The backend retains the result and reserved reply ID
+to retry channel updates without rerunning the prompt. Direct session controls
+continue using newly generated action IDs.
+
+After 24 hours, replies without a persisted completion are checked at most hourly.
+Direct completion handling remains immediate. If a completion callback is lost
+for an older reply, recovery can take an hour plus the recovery backlog; the
+durable answer is retained. Successful pending announcements are not posted again.
+
+Deploy the backend migration/service and update macrod to enable this path.
+Previously released daemons generate unrelated action IDs; this change does
+not repair old pending replies produced by those versions.
diff --git a/crates/coding_agent_worker/src/dispatch.rs b/crates/coding_agent_worker/src/dispatch.rs
index 146944d4aa..5351147901 100644
--- a/crates/coding_agent_worker/src/dispatch.rs
+++ b/crates/coding_agent_worker/src/dispatch.rs
@@ -202,7 +202,9 @@ impl WorkExecutor for Dispatcher {
.ensure_connected()
.await
.map_err(DispatchError::Dial)?;
- self.api.prompt(session, &sender, &content, context).await?;
+ self.api
+ .prompt(session, &sender, &content, message_id, context)
+ .await?;
return Ok(());
}
Err(ApiError::ThreadSessionExists { session: None }) => {
@@ -225,7 +227,9 @@ impl WorkExecutor for Dispatcher {
// answer. SSE has no redelivery; a later `agent_trigger.existing`
// or a 409 on a repeat create (session already exists) is how
// a follow-up lands on the same session.
- self.api.prompt(session, &sender, &content, context).await?;
+ self.api
+ .prompt(session, &sender, &content, message_id, context)
+ .await?;
Ok(())
}
TriggerWork::OpenRequested {
@@ -262,6 +266,7 @@ impl WorkExecutor for Dispatcher {
Ok(())
}
TriggerWork::PromptExisting {
+ message_id,
session,
sender,
content,
@@ -272,7 +277,9 @@ impl WorkExecutor for Dispatcher {
.ensure_connected()
.await
.map_err(DispatchError::Dial)?;
- self.api.prompt(session, &sender, &content, context).await?;
+ self.api
+ .prompt(session, &sender, &content, message_id, context)
+ .await?;
Ok(())
}
}
diff --git a/crates/coding_agent_worker/src/outbound/agent_session.rs b/crates/coding_agent_worker/src/outbound/agent_session.rs
index 327fbddd87..355aae89d9 100644
--- a/crates/coding_agent_worker/src/outbound/agent_session.rs
+++ b/crates/coding_agent_worker/src/outbound/agent_session.rs
@@ -2,7 +2,7 @@
//! them. Both calls act as the harness, and control calls additionally act
//! for the user whose mention they forward.
-use agent_runtime_protocol::domain::action::AgentAction;
+use agent_runtime_protocol::domain::action::{AgentAction, AgentActionId};
use agent_session::domain::model::AgentSessionId;
use agent_session::inbound::axum_router::{
ControlRequest, CreateAgentSessionRequest, CreateAgentSessionResponse,
@@ -71,6 +71,34 @@ impl HarnessApi {
&self,
request: &CreateAgentSessionRequest,
acting_user: &MacroUserIdStr<'static>,
+ ) -> Result<CreateAgentSessionResponse, ApiError> {
+ // A trigger stream is not replayed. Creation can commit before reply
+ // reservation fails, so retry the same identity before any prompt.
+ let attempts = if request.id.is_some() && request.thread.is_some() {
+ 2
+ } else {
+ 0
+ };
+ tokio_retry::RetryIf::start(
+ tokio_retry::strategy::ExponentialBackoff::from_millis(2)
+ .factor(100)
+ .take(attempts),
+ || self.create_session_once(request, acting_user),
+ |error: &ApiError| match error {
+ ApiError::Http(_) => true,
+ ApiError::Refused { status, .. } => {
+ status.is_server_error() || *status == StatusCode::TOO_MANY_REQUESTS
+ }
+ ApiError::ThreadSessionExists { .. } => false,
+ },
+ )
+ .await
+ }
+
+ async fn create_session_once(
+ &self,
+ request: &CreateAgentSessionRequest,
+ acting_user: &MacroUserIdStr<'static>,
) -> Result<CreateAgentSessionResponse, ApiError> {
let response = self
.http
@@ -102,6 +130,18 @@ impl HarnessApi {
actor: &MacroUserIdStr<'static>,
action: AgentAction,
context: Option<TriggerContext>,
+ ) -> Result<(), ApiError> {
+ self.control_with_id(session, actor, action, context, None)
+ .await
+ }
+
+ async fn control_with_id(
+ &self,
+ session: AgentSessionId,
+ actor: &MacroUserIdStr<'static>,
+ action: AgentAction,
+ context: Option<TriggerContext>,
+ action_id: Option<AgentActionId>,
) -> Result<(), ApiError> {
let response = self
.http
@@ -109,7 +149,7 @@ impl HarnessApi {
.header(HARNESS_TOKEN_HEADER, &self.token)
.header(HARNESS_ACTING_USER_HEADER, actor.as_ref())
.json(&ControlRequest {
- action_id: None,
+ action_id,
context,
action,
})
@@ -126,10 +166,17 @@ impl HarnessApi {
session: AgentSessionId,
sender: &MacroUserIdStr<'static>,
text: &str,
+ message_id: macro_uuid::Uuid,
context: Option<TriggerContext>,
) -> Result<(), ApiError> {
- self.control(session, sender, AgentAction::prompt(text), context)
- .await
+ self.control_with_id(
+ session,
+ sender,
+ AgentAction::prompt(text),
+ context,
+ Some(AgentActionId::from_uuid(message_id)),
+ )
+ .await
}
}
@@ -147,3 +194,6 @@ async fn refuse_errors(response: reqwest::Response) -> Result<reqwest::Response,
}
Err(ApiError::Refused { status, message })
}
+
+#[cfg(test)]
+mod test;
diff --git a/crates/coding_agent_worker/src/outbound/agent_session/test.rs b/crates/coding_agent_worker/src/outbound/agent_session/test.rs
new file mode 100644
index 0000000000..72b5c3e0b1
--- /dev/null
+++ b/crates/coding_agent_worker/src/outbound/agent_session/test.rs
@@ -0,0 +1,81 @@
+use super::*;
+use agent_session::inbound::axum_router::CreateSessionThread;
+use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
+
+#[tokio::test]
+async fn transient_create_retries_keep_identity_and_trigger_controls_keep_source_id() {
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let api = HarnessApi {
+ http: reqwest::Client::new(),
+ base: format!("http://{}", listener.local_addr().unwrap()),
+ token: "test".into(),
+ };
+ let session = AgentSessionId::new();
+ let source = macro_uuid::generate_uuid_v7();
+ let server = tokio::spawn(async move {
+ let mut received = Vec::new();
+ let responses = [
+ ("500 Internal Server Error", "{}".to_owned()),
+ (
+ "409 Conflict",
+ serde_json::json!({"message":"already created", "sessionId":session}).to_string(),
+ ),
+ ("200 OK", "{}".to_owned()),
+ ];
+ for (status, body) in responses {
+ let (stream, _) = listener.accept().await.unwrap();
+ let mut stream = BufReader::new(stream);
+ let mut line = String::new();
+ stream.read_line(&mut line).await.unwrap();
+ let mut length = 0;
+ loop {
+ line.clear();
+ stream.read_line(&mut line).await.unwrap();
+ if line == "\r\n" {
+ break;
+ }
+ if let Some(value) = line.to_ascii_lowercase().strip_prefix("content-length:") {
+ length = value.trim().parse::<usize>().unwrap();
+ }
+ }
+ let mut data = vec![0; length];
+ stream.read_exact(&mut data).await.unwrap();
+ received.push(serde_json::from_slice::<serde_json::Value>(&data).unwrap());
+ stream.write_all(format!("HTTP/1.1 {status}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", body.len()).as_bytes()).await.unwrap();
+ }
+ received
+ });
+ let sender = MacroUserIdStr::try_from_email("[email protected]").unwrap();
+ let request = CreateAgentSessionRequest {
+ id: Some(session.as_uuid()),
+ bot_id: Some(bot_id::BotId::TEST_A.as_uuid()),
+ workspace: Some("/workspace".into()),
+ prompt: None,
+ repo_url: None,
+ repo_branch: None,
+ owner: Some(sender.to_string()),
+ instructions: None,
+ model: None,
+ thread: Some(CreateSessionThread {
+ reuse_origin_message: false,
+ channel_id: None,
+ parent: Some(messages::domain::models::MessageParent::Channel(
+ macro_uuid::generate_uuid_v7(),
+ )),
+ thread_id: Some(source),
+ message_id: source,
+ content: "hello".into(),
+ }),
+ };
+ assert!(
+ matches!(api.create_session(&request, &sender).await, Err(ApiError::ThreadSessionExists { session: Some(existing) }) if existing == session)
+ );
+ api.prompt(session, &sender, "hello", source, None)
+ .await
+ .unwrap();
+ let received = server.await.unwrap();
+ assert_eq!(received.len(), 3);
+ assert_eq!(received[0], received[1]);
+ let control: ControlRequest = serde_json::from_value(received[2].clone()).unwrap();
+ assert_eq!(control.action_id, Some(AgentActionId::from_uuid(source)));
+}
diff --git a/crates/coding_agent_worker/src/trigger.rs b/crates/coding_agent_worker/src/trigger.rs
index 2d1ed4e527..c08f62ac2c 100644
--- a/crates/coding_agent_worker/src/trigger.rs
+++ b/crates/coding_agent_worker/src/trigger.rs
@@ -67,6 +67,8 @@ pub enum TriggerWork {
/// service announces the reply into its channel from the trigger event
/// it observed.
PromptExisting {
+ /// Stable identity shared with the backend announcement.
+ message_id: Uuid,
/// The session to feed.
session: AgentSessionId,
/// Who sent the message.
@@ -170,6 +172,7 @@ pub fn trigger_to_work(event: AgentTriggerTopicEvent) -> Result<TriggerWork, Ski
.cloned()
.ok_or(Skipped::NotFromUser)?;
Ok(TriggerWork::PromptExisting {
+ message_id: message.message_id,
session: session_id,
sender,
content: message.content,
diff --git a/crates/coding_agent_worker/src/trigger/test.rs b/crates/coding_agent_worker/src/trigger/test.rs
index 67a7b07c76..b983ecae7e 100644
--- a/crates/coding_agent_worker/src/trigger/test.rs
+++ b/crates/coding_agent_worker/src/trigger/test.rs
@@ -124,6 +124,7 @@ fn document_triggers_become_work_on_the_document() {
assert_eq!(
trigger_to_work(followed).expect("a document follow-up is work"),
TriggerWork::PromptExisting {
+ message_id: Uuid::from_u128(2),
session: test_session(),
sender: sender(),
content: "and this".to_owned(),
@@ -183,6 +184,7 @@ fn a_follow_up_becomes_prompt_existing() {
assert_eq!(
work,
TriggerWork::PromptExisting {
+ message_id: Uuid::from_u128(2),
session: test_session(),
sender: sender(),
content: "keep going".to_owned(),
diff --git a/crates/folds/agent_fold/AGENTS.md b/crates/folds/agent_fold/AGENTS.md
index c2570b1dac..3fafe75030 100644
--- a/crates/folds/agent_fold/AGENTS.md
+++ b/crates/folds/agent_fold/AGENTS.md
@@ -51,6 +51,10 @@ to the browser via `inbound/wasm`.
- Debug a fold against a recorded session: `cargo run -p agent_fold --bin
fold_jsonl -- <path>.jsonl`.
+`FoldedMessage::last_text` supplies the same terminal prose to live lifecycle
+signals and harness recovery from a persisted log. Keep these interpretations
+aligned; recovery must never guess the answer from another turn.
+
## Checks
- `cargo test -p agent_fold`.
diff --git a/crates/folds/agent_fold/src/domain/lifecycle.rs b/crates/folds/agent_fold/src/domain/lifecycle.rs
index f5dc55b22b..eaf548487e 100644
--- a/crates/folds/agent_fold/src/domain/lifecycle.rs
+++ b/crates/folds/agent_fold/src/domain/lifecycle.rs
@@ -24,8 +24,8 @@ use agent_runtime_protocol::domain::action::AgentActionId;
use crate::domain::fold::FoldMachineImpl;
use crate::domain::log::AgentSessionLog;
use crate::domain::model::{
- Author, ElicitationRequestId, FoldEvent, FoldedMessage, MessagePart, OwnedFoldEvent,
- ProjectedSegment, SessionMetadata, TurnId, TurnPhase, TurnSignal, phase, project,
+ Author, ElicitationRequestId, FoldEvent, FoldedMessage, OwnedFoldEvent, ProjectedSegment,
+ SessionMetadata, TurnId, TurnPhase, TurnSignal, phase, project,
};
use crate::domain::ports::FoldMachine as _;
@@ -123,7 +123,7 @@ impl LifecycleFold {
turn: message.id,
action_id: self.action_id(message),
stop: stop.clone(),
- last_text: last_text(message),
+ last_text: message.last_text(),
});
}
@@ -170,17 +170,3 @@ impl LifecycleFold {
.collect();
}
}
-
-/// The last prose part of a message, trimmed; `None` when there is none.
-fn last_text(message: &FoldedMessage) -> Option<String> {
- message
- .parts
- .iter()
- .rev()
- .find_map(|part| match part {
- MessagePart::Text { text } => Some(text.trim()),
- _ => None,
- })
- .filter(|text| !text.is_empty())
- .map(str::to_owned)
-}
diff --git a/crates/folds/agent_fold/src/domain/model.rs b/crates/folds/agent_fold/src/domain/model.rs
index a15ddf4edc..cf3579fdbb 100644
--- a/crates/folds/agent_fold/src/domain/model.rs
+++ b/crates/folds/agent_fold/src/domain/model.rs
@@ -237,6 +237,20 @@ pub struct FoldedMessage {
}
impl FoldedMessage {
+ /// The last prose part, trimmed; absent when the message has no prose.
+ #[must_use]
+ pub fn last_text(&self) -> Option<String> {
+ self.parts
+ .iter()
+ .rev()
+ .find_map(|part| match part {
+ MessagePart::Text { text } => Some(text.trim()),
+ _ => None,
+ })
+ .filter(|text| !text.is_empty())
+ .map(str::to_owned)
+ }
+
/// This message's natural key within its session.
#[must_use]
pub fn id(&self) -> MessageId {
diff --git a/crates/macro_db_client/AGENTS.md b/crates/macro_db_client/AGENTS.md
index 7a77b6700b..9a7db24c6f 100644
--- a/crates/macro_db_client/AGENTS.md
+++ b/crates/macro_db_client/AGENTS.md
@@ -47,6 +47,12 @@ queries used across Rust services.
- After schema or query changes: `nix develop --command just prepare_db` from
the repo root, then commit the regenerated root `.sqlx/`.
+`agent_external_replies` stores external runtime reply destinations and outcomes;
+its queries and recovery policy belong to `agent_harness`. Its creation time is
+stable across retries, and `announced` records successful pending-message posts.
+The additive migrations are compatible with older service binaries; session
+deletion cascades its rows.
+
## Gotchas
- Migrations deploy before service code: never drop a column/table in the
diff --git a/crates/macro_db_client/migrations/20261020212519_agent_external_replies.sql b/crates/macro_db_client/migrations/20261020212519_agent_external_replies.sql
new file mode 100644
index 0000000000..1d7f11d888
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261020212519_agent_external_replies.sql
@@ -0,0 +1,15 @@
+-- The runtime's prompt and the broker's announcement can arrive in either
+-- order. Their shared action ID is the triggering message ID, scoped to the
+-- session so multiple agents can answer the same message independently.
+CREATE TABLE agent_external_replies (
+ session_id UUID NOT NULL REFERENCES agent_session(id) ON DELETE CASCADE,
+ action_id UUID NOT NULL,
+ target JSONB,
+ completion JSONB,
+ finalized BOOLEAN NOT NULL DEFAULT FALSE,
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ PRIMARY KEY (session_id, action_id)
+);
+
+CREATE INDEX agent_external_replies_pending ON agent_external_replies(updated_at)
+ WHERE target IS NOT NULL AND NOT finalized;
diff --git a/crates/macro_db_client/migrations/20261020223622_external_reply_recovery_deadline.sql b/crates/macro_db_client/migrations/20261020223622_external_reply_recovery_deadline.sql
new file mode 100644
index 0000000000..3d5e3d1e75
--- /dev/null
+++ b/crates/macro_db_client/migrations/20261020223622_external_reply_recovery_deadline.sql
@@ -0,0 +1,4 @@
+-- Remember successful posts and bound recovery for prompts never delivered.
+ALTER TABLE agent_external_replies
+ ADD COLUMN created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
+ ADD COLUMN announced BOOLEAN NOT NULL DEFAULT FALSE;
diff --git a/docs/agent-guide/ai-chat.md b/docs/agent-guide/ai-chat.md
index 38bd6f34a9..b31cffac0c 100644
--- a/docs/agent-guide/ai-chat.md
+++ b/docs/agent-guide/ai-chat.md
@@ -553,6 +553,14 @@ the shimmer.
session starts on the first prompt, before its first reply; a startup screen
that temporarily hides the footer is retried during the turn. The menu does not list
installed native skills or session-switching commands such as `/resume`.
+ For a paired runtime configured as a **Chat agent**, mentioning it posts a
+ pending reply in the channel thread. Once the turn completes, that same reply
+ shows the answer (or a stopped/failed result), even if the session view was
+ never opened. Follow-up messages get their own replies. This requires a macrod
+ build that sends source message IDs as control action IDs, together with the
+ backend external-reply journal; older daemons do not supply that correlation.
+ Replies older than 24 hours whose completion callback was lost are recovered
+ hourly, plus any recovery backlog. Normal completion handling remains immediate.
The agent form retains sharing, name, `@tag`, runtime, default model, connections,
channels, instructions, and permission policy.
Runtime and short model lists use styled dropdown buttons: open the field and
diff --git a/docs/development/database.md b/docs/development/database.md
index 6173286607..23dc60bcf3 100644
--- a/docs/development/database.md
+++ b/docs/development/database.md
@@ -45,6 +45,11 @@ replay used to be nearly all of the Rust test suite's runtime. Run the recipe
locally only against a Postgres dedicated to one checkout; every database created
on that server afterwards gets the schema too.
+Set `max_locks_per_transaction = 8192` on that dedicated server, as in
+[CI’s PostgreSQL setup](../../tooling/xtask/crates/xtask_workflows/src/workflows/scripts/configure_postgres.sh),
+and restart PostgreSQL before testing; replacing a pre-migrated schema can exceed
+the default lock capacity.
+
Tests that build their own schema (`#[sqlx::test(migrations = false)]`, or a
crate's test-only schema fixture) must first run
`DROP SCHEMA public CASCADE; CREATE SCHEMA public;`, since their database may
diff --git a/services/agent_harness_service/AGENTS.md b/services/agent_harness_service/AGENTS.md
index 533e39bd2e..a3fbf70ada 100644
--- a/services/agent_harness_service/AGENTS.md
+++ b/services/agent_harness_service/AGENTS.md
@@ -42,6 +42,11 @@ sessions: agent-session trigger events in, sandboxed coding agent sessions
- Internal session tools require a verified acting user plus owner/edit authorization. Keep their restricted prompt/stop route separate from human approval controls.
+The composition root wires `PgExternalReplyStore` into the harness and recovers
+unfinished external chat replies alongside conversation recovery every ten seconds.
+Both the backend and macrod's source-message action IDs are required for replies
+from newly triggered external chat turns to finish in their channel threads.
+
## Checks
- `cargo test -p agent_harness_service` (`SQLX_OFFLINE` unset).
diff --git a/services/agent_harness_service/src/main.rs b/services/agent_harness_service/src/main.rs
index 43bd117e42..9e970a29db 100644
--- a/services/agent_harness_service/src/main.rs
+++ b/services/agent_harness_service/src/main.rs
@@ -1160,6 +1160,9 @@ async fn run() -> anyhow::Result<()> {
// notification ingress channel messages use.
IngressAgentSessionNotifier::new(Arc::clone(&notifications)),
)
+ .with_external_replies(Arc::new(
+ agent_harness::outbound::external_replies::PgExternalReplyStore::new(pool.clone()),
+ ))
.with_conversation_turns(conversation_turns)
.with_admission(admission.clone())
.with_repositories(open_repositories)
@@ -1536,6 +1539,9 @@ async fn run() -> anyhow::Result<()> {
ticker.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
loop {
ticker.tick().await;
+ if let Err(error) = recovery_harness.recover_external_replies().await {
+ tracing::error!(?error, "failed to reconcile external agent replies");
+ }
if let Err(error) = recovery_harness.recover_conversations().await {
tracing::error!(
?error,