@tangle-network/agent-app 0.61.3 → 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 (308) hide show
  1. package/dist/{DesignCanvas-L6J72JQ2.js → DesignCanvas-IQKYAEC4.js} +3 -2
  2. package/dist/{DesignCanvasEditor-QFQD63MR.js → DesignCanvasEditor-O7OCZTGC.js} +5 -4
  3. package/dist/{TimelineEditor-7KBPJQLT.js → TimelineEditor-VB2UJ34D.js} +4 -2
  4. package/dist/VaultPane-AYW7CO33.js +8 -0
  5. package/dist/agent-profiles/d1.js +1 -1
  6. package/dist/agent-profiles/index.d.ts +0 -82
  7. package/dist/agent-profiles/index.js +1 -21
  8. package/dist/agent-profiles/self-management.d.ts +0 -55
  9. package/dist/alerting/index.d.ts +1 -1
  10. package/dist/alerting/index.js +1 -3
  11. package/dist/alerting/slack.d.ts +6 -79
  12. package/dist/app-auth/drizzle.d.ts +17 -271
  13. package/dist/app-auth/drizzle.js +6 -2
  14. package/dist/app-auth/drizzle.js.map +1 -1
  15. package/dist/app-auth/index.js +3 -3
  16. package/dist/assets/types.d.ts +2 -1
  17. package/dist/assistant/AssistantPanel.d.ts +1 -1
  18. package/dist/assistant/index.js +635 -392
  19. package/dist/assistant/index.js.map +1 -1
  20. package/dist/billing/identity-locks.d.ts +5 -0
  21. package/dist/billing/index.js +1 -1
  22. package/dist/brand-extraction/index.js.map +1 -1
  23. package/dist/brand-extraction/types.d.ts +2 -1
  24. package/dist/chat-react/index.js +6 -5
  25. package/dist/chat-routes/attachment-write-safety.d.ts +4 -0
  26. package/dist/chat-routes/draft-persistence.d.ts +2 -14
  27. package/dist/chat-routes/index.d.ts +1 -0
  28. package/dist/chat-routes/index.js +853 -773
  29. package/dist/chat-routes/index.js.map +1 -1
  30. package/dist/chat-routes/sandbox-model-source.d.ts +20 -0
  31. package/dist/chat-routes/stored-attachments.d.ts +18 -0
  32. package/dist/chat-routes/turn-admission.d.ts +1 -1
  33. package/dist/chat-routes/turn-execution.d.ts +2 -2
  34. package/dist/chat-routes/turn-route-contracts.d.ts +3 -29
  35. package/dist/chat-store/core.d.ts +19 -1
  36. package/dist/chat-store/index.js.map +1 -1
  37. package/dist/chat-store/store.d.ts +3 -29
  38. package/dist/{chunk-ATQQCG4I.js → chunk-2VJLLRVV.js} +2 -2
  39. package/dist/{chunk-R4X5474U.js → chunk-52SDU43B.js} +12 -27
  40. package/dist/chunk-52SDU43B.js.map +1 -0
  41. package/dist/{chunk-MDRH3SDA.js → chunk-54YIL7WM.js} +3 -3
  42. package/dist/{chunk-6QJ3GB3A.js → chunk-5GSZGTPJ.js} +2 -2
  43. package/dist/{chunk-5IM2CLYX.js → chunk-6JEDSKFH.js} +232 -218
  44. package/dist/chunk-6JEDSKFH.js.map +1 -0
  45. package/dist/{chunk-6PRQ45QE.js → chunk-6OG4244Y.js} +1 -1
  46. package/dist/chunk-6OG4244Y.js.map +1 -0
  47. package/dist/{chunk-ZDPUC6CM.js → chunk-6ZVSMA4T.js} +3 -3
  48. package/dist/{chunk-TXUAGSFR.js → chunk-76MPX7JQ.js} +47 -1
  49. package/dist/chunk-76MPX7JQ.js.map +1 -0
  50. package/dist/{chunk-BOQ4HVVH.js → chunk-7ISHKBAD.js} +61 -52
  51. package/dist/chunk-7ISHKBAD.js.map +1 -0
  52. package/dist/chunk-7PULOECS.js +22 -0
  53. package/dist/chunk-7PULOECS.js.map +1 -0
  54. package/dist/{chunk-KJODHPWM.js → chunk-CPVGRQ43.js} +511 -339
  55. package/dist/chunk-CPVGRQ43.js.map +1 -0
  56. package/dist/{chunk-LO6VQ35O.js → chunk-CQZVJVME.js} +2 -2
  57. package/dist/{chunk-Y43PXON2.js → chunk-EFQUAWWF.js} +577 -546
  58. package/dist/chunk-EFQUAWWF.js.map +1 -0
  59. package/dist/{chunk-FPNZMIAW.js → chunk-ELX5YITO.js} +1 -1
  60. package/dist/chunk-ELX5YITO.js.map +1 -0
  61. package/dist/chunk-FGAROAUF.js +19 -0
  62. package/dist/chunk-FGAROAUF.js.map +1 -0
  63. package/dist/{chunk-HN6MZ2XL.js → chunk-FTNGGR37.js} +641 -466
  64. package/dist/chunk-FTNGGR37.js.map +1 -0
  65. package/dist/{chunk-G4F2NWZH.js → chunk-FZ3ZFVAC.js} +4 -4
  66. package/dist/{chunk-UCCSJX64.js → chunk-GIEUZBQQ.js} +1112 -644
  67. package/dist/chunk-GIEUZBQQ.js.map +1 -0
  68. package/dist/{chunk-MEKOWH5E.js → chunk-IVBREL7A.js} +785 -526
  69. package/dist/chunk-IVBREL7A.js.map +1 -0
  70. package/dist/{chunk-WSEXFTN4.js → chunk-JBXNU5WD.js} +26 -26
  71. package/dist/chunk-JBXNU5WD.js.map +1 -0
  72. package/dist/{chunk-F2VX6TTJ.js → chunk-JRCZHDRG.js} +2 -41
  73. package/dist/chunk-JRCZHDRG.js.map +1 -0
  74. package/dist/{chunk-LXYQOOXU.js → chunk-JRW6LCIA.js} +552 -302
  75. package/dist/chunk-JRW6LCIA.js.map +1 -0
  76. package/dist/{chunk-TVPH5T4F.js → chunk-KS4I7KVC.js} +16 -22
  77. package/dist/{chunk-TVPH5T4F.js.map → chunk-KS4I7KVC.js.map} +1 -1
  78. package/dist/{chunk-J2BBYVGF.js → chunk-MRZP65DZ.js} +1 -117
  79. package/dist/chunk-MRZP65DZ.js.map +1 -0
  80. package/dist/{chunk-4ZSLX3LY.js → chunk-OODZCBSM.js} +1 -1
  81. package/dist/chunk-OODZCBSM.js.map +1 -0
  82. package/dist/chunk-P4RRC4AO.js +47 -0
  83. package/dist/chunk-P4RRC4AO.js.map +1 -0
  84. package/dist/{chunk-ORQD4AUO.js → chunk-PCAY23GS.js} +1 -6
  85. package/dist/chunk-PCAY23GS.js.map +1 -0
  86. package/dist/{chunk-NMZUW6VJ.js → chunk-PEJ5OP5B.js} +3 -3
  87. package/dist/chunk-PEJ5OP5B.js.map +1 -0
  88. package/dist/{chunk-NHYRIM4I.js → chunk-PIPNDZ6H.js} +304 -194
  89. package/dist/chunk-PIPNDZ6H.js.map +1 -0
  90. package/dist/{chunk-DJIN3YUJ.js → chunk-TCJ4TMOW.js} +83 -47
  91. package/dist/{chunk-DJIN3YUJ.js.map → chunk-TCJ4TMOW.js.map} +1 -1
  92. package/dist/chunk-UHSUY6H6.js +79 -0
  93. package/dist/chunk-UHSUY6H6.js.map +1 -0
  94. package/dist/{chunk-PDD5A43Y.js → chunk-UTL6TEN6.js} +3 -3
  95. package/dist/{chunk-VJBFCNAU.js → chunk-UW2DKTEM.js} +123 -130
  96. package/dist/chunk-UW2DKTEM.js.map +1 -0
  97. package/dist/{chunk-S27DKRI6.js → chunk-VDYEVCQQ.js} +2 -2
  98. package/dist/{chunk-UFQM46NV.js → chunk-VK3F4N4U.js} +2 -2
  99. package/dist/{chunk-6ZIC6OG5.js → chunk-VYBTQK25.js} +126 -122
  100. package/dist/chunk-VYBTQK25.js.map +1 -0
  101. package/dist/{chunk-RL3Z747H.js → chunk-W2BV2XOC.js} +1491 -700
  102. package/dist/chunk-W2BV2XOC.js.map +1 -0
  103. package/dist/{chunk-FUZETA6Z.js → chunk-X6SZGQTK.js} +2 -2
  104. package/dist/{chunk-CD2LQXY6.js → chunk-XOHE6SKU.js} +14 -16
  105. package/dist/{chunk-CD2LQXY6.js.map → chunk-XOHE6SKU.js.map} +1 -1
  106. package/dist/{chunk-ZBB5KQ6B.js → chunk-Y3GO6SG3.js} +73 -66
  107. package/dist/chunk-Y3GO6SG3.js.map +1 -0
  108. package/dist/chunk-ZAMHLXNS.js +56 -0
  109. package/dist/chunk-ZAMHLXNS.js.map +1 -0
  110. package/dist/{chunk-RHFN7TUV.js → chunk-ZDHCV462.js} +47 -44
  111. package/dist/{chunk-RHFN7TUV.js.map → chunk-ZDHCV462.js.map} +1 -1
  112. package/dist/chunk-ZOS3D26Z.js +72 -0
  113. package/dist/chunk-ZOS3D26Z.js.map +1 -0
  114. package/dist/design-canvas/index.js +32 -32
  115. package/dist/design-canvas/index.js.map +1 -1
  116. package/dist/design-canvas-react/components/DesignCanvas.d.ts +1 -1
  117. package/dist/design-canvas-react/components/Workspace.d.ts +1 -1
  118. package/dist/design-canvas-react/components/glyphs.d.ts +31 -40
  119. package/dist/design-canvas-react/components/transform-math.d.ts +0 -8
  120. package/dist/design-canvas-react/components/use-workspace-keyboard.d.ts +2 -2
  121. package/dist/design-canvas-react/contracts.d.ts +2 -2
  122. package/dist/design-canvas-react/engine.js +1 -1
  123. package/dist/design-canvas-react/index.js +9 -7
  124. package/dist/design-canvas-react/index.js.map +1 -1
  125. package/dist/design-canvas-react/lazy.js +1 -1
  126. package/dist/eval/calibration.d.ts +1 -38
  127. package/dist/eval/index.d.ts +2 -4
  128. package/dist/eval/index.js +0 -84
  129. package/dist/eval/index.js.map +1 -1
  130. package/dist/harness/index.d.ts +1 -1
  131. package/dist/harness/index.js +1 -1
  132. package/dist/harness/session-selection.d.ts +2 -2
  133. package/dist/hub-approvals/index.js +1 -1
  134. package/dist/hub-approvals/result.d.ts +1 -1
  135. package/dist/hub-approvals-react/index.js +4 -4
  136. package/dist/hub-approvals-react/index.js.map +1 -1
  137. package/dist/hub-approvals-react/parts.d.ts +1 -1
  138. package/dist/launch-invariants/cli.js +4 -4
  139. package/dist/launch-invariants/index.js +3 -3
  140. package/dist/launch-invariants/testing.js +4 -4
  141. package/dist/legibility/cli.js +1 -1
  142. package/dist/legibility/index.js +1 -1
  143. package/dist/{mention-editor-B23OMFT2.js → mention-editor-4BKBMWIK.js} +83 -57
  144. package/dist/mention-editor-4BKBMWIK.js.map +1 -0
  145. package/dist/missions/index.js +144 -137
  146. package/dist/missions/index.js.map +1 -1
  147. package/dist/operator/index.js +1 -1
  148. package/dist/peer-floors/check.js +1 -1
  149. package/dist/peer-floors/cli.js +6 -14
  150. package/dist/peer-floors/cli.js.map +1 -1
  151. package/dist/peer-floors/source-entries.d.ts +6 -0
  152. package/dist/platform/index.js +10 -10
  153. package/dist/preset-cloudflare/index.js +1 -1
  154. package/dist/profile/index.d.ts +0 -1
  155. package/dist/profile/index.js +1 -422
  156. package/dist/profile/index.js.map +1 -1
  157. package/dist/profile-editor/server.d.ts +0 -1
  158. package/dist/profile-editor/server.js +0 -1
  159. package/dist/profile-editor/server.js.map +1 -1
  160. package/dist/public-consultation/index.d.ts +2 -2
  161. package/dist/public-consultation/index.js +6 -6
  162. package/dist/record/drizzle.js +156 -148
  163. package/dist/record/drizzle.js.map +1 -1
  164. package/dist/runtime/index.js +4 -4
  165. package/dist/runtime/router-turn.js +3 -3
  166. package/dist/sandbox/box-health.d.ts +1 -1
  167. package/dist/sandbox/index.js +5 -5
  168. package/dist/sequences/drizzle.js +5 -15
  169. package/dist/sequences/drizzle.js.map +1 -1
  170. package/dist/sequences/index.d.ts +1 -0
  171. package/dist/sequences/index.js +18 -14
  172. package/dist/sequences/index.js.map +1 -1
  173. package/dist/sequences/row-mapping.d.ts +7 -0
  174. package/dist/sequences-react/components/TimelineEditor.d.ts +0 -40
  175. package/dist/sequences-react/components/TimelineHistory.d.ts +31 -0
  176. package/dist/sequences-react/components/glyphs.d.ts +12 -21
  177. package/dist/sequences-react/components/index.d.ts +0 -2
  178. package/dist/sequences-react/components/interaction-math.d.ts +3 -3
  179. package/dist/sequences-react/contracts.d.ts +1 -1
  180. package/dist/sequences-react/engine/snap.d.ts +2 -2
  181. package/dist/sequences-react/index.js +4 -4
  182. package/dist/sequences-react/index.js.map +1 -1
  183. package/dist/settings-react/index.js +13 -30
  184. package/dist/settings-react/index.js.map +1 -1
  185. package/dist/settings-react/timezone-picker.d.ts +0 -6
  186. package/dist/signoff/browser-budget.d.ts +6 -1
  187. package/dist/signoff/cli.d.ts +0 -7
  188. package/dist/signoff/cli.js +1 -2
  189. package/dist/signoff/cli.js.map +1 -1
  190. package/dist/signoff/index.js +1 -1
  191. package/dist/skills/index.d.ts +1 -1
  192. package/dist/skills/index.js +5 -25
  193. package/dist/skills/marketplace.d.ts +1 -75
  194. package/dist/skills-placement/box.js +3 -4
  195. package/dist/skills-placement/box.js.map +1 -1
  196. package/dist/skills-placement/index.js +3 -4
  197. package/dist/spend/cli-args.d.ts +1 -1
  198. package/dist/spend/cli.js +3 -3
  199. package/dist/spend/cli.js.map +1 -1
  200. package/dist/spend/index.js +1 -1
  201. package/dist/stories/design-canvas/fixtures.d.ts +1 -14
  202. package/dist/stream/index.js +17 -17
  203. package/dist/studio/generation.d.ts +1 -1
  204. package/dist/studio/index.js +1 -1
  205. package/dist/studio-react/index.js +353 -184
  206. package/dist/studio-react/index.js.map +1 -1
  207. package/dist/studio-react/studio-composer.d.ts +1 -5
  208. package/dist/studio-react/studio-history-screen.d.ts +1 -1
  209. package/dist/teams/invitations-api.d.ts +3 -3
  210. package/dist/teams/invitations-api.js +74 -72
  211. package/dist/teams/invitations-api.js.map +1 -1
  212. package/dist/teams/member-sync.d.ts +2 -0
  213. package/dist/teams/members-api.d.ts +2 -2
  214. package/dist/teams/members-api.js +1 -1
  215. package/dist/teams-react/index.js +28 -45
  216. package/dist/teams-react/index.js.map +1 -1
  217. package/dist/tools/index.js +1 -1
  218. package/dist/tools/mcp-arguments.d.ts +1 -1
  219. package/dist/trace/index.d.ts +1 -18
  220. package/dist/trace/index.js +1 -36
  221. package/dist/trace/index.js.map +1 -1
  222. package/dist/trace/mission-flow.d.ts +1 -59
  223. package/dist/turn-health/index.d.ts +1 -1
  224. package/dist/turn-health/index.js +2 -2
  225. package/dist/turn-health/sink.d.ts +2 -1
  226. package/dist/turn-trace/index.js +2 -4
  227. package/dist/turn-trace/recorder.d.ts +0 -2
  228. package/dist/vault/VaultPane.d.ts +0 -20
  229. package/dist/vault/VaultPaneOperations.d.ts +318 -0
  230. package/dist/vault/index.js +2 -1
  231. package/dist/vault/index.js.map +1 -1
  232. package/dist/vault/lazy.js +1 -1
  233. package/dist/web-react/agent-profile-editor.d.ts +1 -2
  234. package/dist/web-react/agent-profile-editor.js +6 -5
  235. package/dist/web-react/chat-composer.d.ts +1 -1
  236. package/dist/web-react/durable-plan-flow.d.ts +6 -1
  237. package/dist/web-react/editor-glyphs.d.ts +12 -0
  238. package/dist/web-react/focus-navigation.d.ts +12 -0
  239. package/dist/web-react/history-controls.d.ts +37 -0
  240. package/dist/web-react/index.js +36 -34
  241. package/dist/web-react/interaction-question-card.d.ts +1 -1
  242. package/dist/web-react/record-grid-writes.d.ts +31 -0
  243. package/dist/web-react/record-grid.d.ts +1 -48
  244. package/dist/web-react/session-history-panel.d.ts +1 -9
  245. package/dist/web-react/use-document-dark.d.ts +1 -0
  246. package/dist/workspace-export/engine.d.ts +1 -6
  247. package/dist/workspace-export/index.js +198 -185
  248. package/dist/workspace-export/index.js.map +1 -1
  249. package/dist/workspace-export/routes.d.ts +1 -1
  250. package/dist/workspace-react/index.js +9 -8
  251. package/dist/workspace-react/index.js.map +1 -1
  252. package/package.json +7 -17
  253. package/dist/VaultPane-RXL3WLA2.js +0 -7
  254. package/dist/agent-profiles/reaper.d.ts +0 -31
  255. package/dist/agent-profiles/reaper.js +0 -29
  256. package/dist/agent-profiles/reaper.js.map +0 -1
  257. package/dist/chunk-4ZSLX3LY.js.map +0 -1
  258. package/dist/chunk-5IM2CLYX.js.map +0 -1
  259. package/dist/chunk-6OSJNJA2.js +0 -251
  260. package/dist/chunk-6OSJNJA2.js.map +0 -1
  261. package/dist/chunk-6PRQ45QE.js.map +0 -1
  262. package/dist/chunk-6ZIC6OG5.js.map +0 -1
  263. package/dist/chunk-BDEBQ4WO.js +0 -38
  264. package/dist/chunk-BDEBQ4WO.js.map +0 -1
  265. package/dist/chunk-BOQ4HVVH.js.map +0 -1
  266. package/dist/chunk-F2VX6TTJ.js.map +0 -1
  267. package/dist/chunk-FPNZMIAW.js.map +0 -1
  268. package/dist/chunk-HN6MZ2XL.js.map +0 -1
  269. package/dist/chunk-J2BBYVGF.js.map +0 -1
  270. package/dist/chunk-KJODHPWM.js.map +0 -1
  271. package/dist/chunk-LTWJYNEC.js +0 -100
  272. package/dist/chunk-LTWJYNEC.js.map +0 -1
  273. package/dist/chunk-LXYQOOXU.js.map +0 -1
  274. package/dist/chunk-MEKOWH5E.js.map +0 -1
  275. package/dist/chunk-NHYRIM4I.js.map +0 -1
  276. package/dist/chunk-NMZUW6VJ.js.map +0 -1
  277. package/dist/chunk-ORQD4AUO.js.map +0 -1
  278. package/dist/chunk-R4X5474U.js.map +0 -1
  279. package/dist/chunk-RL3Z747H.js.map +0 -1
  280. package/dist/chunk-TXUAGSFR.js.map +0 -1
  281. package/dist/chunk-UCCSJX64.js.map +0 -1
  282. package/dist/chunk-VJBFCNAU.js.map +0 -1
  283. package/dist/chunk-WSEXFTN4.js.map +0 -1
  284. package/dist/chunk-Y43PXON2.js.map +0 -1
  285. package/dist/chunk-ZBB5KQ6B.js.map +0 -1
  286. package/dist/eval/parity.d.ts +0 -74
  287. package/dist/mention-editor-B23OMFT2.js.map +0 -1
  288. package/dist/profile/humanizer-skill.d.ts +0 -23
  289. package/dist/profile/humanizer-skill.vendored.d.ts +0 -4
  290. package/dist/sequences-react/components/BrandMark.d.ts +0 -10
  291. package/dist/skills/marketplace-drizzle.d.ts +0 -121
  292. package/dist/skills/marketplace-drizzle.js +0 -34
  293. package/dist/skills/marketplace-drizzle.js.map +0 -1
  294. package/dist/teams-react/components/BrandMark.d.ts +0 -10
  295. /package/dist/{DesignCanvas-L6J72JQ2.js.map → DesignCanvas-IQKYAEC4.js.map} +0 -0
  296. /package/dist/{DesignCanvasEditor-QFQD63MR.js.map → DesignCanvasEditor-O7OCZTGC.js.map} +0 -0
  297. /package/dist/{TimelineEditor-7KBPJQLT.js.map → TimelineEditor-VB2UJ34D.js.map} +0 -0
  298. /package/dist/{VaultPane-RXL3WLA2.js.map → VaultPane-AYW7CO33.js.map} +0 -0
  299. /package/dist/{chunk-ATQQCG4I.js.map → chunk-2VJLLRVV.js.map} +0 -0
  300. /package/dist/{chunk-MDRH3SDA.js.map → chunk-54YIL7WM.js.map} +0 -0
  301. /package/dist/{chunk-6QJ3GB3A.js.map → chunk-5GSZGTPJ.js.map} +0 -0
  302. /package/dist/{chunk-ZDPUC6CM.js.map → chunk-6ZVSMA4T.js.map} +0 -0
  303. /package/dist/{chunk-LO6VQ35O.js.map → chunk-CQZVJVME.js.map} +0 -0
  304. /package/dist/{chunk-G4F2NWZH.js.map → chunk-FZ3ZFVAC.js.map} +0 -0
  305. /package/dist/{chunk-PDD5A43Y.js.map → chunk-UTL6TEN6.js.map} +0 -0
  306. /package/dist/{chunk-S27DKRI6.js.map → chunk-VDYEVCQQ.js.map} +0 -0
  307. /package/dist/{chunk-UFQM46NV.js.map → chunk-VK3F4N4U.js.map} +0 -0
  308. /package/dist/{chunk-FUZETA6Z.js.map → chunk-X6SZGQTK.js.map} +0 -0
@@ -2,6 +2,93 @@ import {
2
2
  assertWorkspaceKeyCryptoUsable
3
3
  } from "./chunk-VGS2NXLB.js";
4
4
 
5
+ // src/billing/identity-locks.ts
6
+ function createWorkspaceKeyLocks(leaseWaitMs, options, now, leaseMs, leasePollMs, leaseRenewIntervalMs, operationId2, sleep2) {
7
+ const localLocks = /* @__PURE__ */ new Map();
8
+ async function withLocalLock(scope, work) {
9
+ const previous = localLocks.get(scope) ?? Promise.resolve();
10
+ let release;
11
+ const current = new Promise((resolve) => {
12
+ release = resolve;
13
+ });
14
+ localLocks.set(scope, current);
15
+ await previous;
16
+ try {
17
+ return await work();
18
+ } finally {
19
+ release();
20
+ if (localLocks.get(scope) === current) localLocks.delete(scope);
21
+ }
22
+ }
23
+ async function withDurableLease(scope, work, onLeaseLost) {
24
+ const leaseOperationId = operationId2();
25
+ const startedAt = Date.now();
26
+ let acquired = false;
27
+ while (!acquired && Date.now() - startedAt <= leaseWaitMs) {
28
+ acquired = await options.store.acquireLease(scope, leaseOperationId, now(), leaseMs);
29
+ if (!acquired) await sleep2(leasePollMs);
30
+ }
31
+ if (!acquired) throw new Error("workspace child-key issuance is busy; retry the request");
32
+ let stopped = false;
33
+ let leaseLost = null;
34
+ let leaseTimer;
35
+ let renewalPromise;
36
+ const scheduleRenewal = () => {
37
+ if (stopped || leaseLost) return;
38
+ leaseTimer = setTimeout(() => {
39
+ leaseTimer = void 0;
40
+ const renewal = (async () => {
41
+ try {
42
+ if (!await options.store.renewLease(scope, leaseOperationId, now(), leaseMs)) {
43
+ leaseLost = new Error("workspace child-key lease was lost during reconciliation");
44
+ return;
45
+ }
46
+ scheduleRenewal();
47
+ } catch (error) {
48
+ leaseLost = error instanceof Error ? error : new Error("workspace child-key lease renewal failed");
49
+ }
50
+ })();
51
+ renewalPromise = renewal;
52
+ void renewal.finally(() => {
53
+ if (renewalPromise === renewal) renewalPromise = void 0;
54
+ }).catch(() => void 0);
55
+ }, leaseRenewIntervalMs);
56
+ };
57
+ scheduleRenewal();
58
+ let workError;
59
+ let leaseLossCleanupError;
60
+ try {
61
+ const result = await work();
62
+ if (leaseLost) throw leaseLost;
63
+ return result;
64
+ } catch (error) {
65
+ workError = error;
66
+ throw error;
67
+ } finally {
68
+ stopped = true;
69
+ if (leaseTimer) clearTimeout(leaseTimer);
70
+ if (renewalPromise) await renewalPromise;
71
+ if (leaseLost && onLeaseLost) {
72
+ try {
73
+ await onLeaseLost();
74
+ } catch (error) {
75
+ leaseLossCleanupError = error;
76
+ }
77
+ }
78
+ try {
79
+ await options.store.releaseLease(scope, leaseOperationId);
80
+ } catch (error) {
81
+ if (workError === void 0) throw error;
82
+ }
83
+ if (workError === void 0 && leaseLossCleanupError) {
84
+ throw new Error("workspace child-key lease was lost and cleanup failed", { cause: leaseLossCleanupError });
85
+ }
86
+ if (workError === void 0 && leaseLost) throw leaseLost;
87
+ }
88
+ }
89
+ return { withLocalLock, withDurableLease };
90
+ }
91
+
5
92
  // src/billing/identity-bound.ts
6
93
  var DEFAULT_KEY_LIFETIME_MS = 55 * 6e4;
7
94
  var DEFAULT_LEASE_MS = 6e4;
@@ -122,88 +209,155 @@ function createIdentityBoundWorkspaceKeyManager(options) {
122
209
  const now = options.now ?? (() => /* @__PURE__ */ new Date());
123
210
  const isRemoteMissing = options.isRemoteMissing ?? defaultRemoteMissing;
124
211
  const recoveryProvisioner = options.recoveryProvisioner ?? options.provisioner;
125
- const localLocks = /* @__PURE__ */ new Map();
126
- async function withLocalLock(scope, work) {
127
- const previous = localLocks.get(scope) ?? Promise.resolve();
128
- let release;
129
- const current = new Promise((resolve) => {
130
- release = resolve;
212
+ const { withLocalLock, withDurableLease } = createWorkspaceKeyLocks(leaseWaitMs, options, now, leaseMs, leasePollMs, leaseRenewIntervalMs, operationId, sleep);
213
+ const { revokePending, markPending, cleanupProvisioning, reconcileProvisioning } = createWorkspaceKeyRecovery(revocationRetryMaxMs, revocationRetryBaseMs, options, now, recoveryProvisioner, isRemoteMissing, product);
214
+ async function retryPendingRevocationsUnlocked(scopeFilter, activeIdentity) {
215
+ const rows = await options.store.listPendingRevocations({
216
+ product,
217
+ workspaceId: scopeFilter?.workspaceId,
218
+ ownerUserId: scopeFilter?.ownerUserId,
219
+ now: now(),
220
+ limit: PENDING_REVOCATION_BATCH_SIZE
131
221
  });
132
- localLocks.set(scope, current);
133
- await previous;
134
- try {
135
- return await work();
136
- } finally {
137
- release();
138
- if (localLocks.get(scope) === current) localLocks.delete(scope);
222
+ let completed = 0;
223
+ let pending = 0;
224
+ for (const row of rows) {
225
+ if (row.product !== product) continue;
226
+ if (scopeFilter && (row.workspaceId !== scopeFilter.workspaceId || row.ownerUserId !== scopeFilter.ownerUserId)) continue;
227
+ if (await revokePending(row, activeIdentity)) completed += 1;
228
+ else pending += 1;
139
229
  }
230
+ return { completed, pending };
140
231
  }
141
- async function withDurableLease(scope, work, onLeaseLost) {
142
- const leaseOperationId = operationId();
143
- const startedAt = Date.now();
144
- let acquired = false;
145
- while (!acquired && Date.now() - startedAt <= leaseWaitMs) {
146
- acquired = await options.store.acquireLease(scope, leaseOperationId, now(), leaseMs);
147
- if (!acquired) await sleep(leasePollMs);
232
+ async function assertNoPendingRevocations(identity) {
233
+ const rows = await options.store.listPendingRevocations({
234
+ product,
235
+ workspaceId: identity.workspaceId,
236
+ ownerUserId: identity.ownerUserId,
237
+ now: now(),
238
+ limit: PENDING_REVOCATION_BATCH_SIZE,
239
+ includeFuture: true
240
+ });
241
+ if (rows.some((row) => row.product === product && row.workspaceId === identity.workspaceId && row.ownerUserId === identity.ownerUserId)) {
242
+ throw new Error("a previous workspace child-key cleanup is pending; issuance is blocked until it completes");
148
243
  }
149
- if (!acquired) throw new Error("workspace child-key issuance is busy; retry the request");
150
- let stopped = false;
151
- let leaseLost = null;
152
- let leaseTimer;
153
- let renewalPromise;
154
- const scheduleRenewal = () => {
155
- if (stopped || leaseLost) return;
156
- leaseTimer = setTimeout(() => {
157
- leaseTimer = void 0;
158
- const renewal = (async () => {
159
- try {
160
- if (!await options.store.renewLease(scope, leaseOperationId, now(), leaseMs)) {
161
- leaseLost = new Error("workspace child-key lease was lost during reconciliation");
162
- return;
163
- }
164
- scheduleRenewal();
165
- } catch (error) {
166
- leaseLost = error instanceof Error ? error : new Error("workspace child-key lease renewal failed");
167
- }
168
- })();
169
- renewalPromise = renewal;
170
- void renewal.finally(() => {
171
- if (renewalPromise === renewal) renewalPromise = void 0;
172
- }).catch(() => void 0);
173
- }, leaseRenewIntervalMs);
174
- };
175
- scheduleRenewal();
176
- let workError;
177
- let leaseLossCleanupError;
244
+ }
245
+ async function retryPendingRevocations(scopeInput, identityInput) {
246
+ const identity = identityInput ? normalizeIdentity(identityInput) : void 0;
247
+ if (identity && scopeInput && (identity.workspaceId !== scopeInput.workspaceId || identity.ownerUserId !== scopeInput.ownerUserId)) {
248
+ throw new Error("workspace child key retry identity does not match its scope");
249
+ }
250
+ if (scopeInput) {
251
+ const workspaceId = scopeInput.workspaceId.trim();
252
+ const ownerUserId = scopeInput.ownerUserId.trim();
253
+ if (!workspaceId || !ownerUserId) throw new Error("workspace child key retry scope is incomplete");
254
+ const scope = scopeKey({ workspaceId, ownerUserId, product });
255
+ return withLocalLock(scope, () => withDurableLease(scope, async () => (await retryPendingRevocationsUnlocked({ workspaceId, ownerUserId }, identity)).completed));
256
+ }
257
+ if (identity) {
258
+ const scope = scopeKey(scopeFor(identity, product));
259
+ return withLocalLock(scope, () => withDurableLease(scope, async () => (await retryPendingRevocationsUnlocked({ workspaceId: identity.workspaceId, ownerUserId: identity.ownerUserId }, identity)).completed));
260
+ }
261
+ const rows = await options.store.listPendingRevocations({ product, now: now(), limit: PENDING_REVOCATION_BATCH_SIZE });
262
+ let completed = 0;
263
+ const scopes = /* @__PURE__ */ new Set();
264
+ for (const row of rows) {
265
+ if (row.product !== product) continue;
266
+ const scope = scopeKey({ workspaceId: row.workspaceId, ownerUserId: row.ownerUserId, product });
267
+ if (scopes.has(scope)) continue;
268
+ scopes.add(scope);
269
+ completed += await withLocalLock(scope, () => withDurableLease(scope, async () => (await retryPendingRevocationsUnlocked({
270
+ workspaceId: row.workspaceId,
271
+ ownerUserId: row.ownerUserId
272
+ })).completed));
273
+ }
274
+ return completed;
275
+ }
276
+ async function readActive(identity) {
277
+ const row = await options.store.getActive(scopeFor(identity, product));
278
+ if (!row) return null;
279
+ if (!sameIdentity(row, identity, product)) {
280
+ await options.store.markRevocationPending({
281
+ id: row.id,
282
+ error: "active child key identity does not match the authenticated owner and source",
283
+ nextAttemptAt: now(),
284
+ incrementAttempts: false
285
+ });
286
+ return null;
287
+ }
288
+ let remote;
178
289
  try {
179
- const result = await work();
180
- if (leaseLost) throw leaseLost;
181
- return result;
290
+ remote = await recoveryProvisioner.getKey(row.keyId);
182
291
  } catch (error) {
183
- workError = error;
184
- throw error;
185
- } finally {
186
- stopped = true;
187
- if (leaseTimer) clearTimeout(leaseTimer);
188
- if (renewalPromise) await renewalPromise;
189
- if (leaseLost && onLeaseLost) {
190
- try {
191
- await onLeaseLost();
192
- } catch (error) {
193
- leaseLossCleanupError = error;
194
- }
195
- }
196
- try {
197
- await options.store.releaseLease(scope, leaseOperationId);
198
- } catch (error) {
199
- if (workError === void 0) throw error;
200
- }
201
- if (workError === void 0 && leaseLossCleanupError) {
202
- throw new Error("workspace child-key lease was lost and cleanup failed", { cause: leaseLossCleanupError });
292
+ if (isRemoteMissing(error)) {
293
+ await options.store.markRevoked(row.id, now());
294
+ return null;
203
295
  }
204
- if (workError === void 0 && leaseLost) throw leaseLost;
296
+ throw error;
297
+ }
298
+ const usage = usageFromRemote(row, remote, now());
299
+ if (usage.exhausted) {
300
+ await options.store.markRevocationPending({
301
+ id: row.id,
302
+ error: "active child key is expired or exhausted",
303
+ nextAttemptAt: now(),
304
+ incrementAttempts: false
305
+ });
306
+ return null;
205
307
  }
308
+ return { row, usage };
206
309
  }
310
+ const { mint } = createWorkspaceKeyMinter(options, now, keyLifetimeMs, product, markPending, cleanupProvisioning, revokePending);
311
+ async function ensureKey(identityInput, keyOptions) {
312
+ const identity = normalizeIdentity(identityInput);
313
+ const budgetUsd = keyOptions?.budgetUsd ?? defaultBudgetUsd;
314
+ requirePositive(budgetUsd, "budgetUsd");
315
+ const scope = scopeKey(scopeFor(identity, product));
316
+ let mintedRemote = null;
317
+ return withLocalLock(scope, () => withDurableLease(scope, async () => {
318
+ const initialCleanup = await retryPendingRevocationsUnlocked(identity, identity);
319
+ if (initialCleanup.pending > 0) throw new Error("a previous workspace child-key cleanup is pending; issuance is blocked until it completes");
320
+ await reconcileProvisioning(identity);
321
+ const active = await readActive(identity);
322
+ if (active) {
323
+ const key = await options.crypto.decrypt(active.row.keyEncrypted);
324
+ if (!key.trim()) throw new Error("workspace child key secret is empty");
325
+ if (Date.parse(active.usage.expiresAt) > now().getTime()) return { key, usage: active.usage, refreshed: false };
326
+ await options.store.markRevocationPending({
327
+ id: active.row.id,
328
+ error: "active child key expired while it was read",
329
+ nextAttemptAt: now(),
330
+ incrementAttempts: false
331
+ });
332
+ }
333
+ const finalCleanup = await retryPendingRevocationsUnlocked(identity, identity);
334
+ if (finalCleanup.pending > 0) throw new Error("a previous workspace child-key cleanup is pending; issuance is blocked until it completes");
335
+ await assertNoPendingRevocations(identity);
336
+ return mint(identity, budgetUsd, (row, remoteId) => {
337
+ mintedRemote = { row, remoteId };
338
+ });
339
+ }, async () => {
340
+ const remote = mintedRemote;
341
+ if (!remote) return;
342
+ const cleaned = await revokePending({
343
+ ...remote.row,
344
+ keyId: remote.remoteId,
345
+ status: "revocation_pending"
346
+ }, identity);
347
+ if (!cleaned) throw new Error("lease-loss child-key cleanup is pending");
348
+ }));
349
+ }
350
+ async function getUsage(identityInput) {
351
+ const identity = normalizeIdentity(identityInput);
352
+ const scope = scopeKey(scopeFor(identity, product));
353
+ return withLocalLock(scope, () => withDurableLease(scope, async () => {
354
+ const active = await readActive(identity);
355
+ return active?.usage ?? null;
356
+ }));
357
+ }
358
+ return { ensureKey, getUsage, retryPendingRevocations };
359
+ }
360
+ function createWorkspaceKeyRecovery(revocationRetryMaxMs, revocationRetryBaseMs, options, now, recoveryProvisioner, isRemoteMissing, product) {
207
361
  function retryDelay(attempts) {
208
362
  return Math.min(revocationRetryMaxMs, revocationRetryBaseMs * 2 ** Math.min(Math.max(attempts - 1, 0), 5));
209
363
  }
@@ -344,102 +498,9 @@ function createIdentityBoundWorkspaceKeyManager(options) {
344
498
  }
345
499
  }
346
500
  }
347
- async function retryPendingRevocationsUnlocked(scopeFilter, activeIdentity) {
348
- const rows = await options.store.listPendingRevocations({
349
- product,
350
- workspaceId: scopeFilter?.workspaceId,
351
- ownerUserId: scopeFilter?.ownerUserId,
352
- now: now(),
353
- limit: PENDING_REVOCATION_BATCH_SIZE
354
- });
355
- let completed = 0;
356
- let pending = 0;
357
- for (const row of rows) {
358
- if (row.product !== product) continue;
359
- if (scopeFilter && (row.workspaceId !== scopeFilter.workspaceId || row.ownerUserId !== scopeFilter.ownerUserId)) continue;
360
- if (await revokePending(row, activeIdentity)) completed += 1;
361
- else pending += 1;
362
- }
363
- return { completed, pending };
364
- }
365
- async function assertNoPendingRevocations(identity) {
366
- const rows = await options.store.listPendingRevocations({
367
- product,
368
- workspaceId: identity.workspaceId,
369
- ownerUserId: identity.ownerUserId,
370
- now: now(),
371
- limit: PENDING_REVOCATION_BATCH_SIZE,
372
- includeFuture: true
373
- });
374
- if (rows.some((row) => row.product === product && row.workspaceId === identity.workspaceId && row.ownerUserId === identity.ownerUserId)) {
375
- throw new Error("a previous workspace child-key cleanup is pending; issuance is blocked until it completes");
376
- }
377
- }
378
- async function retryPendingRevocations(scopeInput, identityInput) {
379
- const identity = identityInput ? normalizeIdentity(identityInput) : void 0;
380
- if (identity && scopeInput && (identity.workspaceId !== scopeInput.workspaceId || identity.ownerUserId !== scopeInput.ownerUserId)) {
381
- throw new Error("workspace child key retry identity does not match its scope");
382
- }
383
- if (scopeInput) {
384
- const workspaceId = scopeInput.workspaceId.trim();
385
- const ownerUserId = scopeInput.ownerUserId.trim();
386
- if (!workspaceId || !ownerUserId) throw new Error("workspace child key retry scope is incomplete");
387
- const scope = scopeKey({ workspaceId, ownerUserId, product });
388
- return withLocalLock(scope, () => withDurableLease(scope, async () => (await retryPendingRevocationsUnlocked({ workspaceId, ownerUserId }, identity)).completed));
389
- }
390
- if (identity) {
391
- const scope = scopeKey(scopeFor(identity, product));
392
- return withLocalLock(scope, () => withDurableLease(scope, async () => (await retryPendingRevocationsUnlocked({ workspaceId: identity.workspaceId, ownerUserId: identity.ownerUserId }, identity)).completed));
393
- }
394
- const rows = await options.store.listPendingRevocations({ product, now: now(), limit: PENDING_REVOCATION_BATCH_SIZE });
395
- let completed = 0;
396
- const scopes = /* @__PURE__ */ new Set();
397
- for (const row of rows) {
398
- if (row.product !== product) continue;
399
- const scope = scopeKey({ workspaceId: row.workspaceId, ownerUserId: row.ownerUserId, product });
400
- if (scopes.has(scope)) continue;
401
- scopes.add(scope);
402
- completed += await withLocalLock(scope, () => withDurableLease(scope, async () => (await retryPendingRevocationsUnlocked({
403
- workspaceId: row.workspaceId,
404
- ownerUserId: row.ownerUserId
405
- })).completed));
406
- }
407
- return completed;
408
- }
409
- async function readActive(identity) {
410
- const row = await options.store.getActive(scopeFor(identity, product));
411
- if (!row) return null;
412
- if (!sameIdentity(row, identity, product)) {
413
- await options.store.markRevocationPending({
414
- id: row.id,
415
- error: "active child key identity does not match the authenticated owner and source",
416
- nextAttemptAt: now(),
417
- incrementAttempts: false
418
- });
419
- return null;
420
- }
421
- let remote;
422
- try {
423
- remote = await recoveryProvisioner.getKey(row.keyId);
424
- } catch (error) {
425
- if (isRemoteMissing(error)) {
426
- await options.store.markRevoked(row.id, now());
427
- return null;
428
- }
429
- throw error;
430
- }
431
- const usage = usageFromRemote(row, remote, now());
432
- if (usage.exhausted) {
433
- await options.store.markRevocationPending({
434
- id: row.id,
435
- error: "active child key is expired or exhausted",
436
- nextAttemptAt: now(),
437
- incrementAttempts: false
438
- });
439
- return null;
440
- }
441
- return { row, usage };
442
- }
501
+ return { revokePending, markPending, cleanupProvisioning, reconcileProvisioning };
502
+ }
503
+ function createWorkspaceKeyMinter(options, now, keyLifetimeMs, product, markPending, cleanupProvisioning, revokePending) {
443
504
  async function mint(identity, budgetUsd, onRemoteKey) {
444
505
  await assertWorkspaceKeyCryptoUsable(options.crypto);
445
506
  const mintOperationId = operationId();
@@ -573,54 +634,7 @@ function createIdentityBoundWorkspaceKeyManager(options) {
573
634
  }, now());
574
635
  return { key: secret, usage, refreshed: true };
575
636
  }
576
- async function ensureKey(identityInput, keyOptions) {
577
- const identity = normalizeIdentity(identityInput);
578
- const budgetUsd = keyOptions?.budgetUsd ?? defaultBudgetUsd;
579
- requirePositive(budgetUsd, "budgetUsd");
580
- const scope = scopeKey(scopeFor(identity, product));
581
- let mintedRemote = null;
582
- return withLocalLock(scope, () => withDurableLease(scope, async () => {
583
- const initialCleanup = await retryPendingRevocationsUnlocked(identity, identity);
584
- if (initialCleanup.pending > 0) throw new Error("a previous workspace child-key cleanup is pending; issuance is blocked until it completes");
585
- await reconcileProvisioning(identity);
586
- const active = await readActive(identity);
587
- if (active) {
588
- const key = await options.crypto.decrypt(active.row.keyEncrypted);
589
- if (!key.trim()) throw new Error("workspace child key secret is empty");
590
- if (Date.parse(active.usage.expiresAt) > now().getTime()) return { key, usage: active.usage, refreshed: false };
591
- await options.store.markRevocationPending({
592
- id: active.row.id,
593
- error: "active child key expired while it was read",
594
- nextAttemptAt: now(),
595
- incrementAttempts: false
596
- });
597
- }
598
- const finalCleanup = await retryPendingRevocationsUnlocked(identity, identity);
599
- if (finalCleanup.pending > 0) throw new Error("a previous workspace child-key cleanup is pending; issuance is blocked until it completes");
600
- await assertNoPendingRevocations(identity);
601
- return mint(identity, budgetUsd, (row, remoteId) => {
602
- mintedRemote = { row, remoteId };
603
- });
604
- }, async () => {
605
- const remote = mintedRemote;
606
- if (!remote) return;
607
- const cleaned = await revokePending({
608
- ...remote.row,
609
- keyId: remote.remoteId,
610
- status: "revocation_pending"
611
- }, identity);
612
- if (!cleaned) throw new Error("lease-loss child-key cleanup is pending");
613
- }));
614
- }
615
- async function getUsage(identityInput) {
616
- const identity = normalizeIdentity(identityInput);
617
- const scope = scopeKey(scopeFor(identity, product));
618
- return withLocalLock(scope, () => withDurableLease(scope, async () => {
619
- const active = await readActive(identity);
620
- return active?.usage ?? null;
621
- }));
622
- }
623
- return { ensureKey, getUsage, retryPendingRevocations };
637
+ return { mint };
624
638
  }
625
639
 
626
640
  // src/billing/index.ts
@@ -781,4 +795,4 @@ export {
781
795
  createPlatformBalanceManager,
782
796
  createWorkspaceKeyManager
783
797
  };
784
- //# sourceMappingURL=chunk-5IM2CLYX.js.map
798
+ //# sourceMappingURL=chunk-6JEDSKFH.js.map