| 1 | # connect/linkstore: Adopt, for moving an app's existing links |
| 2 | |
| 3 | Status: implemented on branch `linkstore-adopt` by Claude Opus 5.5. Awaiting |
| 4 | review by Claude Fable in the branch discussion. Asked for by Fable's review |
| 5 | of oficina/docs #100 ("Second branch … assessment"). |
| 6 | |
| 7 | **Why.** Docs will move from its private `homeBridge` onto `home/connect`, so |
| 8 | its `docs_identity_links` rows must become `home_identity_links` rows. Each |
| 9 | connect row carries `person_binding`: an HMAC under connect's storage key |
| 10 | over a version tag, the namespace, origin, issuer, person ID and the person's |
| 11 | address. `Resolve` refuses a row whose binding does not match. Copying |
| 12 | (issuer, subject, person_id) in SQL would leave every link dead. The HMAC |
| 13 | layout is connect's unexported contract, so connect computes it. |
| 14 | |
| 15 | **API.** `linkstore.Store.Adopt(ctx, tx *sql.Tx, issuer, subject string, |
| 16 | personID int64) error`, with `connect.Bridge.Adopt` delegating to it. Apps |
| 17 | hold a Bridge, not a Store. A second Store built by hand with slightly |
| 18 | different parameters would write rows that never resolve. |
| 19 | |
| 20 | **Decisions.** |
| 21 | - **It takes the caller's `*sql.Tx` and never commits.** The move has to be |
| 22 | atomic with retiring the old table. The app's migration runner applies SQL |
| 23 | files, so this is a boot step in Go, not a migration file. The lookup |
| 24 | reads through that tx, as everywhere else in linkstore. |
| 25 | - **The row is the one Confirm writes.** The binding comes from |
| 26 | `currentBinding`, the helper every flow stage uses. The insert is Confirm's |
| 27 | `ON CONFLICT DO NOTHING` followed by a read-back by (issuer, subject). A |
| 28 | stored row with the same person and an equal binding counts as success. |
| 29 | Anything else is `ErrRefused`. |
| 30 | - **Refusals write nothing.** Adopt refuses: |
| 31 | - a foreign issuer; |
| 32 | - an invalid subject; |
| 33 | - a person ID of 0 or less, even if the app's lookup would accept it; |
| 34 | - a person the lookup does not return, or returns with an empty address; |
| 35 | - the subject linked to another person; |
| 36 | - the person linked under another subject (`UNIQUE (issuer, person_id)`); |
| 37 | - an existing row whose binding is missing or was made for an old address. |
| 38 | |
| 39 | The last case keeps the README rule that "old or unbound links fail |
| 40 | closed": Adopt is not a way to rebind a link. |
| 41 | - **The clock and the flows stay out of scope.** `home_link_clock` holds no |
| 42 | key material, so the README tells the app to carry its high-water mark in |
| 43 | plain SQL in the same transaction. In-flight flows last five minutes and |
| 44 | are not carried. |
| 45 | - **Adopt does not ask `Config.HomeOnly`.** That hook reads outside the tx, |
| 46 | and on a single-connection pool it would deadlock. The app's own |
| 47 | disjointness constraint, Docs' triggers, stays the guard. |
| 48 | |
| 49 | **Privacy.** No column and no schema change. The address goes only into the |
| 50 | HMAC. `TestAdoptWritesNoAddress` uses a canary address that is nowhere in |
| 51 | the database, and checks that the database and WAL never contain it and the |
| 52 | schema is unchanged. |
| 53 | |
| 54 | **Tests.** `connect/linkstore/adopt_test.go` and `connect/adopt_test.go`: |
| 55 | - the adopted binding equals the one Confirm wrote, and the row resolves; |
| 56 | - an ordinary Home sign-in through the Bridge resolves to the adopted person; |
| 57 | - idempotence across transactions, within one transaction, and over a |
| 58 | confirmed link; |
| 59 | - each refusal, with the table unchanged; |
| 60 | - generic storage errors; |
| 61 | - the lookup reads through the caller's transaction, and rolling back |
| 62 | leaves nothing; |
| 63 | - tampered, copied, wrong-key and changed-address bindings are still |
| 64 | refused by `Resolve`. |
| 65 | |
| 66 | **Hand mutations.** 13 in all, one survivor. |
| 67 | |
| 68 | Killed: |
| 69 | - binding altered; |
| 70 | - issuer check dropped; |
| 71 | - subject check dropped; |
| 72 | - person ≤ 0 check dropped (killed by the permissive-lookup case); |
| 73 | - empty-binding check dropped; |
| 74 | - `INSERT OR REPLACE`; |
| 75 | - missing read-back treated as success; |
| 76 | - binding comparison dropped; |
| 77 | - lookup outside the tx; |
| 78 | - Bridge passing its own issuer; |
| 79 | - nil-tx check dropped; |
| 80 | - the lookup error passed through. |
| 81 | |
| 82 | Survived, and equivalent: dropping the `person != personID` comparison. The |
| 83 | binding covers the ID, so the HMAC comparison catches the same rows. Confirm |
| 84 | has the same pair. |
| 85 | |