""" spans never reach a saved transcript, even when a message splits its text across several content blocks.""" import importlib.util import json import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] SPEC = importlib.util.spec_from_file_location( "save_conversation", str(ROOT / ".agents/skills/ckb-add-conversation/scripts/save_conversation.py")) sc = importlib.util.module_from_spec(SPEC) SPEC.loader.exec_module(sc) def entry(*texts): return {"type": "user", "message": {"role": "user", "content": [{"type": "text", "text": t} for t in texts]}, "uuid": "u-1", "timestamp": "2026-11-11T10:00:00Z"} class Redaction(unittest.TestCase): def test_unclosed_tag_redacts_the_rest_of_the_message(self): out = json.dumps(sc.redact(entry("key: sk-1", "23456 ok", "later"))) self.assertNotIn("sk-1", out) self.assertNotIn("23456", out) self.assertIn(" ok", out) self.assertIn("later", out) def test_unclosed_tag_never_closed_redacts_every_later_block(self): out = sc.redact(entry("a secret", "more secret", "still")) texts = [b["text"] for b in out["message"]["content"]] self.assertEqual(texts[1:], ["[redacted]", "[redacted]"]) self.assertEqual(out["uuid"], "u-1") # metadata is untouched self.assertEqual(out["timestamp"], "2026-11-11T10:00:00Z") def test_jsonl_line_round_trip(self): line = json.dumps(entry("x top", "secret y")).encode() got = sc.redact_jsonl(line + b"\n" + b'{"type":"system"}') self.assertNotIn(b"secret", got) self.assertTrue(got.endswith(b'{"type":"system"}')) def test_closed_spans_in_one_block(self): self.assertEqual(sc.redact("a b c d"), f"a {sc.PRIVATE_MARK} c {sc.PRIVATE_MARK}") if __name__ == "__main__": unittest.main()