@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,75 @@
1
+ /**
2
+ * memory-id-guard.ts — the resource-layer refusal for a Memory id that ends in
3
+ * the `.content` property suffix (flair#2199).
4
+ *
5
+ */
6
+ import { MEMORY_CONTENT_SELECTOR_SUFFIX, endsWithContentSelectorSuffix, decodeMemoryIdSegment } from "../src/lib/memory-id-policy.js";
7
+ import { DECLARED_MEMORY_ATTRIBUTES } from "./memory-declared-attributes.js";
8
+ const DECLARED = new Set(DECLARED_MEMORY_ATTRIBUTES);
9
+ /** The named error for a Memory write naming an id that ends in `.content`. */
10
+ export const CONTENT_SUFFIX_ID_ERROR = "memory_id_content_suffix";
11
+ /** The named 400 for one offending id. */
12
+ export function contentSuffixIdDenial(id) {
13
+ return new Response(JSON.stringify({
14
+ error: CONTENT_SUFFIX_ID_ERROR,
15
+ message: `Memory ids must not end in "${MEMORY_CONTENT_SELECTOR_SUFFIX}".`,
16
+ }), { status: 400, headers: { "content-type": "application/json" } });
17
+ }
18
+ /**
19
+ * Refuse a write when any of its named ids (the URL-bound id and/or a body id)
20
+ * ends in the `.content` suffix.
21
+ */
22
+ export function refuseContentSuffixId(ids, target) {
23
+ const rawId = typeof target?.pathname === "string"
24
+ ? decodeMemoryIdSegment(target.pathname.split("/").at(-1) ?? "")
25
+ : undefined;
26
+ const bad = [...ids, rawId].find((id) => endsWithContentSelectorSuffix(id));
27
+ return bad === undefined ? null : contentSuffixIdDenial(String(bad));
28
+ }
29
+ /**
30
+ * The record id a Memory reference resolves to, the way Harper resolves a
31
+ * by-id path: decode it, then drop a trailing `.<declared attribute>` selector
32
+ * (Harper splits at the FIRST dot and treats the remainder as a property ONLY
33
+ * when it names a declared attribute). A reference that carries no such
34
+ * selector resolves to its decoded form; a non-string resolves to undefined.
35
+ * Memory.ts resolves a `supersedes` reference with this once, before anything
36
+ * reads it, and uses the result for the reserved-id check, the authorization
37
+ * read (non-admin agents only), the stored reference and the close
38
+ * (flair#2307).
39
+ */
40
+ export function resolveMemoryReferenceId(ref) {
41
+ if (typeof ref !== "string")
42
+ return undefined;
43
+ const decoded = decodeMemoryIdSegment(ref);
44
+ const dot = decoded.indexOf(".");
45
+ if (dot > -1 && DECLARED.has(decoded.slice(dot + 1)))
46
+ return decoded.slice(0, dot);
47
+ return decoded;
48
+ }
49
+ /** The named error for a `supersedes` target whose read failed (flair#2307). */
50
+ export const SUPERSEDES_TARGET_UNREADABLE_ERROR = "supersedes_target_unreadable";
51
+ /** The named error for a `supersedes` target that does not exist (flair#2307). */
52
+ export const SUPERSEDES_TARGET_MISSING_ERROR = "supersedes_target_missing";
53
+ /**
54
+ * Refuse a write whose `supersedes` target could not be read: the predecessor
55
+ * read every write with a `supersedes` makes (prepareSkillBody in
56
+ * resources/skill-version-write.ts), or, for a non-admin agent, the
57
+ * authorization read. A client error from the read (an id Harper cannot look
58
+ * up) answers 400; anything else 503, since a retry can succeed. Nothing has
59
+ * been written when this is returned.
60
+ */
61
+ export function supersedesTargetUnreadable(err) {
62
+ const code = err?.statusCode;
63
+ const status = typeof code === "number" && code >= 400 && code < 500 ? 400 : 503;
64
+ return new Response(JSON.stringify({
65
+ error: SUPERSEDES_TARGET_UNREADABLE_ERROR,
66
+ message: "the record named by `supersedes` could not be read; nothing was written",
67
+ }), { status, headers: { "content-type": "application/json" } });
68
+ }
69
+ /** Refuse a write whose `supersedes` names a record that does not exist. */
70
+ export function supersedesTargetMissing() {
71
+ return new Response(JSON.stringify({
72
+ error: SUPERSEDES_TARGET_MISSING_ERROR,
73
+ message: "the record named by `supersedes` does not exist; nothing was written",
74
+ }), { status: 409, headers: { "content-type": "application/json" } });
75
+ }
@@ -0,0 +1,236 @@
1
+ /**
2
+ * memory-redaction.ts — server-side credential redaction for explicit Memory
3
+ * writes (flair#2407).
4
+ *
5
+ * ONE redactor. The automatic-capture path and the action-recall cache call
6
+ * `redactSecrets` (packages/flair-mcp/src/secret-redaction.ts), which delegates
7
+ * to `redactSecretsWithCount`; this module calls `redactSecretsWithCount`
8
+ * directly, so the server keeps no second pattern list.
9
+ *
10
+ * Memory text fields are redacted on write. Skill writes refuse metadata with
11
+ * duplicate top-level keys; otherwise name/description and string tags are
12
+ * redacted (flair#2575).
13
+ *
14
+ * Memory post/put/patch and the feed ingest (resources/MemoryFeed.ts) call
15
+ * redactMemoryWrite() on the write body before the row is persisted; post and
16
+ * put call it before the dedup gate and the stored embedding are computed.
17
+ * post, put and the feed ingest call redactSkillWrite() on the EFFECTIVE row
18
+ * after skill-tag inheritance (prepareSkillBody) and before persistence, so an
19
+ * omitted-tag PUT on a stored skill, a superseding POST and a feed ingest
20
+ * redact the skill fields of the row they write.
21
+ * When the count is above zero, a Memory post/put/patch response also carries
22
+ * `stored`: the stored (post-redaction) values of the fields that write's
23
+ * request supplied (flair#2561) — the REDACTED_MEMORY_FIELDS it supplied as
24
+ * strings (suppliedMemoryTextFields) and, for a skill-tagged row, the skill
25
+ * fields it supplied (suppliedSkillTextFields, flair#2575). The supplied
26
+ * fields are read from the body as the request sent it, before a stored row or
27
+ * a skill predecessor is merged into it (suppliedStoredTextFields,
28
+ * storedMemoryText). The feed ingest's response holds, of the row it stored or
29
+ * matched, only named primitive fields, plus `redactedValues` when present
30
+ * (resources/memory-write-response.ts).
31
+ * A skill PUT whose redaction replaced a value always writes its redacted
32
+ * effective row, so each field `stored` reports is what the row holds: the
33
+ * in-place (reserved seed) PUT's unchanged-row check never skips it.
34
+ * put() skips the call for the shipped-skill seed write (isOperatorSeedPut): a
35
+ * PUT of the reserved seed skill id by a verified Basic administrator
36
+ * (src/lib/skill-seed.ts). The seed compares the stored text to its source
37
+ * text, so it must round-trip unchanged. The call does not otherwise depend on
38
+ * the caller: an admin Ed25519 key or an OAuth principal is an agent
39
+ * (resources/skill-write-policy.ts) and is redacted. put()'s `_reindex` re-PUT
40
+ * returns before the call; it refuses a change to these fields.
41
+ *
42
+ * The FederationSync receive path does not call this module: the redactor
43
+ * leaves incoming federated content alone. Federation checks a record's
44
+ * signature, when it carries one, on the incoming record before it adds
45
+ * receiver bookkeeping to the row.
46
+ */
47
+ import { redactSecretsWithCount } from "../packages/flair-mcp/src/secret-redaction.js";
48
+ import { isReservedSeedId } from "./seed-reservation.js";
49
+ import { isSkillWrite, MEMORY_EMBEDDING_INPUT_FIELDS } from "./skill-write.js";
50
+ import { parseSkillMetadata } from "./skill-provenance.js";
51
+ import { skillWriteSource } from "./skill-write-policy.js";
52
+ /** The Memory text fields redacted on a write. */
53
+ export const REDACTED_MEMORY_FIELDS = Object.freeze(["content", "summary", "trigger"]);
54
+ /** Metadata fields selected after duplicate-key validation. */
55
+ export const REDACTED_SKILL_FIELDS = Object.freeze(["name", "description"]);
56
+ /**
57
+ * flair#2561: the string-valued text fields a write request supplies: each of
58
+ * the REDACTED_MEMORY_FIELDS that holds a string in the request body. A field
59
+ * the body sends as null or as a non-string is not in the list. A write reads
60
+ * them from the body as the request sent it, before a stored row or a skill
61
+ * predecessor is merged into that body, so the list names only text fields the
62
+ * request itself carried.
63
+ */
64
+ export function suppliedMemoryTextFields(body) {
65
+ if (!body || typeof body !== "object" || Array.isArray(body))
66
+ return [];
67
+ return REDACTED_MEMORY_FIELDS.filter((field) => typeof body[field] === "string");
68
+ }
69
+ /**
70
+ * flair#2561, flair#2575: the skill fields a write request supplies: `name` and
71
+ * `description` when the body's own `metadata` holds a string under that key,
72
+ * and `tags` when the body carries a `tags` array. Read, like
73
+ * suppliedMemoryTextFields, from the body as the request sent it, before a
74
+ * stored row or a skill predecessor is merged into it, so a field the row
75
+ * inherits (an omitted `tags` array, or a predecessor's `metadata`) is not in
76
+ * the list.
77
+ */
78
+ export function suppliedSkillTextFields(body) {
79
+ if (!body || typeof body !== "object" || Array.isArray(body))
80
+ return [];
81
+ const record = body;
82
+ const meta = parseSkillMetadata(record.metadata);
83
+ const supplied = REDACTED_SKILL_FIELDS.filter((field) => typeof meta[field] === "string");
84
+ if (Array.isArray(record.tags))
85
+ supplied.push("tags");
86
+ return supplied;
87
+ }
88
+ /** The fields a Memory POST or PUT request supplies that its `stored` can
89
+ * report: suppliedMemoryTextFields plus suppliedSkillTextFields. */
90
+ export function suppliedStoredTextFields(body) {
91
+ return [...suppliedMemoryTextFields(body), ...suppliedSkillTextFields(body)];
92
+ }
93
+ /**
94
+ * flair#2467, flair#2561, flair#2575: the values on `row` of the `supplied`
95
+ * fields, copied; a supplied field that holds no value on `row` is left out. A
96
+ * write calls it on the row it stages, after redaction, with the fields its own
97
+ * request supplied (suppliedMemoryTextFields, suppliedStoredTextFields), so the
98
+ * copy holds the stored (post-redaction) value of each of those fields, changed
99
+ * or not, and no other field of the row: a field the row holds from a merged
100
+ * stored row or a skill predecessor is not in it. For a skill-tagged row the
101
+ * supplied skill fields are the `metadata` `name`/`description` (parsed out of
102
+ * the row) and the accepted `tags` entries. When the write replaced any value,
103
+ * its response carries the copy as `stored` next to `redactedValues`; the
104
+ * writer never has to read the row back, so a later write cannot change what
105
+ * this one reports.
106
+ */
107
+ export function storedMemoryText(row, supplied) {
108
+ const stored = {};
109
+ if (!row || typeof row !== "object" || Array.isArray(row))
110
+ return stored;
111
+ const record = row;
112
+ for (const field of REDACTED_MEMORY_FIELDS) {
113
+ if (!supplied.includes(field))
114
+ continue;
115
+ const value = record[field];
116
+ if (typeof value === "string")
117
+ stored[field] = value;
118
+ }
119
+ if (isSkillWrite(record)) {
120
+ const meta = parseSkillMetadata(record.metadata);
121
+ for (const field of REDACTED_SKILL_FIELDS) {
122
+ if (supplied.includes(field) && typeof meta[field] === "string")
123
+ stored[field] = meta[field];
124
+ }
125
+ // Report the entries the schema accepts (`[String]`: a string or null), so
126
+ // `stored.tags` is what the row carries, not only its string entries.
127
+ if (supplied.includes("tags") && Array.isArray(record.tags)) {
128
+ stored.tags = record.tags.filter((tag) => typeof tag === "string" || tag === null);
129
+ }
130
+ }
131
+ return stored;
132
+ }
133
+ /**
134
+ * Whether a Memory PUT is the shipped-skill seed write: the caller is a
135
+ * verified Basic administrator (skillWriteSource "operator") and every id the
136
+ * write names (the URL id and any body id) is the reserved seed skill id.
137
+ */
138
+ export function isOperatorSeedPut(context, auth, ids) {
139
+ const named = ids.filter((id) => id !== undefined && id !== null);
140
+ if (named.length === 0 || !named.every((id) => isReservedSeedId("Memory", id)))
141
+ return false;
142
+ return skillWriteSource(context, auth) === "operator";
143
+ }
144
+ /** The redacted fields an embedding can be computed from, across row kinds:
145
+ * `trigger` for a skill-tagged row with a non-empty trigger, `content` for every
146
+ * other row (MEMORY_EMBEDDING_INPUT_FIELDS, next to embedTextField/skillEmbedText
147
+ * in resources/skill-write.ts). Redaction sees the write body, not the stored
148
+ * row's kind, so it uses the whole set. */
149
+ const EMBEDDING_SOURCE_FIELDS = new Set(MEMORY_EMBEDDING_INPUT_FIELDS);
150
+ /**
151
+ * Redact REDACTED_MEMORY_FIELDS of a write body IN PLACE. When the redaction
152
+ * changes `content` or `trigger`, the fields an embedding is computed from, a
153
+ * caller-supplied `embedding` and `embeddingModel` are removed from the body,
154
+ * so the supplied vector is not stored. For a non-object input it changes
155
+ * nothing and returns `{ count: 0, embeddingDiscarded: false }`.
156
+ */
157
+ export function redactMemoryWrite(content) {
158
+ const none = { count: 0, embeddingDiscarded: false };
159
+ if (!content || typeof content !== "object" || Array.isArray(content))
160
+ return none;
161
+ const row = content;
162
+ let count = 0;
163
+ let embedSourceChanged = false;
164
+ for (const field of REDACTED_MEMORY_FIELDS) {
165
+ const value = row[field];
166
+ if (typeof value !== "string")
167
+ continue;
168
+ const redacted = redactSecretsWithCount(value);
169
+ if (redacted.text !== value) {
170
+ row[field] = redacted.text;
171
+ count += redacted.count;
172
+ if (EMBEDDING_SOURCE_FIELDS.has(field))
173
+ embedSourceChanged = true;
174
+ }
175
+ }
176
+ if (count === 0)
177
+ return none;
178
+ const supplied = row.embedding != null || row.embeddingModel != null;
179
+ if (!supplied || !embedSourceChanged)
180
+ return { count, embeddingDiscarded: false };
181
+ delete row.embedding;
182
+ delete row.embeddingModel;
183
+ return { count, embeddingDiscarded: true };
184
+ }
185
+ /**
186
+ * Redact name/description and string tags after prepareSkillBody has refused
187
+ * metadata with duplicate top-level keys and resolved the effective skill tag.
188
+ * Returns the number of values replaced.
189
+ */
190
+ export function redactSkillWrite(row) {
191
+ if (!row || typeof row !== "object" || Array.isArray(row))
192
+ return 0;
193
+ const record = row;
194
+ return isSkillWrite(record) ? redactSkillFields(record) : 0;
195
+ }
196
+ /** Redact validated skill fields in place. */
197
+ function redactSkillFields(row) {
198
+ let count = 0;
199
+ if (Array.isArray(row.tags)) {
200
+ row.tags = row.tags.map((entry) => {
201
+ if (typeof entry !== "string")
202
+ return entry;
203
+ const redacted = redactSecretsWithCount(entry);
204
+ if (redacted.text === entry)
205
+ return entry;
206
+ count += redacted.count;
207
+ return redacted.text;
208
+ });
209
+ }
210
+ count += redactSkillMetadata(row);
211
+ return count;
212
+ }
213
+ /** Serialize validated metadata only when name or description changes. */
214
+ function redactSkillMetadata(row) {
215
+ const original = row.metadata;
216
+ const asString = typeof original === "string";
217
+ const asObject = original !== null && typeof original === "object" && !Array.isArray(original);
218
+ if (!asString && !asObject)
219
+ return 0;
220
+ const parsed = parseSkillMetadata(original);
221
+ let count = 0;
222
+ for (const field of REDACTED_SKILL_FIELDS) {
223
+ const value = parsed[field];
224
+ if (typeof value !== "string")
225
+ continue;
226
+ const redacted = redactSecretsWithCount(value);
227
+ if (redacted.text !== value) {
228
+ parsed[field] = redacted.text;
229
+ count += redacted.count;
230
+ }
231
+ }
232
+ if (count === 0)
233
+ return 0;
234
+ row.metadata = asString ? JSON.stringify(parsed) : parsed;
235
+ return count;
236
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Non-admin Memory writes discard caller-supplied vectors and model stamps.
3
+ * A vector or stamp counts as caller-supplied only when it differs from the
4
+ * stored row's value; one re-sent unchanged is carried and kept as stored.
5
+ * Embedding is computed when the resulting write needs one.
6
+ * Administrator and trusted in-process writes and federation-sync records are unchanged.
7
+ * Empty-vector normalization and the re-embed request protocol are unchanged;
8
+ * an ordinary non-admin write strips a supplied stamp even alongside a null or empty embedding.
9
+ */
10
+ import { isDeepStrictEqual } from "node:util";
11
+ /** Presence check after empty-embedding normalization, not vector validation. */
12
+ export function hasSuppliedEmbedding(value) {
13
+ return value !== null && value !== undefined;
14
+ }
15
+ /** True when the caller's object body supplies an `embeddingModel` stamp (any
16
+ * value, `null` included). Captured before a stored row is merged in, so a
17
+ * stamp carried from a stored row is never mistaken for one the caller set. */
18
+ export function suppliesEmbeddingModel(content) {
19
+ return !!content && typeof content === "object" && !Array.isArray(content) &&
20
+ Object.prototype.hasOwnProperty.call(content, "embeddingModel");
21
+ }
22
+ function sameVector(a, b) {
23
+ const plain = (v) => (ArrayBuffer.isView(v) ? Array.from(v) : v);
24
+ return isDeepStrictEqual(plain(a), plain(b));
25
+ }
26
+ /** What the caller's own body supplies: a non-null `embedding` and/or an
27
+ * `embeddingModel` that DIFFERS from the stored row's. With no stored row
28
+ * (a create) everything present is supplied. Captured before a stored row is
29
+ * merged into the body. */
30
+ export function suppliedVectorFlags(content, stored) {
31
+ const body = content && typeof content === "object" && !Array.isArray(content) ? content : null;
32
+ const vector = hasSuppliedEmbedding(body?.embedding) && !(stored && sameVector(body.embedding, stored.embedding));
33
+ const stamp = suppliesEmbeddingModel(content) && !(stored && isDeepStrictEqual(body.embeddingModel, stored.embeddingModel));
34
+ return { vector, stamp };
35
+ }
36
+ /** True for actors exempt from discarding caller-supplied vectors:
37
+ * administrators and trusted in-process callers. A stored vector carried by
38
+ * a non-admin skill update can remain. */
39
+ export function keepsSuppliedVector(auth) {
40
+ return auth.kind === "internal" || (auth.kind === "agent" && auth.isAdmin);
41
+ }
42
+ /** Discard a non-admin write's caller-supplied vector and/or model stamp, in
43
+ * place. `supplied` comes from `suppliedVectorFlags`: what the CALLER's body
44
+ * held that differs from the stored row, before any stored row was merged in,
45
+ * so a vector prepareSkillBody carried from a stored row is left alone. A
46
+ * carried vector equal to `stored`'s keeps the stored stamp. A non-object or
47
+ * array body (no single record) is left untouched. Returns true when a field
48
+ * was removed. */
49
+ export function discardSuppliedVector(content, auth, supplied, stored) {
50
+ if (!content || typeof content !== "object" || Array.isArray(content))
51
+ return false;
52
+ if (keepsSuppliedVector(auth))
53
+ return false;
54
+ const carriedVector = !supplied.vector && hasSuppliedEmbedding(content.embedding);
55
+ let discarded = false;
56
+ if (supplied.vector && hasSuppliedEmbedding(content.embedding)) {
57
+ discarded = Object.prototype.hasOwnProperty.call(content, "embedding");
58
+ delete content.embedding;
59
+ }
60
+ if ((supplied.vector || supplied.stamp) && !carriedVector) {
61
+ discarded = Object.prototype.hasOwnProperty.call(content, "embeddingModel") || discarded;
62
+ delete content.embeddingModel;
63
+ }
64
+ else if (supplied.stamp && stored && sameVector(content.embedding, stored.embedding)) {
65
+ if (Object.prototype.hasOwnProperty.call(stored, "embeddingModel"))
66
+ content.embeddingModel = stored.embeddingModel;
67
+ else
68
+ delete content.embeddingModel;
69
+ discarded = true;
70
+ }
71
+ return discarded;
72
+ }