Python API Reference¶
Top-level exports¶
worldoracle
¶
worldoracle - NPC contradiction detector and belief repair for game worlds.
ClosedLoopError
¶
Bases: RuntimeError
Raised when a worldoracle closed-loop gate refuses the world state.
AgentVote(agent_id, choice, round=0, confidence=1.0)
dataclass
¶
One agent vote in a debate/consensus round.
ConsensusCertificate(n_agents, n_rounds, agreement, plurality_choice, faction_count, factions, lambda2_hat, deadline_rounds, converged, vote_count)
dataclass
¶
Machine-checkable snapshot of collective agreement.
Attributes:
| Name | Type | Description |
|---|---|---|
n_agents |
int
|
Distinct agents seen. |
n_rounds |
int
|
Max round index + 1. |
agreement |
float
|
Fraction agreeing with plurality choice (latest round). |
plurality_choice |
str
|
Mode of latest-round votes. |
faction_count |
int
|
Number of distinct choices in latest round. |
factions |
dict[str, tuple[str, ...]]
|
choice → agent_ids (latest round). |
lambda2_hat |
float
|
Proxy for sub-dominant mixing: 1 - agreement (0 = fully mixed/agreed, closer to 1 = slow/stuck factions). |
deadline_rounds |
int | None
|
Estimated rounds to agreement under geometric contraction (ceil log residual / log lambda2) when lambda2 < 1. |
converged |
bool
|
agreement >= min_agreement and faction_count <= max_factions. |
BeliefRepairer
¶
Generate RepairFrames for contradictions.
Strategy priority order (applied in sequence, first match wins): 1. prefer_newer - higher timestamp wins 2. prefer_higher_confidence - higher confidence wins (when timestamps tied) 3. prefer_observation - source == 'observation' wins over hearsay 4. default - no tiebreaker; pred_a kept, reason logged
repair(pred_a, pred_b)
¶
Apply repair strategies in priority order and return a RepairFrame.
Source code in src/worldoracle/predicate.py
BeliefState(npc_id, predicates=list())
dataclass
¶
ContradictionDetector()
dataclass
¶
Find contradictions in a BeliefState.
detect(state)
¶
Return pairs of contradicting predicates (same subject+attribute, different values).
Source code in src/worldoracle/predicate.py
RepairFrame(predicate_a_id, predicate_b_id, strategy, resolved_value, reason, timestamp=0.0)
dataclass
¶
WorldPredicate(subject, attribute, value, source='', confidence=1.0, timestamp=0.0)
dataclass
¶
WorldOracleStore(path=':memory:')
¶
SQLite-backed persistence for WorldPredicates and RepairFrames.
Open (or create) the store at path. Use ':memory:' for in-process use.
Source code in src/worldoracle/store.py
close()
¶
save_predicate(npc_id, pred)
¶
Upsert a predicate for the given NPC.
Source code in src/worldoracle/store.py
get_belief_state(npc_id)
¶
Load all predicates for npc_id and return a BeliefState.
Source code in src/worldoracle/store.py
list_npc_ids()
¶
save_repair(repair)
¶
Upsert a repair frame.
Source code in src/worldoracle/store.py
get_repairs(predicate_a_id='', predicate_b_id='')
¶
Return repair frames, optionally filtered by predicate ID.
Source code in src/worldoracle/store.py
TemporalBeliefStore(store)
¶
Track how beliefs change over time using a separate snapshots table.
Source code in src/worldoracle/temporal.py
record_snapshot()
¶
Record current belief state as a timestamped snapshot. Returns snapshot ID.
Raises RuntimeError if the database fails to assign a row ID.
Source code in src/worldoracle/temporal.py
get_belief_at(subject, predicate, timestamp)
¶
Get a belief snapshot at or before the given timestamp.
Source code in src/worldoracle/temporal.py
get_belief_history(subject, predicate)
¶
Full history of a belief, newest first.
Source code in src/worldoracle/temporal.py
belief_drift(subject, predicate)
¶
Measure how much a belief's confidence has changed. 0=stable, 1=volatile.
Source code in src/worldoracle/temporal.py
gate_beliefs(store, *, mode='human_required', min_predicates=1)
¶
Run consistency check with explicit repair policy.
Modes: - report: detect only, ok if score==1 - auto_repair: allow automatic repair (NOT for legal gates) - human_required: if any contradiction, FAIL and demand human (default - matches farm rule LEGAL GATES NEVER AUTO-FIX)
Phase guard: empty store → FAIL_LOUD (do not run contradiction checks before data exists - Foundry G-CONTRA ordering bug).
Source code in src/worldoracle/closed_loop.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
assert_collective_consensus_ok(votes, **kwargs)
¶
Raise :class:ClosedLoopError unless :func:gate_collective_consensus is ok.
Source code in src/worldoracle/collective.py
certify_consensus(votes, *, min_agreement=0.67, max_factions=1)
¶
Build a consensus certificate from multi-agent vote traces.
Source code in src/worldoracle/collective.py
estimate_deadline(lambda2_hat, *, residual=0.05, max_rounds=10000)
¶
Estimate rounds until residual disagreement under geometric contraction.
deadline ≈ ceil( ln(residual) / ln(λ₂) ) when 0 < λ₂ < 1. Returns 0 if already mixed (λ₂≈0), None if λ₂ >= 1 (no contraction).
Source code in src/worldoracle/collective.py
gate_collective_consensus(votes, *, decision='finalize', min_agreement=0.67, max_factions=1, min_agents=2, max_rounds_without_convergence=None, require_votes=True)
¶
Refuse uncertified multi-agent finalize (COLLECTIVE-CERT / arXiv 2608.05956).
Rules:
- No votes when required → FAIL_LOUD
- Fewer than
min_agents→ FAIL_LOUD decision=finalizewithout convergence → FAIL (black-box stop)decision=finalizepast deadline without agreement → FAILdecision=continuewhen already converged → FAIL (waste rounds)decision=continuewhile not converged (under round budget) → PASSdecision=finalizewhen converged → PASS + certificate fields
Source code in src/worldoracle/collective.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
full_consistency_check(store, auto_repair=True)
¶
Run a full consistency check across ALL NPCs and optionally auto-repair contradictions.
Source code in src/worldoracle/consistency.py
diff_belief_states(store, before_timestamp, after_timestamp, subject=None)
¶
Diff belief state at two points in time using the snapshots table.
Source code in src/worldoracle/diff.py
print_beliefs(state, console=None)
¶
Print a belief state as a rich table.
Source code in src/worldoracle/report.py
print_repairs(repairs, console=None)
¶
Print a list of repair frames as a rich table, or a 'no repairs' message.
Source code in src/worldoracle/report.py
to_json(state, repairs=None)
¶
Serialize a BeliefState (and optional repairs) to JSON.
Source code in src/worldoracle/report.py
to_markdown(states)
¶
Render a list of BeliefStates as a Markdown report.