Skip to content

feat(solver): read the solve's time limit from SolverScope - #280

Merged
blackopsrepl merged 6 commits into
SolverForge:mainfrom
slapers:feat/public-time-limit
Nov 27, 2026
Merged

blackopsrepl merged 6 commits into
SolverForge:mainfrom
slapers:feat/public-time-limit

Conversation

@slapers

@slapers slapers commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Change and Purpose

SolverScope::set_time_limit is public, but SolverScope::time_limit is pub(crate): a custom phase can set the solve's time limit but not read it.

The built-in construction already reads it to take a share of the solve's time (optional_construction_termination). Custom phases need the same: a simulated annealing whose temperature falls with the time gradient, or a phase that runs only for the first part of the solve and then hands over (it sets a shorter limit with set_time_limit and must restore the original one). Today such a phase has to carry the time limit a second time, beside the solver config, and the two can drift apart.

This makes time_limit() public, next to set_time_limit(). No behavior changes.

  • scope/solver/scope_termination.rs: time_limit is pub.
  • scope/tests.rs: time_limit_reads_back_what_was_set (unset is None; a set limit reads back).
  • solverforge-solver/WIREFRAME.md: time_limit() in the SolverScope key methods.
  • solverforge-solver/public-api.txt: blessed with scripts/check-public-api.py --bless.

Verification

  • make fmt-check clippy doc-names file-length facade-reachability public-api: all pass.
  • cargo test --workspace: 1644 passed, 0 failed.
  • make deps not run: cargo-machete is not installed here. The change adds no dependency.

Review Checklist

  • I understand the final diff and will address review findings.
  • The change follows the relevant architecture and style rules in AGENTS.md.
  • Tests cover changed behavior, or I explained why tests are not applicable.
  • Documentation and affected wireframes match any public API changes.
  • The contribution respects copyright, attribution, license compatibility, and confidentiality.

@blackopsrepl

Copy link
Copy Markdown
Collaborator

Thanks @slapers, you were right that a custom phase needs this. It can stop on the limit through should_terminate() and it can read elapsed(), but it had no way to know how much time it has to work with.

I pushed a change on top of your commit. The getter is now remaining_time() instead of time_limit(), for two reasons:

  • A custom phase can also run as a partitioned search's child phase. There the scope has no time limit, only the enclosing solve's deadline and any partition bound, so time_limit() returned None even though the time was bounded. remaining_time() takes the earliest of all of them.
  • With the remaining time you don't need to shorten the solve's limit and put it back. You read remaining_time() when the phase starts, take your share of it, and return when that share is used up. Putting the limit back would also be impossible when the solve had no limit at all.

It returns None when no time limit applies and zero once one is reached. The facade test now calls it from outside the crate, so it stays public. I'll merge it now and it ships in the next release.

blackopsrepl and others added 6 commits November 27, 2026 19:47
Clone requires Sized, so the ?Sized bound on the guard's Clone impl was ignored and clippy's needless_maybe_sized lint rejects it under -D warnings, which failed CI's all-targets clippy on main. Every carrier the guard checks is Sized, so both impls drop the bound; the guard still fails to compile when a carrier gains Clone.
make clippy linted only library code without features, so a lint in test code passed locally and failed CI's cargo clippy --workspace --all-targets --all-features. make build and make test also left out --all-features. The three targets now run the same commands as the CI Rust job, so make lint and make test catch what CI catches.
SolverScope::set_time_limit is public but time_limit is crate-private, so a
custom phase can set the limit but not read it. The built-in construction
takes a share of the limit; a custom phase that needs the same (a time-based
annealing temperature, a phase that runs for the first part of the solve)
has to carry the limit beside the solver config. Make time_limit public.
A custom phase that plans its own schedule needs to know how much time is
left before a time limit ends it. The raw time_limit() field answers that
only for a top-level solve. A custom phase running as a partition's child
phase sees None, because its scope carries the enclosing solve's deadline
and any partition bound instead of a limit, so a schedule built on it
silently loses its time bound there. Using the raw limit also invites a
phase to shorten the solve's limit and restore it, which cannot put back
"no limit" when there was none.

SolverScope::remaining_time() returns the solve's time limit minus
solver-active time, or the earliest inherited deadline (enclosing solve or
partition bound chain), whichever ends first. It returns None when no time
limit applies and zero once one is reached. time_limit() goes back to
crate-private; built-in construction still reads it for its share of the
solve. Tests cover the top-level limit, the inherited deadline in a child
scope and the nested partition bound, and the facade test calls the method
from outside the crate.
…line

The optional assignment pass takes a fifth of the solve's time limit. It read the raw time limit, which a partition's child scope does not carry: the partition inherits the solve's deadline instead. Default phases run inside partitions, so there the optional pass had no time cap and could spend the whole partition deadline before local search started. The share is now a fifth of the scope's span, its solver-active time plus remaining_time(), which equals the time limit for a top-level solve and the inherited deadline for a partition. The crate-private time_limit() accessor has no reader left and is removed.
The custom-phase guide now names SolverScope::remaining_time as the way a phase reads its time, including the deadline it inherits as a partition's child phase, and says a phase that takes a share of the time returns when the share is spent instead of changing the solve's limit.
@blackopsrepl
blackopsrepl force-pushed the feat/public-time-limit branch from 81c4202 to 6421406 Compare November 27, 2026 18:52
@blackopsrepl

Copy link
Copy Markdown
Collaborator

One more thing your change turned up: the built-in construction had the same blind spot. Its optional pass takes a fifth of the solve's time limit, and inside a partition it saw no limit, so it ran uncapped. It now takes its share from remaining_time() as well. I also rebased the branch onto current main, which carries a fix for the clippy failure CI was showing.

@blackopsrepl
blackopsrepl merged commit 6421406 into SolverForge:main Nov 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants