@agentic-sdlc-kit/cli 0.0.0-stage → 0.8.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 (240) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +32 -2
  3. package/dist/application/agent/execution-store.d.ts +55 -0
  4. package/dist/application/agent/execution-store.d.ts.map +1 -0
  5. package/dist/application/agent/execution-store.js +90 -0
  6. package/dist/application/agent/execution-store.js.map +1 -0
  7. package/dist/application/agent/gateway.d.ts +74 -0
  8. package/dist/application/agent/gateway.d.ts.map +1 -0
  9. package/dist/application/agent/gateway.js +580 -0
  10. package/dist/application/agent/gateway.js.map +1 -0
  11. package/dist/application/agent/index.d.ts +6 -0
  12. package/dist/application/agent/index.d.ts.map +1 -0
  13. package/dist/application/agent/index.js +18 -0
  14. package/dist/application/agent/index.js.map +1 -0
  15. package/dist/application/agent/orchestrator.d.ts +76 -0
  16. package/dist/application/agent/orchestrator.d.ts.map +1 -0
  17. package/dist/application/agent/orchestrator.js +422 -0
  18. package/dist/application/agent/orchestrator.js.map +1 -0
  19. package/dist/application/agent/providers/claude-code/claude-argv.d.ts +15 -0
  20. package/dist/application/agent/providers/claude-code/claude-argv.d.ts.map +1 -0
  21. package/dist/application/agent/providers/claude-code/claude-argv.js +98 -0
  22. package/dist/application/agent/providers/claude-code/claude-argv.js.map +1 -0
  23. package/dist/application/agent/providers/claude-code/index.d.ts +7 -0
  24. package/dist/application/agent/providers/claude-code/index.d.ts.map +1 -0
  25. package/dist/application/agent/providers/claude-code/index.js +13 -0
  26. package/dist/application/agent/providers/claude-code/index.js.map +1 -0
  27. package/dist/application/agent/providers/claude-code/mod-parity.d.ts +15 -0
  28. package/dist/application/agent/providers/claude-code/mod-parity.d.ts.map +1 -0
  29. package/dist/application/agent/providers/claude-code/mod-parity.js +41 -0
  30. package/dist/application/agent/providers/claude-code/mod-parity.js.map +1 -0
  31. package/dist/application/agent/providers/claude-code/permission-config.d.ts +59 -0
  32. package/dist/application/agent/providers/claude-code/permission-config.d.ts.map +1 -0
  33. package/dist/application/agent/providers/claude-code/permission-config.js +189 -0
  34. package/dist/application/agent/providers/claude-code/permission-config.js.map +1 -0
  35. package/dist/application/agent/providers/claude-code/provider.d.ts +38 -0
  36. package/dist/application/agent/providers/claude-code/provider.d.ts.map +1 -0
  37. package/dist/application/agent/providers/claude-code/provider.js +577 -0
  38. package/dist/application/agent/providers/claude-code/provider.js.map +1 -0
  39. package/dist/application/agent/providers/claude-code/resume-store.d.ts +25 -0
  40. package/dist/application/agent/providers/claude-code/resume-store.d.ts.map +1 -0
  41. package/dist/application/agent/providers/claude-code/resume-store.js +120 -0
  42. package/dist/application/agent/providers/claude-code/resume-store.js.map +1 -0
  43. package/dist/application/agent/providers/claude-code/stream-parser.d.ts +50 -0
  44. package/dist/application/agent/providers/claude-code/stream-parser.d.ts.map +1 -0
  45. package/dist/application/agent/providers/claude-code/stream-parser.js +129 -0
  46. package/dist/application/agent/providers/claude-code/stream-parser.js.map +1 -0
  47. package/dist/application/agent/test-fixture.d.ts +15 -0
  48. package/dist/application/agent/test-fixture.d.ts.map +1 -0
  49. package/dist/application/agent/test-fixture.js +93 -0
  50. package/dist/application/agent/test-fixture.js.map +1 -0
  51. package/dist/application/agent/workspace.d.ts +24 -0
  52. package/dist/application/agent/workspace.d.ts.map +1 -0
  53. package/dist/application/agent/workspace.js +139 -0
  54. package/dist/application/agent/workspace.js.map +1 -0
  55. package/dist/application/dispatcher.d.ts +13 -0
  56. package/dist/application/dispatcher.d.ts.map +1 -0
  57. package/dist/application/dispatcher.js +104 -0
  58. package/dist/application/dispatcher.js.map +1 -0
  59. package/dist/application/index.d.ts +5 -0
  60. package/dist/application/index.d.ts.map +1 -0
  61. package/dist/application/index.js +6 -0
  62. package/dist/application/index.js.map +1 -0
  63. package/dist/application/registry.d.ts +8 -0
  64. package/dist/application/registry.d.ts.map +1 -0
  65. package/dist/application/registry.js +17 -0
  66. package/dist/application/registry.js.map +1 -0
  67. package/dist/application/version.d.ts +2 -0
  68. package/dist/application/version.d.ts.map +1 -0
  69. package/dist/application/version.js +7 -0
  70. package/dist/application/version.js.map +1 -0
  71. package/dist/commands/doctor.d.ts +3 -0
  72. package/dist/commands/doctor.d.ts.map +1 -0
  73. package/dist/commands/doctor.js +68 -0
  74. package/dist/commands/doctor.js.map +1 -0
  75. package/dist/commands/elicit.d.ts +3 -0
  76. package/dist/commands/elicit.d.ts.map +1 -0
  77. package/dist/commands/elicit.js +163 -0
  78. package/dist/commands/elicit.js.map +1 -0
  79. package/dist/commands/evolve.d.ts +3 -0
  80. package/dist/commands/evolve.d.ts.map +1 -0
  81. package/dist/commands/evolve.js +165 -0
  82. package/dist/commands/evolve.js.map +1 -0
  83. package/dist/commands/feature.d.ts +13 -0
  84. package/dist/commands/feature.d.ts.map +1 -0
  85. package/dist/commands/feature.js +326 -0
  86. package/dist/commands/feature.js.map +1 -0
  87. package/dist/commands/fix.d.ts +3 -0
  88. package/dist/commands/fix.d.ts.map +1 -0
  89. package/dist/commands/fix.js +61 -0
  90. package/dist/commands/fix.js.map +1 -0
  91. package/dist/commands/help.d.ts +4 -0
  92. package/dist/commands/help.d.ts.map +1 -0
  93. package/dist/commands/help.js +32 -0
  94. package/dist/commands/help.js.map +1 -0
  95. package/dist/commands/index.d.ts +21 -0
  96. package/dist/commands/index.d.ts.map +1 -0
  97. package/dist/commands/index.js +22 -0
  98. package/dist/commands/index.js.map +1 -0
  99. package/dist/commands/init.d.ts +3 -0
  100. package/dist/commands/init.d.ts.map +1 -0
  101. package/dist/commands/init.js +90 -0
  102. package/dist/commands/init.js.map +1 -0
  103. package/dist/commands/inspect.d.ts +3 -0
  104. package/dist/commands/inspect.d.ts.map +1 -0
  105. package/dist/commands/inspect.js +64 -0
  106. package/dist/commands/inspect.js.map +1 -0
  107. package/dist/commands/install.d.ts +3 -0
  108. package/dist/commands/install.d.ts.map +1 -0
  109. package/dist/commands/install.js +329 -0
  110. package/dist/commands/install.js.map +1 -0
  111. package/dist/commands/journal.d.ts +3 -0
  112. package/dist/commands/journal.d.ts.map +1 -0
  113. package/dist/commands/journal.js +107 -0
  114. package/dist/commands/journal.js.map +1 -0
  115. package/dist/commands/memory.d.ts +3 -0
  116. package/dist/commands/memory.d.ts.map +1 -0
  117. package/dist/commands/memory.js +85 -0
  118. package/dist/commands/memory.js.map +1 -0
  119. package/dist/commands/new.d.ts +3 -0
  120. package/dist/commands/new.d.ts.map +1 -0
  121. package/dist/commands/new.js +68 -0
  122. package/dist/commands/new.js.map +1 -0
  123. package/dist/commands/next.d.ts +3 -0
  124. package/dist/commands/next.d.ts.map +1 -0
  125. package/dist/commands/next.js +107 -0
  126. package/dist/commands/next.js.map +1 -0
  127. package/dist/commands/optimize.d.ts +3 -0
  128. package/dist/commands/optimize.d.ts.map +1 -0
  129. package/dist/commands/optimize.js +28 -0
  130. package/dist/commands/optimize.js.map +1 -0
  131. package/dist/commands/plugin-linker.d.ts +34 -0
  132. package/dist/commands/plugin-linker.d.ts.map +1 -0
  133. package/dist/commands/plugin-linker.js +177 -0
  134. package/dist/commands/plugin-linker.js.map +1 -0
  135. package/dist/commands/refactor.d.ts +3 -0
  136. package/dist/commands/refactor.d.ts.map +1 -0
  137. package/dist/commands/refactor.js +32 -0
  138. package/dist/commands/refactor.js.map +1 -0
  139. package/dist/commands/types.d.ts +19 -0
  140. package/dist/commands/types.d.ts.map +1 -0
  141. package/dist/commands/types.js +7 -0
  142. package/dist/commands/types.js.map +1 -0
  143. package/dist/commands/validate-plan.d.ts +3 -0
  144. package/dist/commands/validate-plan.d.ts.map +1 -0
  145. package/dist/commands/validate-plan.js +62 -0
  146. package/dist/commands/validate-plan.js.map +1 -0
  147. package/dist/commands/validate-spec.d.ts +3 -0
  148. package/dist/commands/validate-spec.d.ts.map +1 -0
  149. package/dist/commands/validate-spec.js +62 -0
  150. package/dist/commands/validate-spec.js.map +1 -0
  151. package/dist/commands/verify.d.ts +3 -0
  152. package/dist/commands/verify.d.ts.map +1 -0
  153. package/dist/commands/verify.js +103 -0
  154. package/dist/commands/verify.js.map +1 -0
  155. package/dist/commands/version.d.ts +3 -0
  156. package/dist/commands/version.d.ts.map +1 -0
  157. package/dist/commands/version.js +12 -0
  158. package/dist/commands/version.js.map +1 -0
  159. package/dist/index.d.ts +3 -0
  160. package/dist/index.d.ts.map +1 -0
  161. package/dist/index.js +27 -0
  162. package/dist/index.js.map +1 -0
  163. package/dist/output/formatter.d.ts +2 -0
  164. package/dist/output/formatter.d.ts.map +1 -0
  165. package/dist/output/formatter.js +10 -0
  166. package/dist/output/formatter.js.map +1 -0
  167. package/dist/output/index.d.ts +2 -0
  168. package/dist/output/index.d.ts.map +1 -0
  169. package/dist/output/index.js +3 -0
  170. package/dist/output/index.js.map +1 -0
  171. package/package.json +37 -3
  172. package/src/application/agent/execution-store.ts +162 -0
  173. package/src/application/agent/gateway.test.ts +529 -0
  174. package/src/application/agent/gateway.ts +744 -0
  175. package/src/application/agent/index.ts +18 -0
  176. package/src/application/agent/orchestrator.test.ts +341 -0
  177. package/src/application/agent/orchestrator.ts +628 -0
  178. package/src/application/agent/providers/claude-code/claude-argv.ts +119 -0
  179. package/src/application/agent/providers/claude-code/claude-code-smoke.test.ts +139 -0
  180. package/src/application/agent/providers/claude-code/fixtures/fake-claude.mjs +125 -0
  181. package/src/application/agent/providers/claude-code/index.ts +28 -0
  182. package/src/application/agent/providers/claude-code/mod-parity.ts +50 -0
  183. package/src/application/agent/providers/claude-code/permission-config.test.ts +221 -0
  184. package/src/application/agent/providers/claude-code/permission-config.ts +242 -0
  185. package/src/application/agent/providers/claude-code/provider.test.ts +378 -0
  186. package/src/application/agent/providers/claude-code/provider.ts +717 -0
  187. package/src/application/agent/providers/claude-code/resume-store.test.ts +161 -0
  188. package/src/application/agent/providers/claude-code/resume-store.ts +157 -0
  189. package/src/application/agent/providers/claude-code/stream-parser.ts +166 -0
  190. package/src/application/agent/test-fixture.ts +131 -0
  191. package/src/application/agent/workspace.test.ts +104 -0
  192. package/src/application/agent/workspace.ts +156 -0
  193. package/src/application/dispatcher.test.ts +89 -0
  194. package/src/application/dispatcher.ts +98 -0
  195. package/src/application/index.ts +5 -0
  196. package/src/application/registry.ts +24 -0
  197. package/src/application/version.ts +7 -0
  198. package/src/commands/clse.test.ts +181 -0
  199. package/src/commands/doctor.test.ts +29 -0
  200. package/src/commands/doctor.ts +78 -0
  201. package/src/commands/elicit.test.ts +111 -0
  202. package/src/commands/elicit.ts +188 -0
  203. package/src/commands/evolve.ts +209 -0
  204. package/src/commands/feature.test.ts +323 -0
  205. package/src/commands/feature.ts +405 -0
  206. package/src/commands/fix.test.ts +36 -0
  207. package/src/commands/fix.ts +69 -0
  208. package/src/commands/help.test.ts +33 -0
  209. package/src/commands/help.ts +35 -0
  210. package/src/commands/index.ts +21 -0
  211. package/src/commands/init.test.ts +65 -0
  212. package/src/commands/init.ts +103 -0
  213. package/src/commands/inspect.test.ts +57 -0
  214. package/src/commands/inspect.ts +73 -0
  215. package/src/commands/install.test.ts +61 -0
  216. package/src/commands/install.ts +375 -0
  217. package/src/commands/journal.ts +118 -0
  218. package/src/commands/memory.ts +96 -0
  219. package/src/commands/new.test.ts +44 -0
  220. package/src/commands/new.ts +77 -0
  221. package/src/commands/next.test.ts +241 -0
  222. package/src/commands/next.ts +124 -0
  223. package/src/commands/optimize.ts +30 -0
  224. package/src/commands/plugin-linker.test.ts +139 -0
  225. package/src/commands/plugin-linker.ts +196 -0
  226. package/src/commands/refactor.ts +34 -0
  227. package/src/commands/stack-commands.test.ts +67 -0
  228. package/src/commands/tier-c-conformance.test.ts +194 -0
  229. package/src/commands/types.ts +26 -0
  230. package/src/commands/validate-plan.test.ts +107 -0
  231. package/src/commands/validate-plan.ts +67 -0
  232. package/src/commands/validate-spec.test.ts +59 -0
  233. package/src/commands/validate-spec.ts +68 -0
  234. package/src/commands/verify.test.ts +117 -0
  235. package/src/commands/verify.ts +112 -0
  236. package/src/commands/version.test.ts +11 -0
  237. package/src/commands/version.ts +14 -0
  238. package/src/index.ts +30 -0
  239. package/src/output/formatter.ts +10 -0
  240. package/src/output/index.ts +2 -0
@@ -0,0 +1,18 @@
1
+ // packages/cli/src/application/agent/index.ts
2
+ //
3
+ // M36 Mode 1 (kit-orchestrated) execution surface, PR3+PR4 (ADR-0019 §4):
4
+ // the agent gateway, the execution store it persists to, the git-worktree
5
+ // workspace manager, the feature orchestrator that drives the EXISTING
6
+ // core TDD state machine through a configured provider, and — since PR4 —
7
+ // the secure Claude Code provider that fills the injection seam.
8
+ //
9
+ // No new packages (ADR-0019 §4): everything lives in the CLI application
10
+ // layer. Providers are INJECTED via AgentGatewayOptions; the shipped CLI
11
+ // registers claude-code (PR4), whose healthCheck fails honestly (exit 3,
12
+ // never a silent fallback) when the claude CLI is missing.
13
+
14
+ export * from './execution-store.js';
15
+ export * from './gateway.js';
16
+ export * from './workspace.js';
17
+ export * from './orchestrator.js';
18
+ export * from './providers/claude-code/index.js';
@@ -0,0 +1,341 @@
1
+ // packages/cli/src/application/agent/orchestrator.test.ts
2
+ //
3
+ // PR3 ACCEPTANCE (ADR-0019 §5): an integration test completes a simulated
4
+ // feature lifecycle through the mock provider WITHOUT duplicating core
5
+ // workflow logic. The orchestrator drives the SAME core APIs Mode 2 uses
6
+ // (startFeatureRun / runTddPhase / finalizeFeatureRun) against a real git
7
+ // worktree; the mock provider simulates the agent's edits by creating the
8
+ // per-task marker inside the workspace, exactly where a real provider's
9
+ // edits would land.
10
+ //
11
+ // Plus the honest-failure paths: provider not configured, agent failure
12
+ // stops the run, the evidence gate rejects a lying agent, cancellation,
13
+ // and cross-invocation resume.
14
+
15
+ import { execFile } from 'node:child_process';
16
+ import { promises as fs } from 'node:fs';
17
+ import path from 'node:path';
18
+ import { promisify } from 'node:util';
19
+ import { describe, expect, it } from 'vitest';
20
+ import { featureRunPath, getFeatureRunState } from '@agentic-sdlc-kit/core';
21
+ import { MockAgentProvider } from '@agentic-sdlc-kit/testing';
22
+ import { AgentGateway } from './gateway.js';
23
+ import { runFeatureOrchestration } from './orchestrator.js';
24
+ import { buildOrchestrationFixture, markerPath, type OrchestrationFixture } from './test-fixture.js';
25
+
26
+ const exec = promisify(execFile);
27
+
28
+ /**
29
+ * The simulated agent: on GREEN phases it "implements" the task by
30
+ * creating the per-task marker inside the execution workspace (a real
31
+ * provider would edit source files there). Red and refactor phases
32
+ * perform no writes — the red test already fails for the right reason,
33
+ * and refactor keeps behavior.
34
+ */
35
+ function workingAgent(): MockAgentProvider {
36
+ return new MockAgentProvider({
37
+ performWork: async (request) => {
38
+ if (request.instructions.includes('Phase: green')) {
39
+ await fs.writeFile(markerPath(request.workspace.rootPath, request.taskId), 'implemented\n');
40
+ }
41
+ },
42
+ });
43
+ }
44
+
45
+ function gatewayFor(fixture: OrchestrationFixture, provider: MockAgentProvider): AgentGateway {
46
+ return new AgentGateway({ providers: [provider], projectRoot: fixture.root });
47
+ }
48
+
49
+ describe('PR3 acceptance: full feature lifecycle through the mock provider', () => {
50
+ it('completes Red -> Green -> Refactor for every task and final verification, driving the SAME core state machine', async () => {
51
+ const fixture = await buildOrchestrationFixture();
52
+ const gateway = gatewayFor(fixture, workingAgent());
53
+
54
+ const res = await runFeatureOrchestration({
55
+ projectRoot: fixture.root,
56
+ featureId: fixture.featureId,
57
+ gateway,
58
+ providerId: 'mock',
59
+ });
60
+ expect(res.ok).toBe(true);
61
+ if (!res.ok) return;
62
+ const report = res.value;
63
+
64
+ // The run completed with passing final verification.
65
+ expect(report.status).toBe('completed');
66
+ expect(report.verification?.status).toBe('passed');
67
+ expect(report.runId).toBe('RUN-001');
68
+ expect(report.worktreePath).toBe(path.join(fixture.root, '.ai-kit', 'worktrees', 'RUN-001'));
69
+
70
+ // Every task completed.
71
+ expect(report.tasks.map((t) => [t.taskId, t.completed])).toEqual([
72
+ ['TASK-001', true],
73
+ ['TASK-002', true],
74
+ ]);
75
+
76
+ // Exactly one agent execution per task per phase, all completed, all
77
+ // linked to the workflow identity.
78
+ expect(report.executions.map((e) => [e.taskId, e.phase, e.outcome])).toEqual([
79
+ ['TASK-001', 'red', 'completed'],
80
+ ['TASK-001', 'green', 'completed'],
81
+ ['TASK-001', 'refactor', 'completed'],
82
+ ['TASK-002', 'red', 'completed'],
83
+ ['TASK-002', 'green', 'completed'],
84
+ ['TASK-002', 'refactor', 'completed'],
85
+ ]);
86
+ for (const e of report.executions) {
87
+ expect(e.executionId).toMatch(/^exec-RUN-001-TASK-00[12]-(red|green|refactor)-1$/);
88
+ }
89
+
90
+ // NO DUPLICATED WORKFLOW LOGIC: the evidence lives in the worktree's
91
+ // feature-run store — the exact file Mode 2 writes via the same core
92
+ // functions. Every task has a complete chain; every Red record is
93
+ // assertion-classified (the PR1/#9 gate).
94
+ const runStatePath = featureRunPath(report.worktreePath, fixture.featureId);
95
+ expect(await fs.readFile(runStatePath, 'utf8')).toContain('"schemaVersion": 2');
96
+ const runState = await getFeatureRunState(report.worktreePath, fixture.featureId);
97
+ expect(runState.ok).toBe(true);
98
+ if (runState.ok) {
99
+ for (const task of Object.values(runState.value.tasks)) {
100
+ expect(task.phase).toBe('completed');
101
+ const red = task.records.find((r) => r.phase === 'red' && r.status === 'failed');
102
+ expect(red?.verification?.classification).toBe('assertion');
103
+ const passes = task.records.filter((r) => r.status === 'passed');
104
+ expect(passes.map((r) => r.phase)).toEqual(['green', 'refactor']);
105
+ }
106
+ }
107
+
108
+ // The agent's edits (markers) live in the WORKTREE, not the main tree.
109
+ for (const taskId of fixture.taskIds) {
110
+ await expect(fs.access(markerPath(report.worktreePath, taskId))).resolves.toBeUndefined();
111
+ await expect(fs.access(markerPath(fixture.root, taskId))).rejects.toThrow();
112
+ }
113
+
114
+ // Every execution is durably recorded with its frozen request, policy
115
+ // snapshot, accepted events, and echoed policyHash.
116
+ const executions = await gateway.listExecutions();
117
+ expect(executions).toHaveLength(6);
118
+ for (const record of executions) {
119
+ expect(record.status).toBe('completed');
120
+ expect(record.events.length).toBeGreaterThan(0);
121
+ expect(record.policySnapshot.permissions.writablePaths).toEqual([report.worktreePath]);
122
+ for (const event of record.events) {
123
+ expect(event.policyHash).toBe(record.policyHash);
124
+ }
125
+ }
126
+
127
+ // The orchestration state links feature, run, workspace, executions.
128
+ const orchestration = JSON.parse(
129
+ await fs.readFile(
130
+ path.join(fixture.root, '.ai-kit', 'state', 'orchestrations', 'FEATURE-001.json'),
131
+ 'utf8',
132
+ ),
133
+ );
134
+ expect(orchestration.status).toBe('completed');
135
+ expect(orchestration.runId).toBe('RUN-001');
136
+ expect(orchestration.workspace.branch).toBe('kit/RUN-001');
137
+ expect(orchestration.executions).toHaveLength(6);
138
+ });
139
+
140
+ it('produces a clean diff on the worktree branch (baseCommitSha is derivable truth)', async () => {
141
+ const fixture = await buildOrchestrationFixture();
142
+ const gateway = gatewayFor(fixture, workingAgent());
143
+ const res = await runFeatureOrchestration({
144
+ projectRoot: fixture.root,
145
+ featureId: fixture.featureId,
146
+ gateway,
147
+ providerId: 'mock',
148
+ });
149
+ expect(res.ok).toBe(true);
150
+ if (!res.ok) return;
151
+ // Untracked markers are the agent's whole footprint on the branch.
152
+ const status = await exec('git', ['status', '--porcelain'], { cwd: res.value.worktreePath });
153
+ expect(status.stdout.split('\n').filter(Boolean).sort()).toEqual([
154
+ '?? .done-TASK-001',
155
+ '?? .done-TASK-002',
156
+ ]);
157
+ });
158
+ });
159
+
160
+ describe('orchestration honest failures', () => {
161
+ it('fails with AGENT_PROVIDER_NOT_CONFIGURED before touching the filesystem', async () => {
162
+ const fixture = await buildOrchestrationFixture();
163
+ const gateway = new AgentGateway({ providers: [], projectRoot: fixture.root });
164
+ const res = await runFeatureOrchestration({
165
+ projectRoot: fixture.root,
166
+ featureId: fixture.featureId,
167
+ gateway,
168
+ providerId: 'mock',
169
+ });
170
+ expect(res.ok).toBe(false);
171
+ if (!res.ok) {
172
+ expect(res.error.code).toBe('AGENT_PROVIDER_NOT_CONFIGURED');
173
+ expect(res.error.message).toContain('will not fall back');
174
+ }
175
+ // Nothing was created: no worktree, no orchestration state.
176
+ expect(await fs.access(path.join(fixture.root, '.ai-kit', 'worktrees')).catch(() => 'missing')).toBe('missing');
177
+ expect(
178
+ await fs
179
+ .access(path.join(fixture.root, '.ai-kit', 'state', 'orchestrations'))
180
+ .catch(() => 'missing'),
181
+ ).toBe('missing');
182
+ });
183
+
184
+ it('stops at the first failed agent execution and never advances the phase', async () => {
185
+ const fixture = await buildOrchestrationFixture();
186
+ const agent = new MockAgentProvider({ status: 'failed' });
187
+ const gateway = gatewayFor(fixture, agent);
188
+ const res = await runFeatureOrchestration({
189
+ projectRoot: fixture.root,
190
+ featureId: fixture.featureId,
191
+ gateway,
192
+ providerId: 'mock',
193
+ });
194
+ expect(res.ok).toBe(true);
195
+ if (!res.ok) return;
196
+ expect(res.value.status).toBe('failed');
197
+ expect(res.value.executions).toHaveLength(1);
198
+ expect(res.value.executions[0]?.outcome).toBe('failed');
199
+ // The task never left red, and no evidence was recorded.
200
+ const runState = await getFeatureRunState(res.value.worktreePath, fixture.featureId);
201
+ expect(runState.ok && runState.value.tasks['TASK-001']?.phase).toBe('red');
202
+ expect(runState.ok && runState.value.tasks['TASK-001']?.records).toHaveLength(0);
203
+ });
204
+
205
+ it('rejects a lying agent: completed claim, no evidence (the gate is the authority)', async () => {
206
+ const fixture = await buildOrchestrationFixture();
207
+ // The agent "completes" every execution but never implements anything.
208
+ const liar = new MockAgentProvider();
209
+ const gateway = gatewayFor(fixture, liar);
210
+ const res = await runFeatureOrchestration({
211
+ projectRoot: fixture.root,
212
+ featureId: fixture.featureId,
213
+ gateway,
214
+ providerId: 'mock',
215
+ });
216
+ expect(res.ok).toBe(true);
217
+ if (!res.ok) return;
218
+ // Red passes honestly (the test fails for the right reason), then the
219
+ // agent claims green without implementing: runTddPhase's verification
220
+ // fails, the phase gate rejects, the orchestration stops.
221
+ expect(res.value.status).toBe('phase_gate_rejected');
222
+ expect(res.value.message).toContain('evidence gate rejected');
223
+ expect(res.value.executions.map((e) => e.phase)).toEqual(['red', 'green']);
224
+ const runState = await getFeatureRunState(res.value.worktreePath, fixture.featureId);
225
+ // TASK-001 is stuck in green with one red record and one failed green attempt.
226
+ expect(runState.ok && runState.value.tasks['TASK-001']?.phase).toBe('green');
227
+ if (runState.ok) {
228
+ const records = runState.value.tasks['TASK-001']?.records ?? [];
229
+ expect(records.map((r) => `${r.phase}:${r.status}`)).toEqual(['red:failed', 'green:failed']);
230
+ }
231
+ });
232
+
233
+ it('reports needs_approval as a blocked outcome (never silent)', async () => {
234
+ const fixture = await buildOrchestrationFixture();
235
+ const agent = new MockAgentProvider({ status: 'needs_approval' });
236
+ const gateway = gatewayFor(fixture, agent);
237
+ const res = await runFeatureOrchestration({
238
+ projectRoot: fixture.root,
239
+ featureId: fixture.featureId,
240
+ gateway,
241
+ providerId: 'mock',
242
+ });
243
+ expect(res.ok).toBe(true);
244
+ if (res.ok) expect(res.value.status).toBe('needs_approval');
245
+ });
246
+ });
247
+
248
+ describe('cancellation and resumable state', () => {
249
+ it('cancels an in-flight execution on abort and resumes from the pending phase on the next invocation', async () => {
250
+ const fixture = await buildOrchestrationFixture();
251
+
252
+ // First invocation: a slow agent gets cancelled mid-execution. The
253
+ // abort is triggered from onProgress (after the orchestrator has
254
+ // recorded the execution and dispatched it to the gateway) rather than
255
+ // a wall-clock timer: on macOS, `git worktree add` + run-state setup
256
+ // exceeds a 100ms timer, so the abort landed before the first execution
257
+ // was recorded and `executions` came back empty. Triggering from
258
+ // onProgress makes the cancellation deterministic across OSes.
259
+ const slowAgent = new MockAgentProvider({
260
+ delayMs: 5_000,
261
+ performWork: async (request) => {
262
+ if (request.instructions.includes('Phase: green')) {
263
+ await fs.writeFile(markerPath(request.workspace.rootPath, request.taskId), 'implemented\n');
264
+ }
265
+ },
266
+ });
267
+ const controller = new AbortController();
268
+ let dispatchedFirstExecution = false;
269
+ const first = await runFeatureOrchestration({
270
+ projectRoot: fixture.root,
271
+ featureId: fixture.featureId,
272
+ gateway: gatewayFor(fixture, slowAgent),
273
+ providerId: 'mock',
274
+ signal: controller.signal,
275
+ onProgress: (line) => {
276
+ if (!dispatchedFirstExecution && line.includes('agent execution')) {
277
+ dispatchedFirstExecution = true;
278
+ // Defer one tick so the gateway's start() await is in flight
279
+ // (the 5s delay) before the abort propagates — mid-execution.
280
+ setImmediate(() => controller.abort());
281
+ }
282
+ },
283
+ });
284
+ expect(first.ok).toBe(true);
285
+ if (!first.ok) return;
286
+ expect(first.value.status).toBe('cancelled');
287
+ expect(first.value.executions).toHaveLength(1);
288
+ expect(first.value.executions[0]?.outcome).toBe('cancelled');
289
+
290
+ // The worktree survived with its run state (red attempt was NOT yet
291
+ // recorded — the cancelled execution never reached the gate).
292
+ const worktreePath = first.value.worktreePath;
293
+
294
+ // Second invocation: a NEW gateway (fresh process) and a working agent
295
+ // resume from the first pending phase and complete the feature.
296
+ const second = await runFeatureOrchestration({
297
+ projectRoot: fixture.root,
298
+ featureId: fixture.featureId,
299
+ gateway: gatewayFor(fixture, workingAgent()),
300
+ providerId: 'mock',
301
+ });
302
+ expect(second.ok).toBe(true);
303
+ if (!second.ok) return;
304
+ expect(second.value.status).toBe('completed');
305
+ expect(second.value.worktreePath).toBe(worktreePath); // same workspace reused
306
+ // The second invocation's report carries the FULL execution history
307
+ // (the cancelled attempt is durable evidence, not erased): the
308
+ // cancelled red was attempt 1; the resumed red is attempt 2.
309
+ expect(second.value.executions.map((e) => e.executionId)).toEqual([
310
+ 'exec-RUN-001-TASK-001-red-1',
311
+ 'exec-RUN-001-TASK-001-red-2',
312
+ 'exec-RUN-001-TASK-001-green-1',
313
+ 'exec-RUN-001-TASK-001-refactor-1',
314
+ 'exec-RUN-001-TASK-002-red-1',
315
+ 'exec-RUN-001-TASK-002-green-1',
316
+ 'exec-RUN-001-TASK-002-refactor-1',
317
+ ]);
318
+ expect(second.value.executions[0]?.outcome).toBe('cancelled');
319
+ expect(second.value.executions[1]?.outcome).toBe('completed');
320
+ });
321
+
322
+ it('refuses to re-run a completed orchestration (evidence is not discardable)', async () => {
323
+ const fixture = await buildOrchestrationFixture();
324
+ const gateway = gatewayFor(fixture, workingAgent());
325
+ const first = await runFeatureOrchestration({
326
+ projectRoot: fixture.root,
327
+ featureId: fixture.featureId,
328
+ gateway,
329
+ providerId: 'mock',
330
+ });
331
+ expect(first.ok).toBe(true);
332
+ const again = await runFeatureOrchestration({
333
+ projectRoot: fixture.root,
334
+ featureId: fixture.featureId,
335
+ gateway: gatewayFor(fixture, workingAgent()),
336
+ providerId: 'mock',
337
+ });
338
+ expect(again.ok).toBe(false);
339
+ if (!again.ok) expect(again.error.message).toContain('already completed');
340
+ });
341
+ });