@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.
Files changed (143) hide show
  1. package/README.md +7 -6
  2. package/SECURITY.md +4 -1
  3. package/config.yaml +3 -0
  4. package/dist/build-info.json +3 -3
  5. package/dist/cli.js +1726 -376
  6. package/dist/commands/agent.js +160 -2
  7. package/dist/commands/doctor.js +177 -27
  8. package/dist/commands/federation.js +2 -1
  9. package/dist/commands/hook.js +10 -2
  10. package/dist/commands/init.js +150 -44
  11. package/dist/commands/mcp.js +53 -22
  12. package/dist/commands/memory.js +24 -2
  13. package/dist/commands/quality.js +26 -18
  14. package/dist/commands/service.js +137 -22
  15. package/dist/commands/status.js +114 -3
  16. package/dist/commands/upgrade.js +204 -84
  17. package/dist/doctor-client.js +50 -4
  18. package/dist/federation/scheduler.js +253 -4
  19. package/dist/install/clients.js +89 -23
  20. package/dist/install/global-bin-path.js +26 -6
  21. package/dist/lib/atomic-write.js +256 -0
  22. package/dist/lib/daemon-liveness.js +103 -0
  23. package/dist/lib/doctor-run.js +72 -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/flair-agent-role.js +116 -0
  27. package/dist/lib/init-admin-pass.js +4 -2
  28. package/dist/lib/init-listener-environ.js +38 -0
  29. package/dist/lib/init-occupied-listener.js +165 -0
  30. package/dist/lib/launchd-domain-preflight.js +448 -0
  31. package/dist/lib/launchd-management.js +179 -8
  32. package/dist/lib/launchd-repair.js +95 -0
  33. package/dist/lib/mcp-enable.js +667 -72
  34. package/dist/lib/memory-attributes.js +77 -0
  35. package/dist/lib/node-alias-path.js +95 -0
  36. package/dist/lib/npm-registry.js +119 -6
  37. package/dist/lib/owned-pins.js +20 -6
  38. package/dist/lib/process-start-time.js +32 -0
  39. package/dist/lib/service-repoint-apply.js +267 -0
  40. package/dist/lib/service-repoint.js +575 -0
  41. package/dist/lib/toml-string-array.js +104 -0
  42. package/dist/lib/tree-divergence.js +716 -0
  43. package/dist/lib/upgrade-plain-tree.js +18 -8
  44. package/dist/lib/upgrade-prior-liveness.js +69 -0
  45. package/dist/lib/upgrade-rollback.js +116 -0
  46. package/dist/rem/runner.js +11 -0
  47. package/dist/resources/Agent.js +54 -21
  48. package/dist/resources/AgentSeed.js +44 -1
  49. package/dist/resources/Federation.js +84 -54
  50. package/dist/resources/Memory.js +567 -72
  51. package/dist/resources/MemoryArchive.js +9 -0
  52. package/dist/resources/MemoryBootstrap.js +125 -29
  53. package/dist/resources/MemoryFeed.js +204 -3
  54. package/dist/resources/MemoryHostSource.js +90 -0
  55. package/dist/resources/MemoryMaintenance.js +121 -32
  56. package/dist/resources/MemoryReflect.js +4 -1
  57. package/dist/resources/MemoryReindex.js +7 -4
  58. package/dist/resources/MemoryUsage.js +53 -3
  59. package/dist/resources/OrgSkillAssignment.js +209 -0
  60. package/dist/resources/Presence.js +18 -10
  61. package/dist/resources/RecordUsage.js +30 -40
  62. package/dist/resources/Relationship.js +167 -78
  63. package/dist/resources/SemanticSearch.js +19 -6
  64. package/dist/resources/Soul.js +31 -19
  65. package/dist/resources/XAA.js +47 -14
  66. package/dist/resources/agent-auth.js +16 -10
  67. package/dist/resources/agent-status-guard.js +63 -0
  68. package/dist/resources/auth-middleware.js +137 -49
  69. package/dist/resources/auto-promote-lib.js +4 -4
  70. package/dist/resources/bm25-index-service.js +246 -32
  71. package/dist/resources/bm25-status.js +132 -0
  72. package/dist/resources/ed25519-auth.js +11 -54
  73. package/dist/resources/embed-gpu.js +262 -47
  74. package/dist/resources/embeddings-boot.js +52 -17
  75. package/dist/resources/federation-classify.js +34 -0
  76. package/dist/resources/federation-crypto.js +65 -19
  77. package/dist/resources/flair-agent-role-boot.js +26 -0
  78. package/dist/resources/health.js +67 -16
  79. package/dist/resources/host-pointer/registry.js +35 -0
  80. package/dist/resources/host-pointer-adapter.js +15 -0
  81. package/dist/resources/host-source-visibility.js +167 -0
  82. package/dist/resources/host-source.js +151 -0
  83. package/dist/resources/instance-identity.js +3 -2
  84. package/dist/resources/key-lock.js +31 -0
  85. package/dist/resources/mcp-handler.js +147 -51
  86. package/dist/resources/mcp-oauth.js +3 -6
  87. package/dist/resources/mcp-tools.js +47 -25
  88. package/dist/resources/memory-consolidate-lib.js +3 -1
  89. package/dist/resources/memory-declared-attributes.js +103 -0
  90. package/dist/resources/memory-feed-lib.js +5 -1
  91. package/dist/resources/memory-host-source.js +163 -0
  92. package/dist/resources/migrations/synthetic-test-migration.js +4 -1
  93. package/dist/resources/migrations/visibility-backfill.js +4 -1
  94. package/dist/resources/multi-worker-guard.js +121 -0
  95. package/dist/resources/oauth-discovery.js +1 -0
  96. package/dist/resources/oauth-wellknown.js +8 -4
  97. package/dist/resources/originator-instance.js +235 -0
  98. package/dist/resources/promotion-stamp.js +2 -0
  99. package/dist/resources/provenance.js +111 -11
  100. package/dist/resources/record-owner-guard.js +19 -0
  101. package/dist/resources/record-type-kit.js +9 -12
  102. package/dist/resources/record-types.js +22 -6
  103. package/dist/resources/relay-ops.js +28 -3
  104. package/dist/resources/replay-store.js +336 -0
  105. package/dist/resources/request-transaction.js +78 -0
  106. package/dist/resources/search-readiness.js +19 -21
  107. package/dist/resources/semantic-retrieval-core.js +5 -1
  108. package/dist/resources/skill-manifest.js +181 -0
  109. package/dist/resources/skill-provenance.js +15 -10
  110. package/dist/resources/table-patch-policy.js +109 -0
  111. package/dist/resources/table-patches.js +17 -0
  112. package/dist/resources/table-post-policy.js +138 -0
  113. package/dist/resources/table-posts.js +22 -0
  114. package/dist/resources/table-subscription-policy.js +135 -0
  115. package/dist/resources/table-subscriptions.js +19 -0
  116. package/dist/resources/tool-descriptors/index.js +15 -15
  117. package/dist/resources/trust-block.js +10 -3
  118. package/dist/resources/usage-recording.js +169 -41
  119. package/dist/src/lib/flair-agent-role.js +116 -0
  120. package/dist/src/lib/memory-attributes.js +77 -0
  121. package/docs/access-control.md +667 -0
  122. package/docs/api-reference.md +85 -17
  123. package/docs/auth.md +4 -0
  124. package/docs/claude-code.md +132 -0
  125. package/docs/deepseek-harness.md +1 -1
  126. package/docs/deployment.md +1 -1
  127. package/docs/federation.md +1 -1
  128. package/docs/integrations.md +6 -6
  129. package/docs/mcp-clients.md +16 -24
  130. package/docs/notes/mcp-oauth-model2.md +15 -1
  131. package/docs/quickstart.md +5 -0
  132. package/docs/releasing.md +152 -20
  133. package/docs/supply-chain-policy.md +10 -5
  134. package/docs/troubleshooting.md +11 -0
  135. package/docs/upgrade.md +187 -61
  136. package/package.json +10 -5
  137. package/schemas/agent.graphql +9 -6
  138. package/schemas/federation.graphql +2 -11
  139. package/schemas/memory.graphql +137 -17
  140. package/schemas/oauth.graphql +8 -4
  141. package/schemas/replay.graphql +18 -0
  142. package/templates/launchd/start-flair-with-admin-pass.sh +95 -0
  143. package/dist/resources/federation-nonce-store.js +0 -195
@@ -1,5 +1,5 @@
1
1
  import { Resource, databases, server } from "harper";
2
- import { randomBytes } from "node:crypto";
2
+ import { randomBytes, randomUUID } from "node:crypto";
3
3
  import nacl from "tweetnacl";
4
4
  import { allowAdmin } from "./agent-auth.js";
5
5
  import { canonicalize, signBody, verifyBodySignature, signBodyFresh, verifyBodySignatureFresh, generateNonce, } from "./federation-crypto.js";
@@ -9,21 +9,29 @@ import { readAllInstanceRows } from "./instance-identity-rows.js";
9
9
  import { findOrCreateInstance } from "./instance-create-lock.js";
10
10
  import { withDetachedTxnAsync } from "./table-helpers.js";
11
11
  import { isSkillWrite } from "./skill-write.js";
12
+ import { stripInboundMemoryRow, stripServerStampedFields } from "./memory-declared-attributes.js";
12
13
  import { noteWriteStamp } from "./embedding-space-guard.js";
13
14
  import { initFederationCleanup } from "./federation-cleanup.js";
14
- import { createPersistentNonceStore, initNonceStoreCleanup } from "./federation-nonce-store.js";
15
- import { classifyRecord, reconstructRecordVerifyBody, checkPrincipalEntitlement, } from "./federation-classify.js";
16
- export { classifyRecord, reconstructRecordVerifyBody, checkPrincipalEntitlement, recordSignatureVersion, PRINCIPAL_OWNING_TABLES, FEDERATION_TABLE_POLICY, FEDERATION_SYNC_TABLES, } from "./federation-classify.js";
17
- // Module-level nonce store for federation anti-replay.
18
- // Shared across FederationPair + FederationSync — nonces are globally unique
19
- // (generated by signBodyFresh per request with 128-bit random nonces).
20
- //
21
- // Backed by the `Nonce` table (federation-edge-hardening slice 4) so a
22
- // process restart doesn't wipe recently-seen nonces within the ±30s
23
- // freshness window — see federation-nonce-store.ts for the full design.
24
- // The NonceStore interface (has/set/evict) stays synchronous, so this swap
25
- // requires NO change to verifyBodySignatureFresh or its 2 call sites below.
26
- const federationNonceStore = createPersistentNonceStore();
15
+ import { verifyFederationRequestBody } from "./replay-store.js";
16
+ import { classifyRecord, reconstructRecordVerifyBody, checkPrincipalEntitlement, inboundChangesExistingPrincipalStatus, } from "./federation-classify.js";
17
+ export { classifyRecord, reconstructRecordVerifyBody, checkPrincipalEntitlement, inboundChangesExistingPrincipalStatus, recordSignatureVersion, PRINCIPAL_OWNING_TABLES, FEDERATION_TABLE_POLICY, FEDERATION_SYNC_TABLES, } from "./federation-classify.js";
18
+ // Federation anti-replay: FederationPair and FederationSync both verify through
19
+ // verifyFederationRequestBody (resources/replay-store.ts), which claims each
20
+ // body nonce ONCE in the instance-wide `ReplayNonce` table after the signature
21
+ // has verified (flair#2061). Nonces are globally unique (signBodyFresh generates
22
+ // a 128-bit random nonce per request), so the nonce alone is the key.
23
+ /** The response for a body whose signature, freshness or nonce check refused. */
24
+ function federationVerifyRefusal(reason) {
25
+ if (reason === "replay_store_unavailable") {
26
+ return new Response(JSON.stringify({
27
+ error: "replay_store_unavailable",
28
+ detail: "This instance cannot record request nonces right now, so it refuses signed federation requests. The server log names the cause.",
29
+ }), { status: 503, headers: { "content-type": "application/json" } });
30
+ }
31
+ return new Response(JSON.stringify({ error: `invalid signature — ${reason}` }), {
32
+ status: 401, headers: { "content-type": "application/json" },
33
+ });
34
+ }
27
35
  // Re-export for consumers that import from Federation.ts
28
36
  export { canonicalize, signBody, verifyBodySignature, signBodyFresh, verifyBodySignatureFresh, generateNonce };
29
37
  // ─── Per-record signature enforcement mode (federation-edge-hardening slice 3b) ──
@@ -64,8 +72,8 @@ export function payloadIdMismatch(record) {
64
72
  return !record.data || record.data.id !== record.id;
65
73
  }
66
74
  /**
67
- * Field-level Last-Write-Wins merge.
68
- * For each field, the value with the later `updatedAt` wins.
75
+ * Record-level Last-Write-Wins merge.
76
+ * Incoming fields are applied when the remote record's updatedAt is later.
69
77
  * Records with no local counterpart are accepted directly.
70
78
  */
71
79
  export function mergeRecord(local, remote) {
@@ -344,22 +352,16 @@ export class FederationPair extends Resource {
344
352
  status: 401, headers: { "content-type": "application/json" },
345
353
  });
346
354
  }
347
- const verifyResult = verifyBodySignatureFresh(data, publicKey, {
348
- windowMs: 30_000,
349
- nonceStore: federationNonceStore,
350
- });
351
- if (!verifyResult.ok) {
352
- return new Response(JSON.stringify({ error: `invalid signature — ${verifyResult.reason}` }), {
353
- status: 401, headers: { "content-type": "application/json" },
354
- });
355
- }
355
+ const verifyResult = await verifyFederationRequestBody(data, publicKey);
356
+ if (!verifyResult.ok)
357
+ return federationVerifyRefusal(verifyResult.reason);
356
358
  // ── The identity this response hands the peer, decided BEFORE the pairing
357
359
  // token is consumed, BEFORE the peer is read and before ANY peer is written
358
360
  // (flair#1883 round 3). ──────────────────────────────────────────────────
359
361
  //
360
362
  // NOT "before anything": the signature check above has already run and
361
- // RECORDED ITS NONCE (verifyBodySignatureFresh → NonceStore.set(), see
362
- // federation-nonce-store.ts). That is deliberate anti-replay state, not a
363
+ // RECORDED ITS NONCE (verifyFederationRequestBody → the ReplayNonce table,
364
+ // see replay-store.ts). That is deliberate anti-replay state, not a
363
365
  // pairing effect — but it is written, and the refusal is precise about what
364
366
  // it precedes.
365
367
  //
@@ -508,7 +510,7 @@ export class FederationPair extends Resource {
508
510
  */
509
511
  export class FederationSync extends Resource {
510
512
  // Public endpoint by design: spokes have no Harper credentials. Body signature
511
- // (with timestamp + nonce, via verifyBodySignatureFresh) is the auth boundary.
513
+ // (with timestamp + nonce, via verifyFederationRequestBody) is the auth boundary.
512
514
  // Same pattern as FederationPair.
513
515
  allowCreate(_user, _record, _context) {
514
516
  return true;
@@ -535,15 +537,9 @@ export class FederationSync extends Resource {
535
537
  status: 401, headers: { "content-type": "application/json" },
536
538
  });
537
539
  }
538
- const verifyResult = verifyBodySignatureFresh(data, peer.publicKey, {
539
- windowMs: 30_000,
540
- nonceStore: federationNonceStore,
541
- });
542
- if (!verifyResult.ok) {
543
- return new Response(JSON.stringify({ error: `invalid signature — ${verifyResult.reason}` }), {
544
- status: 401, headers: { "content-type": "application/json" },
545
- });
546
- }
540
+ const verifyResult = await verifyFederationRequestBody(data, peer.publicKey);
541
+ if (!verifyResult.ok)
542
+ return federationVerifyRefusal(verifyResult.reason);
547
543
  const startTime = Date.now();
548
544
  let merged = 0;
549
545
  let skipped = 0;
@@ -598,7 +594,7 @@ export class FederationSync extends Resource {
598
594
  // classifyRecord's "merge" verdict above only proves the record is
599
595
  // STRUCTURALLY eligible (known table, self-originated OR the sender
600
596
  // is a hub, not stale, not a no-op) — that hub bypass trusts the
601
- // BATCH-level signature (verifyBodySignatureFresh above, verified
597
+ // BATCH-level signature (verifyFederationRequestBody above, verified
602
598
  // against the SENDER's pinned key). It does NOT prove the record's
603
599
  // *claimed* originator actually produced it — a hub relaying on
604
600
  // behalf of many spokes could otherwise forge a record under any
@@ -655,7 +651,28 @@ export class FederationSync extends Resource {
655
651
  recordSkip(principalSkip);
656
652
  continue;
657
653
  }
654
+ // ── flair#2108: skip an inbound Agent record whose `status` differs
655
+ // from an existing local principal's stored value, whether or not it
656
+ // would win the last-write-wins merge below. The federation path
657
+ // carries no verified administrator authority for that principal (the
658
+ // batch is signed, and a record signature may also be present; neither
659
+ // is an admin claim). The whole record is skipped rather than merged
660
+ // with a pinned status, so the merge stays atomic and the other fields
661
+ // in the record do not land either.
662
+ if (inboundChangesExistingPrincipalStatus(record, local)) {
663
+ recordSkip("agent_status_not_federated");
664
+ continue;
665
+ }
658
666
  const mergedData = mergeRecord(local, record);
667
+ // ── flair#1940 A1'' item 8: the SAME declared-attribute whitelist the
668
+ // writers apply. A dirty pushed row (a legacy direct-insert, or a raw
669
+ // writer that slipped a pointer field past the writers) must not carry
670
+ // a pointer attribute onto the merged Memory row here either. The named
671
+ // federation bookkeeping fields (_originatorInstanceId, _syncedFrom,
672
+ // _syncedAt, meta, kind) are on the whitelist and survive.
673
+ if (record.table === "Memory") {
674
+ stripInboundMemoryRow(mergedData);
675
+ }
659
676
  // ── flair#1542: skills are not federated ──
660
677
  // A skill-tagged Memory is a local, gated artifact (SkillScan + forced
661
678
  // durability on the write path). Merging a pushed skill-tagged row RAW
@@ -692,6 +709,33 @@ export class FederationSync extends Resource {
692
709
  mergedData._originatorInstanceId = decision.originator;
693
710
  mergedData._syncedFrom = instanceId;
694
711
  mergedData._syncedAt = new Date().toISOString();
712
+ // Re-assert the whitelist AFTER stamping the bookkeeping fields, so a
713
+ // dirty inbound row cannot smuggle a pointer field through this merge
714
+ // even if it also carried one of the allowed keys.
715
+ if (record.table === "Memory") {
716
+ stripInboundMemoryRow(mergedData);
717
+ // A1-iv items 1/3: a federated receive is a LOCAL incarnation — the
718
+ // token is OURS, never the peer's. Drop a peer-supplied server-stamped
719
+ // field, then PRESERVE the local row's token on a merge (a new row
720
+ // gets a locally-generated one).
721
+ //
722
+ // Round 22: `provenance` is NOT a local incarnation stamp — it is the
723
+ // ORIGINATOR's signed stamp and must survive the merge. Capture the
724
+ // value mergeRecord SELECTED (the inbound data for a new row; the
725
+ // merge's LWW choice for an update — NOT always local.provenance)
726
+ // before stripping, then restore it in the same per-record apply.
727
+ // Pinned by test/unit-isolated/federation-merge-provenance.test.ts —
728
+ // RED if this capture/restore is removed.
729
+ const mergedProvenance = mergedData.provenance;
730
+ stripServerStampedFields(mergedData);
731
+ if (typeof mergedProvenance === "string") {
732
+ mergedData.provenance = mergedProvenance;
733
+ }
734
+ mergedData.instanceToken =
735
+ local && typeof local.instanceToken === "string" && local.instanceToken.length > 0
736
+ ? local.instanceToken
737
+ : randomUUID();
738
+ }
695
739
  await table.put(mergedData);
696
740
  // embedding-space-guard slice 1: a federation-merged Memory can carry a
697
741
  // FOREIGN embeddingModel (LWW remote-win) — trip the guard's latch so
@@ -814,21 +858,7 @@ if (typeof setTimeout !== "undefined") {
814
858
  // Swallow — in test/resource-env the Harper databases may not be bound.
815
859
  // In production, initFederationCleanup handles its own error paths.
816
860
  });
817
- // Nonce store hydration (slice 4) — load nonces persisted by a previous
818
- // process (if any) before this instance starts guarding live traffic.
819
- // Runs on the same deferred tick as the cleanup sweep above; there is a
820
- // narrow window between process start and hydration completing during
821
- // which a pre-restart nonce isn't yet visible — an accepted tradeoff
822
- // consistent with this file's existing deferred-init pattern (see the
823
- // cleanup sweep comment above).
824
- federationNonceStore.hydrate().catch((err) => {
825
- // Swallow — in test/resource-env the Harper databases may not be bound.
826
- });
827
- // Nonce table eviction sweep — runs on every instance (both hub and
828
- // spoke can be the receiving/verifying side of a signed federation
829
- // request), unlike PairingToken cleanup which is hub-only.
830
- initNonceStoreCleanup().catch((err) => {
831
- // Swallow — in test/resource-env the Harper databases may not be bound.
832
- });
861
+ // Nonce eviction is the ReplayNonce table's own expiration (one scan per
862
+ // instance, on Harper's last worker) — no sweep is started here.
833
863
  }, 0);
834
864
  }