@agent-watch-ai/edge 0.2.5

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 (364) hide show
  1. package/CHANGELOG.md +251 -0
  2. package/LICENSE +21 -0
  3. package/README.md +168 -0
  4. package/dist/billing/billing-mode.d.ts +16 -0
  5. package/dist/billing/billing-mode.js +139 -0
  6. package/dist/billing/constants/billing.constants.d.ts +37 -0
  7. package/dist/billing/constants/billing.constants.js +38 -0
  8. package/dist/billing/index.d.ts +6 -0
  9. package/dist/billing/index.js +6 -0
  10. package/dist/cli/args.d.ts +29 -0
  11. package/dist/cli/args.js +61 -0
  12. package/dist/cli/constants/cli.constants.d.ts +60 -0
  13. package/dist/cli/constants/cli.constants.js +60 -0
  14. package/dist/cli/context.d.ts +51 -0
  15. package/dist/cli/context.js +108 -0
  16. package/dist/cli/doctor.d.ts +15 -0
  17. package/dist/cli/doctor.js +416 -0
  18. package/dist/cli/hook.d.ts +20 -0
  19. package/dist/cli/hook.js +166 -0
  20. package/dist/cli/index.d.ts +13 -0
  21. package/dist/cli/index.js +8 -0
  22. package/dist/cli/misc.d.ts +31 -0
  23. package/dist/cli/misc.js +63 -0
  24. package/dist/cli/setup.d.ts +32 -0
  25. package/dist/cli/setup.js +298 -0
  26. package/dist/cli/status.d.ts +11 -0
  27. package/dist/cli/status.js +174 -0
  28. package/dist/cli/types/cli.types.d.ts +69 -0
  29. package/dist/cli/types/cli.types.js +1 -0
  30. package/dist/cli/ui.d.ts +41 -0
  31. package/dist/cli/ui.js +78 -0
  32. package/dist/cli/uninstall.d.ts +13 -0
  33. package/dist/cli/uninstall.js +83 -0
  34. package/dist/cli.d.ts +2 -0
  35. package/dist/cli.js +124 -0
  36. package/dist/config/config-store.d.ts +29 -0
  37. package/dist/config/config-store.js +75 -0
  38. package/dist/config/config.d.ts +73 -0
  39. package/dist/config/config.js +115 -0
  40. package/dist/config/constants/config.constants.d.ts +62 -0
  41. package/dist/config/constants/config.constants.js +70 -0
  42. package/dist/config/index.d.ts +10 -0
  43. package/dist/config/index.js +5 -0
  44. package/dist/config/repo-config.d.ts +37 -0
  45. package/dist/config/repo-config.js +178 -0
  46. package/dist/config/schemas/config.schema.d.ts +409 -0
  47. package/dist/config/schemas/config.schema.js +97 -0
  48. package/dist/config/types/config.types.d.ts +33 -0
  49. package/dist/config/types/config.types.js +1 -0
  50. package/dist/core/async.d.ts +20 -0
  51. package/dist/core/async.js +32 -0
  52. package/dist/core/constants/core.constants.d.ts +18 -0
  53. package/dist/core/constants/core.constants.js +18 -0
  54. package/dist/core/env.d.ts +12 -0
  55. package/dist/core/env.js +20 -0
  56. package/dist/core/index.d.ts +13 -0
  57. package/dist/core/index.js +8 -0
  58. package/dist/core/logger.d.ts +27 -0
  59. package/dist/core/logger.js +82 -0
  60. package/dist/core/number.d.ts +33 -0
  61. package/dist/core/number.js +47 -0
  62. package/dist/core/object.d.ts +78 -0
  63. package/dist/core/object.js +127 -0
  64. package/dist/core/pipe.d.ts +55 -0
  65. package/dist/core/pipe.js +81 -0
  66. package/dist/core/types/core.types.d.ts +54 -0
  67. package/dist/core/types/core.types.js +4 -0
  68. package/dist/core/version.d.ts +19 -0
  69. package/dist/core/version.js +35 -0
  70. package/dist/core/which.d.ts +13 -0
  71. package/dist/core/which.js +57 -0
  72. package/dist/enforcement/constants/enforcement.constants.d.ts +23 -0
  73. package/dist/enforcement/constants/enforcement.constants.js +22 -0
  74. package/dist/enforcement/decision-cache.d.ts +57 -0
  75. package/dist/enforcement/decision-cache.js +138 -0
  76. package/dist/enforcement/decision-client.d.ts +14 -0
  77. package/dist/enforcement/decision-client.js +84 -0
  78. package/dist/enforcement/enforcement.d.ts +17 -0
  79. package/dist/enforcement/enforcement.js +90 -0
  80. package/dist/enforcement/index.d.ts +11 -0
  81. package/dist/enforcement/index.js +10 -0
  82. package/dist/enforcement/schemas/enforcement.schema.d.ts +30 -0
  83. package/dist/enforcement/schemas/enforcement.schema.js +36 -0
  84. package/dist/enforcement/types/enforcement.types.d.ts +59 -0
  85. package/dist/enforcement/types/enforcement.types.js +1 -0
  86. package/dist/enrollment/constants/enrollment.constants.d.ts +6 -0
  87. package/dist/enrollment/constants/enrollment.constants.js +6 -0
  88. package/dist/enrollment/enrollment-provider.d.ts +5 -0
  89. package/dist/enrollment/enrollment-provider.js +1 -0
  90. package/dist/enrollment/index.d.ts +7 -0
  91. package/dist/enrollment/index.js +1 -0
  92. package/dist/enrollment/manual-enrollment.d.ts +32 -0
  93. package/dist/enrollment/manual-enrollment.js +72 -0
  94. package/dist/enrollment/types/enrollment.types.d.ts +25 -0
  95. package/dist/enrollment/types/enrollment.types.js +1 -0
  96. package/dist/events/canonical-event.d.ts +10 -0
  97. package/dist/events/canonical-event.js +9 -0
  98. package/dist/events/constants/enrich.constants.d.ts +14 -0
  99. package/dist/events/constants/enrich.constants.js +15 -0
  100. package/dist/events/constants/events.constants.d.ts +35 -0
  101. package/dist/events/constants/events.constants.js +57 -0
  102. package/dist/events/enrich.d.ts +16 -0
  103. package/dist/events/enrich.js +226 -0
  104. package/dist/events/event-id.d.ts +32 -0
  105. package/dist/events/event-id.js +49 -0
  106. package/dist/events/index.d.ts +14 -0
  107. package/dist/events/index.js +5 -0
  108. package/dist/events/llm-call.d.ts +14 -0
  109. package/dist/events/llm-call.js +84 -0
  110. package/dist/events/product-event.d.ts +39 -0
  111. package/dist/events/product-event.js +19 -0
  112. package/dist/events/types/enrich.types.d.ts +25 -0
  113. package/dist/events/types/enrich.types.js +1 -0
  114. package/dist/events/types/events.types.d.ts +112 -0
  115. package/dist/events/types/events.types.js +1 -0
  116. package/dist/events/types/llm-call.types.d.ts +79 -0
  117. package/dist/events/types/llm-call.types.js +1 -0
  118. package/dist/events/types/repo-snapshot.types.d.ts +32 -0
  119. package/dist/events/types/repo-snapshot.types.js +1 -0
  120. package/dist/feature/constants/feature.constants.d.ts +10 -0
  121. package/dist/feature/constants/feature.constants.js +10 -0
  122. package/dist/feature/index.d.ts +6 -0
  123. package/dist/feature/index.js +6 -0
  124. package/dist/feature/ticket-candidates.d.ts +13 -0
  125. package/dist/feature/ticket-candidates.js +26 -0
  126. package/dist/git/constants/git.constants.d.ts +84 -0
  127. package/dist/git/constants/git.constants.js +113 -0
  128. package/dist/git/git-context.d.ts +52 -0
  129. package/dist/git/git-context.js +226 -0
  130. package/dist/git/index.d.ts +9 -0
  131. package/dist/git/index.js +3 -0
  132. package/dist/git/remote-sanitize.d.ts +26 -0
  133. package/dist/git/remote-sanitize.js +92 -0
  134. package/dist/git/repo-snapshot.d.ts +40 -0
  135. package/dist/git/repo-snapshot.js +95 -0
  136. package/dist/git/types/git.types.d.ts +36 -0
  137. package/dist/git/types/git.types.js +1 -0
  138. package/dist/git/types/snapshot.types.d.ts +25 -0
  139. package/dist/git/types/snapshot.types.js +1 -0
  140. package/dist/otlp/constants/otlp.constants.d.ts +84 -0
  141. package/dist/otlp/constants/otlp.constants.js +99 -0
  142. package/dist/otlp/http.d.ts +24 -0
  143. package/dist/otlp/http.js +37 -0
  144. package/dist/otlp/index.d.ts +9 -0
  145. package/dist/otlp/index.js +3 -0
  146. package/dist/otlp/normalize.d.ts +14 -0
  147. package/dist/otlp/normalize.js +404 -0
  148. package/dist/otlp/types/otlp.types.d.ts +69 -0
  149. package/dist/otlp/types/otlp.types.js +1 -0
  150. package/dist/pipeline/constants/pipeline.constants.d.ts +23 -0
  151. package/dist/pipeline/constants/pipeline.constants.js +22 -0
  152. package/dist/pipeline/hook-pipeline.d.ts +14 -0
  153. package/dist/pipeline/hook-pipeline.js +297 -0
  154. package/dist/pipeline/index.d.ts +6 -0
  155. package/dist/pipeline/index.js +1 -0
  156. package/dist/pipeline/types/pipeline.types.d.ts +48 -0
  157. package/dist/pipeline/types/pipeline.types.js +1 -0
  158. package/dist/privacy/constants/privacy.constants.d.ts +23 -0
  159. package/dist/privacy/constants/privacy.constants.js +38 -0
  160. package/dist/privacy/index.d.ts +7 -0
  161. package/dist/privacy/index.js +2 -0
  162. package/dist/privacy/sanitizer.d.ts +20 -0
  163. package/dist/privacy/sanitizer.js +52 -0
  164. package/dist/privacy/types/privacy.types.d.ts +13 -0
  165. package/dist/privacy/types/privacy.types.js +1 -0
  166. package/dist/providers/antigravity/antigravity.adapter.d.ts +54 -0
  167. package/dist/providers/antigravity/antigravity.adapter.js +311 -0
  168. package/dist/providers/antigravity/antigravity.detect.d.ts +25 -0
  169. package/dist/providers/antigravity/antigravity.detect.js +64 -0
  170. package/dist/providers/antigravity/antigravity.hooks.d.ts +24 -0
  171. package/dist/providers/antigravity/antigravity.hooks.js +126 -0
  172. package/dist/providers/antigravity/antigravity.provider.d.ts +2 -0
  173. package/dist/providers/antigravity/antigravity.provider.js +22 -0
  174. package/dist/providers/antigravity/constants/antigravity.constants.d.ts +72 -0
  175. package/dist/providers/antigravity/constants/antigravity.constants.js +75 -0
  176. package/dist/providers/antigravity/schemas/antigravity.schema.d.ts +521 -0
  177. package/dist/providers/antigravity/schemas/antigravity.schema.js +64 -0
  178. package/dist/providers/antigravity/types/antigravity.types.d.ts +13 -0
  179. package/dist/providers/antigravity/types/antigravity.types.js +1 -0
  180. package/dist/providers/claude/claude.adapter.d.ts +15 -0
  181. package/dist/providers/claude/claude.adapter.js +141 -0
  182. package/dist/providers/claude/claude.detect.d.ts +20 -0
  183. package/dist/providers/claude/claude.detect.js +57 -0
  184. package/dist/providers/claude/claude.hooks.d.ts +23 -0
  185. package/dist/providers/claude/claude.hooks.js +94 -0
  186. package/dist/providers/claude/claude.otel.d.ts +95 -0
  187. package/dist/providers/claude/claude.otel.js +331 -0
  188. package/dist/providers/claude/claude.provider.d.ts +2 -0
  189. package/dist/providers/claude/claude.provider.js +26 -0
  190. package/dist/providers/claude/constants/claude.constants.d.ts +34 -0
  191. package/dist/providers/claude/constants/claude.constants.js +54 -0
  192. package/dist/providers/claude/constants/claude.otel.constants.d.ts +17 -0
  193. package/dist/providers/claude/constants/claude.otel.constants.js +17 -0
  194. package/dist/providers/claude/schemas/claude.schema.d.ts +72 -0
  195. package/dist/providers/claude/schemas/claude.schema.js +32 -0
  196. package/dist/providers/claude/types/claude.types.d.ts +3 -0
  197. package/dist/providers/claude/types/claude.types.js +1 -0
  198. package/dist/providers/codex/codex.adapter.d.ts +11 -0
  199. package/dist/providers/codex/codex.adapter.js +106 -0
  200. package/dist/providers/codex/codex.detect.d.ts +30 -0
  201. package/dist/providers/codex/codex.detect.js +58 -0
  202. package/dist/providers/codex/codex.hooks.d.ts +27 -0
  203. package/dist/providers/codex/codex.hooks.js +129 -0
  204. package/dist/providers/codex/codex.otel.d.ts +51 -0
  205. package/dist/providers/codex/codex.otel.js +325 -0
  206. package/dist/providers/codex/codex.provider.d.ts +2 -0
  207. package/dist/providers/codex/codex.provider.js +25 -0
  208. package/dist/providers/codex/constants/codex.constants.d.ts +41 -0
  209. package/dist/providers/codex/constants/codex.constants.js +62 -0
  210. package/dist/providers/codex/constants/codex.otel.constants.d.ts +22 -0
  211. package/dist/providers/codex/constants/codex.otel.constants.js +22 -0
  212. package/dist/providers/codex/schemas/codex.schema.d.ts +70 -0
  213. package/dist/providers/codex/schemas/codex.schema.js +32 -0
  214. package/dist/providers/codex/types/codex.types.d.ts +3 -0
  215. package/dist/providers/codex/types/codex.types.js +1 -0
  216. package/dist/providers/constants/provider.constants.d.ts +33 -0
  217. package/dist/providers/constants/provider.constants.js +33 -0
  218. package/dist/providers/cursor/constants/cursor.constants.d.ts +58 -0
  219. package/dist/providers/cursor/constants/cursor.constants.js +95 -0
  220. package/dist/providers/cursor/cursor.adapter.d.ts +11 -0
  221. package/dist/providers/cursor/cursor.adapter.js +331 -0
  222. package/dist/providers/cursor/cursor.detect.d.ts +23 -0
  223. package/dist/providers/cursor/cursor.detect.js +66 -0
  224. package/dist/providers/cursor/cursor.hooks.d.ts +25 -0
  225. package/dist/providers/cursor/cursor.hooks.js +95 -0
  226. package/dist/providers/cursor/cursor.provider.d.ts +2 -0
  227. package/dist/providers/cursor/cursor.provider.js +27 -0
  228. package/dist/providers/cursor/schemas/cursor.schema.d.ts +130 -0
  229. package/dist/providers/cursor/schemas/cursor.schema.js +62 -0
  230. package/dist/providers/cursor/types/cursor.types.d.ts +3 -0
  231. package/dist/providers/cursor/types/cursor.types.js +1 -0
  232. package/dist/providers/gemini/constants/gemini.constants.d.ts +36 -0
  233. package/dist/providers/gemini/constants/gemini.constants.js +58 -0
  234. package/dist/providers/gemini/constants/gemini.otel.constants.d.ts +34 -0
  235. package/dist/providers/gemini/constants/gemini.otel.constants.js +48 -0
  236. package/dist/providers/gemini/gemini.adapter.d.ts +15 -0
  237. package/dist/providers/gemini/gemini.adapter.js +146 -0
  238. package/dist/providers/gemini/gemini.detect.d.ts +23 -0
  239. package/dist/providers/gemini/gemini.detect.js +78 -0
  240. package/dist/providers/gemini/gemini.hooks.d.ts +22 -0
  241. package/dist/providers/gemini/gemini.hooks.js +93 -0
  242. package/dist/providers/gemini/gemini.otel.d.ts +62 -0
  243. package/dist/providers/gemini/gemini.otel.js +274 -0
  244. package/dist/providers/gemini/gemini.provider.d.ts +2 -0
  245. package/dist/providers/gemini/gemini.provider.js +22 -0
  246. package/dist/providers/gemini/schemas/gemini.schema.d.ts +81 -0
  247. package/dist/providers/gemini/schemas/gemini.schema.js +35 -0
  248. package/dist/providers/gemini/types/gemini.types.d.ts +3 -0
  249. package/dist/providers/gemini/types/gemini.types.js +1 -0
  250. package/dist/providers/index.d.ts +14 -0
  251. package/dist/providers/index.js +8 -0
  252. package/dist/providers/provider.d.ts +16 -0
  253. package/dist/providers/provider.js +149 -0
  254. package/dist/providers/registry.d.ts +12 -0
  255. package/dist/providers/registry.js +23 -0
  256. package/dist/providers/shared/constants/hook-refusal.constants.d.ts +6 -0
  257. package/dist/providers/shared/constants/hook-refusal.constants.js +6 -0
  258. package/dist/providers/shared/constants/tooling.constants.d.ts +25 -0
  259. package/dist/providers/shared/constants/tooling.constants.js +25 -0
  260. package/dist/providers/shared/event-builder.d.ts +85 -0
  261. package/dist/providers/shared/event-builder.js +166 -0
  262. package/dist/providers/shared/hook-config.d.ts +80 -0
  263. package/dist/providers/shared/hook-config.js +164 -0
  264. package/dist/providers/shared/hook-refusal.d.ts +16 -0
  265. package/dist/providers/shared/hook-refusal.js +33 -0
  266. package/dist/providers/shared/install-record.d.ts +75 -0
  267. package/dist/providers/shared/install-record.js +113 -0
  268. package/dist/providers/shared/tooling.d.ts +58 -0
  269. package/dist/providers/shared/tooling.js +103 -0
  270. package/dist/providers/shared/types/adapter.types.d.ts +54 -0
  271. package/dist/providers/shared/types/adapter.types.js +1 -0
  272. package/dist/providers/shared/types/hook-config.types.d.ts +32 -0
  273. package/dist/providers/shared/types/hook-config.types.js +1 -0
  274. package/dist/providers/types/provider.types.d.ts +98 -0
  275. package/dist/providers/types/provider.types.js +1 -0
  276. package/dist/snapshot/branch-selection.d.ts +37 -0
  277. package/dist/snapshot/branch-selection.js +69 -0
  278. package/dist/snapshot/budget.d.ts +41 -0
  279. package/dist/snapshot/budget.js +67 -0
  280. package/dist/snapshot/constants/snapshot-stages.constants.d.ts +11 -0
  281. package/dist/snapshot/constants/snapshot-stages.constants.js +11 -0
  282. package/dist/snapshot/constants/snapshot.constants.d.ts +56 -0
  283. package/dist/snapshot/constants/snapshot.constants.js +56 -0
  284. package/dist/snapshot/index.d.ts +16 -0
  285. package/dist/snapshot/index.js +6 -0
  286. package/dist/snapshot/snapshot-event.d.ts +17 -0
  287. package/dist/snapshot/snapshot-event.js +66 -0
  288. package/dist/snapshot/snapshot-pipeline.d.ts +14 -0
  289. package/dist/snapshot/snapshot-pipeline.js +163 -0
  290. package/dist/snapshot/snapshot-state.d.ts +55 -0
  291. package/dist/snapshot/snapshot-state.js +100 -0
  292. package/dist/snapshot/types/snapshot.types.d.ts +86 -0
  293. package/dist/snapshot/types/snapshot.types.js +1 -0
  294. package/dist/storage/atomic-file.d.ts +25 -0
  295. package/dist/storage/atomic-file.js +76 -0
  296. package/dist/storage/constants/storage.constants.d.ts +34 -0
  297. package/dist/storage/constants/storage.constants.js +35 -0
  298. package/dist/storage/index.d.ts +10 -0
  299. package/dist/storage/index.js +5 -0
  300. package/dist/storage/install-state.d.ts +20 -0
  301. package/dist/storage/install-state.js +40 -0
  302. package/dist/storage/json-file.d.ts +13 -0
  303. package/dist/storage/json-file.js +31 -0
  304. package/dist/storage/lock.d.ts +15 -0
  305. package/dist/storage/lock.js +70 -0
  306. package/dist/storage/paths.d.ts +14 -0
  307. package/dist/storage/paths.js +42 -0
  308. package/dist/storage/schemas/storage.schema.d.ts +128 -0
  309. package/dist/storage/schemas/storage.schema.js +27 -0
  310. package/dist/storage/types/storage.types.d.ts +36 -0
  311. package/dist/storage/types/storage.types.js +1 -0
  312. package/dist/transport/constants/transport.constants.d.ts +47 -0
  313. package/dist/transport/constants/transport.constants.js +47 -0
  314. package/dist/transport/cooldown.d.ts +35 -0
  315. package/dist/transport/cooldown.js +62 -0
  316. package/dist/transport/delivery-stats.d.ts +65 -0
  317. package/dist/transport/delivery-stats.js +186 -0
  318. package/dist/transport/delivery.d.ts +26 -0
  319. package/dist/transport/delivery.js +80 -0
  320. package/dist/transport/headers.d.ts +12 -0
  321. package/dist/transport/headers.js +19 -0
  322. package/dist/transport/http-transport.d.ts +39 -0
  323. package/dist/transport/http-transport.js +106 -0
  324. package/dist/transport/index.d.ts +13 -0
  325. package/dist/transport/index.js +7 -0
  326. package/dist/transport/queue.d.ts +177 -0
  327. package/dist/transport/queue.js +474 -0
  328. package/dist/transport/schemas/queue.schema.d.ts +30 -0
  329. package/dist/transport/schemas/queue.schema.js +18 -0
  330. package/dist/transport/transport.d.ts +6 -0
  331. package/dist/transport/transport.js +1 -0
  332. package/dist/transport/types/transport.types.d.ts +102 -0
  333. package/dist/transport/types/transport.types.js +1 -0
  334. package/dist/turns/aggregate-usage.d.ts +20 -0
  335. package/dist/turns/aggregate-usage.js +269 -0
  336. package/dist/turns/claude-transcript.d.ts +18 -0
  337. package/dist/turns/claude-transcript.js +80 -0
  338. package/dist/turns/constants/turns.constants.d.ts +69 -0
  339. package/dist/turns/constants/turns.constants.js +86 -0
  340. package/dist/turns/cursor-transcript.d.ts +21 -0
  341. package/dist/turns/cursor-transcript.js +66 -0
  342. package/dist/turns/index.d.ts +17 -0
  343. package/dist/turns/index.js +7 -0
  344. package/dist/turns/transcript.d.ts +74 -0
  345. package/dist/turns/transcript.js +188 -0
  346. package/dist/turns/turn-state.d.ts +94 -0
  347. package/dist/turns/turn-state.js +234 -0
  348. package/dist/turns/turn-summary.d.ts +27 -0
  349. package/dist/turns/turn-summary.js +164 -0
  350. package/dist/turns/turn-tracker.d.ts +14 -0
  351. package/dist/turns/turn-tracker.js +521 -0
  352. package/dist/turns/types/aggregate-usage.types.d.ts +26 -0
  353. package/dist/turns/types/aggregate-usage.types.js +1 -0
  354. package/dist/turns/types/transcript.types.d.ts +41 -0
  355. package/dist/turns/types/transcript.types.js +1 -0
  356. package/dist/turns/types/turn-state.types.d.ts +37 -0
  357. package/dist/turns/types/turn-state.types.js +1 -0
  358. package/dist/turns/types/turn-summary.types.d.ts +106 -0
  359. package/dist/turns/types/turn-summary.types.js +1 -0
  360. package/dist/turns/types/turn-tracker.types.d.ts +27 -0
  361. package/dist/turns/types/turn-tracker.types.js +1 -0
  362. package/example/README.md +43 -0
  363. package/example/server.mjs +240 -0
  364. package/package.json +89 -0
@@ -0,0 +1,113 @@
1
+ /** Default budget for one git invocation; hooks run on the critical path. */
2
+ export const GIT_TIMEOUT_MS = 1000;
3
+ /** Cap on the changedFiles list; a huge dirty tree must not bloat an event. */
4
+ export const MAX_CHANGED_FILES = 50;
5
+ /** stdout ceiling per git process. */
6
+ export const GIT_MAX_BUFFER_BYTES = 1024 * 1024;
7
+ /**
8
+ * Argument vectors, named so a reader sees intent instead of flags.
9
+ *
10
+ * `symbolic-ref` rather than `branch --show-current`: the latter needs
11
+ * git >= 2.22, and its absence would silently drop branch — and therefore
12
+ * ticket — attribution on older machines. It exits non-zero on a detached
13
+ * HEAD, which is exactly the "no branch" answer we want.
14
+ */
15
+ export const GIT_REPO_ROOT_ARGS = ['rev-parse', '--show-toplevel'];
16
+ export const GIT_BRANCH_ARGS = ['symbolic-ref', '--short', '-q', 'HEAD'];
17
+ export const GIT_COMMIT_ARGS = ['rev-parse', 'HEAD'];
18
+ export const GIT_REMOTE_ARGS = ['config', '--get', 'remote.origin.url'];
19
+ export const GIT_STATUS_ARGS = ['status', '--porcelain'];
20
+ export const GIT_USER_EMAIL_ARGS = ['config', '--get', 'user.email'];
21
+ /** Where `origin/HEAD` points: the remote's default branch, when it is known locally. */
22
+ export const GIT_ORIGIN_HEAD_ARGS = ['symbolic-ref', '--short', '-q', 'refs/remotes/origin/HEAD'];
23
+ /** Field separator inside one `for-each-ref` / `log` record. */
24
+ export const GIT_FIELD_SEPARATOR = '';
25
+ /**
26
+ * The recent branches, newest commit first, with the heads the cache is
27
+ * compared against.
28
+ *
29
+ * One process for the whole listing, and it carries the heads — which is what
30
+ * lets the cache diff run before any `git log`. On an ordinary closing turn,
31
+ * where nothing moved, that is the difference between two git processes and
32
+ * twelve.
33
+ *
34
+ * @param count - How many branches to list, newest commit first.
35
+ * @returns The argument vector.
36
+ */
37
+ export function gitRecentBranchesArgs(count) {
38
+ return [
39
+ 'for-each-ref',
40
+ 'refs/heads',
41
+ '--sort=-committerdate',
42
+ `--count=${String(count)}`,
43
+ `--format=%(refname:short)${GIT_FIELD_SEPARATOR}%(objectname)${GIT_FIELD_SEPARATOR}%(committerdate:iso-strict)`
44
+ ];
45
+ }
46
+ /**
47
+ * The commits on a branch that its default branch does not have.
48
+ *
49
+ * The delta, never the history: `git log <branch>` would carry the trunk's
50
+ * commits into every branch's evidence, so unrelated work would be described
51
+ * by the same subjects and named as one feature.
52
+ *
53
+ * @param defaultBranch - The trunk to subtract.
54
+ * @param branch - The branch to describe.
55
+ * @param count - Ceiling on the commits returned.
56
+ * @returns The argument vector.
57
+ */
58
+ export function gitBranchDeltaArgs(defaultBranch, branch, count) {
59
+ return [
60
+ 'log',
61
+ '-n',
62
+ String(count),
63
+ `--format=%H${GIT_FIELD_SEPARATOR}%s${GIT_FIELD_SEPARATOR}%aI`,
64
+ `${defaultBranch}..${branch}`,
65
+ '--'
66
+ ];
67
+ }
68
+ /**
69
+ * Whether a ref exists at all, for the local fallback when no `origin/HEAD` does.
70
+ *
71
+ * @param ref - The ref to test.
72
+ * @returns The argument vector, which exits non-zero when the ref is absent.
73
+ */
74
+ export function gitVerifyRefArgs(ref) {
75
+ return ['rev-parse', '--verify', '-q', `${ref}^{commit}`];
76
+ }
77
+ /** What a repository's trunk is called when neither git nor the platform said. */
78
+ export const ASSUMED_DEFAULT_BRANCHES = ['main', 'master'];
79
+ /** Node's error code for a child process that overran maxBuffer on stdout. */
80
+ export const STDOUT_MAXBUFFER_CODE = 'ERR_CHILD_PROCESS_STDOUT_MAXBUFFER';
81
+ /** Length of the "XY " status prefix before the path in porcelain output. */
82
+ export const PORCELAIN_PREFIX_LENGTH = 3;
83
+ /** Separator between the old and new path of a rename in porcelain output. */
84
+ export const PORCELAIN_RENAME_SEPARATOR = ' -> ';
85
+ /** C-style escapes git emits inside a quoted path (core.quotePath). */
86
+ export const PORCELAIN_ESCAPES = {
87
+ n: '\n',
88
+ t: '\t',
89
+ r: '\r',
90
+ a: '\x07',
91
+ b: '\b',
92
+ f: '\f',
93
+ v: '\v',
94
+ '"': '"',
95
+ '\\': '\\'
96
+ };
97
+ /** Trailing whitespace only: porcelain lines carry significant leading spaces. */
98
+ export const RE_TRAILING_WHITESPACE = /\s+$/;
99
+ /** A URL scheme followed by "://". */
100
+ export const RE_URL_SCHEME = /^[A-Za-z][\w+.-]*:\/\//;
101
+ /** `scheme://user@` and `scheme://user:password@` prefixes. */
102
+ export const RE_URL_USERINFO = /^(\w+:\/\/)[^/@\s]+@/;
103
+ export const RE_URL_USERINFO_WITH_PASSWORD = /^(\w+:\/\/)[^/@\s]+:[^/@\s]+@/;
104
+ /** Leading `user@` of an scp-like remote. */
105
+ export const RE_SCP_USERINFO = /^[^@/\s]+@/;
106
+ /**
107
+ * scp-like remote: `host:path`. Two or more characters before the colon keeps
108
+ * Windows drive paths ("C:\\...") out.
109
+ */
110
+ export const RE_SCP_REMOTE = /^([\w.-]{2,}):([^\s]+)$/;
111
+ export const RE_LEADING_SLASHES = /^\/+/;
112
+ export const RE_TRAILING_SLASHES = /\/+$/;
113
+ export const RE_DOT_GIT_SUFFIX = /\.git$/;
@@ -0,0 +1,52 @@
1
+ import type { GitContext, GitContextOptions, GitRunner, GitUserEmailOptions } from './types/git.types.js';
2
+ export type { GitContext, GitContextOptions, GitRunner } from './types/git.types.js';
3
+ /**
4
+ * Collect repository context for event enrichment.
5
+ *
6
+ * Every failure degrades to less context, never to a thrown error: hooks run
7
+ * inside the coding agent and must not break when git is missing, slow, or the
8
+ * working directory is not a repository at all.
9
+ *
10
+ * @param options - What to collect and where.
11
+ * @param run - Git runner; injectable for tests.
12
+ * @returns The context that could be resolved.
13
+ */
14
+ export declare function collectGitContext(options: GitContextOptions, run?: GitRunner): Promise<GitContext>;
15
+ /**
16
+ * `git config user.email` for the developer identity on turn summaries.
17
+ *
18
+ * Trimmed to nothing rather than reported as a value: `user.email = " "` is a
19
+ * misconfiguration, and an identity made of whitespace would be keyed on by the
20
+ * budget check as if it named someone.
21
+ *
22
+ * @param cwd - Directory to resolve the config from.
23
+ * @param options - Timeout, injected HOME and runner override.
24
+ * @returns The configured email, or undefined outside git / when unset.
25
+ */
26
+ export declare function gitUserEmail(cwd: string, options?: GitUserEmailOptions): Promise<string | undefined>;
27
+ /**
28
+ * The developer identity attached to a turn — and asked about by the pre-turn
29
+ * budget check.
30
+ *
31
+ * Both callers have to produce the same string: it is the value the platform
32
+ * stored for this developer, so a gate asking about one identity while summaries
33
+ * report another would match no policy at all. Hence one function rather than
34
+ * the same two lines in two modules.
35
+ *
36
+ * @param configuredEmail - `developerEmail` from the effective config, if set.
37
+ * @param cwd - Directory to fall back to `git config user.email` in.
38
+ * @param options - Timeout, injected HOME and runner override.
39
+ * @returns The identity, or undefined when neither source has one.
40
+ */
41
+ export declare function developerIdentity(configuredEmail: string | undefined, cwd: string, options?: GitUserEmailOptions): Promise<string | undefined>;
42
+ /**
43
+ * Run one git command, resolving to undefined on any failure.
44
+ *
45
+ * @param args - Argument vector.
46
+ * @param cwd - Working directory.
47
+ * @param timeoutMs - Kill budget.
48
+ * @param home - HOME to read global git config (identity!) from, so tests and
49
+ * sandboxes never pick up the developer's real one.
50
+ * @returns Trailing-trimmed stdout, or undefined.
51
+ */
52
+ export declare const runGit: GitRunner;
@@ -0,0 +1,226 @@
1
+ import { execFile } from 'node:child_process';
2
+ import path from 'node:path';
3
+ import process from 'node:process';
4
+ import { debugLog } from '../core/logger.js';
5
+ import { normalizeRemote, remoteHash } from './remote-sanitize.js';
6
+ import { GIT_BRANCH_ARGS, GIT_COMMIT_ARGS, GIT_MAX_BUFFER_BYTES, GIT_REMOTE_ARGS, GIT_REPO_ROOT_ARGS, GIT_STATUS_ARGS, GIT_TIMEOUT_MS, GIT_USER_EMAIL_ARGS, MAX_CHANGED_FILES, PORCELAIN_ESCAPES, PORCELAIN_PREFIX_LENGTH, PORCELAIN_RENAME_SEPARATOR, RE_TRAILING_WHITESPACE, STDOUT_MAXBUFFER_CODE } from './constants/git.constants.js';
7
+ /**
8
+ * Collect repository context for event enrichment.
9
+ *
10
+ * Every failure degrades to less context, never to a thrown error: hooks run
11
+ * inside the coding agent and must not break when git is missing, slow, or the
12
+ * working directory is not a repository at all.
13
+ *
14
+ * @param options - What to collect and where.
15
+ * @param run - Git runner; injectable for tests.
16
+ * @returns The context that could be resolved.
17
+ */
18
+ export async function collectGitContext(options, run = runGit) {
19
+ const timeoutMs = options.timeoutMs ?? GIT_TIMEOUT_MS;
20
+ const root = await run(GIT_REPO_ROOT_ARGS, options.cwd, timeoutMs);
21
+ if (!root)
22
+ return { workingDirectory: options.cwd };
23
+ if (options.rootOnly) {
24
+ return { repositoryRoot: root, workingDirectory: options.cwd, repository: path.basename(root) };
25
+ }
26
+ const [branch, commit, remoteRaw, status] = await Promise.all([
27
+ run(GIT_BRANCH_ARGS, root, timeoutMs),
28
+ run(GIT_COMMIT_ARGS, root, timeoutMs),
29
+ run(GIT_REMOTE_ARGS, root, timeoutMs),
30
+ options.includeChangedFiles ? run(GIT_STATUS_ARGS, root, timeoutMs) : Promise.resolve(undefined)
31
+ ]);
32
+ const identity = repositoryIdentity(root, remoteRaw);
33
+ return {
34
+ repositoryRoot: root,
35
+ workingDirectory: options.cwd,
36
+ branch: branch || undefined,
37
+ commit: commit || undefined,
38
+ ...identity,
39
+ changedFiles: status === undefined ? undefined : changedFilesFrom(status, options.maxChangedFiles ?? MAX_CHANGED_FILES)
40
+ };
41
+ }
42
+ /**
43
+ * `git config user.email` for the developer identity on turn summaries.
44
+ *
45
+ * Trimmed to nothing rather than reported as a value: `user.email = " "` is a
46
+ * misconfiguration, and an identity made of whitespace would be keyed on by the
47
+ * budget check as if it named someone.
48
+ *
49
+ * @param cwd - Directory to resolve the config from.
50
+ * @param options - Timeout, injected HOME and runner override.
51
+ * @returns The configured email, or undefined outside git / when unset.
52
+ */
53
+ export async function gitUserEmail(cwd, options = {}) {
54
+ const run = options.run ?? runGit;
55
+ const email = await run(GIT_USER_EMAIL_ARGS, cwd, options.timeoutMs ?? GIT_TIMEOUT_MS, options.home);
56
+ return email?.trim() || undefined;
57
+ }
58
+ /**
59
+ * The developer identity attached to a turn — and asked about by the pre-turn
60
+ * budget check.
61
+ *
62
+ * Both callers have to produce the same string: it is the value the platform
63
+ * stored for this developer, so a gate asking about one identity while summaries
64
+ * report another would match no policy at all. Hence one function rather than
65
+ * the same two lines in two modules.
66
+ *
67
+ * @param configuredEmail - `developerEmail` from the effective config, if set.
68
+ * @param cwd - Directory to fall back to `git config user.email` in.
69
+ * @param options - Timeout, injected HOME and runner override.
70
+ * @returns The identity, or undefined when neither source has one.
71
+ */
72
+ export async function developerIdentity(configuredEmail, cwd, options = {}) {
73
+ const configured = configuredEmail?.trim();
74
+ if (configured)
75
+ return configured;
76
+ return gitUserEmail(cwd, options);
77
+ }
78
+ /**
79
+ * How this repository is named, preferring its remote identity.
80
+ *
81
+ * Only the credential-free normalized remote is ever reported, alongside its
82
+ * hash for backends that want to group without knowing the name. A repository
83
+ * with no usable remote falls back to its directory name.
84
+ *
85
+ * @param root - Repository root path.
86
+ * @param remoteRaw - Raw `remote.origin.url`, when git reported one.
87
+ * @returns The repository/remote/hash fields of the context.
88
+ */
89
+ function repositoryIdentity(root, remoteRaw) {
90
+ const remote = remoteRaw ? normalizeRemote(remoteRaw) : undefined;
91
+ if (!remote)
92
+ return { repository: path.basename(root) };
93
+ return { remote, repository: remote, repositoryHash: remoteRaw ? remoteHash(remoteRaw) : undefined };
94
+ }
95
+ /**
96
+ * Paths from `status --porcelain`, capped.
97
+ *
98
+ * @param status - Raw porcelain output.
99
+ * @param maxFiles - Ceiling on the returned list.
100
+ * @returns Changed paths, truncated to the cap.
101
+ */
102
+ function changedFilesFrom(status, maxFiles) {
103
+ const files = [];
104
+ for (const line of status.split('\n')) {
105
+ if (!line)
106
+ continue;
107
+ const file = parsePorcelainLine(line);
108
+ if (file)
109
+ files.push(file);
110
+ }
111
+ if (files.length > maxFiles) {
112
+ debugLog(`changedFiles truncated: ${files.length} -> ${maxFiles}`);
113
+ return files.slice(0, maxFiles);
114
+ }
115
+ return files;
116
+ }
117
+ /**
118
+ * The path out of one porcelain line ("XY path" or "XY orig -> renamed").
119
+ *
120
+ * @param line - One line of porcelain output.
121
+ * @returns The (new) path, or undefined for a line carrying none.
122
+ */
123
+ function parsePorcelainLine(line) {
124
+ const body = line.slice(PORCELAIN_PREFIX_LENGTH);
125
+ if (!body)
126
+ return undefined;
127
+ const renameSplit = body.split(PORCELAIN_RENAME_SEPARATOR);
128
+ const candidate = renameSplit[renameSplit.length - 1] ?? body;
129
+ if (candidate.length >= 2 && candidate.startsWith('"') && candidate.endsWith('"')) {
130
+ return unquotePorcelainPath(candidate);
131
+ }
132
+ return candidate;
133
+ }
134
+ /**
135
+ * Decode git's C-style path quoting.
136
+ *
137
+ * `"r\303\251sum\303\251.txt"` is the on-the-wire form of `résumé.txt`: octal
138
+ * escapes are individual UTF-8 *bytes*, so they are collected as bytes and
139
+ * decoded once at the end rather than per escape.
140
+ *
141
+ * @param quoted - The quoted path, including its surrounding quotes.
142
+ * @returns The decoded path.
143
+ */
144
+ function unquotePorcelainPath(quoted) {
145
+ const inner = quoted.slice(1, -1);
146
+ const bytes = [];
147
+ let index = 0;
148
+ while (index < inner.length) {
149
+ const char = inner[index];
150
+ if (char !== '\\') {
151
+ bytes.push(...Buffer.from(char, 'utf8'));
152
+ index += 1;
153
+ continue;
154
+ }
155
+ const next = inner[index + 1];
156
+ if (next === undefined)
157
+ break;
158
+ if (next >= '0' && next <= '7') {
159
+ const octal = readOctalEscape(inner, index + 1);
160
+ bytes.push(parseInt(octal, 8));
161
+ index += 1 + octal.length;
162
+ continue;
163
+ }
164
+ bytes.push(...Buffer.from(PORCELAIN_ESCAPES[next] ?? next, 'utf8'));
165
+ index += 2;
166
+ }
167
+ return Buffer.from(bytes).toString('utf8');
168
+ }
169
+ /**
170
+ * The up-to-three octal digits starting at `start`.
171
+ *
172
+ * @param inner - Quoted path contents.
173
+ * @param start - Index of the first digit.
174
+ * @returns The digit run.
175
+ */
176
+ function readOctalEscape(inner, start) {
177
+ let octal = '';
178
+ while (octal.length < 3) {
179
+ const digit = inner[start + octal.length];
180
+ if (digit === undefined || digit < '0' || digit > '7')
181
+ break;
182
+ octal += digit;
183
+ }
184
+ return octal;
185
+ }
186
+ /**
187
+ * Run one git command, resolving to undefined on any failure.
188
+ *
189
+ * @param args - Argument vector.
190
+ * @param cwd - Working directory.
191
+ * @param timeoutMs - Kill budget.
192
+ * @param home - HOME to read global git config (identity!) from, so tests and
193
+ * sandboxes never pick up the developer's real one.
194
+ * @returns Trailing-trimmed stdout, or undefined.
195
+ */
196
+ export const runGit = (args, cwd, timeoutMs, home) => new Promise((resolve) => {
197
+ const env = home ? { ...process.env, HOME: home, XDG_CONFIG_HOME: path.join(home, '.config') } : undefined;
198
+ execFile('git', [...args], { cwd, timeout: timeoutMs, maxBuffer: GIT_MAX_BUFFER_BYTES, windowsHide: true, env }, (error, stdout) => {
199
+ // Trailing-only trim: `status --porcelain` lines carry a significant
200
+ // leading space (" M file"); a full trim() would eat the first character
201
+ // of the first filename.
202
+ if (!error) {
203
+ resolve(stdout.replace(RE_TRAILING_WHITESPACE, ''));
204
+ return;
205
+ }
206
+ resolve(salvageTruncatedStdout(error, stdout));
207
+ });
208
+ });
209
+ /**
210
+ * Recover usable output from a git process that overran maxBuffer.
211
+ *
212
+ * A huge dirty tree is exactly when changedFiles matters most, and callers cap
213
+ * the list anyway — so the truncated output minus its partial last line beats
214
+ * discarding everything.
215
+ *
216
+ * @param error - The exec error.
217
+ * @param stdout - Whatever was captured before the overflow.
218
+ * @returns The salvaged output, or undefined for any other failure.
219
+ */
220
+ function salvageTruncatedStdout(error, stdout) {
221
+ if (error.code !== STDOUT_MAXBUFFER_CODE)
222
+ return undefined;
223
+ if (typeof stdout !== 'string' || !stdout.includes('\n'))
224
+ return undefined;
225
+ return stdout.slice(0, stdout.lastIndexOf('\n')).replace(RE_TRAILING_WHITESPACE, '');
226
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Where the work happened: repository, branch, commit and dirty files, with
3
+ * remote credentials stripped before anything is stored or sent.
4
+ */
5
+ export type { GitContext, GitContextOptions, GitRunner, GitUserEmailOptions, RemoteParts } from './types/git.types.js';
6
+ export type { BranchCommit, BranchRef, SnapshotBranch } from './types/snapshot.types.js';
7
+ export { collectGitContext, developerIdentity, gitUserEmail, runGit } from './git-context.js';
8
+ export { collectBranchCommits, collectBranchRefs, resolveDefaultBranch } from './repo-snapshot.js';
9
+ export { normalizeRemote, remoteHash, stripRemoteCredentials } from './remote-sanitize.js';
@@ -0,0 +1,3 @@
1
+ export { collectGitContext, developerIdentity, gitUserEmail, runGit } from './git-context.js';
2
+ export { collectBranchCommits, collectBranchRefs, resolveDefaultBranch } from './repo-snapshot.js';
3
+ export { normalizeRemote, remoteHash, stripRemoteCredentials } from './remote-sanitize.js';
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Strip credentials a remote URL may embed (`https://user:token@host/...`).
3
+ *
4
+ * Runs before anything is stored, hashed or transmitted — a remote is the one
5
+ * piece of git context that routinely carries a live token.
6
+ *
7
+ * @param remote - Raw remote URL as git reports it.
8
+ * @returns The same URL without its userinfo.
9
+ */
10
+ export declare function stripRemoteCredentials(remote: string): string;
11
+ /**
12
+ * Canonical `host/org/repo` form of a remote.
13
+ *
14
+ * @param remote - Raw remote URL (https, ssh or scp-like).
15
+ * @returns The normalized identity, or undefined when it cannot be parsed.
16
+ */
17
+ export declare function normalizeRemote(remote: string): string | undefined;
18
+ /**
19
+ * Stable pseudonymous identifier for a repository.
20
+ *
21
+ * Lets the backend group a repository's activity without being told its name.
22
+ *
23
+ * @param remote - Raw remote URL.
24
+ * @returns Hash of the normalized remote, or undefined when unparseable.
25
+ */
26
+ export declare function remoteHash(remote: string): string | undefined;
@@ -0,0 +1,92 @@
1
+ import { sha256Hex } from '../events/event-id.js';
2
+ import { RE_DOT_GIT_SUFFIX, RE_LEADING_SLASHES, RE_SCP_REMOTE, RE_SCP_USERINFO, RE_TRAILING_SLASHES, RE_URL_SCHEME, RE_URL_USERINFO, RE_URL_USERINFO_WITH_PASSWORD } from './constants/git.constants.js';
3
+ /**
4
+ * Strip credentials a remote URL may embed (`https://user:token@host/...`).
5
+ *
6
+ * Runs before anything is stored, hashed or transmitted — a remote is the one
7
+ * piece of git context that routinely carries a live token.
8
+ *
9
+ * @param remote - Raw remote URL as git reports it.
10
+ * @returns The same URL without its userinfo.
11
+ */
12
+ export function stripRemoteCredentials(remote) {
13
+ return remote.replace(RE_URL_USERINFO, '$1').replace(RE_URL_USERINFO_WITH_PASSWORD, '$1');
14
+ }
15
+ /**
16
+ * Canonical `host/org/repo` form of a remote.
17
+ *
18
+ * @param remote - Raw remote URL (https, ssh or scp-like).
19
+ * @returns The normalized identity, or undefined when it cannot be parsed.
20
+ */
21
+ export function normalizeRemote(remote) {
22
+ const stripped = stripRemoteCredentials(remote.trim());
23
+ const parts = isUrlRemote(stripped) ? parseUrlRemote(stripped) : parseScpRemote(stripped);
24
+ if (!parts)
25
+ return undefined;
26
+ const cleanPath = parts.path.replace(RE_LEADING_SLASHES, '').replace(RE_DOT_GIT_SUFFIX, '').replace(RE_TRAILING_SLASHES, '');
27
+ if (!cleanPath)
28
+ return undefined;
29
+ return `${parts.host}/${cleanPath}`;
30
+ }
31
+ /**
32
+ * Stable pseudonymous identifier for a repository.
33
+ *
34
+ * Lets the backend group a repository's activity without being told its name.
35
+ *
36
+ * @param remote - Raw remote URL.
37
+ * @returns Hash of the normalized remote, or undefined when unparseable.
38
+ */
39
+ export function remoteHash(remote) {
40
+ const normalized = normalizeRemote(remote);
41
+ if (!normalized)
42
+ return undefined;
43
+ return sha256Hex(normalized);
44
+ }
45
+ /**
46
+ * Whether a remote is scheme-qualified.
47
+ *
48
+ * Branching on "://" rather than on whether `new URL()` throws is deliberate:
49
+ * a userless scp remote ("github.com:org/repo.git") is a *valid* URL whose
50
+ * scheme is the hostname, so a catch-based fallback would never run and the
51
+ * remote would be silently lost.
52
+ *
53
+ * @param remote - Credential-free remote.
54
+ * @returns True when it starts with `scheme://`.
55
+ */
56
+ function isUrlRemote(remote) {
57
+ return RE_URL_SCHEME.test(remote);
58
+ }
59
+ /**
60
+ * Host and path of a scheme-qualified remote.
61
+ *
62
+ * @param remote - Remote starting with `scheme://`.
63
+ * @returns Its parts, or undefined when the URL is malformed.
64
+ */
65
+ function parseUrlRemote(remote) {
66
+ try {
67
+ const url = new URL(remote);
68
+ if (!url.hostname || !url.pathname)
69
+ return undefined;
70
+ return { host: url.hostname, path: url.pathname };
71
+ }
72
+ catch {
73
+ return undefined;
74
+ }
75
+ }
76
+ /**
77
+ * Host and path of an scp-like remote (`[user@]host:path`).
78
+ *
79
+ * The userinfo is dropped before matching so an embedded password can never
80
+ * reach the result.
81
+ *
82
+ * @param remote - Remote without a scheme.
83
+ * @returns Its parts, or undefined when it is not scp-like.
84
+ */
85
+ function parseScpRemote(remote) {
86
+ const match = RE_SCP_REMOTE.exec(remote.replace(RE_SCP_USERINFO, ''));
87
+ const host = match?.[1];
88
+ const remotePath = match?.[2];
89
+ if (!host || !remotePath)
90
+ return undefined;
91
+ return { host, path: remotePath };
92
+ }
@@ -0,0 +1,40 @@
1
+ import type { GitRunner } from './types/git.types.js';
2
+ import type { BranchCommit, BranchRef, CollectCommitsOptions, CollectRefsOptions } from './types/snapshot.types.js';
3
+ export type { BranchCommit, BranchRef, SnapshotBranch } from './types/snapshot.types.js';
4
+ /**
5
+ * The recently committed branches and their heads, in one git process.
6
+ *
7
+ * Deliberately separate from {@link collectBranchCommits}: the heads are what
8
+ * the cache diff needs, and running the per-branch logs before that diff would
9
+ * spend ten processes on every closing turn to discover that nothing changed.
10
+ *
11
+ * @param options - Where to look and how many branches to take.
12
+ * @param run - Git runner; injectable for tests.
13
+ * @returns The branches, newest commit first. Empty outside a repository.
14
+ */
15
+ export declare function collectBranchRefs(options: CollectRefsOptions, run: GitRunner): Promise<BranchRef[]>;
16
+ /**
17
+ * What this repository calls its trunk.
18
+ *
19
+ * `origin/HEAD` first, because it is what the remote says; a local `main` or
20
+ * `master` second, for a clone that never fetched the symbolic ref. Undefined
21
+ * when neither answers — and undefined is meaningful, not a failure: a branch
22
+ * with no base to subtract is sent with no commits rather than with the
23
+ * repository's whole history.
24
+ *
25
+ * @param cwd - Repository directory.
26
+ * @param run - Git runner; the caller's budgeted one caps what this may spend.
27
+ * @returns The default branch name, or undefined.
28
+ */
29
+ export declare function resolveDefaultBranch(cwd: string, run: GitRunner): Promise<string | undefined>;
30
+ /**
31
+ * The commits one branch has that its default branch does not.
32
+ *
33
+ * @param branch - Branch to describe.
34
+ * @param options - Where to look, the base branch and the commit cap.
35
+ * @param run - Git runner.
36
+ * @returns The delta commits, newest first. Empty when there is no base to
37
+ * subtract, which is the honest answer: the alternative is reporting the
38
+ * trunk's commits as this branch's work.
39
+ */
40
+ export declare function collectBranchCommits(branch: string, options: CollectCommitsOptions, run: GitRunner): Promise<BranchCommit[]>;
@@ -0,0 +1,95 @@
1
+ import { ASSUMED_DEFAULT_BRANCHES, GIT_FIELD_SEPARATOR, GIT_ORIGIN_HEAD_ARGS, GIT_TIMEOUT_MS, gitBranchDeltaArgs, gitRecentBranchesArgs, gitVerifyRefArgs } from './constants/git.constants.js';
2
+ /** `origin/HEAD` is reported as `origin/main`; the branch is what comes after. */
3
+ const REMOTE_PREFIX = 'origin/';
4
+ /**
5
+ * The recently committed branches and their heads, in one git process.
6
+ *
7
+ * Deliberately separate from {@link collectBranchCommits}: the heads are what
8
+ * the cache diff needs, and running the per-branch logs before that diff would
9
+ * spend ten processes on every closing turn to discover that nothing changed.
10
+ *
11
+ * @param options - Where to look and how many branches to take.
12
+ * @param run - Git runner; injectable for tests.
13
+ * @returns The branches, newest commit first. Empty outside a repository.
14
+ */
15
+ export async function collectBranchRefs(options, run) {
16
+ const output = await run(gitRecentBranchesArgs(options.branchCount), options.cwd, GIT_TIMEOUT_MS);
17
+ if (!output)
18
+ return [];
19
+ return output
20
+ .split('\n')
21
+ .map((line) => parseBranchRef(line))
22
+ .filter((ref) => ref !== undefined);
23
+ }
24
+ /**
25
+ * What this repository calls its trunk.
26
+ *
27
+ * `origin/HEAD` first, because it is what the remote says; a local `main` or
28
+ * `master` second, for a clone that never fetched the symbolic ref. Undefined
29
+ * when neither answers — and undefined is meaningful, not a failure: a branch
30
+ * with no base to subtract is sent with no commits rather than with the
31
+ * repository's whole history.
32
+ *
33
+ * @param cwd - Repository directory.
34
+ * @param run - Git runner; the caller's budgeted one caps what this may spend.
35
+ * @returns The default branch name, or undefined.
36
+ */
37
+ export async function resolveDefaultBranch(cwd, run) {
38
+ const origin = await run(GIT_ORIGIN_HEAD_ARGS, cwd, GIT_TIMEOUT_MS);
39
+ if (origin)
40
+ return origin.startsWith(REMOTE_PREFIX) ? origin.slice(REMOTE_PREFIX.length) : origin;
41
+ for (const candidate of ASSUMED_DEFAULT_BRANCHES) {
42
+ if (await run(gitVerifyRefArgs(candidate), cwd, GIT_TIMEOUT_MS))
43
+ return candidate;
44
+ }
45
+ return undefined;
46
+ }
47
+ /**
48
+ * The commits one branch has that its default branch does not.
49
+ *
50
+ * @param branch - Branch to describe.
51
+ * @param options - Where to look, the base branch and the commit cap.
52
+ * @param run - Git runner.
53
+ * @returns The delta commits, newest first. Empty when there is no base to
54
+ * subtract, which is the honest answer: the alternative is reporting the
55
+ * trunk's commits as this branch's work.
56
+ */
57
+ export async function collectBranchCommits(branch, options, run) {
58
+ if (!options.defaultBranch || options.defaultBranch === branch)
59
+ return [];
60
+ const output = await run(gitBranchDeltaArgs(options.defaultBranch, branch, options.commitCount), options.cwd, GIT_TIMEOUT_MS);
61
+ if (!output)
62
+ return [];
63
+ return output
64
+ .split('\n')
65
+ .map((line) => parseCommit(line))
66
+ .filter((commit) => commit !== undefined);
67
+ }
68
+ /**
69
+ * One `for-each-ref` record.
70
+ *
71
+ * @param line - `name<sep>sha<sep>date`.
72
+ * @returns The branch, or undefined for a line missing a name or a head.
73
+ */
74
+ function parseBranchRef(line) {
75
+ const [name, headSha, lastCommitAt] = line.split(GIT_FIELD_SEPARATOR);
76
+ if (!name || !headSha)
77
+ return undefined;
78
+ return { name, headSha, lastCommitAt: lastCommitAt || undefined };
79
+ }
80
+ /**
81
+ * One `log` record.
82
+ *
83
+ * A commit with an empty subject is dropped rather than sent: an empty title is
84
+ * nothing for the grouping agent to read, and a work unit standing for it would
85
+ * be a row with no content.
86
+ *
87
+ * @param line - `sha<sep>subject<sep>date`.
88
+ * @returns The commit, or undefined for a line carrying no subject.
89
+ */
90
+ function parseCommit(line) {
91
+ const [sha, subject, authoredAt] = line.split(GIT_FIELD_SEPARATOR);
92
+ if (!sha || !subject)
93
+ return undefined;
94
+ return { sha, subject, authoredAt: authoredAt || undefined };
95
+ }