Skip to content

feat(platform): teams as audiences: one visibility rule, atomic delete, honest UI - #3422

Merged
larryro merged 9 commits into
mainfrom
feat/team-audience-model
Sep 19, 2026
Merged

larryro merged 9 commits into
mainfrom
feat/team-audience-model

Conversation

@larryro

@larryro larryro commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

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.ts states 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 → 400 TEAM_NOT_IN_ORG; a non-admin naming a team they are not in → 403 TEAM_ACCESS_DENIED). A team folder owns the audience of everything inside it (TEAM_INHERITED_FROM_FOLDER). Projects fold the owning team + shared teams into one team_ids set (the REST fields stay, deprecated and derived). The knowledge scope carries isAdmin; the org_<id> pseudo-team is gone.

Migration 0109 adds projects.team_ids, backfills it from the legacy pair, folds single-column document/folder stamps into team_tags, sweeps ghost team ids once (guarded by to_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 is IF NOT EXISTS or an UPDATE whose WHERE matches nothing on a second run; no updated_at_ms moves. Rolling-deploy safe: the legacy mirror columns keep being written, and PROJECT_TEAM_IDS_SQL reads the legacy pair while team_ids is still empty, so a row the previous image writes mid-roll keeps its restriction.

Atomic team delete. DELETE /api/app/teams/:id retires every scope, the identity-provider provenance, the memberships and the row in one transaction, audited; GET /:id/impact previews what it touches and how many items lose their only team. SCIM and the SSO reaper use the same retirement. The daily teams.repair_scopes sweep is removed and unscheduled. GET /teams/directory names every team to any member; the admin removal doors never take a team's last member (409 TEAM_LAST_MEMBER).

Governance. rule_precedence.ts states 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

  • A member of several teams gets the strictest personal budget / context cap (it was the most permissive).
  • Owners and admins now see every hub document and folder (there was no admin bypass on the hub).
  • Historical rows stamped only through the single team_id column (empty team_tags) become team-scoped after 0109; they were read as organization-wide on one door and team-only on the next.
  • Clearing every team on a project (widening to organization-wide) saves without a confirmation; dropping a team while others remain still confirms.
  • A WebDAV COPY or MOVE of a tree that holds a hidden team folder is refused (403); an admin's copy under an organization-wide destination keeps each row's own audience.
  • REST contract 1.16.0 → 1.17.0: Project.teamIds (always present; teamId / sharedWithTeamIds deprecated, derived), Document.teamIds, teamIds on the document and project create/patch bodies, new codes TEAM_NOT_IN_ORG, TEAM_INHERITED_FROM_FOLDER, PROJECT_SHARING_INVALID.
  • The GIN indexes are created inside the migration transaction without CONCURRENTLY (house style); large tenants hold a short share lock during the build.

Verification

  • Backend vitest: 745 files, 76,229 tests. Frontend vitest: 408 files, 3,219 tests. tsc, oxlint, oxfmt, SAST (0 findings), the docs structural suite, lint:manual.
  • backend:integration on 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.
  • Driven in a real browser on bun dev as 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.
  • A read-only adversarial review of the diff found one blocker (WebDAV copy/move re-homing hidden subtrees) and three should-fix items (a plain move re-validating its own audience, a sync refresh lifting a manual restriction, six readers still reading the legacy pair); all fixed and covered.

Out of scope (next PRs)

Automation files: mounts and workflow document.* team gates; SSO empty-group revoke and SCIM overwrite signalling; dropping the mirror columns, PROJECT_TEAM_IDS_SQL and usage_ledger.team_id.

… 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.
@larryro
larryro merged commit c1b3f90 into main Sep 19, 2026
56 checks passed
@larryro
larryro deleted the feat/team-audience-model branch September 19, 2026 05:56
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.

1 participant