ontocast.stategraph.context_resolver¶
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
UnitOntologyContext
¶
Bases: BaseModel
Assembled prompt context: snapshot view + writable catalog IRIs for apply.
Source code in ontocast/stategraph/context_resolver.py
Attributes¶
assembly_mode
property
¶
confidence = 0.0
class-attribute
instance-attribute
¶
patch_sources
property
¶
primary_writable_iri
property
¶
Primary catalog IRI for metrics (first writable, else null).
snapshot
instance-attribute
¶
writable_iris = Field(default_factory=list)
class-attribute
instance-attribute
¶
Functions:¶
aggregate_writable_metrics(unit_contexts)
¶
Aggregate per-unit writable IRI / source / mode metrics.
Accepts either :class:UnitOntologyContext or legacy
(primary_iri, patch_sources, mode) tuples for map-stage collect.
Source code in ontocast/stategraph/context_resolver.py
build_merged_document_ontology_context(context)
¶
Build merged ontology context from reduced document artifacts.
The result depends only on document-level state, so it should be computed
once per document. "ctx/merge_document_ontology.calls" on the budget
tracker exists to make a regression to per-unit calls visible.
Source code in ontocast/stategraph/context_resolver.py
build_unioned_document_ontology_context(context, tools, units)
async
¶
Resolve every unit's context once and union them into one shared context.
Per-unit retrieval gives each unit a smaller chapter than the union would be, and that is a real saving on the first call for a unit. It is a loss on every call after it: the chapter is the bulk of a facts prompt, no two units get the same one, and a provider's prefix cache can therefore serve none of them -- so a document pays the chapter once per unit at full price instead of once at full price and N-1 times at the cached rate.
Unioning is the trade that makes the second arrangement available. It is recall-safe by construction: the union contains every atom each unit's own retrieval selected, so no unit is shown less than it would have been. What it costs is precision -- a unit also sees its siblings' terms -- and the per-call token count, which is why it is a setting and not the default.
Retrieval runs concurrently, bounded the same way the unit fan-out is. In the vector modes this is embedding and graph work with no LLM call; the LLM-selection mode does spend one call per unit here, exactly as it would have spent inside the fan-out.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
UnitLoopContext
|
Document-level loop inputs. |
required |
tools
|
ToolBox
|
Toolbox holding the catalog and retrieval. |
required |
units
|
Sequence[SourceUnit]
|
The document's content units. |
required |
Returns:
| Type | Description |
|---|---|
UnitOntologyContext | None
|
The unioned context, or None when no unit resolved anything -- which |
UnitOntologyContext | None
|
leaves the caller on the per-unit path rather than handing every unit an |
UnitOntologyContext | None
|
empty snapshot. |
Source code in ontocast/stategraph/context_resolver.py
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 | |
resolve_unit_ontology_context(context, tools, unit, *, can_create_vocabulary=False)
async
¶
Assemble the ontology context one content unit is rendered against.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
UnitLoopContext
|
Document-level loop inputs. |
required |
tools
|
ToolBox
|
Toolbox holding the catalog and retrieval. |
required |
unit
|
SourceUnit
|
The content unit being rendered. |
required |
can_create_vocabulary
|
bool
|
Whether the caller can act on an empty context
by inventing vocabulary. True for the ontology loop, which answers
an empty seed with |
False
|
Returns:
| Type | Description |
|---|---|
UnitOntologyContext
|
The resolved context, possibly empty. |
Raises:
| Type | Description |
|---|---|
EmptyOntologyContextError
|
The context is empty, the caller cannot create vocabulary, and this deployment requires a context. |
Source code in ontocast/stategraph/context_resolver.py
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 | |