Repository navigation
Conversation
This branch has not been deployed
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.
Problem and change
A successful managed-meter outcome reserved its sequence in a separate database connection before its outcome row was inserted. A storage capture could lock the outcomes table, observe a later committed maximum and finish while that previously reserved lower sequence was still waiting to be inserted. Move the
nextvalinto the success-outcomeINSERTso PostgreSQL acquires the outcome table's write lock before the sequence is allocated; the capture's SHARE lock must then wait for that writer to commit or abort. Failed outcomes still have no success sequence; duplicate delivery remains idempotent and sequence gaps remain possible.Sources and validation
Inspected
ManagedMeterCatalog._materialize_outcomes, the managed-meter schema, UMCcanonical_storage/database.pycapture fence and project-metered billing §4.4. A deterministic PostgreSQL 17 test calls the real catalog, pauses immediately after sequence allocation, and checks that a separate capture is blocked on the actual table lock; it failed on main and passed after the fix. The complete regression passed against a separate temporary PostgreSQL 17 cluster, checking both a waiting capture and the UMC collector’s 250 ms fail-closed lock timeout, plus failed-outcome and replay cases. Ruff, formatting, Pyright on the changed files and the test inventory passed. CI initially identified the missing integration-test inventory entry; the latest checkpoint adds it. The PostgreSQL lock-mode documentation confirms INSERT's ROW EXCLUSIVE lock conflicts with SHARE, and the sequence documentation explains why rollbacks and ON CONFLICT leave gaps (both read 2026-11-09). Grok approved exact headfb2a209388d6a5f36f90b4b17c0d3488300e871aafter independent throwaway PostgreSQL 17.11 and 19rc1 lock-order, old-writer, conflict and deadlock checks; all CI checks remain required. The CLA check requires the PR author to read and personally accept the Contributor License Agreement; no assent has been asserted by the agent.Rule D56: this change only affects when an internal managed-meter outcome gets its sequence. It does not change document identities, conversion, chunking, embedding/extraction reuse keys, or paid model work. It does not justify a public claim or activate billing.
Limitations and follow-up
This fixes one local sequence-allocation race. It does not prove a cross-provider storage boundary, exact period-end capture, prior receipt delivery, or the new shared-runtime producer. Mixed old/new worker rollout must be drained before relying on the guarantee because an old worker can still hold a sequence outside the table lock. The ordering guarantee also assumes the sequence retains CACHE 1, the default in its engine migration. PostgreSQL can hand out lower cached values after a higher value when CACHE is greater than 1; verify that configuration before relying on a watermark (PostgreSQL CREATE SEQUENCE, read 2026-11-09). Those broader billing gaps remain launch gates in UMC #852/#856. No production resource was modified.
Contributor agreement
Signing on behalf of a legal entity (leave blank if accepting individually):