fix: 修复部分溢出的问题

This commit is contained in:
claude-code-best
2026-04-17 15:14:59 +08:00
parent a3fa374bb3
commit d60e0eaccb
3 changed files with 20 additions and 2 deletions

View File

@@ -330,6 +330,11 @@ async function* queryLoop(
// sites.
let taskBudgetRemaining: number | undefined = undefined
// Guard against stop_hook_blocking infinite loops (see ~line 1326).
// Loop-local to avoid touching the 7 continue sites on State.
const MAX_STOP_HOOK_BLOCKING_RETRIES = 3
let stopHookBlockingCount = 0
// Snapshot immutable env/statsig/session state once at entry. See QueryConfig
// for what's included and why feature() gates are intentionally excluded.
const config = buildQueryConfig()
@@ -1324,6 +1329,13 @@ async function* queryLoop(
}
if (stopHookResult.blockingErrors.length > 0) {
stopHookBlockingCount++
if (stopHookBlockingCount > MAX_STOP_HOOK_BLOCKING_RETRIES) {
yield createAssistantAPIErrorMessage({
content: `Stop hook blocked ${stopHookBlockingCount} times — stopping to prevent infinite loop.`,
})
return { reason: 'completed' }
}
const next: State = {
messages: [
...messagesForQuery,

View File

@@ -505,6 +505,7 @@ export class AcpAgent implements Agent {
includePartialMessages: true,
replayUserMessages: true,
initialMessages: opts.initialMessages,
maxTurns: 200,
}
const queryEngine = new QueryEngine(engineConfig)

View File

@@ -573,6 +573,7 @@ export async function forwardSessionUpdates(
// Race the next message against the abort signal so we unblock
// immediately when cancelled, even if the generator is waiting for
// a slow API response.
let abortHandler: (() => void) | undefined
const nextResult = await Promise.race([
sdkMessages.next(),
new Promise<IteratorResult<SDKMessage, void>>((resolve) => {
@@ -580,10 +581,14 @@ export async function forwardSessionUpdates(
resolve({ done: true, value: undefined })
return
}
const handler = () => resolve({ done: true, value: undefined })
abortSignal.addEventListener('abort', handler, { once: true })
abortHandler = () => resolve({ done: true, value: undefined })
abortSignal.addEventListener('abort', abortHandler, { once: true })
}),
])
// Clean up: remove un-fired listener when generator completes first
if (abortHandler) {
abortSignal.removeEventListener('abort', abortHandler)
}
if (nextResult.done || abortSignal.aborted) break
const msg = nextResult.value