@tpsdev-ai/flair 0.55.0 → 0.55.2
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/build-info.json +3 -3
- package/dist/cli.js +191 -83
- package/dist/commands/doctor.js +172 -27
- package/dist/commands/federation.js +44 -57
- package/dist/commands/init.js +64 -62
- package/dist/commands/rem.js +29 -2
- package/dist/commands/upgrade.js +85 -60
- package/dist/doctor-client.js +336 -138
- package/dist/hook-install.js +359 -176
- package/dist/install/clients.js +907 -208
- package/dist/lib/config-critical-section.js +557 -0
- package/dist/lib/doctor-run.js +107 -27
- package/dist/lib/embed-gpu-doctor.js +40 -7
- package/dist/lib/launchd-repair.js +103 -0
- package/dist/lib/owned-pins.js +255 -81
- package/dist/lib/pin-write-guard.js +87 -0
- package/dist/lib/settings-bytes.js +85 -0
- package/dist/lib/upgrade-plain-tree.js +293 -48
- package/dist/lib/upgrade-status.js +164 -0
- package/dist/lib/wiring-spec.js +238 -0
- package/dist/rem/promote-policy.js +102 -0
- package/dist/resources/MemoryReflect.js +10 -0
- package/dist/resources/auto-promote-lib.js +56 -0
- package/dist/resources/health.js +26 -0
- package/dist/resources/memory-reflect-lib.js +199 -6
- package/dist/resources/migrations/progress.js +17 -0
- package/dist/resources/migrations/runner.js +46 -27
- package/dist/resources/migrations/status.js +2 -1
- package/dist/src/rem/promote-policy.js +102 -0
- package/docs/deployment.md +38 -5
- package/docs/federation.md +19 -0
- package/docs/releasing.md +66 -22
- package/docs/standalone-local.md +7 -4
- package/package.json +3 -1
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
* (b) — the exec-path mismatch warning — already shipped in #1560
|
|
18
18
|
* (`upgrade-exec-path.ts`). This file does not change that wording.
|
|
19
19
|
*/
|
|
20
|
-
import { existsSync, mkdirSync, renameSync, rmSync, cpSync, readdirSync, readFileSync, statSync, lstatSync, } from "node:fs";
|
|
21
|
-
import { basename, dirname, join } from "node:path";
|
|
20
|
+
import { existsSync, mkdirSync, renameSync, rmSync, cpSync, readdirSync, readFileSync, statSync, lstatSync, realpathSync, } from "node:fs";
|
|
21
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
22
22
|
import { homedir } from "node:os";
|
|
23
23
|
import { execFileSync } from "node:child_process";
|
|
24
24
|
import { extract as tarExtract } from "tar";
|
|
@@ -227,61 +227,302 @@ export function formatPlainTreePlan(plan) {
|
|
|
227
227
|
function formatUnitRef(u) {
|
|
228
228
|
return `${u.name} (${u.scope}: ${u.path})`;
|
|
229
229
|
}
|
|
230
|
+
// ─── unit path operands (flair#1758) ────────────────────────────────────────
|
|
231
|
+
//
|
|
232
|
+
// Discovery compares the tree against the PATHS A UNIT ACTUALLY USES, not
|
|
233
|
+
// against arbitrary text. The original matcher scanned the whole file with only
|
|
234
|
+
// a trailing-boundary check, so a canonical path inside a COMMENT matched, and
|
|
235
|
+
// any unrelated value that merely CONTAINED the tree path matched too. Because
|
|
236
|
+
// the caller already canonicalizes the tree (readFlairPackageAt →
|
|
237
|
+
// canonicalPath), the "also try canonicalPath(treeDir)" variant here was dead
|
|
238
|
+
// code: the operand that needs resolving is the one read out of the unit file,
|
|
239
|
+
// not the tree.
|
|
240
|
+
//
|
|
241
|
+
// So: extract operands from ACTIVE `[Service]` `WorkingDirectory` and
|
|
242
|
+
// `ExecStart` directives, then compare canonical-to-canonical.
|
|
243
|
+
//
|
|
244
|
+
// - `WorkingDirectory` is a directory: its resolved path must EQUAL the tree.
|
|
245
|
+
// - `ExecStart` operands are executables/scripts/arguments: keep the original
|
|
246
|
+
// tree-DESCENDANT intent, on PATH-COMPONENT boundaries (so `/opt/flair-spoke`
|
|
247
|
+
// is not "inside" `/opt/flair`).
|
|
248
|
+
//
|
|
249
|
+
// Explicitly NOT done: the whole command line is never canonicalized as one
|
|
250
|
+
// string; quoted values are tokenized with systemd quoting rules rather than
|
|
251
|
+
// split on whitespace; and an operand carrying a systemd specifier (`%i`, `%n`)
|
|
252
|
+
// or a shell variable (`$VAR`) is not a literal host path, so it is skipped
|
|
253
|
+
// rather than matched.
|
|
254
|
+
//
|
|
255
|
+
// KNOWN LIMIT — `RootDirectory=`, `RootImage=` and service-specific bind mounts
|
|
256
|
+
// mean a unit can see DIFFERENT content at the same host pathname, so host
|
|
257
|
+
// `realpath` is not proof of service identity. Automatic matching stays scoped
|
|
258
|
+
// to host-path semantics; `FLAIR_SYSTEMD_UNIT` remains the escape hatch for
|
|
259
|
+
// namespaced configurations.
|
|
260
|
+
/** True when `child` is `root` itself or a path-component descendant of it. */
|
|
261
|
+
function isPathInside(child, root) {
|
|
262
|
+
if (child === root)
|
|
263
|
+
return true;
|
|
264
|
+
return child.startsWith(root.endsWith("/") ? root : `${root}/`);
|
|
265
|
+
}
|
|
266
|
+
/** Strip a leading systemd ExecStart/WorkingDirectory prefix (`-`, `@`, `+`, `!`, `:`). */
|
|
267
|
+
function stripSystemdExecPrefix(word) {
|
|
268
|
+
let i = 0;
|
|
269
|
+
while (i < word.length && "-@+!:".includes(word[i]))
|
|
270
|
+
i++;
|
|
271
|
+
return word.slice(i);
|
|
272
|
+
}
|
|
230
273
|
/**
|
|
231
|
-
*
|
|
232
|
-
*
|
|
233
|
-
*
|
|
274
|
+
* Canonicalize a path even when its leaf — or a middle segment — does not
|
|
275
|
+
* exist, by realpath-ing the deepest EXISTING ancestor and re-appending the
|
|
276
|
+
* remainder.
|
|
277
|
+
*
|
|
278
|
+
* `canonicalPath` alone is not enough: `realpathSync("/opt/flair/flair")`
|
|
279
|
+
* throws when that file is absent, and its fallback returns the LEXICAL path,
|
|
280
|
+
* so a symlinked `/opt/flair` would never resolve for a unit naming a file
|
|
281
|
+
* inside it. Resolving the ancestor is what makes the unit operand and the tree
|
|
282
|
+
* comparable.
|
|
234
283
|
*
|
|
235
|
-
*
|
|
236
|
-
*
|
|
237
|
-
*
|
|
284
|
+
* Only ABSOLUTE host paths are canonicalized — a relative operand is not a path
|
|
285
|
+
* into the tree. Returns null for anything that is not a literal host path
|
|
286
|
+
* (relative, or containing an unresolved `%`/`$`).
|
|
238
287
|
*/
|
|
239
|
-
function
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
288
|
+
function canonicalizeOperand(rawPath) {
|
|
289
|
+
const raw = rawPath.trim();
|
|
290
|
+
if (!raw.startsWith("/"))
|
|
291
|
+
return null;
|
|
292
|
+
if (raw.includes("%") || raw.includes("$"))
|
|
293
|
+
return null;
|
|
294
|
+
const abs = resolve(raw);
|
|
295
|
+
let cur = abs;
|
|
296
|
+
const tail = [];
|
|
297
|
+
for (;;) {
|
|
298
|
+
// `existsSync` is the STRICT check in both runtimes; gate `realpathSync` on
|
|
299
|
+
// it. Bun's `realpathSync` is lenient — for a path ending in a literal
|
|
300
|
+
// backslash it silently drops the backslash and returns the canonical dir,
|
|
301
|
+
// where Node throws ENOENT — so relying on `realpathSync` alone makes the
|
|
302
|
+
// matcher depend on the runtime (production ships Node, tests run Bun).
|
|
303
|
+
// With the gate, a backslash operand falls through to the ancestor walk on
|
|
304
|
+
// both, yielding `path + "\\"` (≠ the tree).
|
|
305
|
+
if (existsSync(cur)) {
|
|
306
|
+
let real = cur;
|
|
307
|
+
try {
|
|
308
|
+
real = realpathSync(cur);
|
|
309
|
+
}
|
|
310
|
+
catch { /* unreadable — fall back to the existing lexical prefix */ }
|
|
311
|
+
return tail.length > 0 ? join(real, ...tail.reverse()) : real;
|
|
312
|
+
}
|
|
313
|
+
const parent = dirname(cur);
|
|
314
|
+
if (parent === cur)
|
|
315
|
+
return abs; // nothing on this path exists — lexical
|
|
316
|
+
tail.push(basename(cur));
|
|
317
|
+
cur = parent;
|
|
258
318
|
}
|
|
259
319
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
320
|
+
/**
|
|
321
|
+
* Tokenize a systemd directive value the way systemd splits ExecStart /
|
|
322
|
+
* WorkingDirectory: whitespace-separated words, single quotes literal, double
|
|
323
|
+
* quotes literal except for `\"`, `\\`, `\$`, and backslash escaping outside
|
|
324
|
+
* quotes. No variable/specifier expansion — callers treat those as non-paths.
|
|
325
|
+
*/
|
|
326
|
+
function splitSystemdWords(value) {
|
|
327
|
+
const words = [];
|
|
328
|
+
let cur = "";
|
|
329
|
+
let started = false;
|
|
330
|
+
let inSingle = false;
|
|
331
|
+
let inDouble = false;
|
|
332
|
+
for (let i = 0; i < value.length; i++) {
|
|
333
|
+
const ch = value[i];
|
|
334
|
+
if (inSingle) {
|
|
335
|
+
if (ch === "'")
|
|
336
|
+
inSingle = false;
|
|
337
|
+
else
|
|
338
|
+
cur += ch;
|
|
339
|
+
started = true;
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
if (inDouble) {
|
|
343
|
+
if (ch === "\\" && i + 1 < value.length && "\"\\$".includes(value[i + 1])) {
|
|
344
|
+
cur += value[++i];
|
|
345
|
+
started = true;
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
if (ch === "\"")
|
|
349
|
+
inDouble = false;
|
|
350
|
+
else
|
|
351
|
+
cur += ch;
|
|
352
|
+
started = true;
|
|
353
|
+
continue;
|
|
354
|
+
}
|
|
355
|
+
if (ch === "'") {
|
|
356
|
+
inSingle = true;
|
|
357
|
+
started = true;
|
|
358
|
+
continue;
|
|
359
|
+
}
|
|
360
|
+
if (ch === "\"") {
|
|
361
|
+
inDouble = true;
|
|
362
|
+
started = true;
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
if (ch === "\\" && i + 1 < value.length) {
|
|
366
|
+
cur += value[++i];
|
|
367
|
+
started = true;
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
if (ch === " " || ch === "\t") {
|
|
371
|
+
if (started) {
|
|
372
|
+
words.push(cur);
|
|
373
|
+
cur = "";
|
|
374
|
+
started = false;
|
|
375
|
+
}
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
cur += ch;
|
|
379
|
+
started = true;
|
|
269
380
|
}
|
|
270
|
-
|
|
381
|
+
if (started)
|
|
382
|
+
words.push(cur);
|
|
383
|
+
return words;
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* True when `line` ends in an unescaped backslash — systemd's continuation
|
|
387
|
+
* marker. Counts the run of trailing backslashes so an escaped `\\` is not
|
|
388
|
+
* treated as a continuation.
|
|
389
|
+
*/
|
|
390
|
+
function endsWithUnescapedBackslash(line) {
|
|
391
|
+
const stripped = line.replace(/[ \t]+$/, "");
|
|
392
|
+
let n = 0;
|
|
393
|
+
for (let i = stripped.length - 1; i >= 0 && stripped[i] === "\\"; i--)
|
|
394
|
+
n++;
|
|
395
|
+
return n % 2 === 1;
|
|
396
|
+
}
|
|
397
|
+
/**
|
|
398
|
+
* Fold systemd line continuations into logical lines before parsing: a physical
|
|
399
|
+
* line ending in an unescaped backslash joins the next line (the backslash and
|
|
400
|
+
* the newline become a single space). Folding first means a directive whose
|
|
401
|
+
* value lands on the continuation line is still read as one directive —
|
|
402
|
+
* otherwise it looks like an empty (reset) assignment.
|
|
403
|
+
*
|
|
404
|
+
* Comment lines (`#`/`;`) are dropped BEFORE the continuation logic, matching
|
|
405
|
+
* systemd's `config_parse()` (`src/shared/conf-parser.c:358` skips a comment at
|
|
406
|
+
* the top of the loop and does NOT touch the pending `continuation`;
|
|
407
|
+
* `:372-401` appends only after that check; `:432` parses a leftover
|
|
408
|
+
* continuation at EOF). A comment block after a backslash-continued line is
|
|
409
|
+
* therefore IGNORED, and the continuation joins whatever follows it —
|
|
410
|
+
* `systemd.syntax(7)`:
|
|
411
|
+
* KeyThree=value 3\
|
|
412
|
+
* # this line is ignored
|
|
413
|
+
* ; this line is ignored too
|
|
414
|
+
* value 3 continued -> KeyThree=value 3 value 3 continued
|
|
415
|
+
*/
|
|
416
|
+
function foldSystemdContinuations(unitText) {
|
|
417
|
+
const logical = [];
|
|
418
|
+
let acc = "";
|
|
419
|
+
let accumulating = false;
|
|
420
|
+
for (const raw of unitText.split(/\r?\n/)) {
|
|
421
|
+
// Comments first: systemd skips them before continuation handling, so a
|
|
422
|
+
// comment line must not terminate (or be flushed into) a pending fold.
|
|
423
|
+
if (raw.trimStart().startsWith("#") || raw.trimStart().startsWith(";"))
|
|
424
|
+
continue;
|
|
425
|
+
const line = accumulating ? raw.replace(/^[ \t]+/, "") : raw;
|
|
426
|
+
if (endsWithUnescapedBackslash(line)) {
|
|
427
|
+
acc += line.replace(/[ \t]+$/, "").slice(0, -1) + " ";
|
|
428
|
+
accumulating = true;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (accumulating) {
|
|
432
|
+
logical.push(acc + line);
|
|
433
|
+
acc = "";
|
|
434
|
+
accumulating = false;
|
|
435
|
+
}
|
|
436
|
+
else {
|
|
437
|
+
logical.push(line);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
if (accumulating)
|
|
441
|
+
logical.push(acc);
|
|
442
|
+
return logical;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* The values of every active `key` directive inside `[Service]`. systemd's
|
|
446
|
+
* RESET semantics are honoured: a blank value (`WorkingDirectory=` /
|
|
447
|
+
* `ExecStart=`) clears the values collected so far for that key, and later
|
|
448
|
+
* non-blank values accumulate again. Comments, directives in other sections,
|
|
449
|
+
* and continued lines (folded first) are handled.
|
|
450
|
+
*
|
|
451
|
+
* Directive names are case-SENSITIVE. `config_item_table_lookup` matches with
|
|
452
|
+
* `streq` (`src/shared/conf-parser.c:67`) and the gperf table carries no
|
|
453
|
+
* `%ignore-case` (`src/core/load-fragment-gperf.gperf.in`), so a mis-cased key
|
|
454
|
+
* (e.g. `workingdirectory=`) is logged as "Unknown key … ignoring" and has NO
|
|
455
|
+
* effect. Matching it here would select a unit systemd does not configure.
|
|
456
|
+
*
|
|
457
|
+
* `opts.single` selects the storage rule for the key. systemd treats
|
|
458
|
+
* `WorkingDirectory` as SINGLE-valued (`config_parse_working_directory` does
|
|
459
|
+
* `free_and_replace`, so the LAST non-blank assignment wins) but `ExecStart` as
|
|
460
|
+
* a genuine LIST whose assignments accumulate. Sharing this scanner — rather
|
|
461
|
+
* than a second reader — keeps reset/continuation/comment handling identical.
|
|
462
|
+
*/
|
|
463
|
+
function activeServiceDirectiveValues(unitText, key, opts = {}) {
|
|
464
|
+
const values = [];
|
|
465
|
+
let section = "";
|
|
466
|
+
for (const rawLine of foldSystemdContinuations(unitText)) {
|
|
467
|
+
const line = rawLine.trim();
|
|
468
|
+
if (line === "" || line.startsWith("#") || line.startsWith(";"))
|
|
469
|
+
continue;
|
|
470
|
+
if (line.startsWith("[")) {
|
|
471
|
+
section = line.slice(1).replace(/\].*$/, "").trim().toLowerCase();
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
if (section !== "service")
|
|
475
|
+
continue;
|
|
476
|
+
const eq = line.indexOf("=");
|
|
477
|
+
if (eq < 0)
|
|
478
|
+
continue;
|
|
479
|
+
// Exact case — systemd is case-sensitive for directive names (see above).
|
|
480
|
+
if (line.slice(0, eq).trim() !== key)
|
|
481
|
+
continue;
|
|
482
|
+
const value = line.slice(eq + 1).trim();
|
|
483
|
+
if (value === "") {
|
|
484
|
+
// Blank assignment RESETS the list for this key — it is NOT a no-op.
|
|
485
|
+
// Drop everything collected so far; later non-blank values accumulate.
|
|
486
|
+
values.length = 0;
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
// Single-valued key: the LAST non-blank assignment wins. List key: every
|
|
490
|
+
// non-blank value is an operand.
|
|
491
|
+
if (opts.single)
|
|
492
|
+
values.length = 0;
|
|
493
|
+
values.push(value);
|
|
494
|
+
}
|
|
495
|
+
return values;
|
|
271
496
|
}
|
|
497
|
+
/**
|
|
498
|
+
* Does this unit's ACTIVE, parsed path operands name the tree? Canonicalizes the
|
|
499
|
+
* unit-side operand (resolving symlinks via its existing ancestor) and the tree,
|
|
500
|
+
* then compares. See the block comment above for what is deliberately excluded.
|
|
501
|
+
*/
|
|
272
502
|
export function unitTextMentionsTree(unitText, treeDir) {
|
|
273
|
-
const
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
503
|
+
const tree = canonicalPath(treeDir);
|
|
504
|
+
// WorkingDirectory: SINGLE-valued — only the last non-blank assignment counts
|
|
505
|
+
// — and its resolved path must BE the tree.
|
|
506
|
+
for (const value of activeServiceDirectiveValues(unitText, "WorkingDirectory", { single: true })) {
|
|
507
|
+
const [first] = splitSystemdWords(value);
|
|
508
|
+
if (first === undefined)
|
|
509
|
+
continue;
|
|
510
|
+
const resolved = canonicalizeOperand(stripSystemdExecPrefix(first));
|
|
511
|
+
if (resolved !== null && resolved === tree)
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
// ExecStart: a path operand that lives INSIDE the tree (executable / script /
|
|
515
|
+
// argument), on path-component boundaries.
|
|
516
|
+
for (const value of activeServiceDirectiveValues(unitText, "ExecStart")) {
|
|
517
|
+
const words = splitSystemdWords(value);
|
|
518
|
+
for (let i = 0; i < words.length; i++) {
|
|
519
|
+
const operand = i === 0 ? stripSystemdExecPrefix(words[i]) : words[i];
|
|
520
|
+
const resolved = canonicalizeOperand(operand);
|
|
521
|
+
if (resolved !== null && isPathInside(resolved, tree))
|
|
522
|
+
return true;
|
|
523
|
+
}
|
|
282
524
|
}
|
|
283
|
-
|
|
284
|
-
return [...variants].some((v) => textMentionsDir(unitText, v));
|
|
525
|
+
return false;
|
|
285
526
|
}
|
|
286
527
|
const SYSTEM_UNIT_DIRS = ["/etc/systemd/system"];
|
|
287
528
|
export function userSystemdDir(home = homedir()) {
|
|
@@ -444,6 +685,10 @@ export async function applyPlainTreeUpgrade(plan, hooks = {}) {
|
|
|
444
685
|
if (existsSync(plan.previousDir)) {
|
|
445
686
|
rmSync(plan.previousDir, { recursive: true, force: true });
|
|
446
687
|
}
|
|
688
|
+
// flair#1758: this rename moves the tree discovery matched. If a symlink in
|
|
689
|
+
// the tree's path is retargeted between discovery (upgrade.ts) and here, the
|
|
690
|
+
// restart can launch a different tree. Documented, not solved — fresh
|
|
691
|
+
// comparison is not locking.
|
|
447
692
|
try {
|
|
448
693
|
renameSync(plan.treeDir, plan.previousDir);
|
|
449
694
|
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* upgrade-status.ts — the single definition of `flair upgrade`'s per-package
|
|
3
|
+
* status: the state set, the direction-aware classifier that produces it, and
|
|
4
|
+
* the renderers that print it (flair#1778 slice 1).
|
|
5
|
+
*
|
|
6
|
+
* Why this module exists: the status set and its rendering were duplicated
|
|
7
|
+
* between src/cli.ts and src/commands/upgrade.ts. src/commands/upgrade.ts may
|
|
8
|
+
* not import src/cli.ts (its header says so), so the shared set lives here and
|
|
9
|
+
* both import it — one definition, no drift.
|
|
10
|
+
*
|
|
11
|
+
* Direction matters now. Before #1778 the classifier was a bare equality test
|
|
12
|
+
* (`installed === latest ? "current" : "outdated"`), so an install AHEAD of the
|
|
13
|
+
* registry's `latest` tag (a staged / never-promoted version, e.g. 0.55.0 while
|
|
14
|
+
* `latest` is still 0.54.2) was classified "outdated" and rendered with an
|
|
15
|
+
* upgrade arrow to a LOWER version — and a plain `flair upgrade` would then
|
|
16
|
+
* install it. "ahead" is that fourth direction and it never carries a remedy.
|
|
17
|
+
*/
|
|
18
|
+
import semver from "semver";
|
|
19
|
+
import { FLAIR_MCP_PACKAGE } from "./mcp-spec.js";
|
|
20
|
+
import { isStrictSemver } from "./npm-registry.js";
|
|
21
|
+
/**
|
|
22
|
+
* Classify an installed version against registry `latest`, direction-aware.
|
|
23
|
+
*
|
|
24
|
+
* installed > latest -> "ahead" (staged / pre-release; never an upgrade target)
|
|
25
|
+
* installed === latest -> "current"
|
|
26
|
+
* installed < latest -> "outdated"
|
|
27
|
+
* unparseable -> "unknown" (never "outdated", never dropped)
|
|
28
|
+
*
|
|
29
|
+
* Never throws: a throw here would be swallowed by the listing's broad
|
|
30
|
+
* `catch {}` and drop the package silently. Non-semver input is a returned
|
|
31
|
+
* "unknown", not an exception.
|
|
32
|
+
*/
|
|
33
|
+
export function classifyInstalledVersion(installed, latest) {
|
|
34
|
+
if (!semver.valid(installed) || !semver.valid(latest))
|
|
35
|
+
return "unknown";
|
|
36
|
+
if (semver.gt(installed, latest))
|
|
37
|
+
return "ahead";
|
|
38
|
+
if (semver.eq(installed, latest))
|
|
39
|
+
return "current";
|
|
40
|
+
return "outdated";
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Icon for a status line. "ahead" uses the affirmative (✅) class, NOT the ⬆️
|
|
44
|
+
* upgrade arrow — an install ahead of latest is not an upgrade.
|
|
45
|
+
*/
|
|
46
|
+
export function upgradeStatusIcon(status) {
|
|
47
|
+
switch (status) {
|
|
48
|
+
case "current":
|
|
49
|
+
case "ahead":
|
|
50
|
+
return "✅";
|
|
51
|
+
case "outdated":
|
|
52
|
+
return "⬆️";
|
|
53
|
+
case "optional":
|
|
54
|
+
return "○";
|
|
55
|
+
case "unknown":
|
|
56
|
+
case "missing":
|
|
57
|
+
return "❔";
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Whether a package's status line should be printed in the default `flair
|
|
62
|
+
* upgrade` listing. Suppresses optional-because-openclaw-is-absent lines — pure
|
|
63
|
+
* noise on machines without openclaw — unless `--all` (showAll) is set. All
|
|
64
|
+
* other statuses always print.
|
|
65
|
+
*/
|
|
66
|
+
export function shouldPrintUpgradeLine(status, showAll) {
|
|
67
|
+
if (status === "optional" && !showAll)
|
|
68
|
+
return false;
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The human-readable suffix for a package status line in `flair upgrade` /
|
|
73
|
+
* `flair upgrade --check` output.
|
|
74
|
+
*
|
|
75
|
+
* "ahead" and "unknown" carry NO remedy: an install ahead of latest is not
|
|
76
|
+
* nudged anywhere (flair#1778), and an unparseable version renders inline.
|
|
77
|
+
*
|
|
78
|
+
* flair-mcp is zero-install via npx — its suffix must never suggest a global
|
|
79
|
+
* install (flair#1168).
|
|
80
|
+
*/
|
|
81
|
+
export function upgradeStatusSuffix(name, status) {
|
|
82
|
+
if (status === "current")
|
|
83
|
+
return " (current)";
|
|
84
|
+
if (status === "ahead" || status === "unknown")
|
|
85
|
+
return "";
|
|
86
|
+
if (status === "missing") {
|
|
87
|
+
return name === FLAIR_MCP_PACKAGE
|
|
88
|
+
? " (zero-install via npx — run: flair doctor --fix)"
|
|
89
|
+
: " (run: npm install -g)";
|
|
90
|
+
}
|
|
91
|
+
if (status === "optional")
|
|
92
|
+
return " (install via: openclaw plugins install @tpsdev-ai/openclaw-flair)";
|
|
93
|
+
// flair-mcp is refreshed by re-pinning its wiring, never `npm install -g` —
|
|
94
|
+
// a global bin does nothing for an `npx -y -p @tpsdev-ai/flair-mcp`
|
|
95
|
+
// invocation (flair#1208). The re-pin is `flair upgrade`'s own job (the
|
|
96
|
+
// #1135/#1167 pin refresh) — never advise `doctor --fix` for it
|
|
97
|
+
// (flair#1324).
|
|
98
|
+
if (status === "outdated" && name === FLAIR_MCP_PACKAGE) {
|
|
99
|
+
return " (npx-wired — flair upgrade refreshes the pin)";
|
|
100
|
+
}
|
|
101
|
+
return "";
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The full listing line for one package. "ahead" renders with NO arrow and NO
|
|
105
|
+
* remedy: `<installed> (ahead of latest <latest>)`. "unknown" renders the raw
|
|
106
|
+
* string with no arrow and no remedy.
|
|
107
|
+
*/
|
|
108
|
+
export function formatUpgradeStatusLine(line) {
|
|
109
|
+
const icon = upgradeStatusIcon(line.status);
|
|
110
|
+
if (line.status === "ahead") {
|
|
111
|
+
return ` ${icon} ${line.name}: ${line.installed ?? "unknown"} (ahead of latest ${line.latest})`;
|
|
112
|
+
}
|
|
113
|
+
if (line.status === "unknown") {
|
|
114
|
+
return ` ${icon} ${line.name}: ${line.installed ?? "unknown"} (unknown)`;
|
|
115
|
+
}
|
|
116
|
+
const label = line.installed
|
|
117
|
+
?? (line.status === "optional" ? "not installed (openclaw not detected)" : "not detected");
|
|
118
|
+
return ` ${icon} ${line.name}: ${label} → ${line.latest}${line.suffix ?? ""}`;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Compare two pin/version strings with STRICT semver, or null when either side
|
|
122
|
+
* is absent or not strict semver (a comparison we cannot make). Pure — the ONE
|
|
123
|
+
* place ordering is derived from.
|
|
124
|
+
*
|
|
125
|
+
* Strictness is `isStrictSemver` (the one definition, flair#1692): `semver.valid`
|
|
126
|
+
* alone NORMALISES a leading "v" (`semver.valid("v0.54.0") === "0.54.0"`), which
|
|
127
|
+
* would let a non-canonical pin compare as if it were canonical. The guard's
|
|
128
|
+
* contract must not depend on every caller normalising first (flair#1789 C1).
|
|
129
|
+
*/
|
|
130
|
+
export function comparePinVersions(a, b) {
|
|
131
|
+
if (!a || !b)
|
|
132
|
+
return null;
|
|
133
|
+
if (!isStrictSemver(a) || !isStrictSemver(b))
|
|
134
|
+
return null;
|
|
135
|
+
return semver.compare(a, b);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The never-lower guard's decision: would writing `next` over `existing` be
|
|
139
|
+
* UNSAFE? TRUE when the write would LOWER the pin (`next < existing`) OR when
|
|
140
|
+
* either side is not strict semver — an unreadable pin cannot be PROVEN safe,
|
|
141
|
+
* so the guard holds. FAILS CLOSED: a pin is rewritten only when the write is
|
|
142
|
+
* provably not a lowering.
|
|
143
|
+
*
|
|
144
|
+
* Named for what it decides, deliberately NOT a boolean called "downgrade":
|
|
145
|
+
* one that returned false for "cannot tell" is exactly the hole this closes
|
|
146
|
+
* (flair#1778). `null`/absent `existing` is not a lowering — there is no pin to
|
|
147
|
+
* lower, so an unpinned hook/client is still (re)written.
|
|
148
|
+
*
|
|
149
|
+
* Used by the pin refresh (flair#1778 D4): the refresh must never rewrite an
|
|
150
|
+
* owned pin to a lower version — or to anything at all it cannot read.
|
|
151
|
+
*/
|
|
152
|
+
export function pinWriteWouldLowerOrIsUnknown(existing, next) {
|
|
153
|
+
// CONTRACT (flair#1789 C2): an ABSENT `existing` is nothing to lower — return
|
|
154
|
+
// false (a first write, or an unpinned hook/client) even when `next` is
|
|
155
|
+
// unresolved, because an unresolved `next` yields an UNPINNED spec by design
|
|
156
|
+
// (mcpServerSpec falls back to the bare package). The guard only fails closed
|
|
157
|
+
// when a pin IS present.
|
|
158
|
+
if (!existing)
|
|
159
|
+
return false;
|
|
160
|
+
const cmp = comparePinVersions(existing, next);
|
|
161
|
+
if (cmp === null)
|
|
162
|
+
return true; // cannot compare => fail closed
|
|
163
|
+
return cmp > 0; // existing > next <=> writing next would LOWER the pin
|
|
164
|
+
}
|