Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: enforce uniqueness and hour alignment for agent runtime usage events - #27983

Merged
jaaydenh merged 12 commits into
mainfrom
runtime-invariants
Aug 17, 2026
Merged

fix: enforce uniqueness and hour alignment for agent runtime usage events#27983
jaaydenh merged 12 commits into
mainfrom
runtime-invariants

Conversation

@jaaydenh

@jaaydenh jaaydenh commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

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.

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-alignment CHECK constraint. 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. 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.

The coderd/notifications sync 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.

@jaaydenh

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread enterprise/coderd/usage/generator.go Outdated

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: 8d5606b79f

ℹ️ 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".

@ibetitsmike ibetitsmike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread enterprise/coderd/usage/generator.go Outdated
@jaaydenh
jaaydenh requested a review from ibetitsmike August 13, 2026 02:14
@jaaydenh
jaaydenh force-pushed the runtime-invariants branch 2 times, most recently from 8fcb4b3 to e8efb48 Compare August 13, 2026 11:32

@ibetitsmike ibetitsmike left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Mux submitted this review on Mike's behalf.

Comment thread coderd/database/migrations/000569_usage_events_agent_runtime_invariants.up.sql Outdated
Comment thread enterprise/coderd/usage/generator.go Outdated
@jaaydenh
jaaydenh requested a review from ibetitsmike August 14, 2026 06:03
@jaaydenh
jaaydenh force-pushed the runtime-invariants branch from f60be11 to 95021df Compare August 17, 2026 07:16
…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.
@jaaydenh
jaaydenh force-pushed the runtime-invariants branch from 95021df to f67ff01 Compare August 17, 2026 08:25
@jaaydenh
jaaydenh merged commit 20c376a into main Aug 17, 2026
28 checks passed
@jaaydenh
jaaydenh deleted the runtime-invariants branch August 17, 2026 09:23
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 17, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants