@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
75 lines • 3.54 kB
JavaScript
import { isProviderTimeoutError } from "@earendil-works/pi-ai/compat";
/**
* Raise the retry-continuation liveness cap to the stream-start budget granted to
* the same retry, so the watchdog can never cancel an attempt on a deadline the
* attempt was never given.
*
* An explicitly disabled cap (`undefined`) stays disabled: the operator opted out
* of the wedge guard, and the provider guards still bound the request. A cap that
* already outlasts the granted guard is returned unchanged, so an operator who
* configured a longer cap keeps it.
*/
function reconcileWatchdogTimeoutMs(streamRetryTimeoutMs, streamStartTimeoutMs) {
if (streamRetryTimeoutMs === undefined || streamRetryTimeoutMs <= 0)
return undefined;
if (streamStartTimeoutMs === undefined)
return streamRetryTimeoutMs;
// This timer starts before the retry reaches its first reader.next(). A 10%
// proportional grace therefore makes the provider guard strictly win without
// imposing a magic absolute delay on short or long operator-configured guards.
return Math.max(streamRetryTimeoutMs, Math.ceil(streamStartTimeoutMs * 1.1));
}
export function createProviderTimeoutRetryPlan({ message, streamRetryTimeoutMs, timeoutMs, streamStartTimeoutMs, }) {
if (!isProviderTimeoutError(message)) {
return { options: {}, watchdogTimeoutMs: undefined };
}
// The retry keeps the user's configured provider guards: shortening them
// made a 90s stream-start budget expire after 30s, so a slow-but-alive
// provider was reported as a second stall instead of being given the budget
// it was configured with. `streamRetryTimeoutMs` still bounds the retry
// continuation itself (see `runBoundedRetryContinuation`), which cancels a
// wedged retry without lying to the provider about its deadline.
//
// The cap is reconciled against the guards this same retry was granted. The
// grace above is required because this continuation starts its clock earlier
// than the provider stream-start reader.
return {
options: {
deferQueuedMessages: true,
...(timeoutMs === undefined ? {} : { timeoutMs }),
...(streamStartTimeoutMs === undefined ? {} : { streamStartTimeoutMs }),
},
watchdogTimeoutMs: reconcileWatchdogTimeoutMs(streamRetryTimeoutMs, streamStartTimeoutMs),
};
}
export async function runBoundedRetryContinuation({ continueRun, getActiveSignal, abortActive, timeoutMs, onStreamStarted, }) {
// The watchdog bounds only the wait for the retried request to start streaming: a retry that streams
// and then works for a long time (a long answer, tool calls, further turns) is not a wedged retry.
let streamStarted = false;
let timer;
const unsubscribe = timeoutMs === undefined
? undefined
: onStreamStarted?.(() => {
streamStarted = true;
clearTimeout(timer);
});
try {
const continuation = continueRun();
const ownedSignal = getActiveSignal();
if (timeoutMs === undefined || ownedSignal === undefined || streamStarted) {
await continuation;
return;
}
timer = setTimeout(() => {
if (!streamStarted && getActiveSignal() === ownedSignal) {
abortActive();
}
}, timeoutMs);
await continuation;
}
finally {
clearTimeout(timer);
unsubscribe?.();
}
}
//# sourceMappingURL=provider-timeout-retry.js.map