Skip to main content

rinne_types/worker/
mod.rs

1//! The `Worker` contract (`CONTEXT.md` §8).
2//!
3//! Anything that takes a subtask and does it is a worker. Two families
4//! (harness, API) share this one contract. The trait and its data types live in
5//! `rinne-core` because the loop engine's dispatcher (Phase 3) consumes the
6//! trait, while the concrete transports and adapters live in `rinne-workers`.
7
8mod descriptor;
9mod event;
10mod exec;
11
12pub use descriptor::{
13    AuthMode, Capability, LatencyProfile, QuotaModel, Transport, WorkerDescriptor, WorkerFamily,
14};
15pub use event::{emit, EventSink, WorkerEvent};
16pub use exec::{
17    format_token_count, Constraints, ContextPacket, ExecStatus, ExecuteRequest, ExecuteResult,
18    InlinedFile, McpServerSpec, McpTransportKind, Role, ToolSpec, Usage,
19};
20
21use async_trait::async_trait;
22use tokio_util::sync::CancellationToken;
23
24use crate::Result;
25
26/// A unit of work that can be dispatched to (`CONTEXT.md` §8).
27///
28/// Implementors normalize their underlying tool's output into [`ExecuteResult`].
29/// Streaming events go to the provided [`EventSink`]; cancellation is observed
30/// via the [`CancellationToken`] (used by `/pause`, budget kills, and
31/// stuck-detector aborts — `CONTEXT.md` §14).
32#[async_trait]
33pub trait Worker: Send + Sync {
34    /// The worker's self-description, used by the scheduler to resolve a node's
35    /// capability `needs` to a concrete worker.
36    fn descriptor(&self) -> &WorkerDescriptor;
37
38    /// Do one unit of work. Returns the normalized result, or an error if the
39    /// worker could not be driven at all (distinct from a worker that ran and
40    /// reported [`ExecStatus::Failed`]).
41    async fn execute(
42        &self,
43        request: ExecuteRequest,
44        events: EventSink,
45        cancel: CancellationToken,
46    ) -> Result<ExecuteResult>;
47
48    /// Whether this worker can actually run a node's attached MCP tools
49    /// (`MCP_SKILLS.md` §6): an API worker with the host agentic loop wired, or a
50    /// harness that can provision MCP servers into itself. The scheduler uses
51    /// this to keep a tool node off a worker that would silently drop its tools.
52    /// Default `false` — a worker opts in by overriding.
53    fn serves_mcp_tools(&self) -> bool {
54        false
55    }
56}
57
58/// Executes an MCP tool call on behalf of the host agentic loop (`MCP_SKILLS.md`
59/// §6). The trait seam keeps the worker adapters MCP-agnostic: an API worker
60/// holds a `dyn ToolExecutor` and calls it when the model emits a tool call; the
61/// concrete implementation (over a warm MCP connection pool) lives in the wiring
62/// layer.
63#[async_trait]
64pub trait ToolExecutor: Send + Sync {
65    /// Invoke a tool by its qualified id (`server.tool`) with JSON `arguments`,
66    /// returning the result rendered as text for the model to read. An error is
67    /// returned as a string the loop feeds back so the model can recover.
68    async fn call(
69        &self,
70        id: &str,
71        arguments: serde_json::Value,
72    ) -> std::result::Result<String, String>;
73}