@ronkovic/aad 0.3.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 (195) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +312 -0
  3. package/bin/aad.js +2 -0
  4. package/package.json +78 -0
  5. package/src/__tests__/e2e/pipeline-e2e.test.ts +279 -0
  6. package/src/__tests__/e2e/resume-e2e.test.ts +200 -0
  7. package/src/__tests__/integration/cli-smoke.test.ts +175 -0
  8. package/src/__tests__/integration/pipeline.test.ts +346 -0
  9. package/src/bun-imports.d.ts +14 -0
  10. package/src/main.ts +52 -0
  11. package/src/modules/claude-provider/__tests__/claude-cli.adapter.test.ts +277 -0
  12. package/src/modules/claude-provider/__tests__/claude-sdk-real-env.test.ts +127 -0
  13. package/src/modules/claude-provider/__tests__/claude-sdk.adapter.test.ts +347 -0
  14. package/src/modules/claude-provider/__tests__/effort-strategy.test.ts +212 -0
  15. package/src/modules/claude-provider/__tests__/provider-registry.test.ts +251 -0
  16. package/src/modules/claude-provider/__tests__/retry.test.ts +201 -0
  17. package/src/modules/claude-provider/claude-cli.adapter.ts +156 -0
  18. package/src/modules/claude-provider/claude-provider.port.ts +35 -0
  19. package/src/modules/claude-provider/claude-sdk.adapter.ts +217 -0
  20. package/src/modules/claude-provider/effort-strategy.ts +94 -0
  21. package/src/modules/claude-provider/index.ts +32 -0
  22. package/src/modules/claude-provider/provider-registry.ts +92 -0
  23. package/src/modules/claude-provider/retry.ts +81 -0
  24. package/src/modules/cli/__tests__/app.test.ts +160 -0
  25. package/src/modules/cli/__tests__/cleanup.test.ts +111 -0
  26. package/src/modules/cli/__tests__/commands.test.ts +186 -0
  27. package/src/modules/cli/__tests__/output.test.ts +329 -0
  28. package/src/modules/cli/__tests__/resume.test.ts +324 -0
  29. package/src/modules/cli/__tests__/run.test.ts +168 -0
  30. package/src/modules/cli/__tests__/shutdown.test.ts +168 -0
  31. package/src/modules/cli/__tests__/status.test.ts +144 -0
  32. package/src/modules/cli/app.ts +241 -0
  33. package/src/modules/cli/commands/cleanup.ts +120 -0
  34. package/src/modules/cli/commands/resume.ts +156 -0
  35. package/src/modules/cli/commands/run.ts +322 -0
  36. package/src/modules/cli/commands/status.ts +101 -0
  37. package/src/modules/cli/index.ts +29 -0
  38. package/src/modules/cli/output.ts +256 -0
  39. package/src/modules/cli/shutdown.ts +122 -0
  40. package/src/modules/dashboard/__tests__/api-routes.test.ts +204 -0
  41. package/src/modules/dashboard/__tests__/file-watcher.test.ts +34 -0
  42. package/src/modules/dashboard/__tests__/server.test.ts +120 -0
  43. package/src/modules/dashboard/__tests__/sse-broadcaster.test.ts +163 -0
  44. package/src/modules/dashboard/__tests__/sse-routes.test.ts +58 -0
  45. package/src/modules/dashboard/__tests__/state-aggregator.test.ts +330 -0
  46. package/src/modules/dashboard/index.ts +8 -0
  47. package/src/modules/dashboard/routes/api.ts +84 -0
  48. package/src/modules/dashboard/routes/sse.ts +37 -0
  49. package/src/modules/dashboard/server.ts +111 -0
  50. package/src/modules/dashboard/services/file-watcher.ts +36 -0
  51. package/src/modules/dashboard/services/sse-broadcaster.ts +81 -0
  52. package/src/modules/dashboard/services/state-aggregator.ts +132 -0
  53. package/src/modules/dashboard/ui/dashboard.html +405 -0
  54. package/src/modules/git-workspace/__tests__/branch-manager.test.ts +335 -0
  55. package/src/modules/git-workspace/__tests__/git-exec.test.ts +91 -0
  56. package/src/modules/git-workspace/__tests__/memory-sync.test.ts +273 -0
  57. package/src/modules/git-workspace/__tests__/merge-service.test.ts +286 -0
  58. package/src/modules/git-workspace/__tests__/settings-merge.test.ts +163 -0
  59. package/src/modules/git-workspace/__tests__/worktree-manager.test.ts +247 -0
  60. package/src/modules/git-workspace/branch-manager.ts +191 -0
  61. package/src/modules/git-workspace/git-exec.ts +124 -0
  62. package/src/modules/git-workspace/index.ts +17 -0
  63. package/src/modules/git-workspace/memory-sync.ts +89 -0
  64. package/src/modules/git-workspace/merge-service.ts +156 -0
  65. package/src/modules/git-workspace/settings-merge.ts +95 -0
  66. package/src/modules/git-workspace/worktree-manager.ts +199 -0
  67. package/src/modules/logging/__tests__/log-store.test.ts +242 -0
  68. package/src/modules/logging/__tests__/logger.test.ts +81 -0
  69. package/src/modules/logging/__tests__/sse-transport.test.ts +93 -0
  70. package/src/modules/logging/index.ts +7 -0
  71. package/src/modules/logging/log-store.ts +80 -0
  72. package/src/modules/logging/logger.ts +55 -0
  73. package/src/modules/logging/transports/sse-transport.ts +28 -0
  74. package/src/modules/multi-repo/__tests__/multi-repo-planner.test.ts +93 -0
  75. package/src/modules/multi-repo/__tests__/repo-context.test.ts +79 -0
  76. package/src/modules/multi-repo/index.ts +12 -0
  77. package/src/modules/multi-repo/multi-repo-planner.ts +112 -0
  78. package/src/modules/multi-repo/repo-context.ts +71 -0
  79. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/progress.json +10 -0
  80. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/queue/completed/task-getall-2.json +10 -0
  81. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/queue/pending/task-1.json +13 -0
  82. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/queue/pending/task-getall-1.json +10 -0
  83. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/queue/pending/task-status-change.json +10 -0
  84. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/workers/worker-1.json +5 -0
  85. package/src/modules/persistence/__tests__/.tmp-stores-test-81991/workers/worker-2.json +5 -0
  86. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/progress.json +10 -0
  87. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/queue/completed/task-getall-2.json +10 -0
  88. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/queue/pending/task-1.json +13 -0
  89. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/queue/pending/task-getall-1.json +10 -0
  90. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/queue/pending/task-status-change.json +10 -0
  91. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/workers/worker-1.json +5 -0
  92. package/src/modules/persistence/__tests__/.tmp-stores-test-82469/workers/worker-2.json +5 -0
  93. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/progress.json +10 -0
  94. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/queue/completed/task-getall-2.json +10 -0
  95. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/queue/pending/task-1.json +13 -0
  96. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/queue/pending/task-getall-1.json +10 -0
  97. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/queue/pending/task-status-change.json +10 -0
  98. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/workers/worker-1.json +5 -0
  99. package/src/modules/persistence/__tests__/.tmp-stores-test-85301/workers/worker-2.json +5 -0
  100. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/progress.json +10 -0
  101. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/queue/completed/task-getall-2.json +10 -0
  102. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/queue/pending/task-1.json +13 -0
  103. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/queue/pending/task-getall-1.json +10 -0
  104. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/queue/pending/task-status-change.json +10 -0
  105. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/workers/worker-1.json +5 -0
  106. package/src/modules/persistence/__tests__/.tmp-stores-test-85759/workers/worker-2.json +5 -0
  107. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/progress.json +10 -0
  108. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/queue/completed/task-getall-2.json +10 -0
  109. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/queue/pending/task-1.json +13 -0
  110. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/queue/pending/task-getall-1.json +10 -0
  111. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/queue/pending/task-status-change.json +10 -0
  112. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/workers/worker-1.json +5 -0
  113. package/src/modules/persistence/__tests__/.tmp-stores-test-86184/workers/worker-2.json +5 -0
  114. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/progress.json +10 -0
  115. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/queue/completed/task-getall-2.json +10 -0
  116. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/queue/pending/task-1.json +13 -0
  117. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/queue/pending/task-getall-1.json +10 -0
  118. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/queue/pending/task-status-change.json +10 -0
  119. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/workers/worker-1.json +5 -0
  120. package/src/modules/persistence/__tests__/.tmp-stores-test-88026/workers/worker-2.json +5 -0
  121. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/progress.json +10 -0
  122. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/queue/completed/task-getall-2.json +10 -0
  123. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/queue/pending/task-1.json +13 -0
  124. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/queue/pending/task-getall-1.json +10 -0
  125. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/queue/pending/task-status-change.json +10 -0
  126. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/workers/worker-1.json +5 -0
  127. package/src/modules/persistence/__tests__/.tmp-stores-test-89475/workers/worker-2.json +5 -0
  128. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/progress.json +10 -0
  129. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/queue/completed/task-getall-2.json +10 -0
  130. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/queue/pending/task-1.json +13 -0
  131. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/queue/pending/task-getall-1.json +10 -0
  132. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/queue/pending/task-status-change.json +10 -0
  133. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/workers/worker-1.json +5 -0
  134. package/src/modules/persistence/__tests__/.tmp-stores-test-89924/workers/worker-2.json +5 -0
  135. package/src/modules/persistence/__tests__/file-lock.test.ts +141 -0
  136. package/src/modules/persistence/__tests__/index.test.ts +38 -0
  137. package/src/modules/persistence/__tests__/stores.test.ts +594 -0
  138. package/src/modules/persistence/file-lock.ts +158 -0
  139. package/src/modules/persistence/fs-run-store.ts +73 -0
  140. package/src/modules/persistence/fs-task-store.ts +152 -0
  141. package/src/modules/persistence/fs-worker-store.ts +116 -0
  142. package/src/modules/persistence/in-memory-stores.ts +98 -0
  143. package/src/modules/persistence/index.ts +60 -0
  144. package/src/modules/persistence/stores.port.ts +60 -0
  145. package/src/modules/planning/__tests__/file-conflict-validator.test.ts +256 -0
  146. package/src/modules/planning/__tests__/planning-service.test.ts +366 -0
  147. package/src/modules/planning/__tests__/project-detection.test.ts +707 -0
  148. package/src/modules/planning/file-conflict-validator.ts +135 -0
  149. package/src/modules/planning/index.ts +40 -0
  150. package/src/modules/planning/planning.service.ts +262 -0
  151. package/src/modules/planning/project-detection.ts +525 -0
  152. package/src/modules/plugin/__tests__/plugin-loader.test.ts +83 -0
  153. package/src/modules/plugin/__tests__/plugin-manager.test.ts +187 -0
  154. package/src/modules/plugin/index.ts +3 -0
  155. package/src/modules/plugin/plugin-loader.ts +46 -0
  156. package/src/modules/plugin/plugin-manager.ts +90 -0
  157. package/src/modules/plugin/plugin.types.ts +37 -0
  158. package/src/modules/process-manager/__tests__/process-manager.test.ts +210 -0
  159. package/src/modules/process-manager/__tests__/worker.test.ts +89 -0
  160. package/src/modules/process-manager/index.ts +5 -0
  161. package/src/modules/process-manager/process-manager.ts +193 -0
  162. package/src/modules/process-manager/worker.ts +106 -0
  163. package/src/modules/task-execution/__tests__/default-spawner.test.ts +154 -0
  164. package/src/modules/task-execution/__tests__/executor.test.ts +760 -0
  165. package/src/modules/task-execution/__tests__/implementer-green.test.ts +286 -0
  166. package/src/modules/task-execution/__tests__/merge-phase.test.ts +368 -0
  167. package/src/modules/task-execution/__tests__/reviewer.test.ts +302 -0
  168. package/src/modules/task-execution/__tests__/tester-red.test.ts +281 -0
  169. package/src/modules/task-execution/__tests__/tester-verify.test.ts +313 -0
  170. package/src/modules/task-execution/executor.ts +303 -0
  171. package/src/modules/task-execution/index.ts +45 -0
  172. package/src/modules/task-execution/phases/default-spawner.ts +49 -0
  173. package/src/modules/task-execution/phases/implementer-green.ts +100 -0
  174. package/src/modules/task-execution/phases/merge.ts +122 -0
  175. package/src/modules/task-execution/phases/reviewer.ts +160 -0
  176. package/src/modules/task-execution/phases/tester-red.ts +100 -0
  177. package/src/modules/task-execution/phases/tester-verify.ts +120 -0
  178. package/src/modules/task-queue/__tests__/dependency-resolver.test.ts +456 -0
  179. package/src/modules/task-queue/__tests__/dispatcher.test.ts +824 -0
  180. package/src/modules/task-queue/__tests__/task-plan.test.ts +122 -0
  181. package/src/modules/task-queue/__tests__/task.test.ts +130 -0
  182. package/src/modules/task-queue/dependency-resolver.ts +171 -0
  183. package/src/modules/task-queue/dispatcher.ts +372 -0
  184. package/src/modules/task-queue/index.ts +16 -0
  185. package/src/modules/task-queue/task-plan.ts +40 -0
  186. package/src/modules/task-queue/task.ts +67 -0
  187. package/src/shared/__tests__/config.test.ts +204 -0
  188. package/src/shared/__tests__/errors.test.ts +285 -0
  189. package/src/shared/__tests__/events.test.ts +496 -0
  190. package/src/shared/__tests__/types.test.ts +360 -0
  191. package/src/shared/config.ts +133 -0
  192. package/src/shared/errors.ts +128 -0
  193. package/src/shared/events.ts +171 -0
  194. package/src/shared/types.ts +143 -0
  195. package/tsconfig.json +30 -0
@@ -0,0 +1,122 @@
1
+ import { describe, test, expect, beforeEach, afterEach } from "bun:test";
2
+ import { rm, mkdir } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { parseTaskPlan, loadTaskPlan } from "../task-plan";
5
+
6
+ const TEST_DIR = join(import.meta.dir, ".tmp-task-plan-test");
7
+
8
+ describe("task-plan", () => {
9
+ beforeEach(async () => {
10
+ await rm(TEST_DIR, { recursive: true, force: true });
11
+ await mkdir(TEST_DIR, { recursive: true });
12
+ });
13
+
14
+ afterEach(async () => {
15
+ await rm(TEST_DIR, { recursive: true, force: true });
16
+ });
17
+
18
+ describe("parseTaskPlan", () => {
19
+ test("parses snake_case task plan JSON", () => {
20
+ const json = {
21
+ run_id: "run-123",
22
+ parent_branch: "main",
23
+ title: "Test Plan",
24
+ description: "A test plan",
25
+ tasks: [
26
+ {
27
+ task_id: "task-1",
28
+ title: "Task 1",
29
+ description: "First task",
30
+ files_to_modify: ["file1.ts"],
31
+ depends_on: [],
32
+ priority: 1,
33
+ },
34
+ {
35
+ task_id: "task-2",
36
+ title: "Task 2",
37
+ description: "Second task",
38
+ files_to_modify: ["file2.ts"],
39
+ depends_on: ["task-1"],
40
+ priority: 2,
41
+ },
42
+ ],
43
+ };
44
+
45
+ const plan = parseTaskPlan(json);
46
+
47
+ expect(plan.runId as string).toBe("run-123");
48
+ expect(plan.parentBranch).toBe("main");
49
+ expect(plan.title).toBe("Test Plan");
50
+ expect(plan.tasks).toHaveLength(2);
51
+ expect(plan.tasks[0]?.taskId as string).toBe("task-1");
52
+ expect(plan.tasks[1]?.dependsOn.map((id) => id as string)).toEqual(["task-1"]);
53
+ });
54
+
55
+ test("handles minimal task plan", () => {
56
+ const json = {
57
+ run_id: "run-456",
58
+ parent_branch: "develop",
59
+ tasks: [],
60
+ };
61
+
62
+ const plan = parseTaskPlan(json);
63
+
64
+ expect(plan.runId as string).toBe("run-456");
65
+ expect(plan.parentBranch).toBe("develop");
66
+ expect(plan.title).toBeUndefined();
67
+ expect(plan.description).toBeUndefined();
68
+ expect(plan.tasks).toHaveLength(0);
69
+ });
70
+
71
+ test("throws on invalid JSON", () => {
72
+ const invalidJson = {
73
+ run_id: "",
74
+ tasks: [],
75
+ };
76
+
77
+ expect(() => parseTaskPlan(invalidJson)).toThrow();
78
+ });
79
+ });
80
+
81
+ describe("loadTaskPlan", () => {
82
+ test("loads and parses task plan from file", async () => {
83
+ const taskPlanPath = join(TEST_DIR, "task_plan.json");
84
+ const json = {
85
+ run_id: "run-789",
86
+ parent_branch: "feature/test",
87
+ title: "File Test Plan",
88
+ tasks: [
89
+ {
90
+ task_id: "task-1",
91
+ title: "Task 1",
92
+ description: "Task from file",
93
+ files_to_modify: [],
94
+ depends_on: [],
95
+ priority: 1,
96
+ },
97
+ ],
98
+ };
99
+
100
+ await Bun.write(taskPlanPath, JSON.stringify(json, null, 2));
101
+
102
+ const plan = await loadTaskPlan(taskPlanPath);
103
+
104
+ expect(plan.runId as string).toBe("run-789");
105
+ expect(plan.title).toBe("File Test Plan");
106
+ expect(plan.tasks).toHaveLength(1);
107
+ });
108
+
109
+ test("throws on non-existent file", async () => {
110
+ const nonExistentPath = join(TEST_DIR, "non-existent.json");
111
+
112
+ await expect(loadTaskPlan(nonExistentPath)).rejects.toThrow();
113
+ });
114
+
115
+ test("throws on invalid JSON file", async () => {
116
+ const invalidPath = join(TEST_DIR, "invalid.json");
117
+ await Bun.write(invalidPath, "{ invalid json");
118
+
119
+ await expect(loadTaskPlan(invalidPath)).rejects.toThrow();
120
+ });
121
+ });
122
+ });
@@ -0,0 +1,130 @@
1
+ import { describe, test, expect } from "bun:test";
2
+ import { parseTask, serializeTask } from "../task";
3
+ import { createTaskId } from "@aad/shared/types";
4
+
5
+ describe("task", () => {
6
+ describe("parseTask", () => {
7
+ test("parses snake_case JSON to Task", () => {
8
+ const json = {
9
+ task_id: "task-1",
10
+ title: "Test Task",
11
+ description: "A test task",
12
+ files_to_modify: ["file1.ts", "file2.ts"],
13
+ depends_on: ["task-0"],
14
+ priority: 1,
15
+ status: "pending",
16
+ retry_count: 0,
17
+ };
18
+
19
+ const task = parseTask(json);
20
+
21
+ expect(task.taskId as string).toBe("task-1");
22
+ expect(task.title).toBe("Test Task");
23
+ expect(task.filesToModify).toEqual(["file1.ts", "file2.ts"]);
24
+ expect(task.dependsOn.map((id) => id as string)).toEqual(["task-0"]);
25
+ expect(task.priority).toBe(1);
26
+ expect(task.status).toBe("pending");
27
+ expect(task.retryCount).toBe(0);
28
+ });
29
+
30
+ test("handles optional fields", () => {
31
+ const json = {
32
+ task_id: "task-2",
33
+ title: "Task 2",
34
+ description: "Running task",
35
+ files_to_modify: [],
36
+ depends_on: [],
37
+ priority: 2,
38
+ status: "running",
39
+ worker_id: "worker-1",
40
+ start_time: "2025-01-01T00:00:00Z",
41
+ retry_count: 1,
42
+ failure_reason: "Previous timeout",
43
+ };
44
+
45
+ const task = parseTask(json);
46
+
47
+ expect(task.workerId as string | undefined).toBe("worker-1");
48
+ expect(task.startTime).toBe("2025-01-01T00:00:00Z");
49
+ expect(task.retryCount).toBe(1);
50
+ expect(task.failureReason).toBe("Previous timeout");
51
+ });
52
+
53
+ test("defaults status to pending", () => {
54
+ const json = {
55
+ task_id: "task-3",
56
+ title: "Task 3",
57
+ description: "Task",
58
+ files_to_modify: [],
59
+ depends_on: [],
60
+ priority: 1,
61
+ retry_count: 0,
62
+ };
63
+
64
+ const task = parseTask(json);
65
+ expect(task.status).toBe("pending");
66
+ expect(task.retryCount).toBe(0);
67
+ });
68
+
69
+ test("throws on invalid JSON", () => {
70
+ const invalidJson = {
71
+ task_id: "",
72
+ title: "Task",
73
+ description: "",
74
+ files_to_modify: [],
75
+ depends_on: [],
76
+ priority: 1,
77
+ };
78
+
79
+ expect(() => parseTask(invalidJson)).toThrow();
80
+ });
81
+ });
82
+
83
+ describe("serializeTask", () => {
84
+ test("serializes Task to snake_case JSON", () => {
85
+ const task = {
86
+ taskId: createTaskId("task-1"),
87
+ title: "Test Task",
88
+ description: "A test task",
89
+ filesToModify: ["file1.ts"],
90
+ dependsOn: [createTaskId("task-0")],
91
+ priority: 1,
92
+ status: "completed" as const,
93
+ workerId: "worker-1" as any,
94
+ startTime: "2025-01-01T00:00:00Z",
95
+ endTime: "2025-01-01T00:10:00Z",
96
+ retryCount: 0,
97
+ };
98
+
99
+ const json = serializeTask(task);
100
+
101
+ expect(json.task_id).toBe("task-1");
102
+ expect(json.files_to_modify).toEqual(["file1.ts"]);
103
+ expect(json.depends_on).toEqual(["task-0"]);
104
+ expect(json.status).toBe("completed");
105
+ expect(json.worker_id).toBe("worker-1");
106
+ expect(json.start_time).toBe("2025-01-01T00:00:00Z");
107
+ expect(json.end_time).toBe("2025-01-01T00:10:00Z");
108
+ });
109
+
110
+ test("handles undefined optional fields", () => {
111
+ const task = {
112
+ taskId: createTaskId("task-2"),
113
+ title: "Task 2",
114
+ description: "Pending task",
115
+ filesToModify: [],
116
+ dependsOn: [],
117
+ priority: 1,
118
+ status: "pending" as const,
119
+ retryCount: 0,
120
+ };
121
+
122
+ const json = serializeTask(task);
123
+
124
+ expect(json.worker_id).toBeUndefined();
125
+ expect(json.start_time).toBeUndefined();
126
+ expect(json.end_time).toBeUndefined();
127
+ expect(json.failure_reason).toBeUndefined();
128
+ });
129
+ });
130
+ });
@@ -0,0 +1,171 @@
1
+ import type { Task, TaskId } from "@aad/shared/types";
2
+ import { CircularDependencyError } from "@aad/shared/errors";
3
+
4
+ export type DependencyStatus = "ready" | "waiting" | "blocked";
5
+
6
+ export interface DependencyResult {
7
+ status: DependencyStatus;
8
+ blockedBy: TaskId[];
9
+ reason?: string;
10
+ }
11
+
12
+ /**
13
+ * Check if a task's dependencies are satisfied
14
+ */
15
+ export function checkDependencies(
16
+ taskId: TaskId,
17
+ tasks: ReadonlyMap<TaskId, Task>
18
+ ): DependencyResult {
19
+ const task = tasks.get(taskId);
20
+
21
+ if (!task) {
22
+ return {
23
+ status: "blocked",
24
+ blockedBy: [],
25
+ reason: "Task not found",
26
+ };
27
+ }
28
+
29
+ if (task.dependsOn.length === 0) {
30
+ return {
31
+ status: "ready",
32
+ blockedBy: [],
33
+ };
34
+ }
35
+
36
+ const blockedBy: TaskId[] = [];
37
+
38
+ for (const depId of task.dependsOn) {
39
+ const depTask = tasks.get(depId);
40
+
41
+ if (!depTask) {
42
+ return {
43
+ status: "blocked",
44
+ blockedBy: [depId],
45
+ reason: `Dependency ${depId as string} not found`,
46
+ };
47
+ }
48
+
49
+ if (depTask.status === "failed") {
50
+ return {
51
+ status: "blocked",
52
+ blockedBy: [depId],
53
+ reason: `Dependency ${depId as string} failed`,
54
+ };
55
+ }
56
+
57
+ if (depTask.status !== "completed") {
58
+ blockedBy.push(depId);
59
+ }
60
+ }
61
+
62
+ if (blockedBy.length > 0) {
63
+ return {
64
+ status: "waiting",
65
+ blockedBy,
66
+ };
67
+ }
68
+
69
+ return {
70
+ status: "ready",
71
+ blockedBy: [],
72
+ };
73
+ }
74
+
75
+ /**
76
+ * Detect circular dependencies using DFS with 3-color algorithm
77
+ * Returns array of cycles if found, null if no cycles
78
+ */
79
+ export function detectCircularDependencies(
80
+ tasks: ReadonlyMap<TaskId, Task>
81
+ ): TaskId[][] | null {
82
+ type Color = "white" | "gray" | "black";
83
+ const colors = new Map<TaskId, Color>();
84
+ const cycles: TaskId[][] = [];
85
+
86
+ // Initialize all nodes as white (unvisited)
87
+ for (const taskId of tasks.keys()) {
88
+ colors.set(taskId, "white");
89
+ }
90
+
91
+ function dfs(taskId: TaskId, path: TaskId[]): void {
92
+ colors.set(taskId, "gray"); // Mark as being visited
93
+ path.push(taskId);
94
+
95
+ const task = tasks.get(taskId);
96
+ if (!task) {
97
+ colors.set(taskId, "black");
98
+ return;
99
+ }
100
+
101
+ for (const depId of task.dependsOn) {
102
+ const color = colors.get(depId);
103
+
104
+ if (color === "gray") {
105
+ // Back edge found - cycle detected
106
+ const cycleStart = path.indexOf(depId);
107
+ if (cycleStart !== -1) {
108
+ const cycle = path.slice(cycleStart);
109
+ cycle.push(depId); // Complete the cycle
110
+ cycles.push(cycle);
111
+ }
112
+ } else if (color === "white") {
113
+ // Continue DFS
114
+ dfs(depId, [...path]);
115
+ }
116
+ // If black, already visited and no cycle in this path
117
+ }
118
+
119
+ colors.set(taskId, "black"); // Mark as fully visited
120
+ }
121
+
122
+ // Visit all unvisited nodes
123
+ for (const taskId of tasks.keys()) {
124
+ if (colors.get(taskId) === "white") {
125
+ dfs(taskId, []);
126
+ }
127
+ }
128
+
129
+ return cycles.length > 0 ? cycles : null;
130
+ }
131
+
132
+ /**
133
+ * Get all tasks that are ready to execute (no pending dependencies)
134
+ */
135
+ export function getReadyTasks(tasks: ReadonlyMap<TaskId, Task>): Task[] {
136
+ const ready: Task[] = [];
137
+
138
+ for (const task of tasks.values()) {
139
+ if (task.status !== "pending") {
140
+ continue;
141
+ }
142
+
143
+ const result = checkDependencies(task.taskId, tasks);
144
+ if (result.status === "ready") {
145
+ ready.push(task);
146
+ }
147
+ }
148
+
149
+ // Sort by priority (higher priority first)
150
+ return ready.sort((a, b) => b.priority - a.priority);
151
+ }
152
+
153
+ /**
154
+ * Validate task plan for circular dependencies
155
+ * Throws CircularDependencyError if cycles detected
156
+ */
157
+ export function validateTaskPlan(tasks: Task[]): void {
158
+ const taskMap = new Map(tasks.map((task) => [task.taskId, task]));
159
+ const cycles = detectCircularDependencies(taskMap);
160
+
161
+ if (cycles) {
162
+ const cycleStrings = cycles.map((cycle) =>
163
+ cycle.map((id) => id as string).join(" -> ")
164
+ );
165
+
166
+ throw new CircularDependencyError("Circular dependencies detected", {
167
+ cycles: cycleStrings,
168
+ count: cycles.length,
169
+ });
170
+ }
171
+ }