sideby 0.31.3 → 0.32.1
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.js +27 -11
- package/dist/src/cli/format.js +3 -2
- package/dist/src/cli/json-schemas.js +25 -5
- package/dist/src/core/ask.js +6 -1
- package/dist/src/core/handoff-rules.js +5 -5
- package/dist/src/core/handoff-select.js +6 -6
- 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/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/menubar/script.js +62 -12
- 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 +1 -0
- package/dist/src/panel/i18n.js +8 -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 +12 -9
- package/dist/src/panel/page.js +3 -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/menubar-snapshot.json +3 -3
- package/schemas/next.json +57 -4
- package/skills/sideby/SKILL.md +2 -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 +12 -8
- package/skills/sideby/references/panel.md +1 -1
- package/skills/sideby/references/quota.md +9 -3
package/README.md
CHANGED
|
@@ -25,7 +25,7 @@ npm i -g sideby
|
|
|
25
25
|
sideby new claude work # create ~/.claude-work, sharing ~/.claude's skills and hooks
|
|
26
26
|
sideby login claude:work # sign in once
|
|
27
27
|
sideby run work # Claude Code on the work account
|
|
28
|
-
sideby quota setup claude --yes # Claude quota
|
|
28
|
+
sideby quota setup claude --yes # optional: live Claude quota while a session runs; undo: sideby quota teardown claude
|
|
29
29
|
sideby ui --background # every account's quota at http://127.0.0.1:17420, still there after you close the terminal
|
|
30
30
|
```
|
|
31
31
|
|
|
@@ -72,7 +72,7 @@ On macOS, the same quota sits in the menu bar (`sideby menubar on`): click or pr
|
|
|
72
72
|
|---|---|---|---|---|
|
|
73
73
|
| `CLAUDE_CONFIG_DIR` and an alias | Yes. Each terminal exports the variable. | Each directory is its own copy. They drift unless you symlink by hand. | That session's status line | One alias per account |
|
|
74
74
|
| [cc-switch](https://github.com/farion1231/cc-switch) | Enabling a provider writes the host's live config, so open sessions share that switch. | Plugin settings are copied across providers as a separate step. | The active provider, from its quota endpoint | Install the desktop app |
|
|
75
|
-
| sideby | Yes. `sideby run` selects the directory for that process and leaves your shell alone. | Linked to the main account. `sideby doctor --fix` repairs copies. Sign-in and sessions stay separate. | Every account, from local files
|
|
75
|
+
| sideby | Yes. `sideby run` selects the directory for that process and leaves your shell alone. | Linked to the main account. `sideby doctor --fix` repairs copies. Sign-in and sessions stay separate. | Every account, from local files and each tool's own CLI. | `npm i -g sideby`, then `sideby new` and sign in |
|
|
76
76
|
|
|
77
77
|
cc-switch's docs describe that switch: it writes the live settings file ([README](https://github.com/farion1231/cc-switch#readme)). [#1105](https://github.com/farion1231/cc-switch/issues/1105) and [#2908](https://github.com/farion1231/cc-switch/issues/2908) ask for several sessions at once. [#1106](https://github.com/farion1231/cc-switch/issues/1106) is closed; the maintainer says Claude Code's "open terminal" can start one provider without changing the global config.
|
|
78
78
|
|
|
@@ -88,7 +88,7 @@ cc-switch's docs describe that switch: it writes the live settings file ([README
|
|
|
88
88
|
| Have the panel ready in the background whenever you log in | **Settings › Startup** in the panel, or `sideby autostart on` | [Start at login](docs/guide/usage.md#start-at-login) |
|
|
89
89
|
| Add an API key account (DeepSeek, a gateway, …) | `sideby new claude deepseek --api` | [API accounts](docs/guide/usage.md#api-accounts) |
|
|
90
90
|
| Find and fix config drift between accounts | `sideby doctor --fix` | [Doctor](docs/guide/usage.md#doctor) |
|
|
91
|
-
| See
|
|
91
|
+
| See every account's quota (no setup) | `sideby quota` | [Claude quota](docs/guide/usage.md#claude-quota) |
|
|
92
92
|
| Find the cheapest auto-compact size from your own sessions, and use it in one click | `sideby tune` (or the panel's Context tuning tab) | [Context tuning](docs/guide/usage.md#context-tuning) |
|
|
93
93
|
| See each account's model and effort, and set them per account | `sideby model <account>` (or click the model in the panel) | [Model and effort](docs/guide/usage.md#model-and-effort) |
|
|
94
94
|
| Stop Claude and Codex when your requests would leave from a country you did not allow, for example when the proxy stopped | **Settings › Network check** in the panel, or `sideby network on` | [Network check](docs/guide/usage.md#network-check) |
|
|
@@ -109,7 +109,7 @@ cc-switch's docs describe that switch: it writes the live settings file ([README
|
|
|
109
109
|
|
|
110
110
|
| Host | Account directories | Sign-in | Quota | Usage (7 days) |
|
|
111
111
|
|---|---|---|---|---|
|
|
112
|
-
| Claude Code | `~/.claude`, `~/.claude-<name>` | `claude auth login` | ✓
|
|
112
|
+
| Claude Code | `~/.claude`, `~/.claude-<name>` | `claude auth login` | ✓ | ✓ |
|
|
113
113
|
| Codex CLI | `~/.codex`, `~/.codex-<name>` | `codex login` | ✓ | ✓ |
|
|
114
114
|
| Grok Build | `~/.grok`, `~/.grok-<name>` | `grok login` | weekly, asked from the `grok` CLI | no public source |
|
|
115
115
|
| pi | `~/.pi/agent`, `~/.pi-<name>/agent` | `/login` inside pi | no public source | no public source |
|
package/dist/build.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"id":"
|
|
1
|
+
{"id":"20261030T095351-4f4eaf"}
|
package/dist/src/cli/commands.js
CHANGED
|
@@ -19,7 +19,7 @@ import { installHint, runHost } from '../core/launch.js';
|
|
|
19
19
|
import { loginAfterLine, loginBeforeLine } from '../core/login-check.js';
|
|
20
20
|
import { accountModel, setAccountModel } from '../core/model-choice.js';
|
|
21
21
|
import { resolvePaths, tildify } from '../core/paths.js';
|
|
22
|
-
import { USAGE_DAYS } from '../core/quota-levels.js';
|
|
22
|
+
import { takeoverLimits, USAGE_DAYS } from '../core/quota-levels.js';
|
|
23
23
|
import { groupsToUndo, remoteControlNothingToDo, remoteControlTodo, } from '../core/remote-control.js';
|
|
24
24
|
import { SEARCH_CATALOG, searchCatalog } from '../core/search-catalog.js';
|
|
25
25
|
import { SHELLS } from '../core/shell-init.js';
|
|
@@ -576,7 +576,7 @@ async function runPick(rt, name, args, io, opts = {}) {
|
|
|
576
576
|
if (choice.ref === null) {
|
|
577
577
|
const now = rt.readContext().now;
|
|
578
578
|
const why = choice.earliestReset
|
|
579
|
-
? `every ${family} account
|
|
579
|
+
? `every signed-in ${family} account has used up its quota; ${choice.earliestReset.ref} is back at ${clock(choice.earliestReset.at, now)}`
|
|
580
580
|
: `no ${family} account can be used now`;
|
|
581
581
|
const signIn = choice.signIn.length
|
|
582
582
|
? `; sign in with \`sideby login ${choice.signIn.join('` or `sideby login ')}\``
|
|
@@ -585,7 +585,7 @@ async function runPick(rt, name, args, io, opts = {}) {
|
|
|
585
585
|
return 1;
|
|
586
586
|
}
|
|
587
587
|
const { pickLine } = await import('../core/pick-commands.js');
|
|
588
|
-
io.err(c.dim(`sideby: ${pickLine(name, choice)}`));
|
|
588
|
+
io.err((choice.reason === 'tight' ? c.yellow : c.dim)(`sideby: ${pickLine(name, choice, family)}`));
|
|
589
589
|
return runAccount(rt, choice.ref, args, io, opts);
|
|
590
590
|
}
|
|
591
591
|
/**
|
|
@@ -631,31 +631,45 @@ export async function cmdNext(a, io) {
|
|
|
631
631
|
return plan.pick && plan.hasQuota ? 0 : 1;
|
|
632
632
|
}
|
|
633
633
|
const now = rt.readContext().now;
|
|
634
|
+
const limits = plan.tight
|
|
635
|
+
? { short: plan.tight.limit, long: plan.tight.longLimit }
|
|
636
|
+
: takeoverLimits(rt.handoffSettings().threshold);
|
|
637
|
+
const low = (e) => e.state === 'full' && e.pressure !== null && e.pressure < 100;
|
|
638
|
+
// The window closest to its own Takeover Limit, which decides the rank: "7d 87% of 95%".
|
|
639
|
+
const closest = (e) => e.window
|
|
640
|
+
? `${e.window.label} ${e.window.usedPercent}% of ${e.window.limit}%`
|
|
641
|
+
: `${Math.round(e.pressure ?? 0)}%`;
|
|
634
642
|
const note = (e) => {
|
|
635
643
|
if (e.sameLoginAs)
|
|
636
644
|
return `same login as ${e.sameLoginAs}, which shares its quota; skipped`;
|
|
637
645
|
if (e.state === 'ready')
|
|
638
|
-
return `
|
|
646
|
+
return `closest to its limit: ${closest(e)}`;
|
|
639
647
|
if (e.state === 'unknown')
|
|
640
648
|
return 'no quota data yet';
|
|
641
649
|
if (e.state === 'api')
|
|
642
650
|
return includeApi ? 'API account, pay per use' : 'left out; add --include-api to use it';
|
|
651
|
+
if (low(e)) {
|
|
652
|
+
const over = `past its limit: ${closest(e)} (limits ${limits.short}% for 5-hour, ${limits.long}% for weekly windows)`;
|
|
653
|
+
return e.resetsAt ? `${over} until ${clock(e.resetsAt, now)}` : over;
|
|
654
|
+
}
|
|
643
655
|
if (e.state === 'full')
|
|
644
|
-
return e.resetsAt ? `back at ${clock(e.resetsAt, now)}` : '
|
|
656
|
+
return e.resetsAt ? `used up; back at ${clock(e.resetsAt, now)}` : 'used up';
|
|
645
657
|
return `signed out; sign in with \`sideby login ${e.ref}\``;
|
|
646
658
|
};
|
|
647
659
|
const mark = { ready: c.green, unknown: c.yellow, api: c.cyan, full: c.red, 'logged-out': c.dim };
|
|
660
|
+
// A full Account with no window used up still has some quota: `low` in the table, and a Last Resort can start it.
|
|
661
|
+
const stateText = (e) => low(e) ? c.yellow('low') : mark[e.state](e.state === 'logged-out' ? 'signed out' : e.state);
|
|
648
662
|
io.out(table(plan.accounts.map((e) => [
|
|
649
663
|
e.ref === plan.pick ? c.green('→') : ' ',
|
|
650
664
|
c.bold(e.ref),
|
|
651
|
-
|
|
665
|
+
stateText(e),
|
|
652
666
|
c.dim(note(e)),
|
|
653
667
|
]), ['', 'ACCOUNT', 'STATE', 'NOTE']));
|
|
654
668
|
if (!plan.pick) {
|
|
655
669
|
const out = plan.accounts.filter((e) => e.state === 'logged-out').map((e) => `sideby login ${e.ref}`);
|
|
656
670
|
const apiLeft = !includeApi && plan.accounts.some((e) => e.state === 'api');
|
|
657
671
|
const why = plan.earliestReset
|
|
658
|
-
? `every ${family} account
|
|
672
|
+
? `every signed-in ${family} account has used up its quota; ${plan.earliestReset.ref} is back at ${clock(plan.earliestReset.at, now)}`
|
|
659
673
|
: `no ${family} account can be used now`;
|
|
660
674
|
const same = plan.accounts.filter((e) => e.sameLoginAs).map((e) => e.ref);
|
|
661
675
|
const sameNote = same.length ? `; ${same.join(', ')} share the quota of ${plan.from}` : '';
|
|
@@ -675,10 +689,12 @@ export async function cmdNext(a, io) {
|
|
|
675
689
|
return 1;
|
|
676
690
|
}
|
|
677
691
|
const reason = picked.state === 'ready'
|
|
678
|
-
? `
|
|
679
|
-
:
|
|
680
|
-
?
|
|
681
|
-
:
|
|
692
|
+
? `furthest from its limits, ${closest(picked)}`
|
|
693
|
+
: plan.tight
|
|
694
|
+
? `every ${family} account is past its limit (${plan.tight.limit}% of a 5-hour window, ${plan.tight.longLimit}% of a weekly one); this one has the most left, ${closest(picked)}`
|
|
695
|
+
: picked.state === 'unknown'
|
|
696
|
+
? 'no account with known room; this one has no quota data yet'
|
|
697
|
+
: 'API account, pay per use';
|
|
682
698
|
io.out(`\nNext: ${c.bold(plan.pick)} (${reason}).`);
|
|
683
699
|
if (a.flags.has('dry-run')) {
|
|
684
700
|
io.out(`Start it with \`${runCmd}\`.`);
|
package/dist/src/cli/format.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { windowLevel } from '../core/quota-levels.js';
|
|
2
2
|
import { MIN_CALLS } from '../core/tuning.js';
|
|
3
3
|
const useColor = () => !process.env.NO_COLOR && Boolean(process.stdout.isTTY);
|
|
4
4
|
const paint = (code) => (s) => (useColor() ? `\x1b[${code}m${s}\x1b[0m` : s);
|
|
@@ -88,7 +88,8 @@ function windowText(w, now) {
|
|
|
88
88
|
if (Date.parse(w.resetsAt) <= now.getTime())
|
|
89
89
|
return `${label} reset`;
|
|
90
90
|
const pct = Math.round(w.usedPercent);
|
|
91
|
-
|
|
91
|
+
// Each window by its own levels: a weekly window warns at 85% and turns red at 95% (spec §3.26).
|
|
92
|
+
const level = windowLevel({ ...w, usedPercent: pct });
|
|
92
93
|
const color = level === 'fail' ? c.red : level === 'warn' ? c.yellow : c.green;
|
|
93
94
|
return `${label} ${color(`${pct}%`)} ${c.dim(`→ ${clock(w.resetsAt, now)}`)}`;
|
|
94
95
|
}
|
|
@@ -429,7 +429,7 @@ const MenubarRow = Type.Object({
|
|
|
429
429
|
description: "A Host whose every limit covers one model group: a line per group under the account, each with its own bars; the account's `bars` are then null.",
|
|
430
430
|
}),
|
|
431
431
|
reset: Type.String({
|
|
432
|
-
description: '
|
|
432
|
+
description: 'Which window resets and how long until it does, such as `7d · in 4d 17h`: the window closest to its own red level (85% for 5 hours, 95% for longer windows). Empty when nothing is used.',
|
|
433
433
|
}),
|
|
434
434
|
note: Type.String({
|
|
435
435
|
description: 'A no-quota explanation, or an old-reading warning beside the bars.',
|
|
@@ -691,12 +691,22 @@ export const OUTPUT_SCHEMAS = {
|
|
|
691
691
|
Type.Literal('full'),
|
|
692
692
|
Type.Literal('logged-out'),
|
|
693
693
|
], {
|
|
694
|
-
description: 'ready and unknown can be picked; api only with --include-api;
|
|
694
|
+
description: 'ready and unknown can be picked; api only with --include-api; logged-out never. full (a window at or past its own Takeover Limit: 5-hour windows the handoff threshold - 10, windows longer than a day the threshold) only as a Last Resort: when no subscription account is ready or unknown, the one with the lowest limitUse and no window used up, and `tight` is set.',
|
|
695
695
|
}),
|
|
696
696
|
pressure: Type.Union([Type.Number(), Type.Null()], {
|
|
697
697
|
description: 'Used percent of the fullest window not reset yet; null without quota.',
|
|
698
698
|
}),
|
|
699
|
-
|
|
699
|
+
limitUse: Type.Union([Type.Number(), Type.Null()], {
|
|
700
|
+
description: 'Limit Use: the percent of its own Takeover Limit that the closest window has used (a weekly 87% under 95 is 91.6); null without quota. Ready accounts rank by it, lowest first.',
|
|
701
|
+
}),
|
|
702
|
+
window: Type.Optional(Type.Object({
|
|
703
|
+
label: Type.String({ description: 'Such as `5h` or `7d`.' }),
|
|
704
|
+
usedPercent: Type.Number({ description: 'Rounded.' }),
|
|
705
|
+
limit: Type.Number({ description: "This window's Takeover Limit in percent." }),
|
|
706
|
+
}, { description: 'The window closest to its own Takeover Limit; absent when none is open.' })),
|
|
707
|
+
resetsAt: Type.Optional(Type.String({
|
|
708
|
+
description: 'For full: when it is usable again. With a window used up, when those windows reset; otherwise when every window is under its Takeover Limit again.',
|
|
709
|
+
})),
|
|
700
710
|
observedAt: Type.Optional(Type.String({ description: "When the account's quota was recorded (its last session)." })),
|
|
701
711
|
quotaFrom: Type.Optional(Type.String({
|
|
702
712
|
description: 'The account the quota was read from, when it is another account of the same login.',
|
|
@@ -708,8 +718,18 @@ export const OUTPUT_SCHEMAS = {
|
|
|
708
718
|
hasQuota: Type.Boolean({
|
|
709
719
|
description: 'False when no account has quota data: the pick is then a guess, and `sideby next` does not start it (exit 1).',
|
|
710
720
|
}),
|
|
721
|
+
tight: Type.Optional(Type.Object({
|
|
722
|
+
limit: Type.Number({
|
|
723
|
+
description: 'The Takeover Limit of 5-hour windows in percent (handoff threshold - 10).',
|
|
724
|
+
}),
|
|
725
|
+
longLimit: Type.Number({
|
|
726
|
+
description: 'The Takeover Limit of windows longer than a day in percent (the handoff threshold).',
|
|
727
|
+
}),
|
|
728
|
+
}, {
|
|
729
|
+
description: 'Set when the pick is a Last Resort: every subscription account has a window at or past its Takeover Limit, and the pick is the one with the lowest limitUse and no window used up.',
|
|
730
|
+
})),
|
|
711
731
|
earliestReset: Type.Optional(Type.Object({ ref: Str, at: Str }, {
|
|
712
|
-
description: 'Set when nothing can be picked and some accounts are full: the first to come back.',
|
|
732
|
+
description: 'Set when nothing can be picked and some accounts are full (every one has a window used up): the first to come back.',
|
|
713
733
|
})),
|
|
714
734
|
}, { title: 'sideby next --json' }),
|
|
715
735
|
'quota-setup': Type.Object({ schemaVersion: Version, family: Str, applied: Type.Boolean(), plan: QuotaSetupPlan }, { title: 'sideby quota setup --json' }),
|
|
@@ -1478,7 +1498,7 @@ export const OUTPUT_SCHEMAS = {
|
|
|
1478
1498
|
'menubar-snapshot': Type.Object({
|
|
1479
1499
|
schemaVersion: Version,
|
|
1480
1500
|
title: Type.String({
|
|
1481
|
-
description: 'The words next to the icon, such as `Codex 62%`, once the tightest tool reaches 60
|
|
1501
|
+
description: 'The words next to the icon, such as `Codex 62%`, once the tightest tool reaches its warning level (60% of a 5-hour window, 85% of a longer one); empty otherwise.',
|
|
1482
1502
|
}),
|
|
1483
1503
|
level: Type.Union(['ok', 'warn', 'fail', 'none'].map((k) => Type.Literal(k))),
|
|
1484
1504
|
tooltip: Str,
|
package/dist/src/core/ask.js
CHANGED
|
@@ -13,6 +13,7 @@ import { writeFileAtomic } from './fs-safe.js';
|
|
|
13
13
|
import { planHandoff } from './handoff.js';
|
|
14
14
|
import { resolvePaths } from './paths.js';
|
|
15
15
|
import { runProbe, spawnProbe } from './probe.js';
|
|
16
|
+
import { takeoverLimits } from './quota-levels.js';
|
|
16
17
|
import { askLauncher, askRunCommand, guessTerminal, installedTerminals, launcherProblem, openTerminal, TERMINALS, validateCustomTemplate, wslDistro, } from './terminal.js';
|
|
17
18
|
export async function pickTaskAccount(rt, opts = {}) {
|
|
18
19
|
const all = (await rt.accounts()).filter((a) => !opts.family || a.family === opts.family);
|
|
@@ -38,7 +39,11 @@ export async function pickTaskAccount(rt, opts = {}) {
|
|
|
38
39
|
}
|
|
39
40
|
if (!candidates.length)
|
|
40
41
|
return { reason: 'none-signed-in' };
|
|
41
|
-
|
|
42
|
+
// As `sideby next`: past the Takeover Limits, the Account closest to room with no window used up (a Last Resort, §3.13).
|
|
43
|
+
const plan = planHandoff('', candidates, rt.readContext().now, {
|
|
44
|
+
limits: takeoverLimits(rt.handoffSettings().threshold),
|
|
45
|
+
lastResort: true,
|
|
46
|
+
});
|
|
42
47
|
const account = all.find((a) => a.ref === plan.pick);
|
|
43
48
|
return account ? { account } : { reason: 'all-full' };
|
|
44
49
|
}
|
|
@@ -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',
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { planHandoff } from './handoff.js';
|
|
2
|
-
import {
|
|
2
|
+
import { takeoverLimits, windowReset } from './quota-levels.js';
|
|
3
3
|
/**
|
|
4
4
|
* Sorts the Accounts this chain used by their latest hop: one that handed over for a Quota window that has reset by
|
|
5
5
|
* `now` may take over again like any other, one with no recorded window only when it is `ready`, the rest stay out.
|
|
@@ -53,11 +53,11 @@ function waitUntil(origin, s, now, ahead) {
|
|
|
53
53
|
return new Date(last).toISOString();
|
|
54
54
|
}
|
|
55
55
|
/**
|
|
56
|
-
* One candidate's §3.13 state; API accounts are pickable here, as the last resort. An Account at or
|
|
57
|
-
* 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 "接力规则").
|
|
58
58
|
*/
|
|
59
59
|
function stateOf(c, s, now) {
|
|
60
|
-
return planHandoff(c.family, [c], now, { includeApi: true,
|
|
60
|
+
return planHandoff(c.family, [c], now, { includeApi: true, limits: takeoverLimits(s.threshold) })
|
|
61
61
|
.accounts[0];
|
|
62
62
|
}
|
|
63
63
|
/** Families in the order `otherFamilies` tries them: `familyOrder` first, then the rest as the candidates come. */
|
|
@@ -81,8 +81,8 @@ export function selectNext(input) {
|
|
|
81
81
|
const allowed = new Set(s.crossOrganization.map(refOf));
|
|
82
82
|
const used = stillUsed(origin.ref, input.used, now);
|
|
83
83
|
const family = s.families[origin.family];
|
|
84
|
-
const
|
|
85
|
-
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));
|
|
86
86
|
let list;
|
|
87
87
|
if (family?.policy === 'order') {
|
|
88
88
|
list = family.order.map(refOf).flatMap((r) => (byRef.has(r) ? [byRef.get(r)] : []));
|
|
@@ -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)`;
|