File size: 9,033 Bytes
04eb996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef9d6c9
 
 
 
04eb996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef9d6c9
04eb996
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef9d6c9
04eb996
 
 
 
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
"""Select foresight-swe-mini and reproduce every number on the card.

    python build.py verified.jsonl

verified.jsonl is data/verified.jsonl from shaswat123/Foresight-SWE. Writes
data/tasks.jsonl, data/leaderboard.csv and data/metrics.json. Upstream publishes
totals and per-model breakdowns only, so accuracy is simulated, not measured.
"""
import collections as C, csv, json, math, sys
import numpy as np
from scipy.stats import spearmanr

rows = [json.loads(l) for l in open(sys.argv[1])]
sizes = json.load(open("data/image_sizes.json"))
L = lambda v: json.loads(v) if isinstance(v, str) else (v or [])
N, K = len(rows), 15
col = lambda k: np.array([r[k] for r in rows])
ids, repo, lang, d1, d2, fm, tt, sec = (col(k) for k in
    ("instance_id", "repo", "language", "pr1_difficulty", "difficulty", "failure_mode", "pr1_task_type", "security"))
sev = np.array(["minor" if r["severity"] == "minor" else "major" for r in rows])
count = lambda k: np.array([len(L(r[k])) for r in rows])
p2p, f2p, ant = count("PASS_TO_PASS"), count("FAIL_TO_PASS"), count("ANTICIPATION_FAIL_TO_PASS")
patch_kb = np.array([len(r["patch"]) / 1e3 for r in rows])
tag = lambda r, k: sizes[r[k].split(":")[1]] / 1e9
image_gb = np.array([tag(r, "docker_image") for r in rows])
both_images_gb = image_gb + np.array([tag(r, "pr2base_image") for r in rows])

# Paper, Tables D.1 and D.2, in points:
# PR-1 easy/medium/hard, PR-2 easy/medium/hard, minor/major, loud/silent.
MODELS = ["Qwen3.8 Flash Next", "GLM-5.3 Flash", "DeepSeek-V4 Flash", "GPT-5.6 Sol", "GPT-5.6 Terra",
          "GPT-5.6 Luna", "Claude Opus 5", "Claude Sonnet 5", "Qwen3.5-397B-A17B", "Qwen3.8-27B", "Qwen3.5-27B"]
RESOLVE = np.array([
    [95.7, 97.4, 85.2, 91.2, 98.1, 85.7, 91.6, 92.2, 92.8, 91.5], [76.6, 80.5, 83.0, 73.6, 90.4, 91.4, 79.5, 81.4, 80.7, 80.6],
    [68.1, 59.7, 52.3, 55.2, 69.2, 54.3, 59.0, 58.1, 61.4, 56.6], [93.6, 81.8, 81.8, 80.0, 90.4, 91.4, 88.0, 82.2, 90.4, 80.6],
    [85.1, 79.2, 64.8, 76.8, 71.2, 71.4, 77.1, 72.9, 83.1, 69.0], [87.2, 76.6, 64.8, 72.0, 76.9, 77.1, 74.7, 73.6, 78.3, 71.3],
    [85.1, 74.0, 77.3, 74.4, 88.5, 74.3, 77.1, 78.3, 78.3, 77.5], [76.6, 57.1, 53.4, 56.8, 65.4, 62.9, 62.7, 58.1, 65.1, 56.6],
    [46.8, 23.4, 6.8, 22.4, 23.1, 17.1, 24.1, 20.2, 24.1, 20.2], [53.2, 27.3, 9.1, 25.6, 28.8, 20.0, 25.3, 25.6, 26.5, 24.8],
    [38.3, 23.4, 3.4, 17.6, 25.0, 11.4, 18.1, 18.6, 16.9, 19.4]]) / 100
FRR = np.array([
    [27.7, 31.2, 8.0, 28.8, 15.4, 0.0, 20.5, 20.9, 25.3, 17.8], [17.0, 14.3, 5.7, 12.0, 15.4, 2.9, 13.3, 10.1, 15.7, 8.5],
    [21.3, 29.9, 8.0, 21.6, 23.1, 2.9, 24.1, 15.5, 24.1, 15.5], [21.3, 23.4, 12.5, 24.8, 15.4, 0.0, 18.1, 18.6, 20.5, 17.1],
    [25.5, 16.9, 8.0, 20.8, 9.6, 2.9, 18.1, 13.2, 18.1, 13.2], [23.4, 23.4, 6.8, 20.8, 17.3, 0.0, 18.1, 15.5, 20.5, 14.0],
    [38.3, 26.0, 15.9, 28.0, 30.8, 2.9, 33.7, 18.6, 32.5, 19.4], [21.3, 20.8, 11.4, 19.2, 19.2, 5.7, 22.9, 13.2, 21.7, 14.0],
    [10.6, 11.7, 1.1, 8.8, 5.8, 2.9, 10.8, 4.7, 9.6, 5.4], [12.8, 10.4, 2.3, 8.8, 7.7, 2.9, 9.6, 6.2, 10.8, 5.4],
    [10.6, 9.1, 1.1, 7.2, 7.7, 0.0, 7.2, 5.4, 4.8, 7.0]]) / 100
FULL_FRR = np.array([20.75, 11.32, 18.87, 18.40, 15.09, 16.51, 24.53, 16.98, 7.08, 7.55, 6.13])
FULL_RESOLVE = np.array([91.98, 80.66, 58.49, 84.43, 74.53, 74.06, 77.83, 59.91, 21.70, 25.47, 18.40])

X = np.array([d1 == "easy", d1 == "medium", d1 == "hard", d2 == "easy", d2 == "medium", d2 == "hard",
              sev == "minor", sev == "major", fm == "loud", fm == "silent"], float).T
XX = np.c_[np.ones(N), X[:, [0, 1, 3, 4, 6, 8]]]
sig = lambda z: 1 / (1 + np.exp(-z))


def fit(margin):
    """Additive logit whose per-group means match one model's table row."""
    counts = margin * X.sum(0)
    t = np.r_[counts[:3].sum(), counts[[0, 1, 3, 4, 6, 8]]]
    b = np.zeros(XX.shape[1])
    b[0] = math.log((t[0] / N + 1e-3) / (1 - t[0] / N + 1e-3))
    for _ in range(200):
        p = sig(XX @ b)
        H = (XX * (p * (1 - p))[:, None]).T @ XX + 1e-3 * np.eye(len(b))
        b += 0.5 * np.linalg.solve(H, t - XX.T @ p)
    return b


bF = np.array([fit(m) for m in FRR])
pF, pR = sig(XX @ bF.T), sig(XX @ np.array([fit(m) for m in RESOLVE]).T)

feats = np.c_[X, *[(lang == l) * 1. for l in ("python", "javascript", "typescript", "go")],
              (tt == "feature") * 1., (tt == "bug") * 1., sec * 1.,
              (repo == "sveltejs/svelte") * 1., (repo == "django/django") * 1.]
Z = np.c_[np.log10(p2p + 1), np.log10(patch_kb + 1), np.log10(ant + 1)]


def objective(S):
    return (((feats[S].mean(0) - feats.mean(0)) ** 2).sum()
            + ((pF[S].mean(0) - pF.mean(0)) ** 2).sum() * 8 + ((pR[S].mean(0) - pR.mean(0)) ** 2).sum() * 2
            + (((Z[S].mean(0) - Z.mean(0)) / Z.std(0)) ** 2).sum() * .02)


def search(pool, seed, iters=25000):
    r = np.random.default_rng(seed)
    S = list(r.choice(pool, K, replace=False))
    over = lambda s: any(v > max(1, math.ceil((repo == rp).mean() * K)) for rp, v in C.Counter(repo[s]).items())
    v, T = objective(S) + over(S), .002
    for _ in range(iters):
        i, o = r.integers(K), r.choice(pool)
        if o in S:
            continue
        S2 = S.copy()
        S2[i] = o
        v2 = objective(S2) + over(S2)
        if v2 < v or r.random() < math.exp(-(v2 - v) / max(T, 1e-9)):
            S, v = S2, v2
        T *= .9995
    return sorted(S), v


# Keep runs short: leave out the tasks that are slow to grade.
pool = np.where((p2p <= 2000) & (ant <= 20) & (f2p <= 100) & (patch_kb <= 60) & (both_images_gb <= 3.5))[0]
S = min((search(pool, s) for s in range(8)), key=lambda x: x[1])[0]
# Removed after selection: upstream's PR-1 install recipe resolves numpy 2 before
# building pandas, so the build fails and no PR-1 test can run, even for the gold patch.
BROKEN = {"pandas__49324-49704"}
S = [i for i in S if ids[i] not in BROKEN]


def calibrate(lin, target):
    a, b = -8., 8.
    for _ in range(40):
        c = (a + b) / 2
        a, b = (c, b) if sig(lin + c).mean() < target else (a, c)
    return c


def simulate(sd, reps=500, seed=7):
    """Shared per-task difficulty (sd, logits) plus per-model noise, on top of the fit."""
    rng, out = np.random.default_rng(seed), C.defaultdict(list)
    for _ in range(reps):
        lin = XX @ bF.T + rng.normal(0, sd, N)[:, None] + rng.normal(0, .5, (N, 11))
        for m in range(11):
            lin[:, m] += calibrate(lin[:, m], FULL_FRR[m] / 100)
        Y = (rng.random((N, 11)) < sig(lin)) * 1.
        full = Y.mean(0)
        designs = {f"random {k}": rng.choice(N, k, replace=False) for k in (10, len(S), 20, 30, 60, 100)}
        designs["foresight-swe-mini"] = np.array(S)
        for name, D in designs.items():
            e = Y[D].mean(0)
            out[name, "error_points"].append(np.abs(e - full).mean() * 100)
            for gap in (.05, .10):
                pairs = [(a, b) for a in range(11) for b in range(a + 1, 11) if abs(full[a] - full[b]) >= gap]
                out[name, f"pairs_{int(gap * 100)}pt"].append(
                    sum((e[a] - e[b]) * (full[a] - full[b]) > 0 for a, b in pairs) / max(len(pairs), 1) * 100)
    table = C.defaultdict(dict)
    for (name, metric), v in out.items():
        table[name][metric] = round(float(np.mean(v)), 1)
    return table


keep = ("instance_id", "repo", "language", "earlier_pr", "later_pr", "pr1_difficulty", "difficulty", "severity",
        "failure_mode", "pr1_task_type", "security", "docker_image")
with open("data/tasks.jsonl", "w") as f:
    for i in S:
        row = {k: rows[i][k] for k in keep}
        row.update(n_fail_to_pass=int(f2p[i]), n_anticipation=int(ant[i]), n_pass_to_pass=int(p2p[i]),
                   image_gb=round(float(image_gb[i]), 2))
        f.write(json.dumps(row) + "\n")
with open("data/leaderboard.csv", "w", newline="") as f:
    w = csv.writer(f)
    w.writerow(["model", "resolve_all_212", "frr_all_212", "predicted_frr_on_mini"])
    for m, name in enumerate(MODELS):
        w.writerow([name, FULL_RESOLVE[m], FULL_FRR[m], round(float(pF[S].mean(0)[m] * 100), 1)])
mix = dict(language=lang, pr1_difficulty=d1, pr2_difficulty=d2, severity=sev, detectability=fm, task_type=tt, security=sec)
metrics = dict(
    n_pool=int(len(pool)), image_gb=round(float(image_gb[S].sum()), 1), max_pass_to_pass=int(p2p[S].max()),
    repos=len(set(repo[S])),
    predicted_frr_max_error=round(float(np.abs(pF[S].mean(0) - pF.mean(0)).max() * 100), 1),
    predicted_resolve_max_error=round(float(np.abs(pR[S].mean(0) - pR.mean(0)).max() * 100), 1),
    variance_explained_frr=[round(float(x), 3) for x in pF.var(0) / (pF.mean(0) * (1 - pF.mean(0)))],
    spearman_resolve_frr_all=round(float(spearmanr(FULL_RESOLVE, FULL_FRR)[0]), 2),
    mix={k: dict(mini=dict(C.Counter(str(x) for x in v[S])),
                 proportional={str(a): round(b / N * len(S), 1) for a, b in C.Counter(v.tolist()).items()})
         for k, v in mix.items()},
    simulation={str(sd): simulate(sd) for sd in (.5, 1, 1.5)})
json.dump(metrics, open("data/metrics.json", "w"), indent=1)
print(" ".join(str(ids[i]) for i in S))