Skip to main content

rinne_core/worker/
descriptor.rs

1//! The worker capability descriptor (`CONTEXT.md` §8).
2//!
3//! Every worker advertises what it can do, how it authenticates, its quota
4//! model, latency profile, and transport. The scheduler resolves a node's
5//! `needs` against these descriptors at dispatch time (`CONTEXT.md` §7, §13).
6
7use serde::{Deserialize, Serialize};
8
9/// A capability a worker can satisfy (`CONTEXT.md` §8).
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(rename_all = "kebab-case")]
12pub enum Capability {
13    CodeEdit,
14    RepoAware,
15    WebSearch,
16    Vision,
17    LongContext,
18    ToolRun,
19    CodeReview,
20    Reasoning,
21    Writing,
22}
23
24/// How a worker authenticates (`CONTEXT.md` §9). Surfaced by `doctor` so the
25/// user always knows which workers are free and which are metered.
26#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
27#[serde(rename_all = "kebab-case")]
28pub enum AuthMode {
29    /// Honors an existing subscription login (Claude Pro/Max, Grok, ChatGPT).
30    /// Not metered per-call.
31    Subscription,
32    /// Uses the user's own API key. Always metered.
33    ApiKey,
34    /// A free tier (e.g. a conductor backend's daily allowance).
35    Free,
36    /// Could not be determined.
37    Unknown,
38}
39
40impl AuthMode {
41    /// Whether using this worker bills the user per call.
42    pub fn is_metered(self) -> bool {
43        matches!(self, AuthMode::ApiKey)
44    }
45
46    pub fn label(self) -> &'static str {
47        match self {
48            AuthMode::Subscription => "subscription",
49            AuthMode::ApiKey => "api-key",
50            AuthMode::Free => "free",
51            AuthMode::Unknown => "unknown",
52        }
53    }
54}
55
56/// The worker family (`CONTEXT.md` §8). Drives dispatch and context assembly:
57/// a harness is an autonomous agent given a chunky task; an API worker is a raw
58/// model given a precise instruction with context inlined.
59#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
60#[serde(rename_all = "lowercase")]
61pub enum WorkerFamily {
62    /// Wraps a native headless CLI call (e.g. `claude -p`). Reads the repo
63    /// itself.
64    Harness,
65    /// A direct model API call on the user's own key. Stateless; sees only what
66    /// is sent.
67    Api,
68}
69
70/// The transport a worker speaks over (`CONTEXT.md` §8, §14). `Acp` is reserved
71/// for V2 and not implemented in v1.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "kebab-case")]
74pub enum Transport {
75    /// `tokio::process` with piped stdio, structured-output parsing.
76    SubprocessJson,
77    /// `reqwest` streaming, for API workers and the conductor backend.
78    Http,
79    /// Thin JSON-RPC 2.0 over stdio (V2).
80    Acp,
81}
82
83/// A worker's quota modeled as a refilling token bucket (`CONTEXT.md` §13).
84///
85/// The scheduler spreads load to avoid hitting limits. A subscription's
86/// rate-limit window, an API tier's RPM, and a free tier's daily reset all map
87/// onto this one shape.
88#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
89pub struct QuotaModel {
90    /// Maximum tokens (request units) the bucket holds.
91    pub capacity: f64,
92    /// Tokens refilled per minute.
93    pub refill_per_minute: f64,
94}
95
96impl QuotaModel {
97    /// An effectively-unmetered bucket, for workers without a known limit.
98    pub fn unlimited() -> Self {
99        Self {
100            capacity: f64::INFINITY,
101            refill_per_minute: f64::INFINITY,
102        }
103    }
104}
105
106/// A coarse latency expectation (`CONTEXT.md` §8, §21). Harness workers spawn a
107/// process and wait on model think time, so they are inherently slower than a
108/// raw API call.
109#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
110#[serde(rename_all = "lowercase")]
111pub enum LatencyProfile {
112    /// Raw API call, low latency.
113    Fast,
114    /// Typical harness round-trip.
115    Medium,
116    /// Heavy or known-slow harness.
117    Slow,
118}
119
120/// The full self-description a worker advertises to the scheduler.
121#[derive(Debug, Clone, Serialize, Deserialize)]
122pub struct WorkerDescriptor {
123    /// Stable worker name, e.g. `claude-code`, `codex`, `anthropic`.
124    pub name: String,
125    pub family: WorkerFamily,
126    pub capabilities: Vec<Capability>,
127    pub auth_mode: AuthMode,
128    pub quota: QuotaModel,
129    pub latency: LatencyProfile,
130    pub transport: Transport,
131    /// Models this worker can run (e.g. `["opus", "sonnet", "haiku"]`). Empty
132    /// means the worker has a single fixed model. The conductor picks from this
133    /// list per node to optimize cost/latency (`CONTEXT.md` §7).
134    #[serde(default)]
135    pub models: Vec<String>,
136}
137
138impl WorkerDescriptor {
139    /// Whether this worker advertises the given capability.
140    pub fn has(&self, cap: Capability) -> bool {
141        self.capabilities.contains(&cap)
142    }
143
144    /// Whether this worker satisfies every capability in `needs` — the
145    /// scheduler's core match test (`CONTEXT.md` §7, §13).
146    pub fn satisfies(&self, needs: &[Capability]) -> bool {
147        needs.iter().all(|n| self.has(*n))
148    }
149}