feat!: a generic core, and Kalshi through it

The orchestrator, the connection loop and the book thread's shell move to
src/core/, generic over an Exchange and its loss rule (ADR 0004).
Polymarket runs through it unchanged. Kalshi joins: a signed handshake
(KALSHI_KEY_ID_FILE, KALSHI_PRIVATE_KEY_FILE), a reconnect on a skipped
sequence number, first one wins by clock and rank, and its market listing
with markets tracked from three hours before they open.

Breaking: the exchange is now the first argument,
`engineering-challenge <polymarket|kalshi|kalshi-demo> [OUTPUT]`.
This commit is contained in:
pascal committed 2026-11-11 20:54:17 +00:00
1 parent 480ee3ad2d
commit 1a5bbcd602
33 files changed
+8150 -1846

No files matched your search

+30 -12
View File
@@ -13,22 +13,31 @@ outside this repo.
agreed plan. Read the current sprint file before writing code.
- `docs/adr/`: hard-to-reverse decisions (0001: threads, shared-nothing;
0002: the orchestrator, its lanes superseded; 0003: two connections per
symbol, first one wins).
symbol, first one wins; 0004: the exchange seam, a websocket layer and a
loss rule per exchange).
- `docs/notes.md`: domain rules and exchange protocol facts (delta semantics,
loss detection, message shapes). Read before touching exchange parsing.
- `GLOSSARY.md`: the terms (symbol, connection, load, first one wins,
duplicate, held, rescued, follow, resync, loss, subscription gap, split,
quiet, resubscribe, silent, recycle, Snapshot, Delta).
- `src/polymarket/orchestrator.rs`: what is tracked, which connections carry
each market, their balancing and their recycling; `src/polymarket/gamma.rs`: the fetchers
(discovery); `src/polymarket/socket.rs`: a connection (read, stamp, decode,
route; reconnects when dropped, silent or recycled);
`src/polymarket/wire.rs`: the exchange's messages;
`src/polymarket/feed.rs`: a book thread, its loss rules (the subtle
part) and its watch for quiet subscriptions; `src/book.rs`: the book and fixed point; `src/reconcile.rs` +
quiet, resubscribe, silent, recycle, sequence number, clock, Snapshot,
Delta).
- `src/core/`: what is the same for every exchange (ADR 0004).
`orchestrator.rs`: what is tracked, which connections carry each market,
their balancing and their recycling; `socket.rs`: a connection (read,
stamp, decode, route; reconnects when dropped, silent, lost a message or
recycled); `feed.rs`: a book thread's shell (Snapshots, moves, the quiet
watch) and the `Rule` trait; `mod.rs`: the `Exchange`, `Session`, `Market`
and `Event` traits.
- `src/polymarket/`: `gamma.rs` the fetchers (discovery), `wire.rs` the
exchange's messages, `feed.rs` the hash rule (the subtle part),
`mod.rs` the exchange and its session.
- `src/kalshi/`: `listing.rs` the fetchers, `wire.rs` the messages,
`sign.rs` the key and the handshake's signature, `feed.rs` the clock and
rank rule, `mod.rs` the exchange and its session (the `seq` check).
- `src/book.rs`: the book and fixed point; `src/reconcile.rs` +
`src/bin/reconcile.rs`: the output check, reading the log's cause lines
(`src/cause.rs`). `tests/fixtures/`: recordings of the live feed, on one and two
connections, that the tests replay.
(`src/cause.rs`). `tests/fixtures/`: recordings of the live feeds that the
tests replay.
- `src/util/` and `sbe_schemas/`: the template's SBE machinery, used as given so
output stays readable by the template's `AlignedSbeReader`. Our code is the
rest of `src/`.
@@ -66,7 +75,7 @@ and let the user settle it. Claude owns the data model.
- **Only a clean stop flushes the output**: SIGINT, SIGTERM or SIGHUP. SIGKILL
or SIGALRM leaves the last 64 MiB chunk unwritten. Bound a run with GNU
`timeout -s INT`, then check the output against the log:
`timeout -s INT 600 ./target/release/engineering-challenge out.sbe 2> run.log;
`timeout -s INT 600 ./target/release/engineering-challenge polymarket out.sbe 2> run.log;
./target/release/reconcile out.sbe run.log`. With the log, `reconcile`
pairs each mismatch with its cause line and fails only on one left
unexplained.
@@ -81,6 +90,15 @@ and let the user settle it. Claude owns the data model.
2026-11-11): a starved collector falls behind and gets closed with 1013.
Check `cat /proc/loadavg` before a live run whose numbers are compared, and
alternate the builds in short rounds rather than running them back to back.
- **Kalshi needs a key; nothing else does**: `KALSHI_KEY_ID_FILE` and
`KALSHI_PRIVATE_KEY_FILE` name its two files (on the server
`/run/secrets/kalshi-demo-key-id` and `kalshi-demo-private-key`; never read
or print them). Opening ~100 connections at once gets some handshakes
refused with 429; each retries after 1 s and gets in.
- **cargo keeps a build whose source got an older mtime**: a script that
restores a file from a copy (`shutil.move`, `cp -p`) must `touch` it, or
the next `cargo test` runs the old binary (a mutation check did, and a
"flaky" test was only the last mutant).
- **Live data beats the docs**: Polymarket's market channel has rules its
docs don't state (`docs/notes.md`); check a protocol assumption on a
recording of the real feed (`tests/fixtures/record.py`) before relying on it.