import json import pytest from dx_productivity.dsh.decision_log import DecisionLog def _row(incident='a' * 24, outcome='pending_issue'): return {'incident_id': incident, 'trigger': 'execution_failed', 'evidence_refs': ['dsh/incidents/x/gates.json'], 'gates': {'execution_authorized': True, 'retry_budget_available': False}, 'candidate_action': 'none', 'authorized_scope': {'task_ids': ['task-1']}, 'outcome': outcome, 'human_takeover': False} def test_decision_log_is_hash_linked_and_survives_restart(tmp_path): store = DecisionLog(tmp_path) first = store.append(_row()) second = DecisionLog(tmp_path).append(_row('b' * 24, 'resolved')) assert first['sequence'] == 1 assert second['previous_hash'] == first['record_hash'] assert DecisionLog(tmp_path).verify()['valid'] is True assert [row['sequence'] for row in DecisionLog(tmp_path).list()] == [1, 2] def test_decision_log_detects_tampering_and_bounds_text(tmp_path): store = DecisionLog(tmp_path) store.append(_row()) path = tmp_path / 'dsh' / 'decisions.jsonl' raw = path.read_text(encoding='utf8').replace('execution_failed', 'x' * 5000) path.write_text(raw, encoding='utf8') assert DecisionLog(tmp_path).verify()['valid'] is False with pytest.raises(ValueError, match='incident_id'): store.append(_row('bad'))