Files
hermes-agent/tests/tools/test_process_checkpoint_worker_handoff.py
Teknium 92fec1607b fix(tools): processes.json writers keep the worker handoffs they do not track
Mechanism: _write_checkpoint rebuilt the profile's processes.json from the
writer's own _running table (+ extra_entries) and atomic-replaced it, with no
read-merge and no lock. This PR made that file the only handoff of a managed
worker's background process to the next interpreter (recover_from_checkpoint
adopts the entry's output_log and tails it), and the always-running owner is
now a routine writer: the owner's next own process start/exit/kill (or a
second concurrent worker) dropped the worker's entry. The process kept
running and logging, but nobody tailed it: notify_on_complete never fired and
process poll/kill could not see it. (The generic CLI-vs-gateway overwrite race
stays pre-existing per round-12 N58; this fixes the PR's own handoff.)

Fix: read-merge-write under an exclusive file lock (processes.json.lock, the
active_sessions _FileLock: fcntl / msvcrt). A writer keeps on-disk entries it
neither runs nor has finished, but only worker handoffs whose output log
still decides an outcome: the log exists and either its recorded .exit is
waiting or the pid is alive with the same start time. Dead, recycled or
non-handoff foreign entries are still dropped.

Red on r13-base2: tests/tools/test_process_checkpoint_worker_handoff.py (real
live process, real checkpoint file): the owner's write dropped proc_worker;
after the fix it is kept, a dead handoff is dropped, and the successor
registry adopts the worker's process.

Review: ahrazzle B2-04
2026-10-11 19:31:36 -07:00

53 lines
2.9 KiB
Python

"""processes.json keeps the worker handoffs its writer does not track (ahrazzle B2-04).
A managed worker's background process reaches the next interpreter only through the profile
checkpoint. The owner (or a second worker) rewrote that file from its own table alone, so its
next process start/exit dropped the worker's entry: nobody tailed the log, notify_on_complete
never fired and poll/kill could not see the process. Real processes, real checkpoint file.
"""
import json
import subprocess
import sys
import time
import psutil
import pytest
@pytest.mark.platforms("linux", "macos")
def test_owner_checkpoint_write_keeps_a_live_worker_handoff(tmp_path, monkeypatch):
monkeypatch.setenv('HERMES_HOME', str(tmp_path))
from tools import process_registry as pr
from tools.process_registry_output_log import output_dir
checkpoint = tmp_path / 'processes.json'
monkeypatch.setattr(pr, 'CHECKPOINT_PATH', checkpoint)
logs = output_dir()
logs.mkdir(parents=True)
worker_proc = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)'], stdin=subprocess.DEVNULL)
owner_proc = subprocess.Popen([sys.executable, '-c', 'import time; time.sleep(60)'], stdin=subprocess.DEVNULL)
try:
handoff = {'session_id': 'proc_worker', 'command': 'dev-server', 'pid': worker_proc.pid, 'pid_scope': 'host',
'host_start_time': pr.ProcessRegistry._safe_host_start_time(worker_proc.pid),
'started_at': time.time(), 'output_log': str(logs / 'proc_worker.log'), 'notify_on_complete': True}
(logs / 'proc_worker.log').write_text('serving\n')
gone = {**handoff, 'session_id': 'proc_gone', 'pid': 2 ** 22 + 7, 'output_log': str(logs / 'proc_gone.log')}
(logs / 'proc_gone.log').write_text('')
checkpoint.write_text(json.dumps([handoff, gone])) # what the exiting worker left behind
owner = pr.ProcessRegistry()
owner._running['proc_owner'] = pr.ProcessSession(
id='proc_owner', command='owner-job', task_id='t', started_at=time.time(), pid=owner_proc.pid,
pid_scope='host', host_start_time=pr.ProcessRegistry._safe_host_start_time(owner_proc.pid))
owner._write_checkpoint() # the owner's own process start
ids = {entry['session_id'] for entry in json.loads(checkpoint.read_text())}
assert ids == {'proc_owner', 'proc_worker'}, ids
# The next interpreter still adopts the worker's process from the file.
successor = pr.ProcessRegistry()
successor._running['proc_owner'] = owner._running['proc_owner']
assert successor.recover_from_checkpoint() == 1
assert successor.get('proc_worker') is not None
finally:
for proc in (worker_proc, owner_proc):
proc.kill()
proc.wait(timeout=10)
assert not psutil.pid_exists(worker_proc.pid) or psutil.Process(worker_proc.pid).status() == psutil.STATUS_ZOMBIE