Skip to content

Fence managed-meter outcome sequences before storage capture - #566

Draft
fazpu wants to merge 4 commits into
mainfrom
fix/meter-outcome-watermark
Draft

fazpu wants to merge 4 commits into
mainfrom
fix/meter-outcome-watermark

Conversation

@fazpu

@fazpu fazpu commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

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 nextval into the success-outcome INSERT so 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, UMC canonical_storage/database.py capture 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 head fb2a209388d6a5f36f90b4b17c0d3488300e871a after 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):

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.

1 participant