Skip to content

The agent that grows with you

fix(cron): an Infinity or negative repeat.completed no longer breaks load_jobs

kshitijk4poor7ca5cca50a37
diff --git a/cron/jobs.py b/cron/jobs.py
index e2a6efda66c..1562ec44725 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -1391,14 +1391,16 @@ def load_jobs() -> List[Dict[str, Any]]:
jobs = [j for j in jobs if isinstance(j, dict)]
repair = repair or "non-object entries dropped"
for job in jobs:
- # A hand-edited "completed" that is not an int (null, "2", 1.0) would crash every counter
- # reader (None += 1, "2" + 1) or render as "None/3" / "2.0/3"; normalize it once here so
- # readers can trust an int.
+ # A hand-edited "completed" that is not a non-negative int (null, "2", 1.0, -5, Infinity)
+ # would crash every counter reader (None += 1, "2" + 1), render as "None/3" / "2.0/3", or
+ # grant extra runs; normalize it once here so readers can trust a non-negative int.
+ # OverflowError: json.loads turns Infinity / 1e999 into float inf, and int(inf) raises.
rep = job.get("repeat")
- if isinstance(rep, dict) and "completed" in rep and type(rep["completed"]) is not int:
+ if isinstance(rep, dict) and "completed" in rep and (
+ type(rep["completed"]) is not int or rep["completed"] < 0):
try:
- rep["completed"] = max(int(rep["completed"] or 0), 0)
- except (TypeError, ValueError):
+ rep["completed"] = max(int(rep["completed"]), 0)
+ except (TypeError, ValueError, OverflowError):
rep["completed"] = 0
repair = repair or "invalid repeat.completed normalized"
# Persist even an empty result, or an all-junk store repeats the repair on every tick.
diff --git a/tests/cron/test_jobs.py b/tests/cron/test_jobs.py
index 9c759bb6177..027e47b6ed7 100644
--- a/tests/cron/test_jobs.py
+++ b/tests/cron/test_jobs.py
@@ -438,9 +438,9 @@ class TestJobCRUD:
with pytest.raises(ValueError, match="Invalid repeat"):
update_job(job["id"], {"repeat": "banana"})
- def test_null_repeat_completed_counts_as_zero(self, tmp_cron_dir):
- """A hand-edited "completed": null must not kill mark_job_run (None += 1) or be
- carried forward by update_job."""
+ def test_invalid_repeat_completed_is_normalized(self, tmp_cron_dir):
+ """A hand-edited "completed" (null, string, float, negative, Infinity) must not kill
+ mark_job_run or the whole store, and must not be carried forward by update_job."""
import json
from cron.jobs import JOBS_FILE, get_job, mark_job_run, update_job
@@ -456,8 +456,10 @@ class TestJobCRUD:
assert get_job(job["id"])["repeat"]["completed"] == 1
set_completed(None)
assert update_job(job["id"], {"repeat": {"times": 5}})["repeat"]["completed"] == 0
- # Other hand-edited shapes: a string would crash ("2" + 1), a float would render "2.0/5".
- for value, expected in (("2", 3), (1.0, 2), ("junk", 1)):
+ # Other hand-edited shapes: a string would crash ("2" + 1), a float would render "2.0/5",
+ # a negative count grants extra runs, and Infinity (json.dumps writes it) raised
+ # OverflowError out of load_jobs, freezing every job.
+ for value, expected in (("2", 3), (1.0, 2), ("junk", 1), (-2, 1), (float("inf"), 1)):
set_completed(value)
mark_job_run(job["id"], success=True)
completed = get_job(job["id"])["repeat"]["completed"]
diff --git a/website/docs/user-guide/features/cron.md b/website/docs/user-guide/features/cron.md
index f8e8500ff67..3a01c034ae3 100644
--- a/website/docs/user-guide/features/cron.md
+++ b/website/docs/user-guide/features/cron.md
@@ -1349,7 +1349,7 @@ Job definitions are plain JSON on disk: they survive `hermes update`, gateway re
Ask the agent to manage jobs through the `cronjob_manage` tool, `hermes cron edit`, or `/cron` — not by patching `jobs.json` directly. Direct edits can fail silently when [file write safety](../security.md#file-write-safety) blocks the path (for example when `HERMES_WRITE_SAFE_ROOT` is set), and the [file-mutation verifier](../configuration.md#file-mutation-verifier) footer is the authoritative signal that nothing was saved.
:::
-If a hand edit leaves `jobs.json` malformed, the scheduler repairs it on the next load instead of stopping: entries in the `jobs` list that are not JSON objects are dropped, and a `repeat.completed` that is not an integer is normalized to one. Each repair is logged as a warning (value types only, never contents).
+If a hand edit leaves `jobs.json` malformed, the scheduler repairs it on the next load instead of stopping: entries in the `jobs` list that are not JSON objects are dropped, and a `repeat.completed` that is not a non-negative integer is normalized to one (0 when it can't be read). Each repair is logged as a warning (value types only, never contents).
Jobs may store `model` and `provider` as `null`. When those fields are omitted, Hermes resolves them at execution time from the global configuration. They only appear in the job record when a per-job override is set.
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md
index c3bfc1ba910..0234ff7f5d8 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/user-guide/features/cron.md
@@ -742,7 +742,7 @@ cronjob(action="create", name="daily-digest",
任务存储在 `~/.hermes/cron/jobs.json`。任务运行的输出保存到 `~/.hermes/cron/output/{job_id}/{timestamp}.md`。
-如果手动编辑使 `jobs.json` 格式出错,调度器会在下次加载时修复它,而不是停止运行:`jobs` 列表中不是 JSON 对象的条目会被丢弃,不是整数的 `repeat.completed` 会被规范化为整数。每次修复都会记录一条警告(只记录值的类型,不记录内容)。
+如果手动编辑使 `jobs.json` 格式出错,调度器会在下次加载时修复它,而不是停止运行:`jobs` 列表中不是 JSON 对象的条目会被丢弃,不是非负整数的 `repeat.completed` 会被规范化为非负整数(无法解析时为 0)。每次修复都会记录一条警告(只记录值的类型,不记录内容)。
任务可能将 `model` 和 `provider` 存储为 `null`。省略这些字段时,Hermes 在执行时从全局配置中解析它们。只有设置了单任务覆盖时,这些字段才会出现在任务记录中。