fix(backends): remember the session id from the first frame so a cut turn stays resumable

This commit is contained in:
hh
2026-08-28 19:38:47 +02:00
parent 609af9fe67
commit 61562e947d
2 changed files with 24 additions and 7 deletions
+15 -3
View File
@@ -325,7 +325,9 @@ class ClaudeSdkBackend:
try:
# Events go out as the CLI produces them: the frontends
# stream text and thinking live, the turn is not buffered.
async for event in self._run_turn(live, prompt, turn, observer):
async for event in self._run_turn(
live, prompt, turn, observer, capture
):
yield event
except Exception:
live.running_turn = None
@@ -343,7 +345,9 @@ class ClaudeSdkBackend:
turn = _Turn()
async with live.lock:
live.running_turn = turn_id or message_id
async for event in self._run_turn(live, prompt, turn, observer):
async for event in self._run_turn(
live, prompt, turn, observer, capture
):
yield event
live.turns += 1
live.last_used = time.monotonic()
@@ -403,12 +407,15 @@ class ClaudeSdkBackend:
prompt: str,
turn: _Turn,
observer: Callable[[Any], None] | None = None,
capture: TurnCapture | None = None,
) -> AsyncIterator[MessageStreamEvent]:
"""Run one prompt, yielding wire events as they arrive.
``turn`` is filled in place (result, synthesized history, count of
events already yielded) so the caller can finish bookkeeping - and
decide whether a retry is still possible - after a failure.
decide whether a retry is still possible - after a failure. The
session id lands in ``capture`` with the first frame, so a turn cut
by a restart still leaves a resumable session behind.
"""
streaming = self._agent.options.include_partial_messages
raw: list[Any] = []
@@ -418,6 +425,11 @@ class ClaudeSdkBackend:
async for message in live.client.receive_response():
if observer is not None:
observer(message)
session_id = getattr(message, "session_id", None)
if isinstance(session_id, str) and session_id and live.session_id is None:
live.session_id = session_id
if capture is not None:
capture.session_id = session_id
if isinstance(message, MirrorErrorMessage):
live.dirty = True
_log.error(