@tpsdev-ai/flair 0.57.0 → 0.58.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 +4 -4
- package/SECURITY.md +4 -1
- package/dist/build-info.json +3 -3
- package/dist/cli.js +1685 -279
- package/dist/commands/doctor.js +162 -4
- package/dist/commands/federation.js +2 -1
- package/dist/commands/hook.js +10 -2
- package/dist/commands/init.js +150 -44
- package/dist/commands/memory.js +24 -2
- package/dist/commands/quality.js +26 -18
- package/dist/commands/service.js +137 -22
- package/dist/commands/status.js +114 -3
- package/dist/commands/upgrade.js +204 -84
- package/dist/doctor-client.js +50 -4
- package/dist/federation/scheduler.js +253 -4
- package/dist/install/clients.js +89 -23
- package/dist/install/global-bin-path.js +26 -6
- package/dist/lib/atomic-write.js +256 -0
- package/dist/lib/daemon-liveness.js +61 -0
- package/dist/lib/doctor-run.js +5 -0
- package/dist/lib/embed-gpu-doctor.js +49 -26
- package/dist/lib/federation-memory-attributes.js +15 -0
- package/dist/lib/init-admin-pass.js +4 -2
- package/dist/lib/init-listener-environ.js +38 -0
- package/dist/lib/init-occupied-listener.js +165 -0
- package/dist/lib/launchd-domain-preflight.js +448 -0
- package/dist/lib/launchd-management.js +176 -6
- package/dist/lib/launchd-repair.js +95 -0
- package/dist/lib/memory-attributes.js +77 -0
- package/dist/lib/node-alias-path.js +95 -0
- package/dist/lib/npm-registry.js +119 -6
- package/dist/lib/owned-pins.js +20 -6
- package/dist/lib/service-repoint-apply.js +267 -0
- package/dist/lib/service-repoint.js +575 -0
- package/dist/lib/toml-string-array.js +104 -0
- package/dist/lib/tree-divergence.js +716 -0
- package/dist/lib/upgrade-plain-tree.js +18 -8
- package/dist/lib/upgrade-prior-liveness.js +69 -0
- package/dist/lib/upgrade-rollback.js +116 -0
- package/dist/rem/runner.js +11 -0
- package/dist/resources/Agent.js +27 -14
- package/dist/resources/AgentSeed.js +35 -1
- package/dist/resources/Federation.js +70 -52
- package/dist/resources/Memory.js +567 -72
- package/dist/resources/MemoryArchive.js +9 -0
- package/dist/resources/MemoryBootstrap.js +4 -0
- package/dist/resources/MemoryFeed.js +204 -3
- package/dist/resources/MemoryHostSource.js +90 -0
- package/dist/resources/MemoryMaintenance.js +121 -32
- package/dist/resources/MemoryReflect.js +4 -1
- package/dist/resources/MemoryReindex.js +7 -4
- package/dist/resources/MemoryUsage.js +53 -3
- package/dist/resources/Presence.js +18 -10
- package/dist/resources/RecordUsage.js +30 -40
- package/dist/resources/Relationship.js +167 -78
- package/dist/resources/SemanticSearch.js +19 -6
- package/dist/resources/Soul.js +31 -19
- package/dist/resources/XAA.js +7 -3
- package/dist/resources/agent-auth.js +16 -10
- package/dist/resources/auth-middleware.js +135 -48
- package/dist/resources/auto-promote-lib.js +4 -4
- package/dist/resources/bm25-index-service.js +246 -32
- package/dist/resources/bm25-status.js +132 -0
- package/dist/resources/ed25519-auth.js +11 -54
- package/dist/resources/embed-gpu.js +262 -47
- package/dist/resources/embeddings-boot.js +52 -17
- package/dist/resources/federation-classify.js +12 -0
- package/dist/resources/federation-crypto.js +65 -19
- package/dist/resources/health.js +57 -14
- package/dist/resources/host-pointer/registry.js +35 -0
- package/dist/resources/host-pointer-adapter.js +15 -0
- package/dist/resources/host-source-visibility.js +167 -0
- package/dist/resources/host-source.js +151 -0
- package/dist/resources/instance-identity.js +3 -2
- package/dist/resources/mcp-handler.js +147 -51
- package/dist/resources/mcp-tools.js +25 -21
- package/dist/resources/memory-consolidate-lib.js +3 -1
- package/dist/resources/memory-declared-attributes.js +103 -0
- package/dist/resources/memory-feed-lib.js +5 -1
- package/dist/resources/memory-host-source.js +163 -0
- package/dist/resources/migrations/synthetic-test-migration.js +4 -1
- package/dist/resources/migrations/visibility-backfill.js +4 -1
- package/dist/resources/originator-instance.js +235 -0
- package/dist/resources/promotion-stamp.js +2 -0
- package/dist/resources/provenance.js +111 -11
- package/dist/resources/record-owner-guard.js +19 -0
- package/dist/resources/record-type-kit.js +9 -12
- package/dist/resources/record-types.js +6 -6
- package/dist/resources/relay-ops.js +28 -3
- package/dist/resources/replay-store.js +289 -0
- package/dist/resources/request-transaction.js +78 -0
- package/dist/resources/search-readiness.js +19 -21
- package/dist/resources/semantic-retrieval-core.js +5 -1
- package/dist/resources/table-patch-policy.js +109 -0
- package/dist/resources/table-patches.js +17 -0
- package/dist/resources/table-post-policy.js +138 -0
- package/dist/resources/table-posts.js +22 -0
- package/dist/resources/table-subscription-policy.js +135 -0
- package/dist/resources/table-subscriptions.js +19 -0
- package/dist/resources/tool-descriptors/index.js +15 -15
- package/dist/resources/trust-block.js +10 -3
- package/dist/resources/usage-recording.js +169 -41
- package/dist/src/lib/memory-attributes.js +77 -0
- package/docs/api-reference.md +79 -15
- package/docs/claude-code.md +132 -0
- package/docs/deployment.md +1 -1
- package/docs/federation.md +1 -1
- package/docs/integrations.md +5 -5
- package/docs/mcp-clients.md +11 -21
- package/docs/notes/mcp-oauth-model2.md +15 -1
- package/docs/quickstart.md +5 -0
- package/docs/releasing.md +134 -2
- package/docs/supply-chain-policy.md +10 -5
- package/docs/troubleshooting.md +11 -0
- package/docs/upgrade.md +187 -61
- package/package.json +9 -5
- package/schemas/agent.graphql +9 -6
- package/schemas/federation.graphql +2 -11
- package/schemas/memory.graphql +106 -17
- package/schemas/replay.graphql +18 -0
- package/templates/launchd/start-flair-with-admin-pass.sh +22 -0
- package/dist/resources/federation-nonce-store.js +0 -195
|
@@ -0,0 +1,716 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tree-divergence.ts — "the CLI you ran and the instance you are talking to do
|
|
3
|
+
* not live in the same install tree" (flair#2034 §2).
|
|
4
|
+
*
|
|
5
|
+
* After a Node bump a service unit can keep serving the install tree of the
|
|
6
|
+
* runtime it was baked at, while the CLI on PATH comes from the tree of the
|
|
7
|
+
* runtime in use now. `flair status` then printed two hints that pointed at
|
|
8
|
+
* each other, and `flair upgrade` reported about a tree that was not the one
|
|
9
|
+
* serving. This module is the one shared answer to "which tree serves this
|
|
10
|
+
* instance, and does it match this CLI".
|
|
11
|
+
*
|
|
12
|
+
* PROOF, NOT INFERENCE. A unit file on disk says what a service manager WOULD
|
|
13
|
+
* run; it does not say what is serving. The serving tree is only reported when
|
|
14
|
+
* a service manager is shown to own the very process that answers:
|
|
15
|
+
*
|
|
16
|
+
* - macOS: the launchd plist for THIS data directory (its ROOTPATH is this
|
|
17
|
+
* data dir) has a running job whose PID is the serving PID;
|
|
18
|
+
* - Linux: the serving process's own cgroup (/proc/<pid>/cgroup) places it
|
|
19
|
+
* in a systemd USER unit of this user; systemd reports that PID as the
|
|
20
|
+
* unit's MainPID, and the unit's FragmentPath is a file of that name in
|
|
21
|
+
* this user's unit directory. The unit is found FROM THE PROCESS, never
|
|
22
|
+
* from a tree path in a unit file, so it is still found after `flair init`
|
|
23
|
+
* re-pointed the file at another tree (the restart-pending state).
|
|
24
|
+
*
|
|
25
|
+
* The serving PID is the process that ANSWERED (see identifyAnsweringPid): the
|
|
26
|
+
* one process listening on the instance's port, which must also be the PID the
|
|
27
|
+
* answering process reported about itself (`/HealthDetail`'s `pid`) when there
|
|
28
|
+
* is one. Harper's `hdb.pid` is never the answer on its own — it is a
|
|
29
|
+
* cross-check. Any disagreement, more than one listener, or nobody listening is
|
|
30
|
+
* UNKNOWN (a stale or reused PID file, a second instance on the port), never a
|
|
31
|
+
* guess. The tree is read from
|
|
32
|
+
* that process (its working directory / command line), never from the unit.
|
|
33
|
+
*
|
|
34
|
+
* Without that proof — a remote target, a directly started server, a
|
|
35
|
+
* system-level or unrelated supervisor, a server run under a different HOME —
|
|
36
|
+
* the tree is UNKNOWN, and no remedy is derived from it. A systemd unit runs a
|
|
37
|
+
* process only as its MainPID: a process that merely sits in a unit's cgroup
|
|
38
|
+
* (a descendant of a CI runner agent, a terminal multiplexer, an ssh session
|
|
39
|
+
* service) was started directly (see unitSupervision). A cgroup path that names
|
|
40
|
+
* its users or units inconsistently is contradictory evidence: no manager is
|
|
41
|
+
* asked about it, and it never leads to a restart (see cgroupOwner).
|
|
42
|
+
*
|
|
43
|
+
* Everything here is pure: the probe hands in every filesystem, process and
|
|
44
|
+
* service-manager read, so the whole decision is unit-testable without a real
|
|
45
|
+
* launchd, systemd or running instance.
|
|
46
|
+
*/
|
|
47
|
+
import { basename, dirname, resolve } from "node:path";
|
|
48
|
+
import semver from "semver";
|
|
49
|
+
import { classifyServiceNodePin, readPlistProgramRefs } from "./launchd-management.js";
|
|
50
|
+
import { unescapeXml } from "./xml-escape.js";
|
|
51
|
+
function unknown(reason) {
|
|
52
|
+
return { kind: "unknown", reason };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The process that answered for this instance — or why it cannot be named.
|
|
56
|
+
*
|
|
57
|
+
* The listener result decides first, and the reported PID (what the answering
|
|
58
|
+
* process said about itself) must agree with it:
|
|
59
|
+
*
|
|
60
|
+
* - exactly one listening PID: the answer — and a reported PID must equal it;
|
|
61
|
+
* - more than one listening PID, or NONE (an empty result means nobody is
|
|
62
|
+
* listening, never "could not read"): unknown, whatever was reported;
|
|
63
|
+
* - the listeners could not be read at all (lsof missing or failing): the
|
|
64
|
+
* reported PID stands on its own — it is the answering process's own
|
|
65
|
+
* report — and without one the answer is unknown.
|
|
66
|
+
*
|
|
67
|
+
* A live PID-file PID that is not the answer — a stale file whose PID was
|
|
68
|
+
* reused, a second instance — makes it unknown too: a conflict is never
|
|
69
|
+
* resolved by picking a side.
|
|
70
|
+
*/
|
|
71
|
+
export function identifyAnsweringPid(respondingPid, evidence) {
|
|
72
|
+
const listeners = evidence.listeners === null ? null : [...new Set(evidence.listeners)];
|
|
73
|
+
const pidFile = evidence.pidFile;
|
|
74
|
+
const reported = typeof respondingPid === "number" && Number.isInteger(respondingPid) && respondingPid > 0 ? respondingPid : null;
|
|
75
|
+
if (listeners !== null && listeners.length === 0)
|
|
76
|
+
return { reason: "no process is listening on the instance's port" };
|
|
77
|
+
if (listeners !== null && listeners.length > 1) {
|
|
78
|
+
return { reason: `more than one process listens on the instance's port (pids ${listeners.join(", ")})` };
|
|
79
|
+
}
|
|
80
|
+
const listener = listeners === null ? null : listeners[0];
|
|
81
|
+
if (reported !== null && listener !== null && reported !== listener) {
|
|
82
|
+
return {
|
|
83
|
+
reason: `the process that answered (pid ${reported}) is not the one listening on the instance's port (pid ${listener})`,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const pid = listener ?? reported;
|
|
87
|
+
if (pid === null) {
|
|
88
|
+
return { reason: "the process listening on the instance's port could not be read, and the instance reported no pid" };
|
|
89
|
+
}
|
|
90
|
+
if (pidFile !== null && pidFile !== pid) {
|
|
91
|
+
return {
|
|
92
|
+
reason: `the process ${listener !== null ? "listening on the instance's port" : "that answered"} (pid ${pid}) is not ` +
|
|
93
|
+
`the one this data directory's PID file names (pid ${pidFile}), so which one serves this data directory is not proven`,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return { pid };
|
|
97
|
+
}
|
|
98
|
+
/** `/user.slice/user-<uid>.slice/user@<uid>.service/[<x>.slice/…]<unit>.service` — the two uids captured (1, 2), the unit (3). */
|
|
99
|
+
const USER_UNIT_CGROUP = /^\/user\.slice\/user-(\d+)\.slice\/user@(\d+)\.service\/(?:[A-Za-z0-9:_.\\-]+\.slice\/)*([A-Za-z0-9:_.@\\-]+\.service)$/;
|
|
100
|
+
/** A user manager's own unit, `user@<uid>.service` — the uid captured. */
|
|
101
|
+
const USER_MANAGER_UNIT = /^user@(\d+)\.service$/;
|
|
102
|
+
/**
|
|
103
|
+
* Why a cgroup path's components contradict each other, or null. systemd runs
|
|
104
|
+
* a user's manager as `user@<uid>.service` at `/user.slice/user-<uid>.slice/`
|
|
105
|
+
* (the same uid, nothing above `user.slice`), and it nests no service inside
|
|
106
|
+
* another service's cgroup: a path that says otherwise does not establish which
|
|
107
|
+
* manager supervises the process, whichever manager is asked.
|
|
108
|
+
*/
|
|
109
|
+
function cgroupContradiction(components) {
|
|
110
|
+
const services = components.filter((c) => c.endsWith(".service") && !USER_MANAGER_UNIT.test(c));
|
|
111
|
+
if (services.length > 1) {
|
|
112
|
+
return (`it nests one service's cgroup in another's (${services.join(", ")}), so which unit supervises the process ` +
|
|
113
|
+
"is ambiguous");
|
|
114
|
+
}
|
|
115
|
+
const managers = components.flatMap((c, at) => {
|
|
116
|
+
const m = USER_MANAGER_UNIT.exec(c);
|
|
117
|
+
return m ? [{ at, name: c, uid: m[1] }] : [];
|
|
118
|
+
});
|
|
119
|
+
if (managers.length > 1) {
|
|
120
|
+
return (`it names more than one user manager (${managers.map((m) => m.name).join(", ")}), so which manager ` +
|
|
121
|
+
"supervises the process is ambiguous");
|
|
122
|
+
}
|
|
123
|
+
const m = managers[0];
|
|
124
|
+
if (m === undefined)
|
|
125
|
+
return null;
|
|
126
|
+
const slice = components[m.at - 1] ?? "";
|
|
127
|
+
const sliceUid = /^user-(\d+)\.slice$/.exec(slice)?.[1];
|
|
128
|
+
if (sliceUid !== undefined && sliceUid !== m.uid) {
|
|
129
|
+
return `its user slice ${slice} and its user manager ${m.name} name different users`;
|
|
130
|
+
}
|
|
131
|
+
// Nothing but the root above user.slice: a user manager inside another unit's
|
|
132
|
+
// cgroup (a service's, a container's) is not where systemd runs it.
|
|
133
|
+
if (sliceUid === undefined || components[m.at - 2] !== "user.slice" || components.slice(0, m.at - 2).some((c) => c !== "")) {
|
|
134
|
+
return (`its user manager ${m.name} is not directly under user.slice/user-${m.uid}.slice at the top of the ` +
|
|
135
|
+
"hierarchy, where systemd runs it");
|
|
136
|
+
}
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Read /proc/<pid>/cgroup text: the unified (`0::`) entry names the cgroup,
|
|
141
|
+
* and a systemd unit's processes live in a cgroup named after the unit. This
|
|
142
|
+
* is the service manager's own placement of the process, so it identifies the
|
|
143
|
+
* unit without reading any unit file. A path whose components contradict each
|
|
144
|
+
* other is `contradictory` — decided here, before any manager is asked about a
|
|
145
|
+
* unit in it (see cgroupContradiction).
|
|
146
|
+
*/
|
|
147
|
+
export function cgroupOwner(text, uid) {
|
|
148
|
+
const line = text.split(/\r?\n/).find((l) => l.startsWith("0::"));
|
|
149
|
+
if (line === undefined)
|
|
150
|
+
return { kind: "unreadable", reason: "it has no cgroup v2 (0::) entry" };
|
|
151
|
+
const path = line.slice(3);
|
|
152
|
+
const components = path.split("/");
|
|
153
|
+
const contradiction = cgroupContradiction(components);
|
|
154
|
+
if (contradiction !== null)
|
|
155
|
+
return { kind: "contradictory", path, reason: contradiction };
|
|
156
|
+
// A fixed pattern: both uids in the path are captured and compared with this
|
|
157
|
+
// user's uid here, never built into the expression.
|
|
158
|
+
const exact = USER_UNIT_CGROUP.exec(path);
|
|
159
|
+
if (exact && exact[1] === String(uid) && exact[2] === String(uid))
|
|
160
|
+
return { kind: "user-service", unit: exact[3], path };
|
|
161
|
+
const services = components.filter((c) => c.endsWith(".service") && !USER_MANAGER_UNIT.test(c));
|
|
162
|
+
if (services.length > 0) {
|
|
163
|
+
// A user manager component with a `/` on both sides: the path is under that user's manager.
|
|
164
|
+
const manager = components.slice(1, -1).map((c) => USER_MANAGER_UNIT.exec(c)).find((m) => m !== null) ?? null;
|
|
165
|
+
return {
|
|
166
|
+
kind: "service",
|
|
167
|
+
unit: services[services.length - 1],
|
|
168
|
+
path,
|
|
169
|
+
user: manager !== null,
|
|
170
|
+
managerUid: manager === null ? null : manager[1],
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return { kind: "none", path };
|
|
174
|
+
}
|
|
175
|
+
export function unitSupervision(pid, owner, uid, mainPidOf) {
|
|
176
|
+
const unit = owner.unit;
|
|
177
|
+
const manager = owner.kind === "user-service" ? "user" : !owner.user ? "system" : owner.managerUid === String(uid) ? "user" : null;
|
|
178
|
+
if (manager === null) {
|
|
179
|
+
return { kind: "unknown", unit, reason: `${unit} is under another user's systemd manager, which flair does not ask` };
|
|
180
|
+
}
|
|
181
|
+
let mainPid;
|
|
182
|
+
try {
|
|
183
|
+
mainPid = mainPidOf ? mainPidOf(unit, manager) : null;
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
mainPid = null;
|
|
187
|
+
}
|
|
188
|
+
if (mainPid === null)
|
|
189
|
+
return { kind: "unknown", unit, reason: `systemd did not report the MainPID of ${unit}` };
|
|
190
|
+
if (mainPid <= 0)
|
|
191
|
+
return { kind: "unknown", unit, reason: `systemd reports no main process for ${unit}` };
|
|
192
|
+
return mainPid === pid ? { kind: "main", unit, manager } : { kind: "other", unit, mainPid };
|
|
193
|
+
}
|
|
194
|
+
/** Why the unit a serving process's cgroup names is not shown to run it (see unitSupervision). */
|
|
195
|
+
function notRunByUnitReason(pid, path, sup) {
|
|
196
|
+
return sup.kind === "other"
|
|
197
|
+
? `no systemd unit runs the serving process (pid ${pid}): it is in the cgroup of ${sup.unit} (${path}), whose ` +
|
|
198
|
+
`main process is pid ${sup.mainPid} — it was started directly`
|
|
199
|
+
: `the serving process (pid ${pid}) is in the cgroup of ${sup.unit} (${path}), and ${sup.reason}, so whether ` +
|
|
200
|
+
"that unit runs it is not known";
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The Linux probe from raw reads — /proc/<pid>/cgroup and `systemctl [--user]
|
|
204
|
+
* show` output — so the adapter in src/cli.ts and the tests run the same
|
|
205
|
+
* parsing and lookup.
|
|
206
|
+
*/
|
|
207
|
+
export function linuxUnitProbe(io) {
|
|
208
|
+
return {
|
|
209
|
+
procCgroup: (pid) => io.readFile(`/proc/${pid}/cgroup`),
|
|
210
|
+
uid: io.uid,
|
|
211
|
+
userUnitDir: io.userUnitDir,
|
|
212
|
+
systemdUserUnit: (unit) => {
|
|
213
|
+
const out = io.systemctlShow(unit, "user");
|
|
214
|
+
return out === null ? null : systemdUnitStateFromShow(out);
|
|
215
|
+
},
|
|
216
|
+
unitMainPid: (unit, manager) => {
|
|
217
|
+
const out = io.systemctlShow(unit, manager);
|
|
218
|
+
return out === null ? null : mainPidFromShow(out);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/** Parse `systemctl show -p A -p B …` output (`Key=value` lines) into a map. */
|
|
223
|
+
export function parseSystemctlShow(text) {
|
|
224
|
+
const out = {};
|
|
225
|
+
for (const line of text.split(/\r?\n/)) {
|
|
226
|
+
const eq = line.indexOf("=");
|
|
227
|
+
if (eq <= 0)
|
|
228
|
+
continue;
|
|
229
|
+
out[line.slice(0, eq)] = line.slice(eq + 1);
|
|
230
|
+
}
|
|
231
|
+
return out;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* A user unit's manager state from `systemctl --user show <unit> -p MainPID
|
|
235
|
+
* -p FragmentPath -p DropInPaths -p WorkingDirectory`. null when a property is
|
|
236
|
+
* missing — an unreadable answer is not an empty one.
|
|
237
|
+
*/
|
|
238
|
+
export function systemdUnitStateFromShow(text) {
|
|
239
|
+
const kv = parseSystemctlShow(text);
|
|
240
|
+
for (const key of ["MainPID", "FragmentPath", "DropInPaths", "WorkingDirectory"]) {
|
|
241
|
+
if (!(key in kv))
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
const pid = Number(kv.MainPID);
|
|
245
|
+
const wd = kv.WorkingDirectory.trim().replace(/^!/, "");
|
|
246
|
+
return {
|
|
247
|
+
mainPid: Number.isInteger(pid) && pid > 0 ? pid : null,
|
|
248
|
+
fragmentPath: kv.FragmentPath.trim() === "" ? null : kv.FragmentPath.trim(),
|
|
249
|
+
dropInPaths: kv.DropInPaths.trim() === "" ? [] : kv.DropInPaths.trim().split(/\s+/),
|
|
250
|
+
workingDirectory: wd === "" ? null : wd,
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* MainPID from `systemctl show -p MainPID …` output: the pid, 0 when the unit
|
|
255
|
+
* has no main process, or null when it is missing or not a number.
|
|
256
|
+
*/
|
|
257
|
+
export function mainPidFromShow(text) {
|
|
258
|
+
const raw = parseSystemctlShow(text).MainPID?.trim();
|
|
259
|
+
return raw !== undefined && /^\d+$/.test(raw) ? Number(raw) : null;
|
|
260
|
+
}
|
|
261
|
+
/** The node binary and working directory an active systemd `[Service]` section names. */
|
|
262
|
+
export function readSystemdServiceRefs(text) {
|
|
263
|
+
let section = "";
|
|
264
|
+
let workingDirectory = null;
|
|
265
|
+
let nodeBin = null;
|
|
266
|
+
for (const raw of text.split(/\r?\n/)) {
|
|
267
|
+
const line = raw.trim();
|
|
268
|
+
if (line === "" || line.startsWith("#") || line.startsWith(";"))
|
|
269
|
+
continue;
|
|
270
|
+
if (line.startsWith("[")) {
|
|
271
|
+
section = line.slice(1).replace(/\].*$/, "").trim().toLowerCase();
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
if (section !== "service")
|
|
275
|
+
continue;
|
|
276
|
+
const eq = line.indexOf("=");
|
|
277
|
+
if (eq < 0)
|
|
278
|
+
continue;
|
|
279
|
+
const key = line.slice(0, eq).trim();
|
|
280
|
+
const value = line.slice(eq + 1).trim();
|
|
281
|
+
if (key === "WorkingDirectory") {
|
|
282
|
+
workingDirectory = value === "" ? null : value.replace(/^-/, "");
|
|
283
|
+
}
|
|
284
|
+
else if (key === "ExecStart") {
|
|
285
|
+
if (value === "") {
|
|
286
|
+
nodeBin = null;
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
for (const word of value.replace(/^[-@:+!]+/, "").split(/\s+/)) {
|
|
290
|
+
if (word.startsWith("/") && /(^|\/)node$/.test(word)) {
|
|
291
|
+
nodeBin = word;
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return { nodeBin, workingDirectory };
|
|
298
|
+
}
|
|
299
|
+
function plistRootPath(raw) {
|
|
300
|
+
const m = /<key>ROOTPATH<\/key>\s*<string>([^<]*)<\/string>/.exec(raw);
|
|
301
|
+
return m ? unescapeXml(m[1]) : null;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Which install tree serves the queried instance — or UNKNOWN, with the reason,
|
|
305
|
+
* when no service manager is shown to own the answering process.
|
|
306
|
+
*/
|
|
307
|
+
export function proveServingTree(p) {
|
|
308
|
+
if (!p.local) {
|
|
309
|
+
return unknown(`${p.queryUrl} is not this data directory's local instance (a remote target, or another port), so its install ` +
|
|
310
|
+
"tree cannot be proven from here");
|
|
311
|
+
}
|
|
312
|
+
const samePath = p.samePath ?? ((a, b) => resolve(a) === resolve(b));
|
|
313
|
+
const answering = () => {
|
|
314
|
+
let evidence;
|
|
315
|
+
try {
|
|
316
|
+
evidence = p.localPids();
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
evidence = { pidFile: null, listeners: null };
|
|
320
|
+
}
|
|
321
|
+
return identifyAnsweringPid(p.respondingPid, evidence);
|
|
322
|
+
};
|
|
323
|
+
if (p.platform === "darwin") {
|
|
324
|
+
const ld = p.launchd;
|
|
325
|
+
if (!ld || !p.exists(ld.plistPath)) {
|
|
326
|
+
return unknown(`no launchd service is registered for this data directory${ld ? ` (${ld.plistPath})` : ""}, so nothing proves ` +
|
|
327
|
+
"which install tree serves this instance (it was started directly, by another supervisor, or is not running)");
|
|
328
|
+
}
|
|
329
|
+
let raw;
|
|
330
|
+
try {
|
|
331
|
+
raw = p.read(ld.plistPath);
|
|
332
|
+
}
|
|
333
|
+
catch (err) {
|
|
334
|
+
return unknown(`the launchd plist ${ld.plistPath} could not be read (${err?.message ?? err})`);
|
|
335
|
+
}
|
|
336
|
+
const root = plistRootPath(raw);
|
|
337
|
+
if (root === null || resolve(root) !== resolve(p.dataDir)) {
|
|
338
|
+
return unknown(`the launchd plist ${ld.plistPath} is not registered to this data directory (${resolve(p.dataDir)})`);
|
|
339
|
+
}
|
|
340
|
+
const jobPid = p.launchdJobPid?.(ld.label) ?? null;
|
|
341
|
+
if (jobPid === null) {
|
|
342
|
+
return unknown(`launchd is not running the job ${ld.label}, so the instance answering was not started by it`);
|
|
343
|
+
}
|
|
344
|
+
const who = answering();
|
|
345
|
+
if ("reason" in who)
|
|
346
|
+
return unknown(`the process serving this instance could not be identified: ${who.reason}`);
|
|
347
|
+
const pid = who.pid;
|
|
348
|
+
if (pid !== jobPid) {
|
|
349
|
+
return unknown(`the process serving this instance (pid ${pid}) is not launchd's job ${ld.label} (pid ${jobPid}) — it was started ` +
|
|
350
|
+
"directly, by another supervisor, or under a different HOME");
|
|
351
|
+
}
|
|
352
|
+
const pkg = p.servingPackage(pid);
|
|
353
|
+
if (!pkg)
|
|
354
|
+
return unknown(`the install tree of the serving process (pid ${pid}) could not be read`);
|
|
355
|
+
const refs = readPlistProgramRefs(ld.plistPath, () => raw);
|
|
356
|
+
const unitNodeBin = refs?.programArguments.find((a) => a.startsWith("/") && /(^|[/\\])node$/.test(a)) ?? null;
|
|
357
|
+
return {
|
|
358
|
+
kind: "proven",
|
|
359
|
+
dir: pkg.dir,
|
|
360
|
+
version: pkg.version,
|
|
361
|
+
pid,
|
|
362
|
+
manager: "launchd",
|
|
363
|
+
unitName: ld.label,
|
|
364
|
+
unitPath: ld.plistPath,
|
|
365
|
+
unitNodeBin,
|
|
366
|
+
unitTree: refs?.workingDirectory ?? null,
|
|
367
|
+
dropInPaths: [],
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
if (p.platform === "linux") {
|
|
371
|
+
const who = answering();
|
|
372
|
+
if ("reason" in who)
|
|
373
|
+
return unknown(`the process serving this instance could not be identified: ${who.reason}`);
|
|
374
|
+
const pid = who.pid;
|
|
375
|
+
const pkg = p.servingPackage(pid);
|
|
376
|
+
if (!pkg)
|
|
377
|
+
return unknown(`the install tree of the serving process (pid ${pid}) could not be read`);
|
|
378
|
+
if (!p.procCgroup || p.uid === undefined || !p.userUnitDir) {
|
|
379
|
+
return unknown("this host's process cgroups cannot be read, so no systemd unit can be proven to own the serving process");
|
|
380
|
+
}
|
|
381
|
+
// The unit comes from the process (its cgroup), never from a tree path in a
|
|
382
|
+
// unit file: after a re-point the file names this CLI's tree while the
|
|
383
|
+
// process still runs the old one, and it must still be found.
|
|
384
|
+
let owner;
|
|
385
|
+
try {
|
|
386
|
+
owner = cgroupOwner(p.procCgroup(pid), p.uid);
|
|
387
|
+
}
|
|
388
|
+
catch (err) {
|
|
389
|
+
return unknown(`the cgroup of the serving process (pid ${pid}) could not be read (${err?.message ?? err})`);
|
|
390
|
+
}
|
|
391
|
+
if (owner.kind === "unreadable")
|
|
392
|
+
return unknown(`the cgroup of the serving process (pid ${pid}) cannot be read: ${owner.reason}`);
|
|
393
|
+
if (owner.kind === "contradictory") {
|
|
394
|
+
return unknown(`the cgroup of the serving process (pid ${pid}) is contradictory (${owner.path}): ${owner.reason}, so which ` +
|
|
395
|
+
"service manager supervises it is not known");
|
|
396
|
+
}
|
|
397
|
+
if (owner.kind === "none") {
|
|
398
|
+
return unknown(`no systemd unit owns the serving process (pid ${pid}, cgroup ${owner.path}) — it was started directly`);
|
|
399
|
+
}
|
|
400
|
+
// A unit runs the process only as its MainPID (see unitSupervision) — the
|
|
401
|
+
// same rule `flair restart` applies (planLinuxRestart).
|
|
402
|
+
if (owner.kind === "service") {
|
|
403
|
+
const sup = unitSupervision(pid, owner, p.uid, p.unitMainPid);
|
|
404
|
+
if (sup.kind !== "main")
|
|
405
|
+
return unknown(notRunByUnitReason(pid, owner.path, sup));
|
|
406
|
+
return unknown(`the serving process (pid ${pid}) is the main process of the systemd unit ${owner.unit} (cgroup ${owner.path}), ` +
|
|
407
|
+
"which is not a user unit of this user that flair re-points (a system-level unit, or a sub-cgroup of a unit)");
|
|
408
|
+
}
|
|
409
|
+
const unit = owner.unit;
|
|
410
|
+
const state = p.systemdUserUnit?.(unit) ?? null;
|
|
411
|
+
if (!state) {
|
|
412
|
+
return unknown(notRunByUnitReason(pid, owner.path, { kind: "unknown", unit, reason: `systemd did not report the user unit ${unit}` }));
|
|
413
|
+
}
|
|
414
|
+
if (state.mainPid !== pid) {
|
|
415
|
+
return unknown(notRunByUnitReason(pid, owner.path, state.mainPid === null
|
|
416
|
+
? { kind: "unknown", unit, reason: `systemd reports no main process for ${unit}` }
|
|
417
|
+
: { kind: "other", unit, mainPid: state.mainPid }));
|
|
418
|
+
}
|
|
419
|
+
// Only a file of that name in this user's unit directory is a file flair reads and re-points.
|
|
420
|
+
const fragment = state.fragmentPath;
|
|
421
|
+
if (fragment === null || basename(fragment) !== unit || !samePath(dirname(fragment), p.userUnitDir)) {
|
|
422
|
+
return unknown(`systemd loads ${unit} from ${fragment ?? "no file"}, not from ${unit} in this user's unit directory ${p.userUnitDir}`);
|
|
423
|
+
}
|
|
424
|
+
let refs = { nodeBin: null, workingDirectory: null };
|
|
425
|
+
// With drop-ins, the unit file alone does not say what systemd runs.
|
|
426
|
+
if (state.dropInPaths.length === 0) {
|
|
427
|
+
try {
|
|
428
|
+
refs = readSystemdServiceRefs(p.read(fragment));
|
|
429
|
+
}
|
|
430
|
+
catch { /* the proof is the cgroup + MainPID + FragmentPath; the refs are detail */ }
|
|
431
|
+
}
|
|
432
|
+
return {
|
|
433
|
+
kind: "proven",
|
|
434
|
+
dir: pkg.dir,
|
|
435
|
+
version: pkg.version,
|
|
436
|
+
pid,
|
|
437
|
+
manager: "systemd-user",
|
|
438
|
+
unitName: unit,
|
|
439
|
+
unitPath: fragment,
|
|
440
|
+
unitNodeBin: refs.nodeBin,
|
|
441
|
+
unitTree: refs.workingDirectory,
|
|
442
|
+
dropInPaths: state.dropInPaths,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
return unknown(`${p.platform} has no service manager flair can check`);
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* How `flair restart` may restart the instance on Linux. A proven user unit is
|
|
449
|
+
* restarted THROUGH systemd. Otherwise the direct path (stop by signal, spawn
|
|
450
|
+
* again) is allowed only when no process it could stop is SUPERVISED by a
|
|
451
|
+
* systemd unit — is that unit's MainPID (see unitSupervision). A supervised
|
|
452
|
+
* process is never stopped and respawned outside its manager: it is refused,
|
|
453
|
+
* with the systemctl command to use. A process that merely sits in some
|
|
454
|
+
* service's cgroup (that unit's MainPID is another process) was started
|
|
455
|
+
* directly and takes the direct path. When the cgroup cannot be read, or the
|
|
456
|
+
* unit's MainPID cannot be learned, it is refused: unknown never licenses a stop.
|
|
457
|
+
* A contradictory cgroup is refused before any manager is asked about it.
|
|
458
|
+
*/
|
|
459
|
+
export function planLinuxRestart(input) {
|
|
460
|
+
const s = input.serving;
|
|
461
|
+
if (s.kind === "proven" && s.manager === "systemd-user")
|
|
462
|
+
return { kind: "systemd", unit: s.unitName };
|
|
463
|
+
const why = s.kind === "unknown" ? s.reason : `it is served by ${describeUnit(s)}`;
|
|
464
|
+
for (const pid of new Set(input.pids)) {
|
|
465
|
+
let owner;
|
|
466
|
+
try {
|
|
467
|
+
owner = cgroupOwner(input.procCgroup(pid), input.uid);
|
|
468
|
+
}
|
|
469
|
+
catch (err) {
|
|
470
|
+
owner = { kind: "unreadable", reason: err?.message ?? String(err) };
|
|
471
|
+
}
|
|
472
|
+
if (owner.kind === "none")
|
|
473
|
+
continue;
|
|
474
|
+
if (owner.kind === "unreadable") {
|
|
475
|
+
return {
|
|
476
|
+
kind: "refuse",
|
|
477
|
+
detail: `refusing to restart: the cgroup of pid ${pid} cannot be read (${owner.reason}), so flair cannot tell whether a ` +
|
|
478
|
+
"service manager supervises it, and it does not stop a process it cannot place. Restart it through whatever runs it.",
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
// Contradictory evidence is refused BEFORE any manager is asked: no MainPID
|
|
482
|
+
// answer about a unit in such a path shows who supervises the process.
|
|
483
|
+
if (owner.kind === "contradictory") {
|
|
484
|
+
return {
|
|
485
|
+
kind: "refuse",
|
|
486
|
+
detail: `refusing to restart: the cgroup of pid ${pid} is contradictory (${owner.path}): ${owner.reason}. flair cannot ` +
|
|
487
|
+
"tell which service manager supervises it, and it does not stop a process it cannot place. Restart it through " +
|
|
488
|
+
"whatever runs it.",
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
const sup = unitSupervision(pid, owner, input.uid, input.unitMainPid);
|
|
492
|
+
// In the unit's cgroup but not its main process: started directly (a CI runner agent's child, for example).
|
|
493
|
+
if (sup.kind === "other")
|
|
494
|
+
continue;
|
|
495
|
+
const cmd = owner.kind === "user-service" || owner.user ? `systemctl --user restart ${owner.unit}` : `systemctl restart ${owner.unit} (as root)`;
|
|
496
|
+
if (sup.kind === "unknown") {
|
|
497
|
+
return {
|
|
498
|
+
kind: "refuse",
|
|
499
|
+
detail: `refusing to restart: pid ${pid} is in the cgroup of the systemd unit ${owner.unit} (cgroup ${owner.path}), and ` +
|
|
500
|
+
`${sup.reason}, so flair cannot tell whether that unit runs it, and it does not stop a process a service manager ` +
|
|
501
|
+
`may supervise. If ${owner.unit} runs this instance, restart it through systemd: ${cmd}`,
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
return {
|
|
505
|
+
kind: "refuse",
|
|
506
|
+
detail: `refusing to restart: pid ${pid} is the main process of the systemd unit ${owner.unit} (cgroup ${owner.path}), and ` +
|
|
507
|
+
`flair restarts only a user unit it proved runs this instance (${why}). flair does not stop a process a service ` +
|
|
508
|
+
`manager supervises and start it again outside that manager. Restart it through systemd: ${cmd}`,
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
return { kind: "direct" };
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* After `systemctl --user restart <unit>`: why the unit's new main process is
|
|
515
|
+
* not a restarted process running from the unit's WorkingDirectory, or null.
|
|
516
|
+
*/
|
|
517
|
+
export function restartedUnitProblem(unit, oldPid, state, cwdOf, samePath) {
|
|
518
|
+
if (!state)
|
|
519
|
+
return `systemd did not report ${unit} after the restart`;
|
|
520
|
+
if (state.mainPid === null)
|
|
521
|
+
return `${unit} has no main process after the restart`;
|
|
522
|
+
if (oldPid !== null && state.mainPid === oldPid)
|
|
523
|
+
return `${unit}'s main process is still pid ${oldPid}`;
|
|
524
|
+
const cwd = cwdOf(state.mainPid);
|
|
525
|
+
if (cwd === null)
|
|
526
|
+
return `the working directory of ${unit}'s new main process (pid ${state.mainPid}) could not be read`;
|
|
527
|
+
if (state.workingDirectory === null || !samePath(cwd, state.workingDirectory)) {
|
|
528
|
+
return `${unit}'s new main process (pid ${state.mainPid}) runs in ${cwd}, not the unit's WorkingDirectory ${state.workingDirectory ?? "(none)"}`;
|
|
529
|
+
}
|
|
530
|
+
return null;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* True for a tree `npm i -g` installed under some Node runtime's global prefix
|
|
534
|
+
* (`<prefix>/lib/node_modules/@tpsdev-ai/flair`) — the tree a Node bump leaves
|
|
535
|
+
* behind. A plain extracted tree or a checkout is a deliberate deployment and
|
|
536
|
+
* is never re-pointed.
|
|
537
|
+
*/
|
|
538
|
+
export function isNpmGlobalFlairTree(dir) {
|
|
539
|
+
return /[/\\]lib[/\\]node_modules[/\\]@tpsdev-ai[/\\]flair[/\\]?$/.test(dir);
|
|
540
|
+
}
|
|
541
|
+
/** True for a strict semver version exactly as written (no leading "v", no spaces, no build metadata). */
|
|
542
|
+
export function isExactSemver(v) {
|
|
543
|
+
return typeof v === "string" && semver.valid(v) === v;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Semver ordering (prereleases included: 0.57.0-beta.1 < 0.57.0): <0, 0, >0 —
|
|
547
|
+
* or null when either side is not an exact semver version. Callers that guard
|
|
548
|
+
* a downgrade treat null as "cannot rule it out" and refuse.
|
|
549
|
+
*/
|
|
550
|
+
export function compareVersions(a, b) {
|
|
551
|
+
if (!isExactSemver(a) || !isExactSemver(b))
|
|
552
|
+
return null;
|
|
553
|
+
return semver.compare(a, b);
|
|
554
|
+
}
|
|
555
|
+
function defaultSamePath(a, b) {
|
|
556
|
+
return resolve(a) === resolve(b);
|
|
557
|
+
}
|
|
558
|
+
export function assessTreeDivergence(input) {
|
|
559
|
+
const samePath = input.samePath ?? defaultSamePath;
|
|
560
|
+
const runningVersion = input.runningVersion ?? null;
|
|
561
|
+
const base = { cli: input.cli, serving: input.serving, runningVersion, restartPending: false, cliOlder: false, nodePin: null };
|
|
562
|
+
const s = input.serving;
|
|
563
|
+
if (s.kind === "unknown")
|
|
564
|
+
return { ...base, state: "unknown" };
|
|
565
|
+
const nodePin = classifyServiceNodePin({
|
|
566
|
+
unitDescription: describeUnit(s),
|
|
567
|
+
unitNodeBin: s.unitNodeBin,
|
|
568
|
+
unitTree: s.unitTree,
|
|
569
|
+
currentNodeBin: input.currentNodeBin,
|
|
570
|
+
cliTree: input.cli.dir,
|
|
571
|
+
}, input.nodePinDeps);
|
|
572
|
+
if (samePath(input.cli.dir, s.dir))
|
|
573
|
+
return { ...base, state: "same", nodePin };
|
|
574
|
+
if (!isNpmGlobalFlairTree(s.dir))
|
|
575
|
+
return { ...base, state: "separate", nodePin };
|
|
576
|
+
return withRunningVersion({
|
|
577
|
+
...base,
|
|
578
|
+
state: "diverged",
|
|
579
|
+
nodePin,
|
|
580
|
+
restartPending: s.unitTree !== null && samePath(s.unitTree, input.cli.dir),
|
|
581
|
+
}, runningVersion);
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* The assessment with the running process's version filled in (read after the
|
|
585
|
+
* proof, so a version probe is only spent when a tree was proven). `cliOlder`
|
|
586
|
+
* compares this CLI with the code the instance RUNS, falling back to the
|
|
587
|
+
* version on disk in the serving tree when the running one is unknown.
|
|
588
|
+
*/
|
|
589
|
+
export function withRunningVersion(a, runningVersion) {
|
|
590
|
+
const servingCodeVersion = runningVersion ?? (a.serving.kind === "proven" ? a.serving.version : null);
|
|
591
|
+
return {
|
|
592
|
+
...a,
|
|
593
|
+
runningVersion,
|
|
594
|
+
cliOlder: a.state === "diverged" &&
|
|
595
|
+
a.cli.version !== null &&
|
|
596
|
+
servingCodeVersion !== null &&
|
|
597
|
+
(compareVersions(a.cli.version, servingCodeVersion) ?? 0) < 0,
|
|
598
|
+
};
|
|
599
|
+
}
|
|
600
|
+
export function describeUnit(s) {
|
|
601
|
+
return s.manager === "launchd"
|
|
602
|
+
? `the launchd service ${s.unitName} (${s.unitPath})`
|
|
603
|
+
: `the systemd user unit ${s.unitName} (${s.unitPath})`;
|
|
604
|
+
}
|
|
605
|
+
/**
|
|
606
|
+
* Diverged, but `flair init` will not re-point this unit: systemd applies
|
|
607
|
+
* drop-ins to it, and a drop-in can set ExecStart= / WorkingDirectory= that the
|
|
608
|
+
* unit file does not show. The remedy is then a hand edit, never `flair init`.
|
|
609
|
+
*/
|
|
610
|
+
export function manualRepointReason(a) {
|
|
611
|
+
const s = a.serving;
|
|
612
|
+
if (a.state !== "diverged" || s.kind !== "proven" || s.manager !== "systemd-user" || s.dropInPaths.length === 0)
|
|
613
|
+
return null;
|
|
614
|
+
return `systemd applies drop-ins to ${s.unitName} (${s.dropInPaths.join(", ")}), so flair does not re-point it`;
|
|
615
|
+
}
|
|
616
|
+
function v(version) {
|
|
617
|
+
return version ? `flair ${version}` : "flair version unknown";
|
|
618
|
+
}
|
|
619
|
+
function relation(version, latest) {
|
|
620
|
+
if (!version)
|
|
621
|
+
return "unknown";
|
|
622
|
+
const c = compareVersions(version, latest);
|
|
623
|
+
return c === null ? "unknown" : c === 0 ? "current" : c < 0 ? "behind" : "ahead";
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* The operator-facing block for a `diverged` or `separate` assessment. Names the
|
|
627
|
+
* ACTOR (the CLI, the instance and the unit that owns it), the STATE (both
|
|
628
|
+
* trees, both versions, "unknown" where a version could not be read) and the
|
|
629
|
+
* REMEDY. Returns [] for `same` and `unknown` — see formatServingTreeLine.
|
|
630
|
+
*/
|
|
631
|
+
export function formatTreeAssessmentLines(a, opts = {}) {
|
|
632
|
+
if (a.serving.kind !== "proven")
|
|
633
|
+
return [];
|
|
634
|
+
const s = a.serving;
|
|
635
|
+
if (a.state === "separate") {
|
|
636
|
+
return [
|
|
637
|
+
`ℹ The instance serves from ${s.dir} (running ${v(a.runningVersion)}; pid ${s.pid}, ${describeUnit(s)}),`,
|
|
638
|
+
` not from this CLI's tree ${a.cli.dir} (${v(a.cli.version)}). It is not an npm-global install, so flair treats it`,
|
|
639
|
+
" as a separately managed deployment and never re-points it. If it is a packed tree, `flair upgrade --tree " + `${s.dir}\` upgrades it.`,
|
|
640
|
+
];
|
|
641
|
+
}
|
|
642
|
+
if (a.state !== "diverged")
|
|
643
|
+
return [];
|
|
644
|
+
const unit = describeUnit(s);
|
|
645
|
+
const lines = [
|
|
646
|
+
"⚠️ This CLI and the running instance are in DIFFERENT install trees.",
|
|
647
|
+
` CLI: ${a.cli.dir} (${v(a.cli.version)})`,
|
|
648
|
+
` instance: ${s.dir} (running ${v(a.runningVersion)}; pid ${s.pid}, ${unit})`,
|
|
649
|
+
];
|
|
650
|
+
if (a.nodePin?.kind === "erroneous")
|
|
651
|
+
lines.push(` Cause: ${a.nodePin.message}`);
|
|
652
|
+
if (opts.latest) {
|
|
653
|
+
lines.push(` Versions: latest published is ${opts.latest}; this CLI is ${relation(a.cli.version, opts.latest)}, ` +
|
|
654
|
+
`the instance is ${relation(a.runningVersion, opts.latest)}.`);
|
|
655
|
+
}
|
|
656
|
+
if (opts.context === "upgrade") {
|
|
657
|
+
lines.push(` \`flair upgrade\` changes this CLI's tree only; the instance keeps serving ${s.dir} until its service is re-pointed.`);
|
|
658
|
+
}
|
|
659
|
+
const manual = manualRepointReason(a);
|
|
660
|
+
if (a.cliOlder) {
|
|
661
|
+
lines.push(` This CLI's tree has an OLDER flair than the instance runs, so re-pointing the service at it now would downgrade the`, " instance — `flair init` will not do that. First update this CLI's tree from this same shell:", " npm i -g @tpsdev-ai/flair", manual
|
|
662
|
+
? ` Then (${manual}) point the unit at ${a.cli.dir} by hand, then: flair restart`
|
|
663
|
+
: " Then: flair init && flair restart");
|
|
664
|
+
return lines;
|
|
665
|
+
}
|
|
666
|
+
if (a.restartPending) {
|
|
667
|
+
lines.push(` ${unit} already names this CLI's tree; the running process started before it was re-pointed.`, " Remedy: flair restart (restarts the instance under the unit and reports which tree then serves)");
|
|
668
|
+
return lines;
|
|
669
|
+
}
|
|
670
|
+
if (manual) {
|
|
671
|
+
lines.push(` ${manual}.`, ` Remedy: point the unit's node, Harper entry and WorkingDirectory at ${a.cli.dir} by hand (where the drop-ins`, " set them, edit the drop-ins), then: flair restart");
|
|
672
|
+
return lines;
|
|
673
|
+
}
|
|
674
|
+
lines.push(" Remedy: flair init && flair restart", ` \`flair init\` re-points ${unit} at this CLI's tree.`, ` It changes only the unit's node, Harper entry, ${s.manager === "launchd" ? "launcher" : "launcher (if it runs one)"} and working directory,`, " and leaves the unit's other settings as they are. It writes only a unit it can prove is this instance's and in a shape", " it supports; otherwise it changes nothing and names the file, what did not match, and the remedy. It also", " re-points the federation-sync shim when that runs another tree.", " init is Flair's full setup command, so it also re-runs its idempotent setup for this data directory: it reuses the existing", " Harper install and admin password, saves the instance's recorded configuration again, and in an interactive shell can", " offer agent and MCP-client setup. `flair restart` then restarts the instance under the re-pointed unit and reports which", " tree serves it.");
|
|
675
|
+
return lines;
|
|
676
|
+
}
|
|
677
|
+
/** One line naming the serving tree — or why it is unknown. */
|
|
678
|
+
export function formatServingTreeLine(a) {
|
|
679
|
+
if (a.serving.kind === "unknown")
|
|
680
|
+
return `serving install tree: unknown — ${a.serving.reason}`;
|
|
681
|
+
const s = a.serving;
|
|
682
|
+
const where = a.state === "same" ? "this CLI's tree" : `this CLI is ${a.cli.dir}`;
|
|
683
|
+
return `serving install tree: ${s.dir} (running ${v(a.runningVersion)}; pid ${s.pid}, ${describeUnit(s)}) — ${where}`;
|
|
684
|
+
}
|
|
685
|
+
/** The assessment as plain JSON for `flair status --json`. */
|
|
686
|
+
export function treeAssessmentJson(a) {
|
|
687
|
+
const s = a.serving;
|
|
688
|
+
return {
|
|
689
|
+
state: a.state,
|
|
690
|
+
cli: { dir: a.cli.dir, version: a.cli.version },
|
|
691
|
+
serving: s.kind === "proven"
|
|
692
|
+
? {
|
|
693
|
+
dir: s.dir,
|
|
694
|
+
treeVersion: s.version,
|
|
695
|
+
runningVersion: a.runningVersion,
|
|
696
|
+
pid: s.pid,
|
|
697
|
+
manager: s.manager,
|
|
698
|
+
unit: s.unitName,
|
|
699
|
+
unitPath: s.unitPath,
|
|
700
|
+
}
|
|
701
|
+
: null,
|
|
702
|
+
unknownReason: s.kind === "unknown" ? s.reason : null,
|
|
703
|
+
restartPending: a.restartPending,
|
|
704
|
+
cliOlder: a.cliOlder,
|
|
705
|
+
nodePin: a.nodePin ? { kind: a.nodePin.kind, unitNodeBin: a.nodePin.unitNodeBin, currentNodeBin: a.nodePin.currentNodeBin } : null,
|
|
706
|
+
remedy: a.state !== "diverged" || manualRepointReason(a) !== null
|
|
707
|
+
? null
|
|
708
|
+
: a.cliOlder
|
|
709
|
+
? ["npm i -g @tpsdev-ai/flair", "flair init", "flair restart"]
|
|
710
|
+
: a.restartPending
|
|
711
|
+
? ["flair restart"]
|
|
712
|
+
: ["flair init", "flair restart"],
|
|
713
|
+
// Set when flair init will not re-point the unit: the remedy is a hand edit, then `flair restart`.
|
|
714
|
+
manualRemedy: manualRepointReason(a),
|
|
715
|
+
};
|
|
716
|
+
}
|