diff --git a/agentdescent/async_evolve.py b/agentdescent/async_evolve.py index 14ce9698..eff77bec 100644 --- a/agentdescent/async_evolve.py +++ b/agentdescent/async_evolve.py @@ -548,6 +548,7 @@ def async_evolve( self_verify = _runtime.self_verify held_out_frac = _runtime.held_out_frac checkpointing = _runtime.checkpointing + round_base = _runtime.round_base cheap_eval_tasks = _runtime.cheap_eval_tasks fusion_tournament = _runtime.fusion_tournament solved_threshold = _runtime.solved_threshold @@ -583,6 +584,7 @@ def async_evolve( shuffle=shuffle, seed=seed, usage=usage, verifier=_pol.verifier, ledger_impl=_pol.ledger, policies_bundle=_pol, checkpointing=checkpointing, + round_base=round_base, allocator=allocator, record_trajectories=record_trajectories, trajectory_store=trajectory_store, contract=contract, extra_artifacts=extra_artifacts, diff --git a/agentdescent/evolution.py b/agentdescent/evolution.py index a5401a45..e35d6e9a 100644 --- a/agentdescent/evolution.py +++ b/agentdescent/evolution.py @@ -2251,13 +2251,13 @@ def invalidate_dependents(self, changed_artifact_id: str, #: for nothing. Read by ``record_round``; set by ``_build_engine`` from #: ``evolve(checkpointing=)``. checkpointing: bool = False - #: Rounds the search had already run when this process started, from the - #: restored checkpoint. Both drivers number rounds from zero per process - #: (``index=r`` in one, ``index=len(history)`` in the other), so without - #: this offset a resume writes ``round_0.json`` over the previous run's, - #: reports ``round 0`` for a search 40 rounds deep, and hands - #: ``_prune_round_files`` a history that no longer sorts. - checkpoint_round_base: int = 0 + #: The number this process's first round gets (#1092). Both drivers number + #: rounds from zero per process (``index=r`` in one, ``index=len(history)`` + #: in the other), so a relaunch on an existing ledger renumbered the rows it + #: appended; the same offset keeps checkpoint names and the reported round + #: increasing. ``_build_engine`` sets it to the further of the restored + #: checkpoint and ``evolve(round_base=)`` (the ledger's last round + 1). + round_base: int = 0 #: Which decision procedure this run is using (#943), as #: :func:`~agentdescent.policies.policy_digest`'s 64-hex content hash. #: Computed once in ``_build_engine`` from the *resolved* Policies (legacy @@ -2329,6 +2329,11 @@ def record_round(self, *, index: int, reward: float, n_items: int, different questions and only the loop knows the second. """ reports = list(reports) + # Number the round against the run, not against the process: a resume + # appends to a ledger that already has rows, and ``index`` is what the + # row says (#1092). Applied here rather than at the two call sites so a + # third driver cannot forget it. + index = self.round_base + index # Offer the reports (with the diff each candidate measured) to a # recorder before they are reduced to counters. `show` reads rejected # candidates from what this writes (#452). @@ -2368,7 +2373,7 @@ def record_round(self, *, index: int, reward: float, n_items: int, from .checkpoint import save_checkpoint save_checkpoint( repo, - self.checkpoint_round_base + index, + index, self.aggregator, artifact_id=self.artifact_id, round_info={ @@ -2551,6 +2556,7 @@ def _build_engine(tasks, reward, *, agent, run, propose, strategy, initial_state ledger_impl: Optional[Any] = None, policies_bundle: Optional[Policies] = None, checkpointing: bool = False, + round_base: int = 0, allocator: Optional[Any] = None, record_trajectories: bool = False, trajectory_store: Optional[Any] = None, @@ -3052,13 +3058,17 @@ def _default_aggregator(ledger, verifier, audit, config, policy): expected_aggregator_type=type(aggregator).__name__, ) # Where this process's round numbering starts. The checkpoint records the - # last round the *search* reached, so the next one is that plus one; a run - # with no checkpoint starts at zero, as it always did. + # last round the *search* reached, so the next one is that plus one; a + # caller resuming on an existing ledger passes its last round + 1 as + # ``round_base``. The further of the two wins: a checkpoint is search state + # and a ledger is the record of it, so either can be ahead of the other and + # the numbering must never go backwards (#1092). _cp_round_base = 0 if _cp_payload is not None: _last = _cp_payload.get("round") if isinstance(_last, int) and _last >= 0: _cp_round_base = _last + 1 + _round_base = max(_cp_round_base, int(round_base or 0)) # Imported here rather than at module scope: `executor` reaches `workspec`, # which reaches back here for `Task`. @@ -3103,7 +3113,7 @@ def _default_aggregator(ledger, verifier, audit, config, policy): test=test, executor=executor, meter=meter, scratch_repo=scratch, checkpoint_payload=_cp_payload, checkpointing=checkpointing, - checkpoint_round_base=_cp_round_base, + round_base=_round_base, policy_digest=policy_digest_value) # A contract-breaking commit lands atomically (commit_atomic); this is the # consumer side -- re-measure the artifacts that depended on the changed @@ -3218,6 +3228,12 @@ def evolve( #: ``repo_path`` in a tempdir) would pay the write cost for nothing. #: Turn it on when the ledger is durable and you might resume. checkpointing: bool = False, + #: The number this run's first round is recorded as (#1092). ``0`` (the + #: default) numbers from zero, as every fresh run always did. A caller + #: relaunching on an existing ledger passes its last recorded round + 1, so + #: the rows it appends keep increasing instead of repeating the numbering. + #: It does not shorten the run: ``rounds`` is still the number executed. + round_base: int = 0, agg_config: Optional[AggregatorConfig] = None, staleness_policy: Optional[StalenessPolicy] = None, aggregator_factory: Optional[AggregatorFactory] = None, @@ -3556,6 +3572,14 @@ def evolve( (state that does not serialise, a lock another process holds, a full disk) is skipped, never raised: the cost of a missing checkpoint is a resume that starts the search fresh, which is what ``False`` does. + round_base: + The number this run's **first** round is recorded as. ``0`` (the + default) numbers from zero, as every fresh run did. A caller relaunching + on a ledger that already holds rounds passes its last recorded round + 1, + so the rows it appends keep increasing instead of repeating ``0, 1, 2``. + Numbering only: ``rounds`` is still the number of rounds executed. A + restored checkpoint further ahead still wins (``max`` of the two), so + numbering never goes backwards. agg_config: Tuning for the reference aggregator (batching, acceptance risk, trust region, staleness tolerance). @@ -3758,7 +3782,8 @@ def evolve( async_ratio=async_ratio, resync_on_commit=resync_on_commit, pipelined_gate=pipelined_gate, gate_workers=gate_workers, self_verify=self_verify, held_out_frac=held_out_frac, - checkpointing=checkpointing, cheap_eval_tasks=cheap_eval_tasks, + checkpointing=checkpointing, round_base=round_base, + cheap_eval_tasks=cheap_eval_tasks, fusion_tournament=fusion_tournament, solved_threshold=solved_threshold, shuffle=shuffle, seed=seed, population_islands=population_islands, ) @@ -3793,6 +3818,7 @@ def evolve( self_verify = _runtime.self_verify held_out_frac = _runtime.held_out_frac checkpointing = _runtime.checkpointing + round_base = _runtime.round_base cheap_eval_tasks = _runtime.cheap_eval_tasks fusion_tournament = _runtime.fusion_tournament solved_threshold = _runtime.solved_threshold @@ -3894,7 +3920,10 @@ def evolve( # Bags already folded into the flat locals above; forward them so # the async surface does not look like it dropped the new knobs, # and a bag-only caller still works if resolve is re-run (#804). - budget=budget, runtime=runtime, observability=observability) + # The *resolved* runtime bag, not the caller's: a flat shortcut + # (``round_base=`` included, #1092) is folded into ``_runtime``, and + # forwarding the raw bag would drop it on this path. + budget=budget, runtime=_runtime, observability=observability) if pipelined_gate: # The mirror of the block above, and the same reasoning: a knob accepted @@ -3954,6 +3983,7 @@ def evolve( shuffle=shuffle, seed=seed, usage=usage, verifier=_pol.verifier, ledger_impl=_pol.ledger, policies_bundle=_pol, checkpointing=checkpointing, + round_base=round_base, allocator=allocator, record_trajectories=record_trajectories, trajectory_store=trajectory_store, contract=contract, extra_artifacts=extra_artifacts, diff --git a/agentdescent/evolve_settings.py b/agentdescent/evolve_settings.py index f4dcc0de..502e75a4 100644 --- a/agentdescent/evolve_settings.py +++ b/agentdescent/evolve_settings.py @@ -45,7 +45,8 @@ "asynchronous", "async_ratio", "resync_on_commit", "pipelined_gate", "gate_workers", "max_seconds", "max_rollouts", "max_calls", "max_tokens", "stop_on_diminishing_returns", "efficiency_floor", "call_budget", - "self_verify", "held_out_frac", "repo_path", "checkpointing", "agg_config", + "self_verify", "held_out_frac", "repo_path", "checkpointing", "round_base", + "agg_config", "staleness_policy", "aggregator_factory", "oracle_budget", "cheap_eval_tasks", "fusion_tournament", "solved_threshold", "shuffle", "seed", "on_round", "on_reports", "stop_when", "verbose", "usage", @@ -130,6 +131,11 @@ class RuntimeSettings: self_verify: bool = True held_out_frac: float = 0.4 checkpointing: bool = False + #: The number this process's first round is recorded as (#1092). A resume + #: passes one past the last round already in the run's ledger, so appended + #: rows keep increasing instead of repeating ``0, 1, 2``. Numbering only: + #: the run still executes its full ``rounds``. + round_base: int = 0 cheap_eval_tasks: Optional[int] = None fusion_tournament: Optional[bool] = None solved_threshold: float = 0.999 diff --git a/agentdescent/runstore.py b/agentdescent/runstore.py index c8984950..ecb78d89 100644 --- a/agentdescent/runstore.py +++ b/agentdescent/runstore.py @@ -828,7 +828,18 @@ def _progress() -> None: usage.on_change = _progress rd.update_status(state="running", pid=os.getpid(), started=time.time(), kind=spec.kind, target=spec.target) - best: List[float] = [] + # A resume relaunches on this run's own directory, so the ledger already + # holds rows and its panel already holds a best reward. Both are read here, + # before the run starts, or the second pass renumbers from zero and reports + # a best that is only its own (#1092). + previous = rd.rounds() + best: List[float] = [r for r in + (_finite_or_none(row.get("held_out_reward")) + for row in previous) if r is not None] + # One past the last round on disk; 0 for a run that has not recorded any, + # which is the fresh-run behaviour. + round_base = 1 + max((row["round"] for row in previous + if isinstance(row.get("round"), int)), default=-1) def on_round(info: RoundInfo) -> None: rd.append_round(info) @@ -896,6 +907,10 @@ def stop_when(info: RoundInfo) -> bool: if spec.record_rollouts and spec.kind != "policy_slot": # `show` reads the merger's rejected candidates back from this (#452). overrides["on_reports"] = on_reports + # Continue the ledger's numbering. The file is the record, so its floor wins + # over a spec that names a smaller base; the engine still takes the further + # of this and a restored checkpoint. + overrides["round_base"] = round_base try: comp = compose(spec, usage=usage, on_round=on_round, repo_path=rd.ledger_path, **overrides) diff --git a/changelog.d/1092-resume-round-numbering.md b/changelog.d/1092-resume-round-numbering.md new file mode 100644 index 00000000..365f931d --- /dev/null +++ b/changelog.d/1092-resume-round-numbering.md @@ -0,0 +1,7 @@ +Fixed +A resumed run continues its ledger's round numbering instead of restarting at +`0`, and `status.best_reward` no longer rewinds below a reward the run's own +history still holds. `execute()` passes one past the last recorded round as the +new `round_base` and seeds the best from the existing rows; a checkpoint further +ahead than the ledger still wins, and an empty ledger still starts at `0` +(#1092). diff --git a/docs/api.md b/docs/api.md index 352b2ee3..0336c237 100644 --- a/docs/api.md +++ b/docs/api.md @@ -254,6 +254,7 @@ evolve( test_frac: float = 0.0, repo_path: Optional[str] = None, checkpointing: bool = False, + round_base: int = 0, agg_config: Optional[AggregatorConfig] = None, staleness_policy: Optional[StalenessPolicy] = None, aggregator_factory: Optional[AggregatorFactory] = None, @@ -320,6 +321,7 @@ evolve( | `test_frac` | `float` | `0.0` | Carve a **test** partition out of the held-out tail (#843). Selection and every gate keep reading the select partition; the test split is scored once, after the loop, and returned as `test_reward` (with `test_tasks` / `select_tasks` for the sizes). `0.0` (the default) is the old single-split run exactly -- the CLI then says the printed reward is select-set rather than an unseen test. Synchronous only: the async path warns that it does not split yet. | | `repo_path` | `Optional[str]` | `None` | Where the git-backed ledger lives. Omit for a throwaway repo that is removed when this call returns (not held until interpreter exit, so a sweep does not accumulate one git repo per run); **passing the same path again resumes** that ledger, and a caller-supplied path is never deleted. Git runs with an isolated config, so a personal `~/.gitconfig` (`commit.gpgsign`, `core.hooksPath`) cannot fail the ledger's own bookkeeping commits. | | `checkpointing` | `bool` | `False` | Save the aggregator's in-memory **search** state to `/checkpoints/` after every round, so a later run on the same `repo_path` resumes the search instead of re-deriving it from the ledger head. `repo_path` alone already resumes the *artifact*; what it cannot carry is what the search learned on the way there -- Beta posteriors, a population archive and its `selected` counts, the early-stop patience counter. Off by default, because a run that never resumes (a scratch repo, a tempdir) would pay the serialise-and-write for nothing, and because an aggregator opts in by implementing `checkpoint()` / `restore()` at all -- one without them is unaffected either way. A checkpoint that cannot be written or read (state that does not serialise, a lock another process holds, a full disk) is skipped, never raised: the cost of a missing checkpoint is a resume that starts the search fresh, which is what `False` does. | +| `round_base` | `int` | `0` | The number this run's **first** round is recorded as. `0` (the default) numbers from zero, as every fresh run did. A caller relaunching on a ledger that already holds rounds passes its last recorded round + 1, so the rows it appends keep increasing instead of repeating `0, 1, 2`. Numbering only: `rounds` is still the number of rounds executed. A restored checkpoint further ahead still wins (`max` of the two), so numbering never goes backwards. | | `agg_config` | `Optional[AggregatorConfig]` | `None` | Tuning for the reference aggregator (batching, acceptance risk, trust region, staleness tolerance). | | `staleness_policy` | `Optional[StalenessPolicy]` | `None` | What to do with a diff proposed against an out-of-date version -- `full` / `guarded` (default) / `reflective`. | | `aggregator_factory` | `Optional[AggregatorFactory]` | `None` | Replace the optimizer entirely; receives `(ledger, verifier, audit, config, staleness_policy)`. | @@ -4912,6 +4914,7 @@ RuntimeSettings( self_verify: bool = True, held_out_frac: float = 0.4, checkpointing: bool = False, + round_base: int = 0, cheap_eval_tasks: Optional[int] = None, fusion_tournament: Optional[bool] = None, solved_threshold: float = 0.999, diff --git a/tests/test_resume_round_numbering.py b/tests/test_resume_round_numbering.py new file mode 100644 index 00000000..70d634cd --- /dev/null +++ b/tests/test_resume_round_numbering.py @@ -0,0 +1,120 @@ +"""A resumed run continues the ledger's round numbering (#1092). + +``resume`` relaunches a run on its existing directory, and ``execute`` appends +to that run's ``rounds.jsonl``. The engine numbered every process from zero, so +a resume without a checkpoint (``checkpointing`` is off by default) appended +``[0, 1, 2]`` after ``[0, 1, 2]`` and reported ``status.round`` 1, 2, 3 again. +The same per-process reset made ``status.best_reward`` start empty, so it +rewound below a reward the run's own history still held. + +The resume still runs its full ``rounds``: this is numbering, not extra work. +""" + +from __future__ import annotations + +from typing import Optional + +import pytest + +from agentdescent import EvolveSpec, Task, demo, evolve, runstore +from agentdescent.evolution import EvolutionResult, RoundInfo + + +def _spec_dict(tmp_path, rounds: int) -> dict: + """A demo spec that does not stop at its target, so ``rounds`` is the bound.""" + spec = demo.build(str(tmp_path / "work")) + spec["evolve"] = dict(spec.get("evolve") or {}, rounds=rounds, n_workers=1, + target_reward=2.0) + return EvolveSpec.from_dict(spec).to_dict() + + +def test_resume_continues_round_numbering(tmp_path, monkeypatch): + monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "home")) + store = str(tmp_path / "runs") + rd = runstore.create(_spec_dict(tmp_path, 3), store=store) + runstore.execute(rd) + assert [row["round"] for row in rd.rounds()] == [0, 1, 2] + + runstore.execute(rd) + + rows = [row["round"] for row in rd.rounds()] + assert rows == [0, 1, 2, 3, 4, 5], f"resume renumbered the rounds: {rows}" + assert rd.status().round == 6 + + +def test_resume_seeds_best_reward_from_the_ledger(tmp_path, monkeypatch): + """A second pass that scores lower must not rewind the panel's best (#1092). + + Driven through ``execute``'s own plumbing with the engine stubbed out: what + is under test is what the store carries across a relaunch, and the real + engine's per-round callback would only add a second, slower spelling of it. + """ + monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "home")) + store = str(tmp_path / "runs") + rd = runstore.create(_spec_dict(tmp_path, 2), store=store) + for i in range(3): + rd.append_round(RoundInfo(round=i, held_out_reward=1.0, n_items=1, + committed=1, rejected=0)) + rd.update_status(state="done", round=3, best_reward=1.0, last_reward=1.0) + + seen: dict = {} + + def fake_compose(spec, **kw): + seen["round_base"] = kw.get("round_base") + + class _Composition: + tree: Optional[dict] = None + notes: list = [] + kwargs: dict = {"rounds": 1} + + def run(self): + return EvolutionResult(state={}, rendered="", final_reward=0.25, + history=[], ledger_log=[]) + + return _Composition() + + monkeypatch.setattr("agentdescent.evolvespec.compose", fake_compose) + runstore.execute(rd) + + assert seen["round_base"] == 3, ( + f"execute() told the engine to start at {seen['round_base']!r}, not at " + "one past the ledger's last round") + assert rd.status().best_reward == 1.0, ( + "best_reward rewound below a reward the history still holds") + + +def test_a_checkpoint_further_ahead_than_the_ledger_still_wins(tmp_path, monkeypatch): + """``max(checkpoint, ledger)``: numbering never goes backwards on a resume.""" + monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "home")) + monkeypatch.setattr("agentdescent.checkpoint.restore_checkpoint", + lambda *a, **kw: {"round": 7}) + repo = tmp_path / "repo" + repo.mkdir() + tasks = [Task(id=f"t{i}", prompt="x", meta={"expected": "GOAL"}) + for i in range(10)] + + def reward(task, output): + return 1.0 if output == task.meta["expected"] else 0.0 + + class _Slot: + def initial(self): + return {} + + def render(self, state): + return state.get("v", "") + + from agentdescent.evolution import SingleSlot + result = evolve(tasks, reward, run=lambda a, t: "x", propose=lambda *a: None, + strategy=SingleSlot(initial_value="v"), n_workers=1, rounds=2, + round_base=1, checkpointing=True, repo_path=str(repo), + self_verify=False, target_reward=2.0) + assert [h.round for h in result.history] == [8, 9] + + +@pytest.mark.parametrize("empty", [True]) +def test_an_empty_ledger_still_starts_at_zero(tmp_path, monkeypatch, empty): + monkeypatch.setenv("AGENTDESCENT_HOME", str(tmp_path / "home")) + store = str(tmp_path / "runs") + rd = runstore.create(_spec_dict(tmp_path, 2), store=store) + runstore.execute(rd) + assert [row["round"] for row in rd.rounds()] == [0, 1]