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
|
@@ -41,6 +41,8 @@
|
|
|
41
41
|
* No TTL, sequence, watermark, or budget machinery exists (R9).
|
|
42
42
|
*/
|
|
43
43
|
import { encodeAnalyticsGroupQueryKey } from "../contracts/analytics-entity.js";
|
|
44
|
+
import { isRecord } from "../contracts/dto-guards.js";
|
|
45
|
+
import { isTransientProviderFailureCategory } from "../contracts/provider-failure.js";
|
|
44
46
|
import { createGuiLogger } from "./log.js";
|
|
45
47
|
/** Default quota/provider poll interval (spec §4.2; existing config overrides). */
|
|
46
48
|
export const DEFAULT_QUOTA_INTERVAL_MS = 60_000;
|
|
@@ -102,11 +104,15 @@ const DERIVED_NOISE_KEYS = new Set([
|
|
|
102
104
|
"untilMs",
|
|
103
105
|
"startMs",
|
|
104
106
|
"endMs",
|
|
107
|
+
// A provisional first paint is replaced by the same numbers: the marker is
|
|
108
|
+
// presentation state, and the unconditional replacement event below clears it.
|
|
109
|
+
"provisional",
|
|
110
|
+
"exactError",
|
|
105
111
|
]);
|
|
106
112
|
function stripDerivedNoise(value) {
|
|
107
113
|
if (Array.isArray(value))
|
|
108
114
|
return value.map(stripDerivedNoise);
|
|
109
|
-
if (
|
|
115
|
+
if (isRecord(value)) {
|
|
110
116
|
const out = {};
|
|
111
117
|
for (const [key, child] of Object.entries(value)) {
|
|
112
118
|
if (DERIVED_NOISE_KEYS.has(key))
|
|
@@ -123,6 +129,18 @@ function substantiveOf(snapshot) {
|
|
|
123
129
|
function staleFlag(value) {
|
|
124
130
|
return typeof value === "object" && value !== null && "stale" in value && value.stale === true;
|
|
125
131
|
}
|
|
132
|
+
/** A provisional first paint whose exact replacement is still computing. */
|
|
133
|
+
function isProvisional(value) {
|
|
134
|
+
return isRecord(value) && value.provisional === true;
|
|
135
|
+
}
|
|
136
|
+
/** Terminal exact-read failure marker on a provisional payload. */
|
|
137
|
+
function hasExactError(cause) {
|
|
138
|
+
return isRecord(cause) && "exactError" in cause && cause.exactError !== undefined;
|
|
139
|
+
}
|
|
140
|
+
/** Terminal exact-read failure carrying the still-useful provisional payload. */
|
|
141
|
+
function withExactFailure(snapshot, message) {
|
|
142
|
+
return { ...snapshot, exactError: message };
|
|
143
|
+
}
|
|
126
144
|
/**
|
|
127
145
|
* True when the source payload itself reports stale data. Such a payload must
|
|
128
146
|
* not enter the cache as fresh: the next read revalidates instead of serving it
|
|
@@ -170,6 +188,17 @@ export function createSnapshotService(options) {
|
|
|
170
188
|
const logger = options.logger ?? createGuiLogger();
|
|
171
189
|
const now = options.now ?? Date.now;
|
|
172
190
|
const cache = new Map();
|
|
191
|
+
/**
|
|
192
|
+
* A just-settled rejected compute. The host's final probe can arrive right
|
|
193
|
+
* after the worker died; joining this outcome briefly avoids starting a
|
|
194
|
+
* second full scan for the same logical read.
|
|
195
|
+
*/
|
|
196
|
+
const settledFailures = new Map();
|
|
197
|
+
/** One announcement per exact payload object; joined exact reads share it. */
|
|
198
|
+
const announcedExact = new WeakSet();
|
|
199
|
+
const SETTLED_FAILURE_GRACE_MS = 5_000;
|
|
200
|
+
let quotaRefresh;
|
|
201
|
+
let quotaPoll;
|
|
173
202
|
const computes = new Map();
|
|
174
203
|
const inflight = new Map();
|
|
175
204
|
const listeners = new Set();
|
|
@@ -199,6 +228,9 @@ export function createSnapshotService(options) {
|
|
|
199
228
|
/** Explicit UI signal outranks read-request inference for this viewer session. */
|
|
200
229
|
let viewScopeExplicit = false;
|
|
201
230
|
let quotaTimer;
|
|
231
|
+
let quotaRetryTimer;
|
|
232
|
+
let quotaRetried = false;
|
|
233
|
+
let pollerEpoch = 0;
|
|
202
234
|
let analyticsTimer;
|
|
203
235
|
let quotaTicking = false;
|
|
204
236
|
let analyticsTicking = false;
|
|
@@ -346,7 +378,45 @@ export function createSnapshotService(options) {
|
|
|
346
378
|
}
|
|
347
379
|
return version;
|
|
348
380
|
}
|
|
349
|
-
|
|
381
|
+
const notifiedLoads = new WeakSet();
|
|
382
|
+
/**
|
|
383
|
+
* Foreground analytics read, stale-while-revalidate. The single analytics
|
|
384
|
+
* worker cannot answer while a synchronous scan runs, so a stale entry returns
|
|
385
|
+
* the last-good DTO immediately flagged stale. A key with no DTO awaits its compute. The compute stays
|
|
386
|
+
* single-flight and commits when it lands; `sessions.changed` is emitted only
|
|
387
|
+
* when a changed result was actually stored, so clients refetch exactly once.
|
|
388
|
+
*/
|
|
389
|
+
function load(key, compute, force = false, promote, foreground = false) {
|
|
390
|
+
const prior = cache.get(key);
|
|
391
|
+
const fresh = loadSingleFlight(key, compute, force, promote, foreground);
|
|
392
|
+
if (force || closed || isQuotaKey(key) || !inflight.has(key))
|
|
393
|
+
return fresh;
|
|
394
|
+
const lastGood = prior && prior.sourceGeneration === sourceGeneration && hasStaleMarker(prior.snapshot)
|
|
395
|
+
? markStale(prior.snapshot)
|
|
396
|
+
: undefined;
|
|
397
|
+
if (lastGood === undefined)
|
|
398
|
+
return fresh;
|
|
399
|
+
const epoch = viewerEpoch;
|
|
400
|
+
void fresh.then((value) => {
|
|
401
|
+
if (closed ||
|
|
402
|
+
epoch !== viewerEpoch ||
|
|
403
|
+
cache.get(key)?.snapshot !== value ||
|
|
404
|
+
notifiedLoads.has(fresh) ||
|
|
405
|
+
// The exact-replacement path owns this announcement when the prior
|
|
406
|
+
// paint was provisional; the scheduler emits once for the payload.
|
|
407
|
+
(prior !== undefined && isProvisional(prior.snapshot)) ||
|
|
408
|
+
// A provisional entry must always announce its exact replacement,
|
|
409
|
+
// even when the substantive payload is unchanged: that event is the
|
|
410
|
+
// only way the UI learns the exact result has landed.
|
|
411
|
+
(prior && !isProvisional(prior.snapshot) && substantiveOf(value) === prior.substantive)) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
notifiedLoads.add(fresh);
|
|
415
|
+
emit({ topic: topicFor(key), at: now() });
|
|
416
|
+
}, () => { });
|
|
417
|
+
return Promise.resolve(lastGood);
|
|
418
|
+
}
|
|
419
|
+
function loadSingleFlight(key, compute, force = false, promote, foreground = false) {
|
|
350
420
|
const epoch = viewerEpoch;
|
|
351
421
|
const generation = sourceGeneration;
|
|
352
422
|
// A closed service does not track, register, or cache: the caller still
|
|
@@ -356,13 +426,21 @@ export function createSnapshotService(options) {
|
|
|
356
426
|
if (!closed)
|
|
357
427
|
computes.set(key, compute);
|
|
358
428
|
const existing = cache.get(key);
|
|
359
|
-
if (
|
|
429
|
+
if (!force &&
|
|
430
|
+
existing &&
|
|
431
|
+
existing.sourceGeneration === sourceGeneration &&
|
|
432
|
+
(!existing.stale ||
|
|
433
|
+
(isQuotaKey(key) && !existing.invalidated && payloadIsStale(existing.snapshot)))) {
|
|
360
434
|
// SAFETY: `load<T>` is only called with a key built from `(kind, resourceId,
|
|
361
435
|
// range)`, and the entry under this key was stored by a compute of that key.
|
|
362
436
|
return Promise.resolve(existing.snapshot);
|
|
363
437
|
}
|
|
364
438
|
const pending = inflight.get(key);
|
|
365
439
|
if (pending && pending.epoch === epoch && pending.sourceGeneration === generation) {
|
|
440
|
+
if (foreground && pending.promote) {
|
|
441
|
+
pending.promote();
|
|
442
|
+
pending.promote = undefined;
|
|
443
|
+
}
|
|
366
444
|
// The key was requested again in the same viewer session and source
|
|
367
445
|
// generation while its compute was still pending: preserve single-flight
|
|
368
446
|
// and let it cache again.
|
|
@@ -374,6 +452,16 @@ export function createSnapshotService(options) {
|
|
|
374
452
|
}
|
|
375
453
|
// A pending task from an ended viewer session or a previous source identity
|
|
376
454
|
// is disowned: this caller runs its own compute instead.
|
|
455
|
+
const settled = settledFailures.get(key);
|
|
456
|
+
if (settled !== undefined) {
|
|
457
|
+
if (!force &&
|
|
458
|
+
settled.epoch === epoch &&
|
|
459
|
+
settled.sourceGeneration === generation &&
|
|
460
|
+
settled.untilMs > now()) {
|
|
461
|
+
return Promise.reject(settled.error);
|
|
462
|
+
}
|
|
463
|
+
settledFailures.delete(key);
|
|
464
|
+
}
|
|
377
465
|
let computation;
|
|
378
466
|
try {
|
|
379
467
|
computation = compute();
|
|
@@ -382,6 +470,7 @@ export function createSnapshotService(options) {
|
|
|
382
470
|
computation = Promise.reject(error);
|
|
383
471
|
}
|
|
384
472
|
const task = {
|
|
473
|
+
promote,
|
|
385
474
|
epoch,
|
|
386
475
|
sourceGeneration: generation,
|
|
387
476
|
promise: Promise.resolve(),
|
|
@@ -394,12 +483,19 @@ export function createSnapshotService(options) {
|
|
|
394
483
|
}
|
|
395
484
|
catch (error) {
|
|
396
485
|
const fallback = tracked.get(key) === task.version ? cache.get(key) : undefined;
|
|
397
|
-
if (!fallback)
|
|
398
|
-
throw error;
|
|
399
486
|
// A previous owner's last-good entry may be served only when the DTO
|
|
400
487
|
// can carry the stale marker. Session snapshots cannot, so their
|
|
401
488
|
// failure is surfaced rather than presented as fresh old-owner data.
|
|
402
|
-
if (
|
|
489
|
+
if (!fallback ||
|
|
490
|
+
(fallback.sourceGeneration !== sourceGeneration && !hasStaleMarker(fallback.snapshot))) {
|
|
491
|
+
if (!closed) {
|
|
492
|
+
settledFailures.set(key, {
|
|
493
|
+
error,
|
|
494
|
+
epoch: task.epoch,
|
|
495
|
+
sourceGeneration: task.sourceGeneration,
|
|
496
|
+
untilMs: now() + SETTLED_FAILURE_GRACE_MS,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
403
499
|
throw error;
|
|
404
500
|
}
|
|
405
501
|
if (!closed && task.epoch === viewerEpoch && task.sourceGeneration === sourceGeneration) {
|
|
@@ -410,6 +506,7 @@ export function createSnapshotService(options) {
|
|
|
410
506
|
// of the same `(kind, resourceId, range)` key, so it is a T snapshot.
|
|
411
507
|
return markStale(fallback.snapshot);
|
|
412
508
|
}
|
|
509
|
+
settledFailures.delete(key);
|
|
413
510
|
// A commit belongs to the viewer session and source identity that started
|
|
414
511
|
// the load: a disconnect or owner handoff already dirtied the entries, so
|
|
415
512
|
// an old result never re-freshens them.
|
|
@@ -450,7 +547,7 @@ export function createSnapshotService(options) {
|
|
|
450
547
|
* Polls one registered key. Returns false when the viewer session ended or
|
|
451
548
|
* the service closed and the tick must stop.
|
|
452
549
|
*/
|
|
453
|
-
async function pollOne(key, compute, epoch, changed) {
|
|
550
|
+
async function pollOne(key, compute, epoch, changed, onSnapshot) {
|
|
454
551
|
const generation = sourceGeneration;
|
|
455
552
|
if (closed || viewerEpoch !== epoch || viewers === 0)
|
|
456
553
|
return false;
|
|
@@ -492,33 +589,85 @@ export function createSnapshotService(options) {
|
|
|
492
589
|
return true;
|
|
493
590
|
const substantive = substantiveOf(snapshot);
|
|
494
591
|
const entry = cache.get(key);
|
|
495
|
-
const
|
|
496
|
-
|
|
592
|
+
const stale = payloadIsStale(snapshot);
|
|
593
|
+
const changedNow = entry === undefined ||
|
|
594
|
+
entry.stale !== stale ||
|
|
595
|
+
entry.substantive !== substantive ||
|
|
596
|
+
(isQuotaKey(key) &&
|
|
597
|
+
entry.snapshot.providers.polledAtMs !==
|
|
598
|
+
snapshot.providers.polledAtMs);
|
|
599
|
+
cache.set(key, {
|
|
600
|
+
snapshot,
|
|
601
|
+
substantive,
|
|
602
|
+
stale,
|
|
603
|
+
sourceGeneration: generation,
|
|
604
|
+
});
|
|
605
|
+
onSnapshot?.(snapshot);
|
|
497
606
|
if (changedNow)
|
|
498
607
|
changed.add(topicFor(key));
|
|
499
608
|
return true;
|
|
500
609
|
}
|
|
501
|
-
async function tickQuota() {
|
|
502
|
-
if (quotaTicking || closed || viewers === 0)
|
|
610
|
+
async function tickQuota(earlyRetry = false) {
|
|
611
|
+
if (quotaTicking || quotaRefresh || closed || viewers === 0)
|
|
503
612
|
return;
|
|
613
|
+
if (!earlyRetry && quotaRetryTimer) {
|
|
614
|
+
clearTimeout(quotaRetryTimer);
|
|
615
|
+
quotaRetryTimer = undefined;
|
|
616
|
+
quotaRetried = true;
|
|
617
|
+
}
|
|
504
618
|
quotaTicking = true;
|
|
505
619
|
const epoch = viewerEpoch;
|
|
620
|
+
const tickPollerEpoch = pollerEpoch;
|
|
621
|
+
const generation = sourceGeneration;
|
|
506
622
|
const changed = new Set();
|
|
623
|
+
let polledSnapshot;
|
|
507
624
|
try {
|
|
508
625
|
// Copy before the async body: `load` can register new computes while a
|
|
509
626
|
// tick awaits, and one tick must not grow while it runs.
|
|
510
627
|
for (const [key, compute] of Array.from(computes)) {
|
|
511
628
|
if (kindOf(key) !== "quota")
|
|
512
629
|
continue;
|
|
513
|
-
|
|
514
|
-
|
|
630
|
+
const poll = pollOne(key, compute, epoch, changed, (snapshot) => {
|
|
631
|
+
polledSnapshot = snapshot;
|
|
632
|
+
});
|
|
633
|
+
quotaPoll = poll;
|
|
634
|
+
try {
|
|
635
|
+
if (!(await poll))
|
|
636
|
+
break;
|
|
637
|
+
}
|
|
638
|
+
finally {
|
|
639
|
+
if (quotaPoll === poll)
|
|
640
|
+
quotaPoll = undefined;
|
|
641
|
+
}
|
|
515
642
|
}
|
|
516
643
|
}
|
|
517
644
|
finally {
|
|
518
645
|
quotaTicking = false;
|
|
519
646
|
}
|
|
520
|
-
if (closed || viewerEpoch !== epoch)
|
|
647
|
+
if (closed || viewerEpoch !== epoch || pollerEpoch !== tickPollerEpoch)
|
|
648
|
+
return;
|
|
649
|
+
if (sourceGeneration !== generation)
|
|
521
650
|
return;
|
|
651
|
+
if (polledSnapshot) {
|
|
652
|
+
const eligible = polledSnapshot.providers.cards.some((card) => card.errors.some((error) => error.kind !== "intentional-filter" &&
|
|
653
|
+
isTransientProviderFailureCategory(error.failureCategory) &&
|
|
654
|
+
!(error.retryAtMs !== undefined && error.retryAtMs > now())));
|
|
655
|
+
if (!eligible) {
|
|
656
|
+
quotaRetried = false;
|
|
657
|
+
if (quotaRetryTimer)
|
|
658
|
+
clearTimeout(quotaRetryTimer);
|
|
659
|
+
quotaRetryTimer = undefined;
|
|
660
|
+
}
|
|
661
|
+
else if (!earlyRetry && !quotaRetried && !quotaRetryTimer && !quotaRefresh) {
|
|
662
|
+
// quota-state caches per provider, so healthy accounts within the failed provider may also be re-requested once per episode.
|
|
663
|
+
quotaRetryTimer = setTimeout(() => {
|
|
664
|
+
quotaRetryTimer = undefined;
|
|
665
|
+
quotaRetried = true;
|
|
666
|
+
void tickQuota(true);
|
|
667
|
+
}, Math.min(10_000, quotaIntervalMs()));
|
|
668
|
+
quotaRetryTimer.unref();
|
|
669
|
+
}
|
|
670
|
+
}
|
|
522
671
|
if (changed.has("quota.changed"))
|
|
523
672
|
emit({ topic: "quota.changed", at: now() });
|
|
524
673
|
}
|
|
@@ -595,6 +744,11 @@ export function createSnapshotService(options) {
|
|
|
595
744
|
analyticsTimer.unref();
|
|
596
745
|
}
|
|
597
746
|
function stopPollers() {
|
|
747
|
+
pollerEpoch += 1;
|
|
748
|
+
if (quotaRetryTimer)
|
|
749
|
+
clearTimeout(quotaRetryTimer);
|
|
750
|
+
quotaRetryTimer = undefined;
|
|
751
|
+
quotaRetried = false;
|
|
598
752
|
if (quotaTimer) {
|
|
599
753
|
clearInterval(quotaTimer);
|
|
600
754
|
quotaTimer = undefined;
|
|
@@ -612,17 +766,114 @@ export function createSnapshotService(options) {
|
|
|
612
766
|
startPollers();
|
|
613
767
|
}
|
|
614
768
|
function dirty() {
|
|
615
|
-
|
|
769
|
+
settledFailures.clear();
|
|
770
|
+
for (const entry of cache.values()) {
|
|
616
771
|
entry.stale = true;
|
|
772
|
+
entry.invalidated = true;
|
|
773
|
+
}
|
|
617
774
|
}
|
|
618
775
|
return {
|
|
619
|
-
home: (input) =>
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
776
|
+
home: (input) => {
|
|
777
|
+
const key = homeKey(input.range);
|
|
778
|
+
const request = load(key, () => {
|
|
779
|
+
const homeRequest = input.background && input.exact === true
|
|
780
|
+
? { range: input.range, background: true, exact: true }
|
|
781
|
+
: input.background
|
|
782
|
+
? { range: input.range, background: true }
|
|
783
|
+
: input.exact === true
|
|
784
|
+
? { range: input.range, exact: true }
|
|
785
|
+
: { range: input.range };
|
|
786
|
+
return options.source.home(homeRequest);
|
|
787
|
+
},
|
|
788
|
+
// An exact read is an explicit revalidation request and must not be
|
|
789
|
+
// satisfied by a cached provisional/marker entry.
|
|
790
|
+
input.exact === true, input.background
|
|
791
|
+
? () => {
|
|
792
|
+
void options.source.home({ range: input.range }).catch(() => { });
|
|
793
|
+
}
|
|
794
|
+
: undefined, !input.background);
|
|
795
|
+
void request.then((snapshot) => {
|
|
796
|
+
if (!isProvisional(snapshot) ||
|
|
797
|
+
hasExactError(snapshot) ||
|
|
798
|
+
cache.get(key)?.snapshot !== snapshot ||
|
|
799
|
+
closed)
|
|
800
|
+
return;
|
|
801
|
+
const exact = load(key, () => options.source.home({ range: input.range, exact: true, background: true }),
|
|
802
|
+
// An exact replacement is explicitly requested; a still-cached
|
|
803
|
+
// provisional paint must never satisfy it.
|
|
804
|
+
true, () => {
|
|
805
|
+
void options.source.home({ range: input.range, exact: true }).catch(() => { });
|
|
806
|
+
});
|
|
807
|
+
const settle = () => {
|
|
808
|
+
const current = cache.get(key);
|
|
809
|
+
if (closed || !current || !isProvisional(current.snapshot))
|
|
810
|
+
return;
|
|
811
|
+
if (hasExactError(current.snapshot))
|
|
812
|
+
return;
|
|
813
|
+
const failed = withExactFailure(current.snapshot, "The exact analytics read failed. Retry to finish this range.");
|
|
814
|
+
cache.set(key, {
|
|
815
|
+
...current,
|
|
816
|
+
snapshot: failed,
|
|
817
|
+
substantive: substantiveOf(failed),
|
|
818
|
+
// Keep the provisional data as a cache hit, but advertise the
|
|
819
|
+
// terminal status. A manual exact retry forces a fresh compute.
|
|
820
|
+
stale: false,
|
|
821
|
+
});
|
|
822
|
+
emit({ topic: topicFor(key), at: now() });
|
|
823
|
+
};
|
|
824
|
+
void exact.then((value) => {
|
|
825
|
+
if (isProvisional(value)) {
|
|
826
|
+
settle();
|
|
827
|
+
return;
|
|
828
|
+
}
|
|
829
|
+
if (isRecord(value)) {
|
|
830
|
+
if (announcedExact.has(value))
|
|
831
|
+
return;
|
|
832
|
+
announcedExact.add(value);
|
|
833
|
+
}
|
|
834
|
+
emit({ topic: topicFor(key), at: now() });
|
|
835
|
+
}, () => settle());
|
|
836
|
+
}, () => { });
|
|
837
|
+
return request;
|
|
838
|
+
},
|
|
839
|
+
quota: (input) => {
|
|
840
|
+
const compute = () => {
|
|
841
|
+
if (!options.source.quota) {
|
|
842
|
+
return Promise.reject(new Error("Quota source unavailable"));
|
|
843
|
+
}
|
|
844
|
+
return options.source.quota();
|
|
845
|
+
};
|
|
846
|
+
if (!input?.refresh)
|
|
847
|
+
return load(quotaKey(), compute);
|
|
848
|
+
if (quotaRefresh)
|
|
849
|
+
return quotaRefresh;
|
|
850
|
+
const refresh = (async () => {
|
|
851
|
+
await quotaPoll;
|
|
852
|
+
await inflight.get(quotaKey())?.promise.catch(() => undefined);
|
|
853
|
+
const cached = cache.get(quotaKey());
|
|
854
|
+
if (cached)
|
|
855
|
+
cached.stale = true;
|
|
856
|
+
const forcedCompute = () => options.source.quota({ refresh: true });
|
|
857
|
+
try {
|
|
858
|
+
const result = await load(quotaKey(), forcedCompute, true);
|
|
859
|
+
if (!closed)
|
|
860
|
+
emit({ topic: "quota.changed", at: now() });
|
|
861
|
+
return result;
|
|
862
|
+
}
|
|
863
|
+
finally {
|
|
864
|
+
if (!closed && computes.get(quotaKey()) === forcedCompute)
|
|
865
|
+
computes.set(quotaKey(), compute);
|
|
866
|
+
}
|
|
867
|
+
})();
|
|
868
|
+
quotaRefresh = refresh;
|
|
869
|
+
void refresh
|
|
870
|
+
.finally(() => {
|
|
871
|
+
if (quotaRefresh === refresh)
|
|
872
|
+
quotaRefresh = undefined;
|
|
873
|
+
})
|
|
874
|
+
.catch(() => undefined);
|
|
875
|
+
return refresh;
|
|
876
|
+
},
|
|
626
877
|
project: (input) => load(projectKey(input.projectId, input.range), () => options.source.project({ projectId: input.projectId, range: input.range })),
|
|
627
878
|
session: (input) => load(sessionKey(input.sessionId), () => options.source.session({ sessionId: input.sessionId })),
|
|
628
879
|
groups: (query) => load(groupsKey(query), () => {
|
|
@@ -671,8 +922,10 @@ export function createSnapshotService(options) {
|
|
|
671
922
|
if (settingsOptions?.scope !== "visibility") {
|
|
672
923
|
for (const [key, entry] of cache) {
|
|
673
924
|
const kind = kindOf(key);
|
|
674
|
-
if (kind === "quota" || kind === "home" || kind === "project")
|
|
925
|
+
if (kind === "quota" || kind === "home" || kind === "project") {
|
|
675
926
|
entry.stale = true;
|
|
927
|
+
entry.invalidated = true;
|
|
928
|
+
}
|
|
676
929
|
}
|
|
677
930
|
// Re-read both interval getters from live settings (R16).
|
|
678
931
|
restartPollers();
|
|
@@ -695,6 +948,10 @@ export function createSnapshotService(options) {
|
|
|
695
948
|
sourceChanged() {
|
|
696
949
|
if (closed)
|
|
697
950
|
return;
|
|
951
|
+
if (quotaRetryTimer)
|
|
952
|
+
clearTimeout(quotaRetryTimer);
|
|
953
|
+
quotaRetryTimer = undefined;
|
|
954
|
+
quotaRetried = false;
|
|
698
955
|
// The cached payloads remain useful as last-good fallback, but they must
|
|
699
956
|
// never be served as fresh data from the previous owner.
|
|
700
957
|
sourceGeneration += 1;
|
|
@@ -716,6 +973,7 @@ export function createSnapshotService(options) {
|
|
|
716
973
|
computes.clear();
|
|
717
974
|
cache.clear();
|
|
718
975
|
inflight.clear();
|
|
976
|
+
settledFailures.clear();
|
|
719
977
|
tracked.clear();
|
|
720
978
|
scopedViewers.clear();
|
|
721
979
|
},
|