File size: 21,797 Bytes
038e086
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
#!/usr/bin/env python3
"""
Comprehensive data cleanup for Arcspan cybersecurity NER.
Fixes all P0/P1 issues from the audit. Idempotent β€” safe to run multiple times.

Usage: python scripts/cleanup_data.py
"""
import json
import re
import shutil
from pathlib import Path
from collections import Counter, defaultdict
from copy import deepcopy

DATA = Path("/home/ubuntu/alkyline/data/processed")
BACKUP = DATA / "backup"

# ─── Constants ───────────────────────────────────────────────────────────────

SECURITY_VENDORS = {
    "ESET", "Trend Micro", "Kaspersky", "Symantec", "SentinelOne",
    "Avast", "Fortinet", "Bitdefender", "Sophos", "Palo Alto", "McAfee",
}

# False positive "at" context patterns
AT_FALSE_POSITIVE_RE = re.compile(
    r'\bat\s+(least|the|a|an|this|that|once|any|all|one|times?|some|which|various)\b'
    r'|(?:aimed|look(?:ing)?|looked|arrive[ds]?|point(?:ed|ing)?|direct(?:ed|ing)?)\s+at\b'
    r'|\bat\b(?!\s+command|\s+utility|\s+scheduler|\s+job)',
    re.IGNORECASE
)

FILEPATH_DATE_RE = re.compile(r'^/\d{1,2}/\d{2,4}$')

# HTML tags to strip (real markup, not cybersec terms like <payload>)
HTML_TAG_RE = re.compile(
    r'</?(?:p|br|div|span|a|b|i|em|strong|ul|ol|li|td|tr|th|table|thead|tbody|'
    r'h[1-6]|img|hr|blockquote|pre|code|dl|dt|dd|sup|sub|font|center|'
    r'section|article|header|footer|nav|main|aside|figure|figcaption|caption|'
    r'small|big|u|s|strike|del|ins|abbr|cite|q|mark|ruby|rt|rp|wbr)'
    r'(?:\s[^>]*)?\s*/?>',
    re.IGNORECASE
)

# Also strip HTML entities
HTML_ENTITY_RE = re.compile(r'&(?:nbsp|amp|lt|gt|quot|apos|#\d+|#x[0-9a-fA-F]+);')

LABEL_MAP_5 = {
    "MALWARE": "Malware", "THREAT_ACTOR": None, "TOOL": None,
    "VULNERABILITY": "Vulnerability", "SYSTEM": "System", "ORGANIZATION": "Organization",
    "IP_ADDRESS": "Indicator", "DOMAIN": "Indicator", "URL": "Indicator",
    "HASH": "Indicator", "EMAIL": "Indicator", "CVE_ID": "Vulnerability", "FILEPATH": None,
}

# ─── Stats tracker ───────────────────────────────────────────────────────────

stats = Counter()


# ─── Helpers ─────────────────────────────────────────────────────────────────

def load_jsonl(path):
    records = []
    with open(path) as f:
        for line in f:
            line = line.strip()
            if line:
                records.append(json.loads(line))
    return records


def save_jsonl(path, records):
    with open(path, "w") as f:
        for rec in records:
            f.write(json.dumps(rec, ensure_ascii=False) + "\n")


def backup_file(path):
    if path.exists():
        dst = BACKUP / path.name
        if not dst.exists():
            shutil.copy2(path, dst)


def get_span_entity(key):
    """Extract (label, entity) from 'LABEL: entity'."""
    parts = key.split(": ", 1)
    return (parts[0], parts[1]) if len(parts) == 2 else (parts[0], "")


def spans_overlap(a_start, a_end, b_start, b_end):
    return a_start < b_end and b_start < a_end


# ─── Fix functions ───────────────────────────────────────────────────────────

def fix_tool_at(rec):
    """Remove 'TOOL: at' false positives (P0-2)."""
    text = rec["text"]
    spans = rec.get("spans", {})
    key = "TOOL: at"
    if key not in spans:
        return 0

    offsets = spans[key]
    kept = []
    removed = 0
    for off in offsets:
        start, end = off[0], off[1]
        # Get context: 40 chars before and after
        ctx_start = max(0, start - 40)
        ctx_end = min(len(text), end + 40)
        context = text[ctx_start:ctx_end].lower()

        # Check if context clearly refers to Unix at command
        if any(p in context for p in ["at command", "at utility", "at scheduler",
                                       "the at tool", "using at to schedule",
                                       "at job", "/usr/bin/at"]):
            kept.append(off)
        else:
            removed += 1

    if removed:
        if kept:
            spans[key] = kept
        else:
            del spans[key]
    return removed


def fix_filepath_dates(rec):
    """Remove FILEPATH spans matching date patterns (P0-3)."""
    spans = rec.get("spans", {})
    removed = 0
    to_delete = []
    for key in list(spans.keys()):
        label, entity = get_span_entity(key)
        if label != "FILEPATH":
            continue
        if FILEPATH_DATE_RE.match(entity):
            to_delete.append(key)
            removed += len(spans[key])

    for key in to_delete:
        del spans[key]
    return removed


def fix_overlapping_spans(rec):
    """Resolve overlapping spans β€” keep longest; remove MALWARE:Play overlapping SYSTEM:Google Play (P0-4)."""
    spans = rec.get("spans", {})
    if not spans:
        return 0

    # Flatten all spans into a list of (start, end, key, offset_idx)
    flat = []
    for key, offsets in spans.items():
        for i, off in enumerate(offsets):
            flat.append((off[0], off[1], key, i))

    if len(flat) < 2:
        return 0

    # Sort by start, then by length descending
    flat.sort(key=lambda x: (x[0], -(x[1] - x[0])))

    to_remove = set()  # (key, offset_idx)
    removed = 0

    for i in range(len(flat)):
        if (flat[i][2], flat[i][3]) in to_remove:
            continue
        for j in range(i + 1, len(flat)):
            if flat[j][0] >= flat[i][1]:
                break  # no more overlaps possible
            if (flat[j][2], flat[j][3]) in to_remove:
                continue
            if not spans_overlap(flat[i][0], flat[i][1], flat[j][0], flat[j][1]):
                continue

            # Overlap found β€” decide which to remove
            i_key, j_key = flat[i][2], flat[j][2]
            i_len = flat[i][1] - flat[i][0]
            j_len = flat[j][1] - flat[j][0]

            # Special case: MALWARE: Play overlapping SYSTEM: Google Play
            if i_key.startswith("MALWARE: Play") and "Google Play" in j_key:
                to_remove.add((flat[i][2], flat[i][3]))
            elif j_key.startswith("MALWARE: Play") and "Google Play" in i_key:
                to_remove.add((flat[j][2], flat[j][3]))
            elif i_len >= j_len:
                to_remove.add((flat[j][2], flat[j][3]))
            else:
                to_remove.add((flat[i][2], flat[i][3]))

    if not to_remove:
        return 0

    # Rebuild spans, removing flagged offsets
    new_spans = {}
    for key, offsets in spans.items():
        kept = [off for i, off in enumerate(offsets) if (key, i) not in to_remove]
        if kept:
            new_spans[key] = kept
        else:
            removed += 1
    removed_count = len(to_remove)
    rec["spans"] = new_spans
    return removed_count


def fix_vendor_labels(rec):
    """Relabel security vendors from SYSTEM β†’ ORGANIZATION (P1-6)."""
    spans = rec.get("spans", {})
    fixed = 0
    for vendor in SECURITY_VENDORS:
        old_key = f"SYSTEM: {vendor}"
        if old_key in spans:
            new_key = f"ORGANIZATION: {vendor}"
            offsets = spans.pop(old_key)
            spans.setdefault(new_key, []).extend(offsets)
            fixed += len(offsets)
    return fixed


def clean_html_str(s):
    """Strip HTML tags and entities from a string."""
    s = HTML_TAG_RE.sub("", s)
    s = HTML_ENTITY_RE.sub("", s)
    return s


def fix_html(rec):
    """Strip HTML tags from text and recalculate span offsets (P1-8)."""
    text = rec["text"]
    if not HTML_TAG_RE.search(text) and not HTML_ENTITY_RE.search(text):
        return 0

    cleaned = clean_html_str(text)
    if cleaned == text:
        return 0

    # Re-find each entity in the cleaned text
    spans = rec.get("spans", {})
    new_spans = {}

    for key, offsets in spans.items():
        label, entity = get_span_entity(key)
        # Clean the entity in the key too
        clean_entity = clean_html_str(entity)
        if not clean_entity.strip():
            continue
        clean_key = f"{label}: {clean_entity}" if clean_entity != entity else key

        new_offsets = []
        for off in offsets:
            orig_entity = text[off[0]:off[1]]
            ce = clean_html_str(orig_entity)
            if not ce.strip():
                continue
            # Find in cleaned text
            idx = cleaned.find(ce)
            if idx == -1:
                idx = cleaned.lower().find(ce.lower())
            if idx != -1:
                new_offsets.append([idx, idx + len(ce)])

        if new_offsets:
            new_spans.setdefault(clean_key, []).extend(new_offsets)

    rec["text"] = cleaned
    rec["spans"] = new_spans
    return 1


def fix_dirty_span_keys(rec):
    """Clean HTML remnants from span keys and fix key↔offset mismatches (post-HTML-strip)."""
    text = rec["text"]
    spans = rec.get("spans", {})
    new_spans = {}
    fixed = 0

    for key, offsets in spans.items():
        label, entity = get_span_entity(key)
        clean_entity = clean_html_str(entity)
        if not clean_entity.strip():
            continue

        # Only remap if HTML was actually removed from the entity
        if clean_entity == entity:
            new_spans.setdefault(key, []).extend(offsets)
            continue

        clean_key = f"{label}: {clean_entity}"
        new_offsets = []
        for off in offsets:
            actual = text[off[0]:off[1]]
            if actual == clean_entity:
                new_offsets.append(off)
            else:
                # Try to find entity near the offset
                search_start = max(0, off[0] - 10)
                search_end = min(len(text), off[1] + 10)
                window = text[search_start:search_end]
                idx = window.find(clean_entity)
                if idx != -1:
                    abs_start = search_start + idx
                    new_offsets.append([abs_start, abs_start + len(clean_entity)])
                    fixed += 1

        if new_offsets:
            new_spans.setdefault(clean_key, []).extend(new_offsets)

    rec["spans"] = new_spans
    return fixed


def verify_offsets(rec):
    """Return list of offset errors."""
    text = rec.get("text", "")
    errors = []
    for key, offsets in rec.get("spans", {}).items():
        _, entity = get_span_entity(key)
        for off in offsets:
            if off[0] < 0 or off[1] > len(text) or off[0] >= off[1]:
                errors.append(f"{key}: [{off[0]},{off[1]}] out of bounds (len={len(text)})")
            else:
                actual = text[off[0]:off[1]]
                if actual != entity:
                    # Allow minor mismatches (whitespace, case)
                    if actual.strip().lower() != entity.strip().lower():
                        errors.append(f"{key}: expected '{entity}' got '{actual}' at [{off[0]},{off[1]}]")
    return errors


def dedup_offsets(rec):
    """Remove duplicate offsets within each span key."""
    spans = rec.get("spans", {})
    for key in spans:
        seen = set()
        unique = []
        for off in spans[key]:
            t = (off[0], off[1])
            if t not in seen:
                seen.add(t)
                unique.append(off)
        spans[key] = unique


# ─── Main cleanup pipeline ──────────────────────────────────────────────────

def main():
    print("=" * 70)
    print("ARCSPAN DATA CLEANUP")
    print("=" * 70)

    # ── Backup ───────────────────────────────────────────────────────────
    BACKUP.mkdir(exist_ok=True)
    all_files = sorted(DATA.glob("*.jsonl"))
    for f in all_files:
        backup_file(f)
    print(f"\nβœ“ Backed up {len(all_files)} files to {BACKUP}/")

    # ── Phase 1: Clean LLM files (P0-2,3,4 + P1-5,6,7,8) ───────────────
    print("\n" + "─" * 70)
    print("PHASE 1: Clean LLM annotation/generation files")
    print("─" * 70)

    llm_files = sorted(DATA.glob("llm_annotated_*.jsonl")) + sorted(DATA.glob("llm_generated_*.jsonl"))

    # P1-5: Deduplicate LLM files
    # Load mitre_v2 and nvd_v2 texts for dedup
    mitre_v2_texts = set()
    nvd_v2_texts = set()
    if (DATA / "llm_annotated_mitre_v2.jsonl").exists():
        for rec in load_jsonl(DATA / "llm_annotated_mitre_v2.jsonl"):
            mitre_v2_texts.add(rec["text"])
            mitre_v2_texts.add(clean_html_str(rec["text"]))
    if (DATA / "llm_annotated_nvd_v2.jsonl").exists():
        for rec in load_jsonl(DATA / "llm_annotated_nvd_v2.jsonl"):
            nvd_v2_texts.add(rec["text"])
            nvd_v2_texts.add(clean_html_str(rec["text"]))

    for fpath in llm_files:
        records = load_jsonl(fpath)
        orig_count = len(records)
        fname = fpath.name

        # P1-5a: Remove texts that exist in v2 files (pre-fix pass)
        if fname == "llm_annotated_mitre.jsonl":
            records = [r for r in records if r["text"] not in mitre_v2_texts]
            stats["mitre_deduped"] += orig_count - len(records)
        elif fname == "llm_annotated_apt.jsonl":
            records = [r for r in records if r["text"] not in mitre_v2_texts]
            stats["apt_deduped_vs_mitre"] += orig_count - len(records)
        elif fname == "llm_annotated_nvd.jsonl":
            records = [r for r in records if r["text"] not in nvd_v2_texts]
            stats["nvd_deduped"] += orig_count - len(records)

        # Apply per-record fixes BEFORE dedup (HTML strip can create new dupes)
        for rec in records:
            # P0-2: Remove TOOL: at false positives
            n = fix_tool_at(rec)
            stats["tool_at_removed"] += n

            # P0-3: Remove FILEPATH date false positives
            n = fix_filepath_dates(rec)
            stats["filepath_date_removed"] += n

            # P1-6: Relabel security vendors
            n = fix_vendor_labels(rec)
            stats["vendor_relabeled"] += n

            # P1-8: Strip HTML
            n = fix_html(rec)
            stats["html_stripped"] += n

            # Post-fix: clean dirty span keys (HTML remnants in keys)
            fix_dirty_span_keys(rec)
            dedup_offsets(rec)

            # P0-4: Fix overlapping spans LAST (after all transforms)
            while True:
                n = fix_overlapping_spans(rec)
                if n == 0:
                    break
                stats["overlaps_fixed"] += n

        # P1-5a (post-fix): re-check against v2 texts after HTML strip
        if fname == "llm_annotated_mitre.jsonl":
            before = len(records)
            records = [r for r in records if r["text"] not in mitre_v2_texts]
            stats["mitre_deduped"] += before - len(records)
        elif fname == "llm_annotated_apt.jsonl":
            before = len(records)
            records = [r for r in records if r["text"] not in mitre_v2_texts]
            stats["apt_deduped_vs_mitre"] += before - len(records)
        elif fname == "llm_annotated_nvd.jsonl":
            before = len(records)
            records = [r for r in records if r["text"] not in nvd_v2_texts]
            stats["nvd_deduped"] += before - len(records)

        # P1-5b: Remove exact duplicate texts within file (after fixes)
        seen_texts = set()
        deduped = []
        for r in records:
            if r["text"] not in seen_texts:
                seen_texts.add(r["text"])
                deduped.append(r)
        stats[f"intra_dedup_{fname}"] += len(records) - len(deduped)
        records = deduped

        # P1-7: Remove short texts
        before = len(records)
        records = [r for r in records if len(r["text"]) >= 20]
        stats["short_removed"] += before - len(records)

        # Remove records with no spans
        before = len(records)
        records = [r for r in records if r.get("spans")]
        stats["empty_spans_removed"] += before - len(records)

        save_jsonl(fpath, records)
        print(f"  {fname}: {orig_count} β†’ {len(records)}")

    # ── Phase 2: Clean aggregated files (P0-1,4 + P1-6,7,8) ────────────
    print("\n" + "─" * 70)
    print("PHASE 2: Clean aggregated files & fix train/test leakage")
    print("─" * 70)

    # Load all aggregated files
    agg_data = {}
    for variant in ["13class", "5class"]:
        for split in ["test", "valid", "train"]:
            key = f"aggregated_{variant}_{split}.jsonl"
            fpath = DATA / key
            if fpath.exists():
                agg_data[key] = load_jsonl(fpath)

    # P0-1: Deduplicate across splits (priority: test > valid > train)
    for variant in ["13class", "5class"]:
        seen_texts = set()
        total_removed = 0
        for split in ["test", "valid", "train"]:
            key = f"aggregated_{variant}_{split}.jsonl"
            if key not in agg_data:
                continue
            records = agg_data[key]
            deduped = []
            for rec in records:
                if rec["text"] not in seen_texts:
                    seen_texts.add(rec["text"])
                    deduped.append(rec)
                else:
                    total_removed += 1
            agg_data[key] = deduped
        stats[f"leakage_removed_{variant}"] += total_removed

    # Apply per-record fixes to aggregated data
    for key, records in agg_data.items():
        orig_count = len(records)
        for rec in records:
            fix_vendor_labels(rec)
            fix_html(rec)
            fix_filepath_dates(rec)
            fix_tool_at(rec)
            fix_dirty_span_keys(rec)
            dedup_offsets(rec)
            while fix_overlapping_spans(rec): pass

        # Remove short texts
        records = [r for r in records if len(r["text"]) >= 20]
        agg_data[key] = records
        print(f"  {key}: {orig_count} β†’ {len(records)}")

    # Save aggregated files
    for key, records in agg_data.items():
        save_jsonl(DATA / key, records)

    # ── Phase 3: Regenerate enriched files ──────────────────────────────
    print("\n" + "─" * 70)
    print("PHASE 3: Regenerate enriched files")
    print("─" * 70)

    # Reload cleaned LLM files
    llm_records = []
    for f in sorted(DATA.glob("llm_annotated_*.jsonl")) + sorted(DATA.glob("llm_generated_*.jsonl")):
        llm_records.extend(load_jsonl(f))
    print(f"  LLM records: {len(llm_records)}")

    # Enriched 13-class train = aggregated 13-class train + all LLM
    agg_13_train = load_jsonl(DATA / "aggregated_13class_train.jsonl")
    enriched_13_train = agg_13_train + llm_records
    save_jsonl(DATA / "enriched_13class_train.jsonl", enriched_13_train)
    print(f"  enriched_13class_train: {len(enriched_13_train)}")

    # Enriched 5-class train = aggregated 5-class train + LLM (mapped)
    agg_5_train = load_jsonl(DATA / "aggregated_5class_train.jsonl")
    llm_5class = []
    for rec in llm_records:
        new_rec = deepcopy(rec)
        new_spans = {}
        for key, offsets in rec["spans"].items():
            label, entity = get_span_entity(key)
            l5 = LABEL_MAP_5.get(label)
            if l5:
                new_spans.setdefault(f"{l5}: {entity}", []).extend(offsets)
        new_rec["spans"] = new_spans
        if new_spans:
            llm_5class.append(new_rec)
    enriched_5_train = agg_5_train + llm_5class
    save_jsonl(DATA / "enriched_5class_train.jsonl", enriched_5_train)
    print(f"  enriched_5class_train: {len(enriched_5_train)}")

    # Valid/test: copy from aggregated
    for split in ["valid", "test"]:
        for variant in ["13class", "5class"]:
            src = DATA / f"aggregated_{variant}_{split}.jsonl"
            dst = DATA / f"enriched_{variant}_{split}.jsonl"
            shutil.copy2(src, dst)
            n = sum(1 for _ in open(dst))
            print(f"  enriched_{variant}_{split}: {n}")

    # ── Phase 4: Verification ───────────────────────────────────────────
    print("\n" + "─" * 70)
    print("PHASE 4: Offset verification")
    print("─" * 70)

    total_checked = 0
    total_errors = 0
    for fpath in sorted(DATA.glob("*.jsonl")):
        if fpath.parent.name == "backup":
            continue
        errors_in_file = 0
        records = load_jsonl(fpath)
        for rec in records:
            errs = verify_offsets(rec)
            errors_in_file += len(errs)
        total_checked += len(records)
        if errors_in_file:
            print(f"  ⚠ {fpath.name}: {errors_in_file} offset errors")
            total_errors += errors_in_file

    if total_errors == 0:
        print(f"  βœ“ All {total_checked} records pass offset verification")
    else:
        print(f"  ⚠ {total_errors} total offset errors across {total_checked} records")

    # ── Summary ─────────────────────────────────────────────────────────
    print("\n" + "=" * 70)
    print("CLEANUP SUMMARY")
    print("=" * 70)
    for k, v in sorted(stats.items()):
        if v > 0:
            print(f"  {k}: {v}")
    print("=" * 70)
    print("Done.")


if __name__ == "__main__":
    main()