Repository navigation
Closed
Labels
wayfinder:grillingWayfinder ticket: grilling
Description
Question
The tables, the package cut, and how the schema moves, with the door, company and device decisions in hand.
- 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);patchesgainscompany_id,owner_user_id,scope;patch_versions.created_by_api_token_idbecomes the machine token.accounts,api_tokensas they are, andtoken_mintsgo. ➡️ That, names to the spec. - The operator. The bootstrap token with no user behind it: a nullable user on
machine_tokenswith an operator flag, or its own row kind. Consequences:patch_versionsmust reference it; moderation keeps working. ➡️ Nullable user plus operator flag; simplest. - 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.
- Package ownership. Companies owns
companies,users,invitesand their migrations; Auth ownsmachine_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"). - Services. One file each:
Session(Clerk verification),MachineTokens,DeviceLogins,Companies,Users;CurrentIdentitycarries user, company, role and machine. ➡️ That, pereffect-service-conventions. - 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.mdnames. ➡️ A checklist line on the build ticket.
From the door (The login door on a tier 0 page, resolved 2026-09-03)
patches.scopeiscompany | public, defaultcompany, set byupload --shareandpatchy share. No session table: Clerk holds the browser session and the shell keeps it fresh, soSession(question 5) is verification only.userskeys on the Clerk user id; the door resolves a viewer (user, company) from the session'ssubin one read.PATCHY_PUBLIC_BASE_URLloses 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_idisNOT NULL, because the row is born inside create-or-join's transaction; the column isdeactivated_at, notremoved_at, and reactivation clears it.invitesgainscreated_at,revoked_at,consumed_atand the Clerk invitation id (for revoke and resend); no expiry column.companies.handleis 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,invitesand their migrations and exposes the lookup; Auth ownsmachine_tokens,device_logins, session verification and the bearer middleware, and depends on Companies. Question 5'sCurrentIdentityis 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.
Metadata
Metadata
Assignees
Labels
wayfinder:grillingWayfinder ticket: grilling
Activity
allisonmahmood commentedon Sep 4, 2026
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_tokensandtoken_mintsnever 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 ownusr_id,clerk_user_id UNIQUE NOT NULL,company_id NOT NULL,email(stored and matched lowercased, unique),name,role(TEXTwith aCHECKonmember | 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/mesay 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 falsewithCHECK ((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(TEXTwith aCHECKonpending | confirmed | denied),machine_name_hint,old_token_id,user_idandmachine_name(set at confirm),expires_at,created_at. No token column: see minting below.patches:account_idgoes;company_id NOT NULL(indexed) andowner_user_id NOT NULLboth 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(TEXTwith aCHECKoncompany | public, defaultcompany). The retention columns stay until patches: drop expiry, add retire and delete with a recovery window #93.patch_versions.created_by_api_token_idbecomescreated_by_machine_token_id NOT NULL.Every enumerated column is
TEXTplus aCHECK, 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_tokensrow, fixed idtok_operator,user_id NULL,operator = true, seeded idempotently at boot fromPATCHY_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. Staledevice_loginsrows 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 theUserslookup; Auth ownsmachine_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 joiningmachine_tokens,usersandcompanies, the way Patches already joinsapi_tokenstoday; 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.
CurrentIdentityand theIdentityschema stay in@patchy/api, because theAuthorizationmiddleware 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.Identitybecomes the union the device-login ticket fixed (userkind: user, company, role, machine;operatorkind: machine), with the user and company structs shared. Auth exports aViewervalue 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) andUsers(lookup by Clerk id, roles, deactivate and reactivate); in Auth,Session(Clerk verification and the handshake, no state),MachineTokens(authenticate, mint, revoke, the hourlylast_used_atstamp) andDeviceLogins(start, confirm, deny, poll).The migration spread (6). The migrator is called in four places, not the three
AGENTS.mdnames:apps/server/src/Server.ts,scripts/dev/src/supervisor.ts,test/postgres.tsandapps/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 inAGENTS.md.Carried to the other tickets
operator = false; the operator token isPATCHY_BOOTSTRAP_API_TOKENas today and publishes nothing, so the packed-CLI e2e andpnpm seed:devpublish as the seeded user.AGENTS.mdcorrection; Operator token replacing Bootstrap principal in Auth's glossary; the wire'sIdentityunion and Auth'sViewer; the context-map line for Serving.allisonmahmood commentedon Sep 4, 2026
Resolved above; decision indexed on the auth map.
allisonmahmood commentedon Sep 4, 2026
Addendum, 2026-09-04. The operator-as-token parts of this resolution (the
operatorkind of/api/me, thetok_operatorrow withuser_id NULLand theoperatorflag,PATCHY_OPERATOR_TOKENin 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.allisonmahmood commentedon Sep 4, 2026
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
operatorkind of/api/me(one shape now),tok_operator, the nullableuser_idandoperatorflag (user_id NOT NULL),PATCHY_BOOTSTRAP_API_TOKENandPATCHY_OPERATOR_TOKEN, theadminscope, and the moderation and pin routes.revokestays only as the bearer revoking itself. The rest stands.