cross.stream / docs
GitHub Discord

Actors

cross.stream actors use Nushell closures to process and act on incoming frames as they are appended to the store.

{
  run: {|frame, state|
    if $frame.topic == "ping" {
      {out: {reply: "pong"}, next: $state}
    } else {
      {next: $state}
    }
  }
}

The actor closure receives each new frame and the current state value. It returns a record that controls output and state:

The out value must be a record. It is stored as the output frame's metadata, alongside actor_id and frame_id. The output frame is appended to <actor-name>.out by default. The closure can also explicitly append frames using the .append command.

Registering

To register an actor, append a registration script with the topic xs.actor.<name>.create. The script must return a record that configures the actor's behavior:

r###'{
  # Required: Actor closure (frame, state) -> {out?, next?}
  run: {|frame, state|
    if $frame.topic == "ping" {
      {out: {reply: "pong"}, next: $state}
    } else {
      {next: $state}
    }
  }

  # Optional: Where to start processing from
  # "new" (default), "first", or scru128 ID
  start: "new"

  # Optional: Heartbeat interval in ms
  pulse: 1000

  # Optional: a topic filter (see Read Options). Omit to receive every
  # frame. The actor's own lifecycle frames and synthetic xs.pulse /
  # xs.threshold are always delivered regardless of the filter.
  topics: ["ping"]

  # Optional: Control output frame behavior
  return_options: {
    suffix: ".response" # Output topic suffix
    ttl: "last:1" # Keep only most recent frame
  }
}'### | .append xs.actor.echo.create

The run closure must accept exactly two positional arguments: the incoming frame and the current state value.

The registration script is stored in CAS and evaluated to obtain the actor's configuration.

Upon a successful start the actor appends an xs.actor.<name>.active frame with metadata:

Configuration Record Fields

FieldDescription
runRequired actor closure {|frame, state| -> {out?, next?}}
initialInitial state value (default: null, or the closure param default)
startWhere to start: "new" (default), "first", or scru128 ID (see Read Options)
pulseHeartbeat interval in ms (see Read Options)
topicsTopic filter: list of strings or one comma-separated string (see Read Options)
return_optionsControls output frames: see Return Options

Return Options

The return_options field controls how return values are handled:

Modules

Actors can use modules registered via xs.module.<name> topics. An actor sees the modules as they existed when it was registered. See Module Topics for details.

r###'{
  run: {|frame, state|
    use my-math
    {out: {result: (my-math double 8)}, next: $state}
  }
}'### | .append xs.actor.processor.create

State

Actors thread state explicitly through the next key in the return record. The second closure parameter receives the current state, and next sets the state for the next invocation:

r#'{
  run: {|frame, state|
    let count = $state + 1
    {out: {count: $count}, next: $count}
  }
  initial: 0
}'# | .append xs.actor.counter.create

The initial state is resolved in order:

  1. The initial field in the config record
  2. The default value on the closure's second parameter (e.g. state = 0)
  3. null if neither is provided

Output

Actors can produce output in two ways:

  1. Return Values: The out record is stored as frame metadata and appended to the actor's output topic (<actor-name>.out by default unless modified by return_options.suffix).
{|frame, state|
  if $frame.topic == "ping" {
    {out: {reply: "pong"}, next: $state}
  } else {
    {next: $state}
  }
}
  1. Explicit Appends: Use the .append command to create frames on any topic.
{|frame, state|
  if $frame.topic == "ping" {
    "pong" | .append response.topic --meta { "type": "response" }
    "logged" | .append audit.topic
    {next: $state}
  } else {
    {next: $state}
  }
}

Keep actors quick

Actors are short-lived choke points, like actors in an async framework: they apply business logic serially, one frame at a time, in order, and should finish in a few milliseconds. Each step mints the state of things as of that point in the stream, and since only one instance of an actor runs at a time, there is no race in applying that logic.

A step's .append calls are held until the closure returns, then written together with its out frame in a single commit. A reader sees all of them or none, and no other writer's frames land between them. If the closure errors, none are written. --ttl ephemeral frames are held too (they are broadcast, not stored), so an actor cannot report progress mid-step.

For long work, such as a slow external call, use a service that follows the stream. Its appends land immediately, and generate carries state from one frame to the next:

{
  run: {||
    .cat --follow --new --topic job.start
    | generate {|frame, n = 0|
        $"job ($n + 1): working" | .append job.progress --ttl ephemeral
        # ... long-running work, appending progress as it goes ...
        $"job ($n + 1): done" | .append job.result
        {next: ($n + 1)}
      }
    | ignore
  }
}

All output frames automatically include:

Built-in Store Commands

Inside the closure an actor has the same store helper commands as actions and services:

Lifecycle

Actors share the unified lifecycle vocabulary documented in the lifecycle reference. In summary:

User inputRuntime ack on successRuntime ack on failure
xs.actor.<name>.createxs.actor.<name>.activexs.actor.<name>.invalid
xs.actor.<name>.termxs.actor.<name>.fin.term,

Plus the runtime emits:

See the lifecycle reference for the full event list, the compaction algorithm, and the invariants the dispatcher honors.