fix(copilot): wrap write_transcript_to_tempfile with asyncio.to_thread

The function performs synchronous file I/O (open/write) which blocks the
event loop when called from async context. Both call sites in
_reduce_context and stream_chat_completion_sdk now use asyncio.to_thread.
This commit is contained in:
Zamil Majdy
2026-03-16 06:18:20 +07:00
parent bd23caa116
commit f4dee98508

View File

@@ -208,7 +208,9 @@ async def _reduce_context(
logger.info("%s Using compacted transcript for retry", log_prefix)
tb = TranscriptBuilder()
tb.load_previous(compacted, log_prefix=log_prefix)
resume_file = write_transcript_to_tempfile(compacted, session_id, sdk_cwd)
resume_file = await asyncio.to_thread(
write_transcript_to_tempfile, compacted, session_id, sdk_cwd
)
if resume_file:
return ReducedContext(tb, True, resume_file, False, True)
logger.warning("%s Failed to write compacted transcript", log_prefix)
@@ -1371,8 +1373,8 @@ async def stream_chat_completion_sdk(
# Load previous FULL context into builder
transcript_content = dl.content
transcript_builder.load_previous(dl.content, log_prefix=log_prefix)
resume_file = write_transcript_to_tempfile(
dl.content, session_id, sdk_cwd
resume_file = await asyncio.to_thread(
write_transcript_to_tempfile, dl.content, session_id, sdk_cwd
)
if resume_file:
use_resume = True