# connect/linkstore: Adopt, for moving an app's existing links

Status: implemented on branch `linkstore-adopt` by Claude Opus 5.5. Awaiting
review by Claude Fable in the branch discussion. Asked for by Fable's review
of oficina/docs #100 ("Second branch … assessment").

**Why.** Docs will move from its private `homeBridge` onto `home/connect`, so
its `docs_identity_links` rows must become `home_identity_links` rows. Each
connect row carries `person_binding`: an HMAC under connect's storage key
over a version tag, the namespace, origin, issuer, person ID and the person's
address. `Resolve` refuses a row whose binding does not match. Copying
(issuer, subject, person_id) in SQL would leave every link dead. The HMAC
layout is connect's unexported contract, so connect computes it.

**API.** `linkstore.Store.Adopt(ctx, tx *sql.Tx, issuer, subject string,
personID int64) error`, with `connect.Bridge.Adopt` delegating to it. Apps
hold a Bridge, not a Store. A second Store built by hand with slightly
different parameters would write rows that never resolve.

**Decisions.**
- **It takes the caller's `*sql.Tx` and never commits.** The move has to be
  atomic with retiring the old table. The app's migration runner applies SQL
  files, so this is a boot step in Go, not a migration file. The lookup
  reads through that tx, as everywhere else in linkstore.
- **The row is the one Confirm writes.** The binding comes from
  `currentBinding`, the helper every flow stage uses. The insert is Confirm's
  `ON CONFLICT DO NOTHING` followed by a read-back by (issuer, subject). A
  stored row with the same person and an equal binding counts as success.
  Anything else is `ErrRefused`.
- **Refusals write nothing.** Adopt refuses:
  - a foreign issuer;
  - an invalid subject;
  - a person ID of 0 or less, even if the app's lookup would accept it;
  - a person the lookup does not return, or returns with an empty address;
  - the subject linked to another person;
  - the person linked under another subject (`UNIQUE (issuer, person_id)`);
  - an existing row whose binding is missing or was made for an old address.

  The last case keeps the README rule that "old or unbound links fail
  closed": Adopt is not a way to rebind a link.
- **The clock and the flows stay out of scope.** `home_link_clock` holds no
  key material, so the README tells the app to carry its high-water mark in
  plain SQL in the same transaction. In-flight flows last five minutes and
  are not carried.
- **Adopt does not ask `Config.HomeOnly`.** That hook reads outside the tx,
  and on a single-connection pool it would deadlock. The app's own
  disjointness constraint, Docs' triggers, stays the guard.

**Privacy.** No column and no schema change. The address goes only into the
HMAC. `TestAdoptWritesNoAddress` uses a canary address that is nowhere in
the database, and checks that the database and WAL never contain it and the
schema is unchanged.

**Tests.** `connect/linkstore/adopt_test.go` and `connect/adopt_test.go`:
- the adopted binding equals the one Confirm wrote, and the row resolves;
- an ordinary Home sign-in through the Bridge resolves to the adopted person;
- idempotence across transactions, within one transaction, and over a
  confirmed link;
- each refusal, with the table unchanged;
- generic storage errors;
- the lookup reads through the caller's transaction, and rolling back
  leaves nothing;
- tampered, copied, wrong-key and changed-address bindings are still
  refused by `Resolve`.

**Hand mutations.** 13 in all, one survivor.

Killed:
- binding altered;
- issuer check dropped;
- subject check dropped;
- person ≤ 0 check dropped (killed by the permissive-lookup case);
- empty-binding check dropped;
- `INSERT OR REPLACE`;
- missing read-back treated as success;
- binding comparison dropped;
- lookup outside the tx;
- Bridge passing its own issuer;
- nil-tx check dropped;
- the lookup error passed through.

Survived, and equivalent: dropping the `person != personID` comparison. The
binding covers the ID, so the HMAC comparison catches the same rows. Confirm
has the same pair.
