-
Notifications
You must be signed in to change notification settings - Fork 52.8k
feat(scheduler): per-job max_iterations override from jobs.json #33323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
inquistiff
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
inquistiff:pr/0017-per-job-max-iterations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Tests for per-job max_iterations override in the cron scheduler. | ||
|
|
||
| Fallback chain under test: | ||
| job["max_iterations"] (positive int) | ||
| > config["agent"]["max_turns"] | ||
| > config["max_turns"] | ||
| > 90 (hard default) | ||
|
|
||
| Run: python -m pytest tests/cron/test_scheduler_max_iterations.py -v | ||
| """ | ||
| import sys | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| sys.path.insert(0, str(Path(__file__).parent.parent.parent)) | ||
|
|
||
|
|
||
| def _resolve(job: dict, cfg: dict): | ||
| """ | ||
| Mirror of the production resolution logic in cron/scheduler.py. | ||
| Must stay in sync with the "Max iterations" block in _run_job. | ||
| Returns (max_iterations: int, warnings: list[str]). | ||
| """ | ||
| warnings = [] | ||
| global_max = cfg.get("agent", {}).get("max_turns") or cfg.get("max_turns") or 90 | ||
| job_val = job.get("max_iterations") | ||
| if job_val is None: | ||
| return global_max, warnings | ||
| if isinstance(job_val, int) and job_val > 0: | ||
| return job_val, warnings | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This duplicates the resolver instead of exercising the scheduler, so it can pass while the production |
||
| warnings.append(f"invalid max_iterations {job_val!r}") | ||
| return global_max, warnings | ||
|
|
||
|
|
||
| class TestFallbackChain(unittest.TestCase): | ||
|
|
||
| def test_per_job_override_wins_over_all_config(self): | ||
| result, warns = _resolve( | ||
| {"max_iterations": 25}, | ||
| {"agent": {"max_turns": 50}, "max_turns": 30}, | ||
| ) | ||
| self.assertEqual(result, 25) | ||
| self.assertEqual(warns, []) | ||
|
|
||
| def test_no_field_falls_back_to_agent_max_turns(self): | ||
| result, warns = _resolve({}, {"agent": {"max_turns": 40}}) | ||
| self.assertEqual(result, 40) | ||
| self.assertEqual(warns, []) | ||
|
|
||
| def test_no_field_falls_back_to_top_level_max_turns(self): | ||
| result, warns = _resolve({}, {"max_turns": 55}) | ||
| self.assertEqual(result, 55) | ||
| self.assertEqual(warns, []) | ||
|
|
||
| def test_empty_config_uses_hard_default_90(self): | ||
| result, warns = _resolve({}, {}) | ||
| self.assertEqual(result, 90) | ||
| self.assertEqual(warns, []) | ||
|
|
||
| def test_agent_max_turns_beats_top_level_max_turns(self): | ||
| result, warns = _resolve({}, {"agent": {"max_turns": 20}, "max_turns": 60}) | ||
| self.assertEqual(result, 20) | ||
|
|
||
|
|
||
| class TestValidation(unittest.TestCase): | ||
|
|
||
| def test_string_value_rejected_warns_and_falls_back(self): | ||
| result, warns = _resolve({"max_iterations": "forty"}, {"agent": {"max_turns": 30}}) | ||
| self.assertEqual(result, 30) | ||
| self.assertEqual(len(warns), 1) | ||
| self.assertIn("forty", warns[0]) | ||
|
|
||
| def test_negative_integer_rejected(self): | ||
| result, warns = _resolve({"max_iterations": -5}, {"max_turns": 45}) | ||
| self.assertEqual(result, 45) | ||
| self.assertEqual(len(warns), 1) | ||
|
|
||
| def test_zero_rejected(self): | ||
| result, warns = _resolve({"max_iterations": 0}, {}) | ||
| self.assertEqual(result, 90) | ||
| self.assertEqual(len(warns), 1) | ||
|
|
||
| def test_float_rejected(self): | ||
| result, warns = _resolve({"max_iterations": 10.5}, {"max_turns": 20}) | ||
| self.assertEqual(result, 20) | ||
| self.assertEqual(len(warns), 1) | ||
|
|
||
| def test_explicit_none_treated_as_absent(self): | ||
| result, warns = _resolve({"max_iterations": None}, {"max_turns": 35}) | ||
| self.assertEqual(result, 35) | ||
| self.assertEqual(warns, []) | ||
|
|
||
|
|
||
| class TestBoundary(unittest.TestCase): | ||
|
|
||
| def test_value_of_one_valid(self): | ||
| result, warns = _resolve({"max_iterations": 1}, {}) | ||
| self.assertEqual(result, 1) | ||
| self.assertEqual(warns, []) | ||
|
|
||
| def test_large_value_accepted(self): | ||
| result, warns = _resolve({"max_iterations": 500}, {}) | ||
| self.assertEqual(result, 500) | ||
| self.assertEqual(warns, []) | ||
|
|
||
|
|
||
| class TestSchedulerImport(unittest.TestCase): | ||
| def test_scheduler_imports_cleanly(self): | ||
| try: | ||
| import cron.scheduler # noqa: F401 | ||
| except ImportError as e: | ||
| self.skipTest(f"Optional deps not installed in test env: {e}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main(verbosity=2) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
boolis a subclass ofint, so a hand-edited JSONtruepasses this condition and silently sets a one-iteration cap. Usetype(_job_max_iter) is intand add boolean cases to the validation coverage.