Skip to content

feat: materialize agent_owner_pubkey on NIP-OA auth#491

Merged
tlongwell-block merged 1 commit into
mainfrom
feat/nip-oa-agent-owner-backfill
May 6, 2026
Merged

feat: materialize agent_owner_pubkey on NIP-OA auth#491
tlongwell-block merged 1 commit into
mainfrom
feat/nip-oa-agent-owner-backfill

Conversation

@tlongwell-block

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #490. When an agent authenticates via NIP-OA, the relay now materializes the agent→owner relationship so cross-connection features (observer frames, channel add/remove policy) work for BYO agents.

What it does

  1. Session-scoped fast pathAuthContext.agent_owner_pubkey is set on successful NIP-OA auth, enabling zero-DB-lookup observer frame authorization for the current connection.

  2. Idempotent DB backfill — On first NIP-OA auth, writes users.agent_owner_pubkey so cross-connection features (owner managing agent from a separate session) work without desktop provisioning.

Safety properties

Concern How it's handled
Does this bypass NIP-OA revocation? No. agent_owner_pubkey is not relay_members. Agent still needs valid NIP-OA every connection.
First-write-wins conflict set_agent_owner uses WHERE agent_owner_pubkey IS NULL. If already owned by someone else, session fast-path only activates after is_agent_owner DB confirmation.
Stale observer cache Pre-warms observer_owner_cache on successful backfill.
BYO agent with no users row ensure_user called for both agent and owner before set_agent_owner.
FK constraint (owner must exist) ensure_user(owner) handles this.
Backfill failure Non-fatal — auth succeeds, just no fast-path or DB record.
Desktop-provisioned agents set_agent_owner is a no-op (column already set). Zero behavioral change.

Changes

  • sprout-auth/lib.rs: AuthContext gains agent_owner_pubkey: Option<PublicKey>
  • sprout-relay/api/mod.rs: enforce_relay_membership returns Option<PublicKey> (owner on NIP-OA success)
  • sprout-relay/handlers/auth.rs: captures owner, validates against DB, backfills, stashes on context
  • sprout-relay/handlers/event.rs: observer frame fast-path from AuthContext

Testing

  • cargo build --workspace
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test -p sprout-relay -p sprout-auth
  • E2E verified: BYO agent connects via NIP-OA → agent_owner_pubkey correctly backfilled in DB

… DB backfill)

When an agent authenticates via NIP-OA, the relay now:

1. Stashes the verified owner on AuthContext.agent_owner_pubkey (session-scoped)
   - Only set if DB confirms the relationship (first-write-wins)
   - Observer frames use this as a fast path (skip DB/cache lookup)

2. Idempotently backfills users.agent_owner_pubkey in the DB
   - Creates user rows if needed (ensure_user for both agent and owner)
   - Uses existing set_agent_owner (WHERE agent_owner_pubkey IS NULL)
   - First-write-wins: if already owned by someone else, verifies match
   - Pre-warms observer_owner_cache on successful backfill

This enables cross-connection features (observer frames, channel policy)
to work for BYO agents that were never provisioned through the desktop.

Security properties:
- Does NOT add agent to relay_members (NIP-OA still required every connect)
- Session fast-path only activates if DB confirms the owner relationship
- Conflicting owner (agent already owned by someone else) is handled safely
- Backfill failure is non-fatal (auth succeeds, just no fast-path/backfill)

Changes:
- sprout-auth: AuthContext gains agent_owner_pubkey field
- api/mod.rs: enforce_relay_membership returns Option<PublicKey>
- handlers/auth.rs: captures owner, backfills DB, stashes on context
- handlers/event.rs: observer frame fast-path from AuthContext
@tlongwell-block tlongwell-block force-pushed the feat/nip-oa-agent-owner-backfill branch from 5a32918 to 50942e7 Compare May 6, 2026 15:59
@tlongwell-block tlongwell-block enabled auto-merge (squash) May 6, 2026 16:05
@tlongwell-block tlongwell-block merged commit 1f1f874 into main May 6, 2026
14 checks passed
@tlongwell-block tlongwell-block deleted the feat/nip-oa-agent-owner-backfill branch May 6, 2026 16:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant