karajan-code 1.49.0 → 1.50.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/docs/README.es.md +1 -1
- package/package.json +1 -1
- package/src/mcp/handlers/direct-handlers.js +393 -0
- package/src/mcp/handlers/hu-handlers.js +93 -0
- package/src/mcp/handlers/management-handlers.js +182 -0
- package/src/mcp/handlers/run-handler.js +283 -0
- package/src/mcp/server-handlers.js +40 -870
- package/src/orchestrator/iteration-stages.js +9 -890
- package/src/orchestrator/pre-loop-stages.js +8 -906
- package/src/orchestrator/preflight-checks.js +3 -1
- package/src/orchestrator/solomon-escalation.js +34 -10
- package/src/orchestrator/stages/architect-stage.js +176 -0
- package/src/orchestrator/stages/coder-stage.js +307 -0
- package/src/orchestrator/stages/hu-reviewer-stage.js +467 -0
- package/src/orchestrator/stages/planner-stage.js +90 -0
- package/src/orchestrator/stages/research-stage.js +74 -0
- package/src/orchestrator/stages/reviewer-stage.js +350 -0
- package/src/orchestrator/stages/sonar-stage.js +267 -0
- package/src/orchestrator/stages/triage-stage.js +137 -0
- package/src/orchestrator.js +25 -9
- package/src/planning-game/decomposition.js +7 -5
- package/src/planning-game/pipeline-adapter.js +6 -2
- package/src/utils/messages.js +61 -0
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HU Reviewer stage logic + splitting logic.
|
|
3
|
+
* Extracted from pre-loop-stages.js for maintainability.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { DiscoverRole } from "../../roles/discover-role.js";
|
|
7
|
+
import { HuReviewerRole } from "../../roles/hu-reviewer-role.js";
|
|
8
|
+
import { createAgent } from "../../agents/index.js";
|
|
9
|
+
import { addCheckpoint } from "../../session-store.js";
|
|
10
|
+
import { emitProgress, makeEvent } from "../../utils/events.js";
|
|
11
|
+
import { buildDecompositionPrompt, parseDecompositionOutput } from "../../prompts/hu-reviewer.js";
|
|
12
|
+
import { createStallDetector } from "../../utils/stall-detector.js";
|
|
13
|
+
import { createHuBatch, loadHuBatch, saveHuBatch, updateStoryStatus, updateStoryQuality, updateStoryCertified, addContextRequest, answerContextRequest } from "../../hu/store.js";
|
|
14
|
+
import { topologicalSort } from "../../hu/graph.js";
|
|
15
|
+
import { detectIndicators, selectHeuristic } from "../../hu/splitting-detector.js";
|
|
16
|
+
import { generateSplitProposal, formatSplitProposalForFDE, buildSplitDependencies } from "../../hu/splitting-generator.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Use an AI agent to decompose a complex task into multiple formal HUs.
|
|
20
|
+
* Returns null if decomposition fails or the task is too simple.
|
|
21
|
+
*/
|
|
22
|
+
async function decomposeTaskIntoHUs({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
23
|
+
const provider = config?.roles?.hu_reviewer?.provider || coderRole.provider;
|
|
24
|
+
const prompt = buildDecompositionPrompt(session.task);
|
|
25
|
+
|
|
26
|
+
emitProgress(
|
|
27
|
+
emitter,
|
|
28
|
+
makeEvent("hu-reviewer:decompose-start", { ...eventBase, stage: "hu-reviewer" }, {
|
|
29
|
+
message: "Decomposing complex task into formal HUs",
|
|
30
|
+
detail: { provider }
|
|
31
|
+
})
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const onOutput = ({ stream, line }) => {
|
|
35
|
+
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "hu-reviewer" }, {
|
|
36
|
+
message: line,
|
|
37
|
+
detail: { stream, agent: provider }
|
|
38
|
+
}));
|
|
39
|
+
};
|
|
40
|
+
const stall = createStallDetector({
|
|
41
|
+
onOutput, emitter, eventBase, stage: "hu-reviewer", provider
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
const agent = createAgent(provider, config, logger);
|
|
45
|
+
const decompStart = Date.now();
|
|
46
|
+
let result;
|
|
47
|
+
try {
|
|
48
|
+
result = await agent.runTask({ prompt, role: "hu-reviewer", onOutput: stall.onOutput });
|
|
49
|
+
} catch (err) {
|
|
50
|
+
logger.warn(`HU decomposition threw: ${err.message}`);
|
|
51
|
+
return null;
|
|
52
|
+
} finally {
|
|
53
|
+
stall.stop();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
trackBudget({
|
|
57
|
+
role: "hu-reviewer-decompose",
|
|
58
|
+
provider,
|
|
59
|
+
model: config?.roles?.hu_reviewer?.model || coderRole.model,
|
|
60
|
+
result,
|
|
61
|
+
duration_ms: Date.now() - decompStart
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (!result.ok) {
|
|
65
|
+
logger.warn(`HU decomposition failed: ${result.error || "unknown"}`);
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const stories = parseDecompositionOutput(result.output);
|
|
70
|
+
if (!stories || stories.length < 2) {
|
|
71
|
+
logger.info("HU decomposition returned < 2 stories, falling back to single auto-story");
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
emitProgress(
|
|
76
|
+
emitter,
|
|
77
|
+
makeEvent("hu-reviewer:decompose-end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
78
|
+
message: `Task decomposed into ${stories.length} HUs`,
|
|
79
|
+
detail: { count: stories.length, ids: stories.map(s => s.id) }
|
|
80
|
+
})
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
return stories;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export async function runDiscoverStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
87
|
+
logger.setContext({ iteration: 0, stage: "discover" });
|
|
88
|
+
const discoverProvider = config?.roles?.discover?.provider || coderRole.provider;
|
|
89
|
+
emitProgress(
|
|
90
|
+
emitter,
|
|
91
|
+
makeEvent("discover:start", { ...eventBase, stage: "discover" }, {
|
|
92
|
+
message: "Discover analyzing task for gaps",
|
|
93
|
+
detail: { discover: discoverProvider, provider: discoverProvider, executorType: "agent" }
|
|
94
|
+
})
|
|
95
|
+
);
|
|
96
|
+
const discoverOnOutput = ({ stream, line }) => {
|
|
97
|
+
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "discover" }, {
|
|
98
|
+
message: line,
|
|
99
|
+
detail: { stream, agent: discoverProvider }
|
|
100
|
+
}));
|
|
101
|
+
};
|
|
102
|
+
const discoverStall = createStallDetector({
|
|
103
|
+
onOutput: discoverOnOutput, emitter, eventBase, stage: "discover", provider: discoverProvider
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const mode = config?.pipeline?.discover?.mode || "gaps";
|
|
107
|
+
const discover = new DiscoverRole({ config, logger, emitter });
|
|
108
|
+
await discover.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
109
|
+
const discoverStart = Date.now();
|
|
110
|
+
let discoverOutput;
|
|
111
|
+
try {
|
|
112
|
+
discoverOutput = await discover.run({ task: session.task, mode, onOutput: discoverStall.onOutput });
|
|
113
|
+
} catch (err) {
|
|
114
|
+
logger.warn(`Discover threw: ${err.message}`);
|
|
115
|
+
discoverOutput = { ok: false, summary: `Discover error: ${err.message}`, result: { error: err.message } };
|
|
116
|
+
} finally {
|
|
117
|
+
discoverStall.stop();
|
|
118
|
+
}
|
|
119
|
+
trackBudget({
|
|
120
|
+
role: "discover",
|
|
121
|
+
provider: discoverProvider,
|
|
122
|
+
model: config?.roles?.discover?.model || coderRole.model,
|
|
123
|
+
result: discoverOutput,
|
|
124
|
+
duration_ms: Date.now() - discoverStart
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
await addCheckpoint(session, {
|
|
128
|
+
stage: "discover",
|
|
129
|
+
iteration: 0,
|
|
130
|
+
ok: discoverOutput.ok,
|
|
131
|
+
provider: discoverProvider,
|
|
132
|
+
model: config?.roles?.discover?.model || coderRole.model || null
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const stageResult = {
|
|
136
|
+
ok: discoverOutput.ok,
|
|
137
|
+
verdict: discoverOutput.result?.verdict || null,
|
|
138
|
+
gaps: discoverOutput.result?.gaps || [],
|
|
139
|
+
mode
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
emitProgress(
|
|
143
|
+
emitter,
|
|
144
|
+
makeEvent("discover:end", { ...eventBase, stage: "discover" }, {
|
|
145
|
+
status: discoverOutput.ok ? "ok" : "fail",
|
|
146
|
+
message: discoverOutput.ok ? "Discovery completed" : `Discovery failed: ${discoverOutput.summary}`,
|
|
147
|
+
detail: { ...stageResult, provider: discoverProvider, executorType: "agent" }
|
|
148
|
+
})
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return { stageResult };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Run the HU Reviewer stage: load stories from YAML or generate from task, evaluate, certify, and return in topological order.
|
|
156
|
+
*/
|
|
157
|
+
export async function runHuReviewerStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget, huFile, askQuestion, pgStories = null }) {
|
|
158
|
+
logger.setContext({ iteration: 0, stage: "hu-reviewer" });
|
|
159
|
+
const huReviewerProvider = config?.roles?.hu_reviewer?.provider || coderRole.provider;
|
|
160
|
+
emitProgress(
|
|
161
|
+
emitter,
|
|
162
|
+
makeEvent("hu-reviewer:start", { ...eventBase, stage: "hu-reviewer" }, {
|
|
163
|
+
message: "HU Reviewer certifying user stories",
|
|
164
|
+
detail: { provider: huReviewerProvider, executorType: "agent" }
|
|
165
|
+
})
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
let stories;
|
|
169
|
+
|
|
170
|
+
if (huFile) {
|
|
171
|
+
// --- Load YAML file ---
|
|
172
|
+
const yaml = await import("js-yaml");
|
|
173
|
+
const fs = await import("node:fs/promises");
|
|
174
|
+
let rawYaml;
|
|
175
|
+
try {
|
|
176
|
+
rawYaml = await fs.readFile(huFile, "utf8");
|
|
177
|
+
} catch (err) {
|
|
178
|
+
const stageResult = { ok: false, error: `Could not read HU file: ${err.message}` };
|
|
179
|
+
emitProgress(emitter, makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
180
|
+
status: "fail", message: stageResult.error
|
|
181
|
+
}));
|
|
182
|
+
return { stageResult };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const parsed = yaml.load(rawYaml);
|
|
187
|
+
stories = Array.isArray(parsed) ? parsed : (parsed?.stories || []);
|
|
188
|
+
} catch (err) {
|
|
189
|
+
const stageResult = { ok: false, error: `Invalid YAML in HU file: ${err.message}` };
|
|
190
|
+
emitProgress(emitter, makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
191
|
+
status: "fail", message: stageResult.error
|
|
192
|
+
}));
|
|
193
|
+
return { stageResult };
|
|
194
|
+
}
|
|
195
|
+
} else if (pgStories && pgStories.length > 0) {
|
|
196
|
+
// --- Use pre-built stories from Planning Game card structured data ---
|
|
197
|
+
stories = pgStories;
|
|
198
|
+
logger.info(`HU Reviewer: using ${pgStories.length} story(ies) from PG card`);
|
|
199
|
+
emitProgress(emitter, makeEvent("hu-reviewer:pg-feed", { ...eventBase, stage: "hu-reviewer" }, {
|
|
200
|
+
message: `Using ${pgStories.length} story(ies) from Planning Game card`,
|
|
201
|
+
detail: { source: "pg-card", storyIds: pgStories.map(s => s.id) }
|
|
202
|
+
}));
|
|
203
|
+
} else {
|
|
204
|
+
// --- Decompose task into formal HUs via AI, or fall back to single auto-story ---
|
|
205
|
+
const decomposed = await decomposeTaskIntoHUs({
|
|
206
|
+
config, logger, emitter, eventBase, session, coderRole, trackBudget
|
|
207
|
+
});
|
|
208
|
+
if (decomposed && decomposed.length > 1) {
|
|
209
|
+
stories = decomposed.map((hu, idx) => {
|
|
210
|
+
const isFirst = idx === 0;
|
|
211
|
+
const fullText = `As a ${hu.role}, I want to ${hu.goal}, so that ${hu.benefit}\n\nTitle: ${hu.title}\nAcceptance Criteria:\n${hu.acceptanceCriteria.map(ac => `- ${ac}`).join("\n")}`;
|
|
212
|
+
const skeletonText = `As a ${hu.role}, I want to ${hu.goal}, so that ${hu.benefit}\n\nTitle: ${hu.title}`;
|
|
213
|
+
return {
|
|
214
|
+
id: hu.id,
|
|
215
|
+
text: isFirst ? fullText : skeletonText,
|
|
216
|
+
blocked_by: hu.dependsOn || [],
|
|
217
|
+
needsRefinement: !isFirst
|
|
218
|
+
};
|
|
219
|
+
});
|
|
220
|
+
} else {
|
|
221
|
+
stories = [{ id: "HU-AUTO-001", text: session.task }];
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (stories.length === 0) {
|
|
226
|
+
const stageResult = { ok: true, certified: 0, stories: [] };
|
|
227
|
+
emitProgress(emitter, makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
228
|
+
status: "ok", message: "No stories to evaluate"
|
|
229
|
+
}));
|
|
230
|
+
return { stageResult };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// --- Create or load batch ---
|
|
234
|
+
const batchSessionId = `hu-${session.id}`;
|
|
235
|
+
let batch;
|
|
236
|
+
try {
|
|
237
|
+
batch = await loadHuBatch(batchSessionId);
|
|
238
|
+
} catch { /* no existing batch */
|
|
239
|
+
batch = await createHuBatch(batchSessionId, stories);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// --- Splitting detection: check each pending HU for split indicators before 6D evaluation ---
|
|
243
|
+
const storiesToSplit = [...batch.stories].filter(s => s.status === "pending");
|
|
244
|
+
for (const story of storiesToSplit) {
|
|
245
|
+
const indicators = detectIndicators(story.original.text);
|
|
246
|
+
if (indicators.length === 0) continue;
|
|
247
|
+
|
|
248
|
+
let heuristic = selectHeuristic(indicators);
|
|
249
|
+
if (!heuristic) continue;
|
|
250
|
+
|
|
251
|
+
const triedHeuristics = [];
|
|
252
|
+
let splitAccepted = false;
|
|
253
|
+
|
|
254
|
+
while (heuristic && !splitAccepted) {
|
|
255
|
+
const proposal = await generateSplitProposal(
|
|
256
|
+
{ id: story.id, text: story.original.text },
|
|
257
|
+
heuristic, config, logger
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
if (!proposal) {
|
|
261
|
+
triedHeuristics.push(heuristic);
|
|
262
|
+
heuristic = selectHeuristic(indicators, triedHeuristics);
|
|
263
|
+
continue;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (!askQuestion) {
|
|
267
|
+
// Autonomous mode: auto-accept the split
|
|
268
|
+
const updatedSubHUs = buildSplitDependencies(proposal.subHUs, story);
|
|
269
|
+
const storyIndex = batch.stories.findIndex(s => s.id === story.id);
|
|
270
|
+
const newStories = updatedSubHUs.map(sub => ({
|
|
271
|
+
id: sub.id, status: "pending",
|
|
272
|
+
original: { text: `${sub.title}\n\n${sub.text}\n\nAcceptance Criteria:\n${(sub.acceptanceCriteria || []).map(ac => `- ${ac}`).join("\n")}` },
|
|
273
|
+
blocked_by: sub.blocked_by || [], certified: null, quality: null,
|
|
274
|
+
context_requests: [], created_at: new Date().toISOString(), updated_at: new Date().toISOString()
|
|
275
|
+
}));
|
|
276
|
+
batch.stories.splice(storyIndex, 1, ...newStories);
|
|
277
|
+
splitAccepted = true;
|
|
278
|
+
emitProgress(emitter, makeEvent("hu-reviewer:split-accepted", { ...eventBase, stage: "hu-reviewer" }, {
|
|
279
|
+
message: `Auto-accepted split of ${story.id} into ${updatedSubHUs.length} sub-HUs`,
|
|
280
|
+
detail: { originalId: story.id, subHUs: updatedSubHUs.map(s => s.id), heuristic }
|
|
281
|
+
}));
|
|
282
|
+
} else {
|
|
283
|
+
// Interactive mode: ask FDE for confirmation
|
|
284
|
+
const formatted = formatSplitProposalForFDE(proposal);
|
|
285
|
+
const question = `Split proposed for ${story.id}:\n${formatted}\nAccept? (yes/no/try another)`;
|
|
286
|
+
emitProgress(emitter, makeEvent("hu-reviewer:split-proposal", { ...eventBase, stage: "hu-reviewer" }, {
|
|
287
|
+
message: `Split proposal for ${story.id} using heuristic: ${heuristic}`,
|
|
288
|
+
detail: { originalId: story.id, heuristic, subHUCount: proposal.subHUs.length }
|
|
289
|
+
}));
|
|
290
|
+
const answer = await askQuestion(question, { iteration: 0, stage: "hu-reviewer" });
|
|
291
|
+
if (!answer || answer.toLowerCase().startsWith("yes")) {
|
|
292
|
+
const updatedSubHUs = buildSplitDependencies(proposal.subHUs, story);
|
|
293
|
+
const storyIndex = batch.stories.findIndex(s => s.id === story.id);
|
|
294
|
+
const newStories = updatedSubHUs.map(sub => ({
|
|
295
|
+
id: sub.id, status: "pending",
|
|
296
|
+
original: { text: `${sub.title}\n\n${sub.text}\n\nAcceptance Criteria:\n${(sub.acceptanceCriteria || []).map(ac => `- ${ac}`).join("\n")}` },
|
|
297
|
+
blocked_by: sub.blocked_by || [], certified: null, quality: null,
|
|
298
|
+
context_requests: [], created_at: new Date().toISOString(), updated_at: new Date().toISOString()
|
|
299
|
+
}));
|
|
300
|
+
batch.stories.splice(storyIndex, 1, ...newStories);
|
|
301
|
+
splitAccepted = true;
|
|
302
|
+
emitProgress(emitter, makeEvent("hu-reviewer:split-accepted", { ...eventBase, stage: "hu-reviewer" }, {
|
|
303
|
+
message: `FDE accepted split of ${story.id} into ${updatedSubHUs.length} sub-HUs`,
|
|
304
|
+
detail: { originalId: story.id, subHUs: updatedSubHUs.map(s => s.id), heuristic }
|
|
305
|
+
}));
|
|
306
|
+
} else if (answer.toLowerCase().includes("try") || answer.toLowerCase().startsWith("no")) {
|
|
307
|
+
triedHeuristics.push(heuristic);
|
|
308
|
+
heuristic = selectHeuristic(indicators, triedHeuristics);
|
|
309
|
+
if (!heuristic) {
|
|
310
|
+
updateStoryStatus(batch, story.id, "needs_context");
|
|
311
|
+
}
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
break;
|
|
316
|
+
}
|
|
317
|
+
await saveHuBatch(batchSessionId, batch);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// --- Evaluate loop (re-evaluate entire batch until all certified or needs_context with no askQuestion) ---
|
|
321
|
+
const huReviewerOnOutput = ({ stream, line }) => {
|
|
322
|
+
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "hu-reviewer" }, {
|
|
323
|
+
message: line,
|
|
324
|
+
detail: { stream, agent: huReviewerProvider }
|
|
325
|
+
}));
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
let maxRounds = 5;
|
|
329
|
+
let round = 0;
|
|
330
|
+
|
|
331
|
+
while (round < maxRounds) {
|
|
332
|
+
round += 1;
|
|
333
|
+
|
|
334
|
+
const pendingStories = batch.stories.filter(s => s.status === "pending" || s.status === "needs_context");
|
|
335
|
+
if (pendingStories.length === 0) break;
|
|
336
|
+
|
|
337
|
+
const storiesToEvaluate = pendingStories.map(s => ({ id: s.id, text: s.original.text }));
|
|
338
|
+
|
|
339
|
+
const stall = createStallDetector({
|
|
340
|
+
onOutput: huReviewerOnOutput, emitter, eventBase, stage: "hu-reviewer", provider: huReviewerProvider
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
const huReviewer = new HuReviewerRole({ config, logger, emitter, createAgentFn: createAgent });
|
|
344
|
+
await huReviewer.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
345
|
+
const reviewStart = Date.now();
|
|
346
|
+
let reviewOutput;
|
|
347
|
+
try {
|
|
348
|
+
reviewOutput = await huReviewer.run({ stories: storiesToEvaluate, onOutput: stall.onOutput });
|
|
349
|
+
} catch (err) {
|
|
350
|
+
logger.warn(`HU Reviewer threw: ${err.message}`);
|
|
351
|
+
reviewOutput = { ok: false, summary: `HU Reviewer error: ${err.message}`, result: { error: err.message } };
|
|
352
|
+
} finally {
|
|
353
|
+
stall.stop();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
trackBudget({
|
|
357
|
+
role: "hu-reviewer",
|
|
358
|
+
provider: huReviewerProvider,
|
|
359
|
+
model: config?.roles?.hu_reviewer?.model || coderRole.model,
|
|
360
|
+
result: reviewOutput,
|
|
361
|
+
duration_ms: Date.now() - reviewStart
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
if (!reviewOutput.ok || !reviewOutput.result?.evaluations) {
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// --- Process evaluations ---
|
|
369
|
+
for (const evaluation of reviewOutput.result.evaluations) {
|
|
370
|
+
const storyId = evaluation.story_id;
|
|
371
|
+
try {
|
|
372
|
+
updateStoryQuality(batch, storyId, evaluation.scores);
|
|
373
|
+
} catch {
|
|
374
|
+
continue; // story not found in batch, skip
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (evaluation.verdict === "certified") {
|
|
378
|
+
updateStoryCertified(batch, storyId, evaluation.certified_hu);
|
|
379
|
+
} else if (evaluation.verdict === "needs_context" && evaluation.context_needed) {
|
|
380
|
+
addContextRequest(batch, storyId, {
|
|
381
|
+
fields_needed: evaluation.context_needed.fields_needed || [],
|
|
382
|
+
question: evaluation.context_needed.question_to_fde || ""
|
|
383
|
+
});
|
|
384
|
+
} else if (evaluation.verdict === "needs_rewrite" && evaluation.rewritten) {
|
|
385
|
+
// Accept the rewrite and re-certify
|
|
386
|
+
updateStoryCertified(batch, storyId, evaluation.rewritten);
|
|
387
|
+
} else {
|
|
388
|
+
updateStoryStatus(batch, storyId, "pending");
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
await saveHuBatch(batchSessionId, batch);
|
|
393
|
+
|
|
394
|
+
// --- Check if any need context ---
|
|
395
|
+
const needsContext = batch.stories.filter(s => s.status === "needs_context");
|
|
396
|
+
if (needsContext.length > 0) {
|
|
397
|
+
const consolidatedQuestions = reviewOutput.result.batch_summary?.consolidated_questions
|
|
398
|
+
|| needsContext.map(s => {
|
|
399
|
+
const pending = s.context_requests.find(r => !r.answered_at);
|
|
400
|
+
return pending ? `[${s.id}] ${pending.question_to_fde}` : null;
|
|
401
|
+
}).filter(Boolean).join("\n");
|
|
402
|
+
|
|
403
|
+
if (!askQuestion) {
|
|
404
|
+
// No interactive input — pause session
|
|
405
|
+
break;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
emitProgress(emitter, makeEvent("hu-reviewer:needs-context", { ...eventBase, stage: "hu-reviewer" }, {
|
|
409
|
+
message: `${needsContext.length} story(ies) need context from FDE`,
|
|
410
|
+
detail: { questions: consolidatedQuestions }
|
|
411
|
+
}));
|
|
412
|
+
|
|
413
|
+
const answer = await askQuestion(
|
|
414
|
+
`The HU Reviewer needs additional context:\n\n${consolidatedQuestions}\n\nPlease provide your answers:`,
|
|
415
|
+
{ iteration: 0, stage: "hu-reviewer" }
|
|
416
|
+
);
|
|
417
|
+
|
|
418
|
+
if (!answer) break;
|
|
419
|
+
|
|
420
|
+
// --- Incorporate FDE answers and re-evaluate ---
|
|
421
|
+
for (const s of needsContext) {
|
|
422
|
+
answerContextRequest(batch, s.id, answer);
|
|
423
|
+
}
|
|
424
|
+
await saveHuBatch(batchSessionId, batch);
|
|
425
|
+
// Loop will re-evaluate entire batch
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
await addCheckpoint(session, {
|
|
430
|
+
stage: "hu-reviewer",
|
|
431
|
+
iteration: 0,
|
|
432
|
+
ok: true,
|
|
433
|
+
certified: batch.stories.filter(s => s.status === "certified").length,
|
|
434
|
+
total: batch.stories.length
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
// --- Return certified stories in topological order ---
|
|
438
|
+
const certifiedStories = batch.stories.filter(s => s.status === "certified");
|
|
439
|
+
let orderedIds;
|
|
440
|
+
try {
|
|
441
|
+
orderedIds = topologicalSort(certifiedStories);
|
|
442
|
+
} catch { /* cyclic dependency */
|
|
443
|
+
orderedIds = certifiedStories.map(s => s.id);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const orderedStories = orderedIds.map(id => batch.stories.find(s => s.id === id)).filter(Boolean);
|
|
447
|
+
|
|
448
|
+
const stageResult = {
|
|
449
|
+
ok: true,
|
|
450
|
+
certified: certifiedStories.length,
|
|
451
|
+
total: batch.stories.length,
|
|
452
|
+
needsContext: batch.stories.filter(s => s.status === "needs_context").length,
|
|
453
|
+
stories: orderedStories,
|
|
454
|
+
batchSessionId
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
emitProgress(
|
|
458
|
+
emitter,
|
|
459
|
+
makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
460
|
+
status: "ok",
|
|
461
|
+
message: `HU Review complete: ${certifiedStories.length}/${batch.stories.length} certified`,
|
|
462
|
+
detail: { ...stageResult, provider: huReviewerProvider, executorType: "agent" }
|
|
463
|
+
})
|
|
464
|
+
);
|
|
465
|
+
|
|
466
|
+
return { stageResult };
|
|
467
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Planner stage logic.
|
|
3
|
+
* Extracted from pre-loop-stages.js for maintainability.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { PlannerRole } from "../../roles/planner-role.js";
|
|
7
|
+
import { createAgent } from "../../agents/index.js";
|
|
8
|
+
import { addCheckpoint, markSessionStatus } from "../../session-store.js";
|
|
9
|
+
import { emitProgress, makeEvent } from "../../utils/events.js";
|
|
10
|
+
import { parsePlannerOutput } from "../../prompts/planner.js";
|
|
11
|
+
import { createStallDetector } from "../../utils/stall-detector.js";
|
|
12
|
+
|
|
13
|
+
export async function runPlannerStage({ config, logger, emitter, eventBase, session, plannerRole, researchContext, architectContext = null, triageDecomposition = null, trackBudget }) {
|
|
14
|
+
const task = session.task;
|
|
15
|
+
logger.setContext({ iteration: 0, stage: "planner" });
|
|
16
|
+
emitProgress(
|
|
17
|
+
emitter,
|
|
18
|
+
makeEvent("planner:start", { ...eventBase, stage: "planner" }, {
|
|
19
|
+
message: `Planner (${plannerRole.provider}) running`,
|
|
20
|
+
detail: { planner: plannerRole.provider, provider: plannerRole.provider, executorType: "agent" }
|
|
21
|
+
})
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
const plannerOnOutput = ({ stream, line }) => {
|
|
25
|
+
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "planner" }, {
|
|
26
|
+
message: line,
|
|
27
|
+
detail: { stream, agent: plannerRole.provider }
|
|
28
|
+
}));
|
|
29
|
+
};
|
|
30
|
+
const plannerStall = createStallDetector({
|
|
31
|
+
onOutput: plannerOnOutput, emitter, eventBase, stage: "planner", provider: plannerRole.provider
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const planRole = new PlannerRole({ config, logger, emitter, createAgentFn: createAgent });
|
|
35
|
+
planRole.context = { task, research: researchContext, architecture: architectContext, triageDecomposition };
|
|
36
|
+
await planRole.init();
|
|
37
|
+
const plannerStart = Date.now();
|
|
38
|
+
let planResult;
|
|
39
|
+
try {
|
|
40
|
+
planResult = await planRole.execute({ task, onOutput: plannerStall.onOutput });
|
|
41
|
+
} catch (err) {
|
|
42
|
+
logger.warn(`Planner threw: ${err.message}`);
|
|
43
|
+
planResult = { ok: false, result: { error: err.message }, summary: `Planner error: ${err.message}` };
|
|
44
|
+
} finally {
|
|
45
|
+
plannerStall.stop();
|
|
46
|
+
}
|
|
47
|
+
trackBudget({ role: "planner", provider: plannerRole.provider, model: plannerRole.model, result: planResult.result, duration_ms: Date.now() - plannerStart });
|
|
48
|
+
await addCheckpoint(session, {
|
|
49
|
+
stage: "planner",
|
|
50
|
+
iteration: 0,
|
|
51
|
+
ok: planResult.ok,
|
|
52
|
+
provider: plannerRole.provider,
|
|
53
|
+
model: plannerRole.model || null
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
if (!planResult.ok) {
|
|
57
|
+
await markSessionStatus(session, "failed");
|
|
58
|
+
const details = planResult.result?.error || planResult.summary || "unknown error";
|
|
59
|
+
emitProgress(
|
|
60
|
+
emitter,
|
|
61
|
+
makeEvent("planner:end", { ...eventBase, stage: "planner" }, {
|
|
62
|
+
status: "fail",
|
|
63
|
+
message: `Planner failed: ${details}`,
|
|
64
|
+
detail: { provider: plannerRole.provider, executorType: "agent" }
|
|
65
|
+
})
|
|
66
|
+
);
|
|
67
|
+
throw new Error(`Planner failed: ${details}`);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const planOutput = planResult.result?.plan || "";
|
|
71
|
+
const plannedTask = planOutput ? `${task}\n\nExecution plan:\n${planOutput}` : task;
|
|
72
|
+
const parsedPlan = parsePlannerOutput(planOutput);
|
|
73
|
+
const stageResult = {
|
|
74
|
+
ok: true,
|
|
75
|
+
title: parsedPlan?.title || null,
|
|
76
|
+
approach: parsedPlan?.approach || null,
|
|
77
|
+
steps: parsedPlan?.steps || [],
|
|
78
|
+
completedSteps: []
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
emitProgress(
|
|
82
|
+
emitter,
|
|
83
|
+
makeEvent("planner:end", { ...eventBase, stage: "planner" }, {
|
|
84
|
+
message: "Planner completed",
|
|
85
|
+
detail: { provider: plannerRole.provider, executorType: "agent" }
|
|
86
|
+
})
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
return { plannedTask, stageResult };
|
|
90
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Researcher stage logic.
|
|
3
|
+
* Extracted from pre-loop-stages.js for maintainability.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { ResearcherRole } from "../../roles/researcher-role.js";
|
|
7
|
+
import { addCheckpoint } from "../../session-store.js";
|
|
8
|
+
import { emitProgress, makeEvent } from "../../utils/events.js";
|
|
9
|
+
import { createStallDetector } from "../../utils/stall-detector.js";
|
|
10
|
+
|
|
11
|
+
export async function runResearcherStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
12
|
+
logger.setContext({ iteration: 0, stage: "researcher" });
|
|
13
|
+
|
|
14
|
+
const researcherProvider = config?.roles?.researcher?.provider || coderRole.provider;
|
|
15
|
+
emitProgress(
|
|
16
|
+
emitter,
|
|
17
|
+
makeEvent("researcher:start", { ...eventBase, stage: "researcher" }, {
|
|
18
|
+
message: "Researcher investigating codebase",
|
|
19
|
+
detail: { researcher: researcherProvider, provider: researcherProvider, executorType: "agent" }
|
|
20
|
+
})
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
const researcherOnOutput = ({ stream, line }) => {
|
|
24
|
+
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "researcher" }, {
|
|
25
|
+
message: line,
|
|
26
|
+
detail: { stream, agent: researcherProvider }
|
|
27
|
+
}));
|
|
28
|
+
};
|
|
29
|
+
const researcherStall = createStallDetector({
|
|
30
|
+
onOutput: researcherOnOutput, emitter, eventBase, stage: "researcher", provider: researcherProvider
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const researcher = new ResearcherRole({ config, logger, emitter });
|
|
34
|
+
await researcher.init({ task: session.task });
|
|
35
|
+
const researchStart = Date.now();
|
|
36
|
+
let researchOutput;
|
|
37
|
+
try {
|
|
38
|
+
researchOutput = await researcher.run({ task: session.task, onOutput: researcherStall.onOutput });
|
|
39
|
+
} catch (err) {
|
|
40
|
+
logger.warn(`Researcher threw: ${err.message}`);
|
|
41
|
+
researchOutput = { ok: false, summary: `Researcher error: ${err.message}`, result: { error: err.message } };
|
|
42
|
+
} finally {
|
|
43
|
+
researcherStall.stop();
|
|
44
|
+
}
|
|
45
|
+
trackBudget({
|
|
46
|
+
role: "researcher",
|
|
47
|
+
provider: config?.roles?.researcher?.provider || coderRole.provider,
|
|
48
|
+
model: config?.roles?.researcher?.model || coderRole.model,
|
|
49
|
+
result: researchOutput,
|
|
50
|
+
duration_ms: Date.now() - researchStart
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
await addCheckpoint(session, {
|
|
54
|
+
stage: "researcher",
|
|
55
|
+
iteration: 0,
|
|
56
|
+
ok: researchOutput.ok,
|
|
57
|
+
provider: config?.roles?.researcher?.provider || coderRole.provider,
|
|
58
|
+
model: config?.roles?.researcher?.model || coderRole.model || null
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
emitProgress(
|
|
62
|
+
emitter,
|
|
63
|
+
makeEvent("researcher:end", { ...eventBase, stage: "researcher" }, {
|
|
64
|
+
status: researchOutput.ok ? "ok" : "fail",
|
|
65
|
+
message: researchOutput.ok ? "Research completed" : `Research failed: ${researchOutput.summary}`,
|
|
66
|
+
detail: { provider: researcherProvider, executorType: "agent" }
|
|
67
|
+
})
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const stageResult = { ok: researchOutput.ok, summary: researchOutput.summary || null };
|
|
71
|
+
const researchContext = researchOutput.ok ? researchOutput.result : null;
|
|
72
|
+
|
|
73
|
+
return { researchContext, stageResult };
|
|
74
|
+
}
|