Fix async injection tool-result ordering

This commit is contained in:
Hypatia May
2026-07-15 11:12:05 +08:00
parent 34bb75b2d2
commit 4ce32807e1
10 changed files with 104 additions and 55 deletions

View File

@@ -85,6 +85,8 @@ export interface LoopHandle {
clearCancel(): void
/** Settle idle waiters when pre-running cancellation skips a turn, without emitting `agent/status`. */
settleIdle(): void
/** Append context that arrived while an assistant tool-call batch was awaiting its results. */
readonly drainDeferredInjections: () => void
}
/**
@@ -349,6 +351,10 @@ async function runTurn(
break
}
// A successful tool step has committed its complete result batch. Context
// accepted while that batch was pending can now join the open turn.
if (stepOutcome.hadToolCalls) handle.drainDeferredInjections()
// Preserve max-token completion unless a later disposal, abort, or error wins.
const stepReason = stepFinishReason(stepOutcome.finish)
if (stepReason) reason = stepReason