pi-ext bugabingas pi extensions
owned by bugabinga
README Source History Refs Compare Notes Search fix(goal): retry transient provider errors
20576daef2cee2d8a26ecdad75c740817e319917
Oliver Jan Krylow <oliver@bugabinga.net > · 11 Jun 2026
unsigned
Browse files Compare with HEAD
Changed files (5) 6 + - Classify goal extension API errors into kinds (context_overflow, transport,
7 + server, rate_limit, stream, parse, auth, quota, bad_request, unknown) with
8 + human-readable short/detail/hint and retryability, and retry retryable errors
9 + indefinitely with exponential backoff capped at 60s instead of pausing after a
11 + - Auto-compact and retry on `context_length_exceeded` via `ctx.compact`, pausing
12 + only if compaction itself fails
13 + - Pause the goal loop only on terminal errors: auth (401/403), quota
14 + (insufficient credit/billing), bad_request (400), and `stopReason === "aborted"`
15 + - Derive user-facing `lastReason` and notifications from the classifier instead
16 + of raw API JSON, while keeping raw errors in `goal-event` entries for debugging
6 17 - Bump TypeScript dev dependency to 6.0.3
7 18 - Render TLDR widget as icon-prefixed padded lines instead of a boxed panel
8 19 - Render goal widget as compact/expanded icon-prefixed lines that wrap instead
extensions/goal/README.md Modified 148 148 Complete, blocked, paused, and repeated judge-failed verdicts notify.
149 149 - Done verdict marks the goal complete and clears persistent
150 150 status/widget/footer.
151 - - Assistant/provider errors retry regardless of error text, then pause after the
151 + - Assistant/provider transport, server, rate-limit, stream, parse, unknown, and
152 + context-overflow errors retry instead of pausing.
153 + Context overflow triggers compaction before retry.
154 + Auth, quota, bad-request, and user-cancel errors pause.
153 155 - Judge errors retry the goal loop, then pause after repeated failures.
154 156 - Blocked verdict must repeat the same blocker for 3 goal turns before the goal
155 157 is marked blocked.
extensions/goal/__tests__/harness.test.ts Modified 578 - it (
"retries assistant errors before pausing" ,
async ()
=> {
578 + it (
"retries assistant errors without pausing" ,
async ()
=> {
579 579 t
= await createTestSession ({ extensions: [
goalPath ()] });
580 580 installSaysErrorStream (t,
"provider crashed" );
582 582 await t.session.
prompt (
"/goal survive provider error" );
583 - await waitFor (()
=> latestGoal ()?.lastReason?.
startsWith (
"retry 1/3" ));
583 + await waitFor (()
=> latestGoal ()?.lastReason?.
startsWith (
"retry 1" ));
585 585 expect (
latestGoal ()).
toMatchObject ({
586 586 objective:
"survive provider error" ,
587 587 status:
"active" ,
588 588 lastVerdict:
"error" ,
589 - lastReason:
"retry 1/3: provider crashed" ,
589 + lastReason:
"retry 1: server error" ,
591 591 expect (
customEvents (
"goal-judge" )).
toEqual ([]);
594 + it (
"retries WebSocket 1006 transport errors instead of pausing" ,
async ()
=> {
595 + t
= await createTestSession ({ extensions: [
goalPath ()] });
596 + installSaysErrorStream (t,
"WebSocket closed 1006 Connection ended" );
598 + await t.session.
prompt (
"/goal survive websocket drop" );
599 + await waitFor (()
=> latestGoal ()?.lastReason?.
startsWith (
"retry 1" ));
601 + expect (
latestGoal ()).
toMatchObject ({
602 + objective:
"survive websocket drop" ,
604 + lastVerdict:
"error" ,
606 + expect (
latestGoal ()?.lastReason).
toContain (
"transport error" );
609 + it (
"pauses only on auth errors with human-readable reason" ,
async ()
=> {
610 + t
= await createTestSession ({ extensions: [
goalPath ()] });
611 + installSaysErrorStream (t,
"401 Unauthorized" );
613 + await t.session.
prompt (
"/goal locked out" );
614 + await waitFor (()
=> latestGoal ()?.
status === "paused" ,
2_000 );
616 + expect (
latestGoal ()).
toMatchObject ({
617 + objective:
"locked out" ,
619 + lastVerdict:
"error" ,
621 + const reason
= latestGoal ()?.lastReason
?? "" ;
622 + expect (reason).
toContain (
"auth failed" );
623 + expect (reason).
toContain (
"check API key" );
624 + expect (reason).not.
toContain (
"401 Unauthorized" );
594 627 it (
"pauses after repeated judge parse failures" ,
async ()
=> {
595 628 const restore
= installJudgeProvider ([
"" ,
"" ,
"" ,
"" ,
"" ,
"" ]);
extensions/goal/__tests__/unit.test.ts Modified 1464 1464 __goalTest.
recoveredAgentErrorPatch (
1466 1466 lastVerdict:
"error" ,
1467 - lastReason:
"retry 1/3: provider crashed" ,
1467 + lastReason:
"retry 1: server error" ,
1469 1469 { stopReason:
"stop" }
as never ,
1471 - ).
toEqual ({ lastVerdict:
undefined , lastReason:
undefined });
1472 + lastVerdict:
undefined ,
1473 + lastReason:
undefined ,
1474 1478 it (
"keeps retry error while the next agent turn also errors" , ()
=> {
1476 1480 __goalTest.
recoveredAgentErrorPatch (
1478 1482 lastVerdict:
"error" ,
1479 - lastReason:
"retry 1/3: provider crashed" ,
1483 + lastReason:
"retry 1: server error" ,
1481 1485 { stopReason:
"error" }
as never ,
1509 1513 expect (__goalTest.
isRetryableError (
"502 Bad Gateway" )).
toBe (
true );
1510 1514 expect (__goalTest.
isRetryableError (
"503 Service Unavailable" )).
toBe (
true );
1512 - it (
"returns false for deterministic parse and request errors" , ()
=> {
1516 + it (
"retries parse errors (transient model glitches)" , ()
=> {
1514 1518 __goalTest.
isRetryableError (
"JSON Parse error: Unrecognized token '@'" ),
1516 - expect (__goalTest.
isRetryableError (
"Unexpected token '@'" )).
toBe (
false );
1517 - expect (__goalTest.
isRetryableError (
"401 Unauthorized" )).
toBe (
false );
1518 - expect (__goalTest.
isRetryableError (
"400 Bad Request" )).
toBe (
false );
1520 + expect (__goalTest.
isRetryableError (
"Unexpected token '@'" )).
toBe (
true );
1520 - it (
"returns false for normal messages" , ()
=> {
1521 - expect (__goalTest.
isRetryableError (
"success" )).
toBe (
false );
1522 - expect (__goalTest.
isRetryableError (
"" )).
toBe (
false );
1522 + it (
"returns false for auth/quota/bad-request errors" , ()
=> {
1523 + expect (__goalTest.
isRetryableError (
"401 Unauthorized" )).
toBe (
false );
1524 + expect (__goalTest.
isRetryableError (
"403 Forbidden" )).
toBe (
false );
1525 + expect (__goalTest.
isRetryableError (
"400 Bad Request" )).
toBe (
false );
1526 + expect (__goalTest.
isRetryableError (
"quota exceeded" )).
toBe (
false );
1527 + expect (__goalTest.
isRetryableError (
"insufficient_quota" )).
toBe (
false );
1529 + it (
"retries websocket close 1006" , ()
=> {
1531 + __goalTest.
isRetryableError (
"WebSocket closed 1006 Connection ended" ),
1534 + it (
"retries context overflow" , ()
=> {
1536 + __goalTest.
isRetryableError (
1537 + 'Codex error: {"type":"error","error":{"code":"context_length_exceeded"}}' ,
1541 + it (
"defaults to retryable for unknown messages" , ()
=> {
1542 + expect (__goalTest.
isRetryableError (
undefined )).
toBe (
true );
1543 + expect (__goalTest.
isRetryableError (
"" )).
toBe (
true );
1544 + expect (__goalTest.
isRetryableError (
"something novel happened" )).
toBe (
1550 + describe (
"classifyError" , ()
=> {
1551 + it (
"extracts kind from Codex-style context overflow" , ()
=> {
1552 + const c
= __goalTest.
classifyError (
1553 + 'Codex error: {"type":"error","error":{"type":"invalid_request_error","code":"context_length_exceeded","message":"Your input exceeds the context window of this model."}}' ,
1555 + expect (c.kind).
toBe (
"context_overflow" );
1556 + expect (c.retryable).
toBe (
true );
1557 + expect (c.short).
toBe (
"context overflow" );
1559 + it (
"classifies transport (WebSocket 1006)" , ()
=> {
1560 + const c
= __goalTest.
classifyError (
1561 + "WebSocket closed 1006 Connection ended" ,
1563 + expect (c.kind).
toBe (
"transport" );
1564 + expect (c.retryable).
toBe (
true );
1566 + it (
"classifies auth" , ()
=> {
1567 + const c
= __goalTest.
classifyError (
"401 Unauthorized" );
1568 + expect (c.kind).
toBe (
"auth" );
1569 + expect (c.retryable).
toBe (
false );
1571 + it (
"classifies quota" , ()
=> {
1572 + const c
= __goalTest.
classifyError (
"insufficient_quota" );
1573 + expect (c.kind).
toBe (
"quota" );
1574 + expect (c.retryable).
toBe (
false );
1576 + it (
"classifies rate limit" , ()
=> {
1577 + const c
= __goalTest.
classifyError (
"429 Rate limit reached" );
1578 + expect (c.kind).
toBe (
"rate_limit" );
1579 + expect (c.retryable).
toBe (
true );
1581 + it (
"classifies server (crashed / 5xx)" , ()
=> {
1582 + const c
= __goalTest.
classifyError (
"provider crashed" );
1583 + expect (c.kind).
toBe (
"server" );
1584 + expect (c.retryable).
toBe (
true );
1585 + const c2
= __goalTest.
classifyError (
"502 Bad Gateway" );
1586 + expect (c2.kind).
toBe (
"server" );
1588 + it (
"classifies parse" , ()
=> {
1589 + const c
= __goalTest.
classifyError (
1590 + "JSON Parse error: Unrecognized token '@'" ,
1592 + expect (c.kind).
toBe (
"parse" );
1593 + expect (c.retryable).
toBe (
true );
1595 + it (
"classifies bad_request" , ()
=> {
1596 + const c
= __goalTest.
classifyError (
"400 Bad Request" );
1597 + expect (c.kind).
toBe (
"bad_request" );
1598 + expect (c.retryable).
toBe (
false );
1600 + it (
"falls back to unknown retryable" , ()
=> {
1601 + const c
= __goalTest.
classifyError (
"weird new error" );
1602 + expect (c.kind).
toBe (
"unknown" );
1603 + expect (c.retryable).
toBe (
true );
1605 + it (
"handles undefined" , ()
=> {
1606 + const c
= __goalTest.
classifyError (
undefined );
1607 + expect (c.kind).
toBe (
"unknown" );
1608 + expect (c.retryable).
toBe (
true );
extensions/goal/index.ts Modified 191 191 const JUDGE_ATTEMPTS
= 2 ;
192 192 const JUDGE_PARSE_FAILURE_LIMIT
= 3 ;
193 193 const BLOCKED_STREAK_LIMIT
= 3 ;
194 - const MAX_ERROR_RETRIES
= 3 ;
194 + const ERROR_BACKOFF_BASE_MS
= 2000 ;
195 + const ERROR_BACKOFF_MAX_MS
= 60_000 ;
195 196 const ANSI_RE
= /\x1b\[[0-?] * [ -/] * [@-~]/ g ;
196 197 const CONTROL_RE
= /[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/ g ;
197 198 const GOAL_WIDGET_LABEL_WIDTH
= 11 ;
768 - function isRetryableError (message
: string )
: boolean {
769 - const text
= message.
toLowerCase ();
770 - if (
! text.
trim ())
return false ;
772 - /json parse | unexpected token | unrecognized token | syntaxerror | bad request | unauthorized | forbidden | auth | quota | cancel | aborted/ .
test (
777 - return /429 | rate | concurrency | timeout | timedout | econnreset | econnrefused | fetch failed | network | 502 | 503 | 504 | bad gateway | service unavailable | gateway timeout | crashed/ .
test (
770 + | "context_overflow" 781 + type ClassifiedError = {
786 + retryable
: boolean ;
789 + const ERROR_PATTERNS
: ReadonlyArray <{
795 + retryable
: boolean ;
799 + test:
/ \b (401 | 403 | unauthorized | forbidden | invalid[_ ] ? api[_ ] ? key | auth(?:entication) ? \s + (?:failed | required | error)) \b / i ,
800 + short:
"auth failed" ,
801 + detail:
"API rejected credentials" ,
802 + hint:
"check API key" ,
807 + test:
/ \b (quota | insufficient[_ ] ? (?:quota | credit) | billing | payment[_ ] ? required | exceeded\s + your\s + current\s + quota) \b / i ,
808 + short:
"quota exhausted" ,
809 + detail:
"API quota or billing limit reached" ,
810 + hint:
"top up or wait" ,
814 + kind:
"context_overflow" ,
815 + test:
/ \b (context[_ ] ? (?:length | window | size)[_ ] ? (?:exceeded | limit | too[_ ] ? long) | input[_ ] ? (?:too[_ ] ? long | exceed) | maximum[_ ] ? context | prompt[_ ] ? is[_ ] ? too[_ ] ? long) \b / i ,
816 + short:
"context overflow" ,
817 + detail:
"input exceeds model context window" ,
818 + hint:
"compacting" ,
822 + kind:
"context_overflow" ,
823 + test:
/"code"\s * :\s * "context_length_exceeded"/ i ,
824 + short:
"context overflow" ,
825 + detail:
"input exceeds model context window" ,
826 + hint:
"compacting" ,
830 + kind:
"rate_limit" ,
831 + test:
/ \b (429 | rate[_ ] ? limit | too[_ ] ? many[_ ] ? requests | concurrency | throttl) \b / i ,
832 + short:
"rate limited" ,
833 + detail:
"API rate limit hit" ,
834 + hint:
"backing off" ,
839 + test:
/ \b (websocket | ws[_ ] ? closed | connection[_ ] ? (?:ended | closed | reset | refused | aborted | dropped | terminated | failed) | econn(reset | refused) | etimedout | enetunreach | ehostunreach | fetch[_ ] ? failed | network[_ ] ? (?:error | failure | unreachable) | socket[_ ] ? (?:hang[_ ] ? up | closed) | dns[_ ] ? lookup[_ ] ? failed) \b / i ,
840 + short:
"transport error" ,
841 + detail:
"network or connection failure" ,
842 + hint:
"reconnecting" ,
847 + test:
/ \b (1006 | abnormal[_ ] ? closure | gone[_ ] ? away) \b / i ,
848 + short:
"connection dropped" ,
849 + detail:
"WebSocket or TCP connection lost" ,
850 + hint:
"reconnecting" ,
855 + test:
/ \b (500 | 502 | 503 | 504 | bad[_ ] ? gateway | service[_ ] ? unavailable | gateway[_ ] ? timeout | server[_ ] ? error | overloaded | crashed | upstream[_ ] ? (?:error | failure)) \b / i ,
856 + short:
"server error" ,
857 + detail:
"upstream server failed" ,
863 + test:
/ \b (stream[_ ] ? (?:error | closed | aborted | reset | timeout) | sse[_ ] ? error | chunked[_ ] ? encoding | unexpected[_ ] ? eof) \b / i ,
864 + short:
"stream error" ,
865 + detail:
"response stream interrupted" ,
871 + test:
/ \b (json[_ ] ? parse | unexpected[_ ] ? token | unrecognized[_ ] ? token | syntax[_ ] ? error | invalid[_ ] ? json | malformed[_ ] ? (?:json | response)) \b / i ,
872 + short:
"parse error" ,
873 + detail:
"could not parse model response" ,
878 + kind:
"bad_request" ,
879 + test:
/ \b (400 | bad[_ ] ? request | invalid[_ ] ? request[_ ] ? error) \b / i ,
880 + short:
"bad request" ,
881 + detail:
"API rejected request shape" ,
886 + function classifyError (raw
: string | undefined )
: ClassifiedError {
887 + const text
= (raw
?? "" ).
trim ();
891 + short:
"unknown error" ,
892 + detail:
"no error message" ,
896 + for (
const pattern
of ERROR_PATTERNS) {
897 + if (pattern.test.
test (text)) {
899 + kind: pattern.kind,
900 + short: pattern.short,
901 + detail: pattern.detail,
902 + hint: pattern.hint,
903 + retryable: pattern.retryable,
909 + short:
"unknown error" ,
910 + detail:
shortText (
cleanGoalText (text),
80 ),
915 + function isRetryableError (message
: string | undefined )
: boolean {
916 + return classifyError (message).retryable;
919 + function errorBackoffMs (retries
: number )
: number {
921 + ERROR_BACKOFF_MAX_MS,
922 + ERROR_BACKOFF_BASE_MS
* 2 ** Math .
min (retries,
10 ),
926 + function errorRetryLabel (retries
: number , classified
: ClassifiedError )
: string {
927 + return `retry ${retries}: ${classified.short}` ;
930 + function errorPauseLabel (classified
: ClassifiedError )
: string {
931 + return `${classified.short}: ${classified.detail}.${classified.hint ? ` ${classified.hint}.` : ""}` ;
782 934 function recoveredAgentErrorPatch (
783 935 state
: GoalState ,
784 936 evidence
: TurnEvidence | undefined ,
787 939 if (
! state.lastReason?.
startsWith (
"retry " ))
return undefined ;
788 940 if (evidence?.stopReason
=== "aborted" || evidence?.stopReason
=== "error" )
789 941 return undefined ;
790 - return { lastVerdict:
undefined , lastReason:
undefined };
942 + return { lastVerdict:
undefined , lastReason:
undefined , errorRetries:
0 };
793 945 function reasonHash (reason
: string )
: string {
2593 - evidence?.stopReason
=== "aborted" || 2594 - evidence?.stopReason
=== "error" 2596 - const errorMessage
= 2597 - evidence.errorMessage
|| `assistant ${evidence.stopReason}` ;
2744 + if (evidence?.stopReason
=== "aborted" ) {
2745 + state
= updateGoal (pi, state, {
2747 + lastReason:
"cancelled by user" ,
2748 + timeUsedSeconds:
currentElapsedSeconds (state),
2750 + appendGoalEvent (pi, ctx,
"paused" , state, {
2751 + reason: state.lastReason,
2753 + renderGoalUi (ctx, state);
2754 + syncUpdateGoalTool ();
2755 + if (ctx.hasUI) ctx.ui.
notify (
"Goal cancelled" ,
"warning" );
2758 + if (evidence?.stopReason
=== "error" ) {
2759 + const errorMessage
= evidence.errorMessage
|| "assistant error" ;
2760 + const classified
= classifyError (errorMessage);
2598 2761 const retries
= (state.errorRetries
?? 0 )
+ 1 ;
2599 - if (
isRetryableError (errorMessage)
&& retries
<= MAX_ERROR_RETRIES) {
2763 + if (classified.kind
=== "context_overflow" ) {
2600 2764 state
= updateGoal (pi, state, {
2601 2765 errorRetries: retries,
2602 2766 lastVerdict:
"error" ,
2603 - lastReason:
`retry ${retries}/${MAX_ERROR_RETRIES}: ${errorMessage}` ,
2767 + lastReason:
errorRetryLabel (retries, classified),
2604 2768 timeUsedSeconds:
currentElapsedSeconds (state),
2606 - appendGoalEvent (pi, ctx,
"error-retry" , state, {
2770 + appendGoalEvent (pi, ctx,
"context-overflow" , state, {
2607 2771 reason: errorMessage,
2611 2775 syncUpdateGoalTool ();
2614 - `Goal retry ${retries}/${MAX_ERROR_RETRIES} — ${errorMessage}` ,
2778 + `Goal: ${classified.short} — compacting then retrying` ,
2781 + const compactState
= state;
2782 + const compactCtx
= ctx;
2784 + compactCtx.
compact ({
2785 + onComplete : ()
=> {
2786 + let latest
: GoalState | undefined ;
2788 + latest
= latestGoalState (compactCtx)
?? compactState;
2790 + if (
! isStaleContextError (error))
throw error;
2793 + if (
! latest
|| latest.
status !== "active" )
return ;
2794 + if (
! ownsGoal (compactCtx, latest))
return ;
2795 + state
= updateGoal (pi, latest, { errorRetries:
0 });
2796 + renderGoalUi (compactCtx, state);
2797 + syncUpdateGoalTool ();
2798 + scheduleContinuation (pi, compactCtx, state, continuation);
2800 + onError : (err)
=> {
2802 + err
instanceof Error && err.message
? err.message
: String (err);
2803 + let latest
: GoalState | undefined ;
2805 + latest
= latestGoalState (compactCtx)
?? compactState;
2807 + if (
! isStaleContextError (error))
throw error;
2810 + if (
! latest)
return ;
2811 + state
= updateGoal (pi, latest, {
2813 + lastVerdict:
"error" ,
2814 + lastReason:
`${classified.short} unrecoverable: ${msg}` ,
2815 + timeUsedSeconds:
currentElapsedSeconds (latest),
2817 + appendGoalEvent (pi, compactCtx,
"paused" , state, {
2818 + reason: state.lastReason,
2820 + renderGoalUi (compactCtx, state);
2821 + syncUpdateGoalTool ();
2822 + if (compactCtx.hasUI)
2823 + compactCtx.ui.
notify (
2824 + `Goal paused — ${state.lastReason}` ,
2831 + err
instanceof Error && err.message
? err.message
: String (err);
2832 + state
= updateGoal (pi, compactState, {
2834 + lastVerdict:
"error" ,
2835 + lastReason:
`${classified.short} unrecoverable: ${msg}` ,
2836 + timeUsedSeconds:
currentElapsedSeconds (compactState),
2838 + appendGoalEvent (pi, ctx,
"paused" , state, {
2839 + reason: state.lastReason,
2841 + renderGoalUi (ctx, state);
2842 + syncUpdateGoalTool ();
2844 + ctx.ui.
notify (
`Goal paused — ${state.lastReason}` ,
"warning" );
2849 + if (classified.retryable) {
2850 + state
= updateGoal (pi, state, {
2851 + errorRetries: retries,
2852 + lastVerdict:
"error" ,
2853 + lastReason:
errorRetryLabel (retries, classified),
2854 + timeUsedSeconds:
currentElapsedSeconds (state),
2856 + appendGoalEvent (pi, ctx,
"error-retry" , state, {
2857 + reason: errorMessage,
2858 + kind: classified.kind,
2861 + renderGoalUi (ctx, state);
2862 + syncUpdateGoalTool ();
2865 + `Goal ${classified.short} — ${ errorRetryLabel (retries, classified)}` ,
2617 2868 const retryState
= state;
2619 2870 ()
=> scheduleContinuation (pi, ctx, retryState, continuation),
2620 - Math .
min (
30000 ,
2000 * retries),
2871 + errorBackoffMs (retries),
2624 2876 state
= updateGoal (pi, state, {
2625 2877 status:
"paused" ,
2626 2878 lastVerdict:
"error" ,
2627 - lastReason: errorMessage,
2879 + lastReason:
errorPauseLabel (classified),
2628 2880 timeUsedSeconds:
currentElapsedSeconds (state),
2630 - appendGoalEvent (pi, ctx,
"paused" , state, { reason: state.lastReason });
2882 + appendGoalEvent (pi, ctx,
"paused" , state, {
2883 + reason: state.lastReason,
2884 + raw: errorMessage,
2885 + kind: classified.kind,
2631 2887 renderGoalUi (ctx, state);
2632 2888 syncUpdateGoalTool ();
3025 3281 judgeDisplayReason,
3026 3282 isJudgeFailure,
3027 3283 isRetryableError,
3028 3285 recoveredAgentErrorPatch,
3029 - MAX_ERROR_RETRIES,
3032 3288 extractStandingGoalObjective,