TranscriptMirrorBatcher: cancellation during store.append() leaves the write outcome unknown and never calls on_error
Title: TranscriptMirrorBatcher: cancellation during store.append() leaves the write outcome unknown and never calls on_error
Hi, and thank you for the SessionStore mirroring feature!
Thanks also to @sylvesterkaczmarek for PR #1289 and to @sigley and @MohammediYunus for reviewing it. #1289 covers cancellation while a flush waits for the lock. This issue is about the case that PR leaves out: cancellation while store.append() is already running.
What happens The batch is detached from the pending buffer in _drain() before the append. If the flush is cancelled inside "await self.store.append(key, entries)", the cancellation (a BaseException) skips the "except Exception" paths in _do_flush(). The batch is not retried and on_error is not called. A later flush() or close() finds nothing pending. The adapter may or may not have written the batch before the cancel, and the caller gets no signal either way.
Reproduction Reproduced on commit cd08fa0 (also the current main on 2026-11-18), Python 3.13, anyio 4.15.1, asyncio. The cancellation is an explicit outer cancel scope, not the internal send_timeout. file_path_to_session_key is patched only to keep the script short.
import anyio import claude_agent_sdk._internal.transcript_mirror_batcher as m
m.file_path_to_session_key = lambda fp, pd: {"project_key": "p", "session_id": "s"}
class Store: def init(self, write_first): self.got, self.calls, self.write_first = [], 0, write_first self.entered, self.gate = anyio.Event(), anyio.Event() async def append(self, key, entries): self.calls += 1 if self.calls == 1: if self.write_first: self.got.extend(e["uuid"] for e in entries) self.entered.set() await self.gate.wait() # outer cancellation arrives here return self.got.extend(e["uuid"] for e in entries)
async def run(write_first): errs = [] async def on_err(key, msg): errs.append(msg) st = Store(write_first) b = m.TranscriptMirrorBatcher(store=st, projects_dir="/x", on_error=on_err) b.enqueue("/x/f.jsonl", [{"uuid": "A"}]) async with anyio.create_task_group() as tg: tg.start_soon(b.flush) await st.entered.wait() tg.cancel_scope.cancel() await b.close() print(f"adapter wrote before cancel={write_first} | store={st.got} | " f"append calls={st.calls} | on_error={errs}")
anyio.run(run, False) anyio.run(run, True)
Output adapter wrote before cancel=False | store=[] | append calls=1 | on_error=[] adapter wrote before cancel=True | store=['A'] | append calls=1 | on_error=[]
So when the adapter had not written yet, the mirror is missing entry A and nothing reports it. When it had written, A is there and nothing is duplicated. From the caller's side the two cases look the same.
Scope This reproduces the mirror behavior in isolation. It does not exercise local transcript persistence or a live CLI session.
Why not just re-queue Re-queueing the whole batch could create duplicates in the second case, which is probably also why a timeout is not retried.
Suggestion When an in-flight SessionStore.append() is cancelled, report that the write outcome is unknown rather than retrying automatically. Could the batcher make a best-effort on_error call for the affected key after releasing the flush lock, with a short shielded deadline, and then re-raise the original cancellation? If the callback cannot be delivered during teardown, a log entry would still make the unknown outcome visible. It would also help to document that a later flush() or close() does not retry that batch.
Happy to prepare a PR with a regression test if this direction works for you. Thanks for taking a look!
Thanks for the careful reproduction, and for linking this to #1289. You are right about the scope: that PR preserves batches when cancellation happens while waiting for the flush lock, but it does not resolve cancellation after store.append() starts. At that point the write may already have committed, so automatically re-queueing could duplicate entries. A bounded, shielded best-effort on_error notification with a log fallback sounds like a sensible direction, subject to the maintainers choosing the cancellation/reporting contract. Happy to review a focused regression covering both write-before-cancel and no-write cases.