@tpsdev-ai/flair 0.59.0 → 0.63.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 (359) hide show
  1. package/README.md +10 -10
  2. package/SECURITY.md +49 -5
  3. package/config.yaml +23 -12
  4. package/dist/bridges/builtins/mem0.js +63 -28
  5. package/dist/bridges/runtime/execute.js +1 -0
  6. package/dist/bridges/runtime/import-runner.js +12 -0
  7. package/dist/bridges/runtime/load-plugin.js +42 -13
  8. package/dist/bridges/runtime/yaml-loader.js +4 -2
  9. package/dist/bridges/types.js +1 -0
  10. package/dist/build-info.json +3 -3
  11. package/dist/cli.js +1054 -367
  12. package/dist/commands/agent.js +233 -90
  13. package/dist/commands/attention.js +4 -3
  14. package/dist/commands/backup-approval.js +218 -0
  15. package/dist/commands/backup.js +211 -39
  16. package/dist/commands/bootstrap.js +3 -1
  17. package/dist/commands/bridge.js +172 -119
  18. package/dist/commands/deploy.js +71 -8
  19. package/dist/commands/doctor.js +711 -146
  20. package/dist/commands/export.js +57 -23
  21. package/dist/commands/federation.js +787 -82
  22. package/dist/commands/grant.js +5 -6
  23. package/dist/commands/help.js +25 -0
  24. package/dist/commands/hook.js +155 -2
  25. package/dist/commands/idp.js +27 -19
  26. package/dist/commands/import.js +136 -20
  27. package/dist/commands/init.js +469 -200
  28. package/dist/commands/integrity.js +387 -0
  29. package/dist/commands/keys.js +247 -58
  30. package/dist/commands/mcp.js +108 -52
  31. package/dist/commands/memory.js +203 -35
  32. package/dist/commands/migrate-harness-memory.js +3 -2
  33. package/dist/commands/orgevent.js +51 -47
  34. package/dist/commands/presence.js +4 -3
  35. package/dist/commands/principal.js +428 -62
  36. package/dist/commands/quality.js +13 -1
  37. package/dist/commands/reembed.js +141 -17
  38. package/dist/commands/relationship.js +2 -1
  39. package/dist/commands/rem.js +537 -80
  40. package/dist/commands/restore.js +413 -65
  41. package/dist/commands/search.js +6 -4
  42. package/dist/commands/service.js +235 -40
  43. package/dist/commands/soul.js +4 -9
  44. package/dist/commands/status.js +94 -38
  45. package/dist/commands/test.js +7 -3
  46. package/dist/commands/upgrade.js +77 -23
  47. package/dist/commands/workspace.js +13 -11
  48. package/dist/component-env.js +31 -41
  49. package/dist/deploy.js +136 -4
  50. package/dist/doctor-client.js +534 -92
  51. package/dist/engine-version-contract.js +1 -0
  52. package/dist/engine-version.js +23 -3
  53. package/dist/fabric-upgrade.js +12 -2
  54. package/dist/federation/presence-send.js +139 -0
  55. package/dist/federation-verify.js +273 -180
  56. package/dist/hook-install.js +670 -6
  57. package/dist/install/clients.js +171 -35
  58. package/dist/keystore.js +72 -0
  59. package/dist/lib/action-recall-runtime.js +328 -0
  60. package/dist/lib/agent-home-fields.js +132 -0
  61. package/dist/lib/agent-home.js +427 -0
  62. package/dist/lib/agent-id-rule.js +88 -0
  63. package/dist/lib/agent-roster.js +33 -0
  64. package/dist/lib/atomic-write.js +1 -1
  65. package/dist/lib/auth-resolve.js +1069 -60
  66. package/dist/lib/backup-approval-store.js +612 -0
  67. package/dist/lib/backup-artifact.js +343 -0
  68. package/dist/lib/capture-runtime.js +124 -0
  69. package/dist/lib/config-critical-section.js +212 -25
  70. package/dist/lib/connect-phase.js +33 -0
  71. package/dist/lib/credential-status-rule.js +78 -0
  72. package/dist/lib/daemon-liveness.js +43 -11
  73. package/dist/lib/doctor-run.js +75 -37
  74. package/dist/lib/durability-copy.js +10 -0
  75. package/dist/lib/embedding-space-doctor.js +75 -0
  76. package/dist/lib/federation-pair-access.js +4 -0
  77. package/dist/lib/federation-retry-queue.js +283 -0
  78. package/dist/lib/federation-retry-row.js +101 -0
  79. package/dist/lib/flair-agent-role.js +4 -0
  80. package/dist/lib/hub-url.js +12 -0
  81. package/dist/lib/init-admin-pass.js +88 -12
  82. package/dist/lib/init-listener-environ.js +1 -2
  83. package/dist/lib/init-occupied-listener.js +15 -111
  84. package/dist/lib/init-spawn-attribution.js +73 -0
  85. package/dist/lib/init-tcp-probe.js +33 -0
  86. package/dist/lib/instance-identity-row.js +20 -0
  87. package/dist/lib/instruction-event-contract.js +17 -0
  88. package/dist/lib/instruction-event-doctor.js +102 -0
  89. package/dist/lib/launchd-management.js +38 -3
  90. package/dist/lib/listener-table.js +268 -0
  91. package/dist/lib/local-delete-instance.js +27 -0
  92. package/dist/lib/local-instance-pair.js +256 -0
  93. package/dist/lib/mcp-enable.js +1182 -341
  94. package/dist/lib/mcp-oauth-env-core.js +133 -0
  95. package/dist/lib/mcp-oauth-env.js +125 -0
  96. package/dist/lib/mcp-spec.js +17 -1
  97. package/dist/lib/memory-attributes.js +4 -0
  98. package/dist/lib/memory-id-policy.js +46 -0
  99. package/dist/lib/memory-integrity.js +309 -0
  100. package/dist/lib/memory-provider-timeout.js +33 -0
  101. package/dist/lib/memory-purge-response.js +20 -0
  102. package/dist/lib/mqtt-config.js +132 -0
  103. package/dist/lib/mqtt-listener-doctor.js +104 -0
  104. package/dist/lib/open-live-row.js +20 -0
  105. package/dist/lib/ops-table-count.js +25 -0
  106. package/dist/lib/ops-url.js +52 -0
  107. package/dist/lib/owned-pins.js +67 -10
  108. package/dist/lib/peer-status.js +27 -0
  109. package/dist/lib/principal-status.js +29 -0
  110. package/dist/lib/process-start-time.js +76 -12
  111. package/dist/lib/promotion-content-hash.js +20 -0
  112. package/dist/lib/proto-safe-record.js +42 -0
  113. package/dist/lib/record-id-path.js +25 -0
  114. package/dist/lib/reembed-server-support.js +66 -0
  115. package/dist/lib/restore-archive.js +760 -0
  116. package/dist/lib/restore-verify.js +104 -0
  117. package/dist/lib/scheduler-platform.js +10 -6
  118. package/dist/lib/secret-keys.js +25 -0
  119. package/dist/lib/secrets-push.js +50 -22
  120. package/dist/lib/skill-assignment-preflight.js +159 -0
  121. package/dist/lib/skill-seed-pending.js +24 -0
  122. package/dist/lib/skill-seed.js +448 -0
  123. package/dist/lib/skill-tombstone-doctor.js +133 -0
  124. package/dist/lib/soul-scope-fields.js +26 -0
  125. package/dist/lib/stop-start-recovery.js +54 -0
  126. package/dist/lib/uninstall-purge.js +266 -11
  127. package/dist/lib/upgrade-prior-liveness.js +2 -1
  128. package/dist/lib/using-flair-skill.js +87 -0
  129. package/dist/mcp-client-assertion.js +114 -14
  130. package/dist/memory-id-checkpoint.js +347 -0
  131. package/dist/memory-node-compare.js +207 -0
  132. package/dist/mqtt-off-deploy.js +550 -0
  133. package/dist/packages/flair-mcp/src/secret-redaction.js +113 -0
  134. package/dist/rem/promote-policy.js +3 -3
  135. package/dist/rem/restore.js +303 -26
  136. package/dist/rem/runner.js +317 -96
  137. package/dist/rem/scheduler.js +21 -4
  138. package/dist/resources/A2AAdapter.js +131 -178
  139. package/dist/resources/AdminMemory.js +2 -0
  140. package/dist/resources/AdminMemoryCandidates.js +116 -0
  141. package/dist/resources/Agent.js +463 -33
  142. package/dist/resources/AgentCard.js +4 -0
  143. package/dist/resources/AgentKeyRegister.js +248 -0
  144. package/dist/resources/AgentKeyRegistration.js +28 -0
  145. package/dist/resources/AgentReadPosition.js +7 -0
  146. package/dist/resources/AgentSeed.js +238 -107
  147. package/dist/resources/AutoPromoteCandidates.js +413 -78
  148. package/dist/resources/Credential.js +183 -10
  149. package/dist/resources/Federation.js +915 -145
  150. package/dist/resources/FederationRetry.js +140 -0
  151. package/dist/resources/InstructionVersion.js +220 -0
  152. package/dist/resources/Integration.js +300 -25
  153. package/dist/resources/MCPClientMetadata.js +6 -3
  154. package/dist/resources/Memory.js +3300 -758
  155. package/dist/resources/MemoryArchive.js +171 -53
  156. package/dist/resources/MemoryBootstrap.js +122 -61
  157. package/dist/resources/MemoryCandidate.js +37 -3
  158. package/dist/resources/MemoryConsolidate.js +2 -2
  159. package/dist/resources/MemoryDedupStats.js +30 -1
  160. package/dist/resources/MemoryFeed.js +545 -50
  161. package/dist/resources/MemoryGrant.js +21 -1
  162. package/dist/resources/MemoryMaintenance.js +368 -46
  163. package/dist/resources/MemoryPurge.js +348 -0
  164. package/dist/resources/MemoryReflect.js +71 -12
  165. package/dist/resources/MemoryReindex.js +111 -17
  166. package/dist/resources/Message.js +16 -5
  167. package/dist/resources/OAuth.js +342 -48
  168. package/dist/resources/OAuthClient.js +1 -1
  169. package/dist/resources/OrgEvent.js +405 -19
  170. package/dist/resources/OrgEventCatchup.js +7 -0
  171. package/dist/resources/OrgEventMaintenance.js +48 -10
  172. package/dist/resources/PairingToken.js +1 -1
  173. package/dist/resources/Presence.js +166 -51
  174. package/dist/resources/PromoteMemoryCandidate.js +24 -7
  175. package/dist/resources/RecordUsage.js +2 -4
  176. package/dist/resources/Relationship.js +23 -0
  177. package/dist/resources/SemanticSearch.js +63 -32
  178. package/dist/resources/Soul.js +191 -76
  179. package/dist/resources/SoulFeed.js +2 -1
  180. package/dist/resources/TeamDirectory.js +35 -0
  181. package/dist/resources/WorkspaceLatest.js +4 -0
  182. package/dist/resources/WorkspaceState.js +21 -1
  183. package/dist/resources/XAA.js +207 -63
  184. package/dist/resources/a2a-stream.js +313 -0
  185. package/dist/resources/a2a-url.js +4 -5
  186. package/dist/resources/agent-admin.js +10 -1
  187. package/dist/resources/agent-auth.js +138 -83
  188. package/dist/resources/agent-read-position-lib.js +4 -1
  189. package/dist/resources/agent-status-guard.js +9 -7
  190. package/dist/resources/auth-middleware.js +534 -171
  191. package/dist/resources/authority-field-guard.js +9 -11
  192. package/dist/resources/auto-promote-lib.js +8 -5
  193. package/dist/resources/basic-auth-header.js +21 -0
  194. package/dist/resources/bm25-index-service.js +4 -5
  195. package/dist/resources/dedup-cluster.js +6 -1
  196. package/dist/resources/ed25519-auth.js +1 -1
  197. package/dist/resources/embedding-space-guard.js +366 -72
  198. package/dist/resources/embeddings-provider.js +25 -0
  199. package/dist/resources/engine-version-stamp-boot.js +53 -0
  200. package/dist/resources/federation-classify.js +161 -0
  201. package/dist/resources/federation-crypto.js +5 -5
  202. package/dist/resources/federation-retry-lib.js +60 -0
  203. package/dist/resources/grant-visibility.js +135 -0
  204. package/dist/resources/harper-routing.js +83 -0
  205. package/dist/resources/health.js +163 -22
  206. package/dist/resources/host-pointer/registry.js +29 -0
  207. package/dist/resources/host-pointer-adapter.js +1 -0
  208. package/dist/resources/host-source-citation.js +49 -0
  209. package/dist/resources/host-source-visibility.js +7 -6
  210. package/dist/resources/in-process.js +24 -22
  211. package/dist/resources/instance-create-lock.js +17 -0
  212. package/dist/resources/instance-identity.js +23 -0
  213. package/dist/resources/instruction-version-record.js +587 -0
  214. package/dist/resources/internal-authority.js +13 -0
  215. package/dist/resources/key-lock.js +3 -3
  216. package/dist/resources/mcp-handler.js +141 -19
  217. package/dist/resources/mcp-oauth-env-guard.js +36 -0
  218. package/dist/resources/mcp-oauth-flag.js +12 -14
  219. package/dist/resources/mcp-oauth.js +450 -56
  220. package/dist/resources/mcp-tool-arguments.js +4 -6
  221. package/dist/resources/mcp-tools.js +187 -40
  222. package/dist/resources/memory-bootstrap-lib.js +5 -15
  223. package/dist/resources/memory-declared-attributes.js +4 -1
  224. package/dist/resources/memory-deletion-history.js +55 -0
  225. package/dist/resources/memory-durability.js +54 -0
  226. package/dist/resources/memory-embed-hash.js +144 -0
  227. package/dist/resources/memory-embedding-value.js +8 -0
  228. package/dist/resources/memory-host-source.js +32 -7
  229. package/dist/resources/memory-id-guard.js +75 -0
  230. package/dist/resources/memory-redaction.js +236 -0
  231. package/dist/resources/memory-reflect-lib.js +1 -1
  232. package/dist/resources/memory-supplied-vector.js +72 -0
  233. package/dist/resources/memory-write-lock.js +489 -0
  234. package/dist/resources/memory-write-response.js +139 -0
  235. package/dist/resources/memory-write-transaction.js +243 -0
  236. package/dist/resources/migration-boot.js +17 -12
  237. package/dist/resources/migrations/boot-progress.js +25 -0
  238. package/dist/resources/migrations/data-dir.js +90 -19
  239. package/dist/resources/migrations/dir-safety.js +61 -3
  240. package/dist/resources/migrations/embedding-stamp.js +462 -82
  241. package/dist/resources/migrations/export.js +1 -0
  242. package/dist/resources/migrations/graph-heal.js +27 -27
  243. package/dist/resources/migrations/ledger.js +5 -2
  244. package/dist/resources/migrations/lock.js +870 -77
  245. package/dist/resources/migrations/progress.js +71 -10
  246. package/dist/resources/migrations/runner.js +212 -18
  247. package/dist/resources/migrations/snapshot.js +1 -0
  248. package/dist/resources/migrations/source-fields.js +5 -5
  249. package/dist/resources/migrations/state.js +36 -1
  250. package/dist/resources/migrations/status.js +2 -13
  251. package/dist/resources/migrations/synthetic-test-migration.js +81 -12
  252. package/dist/resources/migrations/types.js +26 -1
  253. package/dist/resources/migrations/visibility-backfill.js +113 -72
  254. package/dist/resources/oauth-discovery.js +0 -5
  255. package/dist/resources/object-body-guard.js +40 -0
  256. package/dist/resources/org-event-created-at.js +133 -0
  257. package/dist/resources/org-event-reserved.js +92 -0
  258. package/dist/resources/org-event-write-fault.js +19 -0
  259. package/dist/resources/originator-instance.js +39 -7
  260. package/dist/resources/owner-delete-recheck.js +78 -0
  261. package/dist/resources/owner-field-guard.js +39 -5
  262. package/dist/resources/owner-scoped-write-policy.js +256 -0
  263. package/dist/resources/owner-scoped-writes.js +89 -0
  264. package/dist/resources/path-id-rewrite.js +76 -0
  265. package/dist/resources/presence-internal.js +5 -5
  266. package/dist/resources/presence-receipt.js +236 -0
  267. package/dist/resources/promotion-binding.js +43 -0
  268. package/dist/resources/promotion-stamp.js +143 -17
  269. package/dist/resources/protocol-context.js +189 -0
  270. package/dist/resources/record-owner-guard.js +74 -26
  271. package/dist/resources/record-type-kit.js +2 -4
  272. package/dist/resources/record-types.js +1 -1
  273. package/dist/resources/relay-ops.js +78 -6
  274. package/dist/resources/replay-store.js +96 -19
  275. package/dist/resources/request-target.js +163 -0
  276. package/dist/resources/request-transaction.js +46 -1
  277. package/dist/resources/search-readiness.js +4 -1
  278. package/dist/resources/seed-ids.js +9 -0
  279. package/dist/resources/seed-reservation.js +116 -0
  280. package/dist/resources/semantic-retrieval-core.js +27 -15
  281. package/dist/resources/server-row-validation.js +71 -0
  282. package/dist/resources/skill-manifest.js +24 -14
  283. package/dist/resources/skill-subject.js +53 -0
  284. package/dist/resources/skill-version-write.js +789 -0
  285. package/dist/resources/skill-write-fault.js +20 -0
  286. package/dist/resources/skill-write-policy.js +68 -0
  287. package/dist/resources/skill-write.js +67 -7
  288. package/dist/resources/soul-write-policy.js +26 -7
  289. package/dist/resources/table-helpers.js +10 -7
  290. package/dist/resources/table-subscription-policy.js +518 -85
  291. package/dist/resources/table-subscriptions.js +20 -7
  292. package/dist/resources/team-directory.js +248 -0
  293. package/dist/resources/tool-descriptors/index.js +69 -8
  294. package/dist/resources/txn-pause-point.js +235 -0
  295. package/dist/resources/usage-recording.js +186 -70
  296. package/dist/resources/write-back.js +383 -0
  297. package/dist/resources/write-journal-context.js +5 -0
  298. package/dist/resources/write-journal.js +289 -0
  299. package/dist/src/engine-version-contract.js +1 -0
  300. package/dist/src/keystore.js +72 -0
  301. package/dist/src/lib/agent-home-fields.js +132 -0
  302. package/dist/src/lib/agent-id-rule.js +88 -0
  303. package/dist/src/lib/agent-key-register.js +80 -0
  304. package/dist/src/lib/atomic-write.js +256 -0
  305. package/dist/src/lib/credential-status-rule.js +78 -0
  306. package/dist/src/lib/expired-by-agent.js +37 -0
  307. package/dist/src/lib/federation-retry-row.js +101 -0
  308. package/dist/src/lib/flair-agent-role.js +4 -0
  309. package/dist/src/lib/idp-mapping-rule.js +81 -0
  310. package/dist/src/lib/instance-identity-row.js +20 -0
  311. package/dist/src/lib/instruction-event-contract.js +17 -0
  312. package/dist/src/lib/mcp-oauth-env-core.js +133 -0
  313. package/dist/src/lib/memory-attributes.js +4 -0
  314. package/dist/src/lib/memory-id-policy.js +46 -0
  315. package/dist/src/lib/open-live-row.js +20 -0
  316. package/dist/src/lib/principal-status.js +29 -0
  317. package/dist/src/lib/proto-safe-record.js +42 -0
  318. package/dist/src/lib/reembed-server-support.js +66 -0
  319. package/dist/src/lib/scheduler-platform.js +10 -6
  320. package/dist/src/lib/soul-scope-fields.js +26 -0
  321. package/dist/src/lib/systemd-agent-id.js +38 -0
  322. package/dist/src/rem/promote-policy.js +3 -3
  323. package/dist/src/rem/scheduler.js +21 -4
  324. package/docs/access-control.md +164 -54
  325. package/docs/api-reference.md +165 -32
  326. package/docs/auth.md +46 -30
  327. package/docs/bridges.md +11 -0
  328. package/docs/claude-code.md +37 -7
  329. package/docs/deploying-on-fabric.md +178 -0
  330. package/docs/deployment.md +19 -0
  331. package/docs/embedding-in-a-harper-app.md +17 -15
  332. package/docs/entity-vocabulary.md +3 -6
  333. package/docs/federation.md +61 -3
  334. package/docs/hosted-on-fabric.md +43 -36
  335. package/docs/integrations.md +14 -14
  336. package/docs/mcp-clients.md +12 -7
  337. package/docs/n8n.md +17 -11
  338. package/docs/notes/mcp-agent-auth-consumer.md +16 -13
  339. package/docs/notes/mcp-oauth-model2.md +40 -11
  340. package/docs/openclaw.md +1 -1
  341. package/docs/quickstart-fabric.md +4 -4
  342. package/docs/quickstart.md +12 -4
  343. package/docs/releasing.md +25 -19
  344. package/docs/rem.md +32 -3
  345. package/docs/secrets-and-keys.md +21 -4
  346. package/docs/standalone-local.md +30 -2
  347. package/docs/supply-chain-policy.md +34 -16
  348. package/docs/the-team.md +2 -2
  349. package/docs/upgrade.md +46 -6
  350. package/package.json +24 -12
  351. package/schemas/agent-key-registration.graphql +22 -0
  352. package/schemas/agent.graphql +6 -3
  353. package/schemas/federation.graphql +41 -1
  354. package/schemas/memory.graphql +91 -7
  355. package/schemas/oauth.graphql +19 -0
  356. package/schemas/schema.graphql +2 -0
  357. package/templates/bin/flair-rem-nightly.sh.tmpl +7 -1
  358. package/templates/launchd/start-flair-with-admin-pass.sh +188 -10
  359. package/dist/resources/soul-adk-guard.js +0 -68
@@ -0,0 +1,189 @@
1
+ /**
2
+ * The markers Harper's protocol layers put on the call contexts they build,
3
+ * and the tests that read them. Shared by the caller resolver
4
+ * (resources/agent-auth.ts) and the table-subscription guard
5
+ * (resources/table-subscription-policy.ts), so both read a protocol-layer call
6
+ * the same way. Every test here accepts a shape or a marker; none establishes
7
+ * who built the object. The guard's boundary is protocol-layer callers, whose
8
+ * contexts Harper constructs; in-process code is trusted and may pass objects
9
+ * of any shape (see the policy module).
10
+ *
11
+ * Harper's MQTT layer gives every action of a session a context built by the
12
+ * session itself (server/DurableSubscriptionsSession.ts `createContext()`):
13
+ * `{ session, socket, user, authorize: true, request?, url?, headers? }`, where
14
+ * `session` is the MQTT session object and `request` is present only for MQTT
15
+ * over WebSocket (the upgrade request). Harper's native MCP resource
16
+ * subscriptions use `{ user, authorize: true, request: <RequestTarget> }`, a
17
+ * context with no HTTP request, and its native MCP application tools (get_,
18
+ * search_, create_, update_, delete_ and custom-method tools) use
19
+ * `{ user, authorize: true, checkPermission }`, a context with no request at
20
+ * all. flair has no verified bridge from that Harper user to a flair caller, so
21
+ * the resolver answers anonymous for both until one exists. Harper clears
22
+ * `authorize` once its first entry call has used it (resources/Resource.ts), so
23
+ * the MQTT session object, and on an application tool's context the
24
+ * `checkPermission` property, is the marker that stays on the context.
25
+ *
26
+ * A retained MQTT PUBLISH reaches a table's static put()/delete(), which
27
+ * ordinary writes also use. isDirectMqttSessionContext recognises the session's
28
+ * context there, and isLastWillReplayContext the copy of a stored will record
29
+ * Harper replays. Both accept a shape, and use descriptor and prototype reads
30
+ * only — the argument's own property descriptors, and for a session-marked
31
+ * argument descriptors along the nested session's prototype chain — so on a
32
+ * Harper-constructed or plain-object context they invoke no getter and no
33
+ * `getContext()`. An exotic object (a Proxy) observes those reads, and an
34
+ * error one throws propagates to the caller.
35
+ *
36
+ * This module has no imports, so it is unit-tested directly.
37
+ */
38
+ /**
39
+ * Whether `context` carries an MQTT session's markers: a `socket` key (own or
40
+ * inherited) and a `session` object with an `addSubscription` method, read
41
+ * with ordinary property access (getters run). Used by the caller resolver.
42
+ */
43
+ export function isMqttSessionContext(context) {
44
+ if (context == null || typeof context !== "object")
45
+ return false;
46
+ const c = context;
47
+ if (!("socket" in context) || c.session == null || typeof c.session !== "object")
48
+ return false;
49
+ return typeof c.session.addSubscription === "function";
50
+ }
51
+ /**
52
+ * An own data property's value; undefined for a missing key and for an
53
+ * accessor, which is never invoked.
54
+ */
55
+ function ownDataValue(target, key) {
56
+ const descriptor = Object.getOwnPropertyDescriptor(target, key);
57
+ return descriptor && "value" in descriptor ? descriptor.value : undefined;
58
+ }
59
+ /**
60
+ * A data property's value on `target` or its prototype chain; undefined for a
61
+ * missing key and for an accessor, which is never invoked.
62
+ */
63
+ function dataValueInChain(target, key) {
64
+ for (let o = target; o != null; o = Object.getPrototypeOf(o)) {
65
+ const descriptor = Object.getOwnPropertyDescriptor(o, key);
66
+ if (descriptor)
67
+ return "value" in descriptor ? descriptor.value : undefined;
68
+ }
69
+ return undefined;
70
+ }
71
+ /**
72
+ * Whether `value` has the shape of the context Harper's MQTT layer builds
73
+ * for a session's action: an own `socket` property (a data property or an
74
+ * accessor, which is not invoked) and an own data property `session` holding
75
+ * an object whose `addSubscription`, found as a data property along that
76
+ * object's prototype chain, is a function. Harper's `createContext()` returns
77
+ * a plain object literal of this shape, and a session's retained will copies
78
+ * it. This classifies by shape: it does not prove that Harper built the
79
+ * object. It reads the own property descriptor of `socket`; only when one
80
+ * exists, the own descriptor of `session`; and only when that holds an
81
+ * object, descriptors along the session's prototype chain (getPrototypeOf
82
+ * included) until it finds `addSubscription`. It invokes no getter or method
83
+ * of `value` or the session (no `getContext()`, no accessor). A descriptor or
84
+ * prototype read that throws propagates.
85
+ */
86
+ export function isDirectMqttSessionContext(value) {
87
+ if (value == null || typeof value !== "object")
88
+ return false;
89
+ if (!Object.getOwnPropertyDescriptor(value, "socket"))
90
+ return false;
91
+ const session = ownDataValue(value, "session");
92
+ if (session == null || typeof session !== "object")
93
+ return false;
94
+ return typeof dataValueInChain(session, "addSubscription") === "function";
95
+ }
96
+ /**
97
+ * Whether `value` has the shape of the context Harper uses when it replays
98
+ * the last-will messages still stored as its first worker starts (an instance
99
+ * start or a worker restart; server/DurableSubscriptionsSession.ts): a
100
+ * shallow copy of
101
+ * the stored will record (`{ id, topic, data, qos, retain, user }`) on which
102
+ * Harper sets `authorize: true`, passed to put()/delete() for a retained will.
103
+ * It carries no HTTP request and no credential flair verifies; its `user` is
104
+ * the name the session connected with, looked up again. Read from own property
105
+ * descriptors only — `authorize` true, then `topic` a string, then `retain`
106
+ * true, each read only while the previous one matches — so no getter is
107
+ * invoked; a descriptor read that throws propagates. It classifies by shape:
108
+ * an object of that shape is decided as a replayed will wherever it came
109
+ * from.
110
+ */
111
+ export function isLastWillReplayContext(value) {
112
+ if (value == null || typeof value !== "object")
113
+ return false;
114
+ return ownDataValue(value, "authorize") === true
115
+ && typeof ownDataValue(value, "topic") === "string"
116
+ && ownDataValue(value, "retain") === true;
117
+ }
118
+ /**
119
+ * Whether `value` has the shape of Harper's released-transaction sentinel.
120
+ * Harper's sentinel (resources/DatabaseTransaction.ts `RELEASED_TRANSACTION`)
121
+ * is one frozen object, which Harper treats as an absent argument by identity;
122
+ * flair cannot reach that instance. This matcher is broader: it accepts any
123
+ * object, frozen or not, whose own data properties include an `addWrite` and a
124
+ * `setContext` function and `open` 0, read from own property descriptors
125
+ * (`addWrite` first; the others only while the previous ones match). Callers
126
+ * use it only to read an argument both as given and as absent.
127
+ */
128
+ export function isReleasedTransactionPlaceholder(value) {
129
+ if (value == null || typeof value !== "object")
130
+ return false;
131
+ return typeof ownDataValue(value, "addWrite") === "function"
132
+ && typeof ownDataValue(value, "setContext") === "function"
133
+ && ownDataValue(value, "open") === 0;
134
+ }
135
+ /**
136
+ * Whether `value` carries a transaction-shaped object: an own data
137
+ * `transaction` property holding an object with a `commit` method (found as a
138
+ * data property along its prototype chain). Harper tests `instanceof
139
+ * DatabaseTransaction`, a class flair cannot reach; this shape test is
140
+ * broader. Uses descriptor and prototype reads only.
141
+ */
142
+ export function carriesTransaction(value) {
143
+ if (value == null || typeof value !== "object")
144
+ return false;
145
+ const transaction = ownDataValue(value, "transaction");
146
+ if (transaction == null || typeof transaction !== "object")
147
+ return false;
148
+ return typeof dataValueInChain(transaction, "commit") === "function";
149
+ }
150
+ /**
151
+ * Whether `value` has the shape of the context Harper's native MCP application
152
+ * tools build for each call (components/mcp/tools/application.ts
153
+ * `buildContext()`): `{ user, authorize: true, checkPermission }`, where
154
+ * `checkPermission` is the Harper user's role permission object (`{}` when the
155
+ * role has none). It carries no `request`. Harper clears `authorize` once its
156
+ * first entry call has used it (resources/Resource.ts) and leaves
157
+ * `checkPermission`, so `checkPermission` is the marker that stays on the
158
+ * context through the resource's authorization hooks and its handlers. The
159
+ * test: an own data property `checkPermission` whose value is neither null,
160
+ * undefined nor false, and no own data property `request` holding a value
161
+ * other than null or undefined (an accessor `request` is not invoked and does
162
+ * not count as a request). Read from own property descriptors only
163
+ * (`checkPermission` first, `request` only when it matches), so no getter is
164
+ * invoked; a descriptor read that throws propagates. It classifies by shape:
165
+ * it does not prove that Harper built the object.
166
+ */
167
+ export function isPermissionMarkedRequestlessContext(value) {
168
+ if (value == null || typeof value !== "object")
169
+ return false;
170
+ const permission = ownDataValue(value, "checkPermission");
171
+ if (permission == null || permission === false)
172
+ return false;
173
+ return ownDataValue(value, "request") == null;
174
+ }
175
+ /**
176
+ * Whether `context` carries a protocol layer's markers: an MQTT session's
177
+ * markers (isMqttSessionContext); `authorize: true`, which Harper's protocol
178
+ * layers set to ask for authorization and clear once it is used; or the
179
+ * requestless, permission-marked shape of a native MCP application tool's
180
+ * context (isPermissionMarkedRequestlessContext), which keeps its marker after
181
+ * `authorize` is cleared.
182
+ */
183
+ export function isProtocolLayerContext(context) {
184
+ if (context == null || typeof context !== "object")
185
+ return false;
186
+ return context.authorize === true
187
+ || isMqttSessionContext(context)
188
+ || isPermissionMarkedRequestlessContext(context);
189
+ }
@@ -75,6 +75,7 @@
75
75
  * nor exempted with a stated reason. A table added later enters that test's
76
76
  * scope the moment it declares the column — nobody has to know this file exists.
77
77
  */
78
+ import { routedRecordId } from "./request-target.js";
78
79
  /**
79
80
  * Table → the attribute naming the principal that owns a row.
80
81
  *
@@ -121,6 +122,15 @@ export const OWNER_GUARD_EXEMPT = Object.freeze({
121
122
  // writes stamp `authorId` from the authenticated principal; direct resource
122
123
  // reads permit admins and trusted internal callers.
123
124
  MemoryHostSource: "REST writes refused; application writes stamp authorId; direct reads limited to admins and trusted internal callers",
125
+ // Application append-only history; REST writes are refused.
126
+ InstructionVersion: "application append-only; REST writes refused",
127
+ // flair#2266: the private audit log of key registrations. Its `agentId` names
128
+ // the row whose key was registered (the AUDIT TARGET), not the caller — the
129
+ // caller is the admin in `actorId`. Its only application writer is
130
+ // resources/AgentKeyRegister.ts; the Flair resource route
131
+ // (resources/AgentKeyRegistration.ts) admits admin reads and refuses every
132
+ // write. The Harper operations API is a separate raw-table trust root.
133
+ AgentKeyRegistration: "admin-read-only audit log; Flair resource-route writes refused; the registration operation is its only application writer",
124
134
  });
125
135
  /** The verbs that can mutate a record, and therefore need the rule applied. */
126
136
  export const MUTATING_METHODS = Object.freeze(["POST", "PUT", "PATCH", "DELETE"]);
@@ -128,33 +138,41 @@ export function isMutatingMethod(method) {
128
138
  return MUTATING_METHODS.includes(method.toUpperCase());
129
139
  }
130
140
  /**
131
- * Resolve a request path to the guarded table and record id it addresses, or
132
- * null when the path is not a guarded single-record route.
133
- *
134
- * Matches `/<Table>/<id>` ONLY. A collection path (`/<Table>`) addresses no
135
- * existing record, so there is nothing to own and nothing to check — that is the
136
- * "creation is untouched" property, expressed as a parse rather than a special
137
- * case. The table segment is matched EXACTLY so `/SoulFeed/x` can never be read
138
- * as a `Soul` route.
141
+ * Resolve a raw request target to the guarded table and record id it
142
+ * addresses, or null when the target is not one this early check decides.
143
+ *
144
+ * `pathname` is the RAW request path (the target before its `?`). The id is the
145
+ * FULL decoded remainder after the table segment, so an id that itself contains
146
+ * `/`, raw or as `%2F`, resolves to the whole id rather than a shorter prefix.
147
+ *
148
+ * Matches `/<Table>/<id>` ONLY. A bare resource path has no record id; a
149
+ * trailing-slash path is a collection. The table segment is matched EXACTLY so
150
+ * `/SoulFeed/x` can never be read as a `Soul` route.
139
151
  */
140
152
  export function resolveGuardedRecord(pathname) {
141
- const parts = pathname.split("/").filter(Boolean);
142
- if (parts.length < 2)
153
+ const routed = routedRecordId(pathname);
154
+ if (!routed)
155
+ return null; // no record id, or malformed percent-encoding
156
+ return resolveGuardedRecordById(routed.resource, routed.id);
157
+ }
158
+ /**
159
+ * The guarded record for a resource and record id already resolved by Harper's
160
+ * own routing (resources/harper-routing.ts). Null when the resource is not a
161
+ * guarded table, or there is no record id. This is the form
162
+ * the middleware uses at request time, where the id has had Harper's
163
+ * content-type-suffix and property rules applied.
164
+ */
165
+ export function resolveGuardedRecordById(resource, id) {
166
+ if (id == null || id === "")
143
167
  return null;
144
- const table = parts[0];
145
- const ownerField = OWNER_FIELDS[table];
146
- if (!ownerField)
168
+ // Own property only: an inherited name (`constructor`, `__proto__`, …) is not
169
+ // a table with an owner field.
170
+ if (!Object.prototype.hasOwnProperty.call(OWNER_FIELDS, resource))
147
171
  return null;
148
- let id;
149
- try {
150
- id = decodeURIComponent(parts[1]);
151
- }
152
- catch {
153
- return null; // malformed percent-encoding addresses no record we can resolve
154
- }
155
- if (!id)
172
+ const ownerField = OWNER_FIELDS[resource];
173
+ if (!ownerField)
156
174
  return null;
157
- return { table, ownerField, id };
175
+ return { table: resource, ownerField, id };
158
176
  }
159
177
  /**
160
178
  * Decide whether a caller may mutate an already-stored record.
@@ -184,16 +202,46 @@ export function isForbiddenOwnerMutation(record, ownerField, callerAgentId) {
184
202
  * principal: 403 with a body that names only the table.
185
203
  *
186
204
  * One constructor for every place the rule is enforced — the auth middleware
187
- * for `/<Table>/<id>` routes, and resources/relay-ops.ts for a Message POST to
188
- * the collection that reuses an existing message id — so a refused caller gets
189
- * the same response on either route, and it carries nothing from the stored
190
- * row.
205
+ * for `/<Table>/<id>` routes, resources/owner-scoped-write-policy.ts for a
206
+ * non-admin addressed write (POST/PUT/PATCH/DELETE to `/<Table>/<id>`) to an
207
+ * owner-scoped table, and resources/relay-ops.ts for a Message POST to the
208
+ * collection that reuses an existing message id. Carries no stored-row content.
191
209
  */
192
210
  export function ownerMutationRefusal(table) {
193
211
  return new Response(JSON.stringify({
194
212
  error: `forbidden: cannot modify ${table} owned by another principal`,
195
213
  }), { status: 403, headers: { "Content-Type": "application/json" } });
196
214
  }
215
+ /**
216
+ * The refusal for a non-admin PUT, PATCH or DELETE that addresses an
217
+ * owner-scoped table's collection rather than one record: 403 with a body that
218
+ * names only the table (resources/owner-scoped-write-policy.ts).
219
+ */
220
+ export function collectionWriteRefusal(table) {
221
+ return new Response(JSON.stringify({
222
+ error: `forbidden: a non-admin write to ${table} must address one record by its id`,
223
+ }), { status: 403, headers: { "Content-Type": "application/json" } });
224
+ }
225
+ /** The named refusal when a non-admin agent's stored-record read for an owner check fails. */
226
+ export const OWNER_CHECK_UNAVAILABLE_ERROR = "owner_check_unavailable";
227
+ /**
228
+ * Refuse a non-admin agent's write when the stored record its owner check
229
+ * needs cannot be read (resources/owner-field-guard.ts's guard thunk, and the
230
+ * owner-scoped write guard's stored-record read). A numeric 4xx `statusCode`
231
+ * maps to 400; anything else maps to 503. It logs the failure and has no
232
+ * authentication-module dependency, so the owner-scoped write guard can use it
233
+ * without importing that module graph; re-exported by
234
+ * resources/owner-field-guard.ts.
235
+ */
236
+ export function ownerCheckUnavailable(err) {
237
+ console.error("owner-field-guard: the stored record could not be read, so the write was refused", { err });
238
+ const code = err?.statusCode;
239
+ const status = typeof code === "number" && code >= 400 && code < 500 ? 400 : 503;
240
+ return new Response(JSON.stringify({
241
+ error: OWNER_CHECK_UNAVAILABLE_ERROR,
242
+ message: "the stored record could not be read; the write was refused",
243
+ }), { status, headers: { "content-type": "application/json" } });
244
+ }
197
245
  /**
198
246
  * Decide whether a caller may CHANGE the owner field of an already-stored record.
199
247
  *
@@ -136,10 +136,8 @@ function ownerOnlyReadScope(ownerField) {
136
136
  }
137
137
  /**
138
138
  * Resolve the read-scope resolver for a table, by mode:
139
- * - "owner-only": own records only, scoped on `ownerField` (defaults to
140
- * "agentId" — every current owner-only caller uses that field; the
141
- * parameter exists so a future owner-only type using a different field,
142
- * the way OrgEvent's WRITE path uses "authorId", isn't hardcoded out).
139
+ * - "owner-only": own records only, scoped on `ownerField`. The owner field
140
+ * defaults to "agentId"; Credential supplies "principalId".
143
141
  * - "open-within-org": delegates to the EXACT existing
144
142
  * resolveReadScope()/PRIVATE_VISIBILITY semantics in
145
143
  * ./memory-read-scope.ts — Memory's own module, untouched, not
@@ -403,7 +403,7 @@ export const RECORD_TYPES = {
403
403
  // skill_search — skill-tagged Memory recall (flair#1546), rides SemanticSearch
404
404
  // skill_get — full skill read by id (flair#1546), rides Memory.get
405
405
  //
406
- export const COMPOSITE_MCP_TOOLS = ["bootstrap", "attention", "record_usage", "memory_basement", "memory_restore", "skill_store", "skill_search", "skill_get"];
406
+ export const COMPOSITE_MCP_TOOLS = ["bootstrap", "attention", "record_usage", "memory_basement", "memory_restore", "skill_store", "skill_search", "skill_get", "team_directory"];
407
407
  // ─── Runtime immutability ───────────────────────────────────────────────────
408
408
  // Belt-and-suspenders backstop for the "static, compiled" invariant (see
409
409
  // header doc). TypeScript's `as const satisfies` already gives compile-time
@@ -9,7 +9,10 @@
9
9
  * All authorization decisions are made here against an already-resolved auth
10
10
  * verdict; the resource layer only resolves the verdict and forwards the call.
11
11
  */
12
+ import { withKeyLock } from "./key-lock.js";
13
+ import { withOwnedTransaction } from "./request-transaction.js";
12
14
  import { isForbiddenOwnerMutation, ownerMutationRefusal, OWNER_FIELDS } from "./record-owner-guard.js";
15
+ import { txnPausePoint } from "./txn-pause-point.js";
13
16
  import { capDecision, computeContentHash, isUnconsumed, LOCAL_ORG_SENTINEL, reconcileState, sweepDecision, verifyMessageSignature, } from "./relay-lib.js";
14
17
  // ─── HTTP responses (Response is a web global, not a Harper import) ──────────
15
18
  const json = (status, obj) => new Response(JSON.stringify(obj), { status, headers: { "Content-Type": "application/json" } });
@@ -22,6 +25,37 @@ const BAD_REQUEST = (error) => json(400, { error });
22
25
  const CONFLICT = (error) => json(409, { error, reason: "seq_conflict" });
23
26
  /** Over-cap: a synchronous 4xx that names the reason — backpressure that fails loud. */
24
27
  const INBOX_FULL = (scope) => json(429, { error: "inbox full", reason: "inbox_full", scope });
28
+ /** The message-id or sender lock could not be acquired: the send is refused. */
29
+ const SEND_BUSY = () => json(409, { error: "send in progress", reason: "send_contended" });
30
+ /** Without a per-key lock, serialization within one Harper process is
31
+ * unavailable: the send is refused. */
32
+ const SEND_LOCK_UNAVAILABLE = () => json(503, { error: "send lock unavailable", message: "the Message store has no per-key lock; the send was refused" });
33
+ // ─── send-time serialization ────────────────────────────────────────────────
34
+ /** First element of every relay-send lock key (resources/key-lock.ts), so a send
35
+ * lock never collides with another lock on the database. */
36
+ const SEND_LOCK_NAMESPACE = "flair-relay-send";
37
+ /** Per-lock retry schedule: SEND_LOCK_ATTEMPTS attempts, SEND_LOCK_WAIT_MS apart. */
38
+ const SEND_LOCK_ATTEMPTS = 200;
39
+ const SEND_LOCK_WAIT_MS = 10;
40
+ /**
41
+ * Run `fn` while holding every key in `keys`, acquired in the array's order
42
+ * (resources/key-lock.ts). A send holds its locks in ONE fixed order — message
43
+ * id, then recipient, then sender — and only ever waits for a lock later in
44
+ * that order than every lock it already holds, so no wait-for cycle (deadlock)
45
+ * is possible. Returns the innermost outcome: `{kind:"done", value}` when every
46
+ * lock was held and `fn` ran, `{kind:"busy"}` if any lock stayed held, or
47
+ * `{kind:"unavailable"}` if the store has no lock — in the last two cases `fn`
48
+ * never runs.
49
+ */
50
+ async function withKeyLocks(store, keys, fn, attempts, waitMs) {
51
+ const run = async (i) => {
52
+ if (i >= keys.length)
53
+ return { kind: "done", value: await fn() };
54
+ const held = await withKeyLock(store, keys[i], () => run(i + 1), attempts, waitMs);
55
+ return held.kind === "done" ? held.value : held;
56
+ };
57
+ return run(0);
58
+ }
25
59
  // ─── Helpers ────────────────────────────────────────────────────────────────
26
60
  /**
27
61
  * Materialize a table.search() result (async iterable | iterable | promise) to
@@ -32,11 +66,11 @@ const INBOX_FULL = (scope) => json(429, { error: "inbox full", reason: "inbox_fu
32
66
  * call site remain the correctness guarantee (the unit-test table double ignores
33
67
  * the query and returns every row); the conditions are the production index hint.
34
68
  */
35
- async function queryRows(table, conditions = []) {
69
+ async function queryRows(table, conditions = [], ctx) {
36
70
  const query = conditions.length > 0
37
71
  ? { operator: "and", conditions: conditions.map((c) => ({ attribute: c.attribute, comparator: "equals", value: c.value })) }
38
72
  : undefined;
39
- const res = table.search(query);
73
+ const res = table.search(query, ctx);
40
74
  const iter = res && typeof res.then === "function" ? await res : res;
41
75
  const out = [];
42
76
  for await (const row of iter)
@@ -122,6 +156,40 @@ export async function relaySend(deps, auth, content) {
122
156
  return BAD_REQUEST(`signature: ${verdict.reason}`);
123
157
  // Store the server-recomputed canonical hash (verified equal above).
124
158
  content.contentHash = computeContentHash(content);
159
+ // Within one Harper process, three per-key locks serialize a send's
160
+ // check-and-write, and are always acquired in ONE fixed order — message id,
161
+ // then recipient, then sender — so two sends can never deadlock: a send only
162
+ // ever waits for a lock that comes AFTER every lock it already holds, so no
163
+ // wait-for cycle exists. The id lock serializes id ownership; the recipient
164
+ // lock serializes the recipient-wide inbox cap; the sender lock serializes
165
+ // content dedup, thread sequence and the per-sender sub-cap. Cross-process
166
+ // coordination is out of scope: the lock is ONE Harper process's
167
+ // primary-store lock (flair#2333/#2461/#2462).
168
+ const store = deps.messages?.primaryStore;
169
+ const outcome = await withKeyLocks(store, [
170
+ [SEND_LOCK_NAMESPACE, "id", String(content.id)],
171
+ [SEND_LOCK_NAMESPACE, "recipient", String(content.to)],
172
+ [SEND_LOCK_NAMESPACE, "from", String(content.from)],
173
+ ], () => withOwnedTransaction(deps.ctx, (owned) => storeAndDeliver(deps, content, owned)), SEND_LOCK_ATTEMPTS, SEND_LOCK_WAIT_MS);
174
+ if (outcome.kind === "busy")
175
+ return SEND_BUSY();
176
+ if (outcome.kind === "unavailable")
177
+ return SEND_LOCK_UNAVAILABLE();
178
+ // Test-only: pause after the send locks release and the owned transaction
179
+ // commits, before this request's transaction commits (flair#2333).
180
+ const committed = txnPausePoint("relay-send-lock-released");
181
+ if (committed)
182
+ await committed;
183
+ return outcome.value;
184
+ }
185
+ /**
186
+ * The delivery half of relaySend, run while holding the send locks (the
187
+ * message id's, the recipient's and the sender's): the id-ownership answer, the
188
+ * content dedup, the per-thread seq check, the recipient-wide cap check and the
189
+ * store. Extracted so the whole check-and-write runs under those locks; it is
190
+ * never called outside them.
191
+ */
192
+ async function storeAndDeliver(deps, content, ctx) {
125
193
  // Retry-dedup: an identical resend — same primary id, or the same content
126
194
  // hash from this sender to this recipient — returns the already-accepted
127
195
  // envelope. No second row, and (critically) no second charge against the cap.
@@ -137,7 +205,7 @@ export async function relaySend(deps, auth, content) {
137
205
  // cannot be read, the send is refused the same way, never written over it.
138
206
  let existingById;
139
207
  try {
140
- existingById = (await Promise.resolve(deps.messages.get(String(content.id)))) ?? null;
208
+ existingById = (await Promise.resolve(deps.messages.get(String(content.id), ctx))) ?? null;
141
209
  }
142
210
  catch {
143
211
  return ownerMutationRefusal("Message");
@@ -150,7 +218,7 @@ export async function relaySend(deps, auth, content) {
150
218
  }
151
219
  // The recipient's inbox — queried by the @indexed `to`, never the whole table
152
220
  // (Sherlock P0). Serves both dedup (same content from this sender) and the cap.
153
- const recipientRows = await queryRows(deps.messages, [{ attribute: "to", value: content.to }]);
221
+ const recipientRows = await queryRows(deps.messages, [{ attribute: "to", value: content.to }], ctx);
154
222
  const dup = recipientRows.find((r) => r.from === content.from && r.to === content.to && r.contentHash === content.contentHash);
155
223
  if (dup)
156
224
  return acceptedView(dup);
@@ -158,7 +226,7 @@ export async function relaySend(deps, auth, content) {
158
226
  // STRICTLY ahead of the thread's last seq (Sherlock P1). Checked AFTER dedup so
159
227
  // a legitimate retry (same content, caught above) is never rejected. Queried by
160
228
  // the @indexed `threadId`, then filtered to this sender in-code.
161
- const threadRows = await queryRows(deps.messages, [{ attribute: "threadId", value: content.threadId }]);
229
+ const threadRows = await queryRows(deps.messages, [{ attribute: "threadId", value: content.threadId }], ctx);
162
230
  let prevMaxSeq = -1;
163
231
  for (const r of threadRows) {
164
232
  if (r.from !== content.from || r.threadId !== content.threadId)
@@ -194,7 +262,11 @@ export async function relaySend(deps, auth, content) {
194
262
  state: "delivered",
195
263
  deliveredAt: now,
196
264
  };
197
- await deps.messages.put(row);
265
+ // Test-only: pause between the checks above and the store below.
266
+ const pause = txnPausePoint("relay-send-write");
267
+ if (pause)
268
+ await pause;
269
+ await deps.messages.put(row, ctx);
198
270
  return acceptedView(row);
199
271
  }
200
272
  // ─── inbox ──────────────────────────────────────────────────────────────────