Skip to content

Data model, packages and migration #123

Closed

Description

@allisonmahmood

Question

The tables, the package cut, and how the schema moves, with the door, company and device decisions in hand.

  1. Tables. companies (id, handle, name); users (id, clerk user id, company, email, name, role, removed_at); invites (company, email, role, by whom); machine_tokens (id, user, name, hash, created, expires_at, last_used_at, revoked_at); device_logins (device code hash, user code, state, machine name, token once confirmed, expiry); patches gains company_id, owner_user_id, scope; patch_versions.created_by_api_token_id becomes the machine token. accounts, api_tokens as they are, and token_mints go. ➡️ That, names to the spec.
  2. The operator. The bootstrap token with no user behind it: a nullable user on machine_tokens with an operator flag, or its own row kind. Consequences: patch_versions must reference it; moderation keeps working. ➡️ Nullable user plus operator flag; simplest.
  3. Rewrite or migrate. Nothing is deployed, so rewrite the baselines under new ids, as ADR-0003 did — or write forward migrations against the current baseline. ➡️ Rewrite; confirm nothing is deployed first.
  4. Package ownership. Companies owns companies, users, invites and their migrations; Auth owns machine_tokens, device_logins, the session and the bearer middleware; Serving owns the door; Patches keys ownership on users. CONTEXT-MAP.md's "Patches never imports Auth" and "Serving never imports Auth" lines: which stand. ➡️ Patches still receives the principal from middleware; Serving imports Auth for the door, as the map already promised ("the login door asks Auth who opened the page").
  5. Services. One file each: Session (Clerk verification), MachineTokens, DeviceLogins, Companies, Users; CurrentIdentity carries user, company, role and machine. ➡️ That, per effect-service-conventions.
  6. The migration spread. Companies' first migration record lands in the server's run, the dev runner's and the vitest template's — the three places AGENTS.md names. ➡️ A checklist line on the build ticket.

From the door (The login door on a tier 0 page, resolved 2026-09-03)

  • patches.scope is company | public, default company, set by upload --share and patchy share. No session table: Clerk holds the browser session and the shell keeps it fresh, so Session (question 5) is verification only.
  • users keys on the Clerk user id; the door resolves a viewer (user, company) from the session's sub in one read.
  • PATCHY_PUBLIC_BASE_URL loses its default and the Clerk keys are required at startup; both are config, not schema, but the spec's config table lists them as required.

From company and user (Company and user: the minimum that ships, and where the company lives, resolved 2026-09-03)

  • Question 1 sharpens: users.company_id is NOT NULL, because the row is born inside create-or-join's transaction; the column is deactivated_at, not removed_at, and reactivation clears it. invites gains created_at, revoked_at, consumed_at and the Clerk invitation id (for revoke and resend); no expiry column. companies.handle is unique with the format rule (lowercase letters, digits, hyphens, 3–32, no leading or trailing hyphen) and the reserved list enforced in code.
  • Question 4 is answered: the arrow reverses. Companies owns companies, users, invites and their migrations and exposes the lookup; Auth owns machine_tokens, device_logins, session verification and the bearer middleware, and depends on Companies. Question 5's CurrentIdentity is Auth's value, built from Companies' lookup; Serving and Patches import Auth only.
  • Each package serves its own first-party pages: create-or-join and the company page in Companies, Your machines in Auth, the door in Serving.

Activity

  1. added
    wayfinder:grilling
    on Sep 2, 2026
  2. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Resolution (2026-09-04)

    One grilling round with Allison, on top of what the door, company and device-login tickets had already fixed. The tables, the package cut and how the schema moves, in one place for the spec.

    Rewrite, not migrate (3). Nothing is deployed, confirmed by Allison. The history is rewritten as ADR-0003 did: three baselines in dependency order, 0001_companies_baseline (companies, users, invites), 0002_auth_baseline (machine_tokens, device_logins), 0003_patches_baseline (patches, patch_versions). accounts, api_tokens and token_mints never exist in the new history.

    Tables (1).

    • companies: id, handle (unique; lowercase letters, digits, hyphens, 3–32, no leading or trailing hyphen; the reserved list enforced in code), name, created_at.
    • users: Patchy's own usr_ id, clerk_user_id UNIQUE NOT NULL, company_id NOT NULL, email (stored and matched lowercased, unique), name, role (TEXT with a CHECK on member | admin), created_at, deactivated_at. The row is born in create-or-join's transaction, so a user always has a company. Email and name are refreshed from the session claims whenever the door or a first-party page resolves the viewer and the claims differ: a compare on every doored load, a write only on change, so the company page and /api/me say what the person's browser last said.
    • invites: company_id, email (lowercased), role (default member), invited_by, clerk_invitation_id (for revoke and resend), created_at, revoked_at, consumed_at; no expiry column.
    • machine_tokens: id, user_id (nullable for the operator only), operator BOOLEAN NOT NULL DEFAULT false with CHECK ((user_id IS NULL) = operator), name, token_hash UNIQUE, created_at, expires_at, last_used_at, revoked_at.
    • device_logins: user_code (the lookup key, unique while live), device_code_hash, state (TEXT with a CHECK on pending | confirmed | denied), machine_name_hint, old_token_id, user_id and machine_name (set at confirm), expires_at, created_at. No token column: see minting below.
    • patches: account_id goes; company_id NOT NULL (indexed) and owner_user_id NOT NULL both present, so the door's compare and the admin's company-wide view are a single-column read and a later reassignment cannot move a patch across companies; scope (TEXT with a CHECK on company | public, default company). The retention columns stay until patches: drop expiry, add retire and delete with a recovery window #93.
    • patch_versions.created_by_api_token_id becomes created_by_machine_token_id NOT NULL.

    Every enumerated column is TEXT plus a CHECK, never a Postgres enum type, because adding a value to a check is a one-line rewrite and to an enum a locking migration.

    The operator (2). The operator is Patchy running the platform, never a user: one machine_tokens row, fixed id tok_operator, user_id NULL, operator = true, seeded idempotently at boot from PATCHY_BOOTSTRAP_API_TOKEN, whose name stays. The operator cannot publish. Its token moderates, pins, revokes and reads; the upload route refuses an operator bearer with 403. Patchy using Patchy means a company and users like everyone else; the dev seed's token becomes the seeded admin user's machine token, and no column is nullable for one caller. The glossary term Bootstrap principal becomes Operator token, defined by the flag.

    Minting happens at the poll, not at confirm. Confirm records the user, the machine name and the old key's id and sets state = confirmed. The poll that finds that state mints the machine token, revokes the old key and deletes the row in one transaction, and returns the plaintext from memory. No plaintext ever sits in a row; an abandoned confirm mints nothing and the old machine keeps working. This moves the old-key revocation out of confirm's transaction as written on Device login: the protocol, and the machine token's columns; an addendum there points here. Stale device_logins rows are deleted on write: every new start deletes rows past their expiry, so there is no second background job.

    Package ownership (4), as the company ticket decided. Companies owns companies, users, invites, their baseline and the Users lookup; Auth owns machine_tokens, device_logins, their baseline, session verification, device login and the bearer middleware, and depends on Companies; Serving imports Auth for the door; Patches keys ownership on users and still imports neither. Cross-package reads: the bearer lookup, on every API request, is one SQL statement joining machine_tokens, users and companies, the way Patches already joins api_tokens today; the door's viewer resolves through Companies' Users.findByClerkId. "Auth depends on Companies" means Companies owns the DDL and the service Auth calls for create-or-join and the viewer, not that every read hops a service.

    Identity's home. CurrentIdentity and the Identity schema stay in @patchy/api, because the Authorization middleware must be declared there for HttpApi to require it on endpoints, and Auth already imports the api package, so moving the tag into Auth would make the two circular. Identity becomes the union the device-login ticket fixed (user kind: user, company, role, machine; operator kind: machine), with the user and company structs shared. Auth exports a Viewer value for Serving: the user kind minus the machine, built from a Clerk session through Companies' lookup. The context map's "Patches never imports Auth" line stands; Serving's changes to "Serving imports Auth for the door".

    Services (5). One file each, per effect-service-conventions: in Companies, Companies (create-or-join, the handle rule, invites) and Users (lookup by Clerk id, roles, deactivate and reactivate); in Auth, Session (Clerk verification and the handshake, no state), MachineTokens (authenticate, mint, revoke, the hourly last_used_at stamp) and DeviceLogins (start, confirm, deny, poll).

    The migration spread (6). The migrator is called in four places, not the three AGENTS.md names: apps/server/src/Server.ts, scripts/dev/src/supervisor.ts, test/postgres.ts and apps/server/src/test/server.ts. The build ticket that adds the Companies baseline carries all four as a checklist, and the same PR corrects the count in AGENTS.md.

    Carried to the other tickets

    • Dev loop (The dev loop and tests with Clerk): the seeded token is the seeded admin user's machine token, operator = false; the operator token is PATCHY_BOOTSTRAP_API_TOKEN as today and publishes nothing, so the packed-CLI e2e and pnpm seed:dev publish as the seeded user.
    • Spec (Write the auth spec and file the build tickets): the three baselines above, verbatim; the 403 for an operator upload; the four migrator call sites and the AGENTS.md correction; Operator token replacing Bootstrap principal in Auth's glossary; the wire's Identity union and Auth's Viewer; the context-map line for Serving.
  3. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Resolved above; decision indexed on the auth map.

  4. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Addendum, 2026-09-04. The operator-as-token parts of this resolution (the operator kind of /api/me, the tok_operator row with user_id NULL and the operator flag, PATCHY_OPERATOR_TOKEN in the dev loop) are reopened on The operator is a company, not a token. Read that ticket's resolution over these lines; the rest of this resolution stands.

  5. allisonmahmood commented on Sep 4, 2026

    @allisonmahmood
    OwnerAuthor

    Addendum, 2026-09-04. Resolved on The operator is Patchy, not a token: the operator is Patchy with its own later login and dashboards, so every operator-as-token part of this resolution is gone — the operator kind of /api/me (one shape now), tok_operator, the nullable user_id and operator flag (user_id NOT NULL), PATCHY_BOOTSTRAP_API_TOKEN and PATCHY_OPERATOR_TOKEN, the admin scope, and the moderation and pin routes. revoke stays only as the bearer revoking itself. The rest stands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

wayfinder:grilling

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions