Bug Mining & Mutation

The core idea behind Pytifex’s generation step is: don’t ask an LLM to invent type-checking edge cases from nothing — mine real bugs from the checkers’ own GitHub issues and use them as seeds for new variations. A real bug report already proved a checker had a weakness somewhere; that’s a much better starting point than a cold prompt.

The pipeline never tests the original GitHub code directly, by the way. Seeds are inspiration only — what actually gets type-checked is always freshly generated code.

Seed mining (github_issues.py)

Which repos

REPOS = {
    "mypy": "python/mypy",
    "pyrefly": "facebook/pyrefly",
    "ty": "astral-sh/ty",
    "pyright": "microsoft/pyright",
    "zuban": "zubanls/zuban",
}

Note that pyright is in this list even though Pytifex doesn’t actually evaluate pyright — its issue tracker just has a lot of useful code examples, so it’s a source for seeds, not a checker under test.

Filtering to confirmed bugs

Not every closed issue is a real bug — plenty get closed as “won’t fix” or as duplicates. Pytifex checks the GitHub state_reason field:

def is_confirmed_bug(issue: dict) -> bool:
    if issue.get("state") != "closed":
        return False
    if issue.get("state_reason") == "not_planned":
        return False
    return True

Only state_reason == "completed" (or null, for older issues that predate this field) passes.

False positive / false negative labels

FALSE_POSITIVE_LABELS = ["false-positive", "false positive", "spurious", ...]
FALSE_NEGATIVE_LABELS = ["false-negative", "false negative", "missed-error", ...]

This classification gets passed into the prompt so the LLM knows whether the seed was a checker over-reporting (false positive) or under-reporting (false negative).

Pulling code out of the issue body

Two methods. First, plain fenced code blocks:

pattern = r"```(?:python|py)\n(.*?)```"
matches = re.findall(pattern, text, re.DOTALL | re.IGNORECASE)

Second, pyrefly’s sandbox links. These look like pyrefly.org/sandbox/?project=<base64> — the base64 payload decodes to JSON containing the source:

sandbox_pattern = r'https://pyrefly\.org/sandbox/\?project=([A-Za-z0-9%+/=]+)'
# url-decode -> base64-decode -> json parse -> pull out the code

Anything shorter than 50 characters gets thrown out as not useful.

Rate limits

60 requests/hour without a token, 5,000 with one:

export GITHUB_TOKEN=ghp_your_token

Or skip it entirely with --no-github.

Generation (mutation)

The main prompt (build_seed_based_prompt)

Shows 3-5 real bug examples to the model and asks for variations:

prompt = f"""
## REAL BUG EXAMPLES FROM TYPE CHECKER ISSUES:
{seeds_text}

## YOUR TASK:
Using these real bugs as inspiration, generate {num_variations}
NEW Python code examples that:
1. Are VARIATIONS or EXTENSIONS of the patterns shown above
2. Target subtle type system edge cases likely to cause checker disagreements
3. Are self-contained and runnable (include all imports)

## STRATEGY FOR GENERATING DIVERGENCES:
- If a seed shows a false positive in mypy, create a similar case
  that other checkers also get wrong
- If a seed shows a false negative, create variations that test
  the boundaries of what gets caught
- Combine patterns: e.g., TypedDict + Protocol, ParamSpec + classmethod
"""

Every example the LLM produces has to reference a seed_issue (something like python/mypy#12345). Anything without one gets skipped during filtering.

Fallback prompt

If there are no seeds at all — --no-github, or GitHub was just unreachable — it falls back to build_expert_prompt, which drops the real issue examples and uses the pattern descriptions from patterns.py instead.

Rotating through seeds

So you don’t get the same handful of seeds every batch:

start_idx = (attempt - 1) * 3 % len(seed_examples)
batch_seeds = seed_examples[start_idx:start_idx + 5]

Detecting a disagreement

results = run_all_checkers(example.code)
# {"mypy": CheckerResult("ok", ...), "pyrefly": CheckerResult("error", ...), ...}

def has_disagreement(results):
    statuses = [r.status for r in results.values()]
    return len(set(statuses)) > 1

If all four agree — either all ok or all error — the example goes to refinement instead of getting kept.

Refinement

A non-divergent example gets sent back to the LLM with the actual checker output attached, asking for a minimal edit that makes a checker disagree:

def build_refinement_prompt(code, checker_results, seed_example):
    return f"""
    The following Python code was tested with all 4 type checkers
    but they ALL AGREED — meaning this is NOT a useful divergence example.

    ## ACTUAL CHECKER RESULTS (all agree):
    {results_text}

    ## YOUR TASK:
    Modify this code MINIMALLY to create a REAL divergence where at least
    one checker disagrees with the others.
    """

Up to --max-refinements tries (default 2). If a refined version diverges, it shows up in the output marked DISAGREEMENT (refined).

Flags that control this

Flag Effect
--num-examples N stop once N disagreements are found
--batch-size N examples generated per LLM call
--max-attempts N generation rounds before giving up
--max-refinements N refinement passes per non-divergent example
--no-github skip seed mining, generate from patterns only
--model command-a-03-2025 a more capable model, usually a better hit rate