import json from concurrent.futures import ThreadPoolExecutor from hashlib import sha256 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')) def _seal(record): body = {key: value for key, value in record.items() if key != 'record_hash'} payload = json.dumps(body, ensure_ascii=False, sort_keys=True, separators=(',', ':')) return body | {'record_hash': sha256(payload.encode('utf8')).hexdigest()} @pytest.fixture def long_log(tmp_path): # Seed a valid historical log without thousands of quadratic append calls. path = tmp_path / 'dsh' / 'decisions.jsonl' path.parent.mkdir() previous = '' with path.open('w', encoding='utf8') as stream: for sequence in range(1, 2002): last = _seal(_row() | {'sequence': sequence, 'at_utc': '2026-10-31T00:00:00+00:00', 'previous_hash': previous}) stream.write(json.dumps(last) + '\n') previous = last['record_hash'] return DecisionLog(tmp_path), last def test_append_after_2000_records_uses_actual_tail_and_verifies_entire_chain(long_log): store, previous = long_log result = store.append(_row('b' * 24)) assert result['sequence'] == 2002 assert result['previous_hash'] == previous['record_hash'] reopened = DecisionLog(store.path.parent.parent) assert reopened.verify() == {'valid': True, 'checked': 2002, 'last_hash': result['record_hash']} def test_verify_detects_tampering_past_2000_records(long_log): store, last = long_log lines = store.path.read_text(encoding='utf8').splitlines() lines[-1] = json.dumps(last | {'outcome': 'resolved'}) store.path.write_text('\n'.join(lines) + '\n', encoding='utf8') assert store.verify(sequence=2000)['valid'] is True result = store.verify() assert result['valid'] is False and result['checked'] == 2000 before = store.path.read_bytes() with pytest.raises(ValueError, match='decision_log_corrupt'): store.append(_row('b' * 24)) assert store.path.read_bytes() == before def test_verify_rejects_duplicate_sequence_even_with_valid_hash_links(long_log): store, last = long_log duplicate = _seal(last | {'previous_hash': last['record_hash']}) with store.path.open('a', encoding='utf8') as stream: stream.write(json.dumps(duplicate) + '\n') result = store.verify() assert result['valid'] is False and result['checked'] == 2001 @pytest.mark.parametrize('tail', ['{incomplete', '{"incident_id":', '[]\n', '\n']) def test_corrupt_tail_is_reported_and_append_preserves_original_bytes(tmp_path, tail): store = DecisionLog(tmp_path) store.append(_row()) with store.path.open('a', encoding='utf8') as stream: stream.write(tail) before = store.path.read_bytes() assert store.verify()['valid'] is False with pytest.raises(ValueError, match='decision_log_corrupt'): store.list() with pytest.raises(ValueError, match='decision_log_corrupt'): store.append(_row('b' * 24)) assert store.path.read_bytes() == before def test_listing_remains_bounded_and_shows_recent_records(long_log): store, _ = long_log assert [row['sequence'] for row in store.list(limit=3)] == [1999, 2000, 2001] assert len(store.list(limit=100000)) == 2000 def test_append_after_complete_json_without_newline_keeps_records_separate(tmp_path): store = DecisionLog(tmp_path) first = store.append(_row()) store.path.write_bytes(store.path.read_bytes().rstrip(b'\r\n')) second = store.append(_row('b' * 24)) assert len(store.path.read_text(encoding='utf8').splitlines()) == 2 assert second['previous_hash'] == first['record_hash'] assert store.verify()['checked'] == 2 and store.verify()['valid'] is True def test_concurrent_instances_append_one_ordered_chain(tmp_path): def append(index): return DecisionLog(tmp_path).append(_row(f'{index:024x}')) with ThreadPoolExecutor(max_workers=4) as pool: results = list(pool.map(append, range(20))) assert sorted(row['sequence'] for row in results) == list(range(1, 21)) assert DecisionLog(tmp_path).verify()['valid'] is True assert DecisionLog(tmp_path).verify()['checked'] == 20