@bevel-software/platform-core-backend 0.14.0 → 0.15.1

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 (385) hide show
  1. package/THIRD-PARTY-NOTICES.md +6 -6
  2. package/dist/core/catalog-cache-invalidation.d.ts +29 -0
  3. package/dist/core/catalog-cache-invalidation.d.ts.map +1 -0
  4. package/dist/core/catalog-cache-invalidation.js +71 -0
  5. package/dist/core/catalog-cache-invalidation.js.map +1 -0
  6. package/dist/core/create-core-server.d.ts.map +1 -1
  7. package/dist/core/create-core-server.js +99 -8
  8. package/dist/core/create-core-server.js.map +1 -1
  9. package/dist/core/create-core-services.d.ts +18 -1
  10. package/dist/core/create-core-services.d.ts.map +1 -1
  11. package/dist/core/create-core-services.js +96 -43
  12. package/dist/core/create-core-services.js.map +1 -1
  13. package/dist/modules/access/access-control.interface.d.ts +18 -6
  14. package/dist/modules/access/access-control.interface.d.ts.map +1 -1
  15. package/dist/modules/access/access-control.service.d.ts +26 -1
  16. package/dist/modules/access/access-control.service.d.ts.map +1 -1
  17. package/dist/modules/access/access-control.service.js +213 -71
  18. package/dist/modules/access/access-control.service.js.map +1 -1
  19. package/dist/modules/access/access.routes.d.ts.map +1 -1
  20. package/dist/modules/access/access.routes.js +59 -7
  21. package/dist/modules/access/access.routes.js.map +1 -1
  22. package/dist/modules/access-model/access-grammar.d.ts +51 -1
  23. package/dist/modules/access-model/access-grammar.d.ts.map +1 -1
  24. package/dist/modules/access-model/access-grammar.js +95 -1
  25. package/dist/modules/access-model/access-grammar.js.map +1 -1
  26. package/dist/modules/access-model/group-files.d.ts.map +1 -1
  27. package/dist/modules/access-model/group-files.js +4 -1
  28. package/dist/modules/access-model/group-files.js.map +1 -1
  29. package/dist/modules/access-model/plugin-principals.d.ts +49 -0
  30. package/dist/modules/access-model/plugin-principals.d.ts.map +1 -0
  31. package/dist/modules/access-model/plugin-principals.js +125 -0
  32. package/dist/modules/access-model/plugin-principals.js.map +1 -0
  33. package/dist/modules/database/core-schema.d.ts +326 -0
  34. package/dist/modules/database/core-schema.d.ts.map +1 -1
  35. package/dist/modules/database/core-schema.js +52 -0
  36. package/dist/modules/database/core-schema.js.map +1 -1
  37. package/dist/modules/kb-sync/index.d.ts +6 -0
  38. package/dist/modules/kb-sync/index.d.ts.map +1 -0
  39. package/dist/modules/kb-sync/index.js +5 -0
  40. package/dist/modules/kb-sync/index.js.map +1 -0
  41. package/dist/modules/kb-sync/kb-sync.interface.d.ts +77 -0
  42. package/dist/modules/kb-sync/kb-sync.interface.d.ts.map +1 -0
  43. package/dist/modules/kb-sync/kb-sync.interface.js +2 -0
  44. package/dist/modules/kb-sync/kb-sync.interface.js.map +1 -0
  45. package/dist/modules/kb-sync/kb-sync.routes.d.ts +70 -0
  46. package/dist/modules/kb-sync/kb-sync.routes.d.ts.map +1 -0
  47. package/dist/modules/kb-sync/kb-sync.routes.js +135 -0
  48. package/dist/modules/kb-sync/kb-sync.routes.js.map +1 -0
  49. package/dist/modules/kb-sync/kb-sync.service.d.ts +45 -0
  50. package/dist/modules/kb-sync/kb-sync.service.d.ts.map +1 -0
  51. package/dist/modules/kb-sync/kb-sync.service.js +183 -0
  52. package/dist/modules/kb-sync/kb-sync.service.js.map +1 -0
  53. package/dist/modules/kb-sync/sync-auth.d.ts +59 -0
  54. package/dist/modules/kb-sync/sync-auth.d.ts.map +1 -0
  55. package/dist/modules/kb-sync/sync-auth.js +62 -0
  56. package/dist/modules/kb-sync/sync-auth.js.map +1 -0
  57. package/dist/modules/kb-sync/sync-payload.d.ts +32 -0
  58. package/dist/modules/kb-sync/sync-payload.d.ts.map +1 -0
  59. package/dist/modules/kb-sync/sync-payload.js +105 -0
  60. package/dist/modules/kb-sync/sync-payload.js.map +1 -0
  61. package/dist/modules/marketplace/git-http.routes.d.ts +42 -0
  62. package/dist/modules/marketplace/git-http.routes.d.ts.map +1 -0
  63. package/dist/modules/marketplace/git-http.routes.js +190 -0
  64. package/dist/modules/marketplace/git-http.routes.js.map +1 -0
  65. package/dist/modules/marketplace/github-facade/github-facade-admin.routes.d.ts +23 -0
  66. package/dist/modules/marketplace/github-facade/github-facade-admin.routes.d.ts.map +1 -0
  67. package/dist/modules/marketplace/github-facade/github-facade-admin.routes.js +59 -0
  68. package/dist/modules/marketplace/github-facade/github-facade-admin.routes.js.map +1 -0
  69. package/dist/modules/marketplace/github-facade/github-facade-codes.store.d.ts +59 -0
  70. package/dist/modules/marketplace/github-facade/github-facade-codes.store.d.ts.map +1 -0
  71. package/dist/modules/marketplace/github-facade/github-facade-codes.store.js +96 -0
  72. package/dist/modules/marketplace/github-facade/github-facade-codes.store.js.map +1 -0
  73. package/dist/modules/marketplace/github-facade/github-facade-credentials.service.d.ts +99 -0
  74. package/dist/modules/marketplace/github-facade/github-facade-credentials.service.d.ts.map +1 -0
  75. package/dist/modules/marketplace/github-facade/github-facade-credentials.service.js +147 -0
  76. package/dist/modules/marketplace/github-facade/github-facade-credentials.service.js.map +1 -0
  77. package/dist/modules/marketplace/github-facade/github-facade.routes.d.ts +47 -0
  78. package/dist/modules/marketplace/github-facade/github-facade.routes.d.ts.map +1 -0
  79. package/dist/modules/marketplace/github-facade/github-facade.routes.js +228 -0
  80. package/dist/modules/marketplace/github-facade/github-facade.routes.js.map +1 -0
  81. package/dist/modules/marketplace/github-facade/github-facade.service.d.ts +109 -0
  82. package/dist/modules/marketplace/github-facade/github-facade.service.d.ts.map +1 -0
  83. package/dist/modules/marketplace/github-facade/github-facade.service.js +172 -0
  84. package/dist/modules/marketplace/github-facade/github-facade.service.js.map +1 -0
  85. package/dist/modules/marketplace/github-facade/index.d.ts +6 -0
  86. package/dist/modules/marketplace/github-facade/index.d.ts.map +1 -0
  87. package/dist/modules/marketplace/github-facade/index.js +6 -0
  88. package/dist/modules/marketplace/github-facade/index.js.map +1 -0
  89. package/dist/modules/marketplace/index.d.ts +4 -0
  90. package/dist/modules/marketplace/index.d.ts.map +1 -0
  91. package/dist/modules/marketplace/index.js +4 -0
  92. package/dist/modules/marketplace/index.js.map +1 -0
  93. package/dist/modules/marketplace/marketplace-repo.service.d.ts +122 -0
  94. package/dist/modules/marketplace/marketplace-repo.service.d.ts.map +1 -0
  95. package/dist/modules/marketplace/marketplace-repo.service.js +300 -0
  96. package/dist/modules/marketplace/marketplace-repo.service.js.map +1 -0
  97. package/dist/modules/mcp/oauth/bevel-oauth-provider.d.ts.map +1 -1
  98. package/dist/modules/mcp/oauth/bevel-oauth-provider.js +4 -0
  99. package/dist/modules/mcp/oauth/bevel-oauth-provider.js.map +1 -1
  100. package/dist/modules/mcp/oauth/oauth-consent.routes.d.ts +14 -8
  101. package/dist/modules/mcp/oauth/oauth-consent.routes.d.ts.map +1 -1
  102. package/dist/modules/mcp/oauth/oauth-consent.routes.js +13 -1
  103. package/dist/modules/mcp/oauth/oauth-consent.routes.js.map +1 -1
  104. package/dist/modules/mcp/oauth/oauth-state.d.ts +13 -2
  105. package/dist/modules/mcp/oauth/oauth-state.d.ts.map +1 -1
  106. package/dist/modules/mcp/oauth/oauth-state.js +3 -1
  107. package/dist/modules/mcp/oauth/oauth-state.js.map +1 -1
  108. package/dist/modules/plugins/compile/compile-marketplace.d.ts +97 -0
  109. package/dist/modules/plugins/compile/compile-marketplace.d.ts.map +1 -0
  110. package/dist/modules/plugins/compile/compile-marketplace.js +240 -0
  111. package/dist/modules/plugins/compile/compile-marketplace.js.map +1 -0
  112. package/dist/modules/plugins/compile/marketplace-compiler.service.d.ts +66 -0
  113. package/dist/modules/plugins/compile/marketplace-compiler.service.d.ts.map +1 -0
  114. package/dist/modules/plugins/compile/marketplace-compiler.service.js +132 -0
  115. package/dist/modules/plugins/compile/marketplace-compiler.service.js.map +1 -0
  116. package/dist/modules/plugins/discovery/bundle-dialect/bundle.source.d.ts +37 -0
  117. package/dist/modules/plugins/discovery/bundle-dialect/bundle.source.d.ts.map +1 -0
  118. package/dist/modules/plugins/discovery/bundle-dialect/bundle.source.js +135 -0
  119. package/dist/modules/plugins/discovery/bundle-dialect/bundle.source.js.map +1 -0
  120. package/dist/modules/plugins/discovery/bundle-dialect/registry.d.ts +45 -0
  121. package/dist/modules/plugins/discovery/bundle-dialect/registry.d.ts.map +1 -0
  122. package/dist/modules/plugins/discovery/bundle-dialect/registry.js +147 -0
  123. package/dist/modules/plugins/discovery/bundle-dialect/registry.js.map +1 -0
  124. package/dist/modules/plugins/discovery/kb-plugin-source.d.ts +37 -0
  125. package/dist/modules/plugins/discovery/kb-plugin-source.d.ts.map +1 -0
  126. package/dist/modules/plugins/discovery/kb-plugin-source.js +107 -0
  127. package/dist/modules/plugins/discovery/kb-plugin-source.js.map +1 -0
  128. package/dist/modules/plugins/discovery/native.source.d.ts +15 -0
  129. package/dist/modules/plugins/discovery/native.source.d.ts.map +1 -0
  130. package/dist/modules/plugins/discovery/native.source.js +102 -0
  131. package/dist/modules/plugins/discovery/native.source.js.map +1 -0
  132. package/dist/modules/plugins/discovery/plugin-source.d.ts +96 -0
  133. package/dist/modules/plugins/discovery/plugin-source.d.ts.map +1 -0
  134. package/dist/modules/plugins/discovery/plugin-source.js +2 -0
  135. package/dist/modules/plugins/discovery/plugin-source.js.map +1 -0
  136. package/dist/modules/plugins/index.d.ts +10 -1
  137. package/dist/modules/plugins/index.d.ts.map +1 -1
  138. package/dist/modules/plugins/index.js +8 -0
  139. package/dist/modules/plugins/index.js.map +1 -1
  140. package/dist/modules/plugins/plugin-links.d.ts +66 -0
  141. package/dist/modules/plugins/plugin-links.d.ts.map +1 -0
  142. package/dist/modules/plugins/plugin-links.js +160 -0
  143. package/dist/modules/plugins/plugin-links.js.map +1 -0
  144. package/dist/modules/plugins/plugin-links.service.d.ts +114 -0
  145. package/dist/modules/plugins/plugin-links.service.d.ts.map +1 -0
  146. package/dist/modules/plugins/plugin-links.service.js +304 -0
  147. package/dist/modules/plugins/plugin-links.service.js.map +1 -0
  148. package/dist/modules/plugins/plugin-provision.service.d.ts +39 -3
  149. package/dist/modules/plugins/plugin-provision.service.d.ts.map +1 -1
  150. package/dist/modules/plugins/plugin-provision.service.js +152 -52
  151. package/dist/modules/plugins/plugin-provision.service.js.map +1 -1
  152. package/dist/modules/plugins/plugin-rename.service.d.ts +69 -0
  153. package/dist/modules/plugins/plugin-rename.service.d.ts.map +1 -0
  154. package/dist/modules/plugins/plugin-rename.service.js +245 -0
  155. package/dist/modules/plugins/plugin-rename.service.js.map +1 -0
  156. package/dist/modules/plugins/plugins.contract.d.ts +27 -1
  157. package/dist/modules/plugins/plugins.contract.d.ts.map +1 -1
  158. package/dist/modules/plugins/plugins.routes.d.ts +7 -1
  159. package/dist/modules/plugins/plugins.routes.d.ts.map +1 -1
  160. package/dist/modules/plugins/plugins.routes.js +120 -12
  161. package/dist/modules/plugins/plugins.routes.js.map +1 -1
  162. package/dist/modules/plugins/plugins.service.d.ts +50 -3
  163. package/dist/modules/plugins/plugins.service.d.ts.map +1 -1
  164. package/dist/modules/plugins/plugins.service.js +108 -47
  165. package/dist/modules/plugins/plugins.service.js.map +1 -1
  166. package/dist/modules/settings/deployment-settings.service.d.ts +9 -0
  167. package/dist/modules/settings/deployment-settings.service.d.ts.map +1 -1
  168. package/dist/modules/settings/deployment-settings.service.js +82 -2
  169. package/dist/modules/settings/deployment-settings.service.js.map +1 -1
  170. package/dist/modules/settings/setup.routes.d.ts +26 -0
  171. package/dist/modules/settings/setup.routes.d.ts.map +1 -1
  172. package/dist/modules/settings/setup.routes.js +9 -1
  173. package/dist/modules/settings/setup.routes.js.map +1 -1
  174. package/dist/modules/skills/index.d.ts +1 -0
  175. package/dist/modules/skills/index.d.ts.map +1 -1
  176. package/dist/modules/skills/index.js +1 -0
  177. package/dist/modules/skills/index.js.map +1 -1
  178. package/dist/modules/skills/pending-skills.service.d.ts.map +1 -1
  179. package/dist/modules/skills/pending-skills.service.js +9 -4
  180. package/dist/modules/skills/pending-skills.service.js.map +1 -1
  181. package/dist/modules/skills/skill-access-requests.routes.d.ts +38 -0
  182. package/dist/modules/skills/skill-access-requests.routes.d.ts.map +1 -0
  183. package/dist/modules/skills/skill-access-requests.routes.js +168 -0
  184. package/dist/modules/skills/skill-access-requests.routes.js.map +1 -0
  185. package/dist/modules/skills/skills.contract.d.ts +31 -0
  186. package/dist/modules/skills/skills.contract.d.ts.map +1 -1
  187. package/dist/modules/skills/skills.routes.d.ts +28 -3
  188. package/dist/modules/skills/skills.routes.d.ts.map +1 -1
  189. package/dist/modules/skills/skills.routes.js +42 -3
  190. package/dist/modules/skills/skills.routes.js.map +1 -1
  191. package/dist/modules/skills/skills.service.d.ts +4 -0
  192. package/dist/modules/skills/skills.service.d.ts.map +1 -1
  193. package/dist/modules/skills/skills.service.js +83 -22
  194. package/dist/modules/skills/skills.service.js.map +1 -1
  195. package/dist/modules/tool-auth/external-api-key.interface.d.ts +18 -1
  196. package/dist/modules/tool-auth/external-api-key.interface.d.ts.map +1 -1
  197. package/dist/modules/tool-auth/external-api-key.interface.js +2 -1
  198. package/dist/modules/tool-auth/external-api-key.interface.js.map +1 -1
  199. package/dist/modules/tool-auth/external-api-key.service.d.ts +13 -4
  200. package/dist/modules/tool-auth/external-api-key.service.d.ts.map +1 -1
  201. package/dist/modules/tool-auth/external-api-key.service.js +26 -9
  202. package/dist/modules/tool-auth/external-api-key.service.js.map +1 -1
  203. package/dist/modules/tool-manuals/mcp-json-discovery.d.ts +36 -0
  204. package/dist/modules/tool-manuals/mcp-json-discovery.d.ts.map +1 -1
  205. package/dist/modules/tool-manuals/mcp-json-discovery.js +130 -92
  206. package/dist/modules/tool-manuals/mcp-json-discovery.js.map +1 -1
  207. package/dist/modules/tool-manuals/tool-manuals.service.d.ts +6 -1
  208. package/dist/modules/tool-manuals/tool-manuals.service.d.ts.map +1 -1
  209. package/dist/modules/tool-manuals/tool-manuals.service.js +18 -25
  210. package/dist/modules/tool-manuals/tool-manuals.service.js.map +1 -1
  211. package/dist/modules/workflow/git/git.service.d.ts +40 -2
  212. package/dist/modules/workflow/git/git.service.d.ts.map +1 -1
  213. package/dist/modules/workflow/git/git.service.js +227 -54
  214. package/dist/modules/workflow/git/git.service.js.map +1 -1
  215. package/dist/modules/workflow/git/pull-request.service.d.ts +6 -0
  216. package/dist/modules/workflow/git/pull-request.service.d.ts.map +1 -1
  217. package/dist/modules/workflow/git/pull-request.service.js +8 -0
  218. package/dist/modules/workflow/git/pull-request.service.js.map +1 -1
  219. package/dist/modules/workflow/workflow.service.d.ts +52 -1
  220. package/dist/modules/workflow/workflow.service.d.ts.map +1 -1
  221. package/dist/modules/workflow/workflow.service.js +242 -9
  222. package/dist/modules/workflow/workflow.service.js.map +1 -1
  223. package/dist/modules/workspace/startup/steps/plugin-manifests.step.d.ts +35 -0
  224. package/dist/modules/workspace/startup/steps/plugin-manifests.step.d.ts.map +1 -0
  225. package/dist/modules/workspace/startup/steps/plugin-manifests.step.js +121 -0
  226. package/dist/modules/workspace/startup/steps/plugin-manifests.step.js.map +1 -0
  227. package/dist/modules/workspace/startup/steps/seed-tree.d.ts.map +1 -1
  228. package/dist/modules/workspace/startup/steps/seed-tree.js +46 -2
  229. package/dist/modules/workspace/startup/steps/seed-tree.js.map +1 -1
  230. package/dist/modules/workspace/startup/steps/template-files.step.d.ts.map +1 -1
  231. package/dist/modules/workspace/startup/steps/template-files.step.js +112 -25
  232. package/dist/modules/workspace/startup/steps/template-files.step.js.map +1 -1
  233. package/dist/modules/workspace/workspace.routes.d.ts.map +1 -1
  234. package/dist/modules/workspace/workspace.routes.js +9 -9
  235. package/dist/modules/workspace/workspace.routes.js.map +1 -1
  236. package/dist/modules/workspace/workspace.service.d.ts +33 -0
  237. package/dist/modules/workspace/workspace.service.d.ts.map +1 -1
  238. package/dist/modules/workspace/workspace.service.js +110 -16
  239. package/dist/modules/workspace/workspace.service.js.map +1 -1
  240. package/dist/shared/domain-errors.d.ts +20 -0
  241. package/dist/shared/domain-errors.d.ts.map +1 -1
  242. package/dist/shared/domain-errors.js +29 -0
  243. package/dist/shared/domain-errors.js.map +1 -1
  244. package/dist/shared/fs-errors.d.ts +8 -0
  245. package/dist/shared/fs-errors.d.ts.map +1 -0
  246. package/dist/shared/fs-errors.js +11 -0
  247. package/dist/shared/fs-errors.js.map +1 -0
  248. package/dist/shared/fs-walk.d.ts +20 -6
  249. package/dist/shared/fs-walk.d.ts.map +1 -1
  250. package/dist/shared/fs-walk.js +35 -27
  251. package/dist/shared/fs-walk.js.map +1 -1
  252. package/dist/shared/kb-walk.d.ts +52 -0
  253. package/dist/shared/kb-walk.d.ts.map +1 -0
  254. package/dist/shared/kb-walk.js +76 -0
  255. package/dist/shared/kb-walk.js.map +1 -0
  256. package/dist/shared/path-order.d.ts +18 -0
  257. package/dist/shared/path-order.d.ts.map +1 -0
  258. package/dist/shared/path-order.js +35 -0
  259. package/dist/shared/path-order.js.map +1 -0
  260. package/dist/shared/ttl-cache.d.ts +28 -1
  261. package/dist/shared/ttl-cache.d.ts.map +1 -1
  262. package/dist/shared/ttl-cache.js +33 -1
  263. package/dist/shared/ttl-cache.js.map +1 -1
  264. package/kb-template/.bevelignore +1 -1
  265. package/kb-template/AGENTS.md +95 -30
  266. package/kb-template/Skills/.gitkeep +0 -0
  267. package/migrations/0005_github_facade.sql +25 -0
  268. package/migrations/meta/0005_snapshot.json +1717 -0
  269. package/migrations/meta/_journal.json +7 -0
  270. package/package.json +6 -6
  271. package/src/__tests__/kb-layout-config.test.ts +110 -0
  272. package/src/core/__tests__/catalog-cache-invalidation.test.ts +146 -0
  273. package/src/core/catalog-cache-invalidation.ts +92 -0
  274. package/src/core/create-core-server.ts +123 -7
  275. package/src/core/create-core-services.ts +162 -41
  276. package/src/modules/access/__tests__/access-control.invalidate-race.test.ts +86 -0
  277. package/src/modules/access/__tests__/access-control.service.test.ts +19 -3
  278. package/src/modules/access/__tests__/access-plugin-principals.test.ts +368 -0
  279. package/src/modules/access/__tests__/groups-admin.service.test.ts +8 -0
  280. package/src/modules/access/access-control.interface.ts +20 -9
  281. package/src/modules/access/access-control.service.ts +240 -78
  282. package/src/modules/access/access.routes.ts +96 -14
  283. package/src/modules/access-model/__tests__/access-grammar.test.ts +18 -0
  284. package/src/modules/access-model/__tests__/plugin-token-canonical.test.ts +40 -0
  285. package/src/modules/access-model/access-grammar.ts +114 -2
  286. package/src/modules/access-model/group-files.ts +4 -0
  287. package/src/modules/access-model/plugin-principals.ts +137 -0
  288. package/src/modules/database/core-schema.ts +54 -0
  289. package/src/modules/kb-sync/__tests__/kb-sync.routes.test.ts +297 -0
  290. package/src/modules/kb-sync/__tests__/kb-sync.service.test.ts +295 -0
  291. package/src/modules/kb-sync/__tests__/sync-auth.test.ts +118 -0
  292. package/src/modules/kb-sync/__tests__/sync-payload.test.ts +148 -0
  293. package/src/modules/kb-sync/index.ts +19 -0
  294. package/src/modules/kb-sync/kb-sync.interface.ts +79 -0
  295. package/src/modules/kb-sync/kb-sync.routes.ts +197 -0
  296. package/src/modules/kb-sync/kb-sync.service.ts +201 -0
  297. package/src/modules/kb-sync/sync-auth.ts +118 -0
  298. package/src/modules/kb-sync/sync-payload.ts +143 -0
  299. package/src/modules/marketplace/__tests__/github-facade.test.ts +443 -0
  300. package/src/modules/marketplace/__tests__/marketplace-git.test.ts +179 -0
  301. package/src/modules/marketplace/git-http.routes.ts +193 -0
  302. package/src/modules/marketplace/github-facade/github-facade-admin.routes.ts +78 -0
  303. package/src/modules/marketplace/github-facade/github-facade-codes.store.ts +145 -0
  304. package/src/modules/marketplace/github-facade/github-facade-credentials.service.ts +208 -0
  305. package/src/modules/marketplace/github-facade/github-facade.routes.ts +268 -0
  306. package/src/modules/marketplace/github-facade/github-facade.service.ts +221 -0
  307. package/src/modules/marketplace/github-facade/index.ts +25 -0
  308. package/src/modules/marketplace/index.ts +3 -0
  309. package/src/modules/marketplace/marketplace-repo.service.ts +337 -0
  310. package/src/modules/mcp/oauth/bevel-oauth-provider.ts +3 -0
  311. package/src/modules/mcp/oauth/oauth-consent.routes.ts +24 -2
  312. package/src/modules/mcp/oauth/oauth-state.ts +15 -3
  313. package/src/modules/plugins/__tests__/bundle-dialect.e2e.test.ts +163 -0
  314. package/src/modules/plugins/__tests__/compile-marketplace.test.ts +294 -0
  315. package/src/modules/plugins/__tests__/plugin-index.service.test.ts +56 -12
  316. package/src/modules/plugins/__tests__/plugin-links.service.test.ts +287 -0
  317. package/src/modules/plugins/__tests__/plugin-provision.service.test.ts +205 -5
  318. package/src/modules/plugins/__tests__/plugin-rename.service.test.ts +394 -0
  319. package/src/modules/plugins/__tests__/plugins.routes.test.ts +80 -33
  320. package/src/modules/plugins/compile/compile-marketplace.ts +403 -0
  321. package/src/modules/plugins/compile/marketplace-compiler.service.ts +150 -0
  322. package/src/modules/plugins/discovery/__tests__/kb-plugin-source.test.ts +329 -0
  323. package/src/modules/plugins/discovery/bundle-dialect/bundle.source.ts +140 -0
  324. package/src/modules/plugins/discovery/bundle-dialect/registry.ts +175 -0
  325. package/src/modules/plugins/discovery/kb-plugin-source.ts +114 -0
  326. package/src/modules/plugins/discovery/native.source.ts +122 -0
  327. package/src/modules/plugins/discovery/plugin-source.ts +99 -0
  328. package/src/modules/plugins/index.ts +10 -0
  329. package/src/modules/plugins/plugin-links.service.ts +363 -0
  330. package/src/modules/plugins/plugin-links.ts +184 -0
  331. package/src/modules/plugins/plugin-provision.service.ts +160 -54
  332. package/src/modules/plugins/plugin-rename.service.ts +298 -0
  333. package/src/modules/plugins/plugins.contract.ts +28 -1
  334. package/src/modules/plugins/plugins.routes.ts +128 -11
  335. package/src/modules/plugins/plugins.service.ts +106 -53
  336. package/src/modules/settings/__tests__/deployment-settings.service.test.ts +57 -0
  337. package/src/modules/settings/__tests__/setup.routes.sync-status.test.ts +77 -0
  338. package/src/modules/settings/deployment-settings.service.ts +95 -2
  339. package/src/modules/settings/setup.routes.ts +24 -0
  340. package/src/modules/skills/__tests__/skill-access-requests.routes.test.ts +128 -0
  341. package/src/modules/skills/__tests__/skills.service.test.ts +85 -0
  342. package/src/modules/skills/index.ts +1 -0
  343. package/src/modules/skills/pending-skills.service.ts +7 -5
  344. package/src/modules/skills/skill-access-requests.routes.ts +199 -0
  345. package/src/modules/skills/skills.contract.ts +32 -0
  346. package/src/modules/skills/skills.routes.ts +59 -3
  347. package/src/modules/skills/skills.service.ts +84 -22
  348. package/src/modules/tool-auth/external-api-key.interface.ts +20 -1
  349. package/src/modules/tool-auth/external-api-key.service.ts +31 -13
  350. package/src/modules/tool-manuals/__tests__/mcp-json-discovery.test.ts +24 -1
  351. package/src/modules/tool-manuals/__tests__/tool-manuals.cli.test.ts +2 -0
  352. package/src/modules/tool-manuals/mcp-json-discovery.ts +154 -88
  353. package/src/modules/tool-manuals/tool-manuals.service.ts +14 -23
  354. package/src/modules/workflow/__tests__/preserve-roles-yaml.test.ts +1 -1
  355. package/src/modules/workflow/__tests__/workflow.service.commitFileWhileLocked.test.ts +1 -0
  356. package/src/modules/workflow/__tests__/workflow.service.facade.test.ts +2 -2
  357. package/src/modules/workflow/__tests__/workflow.service.releaseLock.test.ts +119 -2
  358. package/src/modules/workflow/__tests__/workflow.service.sync.test.ts +437 -0
  359. package/src/modules/workflow/git/__tests__/git.service.postMergePull.test.ts +3 -3
  360. package/src/modules/workflow/git/__tests__/git.service.pull.test.ts +47 -4
  361. package/src/modules/workflow/git/__tests__/git.service.syncFromRemote.test.ts +253 -0
  362. package/src/modules/workflow/git/git.service.ts +239 -56
  363. package/src/modules/workflow/git/pull-request.service.ts +9 -0
  364. package/src/modules/workflow/workflow.service.ts +260 -9
  365. package/src/modules/workspace/__tests__/workspace.routes.read-gate.test.ts +6 -7
  366. package/src/modules/workspace/__tests__/workspace.service.list-cloned.test.ts +93 -0
  367. package/src/modules/workspace/__tests__/workspace.service.missing-branch.test.ts +28 -0
  368. package/src/modules/workspace/__tests__/workspace.service.read-filter.test.ts +42 -12
  369. package/src/modules/workspace/startup/steps/__tests__/steps.test.ts +219 -4
  370. package/src/modules/workspace/startup/steps/plugin-manifests.step.ts +129 -0
  371. package/src/modules/workspace/startup/steps/seed-tree.ts +44 -2
  372. package/src/modules/workspace/startup/steps/template-files.step.ts +131 -27
  373. package/src/modules/workspace/workspace.routes.ts +9 -17
  374. package/src/modules/workspace/workspace.service.ts +107 -16
  375. package/src/shared/__tests__/filename.test.ts +0 -0
  376. package/src/shared/__tests__/fs-walk.test.ts +22 -1
  377. package/src/shared/__tests__/kb-walk.test.ts +107 -0
  378. package/src/shared/__tests__/path-order.test.ts +24 -0
  379. package/src/shared/__tests__/ttl-cache.test.ts +55 -0
  380. package/src/shared/domain-errors.ts +29 -0
  381. package/src/shared/fs-errors.ts +10 -0
  382. package/src/shared/fs-walk.ts +38 -23
  383. package/src/shared/kb-walk.ts +95 -0
  384. package/src/shared/path-order.ts +33 -0
  385. package/src/shared/ttl-cache.ts +33 -1
@@ -0,0 +1,70 @@
1
+ import express from 'express';
2
+ import type { IAdminAccessService } from '../admin/admin.interface.js';
3
+ import type { IKbSyncService, SyncResult } from './kb-sync.interface.js';
4
+ /**
5
+ * Remote sync — a git host or a pipeline tells Hexis the repository changed.
6
+ *
7
+ * POST /api/sync/<branch> sync that one branch — the branch whose change
8
+ * made the call. What a GitHub Action or a
9
+ * pipeline step uses: the branch name goes in the
10
+ * URL, the body is ignored for branch selection.
11
+ * POST /api/sync the branches named by the body — an Azure
12
+ * DevOps service hook or a GitHub/GitLab webhook
13
+ * payload (they cannot template the URL), an
14
+ * explicit `{ "branches": [...] }`, or nothing,
15
+ * which syncs every clone Hexis holds.
16
+ *
17
+ * Mounted BEFORE every JWT-protected `/api` mount: the caller is a machine
18
+ * holding the deployment's sync secret, not a browser session, and Express 5
19
+ * would otherwise 401 it in the first protected mount. The route parses its
20
+ * own body as raw bytes (the global JSON parser skips this path) because the
21
+ * GitHub-style credential is an HMAC over exactly the bytes that arrived.
22
+ *
23
+ * Status codes are chosen for the two kinds of caller. A pipeline's
24
+ * `curl --fail` must fail exactly when Hexis is not in sync; a webhook host
25
+ * retries 5xx and gives up on 4xx:
26
+ *
27
+ * 200 every branch updated, already current, or not cloned here
28
+ * 409 a branch has a CONFLICT: Hexis-side commits clash with the host.
29
+ * Automatic recovery is queued, but the branch is not in sync at the
30
+ * time of the answer; the body's `error` names the files. A retry
31
+ * answers 200 once recovery (or a person) has cleared it.
32
+ * 503 a branch could not be pulled (origin unreachable, credential
33
+ * refused) — retriable. Also: no sync secret configured.
34
+ * 401 missing or wrong credential
35
+ * 400 a branch name git would refuse, or a body that is not JSON
36
+ */
37
+ export interface KbSyncRouteDeps {
38
+ kbSync: IKbSyncService;
39
+ /** The configured secret, read per request so a setup-screen save applies at once. */
40
+ syncSecret: () => string;
41
+ authService: {
42
+ verifyToken(token: string): {
43
+ userId: string;
44
+ email: string;
45
+ };
46
+ };
47
+ adminAccess: IAdminAccessService;
48
+ }
49
+ /**
50
+ * Stamped on EVERY response this router writes. The endpoint answers 503 for
51
+ * an ordinary result (a branch could not be pulled), and a reverse proxy
52
+ * answers 503 when the backend is down; by status alone the browser cannot
53
+ * tell them apart, and it must — one is a line on the page, the other the
54
+ * maintenance overlay. A proxy standing in for a dead upstream never sets
55
+ * this header. Exposed to cross-origin callers in `create-core-server.ts`.
56
+ */
57
+ export declare const SYNC_RESPONSE_HEADER = "X-Hexis-Sync";
58
+ export declare const SYNC_RESPONSE_MARKER = "result";
59
+ /**
60
+ * Whether a request path belongs to this router and must reach it with its
61
+ * body UNPARSED. The server's global JSON parser consults this: once that
62
+ * parser has read the stream, `express.raw` below sees nothing and the
63
+ * signature credential has no bytes to verify. Both routes share the check,
64
+ * so the branch form (`/api/sync/<branch>`) is covered as well as the bare
65
+ * one — an exact-path exemption used to protect only the latter.
66
+ */
67
+ export declare function isSyncRawBodyPath(path: string): boolean;
68
+ export declare function httpStatusFor(result: SyncResult): 200 | 409 | 503;
69
+ export declare function createKbSyncRoutes(deps: KbSyncRouteDeps): express.Router;
70
+ //# sourceMappingURL=kb-sync.routes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kb-sync.routes.d.ts","sourceRoot":"","sources":["../../../src/modules/kb-sync/kb-sync.routes.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,6BAA6B,CAAC;AAEvE,OAAO,KAAK,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAIzE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,cAAc,CAAC;IACvB,sFAAsF;IACtF,UAAU,EAAE,MAAM,MAAM,CAAC;IACzB,WAAW,EAAE;QAAE,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG;YAAE,MAAM,EAAE,MAAM,CAAC;YAAC,KAAK,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC;IAC/E,WAAW,EAAE,mBAAmB,CAAC;CAClC;AAKD;;;;;;;GAOG;AACH,eAAO,MAAM,oBAAoB,iBAAiB,CAAC;AACnD,eAAO,MAAM,oBAAoB,WAAW,CAAC;AAE7C;;;;;;;GAOG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CASvD;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,GAAG,GAAG,GAAG,GAAG,GAAG,CAIjE;AAID,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAsGxE"}
@@ -0,0 +1,135 @@
1
+ import express from 'express';
2
+ import { assertValidBranchName } from '../kb-fs/branch-name.js';
3
+ import { parseSyncPayload } from './sync-payload.js';
4
+ import { verifySyncCredential } from './sync-auth.js';
5
+ /** Cap on a webhook body. ADO's PR payloads run to a few hundred KB. */
6
+ const BODY_LIMIT = '2mb';
7
+ /**
8
+ * Stamped on EVERY response this router writes. The endpoint answers 503 for
9
+ * an ordinary result (a branch could not be pulled), and a reverse proxy
10
+ * answers 503 when the backend is down; by status alone the browser cannot
11
+ * tell them apart, and it must — one is a line on the page, the other the
12
+ * maintenance overlay. A proxy standing in for a dead upstream never sets
13
+ * this header. Exposed to cross-origin callers in `create-core-server.ts`.
14
+ */
15
+ export const SYNC_RESPONSE_HEADER = 'X-Hexis-Sync';
16
+ export const SYNC_RESPONSE_MARKER = 'result';
17
+ /**
18
+ * Whether a request path belongs to this router and must reach it with its
19
+ * body UNPARSED. The server's global JSON parser consults this: once that
20
+ * parser has read the stream, `express.raw` below sees nothing and the
21
+ * signature credential has no bytes to verify. Both routes share the check,
22
+ * so the branch form (`/api/sync/<branch>`) is covered as well as the bare
23
+ * one — an exact-path exemption used to protect only the latter.
24
+ */
25
+ export function isSyncRawBodyPath(path) {
26
+ // Case-insensitive, because Express routing is: `/api/Sync` reaches this
27
+ // router, and if this check said no the global parser would eat the body
28
+ // first. On either form a signed call would then 401 (no bytes to verify).
29
+ // On the bare form a bearer call would silently sync every clone, since a
30
+ // body that is no longer a Buffer parses as "no body"; the branch form is
31
+ // unaffected there, its branch comes from the URL.
32
+ const lower = path.toLowerCase();
33
+ return lower === '/api/sync' || lower.startsWith('/api/sync/');
34
+ }
35
+ export function httpStatusFor(result) {
36
+ if (result.results.some((r) => r.outcome === 'conflict'))
37
+ return 409;
38
+ if (result.results.some((r) => r.outcome === 'error'))
39
+ return 503;
40
+ return 200;
41
+ }
42
+ export function createKbSyncRoutes(deps) {
43
+ const router = express.Router();
44
+ const rawBody = express.raw({ type: () => true, limit: BODY_LIMIT });
45
+ /**
46
+ * Everything both routes share: the credential check, then the sync and
47
+ * its answer. `select` turns the request into the branches to sync, or
48
+ * sends its own 400 and returns null.
49
+ */
50
+ async function handle(req, res, select) {
51
+ res.setHeader(SYNC_RESPONSE_HEADER, SYNC_RESPONSE_MARKER);
52
+ const raw = Buffer.isBuffer(req.body) ? req.body : undefined;
53
+ const auth = await verifySyncCredential({
54
+ secret: deps.syncSecret(),
55
+ authorization: header(req, 'authorization'),
56
+ gitlabToken: header(req, 'x-gitlab-token'),
57
+ hubSignature: header(req, 'x-hub-signature-256'),
58
+ rawBody: raw,
59
+ }, {
60
+ verifyJwt: (token) => {
61
+ try {
62
+ return { email: deps.authService.verifyToken(token).email };
63
+ }
64
+ catch {
65
+ return null;
66
+ }
67
+ },
68
+ isAdmin: (email) => deps.adminAccess.isAdmin(email),
69
+ });
70
+ if (!auth.ok) {
71
+ if (auth.status === 401)
72
+ res.setHeader('WWW-Authenticate', 'Bearer realm="hexis-sync"');
73
+ res.status(auth.status).json({ error: auth.message });
74
+ return;
75
+ }
76
+ const selection = select();
77
+ if (!selection)
78
+ return;
79
+ const who = auth.credential.kind === 'admin-session' ? auth.credential.email : auth.credential.kind;
80
+ const asked = selection.branches === 'all' ? 'all cloned branches' : selection.branches.join(', ') || '(none)';
81
+ try {
82
+ const result = await deps.kbSync.sync({ branches: selection.branches, by: who });
83
+ const summary = result.results.map((r) => `${r.branch}=${r.outcome}`).join(' ');
84
+ console.log(`[sync] by ${who} via ${selection.source} for ${asked}: ${result.status} ${summary}`);
85
+ res.status(httpStatusFor(result)).json(result);
86
+ }
87
+ catch (err) {
88
+ console.error('[sync] unhandled error:', err);
89
+ res.status(500).json({ error: 'Internal server error' });
90
+ }
91
+ }
92
+ // The branch in the URL. A wildcard rather than `:branch` so a branch with
93
+ // slashes (`ali/new-skill`) works spelled either way — as segments or
94
+ // percent-encoded as one.
95
+ router.post('/sync/*branch', rawBody, (req, res) => handle(req, res, () => {
96
+ const param = req.params.branch;
97
+ const branch = (Array.isArray(param) ? param.join('/') : String(param ?? '')).trim();
98
+ try {
99
+ assertValidBranchName(branch);
100
+ }
101
+ catch {
102
+ res.status(400).json({ error: `Not a valid branch name: ${branch}` });
103
+ return null;
104
+ }
105
+ return { source: 'path', branches: [branch] };
106
+ }));
107
+ router.post('/sync', rawBody, (req, res) => handle(req, res, () => {
108
+ const raw = Buffer.isBuffer(req.body) ? req.body : undefined;
109
+ let body = undefined;
110
+ if (raw && raw.length > 0) {
111
+ try {
112
+ body = JSON.parse(raw.toString('utf8'));
113
+ }
114
+ catch {
115
+ res.status(400).json({ error: 'The request body is not valid JSON.' });
116
+ return null;
117
+ }
118
+ }
119
+ const parsed = parseSyncPayload(body);
120
+ if (parsed.invalid.length > 0) {
121
+ res.status(400).json({
122
+ error: `Not a valid branch name: ${parsed.invalid.join(', ')}`,
123
+ invalid: parsed.invalid,
124
+ });
125
+ return null;
126
+ }
127
+ return { source: parsed.source, branches: parsed.branches };
128
+ }));
129
+ return router;
130
+ }
131
+ function header(req, name) {
132
+ const v = req.headers[name];
133
+ return typeof v === 'string' ? v : undefined;
134
+ }
135
+ //# sourceMappingURL=kb-sync.routes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kb-sync.routes.js","sourceRoot":"","sources":["../../../src/modules/kb-sync/kb-sync.routes.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAEhE,OAAO,EAAE,gBAAgB,EAA0B,MAAM,mBAAmB,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AA2CtD,wEAAwE;AACxE,MAAM,UAAU,GAAG,KAAK,CAAC;AAEzB;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,cAAc,CAAC;AACnD,MAAM,CAAC,MAAM,oBAAoB,GAAG,QAAQ,CAAC;AAE7C;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,IAAY;IAC5C,yEAAyE;IACzE,yEAAyE;IACzE,2EAA2E;IAC3E,0EAA0E;IAC1E,0EAA0E;IAC1E,mDAAmD;IACnD,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;IACjC,OAAO,KAAK,KAAK,WAAW,IAAI,KAAK,CAAC,UAAU,CAAC,YAAY,CAAC,CAAC;AACjE,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,MAAkB;IAC9C,IAAI,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,C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OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,MAAM,CAAC,GAAoB,EAAE,IAAY;IAChD,MAAM,CAAC,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IAC5B,OAAO,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AAC/C,CAAC"}
@@ -0,0 +1,45 @@
1
+ import type { IKbSyncService, LastSync, SyncRequest, SyncResult, SyncWorkflowPort, SyncWorkspacePort } from './kb-sync.interface.js';
2
+ /**
3
+ * Runs remote syncs one at a time and folds everything that arrives
4
+ * mid-run into ONE follow-up.
5
+ *
6
+ * Why coalesce: a git host fires several hooks for one event — Azure DevOps
7
+ * sends a push, a PR-updated and a PR-merged within a second of each other —
8
+ * and each would otherwise queue behind the workspace mutex to pull the same
9
+ * tree again. The follow-up pulls the union of every request that arrived
10
+ * during the run (an "all" absorbs the rest).
11
+ *
12
+ * But each caller is answered for the branches IT asked about. The endpoint
13
+ * promises to fail exactly when the caller's branch is not in sync; a hook
14
+ * for branch A answered with branch B's conflict would fail the wrong
15
+ * subscription — and a host that disables subscriptions after repeated
16
+ * failures would disable the wrong one.
17
+ *
18
+ * Branches are pulled sequentially. N is small (only cloned branches count),
19
+ * the per-clone mutex serialises them anyway, and one slow origin round-trip
20
+ * at a time keeps the process calm during a burst.
21
+ */
22
+ export declare class KbSyncService implements IKbSyncService {
23
+ private readonly workflow;
24
+ private readonly workspaces;
25
+ private readonly now;
26
+ private inFlight;
27
+ /** Everyone who arrived mid-run, each with what they asked for. */
28
+ private waiters;
29
+ private last;
30
+ constructor(workflow: SyncWorkflowPort, workspaces: SyncWorkspacePort, now?: () => number);
31
+ lastSync(): LastSync | null;
32
+ sync(request: SyncRequest): Promise<SyncResult>;
33
+ private start;
34
+ /**
35
+ * Run ONE follow-up for everyone who arrived during the last run: the union
36
+ * of their branches, then each waiter answered from its own share of the
37
+ * result — never from another caller's branches.
38
+ */
39
+ private drain;
40
+ /** The part of a union run that answers one caller: its branches, and its status over them. */
41
+ private static subsetFor;
42
+ private static statusOf;
43
+ private run;
44
+ }
45
+ //# sourceMappingURL=kb-sync.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kb-sync.service.d.ts","sourceRoot":"","sources":["../../../src/modules/kb-sync/kb-sync.service.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EACV,cAAc,EACd,QAAQ,EACR,WAAW,EACX,UAAU,EACV,gBAAgB,EAChB,iBAAiB,EAClB,MAAM,wBAAwB,CAAC;AAEhC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,aAAc,YAAW,cAAc;IAYhD,OAAO,CAAC,QAAQ,CAAC,QAAQ;IACzB,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,GAAG;IAbtB,OAAO,CAAC,QAAQ,CAAiC;IACjD,mEAAmE;IACnE,OAAO,CAAC,OAAO,CAKP;IACR,OAAO,CAAC,IAAI,CAAyB;gBAGlB,QAAQ,EAAE,gBAAgB,EAC1B,UAAU,EAAE,iBAAiB,EAC7B,GAAG,GAAE,MAAM,MAAyB;IAGvD,QAAQ,IAAI,QAAQ,GAAG,IAAI;IAI3B,IAAI,CAAC,OAAO,EAAE,WAAW,GAAG,OAAO,CAAC,UAAU,CAAC;IAW/C,OAAO,CAAC,KAAK;IASb;;;;OAIG;IACH,OAAO,CAAC,KAAK;IA2Bb,+FAA+F;IAC/F,OAAO,CAAC,MAAM,CAAC,SAAS;IAWxB,OAAO,CAAC,MAAM,CAAC,QAAQ;YAWT,GAAG;CAyElB"}
@@ -0,0 +1,183 @@
1
+ import { sanitizeError } from '../workflow/sanitize-error.js';
2
+ /**
3
+ * Runs remote syncs one at a time and folds everything that arrives
4
+ * mid-run into ONE follow-up.
5
+ *
6
+ * Why coalesce: a git host fires several hooks for one event — Azure DevOps
7
+ * sends a push, a PR-updated and a PR-merged within a second of each other —
8
+ * and each would otherwise queue behind the workspace mutex to pull the same
9
+ * tree again. The follow-up pulls the union of every request that arrived
10
+ * during the run (an "all" absorbs the rest).
11
+ *
12
+ * But each caller is answered for the branches IT asked about. The endpoint
13
+ * promises to fail exactly when the caller's branch is not in sync; a hook
14
+ * for branch A answered with branch B's conflict would fail the wrong
15
+ * subscription — and a host that disables subscriptions after repeated
16
+ * failures would disable the wrong one.
17
+ *
18
+ * Branches are pulled sequentially. N is small (only cloned branches count),
19
+ * the per-clone mutex serialises them anyway, and one slow origin round-trip
20
+ * at a time keeps the process calm during a burst.
21
+ */
22
+ export class KbSyncService {
23
+ workflow;
24
+ workspaces;
25
+ now;
26
+ inFlight = null;
27
+ /** Everyone who arrived mid-run, each with what they asked for. */
28
+ waiters = [];
29
+ last = null;
30
+ constructor(workflow, workspaces, now = () => Date.now()) {
31
+ this.workflow = workflow;
32
+ this.workspaces = workspaces;
33
+ this.now = now;
34
+ }
35
+ lastSync() {
36
+ return this.last;
37
+ }
38
+ sync(request) {
39
+ if (!this.inFlight)
40
+ return this.start(request);
41
+ return new Promise((resolve, reject) => {
42
+ // Copied on entry: the caller keeps its array, and a caller that reuses
43
+ // or edits it before the follow-up runs must not change what is pulled
44
+ // or what it is answered for.
45
+ const branches = request.branches === 'all' ? 'all' : [...request.branches];
46
+ this.waiters.push({ branches, by: request.by, resolve, reject });
47
+ });
48
+ }
49
+ start(request) {
50
+ const run = this.run(request);
51
+ this.inFlight = run.catch(() => undefined).finally(() => {
52
+ this.inFlight = null;
53
+ this.drain();
54
+ });
55
+ return run;
56
+ }
57
+ /**
58
+ * Run ONE follow-up for everyone who arrived during the last run: the union
59
+ * of their branches, then each waiter answered from its own share of the
60
+ * result — never from another caller's branches.
61
+ */
62
+ drain() {
63
+ if (this.waiters.length === 0)
64
+ return;
65
+ const waiters = this.waiters;
66
+ this.waiters = [];
67
+ const union = new Set();
68
+ let all = false;
69
+ const by = new Set();
70
+ for (const w of waiters) {
71
+ if (w.branches === 'all')
72
+ all = true;
73
+ else
74
+ for (const b of w.branches)
75
+ union.add(b);
76
+ if (w.by)
77
+ by.add(w.by);
78
+ }
79
+ const request = {
80
+ branches: all ? 'all' : [...union],
81
+ by: by.size > 0 ? [...by].join(', ') : undefined,
82
+ };
83
+ this.start(request).then((result) => {
84
+ for (const w of waiters)
85
+ w.resolve(KbSyncService.subsetFor(result, w.branches));
86
+ }, (err) => {
87
+ for (const w of waiters)
88
+ w.reject(err);
89
+ });
90
+ }
91
+ /** The part of a union run that answers one caller: its branches, and its status over them. */
92
+ static subsetFor(result, branches) {
93
+ if (branches === 'all')
94
+ return result;
95
+ const wanted = new Set(branches);
96
+ const results = result.results.filter((r) => wanted.has(r.branch));
97
+ return {
98
+ status: KbSyncService.statusOf(results),
99
+ results,
100
+ changeRequests: result.changeRequests,
101
+ };
102
+ }
103
+ static statusOf(results) {
104
+ const clean = results.every((r) => r.outcome === 'updated' ||
105
+ r.outcome === 'up-to-date' ||
106
+ r.outcome === 'not-cloned' ||
107
+ r.outcome === 'remote-gone');
108
+ return clean ? 'synced' : 'partial';
109
+ }
110
+ async run(request) {
111
+ const known = await this.workspaces.listClonedWorkspaces();
112
+ const byBranch = new Map(known.map((w) => [w.branch, w.id]));
113
+ const targets = request.branches === 'all'
114
+ ? known.map((w) => ({ branch: w.branch, id: w.id }))
115
+ : request.branches.map((branch) => ({ branch, id: byBranch.get(branch) ?? null }));
116
+ const unreadable = new Map(known.filter((w) => w.unreadable).map((w) => [w.branch, w.unreadable]));
117
+ const results = [];
118
+ for (const { branch, id } of targets) {
119
+ if (id === null) {
120
+ results.push({ branch, outcome: 'not-cloned' });
121
+ continue;
122
+ }
123
+ const cannotRead = unreadable.get(branch);
124
+ if (cannotRead) {
125
+ results.push({ branch, outcome: 'error', error: cannotRead });
126
+ continue;
127
+ }
128
+ let outcome;
129
+ try {
130
+ outcome = await this.workflow.syncWorkspaceFromRemote(id);
131
+ }
132
+ catch (err) {
133
+ // The workflow step promises outcomes, not throws — but one broken
134
+ // clone must never take the other branches, the sweep, or every
135
+ // coalesced caller down with it, so the promise is enforced here too.
136
+ const message = sanitizeError(err);
137
+ console.error(`[sync] unexpected failure syncing "${branch}": ${message}`);
138
+ outcome = { branch, outcome: 'error', error: message };
139
+ }
140
+ results.push(outcome);
141
+ if (outcome.outcome === 'remote-gone') {
142
+ // The host deleted the branch. Left in place, the stale clone would
143
+ // fail its fetch on every following full sync — and a webhook host
144
+ // disables a subscription that keeps failing. The workflow retires it
145
+ // under the branch-lifecycle lock, after asking origin again, so a
146
+ // branch recreated since the pull keeps its clone. Best-effort.
147
+ try {
148
+ const removed = await this.workflow.retireRemoteGoneClone(id);
149
+ if (removed)
150
+ console.log(`[sync] removed the clone of "${branch}" — deleted on the host`);
151
+ else
152
+ console.log(`[sync] kept the clone of "${branch}" — it is back on the host, or already gone`);
153
+ }
154
+ catch (err) {
155
+ console.warn(`[sync] could not remove the clone of "${branch}":`, sanitizeError(err));
156
+ }
157
+ }
158
+ }
159
+ // The host may have deleted a branch (an ADO PR completed with "delete
160
+ // source branch"); the existing sweep closes the Hexis request that
161
+ // pointed at it. Best-effort — it fails safe by closing nothing.
162
+ let closedDeletedBranch = 0;
163
+ try {
164
+ closedDeletedBranch = await this.workflow.closeChangeRequestsWithDeletedBranches();
165
+ }
166
+ catch (err) {
167
+ console.warn('[sync] deleted-branch sweep failed:', err instanceof Error ? err.message : err);
168
+ }
169
+ const result = {
170
+ status: KbSyncService.statusOf(results),
171
+ results,
172
+ changeRequests: { closedDeletedBranch },
173
+ };
174
+ this.last = {
175
+ at: this.now(),
176
+ by: request.by ?? 'unknown',
177
+ status: result.status,
178
+ results: [...results],
179
+ };
180
+ return result;
181
+ }
182
+ }
183
+ //# sourceMappingURL=kb-sync.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"kb-sync.service.js","sourceRoot":"","sources":["../../../src/modules/kb-sync/kb-sync.service.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,C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,CAAC;YACb,OAAO,CAAC,IAAI,CAAC,qCAAqC,EAAE,GAAG,YAAY,KAAK,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAChG,CAAC;QAED,MAAM,MAAM,GAAe;YACzB,MAAM,EAAE,aAAa,CAAC,QAAQ,CAAC,OAAO,CAAC;YACvC,OAAO;YACP,cAAc,EAAE,EAAE,mBAAmB,EAAE;SACxC,CAAC;QACF,IAAI,CAAC,IAAI,GAAG;YACV,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE;YACd,EAAE,EAAE,OAAO,CAAC,EAAE,IAAI,SAAS;YAC3B,MAAM,EAAE,MAAM,CAAC,MAAM;YACrB,OAAO,EAAE,CAAC,GAAG,OAAO,CAAC;SACtB,CAAC;QACF,OAAO,MAAM,CAAC;IAChB,CAAC;CACF"}
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Who may call `POST /api/sync`, decided from the request's credentials.
3
+ *
4
+ * Framework-agnostic on purpose (the route hands in plain strings and the
5
+ * raw body) so the rule is one function with one test, not something spread
6
+ * across middleware.
7
+ *
8
+ * Four credentials are accepted, because the callers cannot all speak the
9
+ * same one:
10
+ *
11
+ * bearer `Authorization: Bearer <secret>` — ADO Web Hooks and
12
+ * any pipeline can set a header.
13
+ * gitlab-token `X-Gitlab-Token: <secret>` — what GitLab sends.
14
+ * github-signature `X-Hub-Signature-256: sha256=<hmac of the raw body>` —
15
+ * GitHub and Gitea cannot set headers, only sign.
16
+ * admin-session a browser JWT whose user is an admin, so a "Sync now"
17
+ * button and a curl with your own session both work while
18
+ * the hook is being wired up.
19
+ *
20
+ * Every comparison is constant-time. The secret never travels in the URL.
21
+ */
22
+ export type SyncCredential = {
23
+ kind: 'bearer' | 'gitlab-token' | 'github-signature';
24
+ } | {
25
+ kind: 'admin-session';
26
+ email: string;
27
+ };
28
+ export type SyncAuthResult = {
29
+ ok: true;
30
+ credential: SyncCredential;
31
+ } | {
32
+ ok: false;
33
+ status: 401 | 503;
34
+ message: string;
35
+ };
36
+ export interface SyncAuthInput {
37
+ /** The configured secret, or empty when none is set. */
38
+ secret: string;
39
+ authorization?: string;
40
+ gitlabToken?: string;
41
+ hubSignature?: string;
42
+ /** The request body exactly as received — the HMAC is over these bytes. */
43
+ rawBody?: Buffer;
44
+ }
45
+ export interface SyncSessionVerifier {
46
+ /** Resolve a browser JWT to its email, or null when it is not one. */
47
+ verifyJwt(token: string): {
48
+ email: string;
49
+ } | null;
50
+ isAdmin(email: string): Promise<boolean>;
51
+ }
52
+ /**
53
+ * Shortest secret the endpoint will honour. The setup screen enforces the
54
+ * same floor on save; this is the boundary that also covers a secret set
55
+ * through the environment, which no validator ever sees.
56
+ */
57
+ export declare const MIN_SYNC_SECRET_LENGTH = 16;
58
+ export declare function verifySyncCredential(input: SyncAuthInput, session: SyncSessionVerifier): Promise<SyncAuthResult>;
59
+ //# sourceMappingURL=sync-auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-auth.d.ts","sourceRoot":"","sources":["../../../src/modules/kb-sync/sync-auth.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,cAAc,GACtB;IAAE,IAAI,EAAE,QAAQ,GAAG,cAAc,GAAG,kBAAkB,CAAA;CAAE,GACxD;IAAE,IAAI,EAAE,eAAe,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE7C,MAAM,MAAM,cAAc,GACtB;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,UAAU,EAAE,cAAc,CAAA;CAAE,GACxC;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,MAAM,EAAE,GAAG,GAAG,GAAG,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtD,MAAM,WAAW,aAAa;IAC5B,wDAAwD;IACxD,MAAM,EAAE,MAAM,CAAC;IACf,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,2EAA2E;IAC3E,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,mBAAmB;IAClC,sEAAsE;IACtE,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACnD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1C;AAMD;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,KAAK,CAAC;AAazC,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAAC,cAAc,CAAC,CA4CzB"}
@@ -0,0 +1,62 @@
1
+ import { createHmac } from 'node:crypto';
2
+ import { timingSafeStringEqual } from '../auth/password-hash.js';
3
+ const NO_SECRET = 'No sync secret is configured. Set KB_SYNC_SECRET (or the setup screen’s sync secret), ' +
4
+ 'or call this endpoint with an administrator session.';
5
+ /**
6
+ * Shortest secret the endpoint will honour. The setup screen enforces the
7
+ * same floor on save; this is the boundary that also covers a secret set
8
+ * through the environment, which no validator ever sees.
9
+ */
10
+ export const MIN_SYNC_SECRET_LENGTH = 16;
11
+ const SECRET_TOO_SHORT = `The configured sync secret is shorter than ${MIN_SYNC_SECRET_LENGTH} characters and is not ` +
12
+ 'accepted. Set a longer KB_SYNC_SECRET, or call this endpoint with an administrator session.';
13
+ function signatureMatches(secret, rawBody, header) {
14
+ const [scheme, hex] = header.split('=', 2);
15
+ if (scheme !== 'sha256' || !hex)
16
+ return false;
17
+ const expected = createHmac('sha256', secret).update(rawBody).digest('hex');
18
+ return timingSafeStringEqual(expected, hex.toLowerCase());
19
+ }
20
+ export async function verifySyncCredential(input, session) {
21
+ const configured = input.secret.trim();
22
+ // A too-short secret is treated as absent for matching purposes and named
23
+ // as the problem below, so a one-character deployment secret can never be
24
+ // what lets a caller in.
25
+ const usable = configured.length >= MIN_SYNC_SECRET_LENGTH;
26
+ const secret = usable ? configured : '';
27
+ const bearer = input.authorization?.startsWith('Bearer ')
28
+ ? input.authorization.slice('Bearer '.length).trim()
29
+ : '';
30
+ // A shared secret presented in any of its three shapes.
31
+ if (secret) {
32
+ if (bearer && timingSafeStringEqual(bearer, secret)) {
33
+ return { ok: true, credential: { kind: 'bearer' } };
34
+ }
35
+ if (input.gitlabToken && timingSafeStringEqual(input.gitlabToken.trim(), secret)) {
36
+ return { ok: true, credential: { kind: 'gitlab-token' } };
37
+ }
38
+ if (input.hubSignature && input.rawBody && signatureMatches(secret, input.rawBody, input.hubSignature.trim())) {
39
+ return { ok: true, credential: { kind: 'github-signature' } };
40
+ }
41
+ }
42
+ // An admin's own session, carried as the bearer. Checked after the secret
43
+ // so a secret that happens to parse as a JWT is never mistaken for one.
44
+ if (bearer) {
45
+ const claim = session.verifyJwt(bearer);
46
+ if (claim && (await session.isAdmin(claim.email))) {
47
+ return { ok: true, credential: { kind: 'admin-session', email: claim.email } };
48
+ }
49
+ }
50
+ // Nothing matched. Say WHY when the reason is the deployment's, not the
51
+ // caller's: a hook pointed at an unconfigured endpoint should read "set
52
+ // the secret", not "wrong secret".
53
+ if (!secret && (bearer || input.gitlabToken || input.hubSignature)) {
54
+ return { ok: false, status: 503, message: configured ? SECRET_TOO_SHORT : NO_SECRET };
55
+ }
56
+ return {
57
+ ok: false,
58
+ status: 401,
59
+ message: 'A valid sync secret or an administrator session is required.',
60
+ };
61
+ }
62
+ //# sourceMappingURL=sync-auth.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-auth.js","sourceRoot":"","sources":["../../../src/modules/kb-sync/sync-auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,qBAAqB,EAAE,MAAM,0BAA0B,CAAC;AA+CjE,MAAM,SAAS,GACb,wFA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AAE,GAAG,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IACxF,CAAC;IACD,OAAO;QACL,EAAE,EAAE,KAAK;QACT,MAAM,EAAE,GAAG;QACX,OAAO,EAAE,8DAA8D;KACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Turn whatever a caller POSTed to `/api/sync` into the branches to sync.
3
+ *
4
+ * Pure: no IO, no throwing on foreign shapes. The endpoint is meant to be
5
+ * pointed at by a webhook subscription whose payload the operator does not
6
+ * control, so an unrecognised body must degrade to "sync everything" — the
7
+ * safe answer, and the one an empty pipeline call gets too — never to a 400
8
+ * that makes the host disable the subscription.
9
+ *
10
+ * Recognised shapes:
11
+ *
12
+ * explicit `{ "branches": ["main", "ali/x"] }`
13
+ * azure-devops `eventType: git.push` → `resource.refUpdates[].name`
14
+ * `eventType: git.pullrequest.*` → `resource.targetRefName`
15
+ * and `resource.sourceRefName`
16
+ * github/gitea `{ "ref": "refs/heads/main" }` (push)
17
+ * gitlab `{ "object_kind": "push", "ref": "refs/heads/main" }`
18
+ *
19
+ * Only `refs/heads/*` refs count; a tag push names nothing to sync. A branch
20
+ * name git would refuse is dropped from a provider payload (the host
21
+ * produced it, nothing we can do) but reported for an explicit body, where
22
+ * it is the caller's mistake to fix.
23
+ */
24
+ export type SyncPayloadSource = 'explicit' | 'azure-devops' | 'github' | 'gitlab' | 'none';
25
+ export interface ParsedSyncPayload {
26
+ source: SyncPayloadSource;
27
+ branches: string[] | 'all';
28
+ /** Explicit-body names git would refuse. Empty for every other source. */
29
+ invalid: string[];
30
+ }
31
+ export declare function parseSyncPayload(body: unknown): ParsedSyncPayload;
32
+ //# sourceMappingURL=sync-payload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sync-payload.d.ts","sourceRoot":"","sources":["../../../src/modules/kb-sync/sync-payload.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,iBAAiB,GAAG,UAAU,GAAG,cAAc,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE3F,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,iBAAiB,CAAC;IAC1B,QAAQ,EAAE,MAAM,EAAE,GAAG,KAAK,CAAC;IAC3B,0EAA0E;IAC1E,OAAO,EAAE,MAAM,EAAE,CAAC;CACnB;AA+CD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,OAAO,GAAG,iBAAiB,CA+DjE"}