URL: /docs/architecture

---
title: Architecture
description: Process model, storage, migrations and the tenancy model.
---

## Process model

Turntable is one process. `turntable serve` opens the database, applies migrations, then starts an HTTP server. Depending on `--roles`, the same process also runs the poller, workers (scheduler, log pipeline, provider supervisor) and the realtime feed. Signing and publishing run in a separate release process, see [Release lane](/docs/release-lane). Sandboxes are separate, disposable environments created by providers, one per job.

## Operations registry

Every capability is defined once in an operations registry. The MCP server, REST API, CLI and UI all sit on that single layer, so no surface has capabilities the others lack. See [Agent interface](/docs/agent-interface).

```mermaid
flowchart TD
  MCP[MCP server] --> R
  REST[REST + OpenAPI] --> R
  CLI[CLI] --> R
  UI[Web UI] --> R
  R[Operations registry] --> Core[Core: scheduler, repos, providers]
  Core --> DB[(SQLite / Postgres)]
```

## Storage

SQLite is the default and lives at `<data-dir>/turntable.db`. Postgres is not built yet. Access goes through Drizzle ORM, and the schema uses portable column types (ULID text ids, epoch-ms integers, text plus CHECK for enums, no arrays) so the Postgres port stays mechanical.

## Embedded migrations

The compiled binary must work from an empty directory, so it never reads assets from disk. drizzle-kit generates SQL migrations, then `packages/core/scripts/gen-migrations.ts` inlines them into `src/db/migrations.generated.ts`. A small dialect-agnostic runner applies them on boot. CI fails if the embedded module is stale.

## Tenancy model

One Turntable instance serves many orgs. An org is the tenant. Isolation is enforced in the data layer, not by convention.

### Entities

- **Org**: the tenant boundary. Has a unique slug.
- **Project**: belongs to one org, slug unique per org. Runs, jobs and logs hang off a project.
- **Project repo**: binds a project to a forge repo (today: GitHub, optionally with an installation id). A `(forge, owner, repo)` triple belongs to exactly one project instance-wide, so webhook routing is unambiguous. Owner and repo are stored lowercase (forges treat them case-insensitively). `forge_repo_id` holds the forge's stable numeric id, unique per forge when set. Webhook routing should prefer it when present, because names change on rename and transfer.
- **User**: global identity. A person can belong to several orgs.
- **Org member**: links a user to an org with a role: `owner`, `admin`, `member` or `viewer`.
- **Local tenant**: `turntable run` uses a built-in org `local` with project `local`, created idempotently by `ensureLocalTenant`.

### Runs, jobs, attempts, logs

- A **run** is one workflow execution for a project. A **job** is one job of that run. A **job attempt** is one try of a job in one sandbox. **Log chunks** are byte-addressed per attempt, so readers resume by offset.
- Job states: `queued`, `running`, `success`, `failure`, `infra_error`, `awaiting_approval`, `cancelled`.
  - `failure` is the job's own non-zero exit.
  - `infra_error` means no trustworthy result: sandbox create failed, provider error, exit code unrecoverable, infra timeout.
  - `success`, `failure` and `cancelled` are terminal. `infra_error` can return to `queued` for a retry, which creates a new attempt.

### Isolation guarantees

1. Every tenant-owned table has an `org_id` column. A test fails if a new table lacks one and is not on the explicit allowlist (`instance`, `orgs`, `users`).
2. All access goes through `packages/core/src/db/repos`. Each function takes a `Scope` (`{ orgId, projectId? }`) and always filters by it. A lookup by id in another org returns nothing, and writes affect nothing.
3. A source-guard test fails if code outside the repos module references a tenant table.
4. Composite foreign keys such as `(run_id, org_id, project_id)` make the database reject a child row whose org or project differs from its parent's, even if application code is wrong. Jobs, attempts and log chunks all carry `project_id`, so a project-narrowed scope cannot reach a sibling project's data in the same org.
5. Portable column types keep a Postgres port mechanical.

### Scope of the guarantee

These rules cover the database layer. The same org boundary applies to secrets, caches, artifacts and sandboxes: none of them is shared across orgs. [Tenant isolation](/docs/tenant-isolation) lists the tests and which keys are per org. Webhook routing (`resolveRepoScope`) is the one deliberately unscoped lookup, because the caller has no tenant yet. When a binding has an `installation_id`, the incoming installation must match it, so a same-named repo under another installation cannot route into the bound org.
