fix: shows live sessions for pty view
This commit is contained in:
@@ -136,6 +136,7 @@ class ClaudeCodeBackend:
|
||||
self._opts = options
|
||||
self._on_parse_error = on_parse_error
|
||||
self._sessions: dict[str, _LiveSession] = {}
|
||||
self._active: dict[str, _LiveSession] = {}
|
||||
self._mcp_config_path: Path | None = None
|
||||
self._session_factory = _session_factory
|
||||
self._closed = False
|
||||
@@ -158,8 +159,15 @@ class ClaudeCodeBackend:
|
||||
``_sessions`` (which is keyed by history fingerprint, not
|
||||
``session_id``). Intended for debug surfaces (e.g. admin
|
||||
terminal viewer) that need to look up a session by id.
|
||||
|
||||
Covers both idle pooled sessions (``_sessions``) and the turn(s)
|
||||
currently running (``_active``) — so a live terminal is visible
|
||||
*during* its turn, not only after it's repooled. ``_active`` wins
|
||||
on key collision, but a session is never in both at once.
|
||||
"""
|
||||
return {s.session_id: s.pty for s in self._sessions.values()}
|
||||
out = {s.session_id: s.pty for s in self._sessions.values()}
|
||||
out.update({sid: s.pty for sid, s in self._active.items()})
|
||||
return out
|
||||
|
||||
async def complete(self, messages: list[Mapping[str, Any]]) -> AsyncIterator[Event]:
|
||||
"""Run one turn against the matching session (or spawn one).
|
||||
@@ -223,6 +231,7 @@ class ClaudeCodeBackend:
|
||||
n_assistant = 0
|
||||
n_user = 0
|
||||
n_system = 0
|
||||
self._active[session.session_id] = session
|
||||
try:
|
||||
_log.info(
|
||||
"complete: sending user msg to session_id=%s (text_len=%d)",
|
||||
@@ -250,6 +259,8 @@ class ClaudeCodeBackend:
|
||||
with contextlib.suppress(Exception):
|
||||
await session.aclose()
|
||||
raise
|
||||
finally:
|
||||
self._active.pop(session.session_id, None)
|
||||
|
||||
synthesized_cycle = synthesize_turn_messages(events)
|
||||
new_history = [*list(messages), *synthesized_cycle]
|
||||
|
||||
Reference in New Issue
Block a user