@carrieros/cli-staging 0.4.0 → 0.4.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/dist/commands/chat.js +3 -3
- package/dist/commands/logout.js +1 -0
- package/dist/commands/permissions.js +1 -0
- package/dist/commands/settings.js +61 -1
- package/dist/commands/update.js +102 -46
- package/dist/commands/usage.js +5 -3
- package/dist/ide/ide-session.js +10 -6
- package/dist/main.js +6 -0
- package/dist/services/api.js +5 -3
- package/dist/services/auto-update.js +221 -0
- package/dist/services/config.js +2 -1
- package/dist/services/context.js +53 -8
- package/dist/services/device-auth.js +3 -1
- package/dist/services/login-flow.js +3 -3
- package/dist/services/meta.js +27 -0
- package/dist/services/session-resolver.js +2 -0
- package/dist/services/update.js +420 -1
- package/dist/shell/shell-chat.js +3 -0
- package/dist/shell/shell-commands.js +1 -1
- package/dist/shell/shell.js +44 -19
- package/dist/ui/command-registry.js +2 -2
- package/package.json +1 -1
package/dist/services/update.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { spawnSync } from "node:child_process";
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
@@ -309,3 +309,422 @@ export function refreshUpdateCache(cache, now = Date.now()) {
|
|
|
309
309
|
cache.set("updateCheckErrorAt", now);
|
|
310
310
|
}
|
|
311
311
|
}
|
|
312
|
+
// ── Channels, installation kinds, background installs ───────────────────────
|
|
313
|
+
// Everything below is async (child_process.spawn), so a check or install never
|
|
314
|
+
// blocks the composer, and takes its npm runner from one injectable seam so the
|
|
315
|
+
// tests never touch the real registry or a real global install.
|
|
316
|
+
/** npm dist-tags a CLI may follow. Never chosen from remote data. */
|
|
317
|
+
export const UPDATE_CHANNELS = ["latest", "beta", "next", "staging"];
|
|
318
|
+
export function isUpdateChannel(v) {
|
|
319
|
+
return typeof v === "string" && UPDATE_CHANNELS.includes(v);
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Packages this CLI may ever install. The name comes from the CLI's OWN
|
|
323
|
+
* package.json, so the staging build (@carrieros/cli-staging) only ever updates
|
|
324
|
+
* itself and can never pull the production package over a staging install.
|
|
325
|
+
*/
|
|
326
|
+
const ALLOWED_PACKAGES = new Set(["@carrieros/cli", "@carrieros/cli-staging"]);
|
|
327
|
+
export function packageName() {
|
|
328
|
+
try {
|
|
329
|
+
const pkgUrl = new URL("../../package.json", import.meta.url);
|
|
330
|
+
const name = JSON.parse(fs.readFileSync(fileURLToPath(pkgUrl), "utf8")).name;
|
|
331
|
+
if (name && ALLOWED_PACKAGES.has(name))
|
|
332
|
+
return name;
|
|
333
|
+
}
|
|
334
|
+
catch {
|
|
335
|
+
/* packaged executable: no package.json on disk */
|
|
336
|
+
}
|
|
337
|
+
return PACKAGE_NAME;
|
|
338
|
+
}
|
|
339
|
+
/** The isolated staging package must follow its own non-latest npm dist-tag. */
|
|
340
|
+
export function defaultUpdateChannel(name = packageName()) {
|
|
341
|
+
return name === "@carrieros/cli-staging" ? "staging" : "latest";
|
|
342
|
+
}
|
|
343
|
+
/** The manual command for a channel — what every failure message offers. */
|
|
344
|
+
export function manualUpdateCommand(channel = defaultUpdateChannel(), name = packageName()) {
|
|
345
|
+
return `npm install -g ${name}@${channel}`;
|
|
346
|
+
}
|
|
347
|
+
/** Output cap: npm can print a lot, and only the tail is ever shown. */
|
|
348
|
+
const MAX_OUTPUT = 64 * 1024;
|
|
349
|
+
const spawnNpm = (args, timeoutMs) => new Promise((resolve) => {
|
|
350
|
+
let stdout = "";
|
|
351
|
+
let stderr = "";
|
|
352
|
+
let settled = false;
|
|
353
|
+
const finish = (r) => {
|
|
354
|
+
if (!settled) {
|
|
355
|
+
settled = true;
|
|
356
|
+
resolve(r);
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
let child;
|
|
360
|
+
try {
|
|
361
|
+
// Same rule as runNpm: every argument is a constant, an allow-listed
|
|
362
|
+
// package name, a validated semver or an allow-listed dist-tag.
|
|
363
|
+
child = SPAWN_SHELL
|
|
364
|
+
? spawn([NPM_BIN, ...args].join(" "), { shell: true, windowsHide: true, env: { ...process.env, CARRIEROS_UPDATING: "1" } })
|
|
365
|
+
: spawn(NPM_BIN, args, { windowsHide: true, env: { ...process.env, CARRIEROS_UPDATING: "1" } });
|
|
366
|
+
}
|
|
367
|
+
catch (err) {
|
|
368
|
+
finish({ status: null, stdout: "", stderr: "", error: err });
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const timer = setTimeout(() => {
|
|
372
|
+
child.kill();
|
|
373
|
+
finish({ status: null, stdout, stderr, error: Object.assign(new Error("npm timed out"), { code: "ETIMEDOUT" }) });
|
|
374
|
+
}, timeoutMs);
|
|
375
|
+
timer.unref?.();
|
|
376
|
+
child.stdout?.on("data", (d) => { if (stdout.length < MAX_OUTPUT)
|
|
377
|
+
stdout += String(d); });
|
|
378
|
+
child.stderr?.on("data", (d) => { if (stderr.length < MAX_OUTPUT)
|
|
379
|
+
stderr += String(d); });
|
|
380
|
+
child.on("error", (err) => { clearTimeout(timer); finish({ status: null, stdout, stderr, error: err }); });
|
|
381
|
+
child.on("close", (code) => { clearTimeout(timer); finish({ status: code, stdout, stderr }); });
|
|
382
|
+
});
|
|
383
|
+
let npmRunner = spawnNpm;
|
|
384
|
+
/** Test seam: replace npm with a fake. Passing null restores the real one. */
|
|
385
|
+
export function setNpmRunnerForTests(runner) {
|
|
386
|
+
npmRunner = runner ?? spawnNpm;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* The version a channel currently points at, from the package's npm dist-tags.
|
|
390
|
+
* Only a well-formed semver is ever returned; anything else is a failure.
|
|
391
|
+
*/
|
|
392
|
+
export async function fetchChannelVersion(channel = defaultUpdateChannel(), name = packageName()) {
|
|
393
|
+
if (!isUpdateChannel(channel))
|
|
394
|
+
return { ok: false, reason: "failed", detail: "Unknown update channel." };
|
|
395
|
+
const res = await npmRunner(["view", name, "dist-tags", "--json"], VIEW_TIMEOUT_MS);
|
|
396
|
+
if (res.error?.code === "ENOENT")
|
|
397
|
+
return { ok: false, reason: "no-npm", detail: "npm was not found on this system." };
|
|
398
|
+
if (res.error)
|
|
399
|
+
return { ok: false, reason: "failed", detail: res.error.code === "ETIMEDOUT" ? "The npm registry did not answer in time." : res.error.message };
|
|
400
|
+
if (res.status !== 0) {
|
|
401
|
+
const first = String(res.stderr || res.stdout || "").trim().split("\n")[0];
|
|
402
|
+
return { ok: false, reason: "failed", detail: first || "npm exited with code " + res.status };
|
|
403
|
+
}
|
|
404
|
+
let tags;
|
|
405
|
+
try {
|
|
406
|
+
tags = JSON.parse(String(res.stdout || "").trim());
|
|
407
|
+
}
|
|
408
|
+
catch {
|
|
409
|
+
return { ok: false, reason: "failed", detail: "Unexpected npm output." };
|
|
410
|
+
}
|
|
411
|
+
if (!tags || typeof tags !== "object" || Array.isArray(tags))
|
|
412
|
+
return { ok: false, reason: "failed", detail: "Unexpected npm output." };
|
|
413
|
+
const version = tags[channel];
|
|
414
|
+
if (version === undefined)
|
|
415
|
+
return { ok: false, reason: "no-channel", detail: `Nothing is published on the "${channel}" channel yet.` };
|
|
416
|
+
if (!isVersionLike(version))
|
|
417
|
+
return { ok: false, reason: "failed", detail: "The registry returned an invalid version." };
|
|
418
|
+
return { ok: true, version: version.trim() };
|
|
419
|
+
}
|
|
420
|
+
/** Current vs the channel's version, as one state for messages and decisions. */
|
|
421
|
+
export async function checkChannel(channel = defaultUpdateChannel(), current = getCurrentVersion()) {
|
|
422
|
+
const latest = await fetchChannelVersion(channel);
|
|
423
|
+
if (!latest.ok) {
|
|
424
|
+
if (latest.reason === "no-npm")
|
|
425
|
+
return { kind: "no-npm", current };
|
|
426
|
+
if (latest.reason === "no-channel")
|
|
427
|
+
return { kind: "no-channel", current, channel, detail: latest.detail };
|
|
428
|
+
return { kind: "unreachable", current, detail: latest.detail };
|
|
429
|
+
}
|
|
430
|
+
let cmp;
|
|
431
|
+
try {
|
|
432
|
+
cmp = compareVersions(current, latest.version);
|
|
433
|
+
}
|
|
434
|
+
catch {
|
|
435
|
+
return { kind: "unreachable", current, detail: "The installed version could not be read." };
|
|
436
|
+
}
|
|
437
|
+
if (cmp === 0)
|
|
438
|
+
return { kind: "current", current, latest: latest.version };
|
|
439
|
+
return cmp < 0 ? { kind: "available", current, latest: latest.version } : { kind: "ahead", current, latest: latest.version };
|
|
440
|
+
}
|
|
441
|
+
async function globalRootAsync() {
|
|
442
|
+
const res = await npmRunner(["root", "-g"], VIEW_TIMEOUT_MS);
|
|
443
|
+
if (res.error || res.status !== 0)
|
|
444
|
+
return null;
|
|
445
|
+
const out = String(res.stdout || "").trim();
|
|
446
|
+
// npm redacts strings that look like secrets in everything it prints —
|
|
447
|
+
// including a UUID-like folder in a path, shown as "***". A redacted path is
|
|
448
|
+
// not a path; never compare against it.
|
|
449
|
+
if (!out || out.includes("***"))
|
|
450
|
+
return null;
|
|
451
|
+
return path.resolve(out);
|
|
452
|
+
}
|
|
453
|
+
function realpathSafe(p) {
|
|
454
|
+
try {
|
|
455
|
+
return fs.realpathSync(p);
|
|
456
|
+
}
|
|
457
|
+
catch {
|
|
458
|
+
return path.resolve(p);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Where npm's global packages can live, computed without parsing npm output:
|
|
463
|
+
* an explicit NPM_CONFIG_PREFIX, npm's default prefix next to this Node, and
|
|
464
|
+
* the per-user Windows location.
|
|
465
|
+
*/
|
|
466
|
+
export function globalRootCandidates(env = process.env, execPath = process.execPath, platform = process.platform) {
|
|
467
|
+
const nm = (prefix) => (platform === "win32" ? path.join(prefix, "node_modules") : path.join(prefix, "lib", "node_modules"));
|
|
468
|
+
const out = [];
|
|
469
|
+
const prefix = env.NPM_CONFIG_PREFIX || env.npm_config_prefix;
|
|
470
|
+
if (prefix)
|
|
471
|
+
out.push(nm(prefix));
|
|
472
|
+
out.push(platform === "win32" ? path.join(path.dirname(execPath), "node_modules") : nm(path.dirname(path.dirname(execPath))));
|
|
473
|
+
if (platform === "win32" && env.APPDATA)
|
|
474
|
+
out.push(path.join(env.APPDATA, "npm", "node_modules"));
|
|
475
|
+
return out;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* The global root implied by the install layout itself: a global package sits
|
|
479
|
+
* at <prefix>/lib/node_modules/<pkg> with its launcher in <prefix>/bin (on
|
|
480
|
+
* Windows: <prefix>/node_modules/<pkg> with <prefix>/cos.cmd). A project
|
|
481
|
+
* dependency's launcher lives in node_modules/.bin instead, so the two cannot
|
|
482
|
+
* be confused.
|
|
483
|
+
*/
|
|
484
|
+
export function layoutGlobalRoot(here, name = packageName(), platform = process.platform) {
|
|
485
|
+
const depth = name.split("/").length;
|
|
486
|
+
let nmDir = here;
|
|
487
|
+
for (let i = 0; i < depth; i++)
|
|
488
|
+
nmDir = path.dirname(nmDir);
|
|
489
|
+
if (path.basename(nmDir) !== "node_modules")
|
|
490
|
+
return null;
|
|
491
|
+
const bins = ["cos", "carrieros", "cos-staging"];
|
|
492
|
+
if (platform === "win32") {
|
|
493
|
+
const prefix = path.dirname(nmDir);
|
|
494
|
+
return bins.some((b) => fs.existsSync(path.join(prefix, b + ".cmd"))) ? nmDir : null;
|
|
495
|
+
}
|
|
496
|
+
const lib = path.dirname(nmDir);
|
|
497
|
+
if (path.basename(lib) !== "lib")
|
|
498
|
+
return null;
|
|
499
|
+
const prefix = path.dirname(lib);
|
|
500
|
+
return bins.some((b) => fs.existsSync(path.join(prefix, "bin", b))) ? nmDir : null;
|
|
501
|
+
}
|
|
502
|
+
export async function detectInstallation(opts = {}) {
|
|
503
|
+
if (opts.packaged)
|
|
504
|
+
return { kind: "packaged" };
|
|
505
|
+
const here = opts.here === undefined ? packageRoot() : opts.here;
|
|
506
|
+
if (!here)
|
|
507
|
+
return { kind: "unknown" };
|
|
508
|
+
const parts = here.split(/[\\/]+/);
|
|
509
|
+
if (parts.includes("_npx"))
|
|
510
|
+
return { kind: "npx" };
|
|
511
|
+
// Compare REAL paths: the launcher is usually a symlink, and /tmp-style
|
|
512
|
+
// aliases differ from what the module was loaded from.
|
|
513
|
+
const real = realpathSafe(here);
|
|
514
|
+
const npmRoot = await globalRootAsync();
|
|
515
|
+
const candidates = [...(npmRoot ? [npmRoot] : []), ...globalRootCandidates()];
|
|
516
|
+
const globalRoot = candidates.map(realpathSafe).find((root) => isInside(root, real)) ?? layoutGlobalRoot(real) ?? null;
|
|
517
|
+
if (globalRoot) {
|
|
518
|
+
const installed = path.join(globalRoot, ...packageName().split("/"));
|
|
519
|
+
try {
|
|
520
|
+
if (fs.lstatSync(installed).isSymbolicLink())
|
|
521
|
+
return { kind: "development", globalRoot };
|
|
522
|
+
}
|
|
523
|
+
catch {
|
|
524
|
+
/* fall through */
|
|
525
|
+
}
|
|
526
|
+
let writable = false;
|
|
527
|
+
try {
|
|
528
|
+
fs.accessSync(globalRoot, fs.constants.W_OK);
|
|
529
|
+
fs.accessSync(here, fs.constants.W_OK);
|
|
530
|
+
writable = true;
|
|
531
|
+
}
|
|
532
|
+
catch {
|
|
533
|
+
writable = false;
|
|
534
|
+
}
|
|
535
|
+
return { kind: "global", globalRoot, writable };
|
|
536
|
+
}
|
|
537
|
+
if (parts.includes("node_modules"))
|
|
538
|
+
return { kind: "local" };
|
|
539
|
+
return { kind: "development" };
|
|
540
|
+
}
|
|
541
|
+
/** The version on disk for the global install, read fresh (never cliVersion()). */
|
|
542
|
+
export function installedGlobalVersion(globalRoot, name = packageName()) {
|
|
543
|
+
try {
|
|
544
|
+
const parsed = JSON.parse(fs.readFileSync(path.join(globalRoot, ...name.split("/"), "package.json"), "utf8"));
|
|
545
|
+
return isVersionLike(parsed.version) ? parsed.version : null;
|
|
546
|
+
}
|
|
547
|
+
catch {
|
|
548
|
+
return null;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Install one EXACT version (never a moving tag), then read back what is on
|
|
553
|
+
* disk. Success means the expected version is installed, not that npm exited 0.
|
|
554
|
+
*/
|
|
555
|
+
export async function installExactVersion(version, globalRoot, name = packageName()) {
|
|
556
|
+
if (!isVersionLike(version) || !ALLOWED_PACKAGES.has(name)) {
|
|
557
|
+
return { ok: false, detail: "Refused: unexpected package or version.", permission: false, installed: null };
|
|
558
|
+
}
|
|
559
|
+
const res = await npmRunner(["install", "-g", `${name}@${version}`], INSTALL_TIMEOUT_MS);
|
|
560
|
+
const output = (String(res.stdout || "") + "\n" + String(res.stderr || "")).trim();
|
|
561
|
+
if (res.error) {
|
|
562
|
+
return {
|
|
563
|
+
ok: false,
|
|
564
|
+
detail: res.error.code === "ENOENT" ? "npm was not found on this system." : res.error.message,
|
|
565
|
+
permission: false,
|
|
566
|
+
installed: null
|
|
567
|
+
};
|
|
568
|
+
}
|
|
569
|
+
if (res.status !== 0) {
|
|
570
|
+
return {
|
|
571
|
+
ok: false,
|
|
572
|
+
detail: lastMeaningfulLines(output) || "npm exited with code " + res.status,
|
|
573
|
+
permission: /EACCES|EPERM|permission denied|Access is denied/i.test(output),
|
|
574
|
+
installed: null
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
const root = globalRoot ?? (await globalRootAsync()) ?? globalRootCandidates().find((r) => fs.existsSync(path.join(r, ...name.split("/")))) ?? null;
|
|
578
|
+
const installed = root ? installedGlobalVersion(root, name) : null;
|
|
579
|
+
return installed === version
|
|
580
|
+
? { ok: true, detail: output, permission: false, installed }
|
|
581
|
+
: { ok: false, detail: `npm finished but ${name}@${version} was not found on disk.`, permission: false, installed };
|
|
582
|
+
}
|
|
583
|
+
// ── Cross-process coordination ──────────────────────────────────────────────
|
|
584
|
+
function pidAlive(pid) {
|
|
585
|
+
if (!Number.isInteger(pid) || pid <= 0)
|
|
586
|
+
return false;
|
|
587
|
+
try {
|
|
588
|
+
process.kill(pid, 0);
|
|
589
|
+
return true;
|
|
590
|
+
}
|
|
591
|
+
catch (err) {
|
|
592
|
+
return err.code === "EPERM";
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
/** A stale lock (owner gone, or older than this) is taken over. */
|
|
596
|
+
const LOCK_STALE_MS = 30 * 60 * 1000;
|
|
597
|
+
/**
|
|
598
|
+
* One update at a time across every CLI window on the machine. The lock file
|
|
599
|
+
* holds the owning pid; a dead owner or an old lock is stale and replaced.
|
|
600
|
+
*/
|
|
601
|
+
export function acquireUpdateLock(dir, pid = process.pid, now = Date.now()) {
|
|
602
|
+
const file = path.join(dir, "update.lock");
|
|
603
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
604
|
+
try {
|
|
605
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
606
|
+
fs.writeFileSync(file, JSON.stringify({ pid, at: now }), { flag: "wx" });
|
|
607
|
+
return true;
|
|
608
|
+
}
|
|
609
|
+
catch (err) {
|
|
610
|
+
if (err.code !== "EEXIST")
|
|
611
|
+
return false;
|
|
612
|
+
try {
|
|
613
|
+
const held = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
614
|
+
const stale = !pidAlive(Number(held.pid)) || now - Number(held.at ?? 0) > LOCK_STALE_MS;
|
|
615
|
+
if (!stale)
|
|
616
|
+
return false;
|
|
617
|
+
fs.rmSync(file, { force: true });
|
|
618
|
+
}
|
|
619
|
+
catch {
|
|
620
|
+
fs.rmSync(file, { force: true });
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return false;
|
|
625
|
+
}
|
|
626
|
+
/** Hand the lock to another process (the detached installer) or rewrite it. */
|
|
627
|
+
export function transferUpdateLock(dir, pid, now = Date.now()) {
|
|
628
|
+
try {
|
|
629
|
+
fs.writeFileSync(path.join(dir, "update.lock"), JSON.stringify({ pid, at: now }));
|
|
630
|
+
}
|
|
631
|
+
catch {
|
|
632
|
+
/* best effort */
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
export function releaseUpdateLock(dir, pid = process.pid) {
|
|
636
|
+
const file = path.join(dir, "update.lock");
|
|
637
|
+
try {
|
|
638
|
+
const held = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
639
|
+
if (Number(held.pid) === pid)
|
|
640
|
+
fs.rmSync(file, { force: true });
|
|
641
|
+
}
|
|
642
|
+
catch {
|
|
643
|
+
/* nothing held */
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
/** True while an update (in this or another process) holds the lock. */
|
|
647
|
+
export function updateInProgress(dir, now = Date.now()) {
|
|
648
|
+
try {
|
|
649
|
+
const held = JSON.parse(fs.readFileSync(path.join(dir, "update.lock"), "utf8"));
|
|
650
|
+
return pidAlive(Number(held.pid)) && now - Number(held.at ?? 0) <= LOCK_STALE_MS;
|
|
651
|
+
}
|
|
652
|
+
catch {
|
|
653
|
+
return false;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* Live CLI windows on this machine. A background install replaces files on
|
|
658
|
+
* disk, so it waits until the last window has closed: no running CLI ever has
|
|
659
|
+
* its package swapped underneath it.
|
|
660
|
+
*/
|
|
661
|
+
export function registerInstance(dir, pid = process.pid) {
|
|
662
|
+
const instances = path.join(dir, "instances");
|
|
663
|
+
const file = path.join(instances, String(pid));
|
|
664
|
+
try {
|
|
665
|
+
fs.mkdirSync(instances, { recursive: true });
|
|
666
|
+
fs.writeFileSync(file, String(Date.now()));
|
|
667
|
+
}
|
|
668
|
+
catch {
|
|
669
|
+
/* best effort */
|
|
670
|
+
}
|
|
671
|
+
return () => fs.rmSync(file, { force: true });
|
|
672
|
+
}
|
|
673
|
+
export function otherLiveInstances(dir, pid = process.pid) {
|
|
674
|
+
const instances = path.join(dir, "instances");
|
|
675
|
+
let names = [];
|
|
676
|
+
try {
|
|
677
|
+
names = fs.readdirSync(instances);
|
|
678
|
+
}
|
|
679
|
+
catch {
|
|
680
|
+
return 0;
|
|
681
|
+
}
|
|
682
|
+
let live = 0;
|
|
683
|
+
for (const name of names) {
|
|
684
|
+
const other = Number(name);
|
|
685
|
+
if (other === pid)
|
|
686
|
+
continue;
|
|
687
|
+
if (pidAlive(other))
|
|
688
|
+
live++;
|
|
689
|
+
else
|
|
690
|
+
fs.rmSync(path.join(instances, name), { force: true });
|
|
691
|
+
}
|
|
692
|
+
return live;
|
|
693
|
+
}
|
|
694
|
+
/** Start npm detached, output to a log file; returns its pid. */
|
|
695
|
+
const spawnDetachedNpm = (command, args, logFile) => {
|
|
696
|
+
let fd = null;
|
|
697
|
+
try {
|
|
698
|
+
fd = fs.openSync(logFile, "w");
|
|
699
|
+
const child = SPAWN_SHELL
|
|
700
|
+
? spawn([command, ...args].join(" "), { shell: true, detached: true, windowsHide: true, stdio: ["ignore", fd, fd], env: { ...process.env, CARRIEROS_UPDATING: "1" } })
|
|
701
|
+
: spawn(command, args, { detached: true, windowsHide: true, stdio: ["ignore", fd, fd], env: { ...process.env, CARRIEROS_UPDATING: "1" } });
|
|
702
|
+
child.unref();
|
|
703
|
+
return child.pid ?? null;
|
|
704
|
+
}
|
|
705
|
+
catch {
|
|
706
|
+
return null;
|
|
707
|
+
}
|
|
708
|
+
finally {
|
|
709
|
+
if (fd !== null)
|
|
710
|
+
fs.closeSync(fd);
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
let detachedSpawner = spawnDetachedNpm;
|
|
714
|
+
export function setDetachedSpawnerForTests(spawner) {
|
|
715
|
+
detachedSpawner = spawner ?? spawnDetachedNpm;
|
|
716
|
+
}
|
|
717
|
+
/**
|
|
718
|
+
* Start the background install of an exact version, detached so it runs after
|
|
719
|
+
* this process has exited. The lock passes to the installer's pid, so no other
|
|
720
|
+
* window starts a second install while it runs.
|
|
721
|
+
*/
|
|
722
|
+
export function startDetachedInstall(dir, version, name = packageName()) {
|
|
723
|
+
if (!isVersionLike(version) || !ALLOWED_PACKAGES.has(name))
|
|
724
|
+
return false;
|
|
725
|
+
const pid = detachedSpawner(NPM_BIN, ["install", "-g", `${name}@${version}`], path.join(dir, "update.log"));
|
|
726
|
+
if (!pid)
|
|
727
|
+
return false;
|
|
728
|
+
transferUpdateLock(dir, pid);
|
|
729
|
+
return true;
|
|
730
|
+
}
|
package/dist/shell/shell-chat.js
CHANGED
|
@@ -119,6 +119,9 @@ const ERROR_HINTS = {
|
|
|
119
119
|
// Context overflow: a new conversation is the one reliable fix, and a
|
|
120
120
|
// roomier model is the other.
|
|
121
121
|
MCAI_BLOCKED: "/new",
|
|
122
|
+
// The model's context limit cannot be verified (a server configuration
|
|
123
|
+
// gap): another model is the fix, not a shorter message.
|
|
124
|
+
MCAI_UNVERIFIED: "/model",
|
|
122
125
|
SESSION_NOT_FOUND: "/sessions",
|
|
123
126
|
CARRIER_CHAT_MAINTENANCE: "/switch standard",
|
|
124
127
|
// Out of credit on this model — pick another rather than retrying into the
|
|
@@ -188,7 +188,7 @@ export async function handleSlash(ctx, program, client, input) {
|
|
|
188
188
|
return STAY;
|
|
189
189
|
// One command: checks, and offers to install when something newer exists.
|
|
190
190
|
case "update":
|
|
191
|
-
await runUpdate();
|
|
191
|
+
await runUpdate({ config: ctx.config, busy: ctx.mainTurnRunning === true });
|
|
192
192
|
return STAY;
|
|
193
193
|
case "sources":
|
|
194
194
|
showSources(ctx);
|
package/dist/shell/shell.js
CHANGED
|
@@ -14,7 +14,8 @@ import { setCommandScope } from "../ui/command-item.js";
|
|
|
14
14
|
import { startScreen, stopScreen, screenScrollToBottom } from "../ui/screen.js";
|
|
15
15
|
import { ensureWorkspaceTrust } from "../ui/trust.js";
|
|
16
16
|
import { activeRepository, inWorkspaceMode, listRepositories } from "../services/workspace-registry.js";
|
|
17
|
-
import { cachedUpdateNotice,
|
|
17
|
+
import { cachedUpdateNotice, shouldRefreshCheck } from "../services/update.js";
|
|
18
|
+
import { AutoUpdater } from "../services/auto-update.js";
|
|
18
19
|
import { chatTypeLabel, loadModels, loadModes, showCarrierMaintenance, syncChatAvailability, loadFastModel } from "./shell-chat.js";
|
|
19
20
|
import { CARET, buildFooter } from "./shell-prompt.js";
|
|
20
21
|
import { dispatchLine, isMainTurn } from "./shell-router.js";
|
|
@@ -65,7 +66,9 @@ export async function runShell(ctx, program) {
|
|
|
65
66
|
// startScreen() has since switched to the alternate buffer, which hides them.
|
|
66
67
|
// Update notice comes from the PREVIOUS check — reading the cache is a
|
|
67
68
|
// property lookup, so the banner is never waiting on npm.
|
|
68
|
-
|
|
69
|
+
// The installed .exe keeps its banner notice; npm installs are announced by
|
|
70
|
+
// the AutoUpdater instead, once per version, between turns.
|
|
71
|
+
const notice = isPackaged() ? cachedUpdateNotice(ctx.config) : null;
|
|
69
72
|
renderWelcomeBanner(ctx, notice ? { notices: [notice] } : undefined);
|
|
70
73
|
if (inWorkspaceMode()) {
|
|
71
74
|
line(" " +
|
|
@@ -111,23 +114,33 @@ export async function runShell(ctx, program) {
|
|
|
111
114
|
// what was being done, as context only. No approval, mutation or command is
|
|
112
115
|
// restored — anything still needed is proposed and approved again.
|
|
113
116
|
await showTaskContext(client, ctx.config.get("sessionId"));
|
|
114
|
-
//
|
|
115
|
-
//
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
117
|
+
// Update notices wait until no turn is running, so a check that finishes
|
|
118
|
+
// mid-answer never prints into the middle of it.
|
|
119
|
+
const updateNotices = [];
|
|
120
|
+
let mainRunningForNotices = () => false;
|
|
121
|
+
const flushUpdateNotices = () => {
|
|
122
|
+
if (mainRunningForNotices())
|
|
123
|
+
return;
|
|
124
|
+
while (updateNotices.length)
|
|
125
|
+
line(" " + c.dim("ⓘ " + updateNotices.shift()));
|
|
126
|
+
};
|
|
127
|
+
// The installed .exe has no npm: it reads the release manifest, once a day,
|
|
128
|
+
// asynchronously. npm installs use the AutoUpdater: an async check at startup
|
|
129
|
+
// (when due) and on its interval, and a background install on exit.
|
|
130
|
+
let autoUpdater = null;
|
|
131
|
+
if (isPackaged()) {
|
|
132
|
+
if (shouldRefreshCheck(ctx.config)) {
|
|
133
|
+
setImmediate(() => void refreshPackagedUpdateCache(ctx.config).catch(() => undefined));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
try {
|
|
138
|
+
autoUpdater = new AutoUpdater({ config: ctx.config, dir: ctx.config.dir, notify: (m) => { updateNotices.push(m); flushUpdateNotices(); } });
|
|
139
|
+
autoUpdater.start();
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
autoUpdater = null; /* never let updates affect the session */
|
|
143
|
+
}
|
|
131
144
|
}
|
|
132
145
|
// Interactive loop driven by the composer/reader. Each iteration reads one
|
|
133
146
|
// line (with the slash palette overlay, the dim status footer, and a
|
|
@@ -147,6 +160,7 @@ export async function runShell(ctx, program) {
|
|
|
147
160
|
let mainTask = null;
|
|
148
161
|
let mainAbort = null;
|
|
149
162
|
const mainRunning = () => mainTask !== null;
|
|
163
|
+
mainRunningForNotices = mainRunning;
|
|
150
164
|
/**
|
|
151
165
|
* One message typed while a turn is still answering.
|
|
152
166
|
*
|
|
@@ -172,6 +186,7 @@ export async function runShell(ctx, program) {
|
|
|
172
186
|
mainTask = null;
|
|
173
187
|
mainAbort = null;
|
|
174
188
|
ctx.mainTurnRunning = false;
|
|
189
|
+
flushUpdateNotices();
|
|
175
190
|
if (queued !== null) {
|
|
176
191
|
const next = queued;
|
|
177
192
|
queued = null;
|
|
@@ -207,11 +222,21 @@ export async function runShell(ctx, program) {
|
|
|
207
222
|
if (process.stdout.isTTY && process.env.TERM !== "dumb")
|
|
208
223
|
process.stdout.write("\x1b[?25h");
|
|
209
224
|
renderExit(ctx.config.get("sessionId"));
|
|
225
|
+
// A release found during the session installs now, detached, after this
|
|
226
|
+
// process (and every other CLI window) has gone.
|
|
227
|
+
try {
|
|
228
|
+
if (autoUpdater?.onExit())
|
|
229
|
+
line(" " + c.dim("Installing CarrierOS CLI " + autoUpdater.pendingVersion + " in the background — it will be ready next time."));
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
/* never block leaving */
|
|
233
|
+
}
|
|
210
234
|
process.stdin.removeAllListeners("keypress");
|
|
211
235
|
process.stdin.removeAllListeners("data");
|
|
212
236
|
process.stdin.pause();
|
|
213
237
|
};
|
|
214
238
|
for (;;) {
|
|
239
|
+
flushUpdateNotices();
|
|
215
240
|
const placeholder = ctx.chatType === "carrier" && ctx.carrierMaintenance !== false
|
|
216
241
|
? ctx.carrierMaintenance === true
|
|
217
242
|
? "Carrier Chat is under maintenance · /switch standard"
|
|
@@ -78,7 +78,7 @@ export const COMMANDS = [
|
|
|
78
78
|
{ name: "/repos", desc: "Repositories in this workspace", group: "Context", rank: 4 },
|
|
79
79
|
{ name: "/whoami", desc: "Your signed-in account", group: "Account", rank: 1 },
|
|
80
80
|
{ name: "/usage", desc: "View API usage", group: "Account", rank: 2 },
|
|
81
|
-
{ name: "/settings", desc: "CLI settings —
|
|
81
|
+
{ name: "/settings", desc: "CLI settings — commands, updates, channel, update-interval", group: "Account", rank: 3, arg: "commands" },
|
|
82
82
|
{ name: "/login", desc: "Sign in via your browser", group: "Account", rank: 3 },
|
|
83
83
|
{ name: "/logout", desc: "Remove saved credentials", group: "Account", rank: 4 },
|
|
84
84
|
{ name: "/help", desc: "Show available commands", group: "Developer", rank: 1 },
|
|
@@ -87,7 +87,7 @@ export const COMMANDS = [
|
|
|
87
87
|
{ name: "/version", desc: "Show CLI version", group: "Developer", rank: 4 },
|
|
88
88
|
{
|
|
89
89
|
name: "/update",
|
|
90
|
-
desc: "Check for and install the latest CLI version",
|
|
90
|
+
desc: "Check for and install the latest CLI version (your update channel)",
|
|
91
91
|
group: "Developer",
|
|
92
92
|
rank: 5
|
|
93
93
|
},
|