Skip to content

The agent that grows with you

fix(gateway): worker.register accepts only the owner's launched worker

Teknium8376dc761428
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.py
index 1238d71990e..396462acf53 100644
--- a/agent/runtime_session_store.py
+++ b/agent/runtime_session_store.py
@@ -73,8 +73,7 @@ class WorkerRPC:
descriptor = query_identify(control_home_for(self.home, endpoint), timeout=5)
if descriptor.get('pid') == os.getpid():
raise WorkerPersistenceError('synchronous_self_rpc')
- ticket = _session_ticket(self.home, endpoint,
- purpose='interactive' if method == 'worker.register' else 'worker-adoption')
+ ticket = _session_ticket(self.home, endpoint, purpose='worker-adoption')
url, protocols = gateway_ws_target(endpoint, ticket)
# The owner is loopback, like connect_gateway's peer: an inherited HTTPS_PROXY must not carry it.
with connect(url, subprotocols=protocols,
diff --git a/gateway/session_worker.py b/gateway/session_worker.py
index b62ef1927a8..6c3f162600f 100644
--- a/gateway/session_worker.py
+++ b/gateway/session_worker.py
@@ -76,7 +76,9 @@ async def worker_request(connection, ref, params, *, operation):
authority._require_admission_open()
scope = {key: params[key] for key in ('execution_id', 'session_id', 'generation')}
if operation == 'register':
- authority.authorize(connection.actor, ref, 'session:control')
+ # Only a worker-adoption ticket (what a launched worker carries): an interactive client
+ # must not park a 'registered' row that nothing reaps (the FIFO would pause until restart).
+ authority.authorize(connection.actor, ref, 'worker:adopt')
if params['kind'] != 'compute':
raise RuntimeStoreError('unsupported_producer')
return await asyncio.to_thread(register_worker_execution, authority.db, epoch=authority.epoch,
diff --git a/tests/gateway/test_worker_register_restriction.py b/tests/gateway/test_worker_register_restriction.py
new file mode 100644
index 00000000000..2e3a62ec0d6
--- /dev/null
+++ b/tests/gateway/test_worker_register_restriction.py
@@ -0,0 +1,50 @@
+"""worker.register requires the worker-adoption capability a launched worker's ticket carries
+(B1-14): an interactive client (session:control) must not park a 'registered' compute row that
+nothing reaps, pausing the session's FIFO until a restart turns it unknown. Real SQLite, real
+processes."""
+import subprocess
+import sys
+from types import SimpleNamespace
+
+import psutil
+import pytest
+
+
+async def test_worker_register_requires_the_owners_launched_worker(tmp_path, monkeypatch):
+ from gateway.config import Platform
+ from gateway.session_authority import LiveSession, SessionAuthority
+ from gateway.session_contract import Principal, SessionRef
+ from gateway.session_worker import worker_request
+ from hermes_state import SessionDB
+ from hermes_state_runtime import RuntimeStoreError, begin_runtime_epoch
+
+ monkeypatch.setenv('HERMES_HOME', str(tmp_path))
+ db = SessionDB(tmp_path / 'state.db')
+ db.create_session('owned', 'telegram')
+ runner = SimpleNamespace(_draining=False, config=SimpleNamespace(multiplex_profiles=False),
+ _adapter_for_source=lambda source: None)
+ authority = SessionAuthority(runner, profile_id='p', instance_id='owner', db=db,
+ epoch=begin_runtime_epoch(db, instance_id='owner'))
+ authority.sessions['owned'] = LiveSession(SimpleNamespace(platform=Platform.TELEGRAM, user_id='human'), 'route')
+ ref = SessionRef('p', 'owned')
+ child = subprocess.Popen([sys.executable, '-c', 'import sys; sys.stdin.read()'], stdin=subprocess.PIPE, # noqa: ASYNC220 -- Popen returns immediately
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+
+ def scope(pid):
+ return {'profile_id': 'p', 'session_id': 'owned', 'execution_id': f'compute-{pid}', 'generation': 0,
+ 'pid': pid, 'birth': psutil.Process(pid).create_time(), 'secret': 'w' * 32, 'kind': 'compute'}
+ interactive = SimpleNamespace(authority=authority, actor=Principal(
+ 'human', 'p', frozenset({'session:create', 'session:read', 'session:submit', 'session:control'}), 't'))
+ worker = SimpleNamespace(authority=authority, actor=Principal('human', 'p', frozenset({'worker:adopt'}), 't'))
+ try:
+ # A default interactive ticket, even naming a live process of the same user.
+ with pytest.raises(RuntimeStoreError, match='permission_denied'):
+ await worker_request(interactive, ref, scope(child.pid), operation='register')
+ assert db._read_one('SELECT COUNT(*) FROM worker_executions')[0] == 0
+ registered = await worker_request(worker, ref, scope(child.pid), operation='register')
+ assert registered['status'] == 'registered'
+ finally:
+ child.stdin.close()
+ child.wait(timeout=5)
+ db.close()