Skip to content

The agent that grows with you

fix(gateway): a managed worker adopts an automation turn as the owner's OS user

Teknium0831057aa666
diff --git a/gateway/session_managed_worker.py b/gateway/session_managed_worker.py
index cf672513369..9a7d81e771a 100644
--- a/gateway/session_managed_worker.py
+++ b/gateway/session_managed_worker.py
@@ -15,7 +15,7 @@ import sys
import time
from agent.managed_worker import accept_result, encode_frame, read_frame
-from gateway.session_worker_reservation import reserve_admission_worker
+from gateway.session_worker_reservation import owner_worker_subject, reserve_admission_worker
from hermes_state_runtime import RuntimeStoreError
# The interpreter only imports psutil before it introduces itself; a child silent this long
@@ -627,7 +627,8 @@ async def execute_managed(authority, ref, row, policy):
# psutil + BEGIN IMMEDIATE: off-loop and retirement-tracked, like admission and claim.
from gateway.session_runtime_workers import tracked_write
scope = await tracked_write(authority, partial(reserve_admission_worker, authority,
- admission_id=row['admission_id'], process=process, principal_id=row['principal_id'], hello=hello))
+ admission_id=row['admission_id'], process=process, principal_id=row['principal_id'], hello=hello,
+ worker_subject=owner_worker_subject()))
worker.worker = (scope['pid'], scope['birth'])
# The child reads nothing else until the exact reservation has committed.
frame = await asyncio.to_thread(_bootstrap, authority, ref, row, policy, scope, text)
diff --git a/gateway/session_worker_reservation.py b/gateway/session_worker_reservation.py
index 1be3bc9e87a..4d996ec261f 100644
--- a/gateway/session_worker_reservation.py
+++ b/gateway/session_worker_reservation.py
@@ -50,12 +50,26 @@ def verify_worker_identity(process, hello):
return pid, birth
-def reserve_admission_worker(authority, *, admission_id, process, principal_id, hello):
+def owner_worker_subject():
+ """The subject a managed worker's adoption ticket carries: the owner's own OS identity. The
+ control channel mints a worker ticket only for a same-user peer (``uid:<n>`` / ``sid:<sid>``)."""
+ if os.name == 'nt':
+ from gateway.runtime_bootstrap_windows import _process_sid
+ return 'sid:' + _process_sid(os.getpid())
+ return f'uid:{os.getuid()}' # windows-footgun: ok — POSIX branch
+
+
+def reserve_admission_worker(authority, *, admission_id, process, principal_id, hello, worker_subject=None):
"""Return a private bootstrap scope; never accept caller-selected kind/target.
Only the owner process may reserve its live direct exec child. The admission
supplies physical session and generation; its durable principal must match
the producer. Do not publish the returned secret on any client event stream.
+
+ The adoption claim binds ``worker_subject``, the identity the worker will authenticate
+ as (``owner_worker_subject``), when given. An automation admission's principal is a route
+ (``automation:<session key>``), never a ticket subject, so a claim bound to it refused every
+ adopt and each bot DM or completion wake on a managed session ended interrupted (#144139).
"""
authority._require_admission_open()
pid, birth = verify_worker_identity(process, hello)
@@ -86,7 +100,7 @@ def reserve_admission_worker(authority, *, admission_id, process, principal_id,
"WHERE admission_id=?", (sid, admission_id))
scope = dict(profile_id=authority.profile_id, session_id=sid, execution_id=execution_id,
generation=generation, pid=pid, birth=birth, secret=secret)
- claim = admission_fingerprint(canonical_target=sid, payload=scope | {'principal': principal_id})
+ claim = admission_fingerprint(canonical_target=sid, payload=scope | {'principal': worker_subject or principal_id})
conn.execute("INSERT INTO worker_executions(execution_id,session_id,kind,owner_epoch,generation,status,adoption_digest) "
"VALUES(?,?,'compute',?,?,'registered',?)",
(execution_id, sid, authority.epoch, generation, _secret_digest(claim)))
diff --git a/tests/gateway/test_worker_reservation_automation_principal.py b/tests/gateway/test_worker_reservation_automation_principal.py
new file mode 100644
index 00000000000..fec5825a53a
--- /dev/null
+++ b/tests/gateway/test_worker_reservation_automation_principal.py
@@ -0,0 +1,72 @@
+"""A managed worker adopts an automation admission's reservation as the owner's own OS user (#144139)."""
+import os
+import subprocess
+import sys
+from types import SimpleNamespace
+
+import psutil
+import pytest
+
+from hermes_state import SessionDB
+from hermes_state_runtime import admit_session_input, begin_runtime_epoch, claim_session_input
+
+AUTOMATION = 'automation:agent:main:local:dm:owned'
+
+
+def _hello(process):
+ from hermes_cli.process_identity import worker_birth
+ return {'type': 'hello', 'pid': process.pid, 'birth': worker_birth(psutil.Process(process.pid)),
+ 'ancestors': [os.getpid()]}
+
+
+def reserved(tmp_path):
+ """A started automation admission (a bot DM or completion wake) and the owner's live direct child."""
+ db = SessionDB(tmp_path / 'state.db')
+ process = subprocess.Popen([sys.executable, '-c', 'import sys; sys.stdin.read()'], stdin=subprocess.PIPE,
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ try:
+ db.create_session('owned', 'cli')
+ epoch = begin_runtime_epoch(db, instance_id='owner')
+ authority = SimpleNamespace(db=db, epoch=epoch, profile_id=str(tmp_path), _require_admission_open=lambda: None,
+ wake_after_worker=lambda session_id: None)
+ admitted = admit_session_input(db, epoch=epoch, principal_id=AUTOMATION, session_id='owned',
+ request_id='bot:dm', payload={})
+ claim_session_input(db, epoch=epoch, session_id='owned')
+ yield authority, admitted, process
+ finally:
+ process.stdin.close()
+ process.wait(timeout=5)
+ db.close()
+
+
+def _connection(authority, subject):
+ actor = SimpleNamespace(subject=subject, profile_id=authority.profile_id, capabilities={'worker:adopt'})
+ return SimpleNamespace(authority=authority, actor=actor)
+
+
+async def test_worker_adopts_an_automation_admission_as_the_owner_user(reserved):
+ from gateway.session_worker import worker_request
+ from gateway.session_worker_reservation import owner_worker_subject, reserve_admission_worker
+ authority, admitted, process = reserved
+ scope = reserve_admission_worker(authority, admission_id=admitted['admission_id'], process=process,
+ principal_id=AUTOMATION, hello=_hello(process),
+ worker_subject=owner_worker_subject())
+ identity = {k: v for k, v in scope.items() if k != 'epoch'}
+ receipt = await worker_request(_connection(authority, owner_worker_subject()), None, identity, operation='adopt')
+ assert receipt['execution_id'] == scope['execution_id']
+
+
+async def test_the_claim_still_refuses_any_other_subject(reserved):
+ from gateway.session_worker import worker_request
+ from gateway.session_worker_reservation import owner_worker_subject, reserve_admission_worker
+ authority, admitted, process = reserved
+ scope = reserve_admission_worker(authority, admission_id=admitted['admission_id'], process=process,
+ principal_id=AUTOMATION, hello=_hello(process),
+ worker_subject=owner_worker_subject())
+ identity = {k: v for k, v in scope.items() if k != 'epoch'}
+ for subject in (AUTOMATION, 'uid:999999'):
+ with pytest.raises(Exception, match='permission_denied'):
+ await worker_request(_connection(authority, subject), None, identity, operation='adopt')