Skip to content

feat(usage): add owner-only usage per team - #331

Open
agent-relay-code[bot] wants to merge 6 commits into
mainfrom
feat/usage-teams
Open

agent-relay-code[bot] wants to merge 6 commits into
mainfrom
feat/usage-teams

Conversation

@agent-relay-code

@agent-relay-code agent-relay-code Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Adds the owner-only usage report per team from docs/specs/team-model.md (sections 4 to 7), for sessions and searches. Depends on the organization teams from #326.

Routes

  • GET /api/v1/organizations/{organizationId}/usage/teams?days=7|30|90
  • GET /api/v1/organizations/{organizationId}/usage/teams/audit

Both are owner-only (admins and the bootstrap bearer get 403 forbidden) and need both teams and usage_people selected: a team of one shows one person's figures, so an installation that turned the People view off does not get them through teams. Neither feature is selected by default.

Grouping

  • Each session read is counted in exactly one row: a team, a crossTeam row for a set of teams (null for a person with no team), noTeam, unattributed or notResolved. The rows' sessions sum to total.sessions.
  • Sessions come only from the read API's session list, where each session carries its own user set. The per-person aggregate is not used, because it counts a shared session once per person.
  • A person's team is decided once per report at teamAsOf (the end of the range, or now when that is earlier): the assignment open then, else the one that closed last inside the range, else none. One query per report, limited to the organization.
  • searches come from usage_people_daily.
  • notResolved stays 0 until the read API can group by user set. The grouping function already takes those buckets.
  • spend is null in every row and listed in unavailable.

Bounds

The People view's bounds apply (pages of at most 200, at most 3 calls and 2 seconds), with a 5-minute cache of its own. A report that did not read every session is labelled truncated, and its session figures are lower bounds. If the read API cannot be read, session figures are null and searches are still served.

Audit

Every served report, cached or not, writes an organization-level usage.teams.viewed row. The People audit list is unchanged.

Not changed

People view behaviour (src/usage/people.ts only exports existing helpers). No migration, no new table, no new feature name, no dashboard change.

Checks run locally at this head

  • tsc --noEmit: clean
  • test/usage-teams.test.ts: 10 of 10
  • test/sessions.test.ts: 50 of 50

The full suite is left to CI.

🤖 Generated with Claude Code


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


Summary by cubic

Adds owner-only usage per team: a report and an audit list under /api/v1/organizations/{organizationId}/usage/teams, requiring both the teams and usage_people features. Admins and the bootstrap bearer get 403 forbidden.

  • Each session read from the read API's session list is counted in exactly one row (a team, a cross-team set, no team, unattributed, or not resolved), so the rows sum to total.sessions; searches come from usage_people_daily. A session listed twice for one workspace counts once, while the same session id in two workspaces counts in each.
  • A person's team is resolved once per report at teamAsOf — the open assignment, else the one that closed last inside the range — so moves and removals count under the later team.
  • Reads are bounded like the People view with their own 5-minute cache, keyed per History reader identity; incomplete reads are flagged truncated, and an unreachable read API leaves session figures null while still serving searches.
  • Every served report, cached or not, writes a usage.teams.viewed audit row.
  • No migration or new table.

Written for commit a1c5282. Summary will update on new commits.

View guided diff Turn on auto-fix

layered-control-agent-host and others added 2 commits November 24, 2026 09:16
GET /api/v1/organizations/{organizationId}/usage/teams and its audit
list, behind both the teams and usage_people features. Each session
read from the read API's session list is counted in exactly one row
(a team, a set of teams, no team, unattributed or not resolved), so
the rows sum to the total. Searches come from usage_people_daily. A
person's team is the assignment open at the end of the range, else
the one that closed last inside it. Reads use the People view's
bounds and a truncated read is labelled. No migration.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3d5e9c0a-436a-47ef-abe1-27bb82f9f4b1

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

layered-control-agent-host and others added 3 commits November 24, 2026 09:30
The team report dropped a session when the same harness and session id
had already been read from another workspace. The People view and the
Usage page count such a session in each workspace, so a team of one
could show fewer sessions than that person. The key now includes the
workspace; a session listed twice for one workspace still counts once.

Tests added for the tiebreak between two closed assignment windows and
for an archived team that only a cross-team row names.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>

@agent-relay-code agent-relay-code Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Source finding at fe97cb5: teamsReport caches the session sample by org, user, date range and workspace IDs. ownerIdentities also supplies the workspace role, caller credential and readerKind; the History reader deliberately includes all three in its own token-cache identity (src/sessions/history.ts). A second credential or reader context for the same owner can hit the Teams cache without its upstream read being authorized. Partition the sample by the complete effective History reader context (use a credential digest, never a raw token), or avoid sharing cached reads across those contexts. Add a regression that warms the cache, changes reader context while retaining the same user and workspace IDs, and requires a fresh upstream authorization. The user-role and feature checks at the HTTP route do not distinguish these contexts. I pushed the main refresh; this is a source finding, not independent approval.

This branch has not been deployed

No deployments
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.

0 participants