@tpsdev-ai/flair 0.55.1 → 0.56.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.
- package/README.md +3 -3
- package/dist/bridges/builtins/markdown.js +2 -2
- package/dist/bridges/discover.js +2 -2
- package/dist/bridges/runtime/allow-list.js +2 -2
- package/dist/build-info.json +3 -3
- package/dist/cli.js +201 -95
- package/dist/commands/attention.js +2 -2
- package/dist/commands/backup.js +2 -2
- package/dist/commands/doctor.js +168 -37
- package/dist/commands/export.js +2 -2
- package/dist/commands/federation.js +229 -95
- package/dist/commands/hook.js +4 -4
- package/dist/commands/init.js +94 -70
- package/dist/commands/mcp.js +2 -2
- package/dist/commands/migrate-harness-memory.js +8 -8
- package/dist/commands/rem.js +31 -4
- package/dist/commands/service.js +25 -1
- package/dist/commands/session.js +2 -2
- package/dist/commands/uninstall.js +2 -2
- package/dist/commands/upgrade.js +88 -69
- package/dist/doctor-client.js +341 -161
- package/dist/engine-version.js +2 -2
- package/dist/federation/scheduler.js +5 -5
- package/dist/hook-install.js +359 -176
- package/dist/install/clients.js +908 -214
- package/dist/keystore.js +10 -2
- package/dist/lib/auth-resolve.js +5 -5
- package/dist/lib/config-critical-section.js +557 -0
- package/dist/lib/doctor-config-path.js +2 -2
- package/dist/lib/doctor-run.js +107 -27
- package/dist/lib/flair-paths.js +29 -0
- package/dist/lib/home.js +60 -0
- package/dist/lib/launchd-repair.js +109 -0
- package/dist/lib/mcp-enable.js +4 -4
- package/dist/lib/owned-pins.js +256 -94
- package/dist/lib/pin-write-guard.js +87 -0
- package/dist/lib/settings-bytes.js +85 -0
- package/dist/lib/socket-path-limit.js +120 -0
- package/dist/lib/uninstall-purge.js +2 -18
- package/dist/lib/upgrade-plain-tree.js +2 -2
- package/dist/lib/upgrade-status.js +164 -0
- package/dist/lib/wiring-spec.js +238 -0
- package/dist/mcp-client-assertion.js +3 -3
- package/dist/rem/promote-policy.js +102 -0
- package/dist/rem/runner.js +3 -3
- package/dist/rem/scheduler.js +5 -5
- package/dist/rem/snapshot.js +2 -2
- package/dist/resources/MemoryReflect.js +10 -0
- package/dist/resources/OAuth.js +62 -11
- package/dist/resources/Presence.js +135 -47
- package/dist/resources/admin-layout.js +7 -2
- package/dist/resources/auto-promote-lib.js +56 -0
- package/dist/resources/health.js +26 -0
- package/dist/resources/memory-reflect-lib.js +215 -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/keystore.js +10 -2
- package/dist/src/lib/home.js +60 -0
- package/dist/src/rem/promote-policy.js +102 -0
- package/dist/src/rem/scheduler.js +5 -5
- package/dist/version-check.js +2 -2
- package/dist/version-handshake.js +2 -2
- package/docs/api-reference.md +31 -6
- package/docs/auth.md +14 -2
- package/docs/deploying-on-fabric.md +5 -4
- package/docs/deployment.md +26 -37
- package/docs/embedding-in-a-harper-app.md +4 -2
- package/docs/entity-vocabulary.md +5 -5
- package/docs/federation.md +58 -19
- package/docs/hosted-on-fabric.md +7 -5
- package/docs/mcp-clients.md +29 -12
- package/docs/releasing.md +66 -22
- package/docs/spoke-bringup.md +41 -22
- package/docs/standalone-local.md +12 -16
- package/package.json +3 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { join, resolve } from "node:path";
|
|
2
|
+
/**
|
|
3
|
+
* socket-path-limit.ts — the OS cap on a Unix domain socket's path.
|
|
4
|
+
*
|
|
5
|
+
* A Unix domain socket (the Harper operations API's `operations-server` socket)
|
|
6
|
+
* is bind()ed at a filesystem path, and that path is stored in a
|
|
7
|
+
* `struct sockaddr_un` whose `sun_path` field is a fixed-size buffer. The
|
|
8
|
+
* kernel counts the trailing NUL terminator, so the USABLE path length is one
|
|
9
|
+
* byte shorter than the field size:
|
|
10
|
+
*
|
|
11
|
+
* - macOS (darwin): `sun_path` is 104 bytes -> 103 usable
|
|
12
|
+
* - Linux: `sun_path` is 108 bytes -> 107 usable
|
|
13
|
+
*
|
|
14
|
+
* (darwin and freebsd: <sys/un.h>'s `SUNPATH_MAX`; linux: un(7) / the 108-byte
|
|
15
|
+
* `sa_data` minus the NUL. See also the macOS `listen() EINVAL` behaviour
|
|
16
|
+
* for a path that overflows the buffer.)
|
|
17
|
+
*
|
|
18
|
+
* Harper's operations API socket lives at `<data-dir>/operations-server`, so a
|
|
19
|
+
* long `flair init --data-dir <dir>` can push it past the cap, and `listen`
|
|
20
|
+
* dies with a bare `EINVAL` that names neither the socket nor the limit
|
|
21
|
+
* (flair#916). These pure predicates let callers refuse a too-long data dir
|
|
22
|
+
* BEFORE anything is written to disk, and say exactly what is wrong and how to
|
|
23
|
+
* fix it.
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* The suffix appended to a data dir to form the operations-socket path,
|
|
27
|
+
* INCLUDING the leading separator. A single 18-byte constant — the part of the
|
|
28
|
+
* socket path that no amount of shortening the data dir can remove.
|
|
29
|
+
*/
|
|
30
|
+
export const OPS_SOCKET_SUFFIX = "/operations-server";
|
|
31
|
+
/**
|
|
32
|
+
* The usable (NUL-excluded) maximum length of a Unix socket path, in BYTES,
|
|
33
|
+
* for a platform. macOS (darwin) and FreeBSD both have a 104-byte sun_path,
|
|
34
|
+
* so 103 usable; Linux has a 108-byte sun_path, so 107 usable. Any unknown
|
|
35
|
+
* platform takes the Linux limit of 107.
|
|
36
|
+
* Unknown platforms fall back to 107 because no real platform is looser than
|
|
37
|
+
* Linux's — falling back lower would refuse a path that is actually legal.
|
|
38
|
+
*/
|
|
39
|
+
export function socketPathLimit(platform) {
|
|
40
|
+
return platform === "darwin" || platform === "freebsd" ? 103 : 107;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Measure a socket path in BYTES (not characters — a multibyte path counts each
|
|
44
|
+
* byte of its UTF-8 encoding) against its platform's `sun_path`-derived limit.
|
|
45
|
+
* Returns OK when `bytes <= limit`; otherwise a refusal.
|
|
46
|
+
*/
|
|
47
|
+
export function checkSocketPathLength(path, platform) {
|
|
48
|
+
const limit = socketPathLimit(platform);
|
|
49
|
+
const bytes = Buffer.byteLength(path, "utf8");
|
|
50
|
+
if (bytes <= limit)
|
|
51
|
+
return { ok: true, bytes, limit };
|
|
52
|
+
return { ok: false, bytes, limit, over: bytes - limit };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Type guard: true when a socket-path check is a refusal (the path overflowed
|
|
56
|
+
* its platform's limit). An explicit type predicate — not control-flow
|
|
57
|
+
* narrowing — so it narrows the refusal branch even under `strict: false`
|
|
58
|
+
* (tsconfig.cli.json turns off strictNullChecks, which disables discriminated
|
|
59
|
+
* union narrowing).
|
|
60
|
+
*/
|
|
61
|
+
export function isSocketPathRefusal(check) {
|
|
62
|
+
return !check.ok;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* An actionable refusal message for a socket path that exceeds its platform's
|
|
66
|
+
* `sun_path` limit. Names the actual constraint — the socket path itself, its
|
|
67
|
+
* byte length, the platform limit — and the remedy: how many bytes shorter the
|
|
68
|
+
* `--data-dir` must be (the `/operations-server` suffix is fixed, so the data
|
|
69
|
+
* dir is the only part that can move).
|
|
70
|
+
*
|
|
71
|
+
* Pure: it only formats. The caller decides whether to print and exit.
|
|
72
|
+
*/
|
|
73
|
+
export function socketPathTooLongMessage(socketPath, dataDir, check) {
|
|
74
|
+
const [head, ...tail] = [
|
|
75
|
+
"Error: the Harper operations socket path is too long for this OS — Harper",
|
|
76
|
+
"would die with a bare `listen EINVAL` instead. Refusing before touching disk.",
|
|
77
|
+
"",
|
|
78
|
+
` Operations socket: ${JSON.stringify(socketPath)}`,
|
|
79
|
+
` Path length: ${check.bytes} bytes`,
|
|
80
|
+
` OS limit: ${check.limit} bytes (Unix-domain-socket sun_path, counting the trailing NUL)`,
|
|
81
|
+
` Data directory: ${JSON.stringify(dataDir)}`,
|
|
82
|
+
"",
|
|
83
|
+
"The socket is always <data-dir>/operations-server, so that suffix is fixed.",
|
|
84
|
+
"Choose a --data-dir that is at least " +
|
|
85
|
+
`${check.over} byte${check.over === 1 ? "" : "s"} shorter, so the socket path` +
|
|
86
|
+
" fits within " +
|
|
87
|
+
`${check.limit} bytes.`,
|
|
88
|
+
];
|
|
89
|
+
return head + "\n" + tail.join("\n");
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* CLI preflight for the operations socket. Builds the socket path from a data
|
|
93
|
+
* directory exactly as Harper binds it (`<data-dir>/operations-server`) and,
|
|
94
|
+
* unless it fits the platform's `sun_path` limit, returns the actionable refusal
|
|
95
|
+
* message. Returns `null` when the path fits.
|
|
96
|
+
*
|
|
97
|
+
* The data dir is resolved to an ABSOLUTE path BEFORE measuring, because Harper
|
|
98
|
+
* binds the socket relative to its own cwd (the flair package directory), not
|
|
99
|
+
* ours — a relative `--data-dir r` would otherwise be measured as the short
|
|
100
|
+
* `r/operations-server` while Harper binds a much longer path. The refusal
|
|
101
|
+
* message prints the RESOLVED path and the resolved data dir.
|
|
102
|
+
*
|
|
103
|
+
* Callers that run it: `flair init` (which accepts `--data-dir`) and
|
|
104
|
+
* `flair start` / `flair restart` (defensive — the default data dir is short).
|
|
105
|
+
* NOT covered here: `flair snapshot create` and the launchd repair path, which
|
|
106
|
+
* hand Harper a socket path without this check; an over-long data dir cannot
|
|
107
|
+
* boot in either case, so it is not a live bug.
|
|
108
|
+
*
|
|
109
|
+
* Pure — it only builds and formats; the caller decides whether to print and
|
|
110
|
+
* exit, so it is unit-testable without `process.exit`.
|
|
111
|
+
*/
|
|
112
|
+
export function opsSocketPathRefusal(dataDir, platform) {
|
|
113
|
+
const resolvedDataDir = resolve(dataDir);
|
|
114
|
+
const socketPath = join(resolvedDataDir, OPS_SOCKET_SUFFIX);
|
|
115
|
+
const check = checkSocketPathLength(socketPath, platform);
|
|
116
|
+
if (isSocketPathRefusal(check)) {
|
|
117
|
+
return socketPathTooLongMessage(socketPath, resolvedDataDir, check);
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
* "fully purged" when anything remains.
|
|
16
16
|
*/
|
|
17
17
|
import { existsSync, rmSync } from "node:fs";
|
|
18
|
-
import { homedir } from "node:os";
|
|
19
18
|
import { join } from "node:path";
|
|
19
|
+
import { resolveHome, withHome } from "./home.js";
|
|
20
20
|
import { ALL_CLIENTS } from "../install/clients.js";
|
|
21
21
|
import { uninstallContinuityHooks, uninstallHook, harnessSupportsContinuity, hookBackupPath, hookSettingsPath, SUPPORTED_HARNESSES, } from "../hook-install.js";
|
|
22
22
|
import { disableScheduler as disableRemScheduler, LAUNCHD_LABEL as REM_LAUNCHD_LABEL, SYSTEMD_SERVICE_UNIT as REM_SYSTEMD_SERVICE, SYSTEMD_TIMER_UNIT as REM_SYSTEMD_TIMER, } from "../rem/scheduler.js";
|
|
@@ -24,22 +24,6 @@ import { disableScheduler as disableFederationScheduler, LAUNCHD_LABEL as FED_LA
|
|
|
24
24
|
/** The published CLI package. Named here so the leftover line cannot drift
|
|
25
25
|
* from the remedy command. */
|
|
26
26
|
export const FLAIR_NPM_PACKAGE = "@tpsdev-ai/flair";
|
|
27
|
-
function resolveHome(homeDir) {
|
|
28
|
-
return homeDir ?? process.env.HOME ?? process.env.USERPROFILE ?? homedir();
|
|
29
|
-
}
|
|
30
|
-
function withHome(homeDir, fn) {
|
|
31
|
-
const prev = process.env.HOME;
|
|
32
|
-
process.env.HOME = homeDir;
|
|
33
|
-
try {
|
|
34
|
-
return fn();
|
|
35
|
-
}
|
|
36
|
-
finally {
|
|
37
|
-
if (prev === undefined)
|
|
38
|
-
delete process.env.HOME;
|
|
39
|
-
else
|
|
40
|
-
process.env.HOME = prev;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
27
|
function displayUnderHome(homeDir, absPath) {
|
|
44
28
|
if (absPath.startsWith(homeDir))
|
|
45
29
|
return "~" + absPath.slice(homeDir.length);
|
|
@@ -127,7 +111,7 @@ function sweepHookBackup(settingsPath, reportedBackup, homeDir, removed, leftove
|
|
|
127
111
|
* unless `omitNpmLeftover` is set.
|
|
128
112
|
*/
|
|
129
113
|
export function purgeFlairInstall(opts = {}) {
|
|
130
|
-
const homeDir =
|
|
114
|
+
const homeDir = opts.homeDir ?? resolveHome();
|
|
131
115
|
const skipUnload = !!opts.skipSchedulerUnload;
|
|
132
116
|
const removed = [];
|
|
133
117
|
const leftovers = [];
|
|
@@ -19,10 +19,10 @@
|
|
|
19
19
|
*/
|
|
20
20
|
import { existsSync, mkdirSync, renameSync, rmSync, cpSync, readdirSync, readFileSync, statSync, lstatSync, realpathSync, } from "node:fs";
|
|
21
21
|
import { basename, dirname, join, resolve } from "node:path";
|
|
22
|
-
import { homedir } from "node:os";
|
|
23
22
|
import { execFileSync } from "node:child_process";
|
|
24
23
|
import { extract as tarExtract } from "tar";
|
|
25
24
|
import { canonicalPath, findFlairPackageDir, readFlairPackageAt, sameInstallPath, } from "./upgrade-exec-path.js";
|
|
25
|
+
import { resolveHome } from "./home.js";
|
|
26
26
|
export const FLAIR_PACKAGE = "@tpsdev-ai/flair";
|
|
27
27
|
/** Sibling suffix for the staged new tree (same filesystem as the live tree). */
|
|
28
28
|
export const UPGRADE_NEXT_SUFFIX = "upgrade-next";
|
|
@@ -525,7 +525,7 @@ export function unitTextMentionsTree(unitText, treeDir) {
|
|
|
525
525
|
return false;
|
|
526
526
|
}
|
|
527
527
|
const SYSTEM_UNIT_DIRS = ["/etc/systemd/system"];
|
|
528
|
-
export function userSystemdDir(home =
|
|
528
|
+
export function userSystemdDir(home = resolveHome()) {
|
|
529
529
|
return join(home, ".config", "systemd", "user");
|
|
530
530
|
}
|
|
531
531
|
/**
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wiring-spec.ts — the interim wiring-spec model (flair#1778 slice 2c-i-a1).
|
|
3
|
+
*
|
|
4
|
+
* ONE decoder + ONE partition for "what version does this wiring entry name?",
|
|
5
|
+
* replacing the extractor that reported a range/tag/unsupported source as
|
|
6
|
+
* `null` — i.e. as if the entry were ABSENT. That null is the bug: every guard
|
|
7
|
+
* downstream reads `null` as "no pin to protect" and OVERWRITES the spec (a
|
|
8
|
+
* `@^0.55.0` range, a `@latest` tag, a `file:` source).
|
|
9
|
+
*
|
|
10
|
+
* A LEAF module on purpose: it imports only `semver` and `isStrictSemver`
|
|
11
|
+
* (`./npm-registry.js`), so every consumer — doctor-client.ts (which owns the
|
|
12
|
+
* old extractor), owned-pins.ts, install/clients.ts — can migrate to it with
|
|
13
|
+
* no import cycle now or later.
|
|
14
|
+
*
|
|
15
|
+
* This PR adds the type and migrates consumers. It does NOT add the writers'
|
|
16
|
+
* guards (that is 2c-i-a2). The one behaviour change here is deliberate and
|
|
17
|
+
* named: a range/tag/unsupported/malformed spec is now PRESENT-BUT-
|
|
18
|
+
* NOT-COMPARABLE instead of null-therefore-absent; `version` and `none` tokens
|
|
19
|
+
* keep every caller's current outcome.
|
|
20
|
+
*
|
|
21
|
+
* ─── Partition rule (each step total, evaluated in this order) ─────────────
|
|
22
|
+
* 1. the spec's source is `file:` / `link:` / `git:` / `git+https:` /
|
|
23
|
+
* `github:` / `http(s):` → `unsupported`
|
|
24
|
+
* 2. no token after `@` (or nothing after `npm:`/`workspace:`) → `none`
|
|
25
|
+
* 3. the token matches /^v\d/ → `malformed`
|
|
26
|
+
* (checked BEFORE range recognition: semver's `validRange` ACCEPTS
|
|
27
|
+
* `v0.55.0`, so a range test first would misclassify it. The leading `v`
|
|
28
|
+
* is NOT normalised away — it is exactly what makes the token
|
|
29
|
+
* non-canonical, and normalising it is how `@v0.55.0` used to be read
|
|
30
|
+
* as the version `0.55.0`.)
|
|
31
|
+
* 4. `isStrictSemver(token)` → `version`
|
|
32
|
+
* (strict `X.Y.Z[-prerelease][+build]`, no leading `v`; a prerelease like
|
|
33
|
+
* `0.56.0-rc.1` IS a version.)
|
|
34
|
+
* 5. `semver.validRange(token)` or a dist-tag word
|
|
35
|
+
* (`latest` / `next` / `beta` / `alpha` / `rc` / `canary` / `stable` /
|
|
36
|
+
* `dev` / `experimental` / `*`) → `range-or-tag`
|
|
37
|
+
* (this catches `0.55`, `~0.55`, `^0.55.0`, `>=1 <2`; the tag set is the
|
|
38
|
+
* npm-conventional one — flair#1809 review carry-over — so `@beta`
|
|
39
|
+
* classifies as a tag rather than `malformed`. Either way it is
|
|
40
|
+
* PRESENT-BUT-NOT-COMPARABLE; only the label differs.)
|
|
41
|
+
* 6. anything else (a 4-part `1.2.3.4`, garbage) → `malformed`
|
|
42
|
+
*
|
|
43
|
+
* `raw` is always kept, so a consumer can preserve the exact bytes it read.
|
|
44
|
+
*/
|
|
45
|
+
import semver from "semver";
|
|
46
|
+
import { isStrictSemver } from "./npm-registry.js";
|
|
47
|
+
/** A source spec that is never an installable version pin. */
|
|
48
|
+
const UNSUPPORTED_SOURCE_RE = /^(?:file:|link:|git:|git\+https:|git\+ssh:|github:|https?:)/;
|
|
49
|
+
/** The dist-tag words npm resolves by name, plus the `*` wildcard. */
|
|
50
|
+
const DIST_TAG_WORDS = new Set([
|
|
51
|
+
"latest",
|
|
52
|
+
"next",
|
|
53
|
+
"beta",
|
|
54
|
+
"alpha",
|
|
55
|
+
"rc",
|
|
56
|
+
"canary",
|
|
57
|
+
"stable",
|
|
58
|
+
"dev",
|
|
59
|
+
"experimental",
|
|
60
|
+
"*",
|
|
61
|
+
]);
|
|
62
|
+
/** Strip the `workspace:` / `npm:` protocol from a dependency-field value. */
|
|
63
|
+
function stripSpecProtocol(spec) {
|
|
64
|
+
return spec.replace(/^(?:workspace|npm):/, "");
|
|
65
|
+
}
|
|
66
|
+
/** Partition one decoded spec token. Total; see the module doc for the rule. */
|
|
67
|
+
export function partitionWiringToken(rawToken) {
|
|
68
|
+
const token = (rawToken ?? "").trim();
|
|
69
|
+
if (UNSUPPORTED_SOURCE_RE.test(token))
|
|
70
|
+
return { kind: "unsupported", value: token };
|
|
71
|
+
if (token === "")
|
|
72
|
+
return { kind: "none", value: null };
|
|
73
|
+
if (/^v\d/.test(token))
|
|
74
|
+
return { kind: "malformed", value: token };
|
|
75
|
+
if (isStrictSemver(token))
|
|
76
|
+
return { kind: "version", value: token };
|
|
77
|
+
if (DIST_TAG_WORDS.has(token) || semver.validRange(token) !== null) {
|
|
78
|
+
return { kind: "range-or-tag", value: token };
|
|
79
|
+
}
|
|
80
|
+
return { kind: "malformed", value: token };
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The token terminators for a `<pkg>@<token>` spec: whitespace, `"`, `'`, `,`,
|
|
84
|
+
* `]` (the delimiters an args array / hook command / TOML line can use).
|
|
85
|
+
*/
|
|
86
|
+
function isTokenTerminator(ch) {
|
|
87
|
+
return ch === undefined || ch === "" || /\s/.test(ch) || ch === '"' || ch === "'" || ch === "," || ch === "]";
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* True when the match at `idx` starts at a token boundary — the start of the
|
|
91
|
+
* text, or a non-identifier character before it. This keeps `<pkg>@` from
|
|
92
|
+
* matching inside a LONGER sibling package name (e.g. `@tpsdev-ai/flair-mcp`
|
|
93
|
+
* inside `@tpsdev-ai/flair-mcp-extra@1.0`), or a name that merely ends with it.
|
|
94
|
+
*/
|
|
95
|
+
function isTokenStart(s, idx) {
|
|
96
|
+
if (idx <= 0)
|
|
97
|
+
return true;
|
|
98
|
+
// A preceding "/" means this match is the TAIL OF A SCOPED SIBLING, not the
|
|
99
|
+
// package: in `npm:@other/@tpsdev-ai/flair-mcp@1.0.0` the `flair-mcp@` match
|
|
100
|
+
// begins right after the sibling scope's "/". Every pkg constant today is
|
|
101
|
+
// scoped (@tpsdev-ai/…), so the gap is unreachable in practice, but an
|
|
102
|
+
// unscoped caller would otherwise read the sibling's pinned tail as its own
|
|
103
|
+
// (flair#1809 review carry-over).
|
|
104
|
+
if (s[idx - 1] === "/")
|
|
105
|
+
return false;
|
|
106
|
+
return !/[A-Za-z0-9_\-.]/.test(s[idx - 1]);
|
|
107
|
+
}
|
|
108
|
+
/** Read from `from` up to the first token terminator. */
|
|
109
|
+
function readSpecToken(s, from) {
|
|
110
|
+
let i = from;
|
|
111
|
+
while (i < s.length && !isTokenTerminator(s[i]))
|
|
112
|
+
i++;
|
|
113
|
+
return s.slice(from, i);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Envelope A — every `<pkg>@<token>` occurrence, found by a plain `indexOf`
|
|
117
|
+
* scan (NO dynamic RegExp). One decoder for all of them: an
|
|
118
|
+
* `npx -y -p <pkg>@<spec>` command, an MCP client `args` array, a Codex TOML
|
|
119
|
+
* `args = [...]` line, a SessionStart hook command, or pi's
|
|
120
|
+
* `npm:<pkg>@<spec>` source all reduce to the same `<pkg>@<spec>` substring.
|
|
121
|
+
*/
|
|
122
|
+
function findPackageSpecs(text, pkg) {
|
|
123
|
+
const needle = `${pkg}@`;
|
|
124
|
+
const out = [];
|
|
125
|
+
let from = 0;
|
|
126
|
+
for (;;) {
|
|
127
|
+
const idx = text.indexOf(needle, from);
|
|
128
|
+
if (idx === -1)
|
|
129
|
+
break;
|
|
130
|
+
if (isTokenStart(text, idx)) {
|
|
131
|
+
const token = readSpecToken(text, idx + needle.length);
|
|
132
|
+
out.push({ raw: needle + token, token });
|
|
133
|
+
}
|
|
134
|
+
from = idx + needle.length;
|
|
135
|
+
}
|
|
136
|
+
return out;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Envelope B — a dependency field `"pkg": "<spec>"` (package.json), whose
|
|
140
|
+
* value may carry the `workspace:` / `npm:` protocol (`workspace:^0.55.0` →
|
|
141
|
+
* `^0.55.0`). Found by string operations: the quoted key, optional whitespace,
|
|
142
|
+
* `:`, optional whitespace, a `"`, then the value up to the next `"`.
|
|
143
|
+
*/
|
|
144
|
+
function decodeDependencyField(text, pkg) {
|
|
145
|
+
const key = `"${pkg}"`;
|
|
146
|
+
let from = 0;
|
|
147
|
+
for (;;) {
|
|
148
|
+
const idx = text.indexOf(key, from);
|
|
149
|
+
if (idx === -1)
|
|
150
|
+
return null;
|
|
151
|
+
if (isTokenStart(text, idx)) {
|
|
152
|
+
let i = idx + key.length;
|
|
153
|
+
while (i < text.length && /\s/.test(text[i]))
|
|
154
|
+
i++;
|
|
155
|
+
if (text[i] === ":") {
|
|
156
|
+
i++;
|
|
157
|
+
while (i < text.length && /\s/.test(text[i]))
|
|
158
|
+
i++;
|
|
159
|
+
if (text[i] === '"') {
|
|
160
|
+
const valueStart = i + 1;
|
|
161
|
+
const end = text.indexOf('"', valueStart);
|
|
162
|
+
if (end === -1)
|
|
163
|
+
return null;
|
|
164
|
+
return {
|
|
165
|
+
raw: text.slice(idx, end + 1),
|
|
166
|
+
pkg,
|
|
167
|
+
token: partitionWiringToken(stripSpecProtocol(text.slice(valueStart, end))),
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
from = idx + key.length;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Decode ONE wiring string into a WiringSpec, or null when `pkg` is not wired
|
|
177
|
+
* in it. Total: every envelope reduces to `<pkg>@<spec>` or a bare package.
|
|
178
|
+
* A bare package (no `@`) is `kind: "none"` — wired, no token.
|
|
179
|
+
*/
|
|
180
|
+
export function decodeWiringSpec(text, pkg) {
|
|
181
|
+
if (typeof text !== "string" || !text.includes(pkg))
|
|
182
|
+
return null;
|
|
183
|
+
const first = findPackageSpecs(text, pkg)[0];
|
|
184
|
+
if (first)
|
|
185
|
+
return { raw: first.raw, pkg, token: partitionWiringToken(first.token) };
|
|
186
|
+
const byDep = decodeDependencyField(text, pkg);
|
|
187
|
+
if (byDep)
|
|
188
|
+
return byDep;
|
|
189
|
+
return { raw: pkg, pkg, token: { kind: "none", value: null } };
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Decode EVERY `<pkg>@<spec>` occurrence in `text` (plus a dependency field
|
|
193
|
+
* when present) — one WiringSpec per entry, never collapsing to the first, in
|
|
194
|
+
* TEXT ORDER. Useful to a caller that must enumerate all wired artifacts
|
|
195
|
+
* (flair#1778 I6).
|
|
196
|
+
*/
|
|
197
|
+
export function decodeWiringSpecs(text, pkg) {
|
|
198
|
+
if (typeof text !== "string" || !text.includes(pkg))
|
|
199
|
+
return [];
|
|
200
|
+
const out = [];
|
|
201
|
+
const seen = new Set();
|
|
202
|
+
for (const spec of findPackageSpecs(text, pkg)) {
|
|
203
|
+
if (seen.has(spec.raw))
|
|
204
|
+
continue;
|
|
205
|
+
seen.add(spec.raw);
|
|
206
|
+
out.push({ raw: spec.raw, pkg, token: partitionWiringToken(spec.token) });
|
|
207
|
+
}
|
|
208
|
+
const dep = decodeDependencyField(text, pkg);
|
|
209
|
+
if (dep && !seen.has(dep.raw))
|
|
210
|
+
out.push(dep);
|
|
211
|
+
if (out.length === 0)
|
|
212
|
+
out.push({ raw: pkg, pkg, token: { kind: "none", value: null } });
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* The pin string a consumer should treat as "what this wiring names".
|
|
217
|
+
*
|
|
218
|
+
* version → the version (compares as today)
|
|
219
|
+
* none (unpinned/absent) → null (today's outcome: nothing to protect)
|
|
220
|
+
* range-or-tag / unsupported / malformed
|
|
221
|
+
* → the token text — PRESENT, and not comparable.
|
|
222
|
+
*
|
|
223
|
+
* The last line is the whole point: feeding that text to
|
|
224
|
+
* `pinWriteWouldLowerOrIsUnknown` makes `comparePinVersions` return null, so the
|
|
225
|
+
* fail-closed guard HOLDS. Before this, those specs yielded null, which the
|
|
226
|
+
* guard reads as "absent — safe to write", and the entry was overwritten.
|
|
227
|
+
*/
|
|
228
|
+
export function wiringPinString(spec) {
|
|
229
|
+
if (!spec)
|
|
230
|
+
return null;
|
|
231
|
+
if (spec.token.kind === "none")
|
|
232
|
+
return null;
|
|
233
|
+
return spec.token.value ?? spec.raw;
|
|
234
|
+
}
|
|
235
|
+
/** True when the spec names a concrete, comparable version. */
|
|
236
|
+
export function isComparableWiringPin(spec) {
|
|
237
|
+
return spec?.token.kind === "version";
|
|
238
|
+
}
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
*/
|
|
55
55
|
import { createPrivateKey, createPublicKey, randomUUID, sign } from "node:crypto";
|
|
56
56
|
import { existsSync, readFileSync } from "node:fs";
|
|
57
|
-
import { homedir } from "node:os";
|
|
58
57
|
import { join } from "node:path";
|
|
58
|
+
import { resolveHome } from "./lib/home.js";
|
|
59
59
|
// ─── RFC 7523 constants ─────────────────────────────────────────────────────
|
|
60
60
|
/** RFC 7523 §2.2 value for `client_assertion_type`. */
|
|
61
61
|
export const CLIENT_ASSERTION_TYPE_JWT_BEARER = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer";
|
|
@@ -76,8 +76,8 @@ export function resolveAgentKeyPath(agentId, keysDirOverride) {
|
|
|
76
76
|
const candidates = [
|
|
77
77
|
keysDirOverride ? join(keysDirOverride, `${agentId}.key`) : null,
|
|
78
78
|
process.env.FLAIR_KEY_DIR ? join(process.env.FLAIR_KEY_DIR, `${agentId}.key`) : null,
|
|
79
|
-
join(
|
|
80
|
-
join(
|
|
79
|
+
join(resolveHome(), ".flair", "keys", `${agentId}.key`),
|
|
80
|
+
join(resolveHome(), ".tps", "secrets", "flair", `${agentId}-priv.key`),
|
|
81
81
|
].filter((p) => Boolean(p));
|
|
82
82
|
return candidates.find((p) => existsSync(p)) ?? null;
|
|
83
83
|
}
|