Repository navigation
feat(usage): add the Plane-A token-usage store (RIG-2872) - #1365
Merged
Merged
Conversation
|
😎 Merged successfully - details. |
|
Compass engineering docs preview: https://compass-server-rig-2872-toke.compass-eng-docs.pages.dev Deployed from Changed pages: |
rigel-mintaka
marked this pull request as ready for review
September 28, 2026 20:17
rigel-mintaka
force-pushed
the
compass-server/rig-2872-token-usage-store
branch
2 times, most recently
from
October 2, 2026 23:47
c9c1ffa to
902d9bc
Compare
This was referenced Oct 3, 2026
The usage package holds the Store contract, an in-memory reference and one contract suite run against both backends. Postgres keeps an append-only token_usage_events log (idempotent on tenant and id) with hourly and daily rollups that can be rebuilt from it. A daily sweeper prunes raw events past --usage-event-retention (default 90 days) and keeps the rollups; each tenant's delete runs under row-level security. The compute-usage event waits on RIG-4046. Refs RIG-2872 Co-authored-by: Matt Wilkinson <matt@rigel.build>
…on (RIG-2872) Review follow-up. New pgtests hold the horizon row and the per-tenant lock to prove that prune and append wait for them. The contract suite now covers unbounded ranges, extreme prune cutoffs and one id shared by two tenants. The horizon row cannot be inserted or deleted by the runtime roles, a missing row fails the prune, and events past the timestamptz range are rejected. Refs RIG-2872 Co-authored-by: Matt Wilkinson <matt@rigel.build>
…rants (RIG-2872) The lock test now covers the rebuild as well as the append. Removing the rebuild's lock turns it red. The privilege test pins the revoked INSERT and DELETE on the prune horizon, and the migration comment names the real hazard. Refs RIG-2872 Co-authored-by: Matt Wilkinson <matt@rigel.build>
…orizon comments (RIG-2872) Refs RIG-2872 Co-authored-by: Matt Wilkinson <matt@rigel.build>
…(RIG-2872) Migrations are append-only now (RIG-4077), so 0001_init.sql returns to main's bytes. 0002 repeats 0001's RLS, grant, and trigger loop bodies for its own tables. An upgrade pgtest opens a database at v1 and checks that Open migrates it forward. The test fails without 0002. Refs RIG-2872 Co-authored-by: Matt Wilkinson <matt@rigel.build>
…e the migration comments (RIG-2872) applyV1Only now holds the lock that migrate() holds, because 0001 edits cluster-global roles. The updated_at test and squawk comments now point at the migration that adds a table, not only at 0001. Refs RIG-2872 Co-authored-by: Matt Wilkinson <matt@rigel.build>
… tenant backfill (RIG-2872) Co-authored-by: Matt Wilkinson <matt@rigel.build>
…2872) Main now requires a NATS URL for serve. Without one, buildServeConfig fails on that check before it reads the retention flag. Co-authored-by: Matt Wilkinson <matt@rigel.build>
…dget (RIG-2872) After the rebase, Serve was one line over the funlen limit (121 > 120). This shortens the retention comment to one line. Co-authored-by: Matt Wilkinson <matt@rigel.build>
rigel-mintaka
force-pushed
the
compass-server/rig-2872-token-usage-store
branch
from
October 3, 2026 17:11
902d9bc to
95edbd7
Compare
mattwilkinsonn
approved these changes
Oct 3, 2026
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.
This PR is part of a stack containing 3 PRs:
mainSummary
Adds the Plane-A token-usage store that the LLM gateway's usage events will be written into. This is the token-usage half of the observability record's T1. The compute-usage (billing) event is left out until its unit is decided (RIG-4046).
go/internal/usage:TokenUsageEvent(the gateway record's field set, with micro-USD money).Storeinterface: append, series, rebuild and prune.NewMemory, the in-memory reference.usagetest.Run, one contract suite that runs against both backends.0002_token_usage.sql;0001_init.sqlis frozen because migrations are append-only per RIG-4077):token_usage_eventsis append-only, keyed (tenant_id, id). A retried batch is ignored.token_usage_rollups_hourlyand_dailyare incremented only from newly inserted rows, and can be rebuilt from the log.tenant_isolationRLS policy. 0002 repeats 0001's RLS, grant and trigger loop bodies for these tables.0002. The runner rejects a gap or a duplicate, so whichever PR merges second renumbers to the next free version.--usage-event-retention(or$COMPASS_USAGE_EVENT_RETENTION). The default is 90 days, and0disables the sweeper. The rollups are kept.Tests
usagetest(11 cases) passes on memory and on Postgres. The cases cover: re-append counts once, mixed batches, UTC hour and day boundaries, filters, empty range, ordering, rebuild after cleared rollups, prune keeps rollups, tenant isolation, and invalid events and queries.TestRetentionSweeper*: prune at start and then daily, a failed prune is retried, and a 0 window disables the sweeper.TestOpenUpgradesV1DatabaseToTokenUsageopens a database that stopped at v1 and checks that Open migrates it to v2 with RLS forced, the horizon row seeded, and tenant-scoped reads. It fails without 0002.Refs RIG-2872