From d342fe8131b2cde019d1b95444057605db1d0bdf Mon Sep 17 00:00:00 2001 From: whning Date: Sat, 31 Oct 2026 20:20:55 +0800 Subject: [PATCH] Validate JSONL records before filtering session events --- codexSessionAudit/codex_session_audit.py | 4 ++-- codexSessionAudit/tests/test_codex_session_audit.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/codexSessionAudit/codex_session_audit.py b/codexSessionAudit/codex_session_audit.py index b9918c5..c326361 100644 --- a/codexSessionAudit/codex_session_audit.py +++ b/codexSessionAudit/codex_session_audit.py @@ -217,8 +217,6 @@ def audit_source(path: Path, label: str, result: AuditResult) -> SourceStats: with handle: for line in handle: stats.records += 1 - if '"response_item"' not in line and '"session_meta"' not in line: - continue try: record = json.loads(line) except json.JSONDecodeError: @@ -227,6 +225,8 @@ def audit_source(path: Path, label: str, result: AuditResult) -> SourceStats: if not isinstance(record, dict): stats.malformed_records += 1 continue + if record.get("type") not in {"response_item", "session_meta"}: + continue update_timestamp(result, record.get("timestamp")) payload = record.get("payload") if not isinstance(payload, dict): diff --git a/codexSessionAudit/tests/test_codex_session_audit.py b/codexSessionAudit/tests/test_codex_session_audit.py index f982369..c965579 100644 --- a/codexSessionAudit/tests/test_codex_session_audit.py +++ b/codexSessionAudit/tests/test_codex_session_audit.py @@ -198,3 +198,14 @@ def test_invalid_timestamp_does_not_leak_into_report(tmp_path: Path) -> None: assert result["summary"]["first_timestamp"] == "2026-10-31T00:00:00Z" assert result["summary"]["last_timestamp"] == "2026-10-31T00:00:00Z" assert "sensitive-invalid-timestamp" not in json.dumps(result) + + +def test_all_jsonl_records_are_validated_before_type_filter(tmp_path: Path) -> None: + session = tmp_path / "sessions" / "one.jsonl" + session.parent.mkdir(parents=True) + other = {"type": "event_msg", "payload": {"type": "message", "role": "user", "text": "response_item"}} + session.write_text("{broken json\n" + json.dumps(other) + "\n", encoding="utf-8") + result = audit_module.audit([tmp_path]).to_dict() + assert result["summary"]["records"] == 2 + assert result["summary"]["malformed_records"] == 1 + assert result["summary"]["user_messages"] == 0