Skip to content

The agent that grows with you

fix(workers): background processes started in a managed turn survive settlement

teknium1bfb510bc3d3c
diff --git a/agent/managed_worker.py b/agent/managed_worker.py
index a87e79aa382..c0a37c28178 100644
--- a/agent/managed_worker.py
+++ b/agent/managed_worker.py
@@ -155,6 +155,15 @@ def outbox_dir(home, execution_id):
return Path(home) / 'worker-outboxes' / execution_id.replace(':', '-')
+def retire_agent(agent):
+ """A settled admission is a turn boundary, not the end of the session: the owner's
+ in-process agent keeps its background processes, sandbox and browser between turns
+ (release_clients), so the worker must too. Only memory extraction is turn-final work."""
+ messages = getattr(agent, '_session_messages', None)
+ agent.shutdown_memory_provider(messages if isinstance(messages, list) else None)
+ agent.release_clients()
+
+
def execute(frame, channel):
# The owner RPC below imports gateway/config modules (hermes_cli.config, providers,
# hermes_cli.plugins) transitively; the policy must already be frozen when they load.
@@ -168,6 +177,10 @@ def execute(frame, channel):
store = RuntimeSessionStore(rpc, scope, outbox_dir(frame['home'], scope['execution_id']))
from gateway.session_kanban import bind_worker_context
bind_worker_context(frame)
+ # Background processes a previous admission of this session started outlive that worker
+ # (F24); adopt them so process_manage in this turn can poll and kill them.
+ from tools.process_registry import process_registry
+ process_registry.recover_from_checkpoint()
# Store construction binds the delegation ledger before tool discovery.
from gateway.session_policy import restore_policy, policy_scope
policy = restore_policy(frame['policy'])
@@ -207,8 +220,7 @@ def execute(frame, channel):
raise ValueError('managed_attachment_unavailable')
frame = {**frame, 'text': content}
result = run_worker_turns(agent, frame, history)
- agent._end_session_on_close = False
- agent.close()
+ retire_agent(agent)
agent = None
store.flush_token_counts()
if result.get('final_response') is None and (result.get('interrupted') or result.get('failed')):
@@ -222,8 +234,7 @@ def execute(frame, channel):
finally:
unregister_gateway_notify(frame['route'])
if agent is not None:
- agent._end_session_on_close = False
- agent.close()
+ retire_agent(agent)
store.close()
diff --git a/tests/gateway/test_managed_worker_launch.py b/tests/gateway/test_managed_worker_launch.py
index 85a02ec7f48..30b3dfc03d3 100644
--- a/tests/gateway/test_managed_worker_launch.py
+++ b/tests/gateway/test_managed_worker_launch.py
@@ -39,7 +39,7 @@ class Model(BaseHTTPRequestHandler):
self.server.requests.append(body)
message = {'role': 'assistant', 'content': None, 'tool_calls': [{
'id': 'managed-tool', 'type': 'function', 'function': {'name': 'terminal',
- 'arguments': json.dumps({'command': self.server.command, 'timeout': 10})}}]}
+ 'arguments': json.dumps({'command': self.server.command, 'timeout': 10, **self.server.tool_args})}}]}
choice = {'index': 0, 'message': message, 'finish_reason': 'tool_calls' if message.get('tool_calls') else 'stop'}
frame = {'id': 'managed-model', 'model': 'managed-model', 'choices': [choice],
'usage': {'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}}
@@ -63,7 +63,7 @@ class Model(BaseHTTPRequestHandler):
@pytest.mark.linux_only
[email protected]('worker_action', ['detach', 'kill', 'controls', 'stop', 'history'])
[email protected]('worker_action', ['detach', 'kill', 'controls', 'stop', 'history', 'background'])
def test_ordinary_owner_launches_tool_worker_and_detach_does_not_cancel(tmp_path, worker_action):
root = Path(__file__).resolve().parents[2]
home, user = tmp_path / 'state', tmp_path / 'user'
@@ -76,6 +76,12 @@ def test_ordinary_owner_launches_tool_worker_and_detach_does_not_cancel(tmp_path
target.mkdir()
(target / 'owned.txt').write_text('owned')
peer.command = 'rm -rf ' + str(target) if peer.control_mode else 'printf MANAGED_TOOL_EFFECT'
+ peer.tool_args = {}
+ gate = tmp_path / 'background-exit'
+ if worker_action == 'background':
+ # A session-owned background process is a turn-boundary survivor, not turn litter (F24).
+ peer.command = f'printf MANAGED_TOOL_EFFECT; while [ ! -e {gate} ]; do sleep .1; done'
+ peer.tool_args = {'background': True}
peer.blocked, peer.release = threading.Event(), threading.Event()
thread = threading.Thread(target=peer.serve_forever, daemon=True)
thread.start()
@@ -189,6 +195,18 @@ def test_ordinary_owner_launches_tool_worker_and_detach_does_not_cancel(tmp_path
assert sum(role == 'assistant' and 'MANAGED_TOOL_DONE' in (content or '') for role, content in rows) == 1, rows
if peer.control_mode:
assert any(role == 'tool' and 'Beta' in content for role, content in rows), rows
+ elif worker_action == 'background':
+ started = json.loads(next(content for role, content in rows if role == 'tool'))
+ assert started['output'] == 'Background process started', started
+ background = psutil.Process(started['pid'])
+ assert background.is_running() and background.status() != psutil.STATUS_ZOMBIE, started
+ assert not psutil.pid_exists(pid), 'worker interpreter still alive after settlement'
+ # The next admission's worker adopts it from the profile checkpoint (process_manage).
+ assert started['pid'] in [e['pid'] for e in json.loads((home / 'processes.json').read_text())]
+ gate.touch()
+ async with asyncio.timeout(10):
+ while psutil.pid_exists(background.pid) and psutil.Process(background.pid).status() != psutil.STATUS_ZOMBIE:
+ await asyncio.sleep(.05)
else:
assert any(role == 'tool' and 'MANAGED_TOOL_EFFECT' in content for role, content in rows), rows
assert query('SELECT status FROM worker_executions WHERE session_id=?', (sid,)) == [('terminal',)]
@@ -225,6 +243,7 @@ def test_ordinary_owner_launches_tool_worker_and_detach_does_not_cancel(tmp_path
with daemon(root, home, env, barrier=False) as (owner, desc):
asyncio.run(exercise(desc, owner))
finally:
+ gate.touch()
peer.release.set()
peer.shutdown()
peer.server_close()