/prometheus-rust-skills:actor-modelfrom prometheus-rust-skills
Tokio-native actor model pattern for Prometheus AGS projects. Implements actors as tokio tasks communicating via mpsc channels with a typed message enum, enabling safe concurrent state management without shared locks. Used in UAR session management, pk-librarian event broadcasting, and parking-lot scheduler design. Use when a component needs to own state exclusively while serving concurrent callers.
Ships in prometheus-rust-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
An actor is a tokio task that owns its state exclusively. External code communicates
with the actor through typed messages over an mpsc channel. This eliminates the
need for Arc<Mutex<T>> on complex stateful objects.
use tokio::sync::{mpsc, oneshot};
// 1. Define the message enum — each variant carries its reply channel
pub enum LibrarianMessage {
Compile {
raw: RawDoc,
reply: oneshot::Sender<anyhow::Result<WikiEntry>>,
},
Lint {
reply: oneshot::Sender<Vec<LintReport>>,
},
Focus {
topic: String,
reply: oneshot::Sender<String>,
},
Shutdown,
}
// 2. Define the actor handle — the public API
#[derive(Clone)]
pub struct LibrarianHandle {
tx: mpsc::Sender<LibrarianMessage>,
}
impl LibrarianHandle {
pub async fn compile(&self, raw: RawDoc) -> anyhow::Result<WikiEntry> {
let (reply_tx, reply_rx) = oneshot::channel();
self.tx
.send(LibrarianMessage::Compile { raw, reply: reply_tx })
.await
.map_err(|_| anyhow::anyhow!("librarian actor shut down"))?;
reply_rx.await.map_err(|_| anyhow::anyhow!("librarian did not reply"))?
}
pub async fn shutdown(&self) {
let _ = self.tx.send(LibrarianMessage::Shutdown).await;
}
}
// 3. Define the actor struct — owns its private state
struct LibrarianActor {
store: MarkdownStore,
model_router: ModelRouter,
event_tx: tokio::sync::broadcast::Sender<LibrarianEvent>,
}
impl LibrarianActor {
async fn run(mut self, mut rx: mpsc::Receiver<LibrarianMessage>) {
while let Some(msg) = rx.recv().await {
match msg {
LibrarianMessage::Compile { raw, reply } => {
let result = self.compile_inner(raw).await;
let _ = reply.send(result); // ignore if caller dropped
}
LibrarianMessage::Lint { reply } => {
let reports = self.lint_inner().await;
let _ = reply.send(reports);
}
LibrarianMessage::Focus { topic, reply } => {
let focus = self.focus_inner(&topic).await;
let _ = reply.send(focus);
}
LibrarianMessage::Shutdown => break,
}
}
tracing::info!("LibrarianActor shutting down");
}
async fn compile_inner(&mut self, raw: RawDoc) -> anyhow::Result<WikiEntry> {
// State mutation is safe — we own `self` exclusively
let entry = self.model_router.compile(&raw).await?;
self.store.upsert(&entry).await?;
let _ = self.event_tx.send(LibrarianEvent::Compiled {
entry_id: entry.id.clone(),
});
Ok(entry)
}
}
// 4. Spawn and return the handle
pub fn spawn_librarian(
store: MarkdownStore,
model_router: ModelRouter,
event_tx: tokio::sync::broadcast::Sender<LibrarianEvent>,
buffer: usize,
) -> LibrarianHandle {
let (tx, rx) = mpsc::channel(buffer);
let actor = LibrarianActor { store, model_router, event_tx };
tokio::spawn(actor.run(rx));
LibrarianHandle { tx }
}
For actors that must restart on failure, use a supervision loop:
pub async fn supervise_librarian(
make_actor: impl Fn() -> LibrarianActor + Send + 'static,
tx: mpsc::Sender<LibrarianMessage>,
mut rx: mpsc::Receiver<LibrarianMessage>,
) {
loop {
let actor = make_actor();
let result = tokio::spawn(actor.run_until_panic(/* ... */)).await;
match result {
Ok(()) => break, // clean shutdown
Err(e) => {
tracing::error!(error = ?e, "actor panicked, restarting in 1s");
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
}
}
| Situation | Use |
|---|---|
| Simple data read/write, no logic | Arc<RwLock<T>> |
| Complex stateful logic with many operations | Actor pattern |
| State must be mutated in response to async I/O | Actor pattern |
| Multiple structs sharing the same data | Arc<RwLock<T>> |
| Need to serialize access to external resource | Actor pattern |
The UAR's parking-lot scheduler is an actor: it owns the job queue exclusively,
processes submissions via mpsc, and emits JobEvents on a broadcast channel.
The scheduler never shares its queue with Arc<Mutex<_>>.
reply.send() and then panicking on the error — use let _ = reply.send(...)reply sender beyond the handler — reply immediately or it leaksArc<Mutex<>> when actor pattern is cleanerunwrap() — all panics escape the task and kill the actor silentlyWhat SKILL.md sets; muted values are Claude Code's defaults
Other plugins' skills for the same job
npx claudepluginhub prometheus-ags/prometheus-skill-system --plugin prometheus-rust-skills13 skills in this plugin
/async-patternsCanonical async Rust patterns for Prometheus AGS projects. Covers tokio task spawning, Arc<RwLock<T>> vs parking_lot::Mutex selection, blocking guard prevention, broadcast channels, graceful shutdown, and structured concurrency. Use whenever writing async code in any Prometheus Fabric crate.
/axum-patternsAxum 0.8 router, middleware, extractor, and state injection patterns for Prometheus AGS projects. Enforces the canonical Axum structure: typed state via Extension/State extractors, Tower middleware composition, structured error types, and Axum-native SSE for MCP servers. Use when building any Axum HTTP service, MCP server, or API gateway in the Prometheus stack.
/error-handlingCanonical Rust error handling for Prometheus AGS projects. Enforces the thiserror/anyhow boundary: thiserror for library crate errors (typed, composable), anyhow for application code (ergonomic, context-rich). Includes #[cold] error path annotation, no unwrap/expect in non-test code, and structured error propagation via the ? operator.
/karpathy-tokenizerTrain GPT-style BPE tokenizers with rustbpe (Karpathy), export to tiktoken for fast Rust inference, and integrate with agent-tokenizer for prompt-budget enforcement./mcp-serverCanonical Axum MCP server pattern for Prometheus AGS projects. Implements the MCP protocol as JSON-RPC 2.0 over HTTP POST /mcp with optional SSE event stream at GET /events. Covers tool registration, handler dispatch, SSE fan-out via broadcast, and stdio transport for Claude Desktop integration. Use when building any new MCP-enabled service in the Prometheus stack.
/performanceProduction-grade Rust performance primitives for Prometheus AGS projects. Covers jemalloc global allocator, #[cold]/#[inline(never)] for error paths, MaybeUninit for zero-cost initialization, std::mem::take for ownership without clone, Arc placement discipline, parking_lot over std::sync, and SIMD-aware buffer patterns. Apply when writing hot paths, server binaries, or inference-adjacent code.
2 plugins in one repository ship this exact skill · first indexed Nov 27, 2026