cli_subagent_monitor.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. """Classic CLI live-work dock (subagents, background processes, active goal, standing loop, queued
  2. prompts) and scoped controls; no agent-loop state is changed. Process rows come from
  3. ``cli_process_dock``, goal/loop/queue rows from ``cli_session_dock``."""
  4. from __future__ import annotations
  5. import json
  6. import logging
  7. import time
  8. from prompt_toolkit.utils import get_cwidth
  9. from agent.i18n import t
  10. from hermes_cli import cli_process_dock as procs
  11. from hermes_cli import cli_session_dock as session_rows
  12. logger = logging.getLogger(__name__)
  13. def _clip(value, width):
  14. text = ' '.join(str(value or '').split())
  15. text = ''.join(c for c in text if c.isprintable())
  16. if get_cwidth(text) <= width:
  17. return text
  18. result = ''
  19. for char in text:
  20. if get_cwidth(result + char) > max(0, width - 1):
  21. break
  22. result += char
  23. return result + ('…' if width else '')
  24. class SubagentMonitor:
  25. def __init__(self, cli):
  26. self.cli = cli
  27. self.entries = []
  28. self.processes = []
  29. self.goal = ''
  30. self.loop = ''
  31. self.loop_badge = ''
  32. self.loop_prompt = ''
  33. self.queued = []
  34. self.selected_id = None
  35. self._signature = None
  36. self._last_poll = 0
  37. self.app = None
  38. self.opening = False
  39. self.collapsed = False
  40. self.detail = False # the monitor's detail view is open (its tick fetches the gateway tail)
  41. @property
  42. def has_rows(self):
  43. return bool(self.entries or self.processes or self.goal or self.loop or self.queued)
  44. @property
  45. def roster(self):
  46. """Agents first, then processes — the order the dock and the full-height monitor paint."""
  47. return [*self.entries, *self.processes]
  48. @staticmethod
  49. def _key(row):
  50. return row.get('key', row.get('subagent_id'))
  51. @property
  52. def selected(self):
  53. return next((r for r in self.roster if self._key(r) == self.selected_id), None)
  54. @property
  55. def selected_process(self):
  56. row = self.selected
  57. return row if row and row.get('kind') == 'process' else None
  58. def subagents(self):
  59. """The subagent backend: the gateway session's view when its children run there (CLI-12),
  60. else this process's own registry; both answer the same ``subagent_*`` calls."""
  61. from hermes_cli.gateway_tools import LocalSubagents, process_view
  62. view = process_view()
  63. if view is not None:
  64. return view
  65. if getattr(self, '_local', None) is None:
  66. self._local = LocalSubagents(self.cli)
  67. return self._local
  68. def refresh(self, now=None):
  69. now = time.time() if now is None else now
  70. entries = self.subagents().subagent_rows(busy=bool(getattr(self.cli, '_agent_running', False)))
  71. for row in entries:
  72. row['elapsed'] = max(0, int(now - (row.get('started_at') or now)))
  73. row['last_tool'] = row.get('last_tool') or ''
  74. row['key'] = row['subagent_id']
  75. row.pop('running_seconds', None)
  76. processes = procs.process_rows(now)
  77. session_rows.sync_automation(self.cli)
  78. goal = session_rows.goal_line(self.cli)
  79. loop_badge, loop, loop_prompt = session_rows.loop_row(self.cli, now)
  80. queued = session_rows.queued_prompts(self.cli)
  81. signature = json.dumps([entries, processes, goal, loop, queued], sort_keys=True, default=str)
  82. changed = signature != self._signature
  83. self._signature = signature
  84. self.entries = entries
  85. self.processes = processes
  86. self.goal = goal
  87. self.loop = loop
  88. self.loop_badge = loop_badge
  89. self.loop_prompt = loop_prompt
  90. self.queued = queued
  91. if self.selected is None:
  92. roster = self.roster
  93. self.selected_id = self._key(roster[0]) if roster else None
  94. return changed
  95. def invalidate(self):
  96. from hermes_cli.cli_terminal_mixin import _run_on_app_loop
  97. app = self.app
  98. if app is not None:
  99. # Teardown clears app.loop; don't let it interleave with a worker's
  100. # invalidate call, which reads the loop more than once.
  101. _run_on_app_loop(app, app.invalidate)
  102. def poll_soon(self):
  103. """Let the spinner thread's next tick poll the gateway, instead of the caller blocking the
  104. UI loop on it."""
  105. self._last_poll = 0
  106. def tick(self):
  107. now = time.monotonic()
  108. if now - self._last_poll < 1:
  109. return
  110. self._last_poll = now
  111. try:
  112. changed = self.refresh()
  113. changed = self._poll_tail() or changed
  114. except Exception: # health: allow BLE001 -- the spinner thread runs this tick; one raise would end that thread for the session
  115. logger.debug("subagent monitor tick failed", exc_info=True)
  116. return
  117. if changed:
  118. if self.app is not None:
  119. self.invalidate()
  120. else:
  121. self.cli._invalidate()
  122. def _poll_tail(self):
  123. """Off the render path (the spinner thread's tick): refresh the gateway tail the open detail
  124. view paints from the cache. True when the text changed."""
  125. row = self.selected
  126. if not self.detail or row is None or row.get('kind') == 'process':
  127. return False
  128. backend = self.subagents()
  129. before = backend.subagent_tail(row['subagent_id'])
  130. return backend.subagent_tail(row['subagent_id'], fetch=True) != before
  131. def select(self, delta):
  132. roster = self.roster
  133. if roster:
  134. index = next((i for i, r in enumerate(roster) if self._key(r) == self.selected_id), 0)
  135. self.selected_id = self._key(roster[(index + delta) % len(roster)])
  136. def control(self, action, message=None, *, target=None):
  137. target = target or self.selected_id
  138. if any(r['key'] == target for r in self.processes):
  139. if action != 'stop':
  140. return {'error': t('cli.subagents.cannot_steer_process')}
  141. return procs.kill(target)
  142. return self.subagents().subagent_control(action, target, message)
  143. def _counts(self, *, session=True):
  144. """Count fragment: ``2 live``, ``1 proc``, ``2 live · 3 procs``; the collapsed heading
  145. (``session=True``) adds ``goal active|paused|parked``, the loop badge and ``N queued``."""
  146. parts = []
  147. if self.entries:
  148. parts.append(t('cli.subagents.count_live', count=len(self.entries)))
  149. if self.processes:
  150. running = sum(r['status'] == 'running' for r in self.processes)
  151. if running:
  152. parts.append(t('cli.subagents.count_procs_one' if running == 1 else 'cli.subagents.count_procs_other',
  153. count=running))
  154. else:
  155. parts.append(t('cli.subagents.count_done', count=len(self.processes)))
  156. if session and self.goal:
  157. parts.append(t('cli.subagents.goal_parked' if self.goal.startswith('⏳') else
  158. 'cli.subagents.goal_paused' if self.goal.startswith('⏸') else 'cli.subagents.goal_active'))
  159. if session and self.loop_badge:
  160. parts.append(self.loop_badge)
  161. if session and self.queued:
  162. parts.append(t('cli.subagents.count_queued', count=len(self.queued)))
  163. return ' · '.join(parts)
  164. def _title(self):
  165. if self.entries and self.processes:
  166. return t('cli.subagents.title_live_work')
  167. return t('cli.subagents.title_subagents') if self.entries else t('cli.subagents.title_processes')
  168. @staticmethod
  169. def _agent_activity(row):
  170. """``last: <tool>`` while a tool runs, else the raw status id, else ``starting``."""
  171. if row.get('last_tool'):
  172. return t('cli.subagents.last_tool', tool=row['last_tool'])
  173. return row.get('status') or t('cli.subagents.starting')
  174. def _collapsed_activity(self):
  175. if self.entries:
  176. return self._agent_activity(self.entries[0])
  177. if self.processes:
  178. return procs.process_activity(self.processes[0])
  179. # The loop's state is already the heading's badge; its prompt is the activity.
  180. return self.goal or self.loop_prompt or t('cli.subagents.next_queued', text=self.queued[0])
  181. def dock_text(self, *, columns, rows):
  182. if not self.has_rows:
  183. return ''
  184. if self.collapsed:
  185. count = self._counts()
  186. # Keep both controls before spending scarce cells on activity. Ctrl+T opens the
  187. # subagent/process monitor, so a goal/queue-only dock offers just the
  188. # collapse/restore shortcut.
  189. if self.entries or self.processes:
  190. headings = (
  191. t('cli.subagents.heading_full', title=self._title(), count=count),
  192. t('cli.subagents.heading_medium', count=count),
  193. t('cli.subagents.heading_compact', count=count),
  194. count,
  195. )
  196. else:
  197. headings = (t('cli.subagents.heading_session_restore', count=count),
  198. t('cli.subagents.heading_session_compact', count=count), count)
  199. width = max(0, columns - 1)
  200. heading = next((text for text in headings if get_cwidth(text) <= width), count)
  201. activity = self._collapsed_activity()
  202. # A goal/queue preview is long prose: clip it into the room left instead of dropping it.
  203. room = width - get_cwidth(heading + ' · ')
  204. if get_cwidth(activity) <= room or (room >= 12 and not (self.entries or self.processes)):
  205. heading += ' · ' + _clip(activity, room)
  206. return _clip(' ' + heading, max(0, columns))
  207. columns = max(0, columns - 2)
  208. lines = [_clip(f' {row}', columns) for row in (self.goal, self.loop) if row]
  209. budget = max(1, min(4, (rows - 10) // 3))
  210. # Both blocks present: split the row budget so neither hides the other entirely.
  211. agent_budget = budget if not self.processes else max(1, budget - max(1, budget // 2))
  212. agent_count = min(len(self.entries), agent_budget)
  213. if self.entries:
  214. hidden = len(self.entries) - agent_count
  215. lines.append(_clip(' ' + t('cli.subagents.subagents_heading', count=len(self.entries)), columns))
  216. for row in self.entries[:agent_count]:
  217. activity = f"{row['elapsed']}s · " + self._agent_activity(row)
  218. # Reserve activity even on narrow terminals; task names use the remainder.
  219. goal_width = max(3, columns - get_cwidth(activity) - 5)
  220. lines.append(_clip(f" ● {_clip(row.get('goal'), goal_width)} · {activity}", columns))
  221. if hidden:
  222. lines.append(_clip(' ' + t('cli.subagents.more_subagents', count=hidden), columns))
  223. if self.processes:
  224. proc_count = min(len(self.processes), max(1, budget - agent_count))
  225. running = sum(r['status'] == 'running' for r in self.processes)
  226. done = len(self.processes) - running
  227. summary = ' · '.join(p for p in (
  228. t('cli.subagents.count_running', count=running) if running else '',
  229. t('cli.subagents.count_done', count=done) if done else '') if p)
  230. controls = t('cli.subagents.controls_expand_collapse') if not self.entries else ''
  231. lines.append(_clip(' ' + t('cli.subagents.processes_heading', summary=summary, controls=controls), columns))
  232. for row in self.processes[:proc_count]:
  233. activity = procs.process_activity(row)
  234. command_width = max(3, columns - get_cwidth(activity) - 5)
  235. lines.append(_clip(f" {procs.process_glyph(row)} {_clip(row['command'], command_width)} · {activity}", columns))
  236. if len(self.processes) > proc_count:
  237. lines.append(_clip(
  238. ' ' + t('cli.subagents.more_processes', count=len(self.processes) - proc_count), columns))
  239. if self.queued:
  240. # Last, so the next prompt to run sits right above the input it came from.
  241. shown = min(len(self.queued), session_rows.QUEUE_ROWS if rows >= 24 else 1)
  242. controls = '' if self.entries or self.processes else t('cli.subagents.controls_collapse')
  243. lines.append(_clip(
  244. ' ' + t('cli.subagents.queue_heading', count=len(self.queued), controls=controls), columns))
  245. for index, text in enumerate(self.queued[:shown], 1):
  246. lines.append(_clip(f' {index}. {text}', columns))
  247. if len(self.queued) > shown:
  248. lines.append(_clip(' ' + t('cli.subagents.more_queued', count=len(self.queued) - shown), columns))
  249. return '\n'.join(' ' + line for line in lines)
  250. def read_tail(path):
  251. if not path:
  252. return t('cli.subagents.transcript_unavailable')
  253. try:
  254. with open(path, 'rb') as stream:
  255. stream.seek(0, 2)
  256. stream.seek(max(0, stream.tell() - 32768))
  257. text = stream.read(32768).decode('utf-8', errors='replace')
  258. return ''.join(c for c in text if c.isprintable() or c in '\n\t')
  259. except OSError:
  260. return t('cli.subagents.transcript_unavailable')
  261. def subagent_tail(monitor, row):
  262. """The child's transcript tail from the monitor's backend (the gateway's is fetched and filtered by
  263. the monitor's tick, never here: the detail view calls this every frame)."""
  264. text = monitor.subagents().subagent_tail(row['subagent_id'])
  265. return t('cli.subagents.transcript_unavailable') if text is None else text
  266. def modal_prompt_active(cli):
  267. """A prompt owns the composer (its text is an answer or a secret, never a chat message):
  268. every composer shortcut stays off while one is open."""
  269. return any(getattr(cli, name, None) for name in (
  270. '_clarify_state', '_approval_state', '_slash_confirm_state', '_sudo_state',
  271. '_secret_state', '_connection_state', '_model_picker_state', '_command_palette_state'))
  272. def build_monitor_application(monitor, **kwargs):
  273. from prompt_toolkit.application import Application
  274. from prompt_toolkit.data_structures import Point
  275. from prompt_toolkit.filters import Condition
  276. from prompt_toolkit.key_binding import KeyBindings
  277. from prompt_toolkit.layout import ConditionalContainer, HSplit, Layout, Window
  278. from prompt_toolkit.layout.controls import FormattedTextControl
  279. from prompt_toolkit.widgets import TextArea
  280. state = {'steering': False, 'confirm': False, 'notice': ''}
  281. steer = TextArea(height=1, prompt=t('cli.subagents.steer_prompt'), multiline=False)
  282. tail = TextArea(read_only=True, scrollbar=True, wrap_lines=True)
  283. def roster_text():
  284. size = app.output.get_size()
  285. rows = []
  286. for row in monitor.roster:
  287. selected = monitor._key(row) == monitor.selected_id
  288. if row.get('kind') == 'process':
  289. prefix = f"{procs.process_glyph(row)} {procs.process_activity(row)} · "
  290. activity = ''
  291. subject = row['command']
  292. else:
  293. prefix = f"{row['elapsed']}s · {row.get('status') or t('cli.subagents.starting')} · "
  294. activity = ' · ' + t('cli.subagents.last_tool', tool=row['last_tool']) if row.get('last_tool') else ''
  295. subject = row.get('goal') or row['subagent_id']
  296. goal_width = max(0, size.columns - 2 - get_cwidth(prefix + activity))
  297. text = f"{'❯' if selected else ' '} " + _clip(prefix + _clip(subject, goal_width) + activity, max(0, size.columns - 2))
  298. # Pad selection in terminal cells, not codepoints (task names may be wide).
  299. text += ' ' * max(0, size.columns - get_cwidth(text))
  300. rows.append(('class:subagent-dock.selected' if selected else '', text + '\n'))
  301. return rows or [('', t('cli.subagents.roster_empty'))]
  302. def cursor():
  303. index = next((i for i, row in enumerate(monitor.roster) if monitor._key(row) == monitor.selected_id), 0)
  304. return Point(x=0, y=index)
  305. roster = Window(FormattedTextControl(roster_text, focusable=True, get_cursor_position=cursor))
  306. def update_tail():
  307. row = monitor.selected
  308. if row is None:
  309. text = t('cli.subagents.entry_gone')
  310. elif row.get('kind') == 'process':
  311. text = procs.process_tail(row['id'])
  312. else:
  313. text = subagent_tail(monitor, row)
  314. if text != tail.text:
  315. following = tail.buffer.cursor_position == len(tail.text)
  316. position = tail.buffer.cursor_position
  317. tail.text = text
  318. tail.buffer.cursor_position = len(text) if following else min(position, len(text))
  319. def header():
  320. row = monitor.selected
  321. title = f"{monitor._title()} · {monitor._counts(session=False)}"
  322. if monitor.detail and row:
  323. title += f" · {monitor._key(row)} · {row.get('goal') or row.get('command') or ''}"
  324. return [('class:subagent-dock.heading', _clip(title, app.output.get_size().columns))]
  325. def footer():
  326. narrow = app.output.get_size().columns < 60
  327. process = monitor.selected_process is not None
  328. if state['confirm']:
  329. noun = t('cli.subagents.noun_process' if process else 'cli.subagents.noun_subagent')
  330. return (t('cli.subagents.footer_confirm_stop_compact') if narrow
  331. else t('cli.subagents.footer_confirm_stop', noun=noun))
  332. if state['steering']:
  333. return t('cli.subagents.footer_steering_compact' if narrow else 'cli.subagents.footer_steering')
  334. steer = '' if process else t('cli.subagents.footer_steer_key')
  335. if narrow:
  336. return (t('cli.subagents.footer_detail_compact', steer=steer) if monitor.detail
  337. else t('cli.subagents.footer_roster_compact'))
  338. steer = '' if process else t('cli.subagents.footer_steer_key_long')
  339. return t('cli.subagents.footer_detail' if monitor.detail else 'cli.subagents.footer_roster', steer=steer)
  340. def run_control(action, message, target):
  341. """Steer/stop off the UI loop: a stalled gateway call (up to its timeout) must not freeze the
  342. monitor's repaint, Esc or Ctrl+C. The answer lands in the notice line when it arrives."""
  343. import asyncio
  344. state['notice'] = t('cli.subagents.control_pending')
  345. def done(future):
  346. try:
  347. result = future.result()
  348. except Exception as exc: # health: allow BLE001 -- a control failure is shown, never raised into the UI loop
  349. result = {'error': str(exc)}
  350. state['notice'] = result.get('error') or result.get('note') or str(result)
  351. app.invalidate()
  352. asyncio.get_running_loop().run_in_executor(
  353. None, lambda: monitor.control(action, message, target=target)).add_done_callback(done)
  354. kb = KeyBindings()
  355. normal = Condition(lambda: not state['steering'] and not state['confirm'])
  356. listing = normal & Condition(lambda: not monitor.detail)
  357. @kb.add('up', filter=listing)
  358. def up(event):
  359. monitor.select(-1)
  360. @kb.add('down', filter=listing)
  361. def down(event):
  362. monitor.select(1)
  363. @kb.add('enter', filter=listing)
  364. def detail(event):
  365. if monitor.selected:
  366. monitor.detail = True
  367. monitor.poll_soon() # the spinner thread's next tick fetches the tail
  368. update_tail()
  369. app.layout.focus(tail)
  370. @kb.add('s', filter=normal)
  371. def start_steer(event):
  372. if monitor.selected and monitor.selected_process is None:
  373. state['steering'] = True
  374. state['target'] = monitor.selected_id
  375. app.layout.focus(steer)
  376. @kb.add('enter', filter=Condition(lambda: state['steering']))
  377. def send_steer(event):
  378. if not steer.text.strip():
  379. return
  380. run_control('steer', steer.text, state['target'])
  381. steer.text = ''
  382. state['steering'] = False
  383. app.layout.focus(tail if monitor.detail else roster)
  384. @kb.add('x', filter=normal)
  385. def stop(event):
  386. if monitor.selected:
  387. state['confirm'] = True
  388. state['target'] = monitor.selected_id
  389. @kb.add('y', filter=Condition(lambda: state['confirm']))
  390. def confirm(event):
  391. run_control('stop', None, state['target'])
  392. state['confirm'] = False
  393. @kb.add('escape', eager=True)
  394. def back(event):
  395. if state['steering'] or state['confirm']:
  396. state['steering'] = state['confirm'] = False
  397. app.layout.focus(tail if monitor.detail else roster)
  398. elif monitor.detail:
  399. monitor.detail = False
  400. app.layout.focus(roster)
  401. else:
  402. app.exit()
  403. @kb.add('q', filter=normal)
  404. @kb.add('f6', filter=normal)
  405. @kb.add('c-t', filter=normal)
  406. @kb.add('c-c')
  407. def close(event):
  408. app.exit()
  409. layout = Layout(HSplit([
  410. Window(FormattedTextControl(header), height=1),
  411. ConditionalContainer(roster, filter=Condition(lambda: not monitor.detail)),
  412. ConditionalContainer(tail, filter=Condition(lambda: monitor.detail)),
  413. ConditionalContainer(steer, filter=Condition(lambda: state['steering'])),
  414. Window(FormattedTextControl(lambda: _clip(state['notice'], app.output.get_size().columns)), height=1),
  415. Window(FormattedTextControl(footer), height=1),
  416. ], style='class:subagent-dock'), focused_element=roster)
  417. def before_render(app):
  418. # Prompts arrive on worker threads; exit on the UI loop, including the
  419. # first frame if a prompt won the race with in_terminal() acquisition.
  420. if modal_prompt_active(monitor.cli) and not app.is_done:
  421. app.exit()
  422. elif monitor.detail:
  423. update_tail()
  424. from prompt_toolkit.styles import Style
  425. from hermes_cli.skin_engine import get_prompt_toolkit_style_overrides
  426. kwargs.setdefault('style', Style.from_dict(get_prompt_toolkit_style_overrides()))
  427. app = Application(layout=layout, key_bindings=kb, full_screen=True, mouse_support=False,
  428. before_render=before_render, **kwargs)
  429. return app
  430. def open_monitor(cli):
  431. import asyncio
  432. from prompt_toolkit.application import in_terminal
  433. monitor = getattr(cli, '_subagent_monitor', None)
  434. if monitor is None or monitor.opening:
  435. return
  436. monitor.opening = True
  437. async def run():
  438. try:
  439. async with in_terminal():
  440. # Paint the roster the dock's tick already holds; refreshing here would block the UI
  441. # loop on the gateway's subagent.list. The spinner thread's next tick re-reads it.
  442. monitor.poll_soon()
  443. monitor.app = build_monitor_application(monitor)
  444. await monitor.app.run_async()
  445. finally:
  446. monitor.app = None
  447. monitor.detail = monitor.opening = False
  448. cli._invalidate()
  449. asyncio.get_running_loop().create_task(run())
  450. def toggle_dock(cli):
  451. monitor = getattr(cli, '_subagent_monitor', None)
  452. if monitor is not None:
  453. monitor.collapsed = not monitor.collapsed
  454. cli._invalidate()
  455. def install_dock(cli):
  456. from prompt_toolkit.application import get_app
  457. from prompt_toolkit.layout import ConditionalContainer, Window
  458. from prompt_toolkit.layout.controls import FormattedTextControl
  459. from prompt_toolkit.filters import Condition
  460. monitor = SubagentMonitor(cli)
  461. cli._subagent_monitor = monitor
  462. monitor.refresh()
  463. def text():
  464. size = get_app().output.get_size()
  465. lines = monitor.dock_text(columns=size.columns, rows=size.rows).splitlines()
  466. return [('class:subagent-dock.heading' if i == 0 else '',
  467. line + ('\n' if i < len(lines) - 1 else ''))
  468. for i, line in enumerate(lines)]
  469. cli._subagent_dock_widget = ConditionalContainer(
  470. Window(FormattedTextControl(text), wrap_lines=False, dont_extend_height=True,
  471. style='class:subagent-dock'),
  472. filter=Condition(lambda: monitor.has_rows and not modal_prompt_active(cli)),
  473. )