Builds on the salvaged yield/light-row change. The live walk still did its store work on the loop thread: `resolve()` -> `_handle()` hydrated a full row and read admissions per session, and the close test parsed a ~26 KB receipt per session. With ~900 live (mostly cron) rows each idle TUI poll cost 0.4-0.5 s of loop CPU and every gateway request waited ~3 s (#141236 comments). `gateway/session_live_listing.py` now owns the walk for both `session.list` (live) and `session.active_list`: authorization stays on the loop (in-memory), and per batch of 256 sessions ONE worker-thread read returns the picker columns, runtime revision/generation, pending admissions and the receipt target's `ended_at` (SQLite extracts the target, so no receipt is parsed in Python). A session retired during the read is dropped and never restored. `closed_and_unviewed`'s rule is shared through `closed_idle_unviewed`, so both paths read the same verdict. Fixes #141236
45 lines
2.5 KiB
Python
45 lines
2.5 KiB
Python
"""``session.active_list`` on the session authority: the live state of the turns it owns.
|
|
|
|
Desktop's silent-turn check (``session-states.ts`` ``onEventSilence``) and its sidebar poll read
|
|
this verb. Unregistered here, the WS fell back to the legacy sidecar's handler, which lists only
|
|
the sidecar's own ``_sessions`` and never an authority-owned session: a foreground tool quiet for
|
|
45 s read as a reaped turn, and Desktop painted "Hermes ended this turn without a reply" over a
|
|
turn that was still running. Rows keep ``server._session_live_item``'s shape; ``id`` and
|
|
``session_key`` are both the session id, as ``session.resume`` reports them.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from hermes_state_runtime import RuntimeStoreError
|
|
|
|
|
|
def _status(live, handle, pending) -> str:
|
|
if handle.execution_state == 'running':
|
|
prompts = live.controls.snapshot(handle.ref.session_id, handle.execution_generation)
|
|
return 'waiting' if prompts else 'working'
|
|
# Admitted, not yet claimed by the drain: the turn exists, its agent has not started.
|
|
return 'starting' if any(row['status'] == 'queued' for row in pending) else 'idle'
|
|
|
|
|
|
async def active_list(connection, ref, params):
|
|
if set(params) - {'profile', 'current_session_id'}:
|
|
raise RuntimeStoreError('invalid_params')
|
|
if 'session:read' not in connection.actor.capabilities:
|
|
raise RuntimeStoreError('permission_denied')
|
|
from gateway.session_live_listing import live_entries
|
|
from hermes_state_sessions import INTERNAL_LISTING_SOURCES
|
|
current = str(params.get('current_session_id') or '')
|
|
rows = []
|
|
for entry in await live_entries(connection.authority, connection.actor):
|
|
row = entry.row
|
|
# The same population as the live ``session.list`` feed, hidden chats included: a hidden
|
|
# chat open in a pane still needs its turn's liveness.
|
|
if row.get('source') in INTERNAL_LISTING_SOURCES or entry.closed:
|
|
continue
|
|
sid, started = entry.session_id, float(row.get('started_at') or 0)
|
|
rows.append({'current': sid == current, 'id': sid, 'session_key': sid,
|
|
'last_active': float(row.get('ended_at') or started), 'started_at': started,
|
|
'message_count': int(row.get('message_count') or 0),
|
|
'model': str(row.get('model') or ''), 'preview': '', 'title': row.get('title') or '',
|
|
'source': row.get('source'), 'status': _status(entry.live, entry.handle, entry.pending)})
|
|
return {'sessions': rows}
|