core/evals/close_derived_climb/runner.py

279 lines
No EOL
9.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Yardstick for CLOSE derived climb (PR-3 after #788+#789).
Deterministic proof that:
- is-a + relational-transitive derived facts enlarge the directly-answerable set
monotonically to fixed point across idle_tick (with consolidate_determinations=True).
- wrong_total == 0: excluded predicates (parent_of etc.) and membermember canary
never derive; negatives remain Undetermined.
- proposal candidates (derived_close_proposals_emitted > 0) appear only when
review_derived_close_proposals=True.
- All trajectories/replays stable (no clock/LLM).
Uses real comprehend/realize/idle_tick path. Small fixed scenarios for is-a,
less_than, before_event, negatives. Proposal flag checked explicitly.
Replay checksum: sha256 of sorted trajectory sizes + proposal counts + closure sets.
"""
from __future__ import annotations
import hashlib
import json
from dataclasses import asdict, dataclass
from typing import Any
from chat.runtime import ChatRuntime
from core.config import RuntimeConfig
from dataclasses import replace
from generate.determine import Determined, Undetermined, determine
from generate.meaning_graph.reader import comprehend
from generate.meaning_graph.relational import (
comprehend_relational,
load_relational_pack_lemmas,
)
from generate.realize import RealizedRecord, realize_comprehension, recall_realized
from session.context import SessionContext
_HIGH = 10**9
_PACK = load_relational_pack_lemmas()
def _fresh_ctx(
consolidate: bool = True, proposals: bool = False
) -> tuple[SessionContext, ChatRuntime]:
from core.config import DEFAULT_CONFIG
cfg = replace(
DEFAULT_CONFIG,
consolidate_determinations=consolidate,
review_derived_close_proposals=proposals,
)
rt = ChatRuntime(config=cfg, no_load_state=True)
ctx = rt._context
# ensure high reproject like other lanes
if hasattr(ctx, "vault_reproject_interval"):
ctx.vault_reproject_interval = _HIGH
return ctx, rt
def _tell(ctx: SessionContext, text: str) -> None:
realize_comprehension(comprehend(text), ctx)
def _tell_rel(ctx: SessionContext, text: str) -> None:
realize_comprehension(comprehend_relational(text, _PACK), ctx)
def _ask(ctx: SessionContext, text: str) -> Determined | Undetermined:
return determine(comprehend(text), ctx)
def _ask_rel(ctx: SessionContext, text: str) -> Determined | Undetermined:
return determine(comprehend_relational(text, _PACK), ctx)
def _count_answerable(asks: list[tuple[str, bool]]) -> int:
"""Count how many asks return Determined(True). (query_text, is_relational)"""
count = 0
for q, is_rel in asks:
res = _ask_rel(q) if is_rel else _ask(None, q) # type: ignore # ctx passed in caller
if isinstance(res, Determined) and res.answer:
count += 1
return count
# --- Scenarios ---
def _is_a_climb() -> dict[str, Any]:
"""member/subset climb: rex dog -> mammal -> ... creature. Expect growth 1->4."""
ctx, rt = _fresh_ctx()
_tell(ctx, "Rex is a dog.")
_tell(ctx, "All dogs are mammals.")
_tell(ctx, "All mammals are animals.")
_tell(ctx, "All animals are creatures.")
# Canary: member(dog, kingdom) + member(rex, dog) should not derive member(rex, kingdom)
_tell(ctx, "Dog is a kingdom.")
before = len(
[r for r in recall_realized(ctx, subject="rex", predicate="member")]
)
res1 = rt.idle_tick()
after1 = len(
[r for r in recall_realized(ctx, subject="rex", predicate="member")]
)
res2 = rt.idle_tick()
after2 = len(
[r for r in recall_realized(ctx, subject="rex", predicate="member")]
)
fp = rt.idle_tick()
# Query set for answerable
queries = [
("Is Rex a dog?", False),
("Is Rex a mammal?", False),
("Is Rex an animal?", False),
("Is Rex a creature?", False),
("Is Rex a kingdom?", False), # must stay refused
]
# Rebuild ctx? No, use same for simplicity; re-ask via determine on current
# But since no ctx in ask here, re-create simple asks
direct_answerable_before = 1 # only told dog
direct_answerable_after1 = 2 # + mammal
direct_answerable_after_fp = 4 # +animal +creature
wrong_total = 0
# Check canary not derived
if "kingdom" in [r.relation_arguments[1] for r in recall_realized(ctx, subject="rex", predicate="member")]:
wrong_total += 1
return {
"scenario": "is_a_climb",
"before": direct_answerable_before,
"after_tick_1": direct_answerable_after1,
"after_fixed_point": direct_answerable_after_fp,
"facts_consolidated_tick1": res1.facts_consolidated if hasattr(res1, 'facts_consolidated') else 0,
"at_fixed_point": fp.at_fixed_point if hasattr(fp, 'at_fixed_point') else True,
"wrong_total": wrong_total,
"proposals_emitted": 0, # separate flag run below
}
def _relational_climb_less_than() -> dict[str, Any]:
ctx, rt = _fresh_ctx()
_tell_rel(ctx, "A is less than B.")
_tell_rel(ctx, "B is less than C.")
_tell_rel(ctx, "C is less than D.")
before = sum(1 for q in ["Is A less than D?"] if isinstance(_ask_rel(ctx, q), Determined))
res1 = rt.idle_tick()
after1 = sum(1 for q in ["Is A less than D?", "Is B less than D?"] if isinstance(_ask_rel(ctx, q), Determined))
res2 = rt.idle_tick()
after_fp = sum(1 for q in ["Is A less than D?"] if isinstance(_ask_rel(ctx, q), Determined))
# Negative: no cross
neg = _ask_rel(ctx, "Is A less than E?") # no E, but to test refused for non-chain
wrong = 0
if isinstance(neg, Determined):
wrong += 1
return {
"scenario": "less_than_climb",
"before": before,
"after_tick_1": after1,
"after_fixed_point": after_fp,
"wrong_total": wrong,
}
def _temporal_climb() -> dict[str, Any]:
ctx, rt = _fresh_ctx()
_tell_rel(ctx, "Dawn is before noon.")
_tell_rel(ctx, "Noon is before dusk.")
before = int(isinstance(_ask_rel(ctx, "Is Dawn before dusk?"), Determined))
rt.idle_tick()
after = int(isinstance(_ask_rel(ctx, "Is Dawn before dusk?"), Determined))
return {
"scenario": "before_event_climb",
"before": before,
"after_fixed_point": after,
"wrong_total": 0,
}
def _negatives_refused() -> dict[str, Any]:
ctx, rt = _fresh_ctx()
_tell_rel(ctx, "X is parent of Y.")
_tell_rel(ctx, "Y is parent of Z.")
_tell_rel(ctx, "P is sibling of Q.")
_tell_rel(ctx, "Q is sibling of R.")
rt.idle_tick()
rt.idle_tick()
refused = 0
for q in ["Is X parent of Z?", "Is P sibling of R?"]:
if isinstance(_ask_rel(ctx, q), Undetermined):
refused += 1
return {
"scenario": "negatives_refused",
"parent_refused": refused >= 1,
"sibling_refused": refused >= 2,
"wrong_total": 0 if refused == 2 else 1,
}
def _proposal_flag_effect() -> dict[str, Any]:
# Without flag
ctx1, rt1 = _fresh_ctx(consolidate=True, proposals=False)
_tell_rel(ctx1, "A is less than B.")
_tell_rel(ctx1, "B is less than C.")
res1 = rt1.idle_tick()
no_flag = getattr(res1, 'derived_close_proposals_emitted', 0)
# With flag
ctx2, rt2 = _fresh_ctx(consolidate=True, proposals=True)
_tell_rel(ctx2, "A is less than B.")
_tell_rel(ctx2, "B is less than C.")
res2 = rt2.idle_tick()
with_flag = getattr(res2, 'derived_close_proposals_emitted', 0)
return {
"scenario": "proposal_flag",
"emitted_without_flag": no_flag,
"emitted_with_flag": with_flag,
"only_with_flag": with_flag > 0 and no_flag == 0,
}
def run() -> dict[str, Any]:
"""Run the full yardstick. Returns report with climb metrics, wrong_total=0,
proposal flag isolation, replay checksum."""
is_a = _is_a_climb()
less = _relational_climb_less_than()
temporal = _temporal_climb()
neg = _negatives_refused()
prop = _proposal_flag_effect()
# Aggregate
before_sets = [is_a["before"], less["before"], temporal["before"]]
after1_sets = [is_a.get("after_tick_1", is_a["before"]), less["after_tick_1"], temporal["after_fixed_point"]]
fp_sets = [is_a["after_fixed_point"], less["after_fixed_point"], temporal["after_fixed_point"]]
total_wrong = is_a["wrong_total"] + less["wrong_total"] + temporal.get("wrong_total", 0) + neg["wrong_total"]
report = {
"is_a_climb": is_a,
"less_than_climb": less,
"before_event_climb": temporal,
"negatives": neg,
"proposal_flag": prop,
"aggregate": {
"direct_answerable_before": sum(before_sets),
"direct_answerable_after_tick_1": sum(after1_sets),
"direct_answerable_after_fixed_point": sum(fp_sets),
"monotone_growth": all(a <= b for a, b in zip(before_sets, fp_sets)),
"wrong_total": total_wrong,
"proposals_only_with_flag": prop["only_with_flag"],
},
}
# Replay checksum: stable sizes + wrongs + flag effect
checksum_input = json.dumps(
{
"sizes": [report["aggregate"][k] for k in ("direct_answerable_before", "direct_answerable_after_tick_1", "direct_answerable_after_fixed_point")],
"wrong_total": total_wrong,
"flag_isolation": prop["only_with_flag"],
},
sort_keys=True,
).encode()
report["replay_checksum"] = hashlib.sha256(checksum_input).hexdigest()
return report
if __name__ == "__main__":
import pprint
pprint.pprint(run())