feat(teams): organization teams behind the teams feature

Merged
AG
12d ago
+1314-17

What

First build step of the team model in docs/specs/team-model.md: the control plane can hold flat, organization-level teams and put members in them. Everything is behind a new feature, teams, which is not in the default set.

  • Migration 0096: organization_teams and organization_team_assignments. Assignments are dated windows; a partial unique index allows one current team per person per organization; a live team's name is unique ignoring case.
  • Routes (owner, admin or bootstrap bearer; 403 feature_not_enabled when the feature is not selected), under /api/v1/organizations/{organizationId}:
    • GET /teams (?archived=true adds archived teams), POST /teams
    • PATCH /teams/{teamId}, DELETE /teams/{teamId} (archive; closes the team's assignments, frees the name)
    • PUT /teams/{teamId}/members/{userId} (assign or move, idempotent), DELETE /teams/{teamId}/members/{userId}
  • GET .../memberships items carry teamId only while the feature is on and the caller is an owner, an admin or the bootstrap bearer; a plain member never sees it. GET /api/v1/capabilities adds teams.
  • Team names are 1 to 80 characters after trimming and must not contain control characters (400 invalid_request).
  • Member removal closes the person's current assignment in the removal transaction, whether or not the feature is selected, so a re-added member starts with no team. Closed assignments stay until the user or the organization is deleted.
  • Audit: team.create, team.rename, team.archive, team.assign, team.unassign, one row per effective change, ids only.
  • Both tables are in CONTROL_TABLES; the schema manifest is regenerated; openapi.yaml, docs/teams.md, docs/features.md, docs/repository-guide.md and the CHANGELOG are updated.

Behavior worth a reviewer's eye

  • Assign, unassign and member removal hold that person's membership row lock; archive holds the team row exclusively and locks each open window as it closes it. The instant used is the database clock read after the locks and never earlier than the end of the person's latest closed window, so windows never overlap. A window that would close at or before its own start is deleted instead of closed.
  • Assign checks team_not_found, then membership_not_found, then team_archived. Unassign answers { unassigned: false } rather than an error when the person is not in that team.
  • Cookie-carried writes get the same Origin checks as the other organization routes.
  • Lock order: membership row, team (shared for assign/unassign, exclusive for archive and rename), then the open assignment.

Not in this change

  • No usage report by team (GET /usage/teams); that is the next step and depends on this one.
  • Nothing is enabled anywhere: the feature is off unless an installation selects it.

Checks (local, this head)

Run one at a time at this head: typecheck clean; test/organization-teams.test.ts 15/15 (adds who sees teamId, control characters in names, and cookie writes without an allowed Origin). At the first head, before the review fixes: test/migrations.test.ts, test/features.test.ts, test/introspect.test.ts, test/multi-tenant.test.ts, test/admin.test.ts passed.

🤖 Generated with Claude Code


Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.


Summary by cubic

Adds flat, organization-level teams behind a new teams feature flag that's off by default. Owners and admins list, create, rename, archive teams and assign, move and unassign members under /api/v1/organizations/{organizationId}/teams.

  • Migration 0096 adds organization_teams and organization_team_assignments: assignments are dated windows, a person has at most one current team per organization, and a live team's name is unique ignoring case.
  • Team names with control characters answer 400 invalid_request on create and rename.
  • Moving a member closes the current assignment and opens a new one; archiving closes the team's assignments and frees its name.
  • Routes answer 403 feature_not_enabled when the feature is off; otherwise owner/admin or bootstrap bearer only.
  • GET .../memberships items carry teamId only for owners, admins and the bootstrap bearer while the feature is on, and GET /api/v1/capabilities reports teams.
  • Removing a member closes their current assignment in the same transaction with the feature on or off, so a re-added member starts with no team.
  • Effective changes write one audit row (team.create, team.rename, team.archive, team.assign, team.unassign) with ids only.
  • Docs, OpenAPI, CHANGELOG and the schema manifest are updated.

Concurrency

  • Every change to a person's assignments holds their membership row lock (archiving holds the team row lock exclusively), and the closes/opens use a database-clock instant never earlier than the person's latest closed window, so windows never overlap.
  • A window that would close at or before its own start is deleted instead of closed.
  • Error order on assign: team_not_found, membership_not_found, team_archived; unassign answers { unassigned: false } instead of erroring when the person is not in that team.

Usage reporting by team (GET /usage/teams) is not included; it builds on this change.