Skip to content

feat(ports): repository and Tx methods take a Scope or a SystemScope, never ids - #73

Merged
aRustyDev merged 5 commits into
mainfrom
feat/scope-port-argument
Oct 23, 2026
Merged

aRustyDev merged 5 commits into
mainfrom
feat/scope-port-argument

Conversation

@aRustyDev

Copy link
Copy Markdown
Owner

Closes #35 (FR-02).

Tenant scoping moves from convention to the types. Every Database and Tx method now takes &Scope (minted only by
authorize) or &SystemScope (minted only by the composition roots), never bare WorkspaceId/ProjectId. The two
places that ignored the tenant key now use it.

Commits (one per bead)

  1. refactor(ports): proof types in thrust-ports. thrust-ports cannot name a type from thrust-core, so
    Scope moves down, with private fields, read accessors and one constructor (Scope::mint). SystemScope is new.
    thrust-core re-exports both.
  2. fix(adapters): tenant key in the data.
    • Migration 0002_sequence_tenant_key.sql (idempotent):
      • adds project_sequences.workspace_id, backfilled;
      • keys the table on (workspace_id, project_id);
      • adds a composite FK to projects (workspace_id, id), so an allocation under the wrong workspace is refused.
    • The membership join requires m.workspace_id = p.workspace_id.
    • The memory adapter matches.
    • It is a new file because 0001 is CREATE ... IF NOT EXISTS and would leave an existing table unchanged.
  3. feat(ports): the port takes the proofs.
    • begin(&Scope).
    • Each Tx write takes the &Scope it writes under. Binding a transaction to a single scope would have broken
      transactions that span projects.
    • insert_work_item and insert_outbox refuse a row of another tenant (check_tenant).
    • The untenanted calls take &SystemScope. App::new receives it from the composition root and keeps it
      private. thrust-api's /internal/seed and /internal/outbox-depth go through thrust_core::seed and
      thrust_core::outbox_depth.
  4. test(core): guards.
  5. docs: patterns.md, architecture/app.md, and an ADR-0045 note recording where the proofs live and who
    mints them.

Deviation from the issue text, agreed with the owner

membership keeps its id arguments. It runs before a Scope exists, only authorize calls it, and #36 reshapes it
around the workspace slug. Criterion 1 on #35 was amended to match.

Evidence

All local, at the PR head:

Check Result
cargo nextest run --workspace 122 passed. The total includes 20 new exhaustive cases in thrust-adapters-memory/tests/tenant.rs.
cargo test --doc --workspace 3 passed: 2 compile_fail doctests and their control. Turned into plain doctests, the two fail with E0451 (private fields) and E0061 (bare ids), so neither passes on an unrelated error.
pg_adapter under Docker 8 passed. The coverage control reaches the 2 new step classes.
scripts/contract-all.sh 16 of 16 cells pass on native-memory, native-pg, worker-memory and worker-cf, including cross_tenant_access_is_rejected_and_has_no_effect.
scripts/mutants.sh All 4 mutants KILLED, ignore-tenant included.
scripts/check-deps.sh exit 0.
wasm32 thrust-worker Builds with default features and with --no-default-features.

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

Break Result
Postgres: drop the m.workspace_id join predicate The model fails on the stray member row.
Postgres: drop the composite FK The model fails on the wrong-workspace allocation, which returned sequence 1.
Memory adapter: remove both checks Exactly the 3 matrix cases they govern fail.
Grep rule: plant Scope::mint in a use-case and SystemScope::mint in thrust-api Both reported, exit 1.

One thing found on the way, not fixed here: scripts/mutants.sh counts the Postgres mutant as KILLED even when
testcontainers cannot reach Docker. With OrbStack and no DOCKER_HOST, both tests panic on SocketNotFoundError
before the mutant runs. The run above set DOCKER_HOST and was confirmed to fail on the mutant: an abandoned
transaction leaves sequence 41 where 1 is expected. Without DOCKER_HOST, that KILLED is vacuous.

🤖 Generated with Claude Code

aRustyDev and others added 5 commits October 22, 2026 21:10
…ust-ports

The ports must take the proof type that authorize mints, and thrust-ports cannot name a type
from thrust-core, so Scope moves down with private fields, read accessors and one constructor,
Scope::mint, which only authorize calls. SystemScope is the proof for the untenanted operations
(outbox relay and sweeper, the consumer's activity write, seed). thrust-core re-exports both.
The port signatures are unchanged here; they take the proofs in a following commit.

Refs: plane-byd.11.1.1
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ership join

Tx::allocate ignored its workspace in both adapters, and project_sequences had no workspace_id,
so an allocation naming the wrong workspace started a counter for another tenant's project.
Migration 0002 adds project_sequences.workspace_id (backfilled), re-keys the table on
(workspace_id, project_id) and adds a composite foreign key to projects (workspace_id, id), so
the Postgres adapter refuses that allocation. It is a separate file because 0001 is CREATE ...
IF NOT EXISTS and would leave an existing table unchanged; every statement in 0002 is idempotent.
The membership join now also requires m.workspace_id = p.workspace_id. The memory adapter keeps
the same answers.

Tests: the generated Postgres call sequences gain an allocation under the other tenant's
workspace and a stray member row written with another workspace (both classes reached by the
coverage control); the memory adapter gets an exhaustive matrix over the same domain. Controls:
dropping the join predicate and dropping the composite foreign key each fail the Postgres
model; removing both checks from the memory adapter fails exactly the three matrix cases they
govern.

Refs: plane-byd.11.1.2
Co-Authored-By: Claude Opus 5.5 <[email protected]>
… never ids

Every Database and Tx method now takes &Scope, minted only by authorize, from which the adapter
reads the tenant key, or &SystemScope for the untenanted calls (undispatched_outbox,
mark_outbox_dispatched, outbox_depth, insert_activity, seed). membership keeps its ids: it runs
before a Scope exists and only authorize calls it (ADR-0045 round 29; #35 criterion amended).

begin takes the Scope as proof of authorisation, and each Tx write takes the Scope it writes
under, so a transaction can still span the projects its caller holds proofs for. insert_work_item
and insert_outbox refuse a row of another tenant (check_tenant in thrust-ports), in both adapters.

The composition roots (thrust-server; thrust-worker, both adapter sets) mint the SystemScope and
hand it to App::new, which keeps it private; the relay, the sweeper, the consumer and seed read it
there. thrust-api's /internal/seed and /internal/outbox-depth go through thrust_core::seed and
thrust_core::outbox_depth instead of calling the port.

Verified: native and both wasm32 Worker builds; cargo nextest run --workspace 122/122; doctests;
pg_adapter 8/8 under Docker; scripts/contract-all.sh 16/16 cells on all four targets.

Refs: plane-byd.11.1.3
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…s mint a SystemScope

Two compile_fail doctests on authorize show that a use-case can neither build a Scope by struct
literal (E0451, private fields) nor call a repository method with bare ids (E0061); a doctest of
the same imports and call through authorize compiles and is the control. Turning both into plain
doctests fails them with exactly those two errors, so neither passes on an unrelated error.

The constructor Scope::mint is pub (the ports crate declares the type and core must call it), so
it is reachable from a doctest. scripts/check-deps.sh, which CI runs, therefore rejects
Scope::mint outside authorize and SystemScope::mint outside the composition roots; tests and
comment lines are exempt. Control: a Scope::mint planted in list_activity and a SystemScope::mint
planted in thrust-api's seed handler were both reported and the script exited 1; restored, it
exits 0. Clippy's disallowed-methods replaces the grep once the clippy job exists (#40).

Refs: plane-byd.11.1.4
Co-Authored-By: Claude Opus 5.5 <[email protected]>
patterns.md's three tenancy paragraphs said the Scope argument was "the decided shape, not yet
the code"; they now describe the code: where the proofs are declared, who mints each, the
membership exception, the tenant checks in the data and in each Tx write, and the two guards.
architecture/app.md's port table matches. ADR-0045 gets an issue #35 note recording the decisions
the round-29 note left open: the proofs live in thrust-ports, minting is enforced by the
check-deps rule, App holds the SystemScope, Tx writes take their scope, membership keeps its ids.
The issue's first acceptance criterion was amended on GitHub to match the membership exception.

Refs: plane-byd.11.1.5
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@aRustyDev
aRustyDev merged commit d12cb5d into main Oct 23, 2026
26 checks passed
@aRustyDev
aRustyDev deleted the feat/scope-port-argument branch October 23, 2026 01:49
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): make Scope the argument of every repository and Tx method

1 participant