session_setting_commands.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """The messaging setting handlers (``/reasoning`` ``/busy`` ``/verbose`` ``/fast`` ...) on a LOCAL session.
  2. ``slash.exec`` runs the same ``_handle_<name>_command`` a messaging chat runs. A local session's
  3. next turn reads its settings from its live session and frozen policy, not from the runner's
  4. per-platform snapshot, so a handler that changes a setting also applies it here: one behaviour
  5. for every surface, held by the gateway.
  6. """
  7. import logging
  8. from types import SimpleNamespace
  9. logger = logging.getLogger(__name__)
  10. def local_live(runner, source):
  11. """The ``LiveSession`` of the local session ``source`` belongs to, else None (messaging chats)."""
  12. return local_authority_live(runner, source)[1]
  13. def local_policy(runner, source):
  14. """The frozen policy of a local session, else None."""
  15. if local_live(runner, source) is None:
  16. return None
  17. from gateway.session_policy import policy_for_source
  18. return policy_for_source(runner, source)
  19. def session_busy_mode(runner, live):
  20. """The busy mode a local session's next input runs under (what ``config.get busy`` reports)."""
  21. value = getattr(live, 'busy_input_mode', None)
  22. if value in ('interrupt', 'steer', 'queue'):
  23. return value
  24. from gateway.session_policy import policy_for_source
  25. policy = policy_for_source(runner, live.source)
  26. # Main's TUI defaulted to ``queue`` (``ui-tui/src/app/useConfigSync.ts`` TUI_BUSY_DEFAULT): in a
  27. # full-screen composer the next prompt is authored while the agent streams, and an unintended
  28. # interrupt loses work. Only an explicit ``display.busy_input_mode`` overrides it; every other
  29. # surface keeps the framework default ``interrupt``.
  30. fallback = 'queue' if policy is not None and getattr(policy, 'source', None) == 'tui' else 'interrupt'
  31. if policy is None:
  32. return runner._effective_busy_input_mode(live.source)
  33. value = (policy.config().get('display') or {}).get('busy_input_mode') or fallback
  34. return value if value in ('interrupt', 'steer', 'queue') else fallback
  35. def local_live_for_route(runner, session_key):
  36. """The local ``LiveSession`` whose route is ``session_key``, else None."""
  37. if not session_key:
  38. return None
  39. from gateway.config import Platform
  40. from gateway.session_authorities import all_authorities
  41. for authority in all_authorities(runner):
  42. for live in list(getattr(authority, 'sessions', {}).values()):
  43. if live.route == session_key and getattr(live.source, 'platform', None) == Platform.LOCAL:
  44. return live
  45. return None
  46. def local_authority_live(runner, source):
  47. """``(authority, live)`` owning the local session ``source`` belongs to, else ``(None, None)``."""
  48. from gateway.config import Platform
  49. if source is None or getattr(source, 'platform', None) != Platform.LOCAL:
  50. return None, None
  51. from gateway.session_authorities import all_authorities
  52. for authority in all_authorities(runner):
  53. live = getattr(authority, 'sessions', {}).get(source.chat_id)
  54. if live is not None and live.source.platform == Platform.LOCAL:
  55. return authority, live
  56. return None, None
  57. _FENCE_ATTEMPTS = 3
  58. def _reasoning_mutation(ref, row, effort):
  59. """The owner's fenced ``reasoning`` mutation of a ``/reasoning`` command, on *row*'s current fences."""
  60. import uuid
  61. return {'session_id': ref.session_id, 'request_id': 'reasoning-command:' + uuid.uuid4().hex,
  62. 'expected_revision': row['runtime_revision'], 'expected_generation': row['runtime_generation'],
  63. 'operation': 'reasoning', 'payload': {'effort': effort}}
  64. async def commit_reasoning_command(authority, actor, ref, value, persist_global):
  65. """``/reasoning <level>`` in either scope, or ``reset``, on a LOCAL session (``config.set`` and
  66. ``slash.exec``): the owner's fenced ``reasoning`` mutation, before the handler replies or writes
  67. config.yaml (``--global``). The level is the session's durable choice and a reset gives it back to the
  68. profile, whatever an older pick said, after an owner restart too. Display words and reads commit nothing,
  69. and so does a messaging chat (no session policy). While a turn runs, the handler applies it for the next
  70. turn as on main and the drain commits it at that turn's end (``apply_pending_reasoning``)."""
  71. import asyncio
  72. from gateway.config import Platform
  73. from gateway.session_mutations import mutate_session
  74. from hermes_constants import parse_reasoning_effort
  75. from hermes_state_mutation_guards import MUTATION_GUARD_REFUSALS
  76. from hermes_state_runtime import RuntimeStoreError
  77. if value == 'reset' and not persist_global: # ``reset --global`` is the handler's named refusal
  78. effort = None
  79. elif value != 'reset' and parse_reasoning_effort(value) is not None:
  80. effort = value
  81. else:
  82. return
  83. live = authority.sessions[ref.session_id]
  84. if live.source.platform != Platform.LOCAL:
  85. return
  86. # The fences are read here, not sent by a client: another viewer's mutation landing in between
  87. # (``revision_conflict``) is no verdict on this command, so it re-reads them. A fence that keeps
  88. # moving defers to the turn boundary like a busy session, as main always applied the command.
  89. for _attempt in range(_FENCE_ATTEMPTS):
  90. row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
  91. if row is None:
  92. raise RuntimeStoreError('not_found')
  93. try:
  94. await mutate_session(authority, actor, ref, _reasoning_mutation(ref, row, effort))
  95. return
  96. except (RuntimeStoreError, *MUTATION_GUARD_REFUSALS) as exc:
  97. reason = getattr(exc, 'reason', 'session_busy')
  98. if reason == 'revision_conflict':
  99. continue
  100. if reason not in ('session_busy', 'unknown_execution'):
  101. raise
  102. break
  103. live.pending_reasoning = (actor.subject, effort)
  104. async def apply_pending_reasoning(authority, ref, live):
  105. """Commit a reasoning command that arrived while a turn ran. The drain calls this at the turn boundary,
  106. under the claim gate and mutation lock, before a deferred model pick (whose level wins, as on main)."""
  107. import asyncio
  108. from gateway.session_mutations import write_mutation
  109. from hermes_state_mutation_guards import MUTATION_GUARD_REFUSALS
  110. from hermes_state_runtime import RuntimeStoreError
  111. pending, live.pending_reasoning = live.pending_reasoning, None
  112. if pending is None:
  113. return
  114. try:
  115. row = await asyncio.to_thread(authority.db.get_session, ref.session_id)
  116. await write_mutation(authority, ref, _reasoning_mutation(ref, row, pending[1]), None, principal_id=pending[0])
  117. except (RuntimeStoreError, *MUTATION_GUARD_REFUSALS) as exc:
  118. if getattr(exc, 'reason', 'session_busy') in ('session_busy', 'unknown_execution'):
  119. live.pending_reasoning = live.pending_reasoning or pending # a worker still runs: the next boundary
  120. else:
  121. logger.warning('Reasoning command on %s not committed: %s', ref.session_id, exc.reason)
  122. except Exception:
  123. # Before the claim: escaping would kill or wedge the drain (the in-memory level still applies).
  124. logger.exception('Reasoning command on %s failed to commit', ref.session_id)
  125. def defer_local_model_pick(authority, live, result, effort=''):
  126. """A typed ``/model`` on a local session: the resolved target becomes the session's canonical
  127. ``model`` mutation, committed by the owner at the next turn boundary (the typed command is
  128. itself the running admission). One writer, so the next request, ``sessions.model`` and
  129. ``session.info`` move together, or the commit is announced as a refusal."""
  130. import uuid
  131. from gateway.session_contract import SessionRef
  132. from gateway.session_deferred_pick import defer_pick
  133. from hermes_state_local import local_receipt
  134. sid = live.source.chat_id
  135. owner = SimpleNamespace(subject=local_receipt(authority.db, sid)['principal_id'])
  136. # The alias the user typed, not the bare model it names: two aliases may name one model on one
  137. # provider at different endpoints, and re-resolving the bare id picks the first such alias.
  138. payload = {'model': result.resolved_via_alias or result.new_model}
  139. if result.target_provider:
  140. payload['provider'] = result.target_provider
  141. if effort:
  142. payload['reasoning'] = effort # `--reasoning <level>`: committed in the same selection
  143. defer_pick(authority, owner, SessionRef(authority.profile_id, sid),
  144. {'request_id': 'typed-model:' + uuid.uuid4().hex, 'payload': payload})