ccdeck 1.43.0 → 1.44.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.
@@ -0,0 +1,583 @@
1
+ // Ensures `cswap` (claude-swap) is available, because the accounts panel is
2
+ // built entirely on the store it maintains.
3
+ //
4
+ // Unlike the ccusage install, this one lands in the user's GLOBAL tool path
5
+ // rather than a private prefix under ~/.agents-deck, and claude-swap handles
6
+ // Claude credentials. That makes it something the user should see happen: the
7
+ // caller prints what this returns, and AGENTS_DECK_NO_INSTALL=1 turns it off
8
+ // entirely. It is always best-effort — the deck's core function does not
9
+ // depend on it, so a failure is reported and then ignored.
10
+ import { run, runDetached } from "./exec.mjs";
11
+ // The version comparator was written out here as well, identical apart from a
12
+ // type guard this copy lacked, and only the self-update one was under test
13
+ // (#374). No cycle: self-update.mjs imports node:* and ./exec.mjs, which this
14
+ // file already imports itself.
15
+ import { isOlder } from "./self-update.mjs";
16
+ import { bootstrapUv, existingBootstrappedUv } from "./uv-bootstrap.mjs";
17
+ import { existsSync, mkdirSync, readdirSync, realpathSync, statSync, writeFileSync } from "node:fs";
18
+ import { join, posix as posixPath, win32 as winPath } from "node:path";
19
+ import { homedir } from "node:os";
20
+
21
+ const INSTALL_TIMEOUT_MS = 180_000; // uv resolves + builds a Python env
22
+
23
+ // Same throttle the ccusage installer uses: check once a day, tracked by a
24
+ // marker file's mtime so the interval survives restarts.
25
+ const UPDATE_CHECK_MS = 24 * 3600_000;
26
+ const MARKER = join(homedir(), ".agents-deck", ".cswap-update-check");
27
+
28
+ /**
29
+ * The subdirectories of `dir`, newest-looking first, or [] when it cannot be
30
+ * read at all.
31
+ *
32
+ * Injected into cswapCandidates below rather than called from it, for the reason
33
+ * the platform is a parameter there: a Windows layout has to be describable from
34
+ * a Mac. A missing directory, a disconnected network drive and a profile the
35
+ * process cannot read are all the same answer — nothing here — never a throw,
36
+ * because this runs unprompted at startup for an optional panel.
37
+ *
38
+ * The sort is numeric so `Python313` sorts above `Python39` rather than below
39
+ * it: when two interpreters both have a cswap, the newer one is the one the user
40
+ * most likely installed it with, and the order this returns is the order the
41
+ * caller probes in.
42
+ *
43
+ * Exported for its test rather than for a caller. Every part of it that can be
44
+ * wrong — which names count as an interpreter, what order they come back in,
45
+ * what a directory that cannot be read answers — is invisible from
46
+ * cswapCandidates, which injects a substitute precisely so its own Windows
47
+ * layout can be checked from a Mac. Driven against real directories in
48
+ * cswap-admin.test.ts, on whichever OS is running the suite.
49
+ */
50
+ export function pythonVersionDirs(dir) {
51
+ try {
52
+ return readdirSync(dir, { withFileTypes: true })
53
+ .filter(e => e.isDirectory() || e.isSymbolicLink())
54
+ .map(e => e.name)
55
+ // `Python312`, and the tagged builds the installer also writes:
56
+ // `Python312-32`, `Python313-arm64`.
57
+ .filter(n => /^Python\d[\w.-]*$/i.test(n))
58
+ .sort((a, b) => b.localeCompare(a, "en", { numeric: true }));
59
+ } catch {
60
+ return [];
61
+ }
62
+ }
63
+
64
+ /**
65
+ * How to invoke cswap: the bare name when PATH resolves it, otherwise an
66
+ * absolute path to where its installers actually put it.
67
+ *
68
+ * ~/.local/bin is where both `uv tool install` and `pipx install` place
69
+ * executables, on every platform, and it is famously not on PATH — that is the
70
+ * whole reason `pipx ensurepath` exists. Installing claude-swap successfully
71
+ * and then reporting it as missing because the shell cannot see it is a bad
72
+ * enough outcome on its own; it is worse now that the deck may have done the
73
+ * installing. So PATH is a convenience here, not the source of truth.
74
+ *
75
+ * Re-resolved when a lookup fails so an install during this process is picked
76
+ * up without a restart.
77
+ */
78
+ /**
79
+ * Every place an installer is known to leave cswap. The platform is a parameter
80
+ * and the directory listing is injected, so the Windows list can be checked from
81
+ * a Mac — which is the only way this list stays right, since it exists entirely
82
+ * for machines the author is not sitting at.
83
+ */
84
+ export function cswapCandidates(platform = process.platform, env = process.env, home = homedir(), {
85
+ versionDirs = pythonVersionDirs,
86
+ } = {}) {
87
+ // The path flavour follows the PLATFORM ARGUMENT, not the host: node's `join`
88
+ // would emit forward slashes when this is exercised from a Mac, which is both
89
+ // wrong for the caller and invisible in a test.
90
+ const { join } = platform === "win32" ? winPath : posixPath;
91
+ const exe = platform === "win32" ? "cswap.exe" : "cswap";
92
+ const dirs = [];
93
+ // Explicit configuration first: someone who set these means them.
94
+ if (env.UV_TOOL_BIN_DIR) dirs.push(env.UV_TOOL_BIN_DIR);
95
+ if (env.XDG_BIN_HOME) dirs.push(env.XDG_BIN_HOME);
96
+ // Where `uv tool install` and `pipx install` put executables, everywhere.
97
+ dirs.push(join(home, ".local", "bin"));
98
+ if (platform === "win32") {
99
+ // pipx before 1.5, and any `pip install --user`. APPDATA is respected when
100
+ // set because a roaming profile moves it off the home directory.
101
+ //
102
+ // THE VERSION SEGMENT IS NOT OPTIONAL (#552). CPython on Windows always
103
+ // puts the interpreter between the root and `Scripts`:
104
+ //
105
+ // %APPDATA%\Python\Python312\Scripts pip install --user
106
+ // %LOCALAPPDATA%\Programs\Python\Python312\Scripts per-user installer
107
+ //
108
+ // — `{userbase}\Python{version_nodot}\Scripts` is sysconfig's `nt_user`
109
+ // scheme, not a convention. The two paths this used to build omitted it, so
110
+ // NEITHER could exist on a real machine: every candidate missed,
111
+ // `cswapVersion` answered null, `ensureCswap` reported `not_on_path`, and
112
+ // the deck re-ran a whole install attempt on every launch for a user who
113
+ // already had cswap.exe sitting there. The POSIX side never had the bug —
114
+ // `~/.local/bin` carries no version — which is why this stayed a Windows
115
+ // false negative in the one function whose whole purpose is to not depend
116
+ // on PATH.
117
+ //
118
+ // Which versions exist is a fact about the machine, so it is read rather
119
+ // than guessed: enumerating Python38…Python315 would be eight wrong paths
120
+ // and a ninth wrong one next year.
121
+ const appData = env.APPDATA || join(home, "AppData", "Roaming");
122
+ const localAppData = env.LOCALAPPDATA || join(home, "AppData", "Local");
123
+ for (const root of [join(appData, "Python"), join(localAppData, "Programs", "Python")]) {
124
+ for (const version of versionDirs(root)) dirs.push(join(root, version, "Scripts"));
125
+ }
126
+ dirs.push(join(home, "scoop", "shims"));
127
+ } else {
128
+ dirs.push(join(home, ".pyenv", "shims"));
129
+ // uv keeps the tool's own venv here and only symlinks into ~/.local/bin; if
130
+ // that link was never made, this is still a working executable.
131
+ dirs.push(join(home, ".local", "share", "uv", "tools", "claude-swap", "bin"));
132
+ dirs.push("/opt/homebrew/bin", "/usr/local/bin");
133
+ }
134
+ return dirs.map(d => join(d, exe));
135
+ }
136
+
137
+ // The distribution name, which is what both installers key their directories on
138
+ // — `cswap` is only the console script.
139
+ const PKG = "claude-swap";
140
+
141
+ /** True when `child` is `dir` or lives under it, in `platform`'s path flavour. */
142
+ function underDir(child, dir, platform) {
143
+ const { sep, normalize } = platform === "win32" ? winPath : posixPath;
144
+ const norm = p => {
145
+ // Windows paths compare case-insensitively, and `C:\x\` and `C:\x` are one
146
+ // directory.
147
+ let s = normalize(String(p));
148
+ if (platform === "win32") s = s.toLowerCase();
149
+ return s.length > 1 && s.endsWith(sep) ? s.slice(0, -sep.length) : s;
150
+ };
151
+ const c = norm(child), d = norm(dir);
152
+ return c === d || c.startsWith(d + sep);
153
+ }
154
+
155
+ /**
156
+ * Where `uv tool install claude-swap` puts the tool's own venv.
157
+ *
158
+ * UV_TOOL_DIR wins outright; otherwise uv's persistent data directory, which is
159
+ * `$XDG_DATA_HOME/uv` or `~/.local/share/uv` on Unix — macOS included, uv does
160
+ * not use `~/Library` — and `%APPDATA%\uv\data` on Windows. The `data` segment
161
+ * is Windows-only and is not optional there.
162
+ */
163
+ function uvToolVenvs(platform, env, home) {
164
+ const { join } = platform === "win32" ? winPath : posixPath;
165
+ if (env.UV_TOOL_DIR) return [join(env.UV_TOOL_DIR, PKG)];
166
+ if (platform === "win32") {
167
+ const appData = env.APPDATA || join(home, "AppData", "Roaming");
168
+ return [join(appData, "uv", "data", "tools", PKG)];
169
+ }
170
+ return [join(env.XDG_DATA_HOME || join(home, ".local", "share"), "uv", "tools", PKG)];
171
+ }
172
+
173
+ /**
174
+ * Where `pipx install claude-swap` puts the package's venv.
175
+ *
176
+ * Read off pipx's own `paths.py`: the venvs are always `<home>/venvs`, and the
177
+ * home is PIPX_HOME when set, else the first EXISTING legacy fallback
178
+ * (`~/.local/pipx`, plus `~/pipx` on Windows), else platformdirs'
179
+ * `user_data_path("pipx")`. Since what gets asked here is whether one specific
180
+ * venv is on disk, every candidate home can simply be tried rather than
181
+ * replaying pipx's precedence.
182
+ *
183
+ * platformdirs on Windows appends the app name twice when no author is given,
184
+ * which pipx does not give — `%LOCALAPPDATA%\pipx\pipx`, not `%LOCALAPPDATA%\
185
+ * pipx`. That doubled segment is real and is the whole path on a modern
186
+ * Windows pipx.
187
+ */
188
+ function pipxVenvs(platform, env, home) {
189
+ const { join } = platform === "win32" ? winPath : posixPath;
190
+ if (env.PIPX_HOME) return [join(env.PIPX_HOME, "venvs", PKG)];
191
+ const homes = [join(home, ".local", "pipx")];
192
+ if (platform === "win32") {
193
+ homes.push(join(home, "pipx"));
194
+ homes.push(join(env.LOCALAPPDATA || join(home, "AppData", "Local"), "pipx", "pipx"));
195
+ } else if (platform === "darwin") {
196
+ homes.push(join(home, "Library", "Application Support", "pipx"));
197
+ } else {
198
+ homes.push(join(env.XDG_DATA_HOME || join(home, ".local", "share"), "pipx"));
199
+ }
200
+ return homes.map(h => join(h, "venvs", PKG));
201
+ }
202
+
203
+ function realpathOrSelf(p) {
204
+ try { return realpathSync(p); } catch { return p; }
205
+ }
206
+
207
+ /**
208
+ * Which installer OWNS the claude-swap this machine runs — "uv", "pipx", or
209
+ * null when nothing offered here does, or when the evidence is ambiguous.
210
+ *
211
+ * The daily upgrade used to be handed to `findInstaller()`, which returns the
212
+ * first tool that answers `--version`. That is the right question when choosing
213
+ * something to install WITH and the wrong one when upgrading something already
214
+ * installed: on a machine with uv present and claude-swap installed some other
215
+ * way — a `pip install --user` copy, which #574 taught cswapBin to find, or a
216
+ * pipx one — the upgrade went to uv, which answers
217
+ *
218
+ * error: Failed to upgrade claude-swap
219
+ * Caused by: `claude-swap` is not installed; run `uv tool install …`
220
+ *
221
+ * and pipx, given someone else's package, answers "Package is not installed.
222
+ * Expected to find <PIPX_HOME>/venvs/claude-swap, but it does not exist." Both
223
+ * go through runDetached, which reads no output and waits for no exit, so the
224
+ * refusal reached nobody while ensureCswap still reported "upgrading" and the
225
+ * marker was already burned for the day. The version never moved and the deck
226
+ * said it was moving, every launch, forever.
227
+ *
228
+ * Two signals, strongest first. The executable the deck actually runs, with its
229
+ * symlinks followed, sitting inside one installer's directory is decisive —
230
+ * that is the POSIX case, where both installers link `~/.local/bin/cswap` at
231
+ * their own venv. Windows copies the launcher instead, so there the layout
232
+ * question is asked directly: exactly one of the two venv directories exists.
233
+ * Zero means nothing offered here owns it — a `pip install --user` copy is the
234
+ * common shape, and `installers()` deliberately refuses to offer bare pip — and
235
+ * two means the machine has both and the resolved path did not say which is on
236
+ * PATH. Both answer null, because a silent boot is better than a daily sentence
237
+ * that is not true.
238
+ *
239
+ * Pure, and platform/env/home/filesystem all arrive as arguments, for the reason
240
+ * cswapCandidates gives: a Windows layout has to be checkable from a Mac.
241
+ */
242
+ export function cswapOwner(bin, platform = process.platform, env = process.env, home = homedir(), {
243
+ exists = existsSync,
244
+ realpath = realpathOrSelf,
245
+ } = {}) {
246
+ const roots = [
247
+ ...uvToolVenvs(platform, env, home).map(dir => ({ owner: "uv", dir })),
248
+ ...pipxVenvs(platform, env, home).map(dir => ({ owner: "pipx", dir })),
249
+ ];
250
+
251
+ // cswapBin answers the bare word whenever PATH resolved it, and a bare word
252
+ // points at no layout at all — only a path can be followed.
253
+ if (typeof bin === "string" && /[\\/]/.test(bin)) {
254
+ // BOTH sides get resolved, or the comparison is between two spellings of one
255
+ // directory rather than between two directories. A symlinked home is the
256
+ // ordinary way that happens — /var → /private/var on macOS, a network or
257
+ // container-mounted profile on Linux — and it would silently turn the
258
+ // strongest signal here into no signal at all.
259
+ const real = realpath(bin);
260
+ const hit = roots.find(r => underDir(real, realpath(r.dir), platform));
261
+ if (hit) return hit.owner;
262
+ }
263
+
264
+ const owners = new Set(roots.filter(r => exists(r.dir)).map(r => r.owner));
265
+ return owners.size === 1 ? [...owners][0] : null;
266
+ }
267
+
268
+ let _bin = null;
269
+ export async function cswapBin() {
270
+ // An explicit path wins over everything and is never cached away — someone
271
+ // debugging a bad resolution needs it to take effect immediately.
272
+ if (process.env.AGENTS_DECK_CSWAP) return process.env.AGENTS_DECK_CSWAP;
273
+ if (_bin) return _bin;
274
+ if ((await run("cswap", ["--version"], { timeout: 8_000 })).ok) return (_bin = "cswap");
275
+
276
+ for (const c of cswapCandidates()) {
277
+ if (existsSync(c) && (await run(c, ["--version"], { timeout: 8_000 })).ok) return (_bin = c);
278
+ }
279
+ return "cswap"; // not found; leave the bare name so errors read sensibly
280
+ }
281
+
282
+ /**
283
+ * Forget the cached resolution — call after installing.
284
+ *
285
+ * WHAT IS ACTUALLY STALE HERE (#383). Note that `cswapBin` above memoizes only
286
+ * SUCCESS: the "not found" answer is the bare name returned without ever being
287
+ * written to `_bin`, so a lookup that found nothing is already re-run on the
288
+ * next call and there is no negative result for this to clear. What it clears is
289
+ * a positive one — a path that resolved, was cached, and has since been
290
+ * superseded. That is not hypothetical on the install path: `cswapVersion` runs
291
+ * `--version` a SECOND time under the default timeout after `cswapBin` has
292
+ * already cached the copy its own 8s probe accepted, so a copy that is present
293
+ * but too slow, half-written or broken caches a path and still reports no
294
+ * version — and the install that follows lands a working cswap somewhere the
295
+ * cached path may not point at. Clearing is cheap; a deck driving the wrong
296
+ * binary for the life of the process is not.
297
+ *
298
+ * Exported for its test rather than for a caller (#383): the one caller is
299
+ * ensureCswap below, whose return value says nothing about which binary the
300
+ * following twenty account operations will be sent to. See cswap-bin-memo.test.ts.
301
+ */
302
+ export function resetCswapBin() { _bin = null; }
303
+
304
+ /** Installed version string, or null when cswap cannot be found. */
305
+ export async function cswapVersion() {
306
+ const r = await run(await cswapBin(), ["--version"]);
307
+ if (!r.ok) return null;
308
+ // "claude-swap 0.25.0" → "0.25.0"
309
+ const m = (r.stdout || r.stderr).trim().match(/(\d+\.\d+\.\d+\S*)/);
310
+ return m ? m[1] : "installed";
311
+ }
312
+
313
+ /**
314
+ * Install claude-swap with whichever Python tool installer is present.
315
+ *
316
+ * `uv` first because it is what claude-swap documents and it is dramatically
317
+ * faster; `pipx` as the established alternative. Deliberately NOT falling back
318
+ * to bare `pip install --user`: that drops the package into the user's default
319
+ * Python environment where it can collide with their own dependencies, which
320
+ * is not a thing to do to someone without asking.
321
+ */
322
+ /**
323
+ * Python interpreters that are safe to execute on this machine.
324
+ *
325
+ * Both desktop platforms ship a fake python that does something other than run
326
+ * python when none is installed, and this code runs unprompted at startup for
327
+ * an optional panel — so neither may be executed on spec.
328
+ *
329
+ * macOS: /usr/bin/python3 is a shim that opens the "install developer tools?"
330
+ * dialog. `xcode-select -p` says whether the real thing is behind it, and is
331
+ * itself only a path lookup.
332
+ *
333
+ * Windows: `python` and `python3` are App Execution Aliases under
334
+ * WindowsApps — zero-length reparse points that open the Microsoft Store. They
335
+ * are on PATH whether or not Python exists, so presence proves nothing and
336
+ * running one opens the Store. `where` reports the paths without executing
337
+ * anything, so the aliases can be filtered out and the real interpreter (if
338
+ * any) called by absolute path. `py`, the Python launcher, only exists when
339
+ * Python was actually installed and is safe as-is.
340
+ *
341
+ * Returns absolute paths or bare command names, best first; empty when there
342
+ * is nothing safe to run.
343
+ */
344
+ let _pythons = null;
345
+ async function safePythons() {
346
+ if (_pythons != null) return _pythons;
347
+
348
+ if (process.platform === "darwin") {
349
+ _pythons = (await run("xcode-select", ["-p"], { timeout: 5_000 })).ok
350
+ ? ["python3", "python"]
351
+ : [];
352
+ return _pythons;
353
+ }
354
+
355
+ if (process.platform !== "win32") {
356
+ _pythons = ["python3", "python"];
357
+ return _pythons;
358
+ }
359
+
360
+ const found = [];
361
+ // The launcher first: it is never an alias.
362
+ if ((await run("py", ["-0"], { timeout: 8_000 })).ok) found.push("py");
363
+ for (const name of ["python", "python3"]) {
364
+ const r = await run("where", [name], { timeout: 8_000 });
365
+ if (!r.ok) continue;
366
+ for (const line of r.stdout.split(/\r?\n/)) {
367
+ const p = line.trim();
368
+ if (!p || /\\WindowsApps\\/i.test(p)) continue; // Store alias, not an interpreter
369
+ found.push(p);
370
+ break;
371
+ }
372
+ }
373
+ _pythons = found;
374
+ return _pythons;
375
+ }
376
+
377
+ /**
378
+ * Ways to install a Python application, best first.
379
+ *
380
+ * `python -m pipx` matters more than it looks: pipx is very often present as a
381
+ * module without a `pipx` on PATH — every Debian/Ubuntu `apt install pipx`, and
382
+ * any `pip install --user pipx` where ~/.local/bin was never added to PATH. The
383
+ * two-entry version of this list reported "needs uv or pipx" to people who had
384
+ * pipx installed, which is the kind of wrong answer that stops someone looking.
385
+ *
386
+ * Every entry carries its UPGRADE command line as well as its install one. The
387
+ * upgrade used to be re-derived from the `via` label instead, and any label that
388
+ * derivation did not recognise fell through to `-m pipx upgrade` — so the
389
+ * bundled uv, which is the only installer present on a machine that had neither
390
+ * uv nor pipx nor a usable python, was asked to run a pipx command it rejects.
391
+ *
392
+ * Every entry also carries the `owner` it speaks for, which is what an upgrade
393
+ * is matched against. It is a field rather than something read back off `via`
394
+ * because deriving behaviour from that label is precisely what went wrong the
395
+ * first time: three of these five spellings are one uv and two are one pipx,
396
+ * and a fourth spelling arriving later must not silently mean "pipx" by
397
+ * default. Several entries can share an owner — the bundled uv upgrades what
398
+ * the system uv installed and vice versa, since both read UV_TOOL_DIR — so the
399
+ * probe still decides WHICH of an owner's spellings runs.
400
+ */
401
+ async function installers() {
402
+ const out = [
403
+ { cmd: "uv", probe: ["--version"], args: ["tool", "install", "claude-swap"], upgrade: ["tool", "upgrade", "claude-swap"], via: "uv", owner: "uv" },
404
+ { cmd: "pipx", probe: ["--version"], args: ["install", "claude-swap"], upgrade: ["upgrade", "claude-swap"], via: "pipx", owner: "pipx" },
405
+ ];
406
+ // A uv fetched on an earlier run counts as installed tooling from here on.
407
+ const own = existingBootstrappedUv();
408
+ if (own) {
409
+ out.push({
410
+ cmd: own,
411
+ probe: ["--version"],
412
+ args: ["tool", "install", "claude-swap"],
413
+ upgrade: ["tool", "upgrade", "claude-swap"],
414
+ via: "uv (bundled)",
415
+ owner: "uv",
416
+ });
417
+ }
418
+ for (const py of await safePythons()) {
419
+ out.push({
420
+ cmd: py,
421
+ probe: ["-m", "pipx", "--version"],
422
+ args: ["-m", "pipx", "install", "claude-swap"],
423
+ upgrade: ["-m", "pipx", "upgrade", "claude-swap"],
424
+ via: `${py} -m pipx`,
425
+ owner: "pipx",
426
+ });
427
+ }
428
+ return out;
429
+ }
430
+
431
+ async function installCswap() {
432
+ for (const { cmd, probe, args, via } of await installers()) {
433
+ if (!(await run(cmd, probe, { timeout: 8_000 })).ok) continue;
434
+ const r = await run(cmd, args, { timeout: INSTALL_TIMEOUT_MS });
435
+ if (r.ok) return { ok: true, via };
436
+ return { ok: false, reason: "install_failed", via, detail: (r.stderr || r.stdout).trim().slice(0, 300) };
437
+ }
438
+
439
+ // Nothing on the machine can install a Python application. Rather than hand
440
+ // the user a command and stop, fetch uv itself — verified, and into the
441
+ // deck's own directory, see uv-bootstrap.mjs — and use that.
442
+ const boot = await bootstrapUv();
443
+ if (!boot.ok) return { ok: false, reason: "no_installer", bootstrap: boot.reason, hint: await installHint() };
444
+
445
+ const r = await run(boot.bin, ["tool", "install", "claude-swap"], { timeout: INSTALL_TIMEOUT_MS });
446
+ if (r.ok) return { ok: true, via: `uv ${boot.version} (fetched)` };
447
+ return { ok: false, reason: "install_failed", via: "uv (fetched)", detail: (r.stderr || r.stdout).trim().slice(0, 300) };
448
+ }
449
+
450
+ /**
451
+ * What to actually type, for this machine.
452
+ *
453
+ * "needs uv or pipx" is a dead end for the person who has neither and no
454
+ * opinion about Python packaging — which is most people running a Node CLI.
455
+ * uv is a single self-contained binary and is what claude-swap documents, so
456
+ * that is what gets recommended; if the machine already has Python, pipx via
457
+ * pip is offered instead because it uses something already installed.
458
+ */
459
+ export async function installHint() {
460
+ const uv = process.platform === "win32"
461
+ ? 'powershell -c "irm https://astral.sh/uv/install.ps1 | iex" (then: uv tool install claude-swap)'
462
+ : "curl -LsSf https://astral.sh/uv/install.sh | sh (then: uv tool install claude-swap)";
463
+ for (const py of await safePythons()) {
464
+ if ((await run(py, ["-c", "import sys"], { timeout: 5_000 })).ok) {
465
+ // Quoted: a resolved Windows path routinely contains spaces.
466
+ const q = /\s/.test(py) ? `"${py}"` : py;
467
+ return `${q} -m pip install --user pipx && ${q} -m pipx install claude-swap`;
468
+ }
469
+ }
470
+ return uv;
471
+ }
472
+
473
+ function updateCheckDue() {
474
+ try { return Date.now() - statSync(MARKER).mtimeMs > UPDATE_CHECK_MS; }
475
+ catch { return true; } // no marker yet
476
+ }
477
+ function touchMarker() {
478
+ try {
479
+ mkdirSync(join(homedir(), ".agents-deck"), { recursive: true });
480
+ writeFileSync(MARKER, String(Date.now()));
481
+ } catch { /* ignore */ }
482
+ }
483
+
484
+ /** Newest claude-swap on PyPI, or null if the check fails. */
485
+ async function latestOnPypi() {
486
+ try {
487
+ const res = await fetch("https://pypi.org/pypi/claude-swap/json", {
488
+ signal: AbortSignal.timeout(6_000),
489
+ });
490
+ if (!res.ok) return null;
491
+ const v = (await res.json())?.info?.version;
492
+ return typeof v === "string" ? v : null;
493
+ } catch {
494
+ return null;
495
+ }
496
+ }
497
+
498
+ /**
499
+ * Upgrade claude-swap in the background when a newer release exists.
500
+ *
501
+ * Detached and unawaited: an upgrade resolves a Python environment and can
502
+ * take tens of seconds, which is not a thing to put in front of the server
503
+ * starting. The running copy keeps working; the new one is there next launch.
504
+ *
505
+ * The command line comes from the installer entry rather than from its label,
506
+ * and the ENTRY comes from cswapOwner rather than from whichever tool answers a
507
+ * probe first: runDetached captures nothing, so an upgrade aimed at the wrong
508
+ * tool fails where nobody can see it while the caller still reports
509
+ * "upgrading". Those are the two halves of "aimed at the wrong tool" — a right
510
+ * argv sent to a tool that does not own the package is just as invisible as a
511
+ * wrong argv, and was the longer-lived of the two.
512
+ */
513
+ function upgradeInBackground({ cmd, upgrade }) {
514
+ runDetached(cmd, upgrade);
515
+ }
516
+
517
+ /**
518
+ * A runnable spelling of the installer that OWNS this claude-swap, or null.
519
+ *
520
+ * The owner is decided from the install layout before anything is probed, and
521
+ * only that owner's entries are then tried — so a uv sitting on a machine whose
522
+ * claude-swap came from pipx is skipped rather than handed an upgrade it will
523
+ * refuse. A null owner probes nothing at all: there is no tool here to ask.
524
+ */
525
+ async function findUpgrader(owner) {
526
+ if (!owner) return null;
527
+ for (const { cmd, probe, upgrade, via, owner: speaksFor } of await installers()) {
528
+ if (speaksFor !== owner) continue;
529
+ if ((await run(cmd, probe, { timeout: 8_000 })).ok) return { cmd, upgrade, via };
530
+ }
531
+ return null;
532
+ }
533
+
534
+ /**
535
+ * Make sure cswap exists and is reasonably current, installing it if missing.
536
+ *
537
+ * Returns a small status the CLI prints verbatim:
538
+ * { state: "present" | "installed" | "upgrading" | "skipped" | "unavailable", ... }
539
+ */
540
+ export async function ensureCswap() {
541
+ if (process.env.AGENTS_DECK_NO_INSTALL === "1") {
542
+ const version = await cswapVersion();
543
+ return version ? { state: "present", version } : { state: "skipped" };
544
+ }
545
+
546
+ const existing = await cswapVersion();
547
+ if (existing) {
548
+ // Installed — the only question left is whether it's stale. One PyPI
549
+ // request a day, and the upgrade itself never blocks startup.
550
+ if (!updateCheckDue()) return { state: "present", version: existing };
551
+ touchMarker();
552
+ const latest = await latestOnPypi();
553
+ if (latest && existing !== "installed" && isOlder(existing, latest)) {
554
+ // Who owns it, not what is installed on the machine: an upgrade aimed at
555
+ // a tool that never installed this package is refused where runDetached
556
+ // cannot see it, and "upgrading" would then be a sentence printed daily
557
+ // about nothing. When nobody offered here owns it, "present" is the whole
558
+ // truth and is what gets said.
559
+ const found = await findUpgrader(cswapOwner(await cswapBin()));
560
+ if (found) {
561
+ upgradeInBackground(found);
562
+ return { state: "upgrading", version: existing, latest, via: found.via };
563
+ }
564
+ }
565
+ return { state: "present", version: existing };
566
+ }
567
+
568
+ const result = await installCswap();
569
+ if (!result.ok) return { state: "unavailable", ...result };
570
+ // Something was just installed, so any path resolved before it is a guess made
571
+ // against a different filesystem. Nothing is cached when the earlier lookup
572
+ // found nothing — see resetCswapBin — but a lookup that DID resolve, to a copy
573
+ // whose `--version` then failed, is exactly the case that got us here.
574
+ resetCswapBin();
575
+
576
+ // Freshly installed tools land in ~/.local/bin, which may not be on the PATH
577
+ // of the shell that launched us — cswapBin looks there directly, so this
578
+ // confirms the install rather than confirming the user's PATH.
579
+ const version = await cswapVersion();
580
+ return version
581
+ ? { state: "installed", via: result.via, version }
582
+ : { state: "unavailable", reason: "not_on_path", via: result.via };
583
+ }