Resolve complete Home identity within caller transactions
4 files changed,
+105
−3
connect/api.go+8 −0connect/linkstore/resolve_on_test.go+76 −0connect/linkstore/store.go+10 −3docs/superpowers/specs/2026-10-25-transactional-link-resolution.md+11 −0
CI passed — run details
diff --git a/connect/api.go b/connect/api.go| index 91aee15..d258cc7 100644 |
| --- a/connect/api.go |
| +++ b/connect/api.go |
| @@ -48,6 +48,14 @@ func (b *Bridge) Resolve(ctx context.Context, subject string) (Person, error) { |
| id, label, err := b.links.Resolve(ctx, b.issuer, subject) |
| return Person{ID: id, Label: label}, err |
| } |
| + |
| +// ResolveOn resolves the fixed Home issuer on the caller's transaction, |
| +// including the keyed person binding and current local admission. |
| +func (b *Bridge) ResolveOn(ctx context.Context, q linkstore.Queryer, subject string) (Person, error) { |
| + id, label, err := b.links.ResolveOn(ctx, q, b.issuer, subject) |
| + return Person{ID: id, Label: label}, err |
| +} |
| + |
| func (b *Bridge) ResolveSession(r *http.Request) (Person, error) { |
| session, err := b.actualSession(r) |
| if err != nil || session == nil { |
diff --git a/connect/linkstore/resolve_on_test.go b/connect/linkstore/resolve_on_test.go| new file mode 100644 |
| index 0000000..05dc9a7 |
| --- /dev/null |
| +++ b/connect/linkstore/resolve_on_test.go |
| @@ -0,0 +1,76 @@ |
| +package linkstore |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "errors" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestResolveOnUsesHeldTransactionAndCompleteBinding(t *testing.T) { |
| + s, _ := fixture(t) |
| + s.db.SetMaxOpenConns(1) |
| + l := local(1) |
| + proof := complete(t, s, l, "subject-one", 42) |
| + if _, e := s.Confirm(ctx, proof, l, testNow); e != nil { |
| + t.Fatal(e) |
| + } |
| + id, label, e := s.Resolve(ctx, issuer, "subject-one") |
| + if e != nil || id != 1 || label != "one@example.com" { |
| + t.Fatal(id, label, e) |
| + } |
| + for _, mode := range []string{"valid", "binding", "unlink", "address", "admission", "storage", "issuer", "subject", "nil"} { |
| + t.Run(mode, func(t *testing.T) { |
| + c, cancel := context.WithTimeout(ctx, time.Second) |
| + defer cancel() |
| + tx, e := s.db.BeginTx(c, nil) |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + defer tx.Rollback() |
| + if _, e = tx.ExecContext(c, "UPDATE home_link_clock SET high_water=high_water WHERE id=1"); e != nil { |
| + t.Fatal(e) |
| + } |
| + old := s.lookup |
| + defer func() { s.lookup = old }() |
| + q := Queryer(tx) |
| + iss, sub := issuer, "subject-one" |
| + switch mode { |
| + case "binding": |
| + _, e = tx.ExecContext(c, "UPDATE home_identity_links SET person_binding='broken'") |
| + case "unlink": |
| + _, e = tx.ExecContext(c, "DELETE FROM home_identity_links") |
| + case "address": |
| + _, e = tx.ExecContext(c, "UPDATE people SET email='changed@example.com' WHERE id=1") |
| + case "admission": |
| + s.lookup = func(context.Context, Queryer, int64) (string, error) { return "", sql.ErrNoRows } |
| + case "storage": |
| + s.lookup = func(context.Context, Queryer, int64) (string, error) { return "", errors.New("secret-diagnostic") } |
| + case "issuer": |
| + iss = "https://wrong.example.com" |
| + case "subject": |
| + sub = "" |
| + case "nil": |
| + q = nil |
| + } |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + id, label, e = s.ResolveOn(c, q, iss, sub) |
| + if mode == "valid" { |
| + if e != nil || id != 1 || label != "one@example.com" { |
| + t.Fatal("escaped transaction", id, label, e) |
| + } |
| + return |
| + } |
| + want := ErrRefused |
| + if mode == "storage" { |
| + want = ErrStorage |
| + } |
| + if e != want || id != 0 || label != "" { |
| + t.Fatal("incomplete or leaked identity", mode, id, label, e) |
| + } |
| + }) |
| + } |
| +} |
diff --git a/connect/linkstore/store.go b/connect/linkstore/store.go| index 544c1ff..07a2a5e 100644 |
| --- a/connect/linkstore/store.go |
| +++ b/connect/linkstore/store.go |
| @@ -178,19 +178,26 @@ func (s *Store) Sweep(ctx context.Context, now time.Time) error { |
| // Resolve reads the current existing person, never EnsurePerson or a cached |
| // address. A deleted or unlinked person is refused without creating anything. |
| func (s *Store) Resolve(ctx context.Context, issuer, subject string) (int64, string, error) { |
| - if issuer != s.issuer || !validSubject(subject) { |
| + return s.ResolveOn(ctx, s.db, issuer, subject) |
| +} |
| + |
| +// ResolveOn repeats the complete identity check on the caller's connection. |
| +// A transaction remains caller-owned. LookupPerson must also use q, so this |
| +// never opens another SQL connection while a caller holds SQLite's writer. |
| +func (s *Store) ResolveOn(ctx context.Context, q Queryer, issuer, subject string) (int64, string, error) { |
| + if q == nil || issuer != s.issuer || !validSubject(subject) { |
| return 0, "", ErrRefused |
| } |
| var id int64 |
| var address, binding string |
| - err := s.db.QueryRowContext(ctx, `SELECT person_id, person_binding FROM home_identity_links WHERE issuer = ? AND subject = ?`, issuer, subject).Scan(&id, &binding) |
| + err := q.QueryRowContext(ctx, `SELECT person_id, person_binding FROM home_identity_links WHERE issuer = ? AND subject = ?`, issuer, subject).Scan(&id, &binding) |
| if errors.Is(err, sql.ErrNoRows) { |
| return 0, "", ErrRefused |
| } |
| if err != nil { |
| return 0, "", ErrStorage |
| } |
| - address, err = s.lookup(ctx, s.db, id) |
| + address, err = s.lookup(ctx, q, id) |
| if errors.Is(err, sql.ErrNoRows) { |
| return 0, "", ErrRefused |
| } |
diff --git a/docs/superpowers/specs/2026-10-25-transactional-link-resolution.md b/docs/superpowers/specs/2026-10-25-transactional-link-resolution.md| new file mode 100644 |
| index 0000000..2d21843 |
| --- /dev/null |
| +++ b/docs/superpowers/specs/2026-10-25-transactional-link-resolution.md |
| @@ -0,0 +1,11 @@ |
| +# Resolve Home identity on the caller's transaction |
| + |
| +Status: proposed bounded amendment for delegated app writes. |
| + |
| +The app adapters recheck current Home person ID/address and admission under their SQLite writer, but bypass linkstore's keyed person_binding validation. A corrupt or changed binding between initial bridge resolution and commit must refuse the write, not commit and fail only on response emission. |
| + |
| +Add linkstore.Store.ResolveOn(ctx, Queryer, issuer, subject) and connect.Bridge.ResolveOn(ctx, Queryer, subject). Existing Resolve delegates to ResolveOn with its stored DB. The new method performs exactly the existing issuer/subject validation, link lookup, LookupPerson using the supplied Queryer, positive-ID/address and constant-time person_binding check, and existing safe refusal/storage errors. No new cryptography, schema, permissions, caches or subject-to-address fallback. The supplied Queryer must be the caller's transaction connection; it remains caller-owned. Bridge uses its fixed configured issuer. |
| + |
| +Each app calls bridge.ResolveOn using tx.Statement.ConnPool (the existing QueryRowContext interface), compares stable ID/address to the request's captured identity, and retains operation-specific local permissions in that transaction. This avoids both the incomplete hand-rolled link check and opening a second SQL connection while holding the writer. |
| + |
| +Tests: ordinary Resolve parity; a real held SQLite transaction sees its own uncommitted valid/invalid binding, deleted link, changed address and admission refusal; max one SQL connection with a context deadline proves no escape to stored DB; storage errors remain redacted; nil queryer refused. A deliberate mutant using the stored DB or skipping binding validation must fail. Apps add trigger-driven binding corruption after content insertion and assert all content/attribution/receipt effects roll back. Review and full Home CI precede publishing the module revision; consuming branches pin it exactly. |