ontocast.onto.unit_states¶
Dedicated state models for parallel unit loops.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
UnitFactsState
¶
Bases: UnitState
Independent per-unit state for facts extraction and critique.
Source code in ontocast/onto/unit_states.py
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
Attributes¶
applied_repairs = Field(default_factory=list, description='Deterministic rewrites the machine applied to rendered graphs (alias repairs, rdf:type literal coercions) — the provenance trail distinguishing machine-altered triples from LLM output.')
class-attribute
instance-attribute
¶
conformance_chapter = Field(default='', description="Shapes-derived CONFORMANCE REQUIREMENTS chapter, rendered once per tenancy and shared into every unit's render and critic prompts. Empty when the deployment has no shapes or the contract is off.")
class-attribute
instance-attribute
¶
conformance_selection_pending = Field(default=False, description="The conformance chapter is to be selected per unit by joining the shapes on this unit's resolved ontology snapshot -- set by the fan-out when the shapes catalog outgrows the prompt cap, consumed by the unit loop right after context resolution.")
class-attribute
instance-attribute
¶
content_unit = Field(description='Unit under processing (mutable)')
class-attribute
instance-attribute
¶
critic_outcome = Field(default=None, description="How the critic pass ended for this unit: 'reviewed' -- a critique came back and was compiled; 'unavailable' -- the call failed (timeout, unparseable response), so the render stands unreviewed and no patch is applied; 'skipped' -- the loop did not call the critic (render below FACTS_CRITIC_MIN_TRIPLES, or a citation-metadata unit). None when no critic pass was configured. A unit whose critic timed out used to leave the loop as SUCCESS, indistinguishable from one the critic accepted.")
class-attribute
instance-attribute
¶
deterministic_findings = Field(default_factory=list, description='Machine-found violations/coverage gaps injected as MANDATORY fixes into the next repair render.')
class-attribute
instance-attribute
¶
facts_updates = Field(default_factory=list)
class-attribute
instance-attribute
¶
facts_user_instruction = Field(default='')
class-attribute
instance-attribute
¶
shapes_contract_terms = Field(default=(), description="IRIs the shapes contract instructs the renderer to emit; exempt from UNKNOWN_TERM so the validator never orders removal of what the contract required. Always the full catalog's terms, whatever chapter selection does.")
class-attribute
instance-attribute
¶
Methods:¶
apply_patch(update)
¶
Apply through facts_updates, the same channel a render uses.
For facts the graph is the unit's product, so nothing downstream has to be told about the change separately.
Source code in ontocast/onto/unit_states.py
patch_target_graph()
¶
product_triple_count()
¶
restore(token)
¶
snapshot_for_rollback()
¶
update_facts()
¶
Apply facts_updates to content_unit.graph and clear the list.
Source code in ontocast/onto/unit_states.py
UnitOntologyState
¶
Bases: UnitState
Independent per-unit state for ontology improvement loop.
Source code in ontocast/onto/unit_states.py
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
Attributes¶
all_updates
property
¶
All ontology updates produced by this unit (applied and pending).
current_domain = Field(default=DEFAULT_DOMAIN)
class-attribute
instance-attribute
¶
deterministic_findings = Field(default_factory=list, description="Machine-found issues in this unit's ontology delta, injected into the critic prompt and summed into the document residual.")
class-attribute
instance-attribute
¶
fresh_ontology = Field(default=None, description='Full Ontology produced on the fresh-create path (empty seed).')
class-attribute
instance-attribute
¶
ontology_max_triples = Field(default=None)
class-attribute
instance-attribute
¶
ontology_updates = Field(default_factory=list)
class-attribute
instance-attribute
¶
ontology_updates_applied = Field(default_factory=list)
class-attribute
instance-attribute
¶
ontology_user_instruction = Field(default='')
class-attribute
instance-attribute
¶
working_graph = Field(default_factory=RDFGraph, description='Mutable scratchpad graph for in-loop GraphUpdate application.')
class-attribute
instance-attribute
¶
Methods:¶
apply_patch(update)
¶
Apply through ontology_updates, never straight to the graph.
This is the one place the two phases genuinely differ. A unit's ontology
product is not working_graph: :meth:build_delta replays
all_updates onto a fresh copy of the snapshot and diffs, so a patch
written directly into the scratchpad would be reported by no delta and
dropped at reduce time -- applied, visible in the loop, and absent from
the output.
Routing through the update channel also inherits the
ontology_max_triples backstop: a refusal leaves the graph untouched
and says so, rather than half-applying.
Source code in ontocast/onto/unit_states.py
build_delta()
¶
Net insert/delete delta of this unit against its prompt snapshot.
All GraphUpdates (applied and pending) are replayed in order onto a copy of the snapshot, then diffed against it. This honors operation order -- a triple deleted and later re-inserted nets out -- and yields:
inserts: true complements (U \ S), never restated context triples;deletes: snapshot triples removed by delete operations, to be propagated onto catalog terminals during reduce.
Fresh path (no GraphUpdates, empty seed): full working graph as inserts. Costs a snapshot copy per call, which is why the snapshot is otherwise shared by reference -- callers on the per-unit hot path budget-time it.
Source code in ontocast/onto/unit_states.py
model_post_init(__context)
¶
Initialize mutable working graph from immutable snapshot.
Source code in ontocast/onto/unit_states.py
patch_target_graph()
¶
product_triple_count()
¶
Insert count, not working-graph size.
The working graph is the snapshot plus this unit's delta, so it barely moves -- a rollback rule keyed on its size would never fire. What the unit contributes is the delta.
Source code in ontocast/onto/unit_states.py
restore(token)
¶
Undo a pass, including what it queued for the reduce step.
Restoring the graph alone is not enough: build_delta replays the
recorded updates, so a rolled-back patch left in
ontology_updates_applied would be put back into the product even
though the scratchpad no longer holds it.
Source code in ontocast/onto/unit_states.py
snapshot_for_rollback()
¶
update_ontology()
¶
Apply ontology_updates to working_graph and clear the list.
Returns:
| Type | Description |
|---|---|
bool
|
True when the updates were applied. False means the |
bool
|
|
bool
|
graph is unchanged -- the caller must not report that as a |
bool
|
successful render without saying so, because a validator run |
bool
|
afterwards would inspect the pre-update graph and find it clean. |
Source code in ontocast/onto/unit_states.py
working_graph_changed()
¶
True when the scratchpad graph differs from the seed snapshot.
Plain set comparison is sound here: the working graph starts as an in-process copy of the snapshot graph (blank-node identity preserved, no serialization round-trip), so canonicalization-grade hashing adds cost without adding correctness.
Source code in ontocast/onto/unit_states.py
UnitState
¶
Bases: BasePydanticModel
Common per-unit workflow state.
content_unit is typed to the :class:SourceUnit base here and narrowed
to :class:ContentUnit by :class:UnitFactsState, which needs the mutable
graph. Declaring it once keeps the progress string and the context-assembly
fields below from being written twice.
Source code in ontocast/onto/unit_states.py
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 | |
Attributes¶
assembly_anchor_iri = Field(default='', description='Primary writable IRI from context assembly (metrics / logging).')
class-attribute
instance-attribute
¶
assembly_mode_used = Field(default=OntologyAssemblyMode.SELECTED_SINGLE_ONTOLOGY_LLM, description='How ontology_snapshot was assembled for this unit.')
class-attribute
instance-attribute
¶
attempt_log = Field(default_factory=list, description='Per-attempt telemetry (render/critic/repair) for this unit.')
class-attribute
instance-attribute
¶
budget_tracker = Field(default_factory=BudgetTracker)
class-attribute
instance-attribute
¶
content_unit = Field(description='Unit under processing')
class-attribute
instance-attribute
¶
critic_fixes_applied = Field(default=0, description='Critic fixes compiled straight to a patch with no LLM call.')
class-attribute
instance-attribute
¶
critic_fixes_junk_refused = Field(default=0, description='Critic inserts refused at compile time for minting a placeholder -- a subject named for an ignored token or artifact, or a new node carrying only annotations and no type.')
class-attribute
instance-attribute
¶
critic_fixes_noop = Field(default=0, description='Critic fixes whose delete set and insert set are the same statements, so they asked for no change at all.')
class-attribute
instance-attribute
¶
critic_fixes_residual = Field(default=0, description='Critic fixes that could not be compiled and were handed back as outstanding work. Never silently dropped.')
class-attribute
instance-attribute
¶
critic_fixes_rolled_back = Field(default=0, description='Critic fixes applied and then undone, one at a time, for leaving the unit worse: deleting without writing, or raising the mandatory finding count. The other fixes of the same pass stay.')
class-attribute
instance-attribute
¶
critic_fixes_unresolved_prefix = Field(default=0, description='Critic fixes whose payload named a prefix neither it nor the unit graph declares, so its statements could not be identified.')
class-attribute
instance-attribute
¶
external_evidence_cache = Field(default_factory=dict)
class-attribute
instance-attribute
¶
external_evidence_hits = Field(default_factory=list)
class-attribute
instance-attribute
¶
external_evidence_plan = Field(default_factory=ExternalEvidencePlan)
class-attribute
instance-attribute
¶
external_evidence_requests = Field(default_factory=dict)
class-attribute
instance-attribute
¶
external_evidence_text = Field(default='')
class-attribute
instance-attribute
¶
failure_reason = Field(default=None)
class-attribute
instance-attribute
¶
failure_stage = Field(default=None)
class-attribute
instance-attribute
¶
llm_graph_format = Field(default=LLMGraphFormat.TURTLE, description="Format used by the LLM for emitting RDF graph payloads: 'turtle' (default) or 'jsonld'.")
class-attribute
instance-attribute
¶
llm_output_layout = Field(default=LLMOutputLayout.COMPACT, description='Whitespace the LLM is asked to use in its structured responses. Threaded from ServerConfig like ontology_context_max_triples; read by both loops.')
class-attribute
instance-attribute
¶
max_visits_per_node = Field(default=1, ge=1)
class-attribute
instance-attribute
¶
node_visits = Field(default_factory=lambda: defaultdict(int))
class-attribute
instance-attribute
¶
ontology_chapter_format = Field(default=OntologyChapterFormat.INHERIT, description="Syntax of the ontology chapter in this unit's prompts: 'inherit' follows llm_graph_format, 'turtle' pins the chapter to Turtle. Threaded from ServerConfig like ontology_context_max_triples. Read by the facts loop only; the ontology loop keeps its chapter in the wire format because its output patches what it reads.")
class-attribute
instance-attribute
¶
ontology_context_max_triples = Field(default=None, description="Triple budget for the ontology chapter in this unit's prompts. None disables condensing. Threaded from ServerConfig alongside llm_graph_format, because the agents that build chapters do not all hold a ToolBox.")
class-attribute
instance-attribute
¶
ontology_patch_sources = Field(default_factory=list, description='Ontology IRIs that contributed to the snapshot context.')
class-attribute
instance-attribute
¶
ontology_snapshot = Field(default_factory=OntologySnapshot.empty, description='Immutable ontology snapshot (prompt view, no catalog id).')
class-attribute
instance-attribute
¶
ontology_text_caps = Field(default_factory=TextCaps, description="Per-role character caps on the text literals of this unit's ontology chapter. Threaded from ServerConfig like ontology_context_max_triples. All-unset is the default and leaves every literal exactly as the catalog authored it. Facts loop only, for the reason the chapter format is: a clipped literal is not the statement an ontology patch would cite.")
class-attribute
instance-attribute
¶
prompt_triple_index = Field(default=None, exclude=True, description='Ids handed to the critic for the graph it was shown, kept so the fixes it cites can be resolved. Excluded from serialization: it is a within-call reference table, not run output, and it is only valid for the graph state its fingerprint names.')
class-attribute
instance-attribute
¶
quarantined_literal_triples = Field(default_factory=list, description='Triples the render withheld from the applied graph because their XSD typed literals were invalid. On the base state because both update agents share the hygiene that produces it.')
class-attribute
instance-attribute
¶
status = Field(default=Status.NOT_VISITED)
class-attribute
instance-attribute
¶
suggestions = Field(default_factory=Suggestions)
class-attribute
instance-attribute
¶
writable_iris = Field(default_factory=list, description="Catalog IRIs that apply() may update from this unit's deltas.")
class-attribute
instance-attribute
¶
Methods:¶
apply_patch(update)
¶
Apply a compiled patch through this phase's own update channel.
Returns False when the phase refused it and its graph is unchanged.
clear_failure()
¶
get_content_unit_progress_string()
¶
get_external_evidence_cache_entry(node)
¶
Return node-scoped evidence cache entry.
get_external_evidence_request(node)
¶
Return node-scoped search request, defaulting to disabled.
load_external_evidence_for_node(node)
¶
Load node-scoped evidence cache into active prompt fields.
Source code in ontocast/onto/unit_states.py
patch_target_graph()
¶
product_triple_count()
¶
restore(token)
¶
set_external_evidence_cache_entry(node, entry)
¶
Persist node-scoped evidence plan/fetch result cache.