Skip to content

graflo.architecture.evolution.aspects.state.plan

Planning the state aspect: validity intervals, observations, units, groundings.

A pure planner: it emits :data:~graflo.architecture.evolution.ops.ManifestOp values and applies nothing. apply_evolution applies them, invert_ops undoes them, and the op list itself is the reviewable artifact -- an operator reads what the lift proposes before any of it runs. That is the whole argument for expressing this as Operations rather than as a bespoke transform: a transform is a black box that either did the right thing or did not.

What a lift does not do. It changes the contract, not the data flow. A lifted manifest declares <Type>State, but no resource populates it, so a manifest that carries an ingestion_model will still report the profile's "provenance materialised at ingest" finding afterwards -- correctly. Scaffolding the pipelines needs resource-level ops and a statement of which resource feeds which type, and is deliberately out of scope here. This is the misreading the next reader will have, so it is also in the CLI help.

Attributes

__all__ = ['StatePlan', 'plan_state'] module-attribute

Classes

StatePlan

Bases: ConfigBaseModel

What the state aspect planned, in the two halves a composed lift needs apart.

Attributes:

Name Type Description
ops list[ManifestOp]

The additive ops, in application order: grounding first, then structure, then the scaffolding.

scaffolded list[str]

Names of the <Type>State and <Type>Observation types the ops add, for an aspect that attaches to them.

retire list[ManifestOp]

The destructive half, to be applied last.

Source code in graflo/architecture/evolution/aspects/state/plan.py
class StatePlan(ConfigBaseModel):
    """What the state aspect planned, in the two halves a composed lift needs apart.

    Attributes:
        ops: The additive ops, in application order: grounding first, then
            structure, then the scaffolding.
        scaffolded: Names of the ``<Type>State`` and ``<Type>Observation``
            types the ops add, for an aspect that attaches to them.
        retire: The destructive half, to be applied last.
    """

    ops: list[ManifestOp] = PydanticField(default_factory=list)
    scaffolded: list[str] = PydanticField(default_factory=list)
    retire: list[ManifestOp] = PydanticField(default_factory=list)

Attributes

ops = PydanticField(default_factory=list) class-attribute instance-attribute
retire = PydanticField(default_factory=list) class-attribute instance-attribute
scaffolded = PydanticField(default_factory=list) class-attribute instance-attribute

Functions:

plan_state(manifest, spec, *, authored=None)

The ops that lift manifest into one that tracks state and measurements.

Parameters:

Name Type Description Default
manifest GraphManifest

the manifest to lift, already finish_init()-ed.

required
spec LiftSpec

the semantic declarations the planner cannot infer.

required
authored dict[str, Any] | None

the manifest as written, when available. Only an authored document distinguishes "this vertex declared no identity" from "this vertex declared every property as its identity", so without it the planner cannot detect the fallback and does not try.

None

Returns:

Type Description
StatePlan

The additive ops, the scaffolded type names, and the removals kept

StatePlan

apart so they can run last. Ordering matters -- a property is moved

StatePlan

onto its State before it is removed from the entity.

Raises:

Type Description
LiftError

a declaration the planner needs is missing, or a name it would mint is already taken.

Source code in graflo/architecture/evolution/aspects/state/plan.py
def plan_state(
    manifest: GraphManifest,
    spec: LiftSpec,
    *,
    authored: dict[str, Any] | None = None,
) -> StatePlan:
    """The ops that lift *manifest* into one that tracks state and measurements.

    Args:
        manifest: the manifest to lift, already ``finish_init()``-ed.
        spec: the semantic declarations the planner cannot infer.
        authored: the manifest as written, when available. Only an authored
            document distinguishes "this vertex declared no identity" from
            "this vertex declared every property as its identity", so without
            it the planner cannot detect the fallback and does not try.

    Returns:
        The additive ops, the scaffolded type names, and the removals kept
        apart so they can run last. Ordering matters -- a property is moved
        onto its ``State`` before it is removed from the entity.

    Raises:
        LiftError: a declaration the planner needs is missing, or a name it
            would mint is already taken.
    """
    schema = manifest.graph_schema
    if schema is None:
        raise LiftError(
            "cannot lift a manifest with no schema block: there are no types to "
            "ground and nothing to attach state to"
        )

    core = schema.core_schema
    vertex_config = core.vertex_config
    existing = set(vertex_config.vertex_set)
    by_name = {vertex.name: vertex for vertex in vertex_config.vertices}

    unknown = sorted(
        (set(spec.grounding) | set(spec.stateful) | set(spec.observed)) - existing
    )
    if unknown:
        raise LiftError(f"spec names types the manifest does not have: {unknown}")

    ops: list[ManifestOp] = []
    ops += _identity_ops(spec, vertex_config, authored)
    ops += _grounding_ops(spec, core)
    ops += _directionality_ops(core)
    ops += _unit_ops(spec, by_name)
    scaffold_ops, scaffolded = _scaffold_ops(spec, by_name, core)
    ops += scaffold_ops
    return StatePlan(ops=ops, scaffolded=scaffolded, retire=_retire_ops(spec, by_name))