sideby 0.35.9 → 0.36.0
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/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +12 -0
- package/dist/src/cli/commands.js +480 -6
- package/dist/src/cli/format.d.ts +12 -1
- package/dist/src/cli/format.js +23 -3
- package/dist/src/cli/json-schemas.js +166 -1
- package/dist/src/cli/main.js +18 -4
- package/dist/src/core/advisor.d.ts +176 -0
- package/dist/src/core/advisor.js +472 -0
- package/dist/src/core/browser-profiles.d.ts +16 -0
- package/dist/src/core/browser-profiles.js +45 -0
- package/dist/src/core/cli-commands.js +25 -1
- package/dist/src/core/config.d.ts +29 -0
- package/dist/src/core/config.js +25 -0
- package/dist/src/core/handoff-select.d.ts +13 -2
- package/dist/src/core/handoff-select.js +6 -2
- package/dist/src/core/launch.d.ts +4 -0
- package/dist/src/core/launch.js +7 -0
- package/dist/src/core/limit-reset.d.ts +198 -0
- package/dist/src/core/limit-reset.js +299 -0
- package/dist/src/core/quota-fetch.d.ts +57 -2
- package/dist/src/core/quota-fetch.js +291 -13
- package/dist/src/core/quota-levels.d.ts +70 -0
- package/dist/src/core/quota-levels.js +80 -0
- package/dist/src/core/quota-store.d.ts +15 -1
- package/dist/src/core/quota-store.js +26 -1
- package/dist/src/core/same-login.d.ts +8 -0
- package/dist/src/core/same-login.js +39 -2
- package/dist/src/core/search-catalog.js +38 -0
- package/dist/src/families/claude/index.js +2 -0
- package/dist/src/families/codex/index.js +3 -1
- package/dist/src/families/codex/quota.d.ts +23 -1
- package/dist/src/families/codex/quota.js +122 -21
- package/dist/src/families/grok/quota-log.d.ts +1 -0
- package/dist/src/menubar/script.js +70 -18
- package/dist/src/menubar/snapshot.d.ts +33 -1
- package/dist/src/menubar/snapshot.js +79 -11
- package/dist/src/menubar/strings.d.ts +16 -0
- package/dist/src/menubar/strings.js +16 -0
- package/dist/src/panel/handler.d.ts +21 -0
- package/dist/src/panel/handler.js +111 -9
- package/dist/src/panel/i18n.d.ts +87 -0
- package/dist/src/panel/i18n.js +174 -0
- package/dist/src/panel/page-logic.d.ts +17 -0
- package/dist/src/panel/page-logic.js +23 -0
- package/dist/src/panel/page-script.js +440 -19
- package/dist/src/panel/page-styles.js +38 -2
- package/dist/src/plugins/loader.d.ts +2 -0
- package/dist/src/plugins/loader.js +27 -0
- package/dist/src/runtime.d.ts +63 -1
- package/dist/src/runtime.js +529 -9
- package/dist/src/types.d.ts +67 -0
- package/package.json +1 -1
- package/schemas/advisor.json +332 -0
- package/schemas/config.json +22 -0
- package/schemas/error.json +4 -0
- package/schemas/menubar-reset.json +93 -0
- package/schemas/menubar-snapshot.json +70 -2
- package/schemas/quota-reset.json +369 -0
- package/schemas/quota.json +30 -0
- package/schemas/update.json +4 -0
- package/skills/sideby/SKILL.md +6 -2
- package/skills/sideby/references/ai-tasks.md +1 -1
- package/skills/sideby/references/panel.md +14 -0
- package/skills/sideby/references/quota.md +27 -0
- package/skills/sideby/references/update.md +1 -1
|
@@ -6,15 +6,70 @@ import { type QuotaStoreEntry } from './quota-store.ts';
|
|
|
6
6
|
* the kept answer only.
|
|
7
7
|
*/
|
|
8
8
|
export type QuotaFetchMode = 'wait' | 'background' | 'force' | 'never';
|
|
9
|
+
/**
|
|
10
|
+
* What decides which Account is asked first when several are due at once (spec §3.26): Pinned Accounts, then shown
|
|
11
|
+
* Accounts with a window at its warning level, then the most recently used, Hidden ones last.
|
|
12
|
+
*/
|
|
13
|
+
export interface QuotaAskHint {
|
|
14
|
+
pinned?: boolean;
|
|
15
|
+
hidden?: boolean;
|
|
16
|
+
/** When the login was last used on this computer, in ms; the newer, the earlier it is asked. */
|
|
17
|
+
usedAt?: number;
|
|
18
|
+
/**
|
|
19
|
+
* The Family's local reading of this Account, when it has one (Claude Code's status line). While it is newer than
|
|
20
|
+
* `everyMinutes` and covers everything the kept answer holds, the Host is not asked (`localCovers`).
|
|
21
|
+
*/
|
|
22
|
+
local?: QuotaResult | null;
|
|
23
|
+
/**
|
|
24
|
+
* Only an answer whose ask began at or after this real time (ms) will do, also when another reader is asking right
|
|
25
|
+
* now or kept its answer meanwhile: a read that must come after a change, such as a Limit Reset just spent
|
|
26
|
+
* (ADR-0072). When an answer was kept does not count: an ask that began before the change can be kept after it.
|
|
27
|
+
*/
|
|
28
|
+
notBefore?: number;
|
|
29
|
+
/**
|
|
30
|
+
* What is kept for the other Accounts of this Account's Same Login, read when called. A reset instant one of them
|
|
31
|
+
* already asked for, or whose answer was taken after it, had its one extra ask (ADR-0073), whichever Account asks
|
|
32
|
+
* for the login now.
|
|
33
|
+
*/
|
|
34
|
+
peers?: () => Promise<readonly (QuotaStoreEntry | null)[]>;
|
|
35
|
+
/**
|
|
36
|
+
* The real time (ms) a Limit Reset was last used for this login (ADR-0072), read when called. An answer whose ask
|
|
37
|
+
* began before it may hold the numbers from before, however late it arrives: it is not kept, and the Account stays
|
|
38
|
+
* due.
|
|
39
|
+
*/
|
|
40
|
+
changedAt?: () => Promise<number | undefined>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Whether a local reading stands in for a due ask (spec §3.26): it was taken within `everyMinutes`, after every
|
|
44
|
+
* window of the kept answer was read and after each of their reset times that has passed, and it shows every window
|
|
45
|
+
* the kept answer holds. A kept model-group window the local reading cannot see (Claude Code's status line has only
|
|
46
|
+
* the plan-wide ones), or a kept count of Limit Resets, still needs the Host. Never for a Family whose local records
|
|
47
|
+
* can replay old numbers (`localFallbackOnly`, Codex), nor before a first answer is kept.
|
|
48
|
+
*/
|
|
49
|
+
export declare function localCovers(fq: Pick<QuotaFetch, 'everyMinutes' | 'localFallbackOnly'>, local: QuotaResult | null | undefined, kept: QuotaStoreEntry | null, now: Date): boolean;
|
|
9
50
|
export declare const FETCH_TIMEOUT_MS = 20000;
|
|
51
|
+
/** Fetches that run at once: in this process, and across every sideby process on the machine. */
|
|
52
|
+
export declare const MAX_ASKS = 4;
|
|
53
|
+
/** Whether this process is asking the Account's Host right now (the Panel marks such a row as being read). */
|
|
54
|
+
export declare function quotaAsking(stateDir: string, family: string, name: string): boolean;
|
|
10
55
|
/** Why fetching is off for `family`, or null when it is on (spec §3.26). */
|
|
11
56
|
export declare function quotaCheckOff(family: string, config: {
|
|
12
57
|
quota?: {
|
|
13
58
|
check?: Record<string, boolean>;
|
|
14
59
|
};
|
|
15
60
|
}, env: Env): string | null;
|
|
16
|
-
/**
|
|
17
|
-
|
|
61
|
+
/**
|
|
62
|
+
* The published reset instants of the kept answer that passed after it was taken and were not asked for yet
|
|
63
|
+
* (ADR-0073): windows that reset at the same instant count once, and so does an instant handled for another Account of
|
|
64
|
+
* the Same Login (`peers`). Empty without a kept answer.
|
|
65
|
+
*/
|
|
66
|
+
export declare function passedResets(kept: QuotaStoreEntry | null, now: Date, peers?: readonly (QuotaStoreEntry | null)[]): string[];
|
|
67
|
+
/**
|
|
68
|
+
* The kept answer for `account`, after asking again when `mode` says so and it is due: older than `everyMinutes`, or
|
|
69
|
+
* one of its windows passed its published reset time since it was taken and that instant was not asked for yet
|
|
70
|
+
* (ADR-0073).
|
|
71
|
+
*/
|
|
72
|
+
export declare function fetchedQuota(fq: QuotaFetch, account: Account, ctx: ReadContext, hostEnv: Env, mode: QuotaFetchMode, hint?: QuotaAskHint): Promise<QuotaStoreEntry | null>;
|
|
18
73
|
/**
|
|
19
74
|
* The Expired Login of a signed-in Subscription Account (ADR-0071), from what its Host wrote (`events`) and the kept
|
|
20
75
|
* Quota answer: expired when the newest sign that the login expired is newer than every sign that it works (a
|
|
@@ -1,10 +1,132 @@
|
|
|
1
|
+
// When sideby calls a Family's `fetchQuota`, and what it shows from the answer (spec §3.26, ADR-0025): the last answer
|
|
2
|
+
// is kept per Account, asked again only when it is older than the Family's `everyMinutes`, when one of its windows
|
|
3
|
+
// passed its published reset time (once per reset instant, ADR-0073) or the user refreshes, and not while a local
|
|
4
|
+
// reading newer than that covers all of it (`localCovers`); one fetch per Account at
|
|
5
|
+
// a time, in this process and across processes, at most four fetches on the machine at once, 20 seconds each; the
|
|
6
|
+
// newer of the kept answer and the local reading is shown.
|
|
7
|
+
import { mkdir } from 'node:fs/promises';
|
|
8
|
+
import { dirname, join } from 'node:path';
|
|
9
|
+
import { takeFileLock } from './lock.js';
|
|
10
|
+
import { windowLevel } from './quota-levels.js';
|
|
1
11
|
import { keptQuota, quotaStoreFile, readQuotaStore, writeQuotaStore, } from './quota-store.js';
|
|
2
12
|
import { inCi } from './update.js';
|
|
13
|
+
/** Whether the ask that gave `entry` began at or after `notBefore` (real ms); false when it does not say. */
|
|
14
|
+
const beganAfter = (entry, notBefore) => Date.parse(entry?.startedAt ?? '') >= notBefore;
|
|
15
|
+
/**
|
|
16
|
+
* Whether a local reading stands in for a due ask (spec §3.26): it was taken within `everyMinutes`, after every
|
|
17
|
+
* window of the kept answer was read and after each of their reset times that has passed, and it shows every window
|
|
18
|
+
* the kept answer holds. A kept model-group window the local reading cannot see (Claude Code's status line has only
|
|
19
|
+
* the plan-wide ones), or a kept count of Limit Resets, still needs the Host. Never for a Family whose local records
|
|
20
|
+
* can replay old numbers (`localFallbackOnly`, Codex), nor before a first answer is kept.
|
|
21
|
+
*/
|
|
22
|
+
export function localCovers(fq, local, kept, now) {
|
|
23
|
+
if (fq.localFallbackOnly || local?.status !== 'ok' || !kept?.quota)
|
|
24
|
+
return false;
|
|
25
|
+
const every = fq.everyMinutes * 60_000;
|
|
26
|
+
// A reading is judged by when it was taken: each window by its own time when it has one, else the reading's.
|
|
27
|
+
const fresh = (at) => Number.isFinite(at) && now.getTime() - at >= -60_000 && now.getTime() - at < every;
|
|
28
|
+
if (!fresh(Date.parse(local.observedAt)))
|
|
29
|
+
return false;
|
|
30
|
+
if (kept.quota.limitResets && !local.limitResets)
|
|
31
|
+
return false;
|
|
32
|
+
const mine = new Map(local.windows.map((w) => [`${w.name ?? ''}\0${w.label}`, w]));
|
|
33
|
+
const keptAt = kept.quota.observedAt;
|
|
34
|
+
return kept.quota.windows.every((w) => {
|
|
35
|
+
const own = mine.get(`${w.name ?? ''}\0${w.label}`);
|
|
36
|
+
if (!own)
|
|
37
|
+
return false;
|
|
38
|
+
const at = Date.parse(own.observedAt ?? local.observedAt);
|
|
39
|
+
if (!fresh(at))
|
|
40
|
+
return false;
|
|
41
|
+
// Read before the kept window was: it tells nothing newer.
|
|
42
|
+
const before = Date.parse(w.observedAt ?? keptAt);
|
|
43
|
+
if (Number.isFinite(before) && at < before)
|
|
44
|
+
return false;
|
|
45
|
+
const reset = Date.parse(w.resetsAt);
|
|
46
|
+
// A window that reset after the local reading was taken shows its new numbers only in a newer reading.
|
|
47
|
+
return !(Number.isFinite(reset) && reset <= now.getTime() && reset > at);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
3
50
|
export const FETCH_TIMEOUT_MS = 20_000;
|
|
4
51
|
/** How long a `background` read waits when nothing is kept yet, so a first Panel load still shows Quota. */
|
|
5
52
|
const FIRST_WAIT_MS = 4_000;
|
|
53
|
+
/** Fetches that run at once: in this process, and across every sideby process on the machine. */
|
|
54
|
+
export const MAX_ASKS = 4;
|
|
55
|
+
/** How long asks that arrive together are gathered before the first is started, so they start in order. */
|
|
56
|
+
const GATHER_MS = 25;
|
|
57
|
+
/** A fetch waits this long for one of the machine's `MAX_ASKS` places, then leaves the kept answer as it is. */
|
|
58
|
+
const SLOT_WAIT_MS = 60_000;
|
|
59
|
+
const LOCK = { staleMs: 30_000, beatMs: 5_000, maxMs: 120_000 };
|
|
6
60
|
/** One fetch per Account file at a time, shared by every reader in this process. */
|
|
7
61
|
const inFlight = new Map();
|
|
62
|
+
/**
|
|
63
|
+
* Why the extra ask after a passed reset time (ADR-0073) was not sent for an Account file: its mark could not be
|
|
64
|
+
* saved. Shown with the kept answer until a write to that file works again.
|
|
65
|
+
*/
|
|
66
|
+
const unmarked = new Map();
|
|
67
|
+
/** `kept` with the reason its extra ask is waiting, when there is one and no fetch error of its own. */
|
|
68
|
+
const noted = (file, kept) => {
|
|
69
|
+
const why = unmarked.get(file);
|
|
70
|
+
return kept && why && !kept.error ? { ...kept, error: why } : kept;
|
|
71
|
+
};
|
|
72
|
+
/** Whether this process is asking the Account's Host right now (the Panel marks such a row as being read). */
|
|
73
|
+
export function quotaAsking(stateDir, family, name) {
|
|
74
|
+
return inFlight.has(quotaStoreFile(stateDir, family, name));
|
|
75
|
+
}
|
|
76
|
+
const waiting = [];
|
|
77
|
+
let running = 0;
|
|
78
|
+
let seq = 0;
|
|
79
|
+
let gathering = false;
|
|
80
|
+
function pump() {
|
|
81
|
+
waiting.sort((a, b) => a.rank - b.rank || b.usedAt - a.usedAt || a.seq - b.seq);
|
|
82
|
+
while (running < MAX_ASKS && waiting.length) {
|
|
83
|
+
running++;
|
|
84
|
+
waiting.shift().start();
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Runs `fn` when one of this process's `MAX_ASKS` places is free, the lowest `rank` first. Asks that arrive together
|
|
89
|
+
* are gathered for a moment before the first starts, so they start in that order.
|
|
90
|
+
*/
|
|
91
|
+
function queued(rank, usedAt, fn) {
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
waiting.push({
|
|
94
|
+
rank,
|
|
95
|
+
usedAt,
|
|
96
|
+
seq: seq++,
|
|
97
|
+
start: () => void fn()
|
|
98
|
+
.then(resolve, reject)
|
|
99
|
+
.finally(() => {
|
|
100
|
+
running--;
|
|
101
|
+
pump();
|
|
102
|
+
}),
|
|
103
|
+
});
|
|
104
|
+
if (gathering)
|
|
105
|
+
return;
|
|
106
|
+
gathering = true;
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
gathering = false;
|
|
109
|
+
pump();
|
|
110
|
+
}, GATHER_MS);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
/** One of the machine's `MAX_ASKS` places (lock files in sideby's state directory), or null when none came free. */
|
|
114
|
+
async function takeSlot(stateDir, waitMs) {
|
|
115
|
+
const dir = join(stateDir, 'quota', 'asking');
|
|
116
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
117
|
+
const deadline = Date.now() + waitMs;
|
|
118
|
+
for (;;) {
|
|
119
|
+
for (let i = 0; i < MAX_ASKS; i++) {
|
|
120
|
+
const release = await takeFileLock(join(dir, `${i}.lock`), LOCK).catch(() => null);
|
|
121
|
+
if (release)
|
|
122
|
+
return release;
|
|
123
|
+
}
|
|
124
|
+
if (Date.now() >= deadline)
|
|
125
|
+
return null;
|
|
126
|
+
// Not unref'd: a CLI that waits for a place must stay alive meanwhile.
|
|
127
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
128
|
+
}
|
|
129
|
+
}
|
|
8
130
|
/** Why fetching is off for `family`, or null when it is on (spec §3.26). */
|
|
9
131
|
export function quotaCheckOff(family, config, env) {
|
|
10
132
|
if (config.quota?.check?.[family] === false)
|
|
@@ -16,8 +138,10 @@ export function quotaCheckOff(family, config, env) {
|
|
|
16
138
|
const sleep = (ms) => new Promise((resolve) => {
|
|
17
139
|
setTimeout(() => resolve(null), ms).unref();
|
|
18
140
|
});
|
|
19
|
-
async function runFetch(fq, account, ctx, hostEnv, file) {
|
|
141
|
+
async function runFetch(fq, account, ctx, hostEnv, file, hint = {}) {
|
|
20
142
|
const kept = await readQuotaStore(file);
|
|
143
|
+
// The real clock, as `attemptedAt`: the Host is asked from here on.
|
|
144
|
+
const startedAt = new Date().toISOString();
|
|
21
145
|
const ac = new AbortController();
|
|
22
146
|
const timer = setTimeout(() => ac.abort(), FETCH_TIMEOUT_MS);
|
|
23
147
|
const aborted = new Promise((_, reject) => {
|
|
@@ -41,6 +165,14 @@ async function runFetch(fq, account, ctx, hostEnv, file) {
|
|
|
41
165
|
}
|
|
42
166
|
if (answer?.status === 'unavailable')
|
|
43
167
|
error = answer.detail ?? answer.reason;
|
|
168
|
+
// Asked before a Limit Reset was used for this login, answered after: these may be the numbers from before, and
|
|
169
|
+
// kept now they would show as the newest. What is kept stays (nothing, for a first ask), and stays due, so the next
|
|
170
|
+
// read asks again.
|
|
171
|
+
if (answer?.status === 'ok' && hint.changedAt) {
|
|
172
|
+
const changed = await hint.changedAt().catch(() => undefined);
|
|
173
|
+
if (changed !== undefined && Date.parse(startedAt) < changed)
|
|
174
|
+
return kept;
|
|
175
|
+
}
|
|
44
176
|
const quota = answer?.status === 'ok' ? keptQuota(answer) : kept?.quota;
|
|
45
177
|
const attemptedAt = new Date().toISOString();
|
|
46
178
|
// An answer that asks for a new sign-in marks the login expired; a good answer clears it; any other failure keeps
|
|
@@ -50,38 +182,169 @@ async function runFetch(fq, account, ctx, hostEnv, file) {
|
|
|
50
182
|
: answer?.status === 'ok'
|
|
51
183
|
? undefined
|
|
52
184
|
: kept?.loginExpiredAt;
|
|
185
|
+
// Marks of reset instants already asked for (ADR-0073) stay only while the kept answer still has that instant.
|
|
186
|
+
const instants = new Set((quota?.windows ?? []).map((w) => Date.parse(w.resetsAt)));
|
|
187
|
+
const resetAsked = (kept?.resetAsked ?? []).filter((t) => instants.has(Date.parse(t)));
|
|
53
188
|
const entry = {
|
|
54
189
|
version: 1,
|
|
55
190
|
attemptedAt,
|
|
191
|
+
startedAt,
|
|
56
192
|
...(quota ? { quota } : {}),
|
|
57
193
|
...(error ? { error } : {}),
|
|
58
194
|
...(loginExpiredAt ? { loginExpiredAt } : {}),
|
|
195
|
+
...(resetAsked.length ? { resetAsked } : {}),
|
|
59
196
|
};
|
|
60
|
-
await writeQuotaStore(file, entry).
|
|
197
|
+
await writeQuotaStore(file, entry).then(() => unmarked.delete(file), () => { });
|
|
61
198
|
return entry;
|
|
62
199
|
}
|
|
63
|
-
/**
|
|
64
|
-
|
|
200
|
+
/** What is kept for the other Accounts of the Same Login, read now; none when it cannot be read. */
|
|
201
|
+
const peersOf = (hint) => hint.peers ? hint.peers().catch(() => []) : Promise.resolve([]);
|
|
202
|
+
/**
|
|
203
|
+
* The published reset instants of the kept answer that passed after it was taken and were not asked for yet
|
|
204
|
+
* (ADR-0073): windows that reset at the same instant count once, and so does an instant handled for another Account of
|
|
205
|
+
* the Same Login (`peers`). Empty without a kept answer.
|
|
206
|
+
*/
|
|
207
|
+
export function passedResets(kept, now, peers = []) {
|
|
208
|
+
const q = kept?.quota;
|
|
209
|
+
if (!q)
|
|
210
|
+
return [];
|
|
211
|
+
const asked = new Set((kept.resetAsked ?? []).map((t) => Date.parse(t)));
|
|
212
|
+
const out = new Map();
|
|
213
|
+
for (const w of q.windows) {
|
|
214
|
+
const at = Date.parse(w.resetsAt);
|
|
215
|
+
const taken = Date.parse(w.observedAt ?? q.observedAt);
|
|
216
|
+
if (!Number.isFinite(at) || !Number.isFinite(taken))
|
|
217
|
+
continue;
|
|
218
|
+
if (at > taken && at <= now.getTime() && !asked.has(at))
|
|
219
|
+
out.set(at, new Date(at).toISOString());
|
|
220
|
+
}
|
|
221
|
+
// A Same Login is one vendor Quota: an instant another of its Accounts asked for, or answered after, is done.
|
|
222
|
+
for (const p of peers) {
|
|
223
|
+
for (const t of p?.resetAsked ?? [])
|
|
224
|
+
out.delete(Date.parse(t));
|
|
225
|
+
// When its kept answer was taken: by the start of the ask that brought it, or, after a later ask failed (the
|
|
226
|
+
// start kept is then that ask's), by the answer's own time. A failed ask is never taken for an answer.
|
|
227
|
+
const answered = !p?.quota
|
|
228
|
+
? Number.NaN
|
|
229
|
+
: Date.parse(p.error ? p.quota.observedAt : (p.startedAt ?? p.quota.observedAt));
|
|
230
|
+
if (Number.isFinite(answered))
|
|
231
|
+
for (const at of [...out.keys()])
|
|
232
|
+
if (answered >= at)
|
|
233
|
+
out.delete(at);
|
|
234
|
+
}
|
|
235
|
+
return [...out.values()];
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* One ask for `file`, across processes: waits for another process that is asking the same Account and takes its
|
|
239
|
+
* answer, marks the reset instants this ask is for before asking, and holds one of the machine's `MAX_ASKS` places
|
|
240
|
+
* while the Host runs. Null (or the kept answer) when no place or lock came free. `extra` is an ask that is due only
|
|
241
|
+
* because a reset time passed inside `everyMinutes` (ADR-0073): it is sent only once its mark is on disk, and not at
|
|
242
|
+
* all when another process marked that instant meanwhile.
|
|
243
|
+
*/
|
|
244
|
+
async function askOnce(fq, account, ctx, hostEnv, file, judged, extra, notBefore, hint = {}) {
|
|
245
|
+
await mkdir(dirname(file), { recursive: true, mode: 0o700 });
|
|
246
|
+
const lock = await takeFileLock(`${file}.lock`, {
|
|
247
|
+
...LOCK,
|
|
248
|
+
wait: { timeoutMs: FETCH_TIMEOUT_MS + 10_000, pollMs: 100 },
|
|
249
|
+
}).catch(() => null);
|
|
250
|
+
// Another process is still asking this Account: what is kept shows meanwhile.
|
|
251
|
+
if (!lock)
|
|
252
|
+
return readQuotaStore(file);
|
|
253
|
+
try {
|
|
254
|
+
const kept = await readQuotaStore(file);
|
|
255
|
+
// Another process asked while this one waited for the lock: its answer is the one this ask wanted.
|
|
256
|
+
// Unless this ask must come after a change, and that answer's ask began before it: kept late, it still holds
|
|
257
|
+
// the numbers from before.
|
|
258
|
+
const early = notBefore !== undefined && !beganAfter(kept, notBefore);
|
|
259
|
+
if (kept && kept.attemptedAt !== judged?.attemptedAt && !early)
|
|
260
|
+
return kept;
|
|
261
|
+
const due = passedResets(kept, ctx.now, await peersOf(hint));
|
|
262
|
+
// Judged again under the lock: another process that was due for the same reset instant marked it and asked while
|
|
263
|
+
// this one waited. `attemptedAt` does not tell, because what its ask brought may not have been kept. The instant
|
|
264
|
+
// had its one ask, so the kept answer shows on until the regular ask of `everyMinutes` is due.
|
|
265
|
+
if (extra && kept && !due.length && !olderThanInterval(fq, kept, ctx.now))
|
|
266
|
+
return kept;
|
|
267
|
+
if (kept && due.length) {
|
|
268
|
+
const marked = await writeQuotaStore(file, {
|
|
269
|
+
...kept,
|
|
270
|
+
resetAsked: [...(kept.resetAsked ?? []), ...due],
|
|
271
|
+
}).then(() => true, () => false);
|
|
272
|
+
if (marked)
|
|
273
|
+
unmarked.delete(file);
|
|
274
|
+
else if (extra) {
|
|
275
|
+
// Without the mark a failed ask would be sent again on every read: "once per reset instant" needs it on disk
|
|
276
|
+
// first. The kept answer goes on showing, with why it was not asked again.
|
|
277
|
+
const why = `a window's reset time has passed, but sideby could not write ${file} to note that it asks again, so it did not ask; it will once that file can be written, or at the next regular check`;
|
|
278
|
+
unmarked.set(file, why);
|
|
279
|
+
return noted(file, kept);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const slot = await takeSlot(ctx.stateDir, SLOT_WAIT_MS);
|
|
283
|
+
if (!slot)
|
|
284
|
+
return readQuotaStore(file);
|
|
285
|
+
try {
|
|
286
|
+
return await runFetch(fq, account, ctx, hostEnv, file, hint);
|
|
287
|
+
}
|
|
288
|
+
finally {
|
|
289
|
+
await slot();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
finally {
|
|
293
|
+
await lock();
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Whether the regular ask is due: nothing is kept, or it was last asked `everyMinutes` ago or longer. A time in the
|
|
298
|
+
* future (a changed clock) counts as old, so it cannot stop every later fetch.
|
|
299
|
+
*/
|
|
300
|
+
function olderThanInterval(fq, kept, now) {
|
|
301
|
+
const at = kept ? Date.parse(kept.attemptedAt) : Number.NaN;
|
|
302
|
+
const age = now.getTime() - at;
|
|
303
|
+
return !kept || !Number.isFinite(at) || age < -60_000 || age >= fq.everyMinutes * 60_000;
|
|
304
|
+
}
|
|
305
|
+
/** Whether a kept answer has a window that counts toward Quota Pressure at or over its warning level. */
|
|
306
|
+
const tight = (kept) => (kept?.quota?.windows ?? []).some((w) => !w.name && windowLevel(w) !== 'ok');
|
|
307
|
+
/**
|
|
308
|
+
* The kept answer for `account`, after asking again when `mode` says so and it is due: older than `everyMinutes`, or
|
|
309
|
+
* one of its windows passed its published reset time since it was taken and that instant was not asked for yet
|
|
310
|
+
* (ADR-0073).
|
|
311
|
+
*/
|
|
312
|
+
export async function fetchedQuota(fq, account, ctx, hostEnv, mode, hint = {}) {
|
|
65
313
|
const file = quotaStoreFile(ctx.stateDir, account.family, account.name);
|
|
66
314
|
const kept = await readQuotaStore(file);
|
|
67
315
|
if (mode === 'never')
|
|
68
316
|
return kept;
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
// A time in the future (a changed clock) counts as old, so it cannot stop every later fetch.
|
|
72
|
-
const stale = !kept || !Number.isFinite(at) || age < -60_000 || age >= fq.everyMinutes * 60_000;
|
|
73
|
-
if (mode !== 'force' && !stale)
|
|
317
|
+
// A local reading that is newer than the Family asks, and shows all the kept answer holds, spares the Host.
|
|
318
|
+
if (mode !== 'force' && localCovers(fq, hint.local, kept, ctx.now))
|
|
74
319
|
return kept;
|
|
320
|
+
const stale = olderThanInterval(fq, kept, ctx.now);
|
|
321
|
+
if (mode !== 'force' && !stale && passedResets(kept, ctx.now, await peersOf(hint)).length === 0) {
|
|
322
|
+
// Nothing is due, so no extra ask is waiting for its mark any more.
|
|
323
|
+
unmarked.delete(file);
|
|
324
|
+
return kept;
|
|
325
|
+
}
|
|
326
|
+
const notBefore = mode === 'force' ? hint.notBefore : undefined;
|
|
75
327
|
let run = inFlight.get(file);
|
|
328
|
+
// An ask already running may have begun before the change this one must see, or may bring an answer another
|
|
329
|
+
// process kept: let it end, and ask anew unless what it brought began after the change.
|
|
330
|
+
while (run && notBefore !== undefined) {
|
|
331
|
+
const joined = await run.catch(() => null);
|
|
332
|
+
if (beganAfter(joined, notBefore))
|
|
333
|
+
return joined;
|
|
334
|
+
run = inFlight.get(file);
|
|
335
|
+
}
|
|
76
336
|
if (!run) {
|
|
77
|
-
|
|
337
|
+
const rank = hint.hidden ? 3 : hint.pinned ? 0 : tight(kept) ? 1 : 2;
|
|
338
|
+
// Due only because a reset time passed inside `everyMinutes`: the one extra ask of ADR-0073.
|
|
339
|
+
const extra = mode !== 'force' && !stale;
|
|
340
|
+
run = queued(rank, hint.usedAt ?? 0, () => askOnce(fq, account, ctx, hostEnv, file, kept, extra, notBefore, hint)).finally(() => inFlight.delete(file));
|
|
78
341
|
inFlight.set(file, run);
|
|
79
342
|
}
|
|
80
343
|
if (mode !== 'background')
|
|
81
344
|
return run;
|
|
82
345
|
run.catch(() => { });
|
|
83
346
|
if (kept)
|
|
84
|
-
return kept;
|
|
347
|
+
return noted(file, kept);
|
|
85
348
|
return Promise.race([run, sleep(FIRST_WAIT_MS)]).then((e) => e ?? kept);
|
|
86
349
|
}
|
|
87
350
|
/**
|
|
@@ -127,8 +390,23 @@ export function shownQuota(local, kept, off, localFallbackOnly = false) {
|
|
|
127
390
|
// A local reading (Claude's status line) sees only the plan-wide windows. Keep the fetched model-group windows it
|
|
128
391
|
// cannot see, so a group's line (such as Fable) does not come and go with whichever reading is newer.
|
|
129
392
|
const seen = new Set(own.windows.map((w) => `${w.name ?? ''}\0${w.label}`));
|
|
130
|
-
|
|
131
|
-
|
|
393
|
+
// Those windows keep the time and source they were read at: the newer reading did not cover them (ADR-0073).
|
|
394
|
+
const groups = fetched.windows
|
|
395
|
+
.filter((w) => w.name && !seen.has(`${w.name}\0${w.label}`))
|
|
396
|
+
.map((w) => {
|
|
397
|
+
const observedAt = w.observedAt ?? fetched.observedAt;
|
|
398
|
+
const source = w.source ?? fetched.source;
|
|
399
|
+
return {
|
|
400
|
+
...w,
|
|
401
|
+
...(observedAt !== own.observedAt ? { observedAt } : {}),
|
|
402
|
+
...(source !== own.source ? { source } : {}),
|
|
403
|
+
};
|
|
404
|
+
});
|
|
405
|
+
// A local reading knows nothing of Limit Resets; the Host's own count stays, with its own read time.
|
|
406
|
+
const limitResets = own.limitResets ?? fetched.limitResets;
|
|
407
|
+
if (!groups.length && !limitResets)
|
|
408
|
+
return own;
|
|
409
|
+
return { ...own, windows: [...own.windows, ...groups], ...(limitResets ? { limitResets } : {}) };
|
|
132
410
|
}
|
|
133
411
|
if (fetched)
|
|
134
412
|
return fetched;
|
|
@@ -59,6 +59,76 @@ export declare function quotaIsStale(quota: {
|
|
|
59
59
|
observedAt: string;
|
|
60
60
|
fetchError?: string;
|
|
61
61
|
}, now: number, lastUseAt?: string | null): boolean;
|
|
62
|
+
/** How old one Quota window's number is, by the one rule the Panel, the CLI and the Menu Bar Item share (ADR-0073). */
|
|
63
|
+
export interface WindowAge {
|
|
64
|
+
/** When this window was read: its own time, else the Quota's. */
|
|
65
|
+
observedAt: string;
|
|
66
|
+
/** Where it was read from: its own source, else the Quota's. */
|
|
67
|
+
source: string;
|
|
68
|
+
/**
|
|
69
|
+
* Old: the latest ask failed, the time cannot be read or lies ahead, or it is over an hour old and this computer
|
|
70
|
+
* has used the login since, or its Usage cannot be read (`quotaIsStale`, ADR-0051).
|
|
71
|
+
*/
|
|
72
|
+
stale: boolean;
|
|
73
|
+
/** Over an hour old, but this computer has not used the login since: the number stands. */
|
|
74
|
+
quiet: boolean;
|
|
75
|
+
/** Whether the age is written next to the number: old, or older than the Family's `everyMinutes`. */
|
|
76
|
+
showAge: boolean;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* One window's age from its own read time (`observedAt` on the window, else the Quota's). `everyMinutes` is how
|
|
80
|
+
* often the Family asks its Host; without it the age is written only once the number is old. Self-contained apart
|
|
81
|
+
* from `quotaIsStale`, so the Panel can inline both.
|
|
82
|
+
*/
|
|
83
|
+
export declare function windowAge(quota: {
|
|
84
|
+
observedAt: string;
|
|
85
|
+
source?: string;
|
|
86
|
+
fetchError?: string;
|
|
87
|
+
}, w: {
|
|
88
|
+
observedAt?: string;
|
|
89
|
+
source?: string;
|
|
90
|
+
} | null | undefined, now: number, lastUseAt?: string | null, everyMinutes?: number): WindowAge;
|
|
91
|
+
/**
|
|
92
|
+
* The oldest age among a Quota's windows (the Quota's own when it has none): what a row says once for all its
|
|
93
|
+
* numbers. `stale`, `quiet` and `showAge` are true when they are for any window.
|
|
94
|
+
*/
|
|
95
|
+
export declare function quotaAge(quota: {
|
|
96
|
+
observedAt: string;
|
|
97
|
+
source?: string;
|
|
98
|
+
fetchError?: string;
|
|
99
|
+
windows?: readonly {
|
|
100
|
+
observedAt?: string;
|
|
101
|
+
source?: string;
|
|
102
|
+
}[];
|
|
103
|
+
}, now: number, lastUseAt?: string | null, everyMinutes?: number): WindowAge;
|
|
104
|
+
/** A known Limit Reset that expires within this many hours makes the action a visible button (ADR-0072). */
|
|
105
|
+
export declare const LIMIT_RESET_EXPIRY_HOURS = 72;
|
|
106
|
+
/**
|
|
107
|
+
* Whether "use a reset" is a visible button on an Account's row, and why (spec §3.40, ADR-0072); null leaves the
|
|
108
|
+
* action in the row's menu and the command palette. `use` (the Host spends one, Codex): the Account holds one, and a
|
|
109
|
+
* plan-wide window is at its warning level or a known one expires within 72 hours. `page` (only the vendor's page
|
|
110
|
+
* spends one, Claude Code): any window is at its warning level, a model group's included, since sideby cannot see the
|
|
111
|
+
* count. A window whose reset time has passed still counts: the numbers are read again before anything is spent.
|
|
112
|
+
* `warn` is the warning level of a Short and of a Long Window (60 and 85). Self-contained, so the Panel can inline it.
|
|
113
|
+
*/
|
|
114
|
+
export declare function limitResetOffer(quota: {
|
|
115
|
+
windows?: readonly {
|
|
116
|
+
label?: string;
|
|
117
|
+
name?: string;
|
|
118
|
+
windowMinutes?: number;
|
|
119
|
+
usedPercent: number;
|
|
120
|
+
}[];
|
|
121
|
+
limitResets?: {
|
|
122
|
+
available: number;
|
|
123
|
+
nextExpiresAt?: string;
|
|
124
|
+
};
|
|
125
|
+
} | null | undefined, kind: 'use' | 'page', now: number, warn: {
|
|
126
|
+
short: number;
|
|
127
|
+
long: number;
|
|
128
|
+
}): {
|
|
129
|
+
tight: number[];
|
|
130
|
+
expiring: boolean;
|
|
131
|
+
} | null;
|
|
62
132
|
/** The warning level of a used percent; `long` for a Long Window, which warns later (spec §3.26). */
|
|
63
133
|
export declare function quotaLevel(usedPercent: number, long?: boolean): QuotaLevel;
|
|
64
134
|
/** One window's warning level by its own kind: a Short Window warns at 60/85, a Long Window at 85/95. */
|
|
@@ -61,6 +61,86 @@ export function quotaIsStale(quota, now, lastUseAt) {
|
|
|
61
61
|
const used = lastUseAt ? Date.parse(lastUseAt) : Number.NaN;
|
|
62
62
|
return !(Number.isFinite(used) && used - at <= 600_000);
|
|
63
63
|
}
|
|
64
|
+
/**
|
|
65
|
+
* One window's age from its own read time (`observedAt` on the window, else the Quota's). `everyMinutes` is how
|
|
66
|
+
* often the Family asks its Host; without it the age is written only once the number is old. Self-contained apart
|
|
67
|
+
* from `quotaIsStale`, so the Panel can inline both.
|
|
68
|
+
*/
|
|
69
|
+
export function windowAge(quota, w, now, lastUseAt, everyMinutes) {
|
|
70
|
+
const observedAt = w?.observedAt || quota.observedAt;
|
|
71
|
+
const source = w?.source || quota.source || '';
|
|
72
|
+
const stale = quotaIsStale({ observedAt, fetchError: quota.fetchError }, now, lastUseAt);
|
|
73
|
+
const age = now - Date.parse(observedAt);
|
|
74
|
+
const every = Number(everyMinutes);
|
|
75
|
+
return {
|
|
76
|
+
observedAt,
|
|
77
|
+
source,
|
|
78
|
+
stale,
|
|
79
|
+
quiet: !stale && age > 3600_000,
|
|
80
|
+
showAge: stale || (Number.isFinite(every) && every > 0 && age > every * 60_000),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The oldest age among a Quota's windows (the Quota's own when it has none): what a row says once for all its
|
|
85
|
+
* numbers. `stale`, `quiet` and `showAge` are true when they are for any window.
|
|
86
|
+
*/
|
|
87
|
+
export function quotaAge(quota, now, lastUseAt, everyMinutes) {
|
|
88
|
+
const list = quota.windows?.length ? quota.windows : [null];
|
|
89
|
+
let out;
|
|
90
|
+
for (const w of list) {
|
|
91
|
+
const a = windowAge(quota, w, now, lastUseAt, everyMinutes);
|
|
92
|
+
if (!out) {
|
|
93
|
+
out = a;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
// A time that cannot be read is the oldest there is.
|
|
97
|
+
const at = Date.parse(a.observedAt);
|
|
98
|
+
const best = Date.parse(out.observedAt);
|
|
99
|
+
const older = Number.isFinite(best) && !(at >= best);
|
|
100
|
+
out = {
|
|
101
|
+
observedAt: older ? a.observedAt : out.observedAt,
|
|
102
|
+
source: older ? a.source : out.source,
|
|
103
|
+
stale: out.stale || a.stale,
|
|
104
|
+
quiet: out.quiet || a.quiet,
|
|
105
|
+
showAge: out.showAge || a.showAge,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
return out;
|
|
109
|
+
}
|
|
110
|
+
/** A known Limit Reset that expires within this many hours makes the action a visible button (ADR-0072). */
|
|
111
|
+
export const LIMIT_RESET_EXPIRY_HOURS = 72;
|
|
112
|
+
/**
|
|
113
|
+
* Whether "use a reset" is a visible button on an Account's row, and why (spec §3.40, ADR-0072); null leaves the
|
|
114
|
+
* action in the row's menu and the command palette. `use` (the Host spends one, Codex): the Account holds one, and a
|
|
115
|
+
* plan-wide window is at its warning level or a known one expires within 72 hours. `page` (only the vendor's page
|
|
116
|
+
* spends one, Claude Code): any window is at its warning level, a model group's included, since sideby cannot see the
|
|
117
|
+
* count. A window whose reset time has passed still counts: the numbers are read again before anything is spent.
|
|
118
|
+
* `warn` is the warning level of a Short and of a Long Window (60 and 85). Self-contained, so the Panel can inline it.
|
|
119
|
+
*/
|
|
120
|
+
export function limitResetOffer(quota, kind, now, warn) {
|
|
121
|
+
if (!quota)
|
|
122
|
+
return null;
|
|
123
|
+
const stock = quota.limitResets;
|
|
124
|
+
if (kind === 'use' && !(stock && stock.available > 0))
|
|
125
|
+
return null;
|
|
126
|
+
const minutes = (w) => {
|
|
127
|
+
const n = Number(w.windowMinutes);
|
|
128
|
+
if (Number.isFinite(n) && n > 0)
|
|
129
|
+
return n;
|
|
130
|
+
const m = /^(\d+(?:\.\d+)?)\s*([mhdw])$/i.exec(String(w.label ?? '').trim());
|
|
131
|
+
return m ? Number(m[1]) * ({ m: 1, h: 60, d: 1440, w: 10080 }[m[2].toLowerCase()] ?? 0) : 0;
|
|
132
|
+
};
|
|
133
|
+
const tight = [];
|
|
134
|
+
(quota.windows ?? []).forEach((w, i) => {
|
|
135
|
+
if (kind === 'use' && w.name)
|
|
136
|
+
return;
|
|
137
|
+
if ((Number(w.usedPercent) || 0) >= (minutes(w) > 1440 ? warn.long : warn.short))
|
|
138
|
+
tight.push(i);
|
|
139
|
+
});
|
|
140
|
+
const expires = kind === 'use' && stock?.nextExpiresAt ? Date.parse(stock.nextExpiresAt) : Number.NaN;
|
|
141
|
+
const expiring = Number.isFinite(expires) && expires > now && expires - now <= 72 * 3600_000;
|
|
142
|
+
return tight.length || expiring ? { tight, expiring } : null;
|
|
143
|
+
}
|
|
64
144
|
/** The warning level of a used percent; `long` for a Long Window, which warns later (spec §3.26). */
|
|
65
145
|
export function quotaLevel(usedPercent, long = false) {
|
|
66
146
|
if (usedPercent >= (long ? QUOTA_LONG_FAIL_PERCENT : QUOTA_FAIL_PERCENT))
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { QuotaResult } from '../types.ts';
|
|
1
|
+
import type { LimitResets, QuotaResult } from '../types.ts';
|
|
2
2
|
export type FetchedQuota = Extract<QuotaResult, {
|
|
3
3
|
status: 'ok';
|
|
4
4
|
}>;
|
|
@@ -6,6 +6,13 @@ export interface QuotaStoreEntry {
|
|
|
6
6
|
version: 1;
|
|
7
7
|
/** When the latest fetch finished, good or not; the next one waits `everyMinutes` from here. */
|
|
8
8
|
attemptedAt: string;
|
|
9
|
+
/**
|
|
10
|
+
* When that fetch began asking the Host (the real clock). What it answers is the Quota of some moment after this,
|
|
11
|
+
* so a reader that needs numbers from after a change (a Limit Reset just used, ADR-0072) compares against this,
|
|
12
|
+
* not against when the answer was kept. Left out by an older sideby: such an answer counts as begun before any
|
|
13
|
+
* change.
|
|
14
|
+
*/
|
|
15
|
+
startedAt?: string;
|
|
9
16
|
/** The latest good answer, kept while later fetches fail. */
|
|
10
17
|
quota?: FetchedQuota;
|
|
11
18
|
/** Why the latest fetch failed, when it did. */
|
|
@@ -15,8 +22,15 @@ export interface QuotaStoreEntry {
|
|
|
15
22
|
* good answer.
|
|
16
23
|
*/
|
|
17
24
|
loginExpiredAt?: string;
|
|
25
|
+
/**
|
|
26
|
+
* The published reset instants (ISO) sideby already asked once more for (ADR-0073), written before that ask so a
|
|
27
|
+
* failed one is not repeated. Only instants of the kept answer's own windows are kept.
|
|
28
|
+
*/
|
|
29
|
+
resetAsked?: string[];
|
|
18
30
|
}
|
|
19
31
|
export declare function quotaStoreFile(stateDir: string, family: string, name: string): string;
|
|
32
|
+
/** The Limit Resets of a reading with only the fields sideby keeps, or undefined when it is not a count. */
|
|
33
|
+
export declare function keptLimitResets(v: unknown): LimitResets | undefined;
|
|
20
34
|
/** Only the fields a Quota reading may carry, so a plugin's extra fields never reach the file. */
|
|
21
35
|
export declare function keptQuota(q: FetchedQuota): FetchedQuota;
|
|
22
36
|
/** The kept answer, or null when there is none or the file is not one sideby wrote. */
|