Skip to content

Architecture

Package boundaries and the guarantees to preserve.

Applications depend on domain packages

Code

apps/web                  Next App Router, auth, UI, server actions
  ├─ auth + permissions   Identity and current workspace authority
  ├─ billing              Subscriptions, paid-state reconciliation, credit ledger
  ├─ agents               Threads, plans, runs, approvals, bounded delegation
  │   ├─ ai               OpenRouter adapter, model allowlist, normalized usage
  │   └─ integrations     Connected accounts, tools, durable external intent
  ├─ email                Encrypted transactional outbox and Resend
  ├─ entitlements + usage Product capabilities and idempotent measurements
  └─ db + env             PostgreSQL, migrations, typed server configuration
packages/ui               shadcn primitives and Tailwind tokens; no domain data

Domain code lives outside framework routes. Server-only exports prevent database and secret-bearing modules from entering browser bundles. Permissions accepts an identity from an authenticated caller and never imports auth. Static pages and builds do not connect to a database just by importing modules.

Identity and authority

Better Auth stores database sessions and email/password credentials. A database trigger creates each personal workspace and owner membership in the user transaction. Organizations use the custom transactional workspace service, not Better Auth’s separate creation writes. Current persisted membership and role authorize each operation; the active-workspace session value is a navigation convenience.

Database invariants protect personal memberships and the last organization owner, including concurrent mutations. Invitations require verified recipient ownership, a current authorized inviter, expiry, and member capacity. Do not bypass these rules with UI-only guards or direct unreviewed table writes.

Commercial SaaS primitives

Stripe reconciliation verifies raw-body signatures and retrieves authoritative current subscription/invoice state under workspace locks. Browser returns do not grant access. Customer and checkout intents are durable so ambiguous provider failures are not blindly retried.

Credits use immutable integer ledger operations and lots. Reservations bound agent spend; grants, settlement, refunds, and allowances have idempotency keys and fingerprints. Usage measurements are separate from billing. Platform administration is a persisted grant, not an owner shortcut.

Agent execution and external actions

The kernel stores threads, messages, versioned agent definitions, steps, plans, leases, and budgets. Plans and action approvals are separate. Sensitive approval binds the saved intent, version/fingerprint, current authority, and expiry. An agent cannot infer permission from conversation text.

External mutations commit durable intent before provider dispatch. They retain a stable key and classified attempts. Outcomes distinguish success, definite nonexecution, and ambiguity. Ambiguous results enter reconciliation_required; replay requires evidence and the existing policy. Cancellation fences unfinished work; it does not claim to undo an already dispatched external action.

Signal is a bounded reference orchestration graph, not a separate runtime. General handles direct tasks; Signal delegates only within its fixed specialists and budgets. There is no recursive workflow builder.

Operational behavior

Encrypted email is queued transactionally and delivered outside user responses. Recovery endpoints claim fenced leases and persist safe job results. Expired/uncertain remote actions are not automatically redispatched. Traces omit hidden reasoning, credentials, raw provider objects, and sensitive tool payloads; richer sources are bounded by visibility rules.

Inspect packages/db/drizzle for the complete migration history. Preserve custom triggers and previous versioned definitions. Build your business logic above these contracts rather than replacing them to get a first demo working.

Explore in context