Skip to content
Skills Library
Programming · Building & coding

atomr-actor-design

/atomr-actor-design

Use when authoring or modifying an actor in a project that depends on atomr. Covers Msg type design, the Actor trait's lifecycle hooks, ask vs tell, supervision strategies, and FSM patterns. Triggers on writing or editing an `impl Actor`, choosing a Msg enum, picking a supervision strategy, or wiring `Props`/`ActorSystem`.

Repository
atomr ↗
Licence
Apache-2.0
Last repository change
30.05.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 30.05.2026, without references to retired models or APIs.

Licence

Apache-2.0, otwarta: można używać i zmieniać, z zachowaniem licencji i informacji o zmianach.

What it does and when to use it

Use when authoring or modifying an actor in a project that depends on atomr. Covers Msg type design, the Actor trait's lifecycle hooks, ask vs tell, supervision strategies, and FSM patterns. Triggers on writing or editing an `impl Actor`, choosing a Msg enum, picking a supervision strategy, or wiring `Props`/`ActorSystem`.

Original author description

Use when authoring or modifying an actor in a project that depends on atomr. Covers Msg type design, the Actor trait's lifecycle hooks, ask vs tell, supervision strategies, and FSM patterns. Triggers on writing or editing an `impl Actor`, choosing a Msg enum, picking a supervision strategy, or wiring `Props`/`ActorSystem`.

How it works

  1. Mental model
  2. The Actor trait
  3. Designing the Msg type
  4. Spawning an actor
  5. Tell vs ask
  6. Supervision
  7. FSM actors
  8. When to reach beyond core
  9. IO managers
  10. Canonical references
Original SKILL.md headings · 175 lines, 841 words
SKILL.md preview
---
name: atomr-actor-design
description: Use when authoring or modifying an actor in a project that depends on atomr. Covers Msg type design, the Actor trait's lifecycle hooks, ask vs tell, supervision strategies, and FSM patterns. Triggers on writing or editing an `impl Actor`, choosing a Msg enum, picking a supervision strategy, or wiring `Props`/`ActorSystem`.
---

# Authoring atomr actors

This skill helps you write idiomatic actors against `atomr` (umbrella
crate `atomr` on crates.io, imported as `atomr`).

## Mental model

A atomr actor is **state + behavior + a typed mailbox**. Each actor:

1. owns its state (no shared mutability),
2. declares one associated `Msg` type (typically an enum),
3. processes messages serially via `async fn handle(&mut self, ctx, msg)`,
4. is addressed by an `ActorRef<Msg>` returned from
   `ActorSystem::actor_of(...)`.

Tell-style sends are fire-and-forget; ask-style sends return a future
resolved by the actor's reply. Failure is supervised by the parent —
the actor itself does not catch panics.

## The Actor trait

Defined in `atomr_core::actor::traits`; available from the prelude.

```rust
use atomr::prelude::*;

#[derive(Default)]
struct Greeter { count: u64 }

#[async_trait]
impl Actor for Greeter {
    type Msg = String;

    async fn handle(&mut self, _ctx: &mut Context<Self>, msg: String) {
        self.count += 1;
        println!("hi {msg} (#{count})", count = self.count);
    }

    // Optional lifecycle hooks (defaults are no-ops):
    async fn pre_start(&mut self, _ctx: &mut Context<Self>) {}
    async fn post_stop(&mut self, _ctx: &mut Context<Self>) {}
    async fn pre_restart(&mut self, _ctx: &mut Context<Self>, _err: &str) {}
    async fn post_restart(&mut self, _ctx: &mut Context<Self>, _err: &str) {}

    fn supervisor_strategy(&self) -> SupervisorStrategy {
        SupervisorStrategy::default()
    }
}
```

Keep in mind:
- `Actor: Sized + Send + 'static` and `Msg: Send + 'static`.
- `handle` is `async`. Long-running work blocks this actor's mailbox —
  spawn a child or pipe a future via `pattern::pipe_to` for parallelism.
- Lifecycle hooks let you re-establish resources after a restart.
  `pre_restart` runs on the dying instance; `post_restart` runs on the
  fresh one. Clean up handles in `post_stop`.

## Designing the Msg type

- **Prefer one `enum` per actor.** It documents the protocol and lets
  the compiler enforce exhaustiveness in `handle`.
- **Make replies addressable.** For ask-style flows, include a reply
  channel inside the variant, or use `pattern::ask` which handles the
  oneshot for you.
- **Keep messages owned and `Send`.** Avoid borrowed data; an
  `ActorRef` lives across threads.
- **Don't smuggle behavior via closures.** Messages should be data;
  behavior belongs in `handle`. This keeps messages serializable for
  remoting/clustering.

```rust
enum Cmd {
    Get { key: String, reply: tokio::sync::oneshot::Sender<Option<String>> },
    Put { key: String, value: String },
}
```

## Spawning an actor

```rust
let system = ActorSystem::create("app", Config::empty()).await?;
let greeter = system.actor_of(Props::create(Greeter::default), "greeter")?;
greeter.tell("world".to_string());
system.terminate().await;
```

`Props::create(F)` takes a factory `F: Fn() -> A` so the supervisor can
re-instantiate the actor on restart. Don't capture mutable state in the
closure — capture `Arc`-shared dependencies instead.

## Tell vs ask

- **`actor_ref.tell(msg)`** — fire-and-forget, infallible, fastest path.
  Use this for the vast majority of message flows.
- **`pattern::ask(actor_ref, msg, timeout)`** — request/response. Returns
  a future resolved by the actor's reply, or a timeout error. The
  actor must be authored to send a reply (often via a oneshot inside
  the message).

Don't `ask` from inside `handle` — it blocks the mailbox. Use
`pattern::pipe_to` to fold an `ask`'s future back into a self-tell.

## Supervision

The default strategy is one-for-one with restart-on-panic. Customize
in `supervisor_strategy`:

```rust
use atomr::prelude::*;
use std::time::Duration;

fn supervisor_strategy(&self) -> SupervisorStrategy {
    OneForOneStrategy::new()
        .with_max_retries(3)
        .with_within(Duration::from_secs(60))
        .with_decider(|err| {
            if err.contains("transient") { Directive::Restart } else { Directive::Stop }
        })
        .into()
}
```

Directives: `Resume` (drop the message, keep state), `Restart` (rebuild
state via the factory), `Stop` (shut the child down), `Escalate` (defer
the decision to the grandparent).

## FSM actors

For protocols with explicit states, prefer the `fsm_macro` (or the
hand-written FSM pattern in `atomr_core::actor`) over branching on a
state field inside `handle`. See `crates/atomr-core/src/fsm_macro.rs`
and `crates/atomr-persistence/src/persistent_fsm.rs` for the pattern.

## When to reach beyond core

| You need… | Reach for… |
|---|---|
| State that survives restart (event sourcing) | `atomr-persistence` — see `atomr-persistence` skill |
| Cross-process / cross-host messaging | `atomr-remote` |
| Membership, sharding, pub/sub | `atomr-cluster*` — see `atomr-cluster` skill |
| Reactive pipelines | `atomr-streams` |
| Deterministic tests | `atomr-testkit` — see `atomr-testing` skill |

## IO managers

For TCP/UDP work, use the IO manager actors rather than touching
sockets directly. `TcpManager` accepts a `Bind { addr }` command for
listeners and a `Connect { addr }` command for outbound connections;
`TcpManager::connect(addr)` is a convenience helper that wraps the
latter. Both return an `ActorRef` for the connection actor that owns
the socket and forwards `Received` / `Closed` messages.

## Canonical references

- `crates/atomr-core/src/actor/traits.rs` — `Actor` trait definition
- `crates/atomr-core/src/supervision.rs` — strategies and directives
- `crates/atomr-core/src/lib.rs` — prelude contents
- `examples/pingpong/` — minimal tell-loop
- `examples/fault-tolerance/` — restart semantics + lifecycle hooks
- `docs/idiomatic-rust.md` — design choices behind the API
- `docs/architecture.md` — runtime structure

## Common mistakes

- **Awaiting long futures inside `handle`.** Mailbox stalls. Use
  `pattern::pipe_to` to fold the result back as a future message.
- **Capturing mutable state in `Props::create`.** Restart re-runs the
  factory; mutable captures violate that contract.
- **Borrowed data in `Msg`.** `Msg: Send + 'static` — own your bytes.
- **Catching panics in `handle`.** Let the supervisor decide.
- **Using `ask` where `tell` would do.** `ask` allocates a oneshot per
  call; on hot paths a tell + reply-via-message is cheaper.

Versions

The database has no other skills named atomr-actor-design.

Database snapshot: 13.11.2026 · package version 38721d63eee4 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100atomr-persistence
    Use when adding event sourcing to an actor in a project that depends on atomr. Covers the Eventsourced trait, journals, snapshots, recovery, RecoveryPermitter, AsyncSnapshotter, and choosing a storage adapter (sql / redis / mongodb / cassandra / aws / azure). Triggers when…
  • 100atomr-python
    Use when working with atomr's Python bindings — authoring Python actors, choosing a GIL/dispatcher strategy (python-pinned / python-subinterpreter-pool / python-nogil / python-subprocess), using ask/tell from async or sync code, or declaring C-extension subinterpreter…
  • 100atomr-agents-tools
    Use when authoring or modifying a `Tool` / `RichTool`, designing a tool's `ToolDescriptor` schema, returning a `ToolReturn::Command`, wiring tools into an `Agent`'s `ToolStrategy`, or using built-in tools (`HandoffTool`, `WriteMemoryTool`, `RecallMemoryTool`). Triggers on `impl…
  • 100atomr-agents-quickstart
    Use when standing up the first atomr-agents project, picking feature flags for the `atomr-agents` umbrella, building a first `Agent`, or running an end-to-end agent turn against `MockRunner`. Triggers on adding `atomr-agents = ...` to Cargo.toml, writing the first `Agent { ...…
  • 100actor-tool-registration
    Use when asked to "register a tool in the tool registry", "add a ToolId", "add GetToolDefinition", "expose an actor tool schema", or when working on a "Register XToolRequest in ToolRegistry" task. Don't use for implementing a brand-new Actor tool from scratch or for migrating…
  • 90Linear OAuth + Actor Authorization
    This skill should be used when implementing Linear OAuth 2.0, OAuth actor authorization, or file-storage authentication. Activates on "linear oauth", "linear auth", "actor token", "linear-actor-token", "file storage".

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.