Scope and Accounts
Accounts are Spine's tenancy primitive: every runtime record belongs to exactly one account_id, and that column is what both RLS and the app layer use to partition tenants. design_schema.scope is the small lever that decides which account a record belongs to.
The tenancy model
public.accounts— the tenant (organization, workspace, customer). Accounts form a hierarchy throughparent_id; theaccount_pathsclosure table stores every ancestor→descendant pair so subtree lookups are one join. Accounts have noaccount_idof their own — the table is the source of truth.public.people— a person belongs to exactly one account (people.account_id NOT NULL) and holds onerole_id. A person is also a runtime record:type_id,data, and a stampeddesign_schema.- Everything else —
items,threads,messages,links,attachments,watchers,item_progress— carriesaccount_id NOT NULL(or via its parent thread formessages).
How scope decides account_id
admin-data create resolves the record's account in order (.framework/functions/admin-data.ts, create):
- An explicit
account_idin the request body, else the caller'sctx.accountId. - If the record references a parent (
thread_id,target_id,parent_id), the parent'saccount_idwins — and the parent's stampedscopeis inherited into the new record'sdesign_schema. A child can never drift into a different tenant than its parent. design_schema.scopethen applies:'account'(the default) keeps the resolved account;'platform'replaces it with the spine-system account's id.
So scope on a type is really "which tenant owns these records": your account's rows, or the platform's shared rows.
The spine-system account
Seeded by .framework/migrations/011_spine_system_account.sql: accounts.slug = 'spine-system' — "Master system account for internal Spine operations." It is the landing account for scope: 'platform' records. The accounts table itself is the exception to the scoping rule: it has no account_id column for scope to land on, so for accounts records admin-data instead sets parent_id to the creator's account when none is given — new accounts are born as children of the account that created them.
How scope affects queries
- Human callers query through
ctx.db— a Supabase client created with the caller's JWT (.framework/functions/_shared/principal.ts,getPrincipalDb). RLS policies on the tenant tables filter rows toaccount_id IN (SELECT get_accessible_accounts(current_actor_id()))(.framework/migrations/002_core_rls_and_functions.sql).current_actor_id()mapsauth.uid()to thepeoplerow;get_accessible_accountsreturns that person's account plus every descendant viaaccount_paths— a parent-account user sees the whole subtree. If the person holdssystem_admin, the function short-circuits and returns all accounts. - Machine callers (API keys) use
adminDb, which bypasses RLS; tenancy is enforced only by the key'saccount_idandscopes— see Permissions. A*:*key reads every tenant. - Platform-scoped
itemsonspine-systemare readable by every actor: theitems_accessRLS policy has a platform branch (.framework/migrations/076_tenant_isolation.sql), andadmin-datamirrors it inside its explicitaccount_idfilter onlist/stats/getreads so machine principals onadminDbreach them too (.framework/functions/_shared/account-scope.ts). Writes stay confined to the caller's accounts. Platform-scope rows on other tables have no such branch — they remain visible only to the spine-system subtree andsystem_admin.
What is not scoped by account
- Types are global.
public.typesuniqueness is(kind, slug)(.framework/migrations/015_types_kind_slug_unique.sql) — deliberately not per-app or per-account, soresolveTypeRecord(kind, slug).single()lookups stay deterministic and every account shares the same type registry. - Apps are global rows in
public.apps, linked to accounts only throughowner_account_idand theget_account_appsRPC, which filters by the caller's role slugs (.framework/functions/apps.ts;.framework/migrations/013_get_account_apps_role_filter.sql). - Roles are global slugs matched verbatim by the permission engine.
Practical consequences
- Put
scope: 'platform'only on types whose records should exist once for the whole installation (system config, shared registries) — never on tenant data. - Child records inherit the parent's account and scope; you cannot re-tenant a record by pointing it at a parent in another account — the parent's account wins, silently.
- Because types are global, pick type slugs as if they were API names:
kind + slugmust be unique across every app installed in the deployment. - Account-scoped tests and seeds must set
account_idon every record or RLS will make them invisible to human-scoped queries.
Why it exists
Recursive queries on accounts.parent_id are slow and error-prone. A closure table makes queries such as "all descendants of X" and "all ancestors of Y" simple, constant-time lookups.
Its full reasoning lives with its parent — see accounts ## Problem it solves (why 3) and the accounts article.
Related
- Choosing a tenancy model — project-per-client vs accounts-as-tenants, for agencies.