test(integration): poll for query-exchange visibility after POST (orders, order_groups)#129
Conversation
Demo's query-exchange replica lags writes by ~10 seconds. Orders and
order_groups created via POST aren't immediately readable via their
GET-by-id endpoints — the failing tests asserted GET success
immediately after CREATE and reliably hit 404 ("not_found" /
service: query-exchange).
Probe evidence (orders):
create -> order_id X
poll 1-3 (~1.5s): 404 not_found
...
poll @ 10s: GET works, status=resting
Same pattern for order_groups (~9.5s).
- Added wait_for_resource (sync) and await_resource (async) helpers in
tests/integration/helpers.py — bounded poll loop that swallows
KalshiNotFoundError until timeout (default 15s, interval 0.5s) and
re-raises if the resource never propagates.
- Wrapped GET-after-POST sites in test_orders.py
(test_order_fill_lifecycle, test_create_get_cancel sync + async) and
test_order_groups.py (test_create_and_get, test_update_limit,
TestOrderGroupsAsync.test_create_get_delete).
- Dropped the now-redundant `await asyncio.sleep(0.5)` workaround +
unused asyncio import from test_order_groups.py.
Verified locally: tests/integration/test_orders.py +
tests/integration/test_order_groups.py — 22/22 passed.
Closes 5 of 11 integration-test failures (#124).
Running tally: A (#126=4) + B (#127=1) + E (#128=1) + C (this=5) = 11/11.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Code Review — PR #129: poll for query-exchange visibility after POSTOverviewSolid, well-motivated fix. The root-cause explanation in the PR description is clear, the helpers are minimal and purposeful, and the call-sites wrap only what they need without touching unrelated code. The decision to keep this in test helpers (rather than adding 404-retry logic to the SDK transport) is the right call — the PR description correctly identifies that transport-level retry would mask real user bugs. Issues
|
Per bot review on PR #129: - Removed unused last_error assignment in wait_for_resource (ruff F841; caused CI lint failure). bare 'raise' re-raises the active exc directly, so the captured ref was dead. - Switched both helpers to PEP 695 type parameter syntax (def wait_for_resource[T](...)) to clear UP047. Drops the standalone TypeVar import. - Expanded await_resource one-line docstring with the demo-lag context so the file reads top-to-bottom. Verified: uv run ruff check . clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Bot feedback addressed in latest commit:
Local ruff: (Pre-existing |
Code Review — PR #129: Poll for query-exchange visibility after POSTOverall this is a clean, well-reasoned fix. The root cause is clearly documented, the implementation is minimal, and the PR description includes probe evidence that makes the ~10s lag concrete. A few observations below. What the PR doesAdds two polling helpers ( Positives
Minor issues / suggestions1. Sequential polling in buy_order = wait_for_resource(lambda: sync_client.orders.get(buy_id))
sell_order = wait_for_resource(lambda: sync_client.orders.get(sell_id))Both orders were created simultaneously, so they should become visible at roughly the same time. In practice the second call will return almost instantly after the first succeeds. Worst-case wall time is 2. Timeout slightly tighter than necessary
3. fetch: Callable[[], Awaitable[T]]This accepts any zero-arg callable returning an awaitable, which is correct for the call sites (all 4. The diff correctly drops Potential concernsNone blocking. The implementation is straightforward and the PR description's verification table (22/22 targeted tests passing, 5/11 tracked failures closed) gives good confidence. SummaryApprove. The fix is correct, minimal, and well-documented. The two micro-suggestions above (sequential-poll comment, timeout margin note) are cosmetic and don't need to block merge. |
Summary
5 integration failures in
test_orders.py(× 3) andtest_order_groups.py(× 2), plus 1 flappingTestOrderGroupsAsync.test_create_get_delete, all surfaced the same error:Root cause
Demo's
query-exchangereplica lags writes by ~10 seconds. POST returns immediately with a validorder_id/order_group_id, but the matchingGET /portfolio/orders/{id}/GET /portfolio/order_groups/{id}404s until the replica catches up.Probe evidence (orders):
create→order_id Xnot_foundstatus=restingSame shape for order_groups (~9.5s).
Fix
Added two test helpers in
tests/integration/helpers.py:Both poll the provided fetch function, swallow
KalshiNotFoundErroruntil the timeout, and re-raise if the resource never propagates. Drop-in wrap of the GET-after-POST call sites — no SDK changes (transport retries on 404 would mask real "user typo'd the order id" bugs in user code).Verification
tests/integration/test_orders.py + test_order_groups.py— 22/22 passedtests/integration/overall on this branch: 5 failed, 211 passed, 29 skipped — the 5 failures are exactly the ones being fixed in fix(models): match live server for AccountApiLimits + tags_by_categories #126 / test(integration): swap bad-auth probe to portfolio.balance() #127, not present on this branch's baseProgress on #124
Closes 5 of 11. Running tally with this PR landed:
Refs #124
Test plan
tests/integration/test_orders.py + test_order_groups.py22/22ruff check tests/integration/clean