Skip to content

fix: members invite through the plugin's endpoints again, the operator alone through its adapter; close step 4.2 - #75

Merged
leandrotcawork merged 4 commits into
mainfrom
fix/conexus-e042-native-invitations
Oct 17, 2026
Merged

leandrotcawork merged 4 commits into
mainfrom
fix/conexus-e042-native-invitations

Conversation

@leandrotcawork

Copy link
Copy Markdown
Collaborator

The founder's answers on the line-by-line code audit of step 4.2 ("1. A", "2. Okay"), its Fechamento items, and step 4.2's closing (delivery L5, L19). Step 5's PR #54 is red on the docs check until docs/steps/ holds one card; this PR removes 4.2's.

Bloqueia 1, audit 2.9: members invite through the plugin's endpoints again

  • A member's invitation, cancel and renewal go through auth.api.createInvitation, auth.api.cancelInvitation and auth.api.createInvitation with resend: true, as in step 4.1, inside the Hub's transaction (written, cancelThrough in invitations.ts; renewed in invitation-management.ts). The library's refusals map as in 4.1 (INVITATION_REFUSALS).
  • The plugin's adapter (getOrgAdapter) stays on the operator's path only: registerCompanyOn/renameCompanyOn, and the first owner's invitation, cancel and renewal when the admission is OwnerInviterProof.
  • Removed from the member path, now native: checkNotMember (deleted: a company the operator may invite into has no member, so the operator path never needed it), the member's role check (mayInviteAs now only feeds the company page's read, as in 4.1), the cancel admitted by invitation:create (the endpoint checks invitation:cancel again), and invitationExpiry (kept only for the operator's renewal).
  • The lock order is unchanged: lockCompany first everywhere.
  • Proof the member path uses the endpoint: removal row invitation-member-native-endpoint writes a member's invitation through the adapter instead; "convidar: o administrador pedindo dono…" in invitations.test.ts catches it, because the Hub no longer checks the creator role or an existing member itself.
  • docs/reference/boundaries.md says exactly this, with the founder's answer.

Bloqueia 2, audit 2.14, rule C14: no catch in a handler

mailInvitation returns MailOutcome, { kind: 'SENT' } | { kind: 'BOUNCED' } (C49), and still marks a bounce as mailed. The registration handler in operations/routes.ts maps it to mailBounced with no try; the member routes in company-routes.ts map BOUNCED to MAIL_BOUNCED through deliveredOrRefused, so their answers are unchanged.

Fechamento

  • 2.12, C27: clientsOf, list and readOperatedCompanies build typed objects from rows parsed at the database boundary; serve validates the answer at the exit. ClientSummary.parse no longer strips fields halfway.
  • 2.3: the owner-is-not-operator rule has one owner, identity: checkOwnerNotOperator, called in registerCompanyOn and in the operator's next owner invitation. isOperator leaves the Central's ports.
  • 2.8: isOperator normalizes with storedAddress.
  • 2.3: the operator's proofs (OperatorProof, OwnerInviterProof) carry the user row their admission read and locked, and the adapter names it as inviter, so the registration and the owner invitation no longer read and lock that row twice.

Step 4.2's closing (L19)

  • docs/architecture.md "Roll back": the D6 exception, P6 B's one-time deletion.
  • Security S6 and S11: the operator acts on a client's company as a fact of the person, never as a member.
  • Decision 0015: a company with no member awaits its first owner.
  • Testing T5: the test Hub exemption that the dependency-cruiser rule already makes.
  • Card amendments, kept where they now live since the card leaves: the member/operator invitation split (boundaries); registerClient answers 201 with mailBounced instead of MAIL_BOUNCED, and the bounced address shows as an ordinary "Aguardando o administrador" invitation once the note is gone (boundaries, the e-mail entry).
  • Roadmap: the use-line placeholder for 4.2 (the founder's use is still to come) and 4.2's row leaves the next-steps table, as 4.1's closing did.
  • The card docs/steps/e042-cadastro-de-cliente.md leaves docs/steps/ (L5).
  • Removal rows repointed to the moved code: client-owner-not-operator, owner-inviter-is-operator, invitation-bounce-marked; all 20 rows on the changed files are caught locally.

Not in this PR, still open: the founder's confirmation of the P6 test-company list before the release; issues #60 to #64.

Checks run locally

lint, typecheck, deps, knip, suppressions, docs; server tests for identity, operations and platform (198 passed); the 20 removal rows on the changed files (all caught). Main, with #65, is merged in.

…r alone through its adapter; close step 4.2

The founder's answers on the 4.2 code audit, "1. A" and "2. Okay":

- A member's invitation, cancel and renewal go back to auth.api.createInvitation, cancelInvitation
  and createInvitation with resend, as in step 4.1. The adapter writes only on the operator's path:
  the client's company, its first owner's invitation, its cancel and its renewal. checkNotMember
  and the member branch of the role check leave; invitationExpiry stays for the operator's renewal.
- mailInvitation answers a typed outcome, SENT or BOUNCED (C49); the registration handler maps it
  with no catch (C14), and a member's invitation still answers MAIL_BOUNCED.
- The owner-is-not-operator rule lives in identity, in registerCompanyOn and the next owner
  invitation; isOperator normalizes with storedAddress; the operator's proof carries the row its
  admission read and holds, so the registration no longer reads and locks it twice.
- The Central's reads build typed objects; the answer's schema validates at the exit (C27, C31).

Closing (delivery L5, L19): the D6 exception under Roll back, the S6/S11 amendment, a note in 0015,
T5's test Hub exemption, the bounce rule in boundaries, the use-line placeholder, and the card leaves
docs/steps.
@leandrotcawork

Copy link
Copy Markdown
Collaborator Author

Reviewer: Opus 5.5 (delta main..f278f8f)

Rules read: AGENTS.md, .agents/skills/conexus-review/SKILL.md (from main), the 4.2 audit report (Bloqueia 2.9 and 2.14, Fechamento 2.3, 2.8, 2.12), PR #52's closing list and PR #75's body, at main 9fe4289. Reviewed: f278f8f0bdcf8287c565d88f1fee4fa54642577a (confirmed from pull/75/head).

Delta scope: the merge base of f278f8f and origin/main is 9fe4289, which is main itself. The merge commit f278f8f only brings in #65, so git diff origin/main...f278f8f is exactly the PR's own commit ed2b3c2 (19 files, +326/−548). I read every changed line.

Ran

  • free -m: 2565 MB available, which meets the 2 GB rule.
  • PostgreSQL 18 (the CI image digest) in Docker with --rm, set up as .github/actions/test-database does (superuser postgres, non-superuser conexus_owner).
  • npm run docs: check-docs: 69 documents ok, with docs/steps/ holding only .gitkeep. biome check apps/server/src/hub docs: clean.
  • Server tests invitations, invitation-lifecycle, invitation-replay, invitation-email, operations/clients, owner-invitation, client-reads: 36/36 pass.
  • node scripts/removals.ts --only invitation-member-native-endpoint: caught. The repointed rows client-owner-not-operator, owner-inviter-is-operator and invitation-bounce-marked: all caught.
  • My own mutation, not a repository row: a member's cancel goes through the adapter (the MEMBER branch of cancelThrough deleted), and a member's renewal skips the endpoint and writes invitationExpiry() itself. invitations, invitation-lifecycle, invitation-replay and companies tests: 26 pass, 0 fail, so no test caught it (finding 1). The worktree was restored afterwards.
  • I read the installed Better Auth 1.7.8 source, dist/plugins/organization/routes/crud-invites.mjs:
    • Lines 88-94: the caller must be a member.
    • Lines 95-100: invitation:create.
    • Line 123: the creator-role refusal.
    • Lines 124-127: USER_IS_ALREADY_A_MEMBER_OF_THIS_ORGANIZATION.
    • Lines 135-149: the resend branch sets expiresAt from invitationExpiresIn. It runs after the role and member checks, so a renewal passes them too.
    • Lines 435-440: cancel checks invitation:["cancel"], with no expiry check, so an expired replacement can still be canceled.
    • auth.ts does not set sendInvitationEmail or cancelPendingInvitationsOnReInvite, so the endpoints send no second e-mail.

Not run: interrogate (time limit), the e2e use script, any screen (no web change in the delta).

Earlier findings

Audit item Status Evidence
Bloqueia 2.9: members back on native endpoints Fixed invitations.ts written: MEMBER goes to auth.api.createInvitation, wrapped in refusedAs(…, INVITATION_REFUSALS). cancelThrough: MEMBER goes to auth.api.cancelInvitation. invitation-management.ts renewed: MEMBER goes to auth.api.createInvitation with resend: true. organizationAdapter is now called only from registerCompanyOn, renameCompanyOn, and the OWNER_INVITER branches. Copied rules on the member path: checkNotMember is deleted; invitationExpiry is used only when admitted.kind === 'OWNER_INVITER' (invitation-management.ts:125); the member cancel is now judged by the endpoint's invitation:cancel; mayInviteAs is left only as the company page's read (companies.ts:65), as in 4.1.
Bloqueia 2.14 / C14: no catch in a handler Fixed operations/routes.ts:31-39 has no try. mailInvitation returns MailOutcome (invitation-mail.ts:24). Member routes keep MAIL_BOUNCED through deliveredOrRefused (company-routes.ts:78,132), the only two callers besides public.ts:54.
Fechamento 2.12 / C27: parse halfway Fixed Client.parse, ClientSummary.parse and Client.shape.state.parse are gone. Rows are parsed at the database (ClientRow, OwnerRow), and serve parses the answer (http.ts, success.body.parse(body)). Zod's default strip drops revision and owners from the list. client-reads passes.
Fechamento 2.3: owner-is-not-operator, one owner Fixed checkOwnerNotOperator in invitations.ts is called from registerCompanyOn (identity/clients.ts:65) and from checkOwnerInvitation. isOperator is no longer on the Central's ports (public.ts).
Fechamento 2.8: storedAddress Fixed admission.ts:128.
Fechamento 2.3: second read-and-lock of the operator row Fixed operatorRow returns the locked row inside OperatorProof and OwnerInviterProof. registerCompanyOn passes input.admitted.inviter. Invite and renew call lockedInviter only for MEMBER.

Closing list (PR #52, L19): D6 exception under "Roll back" in architecture.md ✔. S6/S11 amendment in security.md ✔. Note in decision 0015 ✔. T5 text for the test-Hub exemption in testing.md ✔. The card amendments now live in boundaries.md: the member/operator split, mailBounced 201, and the bounced address read as an ordinary "Aguardando o administrador" ✔. The roadmap's 4.2 use-line placeholder follows 4.1's precedent (0864568) ✔. The card has left docs/steps/ and the docs check passes ✔. The founder's confirmation of the P6 list is still open, and the PR body says so.

Findings

  1. Fechamento. Only one of the three native member calls has a test that fails when it reverts. The removal row covers the endpoint createInvitation. With a member's cancel moved back to the adapter, and a member's renewal moved back to the adapter with invitationExpiry(), all 26 invitation and company tests still pass. Renewal is the exposed one: the creator-role refusal for an administrator renewing an owner's invitation now lives only in the endpoint (crud-invites.mjs:123, which runs before the resend branch at :135). No test exercises it, so a later revert would silently let an administrator renew an owner invitation. Evidence: the mutation run above. Breaks: T12 spirit / boundaries "test fails when…" for the two other calls. Fix: add a renewal case, an administrator renewing an owner's invitation answering INVITATION_ROLE_REFUSED, with a removal row on the MEMBER branch of renewed, and a removal row on the MEMBER branch of cancelThrough if a cancel case can tell the two apart. Otherwise, say in boundaries.md why it cannot.
  2. Fechamento. The amended prose lines are not wrapped at 100 columns like their neighbours: docs/guides/security.md:44 (136 characters, S11), docs/reference/boundaries.md:114 (102) and :229 (145). The leftover short line "A replay with the same" sits in the e-mail entry. Cosmetic.
  3. Issue futura. A member's renewal relies on the endpoint's findPendingInvitation, which filters out expired invitations with the application's clock, while the Hub decides row.live with the database's clock (LIVE, invitations.ts:35). Within the clock skew at the expiry instant, the endpoint finds nothing and creates a second invitation instead of renewing. The unique pending-address index would then refuse it, giving an unmapped error rather than a duplicate. This is the same as step 4.1's native path and is not introduced here. Trigger: a renewal at the second of expiry.

I found nothing bigger than needed. renewed, written and cancelThrough are the minimum one-branch splits. deliveredOrRefused is a two-line helper with two callers. The company-routes.ts reformatting is Biome's, after headers was added. No new Bloqueia.

Verdict

approve. Both Bloqueia from the 4.2 audit are fixed as the founder chose ("1. A", "2. Okay"). Members use the endpoints in Better Auth 1.7.8, and no copied rule remains on their path. The four Fechamento items and the closing list are applied, the docs check passes with no card, the affected tests and the new removal row behave, and the two Fechamento items above go to the closing.

Recommendation for firstmate

Mergeable under L13 once CI is green on f278f8f. Findings 1 and 2 can go to a small follow-up or into step 5's closing. Finding 3 can be an issue.

…lugin's endpoint; rewrap the amended docs

An administrator renewing an owner's invitation answers INVITATION_ROLE_REFUSED, which only the
endpoint decides now; removal row invitation-member-native-renewal proves the member's renewal
goes through it. boundaries.md says why a member's cancel has no such row. The amended lines in
security.md and boundaries.md wrap at 100 columns (delta check 3 of #75, Fechamento 1 and 2).
@leandrotcawork
leandrotcawork merged commit bdf1a9c into main Oct 17, 2026
22 of 26 checks passed
leandrotcawork pushed a commit that referenced this pull request Oct 17, 2026
A member's invitation, renewal and cancellation now go through the plugin's endpoints, still in
the company's scope; the mail after them keeps the sender's scope and takes main's delivered or
refused outcome; the owner's mail keeps the operator's scope. Both sides' removal rows stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leandrotcawork
leandrotcawork deleted the fix/conexus-e042-native-invitations branch November 12, 2026 20:38
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.

2 participants