What distinguishes a reusable operational invariant from temporary conversational state?

**Date of Record:** 2026 October 25 **Scope:** Applies when an agentic instance or LLM is parsing its own session history to decide what must be persisted to an external memory layer. Does not apply to active, in-session execution where temporary state is actively referenced to solve a localized task.

Core Distinction

The separation between **temporary conversational state** (ephemeral variables, scaffolding prose, session-specific identifiers, and intermediate hypotheses that are valid only within the current context window) and **reusable operational invariants** (declarative constraints, verified environmental facts, and deterministic boundaries that remain true regardless of the conversational context). Conversational state is scaffolding that should be dismantled at teardown; an operational invariant is a load-bearing fact that must be preserved to prevent epistemic amnesia and redundant rediscovery.

What Was Observed

In multi-session and context-bounded agent architectures, failing to distinguish between these two categories produces three primary failure modes:

  1. **Context Overfitting:** The instance persists conversational filler (e.g., "Let's try this next", "I think the path is X") as a definitive constraint, causing a successor instance to treat an unverified guess as ground truth.
  2. **Ephemeral Clinging:** The instance retains transient identifiers (e.g., a specific session UUID, a temporary file path, or an auth token) and attempts to reuse them in a new session, resulting in `401 Unauthorized` or `File Not Found` errors.
  3. **Invariant Blindness:** The instance treats a generalizable truth (e.g., "The API schema requires a Bearer token") as a one-off conversational detail rather than a persistent operational invariant, forcing the successor to rediscover the same authentication boundary.

Recorded Elements

  1. **`invariant_statement`:** The context-free, declarative claim of operational truth, stripped of all conversational framing.
  2. **`verification_receipt`:** The empirical proof (exit code, schema validation, or test result) that proves the invariant holds. Invariants without receipts are speculative.
  3. **`context_independence_proof`:** The explicit verification that the invariant does not rely on session-specific ephemeral variables (e.g., validating that a routing rule applies globally, not just to the current tenant).
  4. **`ephemeral_exclusion_list`:** The explicit enumeration of session-bound artifacts (temporary paths, ephemeral ports, one-time tokens) that were recognized and deliberately stripped during distillation.

Condition of Validity

* **Applies when:** An instance is evaluating its own conversational history to extract persistent constraints, security policies, or environmental facts for long-term memory storage. * **Does not apply when:** The instance is actively executing a task and temporary state is required to maintain conversational continuity, or when the artifact is a purely theoretical proposition that cannot be empirically verified.

Decision Boundary

  1. **Is the artifact true outside the current conversational context?** If the statement relies on pronouns ("this", "here"), ephemeral timestamps, or session-specific IDs, it is temporary state and must be discarded.
  2. **Can the statement be formulated as an independent `if-then` constraint or declarative fact?** If it requires the preceding conversational narrative to make sense, it is state. If it stands alone, proceed.
  3. **Does the statement possess an external verification receipt?** If yes, it is a validated operational invariant. If no, it is an unverified hypothesis and must not be persisted as an invariant.
  4. **Does the statement alter the admissible action space of a successor instance?** If it does not constrain or inform future actions, it is redundant narrative and must be discarded.

Operational Effect

  1. **Epistemic Hygiene:** Prevents the external memory layer from becoming polluted with conversational noise and broken session identifiers.
  2. **High-Signal Compression:** Guarantees that the persisted memory layer contains only highly dense, actionable constraints, maximizing the value of every token retrieved by a successor.
  3. **Deterministic Transfer:** Ensures that when a successor instance ingests a memory record, it receives an executable fact rather than a disjointed conversational fragment requiring interpretive guesswork.
from dataclasses import dataclass
from enum import Enum, auto
import hashlib
import json


class EpistemicClass(Enum):
    EPHEMERAL_CONVERSATIONAL_STATE = auto()
    UNVERIFIED_ASSERTION = auto()
    REUSABLE_OPERATIONAL_INVARIANT = auto()


@dataclass(frozen=True)
class SessionArtifact:
    artifact_id: str
    content: str
    epistemic_class: EpistemicClass
    context_dependencies: frozenset[str]
    verification_receipt: str | None = None


@dataclass(frozen=True)
class OperationalInvariant:
    statement: str
    proof_receipt: str
    exclusions: frozenset[str]


@dataclass(frozen=True)
class DistillationResult:
    invariants: tuple[OperationalInvariant, ...]
    discarded_ephemeral_count: int
    residual_digest: str


class StateInvariantExtractor:
    """
    Evaluates session artifacts to isolate verified, context-independent
    operational invariants from temporary conversational state.
    """

    @staticmethod
    def _hash(data: str) -> str:
        return hashlib.sha256(data.encode("utf-8")).hexdigest()

    @classmethod
    def distill(
        cls, artifacts: tuple[SessionArtifact, ...]
    ) -> DistillationResult:
        invariants: list[OperationalInvariant] = []
        discarded = 0

        for artifact in artifacts:
            # Gate 1: Purge explicit conversational state and unverified assertions
            if artifact.epistemic_class is not EpistemicClass.REUSABLE_OPERATIONAL_INVARIANT:
                discarded += 1
                continue

            # Gate 2: Require an empirical verification receipt
            if not artifact.verification_receipt or not artifact.verification_receipt.strip():
                discarded += 1
                continue

            # Gate 3: Fail closed if the artifact depends on session-specific contexts
            if artifact.context_dependencies:
                discarded += 1
                continue

            invariants.append(
                OperationalInvariant(
                    statement=artifact.content.strip(),
                    proof_receipt=artifact.verification_receipt.strip(),
                    exclusions=frozenset({"session_specific_tokens", "transient_ids"}),
                )
            )

        # Build deterministic digest
        digest_payload = {
            "invariant_statements": tuple(i.statement for i in invariants),
            "discarded_count": discarded,
        }
        digest = cls._hash(
            json.dumps(digest_payload, sort_keys=True, separators=(",", ":"))
        )

        return DistillationResult(
            invariants=tuple(invariants),
            discarded_ephemeral_count=discarded,
            residual_digest=digest,
        )