diff --git a/agents/conductors/cortex/_cortex.py b/agents/conductors/cortex/_cortex.py index b03f1865..3217e70e 100644 --- a/agents/conductors/cortex/_cortex.py +++ b/agents/conductors/cortex/_cortex.py @@ -1637,35 +1637,89 @@ def _under(path: Path, root: Path) -> bool: return path == root or root in path.parents -def where_paths(mod, tk, roots: list[Path]) -> list[Path]: - """The task's own `## Where to look` bullets, intersected with the roots. +#: A bullet marks its paths with backticks and leads with a label the reader +#: needs and the scorer does not — "`euclid_dr1_prelim` (project row): +#: `output_ordered_witness/run_0`" — so the first token is never the path. +_BACKTICK_RE = re.compile(r"`([^`]+)`") + + +def _path_tokens(bullet: str) -> list[str]: + """Every path-like token in one bullet, in the order it reads. - A task names where its results are; that is the first place to look. A - bullet pointing outside every root is not this project's tree (a RAL path, - say) and is dropped rather than followed. + Each backticked span, then any bare word carrying a `/`. A label caught + this way costs one `exists()` and is dropped; reading only the first + token, as this once did, cost every real bullet. """ - out = [] - for bullet in _where_bullets(mod, tk): - token = bullet.split()[0].strip("`,.") - if not token: + spans = [m.group(1) for m in _BACKTICK_RE.finditer(bullet)] + bare = [w for w in _BACKTICK_RE.sub(" ", bullet).split() if "/" in w] + out: list[str] = [] + for raw in spans + bare: + token = raw.strip().rstrip(".,;:") + if not token or " " in token or "://" in token: continue - p = Path(token) - if p.is_absolute() and any(_under(p, r) for r in roots) and p.exists(): - out.append(p) + if ".." in Path(token).parts: # no climbing out of a root + continue + if token not in out: + out.append(token) + return out + + +def where_paths(mod, tk, roots: list[Path]) -> list[Path]: + """The task's own `## Where to look` bullets, resolved against the roots. + + A task names where its results are; that is the first place to look. It + names them *relative to the project root* — `output_ordered_witness/…`, + the custom `PYAUTO_OUTPUT_DIR` a run was pointed at — so each token is + tried under every root. An absolute one is kept only if it lands inside a + root: a bullet pointing outside every root is not this project's tree (a + RAL path, say) and is not followed. + """ + out: list[Path] = [] + for bullet in _where_bullets(mod, tk): + for token in _path_tokens(bullet): + p = Path(token).expanduser() + cands = (([p] if any(_under(p, r) for r in roots) else []) + if p.is_absolute() else [r / p for r in roots]) + for cand in cands: + if cand.exists() and cand not in out: + out.append(cand) return out +#: Which of the three things `run_artifacts` scored: the task's own `## Where +#: to look` paths, the newest run under `output/` because the task named no +#: path at all, or nothing — because the paths it named are not on this laptop. +WHERE, FALLBACK, UNRESOLVED = "where", "fallback", "unresolved" + + def run_artifacts(roots: list[Path], where: list[Path], - since: _dt.datetime) -> tuple: - """`(run_dir, zip_path)` — where this run's `search.summary` lives. + since: _dt.datetime, declared: bool = False) -> tuple: + """`(run_dir, zip_path, source)` — where this run's `search.summary` lives. The zip is authoritative when both exist: seven of the subhalo project's extracted run dirs are stale partial extractions, and reading the wall clock out of one of those reports a run that never finished as short. + + A task that *names* its paths and whose paths are not here has **no** + observable run. Falling back to the newest thing under `output/` there + scores it against a stranger's run and reports that stranger's numbers as + its own — eight of nine tasks came back FAILED on 2026-09-09 and none was + a failed run. `source` is how the caller says which of the three happened, + so a fallback run can never be printed as the task's own. """ - bases = list(where) or [r / "output" for r in roots if (r / "output").is_dir()] + if where: + bases, source = list(where), WHERE + elif declared: + return None, None, UNRESOLVED + else: + bases = [r / "output" for r in roots if (r / "output").is_dir()] + source = FALLBACK seen: dict[Path, Path | None] = {} for base in bases: + if base.is_file(): # a bullet may name the zip itself + if base.suffix == ".zip": + seen[base.with_suffix("")] = base + continue if (base / SUMMARY_NAME).is_file() or (base / ".completed").exists(): seen.setdefault(base, None) try: @@ -1676,12 +1730,12 @@ def run_artifacts(roots: list[Path], where: list[Path], except OSError: continue if not seen: - return None, None + return None, None, source scored = [(max(_mtime(d), _mtime(z) if z else _dt.datetime.min), d, z) for d, z in seen.items()] fresh = [row for row in scored if row[0] >= since] best = max(fresh or scored, key=lambda row: (row[0], str(row[1]))) - return best[1], best[2] + return best[1], best[2], source def summary_minutes(run_dir: Path | None, zip_path: Path | None) -> tuple: @@ -1784,7 +1838,7 @@ def leg_err(errs: list[Path]) -> tuple: def leg_wall(outs: list[Path], run_dir, zip_path, budget_minutes, - budget: str) -> tuple: + budget: str, fallback: bool = False) -> tuple: minutes, raw, source = None, "", "" for path in outs: text = _read(path) @@ -1799,7 +1853,11 @@ def leg_wall(outs: list[Path], run_dir, zip_path, budget_minutes, raw, source = _wall(minutes), path.name break if minutes is None: + # Only this branch reads the run dir, so only this one can be reading + # a run the task never claimed. minutes, raw, source = summary_minutes(run_dir, zip_path) + if minutes is not None and fallback: + source += " — a fallback run, not this task's own" if minutes is None: return (UNOBSERVABLE, "no `.out` ending `Finished.` and no `search.summary` — " @@ -1925,7 +1983,9 @@ def score_task(mod, tk, projects: dict) -> dict: logs = find_logs(roots, stems) since = _since(tk) where = where_paths(mod, tk, roots) - run_dir, zip_path = run_artifacts(roots, where, since) + declared = [tok for b in _where_bullets(mod, tk) for tok in _path_tokens(b)] + run_dir, zip_path, run_source = run_artifacts(roots, where, since, + declared=bool(declared)) pattern = (row.get("witness_file") or "").strip() hits = witness_matches(roots, pattern, since, tokens={r.stem for r in tk.runs} @@ -1933,7 +1993,8 @@ def score_task(mod, tk, projects: dict) -> dict: budget = tk.get("Budget") legs = { "err": leg_err(logs["err"]), - "wall": leg_wall(logs["out"], run_dir, zip_path, _mins(budget), budget), + "wall": leg_wall(logs["out"], run_dir, zip_path, _mins(budget), budget, + fallback=run_source == FALLBACK), "version": leg_version(hits), "checkpoint": leg_checkpoint(pull_manifest(roots), roots, run_dir, tk), "resume": leg_resume(logs["out"]), @@ -1955,6 +2016,8 @@ def score_task(mod, tk, projects: dict) -> dict: "logs": logs, "run_dir": run_dir, "zip": zip_path, + "run_source": run_source, + "declared_where": declared, "witness_hits": hits, "readout": _readout(hits), "pulled_to": pulled_to, @@ -2008,9 +2071,18 @@ def member_block(mod, s: dict) -> list[str]: L += ([f"- [{ref.split('#')[0]}] {ref}" for ref in refs] if refs else ["_(none yet — add them as you rule)_"]) L += ["", "**Where to look yourself**", ""] + if s["run_source"] == UNRESOLVED: + named = ", ".join(f"`{tok}`" for tok in s["declared_where"]) + L.append(f"- run dir: **none** — this task's `## Where to look` names " + f"{named}, which is under none of its project roots. Nothing " + "was scored in its place.") for label, value in (("run dir", s["run_dir"]), ("zip", s["zip"])): if value is not None: - L.append(f"- {label}: `{value}`") + mark = (" — **fallback**: this task names no path, so this is the " + "newest run under `output/`, not necessarily its own" + if label == "run dir" and s["run_source"] == FALLBACK + else "") + L.append(f"- {label}: `{value}`{mark}") for kind in ("out", "err"): for path in s["logs"][kind][:2]: L.append(f"- `.{kind}`: `{path}`") @@ -2486,10 +2558,17 @@ def project_digest(key: str, row: dict, c: dict, scored_by_rel: dict, if s: legs = ", ".join(f"{k} {s['legs'][k][0]}" for k in LEGS) L.append(f" - legs: {legs}") + if s["run_source"] == UNRESOLVED: + L.append(" - run dir: none — the paths this task names " + "are under none of its project roots; nothing " + "was scored in their place") for label, value in (("run dir", s["run_dir"]), ("zip", s["zip"])): if value is not None: - L.append(f" - {label}: `{value}`") + mark = (" — fallback, not this task's own run" + if label == "run dir" + and s["run_source"] == FALLBACK else "") + L.append(f" - {label}: `{value}`{mark}") for kind in ("out", "err"): for path in s["logs"][kind][:1]: L.append(f" - `.{kind}`: `{path}`") diff --git a/tests/test_cortex_conductor.py b/tests/test_cortex_conductor.py index d9b8ec68..7360ffbb 100644 --- a/tests/test_cortex_conductor.py +++ b/tests/test_cortex_conductor.py @@ -998,6 +998,72 @@ def test_the_two_legs_the_laptop_cannot_see_are_unobservable_not_failed(board): assert s["health"] == "SUSPECT" +# --- which run is being scored --------------------------------------------- +# A task names its own results in `## Where to look`. Until 2026-09-09 the +# scorer read only the first token of each bullet and kept it only if it was +# absolute — so every real bullet (relative, and led by a project-row label) +# was dropped, and the scorer silently fell back to the newest run anywhere +# under `output/`. Eight of nine live tasks came back FAILED that morning and +# none of them was a failed run. +def _rewrite_where(board_, rel: str, *bullets: str) -> Path: + """Replace one task's `## Where to look` bullets. Returns the task file.""" + path = board_["root"] / rel + body = "".join(f"- {b}\n" for b in bullets) + path.write_text(re.sub(r"## Where to look\n\n(?:- .*\n)+", + f"## Where to look\n\n{body}", + path.read_text(encoding="utf-8"), count=1), + encoding="utf-8") + return path + + +def test_a_relative_where_to_look_bullet_still_finds_the_run(board): + """The shape every task actually writes: a project-row label, then the + path — relative to the project root, because that is where the run wrote + it. Reading only `bullet.split()[0]`, and only when absolute, found + neither half.""" + _rewrite_where(board, "tasks/example/11_healthy.md", + "`example` (project row): `output/searches/bright`") + s = _score(board)["11_healthy"] + assert s["run_source"] == _cortex.WHERE + assert s["run_dir"] == board["mirror"] / "output/searches/bright/aaaa1111" + assert s["legs"]["wall"][0] == _cortex.PASS + assert s["health"] == "HEALTHY" + + +def test_declared_paths_that_are_not_here_are_unobservable_not_failed(board): + """A run written to a custom `PYAUTO_OUTPUT_DIR` that was never pulled is + a run this laptop cannot see. Scoring the newest thing under `output/` in + its place — here the stale `cccc3333` extraction — reports a stranger's + wall clock as a confident PASS about this task; the door's own contract is + that UNOBSERVABLE is not FAIL, and it is not PASS either.""" + _rewrite_where(board, "tasks/subhalo/01_partial.md", + "`subhalo` (project row): `output_ordered_witness/run_0`") + s = _score(board)["01_partial"] + assert s["run_source"] == _cortex.UNRESOLVED + assert s["run_dir"] is None and s["zip"] is None + assert s["legs"]["wall"][0] == _cortex.UNOBSERVABLE + assert s["health"] == "SUSPECT", "a run we cannot see is not a failed run" + block = _collect(board["root"]).stdout.split("## 01_partial")[1] \ + .split("\n## ")[0] + assert "output_ordered_witness/run_0" in block + assert "run dir: **none**" in block + assert "cccc3333" not in block, "no other run scored in its place" + + +def test_a_fallback_run_says_it_is_a_fallback(board): + """A task that names no path at all still gets the old behaviour — but it + is labelled, in the leg that reads it and in the block the human reads, so + a wall clock from someone else's run is never printed as this task's.""" + _rewrite_where(board, "tasks/subhalo/01_partial.md") + s = _score(board)["01_partial"] + assert s["run_source"] == _cortex.FALLBACK + assert s["run_dir"] == board["sub"] / "output/lens_a/cccc3333" + assert s["legs"]["wall"][0] == _cortex.PASS + assert "fallback run, not this task's own" in s["legs"]["wall"][1] + block = _collect(board["root"]).stdout.split("## 01_partial")[1] + assert "**fallback**" in block + + def test_the_manifests_checkpoints_table_is_keyed_by_the_run_directory(board): """The third lookup (PyAutoCortex decision 51). The subhalo-style member's pull carries no job id, so `runs` is empty and the only name both sides can