{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.12.14"
    },
    "execution": {
      "method": "Python subprocess, exact code cell source",
      "deterministic_replay_passed": true
    },
    "runtime": {
      "packages": [],
      "local_assets": []
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "id": "question",
      "metadata": {},
      "source": [
        "# A patch needs two test partitions\n",
        "Fixing the reported issue and preserving ordinary behavior are separate obligations. Fifteen repairs make the difference visible.\n",
        "\nSynthetic reproduction of F2P/P2P acceptance logic; not a SWE-bench result.\n"
      ]
    },
    {
      "cell_type": "code",
      "id": "experiment",
      "metadata": {},
      "execution_count": 1,
      "source": [
        "\"\"\"A patch needs two test partitions\n",
        "Deterministic, standard-library experiment. RESULTS contains the full data.\n",
        "\"\"\"\n",
        "tasks = {\n",
        "    'absolute': (\n",
        "        [(-2, 2), (-3, 3)], [(0, 0), (2, 2), (3, 3)],\n",
        "        [lambda x: x, lambda x: 2, lambda x: -x,\n",
        "         lambda x: 2 if x == -2 else x, abs]),\n",
        "    'clamp': (\n",
        "        [(-1, 0), (-5, 0)], [(0, 0), (5, 5), (10, 10), (15, 10)],\n",
        "        [lambda x: min(x, 10), lambda x: 0, lambda x: max(0, x),\n",
        "         lambda x: 0 if x == -1 else min(x, 10),\n",
        "         lambda x: max(0, min(x, 10))]),\n",
        "    'average': (\n",
        "        [([], 0)], [([2, 4], 3), ([1], 1), ([-2, 4], 1)],\n",
        "        [lambda x: sum(x) / len(x), lambda x: 0, lambda x: sum(x),\n",
        "         lambda x: x[0] if x else 0, lambda x: sum(x) / (len(x) or 1)]),\n",
        "}\n",
        "\n",
        "def passes(fn, case):\n",
        "    value, expected = case\n",
        "    try:\n",
        "        return fn(value) == expected\n",
        "    except Exception:\n",
        "        return False\n",
        "\n",
        "resolved = 0\n",
        "for task, (f2p, p2p, candidates) in tasks.items():\n",
        "    for index, fn in enumerate(candidates):\n",
        "        fixed = sum(passes(fn, case) for case in f2p)\n",
        "        kept = sum(passes(fn, case) for case in p2p)\n",
        "        ok = fixed == len(f2p) and kept == len(p2p)\n",
        "        resolved += ok\n",
        "        print(task, index, f'F2P={fixed}/{len(f2p)}',\n",
        "              f'P2P={kept}/{len(p2p)}', 'resolved=' + str(ok))\n",
        "print('resolved_candidates', resolved, 'of', 15)\n",
        "assert resolved == 3\n",
        "\n",
        "\n",
        "labels={'absolute':['identity','constant 2','negate','special-case -2','absolute value'],'clamp':['upper bound only','constant 0','lower bound only','special-case -1','both bounds'],'average':['divide by length','constant 0','sum only','first element','empty-aware mean']}\n",
        "rows=[]\n",
        "for task,(f2p,p2p,candidates) in tasks.items():\n",
        "    for i,fn in enumerate(candidates):\n",
        "        results=[dict(partition=part,input=value,expected=expected,passed=passes(fn,(value,expected))) for part,caseset in [('F2P',f2p),('P2P',p2p)] for value,expected in caseset]\n",
        "        rows.append(dict(task=task,candidate=i,name=labels[task][i],tests=results,resolved=all(r['passed'] for r in results)))\n",
        "held_out=[('absolute',-0.5,0.5),('clamp',-0.5,0),('clamp',10.5,10),('clamp',3.5,3.5),('average',[-4,-2],-3)]\n",
        "for task,value,expected in held_out: assert passes(tasks[task][2][-1],(value,expected))\n",
        "RESULTS=dict(candidates=rows,held_out=[dict(task=t,input=v,expected=e,passed=True) for t,v,e in held_out])\n",
        "print('Held-out boundary checks:',len(held_out),'passed')\n"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "absolute 0 F2P=0/2 P2P=3/3 resolved=False\n",
            "absolute 1 F2P=1/2 P2P=1/3 resolved=False\n",
            "absolute 2 F2P=2/2 P2P=1/3 resolved=False\n",
            "absolute 3 F2P=1/2 P2P=3/3 resolved=False\n",
            "absolute 4 F2P=2/2 P2P=3/3 resolved=True\n",
            "clamp 0 F2P=0/2 P2P=4/4 resolved=False\n",
            "clamp 1 F2P=2/2 P2P=1/4 resolved=False\n",
            "clamp 2 F2P=2/2 P2P=3/4 resolved=False\n",
            "clamp 3 F2P=1/2 P2P=4/4 resolved=False\n",
            "clamp 4 F2P=2/2 P2P=4/4 resolved=True\n",
            "average 0 F2P=0/1 P2P=3/3 resolved=False\n",
            "average 1 F2P=1/1 P2P=0/3 resolved=False\n",
            "average 2 F2P=1/1 P2P=1/3 resolved=False\n",
            "average 3 F2P=1/1 P2P=1/3 resolved=False\n",
            "average 4 F2P=1/1 P2P=3/3 resolved=True\n",
            "resolved_candidates 3 of 15\n",
            "Held-out boundary checks: 5 passed\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "import base64\n",
        "print('__NOTEBOOK_IMAGE__svg+xml:' + base64.b64encode(Path('figure.svg').read_bytes()).decode())\n"
      ]
    }
  ]
}
