opencode-quota-gui 1.3.0 → 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 +34 -20
- package/dist/bin/opencode-quota-gui.js +0 -0
- 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 +95 -6
- package/dist/contracts/dashboard.js +10 -0
- package/dist/contracts/dto-guards.d.ts +4 -0
- package/dist/contracts/dto-guards.js +31 -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/openai-quota.d.ts +3 -0
- package/dist/contracts/openai-quota.js +3 -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 +60 -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 +3 -3
- package/dist/gui/config.js +77 -7
- package/dist/gui/identity.d.ts +6 -20
- package/dist/gui/identity.js +6 -54
- package/dist/gui/lifecycle.d.ts +5 -3
- package/dist/gui/lifecycle.js +3 -3
- package/dist/gui/primary-quota-windows.js +67 -44
- package/dist/gui/provider-cards.d.ts +20 -8
- package/dist/gui/provider-cards.js +42 -55
- package/dist/gui/quota-notification-detector.d.ts +29 -6
- package/dist/gui/quota-notification-detector.js +136 -47
- package/dist/gui/quota-notification-observer.d.ts +3 -4
- package/dist/gui/quota-notification-observer.js +73 -20
- package/dist/gui/quota-observation.d.ts +4 -0
- package/dist/gui/quota-observation.js +18 -0
- package/dist/gui/rpc.d.ts +2 -0
- package/dist/gui/rpc.js +14 -3
- package/dist/gui/server.js +17 -3
- package/dist/gui/snapshot-service.d.ts +3 -15
- package/dist/gui/snapshot-service.js +281 -23
- package/dist/gui/sources.js +179 -64
- package/dist/gui/temporal-status.d.ts +3 -2
- package/dist/gui/temporal-status.js +10 -6
- package/dist/gui-experimental.cjs +230 -225
- package/dist/lib/accounting-format.d.ts +8 -0
- package/dist/lib/accounting-format.js +20 -2
- package/dist/lib/alibaba-coding-plan-cli.d.ts +79 -0
- package/dist/lib/alibaba-coding-plan-cli.js +539 -0
- 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-auth.d.ts +2 -3
- package/dist/lib/anthropic-auth.js +2 -3
- 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 +68 -14
- package/dist/lib/anthropic.js +1016 -363
- package/dist/lib/api-key-retry-after.d.ts +7 -0
- package/dist/lib/api-key-retry-after.js +62 -0
- package/dist/lib/atomic-json.d.ts +2 -0
- package/dist/lib/atomic-json.js +1 -0
- package/dist/lib/cache-observer-ingestion.d.ts +90 -0
- package/dist/lib/cache-observer-ingestion.js +1008 -0
- package/dist/lib/cache-observer.d.ts +141 -0
- package/dist/lib/cache-observer.js +323 -0
- package/dist/lib/canonical-config-lock.d.ts +9 -0
- package/dist/lib/canonical-config-lock.js +36 -0
- package/dist/lib/chutes.js +62 -51
- package/dist/lib/claude-agent-transcript-ttl.d.ts +48 -0
- package/dist/lib/claude-agent-transcript-ttl.js +154 -0
- package/dist/lib/claude-agent-ttl-tracker.d.ts +21 -0
- package/dist/lib/claude-agent-ttl-tracker.js +152 -0
- package/dist/lib/cli-show.d.ts +5 -0
- package/dist/lib/cli-show.js +21 -8
- package/dist/lib/cli-status.js +3 -6
- package/dist/lib/config-write-target.d.ts +1 -1
- package/dist/lib/config-write-target.js +5 -2
- package/dist/lib/config.d.ts +2 -1
- package/dist/lib/config.js +257 -4
- package/dist/lib/copilot.js +171 -86
- package/dist/lib/dashboard-visibility.d.ts +5 -0
- package/dist/lib/dashboard-visibility.js +11 -10
- package/dist/lib/dashboard-web-policy.d.ts +4 -4
- package/dist/lib/dashboard-web-policy.js +2 -2
- package/dist/lib/deepseek.js +25 -13
- package/dist/lib/display-sanitize.js +2 -0
- package/dist/lib/entries.d.ts +33 -1
- package/dist/lib/entries.js +6 -0
- package/dist/lib/glm-coding-plan.js +98 -74
- package/dist/lib/google-agy.js +90 -12
- package/dist/lib/google-gemini-cli.js +84 -11
- package/dist/lib/grouped-entry-normalization.js +1 -1
- package/dist/lib/http.d.ts +4 -0
- package/dist/lib/http.js +16 -2
- package/dist/lib/init-installer.js +5 -3
- package/dist/lib/kilo.js +37 -18
- package/dist/lib/kimi.js +35 -18
- package/dist/lib/maintainer-announcements.js +2 -2
- package/dist/lib/mimo.d.ts +2 -0
- package/dist/lib/mimo.js +36 -0
- 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/nanogpt.d.ts +1 -0
- package/dist/lib/nanogpt.js +26 -3
- package/dist/lib/ollama-cloud.d.ts +1 -1
- package/dist/lib/ollama-cloud.js +48 -35
- package/dist/lib/openai.js +84 -38
- package/dist/lib/opencode-auth-sqlite.d.ts +2 -2
- package/dist/lib/opencode-auth-sqlite.js +2 -2
- package/dist/lib/opencode-config-editor.d.ts +1 -0
- package/dist/lib/opencode-config-editor.js +6 -1
- package/dist/lib/opencode-config-providers.js +2 -2
- package/dist/lib/opencode-go.js +29 -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 +30 -0
- package/dist/lib/opencode-version-core.js +59 -0
- package/dist/lib/opencode-version.d.ts +2 -22
- package/dist/lib/opencode-version.js +4 -48
- package/dist/lib/opencode-warming-refresh.d.ts +19 -0
- package/dist/lib/opencode-warming-refresh.js +95 -0
- package/dist/lib/opencode-zen.d.ts +3 -0
- package/dist/lib/opencode-zen.js +71 -5
- package/dist/lib/provider-add.js +5 -2
- package/dist/lib/provider-metadata.d.ts +11 -3
- package/dist/lib/provider-metadata.js +20 -2
- package/dist/lib/provider-registration.d.ts +358 -9
- package/dist/lib/provider-registration.js +423 -30
- 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 +23 -10
- package/dist/lib/quota-exhaustion-projection.d.ts +11 -0
- package/dist/lib/quota-exhaustion-projection.js +25 -0
- package/dist/lib/quota-identity.d.ts +64 -0
- package/dist/lib/quota-identity.js +111 -0
- package/dist/lib/quota-providers-remote.d.ts +2 -1
- package/dist/lib/quota-providers-remote.js +59 -3
- package/dist/lib/quota-providers.d.ts +1 -1
- package/dist/lib/quota-providers.js +14 -37
- package/dist/lib/quota-render-data.d.ts +6 -0
- package/dist/lib/quota-render-data.js +45 -19
- 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 +31 -2
- package/dist/lib/quota-state.d.ts +8 -2
- package/dist/lib/quota-state.js +253 -47
- package/dist/lib/quota-stats.d.ts +26 -0
- package/dist/lib/quota-stats.js +40 -1
- package/dist/lib/quota-status.js +3 -51
- 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/scoped-update.d.ts +1 -1
- package/dist/lib/state-file-lock.d.ts +26 -0
- package/dist/lib/state-file-lock.js +205 -0
- package/dist/lib/synthetic.js +65 -54
- package/dist/lib/toast-format-grouped.js +7 -4
- package/dist/lib/tui-compact-format.js +29 -16
- package/dist/lib/types.d.ts +51 -1
- package/dist/lib/types.js +17 -1
- package/dist/lib/xai.js +48 -22
- package/dist/plugin.js +328 -14
- package/dist/provider-logos/NOTICE.md +1 -1
- package/dist/provider-logos/OPENCODE-NOTICE.md +4 -3
- package/dist/providers/alibaba-coding-plan.js +125 -104
- package/dist/providers/alibaba-token-plan.js +9 -5
- package/dist/providers/anthropic.d.ts +16 -3
- package/dist/providers/anthropic.js +172 -220
- package/dist/providers/cache-policies.d.ts +11 -4
- package/dist/providers/cache-policies.js +19 -13
- package/dist/providers/chutes.js +2 -0
- package/dist/providers/claude-agent.d.ts +19 -0
- package/dist/providers/claude-agent.js +215 -0
- package/dist/providers/copilot.js +7 -4
- package/dist/providers/glm-coding-plan-provider.d.ts +1 -0
- package/dist/providers/glm-coding-plan-provider.js +30 -23
- package/dist/providers/google-account-format.js +1 -0
- package/dist/providers/google-agy.d.ts +6 -5
- package/dist/providers/google-agy.js +43 -15
- package/dist/providers/google-gemini-cli.js +1 -0
- package/dist/providers/kilo.js +1 -0
- package/dist/providers/kimi-code.js +2 -0
- package/dist/providers/mimo.js +1 -0
- package/dist/providers/nanogpt.js +1 -0
- package/dist/providers/ollama-cloud.js +10 -0
- package/dist/providers/openai.js +24 -23
- package/dist/providers/opencode-go.js +10 -6
- package/dist/providers/opencode-zen.js +3 -1
- package/dist/providers/openrouter.js +1 -0
- package/dist/providers/quota-providers.js +16 -5
- package/dist/providers/registry.js +2 -0
- package/dist/providers/result-helpers.d.ts +6 -2
- package/dist/providers/result-helpers.js +7 -1
- package/dist/providers/synthetic.js +4 -0
- package/dist/providers/xai.js +9 -8
- package/dist/rpc.d.ts +129 -8
- package/dist/rpc.js +219 -14
- package/dist/ui/assets/{index-cDd9jUVY.css → index-6_g1FHs-.css} +1 -1
- package/dist/ui/assets/index-sZnGUuML.js +10 -0
- package/dist/ui/assets/{popover-BGkgv9s0.js → popover-C4ZLYe9E.js} +1 -1
- package/dist/ui/assets/providers/claude-agent.svg +3 -0
- package/dist/ui/index.html +2 -2
- package/package.json +45 -46
- package/dist/lib/alibaba-coding-plan-local-quota.d.ts +0 -47
- package/dist/lib/alibaba-coding-plan-local-quota.js +0 -146
- package/dist/ui/assets/index-DLHeXYff.js +0 -9
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `autoResumeOnQuotaRecovery` (off by default): after a session terminally fails on a genuine
|
|
3
|
+
* provider quota limit, wait until the failed route's exhausted quota window is observed as reset
|
|
4
|
+
* for the same verified identity, then send exactly one `continue` prompt if the session is still
|
|
5
|
+
* failed, idle and on the same model.
|
|
6
|
+
*
|
|
7
|
+
* Recovery evidence follows the same reset-transition rule as the quota reset notifications: a
|
|
8
|
+
* raised percentage on an unchanged or missing reset boundary is not proof. A temporarily
|
|
9
|
+
* unavailable observation (probe failure, provider cooldown, RPC timeout, missing row or identity)
|
|
10
|
+
* keeps the pending resume and polls again; only authoritative events and evidence cancel it.
|
|
11
|
+
* The first observation that shows a used-up window with a future reset boundary becomes the
|
|
12
|
+
* baseline, so an unavailable first probe never loses the continuation but also never authorizes
|
|
13
|
+
* it without the later transition. A disabled setting or a route whose provider registration has
|
|
14
|
+
* no `autoResume` contract cancels the pending resume and is not polled again.
|
|
15
|
+
*
|
|
16
|
+
* OpenCode 2.4.1 loses the structured quota error for some Claude Agent subscription limits and
|
|
17
|
+
* exposes only a message. Known Claude subscription-limit messages are recognized for this module
|
|
18
|
+
* only; they still require an independently observed exhausted five-hour or weekly window (plus
|
|
19
|
+
* the Fable weekly row only for a Fable model) for the same verified identity. Native retry
|
|
20
|
+
* classification and provider cooldown policies are never changed by that recognition.
|
|
21
|
+
*
|
|
22
|
+
* In-process only. There is no durable queue: a plugin restart drops a pending resume (accepted;
|
|
23
|
+
* the next terminal failure arms a new cycle). No tool action is replayed.
|
|
24
|
+
*/
|
|
25
|
+
import { getQuotaProviderIdsForRuntimeId } from "./provider-metadata.js";
|
|
26
|
+
import { updateObservation } from "./quota-reset-notifications.js";
|
|
27
|
+
import { isQuotaLimitError, sameQuotaWindowBinding, } from "./quota-retry-wait.js";
|
|
28
|
+
/** Confirmed recovery must hold this long before the single continuation. */
|
|
29
|
+
export const QUOTA_AUTO_RESUME_GRACE_MS = 120_000;
|
|
30
|
+
/** The single continuation prompt; no native continue API exists. */
|
|
31
|
+
export const QUOTA_AUTO_RESUME_PROMPT = "continue";
|
|
32
|
+
/** Marker used to ignore the module's own prompt and to make the continuation visible. */
|
|
33
|
+
export const QUOTA_AUTO_RESUME_SOURCE = "opencode-quota-gui-autoresume";
|
|
34
|
+
/** `setTimeout` overflows above this; longer waits are re-armed in chunks. */
|
|
35
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
36
|
+
/** Poll interval after an observation gap; a provider cooldown still paces the actual request. */
|
|
37
|
+
const QUOTA_AUTO_RESUME_RETRY_MS = 60_000;
|
|
38
|
+
function failureError(value) {
|
|
39
|
+
if (value === null || typeof value !== "object")
|
|
40
|
+
return undefined;
|
|
41
|
+
const error = value;
|
|
42
|
+
if (typeof error.type !== "string")
|
|
43
|
+
return undefined;
|
|
44
|
+
return {
|
|
45
|
+
type: error.type,
|
|
46
|
+
...(typeof error.message === "string" ? { message: error.message } : {}),
|
|
47
|
+
...(typeof error.status === "number" ? { status: error.status } : {}),
|
|
48
|
+
...(typeof error.retryAfterMs === "number" && Number.isFinite(error.retryAfterMs)
|
|
49
|
+
? { retryAfterMs: error.retryAfterMs }
|
|
50
|
+
: {}),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Known Claude Agent subscription-limit messages. Auto-resume-only: `isQuotaLimitError` and the
|
|
55
|
+
* native retry hook never see this recognition.
|
|
56
|
+
*
|
|
57
|
+
* OpenCode 2.4.1's claude-agent error converter turns a rejected `rate_limit_event` plus an empty
|
|
58
|
+
* result into `Claude Code subscription limit reached (<window>). Resets at <ISO>.`, and it
|
|
59
|
+
* preserves Claude Code's own text when the result is non-empty. The converter and message shape:
|
|
60
|
+
* https://github.com/anomalyco/opencode/blob/v2.4.1/packages/core/src/plugin/provider/claude-agent/language-model/claude-agent-error.ts
|
|
61
|
+
* Observed payloads and the lossy UnknownProvider classification:
|
|
62
|
+
* https://github.com/anomalyco/opencode/issues/63189 (session text and the empty-result form).
|
|
63
|
+
* Claude Code uses the same `You've hit your <window> limit · resets ...` shape for its weekly
|
|
64
|
+
* limit. `five_hour`/`seven_day` and the other rate-limit types are in the Claude Agent SDK
|
|
65
|
+
* `SDKRateLimitInfo` type: https://unpkg.com/@anthropic-ai/claude-agent-sdk/sdk.d.ts
|
|
66
|
+
*/
|
|
67
|
+
const CLAUDE_SUBSCRIPTION_LIMIT_MESSAGES = [
|
|
68
|
+
{ pattern: /^Claude Code subscription limit reached \(five hour\)/, window: "five_hour" },
|
|
69
|
+
{ pattern: /^Claude Code subscription limit reached \(seven day\)/, window: "week" },
|
|
70
|
+
{ pattern: /^Claude Code subscription limit reached\b/, window: "any" },
|
|
71
|
+
{
|
|
72
|
+
pattern: /^You've hit your (?:session|five-hour|5-hour) limit\b/i,
|
|
73
|
+
window: "five_hour",
|
|
74
|
+
},
|
|
75
|
+
{ pattern: /^You've hit your weekly limit\b/i, window: "week" },
|
|
76
|
+
];
|
|
77
|
+
function claudeSubscriptionWindow(message) {
|
|
78
|
+
if (message === undefined)
|
|
79
|
+
return undefined;
|
|
80
|
+
const normalized = message.trim();
|
|
81
|
+
return CLAUDE_SUBSCRIPTION_LIMIT_MESSAGES.find(({ pattern }) => pattern.test(normalized))?.window;
|
|
82
|
+
}
|
|
83
|
+
/** Claude Agent's runtime ids normalize to this canonical provider in the registration catalog. */
|
|
84
|
+
function isClaudeAgentRoute(providerID) {
|
|
85
|
+
return getQuotaProviderIdsForRuntimeId(providerID).includes("claude-agent");
|
|
86
|
+
}
|
|
87
|
+
/** The `named:Fable weekly` row belongs to the Fable model only (anthropicQuotaEntries). */
|
|
88
|
+
function isFableModel(modelID) {
|
|
89
|
+
return modelID !== undefined && /fable/i.test(modelID);
|
|
90
|
+
}
|
|
91
|
+
/** The window keys that can confirm a recognized Claude subscription-limit message. */
|
|
92
|
+
function subscriptionWindowKeys(resume) {
|
|
93
|
+
if (resume.subscription === "five_hour")
|
|
94
|
+
return ["window:five_hour"];
|
|
95
|
+
const keys = ["window:week"];
|
|
96
|
+
if (isFableModel(resume.modelID))
|
|
97
|
+
keys.push("named:Fable weekly");
|
|
98
|
+
if (resume.subscription === "any")
|
|
99
|
+
keys.unshift("window:five_hour");
|
|
100
|
+
return keys;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A message alone never arms recovery. At least one applicable window must be independently
|
|
104
|
+
* observed as exhausted with a future reset for the same verified binding. A missing row or reset
|
|
105
|
+
* is temporary evidence; a present row that still has quota contradicts the message.
|
|
106
|
+
*/
|
|
107
|
+
function subscriptionConfirmation(collection, resume, nowMs) {
|
|
108
|
+
const expected = subscriptionWindowKeys(resume);
|
|
109
|
+
const present = collection.windows.filter((window) => window.key !== undefined && expected.includes(window.key));
|
|
110
|
+
if (present.some((window) => window.percentRemaining <= 0 && window.resetMs !== undefined && window.resetMs > nowMs)) {
|
|
111
|
+
return "confirmed";
|
|
112
|
+
}
|
|
113
|
+
// A possible window row is missing: absence of evidence, not proof against the message.
|
|
114
|
+
if (present.length < expected.length)
|
|
115
|
+
return "unavailable";
|
|
116
|
+
if (present.every((window) => window.percentRemaining > 0))
|
|
117
|
+
return "contradicted";
|
|
118
|
+
// Exhausted but no future reset yet: keep polling for usable evidence.
|
|
119
|
+
return "unavailable";
|
|
120
|
+
}
|
|
121
|
+
/** Applies model-scoped row semantics before a window can become recovery evidence. */
|
|
122
|
+
function isApplicableWindow(window, resume) {
|
|
123
|
+
return window.key !== "named:Fable weekly" || isFableModel(resume.modelID);
|
|
124
|
+
}
|
|
125
|
+
function sessionIDOf(data) {
|
|
126
|
+
if (data === null || typeof data !== "object")
|
|
127
|
+
return undefined;
|
|
128
|
+
const sessionID = data.sessionID;
|
|
129
|
+
return typeof sessionID === "string" && sessionID !== "" ? sessionID : undefined;
|
|
130
|
+
}
|
|
131
|
+
function sameModel(info, pending) {
|
|
132
|
+
return (pending.modelID !== undefined &&
|
|
133
|
+
pending.providerID !== undefined &&
|
|
134
|
+
info.model?.id === pending.modelID &&
|
|
135
|
+
info.model.providerID === pending.providerID);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Creates the in-process auto-resume controller. Feed every host event through `handleEvent`,
|
|
139
|
+
* user prompt submissions through `onPrompt`, and call `dispose` from the plugin cleanup.
|
|
140
|
+
*/
|
|
141
|
+
export function createQuotaAutoResumption(deps) {
|
|
142
|
+
const now = deps.now ?? Date.now;
|
|
143
|
+
const pending = new Map();
|
|
144
|
+
/** Sessions where an auto-resume already ran for the current user-initiated execution. */
|
|
145
|
+
const consumed = new Set();
|
|
146
|
+
let disposed = false;
|
|
147
|
+
function clearTimer(resume) {
|
|
148
|
+
if (resume.timer !== undefined) {
|
|
149
|
+
clearTimeout(resume.timer);
|
|
150
|
+
resume.timer = undefined;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
function cancel(sessionID) {
|
|
154
|
+
const resume = pending.get(sessionID);
|
|
155
|
+
if (!resume)
|
|
156
|
+
return;
|
|
157
|
+
clearTimer(resume);
|
|
158
|
+
pending.delete(sessionID);
|
|
159
|
+
}
|
|
160
|
+
function cancelAll() {
|
|
161
|
+
for (const sessionID of [...pending.keys()])
|
|
162
|
+
cancel(sessionID);
|
|
163
|
+
}
|
|
164
|
+
function schedule(sessionID, resume, atMs) {
|
|
165
|
+
clearTimer(resume);
|
|
166
|
+
const delay = Math.min(Math.max(1, atMs - now()), MAX_TIMER_MS);
|
|
167
|
+
resume.timer = setTimeout(() => {
|
|
168
|
+
resume.timer = undefined;
|
|
169
|
+
void evaluate(sessionID, resume);
|
|
170
|
+
}, delay);
|
|
171
|
+
resume.timer.unref?.();
|
|
172
|
+
}
|
|
173
|
+
function retryLater(sessionID, resume) {
|
|
174
|
+
schedule(sessionID, resume, now() + QUOTA_AUTO_RESUME_RETRY_MS);
|
|
175
|
+
}
|
|
176
|
+
async function readSession(sessionID) {
|
|
177
|
+
try {
|
|
178
|
+
return await deps.session.get({ sessionID });
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
function sessionState(info, resume) {
|
|
185
|
+
if (info === undefined)
|
|
186
|
+
return { kind: "unavailable" };
|
|
187
|
+
if (info.parentID !== undefined)
|
|
188
|
+
return { kind: "cancel" };
|
|
189
|
+
if (info.outcome !== undefined && info.outcome !== "failed")
|
|
190
|
+
return { kind: "cancel" };
|
|
191
|
+
if (info.outcome === undefined || info.model === undefined)
|
|
192
|
+
return { kind: "unavailable" };
|
|
193
|
+
// The first successful read defines the route; a later change invalidates the continuation.
|
|
194
|
+
if (resume.modelID !== undefined && !sameModel(info, resume))
|
|
195
|
+
return { kind: "cancel" };
|
|
196
|
+
if (resume.idleAtArm !== undefined) {
|
|
197
|
+
const idle = info.time?.idle;
|
|
198
|
+
if (idle === undefined)
|
|
199
|
+
return { kind: "unavailable" };
|
|
200
|
+
if (idle !== resume.idleAtArm)
|
|
201
|
+
return { kind: "cancel" };
|
|
202
|
+
}
|
|
203
|
+
return { kind: "ok", info };
|
|
204
|
+
}
|
|
205
|
+
/** Reads the session; cancels or re-polls itself and returns `undefined` unless it is usable. */
|
|
206
|
+
async function readLiveSession(sessionID, resume) {
|
|
207
|
+
const state = sessionState(await readSession(sessionID), resume);
|
|
208
|
+
if (disposed || pending.get(sessionID) !== resume)
|
|
209
|
+
return undefined;
|
|
210
|
+
if (state.kind === "cancel") {
|
|
211
|
+
cancel(sessionID);
|
|
212
|
+
return undefined;
|
|
213
|
+
}
|
|
214
|
+
if (state.kind === "unavailable") {
|
|
215
|
+
retryLater(sessionID, resume);
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
return state.info;
|
|
219
|
+
}
|
|
220
|
+
async function probeQuota(sessionID, resume, model) {
|
|
221
|
+
let result;
|
|
222
|
+
try {
|
|
223
|
+
result = await deps.probe(model);
|
|
224
|
+
}
|
|
225
|
+
catch {
|
|
226
|
+
result = undefined;
|
|
227
|
+
}
|
|
228
|
+
// The controller may have been disposed or superseded while the probe ran.
|
|
229
|
+
if (disposed || pending.get(sessionID) !== resume)
|
|
230
|
+
return { kind: "cancelled" };
|
|
231
|
+
// A disabled feature or a permanently unsupported route stops the pending resume.
|
|
232
|
+
if (result === undefined || result.kind === "absent")
|
|
233
|
+
return { kind: "unavailable" };
|
|
234
|
+
if (result.kind === "disabled")
|
|
235
|
+
return { kind: "disabled" };
|
|
236
|
+
if (result.kind === "unsupported")
|
|
237
|
+
return { kind: "unsupported" };
|
|
238
|
+
const collection = result.collection;
|
|
239
|
+
if (resume.binding !== undefined) {
|
|
240
|
+
// A different verified identity means this continuation is no longer valid.
|
|
241
|
+
if (collection.binding === undefined)
|
|
242
|
+
return { kind: "unavailable" };
|
|
243
|
+
if (!sameQuotaWindowBinding(resume.binding, collection.binding))
|
|
244
|
+
return { kind: "identity" };
|
|
245
|
+
}
|
|
246
|
+
// Keep the first verified identity for every later observation.
|
|
247
|
+
resume.binding ??= collection.binding;
|
|
248
|
+
return { kind: "collection", collection };
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Applies one observation to every tracked window. Recovery needs the shared reset-transition
|
|
252
|
+
* proof (`updateObservation`), so a raised percentage alone cannot fire a continuation.
|
|
253
|
+
*/
|
|
254
|
+
function observeWindows(collection, resume, nowMs) {
|
|
255
|
+
const windows = resume.windows;
|
|
256
|
+
// The first observation was unavailable: an exhausted window with a future reset boundary is
|
|
257
|
+
// the baseline, and recovery still needs the later transition. An already-available window
|
|
258
|
+
// cannot prove one, so keep the pending resume and poll.
|
|
259
|
+
if (windows === undefined || windows.size === 0) {
|
|
260
|
+
return establishBaseline(collection, resume, nowMs);
|
|
261
|
+
}
|
|
262
|
+
const observed = new Map(collection.windows.flatMap((window) => window.key !== undefined ? [[window.key, window]] : []));
|
|
263
|
+
let incomplete = false;
|
|
264
|
+
let unknown = false;
|
|
265
|
+
let nextCheckAtMs;
|
|
266
|
+
for (const [key, tracked] of windows) {
|
|
267
|
+
const window = observed.get(key);
|
|
268
|
+
// A row that disappears is an absent observation, not proof that the window reset.
|
|
269
|
+
if (!window) {
|
|
270
|
+
incomplete = true;
|
|
271
|
+
unknown = true;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (tracked.recovered && window.percentRemaining > 0)
|
|
275
|
+
continue;
|
|
276
|
+
if (tracked.recovered)
|
|
277
|
+
tracked.recovered = false;
|
|
278
|
+
if (window.resetMs === undefined) {
|
|
279
|
+
incomplete = true;
|
|
280
|
+
unknown = true;
|
|
281
|
+
continue;
|
|
282
|
+
}
|
|
283
|
+
const current = {
|
|
284
|
+
resetAtMs: window.resetMs,
|
|
285
|
+
percentRemaining: window.percentRemaining,
|
|
286
|
+
observedAtMs: nowMs,
|
|
287
|
+
};
|
|
288
|
+
// Keep the latest pre-reset exhaustion as the baseline: a window that recovered once and is
|
|
289
|
+
// used up again must be able to prove its next reset too. `updateObservation` keeps the
|
|
290
|
+
// earlier observation until the boundary passes.
|
|
291
|
+
if (current.percentRemaining <= 0 && current.resetAtMs > nowMs) {
|
|
292
|
+
tracked.observation = current;
|
|
293
|
+
incomplete = true;
|
|
294
|
+
nextCheckAtMs =
|
|
295
|
+
nextCheckAtMs === undefined
|
|
296
|
+
? current.resetAtMs
|
|
297
|
+
: Math.max(nextCheckAtMs, current.resetAtMs);
|
|
298
|
+
continue;
|
|
299
|
+
}
|
|
300
|
+
const update = updateObservation(tracked.observation, current, nowMs);
|
|
301
|
+
// The proof needs a baseline observed before its own reset boundary.
|
|
302
|
+
if (update.observation.observedAtMs <= update.observation.resetAtMs) {
|
|
303
|
+
tracked.observation = update.observation;
|
|
304
|
+
}
|
|
305
|
+
if (update.notify) {
|
|
306
|
+
tracked.recovered = true;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
incomplete = true;
|
|
310
|
+
if (tracked.observation.resetAtMs > nowMs) {
|
|
311
|
+
nextCheckAtMs =
|
|
312
|
+
nextCheckAtMs === undefined
|
|
313
|
+
? tracked.observation.resetAtMs
|
|
314
|
+
: Math.max(nextCheckAtMs, tracked.observation.resetAtMs);
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
317
|
+
// The boundary passed without a fresh one: poll again rather than give up.
|
|
318
|
+
unknown = true;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (!incomplete)
|
|
322
|
+
return { kind: "recovered" };
|
|
323
|
+
if (unknown)
|
|
324
|
+
return { kind: "unknown" };
|
|
325
|
+
return {
|
|
326
|
+
kind: "waiting",
|
|
327
|
+
...(nextCheckAtMs !== undefined ? { nextCheckAtMs } : {}),
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Arms the window baseline from the first usable observation. A window that is already
|
|
332
|
+
* available cannot prove a reset, so the resume stays pending with no baseline and polls.
|
|
333
|
+
*/
|
|
334
|
+
function establishBaseline(collection, resume, nowMs) {
|
|
335
|
+
const binding = collection.binding;
|
|
336
|
+
if (binding === undefined)
|
|
337
|
+
return { kind: "unknown" };
|
|
338
|
+
if (resume.subscription !== undefined) {
|
|
339
|
+
const confirmation = subscriptionConfirmation(collection, resume, nowMs);
|
|
340
|
+
if (confirmation === "contradicted")
|
|
341
|
+
return { kind: "cancel" };
|
|
342
|
+
if (confirmation === "unavailable")
|
|
343
|
+
return { kind: "unknown" };
|
|
344
|
+
}
|
|
345
|
+
const applicable = collection.windows.filter((window) => isApplicableWindow(window, resume));
|
|
346
|
+
const exhausted = applicable.filter((window) => window.percentRemaining <= 0);
|
|
347
|
+
if (exhausted.length === 0)
|
|
348
|
+
return { kind: "unknown" };
|
|
349
|
+
const windows = new Map();
|
|
350
|
+
let latestResetMs = nowMs;
|
|
351
|
+
for (const window of exhausted) {
|
|
352
|
+
if (window.key === undefined || window.resetMs === undefined || !(window.resetMs > nowMs)) {
|
|
353
|
+
// A used-up window without a usable future boundary cannot prove its reset.
|
|
354
|
+
return { kind: "unknown" };
|
|
355
|
+
}
|
|
356
|
+
windows.set(window.key, {
|
|
357
|
+
observation: {
|
|
358
|
+
resetAtMs: window.resetMs,
|
|
359
|
+
percentRemaining: window.percentRemaining,
|
|
360
|
+
observedAtMs: nowMs,
|
|
361
|
+
},
|
|
362
|
+
recovered: false,
|
|
363
|
+
});
|
|
364
|
+
latestResetMs = Math.max(latestResetMs, window.resetMs);
|
|
365
|
+
}
|
|
366
|
+
resume.windows = windows;
|
|
367
|
+
// Honor a provider cooldown from the original failure, counted from that failure.
|
|
368
|
+
return {
|
|
369
|
+
kind: "waiting",
|
|
370
|
+
nextCheckAtMs: Math.max(latestResetMs, resume.retryAfterAtMs ?? latestResetMs),
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
async function fire(sessionID, resume) {
|
|
374
|
+
// Re-check after the probe: a user prompt or a resume may have landed while it ran.
|
|
375
|
+
if ((await readLiveSession(sessionID, resume)) === undefined)
|
|
376
|
+
return;
|
|
377
|
+
// Single-flight: removing the entry before the prompt makes a second fire impossible.
|
|
378
|
+
pending.delete(sessionID);
|
|
379
|
+
// A failure from the execution this prompt starts must not schedule another continue.
|
|
380
|
+
consumed.add(sessionID);
|
|
381
|
+
try {
|
|
382
|
+
await deps.session.prompt({
|
|
383
|
+
sessionID,
|
|
384
|
+
text: QUOTA_AUTO_RESUME_PROMPT,
|
|
385
|
+
metadata: {
|
|
386
|
+
source: QUOTA_AUTO_RESUME_SOURCE,
|
|
387
|
+
failureAt: resume.failureAt,
|
|
388
|
+
},
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
catch {
|
|
392
|
+
deps.log?.("auto-resume prompt failed");
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async function evaluate(sessionID, resume) {
|
|
396
|
+
if (disposed || pending.get(sessionID) !== resume)
|
|
397
|
+
return;
|
|
398
|
+
const info = await readLiveSession(sessionID, resume);
|
|
399
|
+
if (info === undefined)
|
|
400
|
+
return;
|
|
401
|
+
const model = info.model;
|
|
402
|
+
if (model === undefined) {
|
|
403
|
+
retryLater(sessionID, resume);
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
// Capture the failed route and inactivity once, at the first successful read.
|
|
407
|
+
resume.modelID ??= model.id;
|
|
408
|
+
resume.providerID ??= model.providerID;
|
|
409
|
+
resume.idleAtArm ??= info.time?.idle;
|
|
410
|
+
// A recognized Claude subscription-limit message is only valid on the Claude Agent route.
|
|
411
|
+
if (resume.subscription !== undefined && !isClaudeAgentRoute(model.providerID)) {
|
|
412
|
+
cancel(sessionID);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
const route = { id: model.id, providerID: model.providerID };
|
|
416
|
+
const probe = await probeQuota(sessionID, resume, route);
|
|
417
|
+
if (probe.kind === "cancelled")
|
|
418
|
+
return;
|
|
419
|
+
if (probe.kind === "identity") {
|
|
420
|
+
cancel(sessionID);
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
// Turning the setting off, or a route with no auto-resume contract, cancels immediately.
|
|
424
|
+
if (probe.kind === "disabled" || probe.kind === "unsupported") {
|
|
425
|
+
cancel(sessionID);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (probe.kind === "unavailable") {
|
|
429
|
+
// Absent evidence keeps the pending resume: it may arm once a baseline is observable.
|
|
430
|
+
retryLater(sessionID, resume);
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const windows = observeWindows(probe.collection, resume, now());
|
|
434
|
+
if (windows.kind === "cancel") {
|
|
435
|
+
cancel(sessionID);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (windows.kind !== "recovered") {
|
|
439
|
+
if (windows.kind === "waiting") {
|
|
440
|
+
// The window is confirmed used up again; the next reset starts a new grace period.
|
|
441
|
+
resume.recoveredAtMs = undefined;
|
|
442
|
+
schedule(sessionID, resume, windows.nextCheckAtMs ?? now() + QUOTA_AUTO_RESUME_RETRY_MS);
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
// An incomplete observation keeps an already-confirmed recovery; it just cannot fire.
|
|
446
|
+
retryLater(sessionID, resume);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
resume.recoveredAtMs ??= now();
|
|
450
|
+
const fireAtMs = resume.recoveredAtMs + QUOTA_AUTO_RESUME_GRACE_MS;
|
|
451
|
+
if (now() < fireAtMs) {
|
|
452
|
+
schedule(sessionID, resume, fireAtMs);
|
|
453
|
+
return;
|
|
454
|
+
}
|
|
455
|
+
await fire(sessionID, resume);
|
|
456
|
+
}
|
|
457
|
+
async function arm(sessionID, error, subscription) {
|
|
458
|
+
if (disposed || pending.has(sessionID))
|
|
459
|
+
return;
|
|
460
|
+
const failureAt = now();
|
|
461
|
+
const resume = {
|
|
462
|
+
failureAt,
|
|
463
|
+
...(subscription !== undefined ? { subscription } : {}),
|
|
464
|
+
...(error.retryAfterMs !== undefined
|
|
465
|
+
? { retryAfterAtMs: failureAt + error.retryAfterMs }
|
|
466
|
+
: {}),
|
|
467
|
+
};
|
|
468
|
+
// Registered before the awaits so a cancellation event during the probe removes it.
|
|
469
|
+
pending.set(sessionID, resume);
|
|
470
|
+
await evaluate(sessionID, resume);
|
|
471
|
+
}
|
|
472
|
+
return {
|
|
473
|
+
handleEvent(event) {
|
|
474
|
+
if (disposed)
|
|
475
|
+
return;
|
|
476
|
+
const sessionID = sessionIDOf(event.data);
|
|
477
|
+
switch (event.type) {
|
|
478
|
+
case "session.execution.failed": {
|
|
479
|
+
if (sessionID === undefined)
|
|
480
|
+
return;
|
|
481
|
+
// A newer terminal failure supersedes a pending resume for this session.
|
|
482
|
+
cancel(sessionID);
|
|
483
|
+
// The current execution was already resumed once; wait for a fresh user prompt.
|
|
484
|
+
if (consumed.has(sessionID))
|
|
485
|
+
return;
|
|
486
|
+
const data = event.data;
|
|
487
|
+
const error = failureError(data.error);
|
|
488
|
+
if (!error)
|
|
489
|
+
return;
|
|
490
|
+
const typed = isQuotaLimitError(error);
|
|
491
|
+
// OpenCode 2.4.1 exposes some Claude subscription limits as provider.unknown with only
|
|
492
|
+
// a message. Recognize only the known shapes; a message alone still cannot arm without
|
|
493
|
+
// the independent exhausted-window confirmation in `establishBaseline`.
|
|
494
|
+
const subscription = !typed && error.type === "provider.unknown"
|
|
495
|
+
? claudeSubscriptionWindow(error.message)
|
|
496
|
+
: undefined;
|
|
497
|
+
if (!typed && subscription === undefined)
|
|
498
|
+
return;
|
|
499
|
+
void arm(sessionID, error, subscription);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
case "session.execution.interrupted":
|
|
503
|
+
case "session.execution.started":
|
|
504
|
+
case "session.execution.succeeded":
|
|
505
|
+
case "session.model.selected":
|
|
506
|
+
if (sessionID !== undefined)
|
|
507
|
+
cancel(sessionID);
|
|
508
|
+
return;
|
|
509
|
+
case "session.deleted":
|
|
510
|
+
if (sessionID !== undefined) {
|
|
511
|
+
consumed.delete(sessionID);
|
|
512
|
+
cancel(sessionID);
|
|
513
|
+
}
|
|
514
|
+
return;
|
|
515
|
+
case "credential.updated":
|
|
516
|
+
case "credential.switched":
|
|
517
|
+
cancelAll();
|
|
518
|
+
return;
|
|
519
|
+
default:
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
},
|
|
523
|
+
onPrompt(event) {
|
|
524
|
+
if (disposed || event.sessionID === undefined)
|
|
525
|
+
return;
|
|
526
|
+
if (event.metadata?.source === QUOTA_AUTO_RESUME_SOURCE)
|
|
527
|
+
return;
|
|
528
|
+
// A user prompt starts a new execution; it may earn one fresh auto-resume later.
|
|
529
|
+
consumed.delete(event.sessionID);
|
|
530
|
+
cancel(event.sessionID);
|
|
531
|
+
},
|
|
532
|
+
dispose() {
|
|
533
|
+
disposed = true;
|
|
534
|
+
consumed.clear();
|
|
535
|
+
cancelAll();
|
|
536
|
+
},
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
//# sourceMappingURL=quota-auto-resume.js.map
|
|
@@ -56,7 +56,7 @@ const DIALOG_TIME_WINDOWS = new Set([
|
|
|
56
56
|
/**
|
|
57
57
|
* The TUI dialog's label for a row: a time window's quota drops "quota" ("5h quota" is
|
|
58
58
|
* "5h", "Weekly quota" is "Weekly"), since the dialog's table heads that column "window".
|
|
59
|
-
* Other labels, such as "Month spend" or "Fable
|
|
59
|
+
* Other labels, such as "Month spend" or "Fable", stay whole.
|
|
60
60
|
*/
|
|
61
61
|
function getDialogMetricLabel(entry, metricLabel) {
|
|
62
62
|
const metric = entry.semantic?.metric;
|
|
@@ -68,16 +68,28 @@ function getDialogMetricLabel(entry, metricLabel) {
|
|
|
68
68
|
return window && metricLabel === `${window} quota` ? window : metricLabel;
|
|
69
69
|
}
|
|
70
70
|
function getCommandMetricLabel(entry, semanticLabel) {
|
|
71
|
-
if (entry.semantic)
|
|
72
|
-
return semanticLabel;
|
|
73
|
-
const window = getCommandWindowLabel(entry);
|
|
74
71
|
const resultType = entry.accounting?.resultType;
|
|
72
|
+
const explicit = normalizeMetricText(entry.label);
|
|
73
|
+
const metricLabel = normalizeMetricText(entry.metricLabel);
|
|
74
|
+
if (entry.semantic) {
|
|
75
|
+
const window = explicit && !metricLabel ? getCommandWindowLabel(entry) : null;
|
|
76
|
+
if (window) {
|
|
77
|
+
const noun = resultType === "budget"
|
|
78
|
+
? "budget"
|
|
79
|
+
: resultType === "usage"
|
|
80
|
+
? "usage"
|
|
81
|
+
: resultType === "spend"
|
|
82
|
+
? "spend"
|
|
83
|
+
: "quota";
|
|
84
|
+
return `${window} ${noun}`;
|
|
85
|
+
}
|
|
86
|
+
return normalizeMetricText(semanticLabel) || entry.name;
|
|
87
|
+
}
|
|
75
88
|
if (resultType === "balance")
|
|
76
89
|
return "Balance";
|
|
77
90
|
if (resultType === "status")
|
|
78
91
|
return "Status";
|
|
79
|
-
const
|
|
80
|
-
const metricLabel = normalizeMetricText(entry.metricLabel);
|
|
92
|
+
const window = getCommandWindowLabel(entry);
|
|
81
93
|
const noun = resultType === "budget"
|
|
82
94
|
? "budget"
|
|
83
95
|
: resultType === "usage"
|
|
@@ -87,11 +99,12 @@ function getCommandMetricLabel(entry, semanticLabel) {
|
|
|
87
99
|
: resultType === "quota" || resultType === "rate_limit"
|
|
88
100
|
? "quota"
|
|
89
101
|
: "";
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
102
|
+
// A time window keeps the pre-semantic window label: the dialog strips
|
|
103
|
+
// "quota" from its own column, and an explicit label never wins there.
|
|
93
104
|
if (window)
|
|
94
|
-
return `${window} quota`;
|
|
105
|
+
return noun ? `${window} ${noun}` : `${window} quota`;
|
|
106
|
+
if (noun)
|
|
107
|
+
return metricLabel || noun[0].toUpperCase() + noun.slice(1);
|
|
95
108
|
return explicit || (isValueEntry(entry) ? "Value" : "Quota");
|
|
96
109
|
}
|
|
97
110
|
function formatCommandDetails(entry, rightWidth, resetTimeSpaced) {
|
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
import type { FixedWindowProjectionEvidence, QuotaRunwayProjection } from "./entries.js";
|
|
2
2
|
export declare function isFixedWindowProjectionEvidence(value: unknown, resetTimeIso?: string): value is FixedWindowProjectionEvidence;
|
|
3
|
+
/**
|
|
4
|
+
* Builds fixed-window evidence for a provider window whose length is known and whose reset is
|
|
5
|
+
* observed. The start is inferred as `reset - duration`, which is only valid for windows that
|
|
6
|
+
* reset on a fixed cycle; callers must pass a duration that matches that window kind. Returns
|
|
7
|
+
* nothing unless the observation falls strictly inside the inferred window.
|
|
8
|
+
*/
|
|
9
|
+
export declare function buildFixedWindowEvidence(params: {
|
|
10
|
+
durationMs: number;
|
|
11
|
+
resetTimeIso?: string;
|
|
12
|
+
observedAtMs: number;
|
|
13
|
+
}): FixedWindowProjectionEvidence | undefined;
|
|
3
14
|
export declare function projectQuotaRunway(params: {
|
|
4
15
|
percentRemaining: number;
|
|
5
16
|
fixedWindow?: FixedWindowProjectionEvidence;
|
|
@@ -27,6 +27,31 @@ export function isFixedWindowProjectionEvidence(value, resetTimeIso) {
|
|
|
27
27
|
return false;
|
|
28
28
|
return true;
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Builds fixed-window evidence for a provider window whose length is known and whose reset is
|
|
32
|
+
* observed. The start is inferred as `reset - duration`, which is only valid for windows that
|
|
33
|
+
* reset on a fixed cycle; callers must pass a duration that matches that window kind. Returns
|
|
34
|
+
* nothing unless the observation falls strictly inside the inferred window.
|
|
35
|
+
*/
|
|
36
|
+
export function buildFixedWindowEvidence(params) {
|
|
37
|
+
if (!(Number.isFinite(params.durationMs) && params.durationMs > 0))
|
|
38
|
+
return undefined;
|
|
39
|
+
if (!Number.isFinite(params.observedAtMs))
|
|
40
|
+
return undefined;
|
|
41
|
+
const endsAt = parseIsoTimestamp(params.resetTimeIso);
|
|
42
|
+
if (endsAt === null)
|
|
43
|
+
return undefined;
|
|
44
|
+
const startedAt = endsAt - params.durationMs;
|
|
45
|
+
if (!(startedAt < params.observedAtMs && params.observedAtMs < endsAt))
|
|
46
|
+
return undefined;
|
|
47
|
+
return {
|
|
48
|
+
kind: "fixed_window",
|
|
49
|
+
startedAtIso: new Date(startedAt).toISOString(),
|
|
50
|
+
observedAtIso: new Date(params.observedAtMs).toISOString(),
|
|
51
|
+
endsAtIso: new Date(endsAt).toISOString(),
|
|
52
|
+
fullReset: true,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
30
55
|
export function projectQuotaRunway(params) {
|
|
31
56
|
const nowMs = params.nowMs ?? Date.now();
|
|
32
57
|
if (!Number.isFinite(params.percentRemaining) || !Number.isFinite(nowMs))
|