Add authorized conversation create, list and history (#13) #48

Merged
amiryahaya merged 15 commits from feat/13-authorized-conversations into main 2026-10-22 21:17:47 +02:00
Owner

Implements issue #13 (C01): authorized conversation create, list and history, built on the identity (#9) and central authorization (#10) work. Follows the plan; the guide is docs/operations/conversations.md.

This does not authorize operational or classified data. OD-08 and OD-12 in the decision register are still proposals. Synthetic data and development wiring only.

What it adds

  • Routes: POST /api/v1/conversations (cookie, exact origin, CSRF token; reads no body), GET /api/v1/conversations?cursor=&limit= (default 20, max 50) and GET /api/v1/conversations/{id}/messages?before=&limit= (default 50, max 100). No totals anywhere; System messages are never returned.
  • Per-request authorization: owner scope and row-level security, then permits(Conversation, ReadHistory, ..) for the conversation and for every message with that row's own marking and policy version, plus a source check per message. Revoked messages are omitted: no redaction, no placeholder.
  • Uniform responses: absent, foreign, other-organization, revoked and malformed-id requests are byte-identical 404s (status, headers, body); malformed query/cursor/limit is a uniform 400; any dependency failure a uniform 503; everything no-store.
  • Sealed cursors: list cursors are AES-256-GCM tokens bound to the owner, so a token never exposes a withheld conversation's id or time. New required key CONVERSATION_CURSOR_KEY_HEX (32 bytes as 64 hex characters, distinct from the delivery and decoy keys, no sample value).
  • Persistence: keyset-paged list and sequence-paged messages_before in the PostgreSQL repository. Migration 0007 adds a partial index, drops the redundant old index and creates expression statistics so the planner walks the index.
  • Plumbing: Enforcer::begin returns Session<RequestAccess> (its Ready is not a grant); new crate rag-conversations (depends only on rag-domain) holds the use cases; ViewRole makes System unrepresentable in views.

Verification (this branch, local PostgreSQL 17)

  • cargo fmt --all -- --check and cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • cargo test --workspace --locked: 101 passed, 0 failed, 100 ignored (the PostgreSQL suites).
  • bash scripts/test-postgres.sh --local: all nine suites pass (admin 4, authz 13, conversations API 10, conversations revocation 6, identity 30, policy 1, foundation 21, recovery 9, revocation 6).
  • Rust line coverage 91.39% (bash scripts/coverage.sh rust, gate 80%). Web is unchanged: 22 tests and the build pass.
  • Plan tests assert the planner's own choice on realistic data (other owners' rows, ANALYZE, the repository's own SQL, no scan-type switches or index drops).
  • Revocation tests each prove the conversation is visible before the change. Mutation checks (skip a permit, cache a source answer, ignore the policy in list, unsealed cursor, dropped owner binding, System mapped to User) were each caught and reverted.
  • Every task had an independent review and fix rounds, then a whole-branch review: no Critical or Important issues. A concurrency flake in the new append test (2 of 5 runs) was fixed by capping in-flight appends below the pool size (12 of 12 and 25 of 25 passes afterwards).

Please note

  • #11 (protected audit trail) is not built. Denials are not audited; each operation has an AUDIT(#11) hook comment where an event will be emitted.
  • The OD-12 register text was edited to "withhold entirely, omit with no placeholder". It remains proposed; the security authority and records owner should look at this change explicitly. Nothing here is approved.
  • The development-only policy snapshot and the placeholder grant defaults ({Read, CreateConversation, ReadHistory}) stay placeholders until policy administration (#14). Production startup is still blocked, so they cannot take effect there.
  • Until documents exist (#17), every message that cites sources is withheld (DenyAllSources).
  • The list's bounded plan relies on ANALYZE having run; a freshly migrated table before its first autovacuum could still get the slower plan.
  • Known disclosure limits, documented in the guide: a full raw page with fewer survivors, and the ordering between pages, let the owner infer that some of their own rows are withheld and bound their last-activity time. Absent versus foreign lookups can still differ in database timing.
  • POST refusals are 401/403 rather than the uniform 404 (they depend on no conversation); the 401-versus-404 and lapsed-policy mappings remain open decisions from the authorization guide.
  • Follow-ups (not in this PR): a full-raw-page-with-partial-survivors list test, enum-based revocation cases, a testable key-distinctness check, a generic-plan EXPLAIN guard, and checked_mul for the grant lifetime.
  • Two known flaky tests exist in untouched suites: the postgres.rs readiness test and the recovery_postgres rate-limit test.
  • Needs independent human review before merge.

Refs #13, #11, #10, #9, #2.

🤖 Generated with Claude Code

Implements issue #13 (C01): authorized conversation create, list and history, built on the identity (#9) and central authorization (#10) work. Follows the [plan](docs/superpowers/plans/2026-10-23-authorized-conversations.md); the guide is [`docs/operations/conversations.md`](docs/operations/conversations.md). **This does not authorize operational or classified data.** OD-08 and OD-12 in the [decision register](docs/decisions.md) are still proposals. Synthetic data and development wiring only. ## What it adds - **Routes:** `POST /api/v1/conversations` (cookie, exact origin, CSRF token; reads no body), `GET /api/v1/conversations?cursor=&limit=` (default 20, max 50) and `GET /api/v1/conversations/{id}/messages?before=&limit=` (default 50, max 100). No totals anywhere; `System` messages are never returned. - **Per-request authorization:** owner scope and row-level security, then `permits(Conversation, ReadHistory, ..)` for the conversation and for every message with that row's own marking and policy version, plus a source check per message. Revoked messages are omitted: no redaction, no placeholder. - **Uniform responses:** absent, foreign, other-organization, revoked and malformed-id requests are byte-identical 404s (status, headers, body); malformed query/cursor/limit is a uniform 400; any dependency failure a uniform 503; everything `no-store`. - **Sealed cursors:** list cursors are AES-256-GCM tokens bound to the owner, so a token never exposes a withheld conversation's id or time. New required key `CONVERSATION_CURSOR_KEY_HEX` (32 bytes as 64 hex characters, distinct from the delivery and decoy keys, no sample value). - **Persistence:** keyset-paged `list` and sequence-paged `messages_before` in the PostgreSQL repository. Migration `0007` adds a partial index, drops the redundant old index and creates expression statistics so the planner walks the index. - **Plumbing:** `Enforcer::begin` returns `Session<RequestAccess>` (its `Ready` is not a grant); new crate `rag-conversations` (depends only on `rag-domain`) holds the use cases; `ViewRole` makes `System` unrepresentable in views. ## Verification (this branch, local PostgreSQL 17) - `cargo fmt --all -- --check` and `cargo clippy --workspace --all-targets --locked -- -D warnings`: clean. - `cargo test --workspace --locked`: 101 passed, 0 failed, 100 ignored (the PostgreSQL suites). - `bash scripts/test-postgres.sh --local`: all nine suites pass (admin 4, authz 13, conversations API 10, conversations revocation 6, identity 30, policy 1, foundation 21, recovery 9, revocation 6). - Rust line coverage 91.39% (`bash scripts/coverage.sh rust`, gate 80%). Web is unchanged: 22 tests and the build pass. - Plan tests assert the planner's own choice on realistic data (other owners' rows, `ANALYZE`, the repository's own SQL, no scan-type switches or index drops). - Revocation tests each prove the conversation is visible before the change. Mutation checks (skip a permit, cache a source answer, ignore the policy in `list`, unsealed cursor, dropped owner binding, System mapped to User) were each caught and reverted. - Every task had an independent review and fix rounds, then a whole-branch review: no Critical or Important issues. A concurrency flake in the new append test (2 of 5 runs) was fixed by capping in-flight appends below the pool size (12 of 12 and 25 of 25 passes afterwards). ## Please note - **#11 (protected audit trail) is not built.** Denials are not audited; each operation has an `AUDIT(#11)` hook comment where an event will be emitted. - **The OD-12 register text was edited** to "withhold entirely, omit with no placeholder". It remains `proposed`; the security authority and records owner should look at this change explicitly. Nothing here is approved. - The development-only policy snapshot and the placeholder grant defaults ({Read, CreateConversation, ReadHistory}) stay placeholders until policy administration (#14). Production startup is still blocked, so they cannot take effect there. - Until documents exist (#17), every message that cites sources is withheld (`DenyAllSources`). - The list's bounded plan relies on `ANALYZE` having run; a freshly migrated table before its first autovacuum could still get the slower plan. - Known disclosure limits, documented in the guide: a full raw page with fewer survivors, and the ordering between pages, let the owner infer that some of their own rows are withheld and bound their last-activity time. Absent versus foreign lookups can still differ in database timing. - POST refusals are 401/403 rather than the uniform 404 (they depend on no conversation); the 401-versus-404 and lapsed-policy mappings remain open decisions from the authorization guide. - Follow-ups (not in this PR): a full-raw-page-with-partial-survivors list test, enum-based revocation cases, a testable key-distinctness check, a generic-plan EXPLAIN guard, and `checked_mul` for the grant lifetime. - Two known flaky tests exist in untouched suites: the `postgres.rs` readiness test and the `recovery_postgres` rate-limit test. - Needs independent human review before merge. Refs #13, #11, #10, #9, #2. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Five test-first tasks for issue 13: keyset-paged list and message
history reads in the repository, per-request access from the Enforcer,
the conversation use cases with withhold-on-revocation, the three API
routes with boundary tests, and revocation, concurrency and
documentation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Add POST/GET /api/v1/conversations and GET /api/v1/conversations/{id}/messages
over Enforcer::begin and ConversationService, with strict query parsing,
the uniform 404/400/503 outcomes and Cache-Control: no-store everywhere.
Create needs the session cookie, exact origin and CSRF token and reads no
body field. Implement AccessCheck for RequestAccess and an AES-256-GCM list
cursor sealer keyed by the new CONVERSATION_CURSOR_KEY_HEX. main.rs mounts
the routes only with identity, with DenyAllSources and a development-only
static policy snapshot. Add the real-PostgreSQL boundary suite and register
it in the PostgreSQL and coverage runners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A malformed id on the history route is now the same uniform 404 as an
absent id, decided after begin; only the query or cursor can yield a 400
(the history limit range is checked before the id). Development grants and
the development policy share one action set: Read, CreateConversation and
ReadHistory, a placeholder until #14. The boundary suite pins revoked-like
cases (marking above the ceiling, no ReadHistory) and malformed ids as
byte-identical 404s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
test(conversations): keep concurrent appends inside the connection pool
All checks were successful
CI / Web checks (pull_request) Successful in 56s
CI / Web checks (push) Successful in 1m15s
CI / Rust checks (pull_request) Successful in 12m23s
CI / Rust checks (push) Successful in 17m18s
503f8de609
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.