pi-harness-runtime 1.1.511 → 1.1.513
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 +181 -0
- package/footer-status.ts +112 -43
- package/harness/cookie-import-refresh.ts +60 -0
- package/harness/quota-credential-choice.ts +67 -0
- package/harness/quota-refresh-coordinator.ts +129 -0
- package/index.ts +182 -97
- 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/dist/detect-format.d.ts +26 -12
- package/packages/cookie-sanitizer/dist/detect-format.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/detect-format.js +54 -39
- package/packages/cookie-sanitizer/dist/detect-format.js.map +1 -1
- package/packages/cookie-sanitizer/dist/index.d.ts +9 -3
- package/packages/cookie-sanitizer/dist/index.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/index.js +5 -3
- package/packages/cookie-sanitizer/dist/index.js.map +1 -1
- package/packages/cookie-sanitizer/dist/parse-netscape-space.d.ts +73 -0
- package/packages/cookie-sanitizer/dist/parse-netscape-space.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-netscape-space.js +154 -0
- package/packages/cookie-sanitizer/dist/parse-netscape-space.js.map +1 -0
- package/packages/cookie-sanitizer/dist/parse-netscape.d.ts +28 -1
- package/packages/cookie-sanitizer/dist/parse-netscape.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/parse-netscape.js +50 -13
- package/packages/cookie-sanitizer/dist/parse-netscape.js.map +1 -1
- package/packages/cookie-sanitizer/dist/source-state.d.ts +56 -0
- package/packages/cookie-sanitizer/dist/source-state.d.ts.map +1 -0
- package/packages/cookie-sanitizer/dist/source-state.js +61 -0
- package/packages/cookie-sanitizer/dist/source-state.js.map +1 -0
- package/packages/cookie-sanitizer/dist/sync.d.ts +9 -0
- package/packages/cookie-sanitizer/dist/sync.d.ts.map +1 -1
- package/packages/cookie-sanitizer/dist/sync.js +47 -16
- package/packages/cookie-sanitizer/dist/sync.js.map +1 -1
- package/packages/cookie-sanitizer/dist/types.d.ts +1 -1
- package/packages/cookie-sanitizer/dist/types.d.ts.map +1 -1
- package/packages/cookie-sanitizer/package.json +1 -1
- package/packages/cookie-sanitizer/src/detect-format.ts +78 -41
- package/packages/cookie-sanitizer/src/index.ts +34 -1
- package/packages/cookie-sanitizer/src/parse-netscape-space.ts +188 -0
- package/packages/cookie-sanitizer/src/parse-netscape.ts +81 -13
- package/packages/cookie-sanitizer/src/source-state.ts +95 -0
- package/packages/cookie-sanitizer/src/sync.ts +59 -16
- package/packages/cookie-sanitizer/src/types.ts +4 -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/package.json +1 -1
- package/packages/knowledge-retrieval/package.json +1 -1
- package/packages/learning-engine/package.json +1 -1
- package/packages/logging/dist/server/index-store.d.ts +19 -1
- package/packages/logging/dist/server/index-store.d.ts.map +1 -1
- package/packages/logging/dist/server/index-store.js +25 -2
- package/packages/logging/dist/server/index-store.js.map +1 -1
- package/packages/logging/package.json +1 -1
- package/packages/logging/src/server/index-store.ts +40 -2
- 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,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared quota refresh.
|
|
3
|
+
*
|
|
4
|
+
* A quota refresh reaches the provider: it costs a request and it can
|
|
5
|
+
* open a browser. Several things can ask for one at the same time (a
|
|
6
|
+
* watcher event, a periodic tick, a command), and two parallel refreshes
|
|
7
|
+
* would mean two browser captures and two chances to trip a rate limit.
|
|
8
|
+
*
|
|
9
|
+
* The coordinator owns one refresh at a time. A request that arrives while
|
|
10
|
+
* a refresh runs joins that refresh instead of starting a second one, and
|
|
11
|
+
* every requester sees the same outcome.
|
|
12
|
+
*
|
|
13
|
+
* The refresh runs outside the model: it never asks a model anything, it
|
|
14
|
+
* only calls the provider.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** Which credential the refresh used. */
|
|
18
|
+
export type QuotaAuthSource =
|
|
19
|
+
| "browser-profile"
|
|
20
|
+
| "cookie-cache"
|
|
21
|
+
| "none";
|
|
22
|
+
|
|
23
|
+
/** The outcome of a refresh. */
|
|
24
|
+
export interface QuotaRefreshOutcome {
|
|
25
|
+
/** Whether the provider answered with usable quota data. */
|
|
26
|
+
ok: boolean;
|
|
27
|
+
/** Which credential the refresh used. */
|
|
28
|
+
source: QuotaAuthSource;
|
|
29
|
+
/**
|
|
30
|
+
* A safe reason when the refresh failed.
|
|
31
|
+
*
|
|
32
|
+
* The reason names the failure, never a credential: no cookie value, no
|
|
33
|
+
* cookie name, no token, no raw response body.
|
|
34
|
+
*/
|
|
35
|
+
reason?: string;
|
|
36
|
+
/** When the refresh ran, in epoch milliseconds. */
|
|
37
|
+
at?: number;
|
|
38
|
+
/** What asked for the refresh. */
|
|
39
|
+
trigger?: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The options of the coordinator. */
|
|
43
|
+
export interface QuotaRefreshCoordinatorOptions {
|
|
44
|
+
/** The refresh itself. It must never throw. */
|
|
45
|
+
refresh: () => Promise<QuotaRefreshOutcome>;
|
|
46
|
+
/** The clock. Default: `Date.now`. */
|
|
47
|
+
now?: () => number;
|
|
48
|
+
/** An optional log sink for the outcome. */
|
|
49
|
+
logger?: (message: string) => void;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* One refresh at a time, shared by every requester.
|
|
54
|
+
*/
|
|
55
|
+
export class QuotaRefreshCoordinator {
|
|
56
|
+
private readonly refreshFn: () => Promise<QuotaRefreshOutcome>;
|
|
57
|
+
private readonly now: () => number;
|
|
58
|
+
private readonly logger: ((message: string) => void) | undefined;
|
|
59
|
+
private inFlight: Promise<QuotaRefreshOutcome> | null = null;
|
|
60
|
+
private currentToken: object | null = null;
|
|
61
|
+
private last: QuotaRefreshOutcome | null = null;
|
|
62
|
+
|
|
63
|
+
constructor(options: QuotaRefreshCoordinatorOptions) {
|
|
64
|
+
this.refreshFn = options.refresh;
|
|
65
|
+
this.now = options.now ?? (() => Date.now());
|
|
66
|
+
this.logger = options.logger;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Whether a refresh is running right now. */
|
|
70
|
+
get isRefreshing(): boolean {
|
|
71
|
+
return this.inFlight !== null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The outcome of the last refresh, or null when none ran yet. */
|
|
75
|
+
get lastOutcome(): QuotaRefreshOutcome | null {
|
|
76
|
+
return this.last;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Ask for a refresh.
|
|
81
|
+
*
|
|
82
|
+
* A request that arrives while a refresh runs joins that refresh: one
|
|
83
|
+
* fetch, one outcome, every requester sees the same result.
|
|
84
|
+
*/
|
|
85
|
+
request(trigger: string): Promise<QuotaRefreshOutcome> {
|
|
86
|
+
if (this.inFlight) {
|
|
87
|
+
this.logger?.(
|
|
88
|
+
`quota refresh already running; ${trigger} joins the running refresh`,
|
|
89
|
+
);
|
|
90
|
+
return this.inFlight;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const token = {};
|
|
94
|
+
this.currentToken = token;
|
|
95
|
+
const run = (async (): Promise<QuotaRefreshOutcome> => {
|
|
96
|
+
let outcome: QuotaRefreshOutcome;
|
|
97
|
+
try {
|
|
98
|
+
outcome = await this.refreshFn();
|
|
99
|
+
} catch (error) {
|
|
100
|
+
outcome = {
|
|
101
|
+
ok: false,
|
|
102
|
+
source: "none",
|
|
103
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
104
|
+
at: this.now(),
|
|
105
|
+
trigger,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const settled: QuotaRefreshOutcome = {
|
|
109
|
+
...outcome,
|
|
110
|
+
at: outcome.at ?? this.now(),
|
|
111
|
+
trigger: outcome.trigger ?? trigger,
|
|
112
|
+
};
|
|
113
|
+
this.last = settled;
|
|
114
|
+
this.logger?.(
|
|
115
|
+
`quota refresh (${settled.trigger}) via ${settled.source}: ${settled.ok ? "ok" : `failed (${settled.reason ?? "unknown"})`}`,
|
|
116
|
+
);
|
|
117
|
+
// The slot is released before the promise settles, so a request
|
|
118
|
+
// that follows the await starts a new refresh.
|
|
119
|
+
if (this.currentToken === token) {
|
|
120
|
+
this.currentToken = null;
|
|
121
|
+
this.inFlight = null;
|
|
122
|
+
}
|
|
123
|
+
return settled;
|
|
124
|
+
})();
|
|
125
|
+
|
|
126
|
+
this.inFlight = run;
|
|
127
|
+
return run;
|
|
128
|
+
}
|
|
129
|
+
}
|
package/index.ts
CHANGED
|
@@ -164,8 +164,14 @@ import { parseMiniMaxQuotaText } from "./harness/e2e/minimax-quota-parser.js";
|
|
|
164
164
|
import {
|
|
165
165
|
CookieWatcher,
|
|
166
166
|
DEFAULT_DROP_DIR as COOKIE_DROP_DIR,
|
|
167
|
-
|
|
167
|
+
inspectCookieSource,
|
|
168
168
|
} from "./packages/cookie-sanitizer/src/index.ts";
|
|
169
|
+
import {
|
|
170
|
+
QuotaRefreshCoordinator,
|
|
171
|
+
type QuotaRefreshOutcome,
|
|
172
|
+
} from "./harness/quota-refresh-coordinator.ts";
|
|
173
|
+
import { handleCookieWatcherEvent } from "./harness/cookie-import-refresh.ts";
|
|
174
|
+
import { chooseQuotaCredential } from "./harness/quota-credential-choice.ts";
|
|
169
175
|
import {
|
|
170
176
|
providerFromModelId,
|
|
171
177
|
type ProviderId,
|
|
@@ -176,6 +182,7 @@ import {
|
|
|
176
182
|
} from "./packages/quota-manager/src/tui-usage-monitor.ts";
|
|
177
183
|
import { QuotaManager } from "./packages/quota-manager/src/quota-manager.ts";
|
|
178
184
|
import { buildFooterStatusValue } from "./footer-status.ts";
|
|
185
|
+
import type { FooterQuotaSource } from "./footer-status.ts";
|
|
179
186
|
import { getProviderLabel } from "./packages/types/src/ai-providers.ts";
|
|
180
187
|
import { registerGithubLoginCommand } from "./packages/clipboard/dist/src/github-login.js";
|
|
181
188
|
import {
|
|
@@ -8097,19 +8104,48 @@ issue is not authorized work of the current request).
|
|
|
8097
8104
|
const cookieDropDir = COOKIE_DROP_DIR;
|
|
8098
8105
|
const cookieCachePath = join(homedir(), ".config", "minimax-cookies.txt");
|
|
8099
8106
|
|
|
8100
|
-
|
|
8101
|
-
|
|
8102
|
-
|
|
8103
|
-
|
|
8104
|
-
|
|
8105
|
-
|
|
8107
|
+
/**
|
|
8108
|
+
* The shared quota refresh.
|
|
8109
|
+
*
|
|
8110
|
+
* One refresh at a time. A watcher event, a periodic tick and a command
|
|
8111
|
+
* that arrive together share one fetch, so a burst never opens two
|
|
8112
|
+
* browsers and never asks the provider twice.
|
|
8113
|
+
*/
|
|
8114
|
+
const quotaRefresh = new QuotaRefreshCoordinator({
|
|
8115
|
+
refresh: () => autoFetchQuota({ suppressErrors: true }),
|
|
8116
|
+
logger: (message) => logToFile("[pi-harness] quota refresh:", message),
|
|
8117
|
+
});
|
|
8118
|
+
|
|
8119
|
+
/**
|
|
8120
|
+
* The state of the credential source.
|
|
8121
|
+
*
|
|
8122
|
+
* Three facts are different and were conflated: a file was found
|
|
8123
|
+
* (discovery), the file was imported into the canonical cache (import),
|
|
8124
|
+
* and the provider accepted the credential (authentication). The first
|
|
8125
|
+
* two are read from the disk; the third comes from the last fetch.
|
|
8126
|
+
*/
|
|
8127
|
+
const cookieSourceState = (): FooterQuotaSource => {
|
|
8128
|
+
let stage: FooterQuotaSource["stage"] = "none";
|
|
8106
8129
|
try {
|
|
8107
|
-
|
|
8130
|
+
stage = inspectCookieSource({
|
|
8131
|
+
dropDir: cookieDropDir,
|
|
8132
|
+
cachePath: cookieCachePath,
|
|
8133
|
+
}).stage;
|
|
8108
8134
|
} catch {
|
|
8109
|
-
|
|
8135
|
+
stage = "none";
|
|
8110
8136
|
}
|
|
8137
|
+
const last = quotaRefresh.lastOutcome;
|
|
8138
|
+
return {
|
|
8139
|
+
stage,
|
|
8140
|
+
lastFetch: last
|
|
8141
|
+
? { ok: last.ok, source: last.source, reason: last.reason }
|
|
8142
|
+
: null,
|
|
8143
|
+
};
|
|
8111
8144
|
};
|
|
8112
8145
|
|
|
8146
|
+
/** Whether any credential source exists, imported or not. */
|
|
8147
|
+
const hasCookieSource = (): boolean => cookieSourceState().stage !== "none";
|
|
8148
|
+
|
|
8113
8149
|
// --- TUI quota signal plumbing (OpenAI / GLM / Anthropic / OpenRouter) --
|
|
8114
8150
|
// The TUIUsageMonitor parses provider quota-exhaustion messages from pi's
|
|
8115
8151
|
// TUI / message stream and emits signals we write to per-provider mirror
|
|
@@ -8145,49 +8181,22 @@ issue is not authorized work of the current request).
|
|
|
8145
8181
|
dropDir: cookieDropDir,
|
|
8146
8182
|
syncOptions: { cachePath: cookieCachePath, providerHint: "minimax" },
|
|
8147
8183
|
onEvent: (event) => {
|
|
8148
|
-
|
|
8149
|
-
|
|
8150
|
-
|
|
8151
|
-
|
|
8152
|
-
"[pi-harness] cookie-sanitizer: ENOSPC — inotify watchers exhausted.\n" +
|
|
8153
|
-
" Fix (run once as sudo):\n" +
|
|
8154
|
-
" echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/99-watch.conf\n" +
|
|
8155
|
-
" sudo sysctl --system",
|
|
8156
|
-
);
|
|
8157
|
-
} else {
|
|
8158
|
-
logToFile("[pi-harness] cookie-sanitizer:", msg);
|
|
8159
|
-
}
|
|
8160
|
-
}
|
|
8161
|
-
// A successful sync means the canonical cache is fresh; the
|
|
8162
|
-
// next autoFetchQuota() should pick it up immediately. Reset
|
|
8163
|
-
// the rate-limit so we don't wait 15 min for the first scrape.
|
|
8184
|
+
// A successful sync means the canonical cache is fresh. One
|
|
8185
|
+
// shared refresh, outside the model, picks the new credential
|
|
8186
|
+
// up. The rate limit is reset so the periodic tick does not
|
|
8187
|
+
// ask again before the shared refresh finished.
|
|
8164
8188
|
if (event.kind === "sync-ok") {
|
|
8165
8189
|
lastQuotaAutoFetchAt = 0;
|
|
8166
8190
|
}
|
|
8191
|
+
handleCookieWatcherEvent(event, {
|
|
8192
|
+
requestRefresh: (trigger) => {
|
|
8193
|
+
void quotaRefresh.request(trigger);
|
|
8194
|
+
},
|
|
8195
|
+
log: (message) => logToFile("[pi-harness]", message),
|
|
8196
|
+
});
|
|
8167
8197
|
},
|
|
8168
8198
|
});
|
|
8169
8199
|
|
|
8170
|
-
try {
|
|
8171
|
-
cookieWatcher.start();
|
|
8172
|
-
// Sync existing drop-folder cookies now (ignoreInitial: true means
|
|
8173
|
-
// the watcher won't do this automatically on startup).
|
|
8174
|
-
cookieWatcher.triggerNow();
|
|
8175
|
-
} catch (e) {
|
|
8176
|
-
const msg = e instanceof Error ? e.message : String(e);
|
|
8177
|
-
if (msg.includes("ENOSPC")) {
|
|
8178
|
-
logToFile(
|
|
8179
|
-
"[pi-harness] cookie-sanitizer watcher: ENOSPC — " +
|
|
8180
|
-
"inotify watcher limit reached.\n" +
|
|
8181
|
-
"Fix (once, as root):\n" +
|
|
8182
|
-
" echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/99-watch.conf\n" +
|
|
8183
|
-
" sudo sysctl --system\n" +
|
|
8184
|
-
"Drop folder sync still works — polling will resume automatically.",
|
|
8185
|
-
);
|
|
8186
|
-
} else {
|
|
8187
|
-
logToFile("[pi-harness] cookie-sanitizer watcher failed to start:", msg);
|
|
8188
|
-
}
|
|
8189
|
-
}
|
|
8190
|
-
|
|
8191
8200
|
// Store only the setStatus function, NOT the ctx itself.
|
|
8192
8201
|
// Storing ctx causes stale-context errors after pi reload/restart.
|
|
8193
8202
|
let footerSetStatus: ((key: string, value: string) => void) | null = null;
|
|
@@ -8310,7 +8319,7 @@ issue is not authorized work of the current request).
|
|
|
8310
8319
|
footerSetStatus,
|
|
8311
8320
|
tracker,
|
|
8312
8321
|
mirrorStore,
|
|
8313
|
-
|
|
8322
|
+
cookieSourceState,
|
|
8314
8323
|
() => lastActiveProvider,
|
|
8315
8324
|
);
|
|
8316
8325
|
}
|
|
@@ -8386,7 +8395,7 @@ issue is not authorized work of the current request).
|
|
|
8386
8395
|
footerSetStatus,
|
|
8387
8396
|
tracker,
|
|
8388
8397
|
mirrorStore,
|
|
8389
|
-
|
|
8398
|
+
cookieSourceState,
|
|
8390
8399
|
() => lastActiveProvider,
|
|
8391
8400
|
);
|
|
8392
8401
|
}
|
|
@@ -8411,9 +8420,9 @@ issue is not authorized work of the current request).
|
|
|
8411
8420
|
|
|
8412
8421
|
async function autoFetchQuotaFromBrowserProfile(
|
|
8413
8422
|
suppressErrors = false,
|
|
8414
|
-
): Promise<MirrorRecord | null> {
|
|
8423
|
+
): Promise<{ record: MirrorRecord | null; reason?: string }> {
|
|
8415
8424
|
if (!(await hasBrowserProfileAutoFetchSource())) {
|
|
8416
|
-
return null;
|
|
8425
|
+
return { record: null };
|
|
8417
8426
|
}
|
|
8418
8427
|
|
|
8419
8428
|
try {
|
|
@@ -8425,19 +8434,28 @@ issue is not authorized work of the current request).
|
|
|
8425
8434
|
status.page_url.includes("unified-login") ||
|
|
8426
8435
|
status.page_url.includes("login")
|
|
8427
8436
|
) {
|
|
8428
|
-
return
|
|
8437
|
+
return {
|
|
8438
|
+
record: null,
|
|
8439
|
+
reason: "the browser profile is not signed in",
|
|
8440
|
+
};
|
|
8429
8441
|
}
|
|
8430
8442
|
const rawText =
|
|
8431
8443
|
status.usage_lines?.join("\n")?.trim() ||
|
|
8432
8444
|
status.detected_text_sample?.trim() ||
|
|
8433
8445
|
"";
|
|
8434
8446
|
if (!rawText) {
|
|
8435
|
-
return
|
|
8447
|
+
return {
|
|
8448
|
+
record: null,
|
|
8449
|
+
reason: "the browser profile holds no quota reading",
|
|
8450
|
+
};
|
|
8436
8451
|
}
|
|
8437
8452
|
|
|
8438
8453
|
const parsed = parseMiniMaxQuotaText(rawText);
|
|
8439
8454
|
if (parsed.h5UsedPct === undefined && parsed.weeklyUsedPct === undefined) {
|
|
8440
|
-
return
|
|
8455
|
+
return {
|
|
8456
|
+
record: null,
|
|
8457
|
+
reason: "the browser profile reading holds no quota window",
|
|
8458
|
+
};
|
|
8441
8459
|
}
|
|
8442
8460
|
|
|
8443
8461
|
// The console writes the remaining time of a window as text. The
|
|
@@ -8446,39 +8464,43 @@ issue is not authorized work of the current request).
|
|
|
8446
8464
|
const observedAt = Date.parse(status.checked_at);
|
|
8447
8465
|
|
|
8448
8466
|
return {
|
|
8449
|
-
|
|
8450
|
-
|
|
8451
|
-
|
|
8452
|
-
|
|
8453
|
-
|
|
8454
|
-
|
|
8455
|
-
|
|
8456
|
-
|
|
8457
|
-
|
|
8458
|
-
|
|
8459
|
-
|
|
8460
|
-
|
|
8461
|
-
|
|
8462
|
-
|
|
8467
|
+
record: {
|
|
8468
|
+
synced_at: status.checked_at,
|
|
8469
|
+
provider: "minimax",
|
|
8470
|
+
h5_used_pct: parsed.h5UsedPct,
|
|
8471
|
+
h5_resets_at: parsed.h5ResetsAt,
|
|
8472
|
+
h5_resets_at_epoch: resetEpochFromDuration(
|
|
8473
|
+
parsed.h5ResetsAt,
|
|
8474
|
+
observedAt,
|
|
8475
|
+
),
|
|
8476
|
+
weekly_used_pct: parsed.weeklyUsedPct,
|
|
8477
|
+
weekly_resets_at: parsed.weeklyResetsAt,
|
|
8478
|
+
weekly_resets_at_epoch: resetEpochFromDuration(
|
|
8479
|
+
parsed.weeklyResetsAt,
|
|
8480
|
+
observedAt,
|
|
8481
|
+
),
|
|
8482
|
+
},
|
|
8463
8483
|
};
|
|
8464
8484
|
} catch (error) {
|
|
8485
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
8465
8486
|
if (!suppressErrors) {
|
|
8466
8487
|
logToFile(
|
|
8467
8488
|
"[pi-harness]",
|
|
8468
8489
|
"Browser-profile quota fetch skipped:",
|
|
8469
|
-
|
|
8490
|
+
reason,
|
|
8470
8491
|
);
|
|
8471
8492
|
}
|
|
8472
|
-
return null;
|
|
8493
|
+
return { record: null, reason };
|
|
8473
8494
|
}
|
|
8474
8495
|
}
|
|
8475
8496
|
|
|
8476
8497
|
async function autoFetchQuota(options?: {
|
|
8477
8498
|
suppressErrors?: boolean;
|
|
8478
|
-
}): Promise<
|
|
8499
|
+
}): Promise<QuotaRefreshOutcome> {
|
|
8479
8500
|
const suppressErrors = options?.suppressErrors === true;
|
|
8480
|
-
const
|
|
8481
|
-
if (
|
|
8501
|
+
const profile = await autoFetchQuotaFromBrowserProfile(suppressErrors);
|
|
8502
|
+
if (profile.record) {
|
|
8503
|
+
const profileRecord = profile.record;
|
|
8482
8504
|
writeMirrorRecord("minimax", {
|
|
8483
8505
|
synced_at: profileRecord.synced_at,
|
|
8484
8506
|
source: "scrape",
|
|
@@ -8490,11 +8512,19 @@ issue is not authorized work of the current request).
|
|
|
8490
8512
|
weekly_resets_at: profileRecord.weekly_resets_at,
|
|
8491
8513
|
weekly_resets_at_epoch: profileRecord.weekly_resets_at_epoch,
|
|
8492
8514
|
});
|
|
8493
|
-
return true;
|
|
8515
|
+
return { ok: true, source: "browser-profile" };
|
|
8494
8516
|
}
|
|
8495
8517
|
|
|
8496
|
-
|
|
8497
|
-
|
|
8518
|
+
// The browser profile already ran and did not answer. The cookie
|
|
8519
|
+
// cache is the second credential, and it only exists when an import
|
|
8520
|
+
// published it: a file that was found but not imported gives a
|
|
8521
|
+
// scraper nothing to read.
|
|
8522
|
+
const choice = chooseQuotaCredential({
|
|
8523
|
+
browserProfile: { ok: false, reason: profile.reason },
|
|
8524
|
+
cookieStage: cookieSourceState().stage,
|
|
8525
|
+
});
|
|
8526
|
+
if (choice.choice !== "cookie-cache") {
|
|
8527
|
+
return { ok: false, source: "none", reason: choice.reason };
|
|
8498
8528
|
}
|
|
8499
8529
|
|
|
8500
8530
|
try {
|
|
@@ -8509,16 +8539,41 @@ issue is not authorized work of the current request).
|
|
|
8509
8539
|
weekly_resets_at: data.weeklyResetsAt,
|
|
8510
8540
|
weekly_resets_at_epoch: data.weeklyResetsAtEpoch,
|
|
8511
8541
|
});
|
|
8512
|
-
return true;
|
|
8542
|
+
return { ok: true, source: "cookie-cache" };
|
|
8513
8543
|
} catch (error) {
|
|
8544
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
8514
8545
|
if (!suppressErrors) {
|
|
8515
8546
|
logToFile(
|
|
8516
8547
|
"[pi-harness]",
|
|
8517
8548
|
"Quota auto-fetch skipped:",
|
|
8518
|
-
|
|
8549
|
+
reason,
|
|
8519
8550
|
);
|
|
8520
8551
|
}
|
|
8521
|
-
return false;
|
|
8552
|
+
return { ok: false, source: "cookie-cache", reason };
|
|
8553
|
+
}
|
|
8554
|
+
}
|
|
8555
|
+
|
|
8556
|
+
// The watcher starts here, not at the top of the initialization: a sync
|
|
8557
|
+
// that runs during the initialization would ask for a refresh that reads
|
|
8558
|
+
// capture state that does not exist yet.
|
|
8559
|
+
try {
|
|
8560
|
+
cookieWatcher.start();
|
|
8561
|
+
// Sync existing drop-folder cookies now (ignoreInitial: true means
|
|
8562
|
+
// the watcher won't do this automatically on startup).
|
|
8563
|
+
cookieWatcher.triggerNow();
|
|
8564
|
+
} catch (e) {
|
|
8565
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
8566
|
+
if (msg.includes("ENOSPC")) {
|
|
8567
|
+
logToFile(
|
|
8568
|
+
"[pi-harness] cookie-sanitizer watcher: ENOSPC — " +
|
|
8569
|
+
"inotify watcher limit reached.\n" +
|
|
8570
|
+
"Fix (once, as root):\n" +
|
|
8571
|
+
" echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/99-watch.conf\n" +
|
|
8572
|
+
" sudo sysctl --system\n" +
|
|
8573
|
+
"Drop folder sync still works — polling will resume automatically.",
|
|
8574
|
+
);
|
|
8575
|
+
} else {
|
|
8576
|
+
logToFile("[pi-harness] cookie-sanitizer watcher failed to start:", msg);
|
|
8522
8577
|
}
|
|
8523
8578
|
}
|
|
8524
8579
|
|
|
@@ -8671,7 +8726,8 @@ issue is not authorized work of the current request).
|
|
|
8671
8726
|
*/
|
|
8672
8727
|
async function captureQuotaOf(provider: string): Promise<boolean> {
|
|
8673
8728
|
if (provider === "minimax") {
|
|
8674
|
-
|
|
8729
|
+
const outcome = await autoFetchQuota({ suppressErrors: true });
|
|
8730
|
+
return outcome.ok;
|
|
8675
8731
|
}
|
|
8676
8732
|
if (provider === "openai" || provider === "openai-codex") {
|
|
8677
8733
|
return autoFetchOpenAIQuota({ suppressErrors: true });
|
|
@@ -8820,23 +8876,46 @@ issue is not authorized work of the current request).
|
|
|
8820
8876
|
pi.registerCommand("usage-refresh", {
|
|
8821
8877
|
description: "Force refresh quota from provider console",
|
|
8822
8878
|
handler: async (_args: string, ctx: ExtensionCommandContext) => {
|
|
8823
|
-
const
|
|
8824
|
-
|
|
8825
|
-
if (!
|
|
8879
|
+
const source = cookieSourceState();
|
|
8880
|
+
const browserProfile = await hasBrowserProfileAutoFetchSource();
|
|
8881
|
+
if (!hasCookieSource() && !browserProfile) {
|
|
8826
8882
|
ctx.ui.notify(
|
|
8827
|
-
"MiniMax cookies not found. Drop any cookie file (Netscape or EditThisCookie JSON) into ~/.pi-harness-runtime/cookies/ — the runtime normalizes it for you. Or run `bun packages/auth/src/run-minimax-auth.ts auth`.",
|
|
8883
|
+
"MiniMax cookies not found. Drop any cookie file (Netscape, the space-separated Netscape export, or EditThisCookie JSON) into ~/.pi-harness-runtime/cookies/ — the runtime normalizes it for you. Or run `bun packages/auth/src/run-minimax-auth.ts auth`.",
|
|
8828
8884
|
"warning",
|
|
8829
8885
|
);
|
|
8830
8886
|
return;
|
|
8831
8887
|
}
|
|
8832
8888
|
ctx.ui.notify("Fetching quota from MiniMax console...", "info");
|
|
8833
|
-
const
|
|
8834
|
-
|
|
8835
|
-
|
|
8836
|
-
|
|
8837
|
-
|
|
8838
|
-
|
|
8839
|
-
|
|
8889
|
+
const outcome = await quotaRefresh.request("usage-refresh");
|
|
8890
|
+
if (outcome.ok) {
|
|
8891
|
+
const via =
|
|
8892
|
+
outcome.source === "browser-profile"
|
|
8893
|
+
? "the browser profile"
|
|
8894
|
+
: "the imported cookie cache";
|
|
8895
|
+
ctx.ui.notify(
|
|
8896
|
+
`Quota refreshed via ${via}. Run \`/usage\` to see updated status.`,
|
|
8897
|
+
"info",
|
|
8898
|
+
);
|
|
8899
|
+
return;
|
|
8900
|
+
}
|
|
8901
|
+
// The failure names what failed: the import, the authentication
|
|
8902
|
+
// or the provider. It never carries a credential.
|
|
8903
|
+
const reason =
|
|
8904
|
+
outcome.reason ??
|
|
8905
|
+
"the provider did not answer with a quota reading";
|
|
8906
|
+
const via =
|
|
8907
|
+
outcome.source === "cookie-cache"
|
|
8908
|
+
? "the imported cookie cache"
|
|
8909
|
+
: outcome.source === "browser-profile"
|
|
8910
|
+
? "the browser profile"
|
|
8911
|
+
: "no credential";
|
|
8912
|
+
ctx.ui.notify(`Quota refresh failed via ${via}: ${reason}`, "warning");
|
|
8913
|
+
if (source.stage === "discovered" && !browserProfile) {
|
|
8914
|
+
ctx.ui.notify(
|
|
8915
|
+
"The cookie file was found but not imported. Check the harness log for the reason, then run `/usage-refresh` again.",
|
|
8916
|
+
"warning",
|
|
8917
|
+
);
|
|
8918
|
+
}
|
|
8840
8919
|
},
|
|
8841
8920
|
});
|
|
8842
8921
|
|
|
@@ -8908,7 +8987,7 @@ issue is not authorized work of the current request).
|
|
|
8908
8987
|
footerSetStatus,
|
|
8909
8988
|
tracker,
|
|
8910
8989
|
mirrorStore,
|
|
8911
|
-
|
|
8990
|
+
cookieSourceState,
|
|
8912
8991
|
() => lastActiveProvider,
|
|
8913
8992
|
);
|
|
8914
8993
|
} catch (e) {
|
|
@@ -9369,7 +9448,7 @@ issue is not authorized work of the current request).
|
|
|
9369
9448
|
footerSetStatus,
|
|
9370
9449
|
tracker,
|
|
9371
9450
|
mirrorStore,
|
|
9372
|
-
|
|
9451
|
+
cookieSourceState,
|
|
9373
9452
|
() => lastActiveProvider,
|
|
9374
9453
|
);
|
|
9375
9454
|
|
|
@@ -9453,7 +9532,7 @@ issue is not authorized work of the current request).
|
|
|
9453
9532
|
footerSetStatus,
|
|
9454
9533
|
tracker,
|
|
9455
9534
|
mirrorStore,
|
|
9456
|
-
|
|
9535
|
+
cookieSourceState,
|
|
9457
9536
|
() => lastActiveProvider,
|
|
9458
9537
|
);
|
|
9459
9538
|
void maybeAutoFetchQuota(ctx.model?.id ?? null);
|
|
@@ -9491,7 +9570,7 @@ issue is not authorized work of the current request).
|
|
|
9491
9570
|
footerSetStatus,
|
|
9492
9571
|
tracker,
|
|
9493
9572
|
mirrorStore,
|
|
9494
|
-
|
|
9573
|
+
cookieSourceState,
|
|
9495
9574
|
() => lastActiveProvider,
|
|
9496
9575
|
);
|
|
9497
9576
|
|
|
@@ -9588,7 +9667,7 @@ issue is not authorized work of the current request).
|
|
|
9588
9667
|
footerSetStatus,
|
|
9589
9668
|
tracker,
|
|
9590
9669
|
mirrorStore,
|
|
9591
|
-
|
|
9670
|
+
cookieSourceState,
|
|
9592
9671
|
() => lastActiveProvider,
|
|
9593
9672
|
);
|
|
9594
9673
|
}
|
|
@@ -9753,7 +9832,7 @@ function refreshFooterStatus(
|
|
|
9753
9832
|
setStatus: (key: string, value: string) => void,
|
|
9754
9833
|
tracker: UsageTracker,
|
|
9755
9834
|
mirrorStore: MirrorStore,
|
|
9756
|
-
|
|
9835
|
+
cookieSourceState: () => FooterQuotaSource,
|
|
9757
9836
|
getActiveProvider: () => ProviderId | null = () => null,
|
|
9758
9837
|
) {
|
|
9759
9838
|
const nowMs = Date.now();
|
|
@@ -9764,7 +9843,13 @@ function refreshFooterStatus(
|
|
|
9764
9843
|
// mirror ? JSON.stringify(mirror) : null,
|
|
9765
9844
|
// );
|
|
9766
9845
|
const freshness = mirrorStore.freshness(mirror, nowMs);
|
|
9767
|
-
const statusValue = buildFooterStatusValue(
|
|
9846
|
+
const statusValue = buildFooterStatusValue(
|
|
9847
|
+
local,
|
|
9848
|
+
mirror,
|
|
9849
|
+
freshness,
|
|
9850
|
+
cookieSourceState(),
|
|
9851
|
+
provider,
|
|
9852
|
+
);
|
|
9768
9853
|
// Prepend harness version so it shows in the footer during startup
|
|
9769
9854
|
setStatus(
|
|
9770
9855
|
"harness-runtime",
|
package/package.json
CHANGED