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.

10.9 KB
Raw Blame
//! Routes container provisioning between the runtimes this deployment hosts.
//!
//! One deployment can manage two bots: a sandboxed bot, whose sessions come
//! from whichever provider [`HarnessContainers`] holds (Daytona, or local
//! Docker when a developer opts in), and the in-memory agent bot, whose
//! sessions run in-process. The session row's `bot_id` - written before any
//! container is asked for - decides which runtime a session's transport comes
//! from.

use agent_egress::domain::model::SessionToken;
use agent_harness::domain::error::{HarnessError, Result};
use agent_harness::domain::model::{AgentKind, SpawnContainer};
use agent_harness::domain::ports::ContainerManager;
use agent_harness::domain::sandbox::SandboxResizeEffect;
use agent_harness::outbound::containers::{HarnessContainer, HarnessContainers};
use agent_harness::outbound::sidecar::SidecarSender;
use agent_inmem::domain::engine::{AgentIdentity, ReplyDelivery};
use agent_inmem::outbound::manager::{InMemAgentManager, SessionFacts};
use agent_runtime_protocol::domain::connection::ServerChannel;
use agent_runtime_protocol::domain::ports::{Transport, TransportError, TransportSender};
use agent_runtime_protocol::domain::schema::v0::{ToRuntimeMessage, ToServerMessage};
use agent_session::domain::error::AgentSessionError;
use agent_session::domain::model::{AgentSessionId, SandboxSize, SessionManagement};
use agent_session::domain::service::AgentSessionService;

#[cfg(test)]
mod test;

/// A session transport from whichever runtime the session's bot uses.
pub enum RoutedTransport {
    /// A sandboxed session's sidecar-backed transport.
    Sandbox(HarnessContainer),
    /// An in-process session's channel.
    InMem(ServerChannel),
}

/// The sending half of a [`RoutedTransport`].
pub enum RoutedSender {
    /// Sends into the sandbox sidecar socket.
    Sandbox(SidecarSender),
    /// Sends into the in-process agent.
    InMem(tokio::sync::mpsc::UnboundedSender<ToRuntimeMessage>),
}

impl Transport<ToRuntimeMessage, ToServerMessage> for RoutedTransport {
    type Sender = RoutedSender;
    type Receiver = tokio::sync::mpsc::UnboundedReceiver<ToServerMessage>;

    fn split(self) -> (Self::Sender, Self::Receiver) {
        match self {
            Self::Sandbox(transport) => {
                let (sender, receiver) = transport.split();
                (RoutedSender::Sandbox(sender), receiver)
            }
            Self::InMem(channel) => {
                let (sender, receiver) = channel.split();
                (RoutedSender::InMem(sender), receiver)
            }
        }
    }
}

impl TransportSender<ToRuntimeMessage> for RoutedSender {
    async fn send(&self, message: ToRuntimeMessage) -> std::result::Result<(), TransportError> {
        match self {
            Self::Sandbox(sender) => sender.send(message).await,
            Self::InMem(sender) => TransportSender::send(sender, message).await,
        }
    }
}

/// The shared in-memory runtime used by every in-memory agent session.
pub struct InMemRuntime {
    /// In-memory session provisioner.
    pub manager: InMemAgentManager,
}

/// The deployment's [`ContainerManager`]: the sandbox provider for the
/// sandboxed bot, in-process for the in-memory bot when one is configured.
pub struct RoutedContainers<Sessions> {
    sandbox: HarnessContainers,
    inmem: Option<InMemRuntime>,
    sessions: Sessions,
}

#[derive(Debug)]
enum Route {
    Sandbox,
    /// Boxed: the facts dwarf the dataless `Sandbox` variant, and a route is
    /// resolved once per container operation, not on a hot path.
    InMem(Box<SessionFacts>),
}

impl<Sessions> RoutedContainers<Sessions>
where
    Sessions: AgentSessionService,
{
    /// Route by bot: `sessions` is where a session's bot is looked up.
    pub fn new(
        sandbox: HarnessContainers,
        inmem: Option<InMemRuntime>,
        sessions: Sessions,
    ) -> Self {
        Self {
            sandbox,
            inmem,
            sessions,
        }
    }

    /// Which runtime serves `session`. The row exists before any container
    /// operation - `open` creates it first - so the lookup is authoritative.
    ///
    /// Deliberately has no "when in doubt, use the sandbox" branch. The
    /// in-process bot must never be handed a sandbox: it has no repository to
    /// clone and no reason to cost a Daytona container, so a deployment that
    /// cannot serve it refuses the session instead of silently provisioning
    /// the wrong runtime for it.
    async fn route(&self, session: AgentSessionId) -> Result<Route> {
        let row = self
            .sessions
            .get_session(session)
            .await
            .map_err(HarnessError::Session)?;
        if self.inmem.is_some()
            && AgentKind::for_session(row.bot_id, &row.harness) == AgentKind::InMemory
        {
            let bot = self
                .sessions
                .session_bot(row.bot_id)
                .await
                .map_err(HarnessError::Session)?;
            return Ok(Route::InMem(Box::new(SessionFacts {
                id: session,
                owner: row.owner_id,
                // A thread's reply is one message, rewritten only as each
                // passage finishes; an opening line lands with what follows it.
                delivery: if row.thread_id.is_some() {
                    ReplyDelivery::Whole
                } else {
                    ReplyDelivery::Streamed
                },
                model: row.model,
                identity: Some(AgentIdentity {
                    bot: row.bot_id,
                    handle: bot_id::mention_handle(row.bot_id, &bot.handle).to_owned(),
                    name: bot.name,
                }),
                instructions: row.instructions,
                acp_session_id: row.acp_session_id,
            })));
        }
        if AgentKind::for_session(row.bot_id, &row.harness) == AgentKind::InMemory {
            return Err(HarnessError::Container(format!(
                "session {session} belongs to the in-process bot, which this deployment does not serve"
            )));
        }
        Ok(Route::Sandbox)
    }

    fn inmem(&self) -> &InMemRuntime {
        self.inmem
            .as_ref()
            .expect("an in-memory route exists only when the runtime is configured")
    }
}

impl<Sessions> ContainerManager for RoutedContainers<Sessions>
where
    Sessions: AgentSessionService,
{
    type Transport = RoutedTransport;

    async fn spawn(
        &self,
        command: SpawnContainer,
    ) -> Result<agent_session::domain::connection::RuntimeAttachment<Self::Transport>> {
        match self.route(command.session_id).await? {
            Route::InMem(facts) => Ok(agent_session::domain::connection::RuntimeAttachment::solo(
                RoutedTransport::InMem(
                    self.inmem()
                        .manager
                        .attach(*facts, Some(command.egress.session_token))
                        .await?,
                ),
            )),
            Route::Sandbox => self
                .sandbox
                .spawn(command)
                .await
                .map(|attachment| attachment.map_transport(RoutedTransport::Sandbox)),
        }
    }

    /// Delegated to the sandbox provider: the signature carries no session,
    /// so there is no bot to route on. An in-process session has no sandbox,
    /// so the worst this costs is a needless close-and-resume before a
    /// [`Self::resize`] that does nothing.
    fn resize_effect(&self, from: SandboxSize, to: SandboxSize) -> SandboxResizeEffect {
        self.sandbox.resize_effect(from, to)
    }

    /// A no-op for in-process sessions - there is no container whose compute
    /// could change - and the provider's business for everyone else.
    async fn resize(&self, session: AgentSessionId, size: SandboxSize) -> Result<()> {
        match self.route(session).await? {
            Route::InMem(_) => Ok(()),
            Route::Sandbox => self.sandbox.resize(session, size).await,
        }
    }

    /// An in-process session's raw token lives only in the process that was
    /// handed it, and the row keeps only its hash. Another replica may have
    /// resumed the session since, rotating the hash past any token this
    /// process still holds, so every resume rotates the credential. A session
    /// another live replica manages is refused before rotating: its claim
    /// would refuse this attach anyway, and the rotation would revoke the
    /// credential its live agent holds.
    async fn resume(
        &self,
        session: AgentSessionId,
    ) -> Result<agent_session::domain::connection::RuntimeAttachment<Self::Transport>> {
        match self.route(session).await? {
            Route::InMem(facts) => {
                let management = self
                    .sessions
                    .management(session)
                    .await
                    .map_err(HarnessError::Session)?;
                if let SessionManagement::Peer(manager) = management {
                    tracing::info!(
                        %session,
                        peer = %manager.replica,
                        "not rotating the credential of a session another replica manages"
                    );
                    return Err(HarnessError::Session(AgentSessionError::ManagedElsewhere(
                        session,
                    )));
                }
                let token = SessionToken::mint();
                self.sessions
                    .set_egress_token_hash(session, &token.hash())
                    .await
                    .map_err(HarnessError::Session)?;
                Ok(agent_session::domain::connection::RuntimeAttachment::solo(
                    RoutedTransport::InMem(
                        self.inmem()
                            .manager
                            .attach(*facts, Some(token.as_str().to_owned()))
                            .await?,
                    ),
                ))
            }
            Route::Sandbox => self
                .sandbox
                .resume(session)
                .await
                .map(|attachment| attachment.map_transport(RoutedTransport::Sandbox)),
        }
    }

    /// An in-process session has no container environment to read a token
    /// back from, so the manager remembers the one spawn or [`Self::resume`]
    /// handed it. Sandboxes delegate to their provider.
    async fn session_token(&self, session: AgentSessionId) -> Result<Option<String>> {
        match self.route(session).await? {
            Route::InMem(_) => Ok(self.inmem().manager.session_token(session)),
            Route::Sandbox => self.sandbox.session_token(session).await,
        }
    }

    async fn teardown(&self, session: AgentSessionId) -> Result<()> {
        match self.route(session).await? {
            Route::InMem(_) => {
                self.inmem().manager.teardown(session);
                Ok(())
            }
            Route::Sandbox => self.sandbox.teardown(session).await,
        }
    }
}