@1mcp/agent 0.34.4 → 0.35.0-beta.2

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 (333) hide show
  1. package/README.md +1 -0
  2. package/build/admin/assets/admin-console-C5_zD_qW.css +1 -0
  3. package/build/admin/assets/admin-console-CbHHoqLr.js +61 -0
  4. package/build/admin/index.html +13 -0
  5. package/build/application/services/healthService.d.ts +32 -0
  6. package/build/application/services/healthService.d.ts.map +1 -1
  7. package/build/application/services/healthService.js +49 -0
  8. package/build/application/services/healthService.js.map +1 -1
  9. package/build/commands/admin/admin.d.ts +90 -0
  10. package/build/commands/admin/admin.d.ts.map +1 -0
  11. package/build/commands/admin/admin.js +545 -0
  12. package/build/commands/admin/admin.js.map +1 -0
  13. package/build/commands/admin/index.d.ts +3 -0
  14. package/build/commands/admin/index.d.ts.map +1 -0
  15. package/build/commands/admin/index.js +99 -0
  16. package/build/commands/admin/index.js.map +1 -0
  17. package/build/commands/auth/index.d.ts.map +1 -1
  18. package/build/commands/auth/index.js +24 -5
  19. package/build/commands/auth/index.js.map +1 -1
  20. package/build/commands/auth/login.d.ts +1 -0
  21. package/build/commands/auth/login.d.ts.map +1 -1
  22. package/build/commands/auth/login.js +6 -10
  23. package/build/commands/auth/login.js.map +1 -1
  24. package/build/commands/auth/logout.d.ts +3 -0
  25. package/build/commands/auth/logout.d.ts.map +1 -1
  26. package/build/commands/auth/logout.js +19 -32
  27. package/build/commands/auth/logout.js.map +1 -1
  28. package/build/commands/auth/runtimeTargetContext.d.ts +25 -0
  29. package/build/commands/auth/runtimeTargetContext.d.ts.map +1 -0
  30. package/build/commands/auth/runtimeTargetContext.js +69 -0
  31. package/build/commands/auth/runtimeTargetContext.js.map +1 -0
  32. package/build/commands/auth/status.d.ts +1 -0
  33. package/build/commands/auth/status.d.ts.map +1 -1
  34. package/build/commands/auth/status.js +10 -33
  35. package/build/commands/auth/status.js.map +1 -1
  36. package/build/commands/inspect/index.d.ts.map +1 -1
  37. package/build/commands/inspect/index.js +4 -0
  38. package/build/commands/inspect/index.js.map +1 -1
  39. package/build/commands/inspect/inspect.d.ts +1 -0
  40. package/build/commands/inspect/inspect.d.ts.map +1 -1
  41. package/build/commands/inspect/inspect.js +2 -2
  42. package/build/commands/inspect/inspect.js.map +1 -1
  43. package/build/commands/instructions/index.d.ts.map +1 -1
  44. package/build/commands/instructions/index.js +4 -0
  45. package/build/commands/instructions/index.js.map +1 -1
  46. package/build/commands/instructions/instructions.d.ts +1 -0
  47. package/build/commands/instructions/instructions.d.ts.map +1 -1
  48. package/build/commands/instructions/instructions.js.map +1 -1
  49. package/build/commands/instructions/instructionsUtils.js +1 -1
  50. package/build/commands/instructions/instructionsUtils.js.map +1 -1
  51. package/build/commands/mcp/enable.d.ts +89 -2
  52. package/build/commands/mcp/enable.d.ts.map +1 -1
  53. package/build/commands/mcp/enable.js +555 -35
  54. package/build/commands/mcp/enable.js.map +1 -1
  55. package/build/commands/mcp/index.d.ts.map +1 -1
  56. package/build/commands/mcp/index.js +10 -0
  57. package/build/commands/mcp/index.js.map +1 -1
  58. package/build/commands/mcp/restart.d.ts +69 -0
  59. package/build/commands/mcp/restart.d.ts.map +1 -0
  60. package/build/commands/mcp/restart.js +259 -0
  61. package/build/commands/mcp/restart.js.map +1 -0
  62. package/build/commands/mcp/status.d.ts +24 -1
  63. package/build/commands/mcp/status.d.ts.map +1 -1
  64. package/build/commands/mcp/status.js +158 -14
  65. package/build/commands/mcp/status.js.map +1 -1
  66. package/build/commands/proxy/index.d.ts +1 -0
  67. package/build/commands/proxy/index.d.ts.map +1 -1
  68. package/build/commands/proxy/index.js +4 -0
  69. package/build/commands/proxy/index.js.map +1 -1
  70. package/build/commands/run/index.d.ts.map +1 -1
  71. package/build/commands/run/index.js +4 -0
  72. package/build/commands/run/index.js.map +1 -1
  73. package/build/commands/run/run.d.ts +1 -0
  74. package/build/commands/run/run.d.ts.map +1 -1
  75. package/build/commands/run/run.js +2 -2
  76. package/build/commands/run/run.js.map +1 -1
  77. package/build/commands/serve/index.d.ts +8 -0
  78. package/build/commands/serve/index.d.ts.map +1 -1
  79. package/build/commands/serve/index.js +12 -0
  80. package/build/commands/serve/index.js.map +1 -1
  81. package/build/commands/serve/serve.d.ts +13 -0
  82. package/build/commands/serve/serve.d.ts.map +1 -1
  83. package/build/commands/serve/serve.js +115 -57
  84. package/build/commands/serve/serve.js.map +1 -1
  85. package/build/commands/serve/serveBackground.d.ts +48 -26
  86. package/build/commands/serve/serveBackground.d.ts.map +1 -1
  87. package/build/commands/serve/serveBackground.js +230 -61
  88. package/build/commands/serve/serveBackground.js.map +1 -1
  89. package/build/commands/serve/serveRestart.d.ts +1 -1
  90. package/build/commands/serve/serveRestart.js +1 -1
  91. package/build/commands/serve/serveStatus.d.ts +20 -10
  92. package/build/commands/serve/serveStatus.d.ts.map +1 -1
  93. package/build/commands/serve/serveStatus.js +174 -13
  94. package/build/commands/serve/serveStatus.js.map +1 -1
  95. package/build/commands/serve/serveStop.d.ts +13 -4
  96. package/build/commands/serve/serveStop.d.ts.map +1 -1
  97. package/build/commands/serve/serveStop.js +212 -23
  98. package/build/commands/serve/serveStop.js.map +1 -1
  99. package/build/commands/shared/apiClient.d.ts +5 -1
  100. package/build/commands/shared/apiClient.d.ts.map +1 -1
  101. package/build/commands/shared/apiClient.js +17 -9
  102. package/build/commands/shared/apiClient.js.map +1 -1
  103. package/build/commands/shared/clientSurfaceAttachment.d.ts +14 -0
  104. package/build/commands/shared/clientSurfaceAttachment.d.ts.map +1 -1
  105. package/build/commands/shared/clientSurfaceAttachment.js +51 -9
  106. package/build/commands/shared/clientSurfaceAttachment.js.map +1 -1
  107. package/build/commands/shared/commandRunner.d.ts +3 -1
  108. package/build/commands/shared/commandRunner.d.ts.map +1 -1
  109. package/build/commands/shared/commandRunner.js +59 -2
  110. package/build/commands/shared/commandRunner.js.map +1 -1
  111. package/build/commands/shared/serveTargetResolver.d.ts +14 -1
  112. package/build/commands/shared/serveTargetResolver.d.ts.map +1 -1
  113. package/build/commands/shared/serveTargetResolver.js +145 -3
  114. package/build/commands/shared/serveTargetResolver.js.map +1 -1
  115. package/build/commands/target/index.d.ts +3 -0
  116. package/build/commands/target/index.d.ts.map +1 -0
  117. package/build/commands/target/index.js +171 -0
  118. package/build/commands/target/index.js.map +1 -0
  119. package/build/commands/target/target.d.ts +78 -0
  120. package/build/commands/target/target.d.ts.map +1 -0
  121. package/build/commands/target/target.js +471 -0
  122. package/build/commands/target/target.js.map +1 -0
  123. package/build/config/configManager.d.ts +2 -0
  124. package/build/config/configManager.d.ts.map +1 -1
  125. package/build/config/configManager.js +19 -8
  126. package/build/config/configManager.js.map +1 -1
  127. package/build/config/mcpConfigMerge.d.ts.map +1 -1
  128. package/build/config/mcpConfigMerge.js +13 -2
  129. package/build/config/mcpConfigMerge.js.map +1 -1
  130. package/build/constants/mcp.d.ts +1 -1
  131. package/build/constants/mcp.d.ts.map +1 -1
  132. package/build/constants/mcp.js +1 -1
  133. package/build/constants/mcp.js.map +1 -1
  134. package/build/core/client/clientManager.d.ts +24 -1
  135. package/build/core/client/clientManager.d.ts.map +1 -1
  136. package/build/core/client/clientManager.js +268 -2
  137. package/build/core/client/clientManager.js.map +1 -1
  138. package/build/core/client/connectionHandler.d.ts.map +1 -1
  139. package/build/core/client/connectionHandler.js +18 -1
  140. package/build/core/client/connectionHandler.js.map +1 -1
  141. package/build/core/configChangeHandler.d.ts +1 -0
  142. package/build/core/configChangeHandler.d.ts.map +1 -1
  143. package/build/core/configChangeHandler.js +14 -0
  144. package/build/core/configChangeHandler.js.map +1 -1
  145. package/build/core/loading/mcpLoadingManager.d.ts.map +1 -1
  146. package/build/core/loading/mcpLoadingManager.js +16 -4
  147. package/build/core/loading/mcpLoadingManager.js.map +1 -1
  148. package/build/core/runtime/runtimeIdentityService.d.ts +30 -0
  149. package/build/core/runtime/runtimeIdentityService.d.ts.map +1 -0
  150. package/build/core/runtime/runtimeIdentityService.js +63 -0
  151. package/build/core/runtime/runtimeIdentityService.js.map +1 -0
  152. package/build/core/server/agentConfig.d.ts +4 -0
  153. package/build/core/server/agentConfig.d.ts.map +1 -1
  154. package/build/core/server/agentConfig.js +3 -0
  155. package/build/core/server/agentConfig.js.map +1 -1
  156. package/build/core/server/backendStdioSupervisor.d.ts +73 -0
  157. package/build/core/server/backendStdioSupervisor.d.ts.map +1 -0
  158. package/build/core/server/backendStdioSupervisor.js +191 -0
  159. package/build/core/server/backendStdioSupervisor.js.map +1 -0
  160. package/build/core/server/backgroundLaunchConfig.d.ts +15 -0
  161. package/build/core/server/backgroundLaunchConfig.d.ts.map +1 -0
  162. package/build/core/server/backgroundLaunchConfig.js +46 -0
  163. package/build/core/server/backgroundLaunchConfig.js.map +1 -0
  164. package/build/core/server/backgroundRuntimeSupervisor.d.ts +44 -0
  165. package/build/core/server/backgroundRuntimeSupervisor.d.ts.map +1 -0
  166. package/build/core/server/backgroundRuntimeSupervisor.js +252 -0
  167. package/build/core/server/backgroundRuntimeSupervisor.js.map +1 -0
  168. package/build/core/server/backgroundRuntimeSupervisorState.d.ts +30 -0
  169. package/build/core/server/backgroundRuntimeSupervisorState.d.ts.map +1 -0
  170. package/build/core/server/backgroundRuntimeSupervisorState.js +81 -0
  171. package/build/core/server/backgroundRuntimeSupervisorState.js.map +1 -0
  172. package/build/core/server/clientInstanceFactory.d.ts.map +1 -1
  173. package/build/core/server/clientInstanceFactory.js +1 -0
  174. package/build/core/server/clientInstanceFactory.js.map +1 -1
  175. package/build/core/server/clientInstancePool.d.ts +17 -5
  176. package/build/core/server/clientInstancePool.d.ts.map +1 -1
  177. package/build/core/server/clientInstancePool.js +198 -33
  178. package/build/core/server/clientInstancePool.js.map +1 -1
  179. package/build/core/server/clientInstancePoolTypes.d.ts +7 -1
  180. package/build/core/server/clientInstancePoolTypes.d.ts.map +1 -1
  181. package/build/core/server/clientInstancePoolTypes.js.map +1 -1
  182. package/build/core/server/runtimeScopeOwnership.d.ts +61 -0
  183. package/build/core/server/runtimeScopeOwnership.d.ts.map +1 -0
  184. package/build/core/server/runtimeScopeOwnership.js +544 -0
  185. package/build/core/server/runtimeScopeOwnership.js.map +1 -0
  186. package/build/core/server/serverManager.d.ts +2 -0
  187. package/build/core/server/serverManager.d.ts.map +1 -1
  188. package/build/core/server/serverManager.js +30 -0
  189. package/build/core/server/serverManager.js.map +1 -1
  190. package/build/core/server/templateIdentity.d.ts +10 -0
  191. package/build/core/server/templateIdentity.d.ts.map +1 -1
  192. package/build/core/server/templateIdentity.js +33 -0
  193. package/build/core/server/templateIdentity.js.map +1 -1
  194. package/build/core/server/templateServerCleanup.d.ts.map +1 -1
  195. package/build/core/server/templateServerCleanup.js +7 -1
  196. package/build/core/server/templateServerCleanup.js.map +1 -1
  197. package/build/core/server/templateServerManager.d.ts +13 -0
  198. package/build/core/server/templateServerManager.d.ts.map +1 -1
  199. package/build/core/server/templateServerManager.js +129 -4
  200. package/build/core/server/templateServerManager.js.map +1 -1
  201. package/build/core/tools/handlers/serverManagementHandler.d.ts +117 -11
  202. package/build/core/tools/handlers/serverManagementHandler.d.ts.map +1 -1
  203. package/build/core/tools/handlers/serverManagementHandler.js +103 -45
  204. package/build/core/tools/handlers/serverManagementHandler.js.map +1 -1
  205. package/build/core/tools/internal/adapters/helpers/configTransformers.d.ts +2 -2
  206. package/build/core/tools/internal/adapters/management/managementAdapter.d.ts +2 -2
  207. package/build/core/tools/internal/adapters/management/managementAdapter.d.ts.map +1 -1
  208. package/build/core/tools/internal/adapters/management/managementAdapter.js +45 -26
  209. package/build/core/tools/internal/adapters/management/managementAdapter.js.map +1 -1
  210. package/build/core/tools/internal/adapters/management/types.d.ts +21 -2
  211. package/build/core/tools/internal/adapters/management/types.d.ts.map +1 -1
  212. package/build/core/tools/internal/managementHandlers.d.ts.map +1 -1
  213. package/build/core/tools/internal/managementHandlers.js +73 -22
  214. package/build/core/tools/internal/managementHandlers.js.map +1 -1
  215. package/build/core/tools/internal/schemas/discovery.d.ts +3 -3
  216. package/build/core/tools/internal/schemas/edit.js +1 -1
  217. package/build/core/tools/internal/schemas/edit.js.map +1 -1
  218. package/build/core/tools/internal/schemas/index.d.ts +90 -19
  219. package/build/core/tools/internal/schemas/index.d.ts.map +1 -1
  220. package/build/core/tools/internal/schemas/installation.d.ts +7 -7
  221. package/build/core/tools/internal/schemas/management.d.ts +80 -9
  222. package/build/core/tools/internal/schemas/management.d.ts.map +1 -1
  223. package/build/core/tools/internal/schemas/management.js +69 -2
  224. package/build/core/tools/internal/schemas/management.js.map +1 -1
  225. package/build/core/types/client.d.ts +9 -2
  226. package/build/core/types/client.d.ts.map +1 -1
  227. package/build/core/types/client.js +4 -0
  228. package/build/core/types/client.js.map +1 -1
  229. package/build/core/types/transport.d.ts +34 -4
  230. package/build/core/types/transport.d.ts.map +1 -1
  231. package/build/core/types/transport.js +26 -3
  232. package/build/core/types/transport.js.map +1 -1
  233. package/build/domains/admin/adminBackendRestartService.d.ts +41 -0
  234. package/build/domains/admin/adminBackendRestartService.d.ts.map +1 -0
  235. package/build/domains/admin/adminBackendRestartService.js +22 -0
  236. package/build/domains/admin/adminBackendRestartService.js.map +1 -0
  237. package/build/domains/admin/adminConfiguredServerService.d.ts +275 -0
  238. package/build/domains/admin/adminConfiguredServerService.d.ts.map +1 -0
  239. package/build/domains/admin/adminConfiguredServerService.js +1926 -0
  240. package/build/domains/admin/adminConfiguredServerService.js.map +1 -0
  241. package/build/domains/admin/adminConnectivityChecker.d.ts +7 -0
  242. package/build/domains/admin/adminConnectivityChecker.d.ts.map +1 -0
  243. package/build/domains/admin/adminConnectivityChecker.js +42 -0
  244. package/build/domains/admin/adminConnectivityChecker.js.map +1 -0
  245. package/build/domains/admin/adminDomain.d.ts +36 -0
  246. package/build/domains/admin/adminDomain.d.ts.map +1 -0
  247. package/build/domains/admin/adminDomain.js +52 -0
  248. package/build/domains/admin/adminDomain.js.map +1 -0
  249. package/build/domains/admin/adminIdentityService.d.ts +75 -0
  250. package/build/domains/admin/adminIdentityService.d.ts.map +1 -0
  251. package/build/domains/admin/adminIdentityService.js +321 -0
  252. package/build/domains/admin/adminIdentityService.js.map +1 -0
  253. package/build/domains/admin/adminOAuthService.d.ts +26 -0
  254. package/build/domains/admin/adminOAuthService.d.ts.map +1 -0
  255. package/build/domains/admin/adminOAuthService.js +71 -0
  256. package/build/domains/admin/adminOAuthService.js.map +1 -0
  257. package/build/domains/admin/adminOperationService.d.ts +203 -0
  258. package/build/domains/admin/adminOperationService.d.ts.map +1 -0
  259. package/build/domains/admin/adminOperationService.js +749 -0
  260. package/build/domains/admin/adminOperationService.js.map +1 -0
  261. package/build/domains/admin/adminPresetService.d.ts +140 -0
  262. package/build/domains/admin/adminPresetService.d.ts.map +1 -0
  263. package/build/domains/admin/adminPresetService.js +248 -0
  264. package/build/domains/admin/adminPresetService.js.map +1 -0
  265. package/build/domains/admin/runtimeScopeAdminLock.d.ts +22 -0
  266. package/build/domains/admin/runtimeScopeAdminLock.d.ts.map +1 -0
  267. package/build/domains/admin/runtimeScopeAdminLock.js +121 -0
  268. package/build/domains/admin/runtimeScopeAdminLock.js.map +1 -0
  269. package/build/domains/admin/runtimeServerManagerBackendRestartService.d.ts +20 -0
  270. package/build/domains/admin/runtimeServerManagerBackendRestartService.d.ts.map +1 -0
  271. package/build/domains/admin/runtimeServerManagerBackendRestartService.js +78 -0
  272. package/build/domains/admin/runtimeServerManagerBackendRestartService.js.map +1 -0
  273. package/build/domains/config-change/configChange.d.ts +6 -1
  274. package/build/domains/config-change/configChange.d.ts.map +1 -1
  275. package/build/domains/config-change/configChange.js +344 -0
  276. package/build/domains/config-change/configChange.js.map +1 -1
  277. package/build/domains/config-change/types.d.ts +18 -3
  278. package/build/domains/config-change/types.d.ts.map +1 -1
  279. package/build/domains/preset/manager/presetManager.d.ts +1 -0
  280. package/build/domains/preset/manager/presetManager.d.ts.map +1 -1
  281. package/build/domains/preset/manager/presetManager.js +3 -0
  282. package/build/domains/preset/manager/presetManager.js.map +1 -1
  283. package/build/domains/preset/tagAuthoring.d.ts +10 -0
  284. package/build/domains/preset/tagAuthoring.d.ts.map +1 -0
  285. package/build/domains/preset/tagAuthoring.js +94 -0
  286. package/build/domains/preset/tagAuthoring.js.map +1 -0
  287. package/build/domains/runtime-targets/runtimeIdentityVerification.d.ts +51 -0
  288. package/build/domains/runtime-targets/runtimeIdentityVerification.d.ts.map +1 -0
  289. package/build/domains/runtime-targets/runtimeIdentityVerification.js +258 -0
  290. package/build/domains/runtime-targets/runtimeIdentityVerification.js.map +1 -0
  291. package/build/domains/runtime-targets/runtimeTargetStore.d.ts +222 -0
  292. package/build/domains/runtime-targets/runtimeTargetStore.d.ts.map +1 -0
  293. package/build/domains/runtime-targets/runtimeTargetStore.js +1456 -0
  294. package/build/domains/runtime-targets/runtimeTargetStore.js.map +1 -0
  295. package/build/index.d.ts.map +1 -1
  296. package/build/index.js +7 -1
  297. package/build/index.js.map +1 -1
  298. package/build/server.d.ts.map +1 -1
  299. package/build/server.js +18 -2
  300. package/build/server.js.map +1 -1
  301. package/build/transport/http/routes/adminRoutes.d.ts +38 -0
  302. package/build/transport/http/routes/adminRoutes.d.ts.map +1 -0
  303. package/build/transport/http/routes/adminRoutes.js +1475 -0
  304. package/build/transport/http/routes/adminRoutes.js.map +1 -0
  305. package/build/transport/http/routes/healthRoutes.d.ts.map +1 -1
  306. package/build/transport/http/routes/healthRoutes.js +70 -2
  307. package/build/transport/http/routes/healthRoutes.js.map +1 -1
  308. package/build/transport/http/routes/oauthRoutes.d.ts +4 -1
  309. package/build/transport/http/routes/oauthRoutes.d.ts.map +1 -1
  310. package/build/transport/http/routes/oauthRoutes.js +12 -190
  311. package/build/transport/http/routes/oauthRoutes.js.map +1 -1
  312. package/build/transport/http/routes/runtimeIdentityRoutes.d.ts +7 -0
  313. package/build/transport/http/routes/runtimeIdentityRoutes.d.ts.map +1 -0
  314. package/build/transport/http/routes/runtimeIdentityRoutes.js +9 -0
  315. package/build/transport/http/routes/runtimeIdentityRoutes.js.map +1 -0
  316. package/build/transport/http/server.d.ts +3 -0
  317. package/build/transport/http/server.d.ts.map +1 -1
  318. package/build/transport/http/server.js +102 -4
  319. package/build/transport/http/server.js.map +1 -1
  320. package/build/transport/transportFactory.d.ts.map +1 -1
  321. package/build/transport/transportFactory.js +27 -15
  322. package/build/transport/transportFactory.js.map +1 -1
  323. package/build/transport/transportFactory.testSetup.js +2 -0
  324. package/build/transport/transportFactory.testSetup.js.map +1 -1
  325. package/build/utils/core/errorTypes.d.ts +3 -0
  326. package/build/utils/core/errorTypes.d.ts.map +1 -1
  327. package/build/utils/core/errorTypes.js +7 -0
  328. package/build/utils/core/errorTypes.js.map +1 -1
  329. package/build/utils/validation/urlDetection.d.ts +13 -2
  330. package/build/utils/validation/urlDetection.d.ts.map +1 -1
  331. package/build/utils/validation/urlDetection.js +37 -6
  332. package/build/utils/validation/urlDetection.js.map +1 -1
  333. package/package.json +22 -9
@@ -0,0 +1,749 @@
1
+ import { createHash, createHmac, randomUUID } from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import path from 'node:path';
4
+ const ADMIN_STATE_DIR = 'admin';
5
+ const JOURNAL_VERSION = 1;
6
+ const DEFAULT_COMPLETED_RETENTION_MS = 24 * 60 * 60 * 1000;
7
+ const DEFAULT_AUDIT_RETENTION_MS = 30 * 24 * 60 * 60 * 1000;
8
+ const DEFAULT_IN_FLIGHT_WAIT_MS = 100;
9
+ const runtimeScopeMutationStates = new Map();
10
+ export class AdminOperationService {
11
+ runtimeScopeId;
12
+ storageDir;
13
+ now;
14
+ completedRetentionMs;
15
+ auditRetentionMs;
16
+ inFlightWaitMs;
17
+ createOperationId;
18
+ mutationAvailability;
19
+ idempotency;
20
+ recentAuditFacts;
21
+ mutationState;
22
+ constructor(options) {
23
+ this.runtimeScopeId = options.runtimeScopeId;
24
+ this.storageDir = path.join(options.storageDir, ADMIN_STATE_DIR);
25
+ this.now = options.now ?? (() => new Date());
26
+ this.completedRetentionMs = options.completedRetentionMs ?? DEFAULT_COMPLETED_RETENTION_MS;
27
+ this.auditRetentionMs = options.auditRetentionMs ?? DEFAULT_AUDIT_RETENTION_MS;
28
+ this.inFlightWaitMs = options.inFlightWaitMs ?? DEFAULT_IN_FLIGHT_WAIT_MS;
29
+ this.createOperationId = options.createOperationId ?? (() => `op_${randomUUID()}`);
30
+ this.mutationAvailability = options.mutationAvailability ?? { available: true };
31
+ this.mutationState = getRuntimeScopeMutationState(this.storageDir, this.runtimeScopeId);
32
+ this.idempotency = this.mutationState.idempotency;
33
+ this.recentAuditFacts = this.mutationState.recentAuditFacts;
34
+ this.replayJournal();
35
+ }
36
+ async executeReadOnly(input) {
37
+ const result = await input.run(input.context);
38
+ return {
39
+ ok: true,
40
+ status: 'completed',
41
+ operationId: this.createOperationId(),
42
+ operationName: input.operationName,
43
+ result,
44
+ replayed: false,
45
+ };
46
+ }
47
+ async executeDryRun(input) {
48
+ const admissionResult = this.validateRuntimeScope(input.context, input.operationName);
49
+ if (admissionResult) {
50
+ return admissionResult;
51
+ }
52
+ try {
53
+ const result = await input.run(input.context);
54
+ return {
55
+ ok: true,
56
+ status: 'completed',
57
+ operationId: this.createOperationId(),
58
+ operationName: input.operationName,
59
+ result,
60
+ replayed: false,
61
+ };
62
+ }
63
+ catch (error) {
64
+ return {
65
+ ok: false,
66
+ status: 'mutation_failed',
67
+ code: 'mutation_failed',
68
+ retryable: false,
69
+ operationName: input.operationName,
70
+ error: error instanceof Error ? error.message : 'Admin dry-run failed',
71
+ };
72
+ }
73
+ }
74
+ getRecentAuditFacts(options = {}) {
75
+ const limit = Math.max(0, options.limit ?? this.recentAuditFacts.length);
76
+ if (limit === 0) {
77
+ return [];
78
+ }
79
+ return this.recentAuditFacts.slice(-limit).map((fact) => ({
80
+ ...fact,
81
+ actor: { ...fact.actor },
82
+ target: { ...fact.target },
83
+ request: { ...fact.request },
84
+ confirmationFacts: fact.confirmationFacts ? { ...fact.confirmationFacts } : undefined,
85
+ }));
86
+ }
87
+ async executeMutation(input) {
88
+ const admissionResult = this.validateMutationAdmission(input.context, input.operationName);
89
+ if (admissionResult) {
90
+ return admissionResult;
91
+ }
92
+ if (this.mutationState.journalUnavailable) {
93
+ return this.journalUnavailableResult(input.operationName);
94
+ }
95
+ const idempotencyKey = input.context.idempotencyKey?.trim();
96
+ const requestFingerprint = input.context.requestFingerprint?.trim();
97
+ if (!idempotencyKey || !requestFingerprint) {
98
+ return {
99
+ ok: false,
100
+ status: 'idempotency_key_required',
101
+ code: 'idempotency_key_required',
102
+ retryable: false,
103
+ operationName: input.operationName,
104
+ };
105
+ }
106
+ const scopedKeyHash = this.scopedKeyHash(input.context, input.operationName, idempotencyKey);
107
+ const legacyScopedKeyHash = this.legacyScopedKeyHash(input.context, input.operationName, idempotencyKey);
108
+ const fingerprintHash = keyedSecretFingerprint(idempotencyKey, 'request', requestFingerprint);
109
+ const legacyFingerprintHash = legacySecretHash(requestFingerprint);
110
+ let existingKeyHash = scopedKeyHash;
111
+ let existing = this.idempotency.get(existingKeyHash);
112
+ if (!existing) {
113
+ existingKeyHash = legacyScopedKeyHash;
114
+ existing = this.idempotency.get(existingKeyHash);
115
+ }
116
+ if (existing && this.isExpiredTerminal(existing)) {
117
+ this.idempotency.delete(existingKeyHash);
118
+ existing = undefined;
119
+ }
120
+ if (existing &&
121
+ existing.fingerprintHash !== fingerprintHash &&
122
+ existing.fingerprintHash !== legacyFingerprintHash) {
123
+ return {
124
+ ok: false,
125
+ status: 'idempotency_conflict',
126
+ code: 'idempotency_conflict',
127
+ retryable: false,
128
+ operationName: input.operationName,
129
+ };
130
+ }
131
+ if (existing?.state === 'completed' && !this.isExpired(existing.completedAt ?? existing.reservedAt)) {
132
+ return {
133
+ ok: true,
134
+ status: 'completed',
135
+ operationId: existing.operationId,
136
+ operationName: input.operationName,
137
+ result: existing.result,
138
+ replayed: true,
139
+ };
140
+ }
141
+ if (existing?.state === 'failed' && !this.isExpired(existing.completedAt ?? existing.reservedAt)) {
142
+ return {
143
+ ok: false,
144
+ status: 'mutation_failed',
145
+ code: 'mutation_failed',
146
+ retryable: false,
147
+ operationName: existing.operationName,
148
+ error: existing.error ?? 'Admin operation failed',
149
+ };
150
+ }
151
+ if (existing?.state === 'state_unknown') {
152
+ return this.stateUnknownResult(existing);
153
+ }
154
+ if (existing?.state === 'in_flight') {
155
+ return (await this.waitForActiveMutation(existingKeyHash, input.operationName));
156
+ }
157
+ const activePreparation = this.mutationState.activePreparations.get(scopedKeyHash);
158
+ if (activePreparation) {
159
+ if (activePreparation.fingerprintHash !== fingerprintHash) {
160
+ return {
161
+ ok: false,
162
+ status: 'idempotency_conflict',
163
+ code: 'idempotency_conflict',
164
+ retryable: false,
165
+ operationName: input.operationName,
166
+ };
167
+ }
168
+ return (await this.waitForActivePreparation(activePreparation, input.operationName));
169
+ }
170
+ const preparationPromise = this.prepareAndExecuteMutation(input, scopedKeyHash, fingerprintHash);
171
+ this.mutationState.activePreparations.set(scopedKeyHash, {
172
+ fingerprintHash,
173
+ promise: preparationPromise,
174
+ });
175
+ try {
176
+ return await preparationPromise;
177
+ }
178
+ finally {
179
+ const active = this.mutationState.activePreparations.get(scopedKeyHash);
180
+ if (active?.promise === preparationPromise) {
181
+ this.mutationState.activePreparations.delete(scopedKeyHash);
182
+ }
183
+ }
184
+ }
185
+ async prepareAndExecuteMutation(input, scopedKeyHash, fingerprintHash) {
186
+ const prepared = 'prepare' in input ? await input.prepare() : input;
187
+ const readyInput = {
188
+ context: input.context,
189
+ operationName: input.operationName,
190
+ confirmationRequirements: prepared.confirmationRequirements,
191
+ run: prepared.run,
192
+ };
193
+ const confirmationResult = this.validateConfirmations(readyInput);
194
+ if (confirmationResult) {
195
+ return confirmationResult;
196
+ }
197
+ const operationId = this.createOperationId();
198
+ const reservedAt = this.now().toISOString();
199
+ const entry = {
200
+ state: 'in_flight',
201
+ scopedKeyHash,
202
+ fingerprintHash,
203
+ operationId,
204
+ operationName: input.operationName,
205
+ target: sanitizeTarget(input.context.target),
206
+ reservedAt,
207
+ };
208
+ try {
209
+ this.appendJournalRecord(this.reservedRecord(input.context, input.operationName, entry));
210
+ }
211
+ catch {
212
+ this.idempotency.delete(scopedKeyHash);
213
+ this.mutationState.journalUnavailable = true;
214
+ return this.journalUnavailableResult(input.operationName);
215
+ }
216
+ this.idempotency.set(scopedKeyHash, entry);
217
+ const activePromise = this.enqueueMutation(async () => this.runReservedMutation(readyInput, entry));
218
+ this.mutationState.activeMutations.set(scopedKeyHash, {
219
+ promise: activePromise,
220
+ });
221
+ activePromise.finally(() => {
222
+ this.mutationState.activeMutations.delete(scopedKeyHash);
223
+ });
224
+ return (await activePromise);
225
+ }
226
+ async waitForActivePreparation(active, operationName) {
227
+ const timeoutMs = this.retryWaitMs();
228
+ const timeout = new Promise((resolve) => {
229
+ setTimeout(() => resolve(this.operationInProgress(operationName, timeoutMs)), timeoutMs);
230
+ });
231
+ return Promise.race([active.promise, timeout]);
232
+ }
233
+ validateMutationAdmission(context, operationName) {
234
+ const runtimeScopeResult = this.validateRuntimeScope(context, operationName);
235
+ if (runtimeScopeResult)
236
+ return runtimeScopeResult;
237
+ if (!this.mutationAvailability.available) {
238
+ return {
239
+ ok: false,
240
+ status: 'runtime_scope_locked',
241
+ code: 'runtime_scope_locked',
242
+ retryable: true,
243
+ operationName,
244
+ reason: 'writer_lock_unavailable',
245
+ };
246
+ }
247
+ return null;
248
+ }
249
+ validateRuntimeScope(context, operationName) {
250
+ if (context.runtimeIdentity.runtimeScopeId === this.runtimeScopeId)
251
+ return null;
252
+ return {
253
+ ok: false,
254
+ status: 'runtime_scope_mismatch',
255
+ code: 'runtime_scope_mismatch',
256
+ retryable: false,
257
+ operationName,
258
+ };
259
+ }
260
+ async runReservedMutation(input, entry) {
261
+ let result;
262
+ try {
263
+ result = await input.run(input.context);
264
+ }
265
+ catch (error) {
266
+ return this.recordMutationFailure(input, entry, error);
267
+ }
268
+ const completedAt = this.now().toISOString();
269
+ const completedEntry = {
270
+ ...entry,
271
+ state: 'completed',
272
+ completedAt,
273
+ result,
274
+ };
275
+ try {
276
+ this.appendJournalRecord({
277
+ schemaVersion: JOURNAL_VERSION,
278
+ type: 'completed',
279
+ runtimeScopeId: this.runtimeScopeId,
280
+ timestamp: completedAt,
281
+ operationId: entry.operationId,
282
+ operationName: entry.operationName,
283
+ scopedKeyHash: entry.scopedKeyHash,
284
+ fingerprintHash: entry.fingerprintHash,
285
+ result,
286
+ });
287
+ this.appendAuditRecord(input.context, entry, 'completed');
288
+ this.idempotency.set(entry.scopedKeyHash, completedEntry);
289
+ return {
290
+ ok: true,
291
+ status: 'completed',
292
+ operationId: entry.operationId,
293
+ operationName: entry.operationName,
294
+ result,
295
+ replayed: false,
296
+ };
297
+ }
298
+ catch {
299
+ return this.markStateUnknownResult(entry);
300
+ }
301
+ }
302
+ recordMutationFailure(input, entry, error) {
303
+ const failedAt = this.now().toISOString();
304
+ const message = error instanceof Error ? error.message : 'Admin operation failed';
305
+ try {
306
+ this.appendJournalRecord({
307
+ schemaVersion: JOURNAL_VERSION,
308
+ type: 'failed',
309
+ runtimeScopeId: this.runtimeScopeId,
310
+ timestamp: failedAt,
311
+ operationId: entry.operationId,
312
+ operationName: entry.operationName,
313
+ scopedKeyHash: entry.scopedKeyHash,
314
+ fingerprintHash: entry.fingerprintHash,
315
+ error: message,
316
+ });
317
+ this.appendAuditRecord(input.context, entry, 'failed');
318
+ this.idempotency.set(entry.scopedKeyHash, {
319
+ ...entry,
320
+ state: 'failed',
321
+ completedAt: failedAt,
322
+ error: message,
323
+ });
324
+ }
325
+ catch {
326
+ this.mutationState.journalUnavailable = true;
327
+ return this.journalUnavailableResult(entry.operationName);
328
+ }
329
+ return {
330
+ ok: false,
331
+ status: 'mutation_failed',
332
+ code: 'mutation_failed',
333
+ retryable: false,
334
+ operationName: entry.operationName,
335
+ error: message,
336
+ };
337
+ }
338
+ enqueueMutation(operation) {
339
+ const run = this.mutationState.mutationQueue.then(operation, operation);
340
+ this.mutationState.mutationQueue = run.then(() => undefined, () => undefined);
341
+ return run;
342
+ }
343
+ async waitForActiveMutation(scopedKeyHash, operationName) {
344
+ const active = this.mutationState.activeMutations.get(scopedKeyHash);
345
+ if (!active) {
346
+ const entry = this.idempotency.get(scopedKeyHash);
347
+ return entry ? this.markStateUnknownResult(entry) : this.operationInProgress(operationName);
348
+ }
349
+ const timeoutMs = this.retryWaitMs();
350
+ const timeout = new Promise((resolve) => {
351
+ setTimeout(() => resolve(this.operationInProgress(operationName, timeoutMs)), timeoutMs);
352
+ });
353
+ return Promise.race([active.promise, timeout]);
354
+ }
355
+ retryWaitMs() {
356
+ return Math.max(1, this.inFlightWaitMs);
357
+ }
358
+ operationInProgress(operationName, retryAfterMs = this.retryWaitMs()) {
359
+ return {
360
+ ok: false,
361
+ status: 'operation_in_progress',
362
+ code: 'operation_in_progress',
363
+ retryable: true,
364
+ operationName,
365
+ retryAfterMs,
366
+ };
367
+ }
368
+ journalUnavailableResult(operationName) {
369
+ return {
370
+ ok: false,
371
+ status: 'admin_operation_journal_unavailable',
372
+ code: 'admin_operation_journal_unavailable',
373
+ retryable: false,
374
+ operationName,
375
+ };
376
+ }
377
+ validateConfirmations(input) {
378
+ if (!input.confirmationRequirements?.length) {
379
+ return null;
380
+ }
381
+ const facts = input.context.confirmationFacts ?? {};
382
+ const missing = input.confirmationRequirements.filter((requirement) => {
383
+ const actual = facts[requirement.code];
384
+ return requirement.expected === undefined ? actual !== true : actual !== requirement.expected;
385
+ });
386
+ if (missing.length === 0) {
387
+ return null;
388
+ }
389
+ return {
390
+ ok: false,
391
+ status: 'mutation_confirmation_required',
392
+ code: 'mutation_confirmation_required',
393
+ retryable: false,
394
+ operationName: input.operationName,
395
+ confirmationRequirements: input.confirmationRequirements.map((requirement) => ({ ...requirement })),
396
+ };
397
+ }
398
+ replayJournal() {
399
+ const filePath = this.journalFilePath();
400
+ if (!fs.existsSync(filePath)) {
401
+ return;
402
+ }
403
+ try {
404
+ const records = this.readJournalRecords(filePath);
405
+ this.idempotency.clear();
406
+ this.recentAuditFacts.length = 0;
407
+ this.mutationState.journalUnavailable = false;
408
+ for (const record of records) {
409
+ this.applyJournalRecord(record);
410
+ }
411
+ for (const entry of Array.from(this.idempotency.values())) {
412
+ if (entry.state === 'in_flight' && !this.mutationState.activeMutations.has(entry.scopedKeyHash)) {
413
+ this.markStateUnknown(entry);
414
+ }
415
+ }
416
+ }
417
+ catch {
418
+ this.idempotency.clear();
419
+ this.recentAuditFacts.length = 0;
420
+ this.mutationState.journalUnavailable = true;
421
+ return;
422
+ }
423
+ try {
424
+ this.compactJournal();
425
+ }
426
+ catch {
427
+ // Compaction is cleanup only; replayed admission/audit state remains authoritative.
428
+ }
429
+ }
430
+ applyJournalRecord(record) {
431
+ if (record.schemaVersion !== JOURNAL_VERSION || record.runtimeScopeId !== this.runtimeScopeId) {
432
+ return;
433
+ }
434
+ if (record.type === 'reserved') {
435
+ this.idempotency.set(record.scopedKeyHash, {
436
+ state: 'in_flight',
437
+ scopedKeyHash: record.scopedKeyHash,
438
+ fingerprintHash: record.fingerprintHash,
439
+ operationId: record.operationId,
440
+ operationName: record.operationName,
441
+ target: record.target,
442
+ reservedAt: record.timestamp,
443
+ });
444
+ return;
445
+ }
446
+ if (record.type === 'completed') {
447
+ if (this.isExpired(record.timestamp)) {
448
+ this.idempotency.delete(record.scopedKeyHash);
449
+ return;
450
+ }
451
+ this.idempotency.set(record.scopedKeyHash, {
452
+ ...this.entryFromRecord(record),
453
+ state: 'completed',
454
+ completedAt: record.timestamp,
455
+ result: record.result,
456
+ });
457
+ return;
458
+ }
459
+ if (record.type === 'failed') {
460
+ if (this.isExpired(record.timestamp)) {
461
+ this.idempotency.delete(record.scopedKeyHash);
462
+ return;
463
+ }
464
+ this.idempotency.set(record.scopedKeyHash, {
465
+ ...this.entryFromRecord(record),
466
+ state: 'failed',
467
+ completedAt: record.timestamp,
468
+ error: record.error,
469
+ });
470
+ return;
471
+ }
472
+ if (record.type === 'state_unknown') {
473
+ if (this.isAuditExpired(record.timestamp)) {
474
+ this.idempotency.delete(record.scopedKeyHash);
475
+ return;
476
+ }
477
+ this.idempotency.set(record.scopedKeyHash, {
478
+ state: 'state_unknown',
479
+ scopedKeyHash: record.scopedKeyHash,
480
+ fingerprintHash: record.fingerprintHash,
481
+ operationId: record.operationId,
482
+ operationName: record.operationName,
483
+ target: record.target,
484
+ reservedAt: record.reservedAt,
485
+ });
486
+ return;
487
+ }
488
+ if (record.type === 'audit') {
489
+ if (this.isAuditExpired(record.timestamp)) {
490
+ return;
491
+ }
492
+ this.recentAuditFacts.push(auditFactFromRecord(record));
493
+ }
494
+ }
495
+ entryFromRecord(record) {
496
+ const existing = this.idempotency.get(record.scopedKeyHash);
497
+ return {
498
+ scopedKeyHash: record.scopedKeyHash,
499
+ fingerprintHash: record.fingerprintHash,
500
+ operationId: record.operationId,
501
+ operationName: record.operationName,
502
+ target: existing?.target ?? { type: 'unknown' },
503
+ reservedAt: existing?.reservedAt ?? record.timestamp,
504
+ };
505
+ }
506
+ markStateUnknown(entry) {
507
+ const unknownEntry = {
508
+ ...entry,
509
+ state: 'state_unknown',
510
+ };
511
+ this.appendJournalRecord({
512
+ schemaVersion: JOURNAL_VERSION,
513
+ type: 'state_unknown',
514
+ runtimeScopeId: this.runtimeScopeId,
515
+ timestamp: this.now().toISOString(),
516
+ operationId: entry.operationId,
517
+ operationName: entry.operationName,
518
+ scopedKeyHash: entry.scopedKeyHash,
519
+ fingerprintHash: entry.fingerprintHash,
520
+ target: entry.target,
521
+ reservedAt: entry.reservedAt,
522
+ });
523
+ this.idempotency.set(entry.scopedKeyHash, unknownEntry);
524
+ return unknownEntry;
525
+ }
526
+ markStateUnknownResult(entry) {
527
+ try {
528
+ return this.stateUnknownResult(this.markStateUnknown(entry));
529
+ }
530
+ catch {
531
+ this.mutationState.journalUnavailable = true;
532
+ this.idempotency.set(entry.scopedKeyHash, {
533
+ ...entry,
534
+ state: 'state_unknown',
535
+ });
536
+ return this.journalUnavailableResult(entry.operationName);
537
+ }
538
+ }
539
+ stateUnknownResult(entry) {
540
+ return {
541
+ ok: false,
542
+ status: 'operation_state_unknown',
543
+ code: 'operation_state_unknown',
544
+ retryable: false,
545
+ operationName: entry.operationName,
546
+ target: entry.target,
547
+ reservedAt: entry.reservedAt,
548
+ recovery: 'inspect_current_state_and_retry_with_new_idempotency_key',
549
+ };
550
+ }
551
+ reservedRecord(context, operationName, entry) {
552
+ return {
553
+ schemaVersion: JOURNAL_VERSION,
554
+ type: 'reserved',
555
+ runtimeScopeId: this.runtimeScopeId,
556
+ timestamp: entry.reservedAt,
557
+ operationId: entry.operationId,
558
+ operationName,
559
+ scopedKeyHash: entry.scopedKeyHash,
560
+ fingerprintHash: entry.fingerprintHash,
561
+ target: entry.target,
562
+ actor: sanitizeActor(context.actor, this.runtimeScopeId),
563
+ origin: context.origin,
564
+ request: sanitizeRequest(context.request),
565
+ runtimeIdentity: sanitizeRuntimeIdentity(context.runtimeIdentity),
566
+ };
567
+ }
568
+ appendAuditRecord(context, entry, result) {
569
+ const fact = {
570
+ timestamp: this.now().toISOString(),
571
+ operationId: entry.operationId,
572
+ operationName: entry.operationName,
573
+ result,
574
+ actor: sanitizeActor(context.actor, this.runtimeScopeId),
575
+ origin: context.origin,
576
+ target: entry.target,
577
+ request: { requestId: context.request.requestId },
578
+ confirmationFacts: sanitizeConfirmationFacts(context.confirmationFacts),
579
+ };
580
+ this.appendJournalRecord({
581
+ schemaVersion: JOURNAL_VERSION,
582
+ type: 'audit',
583
+ runtimeScopeId: this.runtimeScopeId,
584
+ timestamp: fact.timestamp,
585
+ operationId: fact.operationId,
586
+ operationName: fact.operationName,
587
+ scopedKeyHash: entry.scopedKeyHash,
588
+ result: fact.result,
589
+ actor: fact.actor,
590
+ origin: fact.origin,
591
+ target: fact.target,
592
+ request: fact.request,
593
+ confirmationFacts: fact.confirmationFacts,
594
+ });
595
+ this.recentAuditFacts.push(fact);
596
+ }
597
+ appendJournalRecord(record) {
598
+ fs.mkdirSync(this.storageDir, { recursive: true });
599
+ const filePath = this.journalFilePath();
600
+ const fd = fs.openSync(filePath, 'a', 0o600);
601
+ try {
602
+ fs.chmodSync(filePath, 0o600);
603
+ fs.writeSync(fd, `${JSON.stringify(record)}\n`, undefined, 'utf8');
604
+ fs.fsyncSync(fd);
605
+ }
606
+ finally {
607
+ fs.closeSync(fd);
608
+ }
609
+ }
610
+ scopedKeyHash(context, operationName, idempotencyKey) {
611
+ return keyedSecretFingerprint(idempotencyKey, 'idempotency-scope', JSON.stringify({
612
+ runtimeScopeId: this.runtimeScopeId,
613
+ actorType: context.actor.type,
614
+ accountId: context.actor.accountId,
615
+ sessionId: context.actor.sessionId,
616
+ operationName,
617
+ }));
618
+ }
619
+ legacyScopedKeyHash(context, operationName, idempotencyKey) {
620
+ return legacySecretHash(JSON.stringify({
621
+ runtimeScopeId: this.runtimeScopeId,
622
+ actorType: context.actor.type,
623
+ accountId: context.actor.accountId,
624
+ sessionId: context.actor.sessionId,
625
+ operationName,
626
+ idempotencyKey,
627
+ }));
628
+ }
629
+ isExpired(timestamp) {
630
+ return this.now().getTime() - new Date(timestamp).getTime() > this.completedRetentionMs;
631
+ }
632
+ isAuditExpired(timestamp) {
633
+ return this.now().getTime() - new Date(timestamp).getTime() > this.auditRetentionMs;
634
+ }
635
+ isExpiredTerminal(entry) {
636
+ return ((entry.state === 'completed' || entry.state === 'failed') && this.isExpired(entry.completedAt ?? entry.reservedAt));
637
+ }
638
+ compactJournal() {
639
+ const filePath = this.journalFilePath();
640
+ if (!fs.existsSync(filePath)) {
641
+ return;
642
+ }
643
+ const records = this.readJournalRecords(filePath);
644
+ const retained = records.filter((record) => this.shouldRetainJournalRecord(record));
645
+ if (retained.length === records.length) {
646
+ return;
647
+ }
648
+ const tempPath = `${filePath}.${process.pid}.compact.tmp`;
649
+ const content = retained.length > 0 ? `${retained.map((record) => JSON.stringify(record)).join('\n')}\n` : '';
650
+ fs.writeFileSync(tempPath, content, { encoding: 'utf8', mode: 0o600 });
651
+ fs.renameSync(tempPath, filePath);
652
+ fs.chmodSync(filePath, 0o600);
653
+ }
654
+ shouldRetainJournalRecord(record) {
655
+ if (record.type === 'state_unknown') {
656
+ return !this.isAuditExpired(record.timestamp);
657
+ }
658
+ if (record.type === 'audit') {
659
+ return !this.isAuditExpired(record.timestamp);
660
+ }
661
+ if (record.type === 'completed' || record.type === 'failed') {
662
+ return !this.isExpired(record.timestamp);
663
+ }
664
+ const entry = this.idempotency.get(record.scopedKeyHash);
665
+ return Boolean(entry && !this.isExpiredTerminal(entry));
666
+ }
667
+ readJournalRecords(filePath) {
668
+ return fs
669
+ .readFileSync(filePath, 'utf8')
670
+ .split('\n')
671
+ .filter((line) => line.trim())
672
+ .map((line) => JSON.parse(line));
673
+ }
674
+ journalFilePath() {
675
+ return path.join(this.storageDir, `admin-operations-${stableIdentifierHash(this.runtimeScopeId).slice(0, 24)}.jsonl`);
676
+ }
677
+ }
678
+ function sanitizeActor(actor, runtimeScopeId) {
679
+ return {
680
+ type: actor.type,
681
+ accountIdHash: keyedSecretFingerprint(runtimeScopeId, 'account-id', actor.accountId),
682
+ sessionIdHash: actor.sessionId ? keyedSecretFingerprint(runtimeScopeId, 'session-id', actor.sessionId) : undefined,
683
+ };
684
+ }
685
+ function sanitizeTarget(target) {
686
+ return {
687
+ type: target.type,
688
+ id: target.id,
689
+ };
690
+ }
691
+ function sanitizeRequest(request) {
692
+ return {
693
+ requestId: request.requestId,
694
+ jsonMode: request.jsonMode,
695
+ };
696
+ }
697
+ function sanitizeRuntimeIdentity(runtimeIdentity) {
698
+ return {
699
+ runtimeScopeId: runtimeIdentity.runtimeScopeId,
700
+ runtimeVersion: runtimeIdentity.runtimeVersion,
701
+ };
702
+ }
703
+ function sanitizeConfirmationFacts(facts) {
704
+ if (!facts || Object.keys(facts).length === 0) {
705
+ return undefined;
706
+ }
707
+ return Object.fromEntries(Object.entries(facts).filter(([, value]) => ['string', 'boolean', 'number'].includes(typeof value)));
708
+ }
709
+ function stableIdentifierHash(identifier) {
710
+ return createHash('sha256').update(identifier).digest('base64url');
711
+ }
712
+ // Retained v1 journal entries use unkeyed SHA-256. Keep this only for bounded replay compatibility.
713
+ function legacySecretHash(secret) {
714
+ return createHash('sha256').update(secret).digest('base64url');
715
+ }
716
+ function keyedSecretFingerprint(key, domain, secret) {
717
+ return createHmac('sha256', key).update(domain).update('\0').update(secret).digest('base64url');
718
+ }
719
+ function auditFactFromRecord(record) {
720
+ return {
721
+ timestamp: record.timestamp,
722
+ operationId: record.operationId,
723
+ operationName: record.operationName,
724
+ result: record.result,
725
+ actor: { ...record.actor },
726
+ origin: record.origin,
727
+ target: { ...record.target },
728
+ request: { ...record.request },
729
+ confirmationFacts: record.confirmationFacts ? { ...record.confirmationFacts } : undefined,
730
+ };
731
+ }
732
+ function getRuntimeScopeMutationState(storageDir, runtimeScopeId) {
733
+ const key = `${storageDir}\0${runtimeScopeId}`;
734
+ const existing = runtimeScopeMutationStates.get(key);
735
+ if (existing) {
736
+ return existing;
737
+ }
738
+ const state = {
739
+ mutationQueue: Promise.resolve(),
740
+ activeMutations: new Map(),
741
+ activePreparations: new Map(),
742
+ idempotency: new Map(),
743
+ recentAuditFacts: [],
744
+ journalUnavailable: false,
745
+ };
746
+ runtimeScopeMutationStates.set(key, state);
747
+ return state;
748
+ }
749
+ //# sourceMappingURL=adminOperationService.js.map