@tpsdev-ai/flair 0.56.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.
Files changed (153) hide show
  1. package/README.md +19 -20
  2. package/SECURITY.md +4 -1
  3. package/dist/build-info.json +3 -3
  4. package/dist/cli.js +1800 -293
  5. package/dist/commands/agent.js +22 -15
  6. package/dist/commands/bridge.js +40 -3
  7. package/dist/commands/doctor.js +214 -4
  8. package/dist/commands/federation.js +265 -38
  9. package/dist/commands/hook.js +10 -2
  10. package/dist/commands/init.js +179 -48
  11. package/dist/commands/memory.js +24 -2
  12. package/dist/commands/quality.js +26 -18
  13. package/dist/commands/service.js +137 -22
  14. package/dist/commands/status.js +114 -3
  15. package/dist/commands/upgrade.js +204 -84
  16. package/dist/doctor-client.js +50 -4
  17. package/dist/federation/scheduler.js +253 -4
  18. package/dist/install/clients.js +89 -23
  19. package/dist/install/global-bin-path.js +26 -6
  20. package/dist/lib/atomic-write.js +256 -0
  21. package/dist/lib/auth-resolve.js +46 -0
  22. package/dist/lib/daemon-liveness.js +61 -0
  23. package/dist/lib/doctor-run.js +5 -0
  24. package/dist/lib/embed-gpu-doctor.js +49 -26
  25. package/dist/lib/federation-memory-attributes.js +15 -0
  26. package/dist/lib/init-admin-pass.js +4 -2
  27. package/dist/lib/init-listener-environ.js +38 -0
  28. package/dist/lib/init-occupied-listener.js +165 -0
  29. package/dist/lib/instance-identity-row.js +580 -0
  30. package/dist/lib/launchd-domain-preflight.js +448 -0
  31. package/dist/lib/launchd-management.js +176 -6
  32. package/dist/lib/launchd-repair.js +95 -0
  33. package/dist/lib/memory-attributes.js +77 -0
  34. package/dist/lib/node-alias-path.js +95 -0
  35. package/dist/lib/npm-registry.js +119 -6
  36. package/dist/lib/owned-pins.js +20 -6
  37. package/dist/lib/redact-token-id.js +197 -0
  38. package/dist/lib/service-repoint-apply.js +267 -0
  39. package/dist/lib/service-repoint.js +575 -0
  40. package/dist/lib/toml-string-array.js +104 -0
  41. package/dist/lib/tree-divergence.js +716 -0
  42. package/dist/lib/upgrade-plain-tree.js +18 -8
  43. package/dist/lib/upgrade-prior-liveness.js +69 -0
  44. package/dist/lib/upgrade-rollback.js +116 -0
  45. package/dist/rem/runner.js +11 -0
  46. package/dist/resources/Agent.js +27 -14
  47. package/dist/resources/AgentSeed.js +39 -2
  48. package/dist/resources/Asset.js +3 -11
  49. package/dist/resources/Credential.js +4 -10
  50. package/dist/resources/Federation.js +299 -127
  51. package/dist/resources/Integration.js +8 -32
  52. package/dist/resources/Memory.js +623 -86
  53. package/dist/resources/MemoryArchive.js +9 -0
  54. package/dist/resources/MemoryBootstrap.js +23 -12
  55. package/dist/resources/MemoryCandidate.js +3 -11
  56. package/dist/resources/MemoryDedupStats.js +1 -1
  57. package/dist/resources/MemoryFeed.js +214 -5
  58. package/dist/resources/MemoryGrant.js +19 -40
  59. package/dist/resources/MemoryHostSource.js +90 -0
  60. package/dist/resources/MemoryMaintenance.js +121 -32
  61. package/dist/resources/MemoryReflect.js +4 -1
  62. package/dist/resources/MemoryReindex.js +7 -4
  63. package/dist/resources/MemoryUsage.js +55 -18
  64. package/dist/resources/Presence.js +18 -10
  65. package/dist/resources/RecordUsage.js +30 -40
  66. package/dist/resources/Relationship.js +170 -89
  67. package/dist/resources/SemanticSearch.js +44 -19
  68. package/dist/resources/Soul.js +31 -19
  69. package/dist/resources/XAA.js +7 -3
  70. package/dist/resources/agent-auth.js +16 -10
  71. package/dist/resources/auth-middleware.js +164 -50
  72. package/dist/resources/auto-promote-lib.js +4 -4
  73. package/dist/resources/bm25-index-service.js +246 -32
  74. package/dist/resources/bm25-status.js +132 -0
  75. package/dist/resources/bm25.js +13 -14
  76. package/dist/resources/ed25519-auth.js +11 -54
  77. package/dist/resources/embed-gpu.js +262 -47
  78. package/dist/resources/embeddings-boot.js +52 -17
  79. package/dist/resources/federation-classify.js +12 -0
  80. package/dist/resources/federation-cleanup.js +289 -51
  81. package/dist/resources/federation-crypto.js +65 -19
  82. package/dist/resources/health.js +105 -22
  83. package/dist/resources/host-pointer/registry.js +35 -0
  84. package/dist/resources/host-pointer-adapter.js +15 -0
  85. package/dist/resources/host-source-visibility.js +167 -0
  86. package/dist/resources/host-source.js +151 -0
  87. package/dist/resources/in-process-api.js +2 -1
  88. package/dist/resources/instance-create-lock.js +363 -0
  89. package/dist/resources/instance-identity-rows.js +43 -0
  90. package/dist/resources/instance-identity.js +97 -12
  91. package/dist/resources/mcp-handler.js +155 -51
  92. package/dist/resources/mcp-tool-arguments.js +83 -0
  93. package/dist/resources/mcp-tools.js +28 -25
  94. package/dist/resources/memory-consolidate-lib.js +3 -1
  95. package/dist/resources/memory-declared-attributes.js +103 -0
  96. package/dist/resources/memory-feed-lib.js +5 -1
  97. package/dist/resources/memory-host-source.js +163 -0
  98. package/dist/resources/memory-visibility.js +10 -7
  99. package/dist/resources/migrations/synthetic-test-migration.js +4 -1
  100. package/dist/resources/migrations/visibility-backfill.js +4 -1
  101. package/dist/resources/originator-instance.js +235 -0
  102. package/dist/resources/promotion-stamp.js +2 -0
  103. package/dist/resources/provenance.js +111 -11
  104. package/dist/resources/rate-limit.js +6 -4
  105. package/dist/resources/record-owner-guard.js +19 -0
  106. package/dist/resources/record-type-kit.js +44 -23
  107. package/dist/resources/record-types.js +6 -6
  108. package/dist/resources/relay-ops.js +28 -3
  109. package/dist/resources/replay-store.js +289 -0
  110. package/dist/resources/request-transaction.js +78 -0
  111. package/dist/resources/search-readiness.js +26 -22
  112. package/dist/resources/semantic-retrieval-core.js +103 -4
  113. package/dist/resources/table-helpers.js +33 -0
  114. package/dist/resources/table-patch-policy.js +109 -0
  115. package/dist/resources/table-patches.js +17 -0
  116. package/dist/resources/table-post-policy.js +138 -0
  117. package/dist/resources/table-posts.js +22 -0
  118. package/dist/resources/table-subscription-policy.js +135 -0
  119. package/dist/resources/table-subscriptions.js +19 -0
  120. package/dist/resources/tool-descriptors/index.js +15 -15
  121. package/dist/resources/trust-block.js +10 -3
  122. package/dist/resources/usage-recording.js +169 -41
  123. package/dist/src/lib/instance-identity-row.js +580 -0
  124. package/dist/src/lib/memory-attributes.js +77 -0
  125. package/dist/src/lib/redact-token-id.js +197 -0
  126. package/docs/api-reference.md +80 -16
  127. package/docs/auth.md +5 -5
  128. package/docs/claude-code.md +137 -6
  129. package/docs/deploying-on-fabric.md +6 -3
  130. package/docs/deployment.md +1 -1
  131. package/docs/embedding-in-a-harper-app.md +3 -3
  132. package/docs/federation.md +17 -10
  133. package/docs/hosted-on-fabric.md +4 -3
  134. package/docs/integrations.md +28 -19
  135. package/docs/mcp-clients.md +26 -32
  136. package/docs/n8n.md +8 -8
  137. package/docs/notes/mcp-oauth-model2.md +27 -18
  138. package/docs/openclaw.md +29 -28
  139. package/docs/quickstart.md +5 -0
  140. package/docs/releasing.md +134 -2
  141. package/docs/spoke-bringup.md +14 -11
  142. package/docs/standalone-local.md +2 -2
  143. package/docs/supply-chain-policy.md +28 -10
  144. package/docs/system-requirements.md +1 -1
  145. package/docs/troubleshooting.md +11 -0
  146. package/docs/upgrade.md +189 -63
  147. package/package.json +9 -5
  148. package/schemas/agent.graphql +9 -6
  149. package/schemas/federation.graphql +2 -11
  150. package/schemas/memory.graphql +106 -17
  151. package/schemas/replay.graphql +18 -0
  152. package/templates/launchd/start-flair-with-admin-pass.sh +22 -0
  153. package/dist/resources/federation-nonce-store.js +0 -195
@@ -0,0 +1,267 @@
1
+ /**
2
+ * service-repoint-apply.ts — commit a re-point plan to an operator's unit file
3
+ * (flair#2034 §2).
4
+ *
5
+ * The planners in service-repoint.ts are pure. This module is where a plan
6
+ * meets the file and the service manager, and it holds three rules:
7
+ *
8
+ * 1. A plan is written only over the file it was PLANNED FROM: the file was
9
+ * read as a regular file (never through a symlink), and immediately before
10
+ * the rename it is re-checked — same file, same bytes — or nothing is
11
+ * written (atomic-write.ts, `expect`).
12
+ * 2. systemd: after the write, the manager is reloaded and asked what it now
13
+ * holds for the unit (the file it loaded, its drop-ins, its working
14
+ * directory). When the reload fails or the manager does not hold what was
15
+ * written, the previous bytes are put back, the manager is reloaded again
16
+ * and ASKED AGAIN. That query compares three fields — FragmentPath,
17
+ * drop-ins, WorkingDirectory — with the values captured before the write;
18
+ * when they are back, the result says exactly that, and still calls full
19
+ * agreement between the restored file and the manager unverified (three
20
+ * fields are not the whole unit). Anything short of that — a failed
21
+ * restore, a failed second reload, a different or missing answer — is
22
+ * reported with the manager's state as UNVERIFIED.
23
+ * 3. Linux: a unit with drop-ins — any the manager reports, from any
24
+ * location, or a `<unit>.d` directory beside the file — is refused, and
25
+ * so is one whose MainPID, asked again immediately before the write, is
26
+ * not the serving process the unit was proven to run.
27
+ * 4. `flair restart` on Linux (restartOnLinux): a proven user unit is
28
+ * restarted through systemd and its new main process verified; a process
29
+ * that is the main process of any other systemd unit is never stopped and
30
+ * respawned outside it.
31
+ *
32
+ * Every filesystem and service-manager call is injectable, so each branch is
33
+ * driven by a test with no real launchd, systemd or operator file.
34
+ */
35
+ import { snapshotRegularFile, writeFilesAtomically } from "./atomic-write.js";
36
+ import { planSystemdUnitRuntimeRepoint } from "./service-repoint.js";
37
+ import { planLinuxRestart, restartedUnitProblem, } from "./tree-divergence.js";
38
+ function msg(err) {
39
+ return err?.message ?? String(err);
40
+ }
41
+ /** Apply `plan`, which was computed from `planned` (see the module header). */
42
+ export function applyRepointPlan(plan, planned, opts = {}) {
43
+ const unitPath = planned.path;
44
+ switch (plan.kind) {
45
+ case "current":
46
+ case "pinned-node":
47
+ return { kind: plan.kind, unitPath, detail: plan.detail };
48
+ case "refuse":
49
+ return { kind: "refused", unitPath, detail: plan.detail };
50
+ case "repoint":
51
+ break;
52
+ }
53
+ if (opts.dryRun)
54
+ return { kind: "would-repoint", unitPath, detail: plan.detail.replace(/^re-pointed/, "would re-point") };
55
+ const handEdit = opts.handEdit ? ` ${opts.handEdit}` : "";
56
+ try {
57
+ writeFilesAtomically([{ path: unitPath, content: plan.text, mode: planned.mode, expect: planned }], opts.atomic);
58
+ }
59
+ catch (err) {
60
+ return {
61
+ kind: "refused",
62
+ unitPath,
63
+ detail: `${unitPath} was not re-pointed: ${msg(err)}. Re-run the command to plan again from the file as it is now.${handEdit}`,
64
+ };
65
+ }
66
+ if (!opts.reload)
67
+ return { kind: "repointed", unitPath, detail: plan.detail };
68
+ let failure = null;
69
+ try {
70
+ opts.reload();
71
+ failure = opts.verifyLoaded?.("planned") ?? null;
72
+ }
73
+ catch (err) {
74
+ failure = `the service manager could not reload it (${msg(err)})`;
75
+ }
76
+ if (failure === null)
77
+ return { kind: "repointed", unitPath, detail: plan.detail };
78
+ const unverified = "Run: systemctl --user daemon-reload, then check the unit with systemctl --user show.";
79
+ // Put the planned-from bytes back — only over the bytes flair itself wrote.
80
+ try {
81
+ const written = snapshotRegularFile(unitPath, { lstat: opts.atomic?.lstat, readBytes: opts.atomic?.readBytes });
82
+ if (written.content !== plan.text)
83
+ throw new Error(`${unitPath} was changed after flair wrote it`);
84
+ writeFilesAtomically([{ path: unitPath, content: planned.content, mode: planned.mode, expect: written }], opts.atomic);
85
+ }
86
+ catch (err) {
87
+ return {
88
+ kind: "refused",
89
+ unitPath,
90
+ detail: `${unitPath} was re-pointed on disk, but ${failure}, and restoring its previous content failed (${msg(err)}). ` +
91
+ "The file holds the RE-POINTED unit flair wrote (or, if it was changed after that write, the change); which " +
92
+ `unit the service manager holds is UNVERIFIED. Check the file by hand. ${unverified}`,
93
+ };
94
+ }
95
+ try {
96
+ opts.reload();
97
+ }
98
+ catch (err) {
99
+ return {
100
+ kind: "refused",
101
+ unitPath,
102
+ detail: `${unitPath} was not re-pointed: ${failure}. flair restored the file's previous content, but reloading the ` +
103
+ `service manager again failed (${msg(err)}): the file holds the previous unit; which unit the manager holds is ` +
104
+ `UNVERIFIED. ${unverified}`,
105
+ };
106
+ }
107
+ let back;
108
+ try {
109
+ back = opts.verifyLoaded ? opts.verifyLoaded("previous") : "flair cannot query this service manager";
110
+ }
111
+ catch (err) {
112
+ back = `the service manager could not be queried (${msg(err)})`;
113
+ }
114
+ if (back !== null) {
115
+ return {
116
+ kind: "refused",
117
+ unitPath,
118
+ detail: `${unitPath} was not re-pointed: ${failure}. flair restored the file's previous content and reloaded the ` +
119
+ "service manager, but the manager does not report the FragmentPath, drop-ins and WorkingDirectory captured " +
120
+ `before the write (${back}): the file holds the previous unit; which unit the manager holds is UNVERIFIED. ${unverified}`,
121
+ };
122
+ }
123
+ return {
124
+ kind: "refused",
125
+ unitPath,
126
+ detail: `${unitPath} was not re-pointed: ${failure}. flair restored the file's previous content and reloaded the service ` +
127
+ "manager; the manager's FragmentPath, drop-ins and WorkingDirectory are back at the values captured before the " +
128
+ "write. Only those three fields were checked, so full agreement between the restored file and what the manager " +
129
+ `loaded is unverified.${handEdit}`,
130
+ };
131
+ }
132
+ /**
133
+ * Re-point the systemd USER unit proven to own the serving process (Linux).
134
+ * Refuses drop-ins and anything that is not a regular unit file; plans from the
135
+ * snapshot; after the reload, requires the manager to report the same file, no
136
+ * drop-ins, and the new working directory — else restores (see applyRepointPlan).
137
+ */
138
+ export function repointSystemdUserUnit(serving, targets, deps, opts = {}) {
139
+ const unitPath = serving.unitPath;
140
+ const handEdit = `point its node, Harper entry${targets.launcher ? ", launcher (if it runs one)" : ""} and WorkingDirectory at ` +
141
+ `${targets.workingDirectory} by hand, then run: flair restart`;
142
+ if (serving.dropInPaths.length > 0) {
143
+ return {
144
+ kind: "refused",
145
+ unitPath,
146
+ detail: `systemd applies drop-ins to ${serving.unitName} (${serving.dropInPaths.join(", ")}), and a drop-in can set ` +
147
+ `ExecStart= or WorkingDirectory=, so flair does not re-point it. To move it, ${handEdit}`,
148
+ };
149
+ }
150
+ if (deps.exists(`${unitPath}.d`)) {
151
+ return {
152
+ kind: "refused",
153
+ unitPath,
154
+ detail: `${unitPath}.d exists and can hold drop-ins that set ExecStart= or WorkingDirectory=, so flair does not ` +
155
+ `re-point the unit. To move it, ${handEdit}`,
156
+ };
157
+ }
158
+ let planned;
159
+ try {
160
+ planned = snapshotRegularFile(unitPath, { lstat: deps.atomic?.lstat, readBytes: deps.atomic?.readBytes });
161
+ }
162
+ catch (err) {
163
+ return {
164
+ kind: "refused",
165
+ unitPath,
166
+ detail: `${unitPath} is not re-pointed: ${msg(err)}; flair rewrites only a regular unit file in place. To move it, ${handEdit}`,
167
+ };
168
+ }
169
+ const plan = planSystemdUnitRuntimeRepoint(planned.content, serving.unitTree ?? serving.dir, targets, deps.repoint, unitPath);
170
+ // Capture what the manager holds BEFORE writing: a restore is confirmed
171
+ // against it. When it cannot be read, a failed reload could never be
172
+ // verified, so nothing is written. The same answer must still name the
173
+ // serving process as the unit's MainPID.
174
+ let before = null;
175
+ if (plan.kind === "repoint" && !opts.dryRun) {
176
+ before = deps.unitState(serving.unitName);
177
+ if (!before || before.fragmentPath === null || !deps.repoint.samePath(before.fragmentPath, unitPath) || before.dropInPaths.length > 0) {
178
+ return {
179
+ kind: "refused",
180
+ unitPath,
181
+ detail: `${unitPath} is not re-pointed: systemd ${before ? `reports ${serving.unitName} from ${before.fragmentPath ?? "no file"} with drop-ins [${before.dropInPaths.join(", ")}]` : `did not report ${serving.unitName}`}, ` +
182
+ `so the state to restore to cannot be captured. To move it, ${handEdit}`,
183
+ };
184
+ }
185
+ // The write rests on the proof that this unit runs the serving process (its
186
+ // MainPID). A fresh answer naming another main process, or none, contradicts
187
+ // that proof, so nothing is written.
188
+ if (before.mainPid !== serving.pid) {
189
+ return {
190
+ kind: "refused",
191
+ unitPath,
192
+ detail: `${unitPath} is not re-pointed: systemd now reports ` +
193
+ `${before.mainPid === null ? "no main process" : `pid ${before.mainPid} as the main process`} ` +
194
+ `of ${serving.unitName}, ` +
195
+ `not the serving process (pid ${serving.pid}) it was proven to run, so the unit is not shown to run this ` +
196
+ `instance. Run \`flair status\` to see which process serves it, then re-run the command. To move it, ${handEdit}`,
197
+ };
198
+ }
199
+ }
200
+ const same = (a, b) => (a === null || b === null ? a === b : deps.repoint.samePath(a, b));
201
+ return applyRepointPlan(plan, planned, {
202
+ dryRun: opts.dryRun,
203
+ atomic: deps.atomic,
204
+ reload: deps.reload,
205
+ handEdit: `To move it, ${handEdit}`,
206
+ verifyLoaded: (want) => {
207
+ const st = deps.unitState(serving.unitName);
208
+ if (!st)
209
+ return `systemd did not report ${serving.unitName} after the reload`;
210
+ if (want === "previous") {
211
+ const was = before;
212
+ if (!same(st.fragmentPath, was.fragmentPath) ||
213
+ st.dropInPaths.join("\n") !== was.dropInPaths.join("\n") ||
214
+ !same(st.workingDirectory, was.workingDirectory)) {
215
+ return (`systemd reports ${serving.unitName} from ${st.fragmentPath ?? "no file"}, drop-ins [${st.dropInPaths.join(", ")}], ` +
216
+ `WorkingDirectory ${st.workingDirectory ?? "(none)"}; before the write it was ${was.fragmentPath}, drop-ins [], ` +
217
+ `WorkingDirectory ${was.workingDirectory ?? "(none)"}`);
218
+ }
219
+ return null;
220
+ }
221
+ if (st.fragmentPath === null || !deps.repoint.samePath(st.fragmentPath, unitPath)) {
222
+ return `after the reload systemd loads ${serving.unitName} from ${st.fragmentPath ?? "no file"}, not ${unitPath}`;
223
+ }
224
+ if (st.dropInPaths.length > 0) {
225
+ return `after the reload systemd applies drop-ins to ${serving.unitName} (${st.dropInPaths.join(", ")})`;
226
+ }
227
+ if (st.workingDirectory === null || !deps.repoint.samePath(st.workingDirectory, targets.workingDirectory)) {
228
+ return `after the reload systemd reports WorkingDirectory ${st.workingDirectory ?? "(none)"}, not ${targets.workingDirectory}`;
229
+ }
230
+ return null;
231
+ },
232
+ });
233
+ }
234
+ /**
235
+ * `flair restart` on Linux (see planLinuxRestart). A proven user unit is
236
+ * restarted THROUGH systemd and its new main process must run from the unit's
237
+ * WorkingDirectory; a process that is the main process (MainPID) of any other
238
+ * systemd unit is refused, never stopped and respawned outside its manager, and
239
+ * so is one whose cgroup or unit MainPID cannot be read. A process no unit
240
+ * supervises — none in its cgroup, or one whose MainPID is another process —
241
+ * takes the direct path.
242
+ */
243
+ export async function restartOnLinux(d) {
244
+ const plan = planLinuxRestart({
245
+ serving: d.serving,
246
+ pids: d.pids,
247
+ procCgroup: d.procCgroup,
248
+ uid: d.uid,
249
+ unitMainPid: d.unitMainPid,
250
+ });
251
+ if (plan.kind === "refuse")
252
+ throw new Error(plan.detail);
253
+ if (plan.kind === "direct") {
254
+ await d.direct();
255
+ return "direct";
256
+ }
257
+ const s = d.serving;
258
+ d.beforeRestart?.(s);
259
+ d.log?.(` (restarting through the systemd user unit that runs this instance: ${plan.unit})`);
260
+ d.systemctl(["--user", "daemon-reload"]);
261
+ d.systemctl(["--user", "restart", plan.unit]);
262
+ await d.waitHealthy();
263
+ const problem = restartedUnitProblem(plan.unit, s.pid, d.unitState(plan.unit), d.cwdOf, d.samePath);
264
+ if (problem)
265
+ throw new Error(`restarted ${plan.unit} through systemd, but ${problem}`);
266
+ return "systemd";
267
+ }