fix: enforce uniqueness and hour alignment for agent runtime usage events - #27983
Conversation
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e679d63722
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
ibetitsmike
left a comment
There was a problem hiding this comment.
Mux reviewed this pull request on behalf of Mike.
Requesting changes for the migration collision. I also found one inaccurate concurrency comment in the same layer.
8fcb4b3 to
e8efb48
Compare
ibetitsmike
left a comment
There was a problem hiding this comment.
Mux submitted this review on Mike's behalf.
f60be11 to
95021df
Compare
…ldens with generators make fmt/go (gci) regroups the imports in smtp_internal_test.go, and make gen/golden-files re-renders the AI budget notification goldens with HTML-escaped apostrophes. Both were committed out of sync with the repo's pinned tools, so every local pre-commit run failed its unstaged check. No behavior change.
…ents The usage generator writes hb_agent_runtime_v1 rows with created_at at the UTC hourly bucket start and exactly one row per bucket, but nothing in the schema enforced either invariant. A duplicate bucket row under a different id would be double-counted by any consumer summing runtime_ms, and a misaligned created_at would skew which usage period a bucket is attributed to. Replace the non-unique partial index idx_usage_events_agent_runtime with a unique index of the same shape and add an hour-alignment CHECK constraint. generateBucket treats a unique violation on the bucket index as another replica having won the race, mirroring the existing ON CONFLICT (id) no-op for committed rows.
…mplate goldens with generators" This reverts commit ed531d9d365720b4e394f4b422ea9776886c56a3. The gen/fmt drift it patched over came from main commit 07f79af (fix: markdown rendering improvements) having landed out of sync with its generators; main has since reverted that commit entirely in 8c2f7ad (#27979), so the sync is obsolete and conflicts with the regenerated goldens on latest main.
The expanded arbiter-scoping commentary is not needed on the insert query; the duplicate-bucket behavior is documented on the migration and in generateBucket where it is handled.
…000567 Main landed its own 000565 and 000566 (oauth2 client type constraint and auth method backfill) after this branch was cut, making the migration number a duplicate that fails migrate setup.
…000569 Main landed its own 000567 and 000568 (chat file purge indexes and service account notifications) after this branch was renumbered to 000567, making the migration number a duplicate that fails migrate setup in the test merge.
The (id) arbiter is not limited to committed rows: when a competing row's arbiter index entry is visible, PostgreSQL waits on that transaction and takes the DO NOTHING path if it commits. Only the narrower speculative-insertion race, before the arbiter entry exists, trips the bucket unique index.
…tp test The revert in cb36cfa left behind an unused "github.com/coder/coder/v2/coderd/render" import, breaking compilation of the dispatch test package and failing fmt, lint, and every test-go-pg job. Restore the import block to match main.
Migration comments now state only the current invariant and rollback reason, and generateBucket defers the full ON CONFLICT concurrency contract to the Generator doc comment instead of repeating it.
…000570 Main landed 000569_oauth2_scope_columns after the previous renumber to 000569, so the CI test merge contained two migrations with the same number and every migration-dependent job failed. dump.sql is unchanged because only the file number moves.
95021df to
f67ff01
Compare
The usage generator writes
hb_agent_runtime_v1rows withcreated_atat the UTC hourly bucket start and exactly one row per bucket, but nothing in the schema enforced either invariant. A duplicate bucket row under a different id would be double-counted by any consumer summingruntime_ms, and a misalignedcreated_atwould skew which usage period a bucket is attributed to.This replaces the non-unique partial index
idx_usage_events_agent_runtime(from migration 000561) with a unique index of the same shape and adds an hour-alignmentCHECKconstraint. Both statements validate existing rows: every supported writer has always produced conforming data, so a pre-existing violator is anomalous and failing the migration loudly beats silently rewriting usage rows.generateBuckettreats a unique violation on the bucket index as another replica having won the race, mirroring the existingON CONFLICT (id)no-op for committed rows.The
coderd/notificationssync commit and its revert cancel out (the drift they addressed was fixed on main by #27979); the PR's net diff is only the usage-event changes.Part 1 of a 3-PR stack splitting up #27796 (see there for review history). Stack: this PR → #27984 → #27985.