> ## Documentation Index
> Fetch the complete documentation index at: https://ladygraph.io.vn/llms.txt
> Use this file to discover all available pages before exploring further.

# core

> The data model, the interfaces, and the rules every other package follows.

LadyGraph core: the data model, rules and update logic shared by every system.

## `core.address`

Addresses: canonical URI strings, `<scheme>://<scope>/<locator>#<anchor>`.

#### `AddressParts`

| Field | Type | Default |
| - | - | - |
| `scheme` | `str` | required |
| `scope` | `str` | required |
| `locator` | `str` | required |
| `anchor` | `str` | required |

#### `canonical_address`

```python theme={null}
canonical_address(value: str) -> str
```

#### `parse_address`

```python theme={null}
parse_address(address: str) -> AddressParts
```

## `core.concept`

#### `Candidate`

| Field | Type | Default |
| - | - | - |
| `word` | `str` | required |
| `type` | `Literal['Rule', 'Body']` | required |
| `description` | `str` | required |
| `quote` | `str` | required |

#### `Found`

| Field | Type | Default |
| - | - | - |
| `candidates` | `list[Candidate]` | required |

#### `extraction_schema`

```python theme={null}
extraction_schema(types: list[str], relations: list[str], max_items: int = 40) -> type[pydantic.main.BaseModel]
```

The answer schema for one ontology: `type` and relation `type` can only be values it declares.

## `core.merge`

Applying a Contribution, and merging Concepts (INTERFACE §3, ownership).

#### `ApplyResult`

| Field | Type | Default |
| - | - | - |
| `concepts` | `dict[str, Concept]` | required |
| `events` | `list[tuple[Literal['created', 'updated', 'deprecated', 'deleted', 'merged'], str]]` | factory |

#### `apply_contribution`

```python theme={null}
apply_contribution(concepts: dict[str, Concept], c: Contribution) -> ApplyResult
```

1. Remove this unit's Evidence for the replaced passages (all of them when `replaces` is None), and Relations that lose all their Evidence.

#### `merge_concepts`

```python theme={null}
merge_concepts(keep: Concept, other: Concept) -> tuple[Concept, Concept]
```

Merge `other` into `keep`.

## `core.model`

The LadyGraph data model. Terms are defined in CONTEXT.md; see docs/INTERFACE.md §2–3.

#### `Actorstamp`

| Field | Type | Default |
| - | - | - |
| `by` | `str` | required |
| `at` | `datetime.datetime` | required |

#### `Concept`

| Field | Type | Default |
| - | - | - |
| `id` | `str` | required |
| `scope` | `str` | required |
| `path` | `str` | required |
| `type` | `str` | required |
| `context` | `str` | required |
| `title` | `str` | required |
| `words` | `list[Word]` | `[]` |
| `description` | `str` | `''` |
| `resource` | `Optional[Annotated[str, AfterValidator(func=<function canonical_address at 0x10154a340>)]]` | `None` |
| `body` | `str` | `''` |
| `evidence` | `list[Evidence]` | `[]` |
| `generated` | `Actorstamp` | required |
| `verified` | `list[Actorstamp]` | `[]` |
| `status` | `Literal['draft', 'stable', 'deprecated']` | `'stable'` |
| `relations` | `list[Relation]` | `[]` |
| `merged_into` | `str \| None` | `None` |
| `embedding` | `list[float] \| None` | `None` |
| `embedding_model` | `str` | `''` |
| `extra` | `dict[str, Any]` | `{}` |

| Method | What it does |
| - | - |
| `(property)` | — |
| `trust_tier(self) -> Literal['unverified', 'machine', 'human']` | — |

#### `Context`

| Field | Type | Default |
| - | - | - |
| `id` | `str` | required |
| `scope` | `str` | required |
| `parent` | `str \| None` | `None` |
| `label` | `str` | required |
| `description` | `str` | `''` |
| `metadata_seen` | `dict[str, list[str]]` | `{}` |
| `embedding` | `list[float] \| None` | `None` |
| `embedding_model` | `str` | `''` |
| `status` | `Literal['draft', 'stable']` | `'draft'` |
| `created_at` | `datetime.datetime` | required |

#### `ContextChoice`

A pinned context choice; a rebuild replays it instead of asking the LLM.

| Field | Type | Default |
| - | - | - |
| `passage_fingerprint` | `str` | required |
| `metadata_version` | `str` | required |
| `word` | `str` | required |
| `context` | `str` | required |
| `generated` | `Actorstamp` | required |

#### `Contribution`

| Field | Type | Default |
| - | - | - |
| `format` | `Literal['ladygraph/v1']` | `'ladygraph/v1'` |
| `scope` | `str` | required |
| `unit` | `str` | required |
| `version` | `DocVersion` | required |
| `replaces` | `list[str] \| None` | `None` |
| `access_labels` | `list[str]` | `[]` |
| `stale_after` | `datetime.datetime \| None` | `None` |
| `concepts` | `list[Concept]` | `[]` |
| `contexts` | `list[Context]` | `[]` |
| `choices` | `list[ContextChoice]` | `[]` |

#### `Declared`

An entry whose meaning a human declared in configuration, so no model is needed to read it.

| Field | Type | Default |
| - | - | - |
| `word` | `str` | required |
| `kind` | `str` | `''` |
| `type` | `str` | `''` |
| `keywords` | `list[str]` | `[]` |
| `facts` | `list[str]` | `[]` |
| `source` | `str` | `'config'` |

#### `Document`

| Field | Type | Default |
| - | - | - |
| `address` | `str` | required |
| `scope` | `str` | required |
| `unit` | `str` | required |
| `shape` | `Literal['assemble', 'decompose', 'map']` | required |
| `version` | `DocVersion` | required |
| `title` | `str` | required |
| `passages` | `list[Passage]` | required |
| `metadata` | `dict[str, Any]` | `{}` |
| `access_labels` | `list[str]` | `[]` |
| `valid_from` | `datetime.datetime \| None` | `None` |
| `valid_until` | `datetime.datetime \| None` | `None` |
| `declared` | `Declared \| None` | `None` |
| `rewrite` | `bool` | `False` |
| `reading` | `str` | `''` |
| `source` | `str` | `''` |

#### `DocVersion`

| Field | Type | Default |
| - | - | - |
| `file` | `str` | required |
| `metadata` | `str` | required |
| `passages` | `str` | required |

| Method | What it does |
| - | - |
| `combined(self) -> str` | — |
| `of_passages(fingerprints: list[str]) -> str` | — |

#### `Evidence`

One OKF `sources[]` entry: a position and a Fingerprint, never source text.

| Field | Type | Default |
| - | - | - |
| `id` | `str` | required |
| `resource` | `str` | required |
| `passage` | `str` | required |
| `start` | `int` | required |
| `length` | `int` | required |
| `fingerprint` | `str` | required |
| `unit` | `str` | required |
| `access_labels` | `list[str]` | `[]` |
| `stale_after` | `datetime.datetime \| None` | `None` |

#### `Passage`

| Field | Type | Default |
| - | - | - |
| `address` | `str` | required |
| `text` | `str` | required |
| `fingerprint` | `str` | required |
| `section` | `str` | `''` |

#### `Relation`

| Field | Type | Default |
| - | - | - |
| `type` | `str` | required |
| `target` | `str` | required |
| `weight` | `float` | `1.0` |
| `evidence_ids` | `list[str]` | `[]` |
| `unit` | `str` | required |

#### `Resolution`

| Field | Type | Default |
| - | - | - |
| `status` | `Literal['confirmed', 'moved', 'stale', 'unconfirmed']` | required |
| `start` | `int \| None` | `None` |
| `content` | `str \| None` | `None` |

#### `Word`

| Field | Type | Default |
| - | - | - |
| `text` | `str` | required |
| `kind` | `Literal['document', 'user']` | required |
| `generated` | `Actorstamp` | required |

#### `concept_id`

```python theme={null}
concept_id(scope: str, context_id: str, founding_word: str) -> str
```

Opaque and permanent.

#### `concept_path`

```python theme={null}
concept_path(scope: str, context_path: list[str], title: str) -> str
```

#### `context_id`

```python theme={null}
context_id(scope: str, parent: str | None, founding_label: str) -> str
```

#### `is_human`

```python theme={null}
is_human(actor: str) -> bool
```

## `core.normalize`

The two text normalizations. They must never be swapped.

#### `faithful`

```python theme={null}
faithful(text: str) -> str
```

#### `fingerprint`

```python theme={null}
fingerprint(text: str) -> str
```

Fingerprint of text that is already faithfully normalized.

#### `for_matching`

```python theme={null}
for_matching(text: str) -> str
```

#### `locate`

```python theme={null}
locate(normalized_passage: str, span_fingerprint: str, length: int, hint_start: int | None = None) -> int | None
```

Where the span with this fingerprint starts in the passage, or None.

#### `passage_fingerprint`

```python theme={null}
passage_fingerprint(raw_passage: str) -> str
```

#### `slug`

```python theme={null}
slug(text: str) -> str
```

## `core.ontology`

The ontology: the concept types and relation types a system allows.

#### `Ontology`

| Field | Type | Default |
| - | - | - |
| `types` | `dict[str, TypeSpec]` | `{}` |
| `relations` | `dict[str, RelationSpec]` | `{}` |

| Method | What it does |
| - | - |
| `load(cls, path: pathlib.Path) -> Ontology` | — |
| `from_yaml(cls, text: str) -> Ontology` | — |
| `fingerprint(self) -> str` | Stored in the manifest. |
| `is_known_type(self, type_: str) -> bool` | — |
| `check_relation(self, rel_type: str, src_type: str, dst_type: str \| None, actor: str) -> tuple[RelationCheck, str]` | `dst_type` is None while the target doesn't exist yet; its constraint is checked again when the target appears. |
| `check_cardinality(self, rel_type: str, targets_of_src: int, sources_of_dst: int) -> tuple[RelationCheck, str]` | Counts include the relation being checked. |
| `traversable(self, requested: list[str] \| None) -> list[str]` | Declared types with traverse=True; undeclared types only when named. |
| `extractor_brief(self) -> str` | The types and relations the LLM extractor may emit. |

#### `RelationCheck`

Construct with `RelationCheck(*values)`.

#### `RelationSpec`

| Field | Type | Default |
| - | - | - |
| `description` | `str` | `''` |
| `from_` | `list[str]` | factory |
| `to` | `list[str]` | factory |
| `requires_actor` | `Optional[Literal['human', 'process']]` | `None` |
| `origin` | `str` | `''` |
| `traverse` | `bool` | `True` |
| `weight` | `float` | `1.0` |
| `max_targets` | `int \| None` | `None` |
| `max_sources` | `int \| None` | `None` |
| `limit_hint` | `str` | `''` |

| Method | What it does |
| - | - |
| `(property)` | source:target, e.g. N:1 when each source has at most one target. |

#### `TypeSpec`

| Field | Type | Default |
| - | - | - |
| `description` | `str` | `''` |

## `core.pipeline`

Pipeline interfaces and the update stage (INTERFACE §6).

#### `Embedder`

Text to vectors, sync and async like `llm.LlmClient`.

Construct with `Embedder(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `embed(self, texts: Sequence[str], *, kind: Literal['document', 'query'] = 'document') -> list[list[float]]` | — |
| `aembed(self, texts: Sequence[str], *, kind: Literal['document', 'query'] = 'document') -> list[list[float]]` | — |

#### `Extractor`

Construct with `Extractor(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `extract(self, doc: Document, changed: set[str], ontology: Ontology, registry: ContextRegistry, pinned: list[ContextChoice], context_only: bool) -> Contribution` | Produce Concepts for the passages in `changed` (Fingerprints); leave every other passage alone. |

#### `GraphStore`

Construct with `GraphStore(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `apply(self, contribution: Contribution) -> None` | — |
| `drop_unit(self, scope: str, unit: str) -> None` | — |
| `versions(self, scope: str) -> dict[str, DocVersion]` | — |
| `pinned(self, scope: str, unit: str) -> list[ContextChoice]` | — |
| `known_types(self, scope: str) -> dict[str, str]` | — |
| `registry(self, scope: str) -> ContextRegistry` | — |
| `search(self, q: Query) -> list[Hit]` | — |
| `export(self, scope: str, path: pathlib.Path) -> None` | — |

#### `Resolver`

Construct with `Resolver(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `resolve_many(self, evidence: list[Evidence], timeout_s: float, with_content: bool = False) -> dict[str, Resolution]` | Keyed by evidence id. |

#### `Source`

Construct with `Source(*args, **kwargs)`.

| Method | What it does |
| - | - |
| `manifest(self, scope: str, now: datetime.datetime) -> dict[str, DocVersion]` | Valid change units only, with their versions. |
| `read(self, scope: str, unit: str) -> Document` | — |
| `next_boundary(self, scope: str, now: datetime.datetime) -> datetime.datetime \| None` | The earliest upcoming valid\_from or valid\_until. |

#### `UpdateResult`

| Field | Type | Default |
| - | - | - |
| `applied` | `dict[str, Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop']]` | factory |
| `dropped` | `list[str]` | factory |
| `rejected` | `dict[str, Report]` | factory |
| `next_run` | `datetime.datetime \| None` | `None` |

#### `changed_passages`

```python theme={null}
changed_passages(doc: Document, action: Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop'], pinned: list[ContextChoice]) -> tuple[set[str], set[str] | None]
```

(passages the extractor runs on, passages whose evidence is replaced).

#### `classify`

```python theme={null}
classify(old: DocVersion | None, new: DocVersion | None) -> Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop']
```

#### `contribution_scope`

```python theme={null}
contribution_scope(doc: Document, replaces: set[str] | None) -> dict
```

Fields every Contribution from `doc` must carry.

#### `plan_update`

```python theme={null}
plan_update(source_versions: dict[str, DocVersion], store_versions: dict[str, DocVersion]) -> dict[str, typing.Literal['add', 'file', 'passages', 'metadata', 'unchanged', 'drop']]
```

#### `replaces_for`

```python theme={null}
replaces_for(doc: Document, changed: set[str], pinned: list[ContextChoice], context_only: bool) -> set[str] | None
```

The `replaces` set `run_update` expects, from what an extractor is given.

#### `run_update`

```python theme={null}
run_update(scope: str, source: Source, extractor: Extractor, store: GraphStore, ontology: Ontology, now: datetime.datetime) -> UpdateResult
```

## `core.query`

The query interface (INTERFACE §8).

#### `Hit`

| Field | Type | Default |
| - | - | - |
| `address` | `str` | required |
| `score` | `float` | required |
| `path` | `list[str]` | required |
| `evidence_id` | `str` | required |
| `context_path` | `list[str]` | `[]` |
| `access_labels` | `list[str]` | `[]` |
| `resolution` | `Optional[Literal['confirmed', 'moved', 'unconfirmed']]` | `None` |
| `content` | `str \| None` | `None` |

#### `Query`

| Field | Type | Default |
| - | - | - |
| `scope` | `str` | required |
| `text` | `str \| None` | `None` |
| `vector` | `list[float] \| None` | `None` |
| `seeds` | `int` | `5` |
| `hops` | `int` | `1` |
| `fanout` | `int` | `10` |
| `relation_types` | `list[str] \| None` | `None` |
| `facets` | `dict[str, list[Any]]` | `{}` |
| `access_filter` | `list[str] \| None` | `None` |
| `resolve` | `bool` | `False` |
| `now` | `datetime.datetime \| None` | `None` |

## `core.redaction`

Contact details: patterns, the optional masks, and the rule that a contact detail is never a concept.

#### `contact_sentences`

```python theme={null}
contact_sentences(text: str) -> list[tuple[int, int]]
```

The (start, end) of each sentence of `text` that holds a contact detail or a mask, in order, without repeats.

#### `only_contact_detail`

```python theme={null}
only_contact_detail(text: str) -> bool
```

True when `text` is a contact detail or a mask with, at most, a stray word around it.

## `core.registry`

The Context registry and candidate ranking (INTERFACE §3, "Context").

#### `Candidate`

| Field | Type | Default |
| - | - | - |
| `context` | `Context` | required |
| `score` | `float` | required |

#### `ContextRegistry`

Construct with `ContextRegistry(scope: str, contexts: list[Context])`.

| Method | What it does |
| - | - |
| `copy(self) -> ContextRegistry` | An independent registry with the same contexts, so a caller can add drafts without side effects. |
| `get(self, context_id: str) -> Context \| None` | — |
| `add(self, context: Context) -> None` | — |
| `children(self, parent: str \| None) -> list[Context]` | — |
| `depth(self, context_id: str) -> int` | 1 for a top-level context, 2 for its children, and so on. |
| `path(self, context_id: str) -> list[str]` | Labels from the top level down to this context. |
| `rank(self, parents: list[str \| None], vector: list[float] \| None, metadata: dict[str, list[str] \| str], section: str = '', k: int = 3) -> list[Candidate]` | Top-k children of `parents`: vector similarity, plus a boost when the document metadata matches and a larger one when the section matches. |

## `core.runner`

When to run the update (INTERFACE §6).

#### `Scheduler`

Construct with `Scheduler(source: Source, extractor: Extractor, store: GraphStore, ontology: Ontology, scopes: list[str], *, poll: datetime.timedelta = datetime.timedelta(seconds=900), clock: Callable[[], datetime.datetime] = <function _now at 0x102524400>)`.

| Method | What it does |
| - | - |
| `trigger(self, scope: str) -> None` | The source changed: run this scope on the next tick. |
| `next_due(self) -> datetime.datetime` | — |
| `tick(self) -> dict[str, UpdateResult]` | Run every scope that is due. |
| `run_forever(self, stop: threading.Event \| None = None) -> None` | — |

## `core.validate`

Validating a Contribution before it is applied (INTERFACE §4).

#### `Report`

| Field | Type | Default |
| - | - | - |
| `errors` | `list[str]` | factory |
| `warnings` | `list[str]` | factory |

| Method | What it does |
| - | - |
| `(property)` | — |

#### `validate_contribution`

```python theme={null}
validate_contribution(c: Contribution, ontology: Ontology, known_types: dict[str, str]) -> tuple[Contribution, Report]
```

Return a cleaned copy of `c` and a report.

## `core.validity`

Validity periods: a Document is indexed only while it is in force.

#### `Period`

| Field | Type | Default |
| - | - | - |
| `start` | `datetime.datetime` | required |
| `end` | `datetime.datetime \| None` | required |

| Method | What it does |
| - | - |
| `contains(self, now: datetime.datetime) -> bool` | — |

#### `is_valid`

```python theme={null}
is_valid(published: bool, active: bool, period: Period, now: datetime.datetime) -> bool
```

#### `local_midnight`

```python theme={null}
local_midnight(day: datetime.date, tz: zoneinfo.ZoneInfo = zoneinfo.ZoneInfo(key='Asia/Ho_Chi_Minh')) -> datetime.datetime
```

#### `next_boundary`

```python theme={null}
next_boundary(periods: list[Period], now: datetime.datetime) -> datetime.datetime | None
```

The earliest instant after `now` when some Document enters or leaves.

#### `validity_period`

```python theme={null}
validity_period(published_at: datetime.datetime, effective: datetime.date | None, expiry: datetime.date | None, replacement_effective: list[datetime.date], tz: zoneinfo.ZoneInfo = zoneinfo.ZoneInfo(key='Asia/Ho_Chi_Minh')) -> Period
```

`replacement_effective` holds the effective dates of Documents that fully replace this one AND are themselves valid; the caller resolves that.

## `core.words`

Matching a question against the words a human declared for a concept.

#### `canonical`

```python theme={null}
canonical(normalized: str) -> str
```

Join a form code to its variant letter, on text that `for_matching` has already normalized.

#### `matches`

```python theme={null}
matches(prepared_question: str, clause: str) -> bool
```

#### `positive_terms`

```python theme={null}
positive_terms(clause: str) -> list[str]
```

The terms of a clause that must appear, as written (with their accents), for showing a person.

#### `prepare_question`

```python theme={null}
prepare_question(question: str) -> str
```

The question in the form `matches` needs, so it is normalized once per search.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.