Repository navigation
Description
Context
The permission seam in crates/thrust-core/src/lib.rs mints a Scope only through authorize (lines 157-175), and the type's doc comment (lines 136-137) says that repository calls taking a Scope "cannot be reached unchecked". No repository call takes one. The Database port (crates/thrust-ports/src/lib.rs:92-138) and Tx (lines 142-157) take bare WorkspaceId and ProjectId, and every use-case unwraps the scope through its public getters (crates/thrust-core/src/lib.rs:145-155) right before the call (lines 252, 312, 360, 374, 488). Two adapters implement the port: MemoryDatabase (crates/thrust-adapters-memory/src/lib.rs) and PgDatabase (crates/thrust-adapters-postgres/src/lib.rs).
Problem
Tenant scoping is enforced by convention, not by types.
- Any caller that has the ids can call the port directly.
Scopehas private fields butpub fn workspace_id(),project_id()anduser_id()hand them back (crates/thrust-core/src/lib.rs:145-155); they are also used from outside the crate (crates/thrust-core/tests/authorize.rs:46). Tx::allocateignores_wsin both adapters (crates/thrust-adapters-postgres/src/lib.rs:399,crates/thrust-adapters-memory/src/lib.rs:205), andproject_sequenceshas noworkspace_idcolumn (crates/thrust-adapters-postgres/migrations/0001_init.sql:20-24).- The membership query joins
project_memberswithoutm.workspace_id(crates/thrust-adapters-postgres/src/lib.rs:189-191). undispatched_outbox,mark_outbox_dispatched,outbox_depthandseedtake no tenant, andinsert_activitycarries one only inside the record (crates/thrust-ports/src/lib.rs:119-137).docs/src/architecture/app.md:80already records this.- The plan says otherwise: "repositories accept only a
Scope" (ADR-0051 round 24, line 102; ADR-0041 round 25, line 72), and the ADR-0045 sketch hasbegin(&self, scope: Scope)(ADR-0045:84). - The
ignore-tenantmutant (scripts/mutants.sh:30,crates/thrust-adapters-memory/src/lib.rs:66-70) is killed today only because the one use-case passes the right ids. ADR-0141 names tenant-isolation bugs the highest-impact class (ADR-0141:16). Every later repository copies this port shape, so it is cheapest to change while one aggregate exists.
Proposal
Implements ADR-0041, ADR-0045 and ADR-0051 (round-29 notes).
- Every
DatabaseandTxmethod takes&Scope, never bare ids, andbegintakes the scope as in the ADR-0045 sketch. The adapter derivesworkspace_idandproject_idfrom the scope. - ADR-0045's round-29 note (line 116) leaves one decision to this issue: where the proof type is declared.
thrust-portscannot name a type defined inthrust-core(core depends on ports), and the adapters have to read the ids. SoScopemoves down tothrust-portswith private fields and read accessors. Once the port takes&Scope, ids read back out of a scope can no longer reach it. What has to be enforced is that onlyauthorizemints aScope: give it one constructor, and forbid calls to it anywhere else with clippydisallowed-methods(run by the clippy job from chore(ci): enforce the clippy deny list,cargo fmt --checkandcargo-deny; forbidsqlx,sea-orm,dieselincheck-deps.sh#40) or a grep rule inscripts/check-deps.sh. - Untenanted operations take a separate
&SystemScope, minted only by the two composition roots and the queue consumer (ADR-0045:110): the relay and the sweeper (undispatched_outbox,mark_outbox_dispatched,outbox_depth), the consumer'sinsert_activity, andseed. The consumer authorises by the tenant id the envelope carries (ADR-0141:40). - Add
workspace_idtoproject_sequences(key(workspace_id, project_id)) andAND m.workspace_id = p.workspace_idto the membership join, in both adapters.0001_init.sqlis the spike schema, not the product schema (ADR-0042), so either edit it in place or add0002. - Update
docs/src/architecture/app.md:80. Indocs/src/contributing/patterns.md, the "Repository calls take aScope, never ids" paragraph (round 29) currently says "not yet the code"; change it to describe the code once this lands, and update the "Authorize in the core" and "Scope every tenant query" paragraphs to match.
Acceptance criteria
-
grep -nE 'ws: WorkspaceId|project: ProjectId' crates/thrust-ports/src/lib.rsmatches onlymembership's arguments; every otherDatabaseandTxmethod takes&Scopeor&SystemScope. (Amended 2026-10-22, owner decision:membershipruns before aScopeexists, onlyauthorizecalls it, and feat(core): user-onlyPrincipal, database-authoritative sessions, deleteKvSessionStore#36 reshapes it around the workspace slug; ADR-0045 round 29 already names it the exception.) - Both adapters use the workspace in
allocate,project_sequenceshasworkspace_id, and the membership join testsm.workspace_id. - A
compile_faildoctest inthrust-core, run bycargo test --doc --workspace, shows that a use-case can neither build aScopenor call a repository method with bare ids. A plain doctest of the same call throughauthorizecompiles; it is the control. - Adding a call to the
Scopeconstructor in a use-case fails the lint or the grep rule. Show the failing run once. -
model_work_items_match_the_reference_model(two tenants,crates/thrust-core/tests/model.rs:144) andcross_tenant_access_is_rejected_and_has_no_effect(crates/thrust-contract-tests/tests/contract.rs:136) stay green on all four targets, andscripts/mutants.shstill reportsignore-tenantKILLED. -
cargo nextest run --workspaceandscripts/contract-all.shpass, andgateis green.
References
- Review: .agents/plans/contrail/reviews/2026-10-19-fable-architecture-review.md (FR-02)
crates/thrust-ports/src/lib.rs:92-157;crates/thrust-core/src/lib.rs:136-175;crates/thrust-adapters-postgres/src/lib.rs:189-191,399;crates/thrust-adapters-memory/src/lib.rs:66-70,205;crates/thrust-adapters-postgres/migrations/0001_init.sql:20-24- ADR-0041 (
.agents/plans/contrail/adrs/data/base/0041-tenant-isolation-strategy.md:42,72), ADR-0045 (adrs/data/interfaces/0045-repository-and-unit-of-work-ports.md:19,84,107-116), ADR-0051 (adrs/auth/0051-authorization-model-rbac-rebac.md:102), ADR-0141 (adrs/security/0141-tenancy-and-threat-model.md:16,40,51) - SPEC R-S1;
docs/src/architecture/app.md:80;docs/src/contributing/patterns.md;scripts/mutants.sh:30
Finding: FR-02
Activity
Principal, database-authoritative sessions, deleteKvSessionStore#36worker-pgcell and an instance-levelworker-doSQLite cell #37