Repository navigation
feat(ports): repository and Tx methods take a Scope or a SystemScope, never ids - #73
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #35 (FR-02).
Tenant scoping moves from convention to the types. Every
DatabaseandTxmethod now takes&Scope(minted only byauthorize) or&SystemScope(minted only by the composition roots), never bareWorkspaceId/ProjectId. The twoplaces that ignored the tenant key now use it.
Commits (one per bead)
refactor(ports): proof types inthrust-ports.thrust-portscannot name a type fromthrust-core, soScopemoves down, with private fields, read accessors and one constructor (Scope::mint).SystemScopeis new.thrust-corere-exports both.fix(adapters): tenant key in the data.0002_sequence_tenant_key.sql(idempotent):project_sequences.workspace_id, backfilled;(workspace_id, project_id);projects (workspace_id, id), so an allocation under the wrong workspace is refused.m.workspace_id = p.workspace_id.CREATE ... IF NOT EXISTSand would leave an existing table unchanged.feat(ports): the port takes the proofs.begin(&Scope).Txwrite takes the&Scopeit writes under. Binding a transaction to a single scope would have brokentransactions that span projects.
insert_work_itemandinsert_outboxrefuse a row of another tenant (check_tenant).&SystemScope.App::newreceives it from the composition root and keeps itprivate.
thrust-api's/internal/seedand/internal/outbox-depthgo throughthrust_core::seedandthrust_core::outbox_depth.test(core): guards.compile_faildoctests show that a struct-literalScopeand a repository call with bare ids do not compile,each with a compiling control.
scripts/check-deps.shrejectsScope::mintoutsideauthorize, andSystemScope::mintoutside thecomposition roots. A
pubconstructor inthrust-portsis reachable from a doctest, so the compile-fail halfcovers the literal and bare ids, and the grep covers the constructor. Clippy's
disallowed-methodsreplaces thegrep with chore(ci): enforce the clippy deny list,
cargo fmt --checkandcargo-deny; forbidsqlx,sea-orm,dieselincheck-deps.sh#40.docs:patterns.md,architecture/app.md, and an ADR-0045 note recording where the proofs live and whomints them.
Deviation from the issue text, agreed with the owner
membershipkeeps its id arguments. It runs before aScopeexists, onlyauthorizecalls it, and #36 reshapes itaround the workspace slug. Criterion 1 on #35 was amended to match.
Evidence
All local, at the PR head:
cargo nextest run --workspacethrust-adapters-memory/tests/tenant.rs.cargo test --doc --workspacecompile_faildoctests 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_adapterunder Dockerscripts/contract-all.shcross_tenant_access_is_rejected_and_has_no_effect.scripts/mutants.shignore-tenantincluded.scripts/check-deps.shthrust-worker--no-default-features.Each new check was also seen to fail when the code it guards was broken:
m.workspace_idjoin predicateScope::mintin a use-case andSystemScope::mintinthrust-apiOne thing found on the way, not fixed here:
scripts/mutants.shcounts the Postgres mutant as KILLED even whentestcontainers cannot reach Docker. With OrbStack and no
DOCKER_HOST, both tests panic onSocketNotFoundErrorbefore the mutant runs. The run above set
DOCKER_HOSTand was confirmed to fail on the mutant: an abandonedtransaction leaves sequence 41 where 1 is expected. Without
DOCKER_HOST, that KILLED is vacuous.🤖 Generated with Claude Code