pi-harness-runtime 1.1.509 → 1.1.511
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 +177 -0
- package/harness/local-refusal-marker.ts +104 -0
- package/harness/mirror-record-merge.ts +64 -0
- package/harness/optional-event.ts +27 -0
- package/harness/provider-recovery-runtime.ts +143 -4
- package/harness/weekly-exhaustion-registry.ts +178 -0
- package/harness/weekly-quota-exhaustion.ts +172 -0
- package/index.ts +420 -3
- 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/auto-continue.d.ts.map +1 -1
- package/packages/jev-judge/dist/auto-continue.js +11 -1
- package/packages/jev-judge/dist/auto-continue.js.map +1 -1
- package/packages/jev-judge/dist/question-risk.d.ts +8 -0
- package/packages/jev-judge/dist/question-risk.d.ts.map +1 -1
- package/packages/jev-judge/dist/question-risk.js +17 -0
- package/packages/jev-judge/dist/question-risk.js.map +1 -1
- package/packages/jev-judge/package.json +1 -1
- package/packages/jev-judge/src/auto-continue.ts +13 -1
- package/packages/jev-judge/src/question-risk.ts +19 -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/dist/adapters/email-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/email-adapter.js +1 -0
- package/packages/notification/dist/adapters/email-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/line-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/line-adapter.js +1 -0
- package/packages/notification/dist/adapters/line-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/ntfy-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/ntfy-adapter.js +1 -0
- package/packages/notification/dist/adapters/ntfy-adapter.js.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.d.ts.map +1 -1
- package/packages/notification/dist/adapters/telegram-adapter.js +1 -0
- package/packages/notification/dist/adapters/telegram-adapter.js.map +1 -1
- package/packages/notification/dist/notification-center.d.ts.map +1 -1
- package/packages/notification/dist/notification-center.js +6 -0
- package/packages/notification/dist/notification-center.js.map +1 -1
- package/packages/notification/dist/pending-question.d.ts +69 -0
- package/packages/notification/dist/pending-question.d.ts.map +1 -1
- package/packages/notification/dist/pending-question.js +211 -1
- package/packages/notification/dist/pending-question.js.map +1 -1
- package/packages/notification/dist/telegram-webhook-handler.d.ts +14 -0
- package/packages/notification/dist/telegram-webhook-handler.d.ts.map +1 -1
- package/packages/notification/dist/telegram-webhook-handler.js +14 -0
- package/packages/notification/dist/telegram-webhook-handler.js.map +1 -1
- package/packages/notification/dist/types.d.ts +9 -1
- package/packages/notification/dist/types.d.ts.map +1 -1
- package/packages/notification/package.json +1 -1
- package/packages/notification/src/adapters/email-adapter.ts +1 -0
- package/packages/notification/src/adapters/line-adapter.ts +1 -0
- package/packages/notification/src/adapters/ntfy-adapter.ts +1 -0
- package/packages/notification/src/adapters/telegram-adapter.ts +1 -0
- package/packages/notification/src/notification-center.ts +6 -0
- package/packages/notification/src/pending-question.ts +259 -1
- package/packages/notification/src/telegram-webhook-handler.ts +14 -0
- package/packages/notification/src/types.ts +8 -0
- package/packages/paperclip-integration/package.json +1 -1
- package/packages/performance-optimizer/package.json +1 -1
- package/packages/privilege-broker/package.json +1 -1
- package/packages/prompt-compiler/package.json +1 -1
- package/packages/providers/package.json +1 -1
- package/packages/qdrant-skills/package.json +1 -1
- package/packages/quota-manager/package.json +1 -1
- package/packages/relay/package.json +1 -1
- package/packages/skill-mcp-client/package.json +1 -1
- package/packages/skills/package.json +1 -1
- package/packages/todo-bd-sync/package.json +1 -1
- package/packages/token-estimation/package.json +1 -1
- package/packages/tooling-preference/package.json +1 -1
- package/packages/trajectory/package.json +1 -1
- package/packages/tui/package.json +1 -1
- package/packages/two-phase-review/package.json +1 -1
- package/packages/types/package.json +1 -1
- package/packages/webmcp-tester/package.json +1 -1
- package/packages/wiki/package.json +1 -1
- package/packages/write-review/package.json +1 -1
- package/proactive-compact.ts +21 -0
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The decision of the human about an empty weekly allowance.
|
|
3
|
+
*
|
|
4
|
+
* The weekly allowance of a provider blocks the work until the human
|
|
5
|
+
* decides what happens. There are two choices:
|
|
6
|
+
*
|
|
7
|
+
* - the harness counts down to the reset of the week and continues then,
|
|
8
|
+
* - the human deals with the account itself.
|
|
9
|
+
*
|
|
10
|
+
* The decision belongs to the human alone. The judge is never asked, and
|
|
11
|
+
* there is no timeout that decides: a silence keeps the work paused. The
|
|
12
|
+
* registry therefore holds the decision, the delivery of the message and
|
|
13
|
+
* the identity of the answer - and it holds the first answer, because a
|
|
14
|
+
* second click on an old button must not change a decision that was made.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** The choices of the human. */
|
|
18
|
+
export type WeeklyDecision = "countdown" | "manual";
|
|
19
|
+
|
|
20
|
+
/** The channel a decision came from. */
|
|
21
|
+
export type WeeklyDecisionChannel = "telegram" | "line";
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* One exhausted weekly allowance.
|
|
25
|
+
*/
|
|
26
|
+
export interface WeeklyExhaustionRecord {
|
|
27
|
+
/** The canonical id of the decision. */
|
|
28
|
+
requestId: string;
|
|
29
|
+
/** The provider whose weekly allowance is empty. */
|
|
30
|
+
provider: string;
|
|
31
|
+
/** The absolute epoch of the weekly reset. */
|
|
32
|
+
resetAt: number;
|
|
33
|
+
/** When the decision was requested. */
|
|
34
|
+
createdAt: number;
|
|
35
|
+
/** Did the message reach the user? */
|
|
36
|
+
delivered: boolean;
|
|
37
|
+
/** The choice of the human. */
|
|
38
|
+
decision?: WeeklyDecision;
|
|
39
|
+
/** When the human decided. */
|
|
40
|
+
decidedAt?: number;
|
|
41
|
+
/** The channel the decision came from. */
|
|
42
|
+
channel?: WeeklyDecisionChannel;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The result of a claim of the registry.
|
|
47
|
+
*
|
|
48
|
+
* The shape is flat on purpose: the harness compiles the strict and the
|
|
49
|
+
* lenient variant of TypeScript, and the narrowing of a discriminated
|
|
50
|
+
* union is not available in both of them.
|
|
51
|
+
*/
|
|
52
|
+
export interface WeeklyClaimResult {
|
|
53
|
+
/** Did the human take the decision? */
|
|
54
|
+
ok: boolean;
|
|
55
|
+
/** The decision that was taken. */
|
|
56
|
+
record?: WeeklyExhaustionRecord;
|
|
57
|
+
/** Why the decision was refused. */
|
|
58
|
+
reason?: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The registry of the weekly decisions.
|
|
63
|
+
*
|
|
64
|
+
* One entry per exhausted weekly allowance. The entry is registered
|
|
65
|
+
* before the message is delivered, so that a click that arrives before
|
|
66
|
+
* the delivery is acknowledged still finds its decision; and it is
|
|
67
|
+
* removed when the delivery fails, because a decision nobody can reach
|
|
68
|
+
* would wait for an answer that never comes.
|
|
69
|
+
*/
|
|
70
|
+
export class WeeklyExhaustionRegistry {
|
|
71
|
+
private readonly records = new Map<string, WeeklyExhaustionRecord>();
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Register the decision of an exhausted weekly allowance.
|
|
75
|
+
*
|
|
76
|
+
* A decision that is already registered is kept: the second
|
|
77
|
+
* registration of the same exhaustion is the same decision.
|
|
78
|
+
*/
|
|
79
|
+
register(params: {
|
|
80
|
+
requestId: string;
|
|
81
|
+
provider: string;
|
|
82
|
+
resetAt: number;
|
|
83
|
+
now: number;
|
|
84
|
+
}): WeeklyExhaustionRecord {
|
|
85
|
+
const existing = this.records.get(params.requestId);
|
|
86
|
+
if (existing) return existing;
|
|
87
|
+
const record: WeeklyExhaustionRecord = {
|
|
88
|
+
requestId: params.requestId,
|
|
89
|
+
provider: params.provider,
|
|
90
|
+
resetAt: params.resetAt,
|
|
91
|
+
createdAt: params.now,
|
|
92
|
+
delivered: false,
|
|
93
|
+
};
|
|
94
|
+
this.records.set(params.requestId, record);
|
|
95
|
+
return record;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The decision of an id. */
|
|
99
|
+
get(requestId: string): WeeklyExhaustionRecord | undefined {
|
|
100
|
+
return this.records.get(requestId);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Every decision that waits for an answer. */
|
|
104
|
+
get pending(): WeeklyExhaustionRecord[] {
|
|
105
|
+
return [...this.records.values()].filter(
|
|
106
|
+
(record) => record.decision === undefined,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Did the message of the decision reach the user? */
|
|
111
|
+
markDelivered(requestId: string): boolean {
|
|
112
|
+
const record = this.records.get(requestId);
|
|
113
|
+
if (!record) return false;
|
|
114
|
+
record.delivered = true;
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Take the decision of the human.
|
|
120
|
+
*
|
|
121
|
+
* The first answer wins: a second answer for the same decision is
|
|
122
|
+
* stale, and the reason says so. An answer for a decision that does
|
|
123
|
+
* not exist is stale as well - the button is older than the session.
|
|
124
|
+
*/
|
|
125
|
+
claim(
|
|
126
|
+
requestId: string,
|
|
127
|
+
decision: WeeklyDecision,
|
|
128
|
+
channel: WeeklyDecisionChannel,
|
|
129
|
+
now: number,
|
|
130
|
+
): WeeklyClaimResult {
|
|
131
|
+
const record = this.records.get(requestId);
|
|
132
|
+
if (!record) {
|
|
133
|
+
return { ok: false, reason: "the decision does not exist any more" };
|
|
134
|
+
}
|
|
135
|
+
if (record.decision !== undefined) {
|
|
136
|
+
return {
|
|
137
|
+
ok: false,
|
|
138
|
+
reason: `the decision was already made (${record.decision})`,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
record.decision = decision;
|
|
142
|
+
record.decidedAt = now;
|
|
143
|
+
record.channel = channel;
|
|
144
|
+
return { ok: true, record };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Remove a decision. */
|
|
148
|
+
remove(requestId: string): boolean {
|
|
149
|
+
return this.records.delete(requestId);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Remove every decision. */
|
|
153
|
+
clear(): void {
|
|
154
|
+
this.records.clear();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The canonical id of a weekly decision.
|
|
160
|
+
*
|
|
161
|
+
* The id travels through the callback data of a button, and that data is
|
|
162
|
+
* split on the underscore: an id with an underscore would be cut in two.
|
|
163
|
+
* The id therefore holds no underscore, and it is short enough for the
|
|
164
|
+
* postback data of LINE.
|
|
165
|
+
*/
|
|
166
|
+
export function buildWeeklyRequestId(
|
|
167
|
+
provider: string,
|
|
168
|
+
resetAt: number,
|
|
169
|
+
): string {
|
|
170
|
+
const key = `${provider.toLowerCase()}:${resetAt}`;
|
|
171
|
+
let hash = 2166136261;
|
|
172
|
+
for (let i = 0; i < key.length; i += 1) {
|
|
173
|
+
hash ^= key.charCodeAt(i);
|
|
174
|
+
hash = Math.imul(hash, 16777619);
|
|
175
|
+
}
|
|
176
|
+
const hex = (hash >>> 0).toString(16).padStart(8, "0");
|
|
177
|
+
return `week-${provider.toLowerCase()}-${hex}`;
|
|
178
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The exhaustion of the weekly allowance of a provider.
|
|
3
|
+
*
|
|
4
|
+
* The weekly window is not the five-hour window: it lasts days, and no
|
|
5
|
+
* bounded cycle of retries reaches its end. A weekly allowance that is
|
|
6
|
+
* empty therefore blocks the account until the week resets, and that is a
|
|
7
|
+
* decision of the human: the harness may count down to the reset, or the
|
|
8
|
+
* human may deal with the account itself. Until the human chooses, the
|
|
9
|
+
* work stays paused.
|
|
10
|
+
*
|
|
11
|
+
* The evidence is the mirror of the harness, and it has to be fresh: a row
|
|
12
|
+
* of the mirror that is hours old describes a week that may already have
|
|
13
|
+
* been paid for. The selector therefore reads the row that the harness
|
|
14
|
+
* already has - it never scrapes again - and it refuses a row that is not
|
|
15
|
+
* fresh, not for this provider, or not complete.
|
|
16
|
+
*
|
|
17
|
+
* The module is pure: it decides, it does not observe, notify or
|
|
18
|
+
* schedule.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
import type { ProviderMirrorRecord } from "../mirror.ts";
|
|
22
|
+
import type { ProviderFailure } from "./provider-failure.ts";
|
|
23
|
+
import {
|
|
24
|
+
MAX_RESET_EVIDENCE_AGE_MS,
|
|
25
|
+
normalizeProviderIdentity,
|
|
26
|
+
} from "./provider-recovery.ts";
|
|
27
|
+
import {
|
|
28
|
+
quotaWindowState,
|
|
29
|
+
remainingPctFromUsedPct,
|
|
30
|
+
} from "./quota-threshold.ts";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The evidence of an empty weekly allowance.
|
|
34
|
+
*/
|
|
35
|
+
export interface WeeklyQuotaExhaustion {
|
|
36
|
+
/** The provider whose weekly allowance is empty. */
|
|
37
|
+
provider: string;
|
|
38
|
+
/** The absolute epoch of the weekly reset. */
|
|
39
|
+
resetAt: number;
|
|
40
|
+
/** When the evidence was observed. */
|
|
41
|
+
observedAt: number;
|
|
42
|
+
/** The remaining percentage of the weekly window (zero when it is empty). */
|
|
43
|
+
remainingPct: number;
|
|
44
|
+
/** Where the evidence came from. */
|
|
45
|
+
source: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The names the providers are known by in a message of the harness.
|
|
50
|
+
*
|
|
51
|
+
* The provider writes its own name ("MiniMax"), and the id of the mirror
|
|
52
|
+
* is lower case. A message about the account of the user should carry the
|
|
53
|
+
* name the user knows.
|
|
54
|
+
*/
|
|
55
|
+
const DISPLAY_NAMES: Record<string, string> = {
|
|
56
|
+
minimax: "MiniMax",
|
|
57
|
+
glm: "GLM",
|
|
58
|
+
openai: "OpenAI",
|
|
59
|
+
anthropic: "Anthropic",
|
|
60
|
+
openrouter: "OpenRouter",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The name of a provider for a message of the harness.
|
|
65
|
+
*/
|
|
66
|
+
export function providerDisplayName(provider: string): string {
|
|
67
|
+
const key = normalizeProviderIdentity(provider);
|
|
68
|
+
return DISPLAY_NAMES[key] ?? provider;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Is the weekly allowance of the provider empty?
|
|
73
|
+
*
|
|
74
|
+
* The decision needs three facts, and every one of them has to be there:
|
|
75
|
+
*
|
|
76
|
+
* - the failure is a failure of the quota (an exhausted plan or a rate
|
|
77
|
+
* limit). A rejected credential or an overload says nothing about the
|
|
78
|
+
* weekly allowance,
|
|
79
|
+
* - the row of the mirror is for this provider and is fresh, and
|
|
80
|
+
* - the weekly window of that row is empty and has a reset in the future.
|
|
81
|
+
*
|
|
82
|
+
* A weekly window that is empty but has no usable reset yields nothing:
|
|
83
|
+
* without a moment to wait for, the harness cannot offer a countdown, and
|
|
84
|
+
* the ordinary recovery of the quota keeps its own rules.
|
|
85
|
+
*/
|
|
86
|
+
export function weeklyQuotaExhausted(input: {
|
|
87
|
+
/** The classified failure of the provider. */
|
|
88
|
+
failure: ProviderFailure;
|
|
89
|
+
/** The provider that rejected the request. */
|
|
90
|
+
provider: string;
|
|
91
|
+
/** The row of the mirror, when there is one. */
|
|
92
|
+
mirror: ProviderMirrorRecord | null | undefined;
|
|
93
|
+
/** The clock of the decision. */
|
|
94
|
+
now: number;
|
|
95
|
+
}): WeeklyQuotaExhaustion | undefined {
|
|
96
|
+
const { failure, provider, mirror, now } = input;
|
|
97
|
+
if (failure.kind !== "usage-exhausted" && failure.kind !== "rate-limit") {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
if (!mirror) return undefined;
|
|
101
|
+
if (
|
|
102
|
+
normalizeProviderIdentity(mirror.provider) !==
|
|
103
|
+
normalizeProviderIdentity(provider)
|
|
104
|
+
) {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
const observedAt = Date.parse(mirror.synced_at);
|
|
108
|
+
if (
|
|
109
|
+
!Number.isFinite(observedAt) ||
|
|
110
|
+
observedAt > now ||
|
|
111
|
+
now - observedAt > MAX_RESET_EVIDENCE_AGE_MS
|
|
112
|
+
) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
if (quotaWindowState(mirror.weekly_used_pct) !== "exhausted") {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
const resetAt = mirror.weekly_resets_at_epoch;
|
|
119
|
+
if (typeof resetAt !== "number" || !Number.isFinite(resetAt) || resetAt <= now) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
provider: normalizeProviderIdentity(provider),
|
|
124
|
+
resetAt,
|
|
125
|
+
observedAt,
|
|
126
|
+
remainingPct: remainingPctFromUsedPct(mirror.weekly_used_pct as number),
|
|
127
|
+
source: mirror.source,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* The duration until a reset, in the words of the console.
|
|
133
|
+
*
|
|
134
|
+
* The console of a provider writes the remaining time of a window as
|
|
135
|
+
* "2 days 7 hours 58 minutes", and the harness writes it the same way.
|
|
136
|
+
* The unit that is missing is left out, and a duration below a minute is
|
|
137
|
+
* written in seconds.
|
|
138
|
+
*/
|
|
139
|
+
export function formatResetDuration(ms: number): string {
|
|
140
|
+
const total = Math.max(0, Math.round(ms));
|
|
141
|
+
const days = Math.floor(total / 86_400_000);
|
|
142
|
+
const hours = Math.floor((total % 86_400_000) / 3_600_000);
|
|
143
|
+
const minutes = Math.floor((total % 3_600_000) / 60_000);
|
|
144
|
+
const seconds = Math.floor((total % 60_000) / 1000);
|
|
145
|
+
const parts: string[] = [];
|
|
146
|
+
if (days > 0) parts.push(`${days} ${days === 1 ? "day" : "days"}`);
|
|
147
|
+
if (hours > 0) parts.push(`${hours} ${hours === 1 ? "hour" : "hours"}`);
|
|
148
|
+
if (minutes > 0) {
|
|
149
|
+
parts.push(`${minutes} ${minutes === 1 ? "minute" : "minutes"}`);
|
|
150
|
+
}
|
|
151
|
+
if (parts.length === 0) parts.push(`${seconds} ${seconds === 1 ? "second" : "seconds"}`);
|
|
152
|
+
return parts.join(" ");
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* The message of the exhaustion of the weekly allowance.
|
|
157
|
+
*
|
|
158
|
+
* The message names the provider, the remaining time of the week and the
|
|
159
|
+
* state of the work. It travels in full: a truncated message is not a
|
|
160
|
+
* message the user can decide about.
|
|
161
|
+
*/
|
|
162
|
+
export function weeklyExhaustionMessage(input: {
|
|
163
|
+
/** The provider whose weekly allowance is empty. */
|
|
164
|
+
provider: string;
|
|
165
|
+
/** The absolute epoch of the weekly reset. */
|
|
166
|
+
resetAt: number;
|
|
167
|
+
/** The clock of the message. */
|
|
168
|
+
now: number;
|
|
169
|
+
}): string {
|
|
170
|
+
const { provider, resetAt, now } = input;
|
|
171
|
+
return `${providerDisplayName(provider)} weekly quota exhausted. Resets in ${formatResetDuration(resetAt - now)}. Work is paused.`;
|
|
172
|
+
}
|