mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
Log immutable trace lookup failures before fallback
This commit is contained in:
@@ -416,7 +416,11 @@ def _append_immutable_trace_events(
|
||||
previous_trace_id = latest_map.get("trace_id")
|
||||
previous_hash = latest_map.get("event_hash", "") or ""
|
||||
next_index = int(latest_map.get("event_index", 0) or 0) + 1
|
||||
except Exception:
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"Failed to lookup previous immutable trace event; starting new chain "
|
||||
f"for decision_id={decision_id}: {e}"
|
||||
)
|
||||
# Start a fresh chain if previous trace lookup fails.
|
||||
previous_trace_id = None
|
||||
previous_hash = ""
|
||||
|
||||
@@ -4,7 +4,10 @@ from datetime import datetime
|
||||
import logging
|
||||
from unittest.mock import Mock
|
||||
|
||||
from semantica.context.decision_methods import capture_decision_trace
|
||||
from semantica.context.decision_methods import (
|
||||
_append_immutable_trace_events,
|
||||
capture_decision_trace,
|
||||
)
|
||||
from semantica.context.decision_models import Decision
|
||||
|
||||
|
||||
@@ -112,3 +115,28 @@ def test_capture_decision_trace_accepts_versioned_policy_refs():
|
||||
]
|
||||
assert policy_calls
|
||||
assert policy_calls[0][0][1]["policy_version"] == "3.2"
|
||||
|
||||
|
||||
def test_append_immutable_trace_events_logs_lookup_failure_and_continues(caplog):
|
||||
graph_store = Mock()
|
||||
calls = {"n": 0}
|
||||
|
||||
def _execute_query(*args, **kwargs):
|
||||
calls["n"] += 1
|
||||
if calls["n"] == 1:
|
||||
raise RuntimeError("lookup failed")
|
||||
return {"records": []}
|
||||
|
||||
graph_store.execute_query = Mock(side_effect=_execute_query)
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
_append_immutable_trace_events(
|
||||
graph_store=graph_store,
|
||||
decision_id="decision_trace_test_001",
|
||||
events=[{"event_type": "DECISION_RECORDED", "payload": {"ok": True}}],
|
||||
logger=logging.getLogger("test_logger"),
|
||||
)
|
||||
|
||||
assert "Failed to lookup previous immutable trace event" in caplog.text
|
||||
assert "decision_id=decision_trace_test_001" in caplog.text
|
||||
assert graph_store.execute_query.call_count >= 2
|
||||
|
||||
Reference in New Issue
Block a user