Repository navigation
fix(auth): Disconnect everything and deactivation end device logins not yet collected - #867
Conversation
4a80c94 to
abd5c2f
Compare
…vice logins Each user now records when their credentials last all ended (users.credentials_ended_at, migration 0019). Deactivation and revoking every machine set it in their transaction; reactivation keeps it. Device logins record when they were confirmed, and the terminal's poll, holding the user lock the endings take, never mints from a confirmation at or before the stamp. The stack's agent connections and consents will honour the same stamp, so credentials stay ended whichever release ended them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Servers' clocks can disagree by more than the gap between a confirmation and an ending, so a confirmation committed before Disconnect everything could carry a later time than its stamp and still mint. Confirmation now takes the user lock the endings take, and both read the database's clock under it; the stamp never moves back. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A confirmation from the release before confirmed_at has only its start, on the app server's clock, which the database's stamp can't be compared with. Once the person's credentials have ended, it never mints. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…recision JavaScript dates keep milliseconds, so a confirmation in the same millisecond as an ending but after it was cancelled. The poll now asks Postgres, under the same user lock. Co-Authored-By: Claude Opus 5.5 <[email protected]>
a43b15c to
9e756fe
Compare
Review: GPT-6.1-Sol (xhigh), three roundsRound 1 (at the first commit)
Round 2
Round 3
Confirmed across rounds: one lock order (login, then user; endings take only the user), no deadlock path, migration 0019 additive and rollback-compatible, surfaces (glossaries, ADR-0008, product doc, vitest template) complete. |
Acceptance: PASS with notesChecked Done means
Golden journeys
Risk checks and evidence The focused suites ran serially against isolated real Postgres: These include both endings with server-clock skew, Postgres sub-millisecond precision, the poll/revoke race, repeated endings, transaction rollback and existing token contracts. Additional direct checks on the fresh instance passed: Migration 0019 is next after base's 0018; the migration-record test passes. The shared vitest template consumes the composed server migration record. ADR-0008, product documentation and both glossaries match the behavior. No wire, CLI, runner, company-resource locking, repo-layout or rendered-page change requires another mirror. Rollback answers: match the diff. The two nullable columns and their stamps remain; the previous release can continue using its named columns. No Not run / not yet complete
Notes
|
…874) ## What changed Release 16's What's new entry, written from `dd3f41f`. It covers everything merged since release 15's `through` (`9e4b0df`), and sets the `patchy` release to `0.16.0` (`packages/patchy/package.json` and `CURRENT_RELEASE`). | Change | PRs | | --- | --- | | Fixed: Revoke all machines stops logins still on their way | #867 | | Behind the scenes: `mcp` and `oauth` kept off company addresses | #863 | | Behind the scenes: read-only actions and agent tools understood and enforced before anything writes them | #868 | | Behind the scenes: logs record how long a tool's server takes to start | #865 | #814 is release 15's own entry, so it's left out. #818 and #831 are docs (Patchy is live; the Patchy MCP spec) and #864 is dev tooling, so none of them gets a line. ## Look at - **The entry**, at the top of `releases` in `packages/core/src/whatsNew.ts`. #867 is the only change a person could notice. The page's button is still labelled "Revoke all machines" (Disconnect everything is the spec's future name), so the entry uses that label. - **Rollback safety, from the live commit `d77baaf` to `dd3f41f`.** No PR in the range is labelled `one-way`, and every PR answers both Release impact questions with "Yes". - #867 is the only migration: `0019_credentials_ended` adds two nullable columns, `users.credentials_ended_at` and `device_logins.confirmed_at`. At `d77baaf`, every insert into `users` and `device_logins` lists its columns, and nothing reads either table with `SELECT *`. - `rollback-rehearsal` passed on #863, #865, #867 and #868. #868's run covered #867's migration already on main: `Exercised rollback paths passed: d77baaf → ffe9988 → d77baaf`. - A rollback would reopen what #867 closes: an approved login that hasn't finished could again get past Revoke all machines. It would also let new companies take the `mcp` and `oauth` addresses (#863). No release that writes read-only actions or agent tools has shipped, so going back past #868 leaves nothing behind. ## How it was verified - `vitest run packages/core/src/whatsNew.test.ts packages/api/src/schemas.test.ts`: 14 passed. - `pnpm typecheck`: 23 of 23 tasks pass. Prettier and eslint are clean on the changed files. - The live commit was pinned from `/healthz/deep` (`d77baaf`, release 15). The Deploy run updated last succeeded, and none is unfinished. ## Release impact - [ ] **Breaking** — requires action from anyone already running this - [x] **Not breaking** — no action required The changelog text plus the `patchy` version bump. A CLI installed before this deploy is told to upgrade, as with every release. - **What does a revert leave behind?** Nothing. People whose seen marker reached 16 would just see no new release. - **Does the release before it still work with that?** Yes. If this release misbehaves, roll back to `d77baafcf10c7cdf28c71bd65ea1a0009aa99ced` (release 15). Built by Claude Opus 5.5 (xhigh) in Claude Code on T3 Code. Co-authored-by: Claude Opus 5.5 <[email protected]>
) Stacked on #883 (ticket #851); merges with the rest of #833's stack, not alone. ## What changed A person's agent can now find their company's patches through Patchy's MCP and open one to see its agent tools. `find_patches` lists exactly the patches the person can open in the portal; `open_patch` answers one patch's own document. While the company's agent access is off, every tool call is refused and names the admins. Closes #852 ## Look at 1. **Visibility reuses the portal's rule.** `Catalogue.visible` is `Patches.read({ state: "live" })` through `Patches.Openability`, the same call the portal's All patches makes, then one new query, `Patches.agentToolSummaries`, for the current versions' tool marks and words. It doesn't load whole manifests and starts nothing. `apps/server/src/McpDoor.test.ts` checks it against the portal's own rendered list. It publishes through the API across sharing scopes (company, public), lifecycle states (live, retired, deleted, operator-disabled), a deactivated owner and a second company. Then it compares `/patches` with `find_patches` for an owning admin, a non-owning admin, a member and a member who owns only off patches. Every listed patch opens. Every other one, by name, id or address, gets the same refusal, and searching its name finds nothing. Making the door list off patches fails it, and four catalogue tests. 2. **Choices within the spec's words**, for Allison to see: - `search` matches when every word appears in the name, title, description or owner's name, ignoring case. - The cursor is base64url JSON `{ after: { tools, name, id }, search?, detail? }`: a keyset position, so a publish or retire between pages never repeats or skips one. It carries its search and detail, so `{ cursor }` alone continues a walk; a cursor sent with a different search or detail is refused `invalid_args`. A forged cursor only positions within the person's own list: pointing it at a hidden patch, another company's, or none gives the same page. - `patch` resolves an id first (ids never change), then a name through `Patches.resolveName`, so a former name follows its rename as the address does. An address must have this instance's origin and the person's company handle. Every miss is one `not_found` in the same words. - `actingFor` is `{ person, company }`, each cut at 200 characters. A find page is measured as it grows, so its compact JSON never passes 100,000 characters; a patch whose tools alone would pass it (only a version that skipped the publish check could) is listed with their names, or without them. `open_patch` refuses `too_large` past 64 KiB, which only a version that skipped the publish check could reach. - The `agent_access_off` refusal names where an admin turns it on and the active admins by name, nothing else. 3. **Events** gain `patchRef`, `catalogueTotal`, `catalogueListed`, `catalogueEndedEarly`, `catalogueSearched`, `catalogueDetail` and `catalogueContinued`, beside `patchId`, `versionId` and `resultBytes`. Never the search words or the cursor. ## How it was verified - `packages/mcp`: `Catalogue.test.ts` covers a narrower openability rule, order, 55 patches over two pages with a publish and a retire between them, the read cap with `detail: "tools"`, search, cursor tampering, bad arguments, resolution by name/id/address/former name, other instances' and companies' addresses, and the 64 KiB cap. `Door.test.ts` covers the agent access refusal for a member and an admin, with discovery and the tool list still working. The shared harness moved to `src/test/door.ts`. - `packages/patches`: `agentToolSummaries` by version. `packages/api`: summaries keep `open_patch`'s order. - `pnpm verify` on 90262f1: format, lint, typecheck, synth, postgres-concurrency, cli-smoke, tier2-smoke and browser passed. In `test`, 2237 of 2239 passed. The two that timed out under the machine's load are in packages this PR doesn't touch: `cliRepoBuild.test.ts` (30 s) and `SdkApi.test.ts` (5 s). Both pass alone, and in CI. Every CI check is green on 90262f1. clerk-live passed on a rerun: its two live tests had passed and only the cleanup sweep got a Clerk 404. - No migration, so `rollback-rehearsal` has nothing to rehearse. #837 (PR #868) and #838 (PR #867) are both on `main`. - Real clients against a dev instance, on the final head: Claude Code 2.1.308 through real consent, and Codex 0.163.0 with a minted token on both its revisions. Each found Ben's 56 patches over two pages (50 + 6), with `deal-desk` first as the one with agent tools, and opened it, by name and by address. With agent access off, each call was refused naming Ada Park, the admin. <details><summary>Real clients against a dev instance, transcripts</summary> A `pnpm dev up team` environment (Acme Co: Ada Park, admin; Ben Ortiz, member) with `deal-desk`, a tier 2 patch whose `agent/deals.ts` exports `listDeals` and `moveDeal`, and 55 static pages, `standup-week-01` to `-55`, all published by Ada. Every client ran as Ben from a temp dir outside `$HOME`, with only that invocation's MCP config. Tool results are cut to 220 characters here. ### Claude Code 2.1.308 through real consent `claude --setting-sources project --strict-mcp-config --mcp-config ./mcp.json`, then `/mcp` › Authenticate. A stub `xdg-open` on its `PATH` recorded the authorize URL instead of opening a browser. A headless Playwright browser signed in as Ben (`/dev/[email protected]&return=<authorize>`), saw the consent page below and pressed Allow: ~~~text consent <instance>/oauth/authorize — Connect Claude Code to Acme Co? returned to http://localhost:63680/callback?code=…&state=…&iss=<instance> Authentication successful | You can close this tab and return to Claude Code. ❯ /mcp ⎿ Authentication successful. Connected to patchy-dev-852. ~~~  Then `claude -p` with the same config: ~~~text tool_use mcp__patchy-dev-852__find_patches({}) -> {"actingFor":{"person":"Ben Ortiz","company":"Acme Co"},"total":56,"patches":[{"name":"deal-desk","description":"Track the sales team's open deals and move them through stages.","owner":"Ada Park","address":"<instance>/… tool_use mcp__patchy-dev-852__find_patches({"cursor": "eyJhZnRlciI6eyJ0b29scyI6ZmFsc2UsIm5hbWUiOiJzdGFuZHVwLXdlZWstNDkiLCJpZCI6IjhwdXU4d3FqbXhzbSJ9fQ"}) -> {"actingFor":{"person":"Ben Ortiz","company":"Acme Co"},"total":56,"patches":[{"name":"standup-week-50","description":"Team standup notes for week 50: what shipped and what is next.","owner":"Ada Park","address":"<instance>/… tool_use mcp__patchy-dev-852__open_patch({"patch": "deal-desk"}) -> {"name":"deal-desk","description":"Track the sales team's open deals and move them through stages.","owner":"Ada Park","address":"<instance>/acme/deal-desk","tools":[{"name":"listDeal… assistant: Acme Co has **56 patches**, and listing them took **2 pages**. I'm acting for Ben Ortiz. - **Page 1:** 50 patches, which were `deal-desk` and `standup-week-01` through `standup-week-49`. It returned a `next` cursor. - **Page 2:** 6 patches, `standup-week-50` through `standup-week-55`. It had no `next`, so that was the end. That adds up to the server's reported total of 56: one `deal-desk` plus 55 weekly standup notes. **Only one patch has agent tools: `deal-desk`**, owned by Ada Park. It tracks the sales team's open deals and moves them through stages. None of the 55 `standup-week-*` patches (also Ada Park's) have any agent tools, so I can't read or change them through Patchy. Ada Park would need to add tools for that. ### `deal-desk` tools 1. **`listDeals`** (read) - Lists the team's deals, newest first. - You can pass an optional `stage` to see only deals in that stage: `lead`, `proposal`, `negotiation`, `won` or `lost`. - Each deal comes back with its `id`, `company` (the customer), `stage`, `amount` (value in euros), `createdAt` and `updatedAt`. 2. **`moveDeal`** (change) - Moves one deal to a different stage and returns the updated deal. - It returns `null` if no deal has that id. - It needs both an `id` (taken from `listDeals`) and a target `stage` (same five options). - Any move is logged as Ben Ortiz, made through me. I only opened the patch. I haven't listed any deals or moved anything. ~~~ With agent access turned off at Company by Ada: ~~~text tool_use mcp__patchy-dev-852__find_patches({}) -> error: Agent access is off at Acme Co, so Patchy refuses every agent until an admin turns it on at <instance>/company. Acme Co's admin is Ada Park; tell the person to ask them. assistant: The `find_patches` call didn't list any patches. It returned an error. Here is the exact text: > Agent access is off at Acme Co, so Patchy refuses every agent until an admin turns it on at <instance>/company. Acme Co's admin is Ada Park; tell the person to ask them. Acme Co's agent access is turned off, so Patchy blocks every agent request until it's switched on. You'll need to ask Ada Park, Acme Co's admin, to turn it on at that URL. After that, I can run the call again and list the patches. ~~~ Claude Code's stored token for the dev server was cleared afterwards (`/mcp` › Clear authentication). ### Codex 0.163.0 with a minted token `pnpm dev mcp-token ben --client Codex`, then `codex exec --ephemeral --ignore-user-config -s read-only -c 'mcp_servers.patchy.url="<instance>/mcp"' -c 'mcp_servers.patchy.bearer_token_env_var="PATCHY_MCP_TOKEN"'`. With agent access off, before Ada turned it on (on the first head, before the singular "admin is" fix): ~~~text agent: I’ll find the Patchy MCP tool and return its answer exactly. mcp_tool_call patchy.find_patches({}) -> failed: Agent access is off at Acme Co, so Patchy refuses every agent until an admin turns it on at <instance>/company. Acme Co's admins are Ada Park; tell the person to ask one of them. agent: Agent access is off at Acme Co, so Patchy refuses every agent until an admin turns it on at <instance>/company. Acme Co's admins are Ada Park; tell the person to ask one of them. ~~~ On, at Codex's default revision (2025-06-18), asked: "Using the patchy MCP server: list every patch my company has, following next until there are no more pages. Tell me how many there are in all, how many pages it took, and which ones have agent tools. Then open the one with agent tools by its address and tell me what each of its tools does." ~~~text agent: I’ll use the Patchy tools to list every page of patches, then open the patch with agent tools and inspect what they do. mcp_tool_call patchy.find_patches({}) -> completed: {"actingFor":{"person":"Ben Ortiz","company":"Acme Co"},"total":56,"patches":[{"name":"deal-desk","description":"Track the sales team's open deals and move them through stages.","owner":"Ada Park","address":"<instance>/… mcp_tool_call patchy.find_patches({"cursor": "eyJhZnRlciI6eyJ0b29scyI6ZmFsc2UsIm5hbWUiOiJzdGFuZHVwLXdlZWstNDkiLCJpZCI6IjhwdXU4d3FqbXhzbSJ9fQ"}) -> completed: {"actingFor":{"person":"Ben Ortiz","company":"Acme Co"},"total":56,"patches":[{"name":"standup-week-50","description":"Team standup notes for week 50: what shipped and what is next.","owner":"Ada Park","address":"<instance>/… mcp_tool_call patchy.open_patch({"patch": "<instance>/acme/deal-desk"}) -> completed: {"name":"deal-desk","description":"Track the sales team's open deals and move them through stages.","owner":"Ada Park","address":"<instance>/acme/deal-desk","tools":[{"name":"listDeal… agent: The Patchy MCP connection is acting for **Ben Ortiz at Acme Co**. I followed `next` until it was absent: **56 patches across 2 pages** (50 + 6). All patches are owned by Ada Park: - **`deal-desk`** — the only patch with agent tools. - **`standup-week-01` through `standup-week-55`**, inclusive — 55 standup patches with no agent tools. I opened [deal-desk](<instance>/acme/deal-desk) by its address. Its tools are: | Tool | Kind | What it does | |---|---|---| | `listDeals` | Read | Lists deals newest first, optionally filtered by stage. Returns each deal’s ID, customer, stage, value in euros, and creation/update timestamps. | | `moveDeal` | Change | Takes a deal ID from `listDeals` and a target stage, moves the deal, and returns the updated record. Returns `null` if the ID doesn’t exist. | Both tools accept these stages: `lead`, `proposal`, `negotiation`, `won`, `lost`. ~~~ With `--enable mcp_2026_07_28` it made the same three calls, with the same answer. ### The door's request events for the final runs | Status | Client | Era / version | Method | Tool | Code or patchRef | Catalogue | | ---: | --- | --- | --- | --- | --- | --- | | 200 | Codex | legacy 2025-06-18 | tools/call | find_patches | — | Total=56, Listed=50, EndedEarly=False, Continued=False | | 200 | Codex | legacy 2025-06-18 | tools/call | find_patches | — | Total=56, Listed=6, EndedEarly=False, Continued=True | | 200 | Codex | legacy 2025-06-18 | tools/call | open_patch | name | — | | 401 | — | | — | — | missing_token | — | | 200 | Claude Code | modern 2026-07-28 | server/discover | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/list | — | — | — | | 200 | Claude Code | modern 2026-07-28 | server/discover | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/list | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/call | find_patches | — | Total=56, Listed=50, EndedEarly=False, Continued=False | | 200 | Claude Code | modern 2026-07-28 | tools/call | find_patches | — | Total=56, Listed=6, EndedEarly=False, Continued=True | | 200 | Claude Code | modern 2026-07-28 | tools/call | open_patch | name | — | | 200 | Codex | legacy 2025-06-18 | initialize | — | — | — | | 202 | Codex | legacy 2025-06-18 | notifications/initialized | — | — | — | | 200 | Codex | legacy 2025-06-18 | tools/list | — | — | — | | 200 | Codex | legacy 2025-06-18 | tools/call | find_patches | — | Total=56, Listed=50, EndedEarly=False, Continued=False | | 200 | Codex | legacy 2025-06-18 | tools/call | find_patches | — | Total=56, Listed=6, EndedEarly=False, Continued=True | | 200 | Codex | legacy 2025-06-18 | tools/call | open_patch | address | — | | 200 | Codex | modern 2026-07-28 | server/discover | — | — | — | | 200 | Codex | modern 2026-07-28 | tools/list | — | — | — | | 200 | Codex | modern 2026-07-28 | tools/call | find_patches | — | Total=56, Listed=50, EndedEarly=False, Continued=False | | 200 | Codex | modern 2026-07-28 | tools/call | find_patches | — | Total=56, Listed=6, EndedEarly=False, Continued=True | | 200 | Codex | modern 2026-07-28 | tools/call | open_patch | address | — | | 200 | Claude Code | modern 2026-07-28 | server/discover | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/list | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/call | find_patches | — | Total=56, Listed=50, EndedEarly=False, Continued=False | | 200 | Claude Code | modern 2026-07-28 | tools/call | find_patches | — | Total=56, Listed=6, EndedEarly=False, Continued=True | | 200 | Claude Code | modern 2026-07-28 | tools/call | open_patch | name | — | | 200 | Claude Code | modern 2026-07-28 | server/discover | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/list | — | — | — | | 200 | Claude Code | modern 2026-07-28 | tools/call | find_patches | agent_access_off | — | </details> ## Release impact - [ ] **Breaking** — requires action from anyone already running this - [x] **Not breaking** — no action required No migration and no stored data: the catalogue only reads. - **What does a revert leave behind?** Nothing. Clients that called the two tools get "not built" again, as on #883. - **Does the release before it still work with that?** Yes. Built by Claude Opus 5.5 (xhigh) in Claude Code on T3 Code; reviewed and accepted by GPT-6.1-Sol (xhigh).
What changed
A device login the person approved could still mint a machine token after they pressed Revoke all machines (Disconnect everything), or after they were deactivated and reactivated, if the terminal hadn't collected it yet. Now both endings cancel every approval the terminal hasn't collected.
Each user's row also records when their credentials last all ended. Patchy MCP's agent connections and consents (#846, #849) will refuse anything made at or before that time. That keeps credentials ended even when the ending happened on a release that knows nothing about agent connections, which is why this ships before the stack.
Closes #838
Look at
The poll checks the stamp under the user lock (
packages/auth/src/DeviceLogins.ts). Deactivation (Users.deactivate) and revoke all (MachineTokens.revokeAll) setusers.credentials_ended_atin their transaction; reactivation leaves it. Confirming a device login now recordsconfirmed_at. Before minting, the poll locks the user row (mint and both endings take the same lock) and refuses a confirmation at or before the stamp, compared in Postgres at full precision, answeringunknownas it does today for someone deactivated between confirm and poll. A login confirmed by the release before this one has noconfirmed_at; once the person has a stamp, it never mints.Both times are the database's clock, read under the user's row lock. Confirmation takes a share lock on the user, and the endings take
FOR UPDATE, so a confirmation that commits before an ending always has the earlierclock_timestamp(). Servers' own clocks can disagree by more than the gap between a confirmation and an ending, which Sol showed would let one mint (round 1 in the review comment). The stamp never moves back (GREATEST). When #846 and #849 compare agent connections and consents with the stamp, they should take those times from the database's clock too.The endings don't touch
device_loginsrows. The poll holds the login's row lock while it waits for the user lock, so an ending that deleted those rows under the user lock could deadlock with it. Checking at the poll keeps one lock order (login, then user), and the row is consumed when the terminal polls, or pruned at expiry.A test, red on main's code, green on the head.
Disconnect everything and deactivation cancel confirmations not yet collectedruns approve → revoke all → poll, and approve → deactivate → reactivate → poll, with the confirming server's clock a minute ahead of the ending's. It also fails on this PR's first commit, which used servers' clocks. With main'sDeviceLogins.ts,MachineTokens.tsandUsers.tsrestored under the new test, the first poll mints:On the head:
Tests 1 passed | 17 skipped (18).On a local instance, as
[email protected]:patchy login, confirm on/login/device,POST /machines/revoke-all, then the terminal collects:The server answered
POST /api/login/device/tokenwith 410unknown. Afterwards the login row was gone and the user had no live token. A secondpatchy login, started and confirmed after the ending, logged in normally (This machine is "after the ending".).How it was verified
packages/auth/src/DeviceLogins.test.ts(the two endings above; a login started before the ending but confirmed after it still mints, even on a server whose clock runs behind, or in the same millisecond as the ending; a poll racing revoke all leaves no live token),MachineTokens.test.ts(revoke all moves the stamp forward each time),Users.test.ts(deactivation sets it, a repeat deactivation and reactivation keep it).pnpm check: format, lint, typecheck and synth pass. Its full offline run hit timeouts in suites this PR doesn't touch (patchy CLI, PGlite) at load average ~20, and they passed when rerun alone.pnpm dev, at the first commit. Deactivate → reactivate → poll is covered by the test, not by hand.0019_credentials_ended, the next above main's0018. Tier 2 readers accept handler descriptions, read-only actions and agent tools, and enforce them #837 is being built in parallel; whichever of the two merges second renumbers.Release impact
Two nullable columns, no backfill, no API or CLI change.
users.credentials_ended_atanddevice_logins.confirmed_at, holding the stamps written while this release ran.rollback-rehearsalcheck runs it on this schema. Rolling back past this PR reopens the device-login gap it closes. Once Build Patchy MCP #833's stack is live, it would also let agent connections an older release ended work again, so the stack must not be rolled back past this PR. That constraint belongs to the stack's release, not this one.Built by Claude Opus 5.5 (xhigh) in Claude Code on T3 Code; reviewed and accepted by GPT-6.1-Sol (xhigh).