Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
//! Durable admission and execution records for the channel messages an agent
//! answers in its conversation.
use agent_runtime_protocol::domain::action::AgentActionId;
use agent_session::domain::{error::Result, model::AgentSessionId};
use bot_id::BotId;
use macro_uuid::Uuid;
use super::{
model::{AgentRuntimeConfig, OpenSession, PermissionPolicyConfig, ReplyOutcome},
queue::InFlightTurn,
};
/// The state of one source message, retained after completion for deduplication.
#[derive(
Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, utoipa::ToSchema,
)]
#[serde(rename_all = "snake_case")]
pub enum ConversationTurnState {
/// Accepted durably; no model request has been dispatched.
Queued,
/// Claimed for dispatch; it may have performed external actions.
Running,
/// The turn finished successfully.
Succeeded,
/// The turn failed; retry requires the user's explicit action.
Failed,
/// The user stopped the turn.
Stopped,
/// The managing process disappeared; never automatically repeat its work.
Interrupted,
}
/// Original command and the outcome of its current attempt.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ConversationTurn {
/// Immutable source identity, even when a broker replays after a context reset.
pub source_message_id: Uuid,
/// Persona answering the message. Each agent in a channel keeps its own turn.
pub bot_id: BotId,
/// The session chosen on first admission.
pub session_id: AgentSessionId,
/// Channel containing this turn.
pub channel_id: Uuid,
/// Runtime request identity. Explicit retry mints a fresh one.
pub action_id: AgentActionId,
/// Original persona configuration, prompt, attachments and owner.
pub command: OpenSession,
/// Durable execution state.
pub state: ConversationTurnState,
/// Reply placement and runtime turn identity, recorded before delivery.
pub in_flight: Option<InFlightTurn>,
/// Outcome saved before patching the channel reply.
pub outcome: Option<ReplyOutcome>,
/// Whether the channel reply has been reconciled with the saved outcome.
pub reply_finalized: bool,
/// What the reply shows, saved by the replica that watched the turn
/// before it showed any of it, and once more, finished, before its
/// outcome.
#[serde(default)]
pub reply_segments: Option<Vec<agent_fold::domain::model::ProjectedSegment>>,
/// Stable admission order within the transcript.
pub created_at: chrono::DateTime<chrono::Utc>,
}
/// Small transcript projection. Polling must never reload stored prompts or attachments.
#[derive(Debug, Clone, serde::Deserialize)]
pub struct ConversationTurnStatus {
/// Source channel message.
pub source_message_id: Uuid,
/// Persona answering the message.
pub bot_id: BotId,
/// Session that accepted this message.
pub session_id: AgentSessionId,
/// Current attempt identity.
pub action_id: AgentActionId,
/// Reply in the channel timeline, when one was reserved.
pub reply_message_id: Option<Uuid>,
/// Current execution state.
pub state: ConversationTurnState,
/// Whether an explicit retry would be accepted now: the attempt failed,
/// stopped or was interrupted, and its reply already says so.
pub retryable: bool,
/// Stable admission ordering.
pub created_at: chrono::DateTime<chrono::Utc>,
}
/// Persistence mechanics only; authorization and execution decisions stay in the harness.
/// A held reply reconciliation claim. Dropping it releases the claim soon
/// after, including on cancellation; no external action is retried by acquiring
/// this lease.
#[async_trait::async_trait]
pub trait ConversationLease: Send {
/// Release now, so the session is free for the next claim once this
/// returns. Dropping leaves the release to a background task, so a claim
/// made straight after a drop can still find the lease held.
async fn release(self: Box<Self>);
}
/// The persona configuration adopted when a conversation's session first opens.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct ConversationSettings {
/// Instructions, model, runtime and selected tools for this context.
pub runtime: AgentRuntimeConfig,
/// Persona permission choice at adoption time. The operator may still revoke bypass.
pub permissions: PermissionPolicyConfig,
}
/// Persistence mechanics only; authorization and execution decisions stay in the harness.
#[async_trait::async_trait]
pub trait ConversationTurnStore: Send + Sync + 'static {
/// Serialize a session's bootstrap, before it has a manager to route to.
/// Waits briefly for capacity; `None` means another holder has the session,
/// or this replica had no connection to spare.
async fn claim_delivery(
&self,
session: AgentSessionId,
) -> Result<Option<Box<dyn ConversationLease>>>;
/// Serialize context changes with runtime dispatch across replicas.
/// Waits briefly for capacity; `None` means another holder has the session,
/// or this replica had no connection to spare.
async fn claim_context(
&self,
session: AgentSessionId,
) -> Result<Option<Box<dyn ConversationLease>>>;
/// Cancel work that has not dispatched when its owner starts fresh.
async fn cancel_queued(&self, session: AgentSessionId) -> Result<()>;
/// Read the settings adopted by this session, if it has opened.
async fn settings(&self, session: AgentSessionId) -> Result<Option<ConversationSettings>>;
/// First writer wins, including a crash before the runtime row was created.
async fn pin_settings(
&self,
session: AgentSessionId,
settings: ConversationSettings,
) -> Result<ConversationSettings>;
/// Atomically insert or return the first admission of the message for the
/// command's persona, never replacing its payload. The first admission
/// mints the turn's action id.
async fn admit(
&self,
session: AgentSessionId,
channel: Uuid,
command: OpenSession,
) -> Result<ConversationTurn>;
/// Read a source message's durable admission for one persona.
async fn get(&self, source: Uuid, bot: BotId) -> Result<Option<ConversationTurn>>;
/// Resolve the current runtime attempt without assuming its id is the source id.
async fn by_action(&self, action: AgentActionId) -> Result<Option<ConversationTurn>>;
/// Compare-and-swap an explicitly retried failed attempt to a fresh action id.
async fn retry(&self, source: Uuid, bot: BotId, expected: AgentActionId) -> Result<bool>;
/// At most one replica can move a queued attempt onto a runtime.
async fn claim(&self, action: AgentActionId, turn: &InFlightTurn) -> Result<bool>;
/// Save the posted reply before the model can begin producing output.
async fn record_flight(&self, action: AgentActionId, turn: &InFlightTurn) -> Result<()>;
/// Reserve final reply IDs under a reply lease. Returns false if the
/// attempt is no longer terminal and unfinalized, or its saved flight
/// changed since it was read. No message may be posted on a failed write.
async fn record_reply_flight(
&self,
action: AgentActionId,
expected: &InFlightTurn,
turn: &InFlightTurn,
) -> Result<bool>;
/// Save what a turn's reply shows: as its messages are posted, and
/// finished, before its outcome. Whichever replica reconciles the reply
/// then posts the same messages, and closes the ones already posted.
async fn save_reply(
&self,
action: AgentActionId,
segments: &[agent_fold::domain::model::ProjectedSegment],
) -> Result<()>;
/// Save a terminal outcome before resolving the channel message.
async fn finish(
&self,
action: AgentActionId,
state: ConversationTurnState,
outcome: ReplyOutcome,
) -> Result<()>;
/// Fail an attempt that never dispatched. An attempt some replica has
/// claimed meanwhile is left to that replica.
async fn fail_queued(&self, action: AgentActionId) -> Result<()>;
/// Mark the saved outcome as visible in the channel.
async fn finalize_reply(&self, action: AgentActionId, outcome: &ReplyOutcome) -> Result<()>;
/// Serialize reply reconciliation across replicas, with release on process loss.
/// Waits briefly for capacity; `None` means another holder is reconciling
/// the reply, or this replica had no connection to spare.
async fn claim_reply(
&self,
action: AgentActionId,
) -> Result<Option<Box<dyn ConversationLease>>>;
/// Durable queued work for recovery, in admission order: the oldest
/// queued turn of each session that nothing runs or blocks, which is
/// exactly what a claim could take.
async fn pending(&self, limit: u16) -> Result<Vec<ConversationTurn>>;
/// Claims whose session manager must still be alive; used only for recovery checks.
async fn running(&self, limit: u16) -> Result<Vec<ConversationTurn>>;
/// Read the statuses of the turns one persona's conversation in a channel
/// shows, oldest first, after the caller's access to it was checked:
/// every turn still queued or running, each session's first turn, and
/// the latest `recent` turns.
async fn for_conversation(
&self,
channel: Uuid,
bot: BotId,
recent: u16,
) -> Result<Vec<ConversationTurnStatus>>;
/// Completed outcomes whose reply patch must be retried, without running a model.
async fn pending_replies(&self, limit: u16) -> Result<Vec<ConversationTurn>>;
}