Skip to content

fix(kalshi): stabilize event description template and outcome labels#75

Merged
realfishsam merged 4 commits into
pmxt-dev:mainfrom
0xharryriddle:harry/fix-kalshi-inconsistent
Apr 9, 2026
Merged

fix(kalshi): stabilize event description template and outcome labels#75
realfishsam merged 4 commits into
pmxt-dev:mainfrom
0xharryriddle:harry/fix-kalshi-inconsistent

Conversation

@0xharryriddle

Copy link
Copy Markdown
Contributor

Problem

Two Kalshi normalization regressions:

  1. UnifiedEvent.description could become malformed (e.g. If {x}0, 2026...) when one market in an event had a slightly different suffix date.
  2. MarketOutcome.label could become :: Democratic / Not :: Democratic because subtitle was preferred over yes_sub_title for some markets.

Root Cause

  • Description logic in core/src/exchanges/kalshi/normalizer.ts used raw longest common prefix/suffix slicing, which is brittle under small suffix drift.
  • Label logic in the same file chose market.subtitle || market.yes_sub_title, but Kalshi sometimes uses subtitle for structural metadata (:: Party) rather than candidate names.

What Changed

  1. core/src/exchanges/kalshi/normalizer.ts
  • Added deriveOutcomeLabel() and cleanLabel():
    • Prefer yes_sub_title.
    • Ignore structural labels starting with ::.
  • Replaced old deriveEventDescription() prefix/suffix algorithm with template voting:
    • Template each rules_primary by substituting candidate name with {x}.
    • Choose the most frequent template.
    • Fallback to first rule when needed.
  • Prevents malformed template outputs and preserves meaningful event descriptions.
  1. core/src/exchanges/kalshi/utils.ts
  • Applied the same label precedence/sanitization in the legacy utility mapping path for consistency.
  1. core/test/unit/normalizers/kalshi.test.ts
  • Added regression tests for:
    • Mixed suffix/date description case (KXCABOUT-26MAR-style).
    • subtitle=':: ...' contamination case (KXGOVCA-26-style).

Validation

  • Ran: pnpm -C core test -- kalshi.test.ts

  • Result:

> jest -c jest.config.js -- kalshi.test.ts

 PASS  src/exchanges/kalshi/kalshi.test.ts
 PASS  test/unit/normalizers/kalshi.test.ts

Test Suites: 2 passed, 2 total
Tests:       26 passed, 26 total
Snapshots:   0 total
Time:        0.385 s, estimated 1 s
Ran all test suites matching kalshi.test.ts.

Prevents raw rules (where candidate name could not be substituted) from
winning the event description vote and leaking a specific name.
ASCII \b fails on names with accents, apostrophes, or non-Latin
characters. Unicode property escapes keep templating correct.
@realfishsam realfishsam merged commit e3f7736 into pmxt-dev:main Apr 9, 2026
1 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants