Skip to content

The agent that grows with you

fix(cron): read a job's run history from its owning profile

finn76394f56aa79ec7
diff --git a/hermes_cli/web_routers/cron.py b/hermes_cli/web_routers/cron.py
index 43ed0ba76bf..2a89ada02f1 100644
--- a/hermes_cli/web_routers/cron.py
+++ b/hermes_cli/web_routers/cron.py
@@ -74,6 +74,25 @@ def _job_profile(job_id: str, profile: Optional[str]) -> str:
return selected
+def _job_runs_profile(job_id: str, profile: Optional[str]) -> Optional[str]:
+ """Profile whose state.db holds ``job_id``'s run sessions.
+
+ ``profile`` is a caller *hint*, not proof of ownership: the Desktop lists
+ jobs cross-profile (``?profile=all``) while its per-item calls carry the
+ ambient active profile. Trusting that hint opened a profile that does not
+ hold the job, so the ``cron_{job_id}_*`` id-range scan matched nothing and
+ answered ``200 {"runs": []}`` — the UI rendered "No runs yet" for a job
+ that had run many times (#115345). A hint that does hold the job still
+ wins, so deliberately scoped lookups (the same job id in two profiles,
+ e.g. a copied jobs.json) keep reading the named profile.
+ """
+ if profile:
+ jobs = _call_cron_for_profile(profile, "list_jobs", True)
+ if any(j.get("id") == job_id or j.get("name") == job_id for j in jobs):
+ return profile
+ return _find_cron_job_profile(job_id)
+
+
def _found(job):
if not job:
raise _job_not_found()
@@ -110,7 +129,7 @@ def _list_cron_job_runs_sync(job_id: str, profile: Optional[str] = None, limit:
SessionInfo. Backed by ``SessionDB.list_cron_job_runs`` — a bounded id-range
scan, so cost scales with the requested window, not total cron history.
"""
- selected = profile or _find_cron_job_profile(job_id)
+ selected = _job_runs_profile(job_id, profile)
# job_id may be a human name; resolve to the canonical id used in run-session ids.
canonical = job_id
if selected:
diff --git a/tests/hermes_cli/test_web_server_cron_profiles.py b/tests/hermes_cli/test_web_server_cron_profiles.py
index ef03ab6272f..40d8c3d6b9a 100644
--- a/tests/hermes_cli/test_web_server_cron_profiles.py
+++ b/tests/hermes_cli/test_web_server_cron_profiles.py
@@ -1115,3 +1115,76 @@ def test_list_cron_jobs_carries_each_profiles_ticker_heartbeat_age(isolated_prof
assert ages["default"] is None # no heartbeat file: cannot date the last tick
assert 25 * 3600 <= ages["worker_alpha"] < 25 * 3600 + 60
+
+
+async def test_cron_run_history_resolves_the_jobs_owner_profile(isolated_profiles, monkeypatch):
+ """#115345 — the Desktop lists jobs cross-profile (``?profile=all``) while its
+ per-item calls carry the ambient active profile. The run lookup treated that
+ hint as the owning profile, opened the wrong profile's state.db and answered
+ ``200 {"runs": []}``, so every job owned by another profile rendered
+ "No runs yet" for history that existed."""
+ from hermes_cli import web_server
+
+ worker_job = _web_server_cron._call_cron_for_profile(
+ "worker_alpha",
+ "create_job",
+ prompt="owned by worker",
+ schedule="every 1h",
+ name="cross-profile-runs",
+ )
+ default_job = _web_server_cron._call_cron_for_profile(
+ "default",
+ "create_job",
+ prompt="owned by default",
+ schedule="every 1h",
+ name="default-owned-runs",
+ )
+
+ # Run sessions live in the state.db of the profile that owns the job, keyed
+ # by the canonical id in the session id (cron_{job_id}_{timestamp}).
+ runs_by_profile = {
+ ("worker_alpha", worker_job["id"]): [
+ {"id": f"cron_{worker_job['id']}_1", "started_at": 1.0}
+ ],
+ ("default", default_job["id"]): [
+ {"id": f"cron_{default_job['id']}_1", "started_at": 2.0}
+ ],
+ }
+ opened = []
+
+ class _FakeRunsDB:
+ def __init__(self, profile):
+ self.profile = profile
+
+ def list_cron_job_runs(self, job_id, limit=20, offset=0):
+ opened.append(self.profile)
+ return [dict(row) for row in runs_by_profile.get((self.profile, job_id), ())]
+
+ def close(self):
+ pass
+
+ monkeypatch.setattr(
+ _rt_cron,
+ "_open_session_db_for_profile",
+ lambda profile, *, read_only: _FakeRunsDB(profile),
+ )
+
+ # Active profile is `default`; the listed job belongs to worker_alpha.
+ ambient = await _rt_cron.list_cron_job_runs(worker_job["id"], profile="default")
+ assert [run["id"] for run in ambient["runs"]] == [f"cron_{worker_job['id']}_1"]
+ assert ambient["runs"][0]["profile"] == "worker_alpha"
+ assert opened == ["worker_alpha"]
+
+ # Scope is resolved, not widened: the owning-profile hint and the omitted
+ # legacy lookup still read the owner's store, and profile="default" still
+ # reads default's own job (and only that job) out of default's state.db.
+ opened.clear()
+ owner = await _rt_cron.list_cron_job_runs(worker_job["id"], profile="worker_alpha")
+ omitted = await _rt_cron.list_cron_job_runs(worker_job["id"])
+ own = await _rt_cron.list_cron_job_runs(default_job["id"], profile="default")
+
+ assert [run["id"] for run in owner["runs"]] == [f"cron_{worker_job['id']}_1"]
+ assert [run["id"] for run in omitted["runs"]] == [f"cron_{worker_job['id']}_1"]
+ assert [run["id"] for run in own["runs"]] == [f"cron_{default_job['id']}_1"]
+ assert opened == ["worker_alpha", "worker_alpha", "default"]