@bitkyc08/opencodex 2.100.1 → 2.100.2
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/gui/dist/assets/App-BpJOzw4F.js +59 -0
- package/gui/dist/assets/Tray-BcaI-VCr.js +1 -0
- package/gui/dist/assets/{brand-mark-nr3ziR-T.js → brand-mark-DU-0Vdkr.js} +1 -1
- package/gui/dist/assets/index-Bbor_y80.js +94 -0
- package/gui/dist/assets/index-DJEkIDAi.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-DKCVUUTi.js → segment-thumb-features-e9A_FogJ.js} +1 -1
- package/gui/dist/classic/assets/{App-CgPBKUP-.js → App-BrIlUiLc.js} +18 -18
- package/gui/dist/classic/assets/{Tray-BedmOsE7.js → Tray-DT146AhS.js} +1 -1
- package/gui/dist/classic/assets/index-DyJHagvT.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CakPe9Ni.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/package.json +2 -2
- package/src/adapters/client-fingerprint.ts +5 -3
- package/src/adapters/cursor/native-exec.ts +3 -2
- package/src/adapters/cursor/tool-guidance.ts +3 -3
- package/src/adapters/cursor/tool-wording.ts +10 -1
- package/src/adapters/devin/cloud-direct/tool-schema.ts +20 -2
- package/src/adapters/google-antigravity-wire.ts +13 -6
- package/src/adapters/google-truncation.ts +4 -2
- package/src/adapters/google.ts +8 -2
- package/src/adapters/openai-chat/tool-schema.ts +9 -0
- package/src/adapters/openai-responses/tool-schema.ts +18 -4
- package/src/adapters/run-turn-queue.ts +139 -5
- package/src/adapters/upstream-http-error.ts +4 -2
- package/src/bridge/sse.ts +8 -1
- package/src/claude/chat-tool-dispatch.ts +233 -0
- package/src/claude/inbound-tool-loading.ts +42 -17
- package/src/cli/account-api.ts +6 -0
- package/src/cli/account-extended.ts +60 -6
- package/src/cli/account-key-quota.ts +22 -2
- package/src/cli/account.ts +1 -1
- package/src/cli/capabilities-access-remote.ts +10 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-base.ts +2 -2
- package/src/cli/claude.ts +19 -14
- package/src/cli/config-command.ts +24 -1
- package/src/cli/connect.ts +36 -3
- package/src/cli/gui-pair-client.ts +23 -2
- package/src/cli/help.ts +1 -1
- package/src/cli/hub.ts +18 -1
- package/src/cli/link-child-ops.ts +136 -0
- package/src/cli/link-status-child.ts +94 -0
- package/src/cli/link.ts +22 -9
- package/src/cli/registry.ts +3 -2
- package/src/cli/sync-command.ts +3 -1
- package/src/cli/sync-fanout.ts +8 -81
- package/src/client/link-readiness.ts +16 -6
- package/src/client/link-tunnel-status.ts +76 -0
- package/src/clients/config-export.ts +14 -1
- package/src/codex/account-resume-health.ts +41 -0
- package/src/codex/auth-api/login-flow.ts +6 -0
- package/src/codex/auth-api/routes.ts +35 -2
- package/src/codex/catalog/gather-capture.ts +2 -0
- package/src/codex/catalog/provider-models.ts +13 -8
- package/src/codex/convergence-types.ts +2 -0
- package/src/codex/quota.ts +111 -6
- package/src/combos/cooldown-store.ts +208 -0
- package/src/combos/failover.ts +432 -35
- package/src/config/salvage.ts +1 -0
- package/src/config/schema/config-schema.ts +6 -3
- package/src/config/schema/leaf-validators.ts +34 -12
- package/src/generated/compatibility-version.json +213 -121
- package/src/lib/catalog-etag.ts +10 -0
- package/src/lib/client-error-body.ts +15 -0
- package/src/lib/context-overflow-verdict.ts +266 -9
- package/src/lib/failure-detail.ts +72 -3
- package/src/lib/hard-refusal.ts +259 -0
- package/src/link/child-activity.ts +57 -0
- package/src/link/child-catalog-freshness.ts +149 -0
- package/src/link/child-gated-ocx.ts +117 -0
- package/src/link/child-observations.ts +205 -0
- package/src/link/child-observe.ts +121 -0
- package/src/link/child-ops-engine.ts +162 -0
- package/src/link/child-update-contract.ts +165 -0
- package/src/link/paths.ts +5 -0
- package/src/link/remote-windows.ts +42 -6
- package/src/link/ssh-argv.ts +126 -32
- package/src/link/status-projection.ts +73 -0
- package/src/link/sync-fanout-classify.ts +98 -0
- package/src/link/tunnel-state.ts +10 -4
- package/src/messaging/mcp-queue-rpc.ts +2 -1
- package/src/messaging/mcp-queue-server.ts +5 -0
- package/src/messaging/mcp-reply-tools.ts +24 -9
- package/src/messaging/mcp-reply-wait.ts +70 -0
- package/src/oauth/account-resume-health.ts +60 -0
- package/src/oauth/index.ts +1 -0
- package/src/oauth/store.ts +44 -1
- package/src/providers/api-key-pause-state.ts +23 -0
- package/src/providers/api-key-pause.ts +68 -10
- package/src/providers/api-key-selection-capture.ts +3 -1
- package/src/providers/api-key-selection.ts +3 -2
- package/src/providers/api-keys.ts +18 -5
- package/src/providers/claude-cli-usage-state.ts +11 -6
- package/src/providers/claude-cli-usage.ts +27 -9
- package/src/providers/codex-capacity.ts +11 -2
- package/src/providers/key-failover.ts +13 -6
- package/src/providers/model-discovery-config.ts +31 -0
- package/src/providers/model-discovery-diagnostic.ts +34 -3
- package/src/providers/model-discovery.ts +43 -7
- package/src/providers/model-rename-fields.ts +2 -0
- package/src/providers/openai-sidecar.ts +15 -2
- package/src/providers/provider-id-rewrite.ts +3 -0
- package/src/providers/quota/account-cache.ts +52 -34
- package/src/providers/quota/anthropic-usage-windows.ts +19 -9
- package/src/providers/quota/report-cache.ts +53 -5
- package/src/providers/quota/vendor-probes-key.ts +73 -0
- package/src/providers/quota/vendor-probes-oauth.ts +57 -3
- package/src/providers/quota-key-accounts.ts +12 -5
- package/src/providers/quota-mistral-budget.ts +18 -0
- package/src/providers/quota-types.ts +25 -0
- package/src/providers/quota.ts +22 -22
- package/src/providers/reasoning-metadata.ts +1051 -12
- package/src/quota/reset-observer.ts +4 -2
- package/src/quota/reset-seen-store.ts +9 -0
- package/src/quota/window-mapping.ts +1 -0
- package/src/quota-pool/assemble.ts +4 -1
- package/src/responses/hosted-tool-policy.ts +56 -12
- package/src/router.ts +2 -0
- package/src/server/auth-cors.ts +11 -8
- package/src/server/catalog-download.ts +3 -5
- package/src/server/chat-native.ts +11 -2
- package/src/server/claude-messages.ts +2 -1
- package/src/server/images.ts +41 -13
- package/src/server/index/link-listener.ts +16 -2
- package/src/server/index/link-relay-sessions.ts +43 -2
- package/src/server/index/optional-listeners.ts +18 -0
- package/src/server/management/config-routes.ts +7 -1
- package/src/server/management/context.ts +1 -1
- package/src/server/management/link-child-ops-routes.ts +116 -0
- package/src/server/management/link-route-state.ts +10 -0
- package/src/server/management/link-routes.ts +10 -0
- package/src/server/management/oauth-account-routes.ts +33 -4
- package/src/server/management/provider-overwrite-carry.ts +28 -3
- package/src/server/management/provider-routes.ts +16 -11
- package/src/server/management/route-registry.ts +3 -0
- package/src/server/messages-native.ts +5 -0
- package/src/server/request-log-failure-detail.ts +59 -1
- package/src/server/responses/adapter-dispatch.ts +35 -44
- package/src/server/responses/combo-stream-preflight.ts +146 -29
- package/src/server/responses/compact.ts +16 -5
- package/src/server/responses/context-overflow.ts +58 -2
- package/src/server/responses/core-combo-failure.ts +29 -4
- package/src/server/responses/core-combo-native.ts +3 -1
- package/src/server/responses/core-combo.ts +30 -3
- package/src/server/responses/core-errors.ts +3 -1
- package/src/server/responses/core-opaque-recovery.ts +45 -2
- package/src/server/responses/core-options.ts +12 -0
- package/src/server/responses/passthrough-dispatch.ts +30 -38
- package/src/server/responses/request-transport.ts +43 -14
- package/src/server/responses/reset-replay.ts +58 -2
- package/src/server/responses/run-turn-execution.ts +56 -30
- package/src/types/claude-tool-loading.ts +48 -0
- package/src/types/config.ts +5 -35
- package/src/types/provider.ts +21 -0
- package/src/types.ts +1 -0
- package/src/web-search/parse.ts +21 -2
- package/gui/dist/assets/App-g3Cm2xhZ.js +0 -59
- package/gui/dist/assets/Tray-BMDkAiXy.js +0 -1
- package/gui/dist/assets/index-Bziu8gTy.css +0 -2
- package/gui/dist/assets/index-OYzhQUNP.js +0 -94
- package/gui/dist/classic/assets/index-BR-HhNrp.js +0 -94
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +0 -1
|
@@ -9,6 +9,7 @@ import { createHash, createHmac, randomBytes } from "node:crypto";
|
|
|
9
9
|
import { copyFileSync, existsSync, mkdirSync, readFileSync, realpathSync } from "node:fs";
|
|
10
10
|
import { delimiter, dirname, join, resolve } from "node:path";
|
|
11
11
|
import { atomicWriteFile, expandUserPath, getConfigDir, websocketsEnabled } from "../../config";
|
|
12
|
+
import { isProviderApiKeyPaused } from "../../providers/api-key-pause";
|
|
12
13
|
import { resolveProviderApiKey } from "../../providers/key-store";
|
|
13
14
|
import { captureOAuthAccountSelection, credentialGeneration, getAccountCredentialWithStatus, getAccountSet } from "../../oauth/store";
|
|
14
15
|
import { readKiroAccountModels, kiroObservedContextWindow } from "../../providers/kiro-model-catalog";
|
|
@@ -89,7 +90,7 @@ import {
|
|
|
89
90
|
providerRedirectError,
|
|
90
91
|
} from "../../lib/provider-outbound";
|
|
91
92
|
import { redactSecretString } from "../../lib/redact";
|
|
92
|
-
import { invalidModelDiscoveryHint } from "../../providers/model-discovery-diagnostic";
|
|
93
|
+
import { invalidModelDiscoveryHint, shouldWarnSkippedModelRows } from "../../providers/model-discovery-diagnostic";
|
|
93
94
|
import {
|
|
94
95
|
extractProviderModelItems,
|
|
95
96
|
isRegistryModelDiscoveryUrl,
|
|
@@ -292,6 +293,9 @@ export async function fetchProviderModelsWithAuth(
|
|
|
292
293
|
: { apiKey: await resolveModelsAuthToken(name, prov), observed: false }
|
|
293
294
|
: resolveAuth.resolve(name, prov));
|
|
294
295
|
const apiKey = auth.apiKey;
|
|
296
|
+
if ((prov.authMode === "oauth" && !apiKey) || isProviderApiKeyPaused(prov)) {
|
|
297
|
+
return observed(configured, "degraded");
|
|
298
|
+
}
|
|
295
299
|
if (name === "github-copilot" && apiKey) {
|
|
296
300
|
const transport = resolveGithubCopilotTransport({ ...prov, apiKey }, prov.authMode === "oauth"
|
|
297
301
|
? resolveCopilotApiBaseUrl(auth.oauthApiBaseUrl) : undefined);
|
|
@@ -649,12 +653,6 @@ export async function fetchProviderModelsWithAuth(
|
|
|
649
653
|
"degraded",
|
|
650
654
|
);
|
|
651
655
|
}
|
|
652
|
-
if (prov.authMode === "oauth" && !apiKey) {
|
|
653
|
-
// No usable token (logged out, or account marked needsReauth). Still surface the
|
|
654
|
-
// configured static catalog so the GUI Models tab / rail counts are not empty —
|
|
655
|
-
// matching Cursor's !apiKey → configured degradation and fetch-failure fallback.
|
|
656
|
-
return observed(configured, "degraded");
|
|
657
|
-
}
|
|
658
656
|
const cloudCodeAssist = effectiveGoogleMode(name, prov) === "cloud-code-assist";
|
|
659
657
|
const project = prov.project ?? auth.oauthProjectId;
|
|
660
658
|
if (cloudCodeAssist && !project) return observed(configured, "degraded");
|
|
@@ -864,11 +862,18 @@ export async function fetchProviderModelsWithAuth(
|
|
|
864
862
|
};
|
|
865
863
|
if (shouldLog) {
|
|
866
864
|
console.warn(
|
|
867
|
-
`[opencodex] Provider model discovery for "${name}" ${diagnostic[extracted.reason]} [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}]${extracted.reason === "invalid_shape" ? invalidModelDiscoveryHint("diagnostic" in extracted ? extracted.diagnostic : undefined) : "."}`,
|
|
865
|
+
`[opencodex] Provider model discovery for "${name}" ${diagnostic[extracted.reason]} [status=${res.status}, contentType=${contentType}, urlClass=${urlClass}, fallback=${fallback}]${extracted.reason === "invalid_shape" ? invalidModelDiscoveryHint("diagnostic" in extracted ? extracted.diagnostic : undefined, discovery.skipInvalidRows === true) : "."}`,
|
|
868
866
|
);
|
|
869
867
|
}
|
|
870
868
|
return observed(models, "degraded");
|
|
871
869
|
}
|
|
870
|
+
// The operator opted into skipping invalid rows (#7910): say how many, once, never which.
|
|
871
|
+
const skippedInvalidRows = "skippedInvalidRows" in extracted ? extracted.skippedInvalidRows ?? 0 : 0;
|
|
872
|
+
if (shouldWarnSkippedModelRows(name, skippedInvalidRows, extracted.rawCount)) {
|
|
873
|
+
console.warn(
|
|
874
|
+
`[opencodex] Provider model discovery for "${name}" skipped ${skippedInvalidRows} invalid of ${extracted.rawCount} rows (modelDiscovery.skipInvalidRows) [status=${res.status}, urlClass=${urlClass}].`,
|
|
875
|
+
);
|
|
876
|
+
}
|
|
872
877
|
const items = extracted.items;
|
|
873
878
|
// Ollama Cloud enrichment: /v1/models carries no per-model context or capability metadata,
|
|
874
879
|
// so a newly announced id would otherwise publish generic defaults. /api/show fills that
|
|
@@ -451,6 +451,8 @@ export interface CatalogProviderDiscoveryPolicySnapshot {
|
|
|
451
451
|
readonly preferFirst: CatalogDiscoveryPolicyField<readonly ProviderModelDiscoveryPredicate[] | undefined>;
|
|
452
452
|
readonly maxResponseBytes: number;
|
|
453
453
|
readonly maxModels: number;
|
|
454
|
+
/** Operator opt-in `modelDiscovery.skipInvalidRows` (#7910). */
|
|
455
|
+
readonly skipInvalidRows: boolean;
|
|
454
456
|
readonly trustedOpenAiApi: CatalogTrustedOpenAiApiPolicySnapshot;
|
|
455
457
|
}
|
|
456
458
|
|
package/src/codex/quota.ts
CHANGED
|
@@ -266,6 +266,78 @@ function shortResetHasElapsed(resetAt: number | undefined, now: number): boolean
|
|
|
266
266
|
return resetAtToMs(resetAt) <= now;
|
|
267
267
|
}
|
|
268
268
|
|
|
269
|
+
/**
|
|
270
|
+
* Whether the account's reset tickets went DOWN, which means one was just redeemed.
|
|
271
|
+
*
|
|
272
|
+
* A manual reset zeroes the current window's usage without ending it: the deadline moves further
|
|
273
|
+
* out, but the account keeps spending in what is effectively a fresh window. The percentage we
|
|
274
|
+
* cached before the redemption measures the window that was thrown away, so it must not be
|
|
275
|
+
* carried into the reset one — the user has, by definition, just bought themselves an empty
|
|
276
|
+
* window, and reporting the old 98% keeps them locked out of the quota they paid to recover.
|
|
277
|
+
*
|
|
278
|
+
* Only a DECREASE counts. Upstream periodically grants new tickets (`available_count` rising),
|
|
279
|
+
* which says nothing about current usage and must never invalidate a reading.
|
|
280
|
+
*
|
|
281
|
+
* This is the one signal that catches a redemption made outside this process — in the ChatGPT web
|
|
282
|
+
* UI, or on another machine. The header stream carries no reset-ticket field at all, so it can
|
|
283
|
+
* only ever be learned from the `/wham/usage` payload, and the hard lock delays that probe by
|
|
284
|
+
* minutes-to-an-hour. Keeping the judgement data-driven and here means the moment any observation
|
|
285
|
+
* does carry the new count, the stale percentage is retired instead of replayed.
|
|
286
|
+
*/
|
|
287
|
+
function resetTicketWasRedeemed(
|
|
288
|
+
incoming: Omit<StoredAccountQuota, "updatedAt">,
|
|
289
|
+
existing: StoredAccountQuota | undefined,
|
|
290
|
+
): boolean {
|
|
291
|
+
const previousCount = existing?.resetCredits;
|
|
292
|
+
const nextCount = incoming.resetCredits;
|
|
293
|
+
if (typeof previousCount !== "number" || !Number.isFinite(previousCount)) return false;
|
|
294
|
+
if (typeof nextCount !== "number" || !Number.isFinite(nextCount)) return false;
|
|
295
|
+
return nextCount < previousCount;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Whether the stored weekly reading belongs to a window that is demonstrably over.
|
|
300
|
+
*
|
|
301
|
+
* This is the weekly counterpart of `shortResetHasElapsed`. The short (5h) branch has always
|
|
302
|
+
* expired a carried tuple whose reset had passed; the weekly branch never did, so a stale
|
|
303
|
+
* weekly percentage could outlive its own deadline indefinitely as long as observations omitted
|
|
304
|
+
* the weekly header. Either of these retires the reading (an observation that carries the weekly
|
|
305
|
+
* window itself, including a later deadline, already replaces the tuple before this is consulted):
|
|
306
|
+
*
|
|
307
|
+
* 1. The stored deadline has already elapsed. Usage only accumulates inside an open window, so a
|
|
308
|
+
* percentage pinned to a closed one is history, not current state. This is exactly the rule
|
|
309
|
+
* `shortResetHasElapsed` applies to the 5h tuple and it is what lets a stuck account re-measure.
|
|
310
|
+
* 2. A reset ticket was redeemed (`resetTicketWasRedeemed`) — the window's usage was zeroed on
|
|
311
|
+
* purpose, so the pre-redemption percentage describes a window nobody is spending in any more.
|
|
312
|
+
*
|
|
313
|
+
* Why this matters beyond a cosmetic bar: this display/rotation snapshot feeds quota exhaustion and
|
|
314
|
+
* the pool rotation score. A percentage that can never expire keeps a recovered account excluded and
|
|
315
|
+
* its card at "usage limit reached" while upstream shows a nearly-empty window. The main-account hard
|
|
316
|
+
* lock is unaffected: it reads the separate policy snapshot, which these signals never touch.
|
|
317
|
+
*
|
|
318
|
+
* Policy callers must NOT use these signals. The hard lock's contract is that only an explicit
|
|
319
|
+
* observation that replaces a blocking reading may release it — "a predicted reset is not
|
|
320
|
+
* recovery evidence". There, a clock elapsing, an advanced deadline, and a redeemed ticket are
|
|
321
|
+
* all non-events; the arriving low percentage is what releases it.
|
|
322
|
+
*
|
|
323
|
+
* Observed live, twice:
|
|
324
|
+
* - Upstream had rolled the window over while every response carried only credits headers, so the
|
|
325
|
+
* old 98% was replayed for ~20h against a true 4%.
|
|
326
|
+
* - The user then redeemed a reset ticket in the web UI. Upstream reported 0% used with a deadline
|
|
327
|
+
* 4.84 days further out, but the cache still said 98%, so the hard lock kept refusing to send.
|
|
328
|
+
* Both times only manual cache surgery recovered the account.
|
|
329
|
+
*/
|
|
330
|
+
function weeklyWindowRolledOver(
|
|
331
|
+
incoming: Omit<StoredAccountQuota, "updatedAt">,
|
|
332
|
+
existing: StoredAccountQuota | undefined,
|
|
333
|
+
now: number,
|
|
334
|
+
): boolean {
|
|
335
|
+
if (resetTicketWasRedeemed(incoming, existing)) return true;
|
|
336
|
+
const previousReset = existing?.weeklyResetAt;
|
|
337
|
+
if (typeof previousReset !== "number" || !Number.isFinite(previousReset) || previousReset <= 0) return false;
|
|
338
|
+
return resetAtToMs(previousReset) <= now;
|
|
339
|
+
}
|
|
340
|
+
|
|
269
341
|
/** Display/rotation carry expires; a reset clock cannot retract blocking hard-lock evidence. */
|
|
270
342
|
function assignCarriedShort(
|
|
271
343
|
next: StoredAccountQuota,
|
|
@@ -409,8 +481,14 @@ function mergeAccountQuota(
|
|
|
409
481
|
}
|
|
410
482
|
|
|
411
483
|
if (creditsOnly) {
|
|
412
|
-
|
|
413
|
-
|
|
484
|
+
// A credits-only refresh says nothing about usage windows, so it normally carries the known
|
|
485
|
+
// ones. The exception is a weekly window upstream has already rolled over: replaying that
|
|
486
|
+
// percentage would report the previous window's usage as the current one, and a
|
|
487
|
+
// credits-only-only stream would keep doing so indefinitely. Policy evidence is exempt for
|
|
488
|
+
// the same reason as below: only an explicit lower observation may release the hard lock.
|
|
489
|
+
const weeklyRolledOver = !policyEvidence && weeklyWindowRolledOver(quota, existing, updatedAt);
|
|
490
|
+
if (existing?.weeklyPercent !== undefined && !weeklyRolledOver) next.weeklyPercent = existing.weeklyPercent;
|
|
491
|
+
if (existing?.weeklyResetAt !== undefined && !weeklyRolledOver) next.weeklyResetAt = existing.weeklyResetAt;
|
|
414
492
|
if (existing?.monthlyPercent !== undefined) next.monthlyPercent = existing.monthlyPercent;
|
|
415
493
|
if (existing?.monthlyResetAt !== undefined) next.monthlyResetAt = existing.monthlyResetAt;
|
|
416
494
|
if (existing?.monthlyIsPrimaryWindow === true) next.monthlyIsPrimaryWindow = true;
|
|
@@ -422,9 +500,19 @@ function mergeAccountQuota(
|
|
|
422
500
|
}
|
|
423
501
|
|
|
424
502
|
const existingWeeklyPercent = existing?.weeklyPercent;
|
|
503
|
+
// The one thing a stale reading must never survive: proof that its window is over.
|
|
504
|
+
// Display only. Policy evidence obeys a stricter contract — see preserveKnownWeekly below.
|
|
505
|
+
const weeklyRolledOver = weeklyWindowRolledOver(quota, existing, updatedAt);
|
|
425
506
|
// A reset-only weekly observation is not a lower reading. Policy mode keeps a blocking weekly
|
|
426
507
|
// tuple whole, exactly like preserveKnownShort; a governing monthly-primary observation still
|
|
427
508
|
// replaces it, so that release path does not depend on this guard.
|
|
509
|
+
//
|
|
510
|
+
// This guard is deliberately NOT relaxed for a rolled-over or advanced window. Policy evidence
|
|
511
|
+
// is the hard lock's only input, and "a predicted reset is not recovery evidence" is a pinned
|
|
512
|
+
// contract: `main-account-hard-lock-policy` asserts that advancing `weeklyResetAt` from now+60s
|
|
513
|
+
// to now+120s alone must NOT clear a retained 99. Releasing the lock has to stay a deliberate,
|
|
514
|
+
// explicitly-observed decision, so the policy path is left exactly as upstream wrote it. The
|
|
515
|
+
// display path below is where a dead reading is allowed to expire.
|
|
428
516
|
const preserveKnownWeekly = policyEvidence
|
|
429
517
|
&& quota.weeklyPercent === undefined
|
|
430
518
|
&& quota.monthlyIsPrimaryWindow !== true
|
|
@@ -438,7 +526,17 @@ function mergeAccountQuota(
|
|
|
438
526
|
&& (!policyEvidence || quota.monthlyIsPrimaryWindow === true)) {
|
|
439
527
|
// Legacy monthly-only clearing is unchanged (#382). Policy needs a governing
|
|
440
528
|
// monthly-primary observation: a tertiary-only header cannot retract weekly99.
|
|
441
|
-
} else if (existing?.weeklyPercent !== undefined
|
|
529
|
+
} else if (existing?.weeklyPercent !== undefined
|
|
530
|
+
&& (policyEvidence || !weeklyRolledOver)) {
|
|
531
|
+
// Carry-forward is for windows whose deadline upstream has not moved. Once it has, the
|
|
532
|
+
// percentage we hold belongs to the closed window; re-publishing it would hand the next
|
|
533
|
+
// window a usage figure it never earned. Leaving the field unset is correct here — the
|
|
534
|
+
// caller treats "unknown" as "needs a fresh measurement", which is exactly the state that
|
|
535
|
+
// lets quota auto-refresh recover.
|
|
536
|
+
//
|
|
537
|
+
// Policy mode is exempt and always carries: it must only ever change through an explicit
|
|
538
|
+
// observation, so it never expires on its own clock. That is the pinned contract above, and
|
|
539
|
+
// admitting either signal here re-opens the "predicted reset releases the lock" hole.
|
|
442
540
|
next.weeklyPercent = existing.weeklyPercent;
|
|
443
541
|
if (existing.weeklyResetAt !== undefined) next.weeklyResetAt = existing.weeklyResetAt;
|
|
444
542
|
}
|
|
@@ -685,23 +783,30 @@ export function updateAccountQuota(
|
|
|
685
783
|
if (nextWeekly === undefined && nextMonthly === undefined && resetCredits === undefined) return;
|
|
686
784
|
hydrateAccountQuotasFromDisk();
|
|
687
785
|
const existing = accountQuota.get(accountId);
|
|
786
|
+
// The WHAM/usage path carries the same staleness hazard as `mergeAccountQuota`: pre-seeding
|
|
787
|
+
// from `existing` and only overwriting when a new value arrives replays an expired weekly
|
|
788
|
+
// reading forever, because a caller that omits the weekly window never clears it. That is the
|
|
789
|
+
// second half of the "weekly bar stuck at 98%" bug - the header path is fixed above, and
|
|
790
|
+
// without this the WHAM path would keep the stale value alive on its own.
|
|
791
|
+
const now = Date.now();
|
|
792
|
+
const weeklyExpired = weeklyWindowRolledOver({}, existing, now);
|
|
688
793
|
|
|
689
794
|
const quota: StoredAccountQuota = {
|
|
690
|
-
...(existing?.weeklyPercent !== undefined ? { weeklyPercent: existing.weeklyPercent } : {}),
|
|
795
|
+
...(existing?.weeklyPercent !== undefined && !weeklyExpired ? { weeklyPercent: existing.weeklyPercent } : {}),
|
|
691
796
|
...(existing?.monthlyPercent !== undefined ? { monthlyPercent: existing.monthlyPercent } : {}),
|
|
692
797
|
// Carry provenance with the value it describes. Dropping it here would downgrade a proven
|
|
693
798
|
// explicit-primary reading to "unproven" on the next unrelated weekly update.
|
|
694
799
|
...(existing?.monthlyPercent !== undefined && existing.monthlyIsPrimaryWindow === true
|
|
695
800
|
? { monthlyIsPrimaryWindow: true }
|
|
696
801
|
: {}),
|
|
697
|
-
...(existing?.weeklyResetAt !== undefined ? { weeklyResetAt: existing.weeklyResetAt } : {}),
|
|
802
|
+
...(existing?.weeklyResetAt !== undefined && !weeklyExpired ? { weeklyResetAt: existing.weeklyResetAt } : {}),
|
|
698
803
|
...(existing?.monthlyResetAt !== undefined ? { monthlyResetAt: existing.monthlyResetAt } : {}),
|
|
699
804
|
...(existing?.customWindows !== undefined ? { customWindows: existing.customWindows } : {}),
|
|
700
805
|
...(existing?.resetCredits !== undefined ? { resetCredits: existing.resetCredits } : {}),
|
|
701
806
|
...(existing?.credits !== undefined ? { credits: existing.credits } : {}),
|
|
702
807
|
...(existing?.credits === null && existing.creditObservationUnavailable === true
|
|
703
808
|
? { creditObservationUnavailable: true as const } : {}),
|
|
704
|
-
updatedAt:
|
|
809
|
+
updatedAt: now,
|
|
705
810
|
};
|
|
706
811
|
assignCarriedShort(quota, existing, quota.updatedAt);
|
|
707
812
|
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Disk-backed cooldown map for combo targets.
|
|
3
|
+
*
|
|
4
|
+
* A cooldown that lives only in memory is forgotten on every restart, so a freshly restarted proxy
|
|
5
|
+
* re-offers a target that upstream said to leave alone until its reset. This Map subclass loads
|
|
6
|
+
* `<config dir>/combo-cooldowns.json` on first use and writes back what this process changed.
|
|
7
|
+
* Expiry is stored as an absolute epoch, so reset-aware cooldowns stay correct across restarts.
|
|
8
|
+
* Pattern adopted from Hermes-agent (MIT, NousResearch): rate-limit state persisted per
|
|
9
|
+
* (provider, model) so a restart does not retry a known-exhausted route.
|
|
10
|
+
*
|
|
11
|
+
* Several proxies can share one home, so a write never replaces the file wholesale:
|
|
12
|
+
* - `set()` marks its key dirty. A flush re-reads the file under the config mutation lock and
|
|
13
|
+
* overlays only dirty keys, keeping the later of the two expiries, so it never shortens a
|
|
14
|
+
* cooldown another process wrote and never rewrites a row it did not touch.
|
|
15
|
+
* - `dropLocal()` (and the Map `delete()`/`clear()` it backs) forgets a row in memory only. Expiry
|
|
16
|
+
* checks, sweeps and the revision-guarded probe lift use it, so an expired local copy can never
|
|
17
|
+
* erase a row another process has since extended.
|
|
18
|
+
* - `clearPersisted()` / `clearAllPersisted()` are the only deletes written to disk: they record
|
|
19
|
+
* user-requested clears (combo save, rename, delete). Clearing everything is a global reset.
|
|
20
|
+
* A busy lock or I/O failure keeps the pending state and retries with bounded backoff.
|
|
21
|
+
*
|
|
22
|
+
* Loading: once per config dir, on first use. Rows already in memory win; a file row loads only
|
|
23
|
+
* while unexpired and gets a fresh revision from the caller's counter (revisions are never stored).
|
|
24
|
+
* A running process does not re-read other processes' rows later.
|
|
25
|
+
*/
|
|
26
|
+
import { mkdirSync, readFileSync } from "node:fs";
|
|
27
|
+
import { join } from "node:path";
|
|
28
|
+
import { atomicWriteFile } from "../config/atomic-write";
|
|
29
|
+
import { withConfigMutationLockSync } from "../config/mutation-lock";
|
|
30
|
+
import { getConfigDir } from "../config/paths";
|
|
31
|
+
|
|
32
|
+
export interface PersistableCooldown { cooldownUntil: number; revision: number; status?: number }
|
|
33
|
+
type StoredRow = { cooldownUntil: number; status?: number };
|
|
34
|
+
|
|
35
|
+
const FILE = "combo-cooldowns.json";
|
|
36
|
+
const FLUSH_DELAY_MS = 250;
|
|
37
|
+
const MAX_FLUSH_DELAY_MS = 4_000;
|
|
38
|
+
const MAX_LOAD_ATTEMPTS = 3;
|
|
39
|
+
const SHUTDOWN_ATTEMPTS = 5;
|
|
40
|
+
const SHUTDOWN_RETRY_MS = 200;
|
|
41
|
+
|
|
42
|
+
function errorCode(error: unknown): string | undefined {
|
|
43
|
+
return error && typeof error === "object" && "code" in error ? String((error as { code?: unknown }).code) : undefined;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Valid rows of the file. Missing file or unparsable JSON is empty; other read errors throw. */
|
|
47
|
+
function readStoredRows(path: string): Record<string, StoredRow> {
|
|
48
|
+
let text: string;
|
|
49
|
+
try {
|
|
50
|
+
text = readFileSync(path, "utf8");
|
|
51
|
+
} catch (error) {
|
|
52
|
+
if (errorCode(error) === "ENOENT") return {};
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
let parsed: unknown;
|
|
56
|
+
try { parsed = JSON.parse(text); } catch { return {}; }
|
|
57
|
+
const rows: Record<string, StoredRow> = {};
|
|
58
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return rows;
|
|
59
|
+
for (const [key, value] of Object.entries(parsed as Record<string, unknown>)) {
|
|
60
|
+
const row = value as { cooldownUntil?: unknown; status?: unknown } | null;
|
|
61
|
+
if (typeof row?.cooldownUntil !== "number" || !Number.isFinite(row.cooldownUntil)) continue;
|
|
62
|
+
rows[key] = { cooldownUntil: row.cooldownUntil, ...(typeof row.status === "number" ? { status: row.status } : {}) };
|
|
63
|
+
}
|
|
64
|
+
return rows;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export class PersistedCooldownMap<V extends PersistableCooldown> extends Map<string, V> {
|
|
68
|
+
private loadedFor: string | undefined;
|
|
69
|
+
private failedLoadDir: string | undefined;
|
|
70
|
+
private failedLoads = 0;
|
|
71
|
+
private timer: ReturnType<typeof setTimeout> | undefined;
|
|
72
|
+
private delayMs = FLUSH_DELAY_MS;
|
|
73
|
+
private scheduledDir: string | undefined;
|
|
74
|
+
private readonly dirty = new Set<string>();
|
|
75
|
+
private readonly tombstones = new Set<string>();
|
|
76
|
+
private clearAll = false;
|
|
77
|
+
private readonly nextRevision: () => number;
|
|
78
|
+
|
|
79
|
+
constructor(nextRevision: () => number) {
|
|
80
|
+
super();
|
|
81
|
+
this.nextRevision = nextRevision;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private ensureLoaded(): void {
|
|
85
|
+
const dir = getConfigDir();
|
|
86
|
+
if (this.loadedFor === dir) return;
|
|
87
|
+
if (this.failedLoadDir === dir && this.failedLoads >= MAX_LOAD_ATTEMPTS) return;
|
|
88
|
+
let rows: Record<string, StoredRow>;
|
|
89
|
+
try {
|
|
90
|
+
rows = readStoredRows(join(dir, FILE));
|
|
91
|
+
} catch {
|
|
92
|
+
// A transient read failure (permissions, a locked file on Windows) retries on a later access.
|
|
93
|
+
if (this.failedLoadDir !== dir) { this.failedLoadDir = dir; this.failedLoads = 0; }
|
|
94
|
+
this.failedLoads += 1;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.loadedFor = dir;
|
|
98
|
+
const now = Date.now();
|
|
99
|
+
for (const [key, row] of Object.entries(rows)) {
|
|
100
|
+
if (row.cooldownUntil <= now || super.has(key)) continue;
|
|
101
|
+
super.set(key, { cooldownUntil: row.cooldownUntil, revision: this.nextRevision(), ...(row.status !== undefined ? { status: row.status } : {}) } as V);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private pending(): boolean {
|
|
106
|
+
return this.clearAll || this.dirty.size > 0 || this.tombstones.size > 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private schedule(): void {
|
|
110
|
+
this.scheduledDir ??= getConfigDir();
|
|
111
|
+
if (this.timer) return;
|
|
112
|
+
this.timer = setTimeout(() => { this.timer = undefined; this.flush(); }, this.delayMs);
|
|
113
|
+
(this.timer as { unref?: () => void }).unref?.();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Write pending changes now. Returns false, keeping them and rescheduling, if the write could not run. */
|
|
117
|
+
flush(): boolean {
|
|
118
|
+
if (!this.pending()) return true;
|
|
119
|
+
const dir = this.scheduledDir ?? getConfigDir();
|
|
120
|
+
const path = join(dir, FILE);
|
|
121
|
+
try {
|
|
122
|
+
withConfigMutationLockSync(() => {
|
|
123
|
+
const file = readStoredRows(path);
|
|
124
|
+
const now = Date.now();
|
|
125
|
+
const out: Record<string, StoredRow> = {};
|
|
126
|
+
if (!this.clearAll) {
|
|
127
|
+
for (const [key, row] of Object.entries(file)) {
|
|
128
|
+
if (row.cooldownUntil > now && !this.tombstones.has(key)) out[key] = row;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
for (const key of this.dirty) {
|
|
132
|
+
const own = super.get(key);
|
|
133
|
+
if (!own || own.cooldownUntil <= now) continue;
|
|
134
|
+
const other = out[key];
|
|
135
|
+
out[key] = other && other.cooldownUntil > own.cooldownUntil
|
|
136
|
+
? other
|
|
137
|
+
: { cooldownUntil: own.cooldownUntil, ...(own.status !== undefined ? { status: own.status } : {}) };
|
|
138
|
+
}
|
|
139
|
+
mkdirSync(dir, { recursive: true });
|
|
140
|
+
atomicWriteFile(path, JSON.stringify(out));
|
|
141
|
+
}, dir);
|
|
142
|
+
} catch {
|
|
143
|
+
// Lock held by another process (busy_timeout 0) or an I/O error: keep everything and retry.
|
|
144
|
+
this.delayMs = Math.min(this.delayMs * 2, MAX_FLUSH_DELAY_MS);
|
|
145
|
+
if (!this.timer) this.schedule();
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
this.dirty.clear();
|
|
149
|
+
this.tombstones.clear();
|
|
150
|
+
this.clearAll = false;
|
|
151
|
+
this.scheduledDir = undefined;
|
|
152
|
+
this.delayMs = FLUSH_DELAY_MS;
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Bounded final write for process shutdown; never blocks for more than about a second. */
|
|
157
|
+
flushForShutdown(): void {
|
|
158
|
+
if (this.timer) { clearTimeout(this.timer); this.timer = undefined; }
|
|
159
|
+
for (let attempt = 0; attempt < SHUTDOWN_ATTEMPTS; attempt += 1) {
|
|
160
|
+
if (this.flush()) break;
|
|
161
|
+
if (this.timer) { clearTimeout(this.timer); this.timer = undefined; }
|
|
162
|
+
if (attempt < SHUTDOWN_ATTEMPTS - 1) Bun.sleepSync(SHUTDOWN_RETRY_MS);
|
|
163
|
+
}
|
|
164
|
+
if (this.timer) { clearTimeout(this.timer); this.timer = undefined; }
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Forget a row in memory only (expiry, sweep, probe lift); nothing is written. */
|
|
168
|
+
dropLocal(key: string): boolean { this.ensureLoaded(); return super.delete(key); }
|
|
169
|
+
|
|
170
|
+
/** A user-requested clear of these keys, written to disk as deletes. */
|
|
171
|
+
clearPersisted(keys: Iterable<string>): void {
|
|
172
|
+
this.ensureLoaded();
|
|
173
|
+
for (const key of keys) {
|
|
174
|
+
super.delete(key);
|
|
175
|
+
this.dirty.delete(key);
|
|
176
|
+
this.tombstones.add(key);
|
|
177
|
+
}
|
|
178
|
+
this.schedule();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** A user-requested clear of every row, written to disk as a global reset. */
|
|
182
|
+
clearAllPersisted(): void {
|
|
183
|
+
this.ensureLoaded();
|
|
184
|
+
super.clear();
|
|
185
|
+
this.dirty.clear();
|
|
186
|
+
this.tombstones.clear();
|
|
187
|
+
this.clearAll = true;
|
|
188
|
+
this.schedule();
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
override get(key: string): V | undefined { this.ensureLoaded(); return super.get(key); }
|
|
192
|
+
override has(key: string): boolean { this.ensureLoaded(); return super.has(key); }
|
|
193
|
+
override get size(): number { this.ensureLoaded(); return super.size; }
|
|
194
|
+
override keys(): MapIterator<string> { this.ensureLoaded(); return super.keys(); }
|
|
195
|
+
override values(): MapIterator<V> { this.ensureLoaded(); return super.values(); }
|
|
196
|
+
override entries(): MapIterator<[string, V]> { this.ensureLoaded(); return super.entries(); }
|
|
197
|
+
override [Symbol.iterator](): MapIterator<[string, V]> { this.ensureLoaded(); return super[Symbol.iterator](); }
|
|
198
|
+
override set(key: string, value: V): this {
|
|
199
|
+
this.ensureLoaded();
|
|
200
|
+
super.set(key, value);
|
|
201
|
+
this.tombstones.delete(key);
|
|
202
|
+
this.dirty.add(key);
|
|
203
|
+
this.schedule();
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
override delete(key: string): boolean { return this.dropLocal(key); }
|
|
207
|
+
override clear(): void { this.ensureLoaded(); super.clear(); }
|
|
208
|
+
}
|