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
|
@@ -0,0 +1,510 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The runtime of the automatic resumes after a provider failure.
|
|
3
|
+
*
|
|
4
|
+
* The runtime owns the sequence of a failure:
|
|
5
|
+
*
|
|
6
|
+
* ```text
|
|
7
|
+
* failure -> candidate -> (the run settles) -> attempt -> dispatch
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* Two boundaries matter, and both were missing before.
|
|
11
|
+
*
|
|
12
|
+
* **The settled boundary.** Pi retries a failed turn internally, and it
|
|
13
|
+
* emits the events of the failed attempt before it decides to retry. A
|
|
14
|
+
* recovery that starts at the message of the failed attempt is therefore a
|
|
15
|
+
* competing continuation: while the internal retries run, the harness
|
|
16
|
+
* injects its own resume. The runtime keeps the failure as a candidate and
|
|
17
|
+
* starts the countdown when the run settles, which is the moment the
|
|
18
|
+
* internal retries are over.
|
|
19
|
+
*
|
|
20
|
+
* **The ownership of the dispatch.** The injected message is the literal
|
|
21
|
+
* `resume`, and that message clears an explicit pause of the user. A timer
|
|
22
|
+
* that does not look at the state of the session therefore overrides the
|
|
23
|
+
* decision of the user. Before every dispatch the runtime checks that the
|
|
24
|
+
* session is the same, that the user did not pause, and that no question
|
|
25
|
+
* waits for an answer.
|
|
26
|
+
*
|
|
27
|
+
* The runtime never owns the execution: it hands the resume to the
|
|
28
|
+
* dispatcher, which is the existing executor of the harness.
|
|
29
|
+
*/
|
|
30
|
+
import type { ProviderFailure } from "./provider-failure.ts";
|
|
31
|
+
import type { PendingAction, PendingActionRegistry } from "./pending-actions.ts";
|
|
32
|
+
import type {
|
|
33
|
+
ProviderRecoveryController,
|
|
34
|
+
RecoveryResetEvidence,
|
|
35
|
+
} from "./provider-recovery.ts";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The state of the session, read when a resume would run.
|
|
39
|
+
*/
|
|
40
|
+
export interface ProviderRecoverySessionState {
|
|
41
|
+
/** The session that is current now. */
|
|
42
|
+
sessionId: string;
|
|
43
|
+
/** The user paused the work explicitly. */
|
|
44
|
+
explicitlyPaused: boolean;
|
|
45
|
+
/** A question waits for the answer of the user. */
|
|
46
|
+
questionPending: boolean;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The decision about a dispatch.
|
|
51
|
+
*/
|
|
52
|
+
export type ProviderRecoveryDecision =
|
|
53
|
+
| { allowed: true }
|
|
54
|
+
| { allowed: false; reason: string };
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Whether a scheduled resume may still be dispatched.
|
|
58
|
+
*
|
|
59
|
+
* The resume is the literal `resume` message, and that message clears an
|
|
60
|
+
* explicit pause of the user. A resume that ignores a pause overrides the
|
|
61
|
+
* decision of the user, so the pause stops it. A changed session stops it
|
|
62
|
+
* too: a resume of the previous session belongs to nobody.
|
|
63
|
+
*/
|
|
64
|
+
export function mayDispatchResume(state: {
|
|
65
|
+
/** The session the resume was scheduled in. */
|
|
66
|
+
scheduledSessionId: string;
|
|
67
|
+
/** The state of the session now. */
|
|
68
|
+
current: ProviderRecoverySessionState;
|
|
69
|
+
}): ProviderRecoveryDecision {
|
|
70
|
+
if (state.scheduledSessionId !== state.current.sessionId) {
|
|
71
|
+
return { allowed: false, reason: "the session changed" };
|
|
72
|
+
}
|
|
73
|
+
if (state.current.explicitlyPaused) {
|
|
74
|
+
return { allowed: false, reason: "the user paused the work" };
|
|
75
|
+
}
|
|
76
|
+
if (state.current.questionPending) {
|
|
77
|
+
return { allowed: false, reason: "a question waits for the user" };
|
|
78
|
+
}
|
|
79
|
+
return { allowed: true };
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* A failure that waits for the settled boundary.
|
|
84
|
+
*/
|
|
85
|
+
export interface ProviderFailureCandidate {
|
|
86
|
+
/** The classified failure. */
|
|
87
|
+
failure: ProviderFailure;
|
|
88
|
+
/** The identity of the failed turn. */
|
|
89
|
+
failureKey: string;
|
|
90
|
+
/** The provider that rejected the request. */
|
|
91
|
+
provider: string;
|
|
92
|
+
/**
|
|
93
|
+
* Whether the overload executor may resume this failure.
|
|
94
|
+
*
|
|
95
|
+
* The overload has its own executor, and it never joins the cycle: one
|
|
96
|
+
* failure has one executor.
|
|
97
|
+
*/
|
|
98
|
+
overloadEligible: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Whether the bounded recovery cycle may resume this failure.
|
|
101
|
+
*
|
|
102
|
+
* The classification decides: a rate limit and an exhausted plan join
|
|
103
|
+
* the cycle, an overload keeps its own executor, and a credential
|
|
104
|
+
* failure joins neither.
|
|
105
|
+
*/
|
|
106
|
+
boundedEligible: boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Whether Pi would retry this failure itself.
|
|
109
|
+
*
|
|
110
|
+
* A failure that Pi retries has a retry delay, and the user can cancel
|
|
111
|
+
* that delay. A failure that Pi does not retry has no delay, so no
|
|
112
|
+
* cancellation can be read into it.
|
|
113
|
+
*/
|
|
114
|
+
piRetryExpected: boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Whether Pi made an internal retry attempt after this failure.
|
|
117
|
+
*
|
|
118
|
+
* A retry attempt produces another failed message in the same run, so
|
|
119
|
+
* the harness counts the attempts. When Pi would retry and never did,
|
|
120
|
+
* the delay was cancelled.
|
|
121
|
+
*/
|
|
122
|
+
piRetryAttempted: boolean;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The options of the runtime.
|
|
127
|
+
*/
|
|
128
|
+
export interface ProviderRecoveryRuntimeOptions {
|
|
129
|
+
/** The controller of the cycle. It decides the deadlines. */
|
|
130
|
+
controller: ProviderRecoveryController;
|
|
131
|
+
/** The registry of the pending actions. */
|
|
132
|
+
actions: PendingActionRegistry;
|
|
133
|
+
/** The identity of the pending action of an attempt of the cycle. */
|
|
134
|
+
recoveryActionId: string;
|
|
135
|
+
/** The identity of the pending action of a blocked provider. */
|
|
136
|
+
blockedActionId: string;
|
|
137
|
+
/** The identity of the pending action of the overload resume. */
|
|
138
|
+
overloadActionId: string;
|
|
139
|
+
/** The state of the session, read when a resume would run. */
|
|
140
|
+
context: () => ProviderRecoverySessionState;
|
|
141
|
+
/** The dispatcher. It sends the resume through the existing executor. */
|
|
142
|
+
dispatch: (reason: string) => void | Promise<void>;
|
|
143
|
+
/** The clock. It is injected, so the tests are exact. */
|
|
144
|
+
now?: () => number;
|
|
145
|
+
/** The timer that starts a resume. */
|
|
146
|
+
setTimeout?: (callback: () => void, delayMs: number) => unknown;
|
|
147
|
+
/** The timer that stops a resume. */
|
|
148
|
+
clearTimeout?: (handle: unknown) => void;
|
|
149
|
+
/** The reset evidence of the provider, when the harness observed one. */
|
|
150
|
+
resetEvidence?: (failure: ProviderFailure) => RecoveryResetEvidence | undefined;
|
|
151
|
+
/** The number of attempts the cycle allows. */
|
|
152
|
+
maxAttempts: number;
|
|
153
|
+
/** The wait of the overload executor, in milliseconds. */
|
|
154
|
+
overloadDelayMs?: () => number;
|
|
155
|
+
/** The number of attempts the overload executor allows. */
|
|
156
|
+
overloadMaxAttempts?: number;
|
|
157
|
+
/** The log of the runtime. */
|
|
158
|
+
log?: (message: string) => void;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* The runtime of the automatic resumes after a provider failure.
|
|
163
|
+
*/
|
|
164
|
+
export class ProviderRecoveryRuntime {
|
|
165
|
+
private readonly options: ProviderRecoveryRuntimeOptions;
|
|
166
|
+
private readonly now: () => number;
|
|
167
|
+
private readonly setTimer: (callback: () => void, delayMs: number) => unknown;
|
|
168
|
+
private readonly clearTimer: (handle: unknown) => void;
|
|
169
|
+
/** The failure that waits for the settled boundary. */
|
|
170
|
+
private candidate: ProviderFailureCandidate | null = null;
|
|
171
|
+
/** The timer of the pending attempt of the cycle. */
|
|
172
|
+
private recoveryTimer: unknown = null;
|
|
173
|
+
/** The timer of the pending overload resume. */
|
|
174
|
+
private overloadTimer: unknown = null;
|
|
175
|
+
/** The number of the overload resumes that ran. */
|
|
176
|
+
private overloadAttempts = 0;
|
|
177
|
+
|
|
178
|
+
constructor(options: ProviderRecoveryRuntimeOptions) {
|
|
179
|
+
this.options = options;
|
|
180
|
+
this.now = options.now ?? (() => Date.now());
|
|
181
|
+
this.setTimer =
|
|
182
|
+
options.setTimeout ??
|
|
183
|
+
((callback, delayMs) => setTimeout(callback, delayMs) as unknown);
|
|
184
|
+
this.clearTimer =
|
|
185
|
+
options.clearTimeout ??
|
|
186
|
+
((handle) => clearTimeout(handle as ReturnType<typeof setTimeout>));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Note a failure of a turn.
|
|
191
|
+
*
|
|
192
|
+
* The failure is a candidate: the countdown starts when the run
|
|
193
|
+
* settles, because Pi retries a failed turn internally first.
|
|
194
|
+
*/
|
|
195
|
+
noteFailure(
|
|
196
|
+
failure: ProviderFailure,
|
|
197
|
+
failureKey: string,
|
|
198
|
+
options: {
|
|
199
|
+
provider?: string;
|
|
200
|
+
overloadEligible?: boolean;
|
|
201
|
+
boundedEligible?: boolean;
|
|
202
|
+
piRetryExpected?: boolean;
|
|
203
|
+
piRetryAttempted?: boolean;
|
|
204
|
+
} = {},
|
|
205
|
+
): void {
|
|
206
|
+
this.candidate = {
|
|
207
|
+
failure,
|
|
208
|
+
failureKey,
|
|
209
|
+
provider: (options.provider ?? failure.provider ?? "provider").toLowerCase(),
|
|
210
|
+
overloadEligible: options.overloadEligible === true,
|
|
211
|
+
boundedEligible: options.boundedEligible === true,
|
|
212
|
+
piRetryExpected: options.piRetryExpected === true,
|
|
213
|
+
piRetryAttempted: options.piRetryAttempted === true,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* The run settled.
|
|
219
|
+
*
|
|
220
|
+
* The internal retries of Pi are over, so the failure is final. The
|
|
221
|
+
* recovery starts now, and its deadline is measured from this moment.
|
|
222
|
+
*/
|
|
223
|
+
noteSettled(): void {
|
|
224
|
+
const candidate = this.candidate;
|
|
225
|
+
this.candidate = null;
|
|
226
|
+
if (!candidate) return;
|
|
227
|
+
|
|
228
|
+
if (candidate.piRetryExpected && !candidate.piRetryAttempted) {
|
|
229
|
+
// Pi would retry this failure and never made the attempt: the
|
|
230
|
+
// delay of the retry was cancelled, and the user decided that.
|
|
231
|
+
// An automatic resume would fight that decision.
|
|
232
|
+
this.log(
|
|
233
|
+
`Pi's internal retry was cancelled: no recovery for ${candidate.failure.kind}`,
|
|
234
|
+
);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (candidate.overloadEligible) {
|
|
239
|
+
this.startOverloadResume();
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (!candidate.boundedEligible) {
|
|
243
|
+
// The failure has no executor: the classification says that no
|
|
244
|
+
// automatic retry can fix it. Nothing is scheduled.
|
|
245
|
+
this.log(
|
|
246
|
+
`Provider failure has no automatic executor: ${candidate.failure.kind}`,
|
|
247
|
+
);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
this.startRecovery(candidate);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* A successful turn ends the cycle and clears the blocked row.
|
|
255
|
+
*/
|
|
256
|
+
noteSuccess(provider: string | undefined): void {
|
|
257
|
+
this.cancelPending();
|
|
258
|
+
// The provider works again: the counter of the overload resumes
|
|
259
|
+
// starts at zero, so the next overload gets the full budget.
|
|
260
|
+
this.overloadAttempts = 0;
|
|
261
|
+
// The cycle is keyed by the provider, so a turn without a known
|
|
262
|
+
// provider ends no cycle. It still ends the pending resumes above.
|
|
263
|
+
const known =
|
|
264
|
+
typeof provider === "string" && provider.trim().length > 0;
|
|
265
|
+
if (known) {
|
|
266
|
+
this.options.controller.noteSuccess({
|
|
267
|
+
sessionId: this.options.context().sessionId,
|
|
268
|
+
provider: provider.toLowerCase(),
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
this.options.actions.removeWhere(
|
|
272
|
+
(action) => action.owner === "provider" && action.state === "blocked",
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* A user intervention cancels the pending resumes.
|
|
278
|
+
*
|
|
279
|
+
* A resume that runs after the user took over is a resume that
|
|
280
|
+
* overrides the decision of the user.
|
|
281
|
+
*/
|
|
282
|
+
cancel(): void {
|
|
283
|
+
this.cancelPending();
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Cancel the pending resumes without touching the cycle.
|
|
288
|
+
*
|
|
289
|
+
* The cycle keeps its budget: a cancelled attempt is not an attempt of
|
|
290
|
+
* the budget, but the failure that caused it is still the failure of
|
|
291
|
+
* the cycle.
|
|
292
|
+
*/
|
|
293
|
+
cancelPending(): void {
|
|
294
|
+
this.candidate = null;
|
|
295
|
+
this.cancelRecoveryAttempt();
|
|
296
|
+
this.cancelOverloadResume();
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The session ended: every pending resume dies with it.
|
|
301
|
+
*/
|
|
302
|
+
reset(): void {
|
|
303
|
+
this.cancel();
|
|
304
|
+
this.overloadAttempts = 0;
|
|
305
|
+
this.options.controller.clear();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Whether a resume of the cycle is pending.
|
|
310
|
+
*/
|
|
311
|
+
get hasPendingRecovery(): boolean {
|
|
312
|
+
return this.recoveryTimer !== null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Whether an overload resume is pending.
|
|
317
|
+
*/
|
|
318
|
+
get hasPendingOverload(): boolean {
|
|
319
|
+
return this.overloadTimer !== null;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The number of the overload resumes that ran.
|
|
324
|
+
*
|
|
325
|
+
* The executor of the overload asks the runtime before it schedules
|
|
326
|
+
* another one: the limit of the executor and the count of the runtime
|
|
327
|
+
* are the same number.
|
|
328
|
+
*/
|
|
329
|
+
get overloadAttemptCount(): number {
|
|
330
|
+
return this.overloadAttempts;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Start the cycle of a failure.
|
|
335
|
+
*/
|
|
336
|
+
private startRecovery(candidate: ProviderFailureCandidate): void {
|
|
337
|
+
const { failure, failureKey, provider } = candidate;
|
|
338
|
+
const sessionId = this.options.context().sessionId;
|
|
339
|
+
const outcome = this.options.controller.noteFailure(
|
|
340
|
+
{
|
|
341
|
+
failureKey,
|
|
342
|
+
sessionId,
|
|
343
|
+
provider,
|
|
344
|
+
kind: failure.kind,
|
|
345
|
+
...(failure.window ? { window: failure.window } : {}),
|
|
346
|
+
...(failure.retryAfterMs !== undefined
|
|
347
|
+
? { retryAfterAt: this.now() + failure.retryAfterMs }
|
|
348
|
+
: {}),
|
|
349
|
+
},
|
|
350
|
+
this.options.resetEvidence?.(failure),
|
|
351
|
+
);
|
|
352
|
+
|
|
353
|
+
if (outcome.outcome === "scheduled") {
|
|
354
|
+
this.log(
|
|
355
|
+
`Provider recovery attempt ${outcome.attempt}/${this.maxAttempts} at ${new Date(outcome.dueAt).toISOString()} (bound=${outcome.boundBy})`,
|
|
356
|
+
);
|
|
357
|
+
this.scheduleAttempt(outcome.dueAt, outcome.attempt, failure, sessionId);
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (outcome.outcome === "blocked") {
|
|
362
|
+
this.log(
|
|
363
|
+
`Provider recovery exhausted after ${outcome.attempt} attempts`,
|
|
364
|
+
);
|
|
365
|
+
this.registerBlocked(failure, outcome.attempt);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
this.log(
|
|
370
|
+
`Provider recovery unchanged (${outcome.outcome}), attempt ${outcome.attempt}`,
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* The number of attempts the cycle allows.
|
|
376
|
+
*/
|
|
377
|
+
private get maxAttempts(): number {
|
|
378
|
+
return this.options.maxAttempts;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Register the row of a blocked provider.
|
|
383
|
+
*/
|
|
384
|
+
private registerBlocked(failure: ProviderFailure, attempt: number): void {
|
|
385
|
+
const action: PendingAction = {
|
|
386
|
+
id: this.options.blockedActionId,
|
|
387
|
+
sessionId: this.options.context().sessionId,
|
|
388
|
+
owner: "provider",
|
|
389
|
+
kind: "retry",
|
|
390
|
+
state: "blocked",
|
|
391
|
+
...(failure.provider ? { provider: failure.provider } : {}),
|
|
392
|
+
...(failure.code ? { code: failure.code } : {}),
|
|
393
|
+
reason: `recovery attempts exhausted (${attempt}/${this.maxAttempts})`,
|
|
394
|
+
};
|
|
395
|
+
this.options.actions.register(action);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Start the timer of an attempt of the cycle.
|
|
400
|
+
*/
|
|
401
|
+
private scheduleAttempt(
|
|
402
|
+
dueAt: number,
|
|
403
|
+
attempt: number,
|
|
404
|
+
failure: ProviderFailure,
|
|
405
|
+
sessionId: string,
|
|
406
|
+
): void {
|
|
407
|
+
this.cancelRecoveryAttempt();
|
|
408
|
+
this.options.actions.register({
|
|
409
|
+
id: this.options.recoveryActionId,
|
|
410
|
+
sessionId,
|
|
411
|
+
owner: "provider-recovery",
|
|
412
|
+
kind: "retry",
|
|
413
|
+
state: "scheduled",
|
|
414
|
+
dueAt,
|
|
415
|
+
attempt,
|
|
416
|
+
maxAttempts: this.maxAttempts,
|
|
417
|
+
...(failure.provider ? { provider: failure.provider } : {}),
|
|
418
|
+
...(failure.code ? { code: failure.code } : {}),
|
|
419
|
+
reason: failure.reason,
|
|
420
|
+
});
|
|
421
|
+
const delayMs = Math.max(0, dueAt - this.now());
|
|
422
|
+
this.recoveryTimer = this.setTimer(() => {
|
|
423
|
+
this.recoveryTimer = null;
|
|
424
|
+
// The row leaves when the attempt runs: a countdown must not stay
|
|
425
|
+
// at 00:00.
|
|
426
|
+
this.options.actions.remove(this.options.recoveryActionId);
|
|
427
|
+
this.dispatchIfOwned(sessionId, "provider-recovery");
|
|
428
|
+
}, delayMs);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Start the timer of the overload resume.
|
|
433
|
+
*/
|
|
434
|
+
private startOverloadResume(): void {
|
|
435
|
+
const limit = this.options.overloadMaxAttempts ?? 3;
|
|
436
|
+
if (this.overloadAttempts >= limit) {
|
|
437
|
+
this.log(
|
|
438
|
+
`Provider overload resume not scheduled: ${this.overloadAttempts}/${limit} attempts used`,
|
|
439
|
+
);
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
this.cancelOverloadResume();
|
|
443
|
+
this.overloadAttempts += 1;
|
|
444
|
+
const delayMs = this.options.overloadDelayMs?.() ?? 60_000;
|
|
445
|
+
const sessionId = this.options.context().sessionId;
|
|
446
|
+
this.options.actions.register({
|
|
447
|
+
id: this.options.overloadActionId,
|
|
448
|
+
sessionId,
|
|
449
|
+
owner: "provider-overload",
|
|
450
|
+
kind: "retry",
|
|
451
|
+
state: "scheduled",
|
|
452
|
+
dueAt: this.now() + delayMs,
|
|
453
|
+
attempt: this.overloadAttempts,
|
|
454
|
+
reason: "the provider is overloaded",
|
|
455
|
+
});
|
|
456
|
+
this.overloadTimer = this.setTimer(() => {
|
|
457
|
+
this.overloadTimer = null;
|
|
458
|
+
this.options.actions.remove(this.options.overloadActionId);
|
|
459
|
+
this.dispatchIfOwned(sessionId, "provider-overload");
|
|
460
|
+
}, delayMs);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Cancel the pending attempt of the cycle.
|
|
465
|
+
*/
|
|
466
|
+
private cancelRecoveryAttempt(): void {
|
|
467
|
+
if (this.recoveryTimer !== null) {
|
|
468
|
+
this.clearTimer(this.recoveryTimer);
|
|
469
|
+
this.recoveryTimer = null;
|
|
470
|
+
}
|
|
471
|
+
this.options.actions.remove(this.options.recoveryActionId);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* Cancel the pending overload resume.
|
|
476
|
+
*/
|
|
477
|
+
private cancelOverloadResume(): void {
|
|
478
|
+
if (this.overloadTimer !== null) {
|
|
479
|
+
this.clearTimer(this.overloadTimer);
|
|
480
|
+
this.overloadTimer = null;
|
|
481
|
+
}
|
|
482
|
+
this.options.actions.remove(this.options.overloadActionId);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Dispatch a resume, but only while it still owns the session.
|
|
487
|
+
*
|
|
488
|
+
* The ownership is checked at the moment of the dispatch, not at the
|
|
489
|
+
* moment of the scheduling: the user may pause the work while the
|
|
490
|
+
* countdown runs.
|
|
491
|
+
*/
|
|
492
|
+
private dispatchIfOwned(sessionId: string, reason: string): void {
|
|
493
|
+
const decision = mayDispatchResume({
|
|
494
|
+
scheduledSessionId: sessionId,
|
|
495
|
+
current: this.options.context(),
|
|
496
|
+
});
|
|
497
|
+
if (decision.allowed === false) {
|
|
498
|
+
this.log(`Resume skipped (${reason}): ${decision.reason}`);
|
|
499
|
+
return;
|
|
500
|
+
}
|
|
501
|
+
void this.options.dispatch(reason);
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Write to the log of the runtime.
|
|
506
|
+
*/
|
|
507
|
+
private log(message: string): void {
|
|
508
|
+
this.options.log?.(message);
|
|
509
|
+
}
|
|
510
|
+
}
|