Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(reasoning): tighten checkpoint validation invariants

江俊杰13e90b5044c7
diff --git a/docs/reference/truth_maintenance.md b/docs/reference/truth_maintenance.md
index 8d05fb41..1d38c6f4 100644
--- a/docs/reference/truth_maintenance.md
+++ b/docs/reference/truth_maintenance.md
@@ -169,10 +169,12 @@ The fixed v1 schema is:
The top level and each rule/catalog record have exact field sets; missing and
unknown fields are rejected. `format_version` must be integer `1`;
-`session_version` must be a non-negative integer, and a non-empty support
-catalog requires a positive version because it represents at least one
-committed update. Rules and catalog facts are semantic values, so invalid
-atoms, variables in facts, recursive rules, and arity conflicts are rejected
+`session_version` must be a non-negative integer. A non-empty support catalog
+requires a positive version because it represents at least one committed
+update; an empty catalog requires version zero, and a non-empty catalog with no
+active supports requires at least two commits. Rules and catalog facts are
+semantic values, so invalid atoms, variables in facts, recursive rules, and
+arity conflicts are rejected
with the same `ValidationError` used by construction and `apply()`.
Unsupported future formats are also rejected, and restore never returns a
partially reconstructed session.
diff --git a/semantica/reasoning/_truth_maintenance_checkpoint.py b/semantica/reasoning/_truth_maintenance_checkpoint.py
index dd0ed09f..f31112b4 100644
--- a/semantica/reasoning/_truth_maintenance_checkpoint.py
+++ b/semantica/reasoning/_truth_maintenance_checkpoint.py
@@ -50,6 +50,13 @@ def _fail(message: str, field: str, **details: object) -> ValidationError:
def _require_exact_keys(record: dict, expected: FrozenSet[str], location: str) -> None:
actual = set(record)
+ non_string_keys = [key for key in actual if not isinstance(key, str)]
+ if non_string_keys:
+ raise _fail(
+ "checkpoint field names must be strings",
+ location or "checkpoint",
+ key_types=sorted({type(key).__name__ for key in non_string_keys}),
+ )
missing = expected - actual
unknown = actual - expected
if not missing and not unknown:
@@ -256,6 +263,22 @@ def decode_checkpoint(payload: object) -> _DecodedCheckpoint:
frozenset(support.support_id for support in catalog_supports),
)
+ # An empty catalog cannot follow an effective commit. Withdrawing every
+ # support requires at least an initial insertion and a final retraction.
+ if not catalog_supports and session_version > 0:
+ raise _fail(
+ "session_version cannot be positive when the support catalog is empty",
+ "session_version",
+ session_version=session_version,
+ )
+ if catalog_supports and not active_support_ids and session_version < 2:
+ raise _fail(
+ "session_version must be at least two when no supports are active",
+ "session_version",
+ session_version=session_version,
+ catalog_size=len(catalog_supports),
+ )
+
return _DecodedCheckpoint(
session_version=session_version,
rules=rules,
diff --git a/tests/reasoning/test_truth_maintenance_checkpoint.py b/tests/reasoning/test_truth_maintenance_checkpoint.py
index 2fbef913..f6b629fc 100644
--- a/tests/reasoning/test_truth_maintenance_checkpoint.py
+++ b/tests/reasoning/test_truth_maintenance_checkpoint.py
@@ -681,6 +681,18 @@ def test_rule_only_predicate_arity_survives_restore_without_catalog_facts():
active=["active"],
session_version=1,
),
+ checkpoint_payload(
+ rules=[],
+ catalog={},
+ active=[],
+ session_version=1,
+ ),
+ checkpoint_payload(
+ rules=[],
+ catalog={"source": "A(a)"},
+ active=[],
+ session_version=1,
+ ),
],
ids=[
"invalid-condition",
@@ -692,6 +704,8 @@ def test_rule_only_predicate_arity_survives_restore_without_catalog_facts():
"indirect-cycle",
"rule-arity-conflict",
"inactive-fact-arity-conflict",
+ "positive-version-empty-catalog",
+ "single-commit-all-supports-inactive",
],
)
def test_semantic_rejection_leaves_payload_unchanged(payload):
@@ -701,6 +715,23 @@ def test_semantic_rejection_leaves_payload_unchanged(payload):
assert payload == before
+ "record",
+ [
+ lambda payload: payload,
+ lambda payload: payload["rules"][0],
+ lambda payload: payload["support_catalog"][0],
+ ],
+ ids=["top-level", "rule-record", "catalog-record"],
+)
+def test_non_string_field_names_raise_validation_error(record):
+ payload = json.loads(CHECKPOINT_V1_JSON)
+ record(payload)[42] = "invalid"
+
+ with pytest.raises(ValidationError):
+ TruthMaintenanceSession.from_checkpoint(payload)
+
+
def test_export_uses_captured_rules_not_caller_rule_mutation():
caller_rule = Rule("captured", "Captured", ["A(?x)"], "B(?x)")
session = TruthMaintenanceSession(rules=[caller_rule])
diff --git a/tests/reasoning/test_truth_maintenance_checkpoint_validation.py b/tests/reasoning/test_truth_maintenance_checkpoint_validation.py
index 4b784bd2..949678d8 100644
--- a/tests/reasoning/test_truth_maintenance_checkpoint_validation.py
+++ b/tests/reasoning/test_truth_maintenance_checkpoint_validation.py
@@ -113,6 +113,22 @@ def test_reject_unknown_top_level_field():
decode_checkpoint(payload)
+ "record",
+ [
+ lambda payload: payload,
+ lambda payload: payload["rules"][0],
+ lambda payload: payload["support_catalog"][0],
+ ],
+ ids=["top-level", "rule-record", "catalog-record"],
+)
+def test_reject_non_string_field_names(record):
+ payload = checkpoint_payload()
+ record(payload)[42] = "invalid"
+ with pytest.raises(ValidationError):
+ decode_checkpoint(payload)
+
+
@pytest.mark.parametrize(
"value",
[0, 2, True, 1.0, "1", None],
@@ -144,6 +160,22 @@ def test_reject_version_zero_with_non_empty_catalog():
decode_checkpoint(payload)
+def test_reject_positive_version_with_empty_catalog():
+ payload = checkpoint_payload()
+ payload["support_catalog"] = []
+ payload["active_support_ids"] = []
+ with pytest.raises(ValidationError):
+ decode_checkpoint(payload)
+
+
+def test_reject_single_commit_with_all_supports_inactive():
+ payload = checkpoint_payload()
+ payload["session_version"] = 1
+ payload["active_support_ids"] = []
+ with pytest.raises(ValidationError):
+ decode_checkpoint(payload)
+
+
@pytest.mark.parametrize(
"field,value",
[