diff --git a/.claude/skills/notebooks/SKILL.md b/.claude/skills/notebooks/SKILL.md index 77d21dcb6..e4af201c9 100644 --- a/.claude/skills/notebooks/SKILL.md +++ b/.claude/skills/notebooks/SKILL.md @@ -8,13 +8,17 @@ description: Keep a complex multi-file analysis (bench sweeps, model comparisons The rules and why: `docs/dev/notebooks.md`. The commands: ```bash -node scripts/nb.mjs setup # once: the root .venv from notebooks/pyproject.toml +node scripts/nb.mjs setup # once PER CHECKOUT: its own .venv (never symlink one) node scripts/nb.mjs make notebooks/bench/.ipynb # run make-.py, then execute the notebook node scripts/nb.mjs run notebooks/bench/.ipynb # execute only (outputs written back; exit 1 if a cell raised) NB_REFRESH=1 node scripts/nb.mjs run … # re-extract the pinned inputs from their source node scripts/check-notebooks.mjs # what the hook and CI check ``` +`nb: .venv is …, which is not this checkout's` or `wmlnb.extract: … is outside this checkout` means the environment +came from another clone, so the notebook would read and write that clone's pinned extracts: remove this checkout's +`.venv` and run `node scripts/nb.mjs setup` here. + Inputs: `wmlnb.find_sweep(name)` for a sweep's folder (any clone), `wmlnb.store()` for the bench store on mlbox as DuckDB (views `scores`, `calls`, `price_snapshots`, `box_frames`; pattern: `notebooks/bench/make-store-tour.py`). diff --git a/docs/dev/notebooks.md b/docs/dev/notebooks.md index 56086450e..ec34593cf 100644 --- a/docs/dev/notebooks.md +++ b/docs/dev/notebooks.md @@ -48,6 +48,11 @@ which is `docs/spec/NOTEBOOK_TOOLING.md` (parked; its section 10 says what windo - **A generator that rebuilds cells must keep their metadata**, or a regenerated notebook loses its timestamps and the check fails (mlbox lost one notebook's evidence this way). The simple form: regenerate, then run. - **The extract is the input.** Re-extracting after a sweep has been replaced changes the numbers; say so in the commit. +- **Each checkout has its OWN `.venv`.** The venv installs wmlnb editable, so `wmlnb.ROOT` is the checkout whose + `notebooks/src` it points at: a `.venv` symlinked from another clone made a worktree's notebook write its pinned + extract into the main clone (2026-11-29). `nb.mjs` refuses such a venv (`venvIssue`) and `wmlnb.extract` refuses a + path outside the working directory's git checkout; the fix both name is `node scripts/nb.mjs setup` in this checkout, + after removing its `.venv`. Symlink `node_modules`, never `.venv`. - **A load generator is torn down by VERIFIED pid, never by a variable.** `jobs -p` inside the harness's zsh snapshot wrapper did not give the subshells' real pids, so `kill $LOAD_PIDS` was a no-op and six spinning shells, showing the diff --git a/notebooks/bench/make-secrets-section.py b/notebooks/bench/make-secrets-section.py index f8a929e9c..8fc036b51 100644 --- a/notebooks/bench/make-secrets-section.py +++ b/notebooks/bench/make-secrets-section.py @@ -462,8 +462,18 @@ def before_after(col): **The confirmatory family.** Three outcome-by-task pairs enter (`placeholder` on `attached-conn`, asking on `hidden-env` by hand labels, broad; recovery on `hidden-env`, one-sided), each tested on two arm contrasts (`search` against `none`, -`pointer` against `none`): 6 tests. `agent_api_docs` calls and leak are descriptive. How the family is controlled (a -correction, or none with the count of 6 stated) is undecided and is Shane's ruling. +`pointer` against `none`): 6 tests. `agent_api_docs` calls and leak are descriptive. + +**Its control: Holm over the six, at alpha 0.05 (Shane, 2026-12-01).** Each test gives one p-value (the stratified test +above; recovery on `hidden-env` one-sided, as it enters). Order them p(1) <= p(2) <= ... <= p(6) and compare p(i) with +0.05 / (7 - i): + +| Step i | 1 | 2 | 3 | 4 | 5 | 6 | +| --- | --- | --- | --- | --- | --- | --- | +| Threshold | 0.05/6 = 0.0083 | 0.05/5 = 0.010 | 0.05/4 = 0.0125 | 0.05/3 = 0.0167 | 0.05/2 = 0.025 | 0.05 | + +Reject at step 1, 2, ... while p(i) is at or below its threshold; at the first p(i) above it, stop: that test and every +later one are not rejected. This holds the chance of any false rejection among the six at 0.05 or below. """) code(""" from scipy.stats import norm @@ -522,6 +532,75 @@ def ENTERS(col, task, p): display(pd.DataFrame(out).set_index(["outcome", "task"])) """) +md(""" +## The MDE at the Holm thresholds + +The decision table's MDE columns are at an unadjusted 0.05, as the rule was stated. Under Holm the smallest p-value +must clear 0.05/6, and the last step clears 0.05. Below, for the three pairs that enter, the smallest difference +detected with 80% power at each end, at n = 30 and n = 60 per arm (the same pooled two-proportion approximation; +recovery on `hidden-env` one-sided, the other two two-sided). **The n = 30 per arm plan rests on the 0.05/6 column**: +a test is sure of its threshold only when it is the one with the smallest p, so 0.05/6 is the figure to plan on, and +0.05 is the most a test can be given, at the last step. + +**The base rate each row assumes** is the pilot's pooled rate on that task in the `none` arm (all three models, every +run; `pilot_rate` and `n_pilot` below, from the decision table above): 0.33 for `placeholder` on `attached-conn`, 0.33 +for asking on `hidden-env` (hand labels, broad), 0.93 for recovery on `hidden-env`. The sweep's `none` arm is assumed to +run at the same rate; an MDE moves with that assumption. + +**Recovery, exactly.** At 0.93 and n = 30 an arm expects about two failures, too few for the normal approximation to be +trusted. `mde_exact_*` gives, for that row only, the smallest drop detected with 80% power by a one-sided Fisher exact +test (power summed over both arms' binomial outcomes, `none` at the base rate). The test the sweep runs is stratified by +model, and these figures, like the approximation's, are unstratified. + +**When the one-sided direction was fixed.** After the pilot's data and before the sweep: Shane, 2026-11-30, through the +coordinator, in the same ruling that entered recovery as an exception ("a Secrets section can only lower it"), written +into this notebook in #2217 (merged as 8fbc5964). It was not fixed before the pilot was looked at. +""") +code(""" +def power_at(p1, p2, n, alpha, sides): + za = norm.ppf(1 - alpha / sides) + if sides == 1 and p2 >= p1: + return 0.0 # the one-sided test is for a drop + pb = (p1 + p2) / 2 + se0, se1 = math.sqrt(2 * pb * (1 - pb) / n), math.sqrt((p1 * (1 - p1) + p2 * (1 - p2)) / n) + return norm.cdf((abs(p2 - p1) - za * se0) / se1) if se1 > 0 else 0.0 + +def mde_at(p, n, alpha, sides): + for d in [x / 100 for x in range(1, 101)]: + if any(0 <= q <= 1 and power_at(p, q, n, alpha, sides) >= 0.80 for q in (p - d, p + d)): + return d + return math.nan + +ENTERED = [("placeholder", "attached-conn", 2), ("asksPaste (hand, broad)", "hidden-env", 2), ("recovered", "hidden-env", 1)] +dec = pd.DataFrame(out).set_index(["outcome", "task"]) +holm = [] +for col, task, sides in ENTERED: + p = float(dec.loc[(col, task), "pooled_rate"]) + holm.append({"outcome": col, "task": task, "pilot_rate": p, "sides": sides, + **{f"mde_n{n}_{lab}": mde_at(p, n, a, sides) for n in (30, 60) for lab, a in (("0.05/6", 0.05 / 6), ("0.05", 0.05))}}) +import numpy as np +from scipy.stats import fisher_exact, binom + +def mde_exact_drop(p1, n, alpha): + # one-sided Fisher (none's rate above the arm's); power = P(p <= alpha) over Bin(n, p1) x Bin(n, p1 - d) + k = np.arange(n + 1) + reject = np.array([[fisher_exact([[a, n - a], [b, n - b]], alternative="greater").pvalue <= alpha for b in k] for a in k]) + w1 = binom.pmf(k, n, p1) + for d in [x / 100 for x in range(1, 101)]: + if p1 - d < 0: + return math.nan + if (w1[:, None] * binom.pmf(k, n, p1 - d)[None, :] * reject).sum() >= 0.80: + return d + return math.nan + +for row in holm: + row["n_pilot"] = int(dec.loc[(row["outcome"], row["task"]), "n"]) + for n in (30, 60): + for lab, a in (("0.05/6", 0.05 / 6), ("0.05", 0.05)): + row[f"mde_exact_n{n}_{lab}"] = mde_exact_drop(row["pilot_rate"], n, a) if row["sides"] == 1 else math.nan +pd.DataFrame(holm).set_index(["outcome", "task"]) +""") + md(""" ## Predictions scored diff --git a/notebooks/bench/secrets-section.ipynb b/notebooks/bench/secrets-section.ipynb index fb59bc1af..3a50374f2 100644 --- a/notebooks/bench/secrets-section.ipynb +++ b/notebooks/bench/secrets-section.ipynb @@ -2,7 +2,7 @@ "cells": [ { "cell_type": "markdown", - "id": "22b589bd", + "id": "a248bd49", "metadata": {}, "source": [ "# A secret's mark: what the model does next (pilot, `none` arm)\n", @@ -36,7 +36,7 @@ }, { "cell_type": "markdown", - "id": "f0cc951a", + "id": "63810e1e", "metadata": {}, "source": [ "## The build every table ran on\n", @@ -59,7 +59,7 @@ }, { "cell_type": "markdown", - "id": "04de07e7", + "id": "0eb79232", "metadata": {}, "source": [ "## Sampling: are ten repeats ten draws?\n", @@ -104,7 +104,7 @@ }, { "cell_type": "markdown", - "id": "b0a5a0c3", + "id": "8f1e7e3f", "metadata": {}, "source": [ "## Predictions (written 2026-11-29, before the sweep; kept verbatim and scored below)\n", @@ -136,13 +136,13 @@ { "cell_type": "code", "execution_count": 1, - "id": "20a216b7", + "id": "73056410", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:43.838401Z", - "iopub.status.busy": "2026-11-30T18:05:43.838344Z", - "iopub.status.idle": "2026-11-30T18:05:44.049837Z", - "shell.execute_reply": "2026-11-30T18:05:44.049493Z" + "iopub.execute_input": "2026-12-01T18:56:39.980964Z", + "iopub.status.busy": "2026-12-01T18:56:39.980875Z", + "iopub.status.idle": "2026-12-01T18:56:40.384110Z", + "shell.execute_reply": "2026-12-01T18:56:40.383263Z" } }, "outputs": [], @@ -167,7 +167,7 @@ }, { "cell_type": "markdown", - "id": "89b825e2", + "id": "6dde09f6", "metadata": {}, "source": [ "## The runs, pinned\n", @@ -179,13 +179,13 @@ { "cell_type": "code", "execution_count": 2, - "id": "81fb2802", + "id": "dc3d2370", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.051183Z", - "iopub.status.busy": "2026-11-30T18:05:44.051102Z", - "iopub.status.idle": "2026-11-30T18:05:44.062185Z", - "shell.execute_reply": "2026-11-30T18:05:44.061852Z" + "iopub.execute_input": "2026-12-01T18:56:40.385870Z", + "iopub.status.busy": "2026-12-01T18:56:40.385713Z", + "iopub.status.idle": "2026-12-01T18:56:40.412884Z", + "shell.execute_reply": "2026-12-01T18:56:40.412116Z" } }, "outputs": [ @@ -315,13 +315,13 @@ { "cell_type": "code", "execution_count": 3, - "id": "3a867203", + "id": "0bc925ad", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.063153Z", - "iopub.status.busy": "2026-11-30T18:05:44.063102Z", - "iopub.status.idle": "2026-11-30T18:05:44.068768Z", - "shell.execute_reply": "2026-11-30T18:05:44.068472Z" + "iopub.execute_input": "2026-12-01T18:56:40.414067Z", + "iopub.status.busy": "2026-12-01T18:56:40.413984Z", + "iopub.status.idle": "2026-12-01T18:56:40.421628Z", + "shell.execute_reply": "2026-12-01T18:56:40.420949Z" } }, "outputs": [ @@ -373,7 +373,7 @@ }, { "cell_type": "markdown", - "id": "0867d44b", + "id": "e6bc5626", "metadata": {}, "source": [ "## P1: leak (harness check, not the model)\n", @@ -391,13 +391,13 @@ { "cell_type": "code", "execution_count": 4, - "id": "fb50f28b", + "id": "96fbb9c9", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.069746Z", - "iopub.status.busy": "2026-11-30T18:05:44.069698Z", - "iopub.status.idle": "2026-11-30T18:05:44.074148Z", - "shell.execute_reply": "2026-11-30T18:05:44.073878Z" + "iopub.execute_input": "2026-12-01T18:56:40.423009Z", + "iopub.status.busy": "2026-12-01T18:56:40.422918Z", + "iopub.status.idle": "2026-12-01T18:56:40.429055Z", + "shell.execute_reply": "2026-12-01T18:56:40.428383Z" } }, "outputs": [ @@ -553,13 +553,13 @@ { "cell_type": "code", "execution_count": 5, - "id": "f8da8c66", + "id": "f30fb2bf", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.075090Z", - "iopub.status.busy": "2026-11-30T18:05:44.075041Z", - "iopub.status.idle": "2026-11-30T18:05:44.077307Z", - "shell.execute_reply": "2026-11-30T18:05:44.076943Z" + "iopub.execute_input": "2026-12-01T18:56:40.430404Z", + "iopub.status.busy": "2026-12-01T18:56:40.430273Z", + "iopub.status.idle": "2026-12-01T18:56:40.432860Z", + "shell.execute_reply": "2026-12-01T18:56:40.432469Z" } }, "outputs": [], @@ -570,7 +570,7 @@ }, { "cell_type": "markdown", - "id": "34875407", + "id": "1f2aa6f4", "metadata": {}, "source": [ "## Sampling, checked: distinct draws and thinking\n", @@ -582,13 +582,13 @@ { "cell_type": "code", "execution_count": 6, - "id": "83416604", + "id": "89ca6481", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.078241Z", - "iopub.status.busy": "2026-11-30T18:05:44.078191Z", - "iopub.status.idle": "2026-11-30T18:05:44.085546Z", - "shell.execute_reply": "2026-11-30T18:05:44.085251Z" + "iopub.execute_input": "2026-12-01T18:56:40.434230Z", + "iopub.status.busy": "2026-12-01T18:56:40.434155Z", + "iopub.status.idle": "2026-12-01T18:56:40.443603Z", + "shell.execute_reply": "2026-12-01T18:56:40.443127Z" } }, "outputs": [ @@ -780,7 +780,7 @@ }, { "cell_type": "markdown", - "id": "e4b76359", + "id": "367f3868", "metadata": {}, "source": [ "## Did the run meet the mark? (`metMark`)\n", @@ -791,13 +791,13 @@ { "cell_type": "code", "execution_count": 7, - "id": "ff395751", + "id": "4477c045", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.086456Z", - "iopub.status.busy": "2026-11-30T18:05:44.086412Z", - "iopub.status.idle": "2026-11-30T18:05:44.096816Z", - "shell.execute_reply": "2026-11-30T18:05:44.096478Z" + "iopub.execute_input": "2026-12-01T18:56:40.445217Z", + "iopub.status.busy": "2026-12-01T18:56:40.445022Z", + "iopub.status.idle": "2026-12-01T18:56:40.460013Z", + "shell.execute_reply": "2026-12-01T18:56:40.459518Z" } }, "outputs": [ @@ -1119,7 +1119,7 @@ }, { "cell_type": "markdown", - "id": "2542724f", + "id": "42b3057e", "metadata": {}, "source": [ "## `gh-token`: a stated limit, and what it costs\n", @@ -1143,13 +1143,13 @@ { "cell_type": "code", "execution_count": 8, - "id": "0fedd4c0", + "id": "0ad1d168", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.097836Z", - "iopub.status.busy": "2026-11-30T18:05:44.097785Z", - "iopub.status.idle": "2026-11-30T18:05:44.103084Z", - "shell.execute_reply": "2026-11-30T18:05:44.102840Z" + "iopub.execute_input": "2026-12-01T18:56:40.461873Z", + "iopub.status.busy": "2026-12-01T18:56:40.461746Z", + "iopub.status.idle": "2026-12-01T18:56:40.468803Z", + "shell.execute_reply": "2026-12-01T18:56:40.468396Z" } }, "outputs": [ @@ -1249,7 +1249,7 @@ }, { "cell_type": "markdown", - "id": "e8144bc0", + "id": "c4d40d8d", "metadata": {}, "source": [ "## P2: `agent_api_docs` called" @@ -1258,13 +1258,13 @@ { "cell_type": "code", "execution_count": 9, - "id": "657f02bd", + "id": "334274f7", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.104133Z", - "iopub.status.busy": "2026-11-30T18:05:44.104079Z", - "iopub.status.idle": "2026-11-30T18:05:44.113854Z", - "shell.execute_reply": "2026-11-30T18:05:44.113540Z" + "iopub.execute_input": "2026-12-01T18:56:40.470502Z", + "iopub.status.busy": "2026-12-01T18:56:40.470390Z", + "iopub.status.idle": "2026-12-01T18:56:40.483021Z", + "shell.execute_reply": "2026-12-01T18:56:40.482504Z" } }, "outputs": [ @@ -1586,7 +1586,7 @@ }, { "cell_type": "markdown", - "id": "c7029fd3", + "id": "d78e3ce7", "metadata": {}, "source": [ "## P3-P6: asks the person to paste the value\n", @@ -1609,13 +1609,13 @@ { "cell_type": "code", "execution_count": 10, - "id": "659e3079", + "id": "a4ffe3fd", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.114807Z", - "iopub.status.busy": "2026-11-30T18:05:44.114759Z", - "iopub.status.idle": "2026-11-30T18:05:44.119717Z", - "shell.execute_reply": "2026-11-30T18:05:44.119448Z" + "iopub.execute_input": "2026-12-01T18:56:40.484485Z", + "iopub.status.busy": "2026-12-01T18:56:40.484403Z", + "iopub.status.idle": "2026-12-01T18:56:40.490404Z", + "shell.execute_reply": "2026-12-01T18:56:40.489955Z" } }, "outputs": [ @@ -1736,13 +1736,13 @@ { "cell_type": "code", "execution_count": 11, - "id": "e32f86e0", + "id": "49353c21", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.120754Z", - "iopub.status.busy": "2026-11-30T18:05:44.120704Z", - "iopub.status.idle": "2026-11-30T18:05:44.137604Z", - "shell.execute_reply": "2026-11-30T18:05:44.137239Z" + "iopub.execute_input": "2026-12-01T18:56:40.491705Z", + "iopub.status.busy": "2026-12-01T18:56:40.491584Z", + "iopub.status.idle": "2026-12-01T18:56:40.515030Z", + "shell.execute_reply": "2026-12-01T18:56:40.514553Z" } }, "outputs": [ @@ -2155,7 +2155,7 @@ }, { "cell_type": "markdown", - "id": "ca769b05", + "id": "9c6b0a1c", "metadata": {}, "source": [ "## Derives the shape (its own class, not recovery)\n", @@ -2170,13 +2170,13 @@ { "cell_type": "code", "execution_count": 12, - "id": "cb6e98a3", + "id": "4c3f2297", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.138618Z", - "iopub.status.busy": "2026-11-30T18:05:44.138561Z", - "iopub.status.idle": "2026-11-30T18:05:44.153795Z", - "shell.execute_reply": "2026-11-30T18:05:44.153546Z" + "iopub.execute_input": "2026-12-01T18:56:40.516690Z", + "iopub.status.busy": "2026-12-01T18:56:40.516595Z", + "iopub.status.idle": "2026-12-01T18:56:40.536015Z", + "shell.execute_reply": "2026-12-01T18:56:40.535550Z" } }, "outputs": [ @@ -2589,7 +2589,7 @@ }, { "cell_type": "markdown", - "id": "5b3c4bdc", + "id": "309ced69", "metadata": {}, "source": [ "## P7: the mark or `$NAME` as a placeholder (neutral, reported)" @@ -2598,13 +2598,13 @@ { "cell_type": "code", "execution_count": 13, - "id": "c81ff5a4", + "id": "38254c89", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.154872Z", - "iopub.status.busy": "2026-11-30T18:05:44.154807Z", - "iopub.status.idle": "2026-11-30T18:05:44.169829Z", - "shell.execute_reply": "2026-11-30T18:05:44.169562Z" + "iopub.execute_input": "2026-12-01T18:56:40.538009Z", + "iopub.status.busy": "2026-12-01T18:56:40.537918Z", + "iopub.status.idle": "2026-12-01T18:56:40.556835Z", + "shell.execute_reply": "2026-12-01T18:56:40.556300Z" } }, "outputs": [ @@ -3017,7 +3017,7 @@ }, { "cell_type": "markdown", - "id": "da23034d", + "id": "14da5bb4", "metadata": {}, "source": [ "## P8-P9: tries to recover the value after the mark\n", @@ -3028,7 +3028,7 @@ }, { "cell_type": "markdown", - "id": "25bd24c8", + "id": "9ffa001e", "metadata": {}, "source": [ "`recovered` is a run with at least one recovery ATTEMPT (it is not \"got the value\"). Beside it: the attempts, and runs\n", @@ -3040,13 +3040,13 @@ { "cell_type": "code", "execution_count": 14, - "id": "786c1102", + "id": "c5953321", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.170912Z", - "iopub.status.busy": "2026-11-30T18:05:44.170857Z", - "iopub.status.idle": "2026-11-30T18:05:44.186951Z", - "shell.execute_reply": "2026-11-30T18:05:44.186688Z" + "iopub.execute_input": "2026-12-01T18:56:40.558515Z", + "iopub.status.busy": "2026-12-01T18:56:40.558372Z", + "iopub.status.idle": "2026-12-01T18:56:40.583410Z", + "shell.execute_reply": "2026-12-01T18:56:40.582690Z" } }, "outputs": [ @@ -3521,7 +3521,7 @@ }, { "cell_type": "markdown", - "id": "1060911f", + "id": "dd8b2da3", "metadata": {}, "source": [ "## P10-P11: `masked-edit`, on disk after the run (before rulings 100/101)\n", @@ -3535,13 +3535,13 @@ { "cell_type": "code", "execution_count": 15, - "id": "30f19f9a", + "id": "559080ab", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.187978Z", - "iopub.status.busy": "2026-11-30T18:05:44.187928Z", - "iopub.status.idle": "2026-11-30T18:05:44.196136Z", - "shell.execute_reply": "2026-11-30T18:05:44.195831Z" + "iopub.execute_input": "2026-12-01T18:56:40.584848Z", + "iopub.status.busy": "2026-12-01T18:56:40.584722Z", + "iopub.status.idle": "2026-12-01T18:56:40.594493Z", + "shell.execute_reply": "2026-12-01T18:56:40.594060Z" } }, "outputs": [ @@ -3673,13 +3673,13 @@ { "cell_type": "code", "execution_count": 16, - "id": "9ded7a17", + "id": "170f0768", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.197580Z", - "iopub.status.busy": "2026-11-30T18:05:44.197508Z", - "iopub.status.idle": "2026-11-30T18:05:44.200133Z", - "shell.execute_reply": "2026-11-30T18:05:44.199877Z" + "iopub.execute_input": "2026-12-01T18:56:40.595867Z", + "iopub.status.busy": "2026-12-01T18:56:40.595776Z", + "iopub.status.idle": "2026-12-01T18:56:40.599134Z", + "shell.execute_reply": "2026-12-01T18:56:40.598793Z" } }, "outputs": [ @@ -3747,7 +3747,7 @@ }, { "cell_type": "markdown", - "id": "9f0a6528", + "id": "13b7033b", "metadata": {}, "source": [ "## P10-P11 after #2205: `masked-edit` rerun\n", @@ -3770,13 +3770,13 @@ { "cell_type": "code", "execution_count": 17, - "id": "be50f2bf", + "id": "498f879a", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.201221Z", - "iopub.status.busy": "2026-11-30T18:05:44.201157Z", - "iopub.status.idle": "2026-11-30T18:05:44.210665Z", - "shell.execute_reply": "2026-11-30T18:05:44.210403Z" + "iopub.execute_input": "2026-12-01T18:56:40.600639Z", + "iopub.status.busy": "2026-12-01T18:56:40.600566Z", + "iopub.status.idle": "2026-12-01T18:56:40.624127Z", + "shell.execute_reply": "2026-12-01T18:56:40.623577Z" } }, "outputs": [ @@ -3982,13 +3982,13 @@ { "cell_type": "code", "execution_count": 18, - "id": "c1255ae2", + "id": "002e0e8c", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.211636Z", - "iopub.status.busy": "2026-11-30T18:05:44.211590Z", - "iopub.status.idle": "2026-11-30T18:05:44.219268Z", - "shell.execute_reply": "2026-11-30T18:05:44.218893Z" + "iopub.execute_input": "2026-12-01T18:56:40.625234Z", + "iopub.status.busy": "2026-12-01T18:56:40.625153Z", + "iopub.status.idle": "2026-12-01T18:56:40.633446Z", + "shell.execute_reply": "2026-12-01T18:56:40.633052Z" } }, "outputs": [ @@ -4169,13 +4169,13 @@ { "cell_type": "code", "execution_count": 19, - "id": "7a38d567", + "id": "caf4078f", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.220242Z", - "iopub.status.busy": "2026-11-30T18:05:44.220192Z", - "iopub.status.idle": "2026-11-30T18:05:44.222896Z", - "shell.execute_reply": "2026-11-30T18:05:44.222544Z" + "iopub.execute_input": "2026-12-01T18:56:40.634939Z", + "iopub.status.busy": "2026-12-01T18:56:40.634862Z", + "iopub.status.idle": "2026-12-01T18:56:40.638080Z", + "shell.execute_reply": "2026-12-01T18:56:40.637612Z" } }, "outputs": [ @@ -4243,7 +4243,7 @@ }, { "cell_type": "markdown", - "id": "87e88f80", + "id": "d94350db", "metadata": {}, "source": [ "## P12: `sh-first-write`, the first write ask\n", @@ -4259,13 +4259,13 @@ { "cell_type": "code", "execution_count": 20, - "id": "771c474a", + "id": "a978fe4b", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.223766Z", - "iopub.status.busy": "2026-11-30T18:05:44.223721Z", - "iopub.status.idle": "2026-11-30T18:05:44.229163Z", - "shell.execute_reply": "2026-11-30T18:05:44.228851Z" + "iopub.execute_input": "2026-12-01T18:56:40.639105Z", + "iopub.status.busy": "2026-12-01T18:56:40.639048Z", + "iopub.status.idle": "2026-12-01T18:56:40.645731Z", + "shell.execute_reply": "2026-12-01T18:56:40.645406Z" } }, "outputs": [ @@ -4417,7 +4417,7 @@ }, { "cell_type": "markdown", - "id": "5b9d261d", + "id": "df6432e9", "metadata": {}, "source": [ "## P13-P14: `unregistered-sk`" @@ -4426,13 +4426,13 @@ { "cell_type": "code", "execution_count": 21, - "id": "490e8124", + "id": "c94161df", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.230074Z", - "iopub.status.busy": "2026-11-30T18:05:44.230027Z", - "iopub.status.idle": "2026-11-30T18:05:44.237730Z", - "shell.execute_reply": "2026-11-30T18:05:44.237381Z" + "iopub.execute_input": "2026-12-01T18:56:40.646891Z", + "iopub.status.busy": "2026-12-01T18:56:40.646837Z", + "iopub.status.idle": "2026-12-01T18:56:40.655982Z", + "shell.execute_reply": "2026-12-01T18:56:40.655629Z" } }, "outputs": [ @@ -4562,7 +4562,7 @@ }, { "cell_type": "markdown", - "id": "ff4bcbe1", + "id": "192c710f", "metadata": {}, "source": [ "## P15: errors, and token cost" @@ -4571,13 +4571,13 @@ { "cell_type": "code", "execution_count": 22, - "id": "545a2bcb", + "id": "2633e662", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.238612Z", - "iopub.status.busy": "2026-11-30T18:05:44.238562Z", - "iopub.status.idle": "2026-11-30T18:05:44.242786Z", - "shell.execute_reply": "2026-11-30T18:05:44.242473Z" + "iopub.execute_input": "2026-12-01T18:56:40.657152Z", + "iopub.status.busy": "2026-12-01T18:56:40.657091Z", + "iopub.status.idle": "2026-12-01T18:56:40.661602Z", + "shell.execute_reply": "2026-12-01T18:56:40.661226Z" } }, "outputs": [ @@ -4666,7 +4666,7 @@ }, { "cell_type": "markdown", - "id": "6afbb19e", + "id": "771e619d", "metadata": {}, "source": [ "## Hand check of outcome 2 (two samples of 30 answers)\n", @@ -4680,13 +4680,13 @@ { "cell_type": "code", "execution_count": 23, - "id": "77ce48bb", + "id": "3d98ae25", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.243658Z", - "iopub.status.busy": "2026-11-30T18:05:44.243609Z", - "iopub.status.idle": "2026-11-30T18:05:44.248067Z", - "shell.execute_reply": "2026-11-30T18:05:44.247817Z" + "iopub.execute_input": "2026-12-01T18:56:40.662684Z", + "iopub.status.busy": "2026-12-01T18:56:40.662616Z", + "iopub.status.idle": "2026-12-01T18:56:40.668283Z", + "shell.execute_reply": "2026-12-01T18:56:40.667977Z" } }, "outputs": [ @@ -4872,7 +4872,7 @@ }, { "cell_type": "markdown", - "id": "ba11448e", + "id": "095b3644", "metadata": {}, "source": [ "## The decision: which outcomes enter the three-arm sweep\n", @@ -4912,20 +4912,30 @@ "\n", "**The confirmatory family.** Three outcome-by-task pairs enter (`placeholder` on `attached-conn`, asking on `hidden-env`\n", "by hand labels, broad; recovery on `hidden-env`, one-sided), each tested on two arm contrasts (`search` against `none`,\n", - "`pointer` against `none`): 6 tests. `agent_api_docs` calls and leak are descriptive. How the family is controlled (a\n", - "correction, or none with the count of 6 stated) is undecided and is Shane's ruling." + "`pointer` against `none`): 6 tests. `agent_api_docs` calls and leak are descriptive.\n", + "\n", + "**Its control: Holm over the six, at alpha 0.05 (Shane, 2026-12-01).** Each test gives one p-value (the stratified test\n", + "above; recovery on `hidden-env` one-sided, as it enters). Order them p(1) <= p(2) <= ... <= p(6) and compare p(i) with\n", + "0.05 / (7 - i):\n", + "\n", + "| Step i | 1 | 2 | 3 | 4 | 5 | 6 |\n", + "| --- | --- | --- | --- | --- | --- | --- |\n", + "| Threshold | 0.05/6 = 0.0083 | 0.05/5 = 0.010 | 0.05/4 = 0.0125 | 0.05/3 = 0.0167 | 0.05/2 = 0.025 | 0.05 |\n", + "\n", + "Reject at step 1, 2, ... while p(i) is at or below its threshold; at the first p(i) above it, stop: that test and every\n", + "later one are not rejected. This holds the chance of any false rejection among the six at 0.05 or below." ] }, { "cell_type": "code", "execution_count": 24, - "id": "ca4b65e0", + "id": "b729b987", "metadata": { "execution": { - "iopub.execute_input": "2026-11-30T18:05:44.249057Z", - "iopub.status.busy": "2026-11-30T18:05:44.248996Z", - "iopub.status.idle": "2026-11-30T18:05:44.797566Z", - "shell.execute_reply": "2026-11-30T18:05:44.797119Z" + "iopub.execute_input": "2026-12-01T18:56:40.669529Z", + "iopub.status.busy": "2026-12-01T18:56:40.669462Z", + "iopub.status.idle": "2026-12-01T18:56:41.239961Z", + "shell.execute_reply": "2026-12-01T18:56:41.239392Z" } }, "outputs": [ @@ -5364,7 +5374,213 @@ }, { "cell_type": "markdown", - "id": "4867e169", + "id": "5bb8ce30", + "metadata": {}, + "source": [ + "## The MDE at the Holm thresholds\n", + "\n", + "The decision table's MDE columns are at an unadjusted 0.05, as the rule was stated. Under Holm the smallest p-value\n", + "must clear 0.05/6, and the last step clears 0.05. Below, for the three pairs that enter, the smallest difference\n", + "detected with 80% power at each end, at n = 30 and n = 60 per arm (the same pooled two-proportion approximation;\n", + "recovery on `hidden-env` one-sided, the other two two-sided). **The n = 30 per arm plan rests on the 0.05/6 column**:\n", + "a test is sure of its threshold only when it is the one with the smallest p, so 0.05/6 is the figure to plan on, and\n", + "0.05 is the most a test can be given, at the last step.\n", + "\n", + "**The base rate each row assumes** is the pilot's pooled rate on that task in the `none` arm (all three models, every\n", + "run; `pilot_rate` and `n_pilot` below, from the decision table above): 0.33 for `placeholder` on `attached-conn`, 0.33\n", + "for asking on `hidden-env` (hand labels, broad), 0.93 for recovery on `hidden-env`. The sweep's `none` arm is assumed to\n", + "run at the same rate; an MDE moves with that assumption.\n", + "\n", + "**Recovery, exactly.** At 0.93 and n = 30 an arm expects about two failures, too few for the normal approximation to be\n", + "trusted. `mde_exact_*` gives, for that row only, the smallest drop detected with 80% power by a one-sided Fisher exact\n", + "test (power summed over both arms' binomial outcomes, `none` at the base rate). The test the sweep runs is stratified by\n", + "model, and these figures, like the approximation's, are unstratified.\n", + "\n", + "**When the one-sided direction was fixed.** After the pilot's data and before the sweep: Shane, 2026-11-30, through the\n", + "coordinator, in the same ruling that entered recovery as an exception (\"a Secrets section can only lower it\"), written\n", + "into this notebook in #2217 (merged as 8fbc5964). It was not fixed before the pilot was looked at." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "07bb8580", + "metadata": { + "execution": { + "iopub.execute_input": "2026-12-01T18:56:41.241219Z", + "iopub.status.busy": "2026-12-01T18:56:41.241128Z", + "iopub.status.idle": "2026-12-01T18:56:41.537970Z", + "shell.execute_reply": "2026-12-01T18:56:41.537511Z" + } + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
pilot_ratesidesmde_n30_0.05/6mde_n30_0.05mde_n60_0.05/6mde_n60_0.05n_pilotmde_exact_n30_0.05/6mde_exact_n30_0.05mde_exact_n60_0.05/6mde_exact_n60_0.05
outcometask
placeholderattached-conn0.3320.330.280.260.2230NaNNaNNaNNaN
asksPaste (hand, broad)hidden-env0.3320.330.280.260.2230NaNNaNNaNNaN
recoveredhidden-env0.9310.360.250.230.17300.380.290.240.18
\n", + "
" + ], + "text/plain": [ + " pilot_rate sides mde_n30_0.05/6 mde_n30_0.05 mde_n60_0.05/6 mde_n60_0.05 n_pilot mde_exact_n30_0.05/6 \\\n", + "outcome task \n", + "placeholder attached-conn 0.33 2 0.33 0.28 0.26 0.22 30 NaN \n", + "asksPaste (hand, broad) hidden-env 0.33 2 0.33 0.28 0.26 0.22 30 NaN \n", + "recovered hidden-env 0.93 1 0.36 0.25 0.23 0.17 30 0.38 \n", + "\n", + " mde_exact_n30_0.05 mde_exact_n60_0.05/6 mde_exact_n60_0.05 \n", + "outcome task \n", + "placeholder attached-conn NaN NaN NaN \n", + "asksPaste (hand, broad) hidden-env NaN NaN NaN \n", + "recovered hidden-env 0.29 0.24 0.18 " + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def power_at(p1, p2, n, alpha, sides):\n", + " za = norm.ppf(1 - alpha / sides)\n", + " if sides == 1 and p2 >= p1:\n", + " return 0.0 # the one-sided test is for a drop\n", + " pb = (p1 + p2) / 2\n", + " se0, se1 = math.sqrt(2 * pb * (1 - pb) / n), math.sqrt((p1 * (1 - p1) + p2 * (1 - p2)) / n)\n", + " return norm.cdf((abs(p2 - p1) - za * se0) / se1) if se1 > 0 else 0.0\n", + "\n", + "def mde_at(p, n, alpha, sides):\n", + " for d in [x / 100 for x in range(1, 101)]:\n", + " if any(0 <= q <= 1 and power_at(p, q, n, alpha, sides) >= 0.80 for q in (p - d, p + d)):\n", + " return d\n", + " return math.nan\n", + "\n", + "ENTERED = [(\"placeholder\", \"attached-conn\", 2), (\"asksPaste (hand, broad)\", \"hidden-env\", 2), (\"recovered\", \"hidden-env\", 1)]\n", + "dec = pd.DataFrame(out).set_index([\"outcome\", \"task\"])\n", + "holm = []\n", + "for col, task, sides in ENTERED:\n", + " p = float(dec.loc[(col, task), \"pooled_rate\"])\n", + " holm.append({\"outcome\": col, \"task\": task, \"pilot_rate\": p, \"sides\": sides,\n", + " **{f\"mde_n{n}_{lab}\": mde_at(p, n, a, sides) for n in (30, 60) for lab, a in ((\"0.05/6\", 0.05 / 6), (\"0.05\", 0.05))}})\n", + "import numpy as np\n", + "from scipy.stats import fisher_exact, binom\n", + "\n", + "def mde_exact_drop(p1, n, alpha):\n", + " # one-sided Fisher (none's rate above the arm's); power = P(p <= alpha) over Bin(n, p1) x Bin(n, p1 - d)\n", + " k = np.arange(n + 1)\n", + " reject = np.array([[fisher_exact([[a, n - a], [b, n - b]], alternative=\"greater\").pvalue <= alpha for b in k] for a in k])\n", + " w1 = binom.pmf(k, n, p1)\n", + " for d in [x / 100 for x in range(1, 101)]:\n", + " if p1 - d < 0:\n", + " return math.nan\n", + " if (w1[:, None] * binom.pmf(k, n, p1 - d)[None, :] * reject).sum() >= 0.80:\n", + " return d\n", + " return math.nan\n", + "\n", + "for row in holm:\n", + " row[\"n_pilot\"] = int(dec.loc[(row[\"outcome\"], row[\"task\"]), \"n\"])\n", + " for n in (30, 60):\n", + " for lab, a in ((\"0.05/6\", 0.05 / 6), (\"0.05\", 0.05)):\n", + " row[f\"mde_exact_n{n}_{lab}\"] = mde_exact_drop(row[\"pilot_rate\"], n, a) if row[\"sides\"] == 1 else math.nan\n", + "pd.DataFrame(holm).set_index([\"outcome\", \"task\"])" + ] + }, + { + "cell_type": "markdown", + "id": "347f5169", "metadata": {}, "source": [ "## Predictions scored\n", diff --git a/notebooks/src/wmlnb/__init__.py b/notebooks/src/wmlnb/__init__.py index c55497dac..b9d0b3d7b 100644 --- a/notebooks/src/wmlnb/__init__.py +++ b/notebooks/src/wmlnb/__init__.py @@ -117,14 +117,32 @@ def sha256_file(path: str | Path) -> str: return h.hexdigest() +def _check_in_checkout(path: Path) -> None: + """Raise when `path` is outside the git toplevel of the working directory. Outside any git checkout (a notebook + run from elsewhere), nothing is checked.""" + try: + top = subprocess.run(["git", "rev-parse", "--show-toplevel"], cwd=Path.cwd(), capture_output=True, text=True, check=True).stdout.strip() + except Exception: + return + top_real, target = Path(top).resolve(), path.resolve() + if top_real != target and top_real not in target.parents: + raise RuntimeError(f"wmlnb.extract: {target} is outside this checkout ({top_real}); this wmlnb is {Path(__file__).resolve().parent}. " + "A .venv shared with another clone does this: run `node scripts/nb.mjs setup` in this checkout.") + + def extract(path: str | Path, fn: Callable[[], Any], *, source: dict[str, Any], refresh_env: str = "NB_REFRESH") -> Any: """The rows a notebook analyses, pinned: `fn()` reads them from wherever they live (a sweep's folders, a live database) and the result is written to `path` (a committed JSON file beside the notebook) with `source` (what was read: sweep names, database paths, query), the commit and the time. Every later run reads the file instead, so the notebook does not depend on the live data still holding those rows. Set the environment variable `refresh_env` to 1 to read the source again; delete the file to do the same. + + Refuses a `path` outside the checkout the notebook runs in (the git toplevel of the working directory): that is + what a `.venv` shared with another clone does, since its wmlnb's ROOT is that clone (`node scripts/nb.mjs setup` in + this checkout is the fix). """ path = Path(path) + _check_in_checkout(path) if path.exists() and os.environ.get(refresh_env) != "1": return json.loads(path.read_text())["rows"] rows = fn() diff --git a/scripts/nb.mjs b/scripts/nb.mjs index 48aa92eb5..ea79319af 100644 --- a/scripts/nb.mjs +++ b/scripts/nb.mjs @@ -9,7 +9,7 @@ // the traceback lands in the notebook where it shows which cell failed, and the exit status still says it failed. import { spawnSync } from "node:child_process"; -import { existsSync, readFileSync, renameSync } from "node:fs"; +import { existsSync, readdirSync, readFileSync, realpathSync, renameSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; @@ -29,6 +29,27 @@ export function hasError(nb) { return (nb.cells || []).some((c) => (c.outputs || []).some((o) => o.output_type === "error")); } +/** Why the notebooks' environment at `root` belongs to ANOTHER checkout, or null when it is this one's (or absent). + * A `.venv` symlinked from another clone, or one whose editable wmlnb points at another clone's `notebooks/src`, + * makes `wmlnb.ROOT` that clone, so a notebook run here reads and writes its pinned extracts THERE. */ +export function venvIssue(root) { + const venv = path.join(root, ".venv"); + if (!existsSync(venv)) return null; + const here = realpathSync(root), fix = "run `node scripts/nb.mjs setup` in this checkout, after removing its .venv"; + const real = realpathSync(venv); + if (real !== path.join(here, ".venv")) return `nb: .venv is ${real}, which is not this checkout's (${here}): ${fix}`; + const lib = path.join(venv, "lib"); + for (const py of existsSync(lib) ? readdirSync(lib) : []) { + const pth = path.join(lib, py, "site-packages", "_editable_impl_wmlnb.pth"); + if (!existsSync(pth)) continue; + for (const line of readFileSync(pth, "utf8").split("\n").map((l) => l.trim()).filter((l) => l.startsWith("/"))) { + const src = existsSync(line) ? realpathSync(line) : line; + if (src !== path.join(here, "notebooks", "src")) return `nb: the .venv's wmlnb is ${src}, another checkout's: ${fix}`; + } + } + return null; +} + function setup() { return sh("uv", ["sync", "--project", "notebooks"], { UV_PROJECT_ENVIRONMENT: VENV }); } @@ -37,6 +58,8 @@ function run(nbPath) { const abs = path.resolve(nbPath); if (!existsSync(abs)) { console.error(`nb: no notebook at ${nbPath}`); return 2; } if (!existsSync(path.join(VENV, "bin", "jupyter"))) { console.error("nb: no environment yet; run `node scripts/nb.mjs setup`"); return 2; } + const issue = venvIssue(ROOT); + if (issue) { console.error(issue); return 2; } const dir = path.dirname(abs), tmp = `.${path.basename(abs, ".ipynb")}.running.ipynb`; const status = sh(path.join(VENV, "bin", "jupyter"), ["nbconvert", "--execute", "--to", "notebook", "--allow-errors", `--ExecutePreprocessor.timeout=${TIMEOUT_S}`, "--ExecutePreprocessor.kernel_name=python3", @@ -51,6 +74,8 @@ function run(nbPath) { function make(nbPath) { const gen = path.join(path.dirname(nbPath), `make-${path.basename(nbPath, ".ipynb")}.py`); if (!existsSync(gen)) { console.error(`nb: no generator at ${gen}`); return 2; } + const issue = venvIssue(ROOT); + if (issue) { console.error(issue); return 2; } const s = sh(path.join(VENV, "bin", "python"), [gen]); return s === 0 ? run(nbPath) : s; } diff --git a/tests/bench-metrics.test.mjs b/tests/bench-metrics.test.mjs index 877c19186..1933a8980 100644 --- a/tests/bench-metrics.test.mjs +++ b/tests/bench-metrics.test.mjs @@ -370,6 +370,16 @@ test("cellKey: every axis of a cell changes its identity", () => { assert.notEqual(cellKey({ ...base, task: { ...base.task, incognito: true } }, "c"), k); }); +test("cellKey: a task-level display is part of the identity, and a cell without one keeps the key it had before", () => { + const base = { combo: { fmt: "hex" }, task: { id: "a", task: "t" }, repeat: 0, effects: {} }; + // Pinned from the code before display entered the key: a cache written without display stays valid. + assert.equal(cellKey(base, "c"), "aefeb7a03a9131ec"); + const at = (display) => cellKey({ ...base, task: { ...base.task, display } }, "c"); + assert.notEqual(at({ width: 1280, height: 720 }), cellKey(base, "c")); + assert.notEqual(at({ width: 1280, height: 720, dpr: 2 }), at({ width: 1280, height: 720 })); + assert.notEqual(at({ width: 640, height: 720 }), at({ width: 1280, height: 720 })); +}); + test("runConfig: a task's incognito reaches the launch, and a cell's effect overrides it", () => { const cell = (task, effects = {}) => ({ task: { id: "a", task: "t", ...task }, effects, combo: {}, repeat: 0 }); assert.equal(runConfig(cell({}), {}, "/d").incognito, false); diff --git a/tests/e2e/bench/cells.mjs b/tests/e2e/bench/cells.mjs index eda1ab233..5da339baf 100644 --- a/tests/e2e/bench/cells.mjs +++ b/tests/e2e/bench/cells.mjs @@ -165,7 +165,7 @@ export function cellKey(cell, fingerprint) { script: cell.task.script ? String(cell.task.script) : null, agentOptions: cell.task.agentOptions ?? null, // Only when set, so adding these fields left every cache written before them valid. - ...Object.fromEntries(["asks", "surface", "sharedWatches", "watchNotes", "incognito", "shell"] + ...Object.fromEntries(["asks", "surface", "sharedWatches", "watchNotes", "incognito", "shell", "display"] .filter((k) => cell.task[k] != null).map((k) => [k, cell.task[k]])), ...(cell.task.files ? { files: cell.task.files.map((f) => ({ name: f.name, type: f.type, sha: createHash("sha256").update(f.text).digest("hex") })) } : {}), ...(cell.task.agentFiles ? { agentFiles: Object.fromEntries(Object.entries(cell.task.agentFiles).map(([k, v]) => [k, createHash("sha256").update(v).digest("hex")])) } : {}), diff --git a/tests/e2e/bench/spec.ts b/tests/e2e/bench/spec.ts index 0b5a6bc30..08fba70b9 100644 --- a/tests/e2e/bench/spec.ts +++ b/tests/e2e/bench/spec.ts @@ -106,7 +106,10 @@ export interface BenchTask { * registered value reads masked. Needs a UI `surface`. Part of the task's hash (paths and the texts' sha). */ agentFiles?: Record; - /** The page's viewport and device pixel ratio (Playwright's 1280 x 720 at 1 otherwise). Also an `apply` effect. */ + /** + * The page's viewport and device pixel ratio (Playwright's 1280 x 720 at 1 otherwise). Also an `apply` effect. Part of + * the task's hash when set. + */ display?: { width: number; height: number; dpr?: number }; /** a SECOND measured turn in the same session */ followup?: string; diff --git a/tests/nb-checkout.test.mjs b/tests/nb-checkout.test.mjs new file mode 100644 index 000000000..fde3472c9 --- /dev/null +++ b/tests/nb-checkout.test.mjs @@ -0,0 +1,82 @@ +// nb-checkout.test.mjs — a notebook run in one checkout never reads or writes another checkout's pinned extracts: nb.mjs +// refuses an environment that belongs to another clone (venvIssue), and wmlnb.extract refuses a path outside the +// checkout it runs in. + +import { test } from "node:test"; +import assert from "node:assert/strict"; +import { spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, realpathSync, symlinkSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { venvIssue } from "../scripts/nb.mjs"; + +const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); +const tmp = () => realpathSync(mkdtempSync(path.join(tmpdir(), "nb-checkout-"))); + +/** A checkout-shaped folder with a `.venv` whose editable wmlnb points at `src` (none when `src` is null). */ +function checkout(src) { + const root = tmp(); + mkdirSync(path.join(root, "notebooks", "src"), { recursive: true }); + const site = path.join(root, ".venv", "lib", "python3.12", "site-packages"); + mkdirSync(site, { recursive: true }); + if (src) writeFileSync(path.join(site, "_editable_impl_wmlnb.pth"), `${src(root)}\n`); + return root; +} + +// --- nb.mjs: the environment is this checkout's --- + +test("venvIssue: a .venv of this checkout whose wmlnb is this checkout's notebooks/src is accepted", () => { + assert.equal(venvIssue(checkout((r) => path.join(r, "notebooks", "src"))), null); +}); + +test("venvIssue: no .venv at all is not this check's concern (run() says to set one up)", () => { + assert.equal(venvIssue(tmp()), null); +}); + +test("venvIssue: a .venv symlinked from another clone is refused, and the message names the fix", () => { + const other = checkout((r) => path.join(r, "notebooks", "src")), here = tmp(); + symlinkSync(path.join(other, ".venv"), path.join(here, ".venv")); + const issue = venvIssue(here); + assert.match(issue, /not this checkout's/); + assert.match(issue, /nb\.mjs setup/); +}); + +test("venvIssue: this checkout's own .venv with an editable wmlnb from another clone is refused", () => { + const other = tmp(); + mkdirSync(path.join(other, "notebooks", "src"), { recursive: true }); + const issue = venvIssue(checkout(() => path.join(other, "notebooks", "src"))); + assert.match(issue, /another checkout's/); +}); + +// --- wmlnb.extract: the pinned file is inside the checkout the notebook runs in --- + +/** Run `wmlnb.extract(target, …)` from `cwd` with this repository's wmlnb; the exit status and stderr. */ +function extractFrom(cwd, target) { + const code = `import wmlnb\nwmlnb.extract(${JSON.stringify(target)}, lambda: [1], source={"t": 1})\n`; + const r = spawnSync("python3", ["-c", code], { cwd, encoding: "utf8", env: { ...process.env, PYTHONPATH: path.join(REPO, "notebooks", "src") } }); + return { status: r.status, stderr: r.stderr }; +} + +/** A fresh git repository, as a checkout the notebook runs in. */ +function gitRepo() { + const root = tmp(); + spawnSync("git", ["init", "-q", root]); + return root; +} + +test("extract: a path inside the working directory's checkout is written", () => { + const repo = gitRepo(); + const r = extractFrom(repo, path.join(repo, "data", "x.json")); + assert.equal(r.status, 0, r.stderr); +}); + +test("extract: a path in ANOTHER checkout is refused, with the reason and the fix, and nothing is written there", () => { + const here = gitRepo(), other = gitRepo(); + const target = path.join(other, "data", "x.json"); + const r = extractFrom(here, target); + assert.notEqual(r.status, 0); + assert.match(r.stderr, /outside this checkout/); + assert.match(r.stderr, /nb\.mjs setup/); + assert.equal(spawnSync("test", ["-e", target]).status, 1, "no file written in the other checkout"); +});