Ruby Users Forum

A Ruby gem for building server-driven, reactive web applications. Sidereal combines a Rack-compatible router with an event-driven architecture using typed messages, commands, pages, SSE, and pub/sub.

Only one way to reason about business logic handling and UI updates, whether one-off or long-running, re-tryable tasks.

I talked about the motivation and techniques here.

Built on Datastar (SSE streaming, HTML morphing), Phlex (HTML rendering), Plumb (typed data), Async (fiber concurrency). Designed to run on Falcon.

See the examples directory for demos.

Installation

Add to your Gemfile:

bundle add sidereal

Or install directly:

gem install sidereal

Check what’s installed with sid info, which prints the Sidereal and Ruby versions:

sid info

Setup

Installing the gem gives you the sid command. Use it to generate a new app:

sid new my_app
cd my_app
bin/dev

sid new creates the my_app directory. Its config/components/secrets.rb gives Sidereal the app’s master secret, which Sidereal derives the keys for the session cookie and its signer from: SIDEREAL_SECRET, or else one the first boot creates in storage/secret.txt. So the app, its specs and every fresh checkout run with no setup. boot.rb loads a .env, if there is one, with dotenv. It runs bundle install and bin/sid skills update in it, and prints how to start the server. bin/dev starts it at http://localhost:9292. Open the page in two windows and say hello: every window sees every hello as it happens.

sid new options:

Option What it does
--rspec Adds RSpec to the Gemfile and runs rspec --init
--db Sets up a SQLite database, by running bin/sid db install in the new app
--sourced Uses Sourced for durable, event-sourced storage, by running bin/sid sourced install, which installs a database too
--sidereal-path PATH Uses a local checkout of Sidereal in the Gemfile, instead of GitHub
--skip-bundle Doesn’t run bundle install, or bin/sid skills update, which needs the bundle
--no-skills Doesn’t install AI agent skills with bin/sid skills update (see Agent skills)
--force Writes into a directory that isn’t empty

The app’s name comes before the options: sid new my_app --rspec.

What’s in a new app

my_app/
  boot.rb                     loads and configures the app
  config.ru                   runs App
  config/components/          the connections and services the app's classes use
                              (db.rb and sourced.rb once their installs have run)
  db/migrations/              database migrations, with --db or --sourced
  falcon.rb                   Falcon settings: HOST, PORT and COUNT (worker processes)
  bin/dev                     development server that reloads on code changes
  bin/sid                     the sid command line, with this app loaded
  .agents/skills/             skills that teach AI coding agents to use bin/sid
  .claude/skills              link to .agents/skills, for Claude Code
  system/                     backend code that doesn't know about the web, and
                              the app's own components (system/mailers/client.rb
                              is the mailers.client component)
    greetings.rb              messages for the hello demo
  web/
    app.rb                    App: the commands it accepts, their handlers, and pages
    ui/                       pages, components and the layout (the UI namespace)
      welcome_page.rb         UI::WelcomePage
      layout.rb               UI::Layout
      components/hello.rb     UI::Components::Hello
    assets/                   stylesheets, scripts and images App registers (see Assets)
  storage/                    runtime data: the command store, pubsub socket, databases

Classes are loaded by Zeitwerk, so there are no require statements: name a file after the class it defines. Classes in system/ are top-level (system/greetings.rb defines Greetings), and each directory under web/ is a namespace (web/ui/welcome_page.rb defines UI::WelcomePage).

boot.rb declares components from system/ with sourced-component’s from_dir: a class in a directory there that includes Sourced::Component::Installable is a component, keyed by its path and built with the components it asks for with dep. So system/mailers/client.rb, defining Mailers::Client, is the mailers.client component, and a class that wants it says dep 'mailers.client' (a client reader). Files directly in system/ can’t be components. Two limits follow from the boot order. Each top-level directory owns its key, so a mailers.* component declared in config/components/ is dropped. And a class that a config/components/ file loads, such as a decider passed to Sourced.register, loads before the system/ components are declared, so it can’t dep one. The Sourced reactors in system/ are registered by the same scan that declares those components, so they can (see Registering reactors).

Installable builds a new instance on every read. To make a module, or a class used through its class methods, a component, have its file declare it with __install_component, whose value is the module itself:

# system/pricing/rules.rb: the pricing.rules component
module Pricing::Rules
  def self.__install_component(c) = c.declare(Module).config { self }

  def self.discount(total) = total > 100 ? 10 : 0
end

# elsewhere
dep 'pricing.rules' # a `rules` reader, which returns Pricing::Rules

For a class, declare it as a Class: c.declare(Class).config { self }. dep injects through the constructor, so class methods can’t use it: a component that needs other components is an Installable class.

Running the app in development

bin/dev runs bundle exec falcon host and reloads the app whenever a Ruby file in system/ or web/ changes, or boot.rb, config.ru or falcon.rb does. Browsers reconnect by themselves.

bin/dev                  # http://localhost:9292
PORT=9300 bin/dev        # another port
COUNT=2 bin/dev          # two worker processes

Reloading needs a file watcher: watchexec (brew install watchexec) or fswatch. Without one, the server still runs, without reloading. In production, run bundle exec falcon host instead.

Quick start

A Sidereal app has three main parts: commands (typed data), command handlers (state changes), and pages (reactive UI).

require 'sidereal'

# 1. Define command messages
AddTodo = Sidereal::Message.define('todos.add') do
  attribute :title, Sidereal::Types::String.present
end

# 2. Define a page
class TodoPage < Sidereal::Page
  path '/'

  # React to events by pushing HTML updates via SSE
  on AddTodo do |evt|
    browser.patch_elements load(params)
  end

  def self.load(_params, _ctx)
    new(todos: TODOS.values)
  end

  def initialize(todos: [])
    @todos = todos
  end

  def view_template
    div do
      # An Ajax form to dispatch a command to the backend 
      command AddTodo do |f|
        f.text_field :title, placeholder: 'What needs to be done?'
        button(type: :submit) { 'Add' }
      end

      ul do
        @todos.each { |t| li { t.title } }
      end
    end
  end
end

# 3. Wire it up in an App
TODOS = {}

class TodoApp < Sidereal::App
  session secret: ENV.fetch('SESSION_SECRET')

  # Expose AddTodo to the browser's POST /commands
  handle AddTodo

  # Register the async worker handler
  command AddTodo do |cmd|
    TODOS[cmd.id] = cmd.payload
  end

  page TodoPage
end

Commands

Commands are typed, immutable data objects defined with Message.define. Each message has an auto-generated UUID, a type string, timestamps, metadata, and a typed payload.

AddTodo = Sidereal::Message.define('todos.add') do
  attribute :todo_id, Sidereal::Types::AutoUUID
  attribute :title, Sidereal::Types::String.present
end

RemoveTodo = Sidereal::Message.define('todos.remove') do
  attribute :todo_id, Sidereal::Types::UUID::V4
end

Notify = Sidereal::Message.define('todos.notify') do
  attribute :message, String
end

Commands use dot-separated type strings (e.g. 'todos.add') for registry lookup and serialization. Payload attributes are validated using Plumb types.

cmd = AddTodo.new(payload: { title: 'Buy milk' })
cmd.id              # => "a1b2c3d4-..."
cmd.type            # => "todos.add"
cmd.payload.title   # => "Buy milk"
cmd.created_at      # => 2026-03-26 10:00:00 +0000

Correlation chain

Commands maintain a causation/correlation chain for traceability:

event = source_cmd.correlate(SomeEvent.new(payload: { ... }))
event.causation_id    # => source_cmd.id
event.correlation_id  # => source_cmd.correlation_id

App

Sidereal::App is a web router with that implements a full reactive framework: command handlers, pages, layouts, and SSE streaming. It automatically sets up POST /commands and GET /_updates/:token endpoints, the second for pages’ live updates (see Per-page channels).

class ChatApp < Sidereal::App
  session secret: ENV.fetch('SESSION_SECRET')
  layout ChatLayout

  # SendMessage is submitted from the browser
  # Use .handle to white-list this command in the HTTP endpoint
  handle SendMessage

  # Incoming command is dispatched to a background fiber,
  # and handled by this block
  # The block can optional #dispatch a new command in a workflow
  command SendMessage do |cmd|
    MessageLog.append(cmd)
    dispatch Notify, message: "#{cmd.payload.author}: #{cmd.payload.content}"
  end

  # Notify is dispatched internally and never exposed to the web
  command Notify do |cmd|
    # no-op, but events from this command will still be published
  end

  # Mount a page to be served on ChatPage.path
  page ChatPage
end

Commands

Commands are split into two registrations:

  • command registers an async handler with the app’s Commander. Worker fibers pick the command off the store and run this block. Commands registered only via command are internal — they can be produced by other handlers, automations, or sagas, but cannot be submitted from the browser.
  • handle exposes a command to POST /commands (see Custom command handlers). Any type that isn’t handle-registered returns 404 on POST.

Inside a command block, use dispatch to produce events or enqueue follow-up commands.

# Internal command — dispatched from other handlers, never from the browser
SendEmail = Sidereal::Message.define('mail.send') { attribute :to, String }

command SendEmail do |cmd|
  Mailer.deliver(cmd.payload.to)
end

# Web-facing command — exposed via `handle`, processed async via `command`
handle AddTodo

command AddTodo do |cmd|
  TODOS[cmd.payload.todo_id] = cmd.payload

  # Dispatching a registered command type enqueues it for processing
  dispatch SendEmail, to: 'user@example.com'

  # Dispatching any other message type produces a transient event
  dispatch Notify, message: "Added: #{cmd.payload.title}"
end

Broadcast

Use broadcast inside a command handler to publish a message immediately to the SSE stream, without waiting for the command to finish processing. Useful for progress indicators.

command AskLLM do |cmd|
  broadcast Working  # immediately tells the UI "thinking..."

  response = llm.ask(cmd.payload.content)
  dispatch SendMessage, author: 'Bot', content: response.content
end

The message is correlated to the command being handled, so it goes out on that command’s channel and reaches the pages reacting to its chain. It isn’t stored, and it’s published before anything the handler dispatches, which is what a progress indicator needs. Don’t use it for anything that has to agree with what’s stored.

broadcast goes through the sidereal.broadcaster component, a Sidereal::Broadcaster. Other classes can inject the broadcaster with dep and pass the message they’re handling as from:. For example, a Sourced decider:

class Orders < Sourced::Decider
  dep 'sidereal.broadcaster'

  command PlaceOrder => OrderPlaced do |state, cmd|
    broadcaster.broadcast(Working.new, from: cmd)
    event OrderPlaced, order_id: cmd.payload.order_id
  end
end

Without from:, the message is published uncorrelated, on whatever channel its own resolver gives.

Command helpers

Define helper methods available inside command handlers:

class ChatApp < Sidereal::App
  command_helpers do
    private def chat
      @chat ||= RubyLLM.chat
    end
  end

  command AskLLM do |cmd|
    response = chat.ask(cmd.payload.content)
    dispatch SendMessage, author: 'Bot', content: response.content
  end
end

HTTP command handlers

handle declares which commands the browser is allowed to submit to POST /commands. Types not registered with handle return 404.

handle accepts one or more command classes. Called without a block, it installs the default handler: validate the command, append it to the async store, and return 200. Worker fibers then pick it up and run the matching command block.

class TodoApp < Sidereal::App
  # Expose multiple commands at once with the default handler
  handle AddTodo, RemoveTodo

  command AddTodo do |cmd|  # async worker handler
    TODOS[cmd.payload.todo_id] = cmd.payload
  end
end

Pass a block to handle to process the command synchronously during the HTTP request instead — useful for lightweight mutations, or when you want to stream DOM updates back to the browser immediately:

handle AddTodo do |cmd|
  TODOS[cmd.id] = cmd.payload.to_h
  browser.patch_elements TodoList.new(TODOS.values)
end

A custom handle block replaces the default async-dispatch behaviour. If you still want the async worker to run, call dispatch(cmd) inside the block.

handle AddTodo do |cmd|
  browser.patch_elements %(<p id="notification">Processing...</p>)
  dispatch(cmd) # <= schedule command for background processing
end

handle does not register the command with the async Commander. To have a web-submitted command also processed by workers, pair handle with a command block as shown above.

Inside a handle block you have access to:

  • browser — the SSE stream for pushing DOM updates (see SSE reactions for the full API)
  • dispatch(MessageClass, payload) — correlate and append a follow-up command to the async store
  • reject(errors) — stream field-level validation errors back to the form and stop handling the command
  • store, pubsub, params, session — the usual App instance helpers

Streaming DOM updates

When the browser submits a command via Datastar (the default), the request accepts SSE responses. The handle block can use browser to push HTML patches, signal updates, or JavaScript execution — just like page on reactions:

handle AddTodo do |cmd|
  TODOS[cmd.id] = cmd.payload.to_h

  browser.patch_elements TodoList.new(TODOS.values)
end

browser is an alias for the Datastar dispatcher — each call above produces a one-off SSE event. For multi-step, real-time updates over a single request, use browser.stream { |sse| ... }:

handle GenerateReport do |cmd|
  browser.stream do |sse|
    sse.patch_elements %(<div id="status">Working...</div>)
    expensive_work do |progress|
      sse.patch_signals progress: progress
    end
    sse.patch_elements %(<div id="status">Done</div>)
  end
end

The handle block runs synchronously before any SSE streaming starts, so session[:x] = … writes inside it commit to the session cookie as expected.

Dispatching follow-up commands

Use dispatch to enqueue a command for async processing. The dispatched command is automatically correlated to the source command:

handle AddTodo do |cmd|
  TODOS[cmd.id] = cmd.payload.to_h
  browser.patch_elements TodoList.new(TODOS.values)
  dispatch NotifyUser, text: "Todo added: #{cmd.payload.title}"
end

Custom validation with error streaming

Use reject to stream field-level errors back to the form. This works with the command form component, which generates the matching element IDs. reject halts the handler, so nothing after it runs:

handle PlaceOrder do |cmd|
  reject(email: 'invalid domain') if cmd.payload.email =~ /@invalid\.com/

  ORDERS[cmd.id] = cmd.payload.to_h
  browser.patch_elements OrderConfirmation.new(cmd.payload)
end

Installing extensions

Some things need more than one macro to wire in: a command to expose with handle, a commander to register with commands, maybe a channel resolver or a page. install lets the extensions do that itself, in one line, so the knowledge of what it needs stays with it:

class DataflowApp < Sidereal::App
  install Onboarding, reset: true
end

App.install(installer, ...) calls installer.sidereal_install(app, ...), passing the app class and any extra arguments through. Anything that responds to sidereal_install qualifies; inside it the installer uses the app’s own macros:

module Billing
  def self.sidereal_install(app, webhooks: true)
    app.handle(PlaceOrder)
    app.commands(Billing::Commander)
    app.channel_name(OrderPlaced) { |evt| "orders.#{evt.payload.order_id}" }
    app.page(WebhookLogPage) if webhooks
  end
end

class ShopApp < Sidereal::App
  install Billing, webhooks: false
end

install raises ArgumentError for an object without sidereal_install, and returns the app so it chains like the other macros.

Components and pages respond to sidereal_install too: installing one registers the assets and command handlers it declares. component is another name for install, which reads better for them: component UI::Chat (see Components’ and pages’ own assets and Components’ and pages’ own command handlers).

Components’ and pages’ own command handlers

A component, page or layout can declare the commands it takes from the browser, and handle them, next to the forms that send them. handle works as App.handle does, but only records the handler: the app that installs the component exposes the commands and runs the blocks, in its own requests, with its helpers (browser, session, params, reject, dispatch):

class UI::Chat < Sidereal::Components::BaseComponent
  # Runs during the request, in the app that installs UI::Chat
  handle SendMessage do |cmd|
    reject(text: 'is too long') if cmd.payload.text.size > 500

    # Hand the work on to a background handler
    dispatch StoreMessage, text: cmd.payload.text, author: session[:username]
    browser.patch_elements UI::Chat::Composer.new
  end

  # Without a block, the default: dispatch the command itself
  handle ClearChat

  def view_template
    command SendMessage do |f|
      # ...
    end
  end
end

class ChatApp < Sidereal::App
  component UI::Chat    # exposes SendMessage and ClearChat, with their handlers

  command StoreMessage do |cmd|
    # background work
  end
end

As with assets, page installs a page’s handlers, and layout a layout’s. UI::Chat.handlers lists what a component declares, after its superclass’s. A command handled twice, as by two components with the same superclass, is handled by the last one installed.

Testing them

Sidereal::Testing::Harness handles a component’s commands as an app would, in a small app of its own with the component installed, and returns what each handler did. It’s plain Ruby, for any test framework:

require 'sidereal/testing'

harness = Sidereal::Testing::Harness.new(UI::Chat)
result = harness.handle(SendMessage, { text: 'hi' }, session: { username: 'ada' })

result.status      # => 200
result.command     # => the SendMessage the handler got
result.dispatched  # => [#<StoreMessage text="hi" author="ada">]
result.elements    # => the HTML the handler patched into the page
result.signals     # => the signals it patched, merged
result.events      # => every Datastar event it sent, with its data
result.session     # => the session, as the handler left it

result = harness.handle(SendMessage, { text: 'x' * 501 })
result.rejected?   # => true
result.errors      # => { text: 'is too long' }
  • As a browser sends it: each command is posted to the app’s /commands, as a form, so it’s validated and decoded by the app, and its before_command and reject work as in the app. Pass form: instead of a payload to test what the app makes of a form’s strings: harness.handle(SendMessage, form: { 'text' => '' }).
  • Nothing runs in the background: what the handler dispatches is recorded in result.dispatched, correlated to result.command, instead of appended to the store.
  • Like your app: pass app: ChatApp for a test app that subclasses it, with its sessions, before_command and helpers. ChatApp itself isn’t changed. The session passed to handle is merged into the one the app’s cookie session loads.

Apps and gems can both use it, from RSpec, Minitest or plain Ruby: require 'sidereal/testing' in the test helper. A gem can test its components without an app of its own, since the harness installs them in a plain Sidereal::App subclass by default. Build the components first, with Sidereal.config.build!, when the handler uses any, such as pubsub or one injected with dep, or when the app passed as app: reads its session secret from them, as a generated app’s -> { Sidereal.secrets.derive('session') } does. The harness itself doesn’t need them. sid new --rspec sets this up, with a spec for the welcome page’s command in spec/web/ui/welcome_page_spec.rb.

Rendering components

The component helper renders any object responding to #call(context:), passing the app instance as context. This is how Sidereal pages and layouts are rendered under the hood, and it’s available inside any route block defined on an App subclass.

class MyApp < Sidereal::App
  get '/dashboard' do
    component DashboardPage.new(current_user)
  end

  get '/error' do
    component ErrorPage.new, status: 422
  end
end

Pages

Pages are reactive Phlex components that re-render parts of the UI in response to events via SSE.

class TodoPage < Sidereal::Page
  path '/'

  # React to events -- re-render components via SSE
  on AddTodo do |evt|
    browser.patch_elements TodoList.new(TODOS.values)
  end

  on RemoveTodo do |evt|
    browser.patch_elements TodoList.new(TODOS.values)
  end

  on Notify do |evt|
    browser.patch_elements ActivityItem.new(evt), mode: 'append', selector: '#feed'
  end

  # Load is called on initial page render and on SSE reconnect
  def self.load(_params, _ctx)
    new(todos: TODOS.values)
  end

  def initialize(todos: [])
    @todos = todos
  end

  def view_template
    div do
      render TodoList.new(@todos)

      aside do
        h2 { 'Activity' }
        div(id: 'feed')
      end
    end
  end
end

Page params

load receives the page’s path and query params decoded through its query_schema. Declare it with the types you want. It’s compiled through Plumb::Codec::Forms, so values arrive as Strings and are cast to their declared types:

class ExpensesPage < Sidereal::Page
  path '/accounts/:id/expenses'
  query_schema(
    id: Types::UUID::V4,     # the :id path segment
    from?: Types::Date,      # optional ?from=2026-11-01
    archived?: Types::Boolean
  )

  def self.load(params, _ctx)
    # params => { id: "0b6f…", from: #<Date 2026-11-01> }
    new(Expenses.for(params[:id], from: params[:from]))
  end
end
  • Only declared keys reach load. A query param the schema doesn’t name is dropped, so declare every one the page reads.
  • A subclass inherits its superclass’s schema, and its own query_schema merges into it, so a base page can declare shared params:

    class ReportPage < Sidereal::Page
      query_schema(from?: Types::Date, to?: Types::Date)
    end
    
    class ExpensesPage < ReportPage
      path '/accounts/:id/expenses'
      query_schema(id: Types::UUID::V4) # => Hash[from?: Date, to?: Date, id: UUID::V4]
    end
    
  • Each path segment is in the schema as a String unless you declare it. A page with no query_schema still gets one from its segments (Types::Hash[id: String]), or an empty one.
  • If the params fail the schema, the GET responds 400 Bad Request and load isn’t called. A blank ?from= fails a Types::Date, so make a field .nullable if a form can submit it empty.
  • The same schema decodes the params signal on every SSE connect and reaction, so params in an on block is typed too. If signal params fail it, the page is neither rendered nor subscribed.
  • A custom type needs an encoder registered on Plumb::Codec::Forms before the page is defined (see Serialization).

SSE reactions

Inside an on block, you have access to:

  • browser – the SSE stream for pushing updates
  • load(params) – re-instantiate the page with current data
  • params – the current page params from Datastar signals, decoded through the page’s query_schema
on AddTodo do |evt|
  # Replace an element's content with a re-rendered component
  browser.patch_elements load(params)

  # Or target a specific element
  browser.patch_elements TodoList.new(TODOS.values)

  # Append to a container
  browser.patch_elements ActivityItem.new(evt), mode: 'append', selector: '#feed'

  # Patch signal values
  browser.patch_signals progress: 99

  # Execute JavaScript on the client
  browser.execute_script %(scrollToBottom('messages'))
end

See more about this here.

A page handles its messages one at a time, in arrival order. Every on block runs for each message it matches, because it may read the payload. A reload, from a block-less on or a command form, reads only current state, so reloads are coalesced. If a reload is still waiting to run when a newer one arrives, only the newer one renders. A reload with nothing queued ahead of it renders straight away. A burst arriving while a render is in flight produces one more render once that render finishes, however long the burst. This covers, for example, a Sourced projector replaying its history. It publishes a Projected signal for every batch, but an open page renders at most twice.

Causal reactivity

A page that renders a form for a command almost always wants to re-render when that command comes back over pubsub. You don’t have to write that reaction: every command form rendered inside a page, at any depth of its component tree, registers on CommandClass on that page. on without a block means “reload the page”, so the two pages below react the same way:

class TodoPage < Sidereal::Page
  on AddTodo   # reload when AddTodo comes back

  def view_template
    command AddTodo do |f|
      f.text_field :title
    end
  end
end

class TodoPage < Sidereal::Page
  def view_template
    command AddTodo do |f|   # registers `on AddTodo` on TodoPage as it renders
      f.text_field :title
    end
  end
end

This is the default. A page that would rather say exactly what it reacts to opts out with disable_causal_reactivity!, which stops its forms from registering anything, and keeps every on it declares, block or not:

class AuditPage < Sidereal::Page
  disable_causal_reactivity!
  on AuditEntryAdded          # only this, however many forms the page renders
end

Subclasses inherit the setting.

Reacting to correlated messages

The match is on the message’s own type or on its correlation_type. Every message carries the type of the message at the root of its causal chain (Sourced::Message#correlation_type, recorded in metadata[:correlation_type] by #correlate), so a page registered for AddTodo also reloads on the events a handler produced from it, the commands those events triggered, and so on. This is what makes the same page work over a backend that publishes the command itself and one, like Sourced, that publishes only the resulting events. Naming an event or a projector signal works too: on GamesProjector::Projected reloads on every signal that projector publishes, whichever command’s chain it belongs to.

CleanShot 2026-09-25 at 15 29 51@2x

Reacting to all events in a namespace

on also takes anything that answers sidereal_events with a list of message classes, and registers each of them. A Sidereal::Commander answers with the commands it handles, which the dispatcher publishes along with everything dispatched from them, so on MyCommander covers all of it. With the Sourced integration loaded, a decider answers with the events it evolves, its own and any foreign ones that change its state, and a projector with its Projected signal. So an event-sourced page names the reactor it follows:

class DonationPage < Sidereal::Page
  on Donation          # every event that changes a donation
end

class HomePage < Sidereal::Page
  on GamesProjector    # the lobby's read model committed
end

A handler written with a block always wins for messages of exactly its class. The page checks reactions first and only falls back to the reload when the message’s class has no handler of its own, so on TodoAdded do |evt| ... end next to a rendered command AddTodo runs the block for TodoAdded and reloads for anything else in that chain. At most one of the two runs per message.

Per-page channels

Live updates travel over PubSub channels, and each side names its own end. When the dispatcher publishes a message, the app’s channel_name resolvers pick the channel it goes to. A page names the channels it listens on in #channel_names, and its browser receives every message published to any of them.

A page receives live updates over one SSE stream, subscribed to the PubSub channels the page names in #channel_names. The default is ['system'], where messages go that no channel resolver (below) routes elsewhere. Override it to scope the stream to a narrower topic: “only events for this donation”, say, or “only events for this chat room”.

class DonationPage < Sidereal::Page
  path '/:donation_id'

  def self.load(params, _ctx) = new(donation_id: params[:donation_id])

  def initialize(donation_id:)
    @donation_id = donation_id
  end

  # Each donation page only receives events on its own channel
  def channel_names = "donations.#{channel_segment(@donation_id)}"
end

Interpolate values through channel_segment. It returns the value as a String, and raises unless it’s exactly one plain segment: not blank, no ., not * or >. Without it, a donation id taken from the URL could be >, and the page would sign itself a wildcard subscription to every channel under donations.. Wildcards go in the pattern itself ('campaigns.>'), never in a value.

#channel_names runs on the page being rendered, so it can use anything the page was built with: loaded records, the current user. Return one pattern as a String, or an Array of several to receive more than one stream on the same page, such as the entity’s updates and the user’s notifications. A message whose channel matches more than one of them still arrives once. Return [] or nil for a page that takes no live updates, like a historic snapshot: the layout then opens no stream for it.

class OrderPage < Sidereal::Page
  path '/orders/:id'

  def self.load(params, ctx)
    new(order: Order.find(params[:id]), user: ctx.session[:user_id])
  end

  # A route of the app's own renders frozen snapshots, passing a step
  def initialize(order:, user:, step: nil)
    @order = order
    @user = user
    @step = step
  end

  def channel_names
    return [] if @step  # a frozen snapshot

    patterns = ["accounts.#{channel_segment(@order.account_id)}.orders.#{channel_segment(@order.id)}"]
    patterns << "users.#{channel_segment(@user)}" if @user  # logged in
    patterns
  end
end

The browser never names a channel. The layout signs the page’s key, the params it was rendered with and its channels into a token, with the sidereal.signer component, and points the browser at GET /_updates/<token>. The stream verifies the token, subscribes, and only then loads the page again from those params to render it on connect, so nothing published in between is lost. A forged or altered token gets nothing, so a client can only subscribe to what a page it was shown named. By default, the signer is a Sidereal::Signer, with a key derived from the app’s master secret (see Sidereal’s components).

The token is bound to the session the page rendered in, when the app has sessions: a token from another session is refused. A stream request with no session at all, as from a browser that refuses cookies, still gets the page’s channels, but not the session’s own channel, since it can’t prove it’s in that session. Rack only gives a session its id once something is stored in it, and changes it only when the session is renewed, so a login that writes to the session changes it. To cut off open tabs at logout, renew the session rather than only clearing it (request.env['rack.session.options'][:renew] = true). A refused token, like one signed with a secret that has since changed, gets the page reloaded, which renders a fresh one.

Every page stream in a session also subscribes to that session’s own channel, sidereal.sessions.<session digest>, on top of the page’s channels. Sidereal publishes there what’s meant for one user only: the notifications for a command their browser sent. A command posted to /commands carries its session’s digest in its metadata, under :session_digest (Sidereal::Page::SESSION_DIGEST_KEY), and so does every message correlated from it, however far down the chain. Sidereal::Page.session_channel_for(digest) turns it into the channel. The digest names the session without giving its id away, so it’s safe to store with the messages, and an app can use it to tell which session a message came from, for example in per-session reports. So when a command fails, the toast shows in that user’s open pages, and nobody else’s. To give every visitor a session channel, the layout stores a key in a session that has no id yet, which sets the session cookie. Without session middleware there’s no session channel, and notifications go to the command’s own channel instead. A page with no channels of its own opens no stream, so it shows no notifications either.

Channels are fixed when the page first renders: reactions that re-render it, and the browser’s reconnects, keep the same stream until the page reloads. A page streams only when its class is the one an app mounted with page, wherever it renders, so the stream can find it again.

Routing messages to channels

For events to actually reach a page’s channels, declare how the App derives a channel name from each message. The channel_name macro registers a resolver on the process-global Sidereal.channels registry. Pass message classes positionally to scope the resolver, or no arguments to register a catch-all:

class DonationsApp < Sidereal::App
  # Catch-all: every message goes through this block
  channel_name do |msg|
    "donations.#{msg.payload.donation_id}"
  end

  # Or scope to specific message classes:
  channel_name SelectAmount, EnterDonorDetails do |msg|
    "donations.#{msg.payload.donation_id}"
  end

  handle SelectAmount
end

The block runs for every message the dispatcher publishes — both the incoming command and the events it emits. Resolution is O(1) per message: typed registrations win first, then the catch-all, then a fallback to the literal 'system' channel (so an app that registers nothing still publishes successfully). When a command fails or is retried, the Sidereal::System::NotifyRetry or NotifyFailure it causes goes to the session channel of the browser that sent it. A command no browser sent, like a scheduled one, has none, and its notification goes to the channel the resolvers give the command, so every page subscribed to that channel shows the toast.

Channel routing also works outside the App class — call Sidereal.channels.channel_name(...) from anywhere (e.g. a dedicated routes file) for apps where the registrations grow large enough to warrant their own home.

The registry locks itself once boot is over: Sidereal::Falcon::Environment::Service calls Sidereal.channels.lock! after class-loading and pubsub startup, before workers start consuming. Subsequent channel_name(...) calls raise Sidereal::Channels::LockedError — register routes during boot only.

Channel name syntax

Channel names are dot-separated tokens (e.g. campaigns.abc-123.donations.xyz-999). Subscribers can use two NATS-style wildcards to receive events across multiple concrete channels:

Pattern Matches
campaigns.abc-123 exactly that channel
campaigns.* campaigns.abc-123, campaigns.xyz-999 — one non-empty segment, nothing deeper
campaigns.*.donations.* campaigns.abc.donations.xyz only — * always matches exactly one segment
campaigns.> any channel starting with campaigns. (one or more segments)
> everything published

* may appear anywhere; > must be the trailing token. Published channel names must be concrete — wildcards in publish are rejected. Empty segments (campaigns..x) are rejected on both sides.

This lets pages scope their SSE stream to just what they need. Use an exact channel for a single-entity detail page, and a wildcard for a list or dashboard that should refresh on any change under a prefix:

class DonationPage < Sidereal::Page
  # Only events for this specific donation
  def channel_names = ["campaigns.#{channel_segment(@campaign_id)}.donations.#{channel_segment(@donation_id)}"]
end

class CampaignsListPage < Sidereal::Page
  # Every campaign event and every donation event under every campaign
  def channel_names = 'campaigns.>'
end

Pair this with a hierarchical channel_name block on the App to get routing “for free” from the channel name alone:

class DonationsApp < Sidereal::App
  channel_name do |msg|
    if msg.type.start_with?('donations.')
      "campaigns.#{msg.payload.campaign_id}.donations.#{msg.payload.donation_id}"
    else
      "campaigns.#{msg.payload.campaign_id}"
    end
  end
end

Sub-components

Define inline components as separated classes (or nested classes) for partial re-renders:

class TodoPage < Sidereal::Page
  class TodoList < Sidereal::Components::BaseComponent
    def initialize(todos)
      @todos = todos
    end

    def view_template
      div(id: 'todos') do
        @todos.each do |todo|
          li { todo.title }
        end
      end
    end
  end
end

Command forms

The command helper renders a form wired to POST /commands via Datastar. It handles hidden fields, AJAX submission, and server-side validation error display automatically.

def view_template
  command AddTodo, class: 'add-form' do |f|
    f.text_field :title, placeholder: 'What needs to be done?'
    button(type: :submit) { 'Add' }
  end

  # Hidden payload fields (not shown to the user)
  command RemoveTodo do |f|
    f.payload_fields(todo_id: todo.todo_id)
    button(type: :submit) { 'Remove' }
  end
end

Field helpers: text_field, text_area, number_field, date_field, check_box, and payload_fields for values the user doesn’t edit. Pass a message instance instead of a class to prefill the form. Values are converted to and from the payload’s declared types on the way in and out — see Serialization.

Forms for a state machine

A page for a Sourced machine should only offer the commands the machine accepts in its current state. Declare the machine with with_machine, and every command form rendered inside the page or component, at any depth, is checked against it. A form for one of the machine’s commands (its handled_commands) that it doesn’t accept in its current state (its available_commands) raises Sidereal::CommandNotAvailableError while rendering, instead of showing a form that would be refused when submitted. Forms for commands the machine doesn’t handle at all, such as another machine’s or a plain app command, render as usual:

class BookingPage < Sidereal::Page
  path '/bookings/:booking_id'

  # Evaluated in the instance, once per render
  with_machine { @booking }

  # A HolidayBooking machine, evolved from the booking's history
  def self.load(params, _ctx) = new(Bookings.find(params[:booking_id]))

  def initialize(booking) = @booking = booking

  def view_template
    if @booking.machine.in?(HolidayBooking::ItineraryReady)
      command HolidayBooking::Pay do |f| # accepted here: renders
        f.text_field :card_number
      end
    end

    command HolidayBooking::StartBooking # never accepted once started: raises
  end
end
Sidereal::CommandNotAvailableError: HolidayBooking::StartBooking isn't available
at {HolidayBooking::BookingStarted => {...}}. Available: HolidayBooking::CancelBooking, ...

with_machine is inherited by subclasses. To check only part of a template, pass the machine to the instance helper and wrap that part in a block:

def view_template
  render_header

  with_machine(@booking) do
    render_forms
  end
end

Blocks can nest, and the innermost machine is the one checked. Forms rendered outside any with_machine aren’t checked. The machine can be any object that responds to to_machine_config with a configuration, which answers handled_commands and available_commands with sets of command classes: a Sourced::Machine instance, or a read-only Sourced::Machine::Configuration built from stored paths with MyMachine.configuration(paths), which lets a listing check each record’s forms without loading its history.

The current machine is kept in Thread.current, which Ruby scopes to the current fiber, so concurrent requests under Falcon never see each other’s machine. It’s restored when the block or render ends, even if it raises. A child fiber started during a render, such as an Async task or an external Enumerator, doesn’t inherit it, so forms rendered there aren’t checked.

Layout

Define a layout by subclassing Sidereal::Components::Layout. The base class overrides head and body to automatically inject the necessary Datastar wiring:

  • head — appends the app’s assets and the Datastar JS script tag after your content: the app’s stylesheets, the app’s scripts, then Datastar, so the app’s custom elements and event listeners are in place before Datastar applies itself to the page. A script that imports Datastar must use the same URL as the layout’s tag, or the browser loads a second copy.
  • body — adds page signals (page_key, params) to the data attribute and appends the SSE init div at the end, pointed at the page’s signed /_updates stream. A page with no channels gets no init div.
class AppLayout < Sidereal::Components::Layout
  def view_template
    doctype

    html do
      head do
        meta(name: 'viewport', content: 'width=device-width, initial-scale=1.0')
        title { 'My App' }
      end
      body do
        div(class: 'page') do
          render page   # renders the current page component
        end
      end
    end
  end
end

You can pass additional data attributes and signals to body. Extra signals are merged with the default page signals:

body(data: { class: 'app', signals: { theme: 'dark' } }) do
  render page
end

Set the layout in your App:

class MyApp < Sidereal::App
  layout AppLayout
  # ...
end

A BasicLayout with reset CSS and form styling is provided by default if no layout is specified.

Assets

An app serves its own stylesheets, scripts and other files, such as images and fonts. Register them on App.assets, a Sidereal::Assets, as the app loads:

class MyApp < Sidereal::App
  assets.stylesheet 'app.css', path: Sidereal.root.join('assets/app.css')
  assets.javascript 'app.js', js: "console.log('hello')"
  assets.file 'logo.svg', path: Sidereal.root.join('assets/logo.svg')

  # Or every file in a directory, named by its path in it: .css files are
  # stylesheets, .js files scripts, and the rest files
  assets.directory Sidereal.root.join('assets')
end
  • Names are paths with the file’s extension, unique in the app: 'app.css', 'acme/charts.css', 'img/logo.svg'. A stylesheet’s ends in .css and a script’s in .js. bundle.css is taken. Registering a name again raises Sidereal::Assets::DuplicateAssetError, unless it’s the same asset again, which is left as it is.
  • Content comes from a file (path:), checked when it’s registered, or inline (css:, js:). Sidereal.root is a Pathname of the app’s root directory to build paths from: the working directory, unless the app sets it (Sidereal.root = __dir__), as a generated app’s boot.rb does.
  • Order is registration order. An app reads its superclass’s assets, which come first.
  • Extensions register theirs in sidereal_install, with app.assets (see Installing extensions):

    module AcmeCharts
      def self.sidereal_install(app)
        app.assets.stylesheet 'acme/charts.css', path: File.expand_path('charts.css', __dir__)
        app.assets.javascript 'acme/charts.js', path: File.expand_path('charts.js', __dir__)
      end
    end
    
  • Locking: registering raises once the assets are read or served, so register them all as the app loads.

Components’ and pages’ own assets

A component or a page can declare the stylesheets, scripts and files it needs, next to its code. A relative path: is relative to the file that calls stylesheet, javascript or file, as with require_relative. Called from a helper module, that’s the helper’s file, so pass a helper an absolute path, such as File.expand_path('chat.css', __dir__):

class UI::Chat < Sidereal::Components::BaseComponent
  stylesheet 'chat.css', path: 'chat.css'
  javascript 'chat.js', path: 'chat.js'
  file 'chat/avatar.svg', path: 'avatar.svg'

  # Inline, named after the class: ui/chat-1.css, ui/chat-1.js, ui/chat-2.css...
  css <<~CSS
    .chat { border: 1px solid; }
  CSS

  js <<~JS
    console.log('chat')
  JS
end

Declaring only records them, in UI::Chat.assets, after the superclass’s. They’re registered with an app’s assets when the app installs the component with component, for a page when the app mounts it, and for a layout when the app sets it, or gives it to a page:

class ChatApp < Sidereal::App
  component UI::Chat                   # registers UI::Chat.assets with ChatApp.assets
  layout UI::Layout                    # sets the layout, and registers its assets
  page ChatPage                        # mounts ChatPage, and registers its assets
  page AdminPage, layout: AdminLayout  # and AdminLayout's
end

Components that share a superclass share its assets, which are registered once, however many of them the app installs. Installing a component again, even after the app has served a request, changes nothing. A component without a class name, such as Class.new(...), can’t declare inline assets, since they’re named after it.

Layouts link stylesheets in the order they’re registered, so the order of these lines in the app is the order of the cascade: a layout’s reset after component UI::Chat overrides Chat’s rules. Set the layout first for its styles to come first.

URLs and caching

Every URL carries a digest of what it serves, under /_assets, so browsers and CDNs can keep it for good: it’s served with Cache-Control: public, max-age=31536000, immutable and an ETag, and answers If-None-Match with a 304.

  • Each asset has its own URL, /_assets/img/logo-<digest>.svg, whose digest changes only when that asset does.
  • The stylesheets are also combined into one bundle, /_assets/bundle-<digest>.css, whose digest is computed from the digests of the stylesheets in it. Scripts and files never combine: each script is its own ES module, and joining modules would make them share one scope.

A URL with a digest that isn’t the current one, as from a page rendered before a deploy, is redirected to the current URL with a 302, uncached. Asset requests skip the app’s session and its before hook: assets are public, and their responses set no cookie.

The sidereal.assets.mode component says which URLs layouts link:

Mode Layouts link Digests
:single (default) each stylesheet and script recomputed when a file changes, so an edit shows without a restart. With sidereal.assets.watch off, computed once per process
:combined the stylesheet bundle, and each script computed once per process for the links

Both serve every URL, so either works in any environment. Serving always checks a file against its digest, so a file changed under a running process that doesn’t watch files is redirected to its new URL, never served under its old one.

In :single mode, layouts notice a changed file by checking each asset’s modification time and size as they render, and re-reading the ones that changed. sidereal.assets.watch turns that off, so production can link each asset without checking files on every page. It’s on by default, except when Sidereal.environment is production. Over HTTP/2, linking each asset costs little: the browser fetches them in parallel over one connection, and a change to one asset only changes that asset’s URL. Live reload needs it on. A generated app sets it in config/components/assets.rb, bundling in production:

Sidereal.config.config!('sidereal.assets.mode') do
  Sidereal.environment == 'production' ? :combined : :single
end

The layout links stylesheets and scripts. Link anything else with asset_url, in components (and in the app’s handlers):

img(src: asset_url('img/logo.svg'))

URLs include the app’s mount path, and start with sidereal.assets.host when it’s set, ex. for a CDN that fetches them from the app:

Sidereal.config.config!('sidereal.assets.host') { 'https://cdn.example.com' }

Stylesheets and scripts

  • url() references: in a stylesheet loaded from a file, a relative url(...) to a registered file is rewritten to that file’s digested URL. So the stylesheet’s digest changes when an image it references does. References to unregistered files, data: URIs, other hosts and root-relative paths are left as they are.
  • Scripts load as ES modules (<script type="module">), each its own, in either mode, so their top-level names never clash. They load before Datastar, so custom elements and event listeners are in place before it applies itself to the page.

Memory

Only the stylesheet bundle is held in memory, replaced when its digest changes. Stylesheets and scripts are read from their files when served, and other files are streamed from disk. Digests are computed by streaming each file.

Live reload

With sidereal.assets.live_reload on, open pages update when an asset’s file changes, without a reload or a restart. Generated apps turn it on in development:

Sidereal.config.config!('sidereal.assets.live_reload') do
  Sidereal.environment == 'development'
end

The sidereal.assets.watcher component checks the registered files every 0.3 seconds, on the leader process only, and publishes Sidereal::Assets::Changed on the sidereal.assets channel when any digest changes. Each check stats the files and reads only those whose modification time or size changed. Page streams subscribe to that channel, and each one brings its page up to date:

Changed The page
A stylesheet Swaps in a new <link>, and removes the old one once the new one has loaded
An image, font or other file Renders again, so asset_url links the new URL
A script Reloads, since a module that ran can’t be swapped out

An image a stylesheet references changes that stylesheet’s digest too, so it’s swapped as well. A tab that reconnects also catches up, since a stream syncs the asset tags when it connects.

It works in :single mode, which links each asset with its current digest. A Page that names no channels gets a stream too, which follows only its assets, not its session’s messages. A route that renders HTML without a mounted Page has no stream, so it doesn’t update. Assets registered inline only change when the app restarts.

The app’s command line: bin/sid

bin/sid in an app is sid with that app loaded. It runs with the app’s gems and adds commands that only make sense inside an app. It finds the app from any directory, so ../bin/sid works from a subdirectory too:

bin/sid --help
Command What it does
bin/sid commands list Lists the app’s commands
bin/sid commands info NAME Shows a command’s payload attributes
bin/sid commands dispatch NAME --attribute value ... Sends a command to the app
bin/sid db install Sets up a SQLite database for the app
bin/sid db migrations add NAME Creates a timestamped migration
bin/sid db migrations run Applies every migration that hasn’t run
bin/sid db migrations rollback Rolls back the migration that ran last
bin/sid sourced install Sets up the Sourced integration, and a database for it
bin/sid sourced migration Writes the migration for Sourced’s tables
bin/sid sourced topology Shows how the app’s commands, events and read models connect
bin/sid sourced groups list Lists the consumer groups, with status, partitions and position
bin/sid sourced groups stop NAME Stops a consumer group, so its reactor claims no more work
bin/sid sourced groups start NAME Starts a stopped or failed consumer group again
bin/sid sourced groups reset NAME Resets a consumer group, so its reactor reads everything again
bin/sid sourced messages list Lists the most recent messages in the store, with --tail to follow and --types / --partition to filter
bin/sid sourced messages correlation ID Draws the causal tree of one workflow
bin/sid store stats Counts the messages in each queue of the file system store
bin/sid store list Lists the store’s messages, with why each dead letter failed. --queue and --type filter
bin/sid store show ID Shows a message’s payload and metadata, and its error and backtrace if it failed
bin/sid store requeue ID... Moves dead letters back to ready, with a full set of retries. --all for every one
bin/sid store delete ID... Deletes ready, scheduled or dead messages for good. --all for every dead letter
bin/sid system graph Prints the app’s components and how they depend on each other
bin/sid system tree Prints the app’s components as a tree, and who implemented each
bin/sid console Starts an IRB session with the app loaded
bin/sid info Prints the Sidereal and Ruby versions the app runs with
bin/sid secret Prints a new random secret, for SIDEREAL_SECRET. Also works outside an app, as sid secret

NAME is a command’s class name (Greetings::SayHello) or its type (my_app.greetings.say_hello).

bin/sid loads the app’s boot.rb before it parses the command line, so integrations have registered their commands by the time a name is looked up. Loading declares and implements components but builds none of them, so nothing is connected to yet — a command that needs component values builds them itself. That’s why --help opens no database. If boot.rb raises, bin/sid says so and still prints usage, so the command line keeps working on an app that doesn’t.

Extending the command line

An integration adds its own commands from its setup, the same place it wires components (see Custom backends). A command is a Sidereal::CLI::Command — a Samovar command — and can nest sub-commands of its own:

# lib/my_integration.rb
module MyIntegration
  def self.setup(config, **opts)
    require 'my_integration/cli'
    Sidereal::CLI.register 'mine', MyIntegration::CLI::Namespace
    # ... components, skills
  end
end

So a command exists exactly when the app configures the integration that provides it — the same rule as its components and its skills. Keep the file that defines the command classes light: it is loaded while the app loads, and a command should reach for the heavy parts of the integration from its own #call, after Sidereal::CLI.boot_app!.

Step When What it does
Sidereal::CLI.load_app(root) bin/sid, before parsing Changes into the app root and requires boot.rb, so integrations’ setup runs. Then drops the registered commands whose available? is false
Sidereal::CLI.boot_app! in a command’s #call Sidereal.config.build! — component values become readable. Opens connections; starts nothing
Sidereal.config.start_component!(key, Thread.current) in a command that needs a started component Runs that component’s start hook, and its dependencies’

An app can register commands of its own the same way, from boot.rb or a file under config/components/.

A command can also leave itself out of an app whose configuration makes it useless. Define self.available? on it: once boot.rb has loaded, bin/sid drops each registered command for which it returns false. It runs before anything is built, so it should only read the configuration, ex. which implementation a component has. bin/sid store uses it so it only appears while the file system store is still the app’s store, and not after Sourced has replaced it.

Database

The migrations in db/migrations/ are the database’s definition: committed, and the way any checkout builds one, since storage/ is not committed.

bin/sid db install is the one thing that sets a database up, and nothing else does: sid new --db runs it in the new app, bin/sid sourced install runs it because Sourced keeps its messages in the app’s database, and an app that started without one adds it later by running the same command. So there is only ever one db component to know about, wherever it came from.

bin/sid db install gives an app a SQLite database. It adds sequel and sqlite3 to the Gemfile, runs bundle install, creates db/migrations/ and storage/, ignores storage/ in .gitignore, and writes config/components/db.rb:

Sidereal.config.tap do |c|
  c.declare('db.filepath', Sidereal::Types::String, description: 'Where the SQLite database file lives') { 'storage/db.db' }
  c.declare('db', Sequel::Database, description: "The app's database connection")
  c.component!('db', ['db.filepath']) do
    build do |filepath|
      FileUtils.mkdir_p(File.dirname(filepath))
      Sequel.sqlite(filepath)
    end
    teardown(&:disconnect)
  end
end

Two components: where the file lives, and the connection to it. Each process opens its own when it starts and disconnects on shutdown — a SQLite connection can’t cross a fork. Classes reach it with dep :db.

The file it writes is yours to edit, and the generated comments show the two usual changes — pointing db.filepath at the environment, or re-implementing db for another database entirely. db install never overwrites a file that is already there; it reports skip and leaves it alone, so it’s safe to re-run (pass --force to overwrite, --skip-bundle to skip bundle install).

Then migrate:

bin/sid db migrations add create_things   # db/migrations/20260401120000_create_things.rb
bin/sid db migrations run
bin/sid db migrations rollback            # just the one that ran last

Migrations are plain Sequel migrations in db/migrations/, named with a 14-digit UTC timestamp:

Sequel.migration do
  change do
    create_table(:things) do
      primary_key :id
      String :name, null: false
    end
  end
end

run and rollback build the app’s components to get the connection, so they need config/components/db.rb — without it they say to run bin/sid db install. add only writes a file, so it works before anything is configured.

Sourced

bin/sid sourced install sets the Sourced integration up, and sid new --sourced runs it in the new app. It installs a database first — Sourced keeps the app’s commands and events there — then adds the sourced gem and writes config/components/sourced.rb:

require 'sidereal/integrations/sourced'

Sidereal.config.use Sidereal::Integrations::Sourced, db: 'db'

The integration registers the deciders, projectors and machines in system/ (see Registering reactors). The generated comments show what to re-implement: the worker count, the error strategy behind the retry toasts, and sidereal.runner.process to run a Sourced runtime in every worker rather than only the leader.

Finally it writes the migration for Sourced’s own tables into db/migrations/ and applies it, so the app is ready to boot. Sourced’s store expects its tables to be there rather than creating them, so they are a migration’s job like any other table:

bin/sid db migrations run

A checkout of the app gets its database the same way, since db/migrations/ is committed and storage/ isn’t — or by copying a database over, to keep the data. Starting without the tables stops the host with Sourced::Store::NotInstalledError, naming what’s missing.

bin/sid sourced migration writes that file on its own, for an app that needs it again — after changing sourced.store.table_prefix, say. It renders the migration from Sourced’s template through the app’s own store, so a configured prefix is honoured, and it leaves an existing one alone unless you pass --force. sourced install runs it in a separate process, because the gem it needs has only just been bundled.

boot.rb says nothing about Sourced — the require lives in the generated component file too, so installing Sourced later works exactly like generating with it. Its order is load-bearing in two ways: config/components/ loads after Sidereal.config.use_file_system!, because both implement sidereal.store and the last implementation of a key wins (the other way round a Sourced app would append its commands to files while Sourced’s runtime watched its own tables); and LOADER.eager_load comes after the components, so that the require in config/components/sourced.rb has happened before Zeitwerk loads a system/ class that subclasses Sourced::Decider. In between, Sidereal.config.install_dirs! declares the system/ components: after config/components/, whose components they may inject, and before eager_load, whose classes may inject them.

Topology

The topology, groups and messages commands below come from the integration, so they appear only once the app has configured it; sourced install is built in and available before that.

bin/sid sourced topology prints how the app’s commands, events, read models and automations connect, as a tree starting from each command. Part of the output for the sourced_donations example:

$ bin/sid sourced topology
command donations.enter_donor_details  Donation::EnterDonorDetails
└─ event donations.donor_details_entered  Donation::DonorDetailsEntered
   └─ automation reaction(Donation::DonorDetailsEntered)  in donations
      └─ command donations.send_verification_email  Donation::SendVerificationEmail
         └─ event donations.email_sent  Donation::EmailSent

command donations.start_payment  Donation::StartPayment
└─ event donations.payment_started  Donation::PaymentStarted
   └─ automation reaction(Donation::PaymentStarted)  in donations
      └─ command donations.confirm_payment  Donation::ConfirmPayment
         └─ event donations.payment_confirmed  Donation::PaymentConfirmed
            └─ read model campaigns_projector

command campaigns.create_campaign  Campaign::CreateCampaign
└─ event campaigns.campaign_created  Campaign::CampaignCreated
   └─ read model campaigns_projector (see above)

Each command leads to the events it produces, each event to the read models (projectors) and automations (reactions) that consume it, and each automation to the commands it dispatches. Something that appears more than once is expanded the first time and marked (see above) after that. The tree comes from Sourced.topology, which lists the events and commands each handler declares it produces. Sourced refuses to append or dispatch anything undeclared, so the tree shows everything that can happen, except a machine’s data events.

Add --schemas for a line under each command and event with its payload’s JSON Schema, so one call shows what every message carries.

Machines appear like deciders: each command leads to the states it moves the machine to, the events named <prefix>.runtime.<state> and labelled state, such as state order.runtime.placed for the Order machine in that section. What the machine does in a state (entry and exit actions, timers, starting and stopping child machines) appears as automations that name the state, such as automation actions(placed) in order, state placed. bin/sid commands list lists a machine’s commands as handled by the machine. The tree doesn’t show which states accept which commands. A command dispatched while no active state accepts it is acknowledged with no events, so it does nothing; check the machine’s class for what it accepts when.

Apps that use Sourced also get a sidereal-sourced skill (see Agent skills), which points AI coding agents to bin/sid sourced.

Consumer groups

bin/sid sourced groups list shows the running side — a consumer group per reactor, how many partitions it has claimed, and the furthest position any of them has read:

1 group, store at position 42

Group           Status  Partitions  Position
App::Commander  active  3           40

A group can be taken out of service and put back:

bin/sid sourced groups stop App::Commander --message 'draining for deploy'
bin/sid sourced groups start App::Commander

A stopped group is skipped when work is claimed, so its reactor stops consuming while the rest of the app carries on serving. Messages keep arriving in the store meanwhile; starting the group again picks up from where it left off, including everything that arrived while it was stopped. start also clears the error that stopped a failed group, which is the usual reason to reach for it.

bin/sid sourced groups reset NAME drops a group’s offsets so its reactor reads the whole store again — how a read model is rebuilt after its projector changes. Nothing is lost, since the messages are still there, but the work is redone, so it asks before going ahead; --yes answers for a script, and it refuses outright rather than guess when there’s nobody to ask.

It won’t reset a group that handles its messages exclusively and deletes them as it acks them, such as a Sidereal commander: there is nothing to replay, and dropping the offsets would only orphan the partitions it holds. Sourced skips that case too, but decides it from the groups registered in the running process — which a command line never has — so the check is made against the reactor itself.

All three go through the app’s Sourced router rather than its store, so the reactor’s on_stop, on_start and on_reset run as they would in the app.

A group that isn’t running says why underneath the table — the exception if it failed, or the --message if someone stopped it — and Retries and Retry at columns appear only once a group has retried: how many times, and when it tries next. Groups are registered when the app starts, not when the CLI builds it, so a store whose app has never run reports none.

Messages

bin/sid sourced messages list prints the log, newest hundred last, one message per line — position, id, time, type and payload:

     7  83085e7c-56af-4702-a674-c0927a3dd316  2026-10-07 00:08:25  s.todos.add    {"title":"Coffee"}
     8  71178680-8a77-4d4c-bfdd-41ae7b1902f9  2026-10-07 00:08:28  s.todos.added  {"title":"Coffee"}

--tail keeps it running, polling once a second for whatever arrives next, and --limit changes the page size. A page that comes back full is one the limit truncated, so it reads the next one straight away rather than waiting — a burst catches up at once instead of trickling out a page a second. Only the messages go to stdout — the “Tailing from position N” notice and anything else goes to stderr — so either mode pipes cleanly:

bin/sid sourced messages list --tail | grep todos.added

--types and --partition narrow the log to what a Sourced::Selector built from them selects, and --tail follows only those:

bin/sid sourced messages list --types todos.added,todos.removed
bin/sid sourced messages list --types 'todos.*' --tail
bin/sid sourced messages list --partition course_id=c1
bin/sid sourced messages list --types courses.user_joined --partition course_id=c1,user_id=u1

--types takes message types, comma-separated, and prefix.* for every type in a namespace. A selector is made of message classes, so these are the types the app defines; a name that matches none is an error rather than an empty list.

--partition takes key=value pairs and selects the messages that carry at least one of the keys, in payload or tags, with every key they carry at its value. So course_id=c1,user_id=u1 lists course c1’s messages that are about no user or about u1, and also u1’s messages that are about no course. Without --types it looks in every type the app defines.

bin/sid sourced messages correlation <id> traces one workflow — every message sharing a correlation, nested by what caused what. Any id in the chain will do:

Correlation 0f346b73-30eb-4627-82fb-940427b86711  5 messages

s.todos.add  #3  00:40:26  0f346b73-30eb-4627-82fb-940427b86711  {"title":"Taxes"}
└── s.todos.added  #4  00:40:26  7c2b1628-e533-4c03-8839-5348fd0da391  {"title":"Taxes"}
    ├── s.todos.notify  #5  00:40:26  4631f909-c84d-41b2-ae78-bd909edb4440  {"title":"Taxes"}
    │   └── s.todos.notified  #7  00:40:26  32b3d62f-0674-4d34-a49b-1ddbc3e7c103  {"title":"Taxes"}
    └── s.todos.notify  #6  00:40:26  c02d2d86-c8a6-4916-9dbe-5c50b4e55074  {"title":"Taxes (audit)"}

Message types are coloured by kind — commands cyan, events yellow, the same colours bin/sid sourced topology gives them — so the shape of a workflow reads off the indentation and the colour together. A message of neither kind, such as a plain Sidereal::Message, keeps the default rather than being miscoloured as one.

A chain whose first command has already been deleted — which is what an exclusive consumer such as a commander does on ack — still draws, rooted at the oldest message left.

The component graph

bin/sid system graph prints the components the app is made of, in the order they’re built and started, each with what it depends on:

Sidereal.config  37 components, built

Component                     Type                            State            Needs
sidereal.elector              Interface[start, on_promote, …  built
  Picks and signals the lead process
sidereal.pubsub               Interface[start, subscribe, p…  built            sidereal.elector
  Delivers published messages to their subscribers, such as open pages
db.filepath                   Sidereal::Types::String         built
  Where the SQLite database file lives
db                            Sequel::Database                built            db.filepath
  The app's database connection
sourced.db                    Sequel::Database                built, alias     db
sidereal.dispatcher           Interface[start, stop]          built, deferred  sidereal.store, sidereal.pubsub,
                                                                               sidereal.channels, sidereal.exceptions
  Consumes the store and runs the handlers for each command

Each component’s description, when it was declared with one, is printed dimmed under its row. The State column carries the lifecycle status, the mode when it isn’t the default singleton (alias, dynamic), and whether the component is deferred. Edges wrap into the last column, which sizes itself to the terminal — a component’s key is never shortened, so the type column gives up room first. --dependents turns the edges around, showing what depends on each component rather than what it needs.

A component tree is a DAG rather than a tree, so edges are listed per component instead of being nested — a component with two dependents would otherwise have to appear twice.

It’s most useful when the configuration is broken, so it prints the graph even then, and names the problem underneath:

UnimplementedComponentError: components are declared but not implemented: mailer

A dependency that was never declared is listed too, as Not declared:.

--mermaid prints a Mermaid flowchart of the same graph and nothing else, so it can go straight into a file:

bin/sid system graph --mermaid > graph.mmd

Edges point from each dependency to its dependents, nodes are shaped by mode and styled by status, and a configuration problem goes to stderr so the redirect stays clean.

bin/sid system tree answers a different question — not what depends on what, but how the components nest, which are mounted, and who implemented each:

Sidereal.config  37 components, built

(root)
├── sidereal [mounted]
│   ├── elector Interface[start, on_promote, on_demote, lea… (singleton, built) implemented by (root)
│   │     Picks and signals the lead process
│   ├── store Interface[append] (alias, built) implemented by (root)
│   │     Where dispatched commands are appended, for the dispatcher to consume
│   ├── workers
│   │   └── count Integer[0..] (singleton, built)
│   │         How many worker fibers the dispatcher runs
│   └── dispatcher Interface[start, stop] (singleton, built, deferred)
│         Consumes the store and runs the handlers for each command
├── db Sequel::Database (singleton, built)
│   │ The app's database connection
│   └── filepath Sidereal::Types::String (singleton, built)
│         Where the SQLite database file lives
└── sourced [mounted]
    └── db Sequel::Database (alias, built) implemented by (root)

[mounted] marks a library’s own tree — Sourced’s, under sourced — and implemented by marks a component the app implemented over the library that declared it, which is how use wires the two together. It takes --mermaid too, as a top-down flowchart.

Listing commands

$ bin/sid commands list
Command                     Class                Handled by      Web
my_app.greetings.say_hello  Greetings::SayHello  App::Commander  yes

This lists every command the app knows about:

  • Handled by: the commanders that handle the command. App::Commander is the app’s own, for command blocks in web/app.rb. Commanders added with commands in the app appear by name, and with --sourced, so do Sourced deciders and machines: the Sourced integration adds its reactors’ commands with Sidereal::CLI::Commands.handlers, which another integration can call too.

list and info prepare the app’s components without building them, so they open no database connection.

  • Web: whether the app accepts the command from forms, with handle. A command the web accepts but that nothing handles shows none under Handled by.

Add --schemas for a line under each command with its payload’s JSON Schema. It’s everything needed to dispatch any of the app’s commands, in one call, which makes it the quickest way for scripts and AI agents to learn what the app can do:

$ bin/sid commands list --schemas
Command                     Class                Handled by      Web
my_app.greetings.say_hello  Greetings::SayHello  App::Commander  yes
  {"type":"object","properties":{"name":{"type":"string"}},"required":["name"]}

Inspecting a command

$ bin/sid commands info Greetings::SayHello
my_app.greetings.say_hello  Greetings::SayHello
Handled by  App::Commander
Web         yes

Attribute  Type    Required
name       string  yes

The table has a row per payload attribute. Nested attributes are listed as address.city, and attributes of objects inside an array as items[].sku. The Default and Notes columns appear when any attribute has a default, or options (one of: "small", "large") or other rules.

Add --json for the payload’s JSON Schema instead, and nothing else, ready to pipe to other tools:

$ bin/sid commands info Greetings::SayHello --json
{
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "required": [
    "name"
  ]
}

Dispatching a command

$ bin/sid commands dispatch my_app.greetings.say_hello --name Sidereal
Dispatched my_app.greetings.say_hello  Greetings::SayHello
id  52c79eda-d348-4b88-b056-830bf60e7620
  name  "Sidereal"

With bin/dev running, the hello appears on the welcome page, just as if it had been sent from the form. The command is appended to the app’s store, so if the server isn’t running, it’s handled once it starts.

Attributes follow the command’s payload schema (see bin/sid commands info). bin/sid commands dispatch --help summarizes the syntax:

Command line Payload
--units 2 or --units=2 units: "2"
--gift (a flag without a value) gift: "true"
--address.city Paris address: {city: "Paris"}
--tags vegan --tags spicy tags: ["vegan", "spicy"], for an array attribute

Values are converted to the types the payload declares, the same way form fields from the web are: "2" becomes 2 for an Integer attribute, and "true" becomes true for a Boolean. The command is validated before it’s sent. If any attribute is invalid or missing, nothing is dispatched and every error is listed:

$ bin/sid commands dispatch Greetings::SayHello --name ""
Not dispatched. Invalid attributes for my_app.greetings.say_hello:
  --name: must be present

An unknown attribute, such as a typo, is an error too, and lists the attributes the command takes. So is a command that nothing handles, since it would never run.

Commands dispatched from bin/sid don’t go through the app’s before_command hooks, which run for commands sent from the web.

Agent skills

Sidereal and its integrations provide skills that teach AI coding agents to work with an app, such as sidereal-cli, for using bin/sid, sidereal-components, for giving classes their dependencies with dep, and sidereal-ui, for pages and components that handle their own commands and declare their own assets. bin/sid skills update writes them into the app’s .agents/skills/ directory, where agents look for skills, and links .claude/skills to it for Claude Code. sid new runs it for a new app, unless you pass --no-skills.

Run it again after updating Sidereal or adding an integration:

bin/sid skills update

This writes the skills of Sidereal and of every integration the app configures. It rewrites those skills in .agents/skills/ and leaves any other skills there alone. It connects to nothing: bin/sid has already loaded the app (see Extending the command line), so the skills are registered by the time the command runs.

An integration registers its skills from its setup, with the path to a skill’s SKILL.md or a directory holding it:

# lib/my_integration.rb
module MyIntegration
  def self.setup(config, **opts)
    Sidereal.skills.add('my-integration', File.expand_path('skills/my-integration', __dir__))
    # ... components
  end
end

Console

bin/sid console

Starts IRB with the app loaded, from the app’s root directory, with its components built (Sidereal.config.build!) — so Sidereal.store and anything the app declared is readable. Nothing is started: no pubsub, no workers. For example, Sidereal.dispatch!(Greetings::SayHello, name: 'Ada') appends a command to the store from Ruby, for the running app’s workers to pick up.

Working with time

Dynamically scheduled commands

Chain .at(time) or .in(duration) (aliases) on a dispatch call to defer processing of a command (or event) until a future time. Three accepted forms:

Form Example Resolution
Time / DateTime .at(Time.now + 86400) Absolute target.
Integer .in(3600) Seconds added to Time.now.
Duration String .at('5m'), .in('PT1H30M') Parsed via Fugit.parse_duration, added to Time.now.
command PlaceOrder do |cmd|
  ORDERS[cmd.id] = cmd.payload.to_h

  # Run an hour from now — Integer form
  dispatch(SendReminder, order_id: cmd.id).in(3600)

  # Run 30 minutes from now — Fugit duration String
  dispatch(NudgeUser, order_id: cmd.id).in('30m')

  # Run at a specific instant — Time form
  dispatch(ExpireOrder, order_id: cmd.id).at(Time.now + 86400)

  # ISO8601 durations also work
  dispatch(SendDigest, order_id: cmd.id).in('PT1H')
end

Resolved targets earlier than the message’s created_at raise Sourced::Message::PastMessageDateError — including negative integers (.in(-60)) and durations that resolve to the past.

The dispatched message is appended to the store with its created_at set to the resolved target. Stores that support scheduled delivery hold the message back and only deliver it once that time has passed:

Store Scheduled delivery
Store::FileSystem Honored — future-dated messages are written to a scheduled/ directory and promoted by a background fiber when due.
Store::Memory Ignored — messages are delivered immediately regardless of created_at. Use FileSystem when you need scheduling.

Scheduling does not propagate across correlation: an event dispatched downstream of a scheduled command runs at its own created_at (i.e. immediately), not at the source’s future time.

Fixed schedules

App.schedule registers a sequence of moments in time where commands should fire. The Scheduler is a leader-only fiber that, on each tick, appends commands to the same store the rest of your app uses — so schedule handlers run on the worker pool, in parallel with everything else, with the same retry / dead-letter machinery.

The basics — single-step shorthand

class MyApp < Sidereal::App
  schedule 'Daily cleanup', '5 0 * * *' do |cmd|
    # Runs every day at 00:05.
    # `cmd` is the auto-generated command, materialised as
    # MyApp::Commander::Schedules::SchedDailyCleanup0Step0.
    dispatch SweepStaleOrders, older_than: '1d'
  end
end

The schedule name ('Daily cleanup') is mandatory — it shows up in dead-letter sidecars, dashboards, and cmd.metadata[:schedule_name] so handlers and reactions can identify the source.

Expressions

A step’s expression is anything Fugit.parse accepts, plus stdlib Time / DateTime instances:

Kind Example Behaviour
Specific datetime '2026-12-31T10:00:00' Fires once at that instant.
Time instance Time.now + 60 Fires once at that instant (coerced internally).
Cron (5- or 6-field) '5 0 * * *', '*/5 * * * *' Recurring at every cron match.
Natural language 'every 3 seconds' Recurring.
Duration '10d', '1h30m', 'P12Y12M' Fires once at previous concrete time + duration.

Multi-step schedules — sequence of at calls

For workflows that don’t fit a single step, drop the second positional and use the inner DSL — each at call appends a step to the schedule:

schedule 'Flash sale campaign' do
  at '2026-05-10T10:00:00' do |cmd|
    # Fires once at this exact moment.
    dispatch OpenSale, sale_id: 'flash-2026'
  end

  at 'every day at 9am' do |cmd|
    # Recurring — fires daily until the next concrete step.
    dispatch SendDailyReminders
  end

  at '10d' do |cmd|
    # Fires once at "previous concrete + 10 days".
    # This concrete time also closes the recurring step above.
    dispatch CloseSale, sale_id: 'flash-2026'
  end
end

Steps are validated at registration:

  • No time travel. A specific datetime must be strictly after the previously resolved concrete time.
  • No back-to-back recurring steps. A recurring step can’t follow another recurring step — the first one would have no end. Insert a specific or duration step between them.
  • Durations resolve against the last concrete step, not against any intervening recurring step. So in the example above, '10d' is “10 days after the '2026-05-10T10:00:00' opening step”, not “10 days after the recurring started”. The resolved time also closes the recurring window.
  • A trailing recurring step has no upper bound and runs forever.

Bound-only marker steps

Drop the block / class for a specific or duration step to declare a bound-only marker — anchors the timeline without dispatching anything. Useful as a starting boundary for a following recurring step, or as a closing boundary for a preceding one:

schedule 'Office hours' do
  at '2026-05-10T09:00:00'                # marker — opens the window, no command
  at 'every 5 minutes' do |cmd|
    dispatch HealthCheck
  end
  at '2026-05-10T17:00:00'                # marker — closes the recurring, no command
end

Block-less markers only work for specific or duration steps. A recurring step without a block would fire nothing on every match — meaningless — so it raises at registration.

Explicit command classes (skip auto-generation)

By default, each at block generates a per-step command class under <HostCommander>::Schedules (e.g. MyApp::Commander::Schedules::SchedDailyCleanup0Step0 — Sched<CamelName><ScheduleId>Step<StepIndex>). For steps that should dispatch a domain command you’ve already defined elsewhere, pass the class + payload kwargs instead of a block:

# Define explicit commands and handlers
StartCampaign = Sidereal::Message.define('myapp.start_campaign')
command StartCampaign do |cmd|
  # do something here
end

# Now just define time-based triggers for your own commands
schedule 'Flash sale campaign' do
  at '2026-05-10T10:00:00', StartCampaign
  at 'every day at 9am',    SendEmails, sender: 'acme@company.org'
  at '10d',                 EndCampaign
end

In the explicit form the macro generates no class and defines no handler — it just passes the class and payload through to the Scheduler, which dispatches SendEmails.parse(payload: { sender: 'acme@company.org' }, metadata: { ... }) on every fire. You’re responsible for having a command SendEmails do |cmd| ... end registered.

You can mix block and explicit forms freely across steps in the same schedule.

Metadata stamped on every fire

The Scheduler stamps these metadata keys on every dispatched command:

{
  producer:       "Schedule #0 'Flash sale campaign' step #1 (every day at 9am)",
  schedule_name:  "Flash sale campaign"
}

The producer label includes the schedule’s registration index, name, step index, and the step’s own expression — so dead-letter sidecars and dashboards can pinpoint which step fired. Both keys propagate to downstream commands via Message#correlate, so anything dispatched from inside a schedule handler carries them automatically.

Multi-process: only the leader runs the Scheduler

The Scheduler ticks only on the process that holds Sidereal.elector. With the default Elector::AlwaysLeader (single-process apps) every process is leader; with Elector::FileSystem only one process per host runs the tick fiber. The dispatched commands then flow through the normal store, so any worker fiber on any process can pick them up — schedule handlers are not pinned to the leader.

A few caveats worth knowing:

  • At-most-once per tick. If a process pauses (GC, debugger) past a cron boundary, only the most recent boundary inside the tick window fires. Matches crond’s no-catch-up behaviour.
  • Boundaries crossed during a leader vacancy are lost. Step boundaries are point-in-time signals, not state declarations — if no leader is running at the boundary, no command is dispatched.
  • Within a single leader’s lifetime, every step fires exactly once at its instant (or every cron match, for recurring steps). Steps use the half-open tick window (@last_tick_at, now] — once the boundary moves into the past it can’t be in any future window.
  • Implicit baseline = scheduler-construction time. If your first step is a duration (at '5m', X), it resolves to boot + 5m. Across a leader handoff each leader has its own boot time, so duration-anchored first steps drift; anchor with a specific datetime if stability matters.

Router

Sidereal::App is a subclass ofSidereal::Router , which is a standalone Rack-compatible router with a Sinatra-style DSL and trie-based dispatch. It can be used independently of the full Sidereal app framework.

Basic routes

Route blocks are evaluated in the context of a router instance, with access to request, response, and helper methods like body, status, headers, halt, redirect and send_file.

class MyRouter < Sidereal::Router
  get '/' do
    body 'hello'
  end

  get '/items/:id' do |id:|
    body "item #{id}"
  end

  post '/items' do
    status 201
    body 'created'
  end

  redirect '/old-path', '/new-path'
end

# config.ru
run MyRouter

Callable handlers

Any object responding to #call(request, response, params) can be used as a handler. Callable handlers can either modify the response object or return a raw Rack triplet ([status, headers, body]).

class ShowItem
  def call(request, response, params)
    response.body = ["item #{params[:id]}"]
  end
end

class MyRouter < Sidereal::Router
  get '/items/:id', ShowItem.new

  # Lambdas work too
  get '/health', ->(req, resp, params) { [200, {}, ['ok']] }
end

Before hook

Run logic before every matched route. Use halt to short-circuit.

class MyRouter < Sidereal::Router
  before do
    halt 401, 'unauthorized' unless session[:user_id]
  end

  get '/dashboard' do
    body 'welcome'
  end
end

Sessions

class MyRouter < Sidereal::Router
  session secret: ENV.fetch('SESSION_SECRET')

  post '/login' do
    session[:user_id] = request.params['user_id']
    body 'logged in'
  end

  get '/profile' do
    body "user: #{session[:user_id]}"
  end
end

The secret can also be a callable, which the router calls on each request. It can then read a component, which isn’t built yet when the class loads. A generated app derives the key from its master secret, with Sidereal.secrets:

session secret: -> { Sidereal.secrets.derive('session') }

The router creates the session middleware on the first request, and again whenever the secret changes.

Halt and redirect

halt immediately stops request processing and returns a response.

halt 422                              # status only
halt 200, 'hello'                     # status + body
halt 301, 'Location' => '/new-path'   # status + headers
halt 200, { 'X-Custom' => '1' }, 'ok' # status + headers + body

redirect is a shorthand for halting with a Location header:

redirect '/new-path'              # 301 by default
redirect '/new-path', status: 302 # temporary redirect

Sending files

send_file responds with a file and halts, for files that aren’t assets, such as uploads or generated reports:

get '/uploads/:id' do |id:|
  upload = Upload.find(id)
  send_file upload.path,
    filename: upload.name,                  # the name the browser shows, or saves it as
    disposition: :attachment,               # download it; :inline (the default) shows it
    cache_control: 'private, max-age=3600'
end
  • Streamed: the file is sent from disk, not read into memory. Its body answers to_path, which Falcon sends straight from the file.
  • As Rack::Files serves it:
    • its type comes from its extension, or type: replaces it, and an unknown extension is application/octet-stream;
    • a byte range is answered with a 206, as PDF viewers and video players ask for;
    • If-Modified-Since is answered with a 304;
    • a missing file is a 404.
  • Headers:
    • x-content-type-options: nosniff is always sent, so a browser never runs an uploaded file as another type, such as HTML.
    • filename: can be any text: it’s sent as an ASCII fallback and as UTF-8.
    • cache-control is only sent when you pass it.

Configuration

Sidereal’s configuration is a tree of typed components, built with sourced-component: each one has a type, a default or an implementation, the components it depends on, and lifecycle hooks. Sidereal.config is the root. Sidereal’s own components live under sidereal, integrations mount other libraries next to them (Sourced under sourced), and the app declares its own: a database connection, an API client, a setting.

Declare and implement components while the app loads, before it boots. Any file the app loads will do, such as boot.rb:

# boot.rb
require 'sequel'
require 'sidereal'

Sidereal.config.declare('db', Sequel::Database, description: "The app's database connection")
Sidereal.config.component!('db') do
  build { Sequel.sqlite(ENV.fetch('DB_PATH')) }
  teardown(&:disconnect)
end

Sidereal.config.declare('repos.orders', OrdersRepo, description: 'Reads and writes orders')
Sidereal.config.config!('repos.orders', ['db']) { |db| OrdersRepo.new(db) }

Sidereal.config.config!('sidereal.workers.count') { 10 }
  • declare(key, type, description:) adds a typed component. A block gives it a default: declare('retries', Integer) { 3 }. The optional description: says what it’s for, and bin/sid system graph and bin/sid system tree print it under the component.
  • component!(key, deps) { ... } implements one as a singleton, built once per process, with build, start, stop and teardown hooks. build gets the values of deps, in order, and returns the component’s value.
  • config!(key, deps) { |*values| ... } is the shorthand for a component that only builds. config (without !) builds a new value on every read.
  • alias(key, target) makes a component read another one’s value.
  • env('ORDERS_URL' => 'orders.url') implements components from ENV variables, decoded into their types.
  • Values are parsed through their declared types, so a component that builds the wrong thing fails the boot, naming the component.
  • Declarations can come in any order, from any file: nothing is built until the app boots. requires belong at the top of the file, outside the blocks: they run as the file loads, which is shared by every worker when the app is preloaded.
  • Implementing a component again replaces its implementation, hooks included: the last one wins.

puts Sidereal.config.tree prints every component, its status, and who implemented it. See the sourced-component README for everything a component can do.

Sidereal’s components

Component Default  
sidereal.elector Elector::AlwaysLeader leader election. Started first
sidereal.pubsub PubSub::Memory carries updates to browsers. Depends on the elector, so it starts after it
sidereal.store Store::Memory where commands are appended. Sidereal.store reads it
sidereal.channels, sidereal.exceptions Sidereal.channels, Sidereal.exceptions the channel and exception registries. Locked when they start
sidereal.broadcaster Sidereal::Broadcaster publishes transient messages on the pubsub, on their resolved channel. What broadcast uses
sidereal.workers.count 25 worker fibers of the default dispatcher
sidereal.dispatcher Sidereal::Dispatcher consumes the store. Deferred: the runner starts it
sidereal.runner.targets ['sidereal.dispatcher'] what the runner starts
sidereal.runner.process :all where the runner starts it: :all or :leader (see Custom backends)
sidereal.scheduler Sidereal.scheduler the Scheduler. Starts after the runner
sidereal.secret_key_base SIDEREAL_SECRET, or else storage/secret.txt in the working directory, which building creates the app’s master secret, at least 64 bytes. See Running with Falcon. A generated app implements it in config/components/secrets.rb, the same way, with the file in the app’s storage/
sidereal.secrets Sidereal::KeyGenerator derives a key for each purpose from the master secret, with HKDF-SHA256 (derive('session')), so one secret serves them all, and a derived key reveals neither the secret nor the other keys. Anything that answers derive(purpose), ex. one that reads a key for each purpose from a vault. Sidereal.secrets reads it
sidereal.assets.mode :single what layouts link: each asset (:single), or one stylesheet bundle and each script (:combined)
sidereal.assets.watch true, false in production in :single mode, whether layouts notice when an asset’s file changes. See Assets
sidereal.assets.host nil a host asset URLs start with, such as a CDN’s
sidereal.assets.live_reload false whether open pages update when an asset changes. See Live reload
sidereal.assets.watcher Assets::Watcher checks assets for changes on the leader, when live_reload is on
sidereal.signer Sidereal::Signer, with the key sidereal.secrets derive for it signs and verifies tokens: pages’ stream URLs, and anything an app signs with it (dep 'sidereal.signer'). Anything that answers generate(data, purpose:) and verify(token, purpose:), which returns nil for a token it didn’t make. Sidereal.signer reads it

Implement any of them to replace it. See Custom backends.

Lifecycle

Sidereal::Host#start starts Sidereal.config in every process, leader or follower:

  1. Prepare. The tree is checked: a dependency that isn’t declared, a component declared but not implemented, or a cycle fails the boot. From then on nothing can be declared or implemented.
  2. Build. Every component is built, in dependency order. That’s where a process opens its connections. Building happens while the channel and exception registries are still open, so a component’s build can register an exception subscriber:

    Sidereal.config.declare('error_reporter', ErrorReporter::Client)
    Sidereal.config.config!('error_reporter') do
      ErrorReporter::Client.new(ENV.fetch('ERROR_REPORTER_KEY')).tap do |client|
        Sidereal.exceptions.on_failure { |report| client.notify(report.exception) }
      end
    end
    
  3. Start. Start hooks run in dependency order: the registries lock, the elector and pubsub start, the runner starts the dispatcher (now, or when the process is elected), then the scheduler.

Sidereal::Host#stop stops the dispatcher, then tears every component down in reverse order: stop hooks, then teardown hooks.

Outside a host, reads raise Sourced::Component::NotBuiltError until the configuration is built. Rake tasks, consoles and specs call Sidereal.config.build! once the app has loaded. It builds every component, and starts none:

# Rakefile
require_relative 'boot'
Sidereal.config.build!

task :seed do
  Sidereal.dispatch!(PlaceOrder, sku: 'abc')
end

Values are never shared across processes: the Falcon controller, which forks the workers, refuses to build (see Preload vs lazy loading).

Injecting components into classes

dep adds keyword arguments to a class’s constructor, defaulting to components’ values, with a reader for each:

class OrdersProjector < Sourced::Projector::StateStored
  dep :db
  dep 'repos.orders' => 'orders'

  sync do |context:, **|
    db[:orders].insert_conflict(:replace).insert(context)
  end
end

OrdersProjector.new(partition_values)              # db: Sidereal.config['db']
OrdersProjector.new(partition_values, db: test_db)
  • Keys are relative to Sidereal.config: dep 'sidereal.store', dep 'sourced.store'. The keyword is the key’s last segment, and a hash renames it: dep 'sourced.store' => 'events'.
  • Values are read each time an object is created, so a class can be defined before the configuration is built, but not instantiated. A value passed explicitly isn’t read.
  • A name the class already has a method for is refused (Sourced::Component::InjectionError), since the reader would replace it. Rename it instead: dep 'sidereal.store' => 'commands'.
  • Every other argument reaches the class’s own initialize untouched, so classes that frameworks instantiate themselves, like Sourced’s reactors, work unchanged.
  • dep comes from extend Sidereal::Deps, and is include Sidereal.config.inject(...).

Commanders extend Sidereal::Deps already, and with the Sourced integration loaded so do Sourced::Decider and Sourced::Projector, so their handlers can use what they declare:

class Orders < Sidereal::Commander
  dep 'repos.orders' => 'orders'

  command PlaceOrder do |cmd|
    orders.insert(cmd.payload)
  end
end

On an App, dep reaches both kinds of handler: handle blocks, which run on the app for each request, and command blocks, which run on its commander:

class ShopApp < Sidereal::App
  dep 'repos.orders' => 'orders'

  handle PlaceOrder do |cmd|
    halt 422 if orders.duplicate?(cmd.payload)
    dispatch cmd
  end

  command PlaceOrder do |cmd|
    orders.insert(cmd.payload)
  end
end

Commanders added with commands declare their own. An App subclass inherits its parent’s deps for its handle blocks, but has a commander of its own: it declares what its command blocks use with commander.dep.

Running with Falcon

Sidereal is designed to run on Falcon, which provides the async fiber runtime needed for SSE streaming and concurrent command processing.

Create a falcon.rb file. It requires only the environment: each worker loads the app itself through config.ru (see Preload vs lazy loading).

#!/usr/bin/env falcon-host
# frozen_string_literal: true

require 'sidereal/falcon/environment'

service "my-app" do
  include Sidereal::Falcon::Environment
  include Falcon::Environment::Rackup

  url "http://localhost:9292"
  count 1
end

Run with:

bundle exec falcon host

The host builds and starts the app’s components before it serves requests.

Sidereal derives the keys for an app’s session cookie and for signing pages’ stream URLs from its master secret, which a generated app configures in config/components/secrets.rb: SIDEREAL_SECRET, or else storage/secret.txt, which the first boot creates, readable only by its owner. Every process on the machine shares the file, and it lasts as long as storage/ and the data in it, so a single machine needs no setup. Set SIDEREAL_SECRET instead to a random string of at least 64 bytes (sid secret makes one) when the app runs on several machines, or on a disk that a deploy replaces: every process must have the same secret, kept across deploys. Changing the secret logs everyone out. A backup of storage/ includes it. An app that replaces the master secret, sidereal.secrets or the signer, ex. with a secret from a vault, has the same constraint: every worker has to sign alike, because a page one worker renders may stream from another, and tabs that are still open after a deploy reconnect with tokens signed by the old secret. Those tabs reload when their token is refused.

Workers

Each process that runs the dispatcher handles commands in sidereal.workers.count fibers (25 by default):

Sidereal.config.config!('sidereal.workers.count') { 3 }

Preload vs lazy loading (production)

Falcon forks worker processes (count, which defaults to the CPU count). Where the app loads decides what the workers share, and what a zero-downtime restart picks up. Whichever you choose, every worker builds its own connections: Sidereal builds its configuration in each worker, when it boots.

  • Lazy (default). falcon.rb requires only the environment, and each worker loads config.ru → boot.rb in its own process. Nothing of the app is shared across workers, and Falcon’s zero-downtime restart (SIGHUP), which forks a fresh set of workers, loads the new code from disk — so HUP deploys work.

    # falcon.rb — lazy: the app loads per worker via config.ru
    require 'sidereal/falcon/environment'
    
    service "my-app" do
      include Sidereal::Falcon::Environment
      include Falcon::Environment::Rackup
      url "http://localhost:9292"
    end
    
    # config.ru — loads boot.rb inside each worker
    require_relative 'boot'
    run MyApp
    
  • Preload gems. Gems in a :preload Bundler group are required by Falcon’s controller, which then compacts its heap (Process.warmup), so workers forked from it share them copy-on-write. App code still loads per worker, and HUP deploys still work. As async-service implements it, the warm-up runs after the first workers have started, so it is the workers forked by later restarts that benefit.

  • Preload the app (opt-in). preload "boot.rb" in the service block loads the app once in the controller, before forking: code, classes, component declarations and compiled codecs are shared copy-on-write, and workers boot faster. Things to know:

    • Deploys need a full restart. A HUP forks new workers from the controller, which still holds the code it loaded at start. Restart falcon host (or switch instances in front of it) to pick up new code.
    • Nothing fork-unsafe may be opened while the app loads. The controller calls Sidereal.lock!, so building Sidereal.config there raises Sidereal::ForkError instead of handing every worker the same connection. Declaring and implementing components is fine: that’s what a preloaded app does. A connection opened outside a component can’t be caught, though: open connections in a component’s build, rather than at the top of a file or in a Sequel::Model that reads its schema when defined. Preload through preload, not by requiring the app from falcon.rb, which runs before that check is in place.
    • Preparing before forking is optional. A preload that loads every message type and every component declaration can end with Sidereal.config.prepare!. Preparing builds nothing, so it’s allowed in the controller, and it compiles codecs once for every worker to share (Sourced’s store codec, with the Sourced integration). It also locks the tree, so anything the workers load afterwards (an app.rb that config.ru requires) can’t declare components.
    service "my-app" do
      include Sidereal::Falcon::Environment
      include Falcon::Environment::Rackup
      url "http://localhost:9292"
      preload "boot.rb"   # everything the app needs; config.ru then just requires it
    end
    

For an in-memory backend the distinction hardly matters; it bites when a backend holds a real, fork-unsafe connection.

Custom backends

The store, pubsub, elector and dispatcher are components. By default they’re in-memory implementations: implement them to replace them.

Sidereal.config.config!('sidereal.store', ['db']) { |db| MyStore.new(db) }

# One that runs in the background starts itself in a start hook
Sidereal.config.component!('sidereal.pubsub', ['sidereal.elector']) do
  build { |elector| MyPubSub.new(elector:) }
  start { |pubsub, task| pubsub.start(task) }
end
  • A store must respond to #append(message). A pubsub to #start(task), #subscribe(*patterns) and #publish. #subscribe returns one channel fed by every pattern, which gets a message matching several of them once, and stops them all on #stop. An elector to #start(task), #on_promote, #on_demote and #leader?.
  • Implementing a component replaces its hooks along with how it’s built, so a pubsub, elector or scheduler you implement brings its own start hook, as above.
  • A dispatcher must respond to #start(task) and #stop, and be able to start again after stopping. Implement sidereal.dispatcher with hooks that call them. It stays deferred, so the runner starts it:

    Sidereal.config.component!('sidereal.dispatcher', ['sidereal.store']) do
      build { |store| MyDispatcher.new(store) }
      start { |dispatcher, task| dispatcher.start(task) }
      stop(&:stop)
    end
    

Which process runs the dispatcher. The dispatcher is a deferred component: booting doesn’t start it. The runner starts it instead, with the components it depends on. sidereal.runner.process decides where:

  • :all (default): every process starts a dispatcher at boot.
  • :leader: only the process holding Sidereal.elector starts one, the same rule the Scheduler follows. The dispatcher stops if that process is demoted, and the next leader starts its own.

Web requests keep appending commands from every process; only the consuming side is pinned. That’s the right shape for a backend that serializes writers (Sourced on SQLite, where the Sourced integration sets it for you): reads scale across workers, while handler and projection writes come from one.

Sidereal.config.use_file_system!   # a cross-process elector is what makes :leader meaningful
Sidereal.config.config!('sidereal.runner.process') { :leader }

With the default Elector::AlwaysLeader every process is leader, so the two modes coincide. A dispatcher that fails to start on a later promotion (after a failover) is logged by the elector’s callback guard rather than failing the boot, since promotion happens after boot.

Multi-process shortcut. Sidereal.config.use_file_system! switches the store, pubsub, and elector to their filesystem / unix-socket implementations in one call: the combination needed to run across multiple Falcon workers on one host (a shared on-disk queue, a unix-socket pubsub broker, and file-lock leader election). Files and the socket live under dir: (default ./storage, relative to the working directory). Keep that directory out of version control. Implement any of them afterward to replace it alone:

Sidereal.config.use_file_system!                         # FS store + unix-socket pubsub + file-lock elector
Sidereal.config.config!('sidereal.store') { MyStore.new } # ...but your own store

Integrations. Backends that provide several components at once (a store and a dispatcher, plus bridging) ship as integrations, applied with Sidereal.config.use(SomeIntegration, **opts). It calls SomeIntegration.setup(Sidereal.config, **opts), which mounts and implements components. Anything that responds to setup qualifies, so an integration can live in any gem — Sidereal needs no knowledge of it. use_file_system! is itself one. Integrations that implement the same component replace each other’s: the last one wins. See Using Sourced as a backend for the canonical example.

setup is also where an integration registers the things that aren’t components: its bin/sid commands and its agent skills. One rule covers all three — they exist exactly when the app configures the integration. setup runs while boot.rb loads, before anything is prepared or built, so it can declare and implement freely.

Two later hooks exist for work that needs to see the whole loaded app, both on the component tree’s notifier:

  • config.notifier.subscribe('root.preparing') { ... } fires before the dependency order is resolved and before the tree locks, so it can still declare and implement components. The Sourced integration registers Sidereal’s commanders as reactors there, once every app class has loaded.
  • A component’s own prepare hook runs after the order is resolved. It can act, but anything it declares then is never built — use root.preparing for that.

Filesystem store

Sidereal::Store::FileSystem is a built-in durable store that survives process restarts and lets multiple worker processes on the same host share a queue. It also honors scheduled commands, unlike the default in-memory store.

It isn’t autoloaded: require it explicitly, then implement sidereal.store with it (or call use_file_system!, which also switches the pubsub and elector):

require 'sidereal'
require 'sidereal/store/file_system'

Sidereal.config.config!('sidereal.store') do
  Sidereal::Store::FileSystem.new(root: 'storage/store')
end

File bodies are one JSON document per message, written by the shared transport codec — see Serialization for the wire shape and for adding encoders for your own payload types.

The store creates five sibling directories under root/: tmp/, ready/, scheduled/, processing/, and dead/. Producers append by atomic-renaming from tmp/ into ready/ (or scheduled/ for future-dated messages). A poller fiber claims into processing/; a scheduler fiber promotes due files from scheduled/ to ready/; a sweeper recovers anything left in processing/ by a crashed worker. Permanently-failed messages land in dead/ along with a <f>.error.json sidecar — see Failure handling.

Constructor options:

Option Default Description
root: 'tmp/sidereal-store' Directory holding the four subdirs. Must be on a single local filesystem (atomic rename is unreliable across NFS).
poll_interval: 0.1 Seconds the poller sleeps when ready/ is empty.
scheduler_interval: 1.0 Seconds between scans of scheduled/ for due messages. Sub-second granularity is not provided.
sweep_interval: 60 Seconds between sweeps of stale processing/ files.
stale_threshold: 300 A processing/ file older than this (or owned by a dead PID) is treated as abandoned and renamed back to ready/.
max_in_flight: 50 Bound on the in-process queue between the poller and worker fibers. When handlers fall behind, the queue blocks and disk becomes the buffer.

At-least-once delivery: a crash mid-handling causes the message to be re-claimed once the sweeper recovers the abandoned processing/ file. Handlers must be idempotent.

Single-machine only: the design relies on POSIX atomic rename, which is unreliable across networked filesystems like NFS. Use a different store if you need to fan workers out across hosts.

Failure handling, retries and dead-lettering

When a command handler raises, the dispatcher calls Commander.on_error(exception, message, meta) and uses the returned value to decide what to do next:

Return value Effect
Sidereal::Store::Result::Retry.new(at: time) re-schedule for another attempt at time
Sidereal::Store::Result::Fail.new(error: exception) give up — dead-letter the message
Sidereal::Store::Result::Ack swallow silently — drop the message

The default policy retries with exponential backoff (2 ** meta.retry_count seconds) up to Sidereal::Commander::DEFAULT_MAX_ATTEMPTS attempts, then fails. Override per-commander:

class MyApp < Sidereal::App
  commands do
    def self.on_error(exception, message, meta)
      case exception
      when MyDomain::Invalid
        Sidereal::Store::Result::Fail.new(error: exception)  # bail immediately
      when Net::Timeout
        Sidereal::Store::Result::Retry.new(at: Time.now + (5 * meta.retry_count))
      else
        super  # fall back to the default policy
      end
    end
  end
end

meta.retry_count starts at 1 and increments on each retry. meta.first_appended_at is preserved across retries — useful for “give up after N hours regardless of attempt count” policies.

Where retried/failed messages go

  • Sidereal::Store::FileSystem — Retry renames the message into scheduled/ with a bumped retry_count and the new not_before_ns; the body stays untouched (commanders cannot mutate the message between attempts). Fail writes a sidecar dead/<f>.error.json with the exception class/message/backtrace, then renames the message into dead/. The sweeper does not touch dead/ — those messages are terminal until you act on them manually.
  • Sidereal::Store::Memory — Retry and Fail log at WARN level and ack/drop the message. The in-memory store has no scheduling or dead-letter primitives.

Inspecting and requeueing dead messages. Apps that use the file system store get bin/sid store. It’s left out when something used after use_file_system! replaces the store, as the Sourced integration does. It works on the store’s files directly, so it’s safe to run while the app is running:

bin/sid store stats                           # how many messages wait in ready/, scheduled/, processing/ and dead/
bin/sid store list --queue dead               # each dead letter, with its exception
bin/sid store show 5ecd5fdf                   # payload, metadata, error and backtrace
bin/sid store requeue 5ecd5fdf                # back to ready/, once the cause is fixed
bin/sid store requeue --all --type 'orders.*' # every dead letter in a namespace
bin/sid store delete 5ecd5fdf                 # drop a dead letter, or cancel a ready or scheduled message

A message is named by its id, the start of one, or its filename. Messages are listed and shown as they were serialized, not decoded, so a dead letter whose type the app no longer defines can still be inspected. A message in processing/ belongs to the worker handling it, so it can’t be deleted.

The same operations are public on the store, for scripts: #entries(queue) lists a queue’s Sidereal::Store::FileSystem::Entry values, #read(entry) returns a message’s JSON, #failure(entry) a dead letter’s error, and #delete(entry) removes one.

Once you’ve fixed the underlying cause of failure, Sidereal::Store::FileSystem#requeue(filename) moves a dead-lettered message back into ready/. The new filename has retry_count reset to 1 and not_before_ns set to now (immediately ready); first_append_ns is preserved so age-based diagnostics retain the lineage. The .error.json sidecar is removed.

store = Sidereal::Store::FileSystem.new(root: 'storage/store')
store.requeue('1762000000-1761000000-3-12345-abcdef.json')
# => "<root>/ready/<new-filename>.json"

Path components in the input are stripped via File.basename, so 'abc.json', 'dead/abc.json', and '/abs/dead/abc.json' are all equivalent — the file is always resolved against the store’s configured dead/ directory. Missing files raise ArgumentError.

At-least-once delivery still applies: a worker crash before Retry/Fail is acted on leaves the file in processing/ for the sweeper to recover, which re-runs the handler. Handlers must be idempotent.

Exception reporting

Every retry or terminal failure decision a backend makes is reported through Sidereal.exceptions, a process-global subscriber registry. Subscribers receive a small ExceptionReport value:

ExceptionReport = Data.define(:kind, :exception, :message, :retry_count, :retry_at)
# kind:        :retry | :failure
# exception:   the raw StandardError instance
# message:     the failed Sidereal::Message (typically a command)
# retry_count: 1-indexed attempt number that just failed
# retry_at:    Time of the next attempt (nil on :failure)

Register subscribers during boot — APM hooks, structured loggers, anything you want notified:

Sidereal.exceptions.on_failure do |report|
  Sentry.capture_exception(report.exception, extra: report.message.payload.to_h)
end

Sidereal.exceptions.on_retry do |report|
  StatsD.increment('handler.retry', tags: ["command:#{report.message.class.type}"])
end

Backends call into the registry from inside their retry/fail policy:

Sidereal.exceptions.report_retry(exception:, message:, retry_count:, retry_at:)
Sidereal.exceptions.report_failure(exception:, message:, retry_count:)

Sidereal::Dispatcher does this automatically — its dispatch_notification is the only place that calls these methods today. Other dispatchers (Sourced, custom) wire their own retry/fail callbacks the same way.

System notification messages (the default UI publisher)

Sidereal.exceptions ships with a default subscriber pair pre-installed via Sidereal::Exceptions.with_default_publisher. Each one builds the corresponding Sidereal::System::Notify* from the report and publishes it to the session channel of the browser that sent the failed command, or else to the command’s own channel:

  • Sidereal::System::NotifyRetry — payload: command_type, command_id, command_payload, retry_count, retry_at (ISO8601), error_class, error_message, backtrace.
  • Sidereal::System::NotifyFailure — same payload minus retry_at.

Both inherit from Sidereal::System::Notification (a marker base). The default publisher publishes them via Sidereal.pubsub.publish(Sidereal::Exceptions.notification_channel(failed_command), notify). That’s the session channel of the session digest the command carries in its metadata, when a browser sent it, and otherwise Sidereal.channels.for(failed_command). Either way, the channel comes from the failed command, never from the notification, so the app’s channel_name resolvers never see system messages. Sidereal.channels also routes NotifyRetry and NotifyFailure themselves to 'system', so a catch-all resolver doesn’t have to handle them if something else resolves one.

Pages render the toasts via the default reactions in Sidereal::Page:

on Sidereal::System::NotifyFailure do |evt|
  browser.patch_elements Sidereal::Components::SystemNotifyFailure.new(evt),
    mode: 'prepend', selector: '#sidereal-sysnotify-stack'
end

Override on your own page subclass to render a custom UI:

class TodoPage < Sidereal::Page
  on Sidereal::System::NotifyFailure do |evt|
    browser.patch_elements MyErrorBanner.new(evt)
  end
end

Loop prevention

The dispatcher’s report-call site short-circuits when the failing message is itself a Sidereal::System::Notification. So a buggy on_failure subscriber whose own exception cycles back into the worker doesn’t trigger a fresh report-and-fan-out. Subscriber exceptions are also caught by the registry and logged via Console.error — a single broken subscriber never tears down the worker fiber or prevents later subscribers from firing.

Default dev UI: error toasts

The base Sidereal::Page ships with default reactions that render Sidereal::Components::SystemNotifyRetry (amber) or SystemNotifyFailure (red) toasts and prepend them into a fixed-position stack at the top-right of the page (#sidereal-sysnotify-stack, supplied by the base layout’s sidereal_foot). Each toast shows the command type, error class/message, attempt count, retry time, and a collapsible backtrace; they slide in/out, are dismissable, and carry their own inline <style> so they don’t depend on host CSS.

The default reactions fire in any environment for now. Override on(NotifyRetry) / on(NotifyFailure) on your page to render a custom UI, or use Sidereal::Exceptions.new (without the .with_default_publisher factory) and inject it via the dispatcher’s exceptions: kwarg to suppress the publish entirely for headless deployments.

Adding a new system message type

module Sidereal
  module System
    NotifyDeprecated = Notification.define('sidereal.system.notify_deprecated') do
      attribute :command_type, Sidereal::Types::String
      attribute :reason, Sidereal::Types::String
    end
  end
end

Defining via Notification.define(...) registers it under the Notification registry; the dispatcher’s loop prevention keys off is_a?(Notification) and picks up the new class automatically. You’ll still need to:

  • publish it to a channel the pages that should show it subscribe to. Sidereal::Exceptions.notification_channel(command) gives the one the default publisher uses for NotifyRetry/NotifyFailure: the session channel of the browser that sent the command, or else the command’s own;
  • extend Sidereal::Exceptions.build_notification (or register a custom subscriber that handles the new kind) so reports are translated into the new message;
  • add the corresponding Page.on(...) reaction;
  • optionally, add a UI component to render it.

Serialization

A command crosses two boundaries with very different shapes. It goes over the wire to a store or a pub/sub socket, where it must become bytes; and it goes through an HTML form, where every value — a date, a number, a checkbox — is a String in both directions.

Sidereal compiles a codec for each, from your command payload schemas. You never call either one directly. The point is that you declare payload attributes in the types you actually want to work with, and each boundary translates:

Codec Crosses Encodes Compiled by
Sourced::Message::JSONCodec Store::FileSystem file bodies, PubSub::Unix frames the whole message, envelope included Store#start / #append, PubSub#start / #publish
Sidereal::FormsCodec POST /commands params, <input value="..."> the payload alone, every scalar a String App.handle
Page.query_decoder (Plumb::Codec::Forms) page path and query params, the params signal the page’s query_schema, decode only Page.path / Page.query_schema

Both are built on Plumb’s codecs — Plumb::Codec::JSON and Plumb::Codec::Forms — which rewrite a schema into a decoder/encoder pair by resolving an encoder for every leaf type.

Typed payloads from HTML forms

A browser submits seats=30 as the String "30", published as "1", and a date as "2026-09-01". Without a codec you would either coerce by hand in every handler, or write your schemas in lax types and lose the guarantee. Instead, declare what you mean:

BookCourse = Sidereal::Message.define('courses.book') do
  attribute :course_name, Sidereal::Types::String.present
  attribute :seats, Integer
  attribute :starts_on, Date
  attribute :published, Sidereal::Types::Boolean
end

class CoursesApp < Sidereal::App
  handle BookCourse

  command BookCourse do |cmd|
    cmd.payload.seats        # => 30            (Integer)
    cmd.payload.starts_on    # => #<Date 2026-09-01>
    cmd.payload.published    # => true          (TrueClass)

    # so this just works, with nothing parsed by hand
    dispatch Reminder.at(cmd.payload.starts_on - 7) if cmd.payload.published
  end
end

Only commands that are web-facing (via .handle) are form-decoded. A command registered only with command is never reachable from a form and is never compiled.

What Plumb::Codec::Forms knows out of the box:

Attribute type Accepts from a form Renders back as
Types::String any string itself
Types::Integer "30", "-4" "30"
Types::Float / Types::Decimal "1.5", "9.99", "1e3" "1.5"
Types::Boolean "true"/"1", "false"/"0" (case-insensitive) "true" / "false"
Types::Date "2026-09-01" "2026-09-01"
Types::Time ISO 8601 "2026-09-01T10:00:00.000000+01:00"
Types::Symbol any string itself
Types::URI::Generic / ::HTTP / ::File an RFC 3986 URI itself
anything .nullable "", or an absent field ""

Rendering values back into a form

The same translation runs backwards, so command accepts a message instance as well as a class. A class renders a blank form; an instance prefills each field:

# A class — every field renders empty
command BookCourse do |f|
  f.text_field :course_name   # <input type="text" name="command[payload][course_name]">
  f.text_area :description    # <textarea name="command[payload][description]"></textarea>
  f.number_field :seats
  f.date_field :starts_on
  f.check_box :published
end

# An instance — every set attribute renders its encoded value
command BookCourse.new(payload: {
  course_name: 'Ruby 101', seats: 30,
  starts_on: Date.new(2026, 9, 1), published: true
}) do |f|
  f.text_field :course_name   # <input type="text" ... value="Ruby 101">
  f.date_field :starts_on     # <input type="date" ... value="2026-09-01">
  f.check_box :published      # checked
end

An attribute that is unset renders no value attribute at all, so the same form definition serves both cases — including the one in between, a command half-filled from a previous attempt, where the attributes that are set render and the rest come out blank.

The payload is encoded once per render, not once per field, and per key rather than all-or-nothing. That is what lets a blank or partial command render at all: a strict conversion would reject one outright.

Encoding also happens only at an input’s value=. The command object itself keeps its Ruby values, so logic inside the form block sees what you’d expect:

command course_cmd do |f|
  f.date_field :starts_on
  # a real Date and a real boolean — not "2026-09-01" and "1"
  p { "Starts in #{(f.command.payload.starts_on - Date.today).to_i} days" }
  f.check_box :published unless f.command.payload.published
end

check_box renders a hidden 0 alongside the checkbox, because an unchecked box submits nothing at all. Rack keeps the last value for a repeated name, so a checked box sends "1" and an unchecked one "0".

payload_fields carries values the command doesn’t hold — an id from a loop variable, a preset amount. Those are encoded as attributes of that command, so a hidden field and a visible one for the same attribute always agree, and a key the payload doesn’t declare raises rather than rendering an empty input.

When decoding fails, the result is a flat {attribute => message} hash, which POST /commands streams straight back to the offending field over SSE — see Command forms:

command[payload][seats]=lots
# => the "seats" field gets: Must match /\A-?\d+\z/

Two behaviours worth knowing:

  • Types::Integer.default(0) plus a blank input errors. .default fires for an absent key, and "" is a present value that no Integer encoder accepts. Use Types::Integer.nullable for optional numeric fields.
  • A union reports every branch, so a malformed Types::Boolean reads Must match /\Atrue\z/i, Must be equal to 1, ....

Transport

Stores and pub/sub serialize the whole message — envelope included — because a file body or a socket frame has nowhere else to put an id, a created_at or a correlation chain. That is Sidereal.message_codec, shared by Store::FileSystem and PubSub::Unix:

{
  "id": "97b72e83-c27c-4a0f-b8e7-19abbea9f70e",
  "causation_id": "97b72e83-c27c-4a0f-b8e7-19abbea9f70e",
  "correlation_id": "97b72e83-c27c-4a0f-b8e7-19abbea9f70e",
  "created_at": "2026-08-10T19:51:35.032711+01:00",
  "metadata": {},
  "type": "courses.book",
  "payload": {
    "course_name": "Ruby 101",
    "seats": 30,
    "starts_on": "2026-09-01",
    "published": true
  }
}

Note that the two formats disagree, correctly, about the same schema: seats is a JSON number here and the String "30" in a form, and starts_on is an ISO date string in both but a Date at rest in Ruby. Each codec keeps its own compiled pair per message class, which is what makes that possible.

Nothing compiles on first use. Each transport calls compile! when it starts (and again on the first write, since Sidereal.dispatch! from a CLI can append with no dispatcher running), so a schema the format cannot represent fails at boot rather than on the message that happens to carry it.

The web boundary never reads the envelope. A form supplies command[type] and the payload; id, created_at, metadata and the correlation chain are built server-side, so a request cannot date a command into the future and have the store schedule it.

Custom types

Sooner or later a payload carries something neither format knows — a Money, a Coordinate, a domain enum. Declare an Encoder for it and register it on each codec it will cross. Both are separate registries: teaching one does not teach the other.

An encoder is a class declaring Input => Output plus the two conversions. Output is your Ruby type; Input is the shape the format can carry:

# money.rb
Money = Data.define(:cents, :currency) do
  def self.euros(units) = new(cents: units * 100, currency: 'EUR')
  def to_s = "€#{cents / 100}"
end

# A form field is a String and nothing else, so both parts are packed into one.
# The `Types::` form is needed here because it is a *refinement* — String, but
# only strings matching that pattern.
class MoneyFormsEncoder < Plumb::Encoder[
  Plumb::Types::String[/\A\d+ [A-Z]{3}\z/] => Money
]
  def encode(money) = "#{money.cents} #{money.currency}"

  def decode(str)
    cents, currency = str.split
    Money.new(cents: cents.to_i, currency:)
  end
end

# JSON has objects, so the parts can stay addressable on the wire. A plain class
# is enough where no refinement is involved.
class MoneyJSONEncoder < Plumb::Encoder[
  Plumb::Types::Hash[cents: Integer, currency: String] => Money
]
  def encode(money) = { cents: money.cents, currency: money.currency }

  def decode(hash) = Money.new(cents: hash[:cents], currency: hash[:currency])
end

Plumb::Codec::Forms.encoder(MoneyFormsEncoder)
Plumb::Codec::JSON.encoder(MoneyJSONEncoder)

The two need not agree on a shape, and here they deliberately don’t. One Ruby type reaches each wire in the form that wire can carry:

SelectAmount = Sidereal::Message.define('donations.select_amount') do
  attribute :amount, Sidereal::Types::Any[Money]
end
hidden form field     value="3000 EUR"
store file / frame    "amount": { "cents": 3000, "currency": "EUR" }
command handler       Money[cents: 3000, currency: "EUR"]

Rendering and submitting both go through the encoder, so a preset-amount button is just:

command SelectAmount, key: amount.cents do |f|
  f.payload_fields(amount:)   # => <input type="hidden" value="3000 EUR">
  button(type: :submit) { amount.to_s }
end

Register encoders at load time, before any message type is defined. Every compile walks the whole message registry, so a format missing an encoder for a type any message uses cannot compile at all. require the file at the top of your boot sequence — see examples/donations1/money.rb.

If you get it wrong you find out immediately, and the error names the attribute path:

cannot apply Plumb::Codec::Forms[...] (decode) to Booking::Payload:
field `window` (Range[Integer]) matches no encoder and is not covered by its
noop types. Register an encoder for it, or declare it with .noop.

A type can be representable in one format and not the other, and that is fine — it just means the command cannot be web-facing. Types::Range is the built-in example: Plumb::Codec::JSON encodes it as {from:, to:, exclusive:}, while Plumb::Codec::Forms deliberately does not register it, since a single form field has no sensible shape for it. Such a command serializes for transport, and raises at handle if you try to expose it to the browser.

File uploads

Sidereal::Types::UploadedFile is a struct in the shape Rack parses a multipart file field into: filename, type, name, head and a tempfile. Both codecs know it, so a command can take a file like any other attribute, and refine it as any struct:

UploadReport = Sidereal::Message.define('reports.upload') do
  attribute :title, String
  attribute :report, Sidereal::Types::UploadedFile.where(type: 'application/pdf')
end

The form has to post as multipart/form-data for the browser to send the file:

command UploadReport, enctype: 'multipart/form-data' do |f|
  f.text_field :title
  f.file_field :report, accept: 'application/pdf'
end

In the handler, cmd.payload.report.tempfile is the uploaded file. A wrong content type fails decoding like any other constraint, and the error streams back to the field.

Store the file, dispatch a reference

An uploaded file is usually something to keep, not something to send through the async runtime. So handle the upload command in the web request, put the file in storage there, and dispatch a different command that carries only a reference to it:

# Web-only. Only the handle block below ever sees it, so it holds the file.
UploadReport = Sidereal::Message.define('reports.upload') do
  attribute :title, String
  attribute :report, Sidereal::Types::UploadedFile.where(type: 'application/pdf')
end

# What the async runtime processes: a reference to the stored file.
AttachReport = Sidereal::Message.define('reports.attach') do
  attribute :title, String
  attribute :file_id, String
  attribute :filename, String
end

class ReportsApp < Sidereal::App
  handle UploadReport do |cmd|
    report = cmd.payload.report
    file_id = Uploads.put(report)

    dispatch AttachReport, title: cmd.payload.title, file_id:, filename: report.filename
    browser.patch_elements %(<p id="notice">Uploaded #{report.filename}</p>)
  end

  command AttachReport do |cmd|
    File.open(Uploads.path(cmd.payload.file_id)) do |io|
      # parse, index, thumbnail...
    end
  end
end

Uploads stands for whatever storage the app uses. A local directory is enough to start with:

module Uploads
  DIR = File.expand_path('storage/uploads', __dir__)

  # Copies the upload out of its Tempfile, which isn't kept after the request.
  def self.put(file)
    id = "#{SecureRandom.uuid}#{File.extname(file.filename)}"
    FileUtils.mkdir_p(DIR)
    FileUtils.cp(file.tempfile.path, File.join(DIR, id))
    id
  end

  def self.path(id) = File.join(DIR, File.basename(id))
end

The reference can just as well be a URL, for example to an object store like S3. Whatever it is, the process that runs AttachReport has to be able to reach it. A local directory works for Falcon workers on one host. Across hosts, use shared storage.

UploadReport has a handle block that doesn’t dispatch it, and no command block, so it never reaches the store or a worker. AttachReport is the reverse: it has no handle, so the browser can’t submit it directly.

Dispatching the upload itself

UploadReport can also be dispatched as it is. JSON has no file type, so the store or pub/sub frame carries the file’s bytes Base64-encoded, in a data field in place of tempfile. Decoding writes them to a new Tempfile:

"report": { "filename": "report.pdf", "type": "application/pdf", "name": "...", "head": "...", "data": "JVBERi0xLjcK..." }

That puts the whole file into every stored message and pub/sub frame, and each decode writes a new temporary file. It’s fine for small files. For anything larger, store the file and dispatch a reference, as above.

How it works

CleanShot 2026-04-21 at 14 37 31

sequenceDiagram
    participant Browser
    participant App
    participant PubSub
    participant Store
    participant Worker
    participant CommandHandler

    Browser->>App: POST /commands
    App->>App: Check handled_commands registry (404 if not exposed)
    App->>App: Validate command
    App->>Store: Append command (default handler)
    App->>Browser: 200 OK


    loop Worker fibers
        Worker->>Store: Claim next command
        Store->>Worker: Command
        Worker->>CommandHandler: Handle command
        CommandHandler->>Worker: Result(events, commands)
        Worker->>PubSub: Publish events
        Worker->>Store: Append new commands (if any)
    end


    Browser->>App: GET /_updates/<signed token> (SSE)
    App->>PubSub: Subscribe to the page's channels
    PubSub->>App: Event
    App->>App: Page reactions render HTML
    App->>Browser: SSE patch (HTML fragments)
    Browser->>Browser: Datastar morphs DOM

Commands are processed asynchronously by worker fibers. The browser never waits for command handling to complete – it submits the command and receives UI updates via the SSE stream as events are produced.

Using Sourced as a backend

Sourced is an event sourcing library with a persistent SQLite store, partition-aware consumer groups, and a signal-driven dispatcher. Sidereal can use it as a drop-in backend, replacing the in-memory store and dispatcher.

Setup

Require the Sourced integration, declare the database as a component, then apply the integration:

require 'sequel'
require 'sidereal'
require 'sidereal/integrations/sourced'

Sidereal.config.declare('db', Sequel::Database)
Sidereal.config.component!('db') do
  build { Sequel.sqlite('db/app.db') }
  teardown(&:disconnect)
end

# File-system pubsub and elector
Sidereal.config.use_file_system!
# Sourced as message store and dispatcher, on the 'db' component
Sidereal.config.use Sidereal::Integrations::Sourced, db: 'db'

The integration mounts Sourced’s own configuration at sourced, next to Sidereal’s, and wires the two:

  • Store + dispatcher. sidereal.store is an alias of sourced.store, so commands are appended to Sourced’s store. Sidereal Commanders (command / handle) also register as Sourced reactors, so they run on the same runtime alongside your Deciders and Projectors. db: names the app’s component that Sourced keeps its messages in: sourced.db becomes an alias of it, so your classes can inject the same connection, and your component owns it (built in each process, disconnected on shutdown). Without db:, Sourced uses its own in-memory database.
  • Auto-publish to PubSub. Deciders’ emitted events and Projectors’ updates are published to Sidereal’s PubSub automatically (see Auto-publish below), so Pages re-render over SSE with no hand-written bridge code in your reactors.
  • Error toasts / reporting. Sourced’s retry and terminal-failure events are reported to Sidereal.exceptions, so the default error toasts appear and any on_retry / on_failure / on_fatal subscribers (e.g. an APM hook) fire. When Sourced is the dispatcher it owns retry/fail orchestration, so Sidereal’s automatic exception reporting doesn’t run: this bridge is what surfaces failures in the UI. It’s registered on whatever error strategy you implement.
  • Leader-only runtime. The integration defers sourced.dispatcher, makes it the runner’s target and sets sidereal.runner.process to :leader (see Custom backends), so only the elected process runs the Sourced runtime: commanders, deciders and projectors. SQLite serializes writers, so N runtimes on N workers would queue on each other; with one, the other workers serve pages and queries in parallel. Every worker still appends commands (a form post appends from whichever worker served it); it’s the claiming, handling and projecting that runs in one place. When the leader is demoted the dispatcher stops, and the next leader starts its own. Implement sidereal.runner.process as :all after use to run a runtime on every worker again.
  • Cross-process wake-ups. Sourced’s store announces appends through a notifier that its dispatcher listens on, so a worker picks new messages up at once rather than on the next catch-up poll. Sourced’s default notifier is in-process, which a leader-only runtime would defeat: an append on another worker would wait for the poll. The integration implements sourced.notifier with Sidereal::Integrations::Sourced::Notifier, which carries those announcements over Sidereal’s pubsub: with the unix-socket pubsub, an append on any worker wakes the leader immediately. Appends made outside an Async reactor (a rake task calling Sidereal.dispatch!) can’t reach the socket and fall back to the catch-up poll, which stays the safety net in every case.
  • Per-process setup. Every process builds and starts Sourced’s components, not only the leader, since every worker appends: Sourced’s store compiles its codec when the configuration is prepared, and checks its tables when it starts. So message types must be loaded before the app boots. A rake task or console builds the configuration after loading the app (Sidereal.config.build!, which compiles the codec).
  • Tables. Sourced’s tables come from a migration, which Sourced.store.copy_migration_to { 'db/migrations/..._create_sourced_tables.rb' } writes into your migrations directory (see examples/chess/Rakefile), and the store refuses to start without them. To have it create them on start instead, as a demo might, implement sourced.store.install_tables as true after use.

Sourced’s own components are configured through the same root, after use has mounted them:

Sidereal.config.config!('sourced.workers.count') { 4 }
Sidereal.config.config!('sourced.error_strategy') do
  Sourced::ErrorStrategy.new.retry(times: 3, after: 1)
end

Registering reactors

The integration registers the Sourced reactors among the classes that from_dir loads: in a generated app, system/. It’s the same scan that declares the components there (see Configuration), through sourced-component’s on_dir_load, so system/ is scanned once, for both, and a reload updates both. Apply the integration before the directories install, as a config/components/ file is; reactors: false leaves registering to Sourced.register.

  • What’s registered. Each file’s own constant, as Zeitwerk names it (system/todos.rb defines Todos, system/billing/invoices.rb defines Billing::Invoices), if it’s a reactor: a decider, projector or machine, anything with claim_selector and handle_claim, or a group of reactors returned by to_sourced_reactors. Two kinds are skipped: Sidereal commanders, which the integration registers itself, and reactors that handle no messages, such as an abstract ApplicationDecider that the others subclass. Other constants a file defines aren’t looked at.
  • When. As the directories install, ex. on install_dirs! in boot.rb, so before the router is first built. Files there can dep each other’s components whichever loads first, so a reactor can dep a system/ component. A reactor in a directory of system/ can be a component itself, ex. a projector used as a repository, registered as the class with class-level query methods:

    # system/catalog/books.rb
    class Catalog::Books < Sourced::Projector::StateStored
      def self.__install_component(c) = c.declare(Class).config { self }
      def self.titles = ... # query what it projected
    
      # ...
    end
    
    class Shelf
      extend Sidereal::Deps
      dep 'catalog.books' # a `books` reader, which returns Catalog::Books
    end
    
  • Reloading. When the loader reloads and the components are reconfigured, in one change to Sourced.registry, the reactors the last scan found are unregistered and the reloaded classes registered: the router is rebuilt with the dispatcher stopped, recompiling the store’s codec as it is, and Sidereal’s codec is recompiled after, so messages decode into the reloaded message classes, and a message pending during the reload is handled by the old classes or the new ones, never acked unhandled. A file that raises leaves both the components and the registry as they were. A set the registry rejects, such as two reactors with one group_id, leaves the registry as it was, with the components reconfigured. Either prints the directory’s reload warning. Commanders aren’t reloaded: they keep running the code they booted with. The generated boot.rb doesn’t enable reloading.
  • Anywhere else. A reactor no from_dir loads is registered with Sourced.register(Todos), at any time. One registered from config/components/ loads before the system/ components are declared, so it can’t dep one. One the scan also finds is replaced like the others when it reloads.

Multi-process: use_file_system! (cross-process pubsub + file-lock election) is required whenever you run more than one worker, otherwise the in-process pubsub/elector can’t fan SSE updates across processes. If you start multiple workers with the default in-process components, Sidereal refuses to boot with a loud error telling you to add it (see Running with Falcon).

Defining messages and Deciders

With Sourced as the backend, use Sourced messages and Deciders instead of App.command:

# Define messages using Sourced's message class. AddTodo carries todo_id
# because the Decider partitions by it (generate it client-side, or stamp it
# in a `before_command` hook). Partition keys are references, the attributes
# the store indexes messages by: declare them with `reference` (UUIDs are
# references already).
AddTodo = Sourced::Message.define('todos.add') do
  reference :todo_id, String
  attribute :title, String
end

TodoAdded = Sourced::Message.define('todos.added') do
  reference :todo_id, String
  attribute :title, String
end

# Define a Decider (replaces App.command for async processing)
class TodoDecider < Sourced::Decider
  partition_by :todo_id

  command AddTodo => TodoAdded do |state, cmd|
    event TodoAdded, todo_id: cmd.payload.todo_id, title: cmd.payload.title
  end
end

# In system/todo_decider.rb, the integration registers it. Anywhere else:
Sourced.register(TodoDecider)

A command declares the events its block may emit (AddTodo => TodoAdded, a union such as => Added | Rejected, and | nil when it may emit nothing), and a reaction the messages it may dispatch (reaction TodoAdded => NotifyOwner). The Sourced README covers the rest.

You don’t write any publishing code — that’s the auto-publish below.

State machines

A Sourced::Machine is a Decider written as a statechart. You declare its states and, for each one, the commands it accepts and the state each command moves it to. A command the current state doesn’t accept is refused. Each state is an event the machine appends when it enters that state, and a command block can append data events alongside it:

class Order < Sourced::Machine
  partition_by order_id: Sourced::Types::UUID::V4

  PlaceOrder = Command(:place_order) do
    attribute :order_id, Sourced::Types::AutoUUID
    attribute :item, Sourced::Types::String
  end
  Ship = Command(:ship)
  Cancel = Command(:cancel)

  Placed = State(:placed)
  Shipped = State(:shipped)
  Cancelled = State(:cancelled)

  ItemAdded = Event(:item_added) do
    attribute :item, Sourced::Types::String
  end

  context { { items: [] } }

  evolve ItemAdded do |order, evt|
    order[:items] << evt.payload.item
  end

  initial do
    command PlaceOrder => Placed do |_order, cmd|
      event ItemAdded, item: cmd.payload.item
    end
  end

  state Placed do
    command Ship => Shipped
    command Cancel => Cancelled
  end

  final Shipped
  final Cancelled
end

Sourced.register(Order)

The App exposes its commands with handle, like any other. The integration publishes the events a machine appends, its states and its data events, so on Order on a page reloads it whenever the machine moves. Load the machine in the page with Sourced.load, ask it where it is with machine.in?(state), and declare it with with_machine so that only the forms its current state accepts can render:

class OrderPage < Sidereal::Page
  path '/orders/:order_id'

  on Order
  with_machine { @order }

  def self.load(params, _ctx)
    order, = Sourced.load(Order, order_id: params[:order_id])
    new(params[:order_id], order)
  end

  def initialize(order_id, order)
    @order_id = order_id
    @order = order
  end

  def view_template
    div(id: 'order', data: _d.signals(page_key: self.class.page_key, params: { order_id: @order_id }).to_h) do
      p { @order.context[:items].join(', ') }

      if @order.machine.in?(Order::Placed)
        command Order::Ship do |f|
          f.payload_fields(order_id: @order_id)
          button(type: :submit) { 'Ship' }
        end
      end
    end
  end
end

A machine’s commands carry its partition attributes (order_id here) without declaring them. Rendering command Order::Ship while the order is shipped already would raise Sidereal::CommandNotAvailableError, which is why the form is behind in?.

Machines also have parallel and nested regions, done transitions, timers (after) and child machines. The Sourced docs cover everything a machine can do.

Auto-publish

The integration publishes reactor output to Sidereal’s PubSub for you, so Page reactions pick it up and stream DOM updates via SSE. Every path resolves its channel through your App’s channel_name resolver (Sidereal.channels.for(evt)), and a publish/resolver failure is reported to Sidereal.exceptions (it’s terminal — the store already committed).

  • Deciders publish the events they emitted. The integration injects one generic after_sync into every Sourced::Decider subclass, so no per-reactor wiring is needed.
  • Projectors publish a synthetic “projected” signal so Pages know to re-fetch their read model. You don’t define or publish it: the integration wraps the partition_by macro to auto-define a Projected event class (one reference attribute per partition key) and register an after_sync that publishes it after each committed batch:

    class TodosProjector < Sourced::Projector::StateStored
      partition_by :todo_id
      
      evolve TodoDecider::TodoAdded do |state, evt|
        # update the read model...
      end
      
      sync do |context:, **|
        # persist the read model...
      end
    end
    # => auto-defines TodosProjector::Projected (with a `todo_id` attribute),
    #    published after every batch via Sidereal.channels.for.
      
    Sourced.register(TodosProjector)
    

    Pages react on the generated signal:

    on TodosProjector::Projected do |_evt|
      browser.patch_elements load(params)
    end
    

    The signal’s payload is the projector’s full partition tuple, so multi-key partitions work too — partition_by(:student_id, :course_id) yields a two-attribute Projected, and your channel_name resolver routes it just like your domain events.

    The signal is correlated, not synthetic: a batch may hold messages from several causal chains, so the integration publishes one Projected per distinct correlation_type in the batch, each correlated from the last message of that chain. A page that renders command CreateGame, or declares on CreateGame without a block, therefore reloads when the projector commits the batch holding GameCreated, with no reaction written for the signal. An explicit on MyProjector::Projected do |evt| ... end still fires for every batch regardless of root, which is what a lobby listing every game wants.

Sidereal Commanders are unaffected — they’re not Decider/Projector subclasses and keep publishing via their own path, so there’s no double-publish.

Pages and App

Pages and the App work the same way. handle exposes commands to the browser, and Page on reactions respond to the auto-published events:

class TodoPage < Sidereal::Page
  path '/'

  on TodoAdded do |evt|
    browser.patch_elements load(params)
  end

  def self.load(_params, _ctx)
    new(todos: TODOS.values)
  end

  # ...
end

class TodoApp < Sidereal::App
  session secret: ENV.fetch('SESSION_SECRET')

  channel_name { |msg| "todos.#{msg.payload.todo_id}" }

  # Expose AddTodo to the browser — the default handler
  # appends it to the Sourced store for async processing
  handle AddTodo

  page TodoPage
end

Synchronous Sourced handling

You can also process a command synchronously during the HTTP request using Sourced.handle!, which loads history, runs the Decider, appends events, and returns immediately. Unlike the async dispatcher path, handle! does not run the reactor’s after_sync, so the auto-publish doesn’t fire here — publish the returned events yourself if other connected browsers need them:

handle AddTodo do |cmd|
  _cmd, _decider, events = Sourced.handle!(TodoDecider, cmd)
  events.each { |evt| pubsub.publish(Sidereal.channels.for(evt), evt) }
  browser.patch_elements TodoList.new(TODOS.values)
end

Falcon service

The standard Sidereal Falcon environment works with Sourced: it boots Sidereal.config, Sourced’s components included.

#!/usr/bin/env falcon-host
require 'sidereal/falcon/environment'

service "my-app" do
  include Sidereal::Falcon::Environment
  include Falcon::Environment::Rackup

  url "http://localhost:9292"
  count 1
end

Dashboard

sourced-ui has a monitoring dashboard for Sourced apps. It shows the event store and recent messages, the causation tree of each message, your reactors and how they connect, and their consumer groups with offsets and errors. From it you can start, stop and reset consumer groups. It also draws each machine’s statechart. It isn’t on RubyGems yet, so install it from GitHub:

gem 'sourced-ui', github: 'ismasan/sourced-ui'

Sourced::UI::Dashboard is a Rack app that reads from Sourced.store. With the integration applied, that is your app’s store, so the dashboard shows your app’s messages and reactors. Mount it next to your app in config.ru, after the app has booted:

# config.ru
require_relative 'boot'           # loads your app and applies the Sourced integration
require 'sourced/ui/dashboard'

Sourced::UI::Dashboard.configure do |config|
  config.header_links([
    { label: 'back to app', href: '/', url: false }
  ])
end

map '/sourced' do
  run Sourced::UI::Dashboard
end

map '/' do
  run MyApp
end

The dashboard has no authentication of its own, and it can stop and reset consumer groups, so in production mount it behind your app’s authentication. It signs its session cookie with ENV['SOURCED_DASHBOARD_SESSION_SECRET']. Set that to a random string of at least 64 characters in production. See the sourced-ui README for the full set of options and its Phlex components, such as Statechart. examples/sourced_donations and examples/chess both mount it.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rspec to run the tests. You can also run bin/console for an interactive prompt.

Community

Join us in the sidereal tag on the Ruby Users Forum.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ismasan/sidereal.

License

The gem is available as open source under the terms of the MIT License.