Skip to content

The agent that grows with you

fix: attenuate worker reconnect tickets to execution operations

Tekniume7cd5f9380b1
diff --git a/agent/runtime_session_store.py b/agent/runtime_session_store.py
index a36e55b9da6..2acf9993cf1 100644
--- a/agent/runtime_session_store.py
+++ b/agent/runtime_session_store.py
@@ -46,7 +46,8 @@ class WorkerRPC:
if discovery.state != 'ready' or discovery.endpoint is None:
raise WorkerPersistenceError('owner_unavailable')
endpoint = discovery.endpoint
- ticket = _session_ticket(self.home, endpoint)
+ ticket = _session_ticket(self.home, endpoint,
+ purpose='interactive' if method == 'worker.register' else 'worker-adoption')
url = endpoint.api_origin.replace('https:', 'wss:').replace('http:', 'ws:') + '/api/ws'
with connect(url, subprotocols=['hermes-gateway-v1', 'hermes-gateway-ticket.' + ticket],
open_timeout=5, close_timeout=1, max_size=8 * 1024 * 1024) as ws:
@@ -84,7 +85,7 @@ class RuntimeSessionStore:
raise WorkerPersistenceError('unsafe_outbox')
os.chmod(self.path.parent, 0o700)
if self.path.exists():
- self.journal = json.loads(self.path.read_text())
+ self.journal = json.loads(self.path.read_text(encoding="utf-8"))
if self.journal['scope'] != self.scope:
raise WorkerPersistenceError('outbox_scope_mismatch')
else:
diff --git a/gateway/run_api.py b/gateway/run_api.py
index 43caac1da5e..3ded8886863 100644
--- a/gateway/run_api.py
+++ b/gateway/run_api.py
@@ -146,9 +146,13 @@ class GatewayRuntimeAPI:
ticket, profile_id=self.runner.session_authority.profile_id,
purpose='interactive')
except PermissionError:
- # Browser/OAuth tickets use the same protocol but a different
- # issuer. Let the existing gate validate them, never mint a grant.
- return await self.app(scope, receive, send)
+ try:
+ grant = self.runner.session_ticket_store.redeem(
+ ticket, profile_id=self.runner.session_authority.profile_id,
+ purpose='worker-adoption')
+ except PermissionError:
+ # Browser/OAuth tickets have a separate issuer.
+ return await self.app(scope, receive, send)
from tui_gateway.ws import handle_ws
await handle_ws(ws, auth_identity={'user_id': grant['subject'], 'provider': 'local',
'profile_id': grant['profile_id'],
diff --git a/gateway/session_worker.py b/gateway/session_worker.py
index a6a0df048b8..2a2e4c5c974 100644
--- a/gateway/session_worker.py
+++ b/gateway/session_worker.py
@@ -71,6 +71,8 @@ async def worker_request(connection, ref, params, *, operation):
raise RuntimeStoreError('unsupported_producer')
return await asyncio.to_thread(register_worker_execution, authority.db, epoch=authority.epoch,
**scope, kind='compute', adoption_secret=claim, require_idle=True)
+ if 'worker:adopt' not in connection.actor.capabilities:
+ raise RuntimeStoreError('permission_denied')
_verify(connection, ref, params, claim)
if operation == 'adopt':
return await asyncio.to_thread(adopt_worker_execution, authority.db, epoch=authority.epoch,
diff --git a/hermes_cli/gateway_client.py b/hermes_cli/gateway_client.py
index ccb9ac0332f..e63c01e51f3 100644
--- a/hermes_cli/gateway_client.py
+++ b/hermes_cli/gateway_client.py
@@ -71,10 +71,10 @@ class GatewayClient:
self.pending.pop(rid, None)
-def _session_ticket(home: Path, endpoint) -> str:
+def _session_ticket(home: Path, endpoint, *, purpose="interactive") -> str:
from hermes_cli.gateway_runtime_discovery import _socket_path, _identify_response
request = json.dumps({"protocol": 1, "id": 1, "verb": "session-ticket", "params": {
- "profile_id": endpoint.profile_id, "instance_id": endpoint.instance_id, "purpose": "interactive",
+ "profile_id": endpoint.profile_id, "instance_id": endpoint.instance_id, "purpose": purpose,
}}).encode() + b"\n"
if os.name == "nt":
from gateway.runtime_bootstrap_windows import query_runtime_control
diff --git a/tests/gateway/fixtures/persistence_worker.py b/tests/gateway/fixtures/persistence_worker.py
index ec62b98a8da..c07489104ee 100644
--- a/tests/gateway/fixtures/persistence_worker.py
+++ b/tests/gateway/fixtures/persistence_worker.py
@@ -41,6 +41,7 @@ for line in sys.stdin:
'execution_id': 'owned-worker', 'generation': 0,
'pid': os.getpid(), 'birth': psutil.Process().create_time(), 'secret': 'private-worker-secret'}
registration = transport('worker.register', **scope, kind='compute')
+ assert reason(lambda: transport('session.create', request_id='forbidden-worker-create', source='cli')) == 'permission_denied'
scope['epoch'] = registration['owner_epoch']
store = module.RuntimeSessionStore(transport, scope, home / 'worker-outboxes' / 'owned-worker', max_bytes=20000)
sid = scope['session_id']