@herdctl/core 0.0.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 (520) hide show
  1. package/.turbo/turbo-build.log +4 -0
  2. package/.turbo/turbo-test.log +219 -0
  3. package/.turbo/turbo-typecheck.log +4 -0
  4. package/coverage/base.css +224 -0
  5. package/coverage/block-navigation.js +87 -0
  6. package/coverage/coverage-final.json +51 -0
  7. package/coverage/favicon.png +0 -0
  8. package/coverage/index.html +251 -0
  9. package/coverage/prettify.css +1 -0
  10. package/coverage/prettify.js +2 -0
  11. package/coverage/sort-arrow-sprite.png +0 -0
  12. package/coverage/sorter.js +210 -0
  13. package/coverage/src/config/index.html +191 -0
  14. package/coverage/src/config/index.ts.html +442 -0
  15. package/coverage/src/config/interpolate.ts.html +652 -0
  16. package/coverage/src/config/loader.ts.html +1501 -0
  17. package/coverage/src/config/merge.ts.html +823 -0
  18. package/coverage/src/config/parser.ts.html +1213 -0
  19. package/coverage/src/config/schema.ts.html +1123 -0
  20. package/coverage/src/fleet-manager/errors.ts.html +2326 -0
  21. package/coverage/src/fleet-manager/event-types.ts.html +1219 -0
  22. package/coverage/src/fleet-manager/fleet-manager.ts.html +7030 -0
  23. package/coverage/src/fleet-manager/index.html +206 -0
  24. package/coverage/src/fleet-manager/index.ts.html +469 -0
  25. package/coverage/src/fleet-manager/job-manager.ts.html +2074 -0
  26. package/coverage/src/fleet-manager/job-queue.ts.html +2479 -0
  27. package/coverage/src/fleet-manager/types.ts.html +2602 -0
  28. package/coverage/src/index.html +116 -0
  29. package/coverage/src/index.ts.html +181 -0
  30. package/coverage/src/runner/errors.ts.html +1006 -0
  31. package/coverage/src/runner/index.html +191 -0
  32. package/coverage/src/runner/index.ts.html +256 -0
  33. package/coverage/src/runner/job-executor.ts.html +1429 -0
  34. package/coverage/src/runner/message-processor.ts.html +1150 -0
  35. package/coverage/src/runner/sdk-adapter.ts.html +658 -0
  36. package/coverage/src/runner/types.ts.html +559 -0
  37. package/coverage/src/scheduler/errors.ts.html +388 -0
  38. package/coverage/src/scheduler/index.html +206 -0
  39. package/coverage/src/scheduler/index.ts.html +244 -0
  40. package/coverage/src/scheduler/interval.ts.html +652 -0
  41. package/coverage/src/scheduler/schedule-runner.ts.html +1411 -0
  42. package/coverage/src/scheduler/schedule-state.ts.html +718 -0
  43. package/coverage/src/scheduler/scheduler.ts.html +1795 -0
  44. package/coverage/src/scheduler/types.ts.html +733 -0
  45. package/coverage/src/state/directory.ts.html +736 -0
  46. package/coverage/src/state/errors.ts.html +376 -0
  47. package/coverage/src/state/fleet-state.ts.html +937 -0
  48. package/coverage/src/state/index.html +221 -0
  49. package/coverage/src/state/index.ts.html +322 -0
  50. package/coverage/src/state/job-metadata.ts.html +1420 -0
  51. package/coverage/src/state/job-output.ts.html +1033 -0
  52. package/coverage/src/state/schemas/fleet-state.ts.html +445 -0
  53. package/coverage/src/state/schemas/index.html +176 -0
  54. package/coverage/src/state/schemas/index.ts.html +286 -0
  55. package/coverage/src/state/schemas/job-metadata.ts.html +628 -0
  56. package/coverage/src/state/schemas/job-output.ts.html +616 -0
  57. package/coverage/src/state/schemas/session-info.ts.html +361 -0
  58. package/coverage/src/state/session.ts.html +844 -0
  59. package/coverage/src/state/types.ts.html +262 -0
  60. package/coverage/src/state/utils/atomic.ts.html +748 -0
  61. package/coverage/src/state/utils/index.html +146 -0
  62. package/coverage/src/state/utils/index.ts.html +103 -0
  63. package/coverage/src/state/utils/reads.ts.html +1621 -0
  64. package/coverage/src/work-sources/adapters/github.ts.html +3583 -0
  65. package/coverage/src/work-sources/adapters/index.html +131 -0
  66. package/coverage/src/work-sources/adapters/index.ts.html +277 -0
  67. package/coverage/src/work-sources/errors.ts.html +298 -0
  68. package/coverage/src/work-sources/index.html +176 -0
  69. package/coverage/src/work-sources/index.ts.html +529 -0
  70. package/coverage/src/work-sources/manager.ts.html +1324 -0
  71. package/coverage/src/work-sources/registry.ts.html +619 -0
  72. package/coverage/src/work-sources/types.ts.html +568 -0
  73. package/dist/config/__tests__/agent.test.d.ts +2 -0
  74. package/dist/config/__tests__/agent.test.d.ts.map +1 -0
  75. package/dist/config/__tests__/agent.test.js +752 -0
  76. package/dist/config/__tests__/agent.test.js.map +1 -0
  77. package/dist/config/__tests__/interpolate.test.d.ts +2 -0
  78. package/dist/config/__tests__/interpolate.test.d.ts.map +1 -0
  79. package/dist/config/__tests__/interpolate.test.js +509 -0
  80. package/dist/config/__tests__/interpolate.test.js.map +1 -0
  81. package/dist/config/__tests__/loader.test.d.ts +2 -0
  82. package/dist/config/__tests__/loader.test.d.ts.map +1 -0
  83. package/dist/config/__tests__/loader.test.js +631 -0
  84. package/dist/config/__tests__/loader.test.js.map +1 -0
  85. package/dist/config/__tests__/merge.test.d.ts +2 -0
  86. package/dist/config/__tests__/merge.test.d.ts.map +1 -0
  87. package/dist/config/__tests__/merge.test.js +672 -0
  88. package/dist/config/__tests__/merge.test.js.map +1 -0
  89. package/dist/config/__tests__/parser.test.d.ts +2 -0
  90. package/dist/config/__tests__/parser.test.d.ts.map +1 -0
  91. package/dist/config/__tests__/parser.test.js +476 -0
  92. package/dist/config/__tests__/parser.test.js.map +1 -0
  93. package/dist/config/__tests__/schema.test.d.ts +2 -0
  94. package/dist/config/__tests__/schema.test.d.ts.map +1 -0
  95. package/dist/config/__tests__/schema.test.js +776 -0
  96. package/dist/config/__tests__/schema.test.js.map +1 -0
  97. package/dist/config/index.d.ts +11 -0
  98. package/dist/config/index.d.ts.map +1 -0
  99. package/dist/config/index.js +26 -0
  100. package/dist/config/index.js.map +1 -0
  101. package/dist/config/interpolate.d.ts +76 -0
  102. package/dist/config/interpolate.d.ts.map +1 -0
  103. package/dist/config/interpolate.js +143 -0
  104. package/dist/config/interpolate.js.map +1 -0
  105. package/dist/config/loader.d.ts +147 -0
  106. package/dist/config/loader.d.ts.map +1 -0
  107. package/dist/config/loader.js +336 -0
  108. package/dist/config/loader.js.map +1 -0
  109. package/dist/config/merge.d.ts +84 -0
  110. package/dist/config/merge.d.ts.map +1 -0
  111. package/dist/config/merge.js +138 -0
  112. package/dist/config/merge.js.map +1 -0
  113. package/dist/config/parser.d.ts +143 -0
  114. package/dist/config/parser.d.ts.map +1 -0
  115. package/dist/config/parser.js +316 -0
  116. package/dist/config/parser.js.map +1 -0
  117. package/dist/config/schema.d.ts +1906 -0
  118. package/dist/config/schema.d.ts.map +1 -0
  119. package/dist/config/schema.js +268 -0
  120. package/dist/config/schema.js.map +1 -0
  121. package/dist/fleet-manager/__tests__/coverage.test.d.ts +13 -0
  122. package/dist/fleet-manager/__tests__/coverage.test.d.ts.map +1 -0
  123. package/dist/fleet-manager/__tests__/coverage.test.js +2282 -0
  124. package/dist/fleet-manager/__tests__/coverage.test.js.map +1 -0
  125. package/dist/fleet-manager/__tests__/errors.test.d.ts +7 -0
  126. package/dist/fleet-manager/__tests__/errors.test.d.ts.map +1 -0
  127. package/dist/fleet-manager/__tests__/errors.test.js +557 -0
  128. package/dist/fleet-manager/__tests__/errors.test.js.map +1 -0
  129. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts +7 -0
  130. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts.map +1 -0
  131. package/dist/fleet-manager/__tests__/event-helpers.test.js +368 -0
  132. package/dist/fleet-manager/__tests__/event-helpers.test.js.map +1 -0
  133. package/dist/fleet-manager/__tests__/integration.test.d.ts +11 -0
  134. package/dist/fleet-manager/__tests__/integration.test.d.ts.map +1 -0
  135. package/dist/fleet-manager/__tests__/integration.test.js +949 -0
  136. package/dist/fleet-manager/__tests__/integration.test.js.map +1 -0
  137. package/dist/fleet-manager/__tests__/job-control.test.d.ts +7 -0
  138. package/dist/fleet-manager/__tests__/job-control.test.d.ts.map +1 -0
  139. package/dist/fleet-manager/__tests__/job-control.test.js +215 -0
  140. package/dist/fleet-manager/__tests__/job-control.test.js.map +1 -0
  141. package/dist/fleet-manager/__tests__/job-manager.test.d.ts +7 -0
  142. package/dist/fleet-manager/__tests__/job-manager.test.d.ts.map +1 -0
  143. package/dist/fleet-manager/__tests__/job-manager.test.js +659 -0
  144. package/dist/fleet-manager/__tests__/job-manager.test.js.map +1 -0
  145. package/dist/fleet-manager/__tests__/job-queue.test.d.ts +5 -0
  146. package/dist/fleet-manager/__tests__/job-queue.test.d.ts.map +1 -0
  147. package/dist/fleet-manager/__tests__/job-queue.test.js +315 -0
  148. package/dist/fleet-manager/__tests__/job-queue.test.js.map +1 -0
  149. package/dist/fleet-manager/__tests__/reload.test.d.ts +7 -0
  150. package/dist/fleet-manager/__tests__/reload.test.d.ts.map +1 -0
  151. package/dist/fleet-manager/__tests__/reload.test.js +609 -0
  152. package/dist/fleet-manager/__tests__/reload.test.js.map +1 -0
  153. package/dist/fleet-manager/__tests__/status-queries.test.d.ts +7 -0
  154. package/dist/fleet-manager/__tests__/status-queries.test.d.ts.map +1 -0
  155. package/dist/fleet-manager/__tests__/status-queries.test.js +488 -0
  156. package/dist/fleet-manager/__tests__/status-queries.test.js.map +1 -0
  157. package/dist/fleet-manager/__tests__/trigger.test.d.ts +7 -0
  158. package/dist/fleet-manager/__tests__/trigger.test.d.ts.map +1 -0
  159. package/dist/fleet-manager/__tests__/trigger.test.js +471 -0
  160. package/dist/fleet-manager/__tests__/trigger.test.js.map +1 -0
  161. package/dist/fleet-manager/errors.d.ts +407 -0
  162. package/dist/fleet-manager/errors.d.ts.map +1 -0
  163. package/dist/fleet-manager/errors.js +569 -0
  164. package/dist/fleet-manager/errors.js.map +1 -0
  165. package/dist/fleet-manager/event-types.d.ts +302 -0
  166. package/dist/fleet-manager/event-types.d.ts.map +1 -0
  167. package/dist/fleet-manager/event-types.js +9 -0
  168. package/dist/fleet-manager/event-types.js.map +1 -0
  169. package/dist/fleet-manager/fleet-manager.d.ts +699 -0
  170. package/dist/fleet-manager/fleet-manager.d.ts.map +1 -0
  171. package/dist/fleet-manager/fleet-manager.js +1906 -0
  172. package/dist/fleet-manager/fleet-manager.js.map +1 -0
  173. package/dist/fleet-manager/index.d.ts +17 -0
  174. package/dist/fleet-manager/index.d.ts.map +1 -0
  175. package/dist/fleet-manager/index.js +29 -0
  176. package/dist/fleet-manager/index.js.map +1 -0
  177. package/dist/fleet-manager/job-manager.d.ts +271 -0
  178. package/dist/fleet-manager/job-manager.d.ts.map +1 -0
  179. package/dist/fleet-manager/job-manager.js +443 -0
  180. package/dist/fleet-manager/job-manager.js.map +1 -0
  181. package/dist/fleet-manager/job-queue.d.ts +422 -0
  182. package/dist/fleet-manager/job-queue.d.ts.map +1 -0
  183. package/dist/fleet-manager/job-queue.js +448 -0
  184. package/dist/fleet-manager/job-queue.js.map +1 -0
  185. package/dist/fleet-manager/types.d.ts +680 -0
  186. package/dist/fleet-manager/types.d.ts.map +1 -0
  187. package/dist/fleet-manager/types.js +8 -0
  188. package/dist/fleet-manager/types.js.map +1 -0
  189. package/dist/index.d.ts +20 -0
  190. package/dist/index.d.ts.map +1 -0
  191. package/dist/index.js +26 -0
  192. package/dist/index.js.map +1 -0
  193. package/dist/runner/__tests__/errors.test.d.ts +2 -0
  194. package/dist/runner/__tests__/errors.test.d.ts.map +1 -0
  195. package/dist/runner/__tests__/errors.test.js +264 -0
  196. package/dist/runner/__tests__/errors.test.js.map +1 -0
  197. package/dist/runner/__tests__/job-executor.test.d.ts +2 -0
  198. package/dist/runner/__tests__/job-executor.test.d.ts.map +1 -0
  199. package/dist/runner/__tests__/job-executor.test.js +1345 -0
  200. package/dist/runner/__tests__/job-executor.test.js.map +1 -0
  201. package/dist/runner/__tests__/message-processor.test.d.ts +2 -0
  202. package/dist/runner/__tests__/message-processor.test.d.ts.map +1 -0
  203. package/dist/runner/__tests__/message-processor.test.js +768 -0
  204. package/dist/runner/__tests__/message-processor.test.js.map +1 -0
  205. package/dist/runner/__tests__/sdk-adapter.test.d.ts +2 -0
  206. package/dist/runner/__tests__/sdk-adapter.test.d.ts.map +1 -0
  207. package/dist/runner/__tests__/sdk-adapter.test.js +554 -0
  208. package/dist/runner/__tests__/sdk-adapter.test.js.map +1 -0
  209. package/dist/runner/errors.d.ts +121 -0
  210. package/dist/runner/errors.d.ts.map +1 -0
  211. package/dist/runner/errors.js +212 -0
  212. package/dist/runner/errors.js.map +1 -0
  213. package/dist/runner/index.d.ts +12 -0
  214. package/dist/runner/index.d.ts.map +1 -0
  215. package/dist/runner/index.js +15 -0
  216. package/dist/runner/index.js.map +1 -0
  217. package/dist/runner/job-executor.d.ts +98 -0
  218. package/dist/runner/job-executor.d.ts.map +1 -0
  219. package/dist/runner/job-executor.js +333 -0
  220. package/dist/runner/job-executor.js.map +1 -0
  221. package/dist/runner/message-processor.d.ts +45 -0
  222. package/dist/runner/message-processor.d.ts.map +1 -0
  223. package/dist/runner/message-processor.js +294 -0
  224. package/dist/runner/message-processor.js.map +1 -0
  225. package/dist/runner/sdk-adapter.d.ts +60 -0
  226. package/dist/runner/sdk-adapter.d.ts.map +1 -0
  227. package/dist/runner/sdk-adapter.js +138 -0
  228. package/dist/runner/sdk-adapter.js.map +1 -0
  229. package/dist/runner/types.d.ts +135 -0
  230. package/dist/runner/types.d.ts.map +1 -0
  231. package/dist/runner/types.js +7 -0
  232. package/dist/runner/types.js.map +1 -0
  233. package/dist/scheduler/__tests__/errors.test.d.ts +2 -0
  234. package/dist/scheduler/__tests__/errors.test.d.ts.map +1 -0
  235. package/dist/scheduler/__tests__/errors.test.js +101 -0
  236. package/dist/scheduler/__tests__/errors.test.js.map +1 -0
  237. package/dist/scheduler/__tests__/interval.test.d.ts +2 -0
  238. package/dist/scheduler/__tests__/interval.test.d.ts.map +1 -0
  239. package/dist/scheduler/__tests__/interval.test.js +419 -0
  240. package/dist/scheduler/__tests__/interval.test.js.map +1 -0
  241. package/dist/scheduler/__tests__/schedule-runner.test.d.ts +2 -0
  242. package/dist/scheduler/__tests__/schedule-runner.test.d.ts.map +1 -0
  243. package/dist/scheduler/__tests__/schedule-runner.test.js +634 -0
  244. package/dist/scheduler/__tests__/schedule-runner.test.js.map +1 -0
  245. package/dist/scheduler/__tests__/schedule-state.test.d.ts +2 -0
  246. package/dist/scheduler/__tests__/schedule-state.test.d.ts.map +1 -0
  247. package/dist/scheduler/__tests__/schedule-state.test.js +572 -0
  248. package/dist/scheduler/__tests__/schedule-state.test.js.map +1 -0
  249. package/dist/scheduler/__tests__/scheduler.test.d.ts +2 -0
  250. package/dist/scheduler/__tests__/scheduler.test.d.ts.map +1 -0
  251. package/dist/scheduler/__tests__/scheduler.test.js +987 -0
  252. package/dist/scheduler/__tests__/scheduler.test.js.map +1 -0
  253. package/dist/scheduler/errors.d.ts +61 -0
  254. package/dist/scheduler/errors.d.ts.map +1 -0
  255. package/dist/scheduler/errors.js +81 -0
  256. package/dist/scheduler/errors.js.map +1 -0
  257. package/dist/scheduler/index.d.ts +13 -0
  258. package/dist/scheduler/index.d.ts.map +1 -0
  259. package/dist/scheduler/index.js +17 -0
  260. package/dist/scheduler/index.js.map +1 -0
  261. package/dist/scheduler/interval.d.ts +64 -0
  262. package/dist/scheduler/interval.d.ts.map +1 -0
  263. package/dist/scheduler/interval.js +139 -0
  264. package/dist/scheduler/interval.js.map +1 -0
  265. package/dist/scheduler/schedule-runner.d.ts +149 -0
  266. package/dist/scheduler/schedule-runner.d.ts.map +1 -0
  267. package/dist/scheduler/schedule-runner.js +277 -0
  268. package/dist/scheduler/schedule-runner.js.map +1 -0
  269. package/dist/scheduler/schedule-state.d.ts +105 -0
  270. package/dist/scheduler/schedule-state.d.ts.map +1 -0
  271. package/dist/scheduler/schedule-state.js +151 -0
  272. package/dist/scheduler/schedule-state.js.map +1 -0
  273. package/dist/scheduler/scheduler.d.ts +138 -0
  274. package/dist/scheduler/scheduler.d.ts.map +1 -0
  275. package/dist/scheduler/scheduler.js +423 -0
  276. package/dist/scheduler/scheduler.js.map +1 -0
  277. package/dist/scheduler/types.d.ts +160 -0
  278. package/dist/scheduler/types.d.ts.map +1 -0
  279. package/dist/scheduler/types.js +8 -0
  280. package/dist/scheduler/types.js.map +1 -0
  281. package/dist/state/__tests__/directory.test.d.ts +2 -0
  282. package/dist/state/__tests__/directory.test.d.ts.map +1 -0
  283. package/dist/state/__tests__/directory.test.js +414 -0
  284. package/dist/state/__tests__/directory.test.js.map +1 -0
  285. package/dist/state/__tests__/fleet-state.test.d.ts +2 -0
  286. package/dist/state/__tests__/fleet-state.test.d.ts.map +1 -0
  287. package/dist/state/__tests__/fleet-state.test.js +696 -0
  288. package/dist/state/__tests__/fleet-state.test.js.map +1 -0
  289. package/dist/state/__tests__/job-metadata-schema.test.d.ts +2 -0
  290. package/dist/state/__tests__/job-metadata-schema.test.d.ts.map +1 -0
  291. package/dist/state/__tests__/job-metadata-schema.test.js +329 -0
  292. package/dist/state/__tests__/job-metadata-schema.test.js.map +1 -0
  293. package/dist/state/__tests__/job-metadata.test.d.ts +2 -0
  294. package/dist/state/__tests__/job-metadata.test.d.ts.map +1 -0
  295. package/dist/state/__tests__/job-metadata.test.js +667 -0
  296. package/dist/state/__tests__/job-metadata.test.js.map +1 -0
  297. package/dist/state/__tests__/job-output.test.d.ts +2 -0
  298. package/dist/state/__tests__/job-output.test.d.ts.map +1 -0
  299. package/dist/state/__tests__/job-output.test.js +672 -0
  300. package/dist/state/__tests__/job-output.test.js.map +1 -0
  301. package/dist/state/__tests__/session-schema.test.d.ts +2 -0
  302. package/dist/state/__tests__/session-schema.test.d.ts.map +1 -0
  303. package/dist/state/__tests__/session-schema.test.js +323 -0
  304. package/dist/state/__tests__/session-schema.test.js.map +1 -0
  305. package/dist/state/__tests__/session.test.d.ts +2 -0
  306. package/dist/state/__tests__/session.test.d.ts.map +1 -0
  307. package/dist/state/__tests__/session.test.js +468 -0
  308. package/dist/state/__tests__/session.test.js.map +1 -0
  309. package/dist/state/directory.d.ts +42 -0
  310. package/dist/state/directory.d.ts.map +1 -0
  311. package/dist/state/directory.js +170 -0
  312. package/dist/state/directory.js.map +1 -0
  313. package/dist/state/errors.d.ts +44 -0
  314. package/dist/state/errors.d.ts.map +1 -0
  315. package/dist/state/errors.js +82 -0
  316. package/dist/state/errors.js.map +1 -0
  317. package/dist/state/fleet-state.d.ts +126 -0
  318. package/dist/state/fleet-state.d.ts.map +1 -0
  319. package/dist/state/fleet-state.js +196 -0
  320. package/dist/state/fleet-state.js.map +1 -0
  321. package/dist/state/index.d.ts +21 -0
  322. package/dist/state/index.d.ts.map +1 -0
  323. package/dist/state/index.js +30 -0
  324. package/dist/state/index.js.map +1 -0
  325. package/dist/state/job-metadata.d.ts +151 -0
  326. package/dist/state/job-metadata.d.ts.map +1 -0
  327. package/dist/state/job-metadata.js +287 -0
  328. package/dist/state/job-metadata.js.map +1 -0
  329. package/dist/state/job-output.d.ts +116 -0
  330. package/dist/state/job-output.d.ts.map +1 -0
  331. package/dist/state/job-output.js +218 -0
  332. package/dist/state/job-output.js.map +1 -0
  333. package/dist/state/schemas/__tests__/job-output.test.d.ts +2 -0
  334. package/dist/state/schemas/__tests__/job-output.test.d.ts.map +1 -0
  335. package/dist/state/schemas/__tests__/job-output.test.js +279 -0
  336. package/dist/state/schemas/__tests__/job-output.test.js.map +1 -0
  337. package/dist/state/schemas/fleet-state.d.ts +249 -0
  338. package/dist/state/schemas/fleet-state.d.ts.map +1 -0
  339. package/dist/state/schemas/fleet-state.js +97 -0
  340. package/dist/state/schemas/fleet-state.js.map +1 -0
  341. package/dist/state/schemas/index.d.ts +10 -0
  342. package/dist/state/schemas/index.d.ts.map +1 -0
  343. package/dist/state/schemas/index.js +10 -0
  344. package/dist/state/schemas/index.js.map +1 -0
  345. package/dist/state/schemas/job-metadata.d.ts +118 -0
  346. package/dist/state/schemas/job-metadata.d.ts.map +1 -0
  347. package/dist/state/schemas/job-metadata.js +123 -0
  348. package/dist/state/schemas/job-metadata.js.map +1 -0
  349. package/dist/state/schemas/job-output.d.ts +291 -0
  350. package/dist/state/schemas/job-output.d.ts.map +1 -0
  351. package/dist/state/schemas/job-output.js +132 -0
  352. package/dist/state/schemas/job-output.js.map +1 -0
  353. package/dist/state/schemas/session-info.d.ts +65 -0
  354. package/dist/state/schemas/session-info.d.ts.map +1 -0
  355. package/dist/state/schemas/session-info.js +58 -0
  356. package/dist/state/schemas/session-info.js.map +1 -0
  357. package/dist/state/session.d.ts +92 -0
  358. package/dist/state/session.d.ts.map +1 -0
  359. package/dist/state/session.js +173 -0
  360. package/dist/state/session.js.map +1 -0
  361. package/dist/state/types.d.ts +54 -0
  362. package/dist/state/types.d.ts.map +1 -0
  363. package/dist/state/types.js +18 -0
  364. package/dist/state/types.js.map +1 -0
  365. package/dist/state/utils/__tests__/atomic.test.d.ts +2 -0
  366. package/dist/state/utils/__tests__/atomic.test.d.ts.map +1 -0
  367. package/dist/state/utils/__tests__/atomic.test.js +537 -0
  368. package/dist/state/utils/__tests__/atomic.test.js.map +1 -0
  369. package/dist/state/utils/__tests__/reads.test.d.ts +2 -0
  370. package/dist/state/utils/__tests__/reads.test.d.ts.map +1 -0
  371. package/dist/state/utils/__tests__/reads.test.js +792 -0
  372. package/dist/state/utils/__tests__/reads.test.js.map +1 -0
  373. package/dist/state/utils/atomic.d.ts +89 -0
  374. package/dist/state/utils/atomic.d.ts.map +1 -0
  375. package/dist/state/utils/atomic.js +157 -0
  376. package/dist/state/utils/atomic.js.map +1 -0
  377. package/dist/state/utils/index.d.ts +6 -0
  378. package/dist/state/utils/index.d.ts.map +1 -0
  379. package/dist/state/utils/index.js +6 -0
  380. package/dist/state/utils/index.js.map +1 -0
  381. package/dist/state/utils/reads.d.ts +196 -0
  382. package/dist/state/utils/reads.d.ts.map +1 -0
  383. package/dist/state/utils/reads.js +346 -0
  384. package/dist/state/utils/reads.js.map +1 -0
  385. package/dist/work-sources/__tests__/github.test.d.ts +2 -0
  386. package/dist/work-sources/__tests__/github.test.d.ts.map +1 -0
  387. package/dist/work-sources/__tests__/github.test.js +1334 -0
  388. package/dist/work-sources/__tests__/github.test.js.map +1 -0
  389. package/dist/work-sources/__tests__/manager.test.d.ts +2 -0
  390. package/dist/work-sources/__tests__/manager.test.d.ts.map +1 -0
  391. package/dist/work-sources/__tests__/manager.test.js +424 -0
  392. package/dist/work-sources/__tests__/manager.test.js.map +1 -0
  393. package/dist/work-sources/__tests__/registry.test.d.ts +2 -0
  394. package/dist/work-sources/__tests__/registry.test.d.ts.map +1 -0
  395. package/dist/work-sources/__tests__/registry.test.js +381 -0
  396. package/dist/work-sources/__tests__/registry.test.js.map +1 -0
  397. package/dist/work-sources/__tests__/types.test.d.ts +2 -0
  398. package/dist/work-sources/__tests__/types.test.d.ts.map +1 -0
  399. package/dist/work-sources/__tests__/types.test.js +406 -0
  400. package/dist/work-sources/__tests__/types.test.js.map +1 -0
  401. package/dist/work-sources/adapters/github.d.ts +290 -0
  402. package/dist/work-sources/adapters/github.d.ts.map +1 -0
  403. package/dist/work-sources/adapters/github.js +803 -0
  404. package/dist/work-sources/adapters/github.js.map +1 -0
  405. package/dist/work-sources/adapters/index.d.ts +10 -0
  406. package/dist/work-sources/adapters/index.d.ts.map +1 -0
  407. package/dist/work-sources/adapters/index.js +31 -0
  408. package/dist/work-sources/adapters/index.js.map +1 -0
  409. package/dist/work-sources/errors.d.ts +40 -0
  410. package/dist/work-sources/errors.d.ts.map +1 -0
  411. package/dist/work-sources/errors.js +54 -0
  412. package/dist/work-sources/errors.js.map +1 -0
  413. package/dist/work-sources/index.d.ts +105 -0
  414. package/dist/work-sources/index.d.ts.map +1 -0
  415. package/dist/work-sources/index.js +24 -0
  416. package/dist/work-sources/index.js.map +1 -0
  417. package/dist/work-sources/manager.d.ts +370 -0
  418. package/dist/work-sources/manager.d.ts.map +1 -0
  419. package/dist/work-sources/manager.js +61 -0
  420. package/dist/work-sources/manager.js.map +1 -0
  421. package/dist/work-sources/registry.d.ts +128 -0
  422. package/dist/work-sources/registry.d.ts.map +1 -0
  423. package/dist/work-sources/registry.js +132 -0
  424. package/dist/work-sources/registry.js.map +1 -0
  425. package/dist/work-sources/types.d.ts +127 -0
  426. package/dist/work-sources/types.d.ts.map +1 -0
  427. package/dist/work-sources/types.js +8 -0
  428. package/dist/work-sources/types.js.map +1 -0
  429. package/package.json +23 -0
  430. package/src/config/__tests__/agent.test.ts +864 -0
  431. package/src/config/__tests__/interpolate.test.ts +644 -0
  432. package/src/config/__tests__/loader.test.ts +784 -0
  433. package/src/config/__tests__/merge.test.ts +751 -0
  434. package/src/config/__tests__/parser.test.ts +533 -0
  435. package/src/config/__tests__/schema.test.ts +873 -0
  436. package/src/config/index.ts +119 -0
  437. package/src/config/interpolate.ts +189 -0
  438. package/src/config/loader.ts +472 -0
  439. package/src/config/merge.ts +246 -0
  440. package/src/config/parser.ts +376 -0
  441. package/src/config/schema.ts +346 -0
  442. package/src/fleet-manager/__tests__/coverage.test.ts +2869 -0
  443. package/src/fleet-manager/__tests__/errors.test.ts +660 -0
  444. package/src/fleet-manager/__tests__/event-helpers.test.ts +448 -0
  445. package/src/fleet-manager/__tests__/integration.test.ts +1209 -0
  446. package/src/fleet-manager/__tests__/job-control.test.ts +283 -0
  447. package/src/fleet-manager/__tests__/job-manager.test.ts +869 -0
  448. package/src/fleet-manager/__tests__/job-queue.test.ts +401 -0
  449. package/src/fleet-manager/__tests__/reload.test.ts +751 -0
  450. package/src/fleet-manager/__tests__/status-queries.test.ts +595 -0
  451. package/src/fleet-manager/__tests__/trigger.test.ts +601 -0
  452. package/src/fleet-manager/errors.ts +747 -0
  453. package/src/fleet-manager/event-types.ts +378 -0
  454. package/src/fleet-manager/fleet-manager.ts +2315 -0
  455. package/src/fleet-manager/index.ts +128 -0
  456. package/src/fleet-manager/job-manager.ts +663 -0
  457. package/src/fleet-manager/job-queue.ts +798 -0
  458. package/src/fleet-manager/types.ts +839 -0
  459. package/src/index.ts +32 -0
  460. package/src/runner/__tests__/errors.test.ts +382 -0
  461. package/src/runner/__tests__/job-executor.test.ts +1708 -0
  462. package/src/runner/__tests__/message-processor.test.ts +960 -0
  463. package/src/runner/__tests__/sdk-adapter.test.ts +626 -0
  464. package/src/runner/errors.ts +307 -0
  465. package/src/runner/index.ts +57 -0
  466. package/src/runner/job-executor.ts +448 -0
  467. package/src/runner/message-processor.ts +355 -0
  468. package/src/runner/sdk-adapter.ts +191 -0
  469. package/src/runner/types.ts +158 -0
  470. package/src/scheduler/__tests__/errors.test.ts +159 -0
  471. package/src/scheduler/__tests__/interval.test.ts +515 -0
  472. package/src/scheduler/__tests__/schedule-runner.test.ts +798 -0
  473. package/src/scheduler/__tests__/schedule-state.test.ts +671 -0
  474. package/src/scheduler/__tests__/scheduler.test.ts +1280 -0
  475. package/src/scheduler/errors.ts +101 -0
  476. package/src/scheduler/index.ts +53 -0
  477. package/src/scheduler/interval.ts +189 -0
  478. package/src/scheduler/schedule-runner.ts +442 -0
  479. package/src/scheduler/schedule-state.ts +211 -0
  480. package/src/scheduler/scheduler.ts +570 -0
  481. package/src/scheduler/types.ts +216 -0
  482. package/src/state/__tests__/directory.test.ts +595 -0
  483. package/src/state/__tests__/fleet-state.test.ts +868 -0
  484. package/src/state/__tests__/job-metadata-schema.test.ts +414 -0
  485. package/src/state/__tests__/job-metadata.test.ts +831 -0
  486. package/src/state/__tests__/job-output.test.ts +856 -0
  487. package/src/state/__tests__/session-schema.test.ts +378 -0
  488. package/src/state/__tests__/session.test.ts +604 -0
  489. package/src/state/directory.ts +217 -0
  490. package/src/state/errors.ts +97 -0
  491. package/src/state/fleet-state.ts +284 -0
  492. package/src/state/index.ts +79 -0
  493. package/src/state/job-metadata.ts +445 -0
  494. package/src/state/job-output.ts +316 -0
  495. package/src/state/schemas/__tests__/job-output.test.ts +338 -0
  496. package/src/state/schemas/fleet-state.ts +120 -0
  497. package/src/state/schemas/index.ts +67 -0
  498. package/src/state/schemas/job-metadata.ts +181 -0
  499. package/src/state/schemas/job-output.ts +177 -0
  500. package/src/state/schemas/session-info.ts +92 -0
  501. package/src/state/session.ts +253 -0
  502. package/src/state/types.ts +59 -0
  503. package/src/state/utils/__tests__/atomic.test.ts +723 -0
  504. package/src/state/utils/__tests__/reads.test.ts +1071 -0
  505. package/src/state/utils/atomic.ts +221 -0
  506. package/src/state/utils/index.ts +6 -0
  507. package/src/state/utils/reads.ts +512 -0
  508. package/src/work-sources/__tests__/github.test.ts +1800 -0
  509. package/src/work-sources/__tests__/manager.test.ts +529 -0
  510. package/src/work-sources/__tests__/registry.test.ts +477 -0
  511. package/src/work-sources/__tests__/types.test.ts +479 -0
  512. package/src/work-sources/adapters/github.ts +1166 -0
  513. package/src/work-sources/adapters/index.ts +64 -0
  514. package/src/work-sources/errors.ts +71 -0
  515. package/src/work-sources/index.ts +148 -0
  516. package/src/work-sources/manager.ts +413 -0
  517. package/src/work-sources/registry.ts +178 -0
  518. package/src/work-sources/types.ts +161 -0
  519. package/tsconfig.json +9 -0
  520. package/vitest.config.ts +19 -0
@@ -0,0 +1,723 @@
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
2
+ import { mkdir, readFile, rm, realpath, writeFile, stat } from "node:fs/promises";
3
+ import { join, dirname, basename } from "node:path";
4
+ import { tmpdir } from "node:os";
5
+ import {
6
+ atomicWriteFile,
7
+ atomicWriteYaml,
8
+ atomicWriteJson,
9
+ appendJsonl,
10
+ generateTempPath,
11
+ AtomicWriteError,
12
+ renameWithRetry,
13
+ } from "../atomic.js";
14
+ import { parse as parseYaml } from "yaml";
15
+
16
+ // Helper to create a temp directory
17
+ async function createTempDir(): Promise<string> {
18
+ const baseDir = join(
19
+ tmpdir(),
20
+ `herdctl-atomic-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
21
+ );
22
+ await mkdir(baseDir, { recursive: true });
23
+ // Resolve to real path to handle macOS /var -> /private/var symlink
24
+ return await realpath(baseDir);
25
+ }
26
+
27
+ describe("generateTempPath", () => {
28
+ it("generates temp path in the same directory as target", () => {
29
+ const targetPath = "/some/dir/file.yaml";
30
+ const tempPath = generateTempPath(targetPath);
31
+
32
+ expect(dirname(tempPath)).toBe("/some/dir");
33
+ });
34
+
35
+ it("uses .<filename>.tmp.<random> pattern", () => {
36
+ const targetPath = "/some/dir/state.yaml";
37
+ const tempPath = generateTempPath(targetPath);
38
+ const tempFilename = basename(tempPath);
39
+
40
+ expect(tempFilename).toMatch(/^\.state\.yaml\.tmp\.[a-f0-9]{16}$/);
41
+ });
42
+
43
+ it("generates unique temp paths on each call", () => {
44
+ const targetPath = "/some/dir/file.yaml";
45
+ const tempPath1 = generateTempPath(targetPath);
46
+ const tempPath2 = generateTempPath(targetPath);
47
+
48
+ expect(tempPath1).not.toBe(tempPath2);
49
+ });
50
+
51
+ it("handles filenames with multiple dots", () => {
52
+ const targetPath = "/some/dir/file.backup.yaml";
53
+ const tempPath = generateTempPath(targetPath);
54
+ const tempFilename = basename(tempPath);
55
+
56
+ expect(tempFilename).toMatch(/^\.file\.backup\.yaml\.tmp\.[a-f0-9]{16}$/);
57
+ });
58
+
59
+ it("handles filenames without extensions", () => {
60
+ const targetPath = "/some/dir/Makefile";
61
+ const tempPath = generateTempPath(targetPath);
62
+ const tempFilename = basename(tempPath);
63
+
64
+ expect(tempFilename).toMatch(/^\.Makefile\.tmp\.[a-f0-9]{16}$/);
65
+ });
66
+ });
67
+
68
+ describe("AtomicWriteError", () => {
69
+ it("creates error with correct properties", () => {
70
+ const cause = new Error("Original error");
71
+ const error = new AtomicWriteError(
72
+ "Failed to write",
73
+ "/path/to/file.yaml",
74
+ "/path/to/.file.yaml.tmp.abc123",
75
+ cause
76
+ );
77
+
78
+ expect(error.name).toBe("AtomicWriteError");
79
+ expect(error.message).toBe("Failed to write");
80
+ expect(error.path).toBe("/path/to/file.yaml");
81
+ expect(error.tempPath).toBe("/path/to/.file.yaml.tmp.abc123");
82
+ expect(error.cause).toBe(cause);
83
+ });
84
+
85
+ it("creates error without temp path", () => {
86
+ const error = new AtomicWriteError("Failed to write", "/path/to/file.yaml");
87
+
88
+ expect(error.tempPath).toBeUndefined();
89
+ expect(error.cause).toBeUndefined();
90
+ });
91
+ });
92
+
93
+ describe("atomicWriteFile", () => {
94
+ let tempDir: string;
95
+
96
+ beforeEach(async () => {
97
+ tempDir = await createTempDir();
98
+ });
99
+
100
+ afterEach(async () => {
101
+ await rm(tempDir, { recursive: true, force: true });
102
+ });
103
+
104
+ it("writes content to a new file", async () => {
105
+ const filePath = join(tempDir, "test.txt");
106
+ const content = "Hello, World!";
107
+
108
+ await atomicWriteFile(filePath, content);
109
+
110
+ const result = await readFile(filePath, "utf-8");
111
+ expect(result).toBe(content);
112
+ });
113
+
114
+ it("overwrites existing file content", async () => {
115
+ const filePath = join(tempDir, "test.txt");
116
+ await writeFile(filePath, "old content", "utf-8");
117
+
118
+ await atomicWriteFile(filePath, "new content");
119
+
120
+ const result = await readFile(filePath, "utf-8");
121
+ expect(result).toBe("new content");
122
+ });
123
+
124
+ it("cleans up temp file on successful write", async () => {
125
+ const filePath = join(tempDir, "test.txt");
126
+ await atomicWriteFile(filePath, "content");
127
+
128
+ // List all files in temp dir
129
+ const { readdir } = await import("node:fs/promises");
130
+ const files = await readdir(tempDir);
131
+
132
+ // Should only have the target file, no temp files
133
+ expect(files).toEqual(["test.txt"]);
134
+ });
135
+
136
+ it("writes with specified encoding", async () => {
137
+ const filePath = join(tempDir, "test.txt");
138
+ const content = "Unicode: \u4e2d\u6587";
139
+
140
+ await atomicWriteFile(filePath, content, "utf-8");
141
+
142
+ const result = await readFile(filePath, "utf-8");
143
+ expect(result).toBe(content);
144
+ });
145
+
146
+ it("throws AtomicWriteError when directory does not exist", async () => {
147
+ const filePath = join(tempDir, "nonexistent", "test.txt");
148
+
149
+ await expect(atomicWriteFile(filePath, "content")).rejects.toThrow(
150
+ AtomicWriteError
151
+ );
152
+ });
153
+
154
+ it("preserves original file on write failure", async () => {
155
+ const filePath = join(tempDir, "test.txt");
156
+ const originalContent = "original content";
157
+ await writeFile(filePath, originalContent, "utf-8");
158
+
159
+ // Try to write to a location that will fail
160
+ const badPath = join(tempDir, "nonexistent", "test.txt");
161
+ await expect(atomicWriteFile(badPath, "new content")).rejects.toThrow();
162
+
163
+ // Original file should be unchanged
164
+ const result = await readFile(filePath, "utf-8");
165
+ expect(result).toBe(originalContent);
166
+ });
167
+
168
+ it("cleans up temp file on failure when directory does not exist", async () => {
169
+ // Try to write to a path where the directory doesn't exist
170
+ // This will fail at the writeFile stage, and we verify no temp files are left
171
+ const badPath = join(tempDir, "nonexistent", "subdir", "test.txt");
172
+
173
+ await expect(atomicWriteFile(badPath, "content")).rejects.toThrow(
174
+ AtomicWriteError
175
+ );
176
+
177
+ // The temp file would be in the nonexistent directory, so there's nothing to clean up
178
+ // This tests that the error is properly thrown and the function handles the case
179
+ // where the temp file couldn't even be created
180
+ const { readdir } = await import("node:fs/promises");
181
+ const files = await readdir(tempDir);
182
+ // Should be empty - no orphaned temp files in the parent
183
+ expect(files.filter((f) => f.includes(".tmp."))).toHaveLength(0);
184
+ });
185
+
186
+ it("handles large file content", async () => {
187
+ const filePath = join(tempDir, "large.txt");
188
+ // Create 1MB of content
189
+ const content = "x".repeat(1024 * 1024);
190
+
191
+ await atomicWriteFile(filePath, content);
192
+
193
+ const result = await readFile(filePath, "utf-8");
194
+ expect(result.length).toBe(content.length);
195
+ });
196
+
197
+ it("handles empty content", async () => {
198
+ const filePath = join(tempDir, "empty.txt");
199
+
200
+ await atomicWriteFile(filePath, "");
201
+
202
+ const result = await readFile(filePath, "utf-8");
203
+ expect(result).toBe("");
204
+ });
205
+
206
+ it("handles content with newlines", async () => {
207
+ const filePath = join(tempDir, "multiline.txt");
208
+ const content = "line1\nline2\nline3\n";
209
+
210
+ await atomicWriteFile(filePath, content);
211
+
212
+ const result = await readFile(filePath, "utf-8");
213
+ expect(result).toBe(content);
214
+ });
215
+ });
216
+
217
+ describe("atomicWriteYaml", () => {
218
+ let tempDir: string;
219
+
220
+ beforeEach(async () => {
221
+ tempDir = await createTempDir();
222
+ });
223
+
224
+ afterEach(async () => {
225
+ await rm(tempDir, { recursive: true, force: true });
226
+ });
227
+
228
+ it("writes object as YAML", async () => {
229
+ const filePath = join(tempDir, "config.yaml");
230
+ const data = {
231
+ version: 1,
232
+ fleet: { name: "test-fleet" },
233
+ };
234
+
235
+ await atomicWriteYaml(filePath, data);
236
+
237
+ const content = await readFile(filePath, "utf-8");
238
+ const parsed = parseYaml(content);
239
+ expect(parsed).toEqual(data);
240
+ });
241
+
242
+ it("writes array as YAML", async () => {
243
+ const filePath = join(tempDir, "list.yaml");
244
+ const data = ["item1", "item2", "item3"];
245
+
246
+ await atomicWriteYaml(filePath, data);
247
+
248
+ const content = await readFile(filePath, "utf-8");
249
+ const parsed = parseYaml(content);
250
+ expect(parsed).toEqual(data);
251
+ });
252
+
253
+ it("respects custom indent option", async () => {
254
+ const filePath = join(tempDir, "config.yaml");
255
+ const data = { nested: { value: 1 } };
256
+
257
+ await atomicWriteYaml(filePath, data, { indent: 4 });
258
+
259
+ const content = await readFile(filePath, "utf-8");
260
+ // With indent 4, nested keys should be indented by 4 spaces
261
+ expect(content).toContain(" value:");
262
+ });
263
+
264
+ it("handles complex nested structures", async () => {
265
+ const filePath = join(tempDir, "complex.yaml");
266
+ const data = {
267
+ version: 1,
268
+ agents: [
269
+ {
270
+ name: "agent1",
271
+ config: {
272
+ model: "claude-sonnet",
273
+ permissions: ["read", "write"],
274
+ },
275
+ },
276
+ {
277
+ name: "agent2",
278
+ config: {
279
+ model: "claude-opus",
280
+ permissions: ["read"],
281
+ },
282
+ },
283
+ ],
284
+ metadata: {
285
+ created: "2024-01-01",
286
+ tags: ["production", "test"],
287
+ },
288
+ };
289
+
290
+ await atomicWriteYaml(filePath, data);
291
+
292
+ const content = await readFile(filePath, "utf-8");
293
+ const parsed = parseYaml(content);
294
+ expect(parsed).toEqual(data);
295
+ });
296
+
297
+ it("handles null and undefined values", async () => {
298
+ const filePath = join(tempDir, "nullable.yaml");
299
+ const data = {
300
+ present: "value",
301
+ absent: null,
302
+ };
303
+
304
+ await atomicWriteYaml(filePath, data);
305
+
306
+ const content = await readFile(filePath, "utf-8");
307
+ const parsed = parseYaml(content);
308
+ expect(parsed.present).toBe("value");
309
+ expect(parsed.absent).toBeNull();
310
+ });
311
+
312
+ it("throws AtomicWriteError on failure", async () => {
313
+ const filePath = join(tempDir, "nonexistent", "config.yaml");
314
+
315
+ await expect(atomicWriteYaml(filePath, { key: "value" })).rejects.toThrow(
316
+ AtomicWriteError
317
+ );
318
+ });
319
+ });
320
+
321
+ describe("atomicWriteJson", () => {
322
+ let tempDir: string;
323
+
324
+ beforeEach(async () => {
325
+ tempDir = await createTempDir();
326
+ });
327
+
328
+ afterEach(async () => {
329
+ await rm(tempDir, { recursive: true, force: true });
330
+ });
331
+
332
+ it("writes object as JSON with trailing newline", async () => {
333
+ const filePath = join(tempDir, "data.json");
334
+ const data = { key: "value", number: 42 };
335
+
336
+ await atomicWriteJson(filePath, data);
337
+
338
+ const content = await readFile(filePath, "utf-8");
339
+ expect(content.endsWith("\n")).toBe(true);
340
+ const parsed = JSON.parse(content);
341
+ expect(parsed).toEqual(data);
342
+ });
343
+
344
+ it("uses default indentation of 2 spaces", async () => {
345
+ const filePath = join(tempDir, "data.json");
346
+ const data = { nested: { value: 1 } };
347
+
348
+ await atomicWriteJson(filePath, data);
349
+
350
+ const content = await readFile(filePath, "utf-8");
351
+ expect(content).toContain(' "nested":');
352
+ expect(content).toContain(' "value":');
353
+ });
354
+
355
+ it("respects custom indent option", async () => {
356
+ const filePath = join(tempDir, "data.json");
357
+ const data = { nested: { value: 1 } };
358
+
359
+ await atomicWriteJson(filePath, data, { indent: 4 });
360
+
361
+ const content = await readFile(filePath, "utf-8");
362
+ expect(content).toContain(' "nested":');
363
+ expect(content).toContain(' "value":');
364
+ });
365
+
366
+ it("handles arrays", async () => {
367
+ const filePath = join(tempDir, "array.json");
368
+ const data = [1, 2, 3, { key: "value" }];
369
+
370
+ await atomicWriteJson(filePath, data);
371
+
372
+ const content = await readFile(filePath, "utf-8");
373
+ const parsed = JSON.parse(content);
374
+ expect(parsed).toEqual(data);
375
+ });
376
+
377
+ it("throws AtomicWriteError on failure", async () => {
378
+ const filePath = join(tempDir, "nonexistent", "data.json");
379
+
380
+ await expect(atomicWriteJson(filePath, { key: "value" })).rejects.toThrow(
381
+ AtomicWriteError
382
+ );
383
+ });
384
+ });
385
+
386
+ describe("appendJsonl", () => {
387
+ let tempDir: string;
388
+
389
+ beforeEach(async () => {
390
+ tempDir = await createTempDir();
391
+ });
392
+
393
+ afterEach(async () => {
394
+ await rm(tempDir, { recursive: true, force: true });
395
+ });
396
+
397
+ it("appends single JSON object as line", async () => {
398
+ const filePath = join(tempDir, "log.jsonl");
399
+ const data = { event: "test", timestamp: "2024-01-01" };
400
+
401
+ await appendJsonl(filePath, data);
402
+
403
+ const content = await readFile(filePath, "utf-8");
404
+ expect(content).toBe('{"event":"test","timestamp":"2024-01-01"}\n');
405
+ });
406
+
407
+ it("appends multiple objects as separate lines", async () => {
408
+ const filePath = join(tempDir, "log.jsonl");
409
+
410
+ await appendJsonl(filePath, { id: 1 });
411
+ await appendJsonl(filePath, { id: 2 });
412
+ await appendJsonl(filePath, { id: 3 });
413
+
414
+ const content = await readFile(filePath, "utf-8");
415
+ const lines = content.trim().split("\n");
416
+ expect(lines).toHaveLength(3);
417
+ expect(JSON.parse(lines[0])).toEqual({ id: 1 });
418
+ expect(JSON.parse(lines[1])).toEqual({ id: 2 });
419
+ expect(JSON.parse(lines[2])).toEqual({ id: 3 });
420
+ });
421
+
422
+ it("creates file if it does not exist", async () => {
423
+ const filePath = join(tempDir, "new.jsonl");
424
+
425
+ await appendJsonl(filePath, { event: "created" });
426
+
427
+ const exists = await stat(filePath)
428
+ .then(() => true)
429
+ .catch(() => false);
430
+ expect(exists).toBe(true);
431
+ });
432
+
433
+ it("appends to existing file", async () => {
434
+ const filePath = join(tempDir, "existing.jsonl");
435
+ await writeFile(filePath, '{"id":1}\n', "utf-8");
436
+
437
+ await appendJsonl(filePath, { id: 2 });
438
+
439
+ const content = await readFile(filePath, "utf-8");
440
+ const lines = content.trim().split("\n");
441
+ expect(lines).toHaveLength(2);
442
+ expect(JSON.parse(lines[0])).toEqual({ id: 1 });
443
+ expect(JSON.parse(lines[1])).toEqual({ id: 2 });
444
+ });
445
+
446
+ it("handles complex objects", async () => {
447
+ const filePath = join(tempDir, "complex.jsonl");
448
+ const data = {
449
+ type: "tool_result",
450
+ content: { output: "Hello\nWorld", exitCode: 0 },
451
+ metadata: { tags: ["test", "prod"] },
452
+ };
453
+
454
+ await appendJsonl(filePath, data);
455
+
456
+ const content = await readFile(filePath, "utf-8");
457
+ const parsed = JSON.parse(content.trim());
458
+ expect(parsed).toEqual(data);
459
+ });
460
+
461
+ it("throws AtomicWriteError when directory does not exist", async () => {
462
+ const filePath = join(tempDir, "nonexistent", "log.jsonl");
463
+
464
+ await expect(appendJsonl(filePath, { event: "test" })).rejects.toThrow(
465
+ AtomicWriteError
466
+ );
467
+ });
468
+
469
+ it("handles arrays as JSON lines", async () => {
470
+ const filePath = join(tempDir, "arrays.jsonl");
471
+
472
+ await appendJsonl(filePath, [1, 2, 3]);
473
+ await appendJsonl(filePath, ["a", "b"]);
474
+
475
+ const content = await readFile(filePath, "utf-8");
476
+ const lines = content.trim().split("\n");
477
+ expect(JSON.parse(lines[0])).toEqual([1, 2, 3]);
478
+ expect(JSON.parse(lines[1])).toEqual(["a", "b"]);
479
+ });
480
+
481
+ it("handles primitive values", async () => {
482
+ const filePath = join(tempDir, "primitives.jsonl");
483
+
484
+ await appendJsonl(filePath, "string");
485
+ await appendJsonl(filePath, 42);
486
+ await appendJsonl(filePath, true);
487
+ await appendJsonl(filePath, null);
488
+
489
+ const content = await readFile(filePath, "utf-8");
490
+ const lines = content.trim().split("\n");
491
+ expect(JSON.parse(lines[0])).toBe("string");
492
+ expect(JSON.parse(lines[1])).toBe(42);
493
+ expect(JSON.parse(lines[2])).toBe(true);
494
+ expect(JSON.parse(lines[3])).toBeNull();
495
+ });
496
+ });
497
+
498
+ describe("renameWithRetry", () => {
499
+ let tempDir: string;
500
+
501
+ beforeEach(async () => {
502
+ tempDir = await createTempDir();
503
+ });
504
+
505
+ afterEach(async () => {
506
+ await rm(tempDir, { recursive: true, force: true });
507
+ });
508
+
509
+ it("succeeds on first try when rename succeeds", async () => {
510
+ const oldPath = join(tempDir, "old.txt");
511
+ const newPath = join(tempDir, "new.txt");
512
+ await writeFile(oldPath, "content", "utf-8");
513
+
514
+ await renameWithRetry(oldPath, newPath);
515
+
516
+ const content = await readFile(newPath, "utf-8");
517
+ expect(content).toBe("content");
518
+ });
519
+
520
+ it("retries on EACCES error", async () => {
521
+ const oldPath = join(tempDir, "old.txt");
522
+ const newPath = join(tempDir, "new.txt");
523
+
524
+ let attempts = 0;
525
+ const mockRename = async () => {
526
+ attempts++;
527
+ if (attempts < 3) {
528
+ const err = new Error("Access denied") as NodeJS.ErrnoException;
529
+ err.code = "EACCES";
530
+ throw err;
531
+ }
532
+ // Success on third attempt
533
+ };
534
+
535
+ await renameWithRetry(oldPath, newPath, {
536
+ renameFn: mockRename,
537
+ baseDelayMs: 1, // Fast delays for testing
538
+ });
539
+
540
+ expect(attempts).toBe(3);
541
+ });
542
+
543
+ it("retries on EPERM error", async () => {
544
+ const oldPath = join(tempDir, "old.txt");
545
+ const newPath = join(tempDir, "new.txt");
546
+
547
+ let attempts = 0;
548
+ const mockRename = async () => {
549
+ attempts++;
550
+ if (attempts < 2) {
551
+ const err = new Error("Permission denied") as NodeJS.ErrnoException;
552
+ err.code = "EPERM";
553
+ throw err;
554
+ }
555
+ };
556
+
557
+ await renameWithRetry(oldPath, newPath, {
558
+ renameFn: mockRename,
559
+ baseDelayMs: 1,
560
+ });
561
+
562
+ expect(attempts).toBe(2);
563
+ });
564
+
565
+ it("throws immediately on non-retryable error", async () => {
566
+ const oldPath = join(tempDir, "old.txt");
567
+ const newPath = join(tempDir, "new.txt");
568
+
569
+ let attempts = 0;
570
+ const mockRename = async () => {
571
+ attempts++;
572
+ const err = new Error("No such file") as NodeJS.ErrnoException;
573
+ err.code = "ENOENT";
574
+ throw err;
575
+ };
576
+
577
+ await expect(
578
+ renameWithRetry(oldPath, newPath, { renameFn: mockRename })
579
+ ).rejects.toThrow("No such file");
580
+
581
+ expect(attempts).toBe(1);
582
+ });
583
+
584
+ it("throws after max retries exhausted", async () => {
585
+ const oldPath = join(tempDir, "old.txt");
586
+ const newPath = join(tempDir, "new.txt");
587
+
588
+ let attempts = 0;
589
+ const mockRename = async () => {
590
+ attempts++;
591
+ const err = new Error("Access denied") as NodeJS.ErrnoException;
592
+ err.code = "EACCES";
593
+ throw err;
594
+ };
595
+
596
+ await expect(
597
+ renameWithRetry(oldPath, newPath, {
598
+ renameFn: mockRename,
599
+ maxRetries: 2,
600
+ baseDelayMs: 1,
601
+ })
602
+ ).rejects.toThrow("Access denied");
603
+
604
+ // Initial attempt + 2 retries = 3 total attempts
605
+ expect(attempts).toBe(3);
606
+ });
607
+
608
+ it("uses exponential backoff for delays", async () => {
609
+ const oldPath = join(tempDir, "old.txt");
610
+ const newPath = join(tempDir, "new.txt");
611
+
612
+ const delays: number[] = [];
613
+ let lastTime = Date.now();
614
+ let attempts = 0;
615
+
616
+ const mockRename = async () => {
617
+ const now = Date.now();
618
+ if (attempts > 0) {
619
+ delays.push(now - lastTime);
620
+ }
621
+ lastTime = now;
622
+ attempts++;
623
+
624
+ if (attempts <= 3) {
625
+ const err = new Error("Access denied") as NodeJS.ErrnoException;
626
+ err.code = "EACCES";
627
+ throw err;
628
+ }
629
+ };
630
+
631
+ await renameWithRetry(oldPath, newPath, {
632
+ renameFn: mockRename,
633
+ maxRetries: 3,
634
+ baseDelayMs: 10,
635
+ });
636
+
637
+ // With baseDelayMs=10: delays should be ~10, ~20, ~40
638
+ // Allow some tolerance for timing
639
+ expect(delays[0]).toBeGreaterThanOrEqual(8);
640
+ expect(delays[1]).toBeGreaterThanOrEqual(15);
641
+ expect(delays[2]).toBeGreaterThanOrEqual(30);
642
+ });
643
+
644
+ it("respects custom maxRetries", async () => {
645
+ const oldPath = join(tempDir, "old.txt");
646
+ const newPath = join(tempDir, "new.txt");
647
+
648
+ let attempts = 0;
649
+ const mockRename = async () => {
650
+ attempts++;
651
+ const err = new Error("Access denied") as NodeJS.ErrnoException;
652
+ err.code = "EACCES";
653
+ throw err;
654
+ };
655
+
656
+ await expect(
657
+ renameWithRetry(oldPath, newPath, {
658
+ renameFn: mockRename,
659
+ maxRetries: 5,
660
+ baseDelayMs: 1,
661
+ })
662
+ ).rejects.toThrow();
663
+
664
+ // Initial attempt + 5 retries = 6 total attempts
665
+ expect(attempts).toBe(6);
666
+ });
667
+ });
668
+
669
+ describe("concurrent write safety", () => {
670
+ let tempDir: string;
671
+
672
+ beforeEach(async () => {
673
+ tempDir = await createTempDir();
674
+ });
675
+
676
+ afterEach(async () => {
677
+ await rm(tempDir, { recursive: true, force: true });
678
+ });
679
+
680
+ it("handles concurrent atomic writes to same file (last write wins)", async () => {
681
+ const filePath = join(tempDir, "concurrent.yaml");
682
+
683
+ // Start multiple writes concurrently
684
+ const writes = [];
685
+ for (let i = 0; i < 10; i++) {
686
+ writes.push(atomicWriteYaml(filePath, { version: i }));
687
+ }
688
+
689
+ // Wait for all writes to complete
690
+ await Promise.all(writes);
691
+
692
+ // File should exist and be valid YAML (one of the versions)
693
+ const content = await readFile(filePath, "utf-8");
694
+ const parsed = parseYaml(content);
695
+ expect(typeof parsed.version).toBe("number");
696
+ expect(parsed.version).toBeGreaterThanOrEqual(0);
697
+ expect(parsed.version).toBeLessThan(10);
698
+ });
699
+
700
+ it("handles concurrent JSONL appends", async () => {
701
+ const filePath = join(tempDir, "concurrent.jsonl");
702
+
703
+ // Start multiple appends concurrently
704
+ const appends = [];
705
+ for (let i = 0; i < 100; i++) {
706
+ appends.push(appendJsonl(filePath, { id: i }));
707
+ }
708
+
709
+ // Wait for all appends to complete
710
+ await Promise.all(appends);
711
+
712
+ // All entries should be valid JSON lines
713
+ const content = await readFile(filePath, "utf-8");
714
+ const lines = content.trim().split("\n");
715
+
716
+ // Should have all 100 entries
717
+ expect(lines).toHaveLength(100);
718
+
719
+ // All lines should be valid JSON
720
+ const ids = lines.map((line) => JSON.parse(line).id).sort((a, b) => a - b);
721
+ expect(ids).toEqual(Array.from({ length: 100 }, (_, i) => i));
722
+ });
723
+ });