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
2 changes: 2 additions & 0 deletions agentdescent/async_evolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
56 changes: 43 additions & 13 deletions agentdescent/evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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={
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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`.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion agentdescent/evolve_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion agentdescent/runstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions changelog.d/1092-resume-round-numbering.md
Original file line number Diff line number Diff line change
@@ -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).
3 changes: 3 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 `<repo_path>/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)`. |
Expand Down Expand Up @@ -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,
Expand Down
Loading
Loading