Skip to content

Module anatomy ​

A module is one business area, such as leave or payroll. It owns one Postgres schema, its handlers, its Cedar policies and a manifest. The doors (GraphQL, MCP, CLI) are generated from what it registers: a module writes no door code.

Start from hello

modules/hello is the smallest complete module and the step-by-step guide for writing one.

Layout ​

modules/<id>/
├─ Cargo.toml        package orbit-<id>, behind a cargo feature named <id>
├─ module.toml       the manifest
├─ policies.cedar    who may attempt each action, and on which records
├─ migrations/       NNNN_name.up.sql and .down.sql
├─ src/lib.rs        ID, module(), subscribe(), schedules
├─ src/domain.rs     ID newtypes, domain types, pure rules (no SQL)
├─ src/commands.rs   command handlers
├─ src/queries.rs    query handlers
├─ src/events.rs     event names, payloads, subscribers
└─ tests/            integration, allow/deny and golden-vector tests

The manifest ​

module.toml declares everything the module does. The registry refuses a handler the manifest doesn't declare, and a declared name with no handler.

toml
[module]
id = "leave"
requires = ["core@^0.1"]
licence = "AGPL-3.0-only"

[provides]
commands = ["leave.apply", "leave.approve", "leave.reject"]
queries  = ["leave.balance", "leave.applications"]
events   = ["leave.approval.requested", "leave.application.approved"]

[consumes]
events = ["core.employee.joined", "core.employee.left"]

[ui]
routes = ["/me/leave", "/me/leave/new"]

[mcp]
tools     = ["leave.balance", "leave.apply", "leave.approve"]
sensitive = ["leave.approve"]   # runs only after the person approves the exact call

(Shortened from the real modules/leave/module.toml.)

Migrations ​

  • The first migration creates the module's schema.
  • Every tenant-owned table has tenant_id and, right after it is created, row-level security and grants to the module's own role only:
    sql
    SELECT kernel.enable_tenant_rls('leave.applications');
    SELECT kernel.grant_module('leave.applications', 'leave', ARRAY['SELECT', 'INSERT', 'UPDATE']);
  • Migrations run once for the whole database in orbit migrate, never in a serving process (ADR 0004). Enabling a module for a tenant only flips its flag; disabling never deletes data.

Handlers ​

A command handler is a domain function plus repository calls:

rust
async fn handle(ctx: &Ctx, cmd: ApplyLeave) -> Result<LeaveApplication, DomainError>
  • Ctx carries the tenant, the actor (person, API client or agent), the door, the request id and the open transaction.
  • A handler that acts on a record calls ctx.authorize on it; the bus refuses to commit otherwise.
  • State changes and their events go in the same transaction (the outbox). Subscribers must be idempotent: delivery is at least once.
  • Errors are a typed DomainError, mapped once at the door to GraphQL codes, MCP errors and CLI exit codes.

Policies ​

Every command and query has a Cedar action in policies.cedar, with tests that allow and deny. Duties such as payroll_admin or finance are grants the module stores and hands to Cedar as module roles (ADR 0008).

What you get for free ​

DoorGenerated from
GraphQLone mutation per command, one query field per query
MCPone tool per entry in [mcp] tools, with confirm for sensitive ones
CLIone orbit <module> <verb> per command and query, with JSON output

The committed snapshots (crates/orbit-bin/schema.all-modules.graphql, mcp-tools.all-modules.json, cli-help.all-modules.txt) change with every new action, and tests fail until they are updated.

Personal data and files ​

  • Seal personal fields with orbit-pii; they are encrypted, redacted in logs and revealed only by a Cedar field check (Security model).
  • Accept files only through the files door, POST /files/<module>/<purpose> (ADR 0009).

Checklist for a new module ​

  1. Manifest, data model, commands, events and the Cedar allow/deny matrix.
  2. Failing integration and authz tests, including a cross-tenant leakage test.
  3. Migrations, domain, handlers and policies.
  4. Updated GraphQL, MCP and CLI snapshots.
  5. Web routes and pages, with loading, empty and error states, in light and dark mode.
  6. A user guide page with a screenshot (Contributing).

Orbit by SkySync Tech. Licensed under AGPL-3.0.