Skip to content

Store offline tokens and agent blocks - #45

Open
dcmcand wants to merge 1 commit into
slice/agent-tokens-05-configfrom
slice/agent-tokens-06-store-tokens
Open

dcmcand wants to merge 1 commit into
slice/agent-tokens-05-configfrom
slice/agent-tokens-06-store-tokens

Conversation

@dcmcand

@dcmcand dcmcand commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Slice 6: Offline token tables and the per-person lock

internal/store keeps one sealed offline token per person and admin blocks, every write for one person is serialised on both engines, and a sign-in racing a block never leaves a blocked person a live token.

Spec: nebari-dev/tapas/docs/superpowers/specs/2026-11-30-agent-token-broker-design.md
Plan: nebari-dev/tapas/docs/superpowers/plans/2026-12-01-agent-tokens-and-frames.md (Slice 6)
Contracts touched: Tables person_offline_tokens, person_offline_revocations and person_agent_blocks, and store.OfflineTokens, Per-person write lock and the lease's database clock in internal/store, Audit action agent_access.revoke, Audit action agent_access.unblock
Depends on: #44

  • Migration 00028: the three tables, with refreshing_until for the later refresh lease.
  • store.OfflineTokens: Put, Get, Rotate, Delete, DeleteRev, DeleteGen, List, Expired, Block, Unblock, Blocked. Block moves the token row it deletes (DELETE ... RETURNING) into person_offline_revocations in the transaction that sets the block; Put for a blocked person adds its token there instead of storing it, and answers ErrBlocked.
  • mutatePerson takes a per-person lock as the transaction's first statement: pg_advisory_xact_lock on Postgres, nothing on SQLite (already serialised). mutate and inTxAnnounce route through helpers with an optional first statement and behave as before without one.
  • Nothing calls this yet; the broker slices do.

Reviewer note: write.go and open.go changes touch every store write; the task review checked the no-first-statement path is unchanged.

Validation

Step Command / action Expected Result
sqlite go test -count=1 -race ./internal/store/... ok ok
postgres make test-postgres ok, including the Postgres-only lock tests exit 0 (store and backup); Put/Block race 200 rounds; lock-first over every write method
sqlc make generate-sql && git diff --exit-code internal/store/sqlcgen no drift exit 0
local make test-local ok exit 0

Migration 00028 adds person_offline_tokens (with refreshing_until for
the later refresh lease), person_offline_revocations and
person_agent_blocks.

store.OfflineTokens keeps one sealed token per person and the admins'
blocks: Put, Get, Rotate (compare-and-swap on rev, keeps gen, clears
the lease), Delete, DeleteRev, DeleteGen, List, Expired, Block,
Unblock and Blocked. Block moves the token row it deletes into
person_offline_revocations in the transaction that sets the block, and
Put for a blocked person stores no live row and adds the token it was
given there instead, answering ErrBlocked.

Every write for one person runs through mutatePerson, which takes a
per-person lock as the transaction's first statement: on Postgres
pg_advisory_xact_lock on an FNV-1a key of the person's name, on SQLite
nothing (its write transactions are already serialised). mutate and
inTxAnnounce now route through helpers that take an optional first
statement; with none they behave as before. Open also picks each
engine's statement-time clock in Unix ms for the lease.

Postgres-only tests race a sign-in against a block 200 times and check
that every write method waits for, and takes first, the person lock.
The contracts and patterns registries describe the tables, the lock and
the two audit actions; the plan records the slice's evidence,
checkpoint review, six approved deviations and a merge-order note.
@dcmcand
dcmcand added this pull request to stack #40 December 1, 2026 17:42

This branch has not been deployed

No deployments
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