Skip to content

feat(core): user-only Principal, database-authoritative sessions, delete KvSessionStore - #74

Merged
aRustyDev merged 4 commits into
mainfrom
feat/db-sessions
Oct 23, 2026
Merged

aRustyDev merged 4 commits into
mainfrom
feat/db-sessions

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

Closes #36 (FR-03).

A session now belongs to a user, not to a workspace, and it lives in the database. Principal is { user_id };
authorize takes the workspace slug from the route and resolves the slug, the workspace membership and the project
membership in one query; sessions are rows behind the Database port, stored as a SHA-256 hash; KvSessionStore, the
SESSIONS binding, SessionStore, CacheError and Ports.sessions are gone.

Commits (one per bead)

  1. feat(core): authorize resolves the workspace slug; Principal is user-only.
    • Migration 0003_workspaces.sql (idempotent): workspaces (id, slug) and workspace_members, backfilled for a
      database that has projects and no workspaces (slug = the id as text), then the foreign keys. Each table carries a
      -- scope: tenant|directory comment, the first instance of ADR-0041's marker.
    • membership(slug, project, user) returns Member { workspace_id, role }, NotAMember or NoSuchProject.
    • Every slice route gains /workspaces/{slug}, built from one SLICE_PREFIX constant that stays /api/v1 until chore(api): move the slice's routes out of the /api/v1 prefix; reserve it for wire-compatible routes #47.
  2. feat(ports): sessions are Database rows.
    • Migration 0004_sessions.sql: token_hash (32 bytes, checked), user_id, created_at, expires_at,
      revoked_at, last_seen_at. No plaintext column and no workspace column.
    • session, touch_session, revoke_session and insert_session take no proof type: like membership they run
      before a Scope exists. thrust-core hashes the token (hash_token), so no adapter sees one.
    • authenticate reads the live row on every request and rewrites last_seen_at only when it is an hour old.
  3. test(contract): a revoked session is rejected on the next request, on every target.
    POST /internal/revoke-session (test routes and the admin token) stands in for sign-out until spike(auth): the two-day Rust auth slice (argon2id, sessions, CSRF, API keys, one OIDC login) #38. New mutant
    mutant_ignore_revoked.
  4. docs: flows/app.md, architecture/app.md and infra.md, patterns.md, the defect matrix, and an
    "issue feat(core): user-only Principal, database-authoritative sessions, delete KvSessionStore #36" note in ADR-0052, ADR-0045 and ADR-0041.

Decisions to review

Evidence

Local, at the PR head 62d82ee unless a row says otherwise.

Check Result
cargo nextest run --workspace 208 passed
cargo test --doc --workspace 3 passed
pg_adapter under Docker 10 passed, including migration_0003_backfills_a_database_created_before_it and the_sessions_table_has_a_hash_and_no_plaintext_and_no_workspace
scripts/contract-all.sh 16 of 16 cells, 20 tests in each main pass, run at a4af81a. The two commits after it change documentation and comments only
scripts/mutants.sh 5 of 5 KILLED, with DOCKER_HOST set and no SocketNotFoundError in the Postgres run. ignore-revoked is killed by a_revoked_session_is_rejected_on_the_next_request alone
scripts/check-deps.sh exit 0
wasm32 thrust-worker builds with default features and with --no-default-features
grep -rn "KvSessionStore|SESSIONS" crates/ nothing

Generative first: an exhaustive memory matrix and core matrix over actor × slug × project; slug and session classes in
the Postgres model; a 7 × 3 × 3 × 8 = 504-combination contract matrix; exhaustive session-state matrices (known ×
revoked × expired, the expiry instant included) in the memory adapter and the core. Static cases are the pinned
upgrade-path regression for a stage-2 database, a FIPS 180-4 known answer for hash_token, and the column list.

Each new check was seen to fail when the code it guards was broken:

Break Result
Postgres: drop the workspace_members join 3 tests fail, including the model
Migration 0003: skip the member backfill the upgrade test fails with a foreign-key violation
Memory adapter: ignore workspace membership exactly the 3 non-member cases fail
Postgres: ignore revoked_at / ignore expires_at the Postgres model fails; the shrunk case for the second is a lookup at the expiry instant
sessions gains a plaintext token column the column-list test fails
authenticate always touches / never touches the 3 599 s case / the 3 600 s and 86 400 s cases fail
Memory adapter: expires_at >= now the core and memory matrices fail on the expiry instant
Memory adapter ignores revoked_at (the mutant) the revocation contract test fails

Not verified, and one thing I could not explain

  • The mdBook build could not run here (mdbook-mermaid is not installed). The edited tables were checked for column
    counts with a script that was shown to fail on a malformed table.
  • The first full contract-all.sh run (before the matrix fix) had one extra failure: sweeper_publishes_rows_the_fast_path_did_not
    on worker-memory failed inside a request. It came right after the main pass on that target failed, it passed when
    rerun alone, and it passed in the three full runs since. I found no cause and do not claim it was fixed.
  • During the work, the matrix hit the create rate limit (30 a window per user, counted before authorization) because
    the slug axis created a fresh delete victim for every combination. The slugs that can never reach a delete now share
    one live victim per project, and the item must still be live at the end.

🤖 Generated with Claude Code

aRustyDev and others added 4 commits October 23, 2026 08:40
…only

A session belongs to a user, not to a workspace (ADR-0052 round 29). Principal loses its
workspace_id, and authorize takes the route's workspace slug beside the project: the
Database::membership call resolves the slug, the caller's membership of that workspace and of
the project in one query, and returns Member { workspace_id, role } for authorize to mint the
Scope with. 404 for a slug that names no workspace, a workspace the caller is not in, or a
project outside it; 403 for a workspace member who is not a member of the project or whose
role is too low.

Migration 0003_workspaces.sql adds workspaces (id, slug) and workspace_members, backfills a
database created before it (slug = the id as text) and puts the foreign keys on; it is
idempotent like 0002, with per-table scope comments as the first instance of ADR-0041's
tenant|directory marker. The slice routes gain /workspaces/{slug}, built from one SLICE_PREFIX
constant that stays /api/v1 until #47 moves it.

Tests are generative first: an exhaustive memory matrix and core authorize matrix over actor x
slug x project, new slug classes in the Postgres model and the core model, and a slug axis in
the contract matrix (7 x 3 x 3 x 8 = 504 combinations; slugs that can never reach a delete
share one victim per project, because the create rate limit counts rejected attempts too).
a_session_is_not_bound_to_a_workspace has a 404 control, and a pinned regression covers the
upgrade path of a stage-2 database. Each guard was shown to fail when broken: the
workspace-member join, the member backfill and the memory adapter's membership filter.

Refs: plane-byd.11.2.1
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ror and KvSessionStore

The session row is authoritative and read on every request (ADR-0052 round 29, ADR-0082), so a
revocation is a row update that every target sees at once; the spike's KV store was eventually
consistent and the native server lost its sessions on restart.

Migration 0004_sessions.sql adds the directory table: a SHA-256 token_hash key (32 bytes, checked),
user_id, created_at, expires_at, revoked_at and last_seen_at, with no plaintext column and no
workspace_id. Database gains session, touch_session, revoke_session and insert_session, none of
which takes a proof type: like membership they run before a Scope exists. thrust-core hashes the
token (hash_token, sha2 0.11, already in the Worker graph through SCRAM) so no adapter sees one;
authenticate reads the live row (not revoked, expires_at > now), rewrites last_seen_at only when
it is an hour old, and returns Principal { user_id }; seed hashes the fixture's tokens; revoke_session
is the use case a sign-out will call. CoreConfig gains session_ttl_secs (30 days) and
session_touch_after_secs.

Deleted: the SessionStore trait, CacheError, MemorySessionStore, KvSessionStore, Ports.sessions and
its four construction sites, and the SESSIONS kv_namespaces block in both wrangler files.

Tests are generative first: an exhaustive memory matrix and an exhaustive core matrix over
known x revoked x expired (the expiry boundary included), session ops in the Postgres model
(lookup at, before and after expiry, touch, revoke twice, reinsert of an existing hash), and a
column-list test that fails on a plaintext or workspace column. authenticate_resolves_a_live_session_and_nothing_else
keeps its name and now covers the matrix; a lazy-touch test pins the hour. A FIPS 180-4 known
answer pins hash_token. Each guard was shown to fail when broken: the Postgres query without
revoked_at and without expires_at, an added token column, an always-touch and a never-touch
authenticate, and an off-by-one expiry in the memory adapter.

Refs: plane-byd.11.2.2
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
… every target

POST /internal/revoke-session, behind the same test-routes and admin-token gate as the other
/internal routes, revokes the session behind a bearer token through thrust_core::revoke_session. It
stands in for sign-out until #38 and is idempotent: an unknown or already revoked token is a 204.

a_revoked_session_is_rejected_on_the_next_request lists with a token (200), revokes it, and expects
401 on the very next list and on a create. Its controls: a second, unrevoked token on the same
request gets 200 before and after, and revoking again or revoking a token that was never issued
changes nothing. scripts/contract.sh now expects 20 tests in the main pass. contract-all.sh passes
16 of 16 cells, so the revocation reaches the memory and the Postgres adapters on the native server
and on the Worker (worker-cf reads the row through Hyperdrive with caching off).

mutant_ignore_revoked (the memory adapter's session lookup ignores revoked_at) is a new row in
scripts/mutants.sh, declared in thrust-adapters-memory and forwarded by thrust-server. It is killed
by that one test and no other; the four earlier mutants are still killed, with DOCKER_HOST set and
no SocketNotFoundError in the Postgres run.

Refs: plane-byd.11.2.3
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…ce slug as built

flows/app.md: every slice route carries /workspaces/{slug} (built from one SLICE_PREFIX constant that #47
will change), authentication is a hashed-token lookup of a session row read on every request, a
Principal is a user and nothing else, and the 404/403/401 table now names the slug cases; the table
notes that Plane answers 403 for an unknown slug (PQ-003), which belongs to the compat routes (#44).
/internal/seed and the new /internal/revoke-session are documented.

architecture/app.md and infra.md lose the SessionStore port, the KV binding and the claim that a
postgres server loses its sessions on restart, and infra.md lists the four migrations and the
tenant|directory markers. contributing/patterns.md describes the slug-resolving authorize and the
session calls as the second exception to "repository calls take a Scope". The defect matrix gets a
row for session defects (X15) and its access-table count is now 504; status.md notes the one migration
test.

ADR-0052, ADR-0045 and ADR-0041 get an "issue #36" note: SHA-256 without a pepper and the requirement
it puts on sign-in (#38) to mint at least 128 bits of CSPRNG output, the trait that was not kept, the
session calls as a pre-Scope exception, and the first scope markers. A code comment in hash_token
that overstated what ADR-0052 says about token entropy is corrected, and the last KV mentions in
comments and descriptions go.

The mdBook build could not run locally (mdbook-mermaid is not installed); the edited tables were
checked for column counts with a script that was shown to fail on a malformed table.

Refs: plane-byd.11.2.4
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit 1e13138 into main Oct 23, 2026
26 checks passed
@aRustyDev
aRustyDev deleted the feat/db-sessions branch October 23, 2026 20:31
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.

feat(core): user-only Principal, database-authoritative sessions, delete KvSessionStore

1 participant