Cluster markets into events, themes, and underlying entities for cross-market reasoning.
3. Why This Bot Matters
Cross-market correlation invisible to Risk
Without an ontology, PortfolioGuard sees fifty independent markets where the underlying ontology says they all resolve on the same NFL game. Aggregate exposure to a single real-world event is then dramatically under-counted.
Strategies can't compose multi-market positions
Cross-market arbitrage and hedging strategies need a structured map of which markets share an underlying. Hand-curated lists drift; a generated ontology stays in sync as new markets list each day.
Search and discovery degrade as the catalogue grows
With ten thousand listed markets and no ontology, MarketScanner becomes a substring search. Themed events and entity-level queries — 'all markets resolving on US election' — require structured clusters.
Reconciliation can't verify outcome correctness
When a real-world event fires, gov.reconciler needs to confirm every market that should have resolved did. Without an ontology mapping events to markets, that check is manual.
4. Required Polymarket Inputs
Input
Source
Required?
Use
Full active market list with tags and categories from Gamma API
gamma
Yes
Primary data source for building the market category ontology.
Tag taxonomy from Data API
data
No
Supplement Gamma tag data with canonical tag hierarchy.
5. Required Internal Inputs
Input
Source
Required?
Use
KillSwitch active flag
KillSwitch
Yes
Suppress all ontology build emissions when KillSwitch is active.
6. Parameter Guide
Parameter
Default
Warning
Hard
What it controls
build_interval_s
3600
7200
86400
Seconds between full ontology rebuild cycles.
min_market_count
50
20
5
Minimum number of active markets returned by Gamma before ontology build proceeds.
7. Detailed Parameter Instructions
build_interval_s
What it means
Seconds between full ontology rebuild cycles.
Default
{ "build_interval_s": 3600 }
Why this default matters
3600 s (1 h) captures new market listings and category changes without overloading Gamma API.
Threshold logic
Condition
Action
interval <= 3600 s
Normal
3600–7200 s
WARN — reduced freshness of ontology
> 86400 s
Reject — PARAMETER_CHANGE_REQUIRES_APPROVAL
Developer check
if (p.build_interval_s > p.hard) throw ConfigError('PARAMETER_CHANGE_REQUIRES_APPROVAL');
User-facing English
Market category structure is rebuilt hourly to reflect new listings.
min_market_count
What it means
Minimum number of active markets returned by Gamma before ontology build proceeds.
Default
{ "min_market_count": 50 }
Why this default matters
50 ensures the build is not based on a truncated Gamma response.
Threshold logic
Condition
Action
count >= 50
Normal — proceed with build
5–50
WARN — MARKETONTOLOGYBUILDER_TAG_COUNT_DROP; retain last ontology
< 5
Reject — do not rebuild; emit STALE_DATA
Developer check
if (markets.length < p.min_market_count.hard) emit('STALE_DATA');
User-facing English
Ontology is only updated when a sufficient number of markets are available.
Strategy used category filter to avoid low-quality market segments
The system maintains a live map of market categories to help strategies focus on well-structured markets. Category filters are updated hourly.
Ontology update emitted after new markets added
New markets were detected in a category during the last rebuild cycle. Category-aware strategies will see these markets in their next scan.
18. Failure-Mode Block
main_failure_mode
Gamma API outage for > 1 h causes MarketOntologyBuilder to serve a stale ontology, reducing category-aware strategy accuracy for markets that were newly listed during the outage.
false_positive_risk
Gamma API pagination bug returns a truncated market list, causing MARKETONTOLOGYBUILDER_TAG_COUNT_DROP to fire and retaining the previous (potentially stale) ontology.
false_negative_risk
New market category introduced during a build cycle is missed because the Gamma response is cached, causing the ontology to omit the new category until the next full rebuild.
safe_fallback
If market_count < min_market_count hard floor, retain last full ontology and emit STALE_DATA WARN. Do not replace a valid ontology with a partial one.
required_dependencies
Polymarket Gamma API, KillSwitch active flag, Redis for ontology snapshot storage
19. Failure-Injection Recipes
Scenario
How to inject
Expected behaviour
Recovery
GAMMA_TRUNCATED_RESPONSE
Return only 10 markets from mock Gamma API when 300+ expected
Automatic on next successful full build
GAMMA_API_DOWN
Block Gamma API for 2 h
Full rebuild triggered automatically on Gamma recovery
KILL_SWITCH_ON
Set killswitch.active=true during ontology build cycle
Emission resumes on KillSwitch reset
20. State & Persistence
Cold-start recovery
On cold start, trigger full rebuild on first cycle; last snapshot may be stale.
21. Concurrency & Idempotency
Aspect
Specification
Execution model
single-threaded periodic batch rebuild
Max in-flight
1
Idempotency key
ontology_hash
Per-call timeout (ms)
30000
Backpressure strategy
skip cycle if previous build still in progress
Locking / mutual exclusion
Redis SETNX on build_lock key to prevent concurrent rebuilds
MARKETONTOLOGYBUILDER_TAG_COUNT_DROP WARN; last ontology retained; no rebuild
KillSwitch suppresses emission
killswitch.active=true; valid ontology built internally
No ObservationReport; KILL_SWITCH_ACTIVE logged
Integration Tests
Test
Expected result
Ontology change detected and consumed by strategy
Strategy receives updated ontology_hash and category_count after rebuild
Gamma API down: stale ontology retained; STALE_DATA emitted after 2 h
STALE_DATA WARN after 2 h; last valid ontology snapshot still accessible
Property Tests
Property
Required behaviour
MarketOntologyBuilder never submits or signs orders
Always true
Ontology is never replaced with a build from fewer than min_market_count hard floor markets
Always true
27. Operational Runbook
MarketOntologyBuilder incidents are typically Gamma API outages or truncated responses. Stale ontology does not block trading but may reduce category signal quality.
On-call actions
Alert
First step
Diagnosis
Mitigation
Escalate to
MarketOntologyBuilderStale
Check Gamma API health and last_build_at_ms. Trigger manual rebuild via override if API is healthy.
Intelligence pod lead if stale for > 2 h
MarketOntologyBuilderTagCountDrop
Check Gamma API response count. If truncated, do not replace ontology. Investigate Gamma pagination issues.
Intelligence pod lead if drop is > 10 categories
Manual overrides
force_rebuild — POST /internal/marketontologybuilder/rebuild to trigger an immediate full rebuild — After Gamma API recovery or known category structure changes
Healthcheck
Endpoint: /internal/health/marketontologybuilder | Green: Last build < 2 h ago AND Redis reachable AND Gamma API returning 200 | Red: No successful build for > 2 h OR Redis unreachable
28. Promotion Gates
A bot does not advance to the next readiness state until every gate below is green. Gates are observable from production data — no subjective sign-off.
Promote to Shadow
Gate
How measured
Threshold
Unit tests pass for tag-count drop guard, ontology hash change detection, and KillSwitch suppression
CI test run
100% pass
Promote to Limited live
Gate
How measured
Threshold
Full ontology build completes in < 30 s on staging with 500 active markets
Integration test
Build time < 30 s
Promote to General live
Gate
How measured
Threshold
Zero truncation events over 14-day soak with live Gamma API