diff --git a/app/nullius/execution.py b/app/nullius/execution.py index 2eb16bc4..aad62bf5 100644 --- a/app/nullius/execution.py +++ b/app/nullius/execution.py @@ -253,7 +253,7 @@ class Plan: url_var: str = "MODEL_API_URL" # same for the endpoint, which a run on it cannot go without; "" for none base_url: str = "" # the endpoint when url_var names none: a provider's own (OpenRouter's), or "" for its clients' default model: str = "" - jobs: List[dict] = field(default_factory=list) # the job directories the script writes: [{"name", "agent", "dataset"}] + jobs: List[dict] = field(default_factory=list) # the job directories the script writes: [{"name", "agent", "dataset"}], a shard's also its "launch" parts: List[dict] = field(default_factory=list) # the run, one agent at a time: [{"id", "label", "script"}]; empty: the whole script at once recipe: dict = field(default_factory=dict) # the builder's recipe the script was compiled from, so the run can be opened again as one; {} for a plan from before runner: str = "harbor" # or "standard" (standard/recipe.py): the Nullius standard's own cli diff --git a/app/nullius/runs/launches.py b/app/nullius/runs/launches.py index 1295ea02..d90eb39a 100644 --- a/app/nullius/runs/launches.py +++ b/app/nullius/runs/launches.py @@ -18,7 +18,8 @@ config names the same ``launch`` id (``Job.launch_id``), each run on a machine of its own, perhaps by a run record of its own, perhaps hours apart - are one launch: a shard joins the launch that holds another shard of its id before -any other rule is asked. +any other rule is asked. A run record's plan names the launch id of its shards +too, so a shard joins its launch before its job is written. ``group`` reads no file: it takes the parsed jobs and the records' job lists, and groups. Newest launch first. ``fold`` reads one participant's shards as @@ -129,20 +130,21 @@ def fold(shards): def group(jobs, records: dict | None = None, window: float = WINDOW_SEC) -> list[Launch]: """``jobs``: the parsed jobs (``job_id``, ``dataset``, ``started_at``). - ``records``: ``{execution id: (name, [job ids], launched at)}``, the builder's - run records, the time in seconds. Returns the launches, newest first, each job in exactly one.""" + ``records``: ``{execution id: (name, [job ids], launched at, {launch ids})}``, + the builder's run records, the time in seconds, the launch ids its plan + names for its shards. Returns the launches, newest first, each job in exactly one.""" records = records or {} by_id = {j.job_id: j for j in jobs} placed: set[str] = set() out: list[Launch] = [] - for key, (name, ids, launched) in records.items(): + for key, (name, ids, launched, planned) in records.items(): mine = [i for i in ids if i in by_id and i not in placed] pending = [i for i in ids if i not in by_id and i not in placed] if not mine and not pending: continue placed.update(mine + pending) written = sorted((by_id[i] for i in mine), key=_start_key) - ids = {_launch_id(j) for j in written} - {""} + ids = ({_launch_id(j) for j in written} | set(planned)) - {""} home = next((l for l in out if l.launch_ids & ids), None) if home is not None: # another shard of a launch a record placed already _join(home, written, pending) diff --git a/app/nullius/standard/recipe.py b/app/nullius/standard/recipe.py index 13f9a7ff..4a5d7e10 100644 --- a/app/nullius/standard/recipe.py +++ b/app/nullius/standard/recipe.py @@ -520,9 +520,11 @@ def compile(recipe: dict, defs: Optional[Dict[str, List[dict]]] = None, *, def word_of(d: dict, p: dict) -> str: return "-".join(([d["id"]] if many else []) + ([definitions.job_word(p["o"])] if several else [])) + def launch_of(d: dict, p: dict) -> str: + return "-".join(filter(None, [name, word_of(d, p)])) + def job_of(d: dict, p: dict, index: int) -> str: - base = "-".join(filter(None, [name, word_of(d, p)])) - return f"{base}-shard-{index}of{of}" if of > 1 else base + return f"{launch_of(d, p)}-shard-{index}of{of}" if of > 1 else launch_of(d, p) def options_for_pick(d: dict, p: dict) -> dict: """The benchmark's options for this dataset and this pick: a setup's own, or those of the port.""" @@ -530,7 +532,6 @@ def options_for_pick(d: dict, p: dict) -> dict: def command_for(d: dict, p: dict, index: int) -> str: o, control, given = p["o"], p["control"], p["given"] - launch = "-".join(filter(None, [name, word_of(d, p)])) m = p["model"] if p.get("setup") else model words = ["nullius", "standard", "run", definitions.ref_of(b), o["_ref"], *(["--model", m] if m else []), "--provider", provider, "--dataset", d["id"], *(["--limit", str(limit)] if isinstance(limit, int) else []), "-j", str(conc), @@ -543,7 +544,7 @@ def command_for(d: dict, p: dict, index: int) -> str: "--name", job_of(d, p, index), *[w for k, v in given.items() for w in ("--option", f"{k}={v}")], *(["--backend", "container"] if backend == "container" else []), *(["--unsandboxed"] if side == "agent" and not control and not p.get("setup") and backend == "local" else []), - *(["--shard", f"{index}/{of}", "--launch", launch] if of > 1 else []), + *(["--shard", f"{index}/{of}", "--launch", launch_of(d, p)] if of > 1 else []), *limits.cli_words(limits.of(targets[index - 1])), "--from-run"] return " ".join(shlex.quote(w) for w in words) + ' --jobs "${NULLIUS_JOBS:-jobs_standard}"' @@ -566,7 +567,9 @@ def script_for(d: dict, p: dict, command: str, index: int) -> str: parts.append({"id": word_of(d, p) or definitions.job_word(p["o"]), "label": " · ".join(([d.get("label") or d["id"]] if many else []) + [p["o"]["label"]]) + (f" · shard {i}/{of}" if of > 1 else ""), "script": script_for(d, p, command, i)}) - jobs.append({"name": job_of(d, p, i), "agent": p["o"]["_ref"], "dataset": definitions.ref_of(b)}) + # a shard's launch id, so its run record joins the launch before its job is written (runs.launches) + jobs.append({"name": job_of(d, p, i), "agent": p["o"]["_ref"], "dataset": definitions.ref_of(b), + **({"launch": launch_of(d, p)} if of > 1 else {})}) out["shards"].append({"where": where, "index": i, "of": of, "name": f"{name}-shard-{i}of{of}" if of > 1 else name, "command": "\n".join(commands[-len(pairs):]), "script": parts[0]["script"], "parts": parts, "jobs": jobs}) out["script"] = "\n".join(pt["script"] for pt in out["shards"][0]["parts"]) if len(pairs) > 1 else out["shards"][0]["script"] diff --git a/app/tests/test_launches.py b/app/tests/test_launches.py index 248e8d69..0f1c0076 100644 --- a/app/tests/test_launches.py +++ b/app/tests/test_launches.py @@ -43,7 +43,7 @@ def test_the_window_counts_from_the_launchs_first_run_and_a_dataset_splits(self) def test_the_run_record_wins_and_names_the_runs_not_written_yet(self): jobs = [job("tau-latest3-0919-132944-agent", 24), job("tau-latest3-0919-132944-nop", 0), job("other", 5)] records = {"r1": ("tau-latest3-0919-132944", ["tau-latest3-0919-132944-nop", "tau-latest3-0919-132944-agent", - "tau-latest3-0919-132944-claude-code"], None)} + "tau-latest3-0919-132944-claude-code"], None, set())} out = L.group(jobs, records) rec = next(l for l in out if l.source == "record") self.assertEqual((rec.key, rec.name), ("run:r1", "tau-latest3-0919-132944")) @@ -53,17 +53,17 @@ def test_the_run_record_wins_and_names_the_runs_not_written_yet(self): self.assertEqual([l.job_ids for l in out if l.source == "inferred"], [["other"]]) # the record's jobs never join an inferred launch def test_a_record_with_only_pending_runs_is_still_a_launch(self): - out = L.group([], {"r2": ("sweep", ["sweep-a", "sweep-b"], None)}) + out = L.group([], {"r2": ("sweep", ["sweep-a", "sweep-b"], None, set())}) self.assertEqual([(l.pending, l.started, l.dataset) for l in out], [(["sweep-a", "sweep-b"], None, None)]) def test_a_launch_with_no_job_yet_sorts_by_when_it_was_launched(self): later = (T0 + timedelta(hours=5)).timestamp() - out = L.group([job("older", 0), job("newer", 60 * 10)], {"r": ("r", ["r-nop", "r-agent"], later)}) + out = L.group([job("older", 0), job("newer", 60 * 10)], {"r": ("r", ["r-nop", "r-agent"], later, set())}) self.assertEqual([l.key for l in out], ["inferred:newer", "run:r", "inferred:older"]) self.assertEqual(out[1].started, T0 + timedelta(hours=5)) def test_a_launchs_written_jobs_outrank_when_it_was_launched(self): - out = L.group([job("r-nop", 3)], {"r": ("r", ["r-nop", "r-agent"], (T0 + timedelta(hours=5)).timestamp())}) + out = L.group([job("r-nop", 3)], {"r": ("r", ["r-nop", "r-agent"], (T0 + timedelta(hours=5)).timestamp(), set())}) self.assertEqual(out[0].started, T0 + timedelta(minutes=3)) def test_a_run_with_no_start_is_its_own_launch_and_sorts_last(self): @@ -78,7 +78,7 @@ def test_runs_that_share_no_prefix_are_named_not_wildcarded(self): def test_every_job_lands_in_exactly_one_launch(self): jobs = [job(f"j{k}", k * 7, dataset="ds" + str(k % 2)) for k in range(12)] - records = {"r": ("r", ["j3", "j4", "gone"], None)} + records = {"r": ("r", ["j3", "j4", "gone"], None, set())} placed = [i for l in L.group(jobs, records) for i in l.job_ids] self.assertEqual(sorted(placed), sorted(j.job_id for j in jobs)) diff --git a/app/tests/test_standard_recipe.py b/app/tests/test_standard_recipe.py index 076791f4..fd77857f 100644 --- a/app/tests/test_standard_recipe.py +++ b/app/tests/test_standard_recipe.py @@ -450,7 +450,7 @@ def test_several_machines_are_one_shard_each_under_one_launch(self): for i, sh in enumerate(out["shards"], 1): self.assertIn(f"--name fake-0101-120000-shard-{i}of3", sh["script"]) self.assertIn(f"--shard {i}/3 --launch fake-0101-120000 --from-run", sh["script"]) - self.assertEqual(sh["jobs"], [{"name": sh["name"], "agent": "fake", "dataset": "fakebench"}]) + self.assertEqual(sh["jobs"], [{"name": sh["name"], "agent": "fake", "dataset": "fakebench", "launch": "fake-0101-120000"}]) self.assertEqual([j["name"] for j in out["jobs"]], [sh["name"] for sh in out["shards"]]) self.assertEqual(len(out["parts"]), 3) self.assertIn("3 shards, one per machine", out["review"][-1][2]) diff --git a/app/tests/test_standard_shard.py b/app/tests/test_standard_shard.py index 5eba633e..c19da339 100644 --- a/app/tests/test_standard_shard.py +++ b/app/tests/test_standard_shard.py @@ -278,17 +278,25 @@ def test_shards_of_one_launch_are_one_launch_hours_apart(self): def test_shards_started_by_separate_run_records_join_one_launch(self): jobs = [a_job("r-shard-1of2", 0, "r"), a_job("r-shard-2of2", 0.1, "r")] - records = {"e1": ("r-shard-1of2", ["r-shard-1of2"], None), "e2": ("r-shard-2of2", ["r-shard-2of2", "r-shard-2of2-pending"], None)} + records = {"e1": ("r-shard-1of2", ["r-shard-1of2"], None, set()), "e2": ("r-shard-2of2", ["r-shard-2of2", "r-shard-2of2-pending"], None, set())} out = launches.group(jobs, records) self.assertEqual(len(out), 1) self.assertEqual((out[0].job_ids, out[0].pending), (["r-shard-1of2", "r-shard-2of2"], ["r-shard-2of2-pending"])) self.assertEqual(out[0].prefix, "r-shard-*") self.assertEqual(out[0].title, "r") # named by its launch id, not the jobs' prefix + def test_shards_whose_jobs_are_not_written_join_their_launch_by_the_plans_launch_id(self): + records = {f"e{i}": (f"r-shard-{i}of3", [f"r-shard-{i}of3"], None, {"r"}) for i in (1, 2, 3)} + for written in ([], [a_job("r-shard-2of3", 0, "r")]): + out = launches.group(written, records) + self.assertEqual(len(out), 1, written) + self.assertEqual(sorted(out[0].job_ids + out[0].pending), ["r-shard-1of3", "r-shard-2of3", "r-shard-3of3"]) + self.assertEqual(out[0].title, "r") + def test_a_launch_of_no_shards_or_of_several_launches_is_named_by_its_prefix(self): out = launches.group([a_job("x-1", 0), a_job("x-2", 0.2)]) self.assertEqual(out[0].title, out[0].prefix) - mixed = launches.group([a_job("m-a", 0, "L1"), a_job("m-b", 0.1, "L2")], {"e": ("m", ["m-a", "m-b"], None)}) + mixed = launches.group([a_job("m-a", 0, "L1"), a_job("m-b", 0.1, "L2")], {"e": ("m", ["m-a", "m-b"], None, set())}) self.assertEqual((len(mixed), mixed[0].title), (1, mixed[0].prefix)) def test_a_job_with_no_launch_groups_as_before(self): diff --git a/app/ui_tests/test_launch_shards.py b/app/ui_tests/test_launch_shards.py index af054996..249583d8 100644 --- a/app/ui_tests/test_launch_shards.py +++ b/app/ui_tests/test_launch_shards.py @@ -54,7 +54,7 @@ def write_launch(root, runs, nop=None, stopped="terminus-2", written=("nop", "ji os.remove(os.path.join(d, "result.json")) jobs.append(parse.parse_job(d)) plan = execution.Plan(name=f"{NAME}-shard-{i}of2", script="", runner="standard", host=hosts.parse(host) if host else None, - jobs=[{"name": f"{NAME}-{p}-shard-{i}of2", "agent": p} for p in order], + jobs=[{"name": f"{NAME}-{p}-shard-{i}of2", "agent": p, "launch": f"{NAME}-{p}"} for p in order], parts=[{"id": p, "label": p, "script": "true"} for p in order]) record = ({"id": "s1", "state": "done", "launched": order[-1], "part": len(order) - 1, "ends": {p: DONE for p in order}} if i == 1 else {"id": "s2", "state": "stopped", "launched": stopped, "part": order.index(stopped), "exit_code": 130, @@ -117,6 +117,11 @@ def test_each_participant_is_one_row_of_its_shards_and_every_shard_keeps_its_row self.assertIn('title="fold-terminus-2-shard-2of2">shard 2 · host-2', block) self.assertIn('title="fold-terminus-2-shard-1of2">shard 1 · this machine', block) + def test_a_shard_that_wrote_no_job_yet_is_in_the_launch(self): + block = self.block(written=()) # one launch block, asserted by block() + for p in CONTROLS + AGENTS: + self.assertIn(f"{NAME}-{p}-shard-2of2", block) + def test_a_folded_row_shows_the_merged_job_and_says_what_did_not_run(self): block = self.block() row = self.fold_row(block, "terminus-2") diff --git a/app/web/runs.py b/app/web/runs.py index 3361fd2b..6e123eb2 100644 --- a/app/web/runs.py +++ b/app/web/runs.py @@ -115,8 +115,13 @@ def _grouped(): mine = [e for e in execution.EXECUTIONS.values() if _launched_here(e, have)] merged = launches.merged_sources(jobs) jobs = [j for j in jobs if j.job_id not in merged] - records = {e.id: (e.plan.name, [str(x.get("name")) for x in e.plan.jobs if str(x.get("name")) not in merged], e.started) for e in mine} - return jobs, have, mine, launches.group(jobs, records) + return jobs, have, mine, launches.group(jobs, {e.id: _record(e, merged) for e in mine}) + + +def _record(ex, merged=()) -> tuple: + """A run record as ``launches.group`` reads it, less the jobs a merge stands for.""" + names = [str(x.get("name")) for x in ex.plan.jobs if str(x.get("name")) not in merged] + return ex.plan.name, names, ex.started, {str(x.get("launch") or "") for x in ex.plan.jobs} def _launched_here(ex, have) -> bool: