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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
#!/usr/bin/env python3
"""
MINDI 1.5 Vision-Coder β€” Quality Filter Pipeline

Filters mindi_all.jsonl to remove low-quality examples:
  1. Token length filter   β€” drop if <50 tokens or >4096 tokens
  2. Duplicate detection   β€” SHA-256 hash of assistant content
  3. JSON structure check  β€” valid schema with required fields
  4. Special token check   β€” assistant must have code_start/code_end pair
  5. Quality score filter  β€” keep only quality_score >= 5.0
  6. Content heuristics    β€” drop empty/trivial/boilerplate responses

Usage:
    python scripts/quality_filter.py                  # Full run
    python scripts/quality_filter.py --dry-run        # Preview only
    python scripts/quality_filter.py --min-tokens 100 # Custom min tokens
    python scripts/quality_filter.py --max-tokens 8192 # Custom max tokens
    python scripts/quality_filter.py --min-quality 7.0 # Stricter quality
"""

from __future__ import annotations

import argparse
import hashlib
import json
import sys
import time
from collections import Counter, defaultdict
from pathlib import Path

# ── Paths ─────────────────────────────────────────────────────────────

PROJECT_ROOT = Path(__file__).resolve().parent.parent
INPUT_FILE = PROJECT_ROOT / "data" / "processed" / "mindi_all.jsonl"
OUTPUT_FILE = PROJECT_ROOT / "data" / "processed" / "mindi_filtered.jsonl"
REJECT_FILE = PROJECT_ROOT / "data" / "processed" / "mindi_rejected.jsonl"
REPORT_FILE = PROJECT_ROOT / "data" / "processed" / "filter_report.json"

# ── Required schema fields ────────────────────────────────────────────

REQUIRED_FIELDS = {"id", "type", "source", "messages", "metadata"}
REQUIRED_METADATA = {"language", "tokens"}
VALID_ROLES = {"system", "user", "assistant"}

# ── Protected sources (hand-crafted gold data β€” lighter filtering) ─────

PROTECTED_SOURCES = {"sandbox_examples", "search_examples", "synthetic_nextjs"}

# ── MINDI agentic token scoring bonuses ───────────────────────────────
#   Examples with these tokens teach the model to be an *agent*.
#   Each occurrence adds to the quality_score before the threshold.

MINDI_TOKEN_BONUSES = {
    "<|think_start|>": 2.0,
    "<|search_start|>": 3.0,
    "<|error_start|>": 3.0,
    "<|sandbox_start|>": 3.0,
    "<|critique_start|>": 2.0,
    "<|suggest_start|>": 1.0,
}

# ── Special token pairs that assistant messages should contain ─────────

CODE_TOKEN_PAIRS = [
    ("<|code_start|>", "<|code_end|>"),
]

# At least one of these pairs should be present in assistant content
OPTIONAL_TOKEN_PAIRS = [
    ("<|think_start|>", "<|think_end|>"),
    ("<|critique_start|>", "<|critique_end|>"),
    ("<|suggest_start|>", "<|suggest_end|>"),
    ("<|file_start|>", "<|file_end|>"),
    ("<|search_start|>", "<|search_end|>"),
    ("<|sandbox_start|>", "<|sandbox_end|>"),
    ("<|error_start|>", "<|error_end|>"),
    ("<|fix_start|>", "<|fix_end|>"),
]

# ── Rejection reasons ─────────────────────────────────────────────────

class Reason:
    INVALID_JSON = "invalid_json"
    MISSING_FIELDS = "missing_fields"
    MISSING_METADATA = "missing_metadata"
    NO_MESSAGES = "no_messages"
    BAD_ROLES = "bad_message_roles"
    NO_ASSISTANT = "no_assistant_message"
    EMPTY_ASSISTANT = "empty_assistant_content"
    TOO_SHORT = "too_few_tokens"
    TOO_LONG = "too_many_tokens"
    DUPLICATE = "duplicate_content"
    LOW_QUALITY = "low_quality_score"
    NO_CODE_TOKENS = "missing_code_tokens"
    BOILERPLATE = "boilerplate_content"
    UNMATCHED_TOKENS = "unmatched_special_tokens"


# ── Filter functions ──────────────────────────────────────────────────

def validate_schema(example: dict) -> str | None:
    """Check required fields and structure. Returns rejection reason or None."""
    # Top-level fields
    missing = REQUIRED_FIELDS - set(example.keys())
    if missing:
        return Reason.MISSING_FIELDS

    # Metadata fields
    meta = example.get("metadata", {})
    if not isinstance(meta, dict):
        return Reason.MISSING_METADATA
    missing_meta = REQUIRED_METADATA - set(meta.keys())
    if missing_meta:
        return Reason.MISSING_METADATA

    # Messages array
    messages = example.get("messages", [])
    if not isinstance(messages, list) or len(messages) == 0:
        return Reason.NO_MESSAGES

    # Role validation
    for msg in messages:
        if not isinstance(msg, dict) or "role" not in msg or "content" not in msg:
            return Reason.BAD_ROLES
        if msg["role"] not in VALID_ROLES:
            return Reason.BAD_ROLES

    return None


def get_assistant_content(example: dict) -> str:
    """Extract concatenated assistant message content."""
    parts = []
    for msg in example.get("messages", []):
        if msg.get("role") == "assistant":
            parts.append(msg.get("content", ""))
    return "\n".join(parts)


def check_assistant_exists(example: dict) -> str | None:
    """Must have at least one assistant message with non-empty content."""
    content = get_assistant_content(example)
    if not content:
        return Reason.NO_ASSISTANT
    if len(content.strip()) < 10:
        return Reason.EMPTY_ASSISTANT
    return None


def check_token_length(example: dict, min_tokens: int, max_tokens: int) -> str | None:
    """Filter by token count stored in metadata."""
    tokens = example.get("metadata", {}).get("tokens", 0)
    if tokens < min_tokens:
        return Reason.TOO_SHORT
    if tokens > max_tokens:
        return Reason.TOO_LONG
    return None


def compute_mindi_bonus(example: dict) -> float:
    """Compute bonus score for MINDI agentic special tokens."""
    content = get_assistant_content(example)
    bonus = 0.0
    for token, value in MINDI_TOKEN_BONUSES.items():
        if token in content:
            bonus += value
    return bonus


def check_quality_score(example: dict, min_quality: float) -> str | None:
    """Filter by quality_score + MINDI token bonus."""
    score = example.get("metadata", {}).get("quality_score", 0.0)
    score += compute_mindi_bonus(example)
    if score < min_quality:
        return Reason.LOW_QUALITY
    return None


def check_code_tokens(example: dict) -> str | None:
    """Assistant content must contain code_start/code_end pair."""
    content = get_assistant_content(example)

    for start_tok, end_tok in CODE_TOKEN_PAIRS:
        if start_tok in content and end_tok in content:
            # Check ordering: start before end
            if content.index(start_tok) < content.rindex(end_tok):
                return None  # OK

    return Reason.NO_CODE_TOKENS


def check_unmatched_tokens(example: dict) -> str | None:
    """Ensure all special token pairs are properly matched (start count == end count)."""
    content = get_assistant_content(example)
    all_pairs = CODE_TOKEN_PAIRS + OPTIONAL_TOKEN_PAIRS

    for start_tok, end_tok in all_pairs:
        start_count = content.count(start_tok)
        end_count = content.count(end_tok)
        if start_count != end_count:
            return Reason.UNMATCHED_TOKENS

    return None


def check_boilerplate(example: dict) -> str | None:
    """Detect boilerplate/placeholder assistant responses."""
    content = get_assistant_content(example)
    content_lower = content.lower().strip()

    # Very short code blocks (just placeholder)
    code_markers = ("<|code_start|>", "<|code_end|>")
    if code_markers[0] in content and code_markers[1] in content:
        start_idx = content.index(code_markers[0]) + len(code_markers[0])
        end_idx = content.index(code_markers[1])
        code_body = content[start_idx:end_idx].strip()
        if len(code_body) < 5:
            return Reason.BOILERPLATE

    # Repetitive content (same char repeated)
    stripped = content_lower.replace(" ", "").replace("\n", "")
    if len(stripped) > 20:
        unique_chars = len(set(stripped))
        if unique_chars < 5:
            return Reason.BOILERPLATE

    return None


def content_hash(example: dict) -> str:
    """SHA-256 hash of assistant content for deduplication."""
    content = get_assistant_content(example)
    return hashlib.sha256(content.encode("utf-8", errors="replace")).hexdigest()


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

def run_filter(
    dry_run: bool = False,
    min_tokens: int = 50,
    max_tokens: int = 4096,
    min_quality: float = 5.0,
) -> None:
    """Run the full quality filter pipeline."""

    if not INPUT_FILE.exists():
        print(f"ERROR: Input file not found: {INPUT_FILE}")
        sys.exit(1)

    # Count input lines
    print(f"Counting input examples from {INPUT_FILE.name} ...")
    total_input = sum(1 for _ in open(INPUT_FILE, "r", encoding="utf-8"))
    print(f"  Total input: {total_input:,} examples")
    print()

    # Filter settings
    print("Filter settings:")
    print(f"  Min tokens:   {min_tokens}")
    print(f"  Max tokens:   {max_tokens}")
    print(f"  Min quality:  {min_quality}")
    print(f"  Dry run:      {dry_run}")
    print()

    # Stats tracking
    kept = 0
    rejected = 0
    reject_reasons: Counter = Counter()
    source_kept: Counter = Counter()
    source_rejected: Counter = Counter()
    seen_hashes: set[str] = set()
    token_sum = 0
    quality_sum = 0.0

    # Type distribution
    type_counts: Counter = Counter()

    # Language distribution
    lang_counts: Counter = Counter()

    start_time = time.time()

    out_f = None
    rej_f = None
    if not dry_run:
        OUTPUT_FILE.parent.mkdir(parents=True, exist_ok=True)
        out_f = open(OUTPUT_FILE, "w", encoding="utf-8")
        rej_f = open(REJECT_FILE, "w", encoding="utf-8")

    try:
        with open(INPUT_FILE, "r", encoding="utf-8") as f:
            for line_num, line in enumerate(f, 1):
                line = line.strip()
                if not line:
                    continue

                # Parse JSON
                try:
                    example = json.loads(line)
                except json.JSONDecodeError:
                    reject_reasons[Reason.INVALID_JSON] += 1
                    rejected += 1
                    if rej_f:
                        rej_f.write(line + "\n")
                    continue

                source = example.get("source", "unknown")
                is_protected = source in PROTECTED_SOURCES

                # Run filter chain (order matters: cheapest first)
                # Protected sources: schema + assistant + token length + unmatched only
                # Regular sources: full chain + dedup
                if is_protected:
                    rejection = (
                        validate_schema(example)
                        or check_assistant_exists(example)
                        or check_token_length(example, min_tokens, max_tokens)
                        or check_unmatched_tokens(example)
                    )
                else:
                    rejection = (
                        validate_schema(example)
                        or check_assistant_exists(example)
                        or check_token_length(example, min_tokens, max_tokens)
                        or check_quality_score(example, min_quality)
                        or check_code_tokens(example)
                        or check_unmatched_tokens(example)
                        or check_boilerplate(example)
                    )

                if rejection is None and not is_protected:
                    # Dedup check (skip for protected sources)
                    h = content_hash(example)
                    if h in seen_hashes:
                        rejection = Reason.DUPLICATE

                if rejection is not None:
                    reject_reasons[rejection] += 1
                    source_rejected[source] += 1
                    rejected += 1
                    if rej_f:
                        rej_f.write(line + "\n")
                    continue

                # Passed all filters
                if not is_protected:
                    seen_hashes.add(h)
                kept += 1
                source_kept[source] += 1
                token_sum += example.get("metadata", {}).get("tokens", 0)
                quality_sum += example.get("metadata", {}).get("quality_score", 0.0)
                type_counts[example.get("type", "unknown")] += 1
                lang_counts[example.get("metadata", {}).get("language", "unknown")] += 1

                if out_f:
                    out_f.write(line + "\n")

                # Progress
                if line_num % 50000 == 0:
                    elapsed = time.time() - start_time
                    rate = line_num / elapsed if elapsed > 0 else 0
                    pct = (line_num / total_input) * 100
                    print(f"  [{pct:5.1f}%] Processed {line_num:>10,} | Kept {kept:>10,} | Rejected {rejected:>10,} | {rate:,.0f} ex/s")

    finally:
        if out_f:
            out_f.close()
        if rej_f:
            rej_f.close()

    elapsed = time.time() - start_time

    # ── Summary report ────────────────────────────────────────────
    print()
    print("=" * 60)
    print("  QUALITY FILTER REPORT")
    print("=" * 60)
    print(f"  Input:       {total_input:>10,} examples")
    print(f"  Kept:        {kept:>10,} examples ({kept/total_input*100:.1f}%)")
    print(f"  Rejected:    {rejected:>10,} examples ({rejected/total_input*100:.1f}%)")
    print(f"  Time:        {elapsed:>10.1f} seconds")
    print(f"  Rate:        {total_input/elapsed:>10,.0f} examples/sec")
    print()

    if kept > 0:
        print(f"  Avg tokens:  {token_sum/kept:>10.0f}")
        print(f"  Avg quality: {quality_sum/kept:>10.2f}")
        print(f"  Total tokens:{token_sum:>10,}")
        print()

    # Rejection breakdown
    print("  Rejection breakdown:")
    for reason, count in reject_reasons.most_common():
        pct = count / total_input * 100
        print(f"    {reason:<30s} {count:>10,} ({pct:.1f}%)")
    print()

    # Source breakdown
    print("  Source breakdown (kept / total):")
    all_sources = sorted(set(list(source_kept.keys()) + list(source_rejected.keys())))
    for src in all_sources:
        k = source_kept.get(src, 0)
        total = k + source_rejected.get(src, 0)
        pct = k / total * 100 if total > 0 else 0
        print(f"    {src:<25s} {k:>8,} / {total:>8,} ({pct:.1f}%)")
    print()

    # Type distribution
    print("  Type distribution (kept):")
    for t, c in type_counts.most_common(10):
        print(f"    {t:<25s} {c:>8,}")
    print()

    # Language distribution (top 15)
    print("  Language distribution (kept, top 15):")
    for lang, c in lang_counts.most_common(15):
        print(f"    {lang:<25s} {c:>8,}")
    print()

    if not dry_run:
        print(f"  Output:  {OUTPUT_FILE}")
        print(f"  Rejects: {REJECT_FILE}")

        # Save machine-readable report
        report = {
            "input_count": total_input,
            "kept_count": kept,
            "rejected_count": rejected,
            "kept_pct": round(kept / total_input * 100, 2),
            "avg_tokens": round(token_sum / kept, 1) if kept > 0 else 0,
            "avg_quality": round(quality_sum / kept, 3) if kept > 0 else 0,
            "total_tokens": token_sum,
            "elapsed_seconds": round(elapsed, 1),
            "filter_settings": {
                "min_tokens": min_tokens,
                "max_tokens": max_tokens,
                "min_quality": min_quality,
            },
            "rejection_breakdown": dict(reject_reasons.most_common()),
            "source_kept": dict(source_kept),
            "source_rejected": dict(source_rejected),
            "type_distribution": dict(type_counts.most_common()),
            "language_distribution": dict(lang_counts.most_common(30)),
        }
        with open(REPORT_FILE, "w", encoding="utf-8") as rf:
            json.dump(report, rf, indent=2)
        print(f"  Report:  {REPORT_FILE}")

    print("=" * 60)


# ── CLI ───────────────────────────────────────────────────────────────

def main():
    parser = argparse.ArgumentParser(
        description="MINDI Quality Filter β€” remove low-quality training examples",
    )
    parser.add_argument("--dry-run", action="store_true", help="Preview counts without writing output")
    parser.add_argument("--min-tokens", type=int, default=50, help="Minimum token count (default: 50)")
    parser.add_argument("--max-tokens", type=int, default=4096, help="Maximum token count (default: 4096)")
    parser.add_argument("--min-quality", type=float, default=5.0, help="Minimum quality_score (default: 5.0)")

    args = parser.parse_args()
    run_filter(
        dry_run=args.dry_run,
        min_tokens=args.min_tokens,
        max_tokens=args.max_tokens,
        min_quality=args.min_quality,
    )


if __name__ == "__main__":
    main()