Skip to content

The agent that grows with you

fix(cron): don't attribute no_agent script failures to a provider

Yanir-Rd1fc20432f3b
diff --git a/cron/scheduler.py b/cron/scheduler.py
index 49b40ed58d7..d519662ac5c 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -157,8 +157,41 @@ def _summarize_cron_failure_for_delivery(job: dict, error: str | None) -> str:
f"unintended spend. {remediation}"
)
+ # A no_agent job IS its script — run_job short-circuits it before any model
+ # is reached ("no LLM involvement", see the no_agent branch in run_job). So
+ # provider timeouts, rate limits, auth errors and fallback chains are not
+ # merely unlikely for these jobs, they are structurally impossible. Classify
+ # on the job's MODE before pattern-matching its prose.
+ #
+ # Without this gate the branches below classify by substring, so a script's
+ # own wording decides which subsystem gets blamed. _run_job_script reports a
+ # timeout as "Script timed out after {n}s: {path}" — that contains "timed
+ # out", so it matched the provider branch and the operator was told
+ # "provider timeout. Fallback chain was exhausted or unavailable." for a job
+ # that never opened a socket. "429" or "authentication" appearing anywhere
+ # in a script's output misfires the same way.
+ #
+ # A delivery line that names the wrong subsystem is worse than no line at
+ # all: it does not merely fail to inform, it sends the reader to the wrong
+ # place.
+ #
+ # Falling through leaves the generic cleaner below to report what actually
+ # happened, naming the script. No new message text is needed.
+ provider_reachable = not job.get("no_agent")
+
+ # Script execution happens outside the LLM/provider path (also for
+ # agent-backed jobs that run a context script). Check the script runner's
+ # explicit error contract ("Script timed out after {n}s: {path}") before
+ # generic timeout matching so a script timeout never claims a provider
+ # fallback was attempted (#82460 @jbagdonas, #78503 @daxro).
+ if lower.startswith("script timed out"):
+ return (
+ f"⚠️ Cron '{job_name}' failed: script timed out. "
+ "No model was invoked. Full details saved in cron output."
+ )
+
# Provider/API failures are the common noisy path. Keep these short.
- if "429" in text or "rate limit" in lower or "usage limit" in lower:
+ if provider_reachable and ("429" in text or "rate limit" in lower or "usage limit" in lower):
reason = "rate limit"
if "weekly usage limit" in lower:
reason = "weekly usage limit"
@@ -208,7 +241,9 @@ def _summarize_cron_failure_for_delivery(job: dict, error: str | None) -> str:
"its workdir. Full details saved in cron output."
)
- if "readtimeout" in lower or "timed out" in lower or "timeout" in lower:
+ if provider_reachable and (
+ "readtimeout" in lower or "timed out" in lower or "timeout" in lower
+ ):
return (
f"⚠️ Cron '{job_name}' failed: provider timeout. "
f"{_fallback_chain_phrase()} "
@@ -218,7 +253,9 @@ def _summarize_cron_failure_for_delivery(job: dict, error: str | None) -> str:
# Match authentication/authorization wording at a word boundary and the
# 401/403 status codes as whole tokens, so "oauth", "4015" and similar do
# not trip a misleading auth message.
- if re.search(r"authenticat|authoriz", lower) or re.search(r"\b(401|403)\b", text):
+ if provider_reachable and (
+ re.search(r"authenticat|authoriz", lower) or re.search(r"\b(401|403)\b", text)
+ ):
return (
f"⚠️ Cron '{job_name}' failed: provider authentication error. "
"Full details saved in cron output."
diff --git a/tests/cron/test_cron_no_agent.py b/tests/cron/test_cron_no_agent.py
index 94aea1de141..75ad45d153f 100644
--- a/tests/cron/test_cron_no_agent.py
+++ b/tests/cron/test_cron_no_agent.py
@@ -161,3 +161,59 @@ def test_run_job_script_nul_path_fails_cleanly(hermes_env):
ok, output = _run_job_script("~user\x00bad.sh")
assert ok is False
assert "Blocked" in output
+
+
+# ---------------------------------------------------------------------------
+# _summarize_cron_failure_for_delivery: mode-aware failure attribution
+# ---------------------------------------------------------------------------
+#
+# The summarizer classified failures by substring-matching the error prose and
+# mapped any hit onto a provider-shaped explanation. For a no_agent job that is
+# structurally impossible — run_job short-circuits before any model is reached —
+# so a script whose own text happened to contain "timed out", "429" or
+# "authentication" had its failure attributed to a provider it never called.
+#
+# Observed in practice: _run_job_script reports a timeout as "Script timed out
+# after {n}s: {path}", which was delivered to chat as "provider timeout. Fallback
+# chain was exhausted or unavailable." for a job that never opened a socket.
+#
+# The summarizer had no direct test coverage — the only test referencing it
+# mocks it out and asserts on its arguments — which is why this shipped.
+
+
+ "error",
+ [
+ "Script timed out after 900s: /home/u/.hermes/scripts/nightly.sh",
+ "Script failed: curl returned 429 from api.example.com",
+ "Script failed: gpg authentication failed for key",
+ "Script failed: ReadTimeout contacting localhost",
+ ],
+)
+def test_no_agent_failure_never_blamed_on_a_provider(error):
+ """A script job's failure must never be reported as a provider/fallback failure."""
+ from cron.scheduler import _summarize_cron_failure_for_delivery
+
+ job = {"name": "nightly-job", "no_agent": True, "script": "nightly.sh"}
+ msg = _summarize_cron_failure_for_delivery(job, error)
+
+ assert "provider" not in msg.lower()
+ assert "fallback chain" not in msg.lower()
+ # The operator must be pointed at what actually failed.
+ assert "script" in msg.lower()
+
+
+ ("error", "expected"),
+ [
+ ("ReadTimeout: provider did not respond", "provider timeout"),
+ ("HTTP 429 rate limit exceeded", "provider rate limit"),
+ ("HTTP 401 authentication failed", "provider authentication error"),
+ ],
+)
+def test_agent_job_provider_classification_unchanged(error, expected):
+ """Regression guard: agent-mode jobs keep the provider-shaped summaries."""
+ from cron.scheduler import _summarize_cron_failure_for_delivery
+
+ job = {"name": "daily-digest", "no_agent": False}
+ assert expected in _summarize_cron_failure_for_delivery(job, error)