pi-harness-runtime 1.1.485 → 1.1.487
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +127 -0
- package/harness/pi-retry-policy.ts +142 -0
- package/harness/pi-retry-visibility.ts +8 -0
- package/harness/provider-failure.ts +3 -0
- package/harness/provider-recovery-runtime.ts +510 -0
- package/index.ts +200 -227
- package/package.json +1 -1
- package/packages/agent-policy/package.json +1 -1
- package/packages/auth/package.json +1 -1
- package/packages/autonomous-runtime/package.json +1 -1
- package/packages/checkpoint/package.json +1 -1
- package/packages/clipboard/package.json +1 -1
- package/packages/codex-watcher/package.json +1 -1
- package/packages/config-capture/package.json +1 -1
- package/packages/contracts/package.json +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/evaluation-engine/package.json +1 -1
- package/packages/event-bus/package.json +1 -1
- package/packages/event-store/package.json +1 -1
- package/packages/experience-replay/package.json +1 -1
- package/packages/file-copy-helper/package.json +1 -1
- package/packages/intent-analyzer/package.json +1 -1
- package/packages/jev-judge/dist/continuation-controller.d.ts +8 -0
- package/packages/jev-judge/dist/continuation-controller.d.ts.map +1 -1
- package/packages/jev-judge/dist/continuation-controller.js +10 -0
- package/packages/jev-judge/dist/continuation-controller.js.map +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/jev-judge/src/continuation-controller.ts +11 -0
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/memory-engine/package.json +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skills/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/write-review/package.json +1 -1
package/index.ts
CHANGED
|
@@ -60,6 +60,11 @@ import {
|
|
|
60
60
|
createPendingActionTicker,
|
|
61
61
|
} from "./harness/pending-actions.ts";
|
|
62
62
|
import { classifyProviderFailure, usesBoundedRecovery } from "./harness/provider-failure.ts";
|
|
63
|
+
import {
|
|
64
|
+
expectsPiRetry,
|
|
65
|
+
readPiRetrySettings,
|
|
66
|
+
} from "./harness/pi-retry-policy.ts";
|
|
67
|
+
import { ProviderRecoveryRuntime } from "./harness/provider-recovery-runtime.ts";
|
|
63
68
|
import type { ProviderFailure } from "./harness/provider-failure.ts";
|
|
64
69
|
import {
|
|
65
70
|
createProviderRecoveryController,
|
|
@@ -108,6 +113,7 @@ import { registerGithubLoginCommand } from "./packages/clipboard/dist/src/github
|
|
|
108
113
|
import {
|
|
109
114
|
MAX_PROACTIVE_COMPACT_FAILURES,
|
|
110
115
|
OUTPUT_LIMIT_RESUME_PROMPT,
|
|
116
|
+
PROVIDER_OVERLOAD_AUTO_RESUME_LIMIT,
|
|
111
117
|
PROVIDER_OVERLOAD_RESUME_PROMPT,
|
|
112
118
|
getProviderOverloadResumeDelayMs,
|
|
113
119
|
PROACTIVE_COMPACT_COOLDOWN_MS,
|
|
@@ -381,19 +387,39 @@ const PROVIDER_RECOVERY_ACTION_ID = "provider-recovery";
|
|
|
381
387
|
const PROVIDER_BLOCKED_ACTION_ID = "provider-blocked";
|
|
382
388
|
|
|
383
389
|
/**
|
|
384
|
-
* The
|
|
390
|
+
* The runtime of the automatic resumes after a provider failure.
|
|
385
391
|
*
|
|
386
|
-
* The
|
|
387
|
-
*
|
|
392
|
+
* The runtime owns the sequence of a failure: it waits for the settled
|
|
393
|
+
* boundary, and it checks that a scheduled resume still owns the session
|
|
394
|
+
* before it dispatches. The Jev scope uses it to cancel the pending
|
|
395
|
+
* resumes when the user pauses the work.
|
|
388
396
|
*/
|
|
389
|
-
|
|
397
|
+
let providerRecoveryRuntime: ProviderRecoveryRuntime | null = null;
|
|
390
398
|
|
|
391
399
|
/**
|
|
392
|
-
* The
|
|
400
|
+
* The continuation controller of the todos.
|
|
393
401
|
*
|
|
394
|
-
*
|
|
402
|
+
* The runtime reads the pause state of the user through this handle: the
|
|
403
|
+
* injected resume is a resume, and a resume that ignores a pause overrides
|
|
404
|
+
* the decision of the user.
|
|
395
405
|
*/
|
|
396
|
-
let
|
|
406
|
+
let todoContinuationHandle: { isExplicitlyPaused(): boolean } | null = null;
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* The context of the running extension.
|
|
410
|
+
*
|
|
411
|
+
* The context knows whether messages wait in the queue. A resume that
|
|
412
|
+
* injects itself while the user is typing competes with the user.
|
|
413
|
+
*/
|
|
414
|
+
let activeExtensionContext: ExtensionContext | null = null;
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* The controller of the bounded recovery cycles.
|
|
418
|
+
*
|
|
419
|
+
* The controller decides the deadlines of the attempts; the executor that
|
|
420
|
+
* owns the resume stays the only owner of the execution.
|
|
421
|
+
*/
|
|
422
|
+
const providerRecovery = createProviderRecoveryController();
|
|
397
423
|
|
|
398
424
|
/**
|
|
399
425
|
* The counter of the failures that carry no identity.
|
|
@@ -404,18 +430,23 @@ let providerRecoveryTimer: ReturnType<typeof setTimeout> | null = null;
|
|
|
404
430
|
let providerFailureCounter = 0;
|
|
405
431
|
|
|
406
432
|
/**
|
|
407
|
-
*
|
|
433
|
+
* The number of provider failures of the current run.
|
|
408
434
|
*
|
|
409
|
-
*
|
|
410
|
-
*
|
|
435
|
+
* Pi retries a failed turn internally. Every attempt of that retry
|
|
436
|
+
* produces another failed message, so the counter tells the harness
|
|
437
|
+
* whether Pi made a retry attempt after the failure that it holds.
|
|
411
438
|
*/
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
439
|
+
let providerFailureAttemptsInRun = 0;
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* The abort signal of the run that is going.
|
|
443
|
+
*
|
|
444
|
+
* The signal of the context is the signal of the active run. The user
|
|
445
|
+
* aborts a run with Escape, and the aborted signal is the way the harness
|
|
446
|
+
* learns about it: the installed core does not carry an abort flag on the
|
|
447
|
+
* settled event.
|
|
448
|
+
*/
|
|
449
|
+
let activeRunAbortSignal: AbortSignal | undefined;
|
|
419
450
|
|
|
420
451
|
/**
|
|
421
452
|
* The identity of the pending action of the decision of the judge.
|
|
@@ -1570,6 +1601,9 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
|
|
|
1570
1601
|
|
|
1571
1602
|
// Capture UI context from session_start
|
|
1572
1603
|
pi.on("session_start", (_event, ctx) => {
|
|
1604
|
+
// The context of the running extension: a resume checks it
|
|
1605
|
+
// before it dispatches, so it does not compete with the user.
|
|
1606
|
+
activeExtensionContext = ctx;
|
|
1573
1607
|
// The session of the runtime: the countdown of a question belongs
|
|
1574
1608
|
// to the session that asked it.
|
|
1575
1609
|
const sessionId = (
|
|
@@ -1632,7 +1666,10 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
|
|
|
1632
1666
|
pendingActionTicker.stop();
|
|
1633
1667
|
pendingActions.clear();
|
|
1634
1668
|
providerRecovery.clear();
|
|
1635
|
-
|
|
1669
|
+
// The pending resumes die with the session: a restart does not
|
|
1670
|
+
// inherit a countdown, a blocked provider, or a resume that would
|
|
1671
|
+
// inject itself into the new session.
|
|
1672
|
+
providerRecoveryRuntime?.reset();
|
|
1636
1673
|
setPendingActionsStatus?.(undefined);
|
|
1637
1674
|
setPendingActionsStatus = null;
|
|
1638
1675
|
activeSessionId = "";
|
|
@@ -1946,6 +1983,9 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
|
|
|
1946
1983
|
// so nothing but an explicit resume directive clears
|
|
1947
1984
|
// it.
|
|
1948
1985
|
(sharedState.todoContinuationController as any)?.explicitPause?.();
|
|
1986
|
+
// The user took over: a pending resume of the provider
|
|
1987
|
+
// would inject itself against the decline.
|
|
1988
|
+
providerRecoveryRuntime?.cancel();
|
|
1949
1989
|
logToFile(
|
|
1950
1990
|
"[auto-continue]",
|
|
1951
1991
|
"The reply declined the proposal; the work is paused",
|
|
@@ -2471,6 +2511,9 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
|
|
|
2471
2511
|
validationLedger,
|
|
2472
2512
|
}
|
|
2473
2513
|
);
|
|
2514
|
+
// The runtime of the provider recovery reads the pause state
|
|
2515
|
+
// of the user through this handle.
|
|
2516
|
+
todoContinuationHandle = todoContinuationController;
|
|
2474
2517
|
logToFile("[auto-continue]", "Todo continuation controller initialized");
|
|
2475
2518
|
} catch (err) {
|
|
2476
2519
|
logToFile("[auto-continue]", "Failed to create todo continuation controller:", String(err));
|
|
@@ -2483,6 +2526,7 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
|
|
|
2483
2526
|
// keep a timer alive nor block a later continuation.
|
|
2484
2527
|
todoContinuationController?.dispose?.();
|
|
2485
2528
|
todoContinuationController = null;
|
|
2529
|
+
todoContinuationHandle = null;
|
|
2486
2530
|
sharedState.validationLedger = null;
|
|
2487
2531
|
});
|
|
2488
2532
|
|
|
@@ -2555,6 +2599,9 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
|
|
|
2555
2599
|
todoContinuationController.explicitPause();
|
|
2556
2600
|
logToFile("[auto-continue]", "Explicit pause/stop detected");
|
|
2557
2601
|
}
|
|
2602
|
+
// The user took over: a pending resume would inject
|
|
2603
|
+
// itself against the decision of the user.
|
|
2604
|
+
providerRecoveryRuntime?.cancel();
|
|
2558
2605
|
// Invalidate any pending Jev evaluations to prevent late dispatch
|
|
2559
2606
|
if (taskIntakeController) {
|
|
2560
2607
|
taskIntakeController.invalidatePending();
|
|
@@ -6379,7 +6426,74 @@ Run \`bd ready\` to see current bd issues.
|
|
|
6379
6426
|
});
|
|
6380
6427
|
|
|
6381
6428
|
// --- Auto-track every assistant message ------------------------------
|
|
6429
|
+
// The runtime of the automatic resumes after a provider failure.
|
|
6430
|
+
//
|
|
6431
|
+
// The runtime owns the sequence of a failure: the failure waits for the
|
|
6432
|
+
// settled boundary, because Pi retries a failed turn internally first,
|
|
6433
|
+
// and a scheduled resume checks that it still owns the session before it
|
|
6434
|
+
// dispatches. The resume itself goes through the executor below, so
|
|
6435
|
+
// there is no second resume loop.
|
|
6436
|
+
providerRecoveryRuntime = new ProviderRecoveryRuntime({
|
|
6437
|
+
controller: providerRecovery,
|
|
6438
|
+
actions: pendingActions,
|
|
6439
|
+
recoveryActionId: PROVIDER_RECOVERY_ACTION_ID,
|
|
6440
|
+
blockedActionId: PROVIDER_BLOCKED_ACTION_ID,
|
|
6441
|
+
overloadActionId: PROVIDER_OVERLOAD_ACTION_ID,
|
|
6442
|
+
maxAttempts: MAX_RECOVERY_ATTEMPTS,
|
|
6443
|
+
context: () => ({
|
|
6444
|
+
sessionId: activeSessionId,
|
|
6445
|
+
explicitlyPaused: todoContinuationHandle?.isExplicitlyPaused() ?? false,
|
|
6446
|
+
questionPending:
|
|
6447
|
+
sharedState.pqController?.hasPendingQuestion?.() ?? false,
|
|
6448
|
+
}),
|
|
6449
|
+
resetEvidence: (failure) => applicableResetEvidence(failure),
|
|
6450
|
+
overloadDelayMs: () => getProviderOverloadResumeDelayMs(),
|
|
6451
|
+
overloadMaxAttempts: PROVIDER_OVERLOAD_AUTO_RESUME_LIMIT,
|
|
6452
|
+
dispatch: async (reason) => {
|
|
6453
|
+
// The user is typing: a resume that injects itself now competes
|
|
6454
|
+
// with the user.
|
|
6455
|
+
if (activeExtensionContext?.hasPendingMessages()) {
|
|
6456
|
+
logToFile("[pi-harness]", "Resume skipped: hasPendingMessages=true");
|
|
6457
|
+
return;
|
|
6458
|
+
}
|
|
6459
|
+
await queueAutoResume(reason, PROVIDER_OVERLOAD_RESUME_PROMPT, "followUp");
|
|
6460
|
+
},
|
|
6461
|
+
log: (message) => logToFile("[pi-harness]", message),
|
|
6462
|
+
});
|
|
6463
|
+
|
|
6464
|
+
// The run settled: the internal retries of Pi are over, so a failure
|
|
6465
|
+
// that waited for the boundary is final and the recovery starts now.
|
|
6466
|
+
pi.on("agent_settled", (event) => {
|
|
6467
|
+
// An aborted run is a decision of the user, not a failure of the
|
|
6468
|
+
// provider: an automatic resume would fight the user.
|
|
6469
|
+
//
|
|
6470
|
+
// The installed core of Pi does not carry an abort flag on this
|
|
6471
|
+
// event. The signal of the run is the way the harness learns about
|
|
6472
|
+
// it: the signal belongs to the run that is going, and Escape
|
|
6473
|
+
// aborts it. The optional flag of a newer core is read when it is
|
|
6474
|
+
// there, and it is never assumed.
|
|
6475
|
+
const abortedRun =
|
|
6476
|
+
("aborted" in event && event.aborted === true) ||
|
|
6477
|
+
activeRunAbortSignal?.aborted === true;
|
|
6478
|
+
activeRunAbortSignal = undefined;
|
|
6479
|
+
if (abortedRun) {
|
|
6480
|
+
providerRecoveryRuntime?.cancel();
|
|
6481
|
+
providerFailureAttemptsInRun = 0;
|
|
6482
|
+
return;
|
|
6483
|
+
}
|
|
6484
|
+
providerRecoveryRuntime?.noteSettled();
|
|
6485
|
+
// The run is over: the streak of its failures ends with it, so the
|
|
6486
|
+
// next run starts its own count.
|
|
6487
|
+
providerFailureAttemptsInRun = 0;
|
|
6488
|
+
});
|
|
6489
|
+
|
|
6382
6490
|
pi.on("message_end", async (event, ctx) => {
|
|
6491
|
+
// The context of the running extension: the resume checks it before
|
|
6492
|
+
// it dispatches, so it does not compete with the user.
|
|
6493
|
+
activeExtensionContext = ctx;
|
|
6494
|
+
// The signal of the run that is going: the user aborts a run with
|
|
6495
|
+
// Escape, and the signal is the way the harness learns about it.
|
|
6496
|
+
activeRunAbortSignal = ctx.signal ?? activeRunAbortSignal;
|
|
6383
6497
|
if (isOutputLimitResumePromptMessage(event.message)) {
|
|
6384
6498
|
pendingOutputLimitResumeAfterSettled = false;
|
|
6385
6499
|
return;
|
|
@@ -6410,6 +6524,15 @@ Run \`bd ready\` to see current bd issues.
|
|
|
6410
6524
|
};
|
|
6411
6525
|
};
|
|
6412
6526
|
|
|
6527
|
+
// An aborted turn is a decision of the user, not a failure of the
|
|
6528
|
+
// provider. The candidate of an earlier failure dies with it: an
|
|
6529
|
+
// automatic resume would fight the decision of the user.
|
|
6530
|
+
if (m.stopReason === "aborted" || ctx.signal?.aborted === true) {
|
|
6531
|
+
providerRecoveryRuntime?.cancel();
|
|
6532
|
+
providerFailureAttemptsInRun = 0;
|
|
6533
|
+
return;
|
|
6534
|
+
}
|
|
6535
|
+
|
|
6413
6536
|
// A provider failure is classified before anything else happens. The
|
|
6414
6537
|
// classification reads the error of the turn; the text of an answer
|
|
6415
6538
|
// is only read when it names a known provider failure, so a sentence
|
|
@@ -6424,25 +6547,57 @@ Run \`bd ready\` to see current bd issues.
|
|
|
6424
6547
|
})();
|
|
6425
6548
|
|
|
6426
6549
|
if (providerFailure) {
|
|
6427
|
-
// The
|
|
6428
|
-
//
|
|
6429
|
-
//
|
|
6430
|
-
|
|
6431
|
-
|
|
6432
|
-
|
|
6433
|
-
|
|
6434
|
-
|
|
6435
|
-
|
|
6436
|
-
|
|
6437
|
-
|
|
6438
|
-
|
|
6439
|
-
|
|
6440
|
-
|
|
6441
|
-
|
|
6442
|
-
|
|
6443
|
-
|
|
6550
|
+
// The failure is a candidate: the recovery starts when the run
|
|
6551
|
+
// settles, because Pi retries a failed turn internally first.
|
|
6552
|
+
// A recovery that starts at the message of the failed attempt is
|
|
6553
|
+
// a competing continuation.
|
|
6554
|
+
providerFailureAttemptsInRun += 1;
|
|
6555
|
+
let failureKey =
|
|
6556
|
+
typeof m.responseId === "string" && m.responseId.length > 0
|
|
6557
|
+
? m.responseId
|
|
6558
|
+
: "";
|
|
6559
|
+
if (!failureKey) {
|
|
6560
|
+
providerFailureCounter += 1;
|
|
6561
|
+
failureKey = `${activeSessionId}:${providerFailureCounter}`;
|
|
6562
|
+
}
|
|
6563
|
+
if (providerFailure.retryable) {
|
|
6564
|
+
// The identity of the failure keeps a repeated copy of the
|
|
6565
|
+
// same error from starting a second cycle.
|
|
6566
|
+
const overloadEligible =
|
|
6567
|
+
providerFailure.kind === "overload" &&
|
|
6568
|
+
!isProviderRetryFailedReport(readMessageText(event.message)) &&
|
|
6569
|
+
shouldQueueProviderOverloadResume(
|
|
6570
|
+
m,
|
|
6571
|
+
providerRecoveryRuntime?.overloadAttemptCount ?? 0,
|
|
6572
|
+
ctx.hasPendingMessages(),
|
|
6573
|
+
{ detected: true },
|
|
6574
|
+
);
|
|
6575
|
+
providerRecoveryRuntime?.noteFailure(providerFailure, failureKey, {
|
|
6576
|
+
...(typeof m.provider === "string" ? { provider: m.provider } : {}),
|
|
6577
|
+
overloadEligible,
|
|
6578
|
+
// The classification owns the policy: a rate limit and an
|
|
6579
|
+
// exhausted plan join the cycle, an overload keeps its own
|
|
6580
|
+
// executor. One failure has one executor.
|
|
6581
|
+
boundedEligible: usesBoundedRecovery(providerFailure),
|
|
6582
|
+
// Whether Pi would retry this failure itself, and whether
|
|
6583
|
+
// it made the attempt. A retry that Pi announces and never
|
|
6584
|
+
// makes was cancelled by the user.
|
|
6585
|
+
piRetryExpected: expectsPiRetry(
|
|
6586
|
+
providerFailure,
|
|
6587
|
+
readPiRetrySettings({ cwd: ctx.cwd }),
|
|
6588
|
+
),
|
|
6589
|
+
piRetryAttempted: providerFailureAttemptsInRun > 1,
|
|
6590
|
+
});
|
|
6591
|
+
} else {
|
|
6444
6592
|
// No automatic retry can fix this failure: the row says that
|
|
6445
6593
|
// nothing is scheduled.
|
|
6594
|
+
//
|
|
6595
|
+
// A permanent failure also supersedes the retry of an
|
|
6596
|
+
// earlier failure of the same run: a rate limit that was
|
|
6597
|
+
// followed by a credential error must not schedule a resume
|
|
6598
|
+
// when the run settles.
|
|
6599
|
+
providerRecoveryRuntime?.cancelPending();
|
|
6600
|
+
providerFailureAttemptsInRun = 0;
|
|
6446
6601
|
pendingActions.register({
|
|
6447
6602
|
id: PROVIDER_BLOCKED_ACTION_ID,
|
|
6448
6603
|
sessionId: activeSessionId,
|
|
@@ -6454,6 +6609,11 @@ Run \`bd ready\` to see current bd issues.
|
|
|
6454
6609
|
reason: providerFailure.reason,
|
|
6455
6610
|
});
|
|
6456
6611
|
}
|
|
6612
|
+
} else {
|
|
6613
|
+
// A turn that is not a provider failure ends the streak of the
|
|
6614
|
+
// failures of the run: the next failure is the first attempt of
|
|
6615
|
+
// its own retry.
|
|
6616
|
+
providerFailureAttemptsInRun = 0;
|
|
6457
6617
|
}
|
|
6458
6618
|
|
|
6459
6619
|
if (
|
|
@@ -6476,46 +6636,16 @@ Run \`bd ready\` to see current bd issues.
|
|
|
6476
6636
|
requirement: "Session output",
|
|
6477
6637
|
error: `Attempt ${outputLimitResumeAttempts}`,
|
|
6478
6638
|
});
|
|
6479
|
-
} else if (
|
|
6480
|
-
shouldQueueProviderOverloadResume(
|
|
6481
|
-
m,
|
|
6482
|
-
providerOverloadResumeAttempts,
|
|
6483
|
-
ctx.hasPendingMessages(),
|
|
6484
|
-
{
|
|
6485
|
-
// The classification may know an overload that the text
|
|
6486
|
-
// of the answer does not repeat. A report of exhausted
|
|
6487
|
-
// retries of the SDK is not a fresh failure.
|
|
6488
|
-
detected:
|
|
6489
|
-
providerFailure?.kind === "overload" &&
|
|
6490
|
-
!isProviderRetryFailedReport(readMessageText(event.message)),
|
|
6491
|
-
},
|
|
6492
|
-
)
|
|
6493
|
-
) {
|
|
6494
|
-
providerOverloadResumeAttempts += 1;
|
|
6495
|
-
logToFile("[pi-harness]", `Scheduling provider overload resume #${providerOverloadResumeAttempts}`);
|
|
6496
|
-
scheduleProviderOverloadResume(() => ctx.hasPendingMessages());
|
|
6497
6639
|
} else if (m.stopReason === "stop") {
|
|
6498
6640
|
outputLimitResumeAttempts = 0;
|
|
6499
6641
|
pendingOutputLimitResumeAfterSettled = false;
|
|
6500
|
-
if (providerOverloadResumeAttempts > 0) {
|
|
6501
|
-
logToFile("[pi-harness]", `Counter reset (stopReason=stop), was=${providerOverloadResumeAttempts}`);
|
|
6502
|
-
}
|
|
6503
|
-
providerOverloadResumeAttempts = 0;
|
|
6504
|
-
clearProviderOverloadResume();
|
|
6505
6642
|
// The turn completed: the provider works again. The recovery
|
|
6506
6643
|
// cycle ends with it, so the next failure starts at the first
|
|
6507
|
-
// attempt, and a pending attempt of the cycle is cancelled.
|
|
6508
|
-
|
|
6509
|
-
|
|
6510
|
-
|
|
6511
|
-
|
|
6512
|
-
});
|
|
6513
|
-
}
|
|
6514
|
-
clearProviderRecoveryAttempt();
|
|
6515
|
-
// The turn completed: a provider that was blocked is not blocked
|
|
6516
|
-
// any more. The row of the blocked provider leaves with it.
|
|
6517
|
-
pendingActions.removeWhere(
|
|
6518
|
-
(action) => action.owner === "provider" && action.state === "blocked",
|
|
6644
|
+
// attempt, and a pending attempt of the cycle is cancelled. A
|
|
6645
|
+
// provider that was blocked is not blocked any more, and its row
|
|
6646
|
+
// leaves with it.
|
|
6647
|
+
providerRecoveryRuntime?.noteSuccess(
|
|
6648
|
+
typeof m.provider === "string" ? m.provider : undefined,
|
|
6519
6649
|
);
|
|
6520
6650
|
|
|
6521
6651
|
// --- Auto-Continue: Check todos and keep going -------------------------------
|
|
@@ -6737,8 +6867,6 @@ Run \`bd ready\` to see current bd issues.
|
|
|
6737
6867
|
let outputLimitResumeAttempts = 0;
|
|
6738
6868
|
let pendingOutputLimitResumeAfterCompact = false;
|
|
6739
6869
|
let pendingOutputLimitResumeAfterSettled = false;
|
|
6740
|
-
let providerOverloadResumeAttempts = 0;
|
|
6741
|
-
let providerOverloadResumeTimer: ReturnType<typeof setTimeout> | null = null;
|
|
6742
6870
|
|
|
6743
6871
|
// --- Context-usage escalating warning tiers -----------------------------
|
|
6744
6872
|
// Amber at 75%, red at 85%. No notify above 90% (proactive compact handles it).
|
|
@@ -7951,16 +8079,6 @@ Run \`bd ready\` to see current bd issues.
|
|
|
7951
8079
|
}
|
|
7952
8080
|
}, MINIMAX_REFRESH_MIN_INTERVAL_MS);
|
|
7953
8081
|
|
|
7954
|
-
function clearProviderOverloadResume(): void {
|
|
7955
|
-
if (providerOverloadResumeTimer) {
|
|
7956
|
-
clearTimeout(providerOverloadResumeTimer);
|
|
7957
|
-
providerOverloadResumeTimer = null;
|
|
7958
|
-
}
|
|
7959
|
-
// The pending action goes with the timer: a cancelled resume has no
|
|
7960
|
-
// countdown any more.
|
|
7961
|
-
pendingActions.remove(PROVIDER_OVERLOAD_ACTION_ID);
|
|
7962
|
-
}
|
|
7963
|
-
|
|
7964
8082
|
/**
|
|
7965
8083
|
* The reset of the provider that applies to a failure.
|
|
7966
8084
|
*
|
|
@@ -7995,151 +8113,6 @@ Run \`bd ready\` to see current bd issues.
|
|
|
7995
8113
|
};
|
|
7996
8114
|
}
|
|
7997
8115
|
|
|
7998
|
-
/**
|
|
7999
|
-
* React to a provider failure that the bounded recovery may retry.
|
|
8000
|
-
*
|
|
8001
|
-
* The cycle decides the deadline of the attempt; the existing
|
|
8002
|
-
* auto-resume executor sends the resume, so there is no second resume
|
|
8003
|
-
* loop. The pending action shows the exact deadline the executor uses.
|
|
8004
|
-
*/
|
|
8005
|
-
function scheduleProviderRecovery(
|
|
8006
|
-
failure: ProviderFailure,
|
|
8007
|
-
failureKey: string,
|
|
8008
|
-
): void {
|
|
8009
|
-
const provider = failure.provider ?? "provider";
|
|
8010
|
-
const outcome = providerRecovery.noteFailure(
|
|
8011
|
-
{
|
|
8012
|
-
failureKey,
|
|
8013
|
-
sessionId: activeSessionId,
|
|
8014
|
-
// The cycle is keyed by the id of the provider, so a cycle of
|
|
8015
|
-
// "MiniMax" and a successful turn of "minimax" are the same
|
|
8016
|
-
// cycle.
|
|
8017
|
-
provider: provider.toLowerCase(),
|
|
8018
|
-
kind: failure.kind,
|
|
8019
|
-
...(failure.window ? { window: failure.window } : {}),
|
|
8020
|
-
...(failure.retryAfterMs !== undefined
|
|
8021
|
-
? { retryAfterAt: Date.now() + failure.retryAfterMs }
|
|
8022
|
-
: {}),
|
|
8023
|
-
},
|
|
8024
|
-
applicableResetEvidence(failure),
|
|
8025
|
-
);
|
|
8026
|
-
|
|
8027
|
-
if (outcome.outcome === "scheduled") {
|
|
8028
|
-
logToFile(
|
|
8029
|
-
"[pi-harness]",
|
|
8030
|
-
`Provider recovery attempt ${outcome.attempt}/${MAX_RECOVERY_ATTEMPTS} at ${new Date(outcome.dueAt).toISOString()} (bound=${outcome.boundBy})`,
|
|
8031
|
-
);
|
|
8032
|
-
startProviderRecoveryAttempt(outcome.dueAt, outcome.attempt, failure);
|
|
8033
|
-
return;
|
|
8034
|
-
}
|
|
8035
|
-
|
|
8036
|
-
if (outcome.outcome === "blocked") {
|
|
8037
|
-
logToFile(
|
|
8038
|
-
"[pi-harness]",
|
|
8039
|
-
`Provider recovery exhausted after ${outcome.attempt} attempts`,
|
|
8040
|
-
);
|
|
8041
|
-
pendingActions.register({
|
|
8042
|
-
id: PROVIDER_BLOCKED_ACTION_ID,
|
|
8043
|
-
sessionId: activeSessionId,
|
|
8044
|
-
owner: "provider",
|
|
8045
|
-
kind: "retry",
|
|
8046
|
-
state: "blocked",
|
|
8047
|
-
...(failure.provider ? { provider: failure.provider } : {}),
|
|
8048
|
-
...(failure.code ? { code: failure.code } : {}),
|
|
8049
|
-
reason: `recovery attempts exhausted (${outcome.attempt}/${MAX_RECOVERY_ATTEMPTS})`,
|
|
8050
|
-
});
|
|
8051
|
-
return;
|
|
8052
|
-
}
|
|
8053
|
-
|
|
8054
|
-
// "already-scheduled" and "duplicate": the cycle does not move. The
|
|
8055
|
-
// row of the pending attempt is already there.
|
|
8056
|
-
logToFile(
|
|
8057
|
-
"[pi-harness]",
|
|
8058
|
-
`Provider recovery unchanged (${outcome.outcome}), attempt ${outcome.attempt}`,
|
|
8059
|
-
);
|
|
8060
|
-
}
|
|
8061
|
-
|
|
8062
|
-
/**
|
|
8063
|
-
* Start the timer of a recovery attempt.
|
|
8064
|
-
*
|
|
8065
|
-
* The timer is the executor, so it owns the deadline: the row of the
|
|
8066
|
-
* pending action shows the deadline the timer really uses.
|
|
8067
|
-
*/
|
|
8068
|
-
function startProviderRecoveryAttempt(
|
|
8069
|
-
dueAt: number,
|
|
8070
|
-
attempt: number,
|
|
8071
|
-
failure: ProviderFailure,
|
|
8072
|
-
): void {
|
|
8073
|
-
clearProviderRecoveryAttempt();
|
|
8074
|
-
pendingActions.register({
|
|
8075
|
-
id: PROVIDER_RECOVERY_ACTION_ID,
|
|
8076
|
-
sessionId: activeSessionId,
|
|
8077
|
-
owner: "provider-recovery",
|
|
8078
|
-
kind: "retry",
|
|
8079
|
-
state: "scheduled",
|
|
8080
|
-
dueAt,
|
|
8081
|
-
attempt,
|
|
8082
|
-
maxAttempts: MAX_RECOVERY_ATTEMPTS,
|
|
8083
|
-
...(failure.provider ? { provider: failure.provider } : {}),
|
|
8084
|
-
...(failure.code ? { code: failure.code } : {}),
|
|
8085
|
-
reason: failure.reason,
|
|
8086
|
-
});
|
|
8087
|
-
const delayMs = Math.max(0, dueAt - Date.now());
|
|
8088
|
-
providerRecoveryTimer = setTimeout(() => {
|
|
8089
|
-
providerRecoveryTimer = null;
|
|
8090
|
-
// The row leaves when the attempt runs: a countdown must not stay
|
|
8091
|
-
// at 00:00.
|
|
8092
|
-
pendingActions.remove(PROVIDER_RECOVERY_ACTION_ID);
|
|
8093
|
-
void queueAutoResume(
|
|
8094
|
-
"provider-recovery",
|
|
8095
|
-
PROVIDER_OVERLOAD_RESUME_PROMPT,
|
|
8096
|
-
"followUp",
|
|
8097
|
-
);
|
|
8098
|
-
}, delayMs);
|
|
8099
|
-
}
|
|
8100
|
-
|
|
8101
|
-
function scheduleProviderOverloadResume(
|
|
8102
|
-
hasPendingMessages: () => boolean,
|
|
8103
|
-
): void {
|
|
8104
|
-
clearProviderOverloadResume();
|
|
8105
|
-
const delayMs = getProviderOverloadResumeDelayMs();
|
|
8106
|
-
const delayMinutes = Math.max(1, Math.round(delayMs / 60_000));
|
|
8107
|
-
logToFile(
|
|
8108
|
-
"[pi-harness]",
|
|
8109
|
-
`Provider overloaded; scheduling resume in ${delayMinutes} min (${delayMs}ms) attempt=${providerOverloadResumeAttempts}`,
|
|
8110
|
-
);
|
|
8111
|
-
// The timer is the executor of the resume, so the timer owns the
|
|
8112
|
-
// deadline: the row of the pending action shows the deadline the
|
|
8113
|
-
// timer really uses, never another one.
|
|
8114
|
-
const dueAt = Date.now() + delayMs;
|
|
8115
|
-
pendingActions.register({
|
|
8116
|
-
id: PROVIDER_OVERLOAD_ACTION_ID,
|
|
8117
|
-
sessionId: activeSessionId,
|
|
8118
|
-
owner: "provider-overload",
|
|
8119
|
-
kind: "retry",
|
|
8120
|
-
state: "scheduled",
|
|
8121
|
-
dueAt,
|
|
8122
|
-
attempt: providerOverloadResumeAttempts,
|
|
8123
|
-
reason: "the provider is overloaded",
|
|
8124
|
-
});
|
|
8125
|
-
providerOverloadResumeTimer = setTimeout(() => {
|
|
8126
|
-
providerOverloadResumeTimer = null;
|
|
8127
|
-
// The action leaves the row when it fires: the countdown must
|
|
8128
|
-
// not stay at 00:00.
|
|
8129
|
-
pendingActions.remove(PROVIDER_OVERLOAD_ACTION_ID);
|
|
8130
|
-
logToFile("[pi-harness]", 'Provider resume timer fired, sending "resume"');
|
|
8131
|
-
if (hasPendingMessages()) {
|
|
8132
|
-
logToFile("[pi-harness]", "Resume skipped: hasPendingMessages=true");
|
|
8133
|
-
return;
|
|
8134
|
-
}
|
|
8135
|
-
void queueAutoResume(
|
|
8136
|
-
"provider-overload",
|
|
8137
|
-
PROVIDER_OVERLOAD_RESUME_PROMPT,
|
|
8138
|
-
"followUp",
|
|
8139
|
-
);
|
|
8140
|
-
}, delayMs);
|
|
8141
|
-
}
|
|
8142
|
-
|
|
8143
8116
|
async function queueAutoResume(
|
|
8144
8117
|
reason: string,
|
|
8145
8118
|
content: string,
|
package/package.json
CHANGED