fix: refuse unregistered child and cron persistence in compute workers
This commit is contained in:
@@ -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 acquire
|
||||
|
||||
@@ -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_result
|
||||
result = 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 authority
|
||||
|
||||
@@ -42,7 +42,8 @@ class Model(BaseHTTPRequestHandler):
|
||||
|
||||
|
||||
@pytest.mark.linux_only
|
||||
def test_real_agent_worker_persists_context_usage_and_releases_lease(tmp_path):
|
||||
@pytest.mark.parametrize('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.stderr
|
||||
proof = json.loads(receipt.read_text())
|
||||
assert not proof['failed'], proof
|
||||
assert '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'], proof
|
||||
assert proof['context']['input_tokens'] == 10, proof
|
||||
|
||||
@@ -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_process
|
||||
parent_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 None
|
||||
with _quiet("subagent: failed to open dedicated SessionDB; child persistence disabled", exc_info=True):
|
||||
|
||||
Reference in New Issue
Block a user