feat(markets): centralise settlement state transition rules - #1791
Open
bilhokista wants to merge 4 commits into
Open
feat(markets): centralise settlement state transition rules#1791bilhokista wants to merge 4 commits into
bilhokista wants to merge 4 commits into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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.
Closes #1616.
A note on the states, before anything else
The issue describes the lifecycle as
open → closed → resolving → settled/cancelled. The enum actually in the codebase (MarketSettlementState, added by1776700000000-AddMarketSettlementState.tsand since extended) is:pending → proposed → settling → settled, withproposed → challenged → settledwhen a challenge is raised.I implemented the lifecycle that exists rather than the one in the issue text, since renaming states would be a migration and a breaking API change rather than the guard the issue asks for. Cancellation is carried by the separate
is_cancelledflag and is deliberately left out of the enum. If the states in the issue were the intended target rather than shorthand, say so and I will redo this against them.What was actually missing
The transitions were already guarded — but in four separate places, each written by hand:
markets.service.tshad threesettlement_state !== Xchecks (propose, challenge, resolve-challenge)market-settlement.scheduler.tshad a fourth as an inlinestillEligiblepredicateEach copy was correct. Nothing tied them together, so adding a state or changing a rule meant finding all four by hand, and no test stated the rules anywhere.
The change
backend/src/markets/market-settlement-state.util.ts(new) holdsALLOWED_SETTLEMENT_TRANSITIONSas the single source of truth, pluscanTransition(from, to)anddescribeIllegalTransition(from, to). The rejection message names both states and lists what would have been allowed, so a caller is told what to do rather than only that they were wrong:The service's three guards and the scheduler's predicate now consult that table.
Existing behaviour is preserved deliberately. The exception types are unchanged (
ConflictExceptionon propose,BadRequestExceptionon challenge and adjudication), sincemarkets.service.spec.tsasserts on those. Only the message text is richer.SETTLING → SETTLINGis modelled as legal because the scheduler genuinely re-claims a market it already markedSETTLINGafter a crash — that is existing behaviour, now written down instead of implied.One guard is intentionally not routed through the table:
resolveChallengestill requires exactlyCHALLENGED.SETTLING → SETTLEDis a legal transition, but only via the scheduler, never via admin adjudication — the table describes the lifecycle, not authorisation. There is a comment at that line saying so.Tests
market-settlement-state.util.spec.ts(new) writes out the six intended transitions by hand and derives the illegal set from them, so the test states the rules rather than re-deriving them from the table it is testing. It covers:settledis terminal from every directionHonest note on verification: I could not run the backend Jest suite locally (no full workspace install). Every changed file was parsed with the TypeScript compiler API, and the transition table was transpiled and executed standalone across all 25 pairs — 0 mismatches against the hand-written expectations, and both message shapes were checked against real output (quoted above). CI is the real check on the service- and scheduler-level suites.
🤖 Generated with Claude Code
https://claude.ai/code/session_01CrfEY1tvXrbeMDAUzxfuk7