fix(server): skip one-sided order books + close race and poller recovery tests #12

Merged
pregno merged 3 commits from fix/clob-midpoint-and-test-gaps into main 2026-06-13 01:31:51 +02:00
Owner

Last items from the repo-wide review:

  1. One-sided order books no longer fabricate a midpoint. topOfBook defaulted bestAsk = 1 on a bids-only book and bestBid = 0 on an asks-only book, producing midpoints that jump violently when one side transiently empties and poisoning delta15m history. It now throws (the caller already catches and skips that market's tick, keeping the last snapshot — same handling as the existing empty-book case). An asks side with only zero/non-positive prices counts as one-sided too, preserving the intent of the pre-existing p > 0 filter.

  2. Test: concurrent double-close race. New positions.race.test.ts (separate file to avoid conflicting with the open positions PR): real app + real SQLite, price source gated on a deferred promise so both closes are verifiably in flight past the read-check simultaneously; asserts exactly one 200 and one 409, one closed row. The conditional UPDATE ... WHERE status='open' backstop works correctly — no production bug found.

  3. Test: startPoller error recovery. A throwing discovery run keeps the previous market set and both loops alive; a throwing tick doesn't kill the tick loop. Written against the current self-rescheduling-loop implementation. Recovery behavior confirmed correct — test-only.

Tests: 57 files / 317 passing (+9 over main), typecheck clean.

Last items from the repo-wide review: 1. **One-sided order books no longer fabricate a midpoint.** `topOfBook` defaulted `bestAsk = 1` on a bids-only book and `bestBid = 0` on an asks-only book, producing midpoints that jump violently when one side transiently empties and poisoning `delta15m` history. It now throws (the caller already catches and skips that market's tick, keeping the last snapshot — same handling as the existing empty-book case). An asks side with only zero/non-positive prices counts as one-sided too, preserving the intent of the pre-existing `p > 0` filter. 2. **Test: concurrent double-close race.** New `positions.race.test.ts` (separate file to avoid conflicting with the open positions PR): real app + real SQLite, price source gated on a deferred promise so both closes are verifiably in flight past the read-check simultaneously; asserts exactly one 200 and one 409, one closed row. The conditional `UPDATE ... WHERE status='open'` backstop works correctly — no production bug found. 3. **Test: startPoller error recovery.** A throwing discovery run keeps the previous market set and both loops alive; a throwing tick doesn't kill the tick loop. Written against the current self-rescheduling-loop implementation. Recovery behavior confirmed correct — test-only. Tests: 57 files / 317 passing (+9 over main), typecheck clean.
A bids-only book previously yielded bestAsk=1 and an asks-only book
bestBid=0, producing a fabricated midpoint that jumps violently when
one side transiently empties and poisons delta15m history. Throw (same
handling as the empty-book case: callers skip that market's tick) when
either side is empty or asks contain no positive price.
Fires two simultaneous closes for the same position against the real
app + real SQLite, gating both handlers on a deferred price lookup so
each passes the read-then-check before either runs the conditional
UPDATE ... WHERE status='open'. Asserts exactly one 200 and one 409,
and a single closed row. New file to avoid conflicting with the open
PR touching positions.test.ts.
Two gaps in startPoller resilience: a discovery run that throws must
keep the previous market set and reschedule both loops, and a tick
that throws must not kill the self-rescheduling tick loop. Uses the
existing fake-timer + settle patterns; console.error is stubbed since
both paths log intentionally.
pregno force-pushed fix/clob-midpoint-and-test-gaps from 3d5d7be107 to b83d6024db 2026-06-13 01:31:43 +02:00 Compare
pregno merged commit b170fe26b1 into main 2026-06-13 01:31:51 +02:00
pregno deleted branch fix/clob-midpoint-and-test-gaps 2026-06-13 01:31:51 +02:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
pregno/polymarket-screener!12
No description provided.