@tpsdev-ai/flair 0.59.0 → 0.62.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 (259) hide show
  1. package/README.md +4 -4
  2. package/SECURITY.md +36 -0
  3. package/config.yaml +12 -1
  4. package/dist/bridges/builtins/mem0.js +63 -28
  5. package/dist/bridges/runtime/import-runner.js +12 -0
  6. package/dist/bridges/runtime/load-plugin.js +42 -13
  7. package/dist/bridges/runtime/yaml-loader.js +4 -2
  8. package/dist/build-info.json +3 -3
  9. package/dist/cli.js +991 -248
  10. package/dist/commands/agent.js +224 -43
  11. package/dist/commands/attention.js +4 -3
  12. package/dist/commands/backup.js +202 -34
  13. package/dist/commands/bridge.js +172 -119
  14. package/dist/commands/deploy.js +37 -4
  15. package/dist/commands/doctor.js +592 -146
  16. package/dist/commands/export.js +54 -19
  17. package/dist/commands/federation.js +141 -21
  18. package/dist/commands/hook.js +155 -2
  19. package/dist/commands/idp.js +14 -1
  20. package/dist/commands/import.js +7 -3
  21. package/dist/commands/init.js +462 -193
  22. package/dist/commands/integrity.js +388 -0
  23. package/dist/commands/keys.js +247 -58
  24. package/dist/commands/mcp.js +97 -35
  25. package/dist/commands/memory.js +171 -21
  26. package/dist/commands/migrate-harness-memory.js +3 -2
  27. package/dist/commands/orgevent.js +46 -37
  28. package/dist/commands/presence.js +4 -3
  29. package/dist/commands/principal.js +426 -48
  30. package/dist/commands/quality.js +10 -1
  31. package/dist/commands/reembed.js +141 -17
  32. package/dist/commands/relationship.js +2 -1
  33. package/dist/commands/rem.js +56 -18
  34. package/dist/commands/restore.js +101 -61
  35. package/dist/commands/search.js +6 -4
  36. package/dist/commands/service.js +151 -38
  37. package/dist/commands/soul.js +4 -3
  38. package/dist/commands/status.js +43 -5
  39. package/dist/commands/test.js +3 -2
  40. package/dist/commands/upgrade.js +24 -1
  41. package/dist/commands/workspace.js +6 -3
  42. package/dist/component-env.js +31 -41
  43. package/dist/deploy.js +77 -1
  44. package/dist/doctor-client.js +529 -92
  45. package/dist/engine-version-contract.js +1 -0
  46. package/dist/engine-version.js +23 -3
  47. package/dist/federation-verify.js +4 -3
  48. package/dist/hook-install.js +670 -6
  49. package/dist/keystore.js +72 -0
  50. package/dist/lib/action-recall-runtime.js +328 -0
  51. package/dist/lib/agent-home-fields.js +132 -0
  52. package/dist/lib/agent-home.js +427 -0
  53. package/dist/lib/agent-id-rule.js +46 -0
  54. package/dist/lib/agent-roster.js +33 -0
  55. package/dist/lib/auth-resolve.js +49 -16
  56. package/dist/lib/capture-runtime.js +124 -0
  57. package/dist/lib/credential-status-rule.js +78 -0
  58. package/dist/lib/daemon-liveness.js +41 -10
  59. package/dist/lib/doctor-run.js +75 -37
  60. package/dist/lib/durability-copy.js +10 -0
  61. package/dist/lib/embedding-space-doctor.js +75 -0
  62. package/dist/lib/flair-agent-role.js +4 -0
  63. package/dist/lib/hub-url.js +12 -0
  64. package/dist/lib/init-admin-pass.js +88 -12
  65. package/dist/lib/init-listener-environ.js +1 -2
  66. package/dist/lib/init-occupied-listener.js +15 -111
  67. package/dist/lib/init-spawn-attribution.js +73 -0
  68. package/dist/lib/init-tcp-probe.js +33 -0
  69. package/dist/lib/instance-identity-row.js +20 -0
  70. package/dist/lib/launchd-management.js +24 -0
  71. package/dist/lib/listener-table.js +268 -0
  72. package/dist/lib/local-delete-instance.js +27 -0
  73. package/dist/lib/local-instance-pair.js +256 -0
  74. package/dist/lib/mcp-enable.js +1168 -326
  75. package/dist/lib/mcp-oauth-env-core.js +133 -0
  76. package/dist/lib/mcp-oauth-env.js +125 -0
  77. package/dist/lib/mcp-spec.js +17 -1
  78. package/dist/lib/memory-attributes.js +2 -0
  79. package/dist/lib/memory-id-policy.js +46 -0
  80. package/dist/lib/memory-integrity.js +309 -0
  81. package/dist/lib/memory-purge-response.js +20 -0
  82. package/dist/lib/ops-table-count.js +25 -0
  83. package/dist/lib/owned-pins.js +67 -10
  84. package/dist/lib/peer-status.js +27 -0
  85. package/dist/lib/principal-status.js +29 -0
  86. package/dist/lib/process-start-time.js +76 -12
  87. package/dist/lib/proto-safe-record.js +42 -0
  88. package/dist/lib/record-id-path.js +25 -0
  89. package/dist/lib/reembed-server-support.js +66 -0
  90. package/dist/lib/restore-verify.js +104 -0
  91. package/dist/lib/scheduler-platform.js +10 -6
  92. package/dist/lib/secret-keys.js +25 -0
  93. package/dist/lib/secrets-push.js +50 -22
  94. package/dist/lib/skill-seed-pending.js +24 -0
  95. package/dist/lib/skill-seed.js +365 -0
  96. package/dist/lib/stop-start-recovery.js +54 -0
  97. package/dist/lib/using-flair-skill.js +87 -0
  98. package/dist/mcp-client-assertion.js +112 -4
  99. package/dist/memory-id-checkpoint.js +326 -0
  100. package/dist/memory-node-compare.js +207 -0
  101. package/dist/packages/flair-mcp/src/secret-redaction.js +113 -0
  102. package/dist/rem/restore.js +6 -5
  103. package/dist/rem/runner.js +103 -44
  104. package/dist/resources/Agent.js +321 -1
  105. package/dist/resources/AgentKeyRegister.js +214 -0
  106. package/dist/resources/AgentKeyRegistration.js +28 -0
  107. package/dist/resources/AgentSeed.js +10 -4
  108. package/dist/resources/AutoPromoteCandidates.js +7 -1
  109. package/dist/resources/Credential.js +177 -2
  110. package/dist/resources/Federation.js +751 -143
  111. package/dist/resources/FederationRetry.js +140 -0
  112. package/dist/resources/InstructionVersion.js +220 -0
  113. package/dist/resources/Integration.js +300 -25
  114. package/dist/resources/Memory.js +2113 -604
  115. package/dist/resources/MemoryArchive.js +75 -42
  116. package/dist/resources/MemoryBootstrap.js +110 -39
  117. package/dist/resources/MemoryCandidate.js +21 -1
  118. package/dist/resources/MemoryDedupStats.js +30 -1
  119. package/dist/resources/MemoryFeed.js +327 -45
  120. package/dist/resources/MemoryGrant.js +21 -1
  121. package/dist/resources/MemoryMaintenance.js +129 -25
  122. package/dist/resources/MemoryPurge.js +232 -0
  123. package/dist/resources/MemoryReflect.js +7 -2
  124. package/dist/resources/MemoryReindex.js +24 -8
  125. package/dist/resources/Message.js +5 -3
  126. package/dist/resources/OAuth.js +186 -9
  127. package/dist/resources/OrgEvent.js +126 -3
  128. package/dist/resources/Presence.js +144 -49
  129. package/dist/resources/PromoteMemoryCandidate.js +18 -7
  130. package/dist/resources/Relationship.js +23 -0
  131. package/dist/resources/SemanticSearch.js +63 -32
  132. package/dist/resources/Soul.js +189 -76
  133. package/dist/resources/TeamDirectory.js +35 -0
  134. package/dist/resources/WorkspaceState.js +21 -1
  135. package/dist/resources/XAA.js +181 -63
  136. package/dist/resources/agent-auth.js +40 -15
  137. package/dist/resources/auth-middleware.js +400 -161
  138. package/dist/resources/ed25519-auth.js +1 -1
  139. package/dist/resources/embedding-space-guard.js +366 -72
  140. package/dist/resources/engine-version-stamp-boot.js +53 -0
  141. package/dist/resources/federation-classify.js +72 -0
  142. package/dist/resources/federation-crypto.js +5 -5
  143. package/dist/resources/federation-retry-lib.js +135 -0
  144. package/dist/resources/grant-visibility.js +135 -0
  145. package/dist/resources/harper-routing.js +83 -0
  146. package/dist/resources/health.js +120 -18
  147. package/dist/resources/host-source-citation.js +49 -0
  148. package/dist/resources/host-source-visibility.js +7 -6
  149. package/dist/resources/instance-create-lock.js +17 -0
  150. package/dist/resources/instance-identity.js +23 -0
  151. package/dist/resources/instruction-version-record.js +343 -0
  152. package/dist/resources/key-lock.js +3 -3
  153. package/dist/resources/mcp-handler.js +36 -8
  154. package/dist/resources/mcp-oauth-env-guard.js +36 -0
  155. package/dist/resources/mcp-oauth.js +23 -6
  156. package/dist/resources/mcp-tool-arguments.js +4 -6
  157. package/dist/resources/mcp-tools.js +128 -19
  158. package/dist/resources/memory-bootstrap-lib.js +5 -15
  159. package/dist/resources/memory-declared-attributes.js +3 -1
  160. package/dist/resources/memory-deletion-history.js +55 -0
  161. package/dist/resources/memory-durability.js +54 -0
  162. package/dist/resources/memory-embed-hash.js +88 -0
  163. package/dist/resources/memory-embedding-value.js +8 -0
  164. package/dist/resources/memory-host-source.js +27 -5
  165. package/dist/resources/memory-id-guard.js +75 -0
  166. package/dist/resources/memory-redaction.js +236 -0
  167. package/dist/resources/memory-supplied-vector.js +72 -0
  168. package/dist/resources/memory-write-lock.js +310 -0
  169. package/dist/resources/memory-write-response.js +139 -0
  170. package/dist/resources/migrations/data-dir.js +74 -16
  171. package/dist/resources/migrations/dir-safety.js +1 -1
  172. package/dist/resources/migrations/embedding-stamp.js +193 -79
  173. package/dist/resources/migrations/export.js +1 -0
  174. package/dist/resources/migrations/graph-heal.js +18 -25
  175. package/dist/resources/migrations/lock.js +7 -1
  176. package/dist/resources/migrations/snapshot.js +1 -0
  177. package/dist/resources/migrations/source-fields.js +5 -5
  178. package/dist/resources/migrations/synthetic-test-migration.js +14 -11
  179. package/dist/resources/migrations/visibility-backfill.js +29 -29
  180. package/dist/resources/object-body-guard.js +33 -0
  181. package/dist/resources/org-event-created-at.js +133 -0
  182. package/dist/resources/originator-instance.js +39 -7
  183. package/dist/resources/owner-delete-recheck.js +78 -0
  184. package/dist/resources/owner-field-guard.js +39 -5
  185. package/dist/resources/owner-scoped-write-policy.js +212 -0
  186. package/dist/resources/owner-scoped-writes.js +58 -0
  187. package/dist/resources/presence-receipt.js +236 -0
  188. package/dist/resources/promotion-stamp.js +47 -12
  189. package/dist/resources/record-owner-guard.js +74 -26
  190. package/dist/resources/record-types.js +1 -1
  191. package/dist/resources/relay-ops.js +78 -6
  192. package/dist/resources/replay-store.js +96 -19
  193. package/dist/resources/request-target.js +136 -0
  194. package/dist/resources/request-transaction.js +35 -0
  195. package/dist/resources/search-readiness.js +4 -1
  196. package/dist/resources/seed-ids.js +9 -0
  197. package/dist/resources/seed-reservation.js +117 -0
  198. package/dist/resources/semantic-retrieval-core.js +20 -9
  199. package/dist/resources/skill-manifest.js +20 -11
  200. package/dist/resources/skill-subject.js +53 -0
  201. package/dist/resources/skill-version-write.js +432 -0
  202. package/dist/resources/skill-write-fault.js +17 -0
  203. package/dist/resources/skill-write-policy.js +67 -0
  204. package/dist/resources/skill-write.js +67 -7
  205. package/dist/resources/table-helpers.js +10 -7
  206. package/dist/resources/team-directory.js +248 -0
  207. package/dist/resources/tool-descriptors/index.js +69 -8
  208. package/dist/resources/txn-pause-point.js +118 -0
  209. package/dist/resources/usage-recording.js +171 -63
  210. package/dist/resources/write-back.js +218 -0
  211. package/dist/src/engine-version-contract.js +1 -0
  212. package/dist/src/keystore.js +72 -0
  213. package/dist/src/lib/agent-home-fields.js +132 -0
  214. package/dist/src/lib/agent-id-rule.js +46 -0
  215. package/dist/src/lib/agent-key-register.js +80 -0
  216. package/dist/src/lib/atomic-write.js +256 -0
  217. package/dist/src/lib/credential-status-rule.js +78 -0
  218. package/dist/src/lib/expired-by-agent.js +37 -0
  219. package/dist/src/lib/flair-agent-role.js +4 -0
  220. package/dist/src/lib/idp-mapping-rule.js +81 -0
  221. package/dist/src/lib/instance-identity-row.js +20 -0
  222. package/dist/src/lib/mcp-oauth-env-core.js +133 -0
  223. package/dist/src/lib/memory-attributes.js +2 -0
  224. package/dist/src/lib/memory-id-policy.js +46 -0
  225. package/dist/src/lib/principal-status.js +29 -0
  226. package/dist/src/lib/proto-safe-record.js +42 -0
  227. package/dist/src/lib/reembed-server-support.js +66 -0
  228. package/dist/src/lib/scheduler-platform.js +10 -6
  229. package/dist/src/lib/systemd-agent-id.js +38 -0
  230. package/docs/access-control.md +146 -45
  231. package/docs/api-reference.md +46 -11
  232. package/docs/auth.md +6 -12
  233. package/docs/bridges.md +11 -0
  234. package/docs/claude-code.md +34 -4
  235. package/docs/deploying-on-fabric.md +70 -0
  236. package/docs/deployment.md +2 -0
  237. package/docs/embedding-in-a-harper-app.md +1 -1
  238. package/docs/federation.md +33 -2
  239. package/docs/hosted-on-fabric.md +43 -36
  240. package/docs/integrations.md +5 -5
  241. package/docs/mcp-clients.md +5 -2
  242. package/docs/n8n.md +17 -11
  243. package/docs/notes/mcp-agent-auth-consumer.md +12 -4
  244. package/docs/notes/mcp-oauth-model2.md +34 -5
  245. package/docs/quickstart-fabric.md +2 -2
  246. package/docs/quickstart.md +9 -1
  247. package/docs/releasing.md +12 -6
  248. package/docs/rem.md +8 -0
  249. package/docs/secrets-and-keys.md +19 -2
  250. package/docs/standalone-local.md +1 -0
  251. package/docs/supply-chain-policy.md +13 -3
  252. package/docs/upgrade.md +32 -6
  253. package/package.json +10 -5
  254. package/schemas/agent-key-registration.graphql +22 -0
  255. package/schemas/agent.graphql +3 -0
  256. package/schemas/federation.graphql +38 -1
  257. package/schemas/memory.graphql +84 -5
  258. package/schemas/oauth.graphql +19 -0
  259. package/schemas/schema.graphql +2 -0
@@ -0,0 +1,9 @@
1
+ /**
2
+ * seed-ids.ts — the `using-flair` seed's fixed skill row id (flair#2141 S2).
3
+ *
4
+ * Pure, with no runtime import, so a unit test can read it without a Harper
5
+ * instance. resources/seed-reservation.ts reserves it; src/lib/skill-seed.ts
6
+ * writes it (its `SEED_SKILL_ID` must equal this value, and
7
+ * test/unit/skill-seed.test.ts pins that).
8
+ */
9
+ export const SEED_SKILL_ROW_ID = "skill:using-flair";
@@ -0,0 +1,117 @@
1
+ /**
2
+ * seed-reservation.ts — reserve the `using-flair` seed's fixed ids for the
3
+ * operator source (flair#2141 S2).
4
+ *
5
+ * Normal `flair init` seeds one skill row at a FIXED Memory id and one org
6
+ * assignment at a FIXED OrgSkillAssignment id that points at it. Re-initializing
7
+ * an already-installed default local instance with `flair init --skip-start`
8
+ * defers the seed until a later `flair start` that has the admin credential.
9
+ * Bootstrap may list the
10
+ * assignment for an active agent when it wins same-name conflict resolution,
11
+ * the agent has not opted out, and the entry fits its budget. The row behind
12
+ * that id must be the operator's. The rule:
13
+ *
14
+ * A create, PUT, PATCH or DELETE of the reserved Memory id needs the operator
15
+ * source: an authenticated Basic administrator, or a deliberate internal call
16
+ * (`soulWriteSource`, the same predicate that gates every OrgSkillAssignment
17
+ * write). Every other caller is refused, an agent key whose id equals the
18
+ * administrator's username and an admin agent key included.
19
+ *
20
+ * The decision reads the auth verdict and the request's credential class. It
21
+ * never compares the stored owner with the caller's id: an agent can hold the
22
+ * id the operator's rows are attributed to.
23
+ *
24
+ * The assignment id needs no entry here: every OrgSkillAssignment write
25
+ * already requires the operator source (resources/OrgSkillAssignment.ts), and
26
+ * test/unit/org-skill-assignment-writer-coverage.test.ts pins its writers.
27
+ *
28
+ * Where it applies: Memory post/put/patch/delete, the supersede target, and
29
+ * FeedMemories check caller-chosen ids before writing; federation skips a
30
+ * reserved id (`seed_id_not_federated`). Server bookkeeping is also allowed:
31
+ * lastReflected and embedding backfill update their selected fields. Usage
32
+ * recording intends to change only usageCount, but re-PUTs the stored row and
33
+ * can receive an agent-supplied id. The coverage test classifies raw Memory
34
+ * write sites detected by its supported source patterns; it does not prove
35
+ * that every possible write path is classified.
36
+ */
37
+ import { resolveAgentAuth } from "./agent-auth.js";
38
+ import { FORBIDDEN, UNAUTH } from "./record-type-kit.js";
39
+ import { soulWriteSource } from "./soul-write-policy.js";
40
+ import { SEED_SKILL_ROW_ID } from "./seed-ids.js";
41
+ export { SEED_SKILL_ROW_ID };
42
+ /** Table → ids reserved for the operator source, subject to documented bookkeeping writes. */
43
+ export const RESERVED_SEED_IDS = Object.freeze({
44
+ Memory: Object.freeze([SEED_SKILL_ROW_ID]),
45
+ });
46
+ /** Whether `id` is a reserved seed id on `table`. */
47
+ export function isReservedSeedId(table, id) {
48
+ if (typeof id !== "string" && typeof id !== "number")
49
+ return false;
50
+ return (RESERVED_SEED_IDS[table] ?? []).includes(String(id));
51
+ }
52
+ /** The ids a resource write names: the URL-bound id and any body id (one record or an array). */
53
+ export function writeTargetIds(resource, content) {
54
+ const ids = [];
55
+ try {
56
+ const getId = resource?.getId;
57
+ if (typeof getId === "function")
58
+ ids.push(getId.call(resource));
59
+ }
60
+ catch {
61
+ /* no bound id */
62
+ }
63
+ const records = Array.isArray(content) ? content : [content];
64
+ for (const record of records) {
65
+ if (record && typeof record === "object" && "id" in record)
66
+ ids.push(record.id);
67
+ }
68
+ return ids;
69
+ }
70
+ /**
71
+ * The decision: null when no id is reserved or the caller has operator
72
+ * authority; otherwise 401 (anonymous) or 403.
73
+ */
74
+ export function reservedSeedWriteDenial(table, ids, context, auth) {
75
+ const reserved = ids.find((id) => isReservedSeedId(table, id));
76
+ if (reserved === undefined)
77
+ return null;
78
+ if (auth.kind === "anonymous")
79
+ return UNAUTH();
80
+ if (soulWriteSource(context, auth))
81
+ return null;
82
+ return FORBIDDEN(`seed_id_reserved: ${table} ${JSON.stringify(String(reserved))} requires the operator source ` +
83
+ "(an authenticated Basic administrator or deliberate internal call)");
84
+ }
85
+ /** `reservedSeedWriteDenial`, resolving the auth verdict only when an id is reserved. */
86
+ export async function refuseReservedSeedWrite(table, ids, context) {
87
+ if (!ids.some((id) => isReservedSeedId(table, id)))
88
+ return null;
89
+ return reservedSeedWriteDenial(table, ids, context, await resolveAgentAuth(context));
90
+ }
91
+ /**
92
+ * The LINEAGE form of the reservation (flair#2139 S2). A skill update SUPERSEDES
93
+ * into a fresh physical Memory row that carries the stable logical
94
+ * `skillSubjectId` forward, so reserving the seed's physical id alone would let
95
+ * another caller enter the seed's lineage through a successor row whose own id
96
+ * is not the reserved one. This denies a write whose skill SUBJECT is reserved
97
+ * unless the caller has the operator source, exactly as the physical-id check
98
+ * does. `subjectIds` are the resolved logical subject ids the write targets.
99
+ */
100
+ export function reservedSeedSubjectDenial(table, subjectIds, context, auth) {
101
+ const reserved = subjectIds.find((id) => isReservedSeedId(table, id));
102
+ if (reserved === undefined)
103
+ return null;
104
+ if (auth.kind === "anonymous")
105
+ return UNAUTH();
106
+ if (soulWriteSource(context, auth))
107
+ return null;
108
+ return FORBIDDEN(`seed_subject_reserved: the ${table} ${JSON.stringify(String(reserved))} lineage requires the operator source ` +
109
+ "(an authenticated Basic administrator or deliberate internal call)");
110
+ }
111
+ export function reservedSeedFeedWriteDenial(table, ids) {
112
+ const reserved = ids.find((id) => id != null && isReservedSeedId(table, String(id)));
113
+ if (reserved === undefined)
114
+ return null;
115
+ return FORBIDDEN(`seed_id_reserved: ${table} ${JSON.stringify(String(reserved))} is written by the flair init seed; ` +
116
+ "the feed ingest does not write it");
117
+ }
@@ -63,6 +63,7 @@ import { buildBM25, fuseRrfNormalized, SEM_LIMIT } from "./bm25.js";
63
63
  import { isAllowedBm25Candidate } from "./bm25-filter.js";
64
64
  import { indexedBm25Ids } from "./bm25-index-service.js";
65
65
  import { byRecencyThenId } from "./sort-comparators.js";
66
+ import { isTrustedVectorStamp, trustedVectorCondition } from "./embedding-space-guard.js";
66
67
  /**
67
68
  * The only way a row enters the ranked pool. `_rank` is a required
68
69
  * argument — not an object property that `...any` can swallow — so a
@@ -131,8 +132,14 @@ export async function retrieveCandidates(params) {
131
132
  select: hnswSelect,
132
133
  limit,
133
134
  };
134
- if (conditions.length > 0)
135
- semQuery.conditions = conditions;
135
+ // flair#2573 slice 1: defence in depth on the vector leg. The guard's
136
+ // verdict is the load-bearing exclusion — a non-uniform verdict makes the
137
+ // caller drop the query embedding. This condition is added to the HNSW
138
+ // query alone — never to `conditions`, which the lexical leg shares (a row
139
+ // with an untrusted stamp remains eligible for lexical matching). A query
140
+ // condition cannot keep a vector out of Harper's HNSW traversal; it only
141
+ // excludes the row from the returned set.
142
+ semQuery.conditions = [...conditions, trustedVectorCondition()];
136
143
  const semResults = withDetachedTxn(ctx, () => databases.flair.Memory.search(semQuery));
137
144
  for await (const record of semResults) {
138
145
  if (record.expiresAt && Date.parse(record.expiresAt) < Date.now())
@@ -269,7 +276,8 @@ export async function retrieveCandidates(params) {
269
276
  if (key in full)
270
277
  projected[key] = full[key];
271
278
  allowedById.set(id, projected);
272
- if (qEmb) {
279
+ // A trusted stamp permits cosine scoring; an absent vector scores zero.
280
+ if (qEmb && isTrustedVectorStamp(full.embeddingModel)) {
273
281
  const storedEmbedding = Array.isArray(full.embedding) ? full.embedding : [];
274
282
  semSimById.set(id, cosineSimilarity(qEmb, storedEmbedding));
275
283
  }
@@ -328,9 +336,12 @@ export async function retrieveCandidates(params) {
328
336
  let semSim = semSimById.get(id);
329
337
  if (semSim === undefined && qEmb) {
330
338
  const full = await withDetachedTxn(ctx, () => databases.flair.Memory.get(id));
331
- const storedEmbedding = Array.isArray(full?.embedding) ? full.embedding : [];
332
- semSim = cosineSimilarity(qEmb, storedEmbedding);
333
- semSimById.set(id, semSim);
339
+ // A trusted stamp permits cosine scoring; an absent vector scores zero.
340
+ if (isTrustedVectorStamp(full?.embeddingModel)) {
341
+ const storedEmbedding = Array.isArray(full?.embedding) ? full.embedding : [];
342
+ semSim = cosineSimilarity(qEmb, storedEmbedding);
343
+ semSimById.set(id, semSim);
344
+ }
334
345
  }
335
346
  let keywordHit = false;
336
347
  if (q && String(record.content || "").toLowerCase().includes(String(q).toLowerCase())) {
@@ -463,10 +474,10 @@ export async function retrieveCandidates(params) {
463
474
  sort: { attribute: "embedding", target: qEmb, distance: "cosine" },
464
475
  select: hnswSelect,
465
476
  limit,
477
+ // flair#2573 slice 1: defence in depth on the vector leg (see the hybrid
478
+ // sem leg above); the guard's verdict is the load-bearing exclusion.
479
+ conditions: [...conditions, trustedVectorCondition()],
466
480
  };
467
- if (conditions.length > 0) {
468
- query.conditions = conditions;
469
- }
470
481
  const memoryResults = withDetachedTxn(ctx, () => databases.flair.Memory.search(query));
471
482
  for await (const record of memoryResults) {
472
483
  if (record.expiresAt && Date.parse(record.expiresAt) < Date.now())
@@ -26,7 +26,7 @@ import { formatBase, normalizePriority, parseSkillMetadata, resolveActiveSkills,
26
26
  import { isSkillWrite } from "./skill-write.js";
27
27
  /** The Memory fields the bootstrap skill-row query selects. */
28
28
  export const SKILL_ROW_SELECT = [
29
- "id", "agentId", "visibility", "tags", "metadata", "archived", "validTo", "expiresAt",
29
+ "id", "agentId", "visibility", "tags", "metadata", "archived", "validTo", "expiresAt", "skillSubjectId",
30
30
  ];
31
31
  const NO_ORG = { assignments: [], rows: [], instanceId: null };
32
32
  /**
@@ -62,16 +62,16 @@ export function resolvableSkillRows(rows, isReadable, now = Date.now()) {
62
62
  && !isPast(row.validTo, now));
63
63
  }
64
64
  /**
65
- * flair#2141 S2 hook: the skill rows written by the reserved system writer
66
- * (the operator seed), the only candidates besides the agent's own rows. None
67
- * exist before S2, so this returns no rows.
65
+ * Name-only assignments resolve against the agent's own rows. The operator's
66
+ * shipped skill is assigned by explicit `skillRef`; it is not an implicit
67
+ * fallback for an agent's name-only assignment.
68
68
  */
69
69
  export function seedSkillRows(_rows) {
70
70
  return [];
71
71
  }
72
72
  /**
73
73
  * Resolve a name-only assignment to one skill row: the target agent's own row
74
- * with that name (then, from flair#2141 S2, a seed row; see seedSkillRows).
74
+ * with that name. Explicit org `skillRef` assignments use resolveOrgRef.
75
75
  * More than one row at a step is ambiguous; no row is unresolved.
76
76
  */
77
77
  export function resolveSkillRef(name, rows, agentId) {
@@ -91,12 +91,21 @@ export function resolveSkillRef(name, rows, agentId) {
91
91
  ?? decide(seedSkillRows(named), "seed")
92
92
  ?? { kind: "unresolved", reason: "the agent has no live skill row with this name" };
93
93
  }
94
- /** An org assignment's `skillRef`, resolved against the rows it may name. */
95
- function resolveOrgRef(skillRef, rows) {
96
- if (typeof skillRef === "string" && rows.some((row) => row.id === skillRef)) {
97
- return { kind: "resolved", skillId: skillRef };
94
+ function resolveOrgRef(skillRef, rows, refSubjects) {
95
+ if (typeof skillRef !== "string" || skillRef.length === 0) {
96
+ return { kind: "unresolved", reason: "the skillRef is not a live skill row this agent can read" };
98
97
  }
99
- return { kind: "unresolved", reason: "the skillRef is not a live skill row this agent can read" };
98
+ const byId = rows.filter((row) => row.id === skillRef);
99
+ const matches = byId.length > 0 ? byId : rows.filter((row) => row.skillSubjectId === (refSubjects?.get(skillRef) ?? skillRef));
100
+ if (matches.length === 1)
101
+ return { kind: "resolved", skillId: matches[0].id };
102
+ if (matches.length === 0)
103
+ return { kind: "unresolved", reason: "the skillRef is not a live skill row this agent can read" };
104
+ return {
105
+ kind: "ambiguous",
106
+ reason: `${matches.length} live skill rows resolve this skillRef`,
107
+ candidates: matches.map((row) => row.id).sort(),
108
+ };
100
109
  }
101
110
  /** The "## Active Skills" prose line for a manifest entry. */
102
111
  export function skillLine(entry) {
@@ -164,7 +173,7 @@ export function resolveSkillManifest(assignments, rows, agentId, org = NO_ORG) {
164
173
  continue;
165
174
  }
166
175
  const ref = scope === "org"
167
- ? resolveOrgRef(candidate.skillRef, org.rows)
176
+ ? resolveOrgRef(candidate.skillRef, org.rows, org.refSubjects)
168
177
  : resolveSkillRef(outcome.name, rows, agentId);
169
178
  if (ref.kind === "resolved") {
170
179
  skills.push({ name: outcome.name, skillId: ref.skillId, scope, priority: outcome.priority, source });
@@ -0,0 +1,53 @@
1
+ /**
2
+ * skill-subject.ts — the STABLE logical subject id of a skill lineage
3
+ * (flair#2139 S2). Pure: no Harper imports, so it is unit-testable and callable
4
+ * from every skill writer.
5
+ *
6
+ * Memory.skillSubjectId survives supersession and in-place writes.
7
+ *
8
+ * Derivation, in the one order the spec pins:
9
+ * 1. An EXPLICIT successor (`supersedes` set) inherits its predecessor's
10
+ * established subject, or — on first enrollment — adopts the predecessor's
11
+ * physical ID.
12
+ * 2. Otherwise the row's own stored `skillSubjectId` (carry-over) is used.
13
+ * 3. Otherwise first enrollment of an existing skill row uses that row's
14
+ * physical ID.
15
+ * 4. Otherwise a brand-new skill derives it from its FIRST physical Memory ID
16
+ * (the new row's own id).
17
+ *
18
+ * It is NEVER derived from a name, a content hash, client metadata, or a
19
+ * successor's fresh id when an established subject exists. A client- or
20
+ * body-supplied `skillSubjectId` is never read (see
21
+ * memory-declared-attributes.ts's SERVER_STAMPED_MEMORY_FIELDS, which strips it
22
+ * from every write body).
23
+ */
24
+ function nonEmptyString(value) {
25
+ return typeof value === "string" && value.length > 0 ? value : null;
26
+ }
27
+ /**
28
+ * The stable logical subject id for a skill write. Deterministic and total: as
29
+ * long as `newPhysicalId` is non-empty it returns a non-empty id.
30
+ */
31
+ export function deriveSkillSubjectId(input) {
32
+ const { newPhysicalId, storedHead, predecessor } = input;
33
+ const predecessorSubject = nonEmptyString(predecessor?.skillSubjectId);
34
+ if (predecessorSubject)
35
+ return predecessorSubject;
36
+ const predecessorId = nonEmptyString(predecessor?.id);
37
+ if (predecessorId)
38
+ return predecessorId;
39
+ const carried = nonEmptyString(storedHead?.skillSubjectId);
40
+ if (carried)
41
+ return carried;
42
+ const storedId = nonEmptyString(storedHead?.id);
43
+ if (storedId)
44
+ return storedId;
45
+ return newPhysicalId;
46
+ }
47
+ /** The subject id to stamp on a successor: the predecessor's established
48
+ * subject, else the predecessor's id, else its own (a fresh standalone row). */
49
+ export function carrySkillSubjectId(predecessor, fallbackId) {
50
+ return nonEmptyString(predecessor?.skillSubjectId)
51
+ ?? nonEmptyString(predecessor?.id)
52
+ ?? fallbackId;
53
+ }