pi-harness-runtime 1.1.482 → 1.1.484

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.
Files changed (54) hide show
  1. package/CHANGELOG.md +121 -0
  2. package/harness/auto-quota-resume.ts +33 -3
  3. package/harness/glm-quota-countdown.ts +23 -1
  4. package/harness/pending-actions.ts +408 -0
  5. package/harness/provider-failure.ts +344 -0
  6. package/harness/provider-recovery.ts +351 -0
  7. package/index.ts +471 -0
  8. package/package.json +1 -1
  9. package/packages/agent-policy/package.json +1 -1
  10. package/packages/auth/package.json +1 -1
  11. package/packages/autonomous-runtime/package.json +1 -1
  12. package/packages/checkpoint/package.json +1 -1
  13. package/packages/clipboard/package.json +1 -1
  14. package/packages/codex-watcher/package.json +1 -1
  15. package/packages/config-capture/package.json +1 -1
  16. package/packages/contracts/package.json +1 -1
  17. package/packages/cookie-sanitizer/package.json +1 -1
  18. package/packages/evaluation-engine/package.json +1 -1
  19. package/packages/event-bus/package.json +1 -1
  20. package/packages/event-store/package.json +1 -1
  21. package/packages/experience-replay/package.json +1 -1
  22. package/packages/file-copy-helper/package.json +1 -1
  23. package/packages/intent-analyzer/package.json +1 -1
  24. package/packages/jev-judge/package.json +1 -1
  25. package/packages/knowledge-retrieval/package.json +1 -1
  26. package/packages/learning-engine/package.json +1 -1
  27. package/packages/logging/package.json +1 -1
  28. package/packages/memory-engine/package.json +1 -1
  29. package/packages/notification/package.json +1 -1
  30. package/packages/paperclip-integration/package.json +1 -1
  31. package/packages/performance-optimizer/package.json +1 -1
  32. package/packages/privilege-broker/package.json +1 -1
  33. package/packages/prompt-compiler/package.json +1 -1
  34. package/packages/providers/package.json +1 -1
  35. package/packages/qdrant-skills/package.json +1 -1
  36. package/packages/quota-manager/package.json +1 -1
  37. package/packages/relay/package.json +1 -1
  38. package/packages/skill-mcp-client/package.json +1 -1
  39. package/packages/skills/package.json +1 -1
  40. package/packages/todo-bd-sync/package.json +1 -1
  41. package/packages/token-estimation/package.json +1 -1
  42. package/packages/tooling-preference/package.json +1 -1
  43. package/packages/trajectory/package.json +1 -1
  44. package/packages/tui/dist/harness-footer.d.ts +10 -0
  45. package/packages/tui/dist/harness-footer.d.ts.map +1 -1
  46. package/packages/tui/dist/harness-footer.js +16 -1
  47. package/packages/tui/dist/harness-footer.js.map +1 -1
  48. package/packages/tui/package.json +1 -1
  49. package/packages/tui/src/harness-footer.ts +18 -1
  50. package/packages/two-phase-review/package.json +1 -1
  51. package/packages/types/package.json +1 -1
  52. package/packages/webmcp-tester/package.json +1 -1
  53. package/packages/wiki/package.json +1 -1
  54. package/packages/write-review/package.json +1 -1
package/index.ts CHANGED
@@ -55,6 +55,18 @@ import {
55
55
  PendingQuestionCountdown,
56
56
  type CountdownView,
57
57
  } from "./harness/pending-countdown.ts";
58
+ import {
59
+ createPendingActionRegistry,
60
+ createPendingActionTicker,
61
+ } from "./harness/pending-actions.ts";
62
+ import { classifyProviderFailure } from "./harness/provider-failure.ts";
63
+ import type { ProviderFailure } from "./harness/provider-failure.ts";
64
+ import {
65
+ createProviderRecoveryController,
66
+ MAX_RECOVERY_ATTEMPTS,
67
+ type RecoveryResetEvidence,
68
+ } from "./harness/provider-recovery.ts";
69
+ import { HARNESS_ACTIONS_STATUS_KEY } from "./packages/tui/dist/harness-footer.js";
58
70
  import { endPendingWait, releaseWait } from "./harness/pending-wait.ts";
59
71
  import { isChannelDisabled, setChannelDisabled } from "./harness/notification-settings.ts";
60
72
  import {
@@ -319,6 +331,191 @@ function keyFileFixesOrFallback(feature: string, labels: string[]): string[] {
319
331
  return fallbackKeyFileFixes(feature, labels, harnessKeysDir());
320
332
  }
321
333
 
334
+ /**
335
+ * The pending automatic actions of the session.
336
+ *
337
+ * The registry is the shared view of the automatic actions: every executor
338
+ * registers the deadline it scheduled, the rows are drawn from the
339
+ * registry, and nothing here dispatches an action. A row never claims a
340
+ * deadline that does not exist - an action without one shows its real state
341
+ * (queued, running, blocked).
342
+ */
343
+ const pendingActions = createPendingActionRegistry();
344
+
345
+ /**
346
+ * The session of the Pi runtime. The pending actions belong to it.
347
+ */
348
+ let activeSessionId = "";
349
+
350
+ /**
351
+ * Publishes the rows of the pending actions in the footer.
352
+ *
353
+ * The rows go into their own status key, so the harness footer renders them
354
+ * on their own row: a blocked provider stays visible next to the other
355
+ * statuses.
356
+ */
357
+ let setPendingActionsStatus: ((value: string | undefined) => void) | null = null;
358
+
359
+ /**
360
+ * The ticker of the rows of the pending actions.
361
+ *
362
+ * The ticker draws the rows once per second while an action is pending and
363
+ * stops itself when there is nothing to draw. A tick never dispatches an
364
+ * action, never sends a message and never notifies a channel.
365
+ */
366
+ const pendingActionTicker = createPendingActionTicker({
367
+ registry: pendingActions,
368
+ render: (rows) => {
369
+ setPendingActionsStatus?.(rows.join("\n"));
370
+ },
371
+ });
372
+
373
+ /** The identity of the pending action of the provider overload resume. */
374
+ const PROVIDER_OVERLOAD_ACTION_ID = "provider-overload-resume";
375
+
376
+ /** The identity of the pending action of the bounded provider recovery. */
377
+ const PROVIDER_RECOVERY_ACTION_ID = "provider-recovery";
378
+
379
+ /** The identity of the pending action of a provider that cannot be retried. */
380
+ const PROVIDER_BLOCKED_ACTION_ID = "provider-blocked";
381
+
382
+ /**
383
+ * The controller of the bounded recovery cycles.
384
+ *
385
+ * The controller decides the deadlines of the attempts; the executor that
386
+ * owns the resume stays the only owner of the execution.
387
+ */
388
+ const providerRecovery = createProviderRecoveryController();
389
+
390
+ /**
391
+ * The timer of the pending recovery attempt.
392
+ *
393
+ * It is the executor of the attempt, so it owns the deadline it shows.
394
+ */
395
+ let providerRecoveryTimer: ReturnType<typeof setTimeout> | null = null;
396
+
397
+ /**
398
+ * The counter of the failures that carry no identity.
399
+ *
400
+ * A failure without a response id still needs its own identity, so a copy
401
+ * of it cannot be mistaken for a later attempt.
402
+ */
403
+ let providerFailureCounter = 0;
404
+
405
+ /**
406
+ * Cancel a pending recovery attempt.
407
+ *
408
+ * The cycle ends with it: a cancelled attempt is not an attempt of the
409
+ * budget.
410
+ */
411
+ function clearProviderRecoveryAttempt(): void {
412
+ if (providerRecoveryTimer) {
413
+ clearTimeout(providerRecoveryTimer);
414
+ providerRecoveryTimer = null;
415
+ }
416
+ pendingActions.remove(PROVIDER_RECOVERY_ACTION_ID);
417
+ }
418
+
419
+ /**
420
+ * The identity of the pending action of the decision of the judge.
421
+ *
422
+ * The request is part of the identity: a question that replaces another one
423
+ * has its own row, and the row of the replaced question is not cleared by
424
+ * the stop of the new countdown.
425
+ */
426
+ function jevDecisionActionId(requestId: string): string {
427
+ return `jev-decision:${requestId}`;
428
+ }
429
+
430
+ /**
431
+ * The identity of the pending action of the resume of a job.
432
+ *
433
+ * The job is part of the identity: two jobs have two resumes.
434
+ */
435
+ function jobQuotaResumeActionId(jobId: string): string {
436
+ return `job-quota-resume:${jobId}`;
437
+ }
438
+
439
+ /**
440
+ * The identity of the pending action of the countdown of the GLM quota.
441
+ */
442
+ function glmQuotaActionId(jobId: string): string {
443
+ return `glm-quota:${jobId}`;
444
+ }
445
+
446
+ /**
447
+ * The identities of the continuations that have no scheduled deadline.
448
+ *
449
+ * The output limit and the compaction continue the work right away, so
450
+ * there is no countdown to show for them.
451
+ */
452
+ const OUTPUT_LIMIT_CONTINUATION_ID = "continuation:output-limit";
453
+ const POST_COMPACTION_CONTINUATION_ID = "continuation:post-compaction";
454
+
455
+ /**
456
+ * Register a continuation that runs right away.
457
+ *
458
+ * The row says that the continuation is queued. It never shows a
459
+ * countdown, because there is no deadline: a continuation that runs
460
+ * immediately has no deadline to wait for.
461
+ */
462
+ function registerQueuedContinuation(id: string, reason: string): void {
463
+ pendingActions.register({
464
+ id,
465
+ sessionId: activeSessionId,
466
+ owner: "continuation",
467
+ kind: "resume",
468
+ state: "queued",
469
+ reason,
470
+ });
471
+ }
472
+
473
+ // The countdown of the GLM quota owns the deadline of the reset, so the
474
+ // row shows the reset it really waits for. The row leaves when the
475
+ // countdown ends, so it never stays at 00:00.
476
+ getGLMQuotaCountdown().onTick((event) => {
477
+ const countdown = getGLMQuotaCountdown().getCountdown(event.jobId);
478
+ const resetAtEpoch =
479
+ countdown?.resetAtEpoch ?? Date.now() + event.remainingSeconds * 1000;
480
+ pendingActions.register({
481
+ id: glmQuotaActionId(event.jobId),
482
+ sessionId: activeSessionId,
483
+ owner: "glm-quota",
484
+ kind: "quota-check",
485
+ state: "scheduled",
486
+ dueAt: resetAtEpoch,
487
+ provider: "GLM",
488
+ reason: "the quota of GLM is exhausted",
489
+ });
490
+ });
491
+ getGLMQuotaCountdown().onEnd((jobId) => {
492
+ pendingActions.remove(glmQuotaActionId(jobId));
493
+ });
494
+
495
+ /**
496
+ * The observer of the scheduled job resumes.
497
+ *
498
+ * The job resume registers the deadline it really schedules, so the row
499
+ * shows the moment the resume runs and not the reset of the quota.
500
+ */
501
+ const quotaResumeObserver = {
502
+ scheduled(jobId: string, provider: string, resumeAt: number): void {
503
+ pendingActions.register({
504
+ id: jobQuotaResumeActionId(jobId),
505
+ sessionId: activeSessionId,
506
+ owner: "job-quota",
507
+ kind: "resume",
508
+ state: "scheduled",
509
+ dueAt: resumeAt,
510
+ provider,
511
+ reason: "the quota of the job is exhausted",
512
+ });
513
+ },
514
+ ended(jobId: string, _provider: string): void {
515
+ pendingActions.remove(jobQuotaResumeActionId(jobId));
516
+ },
517
+ };
518
+
322
519
  // Shared state for callback handlers (accessible across init functions)
323
520
  const sharedState = {
324
521
  /**
@@ -1185,6 +1382,19 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
1185
1382
  waitMinutes: MIN_WAIT_MINUTES,
1186
1383
  render: (view: CountdownView) => {
1187
1384
  _countdownRowRequestId = view.requestId;
1385
+ // The countdown owns the deadline of the decision, so the row
1386
+ // of the pending action shows the same deadline. The request
1387
+ // is part of the identity of the action: a question that
1388
+ // replaces another one has its own row.
1389
+ pendingActions.register({
1390
+ id: jevDecisionActionId(view.requestId),
1391
+ sessionId: _piSessionId,
1392
+ owner: "jev-decision",
1393
+ kind: "jev-decision",
1394
+ state: "scheduled",
1395
+ dueAt: Date.now() + view.remainingMs,
1396
+ reason: "the decision of the judge",
1397
+ });
1188
1398
  updateAutoContinueStatus(
1189
1399
  "WAITING",
1190
1400
  view.requestId.slice(0, 8),
@@ -1234,6 +1444,9 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
1234
1444
  // of that countdown is cleared: a row of another state - the
1235
1445
  // judge continues, for example - belongs to that state, and
1236
1446
  // the row of a newer question belongs to that question.
1447
+ if (requestId) {
1448
+ pendingActions.remove(jevDecisionActionId(requestId));
1449
+ }
1237
1450
  if (_countdownRowRequestId && requestId && _countdownRowRequestId !== requestId) {
1238
1451
  return;
1239
1452
  }
@@ -1370,9 +1583,18 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
1370
1583
  endWait("session-switched");
1371
1584
  }
1372
1585
  _piSessionId = sessionId;
1586
+ activeSessionId = sessionId;
1373
1587
  }
1374
1588
  if (ctx.hasUI && ctx.ui) {
1375
1589
  _setAutoContinueStatus = ctx.ui.setStatus.bind(ctx.ui, "harness-auto-continue");
1590
+ // The pending actions of the session get their own row of the
1591
+ // footer: a blocked provider stays visible next to the other
1592
+ // statuses.
1593
+ setPendingActionsStatus = ctx.ui.setStatus.bind(
1594
+ ctx.ui,
1595
+ HARNESS_ACTIONS_STATUS_KEY,
1596
+ );
1597
+ pendingActionTicker.start();
1376
1598
  // A session without a TUI collected the state already: it is
1377
1599
  // shown as soon as the UI is there.
1378
1600
  if (_currentAutoContinueState) {
@@ -1404,6 +1626,16 @@ async function initAutoContinue(pi: ExtensionAPI): Promise<void> {
1404
1626
  }
1405
1627
  _currentAutoContinueState = "";
1406
1628
 
1629
+ // The pending actions die with the session: a restart does not
1630
+ // inherit a countdown or a blocked provider.
1631
+ pendingActionTicker.stop();
1632
+ pendingActions.clear();
1633
+ providerRecovery.clear();
1634
+ clearProviderRecoveryAttempt();
1635
+ setPendingActionsStatus?.(undefined);
1636
+ setPendingActionsStatus = null;
1637
+ activeSessionId = "";
1638
+
1407
1639
  // Reset state
1408
1640
  _inferenceInProgress = false;
1409
1641
  });
@@ -6153,12 +6385,20 @@ Run \`bd ready\` to see current bd issues.
6153
6385
  }
6154
6386
  if (event.message.role !== "assistant") return;
6155
6387
 
6388
+ // A turn ran: a continuation that was queued is no longer queued.
6389
+ // The row leaves with it, so it never stays behind as a promise.
6390
+ pendingActions.removeWhere(
6391
+ (action) => action.owner === "continuation" && action.state === "queued",
6392
+ );
6393
+
6156
6394
  // DEBUG: Log model ID
6157
6395
 
6158
6396
  const m = event.message as {
6159
6397
  role?: string;
6160
6398
  stopReason?: unknown;
6161
6399
  errorMessage?: unknown;
6400
+ responseId?: string;
6401
+ provider?: string;
6162
6402
  content?: unknown;
6163
6403
  usage?: {
6164
6404
  input?: number;
@@ -6169,6 +6409,52 @@ Run \`bd ready\` to see current bd issues.
6169
6409
  };
6170
6410
  };
6171
6411
 
6412
+ // A provider failure is classified before anything else happens. The
6413
+ // classification reads the error of the turn; the text of an answer
6414
+ // is only read when it names a known provider failure, so a sentence
6415
+ // about an error is not a failure.
6416
+ const providerErrorText =
6417
+ typeof m.errorMessage === "string" ? m.errorMessage : "";
6418
+ const providerFailure = providerErrorText
6419
+ ? classifyProviderFailure(providerErrorText)
6420
+ : (() => {
6421
+ const fromText = classifyProviderFailure(readMessageText(event.message));
6422
+ return fromText && fromText.kind !== "unknown" ? fromText : null;
6423
+ })();
6424
+
6425
+ if (providerFailure) {
6426
+ const recoverable =
6427
+ providerFailure.recovery === "bounded-recovery" ||
6428
+ providerFailure.recovery === "retry-after";
6429
+ if (recoverable) {
6430
+ // The bounded recovery cycle decides the deadline of the next
6431
+ // attempt. The identity of the failure keeps a repeated copy
6432
+ // of the same error from starting a second cycle.
6433
+ let failureKey =
6434
+ typeof m.responseId === "string" && m.responseId.length > 0
6435
+ ? m.responseId
6436
+ : "";
6437
+ if (!failureKey) {
6438
+ providerFailureCounter += 1;
6439
+ failureKey = `${activeSessionId}:${providerFailureCounter}`;
6440
+ }
6441
+ scheduleProviderRecovery(providerFailure, failureKey);
6442
+ } else if (!providerFailure.retryable) {
6443
+ // No automatic retry can fix this failure: the row says that
6444
+ // nothing is scheduled.
6445
+ pendingActions.register({
6446
+ id: PROVIDER_BLOCKED_ACTION_ID,
6447
+ sessionId: activeSessionId,
6448
+ owner: "provider",
6449
+ kind: "retry",
6450
+ state: "blocked",
6451
+ ...(providerFailure.provider ? { provider: providerFailure.provider } : {}),
6452
+ ...(providerFailure.code ? { code: providerFailure.code } : {}),
6453
+ reason: providerFailure.reason,
6454
+ });
6455
+ }
6456
+ }
6457
+
6172
6458
  if (
6173
6459
  shouldQueueOutputLimitResume(
6174
6460
  m,
@@ -6179,6 +6465,10 @@ Run \`bd ready\` to see current bd issues.
6179
6465
  outputLimitResumeAttempts += 1;
6180
6466
  pendingOutputLimitResumeAfterCompact = true;
6181
6467
  pendingOutputLimitResumeAfterSettled = true;
6468
+ registerQueuedContinuation(
6469
+ OUTPUT_LIMIT_CONTINUATION_ID,
6470
+ "the output limit continues the turn",
6471
+ );
6182
6472
  await queueAutoResume("output-limit", OUTPUT_LIMIT_RESUME_PROMPT, "steer");
6183
6473
  // Emit OutputLimitContinued to Telegram
6184
6474
  await nc("OutputLimitContinued", {
@@ -6203,6 +6493,21 @@ Run \`bd ready\` to see current bd issues.
6203
6493
  }
6204
6494
  providerOverloadResumeAttempts = 0;
6205
6495
  clearProviderOverloadResume();
6496
+ // The turn completed: the provider works again. The recovery
6497
+ // cycle ends with it, so the next failure starts at the first
6498
+ // attempt, and a pending attempt of the cycle is cancelled.
6499
+ if (typeof m.provider === "string" && m.provider.length > 0) {
6500
+ providerRecovery.noteSuccess({
6501
+ sessionId: activeSessionId,
6502
+ provider: m.provider.toLowerCase(),
6503
+ });
6504
+ }
6505
+ clearProviderRecoveryAttempt();
6506
+ // The turn completed: a provider that was blocked is not blocked
6507
+ // any more. The row of the blocked provider leaves with it.
6508
+ pendingActions.removeWhere(
6509
+ (action) => action.owner === "provider" && action.state === "blocked",
6510
+ );
6206
6511
 
6207
6512
  // --- Auto-Continue: Check todos and keep going -------------------------------
6208
6513
  // If the agent stopped naturally AND the response mentions pending todos,
@@ -7211,6 +7516,7 @@ Run \`bd ready\` to see current bd issues.
7211
7516
  "minimax",
7212
7517
  currentSession.machine,
7213
7518
  mirrorStore,
7519
+ quotaResumeObserver,
7214
7520
  );
7215
7521
  ctx.ui.notify(
7216
7522
  `Job paused.\n` +
@@ -7598,6 +7904,10 @@ Run \`bd ready\` to see current bd issues.
7598
7904
  }
7599
7905
 
7600
7906
  if (forceOutputLimitResume) {
7907
+ registerQueuedContinuation(
7908
+ POST_COMPACTION_CONTINUATION_ID,
7909
+ "the compaction continues the turn",
7910
+ );
7601
7911
  await queueAutoResume(
7602
7912
  "post-compact-output-limit",
7603
7913
  OUTPUT_LIMIT_RESUME_PROMPT,
@@ -7607,6 +7917,10 @@ Run \`bd ready\` to see current bd issues.
7607
7917
  }
7608
7918
 
7609
7919
  outputLimitResumeAttempts = 0;
7920
+ registerQueuedContinuation(
7921
+ POST_COMPACTION_CONTINUATION_ID,
7922
+ "the compaction continues the turn",
7923
+ );
7610
7924
  // pi.dev expects the literal "resume" command to continue after compaction.
7611
7925
  await queueAutoResume("post-compact", "resume", "followUp");
7612
7926
  });
@@ -7633,6 +7947,146 @@ Run \`bd ready\` to see current bd issues.
7633
7947
  clearTimeout(providerOverloadResumeTimer);
7634
7948
  providerOverloadResumeTimer = null;
7635
7949
  }
7950
+ // The pending action goes with the timer: a cancelled resume has no
7951
+ // countdown any more.
7952
+ pendingActions.remove(PROVIDER_OVERLOAD_ACTION_ID);
7953
+ }
7954
+
7955
+ /**
7956
+ * The reset of the provider that applies to a failure.
7957
+ *
7958
+ * The reset only applies when it is fresh, when it belongs to the same
7959
+ * provider and to the same window as the failure, and when it lies in
7960
+ * the future. A failure that names no window has no window, so no reset
7961
+ * applies to it: the quota row is an observation, not a proof about the
7962
+ * limit that the provider reported.
7963
+ */
7964
+ function applicableResetEvidence(
7965
+ failure: ProviderFailure,
7966
+ ): RecoveryResetEvidence | undefined {
7967
+ if (!failure.window || !failure.provider) return undefined;
7968
+ const mirror = mirrorStore.readProvider(failure.provider.toLowerCase());
7969
+ if (!mirror) return undefined;
7970
+ if (mirrorStore.freshness(mirror, Date.now()) !== "fresh") return undefined;
7971
+ const resetAt =
7972
+ failure.window === "weekly"
7973
+ ? mirror.weekly_resets_at_epoch
7974
+ : failure.window === "h5"
7975
+ ? mirror.h5_resets_at_epoch
7976
+ : undefined;
7977
+ if (!resetAt || resetAt <= Date.now()) return undefined;
7978
+ const observedAt = Date.parse(mirror.synced_at);
7979
+ if (!Number.isFinite(observedAt)) return undefined;
7980
+ return {
7981
+ provider: failure.provider,
7982
+ window: failure.window,
7983
+ resetAt,
7984
+ observedAt,
7985
+ source: mirror.source,
7986
+ };
7987
+ }
7988
+
7989
+ /**
7990
+ * React to a provider failure that the bounded recovery may retry.
7991
+ *
7992
+ * The cycle decides the deadline of the attempt; the existing
7993
+ * auto-resume executor sends the resume, so there is no second resume
7994
+ * loop. The pending action shows the exact deadline the executor uses.
7995
+ */
7996
+ function scheduleProviderRecovery(
7997
+ failure: ProviderFailure,
7998
+ failureKey: string,
7999
+ ): void {
8000
+ const provider = failure.provider ?? "provider";
8001
+ const outcome = providerRecovery.noteFailure(
8002
+ {
8003
+ failureKey,
8004
+ sessionId: activeSessionId,
8005
+ // The cycle is keyed by the id of the provider, so a cycle of
8006
+ // "MiniMax" and a successful turn of "minimax" are the same
8007
+ // cycle.
8008
+ provider: provider.toLowerCase(),
8009
+ kind: failure.kind,
8010
+ ...(failure.window ? { window: failure.window } : {}),
8011
+ ...(failure.retryAfterMs !== undefined
8012
+ ? { retryAfterAt: Date.now() + failure.retryAfterMs }
8013
+ : {}),
8014
+ },
8015
+ applicableResetEvidence(failure),
8016
+ );
8017
+
8018
+ if (outcome.outcome === "scheduled") {
8019
+ logToFile(
8020
+ "[pi-harness]",
8021
+ `Provider recovery attempt ${outcome.attempt}/${MAX_RECOVERY_ATTEMPTS} at ${new Date(outcome.dueAt).toISOString()} (bound=${outcome.boundBy})`,
8022
+ );
8023
+ startProviderRecoveryAttempt(outcome.dueAt, outcome.attempt, failure);
8024
+ return;
8025
+ }
8026
+
8027
+ if (outcome.outcome === "blocked") {
8028
+ logToFile(
8029
+ "[pi-harness]",
8030
+ `Provider recovery exhausted after ${outcome.attempt} attempts`,
8031
+ );
8032
+ pendingActions.register({
8033
+ id: PROVIDER_BLOCKED_ACTION_ID,
8034
+ sessionId: activeSessionId,
8035
+ owner: "provider",
8036
+ kind: "retry",
8037
+ state: "blocked",
8038
+ ...(failure.provider ? { provider: failure.provider } : {}),
8039
+ ...(failure.code ? { code: failure.code } : {}),
8040
+ reason: `recovery attempts exhausted (${outcome.attempt}/${MAX_RECOVERY_ATTEMPTS})`,
8041
+ });
8042
+ return;
8043
+ }
8044
+
8045
+ // "already-scheduled" and "duplicate": the cycle does not move. The
8046
+ // row of the pending attempt is already there.
8047
+ logToFile(
8048
+ "[pi-harness]",
8049
+ `Provider recovery unchanged (${outcome.outcome}), attempt ${outcome.attempt}`,
8050
+ );
8051
+ }
8052
+
8053
+ /**
8054
+ * Start the timer of a recovery attempt.
8055
+ *
8056
+ * The timer is the executor, so it owns the deadline: the row of the
8057
+ * pending action shows the deadline the timer really uses.
8058
+ */
8059
+ function startProviderRecoveryAttempt(
8060
+ dueAt: number,
8061
+ attempt: number,
8062
+ failure: ProviderFailure,
8063
+ ): void {
8064
+ clearProviderRecoveryAttempt();
8065
+ pendingActions.register({
8066
+ id: PROVIDER_RECOVERY_ACTION_ID,
8067
+ sessionId: activeSessionId,
8068
+ owner: "provider-recovery",
8069
+ kind: "retry",
8070
+ state: "scheduled",
8071
+ dueAt,
8072
+ attempt,
8073
+ maxAttempts: MAX_RECOVERY_ATTEMPTS,
8074
+ ...(failure.provider ? { provider: failure.provider } : {}),
8075
+ ...(failure.code ? { code: failure.code } : {}),
8076
+ reason: failure.reason,
8077
+ });
8078
+ const delayMs = Math.max(0, dueAt - Date.now());
8079
+ providerRecoveryTimer = setTimeout(() => {
8080
+ providerRecoveryTimer = null;
8081
+ // The row leaves when the attempt runs: a countdown must not stay
8082
+ // at 00:00.
8083
+ pendingActions.remove(PROVIDER_RECOVERY_ACTION_ID);
8084
+ void queueAutoResume(
8085
+ "provider-recovery",
8086
+ PROVIDER_OVERLOAD_RESUME_PROMPT,
8087
+ "followUp",
8088
+ );
8089
+ }, delayMs);
7636
8090
  }
7637
8091
 
7638
8092
  function scheduleProviderOverloadResume(
@@ -7645,8 +8099,25 @@ Run \`bd ready\` to see current bd issues.
7645
8099
  "[pi-harness]",
7646
8100
  `Provider overloaded; scheduling resume in ${delayMinutes} min (${delayMs}ms) attempt=${providerOverloadResumeAttempts}`,
7647
8101
  );
8102
+ // The timer is the executor of the resume, so the timer owns the
8103
+ // deadline: the row of the pending action shows the deadline the
8104
+ // timer really uses, never another one.
8105
+ const dueAt = Date.now() + delayMs;
8106
+ pendingActions.register({
8107
+ id: PROVIDER_OVERLOAD_ACTION_ID,
8108
+ sessionId: activeSessionId,
8109
+ owner: "provider-overload",
8110
+ kind: "retry",
8111
+ state: "scheduled",
8112
+ dueAt,
8113
+ attempt: providerOverloadResumeAttempts,
8114
+ reason: "the provider is overloaded",
8115
+ });
7648
8116
  providerOverloadResumeTimer = setTimeout(() => {
7649
8117
  providerOverloadResumeTimer = null;
8118
+ // The action leaves the row when it fires: the countdown must
8119
+ // not stay at 00:00.
8120
+ pendingActions.remove(PROVIDER_OVERLOAD_ACTION_ID);
7650
8121
  logToFile("[pi-harness]", 'Provider resume timer fired, sending "resume"');
7651
8122
  if (hasPendingMessages()) {
7652
8123
  logToFile("[pi-harness]", "Resume skipped: hasPendingMessages=true");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-harness-runtime",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "[BETA] Codex-style /usage status + autonomous coding harness for pi. Not production ready — expect breaking changes.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi/agent-policy",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "Agent policy runtime for pi-harness - enforces mandatory rule delivery and mutation gating",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-harness/auth",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "pi-harness-runtime — auth",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi/autonomous-runtime",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "Autonomous Operations Runtime — task inbox, lease management, and worker lifecycle (RFC-0101)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi/checkpoint",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "Enhanced checkpoint system with incremental snapshots, diff-based storage, and recovery strategies",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi/clipboard",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "exports": {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-harness/codex-watcher",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "Watches Codex CLI sessions and emits visible messages for PING-PONG monitoring",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@moocoding/config-capture",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "Auto-capture API-fetched configuration values to AGENTS.md",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-harness/contracts",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "type": "module",
5
5
  "description": "RPC schema definitions for pi-harness runtime",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-harness/cookie-sanitizer",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "pi-harness-runtime — cookie sanitizer (provider-quota cookie file normaliser)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi-harness/evaluation-engine",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "description": "Evaluation engine for pi-harness runtime (RFC-0057)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pi/event-bus",
3
- "version": "1.1.482",
3
+ "version": "1.1.484",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "exports": {