Add authorized document registration (#17) #50

Merged
amiryahaya merged 9 commits from feat/17-document-registration into main 2026-10-23 04:40:15 +02:00
Owner

Closes #17.

What this adds

Authorized document registration. A custodian with an Upload grant registers UTF-8 text or Markdown with a full marking; every accepted version is quarantined and nothing is searchable yet (extraction/publication is #18).

  • POST /api/v1/documents, POST /api/v1/documents/{id}/versions, GET /api/v1/documents/{id} (201 / 200 replay / 400 / 401 / 403 / 404 / 409 / 413 / 503; no-store). Idempotency-Key is mandatory.
  • One transaction writes the version, original bytes (bytea, INSERT-only), ingestion job, outbox event document.version.registered (ids only) and an audit row (version/size/hash only; no titles, labels or content).
  • Quarantine is enforced by RLS: the runtime role can only insert quarantined versions and can only select active ones.
  • rag-domain gains the DocumentRegistry port; new crate rag-ingestion holds validation and the service; rag-storage-postgres implements the registry; rag-api adds routes and an oracle-safe DocumentLoader.
  • Migration 0008_documents.sql: composite org FKs, FORCE RLS, INSERT-only grants on originals/jobs/audit_events/ingestion_outbox; the startup privilege check now covers column-level grants.
  • Docs: docs/operations/documents.md, plan in docs/superpowers/plans/2026-10-23-document-registration.md, evidence sentences in docs/decisions.md OD-07/OD-13 (status unchanged, still proposals).

Verification

cargo fmt --check, clippy --workspace --all-targets -D warnings, cargo test --workspace --locked pass, and all 12 real-PostgreSQL suites pass via bash scripts/test-postgres.sh --local (new: schema 11, registry 14, api 18). The coverage script was not run locally; CI will run it (it now includes the three new suites).

Disclosure: on one full run, two tests in documents_registry_postgres failed once (refused_writes_one_audit_row_with_reason_only, active_attributes_hides_quarantined_and_returns_latest_active). The output was not captured and I could not reproduce it (about ten reruns, standalone and in the full script, all green). Cause unexplained.

Rulings made during implementation

  1. document_submissions also stores version, size and content hash so a replay rebuilds the receipt without reading quarantined rows. Cost if wrong: additive migration.
  2. replayed is computed at read time, never stored.
  3. Registry NotFound / InvalidInput refusals are audited best-effort (NotPermitted / InvalidContent).
  4. DOCUMENT_MAX_BYTES (default 2097152) is validated only when identity is enabled, since the routes mount only then.
  5. Deferred: an anonymous caller can make the server buffer up to the body limit (about 12 MiB by default) before the session check. Loopback-only and production startup is blocked by #2; fix before any non-loopback exposure by taking Body and reading after session and key checks.
  6. A marking without group returns the uniform 404 (policy denial), not 400. Documented; nothing stored.

Also from the plan: dev grant defaults/policy gain Upload (placeholder until #14); revisions are owner-only; originals live in PostgreSQL rather than a filesystem/object store.

Deferred minors

No-group 404 vs 400; ActiveDocument Debug includes the title; originals/jobs RLS bind org only (owner bound in the registry transaction); next_version can be set backwards by the owner; column-privilege hardening for older tables (pre-existing); a few test-coverage gaps.

Synthetic data only; no production or approval claims.

🤖 Generated with Claude Code

Closes #17. ## What this adds Authorized document registration. A custodian with an `Upload` grant registers UTF-8 text or Markdown with a full marking; every accepted version is **quarantined** and nothing is searchable yet (extraction/publication is #18). - `POST /api/v1/documents`, `POST /api/v1/documents/{id}/versions`, `GET /api/v1/documents/{id}` (201 / 200 replay / 400 / 401 / 403 / 404 / 409 / 413 / 503; no-store). `Idempotency-Key` is mandatory. - One transaction writes the version, original bytes (`bytea`, INSERT-only), ingestion job, outbox event `document.version.registered` (ids only) and an audit row (version/size/hash only; no titles, labels or content). - Quarantine is enforced by RLS: the runtime role can only insert `quarantined` versions and can only select `active` ones. - `rag-domain` gains the `DocumentRegistry` port; new crate `rag-ingestion` holds validation and the service; `rag-storage-postgres` implements the registry; `rag-api` adds routes and an oracle-safe `DocumentLoader`. - Migration `0008_documents.sql`: composite org FKs, FORCE RLS, INSERT-only grants on originals/jobs/`audit_events`/`ingestion_outbox`; the startup privilege check now covers column-level grants. - Docs: `docs/operations/documents.md`, plan in `docs/superpowers/plans/2026-10-23-document-registration.md`, evidence sentences in `docs/decisions.md` OD-07/OD-13 (status unchanged, still proposals). ## Verification `cargo fmt --check`, `clippy --workspace --all-targets -D warnings`, `cargo test --workspace --locked` pass, and all 12 real-PostgreSQL suites pass via `bash scripts/test-postgres.sh --local` (new: schema 11, registry 14, api 18). The coverage script was not run locally; CI will run it (it now includes the three new suites). **Disclosure:** on one full run, two tests in `documents_registry_postgres` failed once (`refused_writes_one_audit_row_with_reason_only`, `active_attributes_hides_quarantined_and_returns_latest_active`). The output was not captured and I could not reproduce it (about ten reruns, standalone and in the full script, all green). Cause unexplained. ## Rulings made during implementation 1. `document_submissions` also stores version, size and content hash so a replay rebuilds the receipt without reading quarantined rows. Cost if wrong: additive migration. 2. `replayed` is computed at read time, never stored. 3. Registry NotFound / InvalidInput refusals are audited best-effort (NotPermitted / InvalidContent). 4. `DOCUMENT_MAX_BYTES` (default 2097152) is validated only when identity is enabled, since the routes mount only then. 5. **Deferred:** an anonymous caller can make the server buffer up to the body limit (about 12 MiB by default) before the session check. Loopback-only and production startup is blocked by #2; fix before any non-loopback exposure by taking `Body` and reading after session and key checks. 6. A marking without `group` returns the uniform 404 (policy denial), not 400. Documented; nothing stored. Also from the plan: dev grant defaults/policy gain `Upload` (placeholder until #14); revisions are owner-only; originals live in PostgreSQL rather than a filesystem/object store. ## Deferred minors No-group 404 vs 400; `ActiveDocument` Debug includes the title; originals/jobs RLS bind org only (owner bound in the registry transaction); `next_version` can be set backwards by the owner; column-privilege hardening for older tables (pre-existing); a few test-coverage gaps. Synthetic data only; no production or approval claims. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
POST /api/v1/documents and /api/v1/documents/{id}/versions register
quarantined versions through the ingestion service after origin, CSRF,
session and Idempotency-Key checks; GET /api/v1/documents/{id} returns
the latest active version's metadata through the Enforcer with the new
database-backed DocumentLoader, building the body from the same read the
decision was made on. Absent, foreign, quarantined and corrupt documents
are the uniform 404; store failures are 503 for every id alike.

main.rs mounts the routes with identity, reads DOCUMENT_MAX_BYTES
(default 2097152, invalid stops startup) and adds Upload to the
development grants and policy. Real-PostgreSQL suite
documents_api_postgres is registered in scripts/test-postgres.sh.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
fix: audit registry refusals, include document suites in coverage, correct docs
All checks were successful
CI / Web checks (push) Successful in 41s
CI / Web checks (pull_request) Successful in 54s
CI / Rust checks (push) Successful in 11m25s
CI / Rust checks (pull_request) Successful in 13m25s
1e8ade896c
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.