== Listing 7.1: the sandboxed runner on well-behaved and badly-behaved programs ==
program             status                outcome per input (ok:value | err:Name | timeout)
correct             ok                    ok:6 ok:0 ok:3   passed 3/3
wrong on one input  ok                    ok:6 ok:1 ok:3   passed 2/3
raises on []        ok                    ok:6 err:IndexError ok:3   passed 2/3
infinite loop       ok                    timeout timeout timeout   passed 0/3
memory bomb         ok                    err:MemoryError err:MemoryError err:MemoryError   passed 0/3
import os           ok                    err:ImportError err:ImportError err:ImportError   passed 0/3
open a file         ok                    err:NameError err:NameError err:NameError   passed 0/3
print               ok                    err:NameError err:NameError err:NameError   passed 0/3
syntax error        load_error:SyntaxError   passed 0/3
busy but finite     ok                    ok:6 ok:0 ok:3   passed 3/3
limits: 512 MB address space, 10 s CPU, no file writes, 0.1 s per call here, 20 s wall clock
== Listing 7.2: the synthetic task pool and the stand-in model ==
tasks 36 (4 templates x 9 parameter draws), candidate programs per task 15, pass/fail checks per candidate 4104
example task 2 (max_default): Return the maximum of 3*x over xs, or 6 if that is larger or xs is empty.
reference solution:
    def f(xs):
        r = 6
        for x in xs:
            if 3 * x > r:
                r = 3 * x
        return r
non-reference programs that match the reference on all 4000 hidden inputs (equivalent mutants): 7
type      programs  mean eps  min eps  max eps  pass small k=4  pass small k=8  pass random k=4  pass random k=8
param          144     0.599   0.1358    0.875           0.292           0.181            0.118            0.062
op              65     0.550   0.1873    0.889           0.338           0.231            0.169            0.077
guard          216     0.032   0.0005    0.121           1.000           1.000            0.894            0.810
crash           36     0.111   0.1020    0.121           0.333           0.139            0.556            0.444
cheat           36     0.750   0.4845    1.000           1.000           0.194            0.028            0.000
stand-in model: mean probability of the reference program 0.406; mean probability of a hidden-correct program (true pass@1) 0.413
true pass@1 per task: min 0.005, median 0.465, max 0.917
hidden-correct (task, program) pairs that also pass all 40 small visible tests: 43 of 43
== Listing 7.3: pass@k with intervals; bias and variance of the estimator ==
one benchmark run: 36 tasks, n=20 samples per task (counts simulated from the stand-in model)
  k    true  estimate      se      95% interval  plug-in
  1  0.4133    0.4069  0.0542   [0.3007, 0.5132]   0.4069
  5  0.7168    0.6956  0.0650   [0.5681, 0.8231]   0.6808
 10  0.7969    0.7736  0.0630   [0.6502, 0.8970]   0.7495
replications: 4000 repeated benchmark runs; tasks fixed (scenario A) or resampled with replacement (scenario B)
  k  bias unb.  bias plug    var emp var formula  cover A  cover B  var B emp var B form
  1   -0.00000   -0.00000   0.000206    0.000210    1.000    0.939   0.002740   0.002752
  5    0.00025   -0.01550   0.000394    0.000405    1.000    0.940   0.003820   0.003820
 10   -0.00073   -0.02518   0.000685    0.000681    0.999    0.931   0.003364   0.003435
per-task estimator sd at k=10, n=20, for p = 0.05, 0.2, 0.5: 0.3250, 0.1576, 0.0064
same at n=200: 0.0976, 0.0389, 0.0008
== Listing 7.4: the verifier-in-the-loop sampler against Theorem 7.2 ==
suite 'small', k=4: mean P(wrong sample passes) = 0.497; mean precision ceiling p/(p+(1-p)alpha) = 0.523
   n  correct pred  correct meas  wrong pred  wrong meas  samples pred  samples meas
   1        0.4133        0.4172      0.2825      0.2832         1.000         1.000
   4        0.5140        0.5215      0.4264      0.4174         1.546         1.567
  16        0.5232        0.5134      0.4752      0.4854         1.751         1.738
  64        0.5234        0.5172      0.4766      0.4828         1.759         1.770
suite 'random', k=8: mean P(wrong sample passes) = 0.277; mean precision ceiling p/(p+(1-p)alpha) = 0.617
   n  correct pred  correct meas  wrong pred  wrong meas  samples pred  samples meas
   1        0.4133        0.4172      0.1706      0.1699         1.000         1.000
   4        0.5864        0.5796      0.3256      0.3315         1.775         1.769
  16        0.6160        0.6138      0.3819      0.3844         2.066         2.045
  64        0.6166        0.6106      0.3834      0.3894         2.075         2.080
each row averages 36 tasks x 300 simulated loops
== Listing 7.5: test suites as statistical guarantees (Theorem 7.3) ==
(a) P(wrong program passes k iid tests from the real input distribution): predicted (1-eps)^k vs simulated from 1,500 random suites per program
       eps bin  programs   k  predicted  simulated
  [0.00, 0.01)        72   4     0.9928     0.9929
  [0.01, 0.05)       108   4     0.9062     0.9063
  [0.05, 0.20)       104   4     0.5691     0.5701
  [0.20, 1.00)       213   4     0.0487     0.0493
  [0.00, 0.01)        72  16     0.9715     0.9718
  [0.01, 0.05)       108  16     0.6838     0.6837
  [0.05, 0.20)       104  16     0.1177     0.1165
  [0.20, 1.00)       213  16     0.0017     0.0015
(b) residual-error bound for the pool (union over the 15 candidates), delta = 0.05
   k    bound  #wrong programs above bound  violation rate   (rule of thumb ln(15/0.05)/k = 5.70/k)
   8    0.510                          184         0.00065
  16    0.300                          191         0.00015
  32    0.163                          241         0.00074
  64    0.085                          317         0.00085
worst-case-like pool: 15 independent programs each with eps exactly at the k=16 bound 0.300: each passes with probability 0.00333, some program passes in 0.0491 of 200,000 suites (delta = 0.05)
tests needed so that an eps=0.01 program passes with probability <= 0.05: 299; with 15 candidates in the union: 568
(c) the same guard programs under two test distributions, k = 8
   mean eps on real inputs 0.0315; on the small-example distribution 0.0000
   P(pass 8 iid tests): real distribution 0.8047; small-example distribution 1.0000
(d) mutation score (share of wrong programs rejected) of the first k tests, averaged over tasks
   k  small examples  random inputs
   1           0.167          0.293
   2           0.265          0.408
   4           0.341          0.513
   8           0.460          0.587
  16           0.488          0.686
  32           0.498          0.758
  40           0.498          0.782
== Listing 7.6: cost of a loop with retries (Theorem 7.4) and when to stop (Theorem 7.5) ==
hypothetical constants: acceptance probability a = 0.2, cost per attempt c = 1.0, cost of giving up L0 = 40.0
sequential loop (stop at first acceptance), cap n: closed form vs 100,000 simulated loops
   n  P(success) form    meas  E[cost] form    meas
   1           0.2000  0.2007        1.0000  1.0000
   5           0.6723  0.6722        3.3616  3.3744
  10           0.8926  0.8935        4.4631  4.4665
  20           0.9885  0.9883        4.9424  4.9576
parallel batch of n samples (cost c n whatever happens): loss and the optimum of Theorem 7.4(c)
   n        0        2        5        8       10       12       15       20
loss   40.000   27.600   18.107   14.711   14.295   14.749   16.407   20.461
optimal n from the formula 10, by brute force 10; continuous approximation (1/lambda) ln(D lambda / c) = 9.81
optimal n as the cost of an attempt c varies (a = 0.2, L0 = 40):  c=0.1: 20, c=0.5: 13, c=1: 10, c=2: 7, c=5: 3, c=10: 0
optimal n as the acceptance probability varies (c = 1, L0 = 40):    a=0.02: 0, a=0.05: 14, a=0.1: 14, a=0.2: 10, a=0.5: 5, a=0.9: 2
sequential loop with a cap: the loss is linear in (1-a)^n, so the best cap is 0 or infinite
   L0 =   3.0: K = L0 - c/a =  -2.00; loss at cap 0, 5, 20, 100 = 3.000, 4.345, 4.977, 5.000
   L0 =  30.0: K = L0 - c/a =  25.00; loss at cap 0, 5, 20, 100 = 30.000, 13.192, 5.288, 5.000
heterogeneous tasks, a ~ Beta(1, 3) (mean 0.25), c = 1.0, D = 40.0: stop after F = 36 failures
   backward-induction optimum 11.2606; loss of the threshold rule 11.2606
   loss of 'stop after F failures': F=0: 40.000, F=2: 25.750, F=5: 18.279, F=36: 11.261, F=72: 11.764, F=180: 13.508, F=200: 13.755
   numerical integration of the threshold rule's loss over a: 11.2606
   Monte Carlo loss of the threshold rule 11.2271 +- 0.0198 (1,000,000 tasks)
   if every task had a = mean = 0.25, D*a = 10.0 > c: the homogeneous model would say 'never stop'
== Listing 7.7: an agent harness with logs and cost accounting ==
one episode on task 15 (true pass@1 0.012), mode 'feedback', visible suite = 4 small examples, cost = 1.0 per fresh sample, 1.5 per repair, 0.05 per test
   step 1: sample -> op     tail-slice                   passed 2/4 accepted=False cumulative cost 1.20
   step 2: repair -> crash  first-entry                  passed 3/4 accepted=False cumulative cost 2.90
   step 3: repair -> guard  len(xs) >= 8                 passed 4/4 accepted=True cumulative cost 4.60
   outcome: accepted program is WRONG on the hidden suite; total cost 4.60
aggregate over 36 tasks x 200 episodes, step cap S (one set of episodes with cap 8 gives every smaller cap by truncation)
suite 'small', k=4
mode        S  correct   +-95%  wrong acc.  gave up    cost  cost/success  theory iid
resample    1   0.4026  0.1007      0.2922   0.3051   1.200         2.980      0.4133
resample    2   0.4762  0.1097      0.3760   0.1478   1.566         3.289      0.4847
resample    4   0.5032  0.1110      0.4360   0.0608   1.852         3.681      0.5140
resample    8   0.5112  0.1111      0.4722   0.0165   2.042         3.995      0.5219
feedback    1   0.4058  0.0990      0.2863   0.3079   1.200         2.957            
feedback    2   0.5071  0.1101      0.4306   0.0624   1.723         3.399            
feedback    4   0.5224  0.1089      0.4776   0.0000   1.832         3.507            
feedback    8   0.5224  0.1089      0.4776   0.0000   1.832         3.507            
suite 'random', k=8
mode        S  correct   +-95%  wrong acc.  gave up    cost  cost/success  theory iid
resample    1   0.4017  0.0990      0.1751   0.4232   1.400         3.485      0.4133
resample    2   0.5157  0.1114      0.2569   0.2274   1.992         3.864      0.5245
resample    4   0.5811  0.1121      0.3282   0.0907   2.502         4.306      0.5864
resample    8   0.6065  0.1109      0.3717   0.0218   2.820         4.649      0.6103
feedback    1   0.3993  0.1000      0.1717   0.4290   1.400         3.506            
feedback    2   0.5647  0.1113      0.3440   0.0913   2.215         3.923            
feedback    4   0.6103  0.1096      0.3876   0.0021   2.416         3.958            
feedback    8   0.6115  0.1096      0.3885   0.0000   2.420         3.957            
== Listing 7.8: RL against a test verifier with a KL penalty reaches the Gibbs policy (Theorem 7.6) ==
task 5: p = P(correct) = 0.2031, a = P(passes 4 small tests) = 0.4804
   beta = 1.0: max |exact gradient ascent - Gibbs| = 2.83e-11
stochastic leave-one-out policy gradient (G = 8 samples per task per step, 4000 steps, lr 0.5), all 36 tasks
  beta   lambda  max |pi - Gibbs|  proxy pass (Gibbs)  true acc (Gibbs)   formula  true acc (trained)
  2.00    1.649            0.0000              0.7679            0.4453    0.4453              0.4453
  1.00    2.718            0.0002              0.8297            0.4699    0.4699              0.4699
  0.50    7.389            0.0011              0.9184            0.5004    0.5004              0.5005
no training: proxy pass 0.6959, true accuracy 0.4133; beta -> 0 limit (rejection sampling) true accuracy 0.5234
== Listing 7.9: RL with weak and strong test suites (leave-one-out advantages, no KL penalty) ==
36 tasks, G = 8, 300 steps, learning rate 0.5, mean over 3 training seeds; 'true' = passes the hidden suite
verifier             proxy pass  true acc   +-95%  mass on cheat  mass on guards  tasks hacked
(before training)        0.6959    0.4133  0.1002         0.0136          0.1582              
small k=1                0.9955    0.5745  0.1339         0.0083          0.0823         0.444
small k=2                0.9944    0.6232  0.1356         0.0049          0.1040         0.361
small k=4                0.9939    0.6631  0.1355         0.0039          0.1965         0.333
small k=8                0.9935    0.7140  0.1304         0.0022          0.1977         0.250
small k=16               0.9933    0.7299  0.1314         0.0021          0.1923         0.250
random k=4               0.9933    0.7308  0.1338         0.0009          0.0983         0.278
random k=16              0.9934    0.8240  0.1166         0.0003          0.0984         0.167
random k=64              0.9930    0.8710  0.1037         0.0003          0.1238         0.111
oracle (hidden suite)     0.9694    0.9694  0.0452         0.0004          0.0109         0.000
'tasks hacked' = share of tasks whose trained policy passes the training suite with probability above 0.9 but is correct with probability below 0.5
== Listing 7.10: repository-level retrieval with BM25 ==
synthetic repository: 200 functions (half snake_case, half camelCase); example function and issue:
   def decodeFieldLayout(self, size):
       '''size name field path raise item'''
       self._window self._packet
   issue: 'decode layout after field report fails window wrong'
tokenizer           recall@1  recall@3  recall@5  recall@10     MRR
whole word             0.015     0.070     0.090      0.175   0.081
split identifiers      0.580     0.805     0.895      0.975   0.714
paired difference in recall@5 (split minus whole): 0.805, 95% bootstrap interval [0.750, 0.860]
hypothetical coupling to the code model: relevant context multiplies the odds of a correct program by 3, irrelevant context by 0.5
   no retrieval: pass@1 = 0.4133; oracle context: 0.5740
   top-1: split identifiers (recall 0.580) pass@1 = 0.4632; whole word (recall 0.015) pass@1 = 0.3142
   top-3: split identifiers (recall 0.805) pass@1 = 0.5226; whole word (recall 0.070) pass@1 = 0.3288
   top-5: split identifiers (recall 0.895) pass@1 = 0.5463; whole word (recall 0.090) pass@1 = 0.3340
== Listing 7.11: tests that miss the intent, and flaky tests ==
task 2: Return the maximum of 3*x over xs, or 6 if that is larger or xs is empty.
a candidate that agrees with the specification except when a rare condition holds:
    def f(xs):
        r = 6
        for x in xs:
            if 3 * x > r:
                r = 3 * x
        if len(xs) >= 6 and xs[0] == xs[-1]:
            r += 1
        return r
passes 8/8 of the first small examples and 40/40 of all of them; fails 73 of 4000 hidden inputs (eps = 0.0182)
   first counterexample: input [8, -9, 9, 7, 3, 2, 8] expected 27
differential testing against the reference with random inputs: tries until the first failure, mean over 2000 runs, vs 1/eps
program                                eps    1/eps  measured
len(xs) >= 8                        0.1112      9.0       9.2
len(xs) >= 6 and xs[0] == xs[-1]    0.0182     54.8      54.8
len(xs) > 0 and max(xs) == 9 and    0.0377     26.5      25.7
flaky tests: each test run on a passing program fails spuriously with probability f; k = 8 random visible tests
     f  reruns  P(passing prog. accepted) form    meas  samples to accept (form)    meas  wrong acc. share
  0.00       1                          1.0000  1.0000                     2.075   2.086            0.3851
  0.00       3                          1.0000  1.0000                     2.075   2.103            0.3833
  0.02       1                          0.8508  0.8478                     2.439   2.444            0.3899
  0.02       3                          0.9967  0.9964                     2.082   2.091            0.3815
  0.10       1                          0.4305  0.4294                     4.820   4.854            0.3853
  0.10       3                          0.8153  0.8133                     2.545   2.531            0.3856
== Listing 7.12: benchmark contamination ==
hypothetical memorization: 12 of 36 tasks leaked into training data; for them the odds of a correct program are multiplied by 8
true pass@1 on leaked tasks: 0.3033 -> 0.5718; unleaked tasks: 0.4684
measured pass@1 (n=20): leaked 0.5833, unleaked 0.4521; unpaired difference 0.1313 +- 0.2225
paired against fresh variants of the same 12 tasks (same difficulty, never seen): difference 0.3000 +- 0.1052
6-gram overlap of task text + reference solution with the training corpus: leaked mean 1.000, unleaked mean 0.724; AUROC 0.958
task statement alone: leaked mean 1.000, unleaked mean 0.713; AUROC 0.958
unleaked tasks with overlap at least 0.99 : 2 of 24
overlap of unleaked tasks by template (the shared template alone produces overlap): filter_sum 0.644, count_cmp 0.686, max_default 0.752, alt_sum 0.805
== Listing 7.13: code that passes the tests and is insecure ==
30 generated functional tests (strings like '26+10+29+41+81'); probe inputs that a safe parser must reject: ['2*3', '10-4']
candidate             tests (restricted)  tests (eval allowed)   probe results   static scan
split and int                      30/30                 30/30         err,err   nothing found
digit loop                         30/30                 30/30         err,err   nothing found
eval                                0/30                 30/30       ok:6,ok:6   ['name:eval']
eval, hidden name                   0/30                 30/30       ok:6,ok:6   nothing found
