relay_compaction.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Emit ``hermes.compaction`` records as NeMo Relay marks on the owning session's scope stack.
  2. The mark goes under the live turn of the same session when there is one, else under that session's
  3. scope (gateway hygiene and other out-of-turn compactions), else under the live turn the compacting agent
  4. started in (a child session a rotating commit created mid-turn), and never from a turn Relay does not
  5. instrument. The session's own stack matters: Relay resets LLM-history freshness only for the agent scope
  6. that owns a ``compaction`` mark, so the next LLM start after a committed rewrite records the full history
  7. again. No live Relay host or session scope means
  8. there is nothing to record. Relay exporters are configured by the user, so the mark adds no outbound
  9. path of its own.
  10. """
  11. from __future__ import annotations
  12. import logging
  13. from typing import Any
  14. from agent import relay_runtime
  15. logger = logging.getLogger(__name__)
  16. COMPACTION_DATA_SCHEMA = {"name": "hermes.compaction", "version": "1"}
  17. # Marks are observability: after one stalled publish, stop waiting on Relay for the rest of the process
  18. # so a wedged pipeline costs one bounded timeout, not one per compaction under the compression lease.
  19. _stalled = False
  20. def _turn_target(session_id: str) -> tuple[relay_runtime.RelayRuntime, relay_runtime.RelaySession, Any] | None:
  21. turn = relay_runtime.active_turn(session_id)
  22. host = turn.lease.live_runtime() if turn is not None else None
  23. if host is None:
  24. return None
  25. session = turn.lease.session
  26. return host, session, turn.handle or session.handle
  27. def _session_target(session_id: str) -> tuple[relay_runtime.RelayRuntime, relay_runtime.RelaySession, Any] | None:
  28. host = relay_runtime.HOST_REGISTRY.for_profile(create=False)
  29. if not isinstance(host, relay_runtime.RelayRuntime):
  30. return None
  31. session = host.get_session(session_id)
  32. if session is None or session.handle is None:
  33. return None
  34. return host, session, session.handle
  35. def _resolve_target(
  36. session_id: str, turn_session_id: str | None = None,
  37. ) -> tuple[relay_runtime.RelayRuntime, relay_runtime.RelaySession, Any] | None:
  38. """``(host, session, parent handle)`` for ``session_id``: its live turn, then its session scope, then the
  39. live turn of ``turn_session_id``.
  40. ``turn_session_id`` is the session the compacting agent's turn started in. A rotating commit moves the
  41. agent to a child session that has no Relay scope until the next turn opens one, so a later rotation in
  42. the same turn is marked on that turn. When the child already has a scope, that scope owns the next LLM
  43. starts for the child and keeps the mark. A turn Relay does not instrument (a second concurrent turn on
  44. the session, such as a background-review fork) gets nothing: its compaction rewrote its own transcript,
  45. not the live session's."""
  46. if not relay_runtime.relay_instrumentation_enabled():
  47. return None
  48. target = _turn_target(session_id) or _session_target(session_id)
  49. if target is None and turn_session_id:
  50. target = _turn_target(turn_session_id)
  51. return target
  52. def compaction_target_available(session_id: str, *, turn_session_id: str | None = None) -> bool:
  53. """Whether a mark for this session has a live Relay scope to receive it."""
  54. return bool(session_id and _resolve_target(session_id, turn_session_id))
  55. def emit_compaction_mark(
  56. session_id: str, name: str, data: dict[str, Any], *, turn_session_id: str | None = None,
  57. ) -> bool:
  58. """Emit one mark; returns False when no Relay scope owns ``session_id`` (see ``_resolve_target``)."""
  59. target = _resolve_target(session_id, turn_session_id) if session_id else None
  60. if target is None:
  61. logger.debug("no live Relay session for %s; %s mark not recorded", session_id or "none", name)
  62. return False
  63. global _stalled
  64. if _stalled:
  65. return False
  66. host, session, handle = target
  67. try:
  68. host.run_in_session(
  69. session, host.relay.scope.event, name, handle=handle, data=data,
  70. data_schema=dict(COMPACTION_DATA_SCHEMA), metadata=relay_runtime.runtime_metadata(host.runtime_id),
  71. timeout=relay_runtime.SCOPE_OP_TIMEOUT,
  72. )
  73. except TimeoutError:
  74. _stalled = True
  75. raise
  76. return True