ccdeck 1.43.0 → 1.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.
@@ -0,0 +1,595 @@
1
+ // Auto-switch controls: read and write claude-swap's autoswitch settings and
2
+ // run a tick on a schedule.
3
+ //
4
+ // The engine is claude-swap's own — `cswap auto --once` evaluates one tick and
5
+ // exits, honouring the cooldown, quarantine and poll-budget state it keeps in
6
+ // its own files. Running that on an interval gets the same behaviour as the
7
+ // long-lived `cswap auto` loop while leaving all the decisions with the tool
8
+ // that owns them: nothing here decides when to switch, only when to ask.
9
+ //
10
+ // A tick can move the user's live Claude account, so it is off unless turned
11
+ // on and the setting survives restarts.
12
+ import { run } from "./exec.mjs";
13
+ import { cswapBin } from "./cswap-install.mjs";
14
+ import { invalidateClaudeAccountsCache } from "./claude-accounts.mjs";
15
+ import { invalidateQuotaCache } from "./quota.mjs";
16
+ import { readFile, writeFile, mkdir } from "node:fs/promises";
17
+ import { join } from "node:path";
18
+ import { homedir } from "node:os";
19
+
20
+ const STATE_DIR = join(homedir(), ".agents-deck");
21
+ const STATE_PATH = join(STATE_DIR, "cswap-auto.json");
22
+
23
+ const TICK_TIMEOUT_MS = 120_000; // a tick can refresh a token and switch
24
+ const MIN_INTERVAL_S = 15; // claude-swap's own floor
25
+
26
+ // Only these may be written, and only with a value of the right shape. The
27
+ // value reaches an exec argument, and `cswap config set` will happily store
28
+ // whatever it is handed.
29
+ const SETTINGS = {
30
+ "autoswitch.threshold": { type: "number", min: 50, max: 99.9 },
31
+ "autoswitch.intervalSeconds": { type: "number", min: 15, max: 3600 },
32
+ "autoswitch.cooldownSeconds": { type: "number", min: 0, max: 86400 },
33
+ "autoswitch.hysteresisPct": { type: "number", min: 0, max: 50 },
34
+ "autoswitch.model": { type: "model" },
35
+ };
36
+
37
+ // ── one reading at a time ──────────────────────────────────────────────────
38
+
39
+ /**
40
+ * #616: /api/cswap-auto is a GET with no cache, no dedupe and no throttle, and
41
+ * autoStatus() runs BOTH of this module's readers on every one of them — so the
42
+ * number of children was exactly twice the number of requests.
43
+ *
44
+ * Measured on macOS with claude-swap installed, counting real children through a
45
+ * PATH shim: one autoStatus() is 2 children (`cswap config` and `ps -Ao args=`)
46
+ * and about 190ms warm; two back-to-back calls are 4; twenty-five concurrent
47
+ * readers produced 50 — twenty-five Python interpreters and twenty-five `ps` —
48
+ * and took 1.3 to 1.9s between them against 190ms for one, so the cost per
49
+ * reader grows rather than holds. With the guard below the same twenty-five are
50
+ * 2 children and 190ms, which is one reader's worth.
51
+ *
52
+ * On Windows the process-table half is `Get-CimInstance Win32_Process` through
53
+ * PowerShell, carrying an 8s deadline of its own, which is the same order of
54
+ * cost as the Get-Process #544 measured at about six seconds; and where cswap is
55
+ * not on PATH each call also re-pays cswapBin()'s probe, which memoizes only
56
+ * success and which `candidates` expands to four spellings there, two of them
57
+ * launched through cmd.exe.
58
+ *
59
+ * Two callers reach these without an attacker anywhere: AccountsPanel polls the
60
+ * route every 15s per open tab, and runTick asks externalAutoRunning() again
61
+ * before every tick. And it is a GET, so it passes isTrustedRead for any local
62
+ * client that sends neither Origin nor Sec-Fetch-Site — curl, a shell script, a
63
+ * sandboxed agent.
64
+ *
65
+ * The fix is #544's, at the route that sweep did not reach: a minimum gap plus
66
+ * one shared in-flight promise per reader. There is no MAX_OUTSTANDING beside it
67
+ * the way ccusage.mjs has one, and there does not need to be — ccusage keys its
68
+ * cache by date range, so a flood of distinct ranges can never share a run,
69
+ * while each reader here asks exactly one question and every caller of it can
70
+ * therefore join the same child.
71
+ *
72
+ * What is NOT shared is the window, because the two halves are not the same
73
+ * question. See CONFIG_MIN_GAP_MS and EXTERNAL_MIN_GAP_MS.
74
+ */
75
+
76
+ /**
77
+ * `cswap config` — the settings map, which is also what the panel DISPLAYS.
78
+ *
79
+ * The deck is not its only writer: `cswap config set` typed in a terminal
80
+ * changes it behind the deck's back, and the panel is where the user would
81
+ * expect to see that. So this window has to stay well under AccountsPanel's
82
+ * 15s poll, or an edit made outside the deck waits for the window AND the poll.
83
+ * Three seconds does not delay a single tab by one frame — its polls are five
84
+ * gaps apart — while a burst of requests and two tabs whose polls land within
85
+ * three seconds of each other collapse onto one child.
86
+ */
87
+ const CONFIG_MIN_GAP_MS = 3_000;
88
+
89
+ /**
90
+ * The process table — the expensive half, and the one whose answer changes
91
+ * least: it is a boolean about whether the user has their own `cswap auto`
92
+ * running, and nobody starts one between two fifteen-second polls.
93
+ *
94
+ * Ten seconds is chosen against the two scheduled callers rather than against
95
+ * the cost: AccountsPanel's poll is 15s and MIN_INTERVAL_S — claude-swap's own
96
+ * floor, and the smallest tick interval SETTINGS will accept — is also 15, so a
97
+ * gap below both means neither of them is ever handed a reading older than its
98
+ * own period. The deck's tick still decides on a fresh process table, and the
99
+ * panel still shows one; what disappears is the second, third and twenty-fifth
100
+ * copy taken in the same ten seconds.
101
+ *
102
+ * Worst case for a caller in a loop is now 20 `cswap config` and 6 process-table
103
+ * children a minute, whatever it asks for, against a pair per request before.
104
+ */
105
+ const EXTERNAL_MIN_GAP_MS = 10_000;
106
+
107
+ const _config = { last: null, inFlight: null };
108
+ const _external = { last: null, inFlight: null };
109
+
110
+ /**
111
+ * One reading of `read`, shared by everyone who asks inside `gapMs`.
112
+ *
113
+ * Only a real reading is remembered, which is what `value != null` means here:
114
+ * readCswapConfig spells its failure `null` — autoStatus reports
115
+ * `ok: config != null`, so holding one for three seconds would turn a single
116
+ * hiccup into a panel that renders itself as broken for longer than the hiccup
117
+ * lasted — and externalAutoRunning has no failure spelling at all, answering
118
+ * `false` for a process table it could not read because that is the same answer
119
+ * as an empty one and is the safe one either way. The in-flight share still
120
+ * applies to a failing read, so a burst arriving during one is a single failing
121
+ * child rather than a burst of them.
122
+ *
123
+ * `slot.inFlight === mine` on both hops is claude-accounts.mjs's guard and is
124
+ * here for its reason: invalidateCswapAutoCache drops `inFlight` so the next
125
+ * caller starts a read that knows the settings moved, and a read from BEFORE the
126
+ * write must neither store its answer under the new state nor clear the new
127
+ * read's promise on its way out.
128
+ *
129
+ * The reading is not keyed by platform even though externalAutoRunning branches
130
+ * on one. A process does not change platform; the two test files that flip
131
+ * `process.platform` to reach the other half from this one call
132
+ * invalidateCswapAutoCache between cases.
133
+ */
134
+ function throttled(slot, gapMs, read) {
135
+ const now = Date.now();
136
+ if (slot.last && now - slot.last.at < gapMs) return Promise.resolve(slot.last.value);
137
+ if (slot.inFlight) return slot.inFlight;
138
+ const mine = read()
139
+ .then(value => {
140
+ if (value != null && slot.inFlight === mine) slot.last = { at: Date.now(), value };
141
+ return value;
142
+ })
143
+ .finally(() => { if (slot.inFlight === mine) slot.inFlight = null; });
144
+ slot.inFlight = mine;
145
+ return mine;
146
+ }
147
+
148
+ /**
149
+ * Forget both readings, because the deck has just changed what they would say.
150
+ *
151
+ * The one caller is setCswapConfig. There is no `?refresh=1` on /api/cswap-auto
152
+ * and no force argument through autoStatus, because the panel's explicit-refresh
153
+ * path is not a query parameter: every auto-switch control is a POST followed by
154
+ * `load(true)`, which re-fetches this route. Dropping the reading inside the
155
+ * write is what makes that reload show what was written rather than the map read
156
+ * a moment before it — the same disagreement between an optimistic value and the
157
+ * next read that #584 was.
158
+ *
159
+ * The process-table reading goes with it. A settings write does not start
160
+ * anybody's `cswap auto`, so this is not correctness for that half — it is that
161
+ * one function which forgets everything this module is holding cannot be called
162
+ * half-right, and the cost is at most one extra `ps` on a path the user reached
163
+ * by clicking. It is also what the tests reset between cases.
164
+ */
165
+ export function invalidateCswapAutoCache() {
166
+ _config.last = _config.inFlight = null;
167
+ _external.last = _external.inFlight = null;
168
+ }
169
+
170
+ // ── settings ───────────────────────────────────────────────────────────────
171
+
172
+ /**
173
+ * Parse `cswap config` — "key value (default)" per line.
174
+ *
175
+ * Exported for its test rather than for a caller (#383). The two callers here —
176
+ * `autoStatus`, which hands the map straight to the settings panel, and
177
+ * `tickInterval`, which takes the poll interval out of it — both reduce the
178
+ * parse to something a test cannot see through: the panel takes whatever shape
179
+ * it is given, and the interval collapses four fields to one number that is
180
+ * clamped anyway. The parse itself is a regex over human-formatted output from a
181
+ * separate Python tool, on both line-ending conventions. See
182
+ * cswap-auto-readers.test.ts.
183
+ *
184
+ * One reading at a time and one every CONFIG_MIN_GAP_MS at most; the parse below
185
+ * is what a reading is, and admission control is the wrapper. See throttled.
186
+ */
187
+ export function readCswapConfig() {
188
+ return throttled(_config, CONFIG_MIN_GAP_MS, readCswapConfigNow);
189
+ }
190
+
191
+ async function readCswapConfigNow() {
192
+ const r = await run(await cswapBin(), ["config"]);
193
+ if (!r.ok) return null;
194
+ const out = {};
195
+ for (const line of r.stdout.split("\n")) {
196
+ const m = line.match(/^(\S+)\s+(.*?)\s*(\(default\))?\s*$/);
197
+ if (!m || !m[1].includes(".")) continue;
198
+ const raw = m[2].trim();
199
+ out[m[1]] = {
200
+ value: raw === "(none)" ? null : raw,
201
+ isDefault: Boolean(m[3]),
202
+ };
203
+ }
204
+ return out;
205
+ }
206
+
207
+ /**
208
+ * What the model list may be made of before it becomes an argv element.
209
+ *
210
+ * The character class is the same one this field has always had — a
211
+ * comma-separated list of plain model names, bounded at 120 — with the one rule
212
+ * #543 wrote down at cswap-admin.mjs's EMAIL_OK added to the front: *"The
213
+ * leading-character rule is the same argv-position rule ALIAS_OK now carries."*
214
+ *
215
+ * This is the THIRD free-text field to reach an argument vector and the first
216
+ * one that pass missed, because it lives in a different module. It is not a
217
+ * different question. `{ key: "autoswitch.model", value: "-h" }` produced
218
+ * `cswap config set autoswitch.model -h`; argparse on the other side reads the
219
+ * leading dash as an option rather than as data, prints help, exits 0 — so
220
+ * `r.ok` is true and the deck reports a setting saved that was never written,
221
+ * after which the panel's optimistic value disagrees with the next read (#584).
222
+ *
223
+ * Only the first character is constrained, so `claude-3-5-sonnet` and every
224
+ * other dash-bearing model name still works. cswap-argv-position.test.ts
225
+ * enumerates every field this rule covers, so a fourth cannot be added without
226
+ * one.
227
+ */
228
+ const MODEL_LIST_OK = /^(?!-)[A-Za-z0-9 ,._-]{1,120}$/;
229
+
230
+ /** Validate against SETTINGS, then hand to `cswap config set`. */
231
+ export async function setCswapConfig(key, value) {
232
+ const spec = SETTINGS[key];
233
+ if (!spec) return { ok: false, reason: "unknown_setting" };
234
+
235
+ let str;
236
+ if (spec.type === "number") {
237
+ const n = Number(value);
238
+ if (!Number.isFinite(n) || n < spec.min || n > spec.max) return { ok: false, reason: "out_of_range" };
239
+ str = String(n);
240
+ } else if (spec.type === "enum") {
241
+ if (!spec.values.includes(value)) return { ok: false, reason: "bad_value" };
242
+ str = value;
243
+ } else {
244
+ // Model names: a comma-separated list of plain words, or "all".
245
+ str = String(value ?? "").trim();
246
+ if (str && !MODEL_LIST_OK.test(str)) return { ok: false, reason: "bad_value" };
247
+ }
248
+
249
+ const r = await run(await cswapBin(), ["config", "set", key, str]);
250
+ // Whatever the CLI said. A write that reported a failure may still have landed
251
+ // — and `r.ok` is not proof either way here, which is the whole of #584 — so
252
+ // the only safe thing to hold after asking cswap to change a setting is
253
+ // nothing. The panel reloads this route immediately afterwards and gets a real
254
+ // read; see invalidateCswapAutoCache.
255
+ invalidateCswapAutoCache();
256
+ return r.ok ? { ok: true } : { ok: false, reason: "set_failed", detail: (r.stderr || r.stdout).trim().slice(0, 300) };
257
+ }
258
+
259
+ // ── ticks ──────────────────────────────────────────────────────────────────
260
+
261
+ /** Last meaningful event from a `cswap auto --once --json` run. */
262
+ function summarise(stdout) {
263
+ const events = stdout.split("\n")
264
+ .map(l => { try { return JSON.parse(l); } catch { return null; } })
265
+ .filter(e => e && typeof e === "object");
266
+
267
+ const poll = events.find(e => e.event === "poll") ?? null;
268
+ const action = [...events].reverse().find(e => e.event !== "poll" && e.event !== "sleep") ?? null;
269
+
270
+ return {
271
+ event: action?.event ?? "no-switch",
272
+ // Whether the LIVE ACCOUNT MOVED, which is a narrower question than which
273
+ // event came last and the only one the caches care about. Taken over every
274
+ // event rather than over `action`, so a quarantine or an error emitted after
275
+ // the switch cannot hide it; and `dryRun` is checked even though this
276
+ // module's ticks never pass `--dry-run`, because the engine emits the same
277
+ // `switch` event for a decision it did not carry out, and a false positive
278
+ // here throws away readings that cost a subprocess each.
279
+ switched: events.some(e => e.event === "switch" && e.dryRun !== true),
280
+ reason: action?.reason ?? null,
281
+ detail: action?.detail ?? null,
282
+ from: action?.from ?? null,
283
+ to: action?.to ?? null,
284
+ active: poll?.active ?? null,
285
+ threshold: poll?.threshold ?? null,
286
+ headroom: poll?.headroomPct ?? null,
287
+ windows: poll?.windowsPct ?? null,
288
+ };
289
+ }
290
+
291
+ /** Evaluate a tick for real. May switch the active account. */
292
+ async function runAutoTick() {
293
+ const r = await run(await cswapBin(), ["auto", "--once", "--json"], { timeout: TICK_TIMEOUT_MS });
294
+ if (!r.ok && !r.stdout) {
295
+ return { ok: false, reason: "tick_failed", detail: (r.stderr || "").trim().slice(0, 300) };
296
+ }
297
+ return { ok: true, ...summarise(r.stdout) };
298
+ }
299
+
300
+ // ── external engine detection ──────────────────────────────────────────────
301
+
302
+ /**
303
+ * One command line, as a list of the words a process was actually launched
304
+ * with.
305
+ *
306
+ * The quote characters are separators here, not delimiters, and that is the
307
+ * whole point of #552. `Win32_Process.CommandLine` reports what the CREATOR
308
+ * wrote, and every launcher on Windows except a human typing at `cmd.exe`
309
+ * quotes the executable:
310
+ *
311
+ * "C:\Users\dorin\.local\bin\cswap.exe" auto
312
+ *
313
+ * — which is what .NET's `Process.Start` writes, so PowerShell, Windows
314
+ * Terminal's default profile, Task Scheduler and an Explorer shortcut all
315
+ * produce it. A pattern that wanted whitespace immediately after `cswap.exe`
316
+ * saw a `"` there and answered no, for every one of them.
317
+ *
318
+ * The deck's own spawns are the same shape from the other side: viaCmd in
319
+ * src/server/exec.mjs launches a `.cmd` shim as
320
+ * `cmd.exe /d /s /c ""C:\…\cswap.cmd" "auto" "--once""`, with the whole line
321
+ * wrapped in one more pair of quotes because that is what `cmd /c` wants.
322
+ * Treating `"` as a separator takes both apart with no parser and no knowledge
323
+ * of which launcher wrote the line — the outer pair, the per-argument pairs and
324
+ * the bare case all collapse to the same token list.
325
+ *
326
+ * What it deliberately does NOT do is respect a quoted path containing spaces:
327
+ * `"C:\Program Files\cswap\cswap.exe" auto` splits into three tokens rather than
328
+ * two. That costs nothing here — the tail token is still `cswap.exe` followed by
329
+ * `auto`, which is the only question asked — and the alternative is a real
330
+ * command-line parser for a probe whose wrong answer must never be a crash.
331
+ */
332
+ export function commandTokens(line) {
333
+ return String(line ?? "").split(/["\s]+/).filter(Boolean);
334
+ }
335
+
336
+ /** The last path component of a token: `C:\bin\cswap.exe` → `cswap.exe`. */
337
+ const leaf = (token) => token.split(/[\\/]/).pop() ?? "";
338
+
339
+ /** Every spelling of the executable, on every platform. */
340
+ const CSWAP_EXE = /^cswap(\.exe|\.cmd|\.bat)?$/i;
341
+
342
+ /**
343
+ * True when this command line is a long-lived `cswap auto` loop.
344
+ *
345
+ * The rule, stated over tokens rather than characters: some token IS the cswap
346
+ * executable — its last path component, so `/opt/bin/mycswap` and `notcswap`
347
+ * are somebody else's program — and the token straight after it is exactly
348
+ * `auto`, so `autopilot` and `automate` are not this. `--once` anywhere rules
349
+ * the line out: the deck's own ticks carry it, and so does a cron user's.
350
+ *
351
+ * Pure and exported so the Windows shapes can be checked from a Mac. The
352
+ * residual false positive is a line that mentions cswap as an ARGUMENT and then
353
+ * `auto` — `myprog --exe cswap auto`. That direction is the safe one: a wrong
354
+ * `true` is a deck that stays quiet, while a wrong `false` is two engines moving
355
+ * the same live Claude account.
356
+ */
357
+ export function looksLikeAutoLoop(line) {
358
+ if (/--once/i.test(String(line ?? ""))) return false;
359
+ const tokens = commandTokens(line);
360
+ return tokens.some((token, i) =>
361
+ CSWAP_EXE.test(leaf(token)) && String(tokens[i + 1] ?? "").toLowerCase() === "auto");
362
+ }
363
+
364
+ /**
365
+ * True when the user is already running `cswap auto` themselves.
366
+ *
367
+ * Two engines would not corrupt anything — claude-swap serializes decisions
368
+ * under its state lock — but they would double the tick rate against a request
369
+ * budget that is already the scarce resource here, and the user would have two
370
+ * things switching their account with no single place showing why. So the deck
371
+ * reports it and stays out of the way.
372
+ *
373
+ * Exported for its test rather than for a caller (#383). Its two callers reduce
374
+ * it to a boolean on a status object and to a skipped tick, so neither can show
375
+ * WHICH command line was matched — and the matching is the whole function. The
376
+ * two halves also run completely different commands, `ps` against
377
+ * `Get-CimInstance`, so on any one machine only half of it is ever exercised at
378
+ * all. See cswap-auto-readers.test.ts, which drives both from either host.
379
+ *
380
+ * One reading at a time and one every EXTERNAL_MIN_GAP_MS at most — the
381
+ * expensive half of #616, and the one both of its callers ask for on a
382
+ * fifteen-second timer. See throttled.
383
+ */
384
+ export function externalAutoRunning() {
385
+ return throttled(_external, EXTERNAL_MIN_GAP_MS, externalAutoRunningNow);
386
+ }
387
+
388
+ async function externalAutoRunningNow() {
389
+ // A line is the user's loop if it runs `cswap auto` without --once. Our own
390
+ // ticks are --once, and so is a cron user's. See looksLikeAutoLoop.
391
+ const isLoop = looksLikeAutoLoop;
392
+
393
+ if (process.platform === "win32") {
394
+ // No `ps` on Windows, and `tasklist` reports the image name only — every
395
+ // Python tool shows up as python.exe, which cannot tell cswap from
396
+ // anything else. CIM is the one place the full command line is available.
397
+ //
398
+ // `Out-String -Width 32767` is not decoration. `-ExpandProperty` emits
399
+ // strings, and strings leave PowerShell through its console FORMATTER,
400
+ // which hard-wraps at the host buffer width — 80 columns on a redirected
401
+ // stdout, which is what a spawned child always has. A real command line
402
+ // (`"C:\Users\dorin\AppData\Local\Programs\Python\Python312\Scripts\cswap.exe" auto`)
403
+ // is longer than that, so the executable and its subcommand arrived on
404
+ // SEPARATE LINES and no per-line match could ever see both. 32767 is the
405
+ // maximum length Windows allows a command line, so nothing real can wrap.
406
+ const r = await run("powershell.exe", [
407
+ "-NoProfile", "-NonInteractive", "-Command",
408
+ "Get-CimInstance Win32_Process | Select-Object -ExpandProperty CommandLine | Out-String -Width 32767",
409
+ ], { timeout: 8_000 });
410
+ if (!r.ok) return false; // no PowerShell, or the query was refused
411
+ return r.stdout.split("\n").some(isLoop);
412
+ }
413
+
414
+ // `ps`, not `pgrep -a`: BSD pgrep ignores -a and prints bare PIDs, so a
415
+ // command-line match against its output silently never fires.
416
+ const r = await run("ps", ["-Ao", "args="], { timeout: 5_000 });
417
+ if (!r.stdout.trim()) return false;
418
+ return r.stdout.split("\n").some(isLoop);
419
+ }
420
+
421
+ // ── deck-managed loop ──────────────────────────────────────────────────────
422
+
423
+ let _timer = null;
424
+ let _lastTick = null;
425
+ let _enabled = false;
426
+ // Set the instant startLoop is entered and cleared when it settles, because
427
+ // `_timer` cannot do that job: it is assigned AFTER an await, and the window in
428
+ // between is what #537 was. See startLoop.
429
+ let _starting = false;
430
+ // The tick in flight, so the interval can skip rather than stack. See tick.
431
+ let _ticking = null;
432
+
433
+ async function loadState() {
434
+ try { return JSON.parse(await readFile(STATE_PATH, "utf8")); } catch { return {}; }
435
+ }
436
+ async function saveState(state) {
437
+ try {
438
+ await mkdir(STATE_DIR, { recursive: true });
439
+ await writeFile(STATE_PATH, JSON.stringify(state, null, 2));
440
+ } catch { /* best-effort */ }
441
+ }
442
+
443
+ async function tickInterval() {
444
+ const cfg = await readCswapConfig();
445
+ const raw = Number(cfg?.["autoswitch.intervalSeconds"]?.value);
446
+ return Math.max(MIN_INTERVAL_S, Number.isFinite(raw) ? raw : 60) * 1000;
447
+ }
448
+
449
+ /**
450
+ * Everything the deck holds that belongs to ONE Claude account, dropped.
451
+ *
452
+ * The accounts roster is keyed on whichever account claude-swap says is active,
453
+ * and every quota percentage was read for whoever was active when it was
454
+ * collected. A switch makes both of them the wrong account's, and neither cache
455
+ * has any way to find that out for itself: they are refreshed on timers, by
456
+ * panels that were not told.
457
+ *
458
+ * The two caches decay at very different rates, which is why saying nothing was
459
+ * visibly wrong rather than briefly wrong. claude-accounts.mjs holds its roster
460
+ * for CACHE_MS = 5s, so the panel flips to the new account almost at once, while
461
+ * quota.mjs holds its result for a CACHE_MS of its own = 60s — and `_lastGood`
462
+ * outlives even that, coming back under a "stale" label every five seconds until
463
+ * the store has something to say about the account the deck moved TO. So for up
464
+ * to a minute, and for longer than that in the fallback, two panels on one screen
465
+ * described two different accounts, and the wrong one was the big quota bars:
466
+ * sitting at the 90% that triggered the switch, for an account nobody is on.
467
+ */
468
+ function forgetAccountScopedCaches() {
469
+ invalidateClaudeAccountsCache();
470
+ invalidateQuotaCache();
471
+ }
472
+
473
+ async function runTick() {
474
+ // Re-check each time: the user can start their own loop at any point, and
475
+ // the deck should fall silent rather than compete with it.
476
+ if (await externalAutoRunning()) {
477
+ _lastTick = { at: Date.now(), event: "skipped", reason: "external-engine" };
478
+ return;
479
+ }
480
+ const result = await runAutoTick();
481
+ // Before `_lastTick`, not after. This is the only path in the deck that moves
482
+ // the live account without a click behind it, so nothing else is in a position
483
+ // to make the call — and `_lastTick` is what /api/cswap-auto reports, so
484
+ // dropping the caches first means anything that can see the tick happened is
485
+ // already looking at caches that know about it.
486
+ //
487
+ // Only on a tick that actually switched. A tick is mostly a poll that decides
488
+ // to do nothing — cooldown, no candidates, nothing over the threshold — and
489
+ // invalidating on those would throw away readings the deck paid a subprocess
490
+ // for, every interval, forever.
491
+ if (result.switched) forgetAccountScopedCaches();
492
+ _lastTick = { at: Date.now(), ...result };
493
+ }
494
+
495
+ /**
496
+ * One tick at a time, whatever the interval is.
497
+ *
498
+ * The interval floor is 15 seconds (MIN_INTERVAL_S, and SETTINGS allows exactly
499
+ * that), while a single tick can legitimately take 8 for externalAutoRunning's
500
+ * `Get-CimInstance`/`ps` plus 120 for runAutoTick's own timeout. Nothing capped
501
+ * the fan-out, so a slow `cswap auto --once` — one that is refreshing a token
502
+ * and switching an account — could have eight copies of itself running against
503
+ * each other two minutes later, each with a PowerShell process beside it on
504
+ * Windows. `_lastTick` was then written by whichever finished last rather than
505
+ * by the most recent tick, so the panel's "last tick" could go backwards.
506
+ *
507
+ * A skipped tick is not a lost one: the next interval is at most 15 seconds
508
+ * away, and the work this schedules is idempotent by design.
509
+ */
510
+ function tick() {
511
+ if (_ticking) return _ticking;
512
+ _ticking = runTick().finally(() => { _ticking = null; });
513
+ return _ticking;
514
+ }
515
+
516
+ /**
517
+ * Start the deck-managed loop, at most once.
518
+ *
519
+ * `if (_timer) return` looked like a guard and was not one: `_timer` is assigned
520
+ * after `await tickInterval()`, which shells out to `cswap config`, so two
521
+ * callers could both be past the check before either had set it. Two ways in
522
+ * during that window, both reachable from the UI:
523
+ *
524
+ * - enable then disable, a few hundred milliseconds apart. The disable set
525
+ * `_enabled = false` and called stopLoop, which cleared nothing because
526
+ * `_timer` was still null — and then the enable came back and installed the
527
+ * interval. autoStatus() reported `enabled: false` and the toggle read off
528
+ * while every tick went on running `cswap auto --once`, which switches the
529
+ * user's live Claude account. A control that says it is off while it moves
530
+ * credentials is the worst shape this bug could take.
531
+ *
532
+ * - two enables (a double click, or two tabs). Two intervals, only the second
533
+ * reachable from `_timer`, so the first could never be cleared again for the
534
+ * life of the process.
535
+ *
536
+ * initCswapAuto is a third way in: index.mjs fires it unawaited while the server
537
+ * is already accepting requests.
538
+ *
539
+ * `_starting` is set before the await, so the guard covers the whole function.
540
+ * `_enabled` is re-read after it, because the answer may have changed while this
541
+ * was waiting on a subprocess — and a loop that installs itself after the user
542
+ * has turned it off is the same defect from the other side.
543
+ */
544
+ async function startLoop() {
545
+ if (_timer || _starting) return;
546
+ _starting = true;
547
+ try {
548
+ const ms = await tickInterval();
549
+ if (!_enabled) return; // turned off while we were asking cswap
550
+ _timer = setInterval(() => { tick().catch(() => {}); }, ms);
551
+ _timer.unref?.();
552
+ } finally {
553
+ _starting = false;
554
+ }
555
+ tick().catch(() => {}); // don't make the user wait a full interval for the first one
556
+ }
557
+
558
+ function stopLoop() {
559
+ if (_timer) { clearInterval(_timer); _timer = null; }
560
+ }
561
+
562
+ /** Turn the deck-managed loop on or off, persisting the choice. */
563
+ export async function setAutoEnabled(enabled) {
564
+ _enabled = Boolean(enabled);
565
+ await saveState({ ...(await loadState()), enabled: _enabled });
566
+ if (_enabled) await startLoop(); else stopLoop();
567
+ return { ok: true, enabled: _enabled };
568
+ }
569
+
570
+ /** Restore the persisted setting at server boot. */
571
+ export async function initCswapAuto() {
572
+ const state = await loadState();
573
+ if (state.enabled) { _enabled = true; await startLoop(); }
574
+ }
575
+
576
+ export async function autoStatus() {
577
+ const [config, external] = await Promise.all([readCswapConfig(), externalAutoRunning()]);
578
+ return {
579
+ ok: config != null,
580
+ enabled: _enabled,
581
+ external, // user is running their own `cswap auto`
582
+ lastTick: _lastTick,
583
+ settings: config ?? {},
584
+ };
585
+ }
586
+
587
+ // ── per-account rotation flag ──────────────────────────────────────────────
588
+
589
+ /** Hold an account out of auto-rotation, or return it. */
590
+ export async function setAccountEnabled(accountNum, enabled) {
591
+ const num = Number(accountNum);
592
+ if (!Number.isInteger(num) || num < 1 || num > 999) return { ok: false, reason: "bad_account" };
593
+ const r = await run(await cswapBin(), [enabled ? "enable" : "disable", String(num)]);
594
+ return r.ok ? { ok: true } : { ok: false, reason: "command_failed", detail: (r.stderr || r.stdout).trim().slice(0, 300) };
595
+ }