runtime_session_child.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """A delegated child's store inside a worker: owner-reserved, never self-registered (#141793).
  2. The parent's ``RuntimeSessionStore`` asks the owner for a child on its own receipt stream; the
  3. owner mints the child's session row and execution (gateway/session_worker_child.py) and the
  4. child persists through the ordinary ``worker.persist`` path under its own claim. A refusal
  5. (capacity, depth, draining) is a ValueError so ``delegate_task`` reports it as a tool error.
  6. """
  7. import secrets
  8. from functools import partial
  9. from agent.runtime_session_store import RuntimeSessionStore, call_off_event_loop
  10. _REFUSALS = {
  11. 'delegation_capacity': 'the gateway owner refused another subagent: this execution already runs '
  12. 'delegation.max_concurrent_children of them',
  13. 'delegation_depth_exceeded': 'the gateway owner refused a subagent this deep (delegation.max_spawn_depth)',
  14. }
  15. class ChildSessionStore(RuntimeSessionStore):
  16. binds_delegation_ledger = False
  17. @property
  18. def reserved_session_id(self):
  19. return self.scope['session_id']
  20. def create_session(self, session_id, source, **fields):
  21. # The owner created this row at reservation with its source, lineage and profile; only
  22. # the constructor facts are the child's to add.
  23. from hermes_state_worker_lifecycle import _CONSTRUCTOR_CONFIG
  24. config = {k: v for k, v in (fields.get('model_config') or {}).items() if k in _CONSTRUCTOR_CONFIG}
  25. return super().create_session(session_id, 'subagent', model=fields.get('model'),
  26. model_config=config or None, system_prompt=fields.get('system_prompt'))
  27. def close(self):
  28. # The child's agent closes once, after its last write: that is this execution's end.
  29. try:
  30. if not self._outbox_owner.closed and not self.failure:
  31. self.finish()
  32. finally:
  33. super().close()
  34. def reserve_child_store(parent):
  35. """A ``ChildSessionStore`` the owner reserved under ``parent``'s verified execution."""
  36. secret = secrets.token_urlsafe(32)
  37. # Reached from delegate_task's tool callback, which can run on Relay's loop (#139029).
  38. result = call_off_event_loop(parent._apply, 'child.reserve', {'secret': secret})
  39. if 'refused' in result:
  40. raise ValueError(_REFUSALS.get(result['refused'], 'the gateway owner refused a subagent: ' + result['refused']))
  41. child = result['child']
  42. scope = dict(parent.scope, **child, secret=secret)
  43. outbox = parent.path.parent / ('child-' + child['execution_id'].rsplit(':', 1)[1])
  44. # Every child RPC hops off a running loop, not only this reservation: the child is built, run
  45. # and closed inside delegate_task's callback, and its constructor already reads the store
  46. # (ContextCompressor.bind_session_state), which WorkerRPC refuses on Relay's loop.
  47. return ChildSessionStore(partial(call_off_event_loop, parent.rpc), scope, outbox)