|
| 1 | +"""Does ONE example per class generalize to unseen bugs of that class? |
| 2 | +
|
| 3 | +For each class: teach fluidfix from exactly ONE worked example (a register() |
| 4 | +transform written the way that one example would naturally imply), then test |
| 5 | +on N DIFFERENT held-out bugs of the same class. Each held-out case is a real |
| 6 | +tiny module + pytest test: inject the bug, confirm the suite catches it, run |
| 7 | +fluidfix, check the suite goes green AND the line is byte-exact to intended. |
| 8 | +
|
| 9 | +The honest question this answers: for which classes is the repair a pure |
| 10 | +function of the broken line (generalizes from one example) vs. one that needs |
| 11 | +information the line doesn't carry (does not)? |
| 12 | +""" |
| 13 | +import os, re, shutil, sys, tempfile |
| 14 | +sys.path.insert(0, "/Users/kanchetidevieswar/neo/fluidfix/src") |
| 15 | +from fluidfix import Oracle, build_packet, MechanicalObserver, repair, KINDS, ACTS |
| 16 | +from fluidfix.acts import register |
| 17 | + |
| 18 | +PY = sys.executable |
| 19 | + |
| 20 | +def run_case(kind, module_src, intended_line, buggy_line, test_src): |
| 21 | + """Return 'exact' | 'green-only' | 'refused' | 'setup-fail'.""" |
| 22 | + d = tempfile.mkdtemp() |
| 23 | + try: |
| 24 | + fixed = module_src.replace("###LINE###", intended_line) |
| 25 | + broken = module_src.replace("###LINE###", buggy_line) |
| 26 | + open(os.path.join(d, "mod.py"), "w").write(broken) |
| 27 | + open(os.path.join(d, "test_mod.py"), "w").write(test_src) |
| 28 | + oracle = Oracle(d, python=PY) |
| 29 | + if oracle.green(): # bug must actually break the suite |
| 30 | + return "setup-fail" |
| 31 | + packet = build_packet(oracle, "mod.py", coverage_target="mod") |
| 32 | + if packet is None: |
| 33 | + return "setup-fail" |
| 34 | + obs = MechanicalObserver().observe([packet])[0] |
| 35 | + res = repair(oracle, "mod.py", obs) |
| 36 | + if not res.repaired: |
| 37 | + return "refused" |
| 38 | + return "exact" if res.new_line.strip() == intended_line.strip() else "green-only" |
| 39 | + finally: |
| 40 | + shutil.rmtree(d, ignore_errors=True) |
| 41 | + |
| 42 | +# Each class: (register args) + a list of held-out cases (module, intended, buggy, test) |
| 43 | +def M(body): # module template with a ###LINE### slot |
| 44 | + return body |
| 45 | + |
| 46 | +CASES = {} |
| 47 | + |
| 48 | +# ---- classes where the repair is a PURE FUNCTION OF THE LINE (should generalize) |
| 49 | +CASES["cmp-strictness"] = ( |
| 50 | + (0, "strictness", "a comparison with wrong strictness", |
| 51 | + re.compile(r"[<>]=?"), |
| 52 | + lambda l, o: (l.replace(">=", ">", 1) if ">=" in l else l.replace("<=", "<", 1) |
| 53 | + if "<=" in l else l.replace(">", ">=", 1) if ">" in l |
| 54 | + else l.replace("<", "<=", 1))), |
| 55 | + [ |
| 56 | + ("def f(x, t):\n ###LINE###\n return 1\n return 0\n", |
| 57 | + "if x > t:", "if x >= t:", |
| 58 | + "from mod import f\ndef test():\n assert f(5, 5) == 0\n"), |
| 59 | + ("def g(a, b):\n ###LINE###\n return 'hi'\n return 'lo'\n", |
| 60 | + "if a <= b:", "if a < b:", |
| 61 | + "from mod import g\ndef test():\n assert g(5, 5) == 'hi'\n"), |
| 62 | + ("def h(n):\n ###LINE###\n return 'big'\n return 'ok'\n", |
| 63 | + "if n > 100:", "if n >= 100:", |
| 64 | + "from mod import h\ndef test():\n assert h(100) == 'ok'\n"), |
| 65 | + ]) |
| 66 | + |
| 67 | +CASES["additive-flip"] = ( |
| 68 | + (1, "additive", "a + that should be - or vice versa", |
| 69 | + re.compile(r"\s[-+]\s"), |
| 70 | + lambda l, o: (l.replace(" + ", " - ", 1) if " + " in l else l.replace(" - ", " + ", 1))), |
| 71 | + [ |
| 72 | + ("def f(a, b):\n ###LINE###\n", |
| 73 | + "return a - b", "return a + b", |
| 74 | + "from mod import f\ndef test():\n assert f(10, 3) == 7\n"), |
| 75 | + ("def tax(p, t):\n ###LINE###\n", |
| 76 | + "return p + t", "return p - t", |
| 77 | + "from mod import tax\ndef test():\n assert tax(100, 5) == 105\n"), |
| 78 | + ("def bal(s, w):\n ###LINE###\n", |
| 79 | + "return s - w", "return s + w", |
| 80 | + "from mod import bal\ndef test():\n assert bal(50, 20) == 30\n"), |
| 81 | + ]) |
| 82 | + |
| 83 | +CASES["get-default"] = ( |
| 84 | + (2, "get-default", "a .get(key) missing its default", |
| 85 | + re.compile(r"\.get\((\"[^\"]+\"|'[^']+')\)"), |
| 86 | + lambda l, o: re.sub(r"\.get\((\"[^\"]+\"|'[^']+')\)", r".get(\1, 0)", l)), |
| 87 | + [ |
| 88 | + ("def total(d):\n ###LINE###\n", |
| 89 | + 'return d.get("x", 0) + 1', 'return d.get("x") + 1', |
| 90 | + "from mod import total\ndef test():\n assert total({}) == 1\n"), |
| 91 | + ("def s(cfg):\n ###LINE###\n", |
| 92 | + 'return cfg.get("n", 0) * 2', 'return cfg.get("n") * 2', |
| 93 | + "from mod import s\ndef test():\n assert s({}) == 0\n"), |
| 94 | + ("def c(row):\n ###LINE###\n", |
| 95 | + 'return row.get("q", 0) - 1', 'return row.get("q") - 1', |
| 96 | + "from mod import c\ndef test():\n assert c({}) == -1\n"), |
| 97 | + ]) |
| 98 | + |
| 99 | +CASES["swapped-return-operands"] = ( |
| 100 | + (3, "swap", "return a OP b with operands swapped", |
| 101 | + re.compile(r"^\s*return\s+.*\s(?://|[-+*/])\s"), |
| 102 | + lambda l, o: (lambda m: l if not m else f"{m.group(1)}{m.group(4)}{m.group(3)}{m.group(2)}")( |
| 103 | + re.match(r"^(\s*return\s+)(.*?)(\s(?://|[-+*/])\s)(.*)$", l))), |
| 104 | + [ |
| 105 | + ("def d(a, b):\n ###LINE###\n", |
| 106 | + "return a / b", "return b / a", |
| 107 | + "from mod import d\ndef test():\n assert d(10, 2) == 5\n"), |
| 108 | + ("def sub(x, y):\n ###LINE###\n", |
| 109 | + "return x - y", "return y - x", |
| 110 | + "from mod import sub\ndef test():\n assert sub(9, 4) == 5\n"), |
| 111 | + ]) |
| 112 | + |
| 113 | +# ---- classes where the fix needs info NOT ON THE LINE (should NOT generalize) |
| 114 | +# taught from ONE example that implies a specific replacement: |
| 115 | +CASES["wrong-variable"] = ( |
| 116 | + # the example was: return self.name -> return self.email |
| 117 | + # so the most a single example teaches is "return .email instead" |
| 118 | + (4, "wrong-var", "the returned attribute is the wrong one", |
| 119 | + re.compile(r"return \w+\.\w+"), |
| 120 | + lambda l, o: re.sub(r"(return \w+)\.\w+", r"\1.email", l)), |
| 121 | + [ |
| 122 | + # held-out bug #1: should be .id, not .email |
| 123 | + ("class U:\n def __init__(s):\n s.id=7; s.name='a'\ndef f(u):\n ###LINE###\n", |
| 124 | + "return u.id", "return u.name", |
| 125 | + "from mod import U, f\ndef test():\n assert f(U()) == 7\n"), |
| 126 | + # held-out bug #2: should be .total, not .email |
| 127 | + ("class R:\n def __init__(s):\n s.total=99; s.count=1\ndef g(r):\n ###LINE###\n", |
| 128 | + "return r.total", "return r.count", |
| 129 | + "from mod import R, g\ndef test():\n assert g(R()) == 99\n"), |
| 130 | + ]) |
| 131 | + |
| 132 | +CASES["wrong-constant"] = ( |
| 133 | + # the example was: timeout = 30 -> timeout = 60. One example implies "= 60". |
| 134 | + (5, "wrong-const", "a constant set to the wrong value", |
| 135 | + re.compile(r"=\s*\d+\s*$"), |
| 136 | + lambda l, o: re.sub(r"=\s*\d+\s*$", "= 60", l)), |
| 137 | + [ |
| 138 | + ("def f():\n ###LINE###\n return retries\n", |
| 139 | + "retries = 5", "retries = 3", |
| 140 | + "from mod import f\ndef test():\n assert f() == 5\n"), |
| 141 | + ("def g():\n ###LINE###\n return size\n", |
| 142 | + "size = 1024", "size = 512", |
| 143 | + "from mod import g\ndef test():\n assert g() == 1024\n"), |
| 144 | + ]) |
| 145 | + |
| 146 | +CASES["missing-guard"] = ( |
| 147 | + # example: a function needed `if n == 0: return 0` before dividing. |
| 148 | + # one example implies inserting that specific guard. |
| 149 | + (6, "missing-guard", "a division needs a zero-guard", |
| 150 | + re.compile(r"return .+ / \w+\s*$"), |
| 151 | + lambda l, o: (lambda ind: f"{ind}if n == 0:\n{ind} return 0\n{l}")( |
| 152 | + l[:len(l) - len(l.lstrip())])), |
| 153 | + [ |
| 154 | + # held-out: the denominator is 'count', not 'n' — the taught guard checks the wrong name |
| 155 | + ("def rate(total, count):\n ###LINE###\n", |
| 156 | + " if count == 0:\n return 0\n return total / count", |
| 157 | + " return total / count", |
| 158 | + "from mod import rate\ndef test():\n assert rate(10, 0) == 0 and rate(10, 2) == 5\n"), |
| 159 | + ]) |
| 160 | + |
| 161 | +if __name__ == "__main__": |
| 162 | + print(f"{'class':26s} {'held-out':9s} {'exact':6s} {'green':6s} {'refused':8s} verdict") |
| 163 | + print("-" * 78) |
| 164 | + for name, (reg, cases) in CASES.items(): |
| 165 | + # reset registry to shipped state, then teach THIS class from its one example |
| 166 | + for k in list(KINDS): |
| 167 | + if k >= 4: KINDS.pop(k, None) |
| 168 | + for a in list(ACTS): |
| 169 | + if a >= 9: ACTS.pop(a, None) |
| 170 | + register(*reg) |
| 171 | + outcomes = [run_case(name, m, intended, buggy, t) for (m, intended, buggy, t) in cases] |
| 172 | + ex = outcomes.count("exact"); gr = outcomes.count("green-only") |
| 173 | + rf = outcomes.count("refused") + outcomes.count("setup-fail") |
| 174 | + n = len(outcomes) |
| 175 | + verdict = "GENERALIZES" if ex == n else ("partial" if ex + gr > 0 else "does NOT generalize") |
| 176 | + print(f"{name:26s} {n:<9d} {ex:<6d} {gr:<6d} {rf:<8d} {verdict}") |
0 commit comments