opencode-quota-gui 1.3.1 → 1.3.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/README.md +16 -12
- package/dist/contracts/build-identity.js +1 -1
- package/dist/contracts/card-order.d.ts +2 -0
- package/dist/contracts/card-order.js +2 -2
- package/dist/contracts/card-visibility.d.ts +2 -1
- package/dist/contracts/card-visibility.js +2 -1
- package/dist/contracts/dashboard.d.ts +87 -5
- package/dist/contracts/dashboard.js +10 -0
- package/dist/contracts/dto-guards.d.ts +4 -0
- package/dist/contracts/dto-guards.js +29 -7
- package/dist/contracts/guards.js +3 -1
- package/dist/contracts/identity.d.ts +37 -5
- package/dist/contracts/identity.js +43 -0
- package/dist/contracts/provider-auto-resume.d.ts +9 -0
- package/dist/contracts/provider-auto-resume.js +24 -0
- package/dist/contracts/provider-failure.d.ts +22 -0
- package/dist/contracts/provider-failure.js +25 -1
- package/dist/contracts/row-order.d.ts +13 -0
- package/dist/contracts/row-order.js +34 -0
- package/dist/contracts/row-visibility.d.ts +53 -0
- package/dist/contracts/row-visibility.js +130 -0
- package/dist/data/modelsdev-pricing.min.json +1 -1
- package/dist/data/modelsdev-temporal.min.json +3 -634
- package/dist/gui/aggregation.d.ts +29 -1
- package/dist/gui/aggregation.js +157 -4
- package/dist/gui/analytics-bridge.d.ts +8 -9
- package/dist/gui/analytics-bridge.js +197 -54
- package/dist/gui/analytics-engine.d.ts +7 -15
- package/dist/gui/analytics-engine.js +246 -106
- package/dist/gui/analytics-model.d.ts +7 -0
- package/dist/gui/analytics-model.js +75 -9
- package/dist/gui/analytics-pricing-upkeep.d.ts +21 -0
- package/dist/gui/analytics-pricing-upkeep.js +23 -0
- package/dist/gui/analytics-protocol.d.ts +17 -7
- package/dist/gui/analytics-protocol.js +1 -0
- package/dist/gui/analytics-service.d.ts +10 -0
- package/dist/gui/analytics-service.js +30 -9
- package/dist/gui/analytics-state.d.ts +30 -5
- package/dist/gui/analytics-state.js +46 -7
- package/dist/gui/analytics-worker-runtime.d.ts +5 -0
- package/dist/gui/analytics-worker-runtime.js +9 -1
- package/dist/gui/analytics-worker.js +11 -0
- package/dist/gui/config.d.ts +2 -2
- package/dist/gui/config.js +46 -5
- package/dist/gui/identity.d.ts +6 -20
- package/dist/gui/identity.js +6 -54
- package/dist/gui/primary-quota-windows.js +55 -34
- package/dist/gui/provider-cards.d.ts +12 -8
- package/dist/gui/provider-cards.js +12 -53
- package/dist/gui/quota-notification-detector.d.ts +25 -2
- package/dist/gui/quota-notification-detector.js +60 -5
- package/dist/gui/quota-notification-observer.d.ts +1 -1
- package/dist/gui/quota-notification-observer.js +5 -1
- package/dist/gui/quota-observation.d.ts +4 -0
- package/dist/gui/quota-observation.js +18 -0
- package/dist/gui/rpc.js +3 -0
- package/dist/gui/server.js +6 -1
- package/dist/gui/snapshot-service.d.ts +3 -17
- package/dist/gui/snapshot-service.js +159 -9
- package/dist/gui/sources.js +84 -11
- package/dist/gui/temporal-status.d.ts +3 -2
- package/dist/gui/temporal-status.js +10 -6
- package/dist/gui-experimental.cjs +228 -228
- package/dist/lib/accounting-format.d.ts +8 -0
- package/dist/lib/accounting-format.js +12 -2
- package/dist/lib/anthropic-api-auth.d.ts +7 -0
- package/dist/lib/anthropic-api-auth.js +27 -0
- package/dist/lib/anthropic-api-rate-limits.d.ts +47 -0
- package/dist/lib/anthropic-api-rate-limits.js +92 -0
- package/dist/lib/anthropic-oauth-throttle.d.ts +76 -0
- package/dist/lib/anthropic-oauth-throttle.js +379 -0
- package/dist/lib/anthropic.d.ts +22 -4
- package/dist/lib/anthropic.js +498 -180
- package/dist/lib/cache-observer-ingestion.js +27 -0
- package/dist/lib/cache-observer.d.ts +3 -0
- package/dist/lib/cache-observer.js +9 -3
- package/dist/lib/cli-show.d.ts +5 -0
- package/dist/lib/cli-show.js +16 -5
- package/dist/lib/cli-status.js +3 -6
- package/dist/lib/config.d.ts +1 -1
- package/dist/lib/config.js +64 -2
- package/dist/lib/dashboard-visibility.d.ts +5 -0
- package/dist/lib/dashboard-visibility.js +11 -10
- package/dist/lib/display-sanitize.js +1 -0
- package/dist/lib/entries.d.ts +10 -1
- package/dist/lib/entries.js +4 -0
- package/dist/lib/grouped-entry-normalization.js +1 -1
- package/dist/lib/maintainer-announcements.js +2 -2
- package/dist/lib/modelsdev-pricing.d.ts +74 -2
- package/dist/lib/modelsdev-pricing.js +457 -63
- package/dist/lib/modelsdev-temporal.d.ts +6 -0
- package/dist/lib/modelsdev-temporal.js +16 -2
- package/dist/lib/opencode-config-providers.js +2 -2
- package/dist/lib/opencode-storage.d.ts +20 -0
- package/dist/lib/opencode-storage.js +89 -2
- package/dist/lib/opencode-version-core.d.ts +7 -0
- package/dist/lib/opencode-version-core.js +10 -0
- package/dist/lib/opencode-warming-refresh.d.ts +19 -0
- package/dist/lib/opencode-warming-refresh.js +95 -0
- package/dist/lib/provider-metadata.d.ts +11 -3
- package/dist/lib/provider-metadata.js +20 -2
- package/dist/lib/provider-registration.d.ts +342 -3
- package/dist/lib/provider-registration.js +409 -27
- package/dist/lib/quota-auto-resume.d.ts +74 -0
- package/dist/lib/quota-auto-resume.js +539 -0
- package/dist/lib/quota-command-format.js +22 -9
- package/dist/lib/quota-identity.d.ts +64 -0
- package/dist/lib/quota-identity.js +111 -0
- package/dist/lib/quota-providers.js +2 -2
- package/dist/lib/quota-render-data.js +2 -2
- package/dist/lib/quota-reset-notifications.js +11 -155
- package/dist/lib/quota-retry-wait.d.ts +106 -5
- package/dist/lib/quota-retry-wait.js +301 -34
- package/dist/lib/quota-runtime-context.js +1 -0
- package/dist/lib/quota-state-codec.d.ts +2 -0
- package/dist/lib/quota-state-codec.js +14 -1
- package/dist/lib/quota-state.d.ts +1 -1
- package/dist/lib/quota-state.js +60 -28
- package/dist/lib/quota-stats.d.ts +26 -0
- package/dist/lib/quota-stats.js +19 -1
- package/dist/lib/quota-surface-data.js +18 -3
- package/dist/lib/quota-telemetry.js +2 -2
- package/dist/lib/resolved-auth-identity.d.ts +19 -0
- package/dist/lib/resolved-auth-identity.js +44 -0
- package/dist/lib/state-file-lock.d.ts +26 -0
- package/dist/lib/state-file-lock.js +205 -0
- package/dist/lib/toast-format-grouped.js +7 -4
- package/dist/lib/tui-compact-format.js +29 -16
- package/dist/lib/types.d.ts +20 -0
- package/dist/lib/types.js +4 -1
- package/dist/plugin.js +67 -1
- package/dist/providers/alibaba-coding-plan.js +21 -3
- package/dist/providers/alibaba-token-plan.js +9 -5
- package/dist/providers/anthropic.d.ts +6 -2
- package/dist/providers/anthropic.js +123 -14
- package/dist/providers/chutes.js +2 -0
- package/dist/providers/claude-agent.js +24 -5
- package/dist/providers/copilot.js +5 -4
- package/dist/providers/glm-coding-plan-provider.js +30 -23
- package/dist/providers/ollama-cloud.js +10 -0
- package/dist/providers/openai.js +24 -23
- package/dist/providers/opencode-go.js +7 -6
- package/dist/providers/opencode-zen.js +1 -1
- package/dist/providers/result-helpers.d.ts +4 -2
- package/dist/providers/result-helpers.js +5 -1
- package/dist/providers/synthetic.js +4 -0
- package/dist/providers/xai.js +9 -8
- package/dist/rpc.d.ts +8 -0
- package/dist/rpc.js +15 -2
- package/dist/ui/assets/{index-BcueIm3Z.css → index-6_g1FHs-.css} +1 -1
- package/dist/ui/assets/index-sZnGUuML.js +10 -0
- package/dist/ui/assets/{popover-4bFSvRZ7.js → popover-C4ZLYe9E.js} +1 -1
- package/dist/ui/index.html +2 -2
- package/package.json +3 -3
- package/dist/ui/assets/index-C6qRSoh8.js +0 -10
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isCardHidden } from "../contracts/card-visibility.js";
|
|
3
|
+
import { optionalRowId, rowFieldKey, rowIdForEntry, } from "../contracts/identity.js";
|
|
4
|
+
import { effectiveHiddenRowIds } from "../contracts/row-visibility.js";
|
|
5
|
+
import { getQuotaProviderRegistration } from "./provider-metadata.js";
|
|
6
|
+
/**
|
|
7
|
+
* Server-side card identity and entry visibility.
|
|
8
|
+
*
|
|
9
|
+
* The browser-safe row-key/id builders live in `src/contracts/identity.ts`;
|
|
10
|
+
* this module adds the `node:crypto` card hash and the visibility predicate so
|
|
11
|
+
* both `src/lib` and `src/gui` consume one implementation without importing
|
|
12
|
+
* each other.
|
|
13
|
+
*/
|
|
14
|
+
/**
|
|
15
|
+
* Reserved identity key for the provider-scope card (entries/errors that no
|
|
16
|
+
* discovered account owns). It is a scope marker, never an account id.
|
|
17
|
+
*/
|
|
18
|
+
export const PROVIDER_SCOPE_IDENTITY_KEY = "provider";
|
|
19
|
+
const CARD_HASH_DOMAIN = "ocq-card-v1";
|
|
20
|
+
/** Deterministic sha256 hex of the domain-separated (providerId, identityKey) pair. */
|
|
21
|
+
export function hashQuotaCardIdentity(providerId, identityKey) {
|
|
22
|
+
return createHash("sha256")
|
|
23
|
+
.update(`${CARD_HASH_DOMAIN}\0${providerId}\0${identityKey}`, "utf8")
|
|
24
|
+
.digest("hex");
|
|
25
|
+
}
|
|
26
|
+
/** Opaque card id: provider namespace + server-side hash; raw identities never appear. */
|
|
27
|
+
export function buildCardId(providerId, identityKey) {
|
|
28
|
+
// SAFETY: CardId is the opaque brand of exactly `<providerId>:<sha256>`; this
|
|
29
|
+
// builder is its only producer and the hash covers both raw inputs.
|
|
30
|
+
return `${providerId}:${hashQuotaCardIdentity(providerId, identityKey)}`;
|
|
31
|
+
}
|
|
32
|
+
/** The provider-scope card id (unscoped rows/errors, or no discovered accounts). */
|
|
33
|
+
export function buildProviderScopeCardId(providerId) {
|
|
34
|
+
return buildCardId(providerId, PROVIDER_SCOPE_IDENTITY_KEY);
|
|
35
|
+
}
|
|
36
|
+
/** The card a row belongs to: its account card, or the provider-scope card. */
|
|
37
|
+
function cardIdForEntry(providerId, result, entry) {
|
|
38
|
+
const account = (result.accounts ?? []).find((candidate) => candidate.sourceId === entry.accounting.sourceId);
|
|
39
|
+
return account
|
|
40
|
+
? buildCardId(providerId, account.identityKey?.trim() ? account.identityKey : account.sourceId)
|
|
41
|
+
: buildProviderScopeCardId(providerId);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Persistent Settings controls from the provider registration table.
|
|
45
|
+
*
|
|
46
|
+
* The control id is always the declared field id, so it stays stable when the
|
|
47
|
+
* provider renders the field in another shape. Every supported alternate shape
|
|
48
|
+
* (declared explicitly, plus any live emitted shape when the rowKey has one
|
|
49
|
+
* declaration) is an alias of that control. Emitted row ids never change.
|
|
50
|
+
*/
|
|
51
|
+
export function optionalVisibilityRowsFor(providerId, cardId, rows = []) {
|
|
52
|
+
const declared = getQuotaProviderRegistration(providerId)?.optionalVisibilityRows;
|
|
53
|
+
if (!declared || declared.length === 0)
|
|
54
|
+
return undefined;
|
|
55
|
+
const declarationsByField = new Map();
|
|
56
|
+
for (const row of declared) {
|
|
57
|
+
declarationsByField.set(row.rowKey, (declarationsByField.get(row.rowKey) ?? 0) + 1);
|
|
58
|
+
}
|
|
59
|
+
return declared.map((row) => {
|
|
60
|
+
const rowId = optionalRowId(cardId, row.rowKey, row.resultType, row.kind);
|
|
61
|
+
const aliases = new Set();
|
|
62
|
+
for (const shape of row.alternateShapes ?? []) {
|
|
63
|
+
const alias = optionalRowId(cardId, row.rowKey, shape.resultType, shape.kind);
|
|
64
|
+
if (alias !== rowId)
|
|
65
|
+
aliases.add(alias);
|
|
66
|
+
}
|
|
67
|
+
// A rowKey with one declaration names one field: keep any live emitted
|
|
68
|
+
// shape of that field on the same control. Duplicate declarations are two
|
|
69
|
+
// distinct fields (for example a budget and a spend month), so only their
|
|
70
|
+
// exact ids may match.
|
|
71
|
+
if (declarationsByField.get(row.rowKey) === 1) {
|
|
72
|
+
for (const entry of rows) {
|
|
73
|
+
if (entry.rowId !== rowId && rowFieldKey(entry.rowId) === row.rowKey) {
|
|
74
|
+
aliases.add(entry.rowId);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
rowId,
|
|
80
|
+
label: row.label,
|
|
81
|
+
info: row.info,
|
|
82
|
+
...(aliases.size > 0 ? { aliases: [...aliases] } : {}),
|
|
83
|
+
};
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Whether one entry's live row is hidden by the effective card/row settings
|
|
88
|
+
* the browser and native surfaces use. Shared by dashboard cards and the
|
|
89
|
+
* retained legacy reset notice path so a hidden field is silent everywhere.
|
|
90
|
+
* A saved pre-window aggregate id resolves against the emitted entries to the
|
|
91
|
+
* first row it used to match; a declared control resolves through its aliases.
|
|
92
|
+
*/
|
|
93
|
+
export function quotaEntryIsHidden(params) {
|
|
94
|
+
const cardId = cardIdForEntry(params.providerId, params.result, params.entry);
|
|
95
|
+
if (isCardHidden({
|
|
96
|
+
cardId,
|
|
97
|
+
providerId: params.providerId,
|
|
98
|
+
providerScopeHash: hashQuotaCardIdentity(params.providerId, PROVIDER_SCOPE_IDENTITY_KEY),
|
|
99
|
+
}, new Set(params.hiddenCards ?? []))) {
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
const hiddenRows = params.hiddenRows ?? [];
|
|
103
|
+
if (hiddenRows.length === 0)
|
|
104
|
+
return false;
|
|
105
|
+
const cardRowIds = (params.result.entries ?? [params.entry])
|
|
106
|
+
.filter((entry) => cardIdForEntry(params.providerId, params.result, entry) === cardId)
|
|
107
|
+
.map((entry) => rowIdForEntry(cardId, entry));
|
|
108
|
+
const controls = optionalVisibilityRowsFor(params.providerId, cardId, cardRowIds.map((rowId) => ({ rowId })));
|
|
109
|
+
return effectiveHiddenRowIds(cardRowIds, hiddenRows, controls).has(rowIdForEntry(cardId, params.entry));
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=quota-identity.js.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isIP } from "node:net";
|
|
2
2
|
import { lookupCost } from "./modelsdev-pricing.js";
|
|
3
|
-
import {
|
|
3
|
+
import { getQuotaProviderRegistration } from "./provider-metadata.js";
|
|
4
4
|
import { resolvePricingKey } from "./quota-stats.js";
|
|
5
5
|
export const QUOTA_PROVIDER_REMOTE_FORMATS = ["quota-v1", "openrouter-key-v1", "json-v1"];
|
|
6
6
|
export const QUOTA_PROVIDER_MODES = ["remote-api", "local-estimate"];
|
|
@@ -938,7 +938,7 @@ export function validateQuotaProviders(value) {
|
|
|
938
938
|
message: `"${QUOTA_PROVIDERS_AGGREGATE_ID}" is reserved for the aggregate provider`,
|
|
939
939
|
});
|
|
940
940
|
}
|
|
941
|
-
const builtIn =
|
|
941
|
+
const builtIn = getQuotaProviderRegistration(id);
|
|
942
942
|
if (builtIn && !MAINTAINED_LOCAL_ESTIMATE_IDS.includes(id)) {
|
|
943
943
|
issues.push({
|
|
944
944
|
key: `${providerKey}.id`,
|
|
@@ -2,7 +2,7 @@ import { getAnthropicNoDataMessage } from "../providers/anthropic.js";
|
|
|
2
2
|
import { getClaudeAgentNoDataMessage } from "../providers/claude-agent.js";
|
|
3
3
|
import { getProviders } from "../providers/registry.js";
|
|
4
4
|
import { cloneQuotaToastEntry } from "./entries.js";
|
|
5
|
-
import { getQuotaProviderDisplayLabel, getQuotaProviderIdsForRuntimeId,
|
|
5
|
+
import { getQuotaProviderDisplayLabel, getQuotaProviderIdsForRuntimeId, getQuotaProviderRegistration, } from "./provider-metadata.js";
|
|
6
6
|
import { projectQuotaProviderResults } from "./quota-accounting-projection.js";
|
|
7
7
|
import { createQuotaProviderRuntimeContext } from "./quota-runtime-context.js";
|
|
8
8
|
import { fetchQuotaProviderResult, getQuotaProviderResultScope, QUOTA_FORCE_REFRESH, } from "./quota-state.js";
|
|
@@ -62,7 +62,7 @@ export function matchesQuotaProviderCurrentSelection(params) {
|
|
|
62
62
|
}
|
|
63
63
|
if (params.currentProviderID) {
|
|
64
64
|
const explicitId = params.currentProviderID.trim().toLowerCase();
|
|
65
|
-
const catalogShape =
|
|
65
|
+
const catalogShape = getQuotaProviderRegistration(explicitId);
|
|
66
66
|
if (catalogShape?.id === explicitId)
|
|
67
67
|
return params.provider.id === catalogShape.id;
|
|
68
68
|
const runtimeCandidates = getQuotaProviderIdsForRuntimeId(explicitId);
|
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
import { createHash
|
|
2
|
-
import {
|
|
3
|
-
import { mkdir, open, readFile, rm, rmdir, stat, unlink } from "node:fs/promises";
|
|
4
|
-
import { dirname, join } from "node:path";
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
5
3
|
import { writeJsonAtomic } from "./atomic-json.js";
|
|
6
|
-
import { isPercentEntry } from "./entries.js";
|
|
4
|
+
import { isPercentEntry, } from "./entries.js";
|
|
7
5
|
import { getQuotaResetNotificationsStatePath } from "./private-state-paths.js";
|
|
8
6
|
import { getQuotaProviderDisplayLabel } from "./provider-metadata.js";
|
|
9
|
-
import {
|
|
7
|
+
import { acquireStateFileLock } from "./state-file-lock.js";
|
|
10
8
|
const STATE_VERSION = 2;
|
|
11
9
|
/** Pure shared reset-transition age limit. */
|
|
12
10
|
export const MAX_TRANSITION_AGE_MS = 24 * 60 * 60 * 1000;
|
|
13
11
|
const MAX_OBSERVATION_AGE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
14
|
-
const LOCK_RETRY_DELAY_MS = 20;
|
|
15
|
-
const LOCK_TIMEOUT_MS = 2_000;
|
|
16
|
-
const STALE_LOCK_AGE_MS = 30_000;
|
|
17
|
-
const LOCK_OWNER_FILE = "owner.json";
|
|
18
|
-
const LOCK_RECLAIM_FILE = "reclaim";
|
|
19
12
|
const WINDOW_KINDS = {
|
|
20
13
|
fiveHour: "five_hour",
|
|
21
14
|
hourly: "hour",
|
|
@@ -27,11 +20,13 @@ const WINDOW_KINDS = {
|
|
|
27
20
|
function getStatePath() {
|
|
28
21
|
return getQuotaResetNotificationsStatePath();
|
|
29
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Reset-window eligibility comes only from typed semantic metadata. Display
|
|
25
|
+
* labels are never matched; an untyped row cannot prove its window and is
|
|
26
|
+
* skipped instead of guessing.
|
|
27
|
+
*/
|
|
30
28
|
function getWindow(entry) {
|
|
31
|
-
|
|
32
|
-
return entry.semantic.metric.kind === "window" ? entry.semantic.metric.window : null;
|
|
33
|
-
}
|
|
34
|
-
return classifyQuotaWindowText(entry.label ?? "") ?? classifyQuotaWindowText(entry.name);
|
|
29
|
+
return entry.semantic?.metric.kind === "window" ? entry.semantic.metric.window : null;
|
|
35
30
|
}
|
|
36
31
|
function getConfiguredWindow(kind, configured) {
|
|
37
32
|
if (!kind)
|
|
@@ -69,145 +64,6 @@ async function readState(path) {
|
|
|
69
64
|
return { version: STATE_VERSION, observations: {} };
|
|
70
65
|
}
|
|
71
66
|
}
|
|
72
|
-
function isFileSystemError(error, code) {
|
|
73
|
-
return error?.code === code;
|
|
74
|
-
}
|
|
75
|
-
function delay(ms) {
|
|
76
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
77
|
-
}
|
|
78
|
-
function parseLockOwner(value) {
|
|
79
|
-
try {
|
|
80
|
-
const owner = JSON.parse(value);
|
|
81
|
-
if (typeof owner.token !== "string" ||
|
|
82
|
-
!Number.isSafeInteger(owner.pid) ||
|
|
83
|
-
(owner.pid ?? 0) <= 0 ||
|
|
84
|
-
!Number.isFinite(owner.createdAtMs)) {
|
|
85
|
-
return null;
|
|
86
|
-
}
|
|
87
|
-
return owner;
|
|
88
|
-
}
|
|
89
|
-
catch {
|
|
90
|
-
return null;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
async function readLockOwner(lockPath) {
|
|
94
|
-
try {
|
|
95
|
-
return parseLockOwner(await readFile(join(lockPath, LOCK_OWNER_FILE), "utf8"));
|
|
96
|
-
}
|
|
97
|
-
catch (error) {
|
|
98
|
-
if (isFileSystemError(error, "ENOENT"))
|
|
99
|
-
return null;
|
|
100
|
-
throw error;
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
function isProcessAlive(pid) {
|
|
104
|
-
try {
|
|
105
|
-
process.kill(pid, 0);
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
catch (error) {
|
|
109
|
-
return !isFileSystemError(error, "ESRCH");
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
async function getReclaimStatus(lockPath) {
|
|
113
|
-
const owner = await readLockOwner(lockPath);
|
|
114
|
-
if (owner)
|
|
115
|
-
return { reclaimable: !isProcessAlive(owner.pid), allowMissingOwner: false };
|
|
116
|
-
try {
|
|
117
|
-
const lock = await stat(lockPath);
|
|
118
|
-
const stale = Date.now() - lock.mtimeMs > STALE_LOCK_AGE_MS;
|
|
119
|
-
return { reclaimable: stale, allowMissingOwner: stale };
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
if (isFileSystemError(error, "ENOENT")) {
|
|
123
|
-
return { reclaimable: true, allowMissingOwner: true };
|
|
124
|
-
}
|
|
125
|
-
throw error;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
async function tryReclaimStateLock(lockPath, allowMissingOwner) {
|
|
129
|
-
const reclaimPath = join(lockPath, LOCK_RECLAIM_FILE);
|
|
130
|
-
try {
|
|
131
|
-
const handle = await open(reclaimPath, "wx", 0o600);
|
|
132
|
-
await handle.close();
|
|
133
|
-
}
|
|
134
|
-
catch (error) {
|
|
135
|
-
if (isFileSystemError(error, "ENOENT"))
|
|
136
|
-
return true;
|
|
137
|
-
if (!isFileSystemError(error, "EEXIST"))
|
|
138
|
-
throw error;
|
|
139
|
-
try {
|
|
140
|
-
const reclaim = await stat(reclaimPath);
|
|
141
|
-
if (Date.now() - reclaim.mtimeMs <= STALE_LOCK_AGE_MS)
|
|
142
|
-
return false;
|
|
143
|
-
}
|
|
144
|
-
catch (statError) {
|
|
145
|
-
if (isFileSystemError(statError, "ENOENT"))
|
|
146
|
-
return false;
|
|
147
|
-
throw statError;
|
|
148
|
-
}
|
|
149
|
-
const owner = await readLockOwner(lockPath);
|
|
150
|
-
if (owner && isProcessAlive(owner.pid)) {
|
|
151
|
-
await unlink(reclaimPath).catch(() => undefined);
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
if (!owner && !allowMissingOwner)
|
|
155
|
-
return false;
|
|
156
|
-
await rm(lockPath, { recursive: true, force: true });
|
|
157
|
-
return true;
|
|
158
|
-
}
|
|
159
|
-
const owner = await readLockOwner(lockPath);
|
|
160
|
-
const reclaimable = owner ? !isProcessAlive(owner.pid) : allowMissingOwner;
|
|
161
|
-
if (reclaimable) {
|
|
162
|
-
await rm(lockPath, { recursive: true, force: true });
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
await unlink(reclaimPath).catch((error) => {
|
|
166
|
-
if (!isFileSystemError(error, "ENOENT"))
|
|
167
|
-
throw error;
|
|
168
|
-
});
|
|
169
|
-
return false;
|
|
170
|
-
}
|
|
171
|
-
async function acquireStateLock(statePath) {
|
|
172
|
-
await mkdir(dirname(statePath), { recursive: true, mode: 0o700 });
|
|
173
|
-
const lockPath = `${statePath}.lock`;
|
|
174
|
-
const token = randomUUID();
|
|
175
|
-
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
176
|
-
while (true) {
|
|
177
|
-
try {
|
|
178
|
-
await mkdir(lockPath, { mode: 0o700 });
|
|
179
|
-
const owner = { token, pid: process.pid, createdAtMs: Date.now() };
|
|
180
|
-
try {
|
|
181
|
-
writeFileSync(join(lockPath, LOCK_OWNER_FILE), JSON.stringify(owner), {
|
|
182
|
-
flag: "wx",
|
|
183
|
-
mode: 0o600,
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
catch (error) {
|
|
187
|
-
await rmdir(lockPath).catch(() => undefined);
|
|
188
|
-
throw error;
|
|
189
|
-
}
|
|
190
|
-
return async () => {
|
|
191
|
-
const currentOwner = await readLockOwner(lockPath);
|
|
192
|
-
if (currentOwner?.token !== token)
|
|
193
|
-
return;
|
|
194
|
-
await rm(lockPath, { recursive: true, force: true });
|
|
195
|
-
};
|
|
196
|
-
}
|
|
197
|
-
catch (error) {
|
|
198
|
-
if (!isFileSystemError(error, "EEXIST"))
|
|
199
|
-
throw error;
|
|
200
|
-
}
|
|
201
|
-
const status = await getReclaimStatus(lockPath);
|
|
202
|
-
if (status.reclaimable && (await tryReclaimStateLock(lockPath, status.allowMissingOwner))) {
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
if (Date.now() >= deadline) {
|
|
206
|
-
throw new Error(`Timed out acquiring quota reset notification state lock: ${lockPath}`);
|
|
207
|
-
}
|
|
208
|
-
await delay(LOCK_RETRY_DELAY_MS);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
67
|
/**
|
|
212
68
|
* Pure reset proof. The legacy notifier depends on this exact predicate, so do
|
|
213
69
|
* not fork it.
|
|
@@ -298,7 +154,7 @@ export async function observeQuotaResetNotifications(params) {
|
|
|
298
154
|
windows: params.windows,
|
|
299
155
|
nowMs,
|
|
300
156
|
});
|
|
301
|
-
const releaseLock = await
|
|
157
|
+
const releaseLock = await acquireStateFileLock(statePath);
|
|
302
158
|
try {
|
|
303
159
|
const state = await readState(statePath);
|
|
304
160
|
const observations = Object.fromEntries(Object.entries(state.observations).filter(([, observation]) => nowMs - observation.observedAtMs <= MAX_OBSERVATION_AGE_MS));
|
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
* `waitForQuotaReset` (on by default): when a model request hits a provider limit and the quota data
|
|
3
3
|
* shows a used-up window for that model's provider, OpenCode retries once that window resets
|
|
4
4
|
* instead of following its normal retry timing.
|
|
5
|
+
*
|
|
6
|
+
* `autoResumeOnQuotaRecovery` reuses the same window walk and probe: after a terminal quota failure
|
|
7
|
+
* it waits for the failed route's verified identity to recover, then sends one continuation prompt.
|
|
5
8
|
*/
|
|
6
9
|
import { type QuotaProviderResult } from "./entries.js";
|
|
10
|
+
import { type QuotaStatusLiveProbe } from "./quota-render-data.js";
|
|
7
11
|
import type { QuotaSurfaceHost } from "./quota-surface-data.js";
|
|
12
|
+
import type { QuotaToastConfig } from "./types.js";
|
|
8
13
|
/** Extra wait after the reset time, so the provider has reset the window when the retry runs. */
|
|
9
14
|
export declare const QUOTA_RESET_RETRY_BUFFER_MS = 60000;
|
|
10
|
-
/** Longest
|
|
15
|
+
/** Longest wait for a reset; the buffer is added after this cap so a retry never lands just before it. */
|
|
11
16
|
export declare const QUOTA_RESET_RETRY_MAX_DELAY_MS: number;
|
|
12
17
|
/** The parts of OpenCode's retry event this feature reads. */
|
|
13
18
|
export type QuotaRetryEvent = {
|
|
@@ -18,19 +23,115 @@ export type QuotaRetryEvent = {
|
|
|
18
23
|
readonly error: {
|
|
19
24
|
readonly type: string;
|
|
20
25
|
readonly status?: number;
|
|
26
|
+
/** The provider asked to wait at least this long before the next request. */
|
|
27
|
+
readonly retryAfterMs?: number;
|
|
21
28
|
};
|
|
22
29
|
};
|
|
23
|
-
/** OpenCode's error types for a provider rate limit or used-up quota, or any HTTP 429. */
|
|
24
30
|
export declare function isQuotaLimitError(error: QuotaRetryEvent["error"]): boolean;
|
|
31
|
+
/** A percent row observed for the verified binding. */
|
|
32
|
+
type QuotaPercentWindow = {
|
|
33
|
+
/** Stable row identity from `semantic` or `rowKey`; absent when the row has neither. */
|
|
34
|
+
key?: string;
|
|
35
|
+
percentRemaining: number;
|
|
36
|
+
resetMs?: number;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The verified identity every collected window belongs to. Auto-resume requires one; the row
|
|
40
|
+
* walk for `waitForQuotaReset` can run without it.
|
|
41
|
+
*/
|
|
42
|
+
export type QuotaWindowBinding = {
|
|
43
|
+
/** An OpenCode credential row, matched against entry/result account ids. */
|
|
44
|
+
readonly kind: "account";
|
|
45
|
+
readonly sourceId: string;
|
|
46
|
+
} | {
|
|
47
|
+
/**
|
|
48
|
+
* A provider-level identity from the provider cache scope. Only used for single-source
|
|
49
|
+
* routes whose adapter result exposes no account ids of its own.
|
|
50
|
+
*/
|
|
51
|
+
readonly kind: "provider-scope";
|
|
52
|
+
readonly providerId: string;
|
|
53
|
+
readonly scope: string;
|
|
54
|
+
};
|
|
55
|
+
/** The percent rows of the active identity. */
|
|
56
|
+
export type QuotaWindowCollection = {
|
|
57
|
+
/** The identity these rows belong to; absent when the observation is not attributable. */
|
|
58
|
+
binding?: QuotaWindowBinding;
|
|
59
|
+
windows: QuotaPercentWindow[];
|
|
60
|
+
};
|
|
61
|
+
export type QuotaWindowCollectionOptions = {
|
|
62
|
+
/**
|
|
63
|
+
* Auto-resume evidence only: require a verified binding and keep only primary provider route
|
|
64
|
+
* windows. Supplementary, budget, value, named, code-review and MCP rows do not govern a failed
|
|
65
|
+
* model request and are excluded unless `includeNamedWindows` names them.
|
|
66
|
+
*/
|
|
67
|
+
primaryAccountOnly?: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Named windows that are primary evidence for a specific model route. Auto-resume uses this for
|
|
70
|
+
* Claude Agent's model-scoped `Fable weekly` row; the caller decides whether the failed model
|
|
71
|
+
* makes it applicable.
|
|
72
|
+
*/
|
|
73
|
+
includeNamedWindows?: readonly string[];
|
|
74
|
+
};
|
|
75
|
+
export declare function sameQuotaWindowBinding(left: QuotaWindowBinding | undefined, right: QuotaWindowBinding | undefined): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Walks the percent rows for the verified binding. A window is exhausted when its percentage is
|
|
78
|
+
* zero. `resetMs` is the parsed reset instant when the row carries one. A provider-scope binding
|
|
79
|
+
* requires exactly one unaccounted provider result, so two sources can never be mixed.
|
|
80
|
+
*/
|
|
81
|
+
export declare function collectQuotaWindows(results: readonly QuotaProviderResult[], binding?: QuotaWindowBinding, options?: QuotaWindowCollectionOptions): QuotaWindowCollection | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Milliseconds until all used-up windows have reset, plus the buffer, with the reset wait at most
|
|
84
|
+
* the cap. The buffer is added after the cap so the retry never lands just before a reset.
|
|
85
|
+
*/
|
|
86
|
+
export declare function getQuotaResetRetryDelayMs(results: readonly QuotaProviderResult[], nowMs: number, activeSourceId?: string): number | undefined;
|
|
87
|
+
/** Canonical providers for a runtime route that declare auto-resume evidence. */
|
|
88
|
+
export declare function getQuotaAutoResumeProviderIds(modelProviderID: string): ReadonlySet<string>;
|
|
89
|
+
/**
|
|
90
|
+
* Picks the verified identity for auto-resume evidence. A single-source route uses the provider
|
|
91
|
+
* cache scope of its unaccounted result (the Claude CLI login, one API key); everything else
|
|
92
|
+
* needs an active OpenCode credential row. Ambiguous or missing evidence yields undefined.
|
|
93
|
+
*/
|
|
94
|
+
export declare function resolveQuotaEvidenceBinding(params: {
|
|
95
|
+
readonly modelProviderID: string;
|
|
96
|
+
readonly providerResults: readonly QuotaStatusLiveProbe[];
|
|
97
|
+
readonly activeSourceId?: string;
|
|
98
|
+
}): QuotaWindowBinding | undefined;
|
|
99
|
+
export type QuotaModelProbe = {
|
|
100
|
+
config: QuotaToastConfig;
|
|
101
|
+
providerResults: QuotaStatusLiveProbe[];
|
|
102
|
+
/** The active OpenCode credential row, used by the reset-retry path. */
|
|
103
|
+
activeSourceId?: string;
|
|
104
|
+
/** Verified identity for auto-resume evidence. */
|
|
105
|
+
binding?: QuotaWindowBinding;
|
|
106
|
+
};
|
|
107
|
+
export type QuotaModelProbeState = {
|
|
108
|
+
readonly state: "disabled";
|
|
109
|
+
} | {
|
|
110
|
+
readonly state: "unsupported";
|
|
111
|
+
} | {
|
|
112
|
+
readonly state: "unavailable";
|
|
113
|
+
} | {
|
|
114
|
+
readonly state: "probe";
|
|
115
|
+
readonly probe: QuotaModelProbe;
|
|
116
|
+
};
|
|
25
117
|
/**
|
|
26
|
-
*
|
|
27
|
-
*
|
|
118
|
+
* One live quota probe for the model's provider, keeping "the feature is off", "this route can
|
|
119
|
+
* never supply auto-resume evidence", and "the evidence is temporarily unavailable" distinct.
|
|
120
|
+
* Auto-resume stops immediately for the first two and keeps polling the last.
|
|
28
121
|
*/
|
|
29
|
-
export declare function
|
|
122
|
+
export declare function probeQuotaForModelState(host: Pick<QuotaSurfaceHost, "client" | "roots">, model: {
|
|
123
|
+
readonly id: string;
|
|
124
|
+
readonly providerID: string;
|
|
125
|
+
}, isEnabled: (config: QuotaToastConfig) => boolean, options?: {
|
|
126
|
+
includeProviderScope?: boolean;
|
|
127
|
+
}): Promise<QuotaModelProbeState>;
|
|
128
|
+
/** One live quota probe for the model's provider. `undefined` when disabled or unavailable. */
|
|
129
|
+
export declare function probeQuotaForModel(...args: Parameters<typeof probeQuotaForModelState>): Promise<QuotaModelProbe | undefined>;
|
|
30
130
|
/**
|
|
31
131
|
* The retry delay for a limit error on `event.model`, or undefined to keep OpenCode's decision.
|
|
32
132
|
* Quota comes from the same provider refresh path as the sidebar and toasts, but skips the cache:
|
|
33
133
|
* the limit error itself shows the quota changed, so the provider is asked once for fresh data.
|
|
34
134
|
*/
|
|
35
135
|
export declare function resolveQuotaResetRetryDelayMs(host: Pick<QuotaSurfaceHost, "client" | "roots">, event: QuotaRetryEvent): Promise<number | undefined>;
|
|
136
|
+
export {};
|
|
36
137
|
//# sourceMappingURL=quota-retry-wait.d.ts.map
|