Repository navigation
Description
title: "[Bug]: no_agent cron sessions not closed — state.db accumulates zombie sessions"
[Bug]: no_agent cron sessions not closed — state.db accumulates zombie sessions
Summary
When no_agent=True (script-mode) cron jobs run, the SQLite session row is
never closed: ended_at stays NULL, end_reason stays NULL, and the session
continues to look "active" to every tool that reads state.db.
This accumulates zombie sessions in state.db until external cleanup runs.
Root cause (in hermes 0.16.0, commit b1e399d)
cron/scheduler.py::_run_job_impl (line 1310) has a no_agent short-circuit
that runs the script via _run_job_script and returns early, before the
outer _run_job finally block (lines 1921-1942) gets a chance to call
end_session.
def _run_job_impl(job: dict) -> tuple[bool, str, str, Optional[str]]:
# ...
if job.get("no_agent"):
script_path = job.get("script")
if not script_path:
return False, "", "", "no_agent=True but no script is set"
try:
ok, output = _run_job_script(script_path)
finally:
if _prior_cwd is not None:
os.chdir(_prior_cwd)
return ok, output, "", None # ← early return skips outer finallyThe outer finally is the only place that calls end_session. So
no_agent=True jobs never get their session row closed.
Repro
hermes cron add --script /tmp/quick.py --no-agent --schedule "*/5 * * * *"
# let it run 5+ times
sqlite3 ~/.hermes/state.db \
"SELECT COUNT(*) FROM sessions WHERE source='cron' AND ended_at IS NULL"
# expected: 0 (the row should be closed once the script returns)
# actual: 5 (and growing every tick)In our installation we saw 8 cron sessions idle 8–16 h, all with ended_at = NULL,
even though every job's script completed in <30 s. This breaks /sessions
counts, hermes cron list active-job tallies, and any external dashboard
that joins sessions on ended_at.
Suggested fix
In _run_job_impl, when no_agent=True finishes, still call end_session
with the script's exit info before returning. Two minimal options:
-
Move the
end_sessioncall (or its body) into theno_agentbranch so
it runs from_run_job_impl's own finally — script's exit code becomes
end_reason. -
Wrap the
no_agentblock in a try/finally that calls
session_db.end_session(...)with the captured exit info, then re-raise
the early return.
We'd prefer option 1 — it keeps the contract ("every cron job ends with a
closed session row") invariant regardless of no_agent mode.
Workaround (until fixed)
We run an external cleanup cron that sets ended_at on zombie rows:
# 1 h idle cron + ended_at NULL → close as cron_stale_cleanup
# 24 h idle human + ended_at NULL → close as stale_human_cleanup
UPDATE sessions SET ended_at = ?, end_reason = 'cron_stale_cleanup'
WHERE source = 'cron' AND ended_at IS NULL AND started_at < ?That workaround is what makes this tolerable, but it's not a substitute for
the underlying bug — the session row should close at job exit, not an hour
later.
Environment
- hermes-agent: 0.16.0 (commit b1e399d)
- Python: 3.11
- OS: Windows 11 + WSL2 (both observed)
- Cron mode:
no_agent=Truewithscriptset
Activity
liuhao1024 commentedon Jun 8, 2026
This is fixed by #41969 — closes the session row in
state.dbforno_agentscript-mode cron jobs duringmark_job_run(), preventing zombie session accumulation.konsisumer commentedon Jul 7, 2026
These issues look like facets of one underlying problem: #41935, #42851, #44080.
They were grouped by a semantic pass as different symptoms of one underlying problem in
comp/cron. A single coordinated fix may resolve them together — flagging for visibility; no action has been taken on the individual issues.Automated root-cause grouping. Reply to correct if these are unrelated.
teknium1 commentedon Jul 14, 2026
Automated hermes-sweeper review: this zombie-session report no longer reproduces on current main.
cron/scheduler.py:2514-2516intentionally places theno_agentshort-circuit before SessionDB initialization; the script-only branch returns at:2608.cron/scheduler.py:2616-2623, so ano_agenttick cannot create asource='cron'SessionDB row that could remain unclosed.2008f542121462caeefc3559216a129cc39021ae(fix(cron): lazy-import AIAgent + SessionDB so no_agent ticks pay zero).The open #41969 proposal would reintroduce SessionDB work to the script-only path, but the specific accumulation described here is obsolete on main.