@tpsdev-ai/flair 0.57.0 → 0.59.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 +7 -6
- package/SECURITY.md +4 -1
- package/config.yaml +3 -0
- package/dist/build-info.json +3 -3
- package/dist/cli.js +1726 -376
- package/dist/commands/agent.js +160 -2
- package/dist/commands/doctor.js +177 -27
- package/dist/commands/federation.js +2 -1
- package/dist/commands/hook.js +10 -2
- package/dist/commands/init.js +150 -44
- package/dist/commands/mcp.js +53 -22
- 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 +103 -0
- package/dist/lib/doctor-run.js +72 -0
- package/dist/lib/embed-gpu-doctor.js +49 -26
- package/dist/lib/federation-memory-attributes.js +15 -0
- package/dist/lib/flair-agent-role.js +116 -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 +179 -8
- package/dist/lib/launchd-repair.js +95 -0
- package/dist/lib/mcp-enable.js +667 -72
- 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/process-start-time.js +32 -0
- 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 +54 -21
- package/dist/resources/AgentSeed.js +44 -1
- package/dist/resources/Federation.js +84 -54
- package/dist/resources/Memory.js +567 -72
- package/dist/resources/MemoryArchive.js +9 -0
- package/dist/resources/MemoryBootstrap.js +125 -29
- 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/OrgSkillAssignment.js +209 -0
- 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 +47 -14
- package/dist/resources/agent-auth.js +16 -10
- package/dist/resources/agent-status-guard.js +63 -0
- package/dist/resources/auth-middleware.js +137 -49
- 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 +34 -0
- package/dist/resources/federation-crypto.js +65 -19
- package/dist/resources/flair-agent-role-boot.js +26 -0
- package/dist/resources/health.js +67 -16
- 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/key-lock.js +31 -0
- package/dist/resources/mcp-handler.js +147 -51
- package/dist/resources/mcp-oauth.js +3 -6
- package/dist/resources/mcp-tools.js +47 -25
- 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/multi-worker-guard.js +121 -0
- package/dist/resources/oauth-discovery.js +1 -0
- package/dist/resources/oauth-wellknown.js +8 -4
- 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 +22 -6
- package/dist/resources/relay-ops.js +28 -3
- package/dist/resources/replay-store.js +336 -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/skill-manifest.js +181 -0
- package/dist/resources/skill-provenance.js +15 -10
- 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/flair-agent-role.js +116 -0
- package/dist/src/lib/memory-attributes.js +77 -0
- package/docs/access-control.md +667 -0
- package/docs/api-reference.md +85 -17
- package/docs/auth.md +4 -0
- package/docs/claude-code.md +132 -0
- package/docs/deepseek-harness.md +1 -1
- package/docs/deployment.md +1 -1
- package/docs/federation.md +1 -1
- package/docs/integrations.md +6 -6
- package/docs/mcp-clients.md +16 -24
- package/docs/notes/mcp-oauth-model2.md +15 -1
- package/docs/quickstart.md +5 -0
- package/docs/releasing.md +152 -20
- package/docs/supply-chain-policy.md +10 -5
- package/docs/troubleshooting.md +11 -0
- package/docs/upgrade.md +187 -61
- package/package.json +10 -5
- package/schemas/agent.graphql +9 -6
- package/schemas/federation.graphql +2 -11
- package/schemas/memory.graphql +137 -17
- package/schemas/oauth.graphql +8 -4
- package/schemas/replay.graphql +18 -0
- package/templates/launchd/start-flair-with-admin-pass.sh +95 -0
- package/dist/resources/federation-nonce-store.js +0 -195
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* atomic-write.ts — replace a set of files all together, or not at all
|
|
3
|
+
* (flair#2034 §2).
|
|
4
|
+
*
|
|
5
|
+
* The service re-pointing paths (`flair init`, `flair doctor --fix`) rewrite
|
|
6
|
+
* files an operator's service manager reads: a launchd plist, a systemd unit,
|
|
7
|
+
* the federation-sync shim. A write that truncates the file in place and then
|
|
8
|
+
* fails leaves a half-written unit; a two-file update that fails between the
|
|
9
|
+
* files leaves a pair that disagree. Either is worse than not writing at all.
|
|
10
|
+
*
|
|
11
|
+
* `writeFilesAtomically` stages every new file as a temp file in the target's
|
|
12
|
+
* own directory (same filesystem, so the final rename is atomic), fsyncs each
|
|
13
|
+
* one, and only then renames them over their targets. When a rename fails
|
|
14
|
+
* part-way, every target already replaced is restored from the bytes and mode
|
|
15
|
+
* it had before the call, and the remaining temp files are removed. The caller
|
|
16
|
+
* sees either the complete new set or the complete old set.
|
|
17
|
+
*
|
|
18
|
+
* PLANNED FROM THESE BYTES (flair#2034 §2, round 3). A caller that computed
|
|
19
|
+
* the new content FROM the file's current content passes the snapshot it
|
|
20
|
+
* planned from as `expect` (see snapshotRegularFile). Immediately before the
|
|
21
|
+
* rename, the target is lstat'ed and read again: it must still be a regular
|
|
22
|
+
* file (never a symlink — rename would replace the link itself, not the file
|
|
23
|
+
* it points to), the SAME file (device + inode), and hold exactly the planned
|
|
24
|
+
* bytes. Anything else — an operator's edit saved while flair was planning, a
|
|
25
|
+
* file swapped for a symlink — refuses the write and changes nothing. This is a
|
|
26
|
+
* re-check, not a lock: the window between the re-check and the rename is the
|
|
27
|
+
* one syscall apart, and a second flair writer planning from the same bytes
|
|
28
|
+
* computes the same content.
|
|
29
|
+
*
|
|
30
|
+
* BYTES, NOT DECODED TEXT (round 4). A snapshot is taken only of a file whose
|
|
31
|
+
* bytes survive a UTF-8 decode and re-encode unchanged; any other file is
|
|
32
|
+
* refused before anything is planned from it (two different invalid byte
|
|
33
|
+
* sequences decode to the same U+FFFD, so a rewrite would change bytes flair
|
|
34
|
+
* does not own). The re-check compares the file's BYTES with the encoding of
|
|
35
|
+
* the planned content, which is exact for such a file, and the new content is
|
|
36
|
+
* written as its UTF-8 encoding, so every byte outside the replaced values is
|
|
37
|
+
* the byte that was read.
|
|
38
|
+
*
|
|
39
|
+
* The filesystem primitives are injectable so a test can fail any step (a
|
|
40
|
+
* write, an fsync, the Nth rename) and prove the recovery, without a real
|
|
41
|
+
* failing disk.
|
|
42
|
+
*/
|
|
43
|
+
import { closeSync, existsSync, fchmodSync, fsyncSync, lstatSync, openSync, readFileSync, renameSync, statSync, unlinkSync, writeSync, } from "node:fs";
|
|
44
|
+
import { randomBytes } from "node:crypto";
|
|
45
|
+
import { basename, dirname, join } from "node:path";
|
|
46
|
+
function resolveHooks(h) {
|
|
47
|
+
return {
|
|
48
|
+
open: h.open ?? ((p, flags, mode) => openSync(p, flags, mode)),
|
|
49
|
+
write: h.write ?? ((fd, data) => {
|
|
50
|
+
const buf = Buffer.from(data, "utf-8");
|
|
51
|
+
let off = 0;
|
|
52
|
+
while (off < buf.length)
|
|
53
|
+
off += writeSync(fd, buf, off, buf.length - off);
|
|
54
|
+
}),
|
|
55
|
+
fchmod: h.fchmod ?? ((fd, mode) => fchmodSync(fd, mode)),
|
|
56
|
+
fsync: h.fsync ?? ((fd) => fsyncSync(fd)),
|
|
57
|
+
close: h.close ?? ((fd) => closeSync(fd)),
|
|
58
|
+
rename: h.rename ?? ((from, to) => renameSync(from, to)),
|
|
59
|
+
unlink: h.unlink ?? ((p) => unlinkSync(p)),
|
|
60
|
+
exists: h.exists ?? ((p) => existsSync(p)),
|
|
61
|
+
read: h.read ?? ((p) => readFileSync(p, "utf-8")),
|
|
62
|
+
readBytes: h.readBytes ?? ((p) => readFileSync(p)),
|
|
63
|
+
modeOf: h.modeOf ?? ((p) => statSync(p).mode & 0o7777),
|
|
64
|
+
lstat: h.lstat ?? ((p) => lstatSync(p)),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** Why `st` is not a plain regular file, or null. */
|
|
68
|
+
function notRegular(path, st) {
|
|
69
|
+
if (st.isSymbolicLink())
|
|
70
|
+
return `${path} is a symbolic link`;
|
|
71
|
+
if (!st.isFile())
|
|
72
|
+
return `${path} is not a regular file`;
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Read `path` for planning: it must be a regular file (lstat — a symlink is
|
|
77
|
+
* refused, never followed), it must be the same file before and after the
|
|
78
|
+
* read, and its bytes must be valid UTF-8 that re-encodes to exactly those
|
|
79
|
+
* bytes. Throws an Error naming the path and what did not hold.
|
|
80
|
+
*/
|
|
81
|
+
export function snapshotRegularFile(path, hooks = {}) {
|
|
82
|
+
const lstat = hooks.lstat ?? ((p) => lstatSync(p));
|
|
83
|
+
const readBytes = hooks.readBytes ?? ((p) => readFileSync(p));
|
|
84
|
+
const before = lstat(path);
|
|
85
|
+
const bad = notRegular(path, before);
|
|
86
|
+
if (bad)
|
|
87
|
+
throw new Error(bad);
|
|
88
|
+
const bytes = readBytes(path);
|
|
89
|
+
const content = bytes.toString("utf-8");
|
|
90
|
+
if (!Buffer.from(content, "utf-8").equals(bytes)) {
|
|
91
|
+
throw new Error(`${path} is not valid UTF-8, so flair cannot rewrite it without changing bytes it does not own`);
|
|
92
|
+
}
|
|
93
|
+
const after = lstat(path);
|
|
94
|
+
if (notRegular(path, after) || after.dev !== before.dev || after.ino !== before.ino) {
|
|
95
|
+
throw new Error(`${path} was replaced while it was being read`);
|
|
96
|
+
}
|
|
97
|
+
return { path, content, dev: before.dev, ino: before.ino, mode: before.mode & 0o7777 };
|
|
98
|
+
}
|
|
99
|
+
/** The re-check before a rename: null when `path` is still the planned file with the planned bytes. */
|
|
100
|
+
function plannedFileProblem(fs, path, expect) {
|
|
101
|
+
let st;
|
|
102
|
+
try {
|
|
103
|
+
st = fs.lstat(path);
|
|
104
|
+
}
|
|
105
|
+
catch (err) {
|
|
106
|
+
return `${path} could not be checked again before replacing it (${err?.message ?? err})`;
|
|
107
|
+
}
|
|
108
|
+
const bad = notRegular(path, st);
|
|
109
|
+
if (bad)
|
|
110
|
+
return bad;
|
|
111
|
+
if (st.dev !== expect.dev || st.ino !== expect.ino)
|
|
112
|
+
return `${path} was replaced by another file since flair read it`;
|
|
113
|
+
let now;
|
|
114
|
+
try {
|
|
115
|
+
now = fs.readBytes(path);
|
|
116
|
+
}
|
|
117
|
+
catch (err) {
|
|
118
|
+
return `${path} could not be read again before replacing it (${err?.message ?? err})`;
|
|
119
|
+
}
|
|
120
|
+
if (!now.equals(Buffer.from(expect.content, "utf-8")))
|
|
121
|
+
return `${path} was changed since flair read it`;
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function tempPathFor(target) {
|
|
125
|
+
return join(dirname(target), `.${basename(target)}.${process.pid}.${randomBytes(4).toString("hex")}.tmp`);
|
|
126
|
+
}
|
|
127
|
+
/** Write `content` to a NEW temp file next to `target`, fsynced and closed. */
|
|
128
|
+
function stage(fs, target, content, mode) {
|
|
129
|
+
const tmp = tempPathFor(target);
|
|
130
|
+
const fd = fs.open(tmp, "wx", mode);
|
|
131
|
+
try {
|
|
132
|
+
fs.write(fd, content);
|
|
133
|
+
fs.fchmod(fd, mode);
|
|
134
|
+
fs.fsync(fd);
|
|
135
|
+
}
|
|
136
|
+
catch (err) {
|
|
137
|
+
try {
|
|
138
|
+
fs.close(fd);
|
|
139
|
+
}
|
|
140
|
+
catch { /* already failing */ }
|
|
141
|
+
try {
|
|
142
|
+
fs.unlink(tmp);
|
|
143
|
+
}
|
|
144
|
+
catch { /* best effort */ }
|
|
145
|
+
throw err;
|
|
146
|
+
}
|
|
147
|
+
fs.close(fd);
|
|
148
|
+
return tmp;
|
|
149
|
+
}
|
|
150
|
+
/** Best-effort directory fsync so the renames themselves are durable. */
|
|
151
|
+
function fsyncDir(dir) {
|
|
152
|
+
try {
|
|
153
|
+
const fd = openSync(dir, "r");
|
|
154
|
+
try {
|
|
155
|
+
fsyncSync(fd);
|
|
156
|
+
}
|
|
157
|
+
finally {
|
|
158
|
+
closeSync(fd);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
catch {
|
|
162
|
+
/* not every platform allows fsync on a directory; the renames are still atomic */
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Replace every file in `entries`, or none of them.
|
|
167
|
+
*
|
|
168
|
+
* Throws when the set could not be written. The error message says whether the
|
|
169
|
+
* originals were restored; in the (double-failure) case where a restore itself
|
|
170
|
+
* failed, it names every path that may now hold new content.
|
|
171
|
+
*/
|
|
172
|
+
export function writeFilesAtomically(entries, hooks = {}) {
|
|
173
|
+
const fs = resolveHooks(hooks);
|
|
174
|
+
// 1. Remember what each target held, so a failed commit can put it back.
|
|
175
|
+
// A planned-from target must already be exactly what was planned from.
|
|
176
|
+
for (const e of entries) {
|
|
177
|
+
if (!e.expect)
|
|
178
|
+
continue;
|
|
179
|
+
const problem = plannedFileProblem(fs, e.path, e.expect);
|
|
180
|
+
if (problem)
|
|
181
|
+
throw new Error(`refusing to replace ${e.path}: ${problem}; nothing was changed`);
|
|
182
|
+
}
|
|
183
|
+
const originals = entries.map((e) => {
|
|
184
|
+
if (!fs.exists(e.path))
|
|
185
|
+
return { path: e.path, existed: false };
|
|
186
|
+
// A planned-from target is restored from the bytes it was re-checked to hold.
|
|
187
|
+
const content = e.expect ? e.expect.content : fs.read(e.path);
|
|
188
|
+
return { path: e.path, existed: true, content, mode: fs.modeOf(e.path) };
|
|
189
|
+
});
|
|
190
|
+
// 2. Stage every new file. Nothing visible has changed yet.
|
|
191
|
+
const staged = [];
|
|
192
|
+
try {
|
|
193
|
+
for (const e of entries)
|
|
194
|
+
staged.push(stage(fs, e.path, e.content, e.mode));
|
|
195
|
+
}
|
|
196
|
+
catch (err) {
|
|
197
|
+
for (const tmp of staged) {
|
|
198
|
+
try {
|
|
199
|
+
fs.unlink(tmp);
|
|
200
|
+
}
|
|
201
|
+
catch { /* best effort */ }
|
|
202
|
+
}
|
|
203
|
+
throw new Error(`could not stage the new ${entries.length === 1 ? "file" : "files"} (${err?.message ?? err}); ` +
|
|
204
|
+
`nothing was changed: ${entries.map((e) => e.path).join(", ")}`);
|
|
205
|
+
}
|
|
206
|
+
// 3. Commit: rename each staged file over its target.
|
|
207
|
+
const committed = [];
|
|
208
|
+
for (let i = 0; i < entries.length; i++) {
|
|
209
|
+
try {
|
|
210
|
+
// Re-check the planned-from target immediately before replacing it.
|
|
211
|
+
const expect = entries[i].expect;
|
|
212
|
+
const problem = expect ? plannedFileProblem(fs, entries[i].path, expect) : null;
|
|
213
|
+
if (problem)
|
|
214
|
+
throw new Error(`refusing to replace it: ${problem}`);
|
|
215
|
+
fs.rename(staged[i], entries[i].path);
|
|
216
|
+
committed.push(i);
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
// Remove the temps not yet committed, then put back what was replaced.
|
|
220
|
+
for (let j = i; j < staged.length; j++) {
|
|
221
|
+
try {
|
|
222
|
+
fs.unlink(staged[j]);
|
|
223
|
+
}
|
|
224
|
+
catch { /* best effort */ }
|
|
225
|
+
}
|
|
226
|
+
const unrestored = [];
|
|
227
|
+
for (const k of committed.reverse()) {
|
|
228
|
+
const o = originals[k];
|
|
229
|
+
try {
|
|
230
|
+
if (o.existed) {
|
|
231
|
+
const tmp = stage(fs, o.path, o.content, o.mode);
|
|
232
|
+
fs.rename(tmp, o.path);
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
fs.unlink(o.path);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
catch {
|
|
239
|
+
unrestored.push(o.path);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
const cause = err?.message ?? String(err);
|
|
243
|
+
if (unrestored.length > 0) {
|
|
244
|
+
throw new Error(`could not replace ${entries[i].path} (${cause}), and restoring the files already replaced failed for: ` +
|
|
245
|
+
`${unrestored.join(", ")} — those now hold the NEW content while the rest hold the old.`);
|
|
246
|
+
}
|
|
247
|
+
if (committed.length === 0) {
|
|
248
|
+
throw new Error(`could not replace ${entries[i].path} (${cause}); nothing was changed`);
|
|
249
|
+
}
|
|
250
|
+
throw new Error(`could not replace ${entries[i].path} (${cause}); every file was restored to its previous content: ` +
|
|
251
|
+
`${entries.map((e) => e.path).join(", ")}`);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
for (const dir of new Set(entries.map((e) => dirname(e.path))))
|
|
255
|
+
fsyncDir(dir);
|
|
256
|
+
}
|
|
@@ -36,6 +36,23 @@
|
|
|
36
36
|
* reads, `kill(pid, 0)`, the health probe, the start-time readers) live in
|
|
37
37
|
* `src/cli.ts`, so every branch here is unit-testable without a daemon.
|
|
38
38
|
*/
|
|
39
|
+
/**
|
|
40
|
+
* Map the errno from a failed `kill(pid, 0)` to a `PidLiveness`. Pure so the
|
|
41
|
+
* adapter's errno handling is unit-testable.
|
|
42
|
+
*
|
|
43
|
+
* `gone` is returned ONLY for ESRCH — the one errno that says the pid does not
|
|
44
|
+
* exist. EPERM means it exists but is another user's. EVERY other errno
|
|
45
|
+
* (EINVAL, EACCES, ...) is `unknown`: the liveness was not determined, and
|
|
46
|
+
* unknown must never be read as "gone". A caller with the successful case (no
|
|
47
|
+
* error) passes through `alive` itself.
|
|
48
|
+
*/
|
|
49
|
+
export function livenessFromKillError(code) {
|
|
50
|
+
if (code === "ESRCH")
|
|
51
|
+
return { kind: "gone" };
|
|
52
|
+
if (code === "EPERM")
|
|
53
|
+
return { kind: "eperm" };
|
|
54
|
+
return { kind: "unknown", reason: code ?? "the liveness probe failed" };
|
|
55
|
+
}
|
|
39
56
|
/** Something accepted a TCP connection and spoke HTTP — flair or not. */
|
|
40
57
|
export function healthIndicatesListener(health) {
|
|
41
58
|
return health.kind === "ok" || health.kind === "foreign";
|
|
@@ -101,6 +118,15 @@ export function classifyDaemonState(ev, ctx) {
|
|
|
101
118
|
detail: `the recorded pid ${pid} is alive, but its identity could not be verified (${reason}) — refusing to act on it`,
|
|
102
119
|
};
|
|
103
120
|
}
|
|
121
|
+
// Liveness could not be determined (an errno other than ESRCH/EPERM). This is
|
|
122
|
+
// NOT "gone": refusing is the only safe verdict (unknown never licenses an
|
|
123
|
+
// action — neither a stop nor a sidecar removal).
|
|
124
|
+
if (liveness?.kind === "unknown") {
|
|
125
|
+
return {
|
|
126
|
+
state: "UNKNOWN",
|
|
127
|
+
detail: `could not determine whether the recorded pid ${pid} is alive (${liveness.reason}) — refusing to act on it`,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
104
130
|
return { state: "UNKNOWN", detail: "could not determine whether Flair is running" };
|
|
105
131
|
}
|
|
106
132
|
/**
|
|
@@ -145,6 +171,48 @@ export function verifyIdentity(input) {
|
|
|
145
171
|
export function isStartTimeMatch(actualMs, recordedMs, toleranceMs = 2000) {
|
|
146
172
|
return Math.abs(actualMs - recordedMs) <= toleranceMs;
|
|
147
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Does a flair#1454 sidecar's `startTimeMs` agree with a process's start
|
|
176
|
+
* second (flair#2056)? `startSecondMs` is the start time truncated to a whole
|
|
177
|
+
* second, in epoch ms (the second `ps -o lstart=` reports). They agree when
|
|
178
|
+
* they are within 2000 ms of each other, compared in milliseconds: a start
|
|
179
|
+
* second of 12 s against a sidecar of 14.5 s is 2500 ms and does not agree.
|
|
180
|
+
*/
|
|
181
|
+
export function sidecarStartAgrees(startSecondMs, sidecarStartMs) {
|
|
182
|
+
return Math.abs(sidecarStartMs - startSecondMs) <= 2000;
|
|
183
|
+
}
|
|
184
|
+
/** A Harper entry path under an install tree: `…/node_modules/[@<scope>/]harper/dist/bin/harper.js`. */
|
|
185
|
+
const HARPER_ENTRY_PATH = /(^|\/)node_modules\/(@[^/]+\/)?harper\/dist\/bin\/harper\.js$/;
|
|
186
|
+
/** The relative entry Harper's own restart forks, from its package directory (LAUNCH_SERVICE_SCRIPTS.MAIN). */
|
|
187
|
+
const HARPER_RESTART_ENTRY = "dist/bin/harper.js";
|
|
188
|
+
/**
|
|
189
|
+
* Is this a Harper-shaped command line (flair#2056)? It shows what the process
|
|
190
|
+
* was started with, not that it serves anything.
|
|
191
|
+
*
|
|
192
|
+
* The first argument's basename is `node` or `bun`, and the second is a Harper
|
|
193
|
+
* entry path: `…/node_modules/harper/dist/bin/harper.js` or
|
|
194
|
+
* `…/node_modules/@<scope>/harper/dist/bin/harper.js` (what `flair start`, the
|
|
195
|
+
* launchd launcher and a systemd unit pass), or `dist/bin/harper.js` (what
|
|
196
|
+
* Harper's own restart forks). A second argument starting with `-`, or a Harper
|
|
197
|
+
* path in a later argument, does not match.
|
|
198
|
+
*
|
|
199
|
+
* `cmdline` is `/proc/<pid>/cmdline` (NUL-separated: the arguments exactly) or
|
|
200
|
+
* the line `ps -o command=` reports (arguments joined by spaces; split here on
|
|
201
|
+
* whitespace). From the `ps` line this does not establish which argument is the
|
|
202
|
+
* script: an argument containing a space can produce a matching line.
|
|
203
|
+
*/
|
|
204
|
+
export function isHarperProcessCommandLine(cmdline) {
|
|
205
|
+
const args = cmdline.includes("\u0000") ? cmdline.split("\u0000") : cmdline.trim().split(/\s+/);
|
|
206
|
+
if (args.length < 2)
|
|
207
|
+
return false;
|
|
208
|
+
const exe = args[0].split("/").pop();
|
|
209
|
+
if (exe !== "node" && exe !== "bun")
|
|
210
|
+
return false;
|
|
211
|
+
const second = args[1];
|
|
212
|
+
if (second.startsWith("-"))
|
|
213
|
+
return false;
|
|
214
|
+
return second === HARPER_RESTART_ENTRY || HARPER_ENTRY_PATH.test(second);
|
|
215
|
+
}
|
|
148
216
|
/**
|
|
149
217
|
* Parse `/proc/<pid>/stat` field 22 (starttime, in clock ticks).
|
|
150
218
|
*
|
|
@@ -334,3 +402,38 @@ export function shouldAdoptMissingSidecar(input) {
|
|
|
334
402
|
return false;
|
|
335
403
|
return true;
|
|
336
404
|
}
|
|
405
|
+
export function classifySidecarStaleness(sidecar, liveness) {
|
|
406
|
+
// Absent / unreadable is not a stale sidecar to drop; callers only ask about
|
|
407
|
+
// a sidecar they could read.
|
|
408
|
+
if (sidecar.kind !== "present")
|
|
409
|
+
return { kind: "live" };
|
|
410
|
+
if (liveness === null)
|
|
411
|
+
return { kind: "unknown" };
|
|
412
|
+
if (liveness.kind === "gone")
|
|
413
|
+
return { kind: "stale" };
|
|
414
|
+
if (liveness.kind === "alive" || liveness.kind === "eperm")
|
|
415
|
+
return { kind: "live" };
|
|
416
|
+
// liveness.kind === "unknown" — not stale.
|
|
417
|
+
return { kind: "unknown" };
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* The #2055 stop-time gate: remove the sidecar ONLY when the pid it names is
|
|
421
|
+
* CONFIRMED gone AND a fresh O_NOFOLLOW read still names that same pid.
|
|
422
|
+
*
|
|
423
|
+
* `observedPid` is the pid the sidecar named when the stop was observed;
|
|
424
|
+
* `observedPidLiveness` is that pid read AFTER the stop's wait — `gone` (ESRCH)
|
|
425
|
+
* is the confirmation. A process that survived, one owned by another user
|
|
426
|
+
* (EPERM), or one whose liveness could not be read removes nothing.
|
|
427
|
+
*
|
|
428
|
+
* `sidecar` is a fresh O_NOFOLLOW read taken after the stop. A sidecar another
|
|
429
|
+
* supervisor rewrote in between names a DIFFERENT pid and is left alone (the
|
|
430
|
+
* "still names that pid" check). A symlinked or malformed sidecar arrives here
|
|
431
|
+
* as `unreadable` (the read never followed the link) and is likewise not
|
|
432
|
+
* removed — removing on unknown evidence is the defect this gate exists to
|
|
433
|
+
* prevent.
|
|
434
|
+
*/
|
|
435
|
+
export function shouldRemoveSidecarAfterStop(input) {
|
|
436
|
+
if (input.observedPidLiveness.kind !== "gone")
|
|
437
|
+
return false;
|
|
438
|
+
return input.sidecar.kind === "present" && input.sidecar.pid === input.observedPid;
|
|
439
|
+
}
|
package/dist/lib/doctor-run.js
CHANGED
|
@@ -45,6 +45,7 @@ export const DOCTOR_CHECK_IDS = [
|
|
|
45
45
|
"verified-read",
|
|
46
46
|
"keys-prune",
|
|
47
47
|
"launchd-management",
|
|
48
|
+
"worker-threads",
|
|
48
49
|
];
|
|
49
50
|
const MCP_CLIENT_IDS = ["claude-code", "codex", "gemini", "cursor", "antigravity"];
|
|
50
51
|
function result(id, label, status, extra = {}) {
|
|
@@ -331,6 +332,71 @@ function runKeysPrune(ctx) {
|
|
|
331
332
|
detail: `classified ${agentKeyIds.length} agent / ${nodeKeyIds.length} node key(s)`,
|
|
332
333
|
});
|
|
333
334
|
}
|
|
335
|
+
export function readWorkerThreadsObservation(raw) {
|
|
336
|
+
if (raw === undefined)
|
|
337
|
+
return { kind: "serving" };
|
|
338
|
+
if (raw === null || typeof raw !== "object" || Array.isArray(raw))
|
|
339
|
+
return { kind: "unknown" };
|
|
340
|
+
const rec = raw;
|
|
341
|
+
if (rec.state !== "refused" && rec.state !== "unsafe-opt-in")
|
|
342
|
+
return { kind: "unknown" };
|
|
343
|
+
const workerCount = rec.workerCount;
|
|
344
|
+
return {
|
|
345
|
+
kind: "refused",
|
|
346
|
+
state: rec.state,
|
|
347
|
+
workerCount: typeof workerCount === "number" && Number.isFinite(workerCount) ? workerCount : null,
|
|
348
|
+
};
|
|
349
|
+
}
|
|
350
|
+
export function interpretFlairHealth(status, body) {
|
|
351
|
+
const observation = readWorkerThreadsObservation(body?.multiWorker);
|
|
352
|
+
if (status >= 200 && status < 300)
|
|
353
|
+
return { reaching: true, observation };
|
|
354
|
+
if (status === 503 && observation.kind === "refused")
|
|
355
|
+
return { reaching: true, observation };
|
|
356
|
+
return { reaching: false, observation: null };
|
|
357
|
+
}
|
|
358
|
+
export async function probeFlairHealth(url, fetchImpl = fetch, timeoutMs = 3000) {
|
|
359
|
+
try {
|
|
360
|
+
const res = await fetchImpl(url, { signal: AbortSignal.timeout(timeoutMs) });
|
|
361
|
+
let body = null;
|
|
362
|
+
try {
|
|
363
|
+
body = await res.json();
|
|
364
|
+
}
|
|
365
|
+
catch {
|
|
366
|
+
body = null;
|
|
367
|
+
}
|
|
368
|
+
const verdict = interpretFlairHealth(res.status, body);
|
|
369
|
+
return { ...verdict, status: res.status, body };
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
return { reaching: false, status: 0, body: null, observation: null };
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
function runWorkerThreads(ctx) {
|
|
376
|
+
const id = "worker-threads";
|
|
377
|
+
const label = "worker threads";
|
|
378
|
+
const observed = ctx.workerThreads;
|
|
379
|
+
if (!observed) {
|
|
380
|
+
return result(id, label, "skip", { detail: "no /Health response observed at the configured URL" });
|
|
381
|
+
}
|
|
382
|
+
if (observed.kind === "unknown") {
|
|
383
|
+
return result(id, label, "fail", {
|
|
384
|
+
detail: "the /Health response carried a worker-thread state this doctor does not recognize",
|
|
385
|
+
remedy: "Inspect /Health and re-run doctor",
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
if (observed.kind === "serving") {
|
|
389
|
+
return result(id, label, "pass", { detail: "the configured /Health response has no multi-worker refusal field" });
|
|
390
|
+
}
|
|
391
|
+
const count = observed.workerCount === null ? "an unreadable worker count" : `${observed.workerCount} Harper worker threads`;
|
|
392
|
+
const detail = observed.state === "refused"
|
|
393
|
+
? `the configured /Health response reports refusal with ${count}; multi-worker is unsupported until the readiness work lands`
|
|
394
|
+
: `the configured /Health response reports ${count} and an explicit unsafe opt-in`;
|
|
395
|
+
return result(id, label, "fail", {
|
|
396
|
+
detail,
|
|
397
|
+
remedy: "Set THREADS_COUNT=1 and restart flair",
|
|
398
|
+
});
|
|
399
|
+
}
|
|
334
400
|
function runLaunchdManagement(ctx) {
|
|
335
401
|
const id = "launchd-management";
|
|
336
402
|
const label = "launchd management";
|
|
@@ -348,6 +414,11 @@ function runLaunchdManagement(ctx) {
|
|
|
348
414
|
if (m.state === "not-applicable" || m.state === "no-service") {
|
|
349
415
|
return result(id, label, "skip", { detail: m.detail, launchd: m });
|
|
350
416
|
}
|
|
417
|
+
// flair#2040: launchd runs the job but the serving process is unidentified —
|
|
418
|
+
// not an alarm (nothing shows it is detached), and never a pass.
|
|
419
|
+
if (m.state === "unverified") {
|
|
420
|
+
return result(id, label, "warn", { detail: m.detail, remedy: m.remedy?.join(" && "), launchd: m });
|
|
421
|
+
}
|
|
351
422
|
// flair#1693: a registered plist that carries the admin password INLINE is a
|
|
352
423
|
// downgrade from the flair#1573 pass-file shape — the secret lives in a
|
|
353
424
|
// world-readable config file. The running job may be unaffected (launchd
|
|
@@ -383,6 +454,7 @@ export const DOCTOR_CHECKS = [
|
|
|
383
454
|
{ id: "verified-read", label: "per-agent verified-read", run: runVerifiedRead },
|
|
384
455
|
{ id: "keys-prune", label: "keys prune classification", run: runKeysPrune },
|
|
385
456
|
{ id: "launchd-management", label: "launchd management", run: runLaunchdManagement },
|
|
457
|
+
{ id: "worker-threads", label: "worker threads", run: runWorkerThreads },
|
|
386
458
|
];
|
|
387
459
|
/**
|
|
388
460
|
* Run every catalog id. Missing implementations and thrown runners become
|
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* embed-gpu-doctor.ts — the flair doctor line for
|
|
3
|
-
* (flair#1437
|
|
2
|
+
* embed-gpu-doctor.ts — the flair doctor line for a Metal offload that is
|
|
3
|
+
* either engine-reported CPU or unconfirmed (flair#1437; severity split
|
|
4
|
+
* flair#1761; readback flair#2031).
|
|
4
5
|
*
|
|
5
|
-
* /Health
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
6
|
+
* /Health carries `embedding.fallback` only when the engine itself reports
|
|
7
|
+
* CPU after a GPU request, and `embedding.backend === "unconfirmed"` during
|
|
8
|
+
* warmup or when the engine exposed no GPU type or layer count. The pending
|
|
9
|
+
* marker distinguishes the normal warmup window. Operators read `flair
|
|
10
|
+
* doctor`, not Health. This is the single decision: given the public Health
|
|
11
|
+
* embedding field, what (if anything) doctor prints, and whether it weighs
|
|
12
|
+
* on doctor's exit code.
|
|
10
13
|
*
|
|
11
|
-
* flair#1761: the finding is NOT one severity.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* host without the operator asking. So the severity is read off `source`:
|
|
14
|
+
* flair#1761: the finding is NOT one severity. Since 0.55.0 the derived Metal
|
|
15
|
+
* default requests offload on every darwin-arm64 host without the operator
|
|
16
|
+
* asking. Severity is read off `source`:
|
|
15
17
|
*
|
|
16
18
|
* source === "detected" → the default chose for the operator; advisory
|
|
17
19
|
* WARNING, does not count as an issue.
|
|
@@ -19,32 +21,48 @@
|
|
|
19
21
|
* FLAIR_EMBED_GPU_LAYERS; blocking ERROR.
|
|
20
22
|
* missing / unrecognized → FAIL CLOSED: blocking ERROR, exactly as before.
|
|
21
23
|
* An unknown state is never softened to advisory.
|
|
24
|
+
*
|
|
25
|
+
* Unconfirmed is not worded as "did not engage; running CPU".
|
|
22
26
|
*/
|
|
23
27
|
export const EMBED_GPU_DOCTOR_MARKER = "requested GPU offload; Metal did not engage";
|
|
24
|
-
/** Advisory wording (flair#1761).
|
|
25
|
-
* was NOT confirmed — the old "did not engage" over-claimed. */
|
|
28
|
+
/** Advisory wording (flair#1761). A derived default stays a warning. */
|
|
26
29
|
export const EMBED_GPU_DETECTED_MESSAGE = "Automatic Metal acceleration was requested, but engagement could not be verified.";
|
|
30
|
+
/**
|
|
31
|
+
* Blocking wording when the operator asked for offload and the engine
|
|
32
|
+
* exposed no readback (flair#2031). Does not claim the run is on CPU.
|
|
33
|
+
*/
|
|
34
|
+
export const EMBED_GPU_UNCONFIRMED_DOCTOR_MESSAGE = "GPU offload was requested, but the embedding engine did not report whether Metal engaged.";
|
|
27
35
|
/**
|
|
28
36
|
* Doctor finding, or null when there is nothing to say.
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
* `source` (flair#1761):
|
|
32
|
-
*
|
|
33
|
-
* an unrecognized source fails closed.
|
|
37
|
+
* A stated CPU default or a confirmed Metal run is not an issue. A finding
|
|
38
|
+
* is the engine-reported CPU fallback, or `backend: "unconfirmed"`. The
|
|
39
|
+
* severity depends on `source` (flair#1761): the derived Metal default is
|
|
40
|
+
* advisory and an unrecognized source fails closed.
|
|
34
41
|
*/
|
|
35
42
|
export function describeEmbedGpuDoctorFinding(embedding) {
|
|
36
43
|
if (embedding == null || typeof embedding !== "object" || Array.isArray(embedding)) {
|
|
37
44
|
return null;
|
|
38
45
|
}
|
|
39
|
-
const
|
|
40
|
-
|
|
46
|
+
const record = embedding;
|
|
47
|
+
const fallback = record.fallback;
|
|
48
|
+
const cpuFallback = typeof fallback === "string" && fallback.includes(EMBED_GPU_DOCTOR_MARKER);
|
|
49
|
+
const unconfirmed = record.backend === "unconfirmed";
|
|
50
|
+
if (!cpuFallback && !unconfirmed)
|
|
41
51
|
return null;
|
|
52
|
+
const source = record.source;
|
|
53
|
+
if (unconfirmed && !cpuFallback && record.pending === true) {
|
|
54
|
+
return {
|
|
55
|
+
isIssue: false,
|
|
56
|
+
icon: "warn",
|
|
57
|
+
message: "GPU offload was requested; embedding engine warmup is still running.",
|
|
58
|
+
fixHint: "Re-run flair doctor after warmup completes.",
|
|
59
|
+
};
|
|
42
60
|
}
|
|
43
|
-
const source = embedding.source;
|
|
44
61
|
if (source === "detected") {
|
|
45
62
|
// The Metal default requested offload for the operator; nothing was
|
|
46
|
-
// chosen.
|
|
47
|
-
//
|
|
63
|
+
// chosen. Keep it visible as a persistent warning, never a blocking
|
|
64
|
+
// finding, and do not claim the process is on CPU when readback is
|
|
65
|
+
// missing.
|
|
48
66
|
return {
|
|
49
67
|
isIssue: false,
|
|
50
68
|
icon: "warn",
|
|
@@ -53,11 +71,16 @@ export function describeEmbedGpuDoctorFinding(embedding) {
|
|
|
53
71
|
};
|
|
54
72
|
}
|
|
55
73
|
// A source the operator set ("env"), or one we do not recognize at all
|
|
56
|
-
// (missing/unknown) — fail closed
|
|
74
|
+
// (missing/unknown) — fail closed. Unconfirmed does not reuse the CPU
|
|
75
|
+
// sentence.
|
|
57
76
|
return {
|
|
58
77
|
isIssue: true,
|
|
59
78
|
icon: "error",
|
|
60
|
-
message:
|
|
61
|
-
|
|
79
|
+
message: unconfirmed && !cpuFallback
|
|
80
|
+
? EMBED_GPU_UNCONFIRMED_DOCTOR_MESSAGE
|
|
81
|
+
: fallback,
|
|
82
|
+
fixHint: unconfirmed && !cpuFallback
|
|
83
|
+
? "Pin CPU with FLAIR_EMBED_GPU_LAYERS=0, or restart Flair so the embedding engine can report its backend."
|
|
84
|
+
: "Pin CPU with FLAIR_EMBED_GPU_LAYERS=0, or restore @node-llama-cpp/mac-arm64-metal and flair restart.",
|
|
62
85
|
};
|
|
63
86
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The outbound federation projection uses the same declared and named Memory
|
|
3
|
+
* fields as the inbound guard. Pointer inputs are absent from that contract.
|
|
4
|
+
*/
|
|
5
|
+
import { MEMORY_ATTRIBUTES, UNDECLARED_ALLOWED } from "./memory-attributes.js";
|
|
6
|
+
export const FEDERATION_MEMORY_ATTRIBUTES = MEMORY_ATTRIBUTES;
|
|
7
|
+
export const FEDERATION_MEMORY_UNDECLARED = UNDECLARED_ALLOWED;
|
|
8
|
+
/** Harper rejects undeclared names in a string projection. Its named object
|
|
9
|
+
* selectors can read these stored fields; the nested wildcard keeps the whole
|
|
10
|
+
* value of `meta` (including arbitrary journal keys), never the whole Memory
|
|
11
|
+
* row. Scalar values such as `kind` are returned directly. See Harper's
|
|
12
|
+
* searchValidator and Table.transformEntryForSelect. */
|
|
13
|
+
export const FEDERATION_MEMORY_SELECT = Object.freeze(FEDERATION_MEMORY_ATTRIBUTES.map((attribute) => FEDERATION_MEMORY_UNDECLARED.includes(attribute)
|
|
14
|
+
? Object.freeze({ name: attribute, select: Object.freeze(["*"]) })
|
|
15
|
+
: attribute));
|