pi-harness-runtime 1.1.505 → 1.1.507

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 (81) hide show
  1. package/CHANGELOG.md +163 -0
  2. package/harness/provider-quota-reset.ts +125 -0
  3. package/harness/provider-recovery-runtime.ts +77 -2
  4. package/harness/quota-capture.ts +150 -0
  5. package/harness/quota-low-remaining.ts +75 -0
  6. package/harness/quota-reset-duration.ts +87 -0
  7. package/harness/quota-threshold.ts +117 -0
  8. package/harness/resume-scope.ts +85 -0
  9. package/harness/scope-expansion.ts +438 -0
  10. package/index.ts +213 -4
  11. package/mirror.ts +4 -0
  12. package/package.json +1 -1
  13. package/packages/agent-policy/package.json +1 -1
  14. package/packages/auth/package.json +1 -1
  15. package/packages/autonomous-runtime/package.json +1 -1
  16. package/packages/checkpoint/package.json +1 -1
  17. package/packages/clipboard/package.json +1 -1
  18. package/packages/codex-watcher/package.json +1 -1
  19. package/packages/config-capture/package.json +1 -1
  20. package/packages/contracts/package.json +1 -1
  21. package/packages/cookie-sanitizer/package.json +1 -1
  22. package/packages/evaluation-engine/package.json +1 -1
  23. package/packages/event-bus/package.json +1 -1
  24. package/packages/event-store/package.json +1 -1
  25. package/packages/experience-replay/package.json +1 -1
  26. package/packages/file-copy-helper/package.json +1 -1
  27. package/packages/intent-analyzer/package.json +1 -1
  28. package/packages/jev-judge/package.json +1 -1
  29. package/packages/knowledge-retrieval/package.json +1 -1
  30. package/packages/learning-engine/package.json +1 -1
  31. package/packages/logging/package.json +1 -1
  32. package/packages/memory-engine/package.json +1 -1
  33. package/packages/notification/dist/adapters/detail-format.d.ts +5 -2
  34. package/packages/notification/dist/adapters/detail-format.d.ts.map +1 -1
  35. package/packages/notification/dist/adapters/detail-format.js +17 -0
  36. package/packages/notification/dist/adapters/detail-format.js.map +1 -1
  37. package/packages/notification/dist/adapters/email-adapter.d.ts.map +1 -1
  38. package/packages/notification/dist/adapters/email-adapter.js +1 -0
  39. package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
  40. package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
  41. package/packages/notification/dist/adapters/line-adapter.js +1 -0
  42. package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
  43. package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
  44. package/packages/notification/dist/adapters/ntfy-adapter.js +1 -0
  45. package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
  46. package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
  47. package/packages/notification/dist/adapters/telegram-adapter.js +1 -0
  48. package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
  49. package/packages/notification/dist/notification-center.d.ts.map +1 -1
  50. package/packages/notification/dist/notification-center.js +10 -0
  51. package/packages/notification/dist/notification-center.js.map +1 -1
  52. package/packages/notification/dist/types.d.ts +14 -1
  53. package/packages/notification/dist/types.d.ts.map +1 -1
  54. package/packages/notification/package.json +1 -1
  55. package/packages/notification/src/adapters/detail-format.ts +29 -2
  56. package/packages/notification/src/adapters/email-adapter.ts +1 -0
  57. package/packages/notification/src/adapters/line-adapter.ts +1 -0
  58. package/packages/notification/src/adapters/ntfy-adapter.ts +1 -0
  59. package/packages/notification/src/adapters/telegram-adapter.ts +1 -0
  60. package/packages/notification/src/notification-center.ts +14 -0
  61. package/packages/notification/src/types.ts +14 -0
  62. package/packages/paperclip-integration/package.json +1 -1
  63. package/packages/performance-optimizer/package.json +1 -1
  64. package/packages/privilege-broker/package.json +1 -1
  65. package/packages/prompt-compiler/package.json +1 -1
  66. package/packages/providers/package.json +1 -1
  67. package/packages/qdrant-skills/package.json +1 -1
  68. package/packages/quota-manager/package.json +1 -1
  69. package/packages/relay/package.json +1 -1
  70. package/packages/skill-mcp-client/package.json +1 -1
  71. package/packages/skills/package.json +1 -1
  72. package/packages/todo-bd-sync/package.json +1 -1
  73. package/packages/token-estimation/package.json +1 -1
  74. package/packages/tooling-preference/package.json +1 -1
  75. package/packages/trajectory/package.json +1 -1
  76. package/packages/tui/package.json +1 -1
  77. package/packages/two-phase-review/package.json +1 -1
  78. package/packages/types/package.json +1 -1
  79. package/packages/webmcp-tester/package.json +1 -1
  80. package/packages/wiki/package.json +1 -1
  81. package/packages/write-review/package.json +1 -1
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The alert of a low allowance.
3
+ *
4
+ * An allowance that falls below ten percent is worth a message: the human
5
+ * can act before the window is empty. The message is sent once per
6
+ * crossing: a capture of the same state again is not a new crossing, and
7
+ * an allowance that recovers arms the alert again.
8
+ *
9
+ * The alert is not a decision and not a recovery: it needs no approval
10
+ * and it costs no attempt of the recovery cycle.
11
+ */
12
+
13
+ import {
14
+ LOW_REMAINING_THRESHOLD_PCT,
15
+ quotaWindowState,
16
+ } from "./quota-threshold.ts";
17
+
18
+ /** What the tracker says about an observation. */
19
+ export type QuotaAlertDecision =
20
+ /** The allowance is below the threshold and the alert was not sent yet. */
21
+ | "notify"
22
+ /** The allowance is below the threshold and the alert was sent already. */
23
+ | "silent"
24
+ /** The allowance recovered; the alert is armed again. */
25
+ | "rearmed"
26
+ /** Nothing is known about the allowance. */
27
+ | "none";
28
+
29
+ /**
30
+ * The state of the alert, per provider.
31
+ *
32
+ * The tracker remembers the providers whose alert was sent. The state is
33
+ * the state of the allowance, not of the capture: a capture that says
34
+ * nothing does not arm the alert again.
35
+ */
36
+ export class QuotaLowRemainingTracker {
37
+ private readonly thresholdPct: number;
38
+ private readonly alerted = new Set<string>();
39
+
40
+ constructor(thresholdPct: number = LOW_REMAINING_THRESHOLD_PCT) {
41
+ this.thresholdPct = thresholdPct;
42
+ }
43
+
44
+ /**
45
+ * Observe the used percentage of the five-hour window of a provider.
46
+ *
47
+ * The percentage is the used percentage of the mirror: the allowance
48
+ * that is left is its complement.
49
+ */
50
+ observe(
51
+ provider: string,
52
+ usedPct: number | undefined | null,
53
+ ): QuotaAlertDecision {
54
+ const key = provider.toLowerCase();
55
+ const state = quotaWindowState(usedPct);
56
+ if (state === "unknown") return "none";
57
+ if (state === "available") {
58
+ // The allowance recovered: the next crossing alerts again.
59
+ return this.alerted.delete(key) ? "rearmed" : "none";
60
+ }
61
+ if (this.alerted.has(key)) return "silent";
62
+ this.alerted.add(key);
63
+ return "notify";
64
+ }
65
+
66
+ /** Was the alert sent for this provider? */
67
+ isAlerted(provider: string): boolean {
68
+ return this.alerted.has(provider.toLowerCase());
69
+ }
70
+
71
+ /** The threshold of the alert, in percent of the remaining allowance. */
72
+ get threshold(): number {
73
+ return this.thresholdPct;
74
+ }
75
+ }
@@ -0,0 +1,87 @@
1
+ /**
2
+ * The duration of a quota reset, as the console of a provider writes it.
3
+ *
4
+ * The console of MiniMax shows the remaining time of a window as text
5
+ * ("Resets in 1 hr 42 min"). The mirror of the harness needs an absolute
6
+ * moment, because the recovery compares it with the clock when it decides
7
+ * when to continue.
8
+ *
9
+ * The conversion is anchored at the moment of the capture: the text
10
+ * describes the moment of the capture, so the epoch of the reset is the
11
+ * capture plus the duration. A text that is not a duration - a date, an
12
+ * empty field, a number without a unit - proves nothing and yields no
13
+ * epoch.
14
+ */
15
+
16
+ /** The milliseconds of every unit the console writes. */
17
+ const UNIT_MS: Record<string, number> = {
18
+ s: 1000,
19
+ sec: 1000,
20
+ secs: 1000,
21
+ second: 1000,
22
+ seconds: 1000,
23
+ m: 60_000,
24
+ min: 60_000,
25
+ mins: 60_000,
26
+ minute: 60_000,
27
+ minutes: 60_000,
28
+ h: 3_600_000,
29
+ hr: 3_600_000,
30
+ hrs: 3_600_000,
31
+ hour: 3_600_000,
32
+ hours: 3_600_000,
33
+ d: 86_400_000,
34
+ day: 86_400_000,
35
+ days: 86_400_000,
36
+ w: 604_800_000,
37
+ week: 604_800_000,
38
+ weeks: 604_800_000,
39
+ };
40
+
41
+ const DURATION_PATTERN =
42
+ /(\d+(?:\.\d+)?)\s*(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w)\b/gi;
43
+
44
+ /**
45
+ * The duration of a reset text, in milliseconds.
46
+ *
47
+ * The text may carry a prefix ("Resets in ...") and several units
48
+ * ("4 days 20 hr 42 min"). The units are summed. A text without a
49
+ * duration yields nothing.
50
+ */
51
+ export function parseResetDurationMs(
52
+ text: string | undefined | null,
53
+ ): number | undefined {
54
+ if (typeof text !== "string") return undefined;
55
+ const trimmed = text.trim();
56
+ if (!trimmed) return undefined;
57
+ let total = 0;
58
+ let found = false;
59
+ for (const match of trimmed.matchAll(DURATION_PATTERN)) {
60
+ const amount = Number.parseFloat(match[1]);
61
+ const unit = match[2].toLowerCase();
62
+ const unitMs = UNIT_MS[unit];
63
+ if (!Number.isFinite(amount) || unitMs === undefined) continue;
64
+ total += amount * unitMs;
65
+ found = true;
66
+ }
67
+ return found ? Math.round(total) : undefined;
68
+ }
69
+
70
+ /**
71
+ * The absolute epoch of a reset, read from the text of the console.
72
+ *
73
+ * The observation is the anchor: the text describes the state of the
74
+ * window at the moment of the capture.
75
+ */
76
+ export function resetEpochFromDuration(
77
+ text: string | undefined | null,
78
+ observedAtMs: number | undefined | null,
79
+ ): number | undefined {
80
+ if (typeof observedAtMs !== "number" || !Number.isFinite(observedAtMs)) {
81
+ return undefined;
82
+ }
83
+ const duration = parseResetDurationMs(text);
84
+ if (duration === undefined) return undefined;
85
+ const epoch = observedAtMs + duration;
86
+ return Number.isFinite(epoch) ? epoch : undefined;
87
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The threshold of the low-allowance policy.
3
+ *
4
+ * The console of a provider shows the percentage that is left ("5h: 0%
5
+ * left"), the mirror of the harness stores the percentage that is used.
6
+ * The two are complements, and the policy is stated in the remaining
7
+ * percentage, so the conversion and the thresholds live here and are used
8
+ * by name.
9
+ *
10
+ * The module is pure: it decides, it does not observe. The capture of the
11
+ * quota, the freshness of the capture and the scheduling of the recovery
12
+ * belong to the modules around it.
13
+ */
14
+
15
+ /** The remaining percentage below which the allowance of a window is low. */
16
+ export const LOW_REMAINING_THRESHOLD_PCT = 10;
17
+
18
+ /**
19
+ * A reset that is nearer than this is left to the bounded recovery cycle.
20
+ *
21
+ * The cycle of a failure waits 1, 4, 9 and 16 minutes. A reset that
22
+ * arrives within the hour is reached by that cycle, and spending the
23
+ * budget of the cycle on a reset that is about to happen would leave
24
+ * nothing for the failure that comes next.
25
+ */
26
+ export const RESET_HORIZON_MS = 60 * 60_000;
27
+
28
+ /** The remaining percentage of a used percentage. */
29
+ export function remainingPctFromUsedPct(usedPct: number): number {
30
+ return 100 - usedPct;
31
+ }
32
+
33
+ /** The used percentage of a remaining percentage. */
34
+ export function usedPctFromRemainingPct(remainingPct: number): number {
35
+ return 100 - remainingPct;
36
+ }
37
+
38
+ /** The state of one quota window. */
39
+ export type QuotaWindowState =
40
+ /** Nothing is known about the window. */
41
+ | "unknown"
42
+ /** The window still has its allowance. */
43
+ | "available"
44
+ /** The window is below the threshold, but not empty. */
45
+ | "low"
46
+ /** The window is empty. */
47
+ | "exhausted";
48
+
49
+ /**
50
+ * The state of a window, read from its used percentage.
51
+ *
52
+ * An unknown percentage is unknown and nothing else: it is not an empty
53
+ * window and it is not a window with an allowance.
54
+ */
55
+ export function quotaWindowState(
56
+ usedPct: number | undefined | null,
57
+ ): QuotaWindowState {
58
+ if (typeof usedPct !== "number" || !Number.isFinite(usedPct)) {
59
+ return "unknown";
60
+ }
61
+ const remaining = remainingPctFromUsedPct(usedPct);
62
+ if (remaining <= 0) return "exhausted";
63
+ if (remaining < LOW_REMAINING_THRESHOLD_PCT) return "low";
64
+ return "available";
65
+ }
66
+
67
+ /** Is the window below the threshold (nearly empty or empty)? */
68
+ export function isBelowLowThreshold(
69
+ usedPct: number | undefined | null,
70
+ ): boolean {
71
+ const state = quotaWindowState(usedPct);
72
+ return state === "low" || state === "exhausted";
73
+ }
74
+
75
+ /** Is the window completely empty? */
76
+ export function isFullyExhausted(
77
+ usedPct: number | undefined | null,
78
+ ): boolean {
79
+ return quotaWindowState(usedPct) === "exhausted";
80
+ }
81
+
82
+ /** The decision of the policy for one window. */
83
+ export type QuotaResetDecision =
84
+ /** Wait for the reset of the window. */
85
+ | "reset"
86
+ /** Keep the bounded recovery cycle. */
87
+ | "bounded";
88
+
89
+ /**
90
+ * Decide what one window means for a failed request.
91
+ *
92
+ * An empty window blocks the account until it resets, whatever the
93
+ * distance of the reset is: the bounded cycle cannot help, because every
94
+ * attempt of the cycle fails the same way.
95
+ *
96
+ * A window that is nearly empty blocks the account as well, but a reset
97
+ * that arrives within the hour is reached by the cycle, so the cycle
98
+ * keeps the failure.
99
+ */
100
+ export function decideQuotaReset(input: {
101
+ usedPct: number | undefined | null;
102
+ resetAt: number | undefined | null;
103
+ now: number;
104
+ }): QuotaResetDecision {
105
+ const state = quotaWindowState(input.usedPct);
106
+ if (state !== "exhausted" && state !== "low") return "bounded";
107
+ const resetAt = input.resetAt;
108
+ if (
109
+ typeof resetAt !== "number" ||
110
+ !Number.isFinite(resetAt) ||
111
+ resetAt <= input.now
112
+ ) {
113
+ return "bounded";
114
+ }
115
+ if (state === "exhausted") return "reset";
116
+ return resetAt - input.now > RESET_HORIZON_MS ? "reset" : "bounded";
117
+ }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * The scope of an automatic resume.
3
+ *
4
+ * An automatic resume continues the request that is running. It is not an
5
+ * instruction to find more work: a queued `resume` that arrives after the
6
+ * request is complete sends the agent to the backlog, and the backlog is
7
+ * not work of this request.
8
+ *
9
+ * The mechanical question - does authorized work remain? - belongs in code
10
+ * and not in the judge. The judge answers the other question: whether a
11
+ * concrete proposed scope may be taken on.
12
+ *
13
+ * The decision is deliberately conservative. An unknown state is not a
14
+ * completion: the uncertainty is preserved and the resume goes on, because
15
+ * stopping work that is unfinished is worse than asking again.
16
+ */
17
+
18
+ /** The state of the work of the request that is running. */
19
+ export type ResumeWorkState = "unfinished" | "complete" | "unknown";
20
+
21
+ /** What the guard does with an automatic resume. */
22
+ export type ResumeScopeAction = "continue" | "suppress";
23
+
24
+ export interface ResumeScopeInput {
25
+ /** The state of the work of the request that is running. */
26
+ work: ResumeWorkState;
27
+ /** The reason of the resume, for the report. */
28
+ reason: string;
29
+ }
30
+
31
+ export interface ResumeScopeDecision {
32
+ /** The action of the guard. */
33
+ action: ResumeScopeAction;
34
+ /** The reason of the decision, for the log. */
35
+ reason: string;
36
+ /** The line the user sees when the resume is suppressed. */
37
+ report?: string;
38
+ }
39
+
40
+ /**
41
+ * The rule that the agent keeps across a compaction.
42
+ *
43
+ * The rule is reinforcement, not enforcement: the guard in the code owns
44
+ * the decision, and this text tells the agent what the guard decided and
45
+ * why the backlog is not the next task.
46
+ */
47
+ export const RESUME_SCOPE_RULE = [
48
+ "An automatic resume continues only the current authorized request.",
49
+ "If that request and its required validation are finished, remain stopped.",
50
+ "Pending issues in bd or another backlog are not pending work of this request.",
51
+ "Propose any new scope separately; do not create executable todos for it",
52
+ "until the human or the configured Jev proposal policy authorizes it.",
53
+ ].join(" ");
54
+
55
+ /**
56
+ * The decision of the guard.
57
+ *
58
+ * - Work of the request remains: the resume goes on, and it continues that
59
+ * work only.
60
+ * - The work of the request is complete: the resume is suppressed. The
61
+ * agent is not told to find something else, and no backlog issue becomes
62
+ * work of this request.
63
+ * - The state is unknown: the resume goes on. The uncertainty is preserved
64
+ * - the guard does not declare a completion it cannot prove, and it does
65
+ * not substitute unrelated work either.
66
+ */
67
+ export function decideResumeScope(input: ResumeScopeInput): ResumeScopeDecision {
68
+ if (input.work === "complete") {
69
+ return {
70
+ action: "suppress",
71
+ reason: "the work of the request is complete",
72
+ report: `The work of the request is complete; the automatic resume (${input.reason}) is not sent.`,
73
+ };
74
+ }
75
+ if (input.work === "unknown") {
76
+ return {
77
+ action: "continue",
78
+ reason: "the state of the work is unknown; the uncertainty is preserved",
79
+ };
80
+ }
81
+ return {
82
+ action: "continue",
83
+ reason: "the work of the request is unfinished",
84
+ };
85
+ }