@herdctl/core 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. package/dist/config/__tests__/agent.test.js +31 -13
  2. package/dist/config/__tests__/agent.test.js.map +1 -1
  3. package/dist/config/__tests__/merge.test.js +9 -2
  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 +828 -24
  12. package/dist/config/schema.d.ts.map +1 -1
  13. package/dist/config/schema.js +118 -6
  14. package/dist/config/schema.js.map +1 -1
  15. package/dist/fleet-manager/__tests__/coverage.test.js +11 -332
  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 +109 -0
  20. package/dist/fleet-manager/__tests__/integration.test.js.map +1 -1
  21. package/dist/fleet-manager/__tests__/reload.test.js +1 -1
  22. package/dist/fleet-manager/__tests__/reload.test.js.map +1 -1
  23. package/dist/fleet-manager/config-reload.d.ts +164 -0
  24. package/dist/fleet-manager/config-reload.d.ts.map +1 -0
  25. package/dist/fleet-manager/config-reload.js +445 -0
  26. package/dist/fleet-manager/config-reload.js.map +1 -0
  27. package/dist/fleet-manager/context.d.ts +76 -0
  28. package/dist/fleet-manager/context.d.ts.map +1 -0
  29. package/dist/fleet-manager/context.js +11 -0
  30. package/dist/fleet-manager/context.js.map +1 -0
  31. package/dist/fleet-manager/errors.d.ts +0 -25
  32. package/dist/fleet-manager/errors.d.ts.map +1 -1
  33. package/dist/fleet-manager/errors.js +0 -38
  34. package/dist/fleet-manager/errors.js.map +1 -1
  35. package/dist/fleet-manager/event-emitters.d.ts +123 -0
  36. package/dist/fleet-manager/event-emitters.d.ts.map +1 -0
  37. package/dist/fleet-manager/event-emitters.js +136 -0
  38. package/dist/fleet-manager/event-emitters.js.map +1 -0
  39. package/dist/fleet-manager/event-types.d.ts +0 -15
  40. package/dist/fleet-manager/event-types.d.ts.map +1 -1
  41. package/dist/fleet-manager/fleet-manager.d.ts +40 -653
  42. package/dist/fleet-manager/fleet-manager.d.ts.map +1 -1
  43. package/dist/fleet-manager/fleet-manager.js +95 -1720
  44. package/dist/fleet-manager/fleet-manager.js.map +1 -1
  45. package/dist/fleet-manager/index.d.ts +13 -2
  46. package/dist/fleet-manager/index.d.ts.map +1 -1
  47. package/dist/fleet-manager/index.js +19 -6
  48. package/dist/fleet-manager/index.js.map +1 -1
  49. package/dist/fleet-manager/job-control.d.ts +64 -0
  50. package/dist/fleet-manager/job-control.d.ts.map +1 -0
  51. package/dist/fleet-manager/job-control.js +296 -0
  52. package/dist/fleet-manager/job-control.js.map +1 -0
  53. package/dist/fleet-manager/log-streaming.d.ts +171 -0
  54. package/dist/fleet-manager/log-streaming.d.ts.map +1 -0
  55. package/dist/fleet-manager/log-streaming.js +503 -0
  56. package/dist/fleet-manager/log-streaming.js.map +1 -0
  57. package/dist/fleet-manager/schedule-executor.d.ts +63 -0
  58. package/dist/fleet-manager/schedule-executor.d.ts.map +1 -0
  59. package/dist/fleet-manager/schedule-executor.js +209 -0
  60. package/dist/fleet-manager/schedule-executor.js.map +1 -0
  61. package/dist/fleet-manager/schedule-management.d.ts +71 -0
  62. package/dist/fleet-manager/schedule-management.d.ts.map +1 -0
  63. package/dist/fleet-manager/schedule-management.js +171 -0
  64. package/dist/fleet-manager/schedule-management.js.map +1 -0
  65. package/dist/fleet-manager/status-queries.d.ts +105 -0
  66. package/dist/fleet-manager/status-queries.d.ts.map +1 -0
  67. package/dist/fleet-manager/status-queries.js +247 -0
  68. package/dist/fleet-manager/status-queries.js.map +1 -0
  69. package/dist/fleet-manager/types.d.ts +0 -39
  70. package/dist/fleet-manager/types.d.ts.map +1 -1
  71. package/dist/runner/__tests__/job-executor.test.js +206 -1
  72. package/dist/runner/__tests__/job-executor.test.js.map +1 -1
  73. package/dist/runner/job-executor.d.ts +9 -0
  74. package/dist/runner/job-executor.d.ts.map +1 -1
  75. package/dist/runner/job-executor.js +78 -4
  76. package/dist/runner/job-executor.js.map +1 -1
  77. package/dist/runner/types.d.ts +2 -0
  78. package/dist/runner/types.d.ts.map +1 -1
  79. package/dist/scheduler/__tests__/cron.test.d.ts +2 -0
  80. package/dist/scheduler/__tests__/cron.test.d.ts.map +1 -0
  81. package/dist/scheduler/__tests__/cron.test.js +867 -0
  82. package/dist/scheduler/__tests__/cron.test.js.map +1 -0
  83. package/dist/scheduler/__tests__/scheduler.test.js +164 -5
  84. package/dist/scheduler/__tests__/scheduler.test.js.map +1 -1
  85. package/dist/scheduler/cron.d.ts +126 -0
  86. package/dist/scheduler/cron.d.ts.map +1 -0
  87. package/dist/scheduler/cron.js +390 -0
  88. package/dist/scheduler/cron.js.map +1 -0
  89. package/dist/scheduler/errors.d.ts +81 -1
  90. package/dist/scheduler/errors.d.ts.map +1 -1
  91. package/dist/scheduler/errors.js +81 -6
  92. package/dist/scheduler/errors.js.map +1 -1
  93. package/dist/scheduler/index.d.ts +1 -0
  94. package/dist/scheduler/index.d.ts.map +1 -1
  95. package/dist/scheduler/index.js +2 -0
  96. package/dist/scheduler/index.js.map +1 -1
  97. package/dist/scheduler/schedule-runner.d.ts +2 -2
  98. package/dist/scheduler/schedule-runner.d.ts.map +1 -1
  99. package/dist/scheduler/schedule-runner.js +20 -8
  100. package/dist/scheduler/schedule-runner.js.map +1 -1
  101. package/dist/scheduler/scheduler.d.ts +4 -4
  102. package/dist/scheduler/scheduler.d.ts.map +1 -1
  103. package/dist/scheduler/scheduler.js +86 -20
  104. package/dist/scheduler/scheduler.js.map +1 -1
  105. package/dist/scheduler/types.d.ts +1 -1
  106. package/dist/scheduler/types.d.ts.map +1 -1
  107. package/dist/state/schemas/job-metadata.d.ts +2 -2
  108. package/package.json +33 -8
  109. package/.turbo/turbo-build.log +0 -4
  110. package/.turbo/turbo-test.log +0 -219
  111. package/.turbo/turbo-typecheck.log +0 -4
  112. package/coverage/base.css +0 -224
  113. package/coverage/block-navigation.js +0 -87
  114. package/coverage/coverage-final.json +0 -51
  115. package/coverage/favicon.png +0 -0
  116. package/coverage/index.html +0 -251
  117. package/coverage/prettify.css +0 -1
  118. package/coverage/prettify.js +0 -2
  119. package/coverage/sort-arrow-sprite.png +0 -0
  120. package/coverage/sorter.js +0 -210
  121. package/coverage/src/config/index.html +0 -191
  122. package/coverage/src/config/index.ts.html +0 -442
  123. package/coverage/src/config/interpolate.ts.html +0 -652
  124. package/coverage/src/config/loader.ts.html +0 -1501
  125. package/coverage/src/config/merge.ts.html +0 -823
  126. package/coverage/src/config/parser.ts.html +0 -1213
  127. package/coverage/src/config/schema.ts.html +0 -1123
  128. package/coverage/src/fleet-manager/errors.ts.html +0 -2326
  129. package/coverage/src/fleet-manager/event-types.ts.html +0 -1219
  130. package/coverage/src/fleet-manager/fleet-manager.ts.html +0 -7030
  131. package/coverage/src/fleet-manager/index.html +0 -206
  132. package/coverage/src/fleet-manager/index.ts.html +0 -469
  133. package/coverage/src/fleet-manager/job-manager.ts.html +0 -2074
  134. package/coverage/src/fleet-manager/job-queue.ts.html +0 -2479
  135. package/coverage/src/fleet-manager/types.ts.html +0 -2602
  136. package/coverage/src/index.html +0 -116
  137. package/coverage/src/index.ts.html +0 -181
  138. package/coverage/src/runner/errors.ts.html +0 -1006
  139. package/coverage/src/runner/index.html +0 -191
  140. package/coverage/src/runner/index.ts.html +0 -256
  141. package/coverage/src/runner/job-executor.ts.html +0 -1429
  142. package/coverage/src/runner/message-processor.ts.html +0 -1150
  143. package/coverage/src/runner/sdk-adapter.ts.html +0 -658
  144. package/coverage/src/runner/types.ts.html +0 -559
  145. package/coverage/src/scheduler/errors.ts.html +0 -388
  146. package/coverage/src/scheduler/index.html +0 -206
  147. package/coverage/src/scheduler/index.ts.html +0 -244
  148. package/coverage/src/scheduler/interval.ts.html +0 -652
  149. package/coverage/src/scheduler/schedule-runner.ts.html +0 -1411
  150. package/coverage/src/scheduler/schedule-state.ts.html +0 -718
  151. package/coverage/src/scheduler/scheduler.ts.html +0 -1795
  152. package/coverage/src/scheduler/types.ts.html +0 -733
  153. package/coverage/src/state/directory.ts.html +0 -736
  154. package/coverage/src/state/errors.ts.html +0 -376
  155. package/coverage/src/state/fleet-state.ts.html +0 -937
  156. package/coverage/src/state/index.html +0 -221
  157. package/coverage/src/state/index.ts.html +0 -322
  158. package/coverage/src/state/job-metadata.ts.html +0 -1420
  159. package/coverage/src/state/job-output.ts.html +0 -1033
  160. package/coverage/src/state/schemas/fleet-state.ts.html +0 -445
  161. package/coverage/src/state/schemas/index.html +0 -176
  162. package/coverage/src/state/schemas/index.ts.html +0 -286
  163. package/coverage/src/state/schemas/job-metadata.ts.html +0 -628
  164. package/coverage/src/state/schemas/job-output.ts.html +0 -616
  165. package/coverage/src/state/schemas/session-info.ts.html +0 -361
  166. package/coverage/src/state/session.ts.html +0 -844
  167. package/coverage/src/state/types.ts.html +0 -262
  168. package/coverage/src/state/utils/atomic.ts.html +0 -748
  169. package/coverage/src/state/utils/index.html +0 -146
  170. package/coverage/src/state/utils/index.ts.html +0 -103
  171. package/coverage/src/state/utils/reads.ts.html +0 -1621
  172. package/coverage/src/work-sources/adapters/github.ts.html +0 -3583
  173. package/coverage/src/work-sources/adapters/index.html +0 -131
  174. package/coverage/src/work-sources/adapters/index.ts.html +0 -277
  175. package/coverage/src/work-sources/errors.ts.html +0 -298
  176. package/coverage/src/work-sources/index.html +0 -176
  177. package/coverage/src/work-sources/index.ts.html +0 -529
  178. package/coverage/src/work-sources/manager.ts.html +0 -1324
  179. package/coverage/src/work-sources/registry.ts.html +0 -619
  180. package/coverage/src/work-sources/types.ts.html +0 -568
  181. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts +0 -7
  182. package/dist/fleet-manager/__tests__/event-helpers.test.d.ts.map +0 -1
  183. package/dist/fleet-manager/__tests__/event-helpers.test.js +0 -368
  184. package/dist/fleet-manager/__tests__/event-helpers.test.js.map +0 -1
  185. package/src/config/__tests__/agent.test.ts +0 -864
  186. package/src/config/__tests__/interpolate.test.ts +0 -644
  187. package/src/config/__tests__/loader.test.ts +0 -784
  188. package/src/config/__tests__/merge.test.ts +0 -751
  189. package/src/config/__tests__/parser.test.ts +0 -533
  190. package/src/config/__tests__/schema.test.ts +0 -873
  191. package/src/config/index.ts +0 -119
  192. package/src/config/interpolate.ts +0 -189
  193. package/src/config/loader.ts +0 -472
  194. package/src/config/merge.ts +0 -246
  195. package/src/config/parser.ts +0 -376
  196. package/src/config/schema.ts +0 -346
  197. package/src/fleet-manager/__tests__/coverage.test.ts +0 -2869
  198. package/src/fleet-manager/__tests__/errors.test.ts +0 -660
  199. package/src/fleet-manager/__tests__/event-helpers.test.ts +0 -448
  200. package/src/fleet-manager/__tests__/integration.test.ts +0 -1209
  201. package/src/fleet-manager/__tests__/job-control.test.ts +0 -283
  202. package/src/fleet-manager/__tests__/job-manager.test.ts +0 -869
  203. package/src/fleet-manager/__tests__/job-queue.test.ts +0 -401
  204. package/src/fleet-manager/__tests__/reload.test.ts +0 -751
  205. package/src/fleet-manager/__tests__/status-queries.test.ts +0 -595
  206. package/src/fleet-manager/__tests__/trigger.test.ts +0 -601
  207. package/src/fleet-manager/errors.ts +0 -747
  208. package/src/fleet-manager/event-types.ts +0 -378
  209. package/src/fleet-manager/fleet-manager.ts +0 -2315
  210. package/src/fleet-manager/index.ts +0 -128
  211. package/src/fleet-manager/job-manager.ts +0 -663
  212. package/src/fleet-manager/job-queue.ts +0 -798
  213. package/src/fleet-manager/types.ts +0 -839
  214. package/src/index.ts +0 -32
  215. package/src/runner/__tests__/errors.test.ts +0 -382
  216. package/src/runner/__tests__/job-executor.test.ts +0 -1708
  217. package/src/runner/__tests__/message-processor.test.ts +0 -960
  218. package/src/runner/__tests__/sdk-adapter.test.ts +0 -626
  219. package/src/runner/errors.ts +0 -307
  220. package/src/runner/index.ts +0 -57
  221. package/src/runner/job-executor.ts +0 -448
  222. package/src/runner/message-processor.ts +0 -355
  223. package/src/runner/sdk-adapter.ts +0 -191
  224. package/src/runner/types.ts +0 -158
  225. package/src/scheduler/__tests__/errors.test.ts +0 -159
  226. package/src/scheduler/__tests__/interval.test.ts +0 -515
  227. package/src/scheduler/__tests__/schedule-runner.test.ts +0 -798
  228. package/src/scheduler/__tests__/schedule-state.test.ts +0 -671
  229. package/src/scheduler/__tests__/scheduler.test.ts +0 -1280
  230. package/src/scheduler/errors.ts +0 -101
  231. package/src/scheduler/index.ts +0 -53
  232. package/src/scheduler/interval.ts +0 -189
  233. package/src/scheduler/schedule-runner.ts +0 -442
  234. package/src/scheduler/schedule-state.ts +0 -211
  235. package/src/scheduler/scheduler.ts +0 -570
  236. package/src/scheduler/types.ts +0 -216
  237. package/src/state/__tests__/directory.test.ts +0 -595
  238. package/src/state/__tests__/fleet-state.test.ts +0 -868
  239. package/src/state/__tests__/job-metadata-schema.test.ts +0 -414
  240. package/src/state/__tests__/job-metadata.test.ts +0 -831
  241. package/src/state/__tests__/job-output.test.ts +0 -856
  242. package/src/state/__tests__/session-schema.test.ts +0 -378
  243. package/src/state/__tests__/session.test.ts +0 -604
  244. package/src/state/directory.ts +0 -217
  245. package/src/state/errors.ts +0 -97
  246. package/src/state/fleet-state.ts +0 -284
  247. package/src/state/index.ts +0 -79
  248. package/src/state/job-metadata.ts +0 -445
  249. package/src/state/job-output.ts +0 -316
  250. package/src/state/schemas/__tests__/job-output.test.ts +0 -338
  251. package/src/state/schemas/fleet-state.ts +0 -120
  252. package/src/state/schemas/index.ts +0 -67
  253. package/src/state/schemas/job-metadata.ts +0 -181
  254. package/src/state/schemas/job-output.ts +0 -177
  255. package/src/state/schemas/session-info.ts +0 -92
  256. package/src/state/session.ts +0 -253
  257. package/src/state/types.ts +0 -59
  258. package/src/state/utils/__tests__/atomic.test.ts +0 -723
  259. package/src/state/utils/__tests__/reads.test.ts +0 -1071
  260. package/src/state/utils/atomic.ts +0 -221
  261. package/src/state/utils/index.ts +0 -6
  262. package/src/state/utils/reads.ts +0 -512
  263. package/src/work-sources/__tests__/github.test.ts +0 -1800
  264. package/src/work-sources/__tests__/manager.test.ts +0 -529
  265. package/src/work-sources/__tests__/registry.test.ts +0 -477
  266. package/src/work-sources/__tests__/types.test.ts +0 -479
  267. package/src/work-sources/adapters/github.ts +0 -1166
  268. package/src/work-sources/adapters/index.ts +0 -64
  269. package/src/work-sources/errors.ts +0 -71
  270. package/src/work-sources/index.ts +0 -148
  271. package/src/work-sources/manager.ts +0 -413
  272. package/src/work-sources/registry.ts +0 -178
  273. package/src/work-sources/types.ts +0 -161
  274. package/tsconfig.json +0 -9
  275. package/vitest.config.ts +0 -19
@@ -1,856 +0,0 @@
1
- import { describe, it, expect, beforeEach, afterEach } from "vitest";
2
- import { mkdir, rm, realpath, writeFile, readFile } from "node:fs/promises";
3
- import { join } from "node:path";
4
- import { tmpdir } from "node:os";
5
- import {
6
- getJobOutputPath,
7
- appendJobOutput,
8
- appendJobOutputBatch,
9
- readJobOutput,
10
- readJobOutputAll,
11
- type JobOutputLogger,
12
- } from "../job-output.js";
13
- import {
14
- type JobOutputMessage,
15
- type JobOutputInput,
16
- } from "../schemas/job-output.js";
17
- import { StateFileError } from "../errors.js";
18
-
19
- // Helper to create a temp directory
20
- async function createTempDir(): Promise<string> {
21
- const baseDir = join(
22
- tmpdir(),
23
- `herdctl-output-test-${Date.now()}-${Math.random().toString(36).slice(2)}`
24
- );
25
- await mkdir(baseDir, { recursive: true });
26
- // Resolve to real path to handle macOS /var -> /private/var symlink
27
- return await realpath(baseDir);
28
- }
29
-
30
- // Helper to create a mock logger
31
- function createMockLogger(): JobOutputLogger & { warnings: string[] } {
32
- const warnings: string[] = [];
33
- return {
34
- warnings,
35
- warn: (message: string) => warnings.push(message),
36
- };
37
- }
38
-
39
- // Helper to read raw JSONL file
40
- async function readRawJsonl(filePath: string): Promise<string[]> {
41
- const content = await readFile(filePath, "utf-8");
42
- return content.split("\n").filter((line) => line.trim() !== "");
43
- }
44
-
45
- // Helper to write raw JSONL content
46
- async function writeRawJsonl(
47
- filePath: string,
48
- lines: string[]
49
- ): Promise<void> {
50
- await writeFile(filePath, lines.join("\n") + "\n", "utf-8");
51
- }
52
-
53
- describe("getJobOutputPath", () => {
54
- it("returns correct path for job ID", () => {
55
- const path = getJobOutputPath("/path/to/jobs", "job-2024-01-15-abc123");
56
- expect(path).toBe("/path/to/jobs/job-2024-01-15-abc123.jsonl");
57
- });
58
-
59
- it("handles various job ID formats", () => {
60
- const path = getJobOutputPath("/jobs", "job-2025-12-31-xyz999");
61
- expect(path).toBe("/jobs/job-2025-12-31-xyz999.jsonl");
62
- });
63
- });
64
-
65
- describe("appendJobOutput", () => {
66
- let tempDir: string;
67
-
68
- beforeEach(async () => {
69
- tempDir = await createTempDir();
70
- });
71
-
72
- afterEach(async () => {
73
- await rm(tempDir, { recursive: true, force: true });
74
- });
75
-
76
- it("creates file and appends system message", async () => {
77
- const jobId = "job-2024-01-15-test01";
78
-
79
- await appendJobOutput(tempDir, jobId, {
80
- type: "system",
81
- content: "System initialized",
82
- });
83
-
84
- const outputPath = getJobOutputPath(tempDir, jobId);
85
- const lines = await readRawJsonl(outputPath);
86
-
87
- expect(lines).toHaveLength(1);
88
- const message = JSON.parse(lines[0]) as JobOutputMessage;
89
- expect(message.type).toBe("system");
90
- expect(message.timestamp).toMatch(/^\d{4}-\d{2}-\d{2}T/);
91
- if (message.type === "system") {
92
- expect(message.content).toBe("System initialized");
93
- }
94
- });
95
-
96
- it("appends assistant message with content", async () => {
97
- const jobId = "job-2024-01-15-test02";
98
-
99
- await appendJobOutput(tempDir, jobId, {
100
- type: "assistant",
101
- content: "Hello, world!",
102
- partial: false,
103
- });
104
-
105
- const messages = await readJobOutputAll(tempDir, jobId);
106
- expect(messages).toHaveLength(1);
107
- expect(messages[0].type).toBe("assistant");
108
- if (messages[0].type === "assistant") {
109
- expect(messages[0].content).toBe("Hello, world!");
110
- expect(messages[0].partial).toBe(false);
111
- }
112
- });
113
-
114
- it("appends tool_use message with input", async () => {
115
- const jobId = "job-2024-01-15-test03";
116
-
117
- await appendJobOutput(tempDir, jobId, {
118
- type: "tool_use",
119
- tool_name: "read_file",
120
- tool_use_id: "tool-123",
121
- input: { path: "/etc/hosts" },
122
- });
123
-
124
- const messages = await readJobOutputAll(tempDir, jobId);
125
- expect(messages).toHaveLength(1);
126
- expect(messages[0].type).toBe("tool_use");
127
- if (messages[0].type === "tool_use") {
128
- expect(messages[0].tool_name).toBe("read_file");
129
- expect(messages[0].tool_use_id).toBe("tool-123");
130
- expect(messages[0].input).toEqual({ path: "/etc/hosts" });
131
- }
132
- });
133
-
134
- it("appends tool_result message", async () => {
135
- const jobId = "job-2024-01-15-test04";
136
-
137
- await appendJobOutput(tempDir, jobId, {
138
- type: "tool_result",
139
- tool_use_id: "tool-123",
140
- result: "127.0.0.1 localhost",
141
- success: true,
142
- });
143
-
144
- const messages = await readJobOutputAll(tempDir, jobId);
145
- expect(messages).toHaveLength(1);
146
- expect(messages[0].type).toBe("tool_result");
147
- if (messages[0].type === "tool_result") {
148
- expect(messages[0].tool_use_id).toBe("tool-123");
149
- expect(messages[0].result).toBe("127.0.0.1 localhost");
150
- expect(messages[0].success).toBe(true);
151
- }
152
- });
153
-
154
- it("appends error message", async () => {
155
- const jobId = "job-2024-01-15-test05";
156
-
157
- await appendJobOutput(tempDir, jobId, {
158
- type: "error",
159
- message: "Something went wrong",
160
- code: "ERR_TIMEOUT",
161
- });
162
-
163
- const messages = await readJobOutputAll(tempDir, jobId);
164
- expect(messages).toHaveLength(1);
165
- expect(messages[0].type).toBe("error");
166
- if (messages[0].type === "error") {
167
- expect(messages[0].message).toBe("Something went wrong");
168
- expect(messages[0].code).toBe("ERR_TIMEOUT");
169
- }
170
- });
171
-
172
- it("appends multiple messages to same file", async () => {
173
- const jobId = "job-2024-01-15-test06";
174
-
175
- await appendJobOutput(tempDir, jobId, {
176
- type: "system",
177
- content: "Start",
178
- });
179
- await appendJobOutput(tempDir, jobId, {
180
- type: "assistant",
181
- content: "Processing...",
182
- });
183
- await appendJobOutput(tempDir, jobId, {
184
- type: "assistant",
185
- content: "Done!",
186
- });
187
-
188
- const messages = await readJobOutputAll(tempDir, jobId);
189
- expect(messages).toHaveLength(3);
190
- expect(messages[0].type).toBe("system");
191
- expect(messages[1].type).toBe("assistant");
192
- expect(messages[2].type).toBe("assistant");
193
- });
194
-
195
- it("adds timestamp automatically", async () => {
196
- const jobId = "job-2024-01-15-test07";
197
- const before = new Date();
198
-
199
- await appendJobOutput(tempDir, jobId, {
200
- type: "assistant",
201
- content: "Test",
202
- });
203
-
204
- const after = new Date();
205
- const messages = await readJobOutputAll(tempDir, jobId);
206
-
207
- const timestamp = new Date(messages[0].timestamp);
208
- expect(timestamp.getTime()).toBeGreaterThanOrEqual(before.getTime() - 1000);
209
- expect(timestamp.getTime()).toBeLessThanOrEqual(after.getTime() + 1000);
210
- });
211
-
212
- it("throws StateFileError for invalid message type", async () => {
213
- const jobId = "job-2024-01-15-test08";
214
-
215
- await expect(
216
- appendJobOutput(tempDir, jobId, {
217
- type: "invalid" as "system",
218
- content: "Test",
219
- })
220
- ).rejects.toThrow(StateFileError);
221
- });
222
-
223
- it("throws StateFileError for message without type", async () => {
224
- const jobId = "job-2024-01-15-test09";
225
-
226
- await expect(
227
- appendJobOutput(tempDir, jobId, {} as JobOutputInput)
228
- ).rejects.toThrow(StateFileError);
229
- });
230
-
231
- it("throws StateFileError when directory does not exist", async () => {
232
- const nonExistentDir = join(tempDir, "does-not-exist");
233
-
234
- await expect(
235
- appendJobOutput(nonExistentDir, "job-2024-01-15-nodir1", {
236
- type: "system",
237
- content: "Test",
238
- })
239
- ).rejects.toThrow(StateFileError);
240
- });
241
-
242
- it("writes immediately without buffering", async () => {
243
- const jobId = "job-2024-01-15-test10";
244
- const outputPath = getJobOutputPath(tempDir, jobId);
245
-
246
- // Write first message
247
- await appendJobOutput(tempDir, jobId, {
248
- type: "assistant",
249
- content: "First",
250
- });
251
-
252
- // Read immediately - should be there
253
- const lines1 = await readRawJsonl(outputPath);
254
- expect(lines1).toHaveLength(1);
255
-
256
- // Write second message
257
- await appendJobOutput(tempDir, jobId, {
258
- type: "assistant",
259
- content: "Second",
260
- });
261
-
262
- // Read immediately - both should be there
263
- const lines2 = await readRawJsonl(outputPath);
264
- expect(lines2).toHaveLength(2);
265
- });
266
- });
267
-
268
- describe("appendJobOutputBatch", () => {
269
- let tempDir: string;
270
-
271
- beforeEach(async () => {
272
- tempDir = await createTempDir();
273
- });
274
-
275
- afterEach(async () => {
276
- await rm(tempDir, { recursive: true, force: true });
277
- });
278
-
279
- it("appends multiple messages at once", async () => {
280
- const jobId = "job-2024-01-15-batch1";
281
-
282
- await appendJobOutputBatch(tempDir, jobId, [
283
- { type: "tool_use", tool_name: "read_file", input: { path: "/tmp/test" } },
284
- { type: "tool_result", result: "file contents", success: true },
285
- ]);
286
-
287
- const messages = await readJobOutputAll(tempDir, jobId);
288
- expect(messages).toHaveLength(2);
289
- expect(messages[0].type).toBe("tool_use");
290
- expect(messages[1].type).toBe("tool_result");
291
- });
292
-
293
- it("applies same timestamp to all messages in batch", async () => {
294
- const jobId = "job-2024-01-15-batch2";
295
-
296
- await appendJobOutputBatch(tempDir, jobId, [
297
- { type: "assistant", content: "One" },
298
- { type: "assistant", content: "Two" },
299
- { type: "assistant", content: "Three" },
300
- ]);
301
-
302
- const messages = await readJobOutputAll(tempDir, jobId);
303
- expect(messages[0].timestamp).toBe(messages[1].timestamp);
304
- expect(messages[1].timestamp).toBe(messages[2].timestamp);
305
- });
306
-
307
- it("validates all messages before writing any", async () => {
308
- const jobId = "job-2024-01-15-batch3";
309
-
310
- await expect(
311
- appendJobOutputBatch(tempDir, jobId, [
312
- { type: "assistant", content: "Valid" },
313
- { type: "invalid" as "system" }, // Invalid
314
- { type: "assistant", content: "Also valid" },
315
- ])
316
- ).rejects.toThrow(StateFileError);
317
-
318
- // File should not exist since validation failed
319
- const messages = await readJobOutputAll(tempDir, jobId);
320
- expect(messages).toHaveLength(0);
321
- });
322
-
323
- it("handles empty array", async () => {
324
- const jobId = "job-2024-01-15-batch4";
325
-
326
- await appendJobOutputBatch(tempDir, jobId, []);
327
-
328
- const messages = await readJobOutputAll(tempDir, jobId);
329
- expect(messages).toHaveLength(0);
330
- });
331
-
332
- it("reports correct index for invalid message", async () => {
333
- const jobId = "job-2024-01-15-batch5";
334
-
335
- await expect(
336
- appendJobOutputBatch(tempDir, jobId, [
337
- { type: "assistant", content: "Valid" },
338
- { type: "assistant", content: "Also valid" },
339
- {} as JobOutputInput, // Invalid at index 2
340
- ])
341
- ).rejects.toThrow(/index 2/);
342
- });
343
- });
344
-
345
- describe("readJobOutput", () => {
346
- let tempDir: string;
347
-
348
- beforeEach(async () => {
349
- tempDir = await createTempDir();
350
- });
351
-
352
- afterEach(async () => {
353
- await rm(tempDir, { recursive: true, force: true });
354
- });
355
-
356
- it("yields nothing for non-existent file", async () => {
357
- const messages: JobOutputMessage[] = [];
358
- for await (const msg of readJobOutput(tempDir, "job-2024-01-15-nofile")) {
359
- messages.push(msg);
360
- }
361
- expect(messages).toHaveLength(0);
362
- });
363
-
364
- it("yields nothing for empty file", async () => {
365
- const jobId = "job-2024-01-15-empty1";
366
- const outputPath = getJobOutputPath(tempDir, jobId);
367
- await writeFile(outputPath, "", "utf-8");
368
-
369
- const messages: JobOutputMessage[] = [];
370
- for await (const msg of readJobOutput(tempDir, jobId)) {
371
- messages.push(msg);
372
- }
373
- expect(messages).toHaveLength(0);
374
- });
375
-
376
- it("yields messages in order", async () => {
377
- const jobId = "job-2024-01-15-order1";
378
-
379
- await appendJobOutput(tempDir, jobId, { type: "system", content: "First" });
380
- await appendJobOutput(tempDir, jobId, {
381
- type: "assistant",
382
- content: "Second",
383
- });
384
- await appendJobOutput(tempDir, jobId, {
385
- type: "assistant",
386
- content: "Third",
387
- });
388
-
389
- const messages: JobOutputMessage[] = [];
390
- for await (const msg of readJobOutput(tempDir, jobId)) {
391
- messages.push(msg);
392
- }
393
-
394
- expect(messages).toHaveLength(3);
395
- if (messages[0].type === "system") {
396
- expect(messages[0].content).toBe("First");
397
- }
398
- });
399
-
400
- it("skips empty lines", async () => {
401
- const jobId = "job-2024-01-15-empty2";
402
- const outputPath = getJobOutputPath(tempDir, jobId);
403
-
404
- await writeRawJsonl(outputPath, [
405
- JSON.stringify({ type: "system", timestamp: "2024-01-15T10:00:00Z" }),
406
- "",
407
- " ",
408
- JSON.stringify({
409
- type: "assistant",
410
- content: "Hi",
411
- timestamp: "2024-01-15T10:00:01Z",
412
- }),
413
- ]);
414
-
415
- const messages = await readJobOutputAll(tempDir, jobId);
416
- expect(messages).toHaveLength(2);
417
- });
418
-
419
- it("throws StateFileError for invalid JSON by default", async () => {
420
- const jobId = "job-2024-01-15-invalid1";
421
- const outputPath = getJobOutputPath(tempDir, jobId);
422
-
423
- await writeRawJsonl(outputPath, [
424
- JSON.stringify({ type: "system", timestamp: "2024-01-15T10:00:00Z" }),
425
- "not valid json",
426
- JSON.stringify({
427
- type: "assistant",
428
- content: "Hi",
429
- timestamp: "2024-01-15T10:00:01Z",
430
- }),
431
- ]);
432
-
433
- await expect(async () => {
434
- for await (const _msg of readJobOutput(tempDir, jobId)) {
435
- // consume
436
- }
437
- }).rejects.toThrow(StateFileError);
438
- });
439
-
440
- it("skips invalid JSON when skipInvalidLines is true", async () => {
441
- const logger = createMockLogger();
442
- const jobId = "job-2024-01-15-skip1";
443
- const outputPath = getJobOutputPath(tempDir, jobId);
444
-
445
- await writeRawJsonl(outputPath, [
446
- JSON.stringify({ type: "system", timestamp: "2024-01-15T10:00:00Z" }),
447
- "not valid json",
448
- JSON.stringify({
449
- type: "assistant",
450
- content: "Hi",
451
- timestamp: "2024-01-15T10:00:01Z",
452
- }),
453
- ]);
454
-
455
- const messages: JobOutputMessage[] = [];
456
- for await (const msg of readJobOutput(tempDir, jobId, {
457
- skipInvalidLines: true,
458
- logger,
459
- })) {
460
- messages.push(msg);
461
- }
462
-
463
- expect(messages).toHaveLength(2);
464
- expect(logger.warnings.length).toBeGreaterThan(0);
465
- });
466
-
467
- it("throws StateFileError for invalid schema by default", async () => {
468
- const jobId = "job-2024-01-15-schema1";
469
- const outputPath = getJobOutputPath(tempDir, jobId);
470
-
471
- await writeRawJsonl(outputPath, [
472
- JSON.stringify({ type: "system", timestamp: "2024-01-15T10:00:00Z" }),
473
- JSON.stringify({ type: "invalid_type", timestamp: "2024-01-15T10:00:01Z" }),
474
- ]);
475
-
476
- await expect(async () => {
477
- for await (const _msg of readJobOutput(tempDir, jobId)) {
478
- // consume
479
- }
480
- }).rejects.toThrow(StateFileError);
481
- });
482
-
483
- it("skips invalid schema when skipInvalidLines is true", async () => {
484
- const logger = createMockLogger();
485
- const jobId = "job-2024-01-15-schema2";
486
- const outputPath = getJobOutputPath(tempDir, jobId);
487
-
488
- await writeRawJsonl(outputPath, [
489
- JSON.stringify({ type: "system", timestamp: "2024-01-15T10:00:00Z" }),
490
- JSON.stringify({ type: "invalid_type", timestamp: "2024-01-15T10:00:01Z" }),
491
- JSON.stringify({
492
- type: "assistant",
493
- content: "Valid",
494
- timestamp: "2024-01-15T10:00:02Z",
495
- }),
496
- ]);
497
-
498
- const messages: JobOutputMessage[] = [];
499
- for await (const msg of readJobOutput(tempDir, jobId, {
500
- skipInvalidLines: true,
501
- logger,
502
- })) {
503
- messages.push(msg);
504
- }
505
-
506
- expect(messages).toHaveLength(2);
507
- expect(logger.warnings.length).toBeGreaterThan(0);
508
- });
509
-
510
- it("handles large files efficiently with streaming", async () => {
511
- const jobId = "job-2024-01-15-large1";
512
-
513
- // Write many messages
514
- const count = 1000;
515
- for (let i = 0; i < count; i++) {
516
- await appendJobOutput(tempDir, jobId, {
517
- type: "assistant",
518
- content: `Message ${i}`,
519
- });
520
- }
521
-
522
- // Read with streaming - should not load all into memory at once
523
- let readCount = 0;
524
- for await (const _msg of readJobOutput(tempDir, jobId)) {
525
- readCount++;
526
- }
527
-
528
- expect(readCount).toBe(count);
529
- });
530
- });
531
-
532
- describe("readJobOutputAll", () => {
533
- let tempDir: string;
534
-
535
- beforeEach(async () => {
536
- tempDir = await createTempDir();
537
- });
538
-
539
- afterEach(async () => {
540
- await rm(tempDir, { recursive: true, force: true });
541
- });
542
-
543
- it("returns empty array for non-existent file", async () => {
544
- const messages = await readJobOutputAll(tempDir, "job-2024-01-15-nope");
545
- expect(messages).toEqual([]);
546
- });
547
-
548
- it("returns all messages as array", async () => {
549
- const jobId = "job-2024-01-15-all01";
550
-
551
- await appendJobOutputBatch(tempDir, jobId, [
552
- { type: "system", content: "Init" },
553
- { type: "assistant", content: "Hello" },
554
- { type: "tool_use", tool_name: "bash", input: { command: "ls" } },
555
- { type: "tool_result", result: "file1\nfile2", success: true },
556
- { type: "assistant", content: "Done" },
557
- ]);
558
-
559
- const messages = await readJobOutputAll(tempDir, jobId);
560
-
561
- expect(messages).toHaveLength(5);
562
- expect(messages[0].type).toBe("system");
563
- expect(messages[1].type).toBe("assistant");
564
- expect(messages[2].type).toBe("tool_use");
565
- expect(messages[3].type).toBe("tool_result");
566
- expect(messages[4].type).toBe("assistant");
567
- });
568
-
569
- it("passes options to readJobOutput", async () => {
570
- const logger = createMockLogger();
571
- const jobId = "job-2024-01-15-opts1";
572
- const outputPath = getJobOutputPath(tempDir, jobId);
573
-
574
- await writeRawJsonl(outputPath, [
575
- JSON.stringify({ type: "system", timestamp: "2024-01-15T10:00:00Z" }),
576
- "invalid",
577
- JSON.stringify({
578
- type: "assistant",
579
- content: "Valid",
580
- timestamp: "2024-01-15T10:00:01Z",
581
- }),
582
- ]);
583
-
584
- const messages = await readJobOutputAll(tempDir, jobId, {
585
- skipInvalidLines: true,
586
- logger,
587
- });
588
-
589
- expect(messages).toHaveLength(2);
590
- expect(logger.warnings.length).toBeGreaterThan(0);
591
- });
592
- });
593
-
594
- describe("concurrent operations", () => {
595
- let tempDir: string;
596
-
597
- beforeEach(async () => {
598
- tempDir = await createTempDir();
599
- });
600
-
601
- afterEach(async () => {
602
- await rm(tempDir, { recursive: true, force: true });
603
- });
604
-
605
- it("handles concurrent appends to same file", async () => {
606
- const jobId = "job-2024-01-15-conc01";
607
-
608
- // Concurrent appends
609
- const appends = [];
610
- for (let i = 0; i < 50; i++) {
611
- appends.push(
612
- appendJobOutput(tempDir, jobId, {
613
- type: "assistant",
614
- content: `Message ${i}`,
615
- })
616
- );
617
- }
618
-
619
- await Promise.all(appends);
620
-
621
- const messages = await readJobOutputAll(tempDir, jobId);
622
- expect(messages).toHaveLength(50);
623
- });
624
-
625
- it("handles concurrent reads of same file", async () => {
626
- const jobId = "job-2024-01-15-conc02";
627
-
628
- // Write some messages
629
- await appendJobOutputBatch(tempDir, jobId, [
630
- { type: "system", content: "Init" },
631
- { type: "assistant", content: "Hello" },
632
- { type: "assistant", content: "World" },
633
- ]);
634
-
635
- // Concurrent reads
636
- const reads = [];
637
- for (let i = 0; i < 20; i++) {
638
- reads.push(readJobOutputAll(tempDir, jobId));
639
- }
640
-
641
- const results = await Promise.all(reads);
642
-
643
- for (const messages of results) {
644
- expect(messages).toHaveLength(3);
645
- }
646
- });
647
-
648
- it("handles concurrent read and write", async () => {
649
- const jobId = "job-2024-01-15-conc03";
650
-
651
- // Write initial messages
652
- await appendJobOutputBatch(tempDir, jobId, [
653
- { type: "system", content: "Init" },
654
- ]);
655
-
656
- // Concurrent read and write
657
- const writePromise = (async () => {
658
- for (let i = 0; i < 10; i++) {
659
- await appendJobOutput(tempDir, jobId, {
660
- type: "assistant",
661
- content: `New ${i}`,
662
- });
663
- }
664
- })();
665
-
666
- const readPromise = readJobOutputAll(tempDir, jobId, {
667
- skipInvalidLines: true,
668
- });
669
-
670
- await Promise.all([writePromise, readPromise]);
671
-
672
- // Final read should have all messages
673
- const finalMessages = await readJobOutputAll(tempDir, jobId);
674
- expect(finalMessages.length).toBeGreaterThanOrEqual(1);
675
- expect(finalMessages.length).toBeLessThanOrEqual(11);
676
- });
677
- });
678
-
679
- describe("SDK message type coverage", () => {
680
- let tempDir: string;
681
-
682
- beforeEach(async () => {
683
- tempDir = await createTempDir();
684
- });
685
-
686
- afterEach(async () => {
687
- await rm(tempDir, { recursive: true, force: true });
688
- });
689
-
690
- it("handles system message with subtype", async () => {
691
- const jobId = "job-2024-01-15-sdk01";
692
-
693
- await appendJobOutput(tempDir, jobId, {
694
- type: "system",
695
- content: "Session started",
696
- subtype: "session_start",
697
- });
698
-
699
- const messages = await readJobOutputAll(tempDir, jobId);
700
- expect(messages[0].type).toBe("system");
701
- if (messages[0].type === "system") {
702
- expect(messages[0].subtype).toBe("session_start");
703
- }
704
- });
705
-
706
- it("handles assistant message with usage info", async () => {
707
- const jobId = "job-2024-01-15-sdk02";
708
-
709
- await appendJobOutput(tempDir, jobId, {
710
- type: "assistant",
711
- content: "Response text",
712
- usage: {
713
- input_tokens: 100,
714
- output_tokens: 50,
715
- },
716
- });
717
-
718
- const messages = await readJobOutputAll(tempDir, jobId);
719
- if (messages[0].type === "assistant") {
720
- expect(messages[0].usage?.input_tokens).toBe(100);
721
- expect(messages[0].usage?.output_tokens).toBe(50);
722
- }
723
- });
724
-
725
- it("handles tool_use with complex input", async () => {
726
- const jobId = "job-2024-01-15-sdk03";
727
-
728
- await appendJobOutput(tempDir, jobId, {
729
- type: "tool_use",
730
- tool_name: "edit_file",
731
- tool_use_id: "tool-456",
732
- input: {
733
- path: "/src/index.ts",
734
- changes: [
735
- { line: 10, action: "replace", content: "new content" },
736
- { line: 20, action: "delete" },
737
- ],
738
- },
739
- });
740
-
741
- const messages = await readJobOutputAll(tempDir, jobId);
742
- if (messages[0].type === "tool_use") {
743
- expect(messages[0].input).toEqual({
744
- path: "/src/index.ts",
745
- changes: [
746
- { line: 10, action: "replace", content: "new content" },
747
- { line: 20, action: "delete" },
748
- ],
749
- });
750
- }
751
- });
752
-
753
- it("handles tool_result with error", async () => {
754
- const jobId = "job-2024-01-15-sdk04";
755
-
756
- await appendJobOutput(tempDir, jobId, {
757
- type: "tool_result",
758
- tool_use_id: "tool-789",
759
- success: false,
760
- error: "File not found: /nonexistent",
761
- });
762
-
763
- const messages = await readJobOutputAll(tempDir, jobId);
764
- if (messages[0].type === "tool_result") {
765
- expect(messages[0].success).toBe(false);
766
- expect(messages[0].error).toBe("File not found: /nonexistent");
767
- }
768
- });
769
-
770
- it("handles error message with stack trace", async () => {
771
- const jobId = "job-2024-01-15-sdk05";
772
-
773
- await appendJobOutput(tempDir, jobId, {
774
- type: "error",
775
- message: "Unexpected error",
776
- code: "ERR_UNKNOWN",
777
- stack: "Error: Unexpected error\n at foo.ts:10\n at bar.ts:20",
778
- });
779
-
780
- const messages = await readJobOutputAll(tempDir, jobId);
781
- if (messages[0].type === "error") {
782
- expect(messages[0].message).toBe("Unexpected error");
783
- expect(messages[0].code).toBe("ERR_UNKNOWN");
784
- expect(messages[0].stack).toContain("at foo.ts:10");
785
- }
786
- });
787
- });
788
-
789
- describe("edge cases", () => {
790
- let tempDir: string;
791
-
792
- beforeEach(async () => {
793
- tempDir = await createTempDir();
794
- });
795
-
796
- afterEach(async () => {
797
- await rm(tempDir, { recursive: true, force: true });
798
- });
799
-
800
- it("handles message with special characters", async () => {
801
- const jobId = "job-2024-01-15-edge01";
802
-
803
- await appendJobOutput(tempDir, jobId, {
804
- type: "assistant",
805
- content: 'Content with "quotes", \\backslashes\\, and\nnewlines',
806
- });
807
-
808
- const messages = await readJobOutputAll(tempDir, jobId);
809
- if (messages[0].type === "assistant") {
810
- expect(messages[0].content).toBe(
811
- 'Content with "quotes", \\backslashes\\, and\nnewlines'
812
- );
813
- }
814
- });
815
-
816
- it("handles message with unicode", async () => {
817
- const jobId = "job-2024-01-15-edge02";
818
-
819
- await appendJobOutput(tempDir, jobId, {
820
- type: "assistant",
821
- content: "Hello 世界! 🌍 Γεια σου κόσμε",
822
- });
823
-
824
- const messages = await readJobOutputAll(tempDir, jobId);
825
- if (messages[0].type === "assistant") {
826
- expect(messages[0].content).toBe("Hello 世界! 🌍 Γεια σου κόσμε");
827
- }
828
- });
829
-
830
- it("handles very long content", async () => {
831
- const jobId = "job-2024-01-15-edge03";
832
- const longContent = "x".repeat(100000);
833
-
834
- await appendJobOutput(tempDir, jobId, {
835
- type: "assistant",
836
- content: longContent,
837
- });
838
-
839
- const messages = await readJobOutputAll(tempDir, jobId);
840
- if (messages[0].type === "assistant") {
841
- expect(messages[0].content).toHaveLength(100000);
842
- }
843
- });
844
-
845
- it("handles null/undefined optional fields", async () => {
846
- const jobId = "job-2024-01-15-edge04";
847
-
848
- await appendJobOutput(tempDir, jobId, {
849
- type: "assistant",
850
- // No content, partial, or usage
851
- });
852
-
853
- const messages = await readJobOutputAll(tempDir, jobId);
854
- expect(messages[0].type).toBe("assistant");
855
- });
856
- });