Repository navigation
feat(platform): teams as audiences: one visibility rule, atomic delete, honest UI - #3422
Merged
Merged
Conversation
… projects One rule for who may see a team-scoped resource: `teamIds` (documents and folders `team_tags`, projects the new `team_ids`), empty = organization-wide, otherwise a member of any listed team, owners and admins always. `core/lib/audience.ts` carries it once — the point read, the SQL predicate every list door embeds, and the assignment rule (`TEAM_NOT_IN_ORG`, `TEAM_ACCESS_DENIED`). A team folder owns the audience of everything inside it (`TEAM_INHERITED_FROM_FOLDER`), a plain move keeps a document's own audience, and a cloud-sync refresh no longer lifts a manual restriction. Migration 0109 adds `projects.team_ids`, folds the legacy pair and the single-column stamps into the arrays, sweeps ghost team ids once, and adds the per-team GIN indexes. It is idempotent by construction: every statement is IF NOT EXISTS or a self-masking UPDATE, no `updated_at_ms` moves. The legacy mirror columns keep being written for the previous image during a rolling deploy; `PROJECT_TEAM_IDS_SQL` reads the pair while the array is still empty. The knowledge scope carries `isAdmin` and drops the `org_<id>` pseudo-team; project readers that still read the legacy pair use the audience expression. REST 1.17.0: `Project.teamIds` (legacy fields deprecated, derived), `Document.teamIds`, `teamIds` on the create and patch bodies.
`DELETE /api/app/teams/:id` retires every scope the team held (audiences, conversation queues, sync configs), its identity-provider provenance, its memberships and the row in ONE transaction, audited, then re-stamps the corpus copies; `GET /:id/impact` says beforehand what it touches and how many items lose their only team. SCIM `deleteGroup` and the SSO reaper run the same retirement inside their own transactions; Better Auth's hook stays for direct plugin callers. The daily `teams.repair_scopes` sweep is gone (nothing can mint a ghost any more) and `registerSchedules` unschedules it. `GET /teams/directory` names every team to any member, `GET /` marks provider-synced teams, a roster is visible to its members and admins, and the admin removal doors never take a team's last member (`TEAM_LAST_MEMBER`).
`core/governance/rule_precedence.ts` states once how rules combine for a member of several teams: a limit takes the strictest value (budgets and the context-window cap — the personal tier used to take the most permissive), a grant is the union with a block winning (model access, unchanged), a single pick follows the list order (default model). The budget banner derives its warnings from the same standing the admission gate enforces, with a team bucket per team; the `selectedTeamId` parameter is gone. The usage ledger no longer records a team for voice output, and the usage metrics fold by user only.
The WebDAV handlers were team-blind: any app password listed and read every team folder and document, and MKCOL/PUT/COPY/MOVE ignored team folders altogether. Every method now hands the credential owner to the handlers, which resolve their audience grant (fail-closed to the org-wide rows for a missing or disabled identity), filter listings in the statement, hide what the caller cannot see, stamp writes with the landing folder's audience, and refuse a delete, copy or move whose tree holds a team folder or document outside the caller's audience — an admin's copy under an org-wide destination keeps each row's own audience. The integration check proves the whole model on real Postgres: the rollout fallback, the assignment and inheritance refusals, the admin bypass, the atomic delete through every door, the last-member rule, the WebDAV refusals, and migration 0109 applied twice more with a byte-identical snapshot.
The account menu's "Team" row was a client-side filter dressed as a context switch: it redirected to chat, hid nothing org-wide, and showed no sign of the chosen team anywhere. A team is an audience label on work, not a workspace, so the switcher and its provider are gone. The menu row is read-only and opens the account page's new "Your teams" section; the documents and projects lists carry a Teams filter (Organization-wide, My teams, each team by name) in the URL, the inbox a queue filter and a queue chip; projects show their audience by name (via the team directory, so a team the viewer is not in still reads as itself), choose it on create and on General, and confirm only a change that narrows access; team settings delete through the atomic door with the impact preview, lock provider-synced teams, and map the last-member refusal. en, de and fr ship together.
Teams, account preferences, documents, project concepts, and the two governance pages in en, de and fr: the one visibility rule, the admin bypass, the folder inheritance, the Teams filters, the account page's section, the delete preview and what becomes organization-wide, synced teams, and how rules combine for a member of several teams.
NAV-F6 becomes the read-only Teams row, SET-F18/F19/F41 gain the delete preview, the server-side last-member refusal and synced teams; new boxes for the documents Teams filter, the project audience column and pickers, the inbox queue chip and filter, and the account page's Your teams; the error map lists the new assignment codes.
The skills page resolves visibility badges through `useTeamDirectory` now, and the browser-mode suite imports the real module graph, so its mock of the teams hooks must export it too. The inbox's queue-filter tokens are module-private — nothing imports them, and knip says so.
…dience The upload hook still followed the single-team era: two selected teams meant two document rows over the same blob, each visible to one team and neither to both. Teams are the document's audience now, so one create carries `teamIds` through the blob-upload door; an empty selection stays organization-wide.
5 tasks done
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.
Why
A member of two teams "switches team" in the account menu and nothing changes. The audit behind this PR found the reason: the row was a client-side filter dressed as a context switch (it redirected to chat, hid nothing organization-wide, and showed the chosen team nowhere), and behind it the backend used "team" for six different jobs with rules that disagreed with each other — plus a set of doors that ignored teams altogether (WebDAV, folder creation, cloud imports, the document retag lane, two team lookups without an organization join).
This PR implements the chosen model end to end: a team is an audience label on work and a queue for conversations, never a workspace to switch into.
What changes
One visibility rule. A document, folder or project carries
teamIds(empty = organization-wide; otherwise members of any listed team; owners and admins always).core/lib/audience.tsstates it once: the point read (canSeeAudience), the SQL predicate every list door embeds (audienceClause), and the assignment rule (assertTeamsAssignable: an id that is not one of the organization's teams → 400TEAM_NOT_IN_ORG; a non-admin naming a team they are not in → 403TEAM_ACCESS_DENIED). A team folder owns the audience of everything inside it (TEAM_INHERITED_FROM_FOLDER). Projects fold the owning team + shared teams into oneteam_idsset (the REST fields stay, deprecated and derived). The knowledge scope carriesisAdmin; theorg_<id>pseudo-team is gone.Migration 0109 adds
projects.team_ids, backfills it from the legacy pair, folds single-column document/folder stamps intoteam_tags, sweeps ghost team ids once (guarded byto_regclass('"team"')because app migrations run before Better Auth's tables exist on a fresh database), and adds three GIN indexes for the per-team lanes. It is idempotent by construction — every statement isIF NOT EXISTSor anUPDATEwhoseWHEREmatches nothing on a second run; noupdated_at_msmoves. Rolling-deploy safe: the legacy mirror columns keep being written, andPROJECT_TEAM_IDS_SQLreads the legacy pair whileteam_idsis still empty, so a row the previous image writes mid-roll keeps its restriction.Atomic team delete.
DELETE /api/app/teams/:idretires every scope, the identity-provider provenance, the memberships and the row in one transaction, audited;GET /:id/impactpreviews what it touches and how many items lose their only team. SCIM and the SSO reaper use the same retirement. The dailyteams.repair_scopessweep is removed and unscheduled.GET /teams/directorynames every team to any member; the admin removal doors never take a team's last member (409TEAM_LAST_MEMBER).Governance.
rule_precedence.tsstates how rules combine for a member of several teams: limits take the strictest value, grants the union with a block winning, a single pick the first rule in list order. The budget banner now derives its warnings from the same standing the admission gate enforces, with a per-team bucket. The usage ledger no longer records a team.WebDAV handlers resolve the credential owner's audience (fail-closed), filter listings in the statement, stamp writes with the landing folder's audience, and refuse a delete, copy or move whose tree holds a team folder or document the caller cannot see.
Frontend. The switcher and its provider are deleted. The account menu's row is read-only ("Teams: A, B") and opens the account page's new Your teams section. Documents and projects carry a Teams filter (Organization-wide, My teams, each team by name) in the URL; the inbox gets a queue chip and a Filter by queue. Projects show their audience by name, choose it on create ("Who can see it") and on General ("Audience", confirming only a change that narrows access). Team settings delete through the atomic door with the impact preview, mark provider-synced teams and lock their edit dialog, and map the last-member refusal. en, de and fr ship together; the product docs (en/de/fr) and the manual suites are updated.
Behaviour changes to be aware of
team_idcolumn (emptyteam_tags) become team-scoped after 0109; they were read as organization-wide on one door and team-only on the next.Project.teamIds(always present;teamId/sharedWithTeamIdsdeprecated, derived),Document.teamIds,teamIdson the document and project create/patch bodies, new codesTEAM_NOT_IN_ORG,TEAM_INHERITED_FROM_FOLDER,PROJECT_SHARING_INVALID.CONCURRENTLY(house style); large tenants hold a short share lock during the build.Verification
lint:manual.backend:integrationon a fresh throwaway Postgres + MinIO: 703/703, including 14 new probes — the rollout fallback (legacy pair, empty array → still team-scoped), the delete preview, the admin-only doors and the member-visible directory, the SCIM / app-door / Better Auth deletion lanes, the last-member rule, the assignment and inheritance refusals, admin visibility versus a member outside the team (hub page, folder list, direct read), WebDAV PROPFIND and COPY/MOVE refusals, and migration 0109 applied twice more on live rows with a byte-identical snapshot.bun devas an owner and as a throwaway editor in no team: menu row, account section, documents filter and URL, project audience column/pickers and the narrowing confirmation, inbox queue chip and filter, team delete preview and atomic delete, synced-team lock; the editor sees neither the team document nor a "Manage teams" link.Out of scope (next PRs)
Automation
files:mounts and workflowdocument.*team gates; SSO empty-group revoke and SCIM overwrite signalling; dropping the mirror columns,PROJECT_TEAM_IDS_SQLandusage_ledger.team_id.