Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 96 additions & 16 deletions agents/conductors/cortex/_cortex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2145,11 +2145,16 @@ def apply_ops(root: Path, mod, scored: list) -> list[str]:
return notes


def run_pull(projects: dict, keys: list[str]) -> list[str]:
def run_pull(projects: dict, keys: list[str], roots_by_key=None) -> list[str]:
"""Run each project's own `<sync_cli> pull`. The command is printed before
it runs: this is the one thing `collect` does that touches the cluster, and
it is the human's own CLI doing it."""
it is the human's own CLI doing it.

`roots_by_key` adds the `output*` roots that project's tasks declare, so a
run written to a custom `PYAUTO_OUTPUT_DIR` is fetched without anyone
editing the project's `PULL_DIRS` by hand."""
notes = []
roots_by_key = roots_by_key or {}
for key in keys:
row = projects.get(key, {})
local = (row.get("local_path") or "").strip()
Expand All @@ -2158,9 +2163,11 @@ def run_pull(projects: dict, keys: list[str]) -> list[str]:
notes.append(f"{key}: no `pull` verb in projects.yaml — not pulled")
continue
cmd = [str(Path(local) / cli), "pull"]
print(f"$ cd {local} && {cli} pull")
env = pull_env(roots_by_key.get(key))
print(f"$ {pull_shell(row, roots_by_key.get(key))}")
try:
r = subprocess.run(cmd, cwd=local, capture_output=True, text=True)
r = subprocess.run(cmd, cwd=local, capture_output=True, text=True,
env=env)
except (OSError, subprocess.SubprocessError) as e:
notes.append(f"{key}: pull could not run ({e}) — scored anyway")
continue
Expand Down Expand Up @@ -2191,7 +2198,8 @@ def cmd_collect(root: Path, mod, a) -> int:

if a.pull:
notes += run_pull(projects, sorted({tk.get("Project") or tk.project_dir
for tk in tasks}))
for tk in tasks}),
output_roots_by_project(mod, tasks))
stamp = a.refreshed.strip() or (_utc_now() if a.pull else "")

scored = [score_task(mod, tk, projects) for tk in tasks]
Expand Down Expand Up @@ -2309,6 +2317,65 @@ def checkin_keys(projects: dict, tasks: list, only: list) -> tuple:
return sorted(keys), notes


#: A caller may add output roots to a project's own `PULL_DIRS` through this
#: environment variable — the same override convention `hpc/sync.conf` already
#: documents for `HPC_HOST` / `HPC_BASE` / `PROJECT_NAME`. Space-separated,
#: because an environment variable cannot carry a bash array. The scripts
#: *append* it, so a project's own roots always pull and an unset variable
#: reproduces today's behaviour exactly.
PULL_DIRS_ENV = "PYAUTO_PULL_DIRS"

#: Only `output*` roots are ever passed. A pull rsyncs **remote → local**, so
#: asking one for `results/`, `wiki/` or `scripts/` — all of which real tasks
#: name in `## Where to look` — would overwrite the human's own source with the
#: cluster's copy. Runs written to a custom `PYAUTO_OUTPUT_DIR` are named by
#: convention (`output_ordered_witness`, `output_sed`), and that convention is
#: the guard.
OUTPUT_ROOT_RE = re.compile(r"^output[\w.-]*$")


def declared_output_roots(mod, tasks) -> list[str]:
"""The `output*` roots these tasks say they wrote to — first segment only.

A task already names where its results are; that is also the answer to
"what should the pull fetch?". Absolute tokens are skipped: they are not
project-relative roots, and `PULL_DIRS` entries are relative to the remote
project root.
"""
out: list[str] = []
for tk in tasks:
for bullet in _where_bullets(mod, tk):
for token in _path_tokens(bullet):
p = Path(token)
if p.is_absolute() or not p.parts:
continue
head = p.parts[0]
if OUTPUT_ROOT_RE.match(head) and head not in out:
out.append(head)
return sorted(out)


def output_roots_by_project(mod, tasks) -> dict[str, list[str]]:
"""`{project key: the `output*` roots its tasks declare}`."""
by_key: dict[str, list] = {}
for tk in tasks:
by_key.setdefault(tk.get("Project") or tk.project_dir, []).append(tk)
return {key: declared_output_roots(mod, group)
for key, group in by_key.items()}


def pull_env(roots) -> dict | None:
"""The environment for one project's pull, or `None` to inherit unchanged.

`None` rather than a copy of `os.environ` matters: a project that declares
no extra root runs exactly the command it runs today.
"""
roots = [r for r in (roots or []) if r]
if not roots:
return None
return {**os.environ, PULL_DIRS_ENV: " ".join(roots)}


def pull_cmd(row: dict) -> tuple | None:
"""`(argv, cwd)` for this project's own `<sync_cli> pull`, or None when the
row has no such verb. Every path comes from the row."""
Expand All @@ -2319,14 +2386,21 @@ def pull_cmd(row: dict) -> tuple | None:
return [str(Path(local) / cli), "pull"], local


def pull_shell(row: dict) -> str:
"""The pull as a human would type it — what `--dry-run` prints."""
def pull_shell(row: dict, roots=()) -> str:
"""The pull as a human would type it — what `--dry-run` prints.

The extra roots are shown as the assignment they are, so the dry run is a
command the human can paste and get the same pull.
"""
cmd = pull_cmd(row)
return (f"cd {cmd[1]} && {row.get('sync_cli')} pull" if cmd
else "(no `pull` verb in projects.yaml)")
if not cmd:
return "(no `pull` verb in projects.yaml)"
roots = [r for r in (roots or []) if r]
prefix = f'{PULL_DIRS_ENV}="{" ".join(roots)}" ' if roots else ""
return f"cd {cmd[1]} && {prefix}{row.get('sync_cli')} pull"


def run_pull_streamed(projects: dict, keys: list) -> dict:
def run_pull_streamed(projects: dict, keys: list, roots_by_key=None) -> dict:
"""`{key: (rc, note)}` — each project's own pull, **streamed**.

Not captured: a pull runs for minutes and the human is watching this one
Expand All @@ -2341,9 +2415,10 @@ def run_pull_streamed(projects: dict, keys: list) -> dict:
results[key] = (None, "no `pull` verb in projects.yaml — not pulled")
continue
argv, cwd = cmd
print(f"\n$ {pull_shell(row)}", flush=True)
roots = (roots_by_key or {}).get(key)
print(f"\n$ {pull_shell(row, roots)}", flush=True)
try:
rc = subprocess.run(argv, cwd=cwd).returncode
rc = subprocess.run(argv, cwd=cwd, env=pull_env(roots)).returncode
except (OSError, subprocess.SubprocessError) as e:
results[key] = (None, f"pull could not run ({e}) — scored anyway")
continue
Expand Down Expand Up @@ -2617,6 +2692,9 @@ def cmd_checkin(root: Path, mod, a) -> int:
for tk in live:
live_by_key.setdefault(tk.get("Project") or tk.project_dir,
[]).append(tk.rel)
# What each project's live tasks say they wrote to — added to that
# project's own `PULL_DIRS` so a custom `PYAUTO_OUTPUT_DIR` run arrives.
roots_by_key = output_roots_by_project(mod, live)

if not a.apply: # the default: say what it would do, touch nothing
print(f"cortex check-in (dry run) — {len(keys)} project(s), "
Expand All @@ -2625,7 +2703,7 @@ def cmd_checkin(root: Path, mod, a) -> int:
for key in keys:
row = projects.get(key, {})
print(f"\n{key} [{(row.get('status') or '?').strip()}]")
print(f" pull: $ {pull_shell(row)}")
print(f" pull: $ {pull_shell(row, roots_by_key.get(key))}")
print(f" root: {pull_root(row) or '(no readable pull root)'}")
rels = live_by_key.get(key, [])
print(" score: " + (", ".join(rels) if rels
Expand Down Expand Up @@ -2653,7 +2731,7 @@ def cmd_checkin(root: Path, mod, a) -> int:
# --- 1. sync ---------------------------------------------------------
pulls: dict[str, tuple] = {}
if not a.skip_pull:
pulls = run_pull_streamed(projects, keys)
pulls = run_pull_streamed(projects, keys, roots_by_key)
for key, (rc, note) in pulls.items():
if rc != 0:
if note:
Expand All @@ -2665,8 +2743,10 @@ def cmd_checkin(root: Path, mod, a) -> int:
notes.append(f"{key}: pulled, but no readable pull root — no "
"manifest written")
continue
written = write_pull_manifest(target, key, pull_shell(row), rc,
live_by_key.get(key, []))
written = write_pull_manifest(target, key,
pull_shell(row,
roots_by_key.get(key)),
rc, live_by_key.get(key, []))
notes.append(f"{key}: pulled → {written}" if written else
f"{key}: pulled, but the manifest could not be written")
stamp = a.refreshed.strip() or (_utc_now() if pulls else
Expand Down
82 changes: 82 additions & 0 deletions tests/test_cortex_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,88 @@ def test_a_fallback_run_says_it_is_a_fallback(board):
assert "**fallback**" in block


# --- what the pull is asked to fetch ---------------------------------------
# Each science project's `hpc/sync` hard-codes `PULL_DIRS`, so a run written to
# a custom `PYAUTO_OUTPUT_DIR` never came down at all (166MB of euclid_dr1_prelim
# results sat on RAL on 2026-09-09). The check-in now passes the `output*` roots
# its tasks declare. Only `output*`: a pull rsyncs remote -> local, so handing it
# `scripts/` or `wiki/` would overwrite the human's own source.
def test_only_output_shaped_roots_are_ever_offered_to_a_pull(board):
"""The guard that keeps a pull from overwriting local source. Real tasks
name `results/`, `wiki/…`, `scripts/…` and `hpc/…` in the same bullets as
their output roots."""
_rewrite_where(board, "tasks/example/11_healthy.md",
"`example` (project row): `output_ordered_witness/run_0/`",
"results: `results/searches/bright.json`",
"the write-up: `wiki/project/state.md`",
"the submit script: `hpc/batch_cpu/submit_vis_lp`",
"scripts: `scripts/control_unordered.py`",
"absolute: `/mnt/ral/jnightin/example/output_elsewhere`")
mod = _cortex.load_cortex(board["root"])
tk = [t for t in mod.load_tasks(board["root"])[0]
if t.slug == "11_healthy"][0]
assert _cortex.declared_output_roots(mod, [tk]) == ["output_ordered_witness"]


def test_the_roots_of_a_projects_tasks_are_pooled_and_deduped(board):
"""One pull serves every live task of a project, so the roots are the
union — and `output/` named twice is still one directory."""
_rewrite_where(board, "tasks/example/11_healthy.md",
"`output_ordered_witness/run_0/`", "`output/searches`")
_rewrite_where(board, "tasks/example/12_resumed.md",
"`output_sed/run_9/`", "`output/searches`")
mod = _cortex.load_cortex(board["root"])
tasks = [t for t in mod.load_tasks(board["root"])[0]
if t.slug in ("11_healthy", "12_resumed")]
by_key = _cortex.output_roots_by_project(mod, tasks)
assert by_key["example"] == ["output", "output_ordered_witness",
"output_sed"]


def test_a_project_declaring_no_extra_root_pulls_exactly_as_before(board):
"""`pull_env` returns None, not a copy of the environment: a project with
nothing to add runs the command it runs today, byte for byte."""
mod = _cortex.load_cortex(board["root"])
projects = mod.load_projects(board["root"])[0]
row = projects["example"]
assert _cortex.pull_env([]) is None
assert _cortex.pull_env(None) is None
assert _cortex.PULL_DIRS_ENV not in _cortex.pull_shell(row)
assert _cortex.pull_shell(row) == _cortex.pull_shell(row, [])


def test_the_declared_roots_reach_the_pull_as_an_environment_assignment(board):
"""The seam itself: `hpc/sync` reads `PYAUTO_PULL_DIRS` the same way it
already reads `HPC_HOST` — so the dry run prints an assignment the human
can paste, and the real pull sets it in the child's environment."""
mod = _cortex.load_cortex(board["root"])
projects = mod.load_projects(board["root"])[0]
row = projects["example"]
roots = ["output_ordered_witness"]
assert 'PYAUTO_PULL_DIRS="output_ordered_witness" hpc/sync pull' in \
_cortex.pull_shell(row, roots)

seen = {}

class _Done:
returncode = 0

def _fake_run(argv, **kw):
seen["argv"], seen["env"] = argv, kw.get("env")
return _Done()

monkey = _cortex.subprocess.run
_cortex.subprocess.run = _fake_run
try:
_cortex.run_pull_streamed(projects, ["example"], {"example": roots})
assert seen["env"][_cortex.PULL_DIRS_ENV] == "output_ordered_witness"
assert seen["env"]["PATH"], "the child keeps the inherited environment"
_cortex.run_pull_streamed(projects, ["example"], {})
assert seen["env"] is None, "nothing declared, nothing overridden"
finally:
_cortex.subprocess.run = monkey


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
Expand Down
Loading