Skip to content

polish: cursor guard O(1); batch bytes path; bench scripts; close terminality; env isolation; from_env test - #236

Merged
TexasCoding merged 1 commit into
mainfrom
audit/w3b-polish-perf-test
May 21, 2026
Merged

TexasCoding merged 1 commit into
mainfrom
audit/w3b-polish-perf-test

Conversation

@TexasCoding

Copy link
Copy Markdown
Owner

Wave 3-B of audit umbrella. 6 P4 polish items.

Changes

  • _list_all cursor-loop guard switched from set[str] to single last_cursor (O(1) memory) in both sync and async.
  • New _post_json / _delete_with_body_json bytes helpers; batch_create / batch_cancel route through them via model_dump_json + content=, skipping one full dict-walk pass per call. Transport gained content / headers kwargs with mutual exclusion against json=.
  • New scripts/bench_ws_recv.py, scripts/bench_orderbook_delta.py, scripts/bench_request_hot_path.py for perf-regression visibility (all smoke-tested).
  • KALSHI_PRIVATE_KEY precedence over KALSHI_PRIVATE_KEY_PATH pinned via from_env + try_from_env tests verifying signature against the inline key.
  • tests/integration/conftest.py env-bridging moved from import-time os.environ mutation to a session-scoped monkeypatch fixture (test isolation fix).
  • KalshiAuth.close() is now terminal — _closed flag set, _get_sign_executor raises RuntimeError instead of silently respawning (pairs with Auth: KalshiClient.close() shouldn't shut down externally-provided KalshiAuth executor #210 ownership tracking).

436 unit tests green. ruff clean.

Closes #223
Refs #224, Refs #210

…minality; env isolation; from_env test

- _list_all cursor-loop guard switched from set[str] to single last_cursor: O(1) memory.

- Resource _base: new _post_json / _delete_with_body_json bytes helpers. Batch order paths use model_dump_json + httpx content= to skip one full dict-walk pass per call.

- Transport.request: optional content + headers kwargs (errors if json+content both set).

- KalshiAuth.close() is now terminal: subsequent sign_request_async raises RuntimeError instead of silently respawning the executor (pairs with #210 ownership tracking).

- tests/integration/conftest.py: env-bridging moved from import-time mutation to a session-scoped fixture with monkeypatch.

- tests/test_auth.py: KALSHI_PRIVATE_KEY takes precedence over KALSHI_PRIVATE_KEY_PATH in from_env / try_from_env (pinning current behavior).

- New scripts/bench_ws_recv.py, scripts/bench_orderbook_delta.py, scripts/bench_request_hot_path.py for perf regression visibility.

Closes #223

Refs #224
@claude

claude Bot commented May 21, 2026

Copy link
Copy Markdown

Code Review — PR #236

Overview

Wave 3-B delivers 6 focused polish items: O(1) cursor guard, batch-ops bytes path, bench scripts, close() terminality, env isolation in integration tests, and from_env precedence tests. The work is clean overall — a few items below are worth addressing before merge.


Issues

1. Cursor guard now allows multi-step cycles (behavioral change)

File: kalshi/resources/_base.py — _list_all (sync + async)

The switch from set[str] to last_cursor is a real memory win, but it silently removes protection against non-adjacent cursor cycles (A → B → A → B → …). The old code would have raised on the second A; the new code loops forever. The new test test_list_all_cursor_loop_detection_uses_last_cursor_only_o1 explicitly validates this — but the docstring only says "O(1) regardless of page count" without noting the reduced cycle-detection coverage.

A max_pages cap (already supported) mitigates this in practice, but the docstring should be honest about the tradeoff: this guard only detects consecutive replays, not arbitrary cycles.

2. headers parameter name is ambiguous in request()

File: kalshi/_base_client.py — SyncTransport.request / AsyncTransport.request

The new headers: dict[str, str] | None param is intended exclusively for body-helper Content-Type injection. The existing extra_headers already handles per-call headers. The generic name headers is confusing — a future caller could try headers={"X-Foo": "bar"} thinking it works like extra_headers. body_headers (which is what the internal variable is named) would be clearer, or at minimum the docstring should explicitly say "intended for body-helper Content-Type; use extra_headers for per-call headers."

3. Test indentation in test_auth.py

File: tests/test_auth.py, lines ~899 and ~945

with pytest.raises(InvalidSignature):
                    key_b.public_key().verify(sig, b"1GET/x", pss, hashes.SHA256())

The verify() call inside pytest.raises is indented ~20 extra spaces past the with block. This is cosmetically odd and may confuse a reader into thinking the indentation is meaningful. Should be 4 spaces inside the with.

4. Test duplication in test_auth.py

test_from_env_pem_takes_precedence_over_path and test_try_from_env_pem_takes_precedence_over_path are ~90% identical (only from_env() vs try_from_env()). A 10-line private helper taking the loader callable would eliminate ~30 lines of copy-paste. Not blocking, but worth noting per the CLAUDE.md "If 200 lines could be 50" guideline.


Smaller Observations

  • Missing unit tests for new base helpers in isolation. _post_json and _delete_with_body_json are tested transitively through batch_create/batch_cancel, but test_base_helpers.py has no direct test for them (e.g., testing the mutual exclusion TypeError for json= + content= simultaneously, or that _delete_with_body_json returns None on 204).

  • sync sign_request is not blocked after close(). The _closed flag guards _get_sign_executor (async path only). A closed KalshiAuth can still sign synchronously without error. The docstring says "callers should treat the instance as retired" — that's honest, but the asymmetry means a test relying on close() as a hard fence could still sign via the sync path unexpectedly. Whether to harden this is a separate decision, but it's worth a deliberate call.

  • bench_ws_recv.py restricts --depth to choices=[10, 100, 500]. Scripts are not production code, but the restriction is arbitrary and will confuse users who try --depth 50. Easy to drop.


What's Good

  • The _build_batch_create_body / _build_batch_cancel_body return-type change to bytes is clean and the Decimal precision test (test_batch_create_bytes_path_preserves_decimal_precision) is exactly the right regression anchor.
  • close() terminality is well-implemented: the _closed flag is set inside the lock atomically with executor shutdown, and the error message is actionable.
  • The integration conftest.py fix is correct — session-scoped monkeypatch is the right tool here, and the rollback guarantee eliminates the import-time env leak.
  • Bench scripts are well-structured and self-contained; the GC-count reporting in bench_ws_recv.py is a nice touch.
  • Test renames (TestBatchCancelRoutesThroughDeleteWithBodyJson) and the assertion-level verification (bytes content + json.loads check) are more robust than the old assert_called_once_with on a dict.

Summary: Items 1–3 above should be addressed before merge. Item 4 is a suggestion. Everything else is ready.

@TexasCoding
TexasCoding merged commit c5926f6 into main May 21, 2026
5 checks passed
@TexasCoding
TexasCoding deleted the audit/w3b-polish-perf-test branch May 21, 2026 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant