The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: refuse unregistered child and cron persistence in compute workers
Teknium2ee0725c324f
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex 2126f728879..d3047de2e83 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -1682,6 +1682,11 @@ def _open_cron_session_db(job: dict):# no timeout of its own against a wedged sqlite3.connect (e.g. a stale flock left by a crashed sibling# process). An unbounded hang here would wedge the job's worker thread, so the init is bounded and a# timeout proceeds without a session store instead of blocking the run forever.+ from agent.runtime_session_store import WorkerPersistenceError, is_worker_process+ if is_worker_process():+ # Do not turn an unsupported worker assignment into the legacy None+ # fallback: that would run billed inference without durable persistence.+ raise WorkerPersistenceError('worker_cron_registration_required')_session_db_timeout = _get_session_db_timeout()try:from hermes_state_registry import acquirediff --git a/tests/gateway/fixtures/agent_persistence_worker.py b/tests/gateway/fixtures/agent_persistence_worker.pyindex 4fc47078505..8f0f1ab34aa 100644--- a/tests/gateway/fixtures/agent_persistence_worker.py+++ b/tests/gateway/fixtures/agent_persistence_worker.py@@ -45,12 +45,38 @@ def main():session_db=store, session_id=command['session_id'], enabled_toolsets=[],skip_context_files=True, skip_memory=True, skip_background_review=True, quiet_mode=True)try:+ refusals = {}+ if command.get('probe_constructors'):+ from concurrent.futures import ThreadPoolExecutor+ from hermes_state_registry import release_or_close+ from tools.delegate_tool import _open_child_session_db+ from tools.async_delegation import _connect+ from cron.scheduler import _open_cron_session_db, run_job+ operations = {'child': lambda: _open_child_session_db(agent),+ 'cron': lambda: _open_cron_session_db({'id': 'worker-probe'}),+ 'ledger': _connect}+ def attempt(name, fn):+ try:+ opened = fn()+ if opened is not None:+ release_or_close(opened)+ return name, 'unexpected_success'+ except Exception as exc:+ return name, str(exc)+ with ThreadPoolExecutor(max_workers=1) as pool:+ refusals = dict(pool.submit(attempt, name, fn).result() for name, fn in operations.items())+ scripts = home / 'scripts'+ scripts.mkdir(exist_ok=True)+ (scripts / 'worker-probe.py').write_text("print('SCRIPT_ONLY_RESULT')\n")+ script_result = run_job({'id': 'script-probe', 'name': 'script-probe', 'no_agent': True,+ 'script': 'worker-probe.py', 'prompt': '', 'deliver': 'local'})+ refusals['script_result'] = script_resultresult = agent.run_conversation('WORKER_INFERENCE')context = store.get_session(command['session_id'])store.flush_token_counts()finished = store.finish()receipt = {'result': result['final_response'], 'failed': result.get('failed'),- 'context': context, 'finished': finished, 'opens': opens, 'fds': writable_fds()}+ 'context': context, 'finished': finished, 'opens': opens, 'fds': writable_fds(), 'refusals': refusals}Path(command['receipt']).write_text(json.dumps(receipt, default=str))finally:agent._end_session_on_close = False # session lifecycle belongs to the authoritydiff --git a/tests/gateway/test_worker_agent_execution.py b/tests/gateway/test_worker_agent_execution.pyindex 7508a0d3198..02ad40b4e42 100644--- a/tests/gateway/test_worker_agent_execution.py+++ b/tests/gateway/test_worker_agent_execution.py@@ -42,7 +42,8 @@ class Model(BaseHTTPRequestHandler):@pytest.mark.linux_only-def test_real_agent_worker_persists_context_usage_and_releases_lease(tmp_path):[email protected]('probe_constructors', [False, True])+def test_real_agent_worker_persists_context_usage_and_releases_lease(tmp_path, probe_constructors):root = Path(__file__).resolve().parents[2]home, user = tmp_path / 'state', tmp_path / 'user'home.mkdir(mode=0o700)@@ -70,12 +71,19 @@ def test_real_agent_worker_persists_context_usage_and_releases_lease(tmp_path):receipt = tmp_path / 'receipt.json'result = subprocess.run([sys.executable, str(root / 'tests/gateway/fixtures/agent_persistence_worker.py')],cwd=root, env=env, input=json.dumps({'home': str(home), 'url': url, 'session_id': sid,- 'receipt': str(receipt)}) + '\n',+ 'receipt': str(receipt), 'probe_constructors': probe_constructors}) + '\n',text=True, capture_output=True, timeout=60)assert result.returncode == 0, result.stdout + result.stderrproof = json.loads(receipt.read_text())assert not proof['failed'], proofassert 'RECOVERY_ACK_WORKER_INFERENCE' in proof['result'], proof+ if probe_constructors:+ assert proof['refusals']['child'] == 'worker_child_registration_required', proof['refusals']+ assert proof['refusals']['cron'] == 'worker_cron_registration_required', proof['refusals']+ assert proof['refusals']['ledger'] == 'worker_delegation_ledger_unavailable', proof['refusals']+ success, doc, final_response, error = proof['refusals']['script_result']+ assert success and error is None and 'SCRIPT_ONLY_RESULT' in doc+ assert final_response.strip() == 'SCRIPT_ONLY_RESULT'assert proof['opens'] == [] and proof['fds'] == [], json.dumps(proof['opens'], indent=2)assert proof['context']['system_prompt'], proofassert proof['context']['input_tokens'] == 10, proofdiff --git a/tools/delegate_tool.py b/tools/delegate_tool.pyindex 4fe93157783..80b60d25d82 100644--- a/tools/delegate_tool.py+++ b/tools/delegate_tool.py@@ -94,7 +94,12 @@ def _open_child_session_db(parent_agent) -> Any:# profiles), and a bare SessionDB() would write the child's transcript into the launch profile's db,# breaking parent_session_id lineage and session_search. AsyncSessionDB wrappers (gateway) forward# .db_path via __getattr__, so this works through them.+ from agent.runtime_session_store import RuntimeSessionStore, WorkerPersistenceError, is_worker_processparent_session_db = getattr(parent_agent, "_session_db", None)+ if is_worker_process() or isinstance(parent_session_db, RuntimeSessionStore):+ # Missing db_path on a scoped store is NOT permission to open the+ # launch profile. Child identity must first be reserved by the owner.+ raise WorkerPersistenceError('worker_child_registration_required')if parent_session_db is None:return Nonewith _quiet("subagent: failed to open dedicated SessionDB; child persistence disabled", exc_info=True):