sideby 0.43.0 → 0.45.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.
Files changed (52) hide show
  1. package/dist/build.json +1 -1
  2. package/dist/src/cli/commands.d.ts +4 -0
  3. package/dist/src/cli/commands.js +9 -8
  4. package/dist/src/cli/json-schemas.js +58 -0
  5. package/dist/src/core/cli-commands.js +0 -10
  6. package/dist/src/core/egress.d.ts +1 -7
  7. package/dist/src/core/egress.js +7 -36
  8. package/dist/src/core/file-memo.d.ts +2 -0
  9. package/dist/src/core/file-memo.js +20 -0
  10. package/dist/src/core/handoff.d.ts +9 -0
  11. package/dist/src/core/handoff.js +10 -0
  12. package/dist/src/core/history.d.ts +1 -1
  13. package/dist/src/core/history.js +2 -2
  14. package/dist/src/core/memo-store.js +32 -8
  15. package/dist/src/core/network.d.ts +5 -35
  16. package/dist/src/core/network.js +18 -54
  17. package/dist/src/core/themes.js +0 -7
  18. package/dist/src/core/update.d.ts +1 -1
  19. package/dist/src/core/update.js +4 -12
  20. package/dist/src/families/claude/quota-fetch.js +2 -2
  21. package/dist/src/families/claude/tuning.d.ts +0 -1
  22. package/dist/src/families/claude/tuning.js +1 -1
  23. package/dist/src/families/quota-watch.d.ts +12 -0
  24. package/dist/src/families/quota-watch.js +24 -0
  25. package/dist/src/network/hook.js +1 -1
  26. package/dist/src/network/page.d.ts +1 -1
  27. package/dist/src/network/page.js +15 -181
  28. package/dist/src/panel/changes.d.ts +57 -0
  29. package/dist/src/panel/changes.js +178 -0
  30. package/dist/src/panel/handler.d.ts +38 -0
  31. package/dist/src/panel/handler.js +263 -93
  32. package/dist/src/panel/i18n.d.ts +23 -0
  33. package/dist/src/panel/i18n.js +46 -0
  34. package/dist/src/panel/page-logic.d.ts +22 -29
  35. package/dist/src/panel/page-logic.js +22 -33
  36. package/dist/src/panel/page-script.js +114 -45
  37. package/dist/src/panel/page-styles.js +26 -26
  38. package/dist/src/panel/page.js +3 -1
  39. package/dist/src/quota/claude-statusline.js +1 -1
  40. package/dist/src/runtime.d.ts +13 -1
  41. package/dist/src/runtime.js +14 -8
  42. package/dist/src/types.d.ts +2 -2
  43. package/dist/src/unattended/hook.js +0 -1
  44. package/dist/testing/index.js +1 -1
  45. package/package.json +2 -1
  46. package/schemas/network-diagnose.json +239 -0
  47. package/schemas/next.json +25 -0
  48. package/skills/sideby/SKILL.md +2 -2
  49. package/skills/sideby/references/handoff.md +2 -0
  50. package/skills/sideby/references/network.md +5 -3
  51. package/dist/src/network/page-script.d.ts +0 -40
  52. package/dist/src/network/page-script.js +0 -270
@@ -0,0 +1,178 @@
1
+ // The Panel's change feed (spec §3.45, ADR-0084): while a page asks, watch the files Quota lands in and count their
2
+ // changes, so the page reads the state within about a second instead of at its next 20-second round. Events only bump
3
+ // a counter: no watched file is ever read here.
4
+ import { statSync, watch } from 'node:fs';
5
+ import { join } from 'node:path';
6
+ /** How long after the first event a change counts: an atomic write (temporary file, then rename) counts once. */
7
+ export const GATHER_MS = 300;
8
+ /** How long a request with nothing new is held. */
9
+ export const HOLD_MS = 25_000;
10
+ /** Watchers close this long after the last request: the page asks again at once, so only a gone page is this quiet. */
11
+ export const IDLE_MS = 40_000;
12
+ /** How often the folders are listed again while a page asks (a new Account, a folder made since). */
13
+ const RESCAN_MS = 60_000;
14
+ /** With several sessions open the status line rewrites its cache on every reply; each change rereads every Account. */
15
+ const QUOTA_GAP_MS = 5000;
16
+ /** sideby's own churn under `<state>/quota`: ask locks, the folder `claude -p /usage` runs in, Limit Reset records. */
17
+ const OWN = new Set(['asking', 'claude-run', 'limit-reset']);
18
+ /** `<state>/quota`: the status line cache and the kept answers of each Host's own CLI. */
19
+ export function stateQuotaDir(stateDir) {
20
+ return {
21
+ dir: join(stateDir, 'quota'),
22
+ recursive: true,
23
+ match: {
24
+ test: (name) => {
25
+ const parts = name.split(/[\\/]/);
26
+ const base = parts[parts.length - 1] ?? '';
27
+ return base.endsWith('.json') && !base.startsWith('.') && !OWN.has(parts[0] ?? '');
28
+ },
29
+ },
30
+ gapMs: QUOTA_GAP_MS,
31
+ };
32
+ }
33
+ const realTimers = {
34
+ now: () => Date.now(),
35
+ set: (fn, ms) => setTimeout(fn, ms).unref(),
36
+ clear: (t) => clearTimeout(t),
37
+ };
38
+ /**
39
+ * Turns events into counted changes: one `GATHER_MS` after the first event, and never sooner than `gapMs` (the
40
+ * event's folder) after the last one. Events meanwhile join that change; one whose folder allows an earlier time
41
+ * brings it forward.
42
+ */
43
+ export function changeClock(fire, timers = realTimers) {
44
+ let last = Number.NEGATIVE_INFINITY;
45
+ let due = null;
46
+ let timer;
47
+ return {
48
+ poke(gapMs) {
49
+ const at = timers.now();
50
+ const want = Math.max(at + GATHER_MS, last + gapMs);
51
+ if (due !== null && due <= want)
52
+ return;
53
+ if (due !== null)
54
+ timers.clear(timer);
55
+ due = want;
56
+ timer = timers.set(() => {
57
+ due = null;
58
+ last = timers.now();
59
+ fire();
60
+ }, want - at);
61
+ },
62
+ stop() {
63
+ if (due !== null)
64
+ timers.clear(timer);
65
+ due = null;
66
+ },
67
+ };
68
+ }
69
+ const isDir = (p) => {
70
+ try {
71
+ return statSync(p).isDirectory();
72
+ }
73
+ catch {
74
+ return false;
75
+ }
76
+ };
77
+ /**
78
+ * The feed of one Panel. `dirs` lists what to watch; `onChange` runs once per counted change, before held requests
79
+ * are answered.
80
+ */
81
+ export function createChangeFeed(opts) {
82
+ let seq = 0;
83
+ const waiters = new Set();
84
+ const watchers = new Map();
85
+ let lastAsk = 0;
86
+ let lastScan = 0;
87
+ let scanning = null;
88
+ let idle;
89
+ const clock = changeClock(() => {
90
+ seq++;
91
+ opts.onChange?.();
92
+ for (const done of [...waiters])
93
+ done();
94
+ });
95
+ function closeAll() {
96
+ for (const w of watchers.values())
97
+ w.close();
98
+ watchers.clear();
99
+ clock.stop();
100
+ lastScan = 0;
101
+ if (idle)
102
+ clearInterval(idle);
103
+ idle = undefined;
104
+ }
105
+ async function scan() {
106
+ lastScan = Date.now();
107
+ const dirs = await opts.dirs().catch(() => []);
108
+ const want = new Map(dirs.map((d) => [`${d.recursive ? 'r' : 'f'}\0${d.dir}`, d]));
109
+ for (const [key, w] of watchers)
110
+ if (!want.has(key)) {
111
+ w.close();
112
+ watchers.delete(key);
113
+ }
114
+ for (const [key, d] of want) {
115
+ if (watchers.has(key) || !isDir(d.dir))
116
+ continue;
117
+ // A platform without recursive watching, or a folder that cannot be watched, is left out: the page polls.
118
+ try {
119
+ const w = watch(d.dir, { recursive: d.recursive, persistent: false }, (_event, name) => {
120
+ if (name === null || d.match.test(String(name)))
121
+ clock.poke(d.gapMs);
122
+ });
123
+ // Unhandled, a watcher's error would end the process (a Portal's worker with it).
124
+ w.on('error', () => {
125
+ w.close();
126
+ if (watchers.get(key) === w)
127
+ watchers.delete(key);
128
+ });
129
+ watchers.set(key, w);
130
+ }
131
+ catch { }
132
+ }
133
+ }
134
+ async function ensure() {
135
+ if (!idle) {
136
+ idle = setInterval(() => {
137
+ if (!waiters.size && Date.now() - lastAsk > IDLE_MS)
138
+ closeAll();
139
+ }, 10_000).unref();
140
+ }
141
+ if (!scanning && (!lastScan || Date.now() - lastScan > RESCAN_MS))
142
+ scanning = scan().finally(() => {
143
+ scanning = null;
144
+ });
145
+ if (scanning)
146
+ await scanning;
147
+ }
148
+ return {
149
+ /**
150
+ * The answer for a page that last saw `since` (null: none yet). Held up to `HOLD_MS` while nothing changed, or
151
+ * until `gone` settles (the request closed).
152
+ */
153
+ async wait(since, gone) {
154
+ lastAsk = Date.now();
155
+ await ensure();
156
+ const now = () => ({
157
+ seq,
158
+ changed: since !== null && since !== seq,
159
+ watching: watchers.size > 0,
160
+ });
161
+ if (since === null || since !== seq || !watchers.size)
162
+ return now();
163
+ return new Promise((resolve) => {
164
+ const done = () => {
165
+ clearTimeout(timer);
166
+ waiters.delete(done);
167
+ lastAsk = Date.now();
168
+ resolve(now());
169
+ };
170
+ const timer = setTimeout(done, HOLD_MS).unref();
171
+ waiters.add(done);
172
+ void gone.then(done, done);
173
+ });
174
+ },
175
+ /** For tests: closes every watcher now. */
176
+ close: closeAll,
177
+ };
178
+ }
@@ -152,6 +152,10 @@ export type PanelAccount = (AccountStatus | (Account & Partial<AccountStatus>))
152
152
  modelInfo?: Omit<AccountModel, 'ref' | 'family'>;
153
153
  /** Its reads take longer than the state waits (`readBudget`); they go on, and a later state carries them. */
154
154
  pending?: true;
155
+ /** Its usage is still being read (the rest is current); a later state carries it. */
156
+ usagePending?: true;
157
+ /** The last complete state, shown while a new read of the Account takes longer than the state waits. */
158
+ refreshing?: true;
155
159
  };
156
160
  export type { DoctorHistory };
157
161
  /** Counts of failing and warning Findings per checked Account, from the Doctor history. */
@@ -283,6 +287,11 @@ export interface PanelState {
283
287
  * limit (a Last Resort), `unknown` when it has no Quota data and the accounts with data are full.
284
288
  */
285
289
  handoffReason?: Record<string, 'expiring' | 'room' | 'tight' | 'unknown'>;
290
+ /**
291
+ * Family → the "Best to use now" card's pick (spec §3.45): the same plan as `handoff`, also for a Family with one
292
+ * login, and when the running sessions are not known in time (ranked as if none ran).
293
+ */
294
+ pickNow?: Record<string, PickNow>;
286
295
  /** Auto Handoff settings (spec §3.17) and the Families that can start one; readiness comes from /api/handoff/status. */
287
296
  autoHandoff: {
288
297
  settings: HandoffSettings;
@@ -403,6 +412,24 @@ export declare function autostartView(s: AutostartStatus, home: string): {
403
412
  missing?: string[] | undefined;
404
413
  loaded?: boolean | undefined;
405
414
  };
415
+ /** One Family's "Best to use now" (spec §3.45). */
416
+ export interface PickNow {
417
+ /** The Account `sideby next` would pick; null when none can be started now. */
418
+ pick: string | null;
419
+ /** Whether any Account of the Family has Quota data; without any there is no pick. */
420
+ hasQuota: boolean;
421
+ /** Why the pick goes first, as `handoffReason`. */
422
+ reason?: 'expiring' | 'room' | 'tight' | 'unknown';
423
+ /** The Live Sessions of the pick's login, Pick Records included, when any. */
424
+ running?: number;
425
+ /** Why there is no pick: no Quota data, every signed-in Account has a window used up, or none is signed in. */
426
+ why?: 'no-quota' | 'used-up' | 'signed-out';
427
+ /** With `used-up`: the Account that comes back first. */
428
+ earliestReset?: {
429
+ ref: string;
430
+ at: string;
431
+ };
432
+ }
406
433
  /** The Live Sessions and Pick Records a pick counts (ADR-0083). */
407
434
  export interface PickLoad {
408
435
  live: ReadonlyMap<string, number>;
@@ -412,6 +439,17 @@ export interface PickLoad {
412
439
  export declare function handoffByFamily(rt: Runtime, accounts: readonly PanelAccount[], now: Date, load?: PickLoad): Record<string, string>;
413
440
  /** The plan behind each pick of `handoffByFamily`, spread by Room per Session when `load` is given (ADR-0083). */
414
441
  export declare function handoffPlans(rt: Runtime, accounts: readonly PanelAccount[], now: Date, load?: PickLoad): Record<string, HandoffPlan>;
442
+ /**
443
+ * `sideby next`'s plan for every Family with a Quota source and a signed-in or unknown Subscription Account read
444
+ * already, with how many logins it has: the Next pick takes those with two or more, the "Best to use now" card
445
+ * (spec §3.45) every one.
446
+ */
447
+ export declare function familyPlans(rt: Runtime, accounts: readonly PanelAccount[], now: Date, load?: PickLoad): Record<string, {
448
+ plan: HandoffPlan;
449
+ logins: number;
450
+ }>;
451
+ /** The "Best to use now" card's entry of a Family's plan (spec §3.45). */
452
+ export declare function pickNowOf(plan: HandoffPlan): PickNow;
415
453
  export type RemoteControlView = Omit<RemoteControlGroup, 'file'> & {
416
454
  file: string;
417
455
  };