Skip to content

feat(identity-pg): Postgres adapter for people, sessions, grants and the auth log - #788

Merged
ajianaz merged 1 commit into
developfrom
feat/identity-postgres
Dec 5, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/identity-postgres

Conversation

@ajianaz

@ajianaz ajianaz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What

ADR-0022 slice 8, Postgres for identity. A migration and a new crate, meldframe-identity-pg, that implement the IdentityStore seam of meldframe-identity on Postgres: people, sign-in sessions, login attempts, agent grants with the deny, the Gatehouse revoke calls and the auth event log. The 85-case store_contract! passes on it unchanged.

This pull request is stacked on #782 (slice 7b), so its base is feat/identity-sessions-grants and the diff holds only slice 8. When #782 merges, the base moves to develop.

Why

Slices 3b to 7b built the rules and the in-memory reference adapter. Nothing survives a restart until the store is real, and three rules (the ten-session count, the concurrent sign-in cap and one live grant for each person and client) only mean something when two callers race. The slices that follow (11b sessions on the wire, 12, 15c, 16) read and write through this adapter.

Changes

  • migrations/0009_identity.sql: nine tables, the guard triggers people_guard, sign_in_sessions_guard, agent_grants_guard, auth_events_guard and auth_events_no_truncate, and the unique partial index agent_grants_one_live. A session that ended stays ended, a confirmed revoke is final, a bound and a last use only rise, and an auth event is edited only to erase its subject and leaves only after 90 days.
  • crates/integrations/meldframe-identity-pg (kind bin): the adapter over the pool and the private runtime of PgStore, synchronous at the seam and bridged with block_on. Time is only the now_ms of the call; the one exception is the retention trigger of auth_events. Each call that counts and then inserts takes an advisory lock (people, then person, then workspace). No raw cookie, token or email is stored, only SHA-256 names. Auth events: a refused sign-in is kept once for each subject and UTC day, at most 1,000 a day, with erasure of a subject and a purge cut at 90 days.
  • crates/integrations/meldframe-store-pg: PgStore::pool() and PgStore::block_on(), and db_i64 exported.
  • crates/core/meldframe-identity: the stored words (ALL and parse) come from one macro in words.rs; Session::holds_slot, Grant::new_pending and check_time are public so the adapter calls the rule and does not copy it.
  • tools/check-migrations.sh: a sixth section for the identity schema (unique subject, the guards, one live grant for each pair, append-only events with the 89 and 91 day cases, no truncate, the cascade from a person).
  • Docs: the ADR-0022 slice 8 note with the ten places the ADR was silent and what was not covered, docs/epics.md, AGENTS.md and docs/architecture.md.

Testing

Run against a local Postgres 16 with MELDFRAME_REQUIRE_PG=1:

  • cargo test -p meldframe-identity-pg -p meldframe-identity: 148 tests in the new crate (85 contract cases, the rest differential, concurrency, schema, restart, purge, events, hostile input, isolation and an async caller) and 154 in the core crate, all passing.
  • Differential test: the memory and the Postgres adapters agree on every step of 24 seeded walks of 400 steps.
  • Concurrency under real threads: the ten-session cap, the concurrent sign-in cap, one live grant for each pair, the grant cap of a shared workspace and allowing revoked clients again at once.
  • Hand-made mutants (each lock removed, DB now() in place of now_ms, the unique index dropped): each fails a test. The unique index on live grants is killed by the schema test and by check-migrations.sh; the locks mask it in the threaded tests.
  • tools/check-migrations.sh section 6, cargo fmt --check, cargo clippy --workspace --all-targets -- -D warnings, cargo xtask deps-check, mm-check, size-check, licences --check, portable-check, sh scripts/ci/selftest.sh all pass.
  • Not run: cargo deny check (not installed on the machine used).
  • Known and not from this change: the WASM run of seeded in meldframe-identity fails the same way on the unmodified base (feat(identity): sign-in sessions, login attempts and agent grants with the deny #782).
  • Not covered: a Postgres other than 16, a real Gatehouse, load at volume, a primary failover during a call, and the wire (slice 11b).

Cora: the local deterministic review (cora review --base feat/identity-sessions-grants --no-llm) found no issues, and cora mcp answered initialize and tools/list. An agent review through cora.review_prepare and review_finalize was not done, so no independent review is claimed here; the CI Cora review is the first one.


@ajianaz
ajianaz force-pushed the feat/identity-sessions-grants branch from 37b45db to afff9da Compare December 5, 2026 01:17
…the auth log

ADR-0022 slice 8. migrations/0009_identity.sql adds nine tables, the unique
index that allows one live grant for each person and client, and the guard
triggers (a session that ended and a grant that was revoked stay so, a bound
only rises, an auth event is edited only to erase its subject and leaves only
after 90 days).

The new crate meldframe-identity-pg (kind bin) implements the IdentityStore
seam over the pool and runtime of PgStore and passes the 85-case contract
unchanged. Time is always the now_ms of the call; the one exception is the
retention trigger of auth_events. Every call that counts and then inserts
takes an advisory lock (person, then workspace), proved by threaded tests
and by hand-made mutants that remove each lock and the index.

PgStore gains pool() and block_on(), and exports db_i64. meldframe-identity
makes the stored words (ALL and parse), Session::holds_slot,
Grant::new_pending and check_time public so the adapter calls the rule of the
core and has no copy of it. tools/check-migrations.sh has a sixth section.

Signed-off-by: Anaz S. Aji <12648688+ajianaz@users.noreply.github.com>
@ajianaz
ajianaz changed the base branch from feat/identity-sessions-grants to develop December 5, 2026 09:01
@ajianaz
ajianaz force-pushed the feat/identity-postgres branch from 82af5d1 to 9aa24d5 Compare December 5, 2026 09:01
@ajianaz
ajianaz merged commit 4c1daf9 into develop Dec 5, 2026
10 of 11 checks passed
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