oira666_pi-limits-wait 0.5.2 → 0.5.4
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/README.md +150 -97
- package/package.json +8 -5
- package/src/constants.ts +38 -38
- package/src/index.ts +36 -2
- package/src/models.ts +166 -155
- package/src/retry-errors.ts +263 -263
- package/src/retry-summary.ts +139 -0
- package/src/settings.ts +151 -130
- package/src/stream.ts +166 -29
- package/src/telemetry.ts +78 -0
- package/src/ui.ts +234 -179
package/src/stream.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ModelRuntime, type ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
2
|
import {
|
|
3
3
|
createAssistantMessageEventStream,
|
|
4
|
+
isContextOverflow,
|
|
4
5
|
type Api,
|
|
5
6
|
type AssistantMessage,
|
|
6
7
|
type AssistantMessageEvent,
|
|
@@ -28,9 +29,10 @@ import {
|
|
|
28
29
|
} from "./models.js";
|
|
29
30
|
import { withAttemptResponseObserver } from "./response-observer.js";
|
|
30
31
|
import { errorMessageWithCauses, getRetryableError, isAbortMessage, parseRetryDelayMs, retryAfterHeaderMs } from "./retry-errors.js";
|
|
32
|
+
import type { RetryPeriodTracker } from "./retry-summary.js";
|
|
31
33
|
import { loadFallbackSettings } from "./settings.js";
|
|
32
34
|
import { state } from "./state.js";
|
|
33
|
-
import type { FallbackModel, RetryableError, RuntimeStreamSimpleFn } from "./types.js";
|
|
35
|
+
import type { FallbackModel, RetryReason, RetryableError, RuntimeStreamSimpleFn } from "./types.js";
|
|
34
36
|
import { clearModelStatus, freezingEnabled, reasonLabel, waitForRetry } from "./ui.js";
|
|
35
37
|
|
|
36
38
|
export function __setNonRetryableTuningForTests(maxAttempts: number, retryDelayMs: number): void {
|
|
@@ -65,12 +67,10 @@ function freshMessage(model: Model<Api>): AssistantMessage {
|
|
|
65
67
|
};
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
function
|
|
70
|
+
function isExplicitAbortErrorEvent(event: AssistantMessageEvent, signal?: AbortSignal): boolean {
|
|
69
71
|
if (signal?.aborted) return true;
|
|
70
72
|
if (event.type !== "error") return false;
|
|
71
|
-
return event.reason === "aborted"
|
|
72
|
-
|| event.error.stopReason === "aborted"
|
|
73
|
-
|| isAbortMessage(event.error.errorMessage ?? "");
|
|
73
|
+
return event.reason === "aborted" || event.error.stopReason === "aborted";
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
function responseErrorMessage(response: ProviderResponse): string {
|
|
@@ -168,6 +168,7 @@ export function streamWithLimitsRetry(
|
|
|
168
168
|
context: Context,
|
|
169
169
|
options?: ModelsSimpleStreamOptions,
|
|
170
170
|
ownerSignal?: AbortSignal,
|
|
171
|
+
retryPeriod?: RetryPeriodTracker,
|
|
171
172
|
): AssistantMessageEventStream {
|
|
172
173
|
const output = createAssistantMessageEventStream();
|
|
173
174
|
const signal = options?.signal && ownerSignal
|
|
@@ -189,12 +190,37 @@ export function streamWithLimitsRetry(
|
|
|
189
190
|
const nonRetryableAttempts = new Map<string, number>();
|
|
190
191
|
const triedModels = new Set<string>();
|
|
191
192
|
|
|
193
|
+
const prepareAttemptForPi = async (): Promise<boolean> => {
|
|
194
|
+
const originalKey = modelKey(model);
|
|
195
|
+
const attemptKey = modelKey(attempt.model);
|
|
196
|
+
if (attemptKey === originalKey) return true;
|
|
197
|
+
const canSelect = (current: Model<Api> | undefined) => {
|
|
198
|
+
if (!current) return true;
|
|
199
|
+
const currentKey = modelKey(current);
|
|
200
|
+
// Never undo a newer, unrelated user model selection. Pi deliberately
|
|
201
|
+
// treats the resulting overflow as stale instead of compacting for it.
|
|
202
|
+
return currentKey === originalKey || currentKey === attemptKey;
|
|
203
|
+
};
|
|
204
|
+
if (!canSelect(state.sharedCtx?.model)) return false;
|
|
205
|
+
return switchPiModel(attempt, signal, canSelect);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const managedWait = async (reason: RetryReason, waitMs: number, error: string) =>
|
|
209
|
+
waitForRetry(reason, waitMs, signal, {
|
|
210
|
+
periodId: retryPeriod?.periodId,
|
|
211
|
+
model: attempt.model,
|
|
212
|
+
error,
|
|
213
|
+
events: retryPeriod?.events,
|
|
214
|
+
onEnd: (elapsedMs) => retryPeriod?.recordWait(elapsedMs),
|
|
215
|
+
});
|
|
216
|
+
|
|
192
217
|
const flush = (buffer: AssistantMessageEvent[]) => {
|
|
193
218
|
for (const event of buffer) output.push(event);
|
|
194
219
|
committed = true;
|
|
195
220
|
};
|
|
196
221
|
|
|
197
222
|
const pushAbort = (message: string) => {
|
|
223
|
+
retryPeriod?.finalize();
|
|
198
224
|
if (!committed) {
|
|
199
225
|
output.push({ type: "start", partial: freshMessage(attempt.model) });
|
|
200
226
|
committed = true;
|
|
@@ -281,8 +307,10 @@ export function streamWithLimitsRetry(
|
|
|
281
307
|
await priorOnResponse?.(response, responseModel);
|
|
282
308
|
if (signal?.aborted) return;
|
|
283
309
|
// Throw only after the canonical callback, so after_provider_response
|
|
284
|
-
// still sees
|
|
285
|
-
|
|
310
|
+
// still sees every HTTP failure. All statuses remain retry eligible:
|
|
311
|
+
// classified failures use their normal policy and 400/403 deliberately
|
|
312
|
+
// enter the configured unknown-error retry loop.
|
|
313
|
+
if (response.status >= 400) throw new Error(observedHttpError);
|
|
286
314
|
};
|
|
287
315
|
|
|
288
316
|
try {
|
|
@@ -315,14 +343,45 @@ export function streamWithLimitsRetry(
|
|
|
315
343
|
if (!committed) {
|
|
316
344
|
if (event.type === "error") {
|
|
317
345
|
const errMsg = enrichError(event.error.errorMessage ?? "", observedHttpError);
|
|
318
|
-
if (
|
|
346
|
+
if (isExplicitAbortErrorEvent(event, signal)) {
|
|
347
|
+
if (buffer.length > 0) flush(buffer);
|
|
348
|
+
pushAbort(errMsg || "Operation aborted");
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
const surfacedEvent = errMsg === (event.error.errorMessage ?? "")
|
|
352
|
+
? event
|
|
353
|
+
: { ...event, error: { ...event.error, errorMessage: errMsg } };
|
|
354
|
+
// Pi owns context-overflow recovery. Use Pi's canonical detector
|
|
355
|
+
// before this extension's message heuristics so mixed transport/
|
|
356
|
+
// abort wording cannot hide a real overflow from the host.
|
|
357
|
+
if (isContextOverflow(surfacedEvent.error, attempt.model.contextWindow)) {
|
|
358
|
+
// If this request had already moved to a fallback attempt,
|
|
359
|
+
// commit that attempted model when safe. Pi intentionally
|
|
360
|
+
// ignores overflow errors from a non-current model.
|
|
361
|
+
await prepareAttemptForPi();
|
|
362
|
+
if (signal?.aborted) {
|
|
363
|
+
pushAbort("Request aborted while switching model for context-overflow recovery.");
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
retryPeriod?.finalize();
|
|
367
|
+
if (buffer.length > 0) flush(buffer);
|
|
368
|
+
else {
|
|
369
|
+
output.push({ type: "start", partial: freshMessage(attempt.model) });
|
|
370
|
+
committed = true;
|
|
371
|
+
}
|
|
372
|
+
output.push(surfacedEvent);
|
|
373
|
+
finished = true;
|
|
374
|
+
output.end();
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
if (isAbortMessage(errMsg)) {
|
|
319
378
|
if (buffer.length > 0) flush(buffer);
|
|
320
379
|
pushAbort(errMsg || "Operation aborted");
|
|
321
380
|
return;
|
|
322
381
|
}
|
|
323
382
|
retryable = getRetryableError(errMsg);
|
|
324
383
|
if (retryable) retryableErrorMessage = errMsg;
|
|
325
|
-
else nonRetryableError = { message: errMsg, event };
|
|
384
|
+
else nonRetryableError = { message: errMsg, event: surfacedEvent };
|
|
326
385
|
break;
|
|
327
386
|
}
|
|
328
387
|
|
|
@@ -336,7 +395,7 @@ export function streamWithLimitsRetry(
|
|
|
336
395
|
pushAbort("Request aborted before switching model.");
|
|
337
396
|
return;
|
|
338
397
|
}
|
|
339
|
-
await
|
|
398
|
+
await prepareAttemptForPi();
|
|
340
399
|
if (signal?.aborted) {
|
|
341
400
|
pushAbort("Request aborted while switching model.");
|
|
342
401
|
return;
|
|
@@ -347,6 +406,7 @@ export function streamWithLimitsRetry(
|
|
|
347
406
|
output.push({ type: "start", partial: freshMessage(attempt.model) });
|
|
348
407
|
committed = true;
|
|
349
408
|
}
|
|
409
|
+
if (event.type === "done") retryPeriod?.finalize();
|
|
350
410
|
output.push(event);
|
|
351
411
|
if (event.type === "done") {
|
|
352
412
|
finished = true;
|
|
@@ -356,6 +416,8 @@ export function streamWithLimitsRetry(
|
|
|
356
416
|
continue;
|
|
357
417
|
}
|
|
358
418
|
|
|
419
|
+
if (event.type === "error") retryPeriod?.recordReason(event.error.errorMessage ?? "Provider stream error");
|
|
420
|
+
if (event.type === "done" || event.type === "error") retryPeriod?.finalize();
|
|
359
421
|
output.push(event);
|
|
360
422
|
if (event.type === "done" || event.type === "error") {
|
|
361
423
|
finished = true;
|
|
@@ -364,17 +426,61 @@ export function streamWithLimitsRetry(
|
|
|
364
426
|
}
|
|
365
427
|
}
|
|
366
428
|
});
|
|
367
|
-
await deliverObservedResponses();
|
|
368
|
-
if (finished) return;
|
|
369
|
-
} catch (caught) {
|
|
370
|
-
let err = caught;
|
|
371
429
|
try {
|
|
372
430
|
await deliverObservedResponses();
|
|
373
431
|
} catch (responseError) {
|
|
374
|
-
|
|
432
|
+
// A terminal provider event has already been surfaced. Observer
|
|
433
|
+
// callback failures must not start a hidden retry behind that event.
|
|
434
|
+
if (finished) return;
|
|
435
|
+
throw responseError;
|
|
436
|
+
}
|
|
437
|
+
if (finished) return;
|
|
438
|
+
} catch (caught) {
|
|
439
|
+
let err = caught;
|
|
440
|
+
const caughtMessage = errorMessageWithCauses(caught);
|
|
441
|
+
const caughtAssistant = freshMessage(attempt.model);
|
|
442
|
+
caughtAssistant.stopReason = "error";
|
|
443
|
+
caughtAssistant.errorMessage = caughtMessage;
|
|
444
|
+
const caughtIsOverflow = isContextOverflow(caughtAssistant, attempt.model.contextWindow);
|
|
445
|
+
if (caughtIsOverflow) {
|
|
446
|
+
// Response callbacks are observational here. Never let a slow or
|
|
447
|
+
// failing callback delay a provider overflow from reaching Pi.
|
|
448
|
+
void deliverObservedResponses().catch(() => undefined);
|
|
449
|
+
} else {
|
|
450
|
+
try {
|
|
451
|
+
await deliverObservedResponses();
|
|
452
|
+
} catch (responseError) {
|
|
453
|
+
err = responseError;
|
|
454
|
+
}
|
|
375
455
|
}
|
|
376
456
|
const errMsg = enrichError(errorMessageWithCauses(err), observedHttpError ?? errorHttpMetadata(err));
|
|
377
|
-
const
|
|
457
|
+
const explicitlyAborted = Boolean(signal?.aborted);
|
|
458
|
+
const error = freshMessage(attempt.model);
|
|
459
|
+
error.stopReason = "error";
|
|
460
|
+
error.errorMessage = errMsg;
|
|
461
|
+
// Synchronous provider throws do not include an AssistantMessage event,
|
|
462
|
+
// so build the same terminal shape Pi receives and apply Pi's detector
|
|
463
|
+
// before classifying network or message-based abort heuristics.
|
|
464
|
+
if (!explicitlyAborted && isContextOverflow(error, attempt.model.contextWindow)) {
|
|
465
|
+
await prepareAttemptForPi();
|
|
466
|
+
if (signal?.aborted) {
|
|
467
|
+
pushAbort("Request aborted while switching model for context-overflow recovery.");
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
retryPeriod?.finalize();
|
|
471
|
+
if (!committed) {
|
|
472
|
+
if (buffer.length > 0) flush(buffer);
|
|
473
|
+
else {
|
|
474
|
+
output.push({ type: "start", partial: freshMessage(attempt.model) });
|
|
475
|
+
committed = true;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
output.push({ type: "error", reason: "error", error });
|
|
479
|
+
finished = true;
|
|
480
|
+
output.end();
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
const aborted = explicitlyAborted || isAbortMessage(errMsg);
|
|
378
484
|
retryable = aborted ? undefined : getRetryableError(errMsg);
|
|
379
485
|
if (retryable) retryableErrorMessage = errMsg;
|
|
380
486
|
else if (aborted) {
|
|
@@ -394,6 +500,8 @@ export function streamWithLimitsRetry(
|
|
|
394
500
|
const failureEvent = nonRetryableError?.event;
|
|
395
501
|
const failureMessage = nonRetryableError?.message ?? "Provider stream ended without a terminal event.";
|
|
396
502
|
const emitFailure = () => {
|
|
503
|
+
retryPeriod?.recordReason(failureMessage);
|
|
504
|
+
retryPeriod?.finalize();
|
|
397
505
|
if (!committed) {
|
|
398
506
|
if (buffer.length > 0) flush(buffer);
|
|
399
507
|
else {
|
|
@@ -417,10 +525,13 @@ export function streamWithLimitsRetry(
|
|
|
417
525
|
nonRetryableAttempts.set(key, attempts);
|
|
418
526
|
if (attempts < state.nonRetryableMaxAttempts) {
|
|
419
527
|
notifyRetryingAfterError(attempt.model, state.nonRetryableRetryDelayMs, failureMessage);
|
|
420
|
-
|
|
528
|
+
retryPeriod?.beginRetry(failureMessage);
|
|
529
|
+
const wait = await managedWait("retry", state.nonRetryableRetryDelayMs, failureMessage);
|
|
530
|
+
if (wait === "aborted" || signal?.aborted) {
|
|
421
531
|
pushAbort("Request aborted while retrying after error.");
|
|
422
532
|
return;
|
|
423
533
|
}
|
|
534
|
+
retryPeriod?.completeRetry();
|
|
424
535
|
continue;
|
|
425
536
|
}
|
|
426
537
|
|
|
@@ -430,16 +541,19 @@ export function streamWithLimitsRetry(
|
|
|
430
541
|
rememberNonRetryableFailure(attempt.model, failureMessage);
|
|
431
542
|
const next = nextAvailableCandidate(attempt.model);
|
|
432
543
|
if (next) {
|
|
544
|
+
retryPeriod?.recordImmediateRetry(failureMessage);
|
|
433
545
|
attempt = next;
|
|
434
546
|
continue;
|
|
435
547
|
}
|
|
436
548
|
const deadline = earliestCandidateDeadline(attempt.model);
|
|
437
549
|
if (deadline) {
|
|
438
|
-
|
|
439
|
-
|
|
550
|
+
retryPeriod?.beginRetry(failureMessage);
|
|
551
|
+
const wait = await managedWait("model-frozen", Math.max(0, deadline - Date.now()), failureMessage);
|
|
552
|
+
if (wait === "aborted" || signal?.aborted) {
|
|
440
553
|
pushAbort("Request aborted while all fallback models were frozen.");
|
|
441
554
|
return;
|
|
442
555
|
}
|
|
556
|
+
retryPeriod?.completeRetry();
|
|
443
557
|
attempt = nextAvailableCandidate(attempt.model)
|
|
444
558
|
?? configuredAttempt(getPrimaryModel(attempt.model));
|
|
445
559
|
continue;
|
|
@@ -447,6 +561,7 @@ export function streamWithLimitsRetry(
|
|
|
447
561
|
} else {
|
|
448
562
|
const next = candidateOrder(attempt.model).find((entry) => !triedModels.has(modelKey(entry.model)));
|
|
449
563
|
if (next) {
|
|
564
|
+
retryPeriod?.recordImmediateRetry(failureMessage);
|
|
450
565
|
attempt = next;
|
|
451
566
|
continue;
|
|
452
567
|
}
|
|
@@ -460,37 +575,49 @@ export function streamWithLimitsRetry(
|
|
|
460
575
|
if (allowFallback) {
|
|
461
576
|
if (retryable.reason === "network") {
|
|
462
577
|
notifyRetryableError(attempt.model, retryable, retryableErrorMessage);
|
|
463
|
-
|
|
578
|
+
retryPeriod?.beginRetry(retryableErrorMessage);
|
|
579
|
+
const wait = await managedWait(retryable.reason, retryable.waitMs, retryableErrorMessage);
|
|
580
|
+
if (wait === "aborted" || signal?.aborted) {
|
|
464
581
|
pushAbort(`Request aborted during ${reasonLabel(retryable.reason).toLowerCase()} wait.`);
|
|
465
582
|
return;
|
|
466
583
|
}
|
|
584
|
+
retryPeriod?.completeRetry();
|
|
467
585
|
continue;
|
|
468
586
|
}
|
|
469
587
|
|
|
470
588
|
rememberRateLimit(attempt.model, retryable, retryableErrorMessage);
|
|
471
589
|
const next = nextAvailableCandidate(attempt.model);
|
|
472
590
|
if (next) {
|
|
591
|
+
retryPeriod?.recordImmediateRetry(retryableErrorMessage);
|
|
473
592
|
attempt = next;
|
|
474
593
|
continue;
|
|
475
594
|
}
|
|
476
595
|
const deadline = earliestCandidateDeadline(attempt.model);
|
|
477
596
|
const waitMs = deadline ? Math.max(0, deadline - Date.now()) : retryable.waitMs;
|
|
478
|
-
|
|
597
|
+
retryPeriod?.beginRetry(retryableErrorMessage);
|
|
598
|
+
const wait = await managedWait(retryable.reason, waitMs, retryableErrorMessage);
|
|
599
|
+
if (wait === "aborted" || signal?.aborted) {
|
|
479
600
|
pushAbort(`Request aborted during ${reasonLabel(retryable.reason).toLowerCase()} wait.`);
|
|
480
601
|
return;
|
|
481
602
|
}
|
|
603
|
+
retryPeriod?.completeRetry();
|
|
482
604
|
attempt = nextAvailableCandidate(attempt.model)
|
|
483
605
|
?? configuredAttempt(getPrimaryModel(attempt.model));
|
|
484
606
|
continue;
|
|
485
607
|
}
|
|
486
608
|
|
|
487
609
|
notifyRetryableError(attempt.model, retryable, retryableErrorMessage);
|
|
488
|
-
|
|
610
|
+
retryPeriod?.beginRetry(retryableErrorMessage);
|
|
611
|
+
const wait = await managedWait(retryable.reason, retryable.waitMs, retryableErrorMessage);
|
|
612
|
+
if (wait === "aborted" || signal?.aborted) {
|
|
489
613
|
pushAbort(`Request aborted during ${reasonLabel(retryable.reason).toLowerCase()} wait.`);
|
|
490
614
|
return;
|
|
491
615
|
}
|
|
616
|
+
retryPeriod?.completeRetry();
|
|
492
617
|
}
|
|
493
618
|
})().catch((error) => {
|
|
619
|
+
retryPeriod?.recordReason(errorMessageWithCauses(error));
|
|
620
|
+
retryPeriod?.finalize();
|
|
494
621
|
const failure = freshMessage(model);
|
|
495
622
|
failure.stopReason = signal?.aborted ? "aborted" : "error";
|
|
496
623
|
failure.errorMessage = errorMessageWithCauses(error);
|
|
@@ -526,23 +653,30 @@ interface InterceptionSlot {
|
|
|
526
653
|
type InterceptablePrototype = ModelRuntime & { [INTERCEPTION_SYMBOL]?: InterceptionSlot };
|
|
527
654
|
|
|
528
655
|
/** Install one stable, reload-safe process-wide trampoline. */
|
|
529
|
-
export function installModelRuntimeInterception(
|
|
656
|
+
export function installModelRuntimeInterception(
|
|
657
|
+
createRetryPeriod?: () => RetryPeriodTracker,
|
|
658
|
+
): () => boolean {
|
|
530
659
|
const prototype = ModelRuntime.prototype as InterceptablePrototype;
|
|
531
660
|
const owner = {};
|
|
532
661
|
const controller = new AbortController();
|
|
533
662
|
let slot = prototype[INTERCEPTION_SYMBOL];
|
|
534
663
|
|
|
535
|
-
const handler: InterceptionHandler = (runtime, model, context, options, delegate) =>
|
|
536
|
-
|
|
664
|
+
const handler: InterceptionHandler = (runtime, model, context, options, delegate) => {
|
|
665
|
+
let retryPeriod: RetryPeriodTracker | undefined;
|
|
666
|
+
try { retryPeriod = createRetryPeriod?.(); } catch { /* summary tracking is observational */ }
|
|
667
|
+
return streamWithLimitsRetry(runtime, delegate, model, context, options, controller.signal, retryPeriod);
|
|
668
|
+
};
|
|
537
669
|
|
|
538
670
|
if (slot && prototype.streamSimple === slot.wrapper) {
|
|
539
|
-
slot.abortOwner
|
|
671
|
+
const previousAbort = slot.abortOwner;
|
|
540
672
|
slot.owner = owner;
|
|
541
673
|
slot.abortOwner = () => controller.abort();
|
|
542
674
|
slot.handler = handler;
|
|
675
|
+
// Publish the new owner before abort telemetry can re-enter interception.
|
|
676
|
+
previousAbort?.();
|
|
543
677
|
} else {
|
|
678
|
+
const previousAbort = slot?.abortOwner;
|
|
544
679
|
if (slot) {
|
|
545
|
-
slot.abortOwner?.();
|
|
546
680
|
slot.owner = undefined;
|
|
547
681
|
slot.abortOwner = undefined;
|
|
548
682
|
slot.handler = undefined;
|
|
@@ -565,14 +699,17 @@ export function installModelRuntimeInterception(): () => boolean {
|
|
|
565
699
|
value: slot,
|
|
566
700
|
});
|
|
567
701
|
prototype.streamSimple = slot.wrapper;
|
|
702
|
+
previousAbort?.();
|
|
568
703
|
}
|
|
569
704
|
|
|
570
705
|
return () => {
|
|
571
706
|
if (slot?.owner !== owner) return false;
|
|
572
|
-
slot.abortOwner
|
|
707
|
+
const abortOwner = slot.abortOwner;
|
|
573
708
|
slot.owner = undefined;
|
|
574
709
|
slot.abortOwner = undefined;
|
|
575
710
|
slot.handler = undefined;
|
|
576
|
-
|
|
711
|
+
abortOwner?.();
|
|
712
|
+
// A re-entrant install during abort owns the shared slot now.
|
|
713
|
+
return slot.owner === undefined;
|
|
577
714
|
};
|
|
578
715
|
}
|
package/src/telemetry.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { EventBus } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { Api, Model } from "@earendil-works/pi-ai";
|
|
3
|
+
import type { RetryReason } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** Public, versioned pi.events channel for limits-wait wait telemetry. */
|
|
6
|
+
export const LIMITS_WAIT_EVENT_CHANNEL = "oira666.pi-limits-wait.wait.v1";
|
|
7
|
+
|
|
8
|
+
export type LimitsWaitModel = {
|
|
9
|
+
provider: string;
|
|
10
|
+
id: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type LimitsWaitWaitOutcome = "waited" | "skipped" | "aborted";
|
|
14
|
+
|
|
15
|
+
export type LimitsWaitWaitStartPayload = {
|
|
16
|
+
version: 1;
|
|
17
|
+
phase: "start";
|
|
18
|
+
waitId: string;
|
|
19
|
+
periodId?: string;
|
|
20
|
+
reason: RetryReason;
|
|
21
|
+
plannedDurationMs: number;
|
|
22
|
+
plannedDeadline: number;
|
|
23
|
+
startedAt: number;
|
|
24
|
+
model?: LimitsWaitModel;
|
|
25
|
+
error?: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type LimitsWaitWaitEndPayload = {
|
|
29
|
+
version: 1;
|
|
30
|
+
phase: "end";
|
|
31
|
+
waitId: string;
|
|
32
|
+
periodId?: string;
|
|
33
|
+
reason: RetryReason;
|
|
34
|
+
plannedDurationMs: number;
|
|
35
|
+
plannedDeadline: number;
|
|
36
|
+
startedAt: number;
|
|
37
|
+
endedAt: number;
|
|
38
|
+
actualElapsedMs: number;
|
|
39
|
+
outcome: LimitsWaitWaitOutcome;
|
|
40
|
+
model?: LimitsWaitModel;
|
|
41
|
+
error?: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type LimitsWaitWaitEventPayload = LimitsWaitWaitStartPayload | LimitsWaitWaitEndPayload;
|
|
45
|
+
|
|
46
|
+
// Concise public aliases; the longer names remain descriptive and compatible.
|
|
47
|
+
export type LimitsWaitStartEventPayload = LimitsWaitWaitStartPayload;
|
|
48
|
+
export type LimitsWaitEndEventPayload = LimitsWaitWaitEndPayload;
|
|
49
|
+
export type LimitsWaitEventPayload = LimitsWaitWaitEventPayload;
|
|
50
|
+
export type LimitsWaitOutcome = LimitsWaitWaitOutcome;
|
|
51
|
+
|
|
52
|
+
export type WaitTelemetryOptions = {
|
|
53
|
+
periodId?: string;
|
|
54
|
+
model?: Pick<Model<Api>, "provider" | "id">;
|
|
55
|
+
error?: string;
|
|
56
|
+
events?: EventBus;
|
|
57
|
+
onEnd?: (actualElapsedMs: number, outcome: LimitsWaitWaitOutcome) => void;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
let nextWaitId = 0;
|
|
61
|
+
|
|
62
|
+
export function createWaitId(): string {
|
|
63
|
+
nextWaitId = (nextWaitId + 1) % Number.MAX_SAFE_INTEGER;
|
|
64
|
+
return `wait-${Date.now().toString(36)}-${nextWaitId.toString(36)}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function waitModel(model: WaitTelemetryOptions["model"]): LimitsWaitModel | undefined {
|
|
68
|
+
return model ? { provider: model.provider, id: model.id } : undefined;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Event listeners are observational and must never alter retry control flow. */
|
|
72
|
+
export function emitWaitTelemetry(events: EventBus | undefined, payload: LimitsWaitWaitEventPayload): void {
|
|
73
|
+
try {
|
|
74
|
+
events?.emit(LIMITS_WAIT_EVENT_CHANNEL, payload);
|
|
75
|
+
} catch {
|
|
76
|
+
// Custom/test event buses may throw even though Pi's standard bus isolates listeners.
|
|
77
|
+
}
|
|
78
|
+
}
|