Skip to content

Incremental engine for long horizon agents 🌟 Star if you like it!

36.6 KB
Raw Blame
"""End-to-end tests for memo state validation.

Tests cover:
- Function-level memoization with state validation (sync and async)
- Component-level memoization with state validation (sync and async)
- State unchanged β†’ memo reused (function not re-executed)
- State changed, not reusable β†’ function re-executed, new states persisted
- State changed, still reusable β†’ function NOT re-executed, new states persisted
- State changed β†’ previously declared target states cleaned up (sync and async)
"""

from dataclasses import dataclass
from typing import Any

import pytest

import cocoindex as coco

from cocoindex._internal.memo_fingerprint import unregister_memo_type
from tests import common
from tests.common.target_states import (
    DictDataWithPrev,
    GlobalDictTarget,
    Metrics,
)

coco_env = common.create_test_env(__file__)


@dataclass
class Entry:
    """Entry with memo key and memo state for testing."""

    name: str
    version: int  # contributes to memo key
    state_value: int  # contributes to memo state (simulates mtime, etc.)
    content: str  # actual data

    def __coco_memo_key__(self) -> object:
        return (self.name, self.version)

    def __coco_memo_state__(self, prev_state: Any) -> coco.MemoStateOutcome:
        # State changed β†’ not reusable (simple case matching prior behavior)
        memo_valid = (
            not coco.is_non_existence(prev_state) and self.state_value == prev_state
        )
        return coco.MemoStateOutcome(state=self.state_value, memo_valid=memo_valid)


# ============================================================================
# Function-level state validation (sync)
# ============================================================================

_source_data: dict[str, Entry] = {}
_metrics = Metrics()


@coco.fn(memo=True)
def _transform_entry(entry: Entry) -> str:
    _metrics.increment("call.transform_entry")
    return f"processed: {entry.content}"


@coco.fn
def _process_data() -> None:
    for key, value in _source_data.items():
        transformed = _transform_entry(value)
        coco.declare_target_state(GlobalDictTarget.target_state(key, transformed))


def test_state_validation_sync() -> None:
    GlobalDictTarget.store.clear()
    _source_data.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(name="test_state_validation_sync", environment=coco_env),
        _process_data,
    )

    # Run 1: cache miss β€” both entries execute
    _source_data["A"] = Entry(name="A", version=1, state_value=100, content="contentA1")
    _source_data["B"] = Entry(name="B", version=1, state_value=200, content="contentB1")
    app.update_blocking()
    assert _metrics.collect() == {"call.transform_entry": 2}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="processed: contentA1", prev=[], prev_may_be_missing=True
        ),
        "B": DictDataWithPrev(
            data="processed: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 2: same entries, same state β†’ memo valid, 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: A state changes (state_value 100β†’101), B unchanged β†’ A re-executes
    _source_data["A"] = Entry(name="A", version=1, state_value=101, content="contentA2")
    app.update_blocking()
    assert _metrics.collect() == {"call.transform_entry": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="processed: contentA2",
            prev=["processed: contentA1"],
            prev_may_be_missing=False,
        ),
        "B": DictDataWithPrev(
            data="processed: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 4: no changes β†’ 0 calls (new states persisted correctly from run 3)
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# Function-level state validation (async)
# ============================================================================


@coco.fn.as_async(memo=True)
def _transform_entry_async(entry: Entry) -> str:
    _metrics.increment("call.transform_entry_async")
    return f"processed_async: {entry.content}"


@coco.fn
async def _process_data_async() -> None:
    for key, value in _source_data.items():
        transformed = await _transform_entry_async(value)
        coco.declare_target_state(GlobalDictTarget.target_state(key, transformed))


def test_state_validation_async() -> None:
    GlobalDictTarget.store.clear()
    _source_data.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(name="test_state_validation_async", environment=coco_env),
        _process_data_async,
    )

    # Run 1: cache miss β€” both entries execute
    _source_data["A"] = Entry(name="A", version=1, state_value=100, content="contentA1")
    _source_data["B"] = Entry(name="B", version=1, state_value=200, content="contentB1")
    app.update_blocking()
    assert _metrics.collect() == {"call.transform_entry_async": 2}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="processed_async: contentA1", prev=[], prev_may_be_missing=True
        ),
        "B": DictDataWithPrev(
            data="processed_async: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 2: same entries, same state β†’ memo valid, 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: A state changes β†’ A re-executes
    _source_data["A"] = Entry(name="A", version=1, state_value=101, content="contentA2")
    app.update_blocking()
    assert _metrics.collect() == {"call.transform_entry_async": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="processed_async: contentA2",
            prev=["processed_async: contentA1"],
            prev_may_be_missing=False,
        ),
        "B": DictDataWithPrev(
            data="processed_async: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 4: no changes β†’ 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# Component-level state validation (sync)
# ============================================================================


@coco.fn(memo=True)
def _declare_entry(key: str, entry: Entry) -> None:
    _metrics.increment("call.declare_entry")
    coco.declare_target_state(
        GlobalDictTarget.target_state(key, f"comp: {entry.content}")
    )


@coco.fn
def _declare_data() -> None:
    for key, value in _source_data.items():
        _declare_entry(key, value)


def test_state_validation_component_sync() -> None:
    GlobalDictTarget.store.clear()
    _source_data.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_validation_component_sync", environment=coco_env
        ),
        _declare_data,
    )

    # Run 1: cache miss
    _source_data["A"] = Entry(name="A", version=1, state_value=100, content="contentA1")
    _source_data["B"] = Entry(name="B", version=1, state_value=200, content="contentB1")
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_entry": 2}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="comp: contentA1", prev=[], prev_may_be_missing=True
        ),
        "B": DictDataWithPrev(
            data="comp: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 2: unchanged β†’ no re-execution
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: A state changes β†’ A re-executes, target state updated
    _source_data["A"] = Entry(name="A", version=1, state_value=101, content="contentA2")
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_entry": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="comp: contentA2",
            prev=["comp: contentA1"],
            prev_may_be_missing=False,
        ),
        "B": DictDataWithPrev(
            data="comp: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 4: no changes β†’ 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# Target state cleanup after state change (sync)
# ============================================================================


@dataclass
class MultiEntry:
    """Entry with memo key, memo state, and multiple sub-items for target states."""

    name: str
    version: int  # contributes to memo key
    state_value: int  # contributes to memo state
    items: dict[str, str]  # key β†’ content; each becomes a target state

    def __coco_memo_key__(self) -> object:
        return (self.name, self.version)

    def __coco_memo_state__(self, prev_state: Any) -> coco.MemoStateOutcome:
        memo_valid = (
            not coco.is_non_existence(prev_state) and self.state_value == prev_state
        )
        return coco.MemoStateOutcome(state=self.state_value, memo_valid=memo_valid)


_multi_source_data: dict[str, MultiEntry] = {}


@coco.fn(memo=True)
def _declare_multi(entry: MultiEntry) -> None:
    _metrics.increment("call.declare_multi")
    for key, content in entry.items.items():
        coco.declare_target_state(GlobalDictTarget.target_state(key, content))


@coco.fn
def _declare_multi_data() -> None:
    for value in _multi_source_data.values():
        _declare_multi(value)


def test_state_validation_target_state_cleanup_sync() -> None:
    """State change (memo key unchanged) causes re-execution that declares fewer
    target states β†’ the previously-declared target states should be cleaned up."""
    GlobalDictTarget.store.clear()
    _multi_source_data.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_validation_target_state_cleanup_sync",
            environment=coco_env,
        ),
        _declare_multi_data,
    )

    # Run 1: cache miss β€” declares target states A and B
    _multi_source_data["E1"] = MultiEntry(
        name="E1",
        version=1,
        state_value=100,
        items={"A": "contentA", "B": "contentB"},
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_multi": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(data="contentA", prev=[], prev_may_be_missing=True),
        "B": DictDataWithPrev(data="contentB", prev=[], prev_may_be_missing=True),
    }

    # Run 2: same state β†’ memo valid, 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: state changes (100β†’101), B removed from items β†’ re-executes,
    # only A declared. B target state should be cleaned up.
    _multi_source_data["E1"] = MultiEntry(
        name="E1",
        version=1,
        state_value=101,
        items={"A": "contentA2"},
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_multi": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="contentA2", prev=["contentA"], prev_may_be_missing=False
        ),
    }

    # Run 4: no changes β†’ 0 calls (new state persisted correctly)
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# Target state cleanup after state change (async)
# ============================================================================


@coco.fn.as_async(memo=True)
def _declare_multi_async(entry: MultiEntry) -> None:
    _metrics.increment("call.declare_multi_async")
    for key, content in entry.items.items():
        coco.declare_target_state(GlobalDictTarget.target_state(key, content))


@coco.fn
async def _declare_multi_data_async() -> None:
    for value in _multi_source_data.values():
        await _declare_multi_async(value)


def test_state_validation_target_state_cleanup_async() -> None:
    """Async variant: state change causes re-execution that declares fewer
    target states β†’ previously-declared target states should be cleaned up."""
    GlobalDictTarget.store.clear()
    _multi_source_data.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_validation_target_state_cleanup_async",
            environment=coco_env,
        ),
        _declare_multi_data_async,
    )

    # Run 1: cache miss β€” declares target states A and B
    _multi_source_data["E1"] = MultiEntry(
        name="E1",
        version=1,
        state_value=100,
        items={"A": "contentA", "B": "contentB"},
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_multi_async": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(data="contentA", prev=[], prev_may_be_missing=True),
        "B": DictDataWithPrev(data="contentB", prev=[], prev_may_be_missing=True),
    }

    # Run 2: same state β†’ memo valid, 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: state changes (100β†’101), B removed from items β†’ re-executes,
    # only A declared. B target state should be cleaned up.
    _multi_source_data["E1"] = MultiEntry(
        name="E1",
        version=1,
        state_value=101,
        items={"A": "contentA2"},
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_multi_async": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="contentA2", prev=["contentA"], prev_may_be_missing=False
        ),
    }

    # Run 4: no changes β†’ 0 calls (new state persisted correctly)
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# Component-level state validation (async)
# ============================================================================


@coco.fn.as_async(memo=True)
def _declare_entry_async(key: str, entry: Entry) -> None:
    _metrics.increment("call.declare_entry_async")
    coco.declare_target_state(
        GlobalDictTarget.target_state(key, f"comp_async: {entry.content}")
    )


@coco.fn
async def _declare_data_async() -> None:
    for key, value in _source_data.items():
        await _declare_entry_async(key, value)


def test_state_validation_component_async() -> None:
    GlobalDictTarget.store.clear()
    _source_data.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_validation_component_async", environment=coco_env
        ),
        _declare_data_async,
    )

    # Run 1: cache miss
    _source_data["A"] = Entry(name="A", version=1, state_value=100, content="contentA1")
    _source_data["B"] = Entry(name="B", version=1, state_value=200, content="contentB1")
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_entry_async": 2}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="comp_async: contentA1", prev=[], prev_may_be_missing=True
        ),
        "B": DictDataWithPrev(
            data="comp_async: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 2: unchanged β†’ no re-execution
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: A state changes β†’ A re-executes
    _source_data["A"] = Entry(name="A", version=1, state_value=101, content="contentA2")
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_entry_async": 1}
    assert GlobalDictTarget.store.data == {
        "A": DictDataWithPrev(
            data="comp_async: contentA2",
            prev=["comp_async: contentA1"],
            prev_may_be_missing=False,
        ),
        "B": DictDataWithPrev(
            data="comp_async: contentB1", prev=[], prev_may_be_missing=True
        ),
    }

    # Run 4: no changes β†’ 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# State changed but reusable (sync) β€” multi-level validation
# ============================================================================


@dataclass
class TwoLevelEntry:
    """Simulates a file with mtime + content fingerprint.

    The state is (mtime, fingerprint). On validation:
    - If mtime unchanged β†’ reusable immediately.
    - If mtime changed, compare fingerprints: if same β†’ reusable (state updated
      with new mtime), if different β†’ not reusable.
    """

    name: str
    mtime: int
    fingerprint: str
    content: str

    def __coco_memo_key__(self) -> object:
        return self.name

    def __coco_memo_state__(self, prev_state: Any) -> coco.MemoStateOutcome:
        new_state = (self.mtime, self.fingerprint)
        if coco.is_non_existence(prev_state):
            return coco.MemoStateOutcome(state=new_state, memo_valid=True)
        prev_mtime, prev_fp = prev_state
        if self.mtime == prev_mtime:
            # mtime unchanged β€” definitely reusable
            return coco.MemoStateOutcome(state=new_state, memo_valid=True)
        # mtime changed β€” check content fingerprint
        return coco.MemoStateOutcome(
            state=new_state, memo_valid=self.fingerprint == prev_fp
        )


_two_level_source: dict[str, TwoLevelEntry] = {}


@coco.fn(memo=True)
def _process_two_level(entry: TwoLevelEntry) -> str:
    _metrics.increment("call.process_two_level")
    return f"result: {entry.content}"


@coco.fn
def _run_two_level() -> None:
    for key, value in _two_level_source.items():
        result = _process_two_level(value)
        coco.declare_target_state(GlobalDictTarget.target_state(key, result))


def test_state_changed_but_reusable_sync() -> None:
    """Multi-level validation: mtime changes but content fingerprint is the same.
    Function should NOT re-execute, but new state (with updated mtime) should be persisted."""
    GlobalDictTarget.store.clear()
    _two_level_source.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_changed_but_reusable_sync", environment=coco_env
        ),
        _run_two_level,
    )

    # Run 1: cache miss β€” executes
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=1000, fingerprint="abc123", content="hello"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.process_two_level": 1}
    assert GlobalDictTarget.store.data == {
        "X": DictDataWithPrev(data="result: hello", prev=[], prev_may_be_missing=True),
    }

    # Run 2: same mtime β†’ reusable, 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: mtime changes (1000β†’2000) but fingerprint unchanged β†’ reusable, 0 calls
    # State changes from (1000, "abc123") to (2000, "abc123") β€” persisted but no re-execution.
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=2000, fingerprint="abc123", content="hello"
    )
    app.update_blocking()
    assert _metrics.collect() == {}  # NO re-execution!
    assert GlobalDictTarget.store.data == {
        "X": DictDataWithPrev(data="result: hello", prev=[], prev_may_be_missing=True),
    }

    # Run 4: same mtime (2000) β€” verifies the updated state was persisted
    # (if state wasn't persisted, the old mtime 1000 would be stored, and
    # comparing with 2000 would trigger a fingerprint check again)
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 5: mtime changes again AND fingerprint changes β†’ not reusable, re-executes
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=3000, fingerprint="def456", content="world"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.process_two_level": 1}
    assert GlobalDictTarget.store.data == {
        "X": DictDataWithPrev(
            data="result: world",
            prev=["result: hello"],
            prev_may_be_missing=False,
        ),
    }

    # Run 6: no changes β†’ 0 calls
    app.update_blocking()
    assert _metrics.collect() == {}


# ============================================================================
# State changed but reusable (async)
# ============================================================================


@coco.fn.as_async(memo=True)
def _process_two_level_async(entry: TwoLevelEntry) -> str:
    _metrics.increment("call.process_two_level_async")
    return f"result_async: {entry.content}"


@coco.fn
async def _run_two_level_async() -> None:
    for key, value in _two_level_source.items():
        result = await _process_two_level_async(value)
        coco.declare_target_state(GlobalDictTarget.target_state(key, result))


def test_state_changed_but_reusable_async() -> None:
    """Async variant: mtime changes but fingerprint same β†’ no re-execution."""
    GlobalDictTarget.store.clear()
    _two_level_source.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_changed_but_reusable_async", environment=coco_env
        ),
        _run_two_level_async,
    )

    # Run 1: cache miss β€” executes
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=1000, fingerprint="abc123", content="hello"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.process_two_level_async": 1}

    # Run 2: mtime changes but fingerprint same β†’ reusable, 0 calls
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=2000, fingerprint="abc123", content="hello"
    )
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: verify updated state persisted (same mtime 2000 β†’ 0 calls)
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 4: fingerprint changes β†’ re-executes
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=3000, fingerprint="def456", content="world"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.process_two_level_async": 1}


# ============================================================================
# State changed but reusable β€” component-level (sync)
# ============================================================================


@coco.fn(memo=True)
def _declare_two_level(entry: TwoLevelEntry) -> None:
    _metrics.increment("call.declare_two_level")
    coco.declare_target_state(
        GlobalDictTarget.target_state(entry.name, f"comp: {entry.content}")
    )


@coco.fn
def _run_two_level_comp() -> None:
    for value in _two_level_source.values():
        _declare_two_level(value)


def test_state_changed_but_reusable_component_sync() -> None:
    """Component-level: mtime changes but fingerprint same β†’ no re-execution."""
    GlobalDictTarget.store.clear()
    _two_level_source.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_state_changed_but_reusable_comp_sync", environment=coco_env
        ),
        _run_two_level_comp,
    )

    # Run 1: cache miss
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=1000, fingerprint="abc123", content="hello"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_two_level": 1}

    # Run 2: mtime changes but fingerprint same β†’ reusable
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=2000, fingerprint="abc123", content="hello"
    )
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 3: verify state persisted
    app.update_blocking()
    assert _metrics.collect() == {}

    # Run 4: fingerprint changes β†’ re-executes
    _two_level_source["X"] = TwoLevelEntry(
        name="X", mtime=3000, fingerprint="def456", content="world"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.declare_two_level": 1}


# ============================================================================
# State collected again after a re-run (`recollect_after_run`)
# ============================================================================

# What each key's function reads when it runs, and the version of each thing read.
_reads: dict[str, list[str]] = {}
_versions: dict[str, int] = {}


class _Tracked:
    """A key whose state is the set of things its function read, with versions.

    The set is only known once the function has run, so a re-run triggered by
    the state check must store the state collected after it.
    """

    def __init__(self, key: str) -> None:
        self.key = key
        self.observed: list[tuple[str, int]] = []

    def __coco_memo_key__(self) -> object:
        return self.key

    def __coco_memo_state__(
        self, prev_state: list[tuple[str, int]] | coco.NonExistenceType
    ) -> coco.MemoStateOutcome:
        if coco.is_non_existence(prev_state):
            return coco.MemoStateOutcome(state=list(self.observed))
        valid = all(_versions[name] == version for name, version in prev_state)
        return coco.MemoStateOutcome(
            state=prev_state, memo_valid=valid, recollect_after_run=True
        )


def _read_all(tracked: _Tracked) -> str:
    tracked.observed = [(name, _versions[name]) for name in _reads[tracked.key]]
    return ",".join(f"{name}@{version}" for name, version in tracked.observed)


@coco.fn(memo=True)
def _tracked_sync(tracked: _Tracked) -> str:
    _metrics.increment("call.tracked")
    return _read_all(tracked)


@coco.fn.as_async(memo=True)
def _tracked_async(tracked: _Tracked) -> str:
    _metrics.increment("call.tracked")
    return _read_all(tracked)


@coco.fn
def _process_tracked_sync() -> None:
    for key in _reads:
        coco.declare_target_state(
            GlobalDictTarget.target_state(key, _tracked_sync(_Tracked(key)))
        )


@coco.fn
async def _process_tracked_async() -> None:
    for key in _reads:
        value = await _tracked_async(_Tracked(key))
        coco.declare_target_state(GlobalDictTarget.target_state(key, value))


@pytest.mark.parametrize(
    ("name", "main"),
    [
        ("test_recollect_after_run_sync", _process_tracked_sync),
        ("test_recollect_after_run_async", _process_tracked_async),
    ],
)
def test_recollect_after_run(name: str, main: Any) -> None:
    GlobalDictTarget.store.clear()
    _metrics.clear()
    _reads.clear()
    _versions.clear()
    _reads["A"] = ["x"]
    _versions.update(x=1, y=1)

    app = coco.App(coco.AppConfig(name=name, environment=coco_env), main)

    app.update_blocking()
    assert _metrics.collect() == {"call.tracked": 1}
    assert GlobalDictTarget.store.data["A"].data == "x@1"

    # Nothing it read changed: reused.
    app.update_blocking()
    assert _metrics.collect() == {}

    # `x` changed, and the re-run reads `y` instead.
    _versions["x"] = 2
    _reads["A"] = ["y"]
    app.update_blocking()
    assert _metrics.collect() == {"call.tracked": 1}
    assert GlobalDictTarget.store.data["A"].data == "y@1"

    # The stored state is the one collected after the re-run, so a change to
    # `y` is seen. Without it the state would still name `x` and this run
    # would reuse a stale result.
    _versions["y"] = 2
    app.update_blocking()
    assert _metrics.collect() == {"call.tracked": 1}
    assert GlobalDictTarget.store.data["A"].data == "y@2"

    # A change to `x`, no longer read, does not re-run it.
    _versions["x"] = 3
    app.update_blocking()
    assert _metrics.collect() == {}


@coco.fn(memo=True)
def _tracked_component(tracked: _Tracked) -> None:
    coco.declare_target_state(
        GlobalDictTarget.target_state(tracked.key, _read_all(tracked))
    )


@coco.fn
async def _mount_tracked() -> None:
    for key in _reads:
        # `use_mount` so the child's failure reaches the caller.
        await coco.use_mount(
            coco.component_subpath(key), _tracked_component, _Tracked(key)
        )


def test_recollect_after_run_rejected_for_component() -> None:
    GlobalDictTarget.store.clear()
    _reads.clear()
    _versions.clear()
    _reads["A"] = ["x"]
    _versions["x"] = 1

    app = coco.App(
        coco.AppConfig(
            name="test_recollect_after_run_rejected_for_component",
            environment=coco_env,
        ),
        _mount_tracked,
    )
    app.update_blocking()

    # The state check rejects the memo and asks for a recollect, which a
    # memoized component cannot honor: it must fail, not store a stale state.
    _versions["x"] = 2
    with pytest.raises(Exception, match="recollect_after_run"):
        app.update_blocking()


# ============================================================================
# State changed, still reusable β†’ the refreshed entry keeps its target states
# and the entries it depends on across the commit
# ============================================================================


@dataclass
class RefreshEntry:
    """Key stays put; the state moves on every run but stays valid."""

    name: str
    state_value: int
    content: str

    def __coco_memo_key__(self) -> object:
        return self.name

    def __coco_memo_state__(self, prev_state: Any) -> coco.MemoStateOutcome:
        return coco.MemoStateOutcome(
            state=self.state_value, memo_valid=not coco.is_non_existence(prev_state)
        )


_refresh_data: dict[str, RefreshEntry] = {}
_refresh_cfg = {"salt": 0}


@coco.fn(memo=True)
def _declare_refresh_inner(entry: RefreshEntry) -> None:
    _metrics.increment("call.refresh_inner")
    coco.declare_target_state(
        GlobalDictTarget.target_state(f"{entry.name}/inner", f"inner: {entry.content}")
    )


@coco.fn(memo=True)
def _declare_refresh_outer(entry: RefreshEntry, salt: int) -> None:
    _metrics.increment("call.refresh_outer")
    coco.declare_target_state(
        GlobalDictTarget.target_state(entry.name, f"outer: {entry.content}")
    )
    _declare_refresh_inner(entry)


@coco.fn
def _declare_refresh_data() -> None:
    for value in _refresh_data.values():
        _declare_refresh_outer(value, _refresh_cfg["salt"])


def test_state_refresh_keeps_targets_and_nested_entries() -> None:
    GlobalDictTarget.store.clear()
    _refresh_data.clear()
    _metrics.clear()
    _refresh_cfg["salt"] = 0

    app = coco.App(
        coco.AppConfig(
            name="test_state_refresh_keeps_targets_and_nested_entries",
            environment=coco_env,
        ),
        _declare_refresh_data,
    )

    # Run 1: cache miss β€” outer and inner execute and declare their targets.
    _refresh_data["A"] = RefreshEntry(name="A", state_value=1, content="c1")
    app.update_blocking()
    assert _metrics.collect() == {"call.refresh_outer": 1, "call.refresh_inner": 1}
    expected = {
        "A": DictDataWithPrev(data="outer: c1", prev=[], prev_may_be_missing=True),
        "A/inner": DictDataWithPrev(
            data="inner: c1", prev=[], prev_may_be_missing=True
        ),
    }
    assert GlobalDictTarget.store.data == expected

    # Run 2: the state moved (1 β†’ 2) but stays valid, so outer's entry is
    # refreshed without running. Its target states, and inner's, must survive
    # the commit: the refreshed entry still carries them over.
    _refresh_data["A"] = RefreshEntry(name="A", state_value=2, content="c1")
    app.update_blocking()
    assert _metrics.collect() == {}
    assert GlobalDictTarget.store.data == expected

    # Run 3: outer's key moves (salt) so its body runs and probes inner. The
    # nested entry survived run 2's flush, so inner hits (state 2 β†’ 3, valid).
    _refresh_cfg["salt"] = 1
    _refresh_data["A"] = RefreshEntry(name="A", state_value=3, content="c1")
    app.update_blocking()
    assert _metrics.collect() == {"call.refresh_outer": 1}
    assert {k: v.data for k, v in GlobalDictTarget.store.data.items()} == {
        "A": "outer: c1",
        "A/inner": "inner: c1",
    }


# ============================================================================
# Raw class-object metaclass memo methods are ignored (sync)
# ============================================================================

_class_object_source: dict[str, type] = {}
_class_object_prev_states: list[Any] = []


class _ClassObjectMemoMeta(type):
    def __coco_memo_key__(cls) -> object:
        raise AssertionError("class objects must not call metaclass memo key")

    def __coco_memo_state__(cls, prev_state: Any) -> coco.MemoStateOutcome:
        _class_object_prev_states.append(prev_state)
        raise AssertionError("class objects must not call metaclass memo state")


def _make_class_object(
    stable_key: str,
    state_value: int,
    content: str,
    stable_type_id: str | None = None,
) -> type:
    attrs: dict[str, object] = {
        "stable_key": stable_key,
        "state_value": state_value,
        "content": content,
    }
    if stable_type_id is not None:
        attrs["__coco_memo_type_id__"] = stable_type_id
    return _ClassObjectMemoMeta(
        f"ClassObject_{stable_key}_{state_value}_{content}",
        (),
        attrs,
    )


@coco.fn(memo=True)
def _transform_class_object(cls: type) -> str:
    _metrics.increment("call.transform_class_object")
    return f"class content: {getattr(cls, 'content')}"


@coco.fn
def _process_class_objects() -> None:
    for key, cls in _class_object_source.items():
        result = _transform_class_object(cls)
        coco.declare_target_state(GlobalDictTarget.target_state(key, result))


def test_class_object_metaclass_memo_methods_are_ignored_sync() -> None:
    """Class objects use stable type IDs and never call metaclass memo methods."""
    GlobalDictTarget.store.clear()
    _class_object_source.clear()
    _class_object_prev_states.clear()
    _metrics.clear()

    app = coco.App(
        coco.AppConfig(
            name="test_class_object_metaclass_memo_methods_are_ignored_sync",
            environment=coco_env,
        ),
        _process_class_objects,
    )

    stable_type_id = "test.ClassObjectMemo/v1"
    cls = _make_class_object("stable", 1, "A", stable_type_id)

    # Run 1: the transform executes and no metaclass memo methods are called.
    _class_object_source["row"] = cls
    app.update_blocking()
    assert _metrics.collect() == {"call.transform_class_object": 1}
    assert _class_object_prev_states == []
    assert GlobalDictTarget.store.data == {
        "row": DictDataWithPrev(
            data="class content: A", prev=[], prev_may_be_missing=True
        )
    }

    # Run 2: a distinct class object with the same stable type ID is a cache hit.
    # Class schema/state changes are intentionally not part of the class-object
    # fingerprint; users who depend on them must include them with `memo_key=`.
    _class_object_source["row"] = _make_class_object("renamed", 2, "B", stable_type_id)
    app.update_blocking()
    assert _metrics.collect() == {}
    assert _class_object_prev_states == []
    assert GlobalDictTarget.store.data["row"].data == "class content: A"

    # Run 3: a distinct stable type ID changes the fingerprint, so the transform
    # re-executes and the previous target state is made available.
    _class_object_source["row"] = _make_class_object(
        "changed", 3, "C", "test.ClassObjectMemo/v2"
    )
    app.update_blocking()
    assert _metrics.collect() == {"call.transform_class_object": 1}
    assert _class_object_prev_states == []
    assert GlobalDictTarget.store.data == {
        "row": DictDataWithPrev(
            data="class content: C",
            prev=["class content: A"],
            prev_may_be_missing=False,
        )
    }


def test_class_object_registered_metaclass_memo_state_sync() -> None:
    GlobalDictTarget.store.clear()
    _class_object_source.clear()
    _class_object_prev_states.clear()
    _metrics.clear()

    def class_object_key(cls: Any) -> object:
        return cls.stable_key

    def class_object_state(cls: Any, prev_state: Any) -> coco.MemoStateOutcome:
        return coco.MemoStateOutcome(
            state=cls.state_value,
            memo_valid=(
                not coco.is_non_existence(prev_state) and cls.state_value == prev_state
            ),
        )

    coco.register_memo_key_function(
        _ClassObjectMemoMeta,
        class_object_key,
        state_fn=class_object_state,
    )
    try:
        app = coco.App(
            coco.AppConfig(
                name="test_class_object_registered_metaclass_memo_state_sync",
                environment=coco_env,
            ),
            _process_class_objects,
        )

        _class_object_source["row"] = _make_class_object("row", 1, "A")
        app.update_blocking()
        assert _metrics.collect() == {"call.transform_class_object": 1}
        assert GlobalDictTarget.store.data["row"].data == "class content: A"

        _class_object_source["row"] = _make_class_object("row", 1, "B")
        app.update_blocking()
        assert _metrics.collect() == {}
        assert GlobalDictTarget.store.data["row"].data == "class content: A"

        _class_object_source["row"] = _make_class_object("row", 2, "C")
        app.update_blocking()
        assert _metrics.collect() == {"call.transform_class_object": 1}
        assert GlobalDictTarget.store.data["row"].data == "class content: C"
        assert _class_object_prev_states == []
    finally:
        unregister_memo_type(_ClassObjectMemoMeta)