sideby 0.31.2 → 0.32.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/README.md +4 -4
- package/dist/build.json +1 -1
- package/dist/src/cli/commands.d.ts +0 -4
- package/dist/src/cli/commands.js +76 -14
- package/dist/src/cli/format.js +3 -2
- package/dist/src/cli/json-schemas.js +45 -5
- package/dist/src/cli/main.js +19 -159
- package/dist/src/core/ask.js +6 -1
- package/dist/src/core/cli-commands.d.ts +56 -0
- package/dist/src/core/cli-commands.js +842 -0
- package/dist/src/core/handoff-chain.d.ts +9 -3
- package/dist/src/core/handoff-chain.js +3 -2
- package/dist/src/core/handoff-rules.js +5 -5
- package/dist/src/core/handoff-run.js +19 -2
- package/dist/src/core/handoff-select.d.ts +15 -5
- package/dist/src/core/handoff-select.js +34 -22
- package/dist/src/core/handoff.d.ts +36 -3
- package/dist/src/core/handoff.js +57 -18
- package/dist/src/core/pick-commands.d.ts +19 -4
- package/dist/src/core/pick-commands.js +19 -5
- package/dist/src/core/quota-levels.d.ts +67 -4
- package/dist/src/core/quota-levels.js +82 -6
- package/dist/src/core/search-catalog.js +20 -0
- package/dist/src/families/claude/index.js +3 -0
- package/dist/src/families/claude/quota-fetch.d.ts +26 -0
- package/dist/src/families/claude/quota-fetch.js +236 -0
- package/dist/src/handoff/hook.js +5 -0
- package/dist/src/menubar/script.js +8 -3
- package/dist/src/menubar/snapshot.d.ts +9 -5
- package/dist/src/menubar/snapshot.js +37 -24
- package/dist/src/menubar/strings.d.ts +6 -4
- package/dist/src/menubar/strings.js +6 -4
- package/dist/src/panel/handler.js +7 -3
- package/dist/src/panel/i18n.d.ts +15 -0
- package/dist/src/panel/i18n.js +36 -6
- package/dist/src/panel/page-logic.d.ts +32 -8
- package/dist/src/panel/page-logic.js +43 -16
- package/dist/src/panel/page-script.js +129 -17
- package/dist/src/panel/page-styles.js +18 -0
- package/dist/src/panel/page.js +6 -1
- package/dist/src/runtime.d.ts +2 -1
- package/dist/src/runtime.js +10 -4
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.js +2 -0
- package/package.json +2 -2
- package/schemas/commands-list.json +165 -0
- package/schemas/menubar-snapshot.json +3 -3
- package/schemas/next.json +57 -4
- package/skills/sideby/SKILL.md +3 -2
- package/skills/sideby/references/accounts.md +1 -1
- package/skills/sideby/references/ai-tasks.md +2 -2
- package/skills/sideby/references/diagnose.md +1 -1
- package/skills/sideby/references/handoff.md +13 -9
- package/skills/sideby/references/panel.md +11 -1
- package/skills/sideby/references/quota.md +9 -3
|
@@ -68,11 +68,16 @@ export interface ReadyFile {
|
|
|
68
68
|
sessionId?: string;
|
|
69
69
|
transcriptPath?: string;
|
|
70
70
|
}
|
|
71
|
-
/**
|
|
71
|
+
/**
|
|
72
|
+
* The parent kept a session that waits for a reset at `until` (ADR-0018): it decided to wait, or the Host stopped at
|
|
73
|
+
* its limit with no account to take over. `trigger` is what made the session ready; a `limit` one is cleared by the
|
|
74
|
+
* hook at the next finished turn, since the Host has continued by then.
|
|
75
|
+
*/
|
|
72
76
|
export interface WaitingFile {
|
|
73
77
|
runId: string;
|
|
74
78
|
until: string;
|
|
75
79
|
at: string;
|
|
80
|
+
trigger?: HandoffTrigger;
|
|
76
81
|
}
|
|
77
82
|
export interface ChainHop {
|
|
78
83
|
ref: string;
|
|
@@ -110,8 +115,9 @@ export declare function writeJsonFile(path: string, value: unknown): Promise<voi
|
|
|
110
115
|
/**
|
|
111
116
|
* `sideby handoff ready` (spec §3.17): asks the parent to hand over after this turn. Works only inside a run sideby
|
|
112
117
|
* started with Auto Handoff; `briefFile`, when given, is copied into the chain directory (mode 600); `to` is the
|
|
113
|
-
* Account ref to hand over to. While the session waits for a reset (`waiting.json
|
|
114
|
-
*
|
|
118
|
+
* Account ref to hand over to. While the session waits for a reset (`waiting.json`: the parent chose to wait, or the
|
|
119
|
+
* Host stopped at its limit with no account to take over; typed as `! sideby handoff ready`), no turn will end to pick
|
|
120
|
+
* up a request, so it writes `ready.json` at once (`now`).
|
|
115
121
|
* Returns the request, or null outside such a run.
|
|
116
122
|
*/
|
|
117
123
|
export declare function requestHandoff(env: Readonly<Record<string, string | undefined>>, briefFile?: string, to?: string, now?: Date): Promise<(ReadyFile & {
|
|
@@ -57,8 +57,9 @@ export async function writeJsonFile(path, value) {
|
|
|
57
57
|
/**
|
|
58
58
|
* `sideby handoff ready` (spec §3.17): asks the parent to hand over after this turn. Works only inside a run sideby
|
|
59
59
|
* started with Auto Handoff; `briefFile`, when given, is copied into the chain directory (mode 600); `to` is the
|
|
60
|
-
* Account ref to hand over to. While the session waits for a reset (`waiting.json
|
|
61
|
-
*
|
|
60
|
+
* Account ref to hand over to. While the session waits for a reset (`waiting.json`: the parent chose to wait, or the
|
|
61
|
+
* Host stopped at its limit with no account to take over; typed as `! sideby handoff ready`), no turn will end to pick
|
|
62
|
+
* up a request, so it writes `ready.json` at once (`now`).
|
|
62
63
|
* Returns the request, or null outside such a run.
|
|
63
64
|
*/
|
|
64
65
|
export async function requestHandoff(env, briefFile, to, now = new Date()) {
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
// Panel draws its own words from the same ids and params, so both always say the same thing.
|
|
4
4
|
import { LEVEL_MAX } from '../handoff/levels.js';
|
|
5
5
|
import { handoffOverview } from './handoff-overview.js';
|
|
6
|
-
import {
|
|
6
|
+
import { takeoverLimits } from './quota-levels.js';
|
|
7
7
|
const list = (xs) => xs.join(' → ');
|
|
8
8
|
/** The rules for `settings`; `titles` names each Family, `families` is every Family in load order. */
|
|
9
9
|
export function handoffRules(s, ctx) {
|
|
10
10
|
const title = (f) => ctx.titles[f] ?? f;
|
|
11
|
-
const limit =
|
|
11
|
+
const { short: limit, long: longLimit } = takeoverLimits(s.threshold);
|
|
12
12
|
const familyOrder = [
|
|
13
13
|
...new Set([...s.familyOrder.filter((f) => ctx.families.includes(f)), ...ctx.families]),
|
|
14
14
|
];
|
|
@@ -25,13 +25,13 @@ export function handoffRules(s, ctx) {
|
|
|
25
25
|
id: 'windows',
|
|
26
26
|
params: {},
|
|
27
27
|
settings: [],
|
|
28
|
-
text: 'Every quota window counts (5 hours, weekly, a billing period)
|
|
28
|
+
text: 'Every quota window counts (5 hours, weekly, a billing period). The session hands over when its fullest window reaches the level above. No host has a daily quota.',
|
|
29
29
|
},
|
|
30
30
|
{
|
|
31
31
|
id: 'takeover',
|
|
32
|
-
params: { limit },
|
|
32
|
+
params: { limit, longLimit },
|
|
33
33
|
settings: ['threshold'],
|
|
34
|
-
text: `An account
|
|
34
|
+
text: `An account does not take over once a 5-hour window is ${limit}% used (${s.threshold}% − 10) or a weekly or longer window is ${longLimit}% used (${s.threshold}%), so the next one starts with room to work: one 5-hour window costs only about a fifth of a week. Among those that can, the one furthest from these limits goes first.`,
|
|
35
35
|
},
|
|
36
36
|
{
|
|
37
37
|
id: 'wait',
|
|
@@ -135,6 +135,18 @@ export function hookEntryWorks(env) {
|
|
|
135
135
|
execFile('sideby', ['handoff-hook', '--check'], { env: env, timeout: 5000 }, (err, out) => resolve(!err && String(out).trim() === 'ok'));
|
|
136
136
|
});
|
|
137
137
|
}
|
|
138
|
+
/** How long a Host stopped at its limit waits when its window's reset is unknown: the shortest Quota window. */
|
|
139
|
+
const LIMIT_WAIT_FALLBACK_MS = 5 * 60 * 60_000;
|
|
140
|
+
/**
|
|
141
|
+
* When a Host stopped at its limit continues by itself: the reset of the window that stopped it, or, without a future
|
|
142
|
+
* one, `LIMIT_WAIT_FALLBACK_MS` from now (the hook also clears the file at the next finished turn).
|
|
143
|
+
*/
|
|
144
|
+
function limitUntil(ready, now = new Date()) {
|
|
145
|
+
const at = ready.window ? Date.parse(ready.window.resetsAt) : Number.NaN;
|
|
146
|
+
return Number.isFinite(at) && at > now.getTime()
|
|
147
|
+
? new Date(at).toISOString()
|
|
148
|
+
: new Date(now.getTime() + LIMIT_WAIT_FALLBACK_MS).toISOString();
|
|
149
|
+
}
|
|
138
150
|
/** The decision, or `none` with the reason when sideby cannot tell (an account removed mid-chain, say). */
|
|
139
151
|
async function decide(rt, account, used, to, manual = false) {
|
|
140
152
|
try {
|
|
@@ -284,11 +296,16 @@ export async function runWithHandoff(rt, ref, userArgs, io, opts = {}) {
|
|
|
284
296
|
break;
|
|
285
297
|
}
|
|
286
298
|
await rm(join(dir, FILES.ready), { force: true });
|
|
287
|
-
|
|
299
|
+
// A session kept at its limit (or kept to wait) ends no turn until it continues, so a request would never be
|
|
300
|
+
// picked up: `waiting.json` lets `handoff ready` hand over at once (ADR-0018). A `threshold` none is still in
|
|
301
|
+
// its turns and gets none.
|
|
302
|
+
const until = d.kind === 'wait' ? d.until : got.ready.trigger === 'limit' ? limitUntil(got.ready) : undefined;
|
|
303
|
+
if (until)
|
|
288
304
|
await writeJsonFile(join(dir, FILES.waiting), {
|
|
289
305
|
runId,
|
|
290
|
-
until
|
|
306
|
+
until,
|
|
291
307
|
at: new Date().toISOString(),
|
|
308
|
+
trigger: got.ready.trigger,
|
|
292
309
|
});
|
|
293
310
|
kept = d;
|
|
294
311
|
}
|
|
@@ -49,12 +49,19 @@ export type UsedAccount = string | {
|
|
|
49
49
|
resetsAt: string;
|
|
50
50
|
};
|
|
51
51
|
};
|
|
52
|
+
/** What an automatic handoff does with the Accounts this chain already used (spec §3.17 "选号" 3, ADR-0060). */
|
|
53
|
+
export interface UsedRule {
|
|
54
|
+
/** Left out: the origin, a bare ref, and an Account whose latest hop handed over for a window that has not reset. */
|
|
55
|
+
out: Set<string>;
|
|
56
|
+
/** Back only when its own Quota data shows it ready: its latest hop recorded no window (a manual handoff, an older chain). */
|
|
57
|
+
ifReady: Set<string>;
|
|
58
|
+
}
|
|
52
59
|
/**
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
60
|
+
* Sorts the Accounts this chain used by their latest hop: one that handed over for a Quota window that has reset by
|
|
61
|
+
* `now` may take over again like any other, one with no recorded window only when it is `ready`, the rest stay out.
|
|
62
|
+
* A bare ref stays out, and so does the origin.
|
|
56
63
|
*/
|
|
57
|
-
export declare function stillUsed(origin: string, used: readonly UsedAccount[], now: Date):
|
|
64
|
+
export declare function stillUsed(origin: string, used: readonly UsedAccount[], now: Date): UsedRule;
|
|
58
65
|
/** Organizations when both have one, else email domains when both have one, else the same. */
|
|
59
66
|
export declare function sameOrganization(a?: AccountIdentity, b?: AccountIdentity): boolean;
|
|
60
67
|
export declare function selectNext(input: {
|
|
@@ -63,7 +70,10 @@ export declare function selectNext(input: {
|
|
|
63
70
|
settings: HandoffSettings;
|
|
64
71
|
/** Short command → Account ref, so lists may name either. */
|
|
65
72
|
aliases: Readonly<Record<string, string>>;
|
|
66
|
-
/**
|
|
73
|
+
/**
|
|
74
|
+
* Accounts this chain already used, the origin included; one whose window has reset since may come back, and one
|
|
75
|
+
* whose hop recorded no window when its Quota shows it ready.
|
|
76
|
+
*/
|
|
67
77
|
used: readonly UsedAccount[];
|
|
68
78
|
now: Date;
|
|
69
79
|
/** Decide now what happens when the origin's fullest window reaches the threshold (see `waitUntil`). */
|
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
import { planHandoff } from './handoff.js';
|
|
2
|
-
import {
|
|
2
|
+
import { takeoverLimits, windowReset } from './quota-levels.js';
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Sorts the Accounts this chain used by their latest hop: one that handed over for a Quota window that has reset by
|
|
5
|
+
* `now` may take over again like any other, one with no recorded window only when it is `ready`, the rest stay out.
|
|
6
|
+
* A bare ref stays out, and so does the origin.
|
|
7
7
|
*/
|
|
8
8
|
export function stillUsed(origin, used, now) {
|
|
9
|
-
const
|
|
9
|
+
const latest = new Map();
|
|
10
10
|
for (const u of used) {
|
|
11
11
|
if (typeof u === 'string')
|
|
12
|
-
|
|
12
|
+
latest.set(u, 'out');
|
|
13
|
+
else if (u.window === undefined)
|
|
14
|
+
latest.set(u.ref, 'ifReady');
|
|
13
15
|
else
|
|
14
|
-
|
|
16
|
+
latest.set(u.ref, windowReset(u.window.resetsAt, now) ? 'back' : 'out');
|
|
15
17
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
return out;
|
|
18
|
+
latest.set(origin, 'out');
|
|
19
|
+
const refs = (k) => new Set([...latest].flatMap(([ref, v]) => (v === k ? [ref] : [])));
|
|
20
|
+
return { out: refs('out'), ifReady: refs('ifReady') };
|
|
19
21
|
}
|
|
20
22
|
const domain = (email) => email.slice(email.lastIndexOf('@') + 1).toLowerCase();
|
|
21
23
|
/** Organizations when both have one, else email domains when both have one, else the same. */
|
|
@@ -51,11 +53,11 @@ function waitUntil(origin, s, now, ahead) {
|
|
|
51
53
|
return new Date(last).toISOString();
|
|
52
54
|
}
|
|
53
55
|
/**
|
|
54
|
-
* One candidate's §3.13 state; API accounts are pickable here, as the last resort. An Account at or
|
|
55
|
-
* Takeover Limit counts as full, so the next Account starts with room to work (spec §3.17 "接力规则").
|
|
56
|
+
* One candidate's §3.13 state; API accounts are pickable here, as the last resort. An Account with a window at or
|
|
57
|
+
* above its Takeover Limit counts as full, so the next Account starts with room to work (spec §3.17 "接力规则").
|
|
56
58
|
*/
|
|
57
59
|
function stateOf(c, s, now) {
|
|
58
|
-
return planHandoff(c.family, [c], now, { includeApi: true,
|
|
60
|
+
return planHandoff(c.family, [c], now, { includeApi: true, limits: takeoverLimits(s.threshold) })
|
|
59
61
|
.accounts[0];
|
|
60
62
|
}
|
|
61
63
|
/** Families in the order `otherFamilies` tries them: `familyOrder` first, then the rest as the candidates come. */
|
|
@@ -79,8 +81,8 @@ export function selectNext(input) {
|
|
|
79
81
|
const allowed = new Set(s.crossOrganization.map(refOf));
|
|
80
82
|
const used = stillUsed(origin.ref, input.used, now);
|
|
81
83
|
const family = s.families[origin.family];
|
|
82
|
-
const
|
|
83
|
-
const rank = (fam) => planHandoff(fam, input.candidates.filter((c) => c.family === fam), now, { includeApi: true,
|
|
84
|
+
const limits = takeoverLimits(s.threshold);
|
|
85
|
+
const rank = (fam) => planHandoff(fam, input.candidates.filter((c) => c.family === fam), now, { includeApi: true, limits }).accounts.map((e) => byRef.get(e.ref));
|
|
84
86
|
let list;
|
|
85
87
|
if (family?.policy === 'order') {
|
|
86
88
|
list = family.order.map(refOf).flatMap((r) => (byRef.has(r) ? [byRef.get(r)] : []));
|
|
@@ -101,14 +103,24 @@ export function selectNext(input) {
|
|
|
101
103
|
const skipped = list
|
|
102
104
|
.filter((c) => sameLogin.has(c.ref))
|
|
103
105
|
.map((c) => ({ ref: c.ref, reason: `same login as ${origin.ref}, so it shares the same quota` }));
|
|
104
|
-
|
|
106
|
+
// A used Account with no recorded window comes back only on Quota data that shows it ready: not on a guess
|
|
107
|
+
// (`unknown`, an API account). It is still counted while full, so its reset can be named.
|
|
108
|
+
const unsure = (c) => used.ifReady.has(c.ref) && !['ready', 'full', 'logged-out'].includes(stateOf(c, s, now).state);
|
|
105
109
|
// Could take over if the settings allowed it: another login, its Host installed and able to receive.
|
|
106
|
-
const couldTake = (c) => !sameLogin.has(c.ref) && !used.has(c.ref) && c.installed && c.receive;
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
(allowed.has(c.ref) || sameOrganization(origin.identity, c.identity))
|
|
110
|
+
const couldTake = (c) => !sameLogin.has(c.ref) && !used.out.has(c.ref) && !unsure(c) && c.installed && c.receive;
|
|
111
|
+
const allows = (c) =>
|
|
112
|
+
// An API account pays per request, so it gets past no vendor's limit: sameFamily does not apply to it, but it
|
|
113
|
+
// takes over only when the user allowed paying for it (`api`, ADR-0046).
|
|
114
|
+
(c.kind === 'api' ? s.api : s.sameFamily || c.family !== origin.family) &&
|
|
115
|
+
(allowed.has(c.ref) || sameOrganization(origin.identity, c.identity));
|
|
116
|
+
const eligible = list.filter((c) => couldTake(c) && allows(c));
|
|
117
|
+
for (const c of list)
|
|
118
|
+
if (unsure(c) && !sameLogin.has(c.ref) && c.installed && c.receive && allows(c))
|
|
119
|
+
skipped.push({
|
|
120
|
+
ref: c.ref,
|
|
121
|
+
reason: 'this chain already used it and its quota is not known, so it is not picked again',
|
|
122
|
+
});
|
|
123
|
+
const withSkipped = skipped.length ? { skipped } : {};
|
|
112
124
|
const states = eligible.map((c) => ({ c, e: stateOf(c, s, now) }));
|
|
113
125
|
const first = states.find(({ e }) => e.state === 'ready' || e.state === 'unknown') ??
|
|
114
126
|
states.find(({ e }) => e.state === 'api');
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { Account, LoginState, QuotaResult } from '../types.ts';
|
|
2
|
+
import { type TakeoverLimits } from './quota-levels.ts';
|
|
2
3
|
export type HandoffState = 'ready' | 'unknown' | 'api' | 'full' | 'logged-out';
|
|
3
4
|
export interface HandoffCandidate {
|
|
4
5
|
ref: string;
|
|
@@ -16,7 +17,22 @@ export interface HandoffEntry {
|
|
|
16
17
|
state: HandoffState;
|
|
17
18
|
/** Quota Pressure in percent; null when the Account has no Quota. */
|
|
18
19
|
pressure: number | null;
|
|
19
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* Limit Use: the percent of its own Takeover Limit that the closest window has used (a weekly window at 87% under
|
|
22
|
+
* 95 is 91.6), null when the Account has no Quota. Ready Accounts rank by it, lowest first.
|
|
23
|
+
*/
|
|
24
|
+
limitUse: number | null;
|
|
25
|
+
/** That window: its label (`5h`, `7d`), used percent and own Takeover Limit; absent when no window is open. */
|
|
26
|
+
window?: {
|
|
27
|
+
label: string;
|
|
28
|
+
usedPercent: number;
|
|
29
|
+
limit: number;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* For `full`: when the last window at or over its own Takeover Limit resets, so the Account is usable again. In a
|
|
33
|
+
* plan for a start the user asks for (`lastResort`), an Account with a window used up is usable again once those
|
|
34
|
+
* windows reset, and one with none used up is under the Takeover Limits again at this time.
|
|
35
|
+
*/
|
|
20
36
|
resetsAt?: string;
|
|
21
37
|
/** When the Quota was recorded; it is only as fresh as the Account's last session. */
|
|
22
38
|
observedAt?: string;
|
|
@@ -41,6 +57,16 @@ export interface HandoffPlan {
|
|
|
41
57
|
* out), so `sideby next` does not start it and the Panel shows no recommendation.
|
|
42
58
|
*/
|
|
43
59
|
hasQuota: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* Set when the pick is a Last Resort (spec §3.13): no Subscription Account was below its Takeover Limits (`limit` for
|
|
62
|
+
* Short Windows, `longLimit` for Long ones), so the pick is the signed-in one with the lowest Limit Use that has no
|
|
63
|
+
* window used up. Only starts a person asks for (`sideby next`, Pick Commands, the Panel) allow one; Auto Handoff
|
|
64
|
+
* never does.
|
|
65
|
+
*/
|
|
66
|
+
tight?: {
|
|
67
|
+
limit: number;
|
|
68
|
+
longLimit: number;
|
|
69
|
+
};
|
|
44
70
|
/** Set when nothing can be picked and some Accounts are full: the first to come back. */
|
|
45
71
|
earliestReset?: {
|
|
46
72
|
ref: string;
|
|
@@ -48,9 +74,16 @@ export interface HandoffPlan {
|
|
|
48
74
|
};
|
|
49
75
|
}
|
|
50
76
|
export declare function planHandoff(family: string, candidates: readonly HandoffCandidate[], now: Date,
|
|
51
|
-
/**
|
|
77
|
+
/**
|
|
78
|
+
* `limits`: the Takeover Limits, at or above which a window makes an Account full (default 85 for Short Windows and
|
|
79
|
+
* 95 for Long ones, the limits at threshold 95).
|
|
80
|
+
* `lastResort`: a start the user asks for; when no Subscription Account is `ready` or `unknown`, pick the full one
|
|
81
|
+
* with the lowest Limit Use and no window used up, before any API Account (`tight` says so). Auto Handoff leaves it
|
|
82
|
+
* off.
|
|
83
|
+
*/
|
|
52
84
|
opts?: {
|
|
53
85
|
includeApi?: boolean;
|
|
54
86
|
from?: string;
|
|
55
|
-
|
|
87
|
+
limits?: TakeoverLimits;
|
|
88
|
+
lastResort?: boolean;
|
|
56
89
|
}): HandoffPlan;
|
package/dist/src/core/handoff.js
CHANGED
|
@@ -1,13 +1,40 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DEFAULT_TAKEOVER_LIMITS, limitOf, limitUse, openWindows, quotaPressure, windowReset, } from './quota-levels.js';
|
|
2
2
|
const ORDER = { ready: 0, unknown: 1, api: 2, full: 3, 'logged-out': 4 };
|
|
3
3
|
/** A full Account without a readable reset time sorts after every one that has one. */
|
|
4
4
|
const resetTime = (e) => (e.resetsAt ? Date.parse(e.resetsAt) : Number.POSITIVE_INFINITY);
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The latest reset among the counted windows at or above their `level` that have not reset yet: when the Account is
|
|
7
|
+
* under it again.
|
|
8
|
+
*/
|
|
9
|
+
function lastReset(quota, now, level) {
|
|
10
|
+
let last;
|
|
11
|
+
if (quota.status === 'ok')
|
|
12
|
+
for (const w of quota.windows) {
|
|
13
|
+
const t = Date.parse(w.resetsAt);
|
|
14
|
+
if (!w.name && w.usedPercent >= level(w) && !windowReset(w.resetsAt, now) && Number.isFinite(t))
|
|
15
|
+
last = Math.max(last ?? t, t);
|
|
16
|
+
}
|
|
17
|
+
return last;
|
|
18
|
+
}
|
|
19
|
+
/** A full Subscription Account a Last Resort may start: no window used up (pressure under 100). */
|
|
20
|
+
const lastResortOk = (e) => e.state === 'full' && !e.sameLoginAs && e.pressure !== null && e.pressure < 100;
|
|
21
|
+
function entry(c, now, limits, lastResort) {
|
|
6
22
|
const pressure = quotaPressure(c.quota, now);
|
|
23
|
+
const use = limitUse(c.quota, now, limits);
|
|
7
24
|
const base = {
|
|
8
25
|
ref: c.ref,
|
|
9
26
|
kind: c.kind,
|
|
10
27
|
pressure,
|
|
28
|
+
limitUse: use?.percent ?? null,
|
|
29
|
+
...(use?.window
|
|
30
|
+
? {
|
|
31
|
+
window: {
|
|
32
|
+
label: use.window.label,
|
|
33
|
+
usedPercent: Math.round(use.window.usedPercent),
|
|
34
|
+
limit: limitOf(use.window, limits),
|
|
35
|
+
},
|
|
36
|
+
}
|
|
37
|
+
: {}),
|
|
11
38
|
...(c.quota.status === 'ok' ? { observedAt: c.quota.observedAt } : {}),
|
|
12
39
|
...(c.quota.status === 'ok' && c.quota.from ? { quotaFrom: c.quota.from } : {}),
|
|
13
40
|
};
|
|
@@ -17,22 +44,25 @@ function entry(c, now, fullAt) {
|
|
|
17
44
|
return { ...base, state: 'logged-out' };
|
|
18
45
|
if (pressure === null)
|
|
19
46
|
return { ...base, state: 'unknown' };
|
|
20
|
-
|
|
47
|
+
// Full once any window reaches its own Takeover Limit: 85 for a Short Window, 95 for a Long one by default.
|
|
48
|
+
const level = (w) => limitOf(w, limits);
|
|
49
|
+
if (!openWindows(c.quota, now)?.some((w) => w.usedPercent >= level(w)))
|
|
21
50
|
return { ...base, state: 'ready' };
|
|
22
|
-
// Usable again only when every full window has reset: the latest of their reset times.
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
for (const w of c.quota.windows) {
|
|
26
|
-
const t = Date.parse(w.resetsAt);
|
|
27
|
-
if (w.usedPercent >= fullAt && !windowReset(w.resetsAt, now) && Number.isFinite(t))
|
|
28
|
-
last = Math.max(last ?? t, t);
|
|
29
|
-
}
|
|
51
|
+
// Usable again only when every full window has reset: the latest of their reset times. For a Last Resort a window
|
|
52
|
+
// used up is what stops an Account, so its reset is when it can start again.
|
|
53
|
+
const last = lastReset(c.quota, now, lastResort && pressure >= 100 ? () => 100 : level);
|
|
30
54
|
return { ...base, state: 'full', ...(last === undefined ? {} : { resetsAt: new Date(last).toISOString() }) };
|
|
31
55
|
}
|
|
32
56
|
export function planHandoff(family, candidates, now,
|
|
33
|
-
/**
|
|
57
|
+
/**
|
|
58
|
+
* `limits`: the Takeover Limits, at or above which a window makes an Account full (default 85 for Short Windows and
|
|
59
|
+
* 95 for Long ones, the limits at threshold 95).
|
|
60
|
+
* `lastResort`: a start the user asks for; when no Subscription Account is `ready` or `unknown`, pick the full one
|
|
61
|
+
* with the lowest Limit Use and no window used up, before any API Account (`tight` says so). Auto Handoff leaves it
|
|
62
|
+
* off.
|
|
63
|
+
*/
|
|
34
64
|
opts = {}) {
|
|
35
|
-
const
|
|
65
|
+
const limits = opts.limits ?? DEFAULT_TAKEOVER_LIMITS;
|
|
36
66
|
const current = opts.from ? candidates.find((c) => c.ref === opts.from) : undefined;
|
|
37
67
|
const byRef = new Map(candidates.map((c) => [c.ref, c]));
|
|
38
68
|
const tie = (a, b) => {
|
|
@@ -40,23 +70,32 @@ opts = {}) {
|
|
|
40
70
|
const y = byRef.get(b.ref);
|
|
41
71
|
return Number(y.isMain) - Number(x.isMain) || x.name.localeCompare(y.name, undefined, { numeric: true });
|
|
42
72
|
};
|
|
73
|
+
// With `lastResort`, a full Account with no window used up ranks after `unknown` and before `api`, lowest Limit Use
|
|
74
|
+
// first.
|
|
75
|
+
const rank = (e) => (opts.lastResort && lastResortOk(e) ? 1.5 : ORDER[e.state]);
|
|
43
76
|
const accounts = candidates
|
|
44
77
|
.map((c) => {
|
|
45
|
-
const e = entry(c, now,
|
|
78
|
+
const e = entry(c, now, limits, Boolean(opts.lastResort));
|
|
46
79
|
return current?.sameLogin?.includes(c.ref) ? { ...e, sameLoginAs: current.ref } : e;
|
|
47
80
|
})
|
|
48
81
|
.sort((a, b) =>
|
|
49
82
|
// A Same Login partner of the current Account can never be picked, so it goes with the others that cannot.
|
|
50
83
|
Number(Boolean(a.sameLoginAs)) - Number(Boolean(b.sameLoginAs)) ||
|
|
51
|
-
|
|
52
|
-
(a.state === 'ready' ? a.
|
|
84
|
+
rank(a) - rank(b) ||
|
|
85
|
+
(a.state === 'ready' || rank(a) === 1.5 ? a.limitUse - b.limitUse : 0) ||
|
|
53
86
|
(a.state === 'full' ? resetTime(a) - resetTime(b) || 0 : 0) ||
|
|
54
87
|
tie(a, b));
|
|
55
88
|
const usable = (e) => !e.sameLoginAs &&
|
|
56
|
-
(e.state === 'ready' ||
|
|
57
|
-
|
|
89
|
+
(e.state === 'ready' ||
|
|
90
|
+
e.state === 'unknown' ||
|
|
91
|
+
(opts.lastResort && lastResortOk(e)) ||
|
|
92
|
+
(e.state === 'api' && Boolean(opts.includeApi)));
|
|
93
|
+
const picked = accounts.find(usable);
|
|
94
|
+
const pick = picked?.ref ?? null;
|
|
58
95
|
const hasQuota = accounts.some((e) => e.state === 'ready' || e.state === 'full');
|
|
59
96
|
const plan = { family, ...(current ? { from: current.ref } : {}), pick, accounts, hasQuota };
|
|
97
|
+
if (picked?.state === 'full')
|
|
98
|
+
plan.tight = { limit: limits.short, longLimit: limits.long };
|
|
60
99
|
if (pick === null) {
|
|
61
100
|
const first = accounts.find((e) => e.state === 'full' && e.resetsAt);
|
|
62
101
|
if (first?.resetsAt)
|
|
@@ -7,10 +7,25 @@ export declare const DEFAULT_PICK_NAMES: Readonly<Record<string, string>>;
|
|
|
7
7
|
*/
|
|
8
8
|
export declare function suggestPickName(family: string, bin: string, familyAliases: readonly string[]): string | null;
|
|
9
9
|
/** What a Pick Command starts this time, or why it starts nothing. */
|
|
10
|
-
export type PickChoice =
|
|
10
|
+
export type PickChoice =
|
|
11
|
+
/** `pressure`: the used percent of `window`, the window closest to its own Takeover Limit. */
|
|
12
|
+
{
|
|
11
13
|
ref: string;
|
|
12
14
|
reason: 'most-room';
|
|
13
15
|
pressure: number;
|
|
16
|
+
window: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A Last Resort (spec §3.13): every Account is past its Takeover Limits (`limit` for Short Windows, `longLimit` for
|
|
20
|
+
* Long ones); this one is the furthest from them.
|
|
21
|
+
*/
|
|
22
|
+
| {
|
|
23
|
+
ref: string;
|
|
24
|
+
reason: 'tight';
|
|
25
|
+
pressure: number;
|
|
26
|
+
window: string;
|
|
27
|
+
limit: number;
|
|
28
|
+
longLimit: number;
|
|
14
29
|
}
|
|
15
30
|
/** No Account of the Family has Quota data: the Main Account, or the Handoff pick when the main cannot start. */
|
|
16
31
|
| {
|
|
@@ -32,11 +47,11 @@ export type PickChoice = {
|
|
|
32
47
|
};
|
|
33
48
|
/**
|
|
34
49
|
* Turns a Handoff plan into the Account to start (ADR-0035). With some Quota data: the pick. Without any: the Main
|
|
35
|
-
* Account (`mainRef`, a Subscription Account) when it can start, else the pick
|
|
36
|
-
* or API): nothing starts.
|
|
50
|
+
* Account (`mainRef`, a Subscription Account) when it can start, else the pick, which past the Takeover Limits is the
|
|
51
|
+
* Account with the lowest Limit Use and no window used up (`tight`). Nothing usable (every window used up, signed out or API): nothing starts.
|
|
37
52
|
*/
|
|
38
53
|
export declare function choosePick(plan: HandoffPlan, mainRef: string | null): PickChoice;
|
|
39
54
|
/** The line a Pick Command prints before it starts the Host. */
|
|
40
55
|
export declare function pickLine(name: string, choice: Exclude<PickChoice, {
|
|
41
56
|
ref: null;
|
|
42
|
-
}
|
|
57
|
+
}>, family: string): string;
|
|
@@ -12,8 +12,8 @@ export function suggestPickName(family, bin, familyAliases) {
|
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Turns a Handoff plan into the Account to start (ADR-0035). With some Quota data: the pick. Without any: the Main
|
|
15
|
-
* Account (`mainRef`, a Subscription Account) when it can start, else the pick
|
|
16
|
-
* or API): nothing starts.
|
|
15
|
+
* Account (`mainRef`, a Subscription Account) when it can start, else the pick, which past the Takeover Limits is the
|
|
16
|
+
* Account with the lowest Limit Use and no window used up (`tight`). Nothing usable (every window used up, signed out or API): nothing starts.
|
|
17
17
|
*/
|
|
18
18
|
export function choosePick(plan, mainRef) {
|
|
19
19
|
const startable = (ref) => plan.accounts.some((e) => e.ref === ref && !e.sameLoginAs && (e.state === 'ready' || e.state === 'unknown'));
|
|
@@ -23,8 +23,19 @@ export function choosePick(plan, mainRef) {
|
|
|
23
23
|
const e = plan.accounts.find((x) => x.ref === plan.pick);
|
|
24
24
|
if (!plan.hasQuota)
|
|
25
25
|
return { ref: plan.pick, reason: 'no-quota-data' };
|
|
26
|
+
// The window that decided the pick: the one closest to its own limit (a weekly 87% before a 5-hour 43%).
|
|
27
|
+
const w = e.window ?? { label: '', usedPercent: Math.round(e.pressure ?? 0) };
|
|
28
|
+
if (plan.tight && e.pressure !== null)
|
|
29
|
+
return {
|
|
30
|
+
ref: plan.pick,
|
|
31
|
+
reason: 'tight',
|
|
32
|
+
pressure: w.usedPercent,
|
|
33
|
+
window: w.label,
|
|
34
|
+
limit: plan.tight.limit,
|
|
35
|
+
longLimit: plan.tight.longLimit,
|
|
36
|
+
};
|
|
26
37
|
return e.state === 'ready' && e.pressure !== null
|
|
27
|
-
? { ref: plan.pick, reason: 'most-room', pressure:
|
|
38
|
+
? { ref: plan.pick, reason: 'most-room', pressure: w.usedPercent, window: w.label }
|
|
28
39
|
: { ref: plan.pick, reason: 'others-full' };
|
|
29
40
|
}
|
|
30
41
|
return {
|
|
@@ -35,9 +46,12 @@ export function choosePick(plan, mainRef) {
|
|
|
35
46
|
};
|
|
36
47
|
}
|
|
37
48
|
/** The line a Pick Command prints before it starts the Host. */
|
|
38
|
-
export function pickLine(name, choice) {
|
|
49
|
+
export function pickLine(name, choice, family) {
|
|
50
|
+
const used = (c) => `${c.window ? `${c.window} ` : ''}${c.pressure}% used`;
|
|
51
|
+
if (choice.reason === 'tight')
|
|
52
|
+
return `${name} → ${choice.ref} (every ${family} account is past its limit, ${choice.limit}% of a 5-hour window or ${choice.longLimit}% of a weekly one; this one has the most left, ${used(choice)})`;
|
|
39
53
|
if (choice.reason === 'most-room')
|
|
40
|
-
return `${name} → ${choice.ref} (${choice
|
|
54
|
+
return `${name} → ${choice.ref} (${used(choice)}, the most room)`;
|
|
41
55
|
if (choice.reason === 'others-full')
|
|
42
56
|
return `${name} → ${choice.ref} (no quota data for it yet; the accounts with data are full)`;
|
|
43
57
|
return `${name} → ${choice.ref} (no quota data yet; \`sideby quota\` shows why)`;
|
|
@@ -1,16 +1,51 @@
|
|
|
1
1
|
import type { QuotaResult, QuotaWindow } from '../types.ts';
|
|
2
2
|
/**
|
|
3
|
-
* How full a Quota window must be before the CLI and the
|
|
4
|
-
*
|
|
3
|
+
* How full a Quota window must be before the CLI, the Panel and the menu bar warn about it (yellow) and show it as
|
|
4
|
+
* nearly used up (red). A Short Window (5 hours) uses the first pair, a Long Window (weekly, a billing period) the
|
|
5
|
+
* second: one full 5-hour window costs only about a fifth of a weekly one, so 87% of a week still leaves several
|
|
6
|
+
* 5-hour windows of work (spec §3.26, ADR-0062). The Panel interpolates these numbers into its inline script.
|
|
5
7
|
*/
|
|
6
8
|
export declare const QUOTA_FAIL_PERCENT = 85;
|
|
7
9
|
export declare const QUOTA_WARN_PERCENT = 60;
|
|
10
|
+
export declare const QUOTA_LONG_FAIL_PERCENT = 95;
|
|
11
|
+
export declare const QUOTA_LONG_WARN_PERCENT = 85;
|
|
12
|
+
/** A window longer than this many minutes (one day) is a Long Window. No Host has a daily Quota. */
|
|
13
|
+
export declare const LONG_WINDOW_MINUTES = 1440;
|
|
8
14
|
/**
|
|
9
15
|
* Takeover Limit (spec §3.17 "接力规则"): an Account more used than this does not take over a Handoff, so the next
|
|
10
|
-
* Account starts with room to work.
|
|
16
|
+
* Account starts with room to work. For a Short Window it sits this many points below the Handoff Threshold and moves
|
|
17
|
+
* with it; a Long Window's limit is the Handoff Threshold itself (`takeoverLimits`).
|
|
11
18
|
*/
|
|
12
19
|
export declare const TAKEOVER_GAP = 10;
|
|
13
20
|
export declare function takeoverLimit(threshold: number): number;
|
|
21
|
+
/** The Takeover Limit of each kind of window: at or above it, that window makes an Account full. */
|
|
22
|
+
export interface TakeoverLimits {
|
|
23
|
+
/** Short Windows (5 hours): the Handoff Threshold − 10, 85 by default. */
|
|
24
|
+
short: number;
|
|
25
|
+
/** Long Windows (weekly, a billing period): the Handoff Threshold, 95 by default. */
|
|
26
|
+
long: number;
|
|
27
|
+
}
|
|
28
|
+
export declare function takeoverLimits(threshold: number): TakeoverLimits;
|
|
29
|
+
/** The limits at the default Handoff Threshold (95): 85 for Short Windows, 95 for Long ones. */
|
|
30
|
+
export declare const DEFAULT_TAKEOVER_LIMITS: TakeoverLimits;
|
|
31
|
+
/**
|
|
32
|
+
* A window's length in minutes: `windowMinutes`, or read from a label such as `5h`, `7d` or `90m` when that is
|
|
33
|
+
* missing; 0 when neither tells. The Panel keeps its own copy in `longWindow` (page-logic.ts); a test keeps them equal.
|
|
34
|
+
*/
|
|
35
|
+
export declare function windowLength(w: {
|
|
36
|
+
label?: string;
|
|
37
|
+
windowMinutes?: number;
|
|
38
|
+
}): number;
|
|
39
|
+
/** A Long Window: longer than one day (weekly, a billing period). Every other window is a Short Window. */
|
|
40
|
+
export declare function isLongWindow(w: {
|
|
41
|
+
label?: string;
|
|
42
|
+
windowMinutes?: number;
|
|
43
|
+
}): boolean;
|
|
44
|
+
/** The window's own Takeover Limit. */
|
|
45
|
+
export declare function limitOf(w: {
|
|
46
|
+
label?: string;
|
|
47
|
+
windowMinutes?: number;
|
|
48
|
+
}, limits: TakeoverLimits): number;
|
|
14
49
|
/** Days of Usage every Family reports. */
|
|
15
50
|
export declare const USAGE_DAYS = 7;
|
|
16
51
|
export type QuotaLevel = 'ok' | 'warn' | 'fail';
|
|
@@ -24,7 +59,35 @@ export declare function quotaIsStale(quota: {
|
|
|
24
59
|
observedAt: string;
|
|
25
60
|
fetchError?: string;
|
|
26
61
|
}, now: number, lastUseAt?: string | null): boolean;
|
|
27
|
-
|
|
62
|
+
/** The warning level of a used percent; `long` for a Long Window, which warns later (spec §3.26). */
|
|
63
|
+
export declare function quotaLevel(usedPercent: number, long?: boolean): QuotaLevel;
|
|
64
|
+
/** One window's warning level by its own kind: a Short Window warns at 60/85, a Long Window at 85/95. */
|
|
65
|
+
export declare function windowLevel(w: {
|
|
66
|
+
label?: string;
|
|
67
|
+
windowMinutes?: number;
|
|
68
|
+
usedPercent: number;
|
|
69
|
+
}): QuotaLevel;
|
|
70
|
+
/** The more serious of two levels. */
|
|
71
|
+
export declare function worseLevel(a: QuotaLevel, b: QuotaLevel): QuotaLevel;
|
|
72
|
+
/**
|
|
73
|
+
* The window that runs out first relative to its own kind: the highest used percent over that window's red level
|
|
74
|
+
* (85 for a Short Window, 95 for a Long one), the first on a tie. What the menu bar's reset column and title speak
|
|
75
|
+
* about. Undefined without windows.
|
|
76
|
+
*/
|
|
77
|
+
export declare function bindingWindow<W extends {
|
|
78
|
+
label?: string;
|
|
79
|
+
windowMinutes?: number;
|
|
80
|
+
usedPercent: number;
|
|
81
|
+
}>(windows: readonly W[]): W | undefined;
|
|
82
|
+
/**
|
|
83
|
+
* Limit Use: how close an Account is to being full, as the percent of its own Takeover Limit that its closest window
|
|
84
|
+
* has used (a weekly window at 87% under a limit of 95 is 91.6; a 5-hour window at 43% under 85 is 50.6), with that
|
|
85
|
+
* window. Null when the Account has no Quota. A Handoff ranks ready Accounts by it, lowest first (spec §3.13).
|
|
86
|
+
*/
|
|
87
|
+
export declare function limitUse(quota: QuotaResult | undefined, now: Date, limits: TakeoverLimits): {
|
|
88
|
+
percent: number;
|
|
89
|
+
window: QuotaWindow | null;
|
|
90
|
+
} | null;
|
|
28
91
|
/** True once a window's reset time has passed; an unreadable time never counts as reset. */
|
|
29
92
|
export declare function windowReset(resetsAt: string, now: Date): boolean;
|
|
30
93
|
/**
|