@herdctl/core 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (284) hide show
  1. package/dist/config/__tests__/agent.test.js +61 -13
  2. package/dist/config/__tests__/agent.test.js.map +1 -1
  3. package/dist/config/__tests__/merge.test.js +10 -3
  4. package/dist/config/__tests__/merge.test.js.map +1 -1
  5. package/dist/config/__tests__/schema.test.js +350 -1
  6. package/dist/config/__tests__/schema.test.js.map +1 -1
  7. package/dist/config/index.d.ts +1 -1
  8. package/dist/config/index.d.ts.map +1 -1
  9. package/dist/config/index.js +3 -1
  10. package/dist/config/index.js.map +1 -1
  11. package/dist/config/schema.d.ts +841 -27
  12. package/dist/config/schema.d.ts.map +1 -1
  13. package/dist/config/schema.js +129 -10
  14. package/dist/config/schema.js.map +1 -1
  15. package/dist/fleet-manager/__tests__/coverage.test.js +14 -331
  16. package/dist/fleet-manager/__tests__/coverage.test.js.map +1 -1
  17. package/dist/fleet-manager/__tests__/errors.test.js +1 -49
  18. package/dist/fleet-manager/__tests__/errors.test.js.map +1 -1
  19. package/dist/fleet-manager/__tests__/integration.test.js +114 -0
  20. package/dist/fleet-manager/__tests__/integration.test.js.map +1 -1
  21. package/dist/fleet-manager/__tests__/job-control.test.js +13 -14
  22. package/dist/fleet-manager/__tests__/job-control.test.js.map +1 -1
  23. package/dist/fleet-manager/__tests__/reload.test.js +12 -2
  24. package/dist/fleet-manager/__tests__/reload.test.js.map +1 -1
  25. package/dist/fleet-manager/__tests__/status-queries.test.js +6 -0
  26. package/dist/fleet-manager/__tests__/status-queries.test.js.map +1 -1
  27. package/dist/fleet-manager/__tests__/trigger.test.js +10 -2
  28. package/dist/fleet-manager/__tests__/trigger.test.js.map +1 -1
  29. package/dist/fleet-manager/config-reload.d.ts +164 -0
  30. package/dist/fleet-manager/config-reload.d.ts.map +1 -0
  31. package/dist/fleet-manager/config-reload.js +445 -0
  32. package/dist/fleet-manager/config-reload.js.map +1 -0
  33. package/dist/fleet-manager/context.d.ts +76 -0
  34. package/dist/fleet-manager/context.d.ts.map +1 -0
  35. package/dist/fleet-manager/context.js +11 -0
  36. package/dist/fleet-manager/context.js.map +1 -0
  37. package/dist/fleet-manager/errors.d.ts +0 -25
  38. package/dist/fleet-manager/errors.d.ts.map +1 -1
  39. package/dist/fleet-manager/errors.js +0 -38
  40. package/dist/fleet-manager/errors.js.map +1 -1
  41. package/dist/fleet-manager/event-emitters.d.ts +123 -0
  42. package/dist/fleet-manager/event-emitters.d.ts.map +1 -0
  43. package/dist/fleet-manager/event-emitters.js +136 -0
  44. package/dist/fleet-manager/event-emitters.js.map +1 -0
  45. package/dist/fleet-manager/event-types.d.ts +0 -15
  46. package/dist/fleet-manager/event-types.d.ts.map +1 -1
  47. package/dist/fleet-manager/fleet-manager.d.ts +40 -653
  48. package/dist/fleet-manager/fleet-manager.d.ts.map +1 -1
  49. package/dist/fleet-manager/fleet-manager.js +95 -1720
  50. package/dist/fleet-manager/fleet-manager.js.map +1 -1
  51. package/dist/fleet-manager/index.d.ts +13 -2
  52. package/dist/fleet-manager/index.d.ts.map +1 -1
  53. package/dist/fleet-manager/index.js +19 -6
  54. package/dist/fleet-manager/index.js.map +1 -1
  55. package/dist/fleet-manager/job-control.d.ts +67 -0
  56. package/dist/fleet-manager/job-control.d.ts.map +1 -0
  57. package/dist/fleet-manager/job-control.js +333 -0
  58. package/dist/fleet-manager/job-control.js.map +1 -0
  59. package/dist/fleet-manager/log-streaming.d.ts +171 -0
  60. package/dist/fleet-manager/log-streaming.d.ts.map +1 -0
  61. package/dist/fleet-manager/log-streaming.js +503 -0
  62. package/dist/fleet-manager/log-streaming.js.map +1 -0
  63. package/dist/fleet-manager/schedule-executor.d.ts +63 -0
  64. package/dist/fleet-manager/schedule-executor.d.ts.map +1 -0
  65. package/dist/fleet-manager/schedule-executor.js +209 -0
  66. package/dist/fleet-manager/schedule-executor.js.map +1 -0
  67. package/dist/fleet-manager/schedule-management.d.ts +71 -0
  68. package/dist/fleet-manager/schedule-management.d.ts.map +1 -0
  69. package/dist/fleet-manager/schedule-management.js +171 -0
  70. package/dist/fleet-manager/schedule-management.js.map +1 -0
  71. package/dist/fleet-manager/status-queries.d.ts +105 -0
  72. package/dist/fleet-manager/status-queries.d.ts.map +1 -0
  73. package/dist/fleet-manager/status-queries.js +247 -0
  74. package/dist/fleet-manager/status-queries.js.map +1 -0
  75. package/dist/fleet-manager/types.d.ts +0 -39
  76. package/dist/fleet-manager/types.d.ts.map +1 -1
  77. package/dist/runner/__tests__/job-executor.test.js +206 -1
  78. package/dist/runner/__tests__/job-executor.test.js.map +1 -1
  79. package/dist/runner/job-executor.d.ts +9 -0
  80. package/dist/runner/job-executor.d.ts.map +1 -1
  81. package/dist/runner/job-executor.js +78 -4
  82. package/dist/runner/job-executor.js.map +1 -1
  83. package/dist/runner/message-processor.d.ts.map +1 -1
  84. package/dist/runner/message-processor.js +53 -0
  85. package/dist/runner/message-processor.js.map +1 -1
  86. package/dist/runner/types.d.ts +3 -1
  87. package/dist/runner/types.d.ts.map +1 -1
  88. package/dist/scheduler/__tests__/cron.test.d.ts +2 -0
  89. package/dist/scheduler/__tests__/cron.test.d.ts.map +1 -0
  90. package/dist/scheduler/__tests__/cron.test.js +867 -0
  91. package/dist/scheduler/__tests__/cron.test.js.map +1 -0
  92. package/dist/scheduler/__tests__/scheduler.test.js +164 -5
  93. package/dist/scheduler/__tests__/scheduler.test.js.map +1 -1
  94. package/dist/scheduler/cron.d.ts +126 -0
  95. package/dist/scheduler/cron.d.ts.map +1 -0
  96. package/dist/scheduler/cron.js +390 -0
  97. package/dist/scheduler/cron.js.map +1 -0
  98. package/dist/scheduler/errors.d.ts +81 -1
  99. package/dist/scheduler/errors.d.ts.map +1 -1
  100. package/dist/scheduler/errors.js +81 -6
  101. package/dist/scheduler/errors.js.map +1 -1
  102. package/dist/scheduler/index.d.ts +1 -0
  103. package/dist/scheduler/index.d.ts.map +1 -1
  104. package/dist/scheduler/index.js +2 -0
  105. package/dist/scheduler/index.js.map +1 -1
  106. package/dist/scheduler/schedule-runner.d.ts +2 -2
  107. package/dist/scheduler/schedule-runner.d.ts.map +1 -1
  108. package/dist/scheduler/schedule-runner.js +20 -8
  109. package/dist/scheduler/schedule-runner.js.map +1 -1
  110. package/dist/scheduler/scheduler.d.ts +4 -4
  111. package/dist/scheduler/scheduler.d.ts.map +1 -1
  112. package/dist/scheduler/scheduler.js +95 -20
  113. package/dist/scheduler/scheduler.js.map +1 -1
  114. package/dist/scheduler/types.d.ts +1 -1
  115. package/dist/scheduler/types.d.ts.map +1 -1
  116. package/dist/state/schemas/job-metadata.d.ts +2 -2
  117. package/package.json +33 -8
  118. package/.turbo/turbo-build.log +0 -4
  119. package/.turbo/turbo-test.log +0 -219
  120. package/.turbo/turbo-typecheck.log +0 -4
  121. package/coverage/base.css +0 -224
  122. package/coverage/block-navigation.js +0 -87
  123. package/coverage/coverage-final.json +0 -51
  124. package/coverage/favicon.png +0 -0
  125. package/coverage/index.html +0 -251
  126. package/coverage/prettify.css +0 -1
  127. package/coverage/prettify.js +0 -2
  128. package/coverage/sort-arrow-sprite.png +0 -0
  129. package/coverage/sorter.js +0 -210
  130. package/coverage/src/config/index.html +0 -191
  131. package/coverage/src/config/index.ts.html +0 -442
  132. package/coverage/src/config/interpolate.ts.html +0 -652
  133. package/coverage/src/config/loader.ts.html +0 -1501
  134. package/coverage/src/config/merge.ts.html +0 -823
  135. package/coverage/src/config/parser.ts.html +0 -1213
  136. package/coverage/src/config/schema.ts.html +0 -1123
  137. package/coverage/src/fleet-manager/errors.ts.html +0 -2326
  138. package/coverage/src/fleet-manager/event-types.ts.html +0 -1219
  139. package/coverage/src/fleet-manager/fleet-manager.ts.html +0 -7030
  140. package/coverage/src/fleet-manager/index.html +0 -206
  141. package/coverage/src/fleet-manager/index.ts.html +0 -469
  142. package/coverage/src/fleet-manager/job-manager.ts.html +0 -2074
  143. package/coverage/src/fleet-manager/job-queue.ts.html +0 -2479
  144. package/coverage/src/fleet-manager/types.ts.html +0 -2602
  145. package/coverage/src/index.html +0 -116
  146. package/coverage/src/index.ts.html +0 -181
  147. package/coverage/src/runner/errors.ts.html +0 -1006
  148. package/coverage/src/runner/index.html +0 -191
  149. package/coverage/src/runner/index.ts.html +0 -256
  150. package/coverage/src/runner/job-executor.ts.html +0 -1429
  151. package/coverage/src/runner/message-processor.ts.html +0 -1150
  152. package/coverage/src/runner/sdk-adapter.ts.html +0 -658
  153. package/coverage/src/runner/types.ts.html +0 -559
  154. package/coverage/src/scheduler/errors.ts.html +0 -388
  155. package/coverage/src/scheduler/index.html +0 -206
  156. package/coverage/src/scheduler/index.ts.html +0 -244
  157. package/coverage/src/scheduler/interval.ts.html +0 -652
  158. package/coverage/src/scheduler/schedule-runner.ts.html +0 -1411
  159. package/coverage/src/scheduler/schedule-state.ts.html +0 -718
  160. package/coverage/src/scheduler/scheduler.ts.html +0 -1795
  161. package/coverage/src/scheduler/types.ts.html +0 -733
  162. package/coverage/src/state/directory.ts.html +0 -736
  163. package/coverage/src/state/errors.ts.html +0 -376
  164. package/coverage/src/state/fleet-state.ts.html +0 -937
  165. package/coverage/src/state/index.html +0 -221
  166. package/coverage/src/state/index.ts.html +0 -322
  167. package/coverage/src/state/job-metadata.ts.html +0 -1420
  168. package/coverage/src/state/job-output.ts.html +0 -1033
  169. package/coverage/src/state/schemas/fleet-state.ts.html +0 -445
  170. package/coverage/src/state/schemas/index.html +0 -176
  171. package/coverage/src/state/schemas/index.ts.html +0 -286
  172. package/coverage/src/state/schemas/job-metadata.ts.html +0 -628
  173. package/coverage/src/state/schemas/job-output.ts.html +0 -616
  174. package/coverage/src/state/schemas/session-info.ts.html +0 -361
  175. package/coverage/src/state/session.ts.html +0 -844
  176. package/coverage/src/state/types.ts.html +0 -262
  177. package/coverage/src/state/utils/atomic.ts.html +0 -748
  178. package/coverage/src/state/utils/index.html +0 -146
  179. package/coverage/src/state/utils/index.ts.html +0 -103
  180. package/coverage/src/state/utils/reads.ts.html +0 -1621
  181. package/coverage/src/work-sources/adapters/github.ts.html +0 -3583
  182. package/coverage/src/work-sources/adapters/index.html +0 -131
  183. package/coverage/src/work-sources/adapters/index.ts.html +0 -277
  184. package/coverage/src/work-sources/errors.ts.html +0 -298
  185. package/coverage/src/work-sources/index.html +0 -176
  186. package/coverage/src/work-sources/index.ts.html +0 -529
  187. package/coverage/src/work-sources/manager.ts.html +0 -1324
  188. package/coverage/src/work-sources/registry.ts.html +0 -619
  189. package/coverage/src/work-sources/types.ts.html +0 -568
  190. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts +0 -7
  191. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts.map +0 -1
  192. package/dist/fleet-manager/__tests__/event-helpers.test.js +0 -368
  193. package/dist/fleet-manager/__tests__/event-helpers.test.js.map +0 -1
  194. package/src/config/__tests__/agent.test.ts +0 -864
  195. package/src/config/__tests__/interpolate.test.ts +0 -644
  196. package/src/config/__tests__/loader.test.ts +0 -784
  197. package/src/config/__tests__/merge.test.ts +0 -751
  198. package/src/config/__tests__/parser.test.ts +0 -533
  199. package/src/config/__tests__/schema.test.ts +0 -873
  200. package/src/config/index.ts +0 -119
  201. package/src/config/interpolate.ts +0 -189
  202. package/src/config/loader.ts +0 -472
  203. package/src/config/merge.ts +0 -246
  204. package/src/config/parser.ts +0 -376
  205. package/src/config/schema.ts +0 -346
  206. package/src/fleet-manager/__tests__/coverage.test.ts +0 -2869
  207. package/src/fleet-manager/__tests__/errors.test.ts +0 -660
  208. package/src/fleet-manager/__tests__/event-helpers.test.ts +0 -448
  209. package/src/fleet-manager/__tests__/integration.test.ts +0 -1209
  210. package/src/fleet-manager/__tests__/job-control.test.ts +0 -283
  211. package/src/fleet-manager/__tests__/job-manager.test.ts +0 -869
  212. package/src/fleet-manager/__tests__/job-queue.test.ts +0 -401
  213. package/src/fleet-manager/__tests__/reload.test.ts +0 -751
  214. package/src/fleet-manager/__tests__/status-queries.test.ts +0 -595
  215. package/src/fleet-manager/__tests__/trigger.test.ts +0 -601
  216. package/src/fleet-manager/errors.ts +0 -747
  217. package/src/fleet-manager/event-types.ts +0 -378
  218. package/src/fleet-manager/fleet-manager.ts +0 -2315
  219. package/src/fleet-manager/index.ts +0 -128
  220. package/src/fleet-manager/job-manager.ts +0 -663
  221. package/src/fleet-manager/job-queue.ts +0 -798
  222. package/src/fleet-manager/types.ts +0 -839
  223. package/src/index.ts +0 -32
  224. package/src/runner/__tests__/errors.test.ts +0 -382
  225. package/src/runner/__tests__/job-executor.test.ts +0 -1708
  226. package/src/runner/__tests__/message-processor.test.ts +0 -960
  227. package/src/runner/__tests__/sdk-adapter.test.ts +0 -626
  228. package/src/runner/errors.ts +0 -307
  229. package/src/runner/index.ts +0 -57
  230. package/src/runner/job-executor.ts +0 -448
  231. package/src/runner/message-processor.ts +0 -355
  232. package/src/runner/sdk-adapter.ts +0 -191
  233. package/src/runner/types.ts +0 -158
  234. package/src/scheduler/__tests__/errors.test.ts +0 -159
  235. package/src/scheduler/__tests__/interval.test.ts +0 -515
  236. package/src/scheduler/__tests__/schedule-runner.test.ts +0 -798
  237. package/src/scheduler/__tests__/schedule-state.test.ts +0 -671
  238. package/src/scheduler/__tests__/scheduler.test.ts +0 -1280
  239. package/src/scheduler/errors.ts +0 -101
  240. package/src/scheduler/index.ts +0 -53
  241. package/src/scheduler/interval.ts +0 -189
  242. package/src/scheduler/schedule-runner.ts +0 -442
  243. package/src/scheduler/schedule-state.ts +0 -211
  244. package/src/scheduler/scheduler.ts +0 -570
  245. package/src/scheduler/types.ts +0 -216
  246. package/src/state/__tests__/directory.test.ts +0 -595
  247. package/src/state/__tests__/fleet-state.test.ts +0 -868
  248. package/src/state/__tests__/job-metadata-schema.test.ts +0 -414
  249. package/src/state/__tests__/job-metadata.test.ts +0 -831
  250. package/src/state/__tests__/job-output.test.ts +0 -856
  251. package/src/state/__tests__/session-schema.test.ts +0 -378
  252. package/src/state/__tests__/session.test.ts +0 -604
  253. package/src/state/directory.ts +0 -217
  254. package/src/state/errors.ts +0 -97
  255. package/src/state/fleet-state.ts +0 -284
  256. package/src/state/index.ts +0 -79
  257. package/src/state/job-metadata.ts +0 -445
  258. package/src/state/job-output.ts +0 -316
  259. package/src/state/schemas/__tests__/job-output.test.ts +0 -338
  260. package/src/state/schemas/fleet-state.ts +0 -120
  261. package/src/state/schemas/index.ts +0 -67
  262. package/src/state/schemas/job-metadata.ts +0 -181
  263. package/src/state/schemas/job-output.ts +0 -177
  264. package/src/state/schemas/session-info.ts +0 -92
  265. package/src/state/session.ts +0 -253
  266. package/src/state/types.ts +0 -59
  267. package/src/state/utils/__tests__/atomic.test.ts +0 -723
  268. package/src/state/utils/__tests__/reads.test.ts +0 -1071
  269. package/src/state/utils/atomic.ts +0 -221
  270. package/src/state/utils/index.ts +0 -6
  271. package/src/state/utils/reads.ts +0 -512
  272. package/src/work-sources/__tests__/github.test.ts +0 -1800
  273. package/src/work-sources/__tests__/manager.test.ts +0 -529
  274. package/src/work-sources/__tests__/registry.test.ts +0 -477
  275. package/src/work-sources/__tests__/types.test.ts +0 -479
  276. package/src/work-sources/adapters/github.ts +0 -1166
  277. package/src/work-sources/adapters/index.ts +0 -64
  278. package/src/work-sources/errors.ts +0 -71
  279. package/src/work-sources/index.ts +0 -148
  280. package/src/work-sources/manager.ts +0 -413
  281. package/src/work-sources/registry.ts +0 -178
  282. package/src/work-sources/types.ts +0 -161
  283. package/tsconfig.json +0 -9
  284. package/vitest.config.ts +0 -19
@@ -1,217 +0,0 @@
1
- /**
2
- * State directory management
3
- *
4
- * Provides functions to initialize and access the .herdctl/ state directory
5
- */
6
-
7
- import { mkdir, stat, access, constants } from "node:fs/promises";
8
- import { join, resolve } from "node:path";
9
- import {
10
- type StateDirectory,
11
- type InitStateDirectoryOptions,
12
- type StateDirectoryValidation,
13
- STATE_SUBDIRECTORIES,
14
- DEFAULT_STATE_DIR_NAME,
15
- STATE_FILE_NAME,
16
- } from "./types.js";
17
- import {
18
- StateDirectoryCreateError,
19
- StateDirectoryValidationError,
20
- StateFileError,
21
- getPermissionErrorMessage,
22
- } from "./errors.js";
23
- import { createInitialFleetState, FleetStateSchema } from "./schemas/index.js";
24
- import { atomicWriteYaml } from "./utils/index.js";
25
- import { safeReadYaml } from "./utils/reads.js";
26
-
27
- /**
28
- * Get the paths for all state directory components
29
- *
30
- * @param rootPath - Root path to the state directory (e.g., /path/to/.herdctl)
31
- * @returns StateDirectory object with paths to all subdirectories and files
32
- */
33
- export function getStateDirectory(rootPath?: string): StateDirectory {
34
- const root = rootPath
35
- ? resolve(rootPath)
36
- : resolve(process.cwd(), DEFAULT_STATE_DIR_NAME);
37
-
38
- return {
39
- root,
40
- jobs: join(root, "jobs"),
41
- sessions: join(root, "sessions"),
42
- logs: join(root, "logs"),
43
- stateFile: join(root, STATE_FILE_NAME),
44
- };
45
- }
46
-
47
- /**
48
- * Check if a path exists and is a directory
49
- */
50
- async function isDirectory(path: string): Promise<boolean> {
51
- try {
52
- const stats = await stat(path);
53
- return stats.isDirectory();
54
- } catch {
55
- return false;
56
- }
57
- }
58
-
59
- /**
60
- * Check if a path exists (file or directory)
61
- */
62
- async function pathExists(path: string): Promise<boolean> {
63
- try {
64
- await access(path, constants.F_OK);
65
- return true;
66
- } catch {
67
- return false;
68
- }
69
- }
70
-
71
- /**
72
- * Validate that the state directory structure exists and is accessible
73
- *
74
- * @param stateDir - StateDirectory object to validate
75
- * @returns Validation result with any missing paths or errors
76
- */
77
- export async function validateStateDirectory(
78
- stateDir: StateDirectory
79
- ): Promise<StateDirectoryValidation> {
80
- const missing: string[] = [];
81
- const errors: string[] = [];
82
-
83
- // Check root directory
84
- if (!(await isDirectory(stateDir.root))) {
85
- const exists = await pathExists(stateDir.root);
86
- if (exists) {
87
- errors.push(`'${stateDir.root}' exists but is not a directory`);
88
- } else {
89
- missing.push(stateDir.root);
90
- }
91
- }
92
-
93
- // Check subdirectories
94
- for (const subdir of STATE_SUBDIRECTORIES) {
95
- const subdirPath = stateDir[subdir];
96
- if (!(await isDirectory(subdirPath))) {
97
- const exists = await pathExists(subdirPath);
98
- if (exists) {
99
- errors.push(`'${subdirPath}' exists but is not a directory`);
100
- } else {
101
- missing.push(subdirPath);
102
- }
103
- }
104
- }
105
-
106
- // Check state file (should exist, can be file)
107
- if (!(await pathExists(stateDir.stateFile))) {
108
- missing.push(stateDir.stateFile);
109
- }
110
-
111
- return {
112
- valid: missing.length === 0 && errors.length === 0,
113
- missing,
114
- errors,
115
- };
116
- }
117
-
118
- /**
119
- * Create a single directory with descriptive error handling
120
- */
121
- async function createDirectory(path: string): Promise<void> {
122
- try {
123
- await mkdir(path, { recursive: true });
124
- } catch (error) {
125
- const code = (error as NodeJS.ErrnoException).code;
126
- throw new StateDirectoryCreateError(
127
- getPermissionErrorMessage(code, path),
128
- path,
129
- error as Error
130
- );
131
- }
132
- }
133
-
134
- /**
135
- * Initialize the state directory structure
136
- *
137
- * Creates the .herdctl/ directory and its subdirectories (jobs/, sessions/, logs/)
138
- * if they don't exist. Also creates an initial state.yaml file with empty fleet state.
139
- *
140
- * @param options - Options for initialization
141
- * @returns StateDirectory object with paths to all subdirectories
142
- * @throws {StateDirectoryCreateError} If directories cannot be created
143
- * @throws {StateFileError} If state.yaml cannot be created
144
- *
145
- * @example
146
- * ```typescript
147
- * // Initialize in current directory
148
- * const stateDir = await initStateDirectory();
149
- *
150
- * // Initialize in custom location
151
- * const stateDir = await initStateDirectory({ path: '/custom/path/.herdctl' });
152
- * ```
153
- */
154
- export async function initStateDirectory(
155
- options: InitStateDirectoryOptions = {}
156
- ): Promise<StateDirectory> {
157
- const stateDir = getStateDirectory(options.path);
158
-
159
- // Create root directory
160
- await createDirectory(stateDir.root);
161
-
162
- // Create subdirectories
163
- for (const subdir of STATE_SUBDIRECTORIES) {
164
- await createDirectory(stateDir[subdir]);
165
- }
166
-
167
- // Create state.yaml if it doesn't exist
168
- if (!(await pathExists(stateDir.stateFile))) {
169
- try {
170
- const initialState = createInitialFleetState();
171
- await atomicWriteYaml(stateDir.stateFile, initialState);
172
- } catch (error) {
173
- throw new StateFileError(
174
- `Failed to create initial state file: ${(error as Error).message}`,
175
- stateDir.stateFile,
176
- "write",
177
- error as Error
178
- );
179
- }
180
- } else {
181
- // Validate existing state.yaml is readable and valid
182
- const result = await safeReadYaml(stateDir.stateFile);
183
- if (!result.success) {
184
- throw new StateFileError(
185
- `Existing state file is corrupted or unreadable: ${result.error.message}`,
186
- stateDir.stateFile,
187
- "read",
188
- result.error
189
- );
190
- }
191
-
192
- // Validate against schema
193
- const parseResult = FleetStateSchema.safeParse(result.data);
194
- if (!parseResult.success) {
195
- const issues = parseResult.error.issues
196
- .map((i) => `${i.path.join(".")}: ${i.message}`)
197
- .join("; ");
198
- throw new StateFileError(
199
- `Existing state file has invalid schema: ${issues}`,
200
- stateDir.stateFile,
201
- "read"
202
- );
203
- }
204
- }
205
-
206
- // Final validation
207
- const validation = await validateStateDirectory(stateDir);
208
- if (!validation.valid) {
209
- const allIssues = [...validation.missing, ...validation.errors];
210
- throw new StateDirectoryValidationError(
211
- `State directory validation failed: ${allIssues.join(", ")}`,
212
- validation.missing
213
- );
214
- }
215
-
216
- return stateDir;
217
- }
@@ -1,97 +0,0 @@
1
- /**
2
- * Error classes for state management operations
3
- *
4
- * Provides typed errors with descriptive messages for state-related failures
5
- */
6
-
7
- /**
8
- * Base error class for state management errors
9
- */
10
- export class StateError extends Error {
11
- constructor(message: string) {
12
- super(message);
13
- this.name = "StateError";
14
- }
15
- }
16
-
17
- /**
18
- * Error thrown when the state directory cannot be created
19
- */
20
- export class StateDirectoryCreateError extends StateError {
21
- /** Path that could not be created */
22
- public readonly path: string;
23
- /** System error code if available (e.g., EACCES, ENOENT) */
24
- public readonly code?: string;
25
-
26
- constructor(message: string, path: string, cause?: Error) {
27
- super(message);
28
- this.name = "StateDirectoryCreateError";
29
- this.path = path;
30
- this.cause = cause;
31
- this.code = (cause as NodeJS.ErrnoException | undefined)?.code;
32
- }
33
- }
34
-
35
- /**
36
- * Error thrown when the state directory validation fails
37
- */
38
- export class StateDirectoryValidationError extends StateError {
39
- /** Paths that failed validation */
40
- public readonly missingPaths: string[];
41
-
42
- constructor(message: string, missingPaths: string[]) {
43
- super(message);
44
- this.name = "StateDirectoryValidationError";
45
- this.missingPaths = missingPaths;
46
- }
47
- }
48
-
49
- /**
50
- * Error thrown when the state file cannot be read or written
51
- */
52
- export class StateFileError extends StateError {
53
- /** Path to the state file */
54
- public readonly path: string;
55
- /** The operation that failed */
56
- public readonly operation: "read" | "write";
57
-
58
- constructor(
59
- message: string,
60
- path: string,
61
- operation: "read" | "write",
62
- cause?: Error
63
- ) {
64
- super(message);
65
- this.name = "StateFileError";
66
- this.path = path;
67
- this.operation = operation;
68
- this.cause = cause;
69
- }
70
- }
71
-
72
- /**
73
- * Get a descriptive error message for common permission errors
74
- */
75
- export function getPermissionErrorMessage(
76
- code: string | undefined,
77
- path: string
78
- ): string {
79
- switch (code) {
80
- case "EACCES":
81
- return `Permission denied: Cannot access '${path}'. Check file permissions.`;
82
- case "EPERM":
83
- return `Operation not permitted: Cannot modify '${path}'. This may require elevated privileges.`;
84
- case "EROFS":
85
- return `Read-only filesystem: Cannot write to '${path}'.`;
86
- case "ENOSPC":
87
- return `No space left on device: Cannot create '${path}'.`;
88
- case "ENOENT":
89
- return `Path does not exist: '${path}' or one of its parent directories is missing.`;
90
- case "ENOTDIR":
91
- return `Not a directory: A component of '${path}' is not a directory.`;
92
- case "EEXIST":
93
- return `Path already exists: '${path}' already exists as a file but should be a directory.`;
94
- default:
95
- return `Failed to access '${path}'${code ? ` (${code})` : ""}.`;
96
- }
97
- }
@@ -1,284 +0,0 @@
1
- /**
2
- * Fleet state management (state.yaml)
3
- *
4
- * Provides functions for reading, writing, and updating fleet state.
5
- * Handles missing files gracefully and corrupted files with warnings.
6
- */
7
-
8
- import { ZodError } from "zod";
9
- import {
10
- FleetStateSchema,
11
- createInitialFleetState,
12
- type FleetState,
13
- type AgentState,
14
- } from "./schemas/fleet-state.js";
15
- import { safeReadYaml } from "./utils/reads.js";
16
- import { atomicWriteYaml } from "./utils/atomic.js";
17
- import { StateFileError } from "./errors.js";
18
-
19
- /**
20
- * Logger interface for warning messages
21
- */
22
- export interface StateLogger {
23
- warn: (message: string) => void;
24
- }
25
-
26
- /**
27
- * Default console logger
28
- */
29
- const defaultLogger: StateLogger = {
30
- warn: (message: string) => console.warn(`[herdctl] ${message}`),
31
- };
32
-
33
- /**
34
- * Options for reading fleet state
35
- */
36
- export interface ReadFleetStateOptions {
37
- /**
38
- * Logger for warning messages (e.g., corrupted file warnings)
39
- * Default: console.warn
40
- */
41
- logger?: StateLogger;
42
- }
43
-
44
- /**
45
- * Options for writing fleet state
46
- */
47
- export interface WriteFleetStateOptions {
48
- /**
49
- * YAML indent level
50
- * Default: 2
51
- */
52
- indent?: number;
53
- }
54
-
55
- /**
56
- * Read and validate fleet state from state.yaml
57
- *
58
- * Handles:
59
- * - Missing file: Returns default empty state
60
- * - Corrupted file: Logs warning, returns default empty state
61
- * - Valid file: Returns validated FleetState
62
- *
63
- * @param stateFilePath - Path to state.yaml file
64
- * @param options - Read options including logger
65
- * @returns Validated FleetState object
66
- *
67
- * @example
68
- * ```typescript
69
- * const state = await readFleetState('.herdctl/state.yaml');
70
- * console.log(state.fleet.started_at);
71
- * console.log(state.agents);
72
- * ```
73
- */
74
- export async function readFleetState(
75
- stateFilePath: string,
76
- options: ReadFleetStateOptions = {}
77
- ): Promise<FleetState> {
78
- const logger = options.logger ?? defaultLogger;
79
-
80
- // Attempt to read the file
81
- const readResult = await safeReadYaml<unknown>(stateFilePath);
82
-
83
- // Handle file not found - return default state
84
- if (!readResult.success) {
85
- if (readResult.error.code === "ENOENT") {
86
- return createInitialFleetState();
87
- }
88
- // For other read errors, log and return default
89
- logger.warn(
90
- `Failed to read state file '${stateFilePath}': ${readResult.error.message}. Using default state.`
91
- );
92
- return createInitialFleetState();
93
- }
94
-
95
- // Handle empty file - return default state
96
- if (readResult.data === null || readResult.data === undefined) {
97
- return createInitialFleetState();
98
- }
99
-
100
- // Validate against schema
101
- try {
102
- return FleetStateSchema.parse(readResult.data);
103
- } catch (error) {
104
- if (error instanceof ZodError) {
105
- const issues = error.issues
106
- .map((i) => `${i.path.join(".") || "(root)"}: ${i.message}`)
107
- .join(", ");
108
- logger.warn(
109
- `Corrupted state file '${stateFilePath}': ${issues}. Using default state.`
110
- );
111
- return createInitialFleetState();
112
- }
113
- // Unexpected error - log and return default
114
- logger.warn(
115
- `Unexpected error parsing state file '${stateFilePath}': ${(error as Error).message}. Using default state.`
116
- );
117
- return createInitialFleetState();
118
- }
119
- }
120
-
121
- /**
122
- * Write fleet state to state.yaml atomically
123
- *
124
- * Uses atomic write (write to temp file, then rename) to prevent corruption.
125
- *
126
- * @param stateFilePath - Path to state.yaml file
127
- * @param state - FleetState object to write
128
- * @param options - Write options
129
- * @throws {StateFileError} If write fails
130
- *
131
- * @example
132
- * ```typescript
133
- * const state = await readFleetState('.herdctl/state.yaml');
134
- * state.fleet.started_at = new Date().toISOString();
135
- * await writeFleetState('.herdctl/state.yaml', state);
136
- * ```
137
- */
138
- export async function writeFleetState(
139
- stateFilePath: string,
140
- state: FleetState,
141
- options: WriteFleetStateOptions = {}
142
- ): Promise<void> {
143
- // Validate before writing
144
- const validatedState = FleetStateSchema.parse(state);
145
-
146
- try {
147
- await atomicWriteYaml(stateFilePath, validatedState, {
148
- indent: options.indent ?? 2,
149
- });
150
- } catch (error) {
151
- throw new StateFileError(
152
- `Failed to write state file '${stateFilePath}': ${(error as Error).message}`,
153
- stateFilePath,
154
- "write",
155
- error as Error
156
- );
157
- }
158
- }
159
-
160
- /**
161
- * Partial updates for agent state
162
- */
163
- export type AgentStateUpdates = Partial<AgentState>;
164
-
165
- /**
166
- * Update a single agent's state within the fleet state
167
- *
168
- * This is a convenience function that:
169
- * 1. Reads current state
170
- * 2. Applies partial updates to the specified agent
171
- * 3. Writes the updated state back atomically
172
- *
173
- * If the agent doesn't exist, it will be created with the provided updates.
174
- *
175
- * @param stateFilePath - Path to state.yaml file
176
- * @param agentName - Name of the agent to update
177
- * @param updates - Partial AgentState updates to apply
178
- * @param options - Options for read/write operations
179
- * @returns The updated FleetState
180
- * @throws {StateFileError} If write fails
181
- *
182
- * @example
183
- * ```typescript
184
- * // Mark agent as running with a job
185
- * await updateAgentState('.herdctl/state.yaml', 'my-agent', {
186
- * status: 'running',
187
- * current_job: 'job-123',
188
- * });
189
- *
190
- * // Clear error state
191
- * await updateAgentState('.herdctl/state.yaml', 'my-agent', {
192
- * status: 'idle',
193
- * error_message: null,
194
- * });
195
- * ```
196
- */
197
- export async function updateAgentState(
198
- stateFilePath: string,
199
- agentName: string,
200
- updates: AgentStateUpdates,
201
- options: ReadFleetStateOptions & WriteFleetStateOptions = {}
202
- ): Promise<FleetState> {
203
- // Read current state
204
- const currentState = await readFleetState(stateFilePath, options);
205
-
206
- // Get current agent state or create new one
207
- const currentAgentState = currentState.agents[agentName] ?? { status: "idle" };
208
-
209
- // Merge updates
210
- const updatedAgentState: AgentState = {
211
- ...currentAgentState,
212
- ...updates,
213
- };
214
-
215
- // Update the state
216
- const updatedState: FleetState = {
217
- ...currentState,
218
- agents: {
219
- ...currentState.agents,
220
- [agentName]: updatedAgentState,
221
- },
222
- };
223
-
224
- // Write back
225
- await writeFleetState(stateFilePath, updatedState, options);
226
-
227
- return updatedState;
228
- }
229
-
230
- /**
231
- * Initialize fleet state with started_at timestamp if not already set
232
- *
233
- * @param stateFilePath - Path to state.yaml file
234
- * @param options - Options for read/write operations
235
- * @returns The initialized FleetState
236
- */
237
- export async function initializeFleetState(
238
- stateFilePath: string,
239
- options: ReadFleetStateOptions & WriteFleetStateOptions = {}
240
- ): Promise<FleetState> {
241
- const currentState = await readFleetState(stateFilePath, options);
242
-
243
- // Only set started_at if not already set
244
- if (!currentState.fleet.started_at) {
245
- const updatedState: FleetState = {
246
- ...currentState,
247
- fleet: {
248
- ...currentState.fleet,
249
- started_at: new Date().toISOString(),
250
- },
251
- };
252
- await writeFleetState(stateFilePath, updatedState, options);
253
- return updatedState;
254
- }
255
-
256
- return currentState;
257
- }
258
-
259
- /**
260
- * Remove an agent from the fleet state
261
- *
262
- * @param stateFilePath - Path to state.yaml file
263
- * @param agentName - Name of the agent to remove
264
- * @param options - Options for read/write operations
265
- * @returns The updated FleetState
266
- */
267
- export async function removeAgentState(
268
- stateFilePath: string,
269
- agentName: string,
270
- options: ReadFleetStateOptions & WriteFleetStateOptions = {}
271
- ): Promise<FleetState> {
272
- const currentState = await readFleetState(stateFilePath, options);
273
-
274
- // Create new agents map without the specified agent
275
- const { [agentName]: _, ...remainingAgents } = currentState.agents;
276
-
277
- const updatedState: FleetState = {
278
- ...currentState,
279
- agents: remainingAgents,
280
- };
281
-
282
- await writeFleetState(stateFilePath, updatedState, options);
283
- return updatedState;
284
- }
@@ -1,79 +0,0 @@
1
- /**
2
- * State management module
3
- *
4
- * Provides utilities for managing herdctl state files including:
5
- * - State directory initialization and management
6
- * - Atomic file writes to prevent corruption
7
- * - YAML and JSONL file operations
8
- * - Safe reads with validation
9
- * - Fleet state (state.yaml) management
10
- * - Job metadata (job-<id>.yaml) management
11
- */
12
-
13
- // Re-export types
14
- export * from "./types.js";
15
-
16
- // Re-export errors
17
- export * from "./errors.js";
18
-
19
- // Re-export schemas
20
- export * from "./schemas/index.js";
21
-
22
- // Re-export directory functions
23
- export {
24
- initStateDirectory,
25
- getStateDirectory,
26
- validateStateDirectory,
27
- } from "./directory.js";
28
-
29
- // Re-export file utilities
30
- export * from "./utils/index.js";
31
-
32
- // Re-export fleet state functions
33
- export {
34
- readFleetState,
35
- writeFleetState,
36
- updateAgentState,
37
- initializeFleetState,
38
- removeAgentState,
39
- type StateLogger,
40
- type ReadFleetStateOptions,
41
- type WriteFleetStateOptions,
42
- type AgentStateUpdates,
43
- } from "./fleet-state.js";
44
-
45
- // Re-export job metadata functions
46
- export {
47
- createJob,
48
- updateJob,
49
- getJob,
50
- listJobs,
51
- deleteJob,
52
- type JobMetadataOptions,
53
- type JobLogger,
54
- type JobMetadataUpdates,
55
- type ListJobsFilter,
56
- type ListJobsResult,
57
- } from "./job-metadata.js";
58
-
59
- // Re-export job output functions
60
- export {
61
- getJobOutputPath,
62
- appendJobOutput,
63
- appendJobOutputBatch,
64
- readJobOutput,
65
- readJobOutputAll,
66
- type JobOutputLogger,
67
- type JobOutputOptions,
68
- type ReadJobOutputOptions,
69
- } from "./job-output.js";
70
-
71
- // Re-export session functions
72
- export {
73
- getSessionInfo,
74
- updateSessionInfo,
75
- clearSession,
76
- type SessionOptions,
77
- type SessionLogger,
78
- type SessionInfoUpdates,
79
- } from "./session.js";