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
|
@@ -1,909 +1,11 @@
|
|
|
1
|
-
import { TriageRole } from "../roles/triage-role.js";
|
|
2
|
-
import { ResearcherRole } from "../roles/researcher-role.js";
|
|
3
|
-
import { PlannerRole } from "../roles/planner-role.js";
|
|
4
|
-
import { DiscoverRole } from "../roles/discover-role.js";
|
|
5
|
-
import { ArchitectRole } from "../roles/architect-role.js";
|
|
6
|
-
import { HuReviewerRole } from "../roles/hu-reviewer-role.js";
|
|
7
|
-
import { createAgent } from "../agents/index.js";
|
|
8
|
-
import { createArchitectADRs } from "../planning-game/architect-adrs.js";
|
|
9
|
-
import { addCheckpoint, markSessionStatus } from "../session-store.js";
|
|
10
|
-
import { emitProgress, makeEvent } from "../utils/events.js";
|
|
11
|
-
import { parsePlannerOutput } from "../prompts/planner.js";
|
|
12
|
-
import { buildDecompositionPrompt, parseDecompositionOutput } from "../prompts/hu-reviewer.js";
|
|
13
|
-
import { selectModelsForRoles } from "../utils/model-selector.js";
|
|
14
|
-
import { createStallDetector } from "../utils/stall-detector.js";
|
|
15
|
-
import { createHuBatch, loadHuBatch, saveHuBatch, updateStoryStatus, updateStoryQuality, updateStoryCertified, addContextRequest, answerContextRequest } from "../hu/store.js";
|
|
16
|
-
import { topologicalSort } from "../hu/graph.js";
|
|
17
|
-
import { detectIndicators, selectHeuristic } from "../hu/splitting-detector.js";
|
|
18
|
-
import { generateSplitProposal, formatSplitProposalForFDE, buildSplitDependencies } from "../hu/splitting-generator.js";
|
|
19
|
-
|
|
20
|
-
const ROLE_NAMES = ["planner", "researcher", "architect", "refactorer", "reviewer", "tester", "security", "impeccable"];
|
|
21
|
-
|
|
22
|
-
function buildRoleOverrides(recommendedRoles, pipelineConfig) {
|
|
23
|
-
const overrides = {};
|
|
24
|
-
for (const role of ROLE_NAMES) {
|
|
25
|
-
overrides[`${role}Enabled`] = recommendedRoles.has(role) || Boolean(pipelineConfig[role]?.enabled);
|
|
26
|
-
}
|
|
27
|
-
return overrides;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
function applyModelSelection(triageOutput, config, emitter, eventBase) {
|
|
31
|
-
if (!triageOutput.ok || !config?.model_selection?.enabled) return null;
|
|
32
|
-
const level = triageOutput.result?.level;
|
|
33
|
-
if (!level) return null;
|
|
34
|
-
|
|
35
|
-
const { modelOverrides, reasoning } = selectModelsForRoles({ level, config });
|
|
36
|
-
for (const [role, model] of Object.entries(modelOverrides)) {
|
|
37
|
-
if (config.roles?.[role] && !config.roles[role].model) {
|
|
38
|
-
config.roles[role].model = model;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
const modelSelection = { modelOverrides, reasoning };
|
|
42
|
-
emitProgress(
|
|
43
|
-
emitter,
|
|
44
|
-
makeEvent("model-selection:applied", { ...eventBase, stage: "triage" }, {
|
|
45
|
-
message: "Smart model selection applied",
|
|
46
|
-
detail: modelSelection
|
|
47
|
-
})
|
|
48
|
-
);
|
|
49
|
-
return modelSelection;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export async function runTriageStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
53
|
-
logger.setContext({ iteration: 0, stage: "triage" });
|
|
54
|
-
|
|
55
|
-
const triageProvider = config?.roles?.triage?.provider || coderRole.provider;
|
|
56
|
-
emitProgress(
|
|
57
|
-
emitter,
|
|
58
|
-
makeEvent("triage:start", { ...eventBase, stage: "triage" }, {
|
|
59
|
-
message: "Triage classifying task complexity",
|
|
60
|
-
detail: { provider: triageProvider, executorType: "agent" }
|
|
61
|
-
})
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
const triageOnOutput = ({ stream, line }) => {
|
|
65
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "triage" }, {
|
|
66
|
-
message: line,
|
|
67
|
-
detail: { stream, agent: triageProvider }
|
|
68
|
-
}));
|
|
69
|
-
};
|
|
70
|
-
const triageStall = createStallDetector({
|
|
71
|
-
onOutput: triageOnOutput, emitter, eventBase, stage: "triage", provider: triageProvider
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
const triage = new TriageRole({ config, logger, emitter });
|
|
75
|
-
await triage.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
76
|
-
const triageStart = Date.now();
|
|
77
|
-
let triageOutput;
|
|
78
|
-
try {
|
|
79
|
-
triageOutput = await triage.run({ task: session.task, onOutput: triageStall.onOutput });
|
|
80
|
-
} catch (err) {
|
|
81
|
-
logger.warn(`Triage threw: ${err.message}`);
|
|
82
|
-
triageOutput = { ok: false, summary: `Triage error: ${err.message}`, result: { error: err.message } };
|
|
83
|
-
} finally {
|
|
84
|
-
triageStall.stop();
|
|
85
|
-
}
|
|
86
|
-
trackBudget({
|
|
87
|
-
role: "triage",
|
|
88
|
-
provider: config?.roles?.triage?.provider || coderRole.provider,
|
|
89
|
-
model: config?.roles?.triage?.model || coderRole.model,
|
|
90
|
-
result: triageOutput,
|
|
91
|
-
duration_ms: Date.now() - triageStart
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
await addCheckpoint(session, {
|
|
95
|
-
stage: "triage",
|
|
96
|
-
iteration: 0,
|
|
97
|
-
ok: triageOutput.ok,
|
|
98
|
-
provider: config?.roles?.triage?.provider || coderRole.provider,
|
|
99
|
-
model: config?.roles?.triage?.model || coderRole.model || null
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
const recommendedRoles = new Set(triageOutput.result?.roles || []);
|
|
103
|
-
const roleOverrides = triageOutput.ok
|
|
104
|
-
? buildRoleOverrides(recommendedRoles, config.pipeline || {})
|
|
105
|
-
: {};
|
|
106
|
-
|
|
107
|
-
const shouldDecompose = triageOutput.result?.shouldDecompose || false;
|
|
108
|
-
const subtasks = triageOutput.result?.subtasks || [];
|
|
109
|
-
|
|
110
|
-
const stageResult = {
|
|
111
|
-
ok: triageOutput.ok,
|
|
112
|
-
level: triageOutput.result?.level || null,
|
|
113
|
-
roles: Array.from(recommendedRoles),
|
|
114
|
-
reasoning: triageOutput.result?.reasoning || null,
|
|
115
|
-
taskType: triageOutput.result?.taskType || "sw",
|
|
116
|
-
shouldDecompose,
|
|
117
|
-
subtasks
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
const modelSelection = applyModelSelection(triageOutput, config, emitter, eventBase);
|
|
121
|
-
if (modelSelection) {
|
|
122
|
-
stageResult.modelSelection = modelSelection;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
if (shouldDecompose && subtasks.length > 0) {
|
|
126
|
-
emitProgress(
|
|
127
|
-
emitter,
|
|
128
|
-
makeEvent("triage:decompose", { ...eventBase, stage: "triage" }, {
|
|
129
|
-
message: `Task decomposition recommended: ${subtasks.length} subtask${subtasks.length === 1 ? "" : "s"}`,
|
|
130
|
-
detail: { shouldDecompose, subtasks }
|
|
131
|
-
})
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
emitProgress(
|
|
136
|
-
emitter,
|
|
137
|
-
makeEvent("triage:end", { ...eventBase, stage: "triage" }, {
|
|
138
|
-
status: triageOutput.ok ? "ok" : "fail",
|
|
139
|
-
message: triageOutput.ok ? "Triage completed" : `Triage failed: ${triageOutput.summary}`,
|
|
140
|
-
detail: { ...stageResult, provider: triageProvider, executorType: "agent" }
|
|
141
|
-
})
|
|
142
|
-
);
|
|
143
|
-
|
|
144
|
-
return { roleOverrides, stageResult };
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export async function runResearcherStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
148
|
-
logger.setContext({ iteration: 0, stage: "researcher" });
|
|
149
|
-
|
|
150
|
-
const researcherProvider = config?.roles?.researcher?.provider || coderRole.provider;
|
|
151
|
-
emitProgress(
|
|
152
|
-
emitter,
|
|
153
|
-
makeEvent("researcher:start", { ...eventBase, stage: "researcher" }, {
|
|
154
|
-
message: "Researcher investigating codebase",
|
|
155
|
-
detail: { researcher: researcherProvider, provider: researcherProvider, executorType: "agent" }
|
|
156
|
-
})
|
|
157
|
-
);
|
|
158
|
-
|
|
159
|
-
const researcherOnOutput = ({ stream, line }) => {
|
|
160
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "researcher" }, {
|
|
161
|
-
message: line,
|
|
162
|
-
detail: { stream, agent: researcherProvider }
|
|
163
|
-
}));
|
|
164
|
-
};
|
|
165
|
-
const researcherStall = createStallDetector({
|
|
166
|
-
onOutput: researcherOnOutput, emitter, eventBase, stage: "researcher", provider: researcherProvider
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
const researcher = new ResearcherRole({ config, logger, emitter });
|
|
170
|
-
await researcher.init({ task: session.task });
|
|
171
|
-
const researchStart = Date.now();
|
|
172
|
-
let researchOutput;
|
|
173
|
-
try {
|
|
174
|
-
researchOutput = await researcher.run({ task: session.task, onOutput: researcherStall.onOutput });
|
|
175
|
-
} catch (err) {
|
|
176
|
-
logger.warn(`Researcher threw: ${err.message}`);
|
|
177
|
-
researchOutput = { ok: false, summary: `Researcher error: ${err.message}`, result: { error: err.message } };
|
|
178
|
-
} finally {
|
|
179
|
-
researcherStall.stop();
|
|
180
|
-
}
|
|
181
|
-
trackBudget({
|
|
182
|
-
role: "researcher",
|
|
183
|
-
provider: config?.roles?.researcher?.provider || coderRole.provider,
|
|
184
|
-
model: config?.roles?.researcher?.model || coderRole.model,
|
|
185
|
-
result: researchOutput,
|
|
186
|
-
duration_ms: Date.now() - researchStart
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
await addCheckpoint(session, {
|
|
190
|
-
stage: "researcher",
|
|
191
|
-
iteration: 0,
|
|
192
|
-
ok: researchOutput.ok,
|
|
193
|
-
provider: config?.roles?.researcher?.provider || coderRole.provider,
|
|
194
|
-
model: config?.roles?.researcher?.model || coderRole.model || null
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
emitProgress(
|
|
198
|
-
emitter,
|
|
199
|
-
makeEvent("researcher:end", { ...eventBase, stage: "researcher" }, {
|
|
200
|
-
status: researchOutput.ok ? "ok" : "fail",
|
|
201
|
-
message: researchOutput.ok ? "Research completed" : `Research failed: ${researchOutput.summary}`,
|
|
202
|
-
detail: { provider: researcherProvider, executorType: "agent" }
|
|
203
|
-
})
|
|
204
|
-
);
|
|
205
|
-
|
|
206
|
-
const stageResult = { ok: researchOutput.ok, summary: researchOutput.summary || null };
|
|
207
|
-
const researchContext = researchOutput.ok ? researchOutput.result : null;
|
|
208
|
-
|
|
209
|
-
return { researchContext, stageResult };
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
async function handleArchitectClarification({ architectOutput, askQuestion, config, logger, emitter, eventBase, session, architectOnOutput, architectProvider, coderRole, researchContext, discoverResult, triageLevel, trackBudget }) {
|
|
213
|
-
if (!architectOutput.ok
|
|
214
|
-
|| architectOutput.result?.verdict !== "needs_clarification"
|
|
215
|
-
|| !architectOutput.result?.questions?.length) {
|
|
216
|
-
return architectOutput;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const questions = architectOutput.result.questions;
|
|
220
|
-
if (!askQuestion) {
|
|
221
|
-
logger.warn("Architect returned needs_clarification but no interactive input available — continuing with best-effort decisions");
|
|
222
|
-
return architectOutput;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
const formatted = "The architect needs clarification before proceeding:\n\n"
|
|
226
|
-
+ questions.map((q, i) => `${i + 1}. ${q}`).join("\n")
|
|
227
|
-
+ "\n\nPlease provide your answers:";
|
|
228
|
-
|
|
229
|
-
emitProgress(
|
|
230
|
-
emitter,
|
|
231
|
-
makeEvent("architect:clarification", { ...eventBase, stage: "architect" }, {
|
|
232
|
-
message: "Architect needs clarification — pausing for human input",
|
|
233
|
-
detail: { questions }
|
|
234
|
-
})
|
|
235
|
-
);
|
|
236
|
-
|
|
237
|
-
const answer = await askQuestion(formatted, { iteration: 0, stage: "architect" });
|
|
238
|
-
if (!answer) return architectOutput;
|
|
239
|
-
|
|
240
|
-
const architect2 = new ArchitectRole({ config, logger, emitter, createAgentFn: createAgent });
|
|
241
|
-
await architect2.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
242
|
-
const rerunStart = Date.now();
|
|
243
|
-
const rerunStall = createStallDetector({
|
|
244
|
-
onOutput: architectOnOutput, emitter, eventBase, stage: "architect", provider: architectProvider
|
|
245
|
-
});
|
|
246
|
-
let result;
|
|
247
|
-
try {
|
|
248
|
-
result = await architect2.execute({
|
|
249
|
-
task: session.task,
|
|
250
|
-
onOutput: rerunStall.onOutput,
|
|
251
|
-
researchContext,
|
|
252
|
-
discoverResult,
|
|
253
|
-
triageLevel,
|
|
254
|
-
humanAnswers: answer
|
|
255
|
-
});
|
|
256
|
-
} finally {
|
|
257
|
-
rerunStall.stop();
|
|
258
|
-
}
|
|
259
|
-
trackBudget({
|
|
260
|
-
role: "architect",
|
|
261
|
-
provider: architectProvider,
|
|
262
|
-
model: config?.roles?.architect?.model || coderRole.model,
|
|
263
|
-
result,
|
|
264
|
-
duration_ms: Date.now() - rerunStart
|
|
265
|
-
});
|
|
266
|
-
return result;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
export async function runArchitectStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget, researchContext = null, discoverResult = null, triageLevel = null, askQuestion = null }) {
|
|
270
|
-
logger.setContext({ iteration: 0, stage: "architect" });
|
|
271
|
-
const architectProvider = config?.roles?.architect?.provider || coderRole.provider;
|
|
272
|
-
emitProgress(
|
|
273
|
-
emitter,
|
|
274
|
-
makeEvent("architect:start", { ...eventBase, stage: "architect" }, {
|
|
275
|
-
message: "Architect designing solution architecture",
|
|
276
|
-
detail: { architect: architectProvider, provider: architectProvider, executorType: "agent" }
|
|
277
|
-
})
|
|
278
|
-
);
|
|
279
|
-
const architectOnOutput = ({ stream, line }) => {
|
|
280
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "architect" }, {
|
|
281
|
-
message: line,
|
|
282
|
-
detail: { stream, agent: architectProvider }
|
|
283
|
-
}));
|
|
284
|
-
};
|
|
285
|
-
const architectStall = createStallDetector({
|
|
286
|
-
onOutput: architectOnOutput, emitter, eventBase, stage: "architect", provider: architectProvider
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
const architect = new ArchitectRole({ config, logger, emitter, createAgentFn: createAgent });
|
|
290
|
-
await architect.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
291
|
-
const architectStart = Date.now();
|
|
292
|
-
let architectOutput;
|
|
293
|
-
try {
|
|
294
|
-
architectOutput = await architect.execute({
|
|
295
|
-
task: session.task,
|
|
296
|
-
onOutput: architectStall.onOutput,
|
|
297
|
-
researchContext,
|
|
298
|
-
discoverResult,
|
|
299
|
-
triageLevel
|
|
300
|
-
});
|
|
301
|
-
} catch (err) {
|
|
302
|
-
logger.warn(`Architect threw: ${err.message}`);
|
|
303
|
-
architectOutput = { ok: false, summary: `Architect error: ${err.message}`, result: { error: err.message } };
|
|
304
|
-
} finally {
|
|
305
|
-
architectStall.stop();
|
|
306
|
-
}
|
|
307
|
-
trackBudget({
|
|
308
|
-
role: "architect",
|
|
309
|
-
provider: architectProvider,
|
|
310
|
-
model: config?.roles?.architect?.model || coderRole.model,
|
|
311
|
-
result: architectOutput,
|
|
312
|
-
duration_ms: Date.now() - architectStart
|
|
313
|
-
});
|
|
314
|
-
|
|
315
|
-
await addCheckpoint(session, {
|
|
316
|
-
stage: "architect",
|
|
317
|
-
iteration: 0,
|
|
318
|
-
ok: architectOutput.ok,
|
|
319
|
-
provider: architectProvider,
|
|
320
|
-
model: config?.roles?.architect?.model || coderRole.model || null
|
|
321
|
-
});
|
|
322
|
-
|
|
323
|
-
// --- Interactive clarification loop ---
|
|
324
|
-
architectOutput = await handleArchitectClarification({
|
|
325
|
-
architectOutput, askQuestion, config, logger, emitter, eventBase, session,
|
|
326
|
-
architectOnOutput, architectProvider, coderRole, researchContext, discoverResult, triageLevel, trackBudget
|
|
327
|
-
});
|
|
328
|
-
|
|
329
|
-
const stageResult = {
|
|
330
|
-
ok: architectOutput.ok,
|
|
331
|
-
verdict: architectOutput.result?.verdict || null,
|
|
332
|
-
architecture: architectOutput.result?.architecture || null,
|
|
333
|
-
questions: architectOutput.result?.questions || []
|
|
334
|
-
};
|
|
335
|
-
|
|
336
|
-
emitProgress(
|
|
337
|
-
emitter,
|
|
338
|
-
makeEvent("architect:end", { ...eventBase, stage: "architect" }, {
|
|
339
|
-
status: architectOutput.ok ? "ok" : "fail",
|
|
340
|
-
message: architectOutput.ok ? "Architecture completed" : `Architecture failed: ${architectOutput.summary}`,
|
|
341
|
-
detail: { ...stageResult, provider: architectProvider, executorType: "agent" }
|
|
342
|
-
})
|
|
343
|
-
);
|
|
344
|
-
|
|
345
|
-
const architectContext = architectOutput.ok ? architectOutput.result : null;
|
|
346
|
-
|
|
347
|
-
// TODO: Move ADR creation to planning-game/pipeline-adapter.js (PG coupling still here because
|
|
348
|
-
// stageResult.adrs is consumed synchronously within runArchitectStage's return value).
|
|
349
|
-
// Generate ADRs from architect tradeoffs when PG is linked
|
|
350
|
-
const tradeoffs = architectOutput.result?.architecture?.tradeoffs;
|
|
351
|
-
if (architectOutput.ok
|
|
352
|
-
&& architectOutput.result?.verdict === "ready"
|
|
353
|
-
&& tradeoffs?.length > 0
|
|
354
|
-
&& session.pg_task_id
|
|
355
|
-
&& session.pg_project_id) {
|
|
356
|
-
try {
|
|
357
|
-
const pgClient = await import("../planning-game/client.js");
|
|
358
|
-
const adrResult = await createArchitectADRs({
|
|
359
|
-
tradeoffs,
|
|
360
|
-
pgTaskId: session.pg_task_id,
|
|
361
|
-
pgProject: session.pg_project_id,
|
|
362
|
-
taskTitle: session.task,
|
|
363
|
-
mcpClient: pgClient
|
|
364
|
-
});
|
|
365
|
-
stageResult.adrs = adrResult;
|
|
366
|
-
if (adrResult.created > 0) {
|
|
367
|
-
logger.info(`Architect: created ${adrResult.created} ADR(s) in Planning Game`);
|
|
368
|
-
}
|
|
369
|
-
} catch (err) {
|
|
370
|
-
logger.warn(`Architect: failed to create ADRs: ${err.message}`);
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
return { architectContext, stageResult };
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
export async function runPlannerStage({ config, logger, emitter, eventBase, session, plannerRole, researchContext, architectContext = null, triageDecomposition = null, trackBudget }) {
|
|
378
|
-
const task = session.task;
|
|
379
|
-
logger.setContext({ iteration: 0, stage: "planner" });
|
|
380
|
-
emitProgress(
|
|
381
|
-
emitter,
|
|
382
|
-
makeEvent("planner:start", { ...eventBase, stage: "planner" }, {
|
|
383
|
-
message: `Planner (${plannerRole.provider}) running`,
|
|
384
|
-
detail: { planner: plannerRole.provider, provider: plannerRole.provider, executorType: "agent" }
|
|
385
|
-
})
|
|
386
|
-
);
|
|
387
|
-
|
|
388
|
-
const plannerOnOutput = ({ stream, line }) => {
|
|
389
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "planner" }, {
|
|
390
|
-
message: line,
|
|
391
|
-
detail: { stream, agent: plannerRole.provider }
|
|
392
|
-
}));
|
|
393
|
-
};
|
|
394
|
-
const plannerStall = createStallDetector({
|
|
395
|
-
onOutput: plannerOnOutput, emitter, eventBase, stage: "planner", provider: plannerRole.provider
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
const planRole = new PlannerRole({ config, logger, emitter, createAgentFn: createAgent });
|
|
399
|
-
planRole.context = { task, research: researchContext, architecture: architectContext, triageDecomposition };
|
|
400
|
-
await planRole.init();
|
|
401
|
-
const plannerStart = Date.now();
|
|
402
|
-
let planResult;
|
|
403
|
-
try {
|
|
404
|
-
planResult = await planRole.execute({ task, onOutput: plannerStall.onOutput });
|
|
405
|
-
} catch (err) {
|
|
406
|
-
logger.warn(`Planner threw: ${err.message}`);
|
|
407
|
-
planResult = { ok: false, result: { error: err.message }, summary: `Planner error: ${err.message}` };
|
|
408
|
-
} finally {
|
|
409
|
-
plannerStall.stop();
|
|
410
|
-
}
|
|
411
|
-
trackBudget({ role: "planner", provider: plannerRole.provider, model: plannerRole.model, result: planResult.result, duration_ms: Date.now() - plannerStart });
|
|
412
|
-
await addCheckpoint(session, {
|
|
413
|
-
stage: "planner",
|
|
414
|
-
iteration: 0,
|
|
415
|
-
ok: planResult.ok,
|
|
416
|
-
provider: plannerRole.provider,
|
|
417
|
-
model: plannerRole.model || null
|
|
418
|
-
});
|
|
419
|
-
|
|
420
|
-
if (!planResult.ok) {
|
|
421
|
-
await markSessionStatus(session, "failed");
|
|
422
|
-
const details = planResult.result?.error || planResult.summary || "unknown error";
|
|
423
|
-
emitProgress(
|
|
424
|
-
emitter,
|
|
425
|
-
makeEvent("planner:end", { ...eventBase, stage: "planner" }, {
|
|
426
|
-
status: "fail",
|
|
427
|
-
message: `Planner failed: ${details}`,
|
|
428
|
-
detail: { provider: plannerRole.provider, executorType: "agent" }
|
|
429
|
-
})
|
|
430
|
-
);
|
|
431
|
-
throw new Error(`Planner failed: ${details}`);
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
const planOutput = planResult.result?.plan || "";
|
|
435
|
-
const plannedTask = planOutput ? `${task}\n\nExecution plan:\n${planOutput}` : task;
|
|
436
|
-
const parsedPlan = parsePlannerOutput(planOutput);
|
|
437
|
-
const stageResult = {
|
|
438
|
-
ok: true,
|
|
439
|
-
title: parsedPlan?.title || null,
|
|
440
|
-
approach: parsedPlan?.approach || null,
|
|
441
|
-
steps: parsedPlan?.steps || [],
|
|
442
|
-
completedSteps: []
|
|
443
|
-
};
|
|
444
|
-
|
|
445
|
-
emitProgress(
|
|
446
|
-
emitter,
|
|
447
|
-
makeEvent("planner:end", { ...eventBase, stage: "planner" }, {
|
|
448
|
-
message: "Planner completed",
|
|
449
|
-
detail: { provider: plannerRole.provider, executorType: "agent" }
|
|
450
|
-
})
|
|
451
|
-
);
|
|
452
|
-
|
|
453
|
-
return { plannedTask, stageResult };
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
export async function runDiscoverStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
457
|
-
logger.setContext({ iteration: 0, stage: "discover" });
|
|
458
|
-
const discoverProvider = config?.roles?.discover?.provider || coderRole.provider;
|
|
459
|
-
emitProgress(
|
|
460
|
-
emitter,
|
|
461
|
-
makeEvent("discover:start", { ...eventBase, stage: "discover" }, {
|
|
462
|
-
message: "Discover analyzing task for gaps",
|
|
463
|
-
detail: { discover: discoverProvider, provider: discoverProvider, executorType: "agent" }
|
|
464
|
-
})
|
|
465
|
-
);
|
|
466
|
-
const discoverOnOutput = ({ stream, line }) => {
|
|
467
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "discover" }, {
|
|
468
|
-
message: line,
|
|
469
|
-
detail: { stream, agent: discoverProvider }
|
|
470
|
-
}));
|
|
471
|
-
};
|
|
472
|
-
const discoverStall = createStallDetector({
|
|
473
|
-
onOutput: discoverOnOutput, emitter, eventBase, stage: "discover", provider: discoverProvider
|
|
474
|
-
});
|
|
475
|
-
|
|
476
|
-
const mode = config?.pipeline?.discover?.mode || "gaps";
|
|
477
|
-
const discover = new DiscoverRole({ config, logger, emitter });
|
|
478
|
-
await discover.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
479
|
-
const discoverStart = Date.now();
|
|
480
|
-
let discoverOutput;
|
|
481
|
-
try {
|
|
482
|
-
discoverOutput = await discover.run({ task: session.task, mode, onOutput: discoverStall.onOutput });
|
|
483
|
-
} catch (err) {
|
|
484
|
-
logger.warn(`Discover threw: ${err.message}`);
|
|
485
|
-
discoverOutput = { ok: false, summary: `Discover error: ${err.message}`, result: { error: err.message } };
|
|
486
|
-
} finally {
|
|
487
|
-
discoverStall.stop();
|
|
488
|
-
}
|
|
489
|
-
trackBudget({
|
|
490
|
-
role: "discover",
|
|
491
|
-
provider: discoverProvider,
|
|
492
|
-
model: config?.roles?.discover?.model || coderRole.model,
|
|
493
|
-
result: discoverOutput,
|
|
494
|
-
duration_ms: Date.now() - discoverStart
|
|
495
|
-
});
|
|
496
|
-
|
|
497
|
-
await addCheckpoint(session, {
|
|
498
|
-
stage: "discover",
|
|
499
|
-
iteration: 0,
|
|
500
|
-
ok: discoverOutput.ok,
|
|
501
|
-
provider: discoverProvider,
|
|
502
|
-
model: config?.roles?.discover?.model || coderRole.model || null
|
|
503
|
-
});
|
|
504
|
-
|
|
505
|
-
const stageResult = {
|
|
506
|
-
ok: discoverOutput.ok,
|
|
507
|
-
verdict: discoverOutput.result?.verdict || null,
|
|
508
|
-
gaps: discoverOutput.result?.gaps || [],
|
|
509
|
-
mode
|
|
510
|
-
};
|
|
511
|
-
|
|
512
|
-
emitProgress(
|
|
513
|
-
emitter,
|
|
514
|
-
makeEvent("discover:end", { ...eventBase, stage: "discover" }, {
|
|
515
|
-
status: discoverOutput.ok ? "ok" : "fail",
|
|
516
|
-
message: discoverOutput.ok ? "Discovery completed" : `Discovery failed: ${discoverOutput.summary}`,
|
|
517
|
-
detail: { ...stageResult, provider: discoverProvider, executorType: "agent" }
|
|
518
|
-
})
|
|
519
|
-
);
|
|
520
|
-
|
|
521
|
-
return { stageResult };
|
|
522
|
-
}
|
|
523
|
-
|
|
524
1
|
/**
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
* @returns {Promise<Array|null>} Array of decomposed HUs or null.
|
|
2
|
+
* Pre-loop stage functions.
|
|
3
|
+
* Implementations have been extracted into ./stages/ sub-modules.
|
|
4
|
+
* This file re-exports everything for backward compatibility.
|
|
529
5
|
*/
|
|
530
|
-
async function decomposeTaskIntoHUs({ config, logger, emitter, eventBase, session, coderRole, trackBudget }) {
|
|
531
|
-
const provider = config?.roles?.hu_reviewer?.provider || coderRole.provider;
|
|
532
|
-
const prompt = buildDecompositionPrompt(session.task);
|
|
533
|
-
|
|
534
|
-
emitProgress(
|
|
535
|
-
emitter,
|
|
536
|
-
makeEvent("hu-reviewer:decompose-start", { ...eventBase, stage: "hu-reviewer" }, {
|
|
537
|
-
message: "Decomposing complex task into formal HUs",
|
|
538
|
-
detail: { provider }
|
|
539
|
-
})
|
|
540
|
-
);
|
|
541
|
-
|
|
542
|
-
const onOutput = ({ stream, line }) => {
|
|
543
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "hu-reviewer" }, {
|
|
544
|
-
message: line,
|
|
545
|
-
detail: { stream, agent: provider }
|
|
546
|
-
}));
|
|
547
|
-
};
|
|
548
|
-
const stall = createStallDetector({
|
|
549
|
-
onOutput, emitter, eventBase, stage: "hu-reviewer", provider
|
|
550
|
-
});
|
|
551
|
-
|
|
552
|
-
const agent = createAgent(provider, config, logger);
|
|
553
|
-
const decompStart = Date.now();
|
|
554
|
-
let result;
|
|
555
|
-
try {
|
|
556
|
-
result = await agent.runTask({ prompt, role: "hu-reviewer", onOutput: stall.onOutput });
|
|
557
|
-
} catch (err) {
|
|
558
|
-
logger.warn(`HU decomposition threw: ${err.message}`);
|
|
559
|
-
return null;
|
|
560
|
-
} finally {
|
|
561
|
-
stall.stop();
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
trackBudget({
|
|
565
|
-
role: "hu-reviewer-decompose",
|
|
566
|
-
provider,
|
|
567
|
-
model: config?.roles?.hu_reviewer?.model || coderRole.model,
|
|
568
|
-
result,
|
|
569
|
-
duration_ms: Date.now() - decompStart
|
|
570
|
-
});
|
|
571
|
-
|
|
572
|
-
if (!result.ok) {
|
|
573
|
-
logger.warn(`HU decomposition failed: ${result.error || "unknown"}`);
|
|
574
|
-
return null;
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
const stories = parseDecompositionOutput(result.output);
|
|
578
|
-
if (!stories || stories.length < 2) {
|
|
579
|
-
logger.info("HU decomposition returned < 2 stories, falling back to single auto-story");
|
|
580
|
-
return null;
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
emitProgress(
|
|
584
|
-
emitter,
|
|
585
|
-
makeEvent("hu-reviewer:decompose-end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
586
|
-
message: `Task decomposed into ${stories.length} HUs`,
|
|
587
|
-
detail: { count: stories.length, ids: stories.map(s => s.id) }
|
|
588
|
-
})
|
|
589
|
-
);
|
|
590
|
-
|
|
591
|
-
return stories;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* Run the HU Reviewer stage: load stories from YAML or generate from task, evaluate, certify, and return in topological order.
|
|
596
|
-
* @param {object} params
|
|
597
|
-
* @returns {Promise<{stageResult: object}>}
|
|
598
|
-
*/
|
|
599
|
-
export async function runHuReviewerStage({ config, logger, emitter, eventBase, session, coderRole, trackBudget, huFile, askQuestion, pgStories = null }) {
|
|
600
|
-
logger.setContext({ iteration: 0, stage: "hu-reviewer" });
|
|
601
|
-
const huReviewerProvider = config?.roles?.hu_reviewer?.provider || coderRole.provider;
|
|
602
|
-
emitProgress(
|
|
603
|
-
emitter,
|
|
604
|
-
makeEvent("hu-reviewer:start", { ...eventBase, stage: "hu-reviewer" }, {
|
|
605
|
-
message: "HU Reviewer certifying user stories",
|
|
606
|
-
detail: { provider: huReviewerProvider, executorType: "agent" }
|
|
607
|
-
})
|
|
608
|
-
);
|
|
609
|
-
|
|
610
|
-
let stories;
|
|
611
|
-
|
|
612
|
-
if (huFile) {
|
|
613
|
-
// --- Load YAML file ---
|
|
614
|
-
const yaml = await import("js-yaml");
|
|
615
|
-
const fs = await import("node:fs/promises");
|
|
616
|
-
let rawYaml;
|
|
617
|
-
try {
|
|
618
|
-
rawYaml = await fs.readFile(huFile, "utf8");
|
|
619
|
-
} catch (err) {
|
|
620
|
-
const stageResult = { ok: false, error: `Could not read HU file: ${err.message}` };
|
|
621
|
-
emitProgress(emitter, makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
622
|
-
status: "fail", message: stageResult.error
|
|
623
|
-
}));
|
|
624
|
-
return { stageResult };
|
|
625
|
-
}
|
|
626
|
-
|
|
627
|
-
try {
|
|
628
|
-
const parsed = yaml.load(rawYaml);
|
|
629
|
-
stories = Array.isArray(parsed) ? parsed : (parsed?.stories || []);
|
|
630
|
-
} catch (err) {
|
|
631
|
-
const stageResult = { ok: false, error: `Invalid YAML in HU file: ${err.message}` };
|
|
632
|
-
emitProgress(emitter, makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
633
|
-
status: "fail", message: stageResult.error
|
|
634
|
-
}));
|
|
635
|
-
return { stageResult };
|
|
636
|
-
}
|
|
637
|
-
} else if (pgStories && pgStories.length > 0) {
|
|
638
|
-
// --- Use pre-built stories from Planning Game card structured data ---
|
|
639
|
-
stories = pgStories;
|
|
640
|
-
logger.info(`HU Reviewer: using ${pgStories.length} story(ies) from PG card`);
|
|
641
|
-
emitProgress(emitter, makeEvent("hu-reviewer:pg-feed", { ...eventBase, stage: "hu-reviewer" }, {
|
|
642
|
-
message: `Using ${pgStories.length} story(ies) from Planning Game card`,
|
|
643
|
-
detail: { source: "pg-card", storyIds: pgStories.map(s => s.id) }
|
|
644
|
-
}));
|
|
645
|
-
} else {
|
|
646
|
-
// --- Decompose task into formal HUs via AI, or fall back to single auto-story ---
|
|
647
|
-
const decomposed = await decomposeTaskIntoHUs({
|
|
648
|
-
config, logger, emitter, eventBase, session, coderRole, trackBudget
|
|
649
|
-
});
|
|
650
|
-
if (decomposed && decomposed.length > 1) {
|
|
651
|
-
stories = decomposed.map((hu, idx) => {
|
|
652
|
-
const isFirst = idx === 0;
|
|
653
|
-
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")}`;
|
|
654
|
-
const skeletonText = `As a ${hu.role}, I want to ${hu.goal}, so that ${hu.benefit}\n\nTitle: ${hu.title}`;
|
|
655
|
-
return {
|
|
656
|
-
id: hu.id,
|
|
657
|
-
text: isFirst ? fullText : skeletonText,
|
|
658
|
-
blocked_by: hu.dependsOn || [],
|
|
659
|
-
needsRefinement: !isFirst
|
|
660
|
-
};
|
|
661
|
-
});
|
|
662
|
-
} else {
|
|
663
|
-
stories = [{ id: "HU-AUTO-001", text: session.task }];
|
|
664
|
-
}
|
|
665
|
-
}
|
|
666
|
-
|
|
667
|
-
if (stories.length === 0) {
|
|
668
|
-
const stageResult = { ok: true, certified: 0, stories: [] };
|
|
669
|
-
emitProgress(emitter, makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
670
|
-
status: "ok", message: "No stories to evaluate"
|
|
671
|
-
}));
|
|
672
|
-
return { stageResult };
|
|
673
|
-
}
|
|
674
|
-
|
|
675
|
-
// --- Create or load batch ---
|
|
676
|
-
const batchSessionId = `hu-${session.id}`;
|
|
677
|
-
let batch;
|
|
678
|
-
try {
|
|
679
|
-
batch = await loadHuBatch(batchSessionId);
|
|
680
|
-
} catch { /* no existing batch */
|
|
681
|
-
batch = await createHuBatch(batchSessionId, stories);
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
// --- Splitting detection: check each pending HU for split indicators before 6D evaluation ---
|
|
685
|
-
const storiesToSplit = [...batch.stories].filter(s => s.status === "pending");
|
|
686
|
-
for (const story of storiesToSplit) {
|
|
687
|
-
const indicators = detectIndicators(story.original.text);
|
|
688
|
-
if (indicators.length === 0) continue;
|
|
689
|
-
|
|
690
|
-
let heuristic = selectHeuristic(indicators);
|
|
691
|
-
if (!heuristic) continue;
|
|
692
|
-
|
|
693
|
-
const triedHeuristics = [];
|
|
694
|
-
let splitAccepted = false;
|
|
695
|
-
|
|
696
|
-
while (heuristic && !splitAccepted) {
|
|
697
|
-
const proposal = await generateSplitProposal(
|
|
698
|
-
{ id: story.id, text: story.original.text },
|
|
699
|
-
heuristic, config, logger
|
|
700
|
-
);
|
|
701
|
-
|
|
702
|
-
if (!proposal) {
|
|
703
|
-
triedHeuristics.push(heuristic);
|
|
704
|
-
heuristic = selectHeuristic(indicators, triedHeuristics);
|
|
705
|
-
continue;
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
if (!askQuestion) {
|
|
709
|
-
// Autonomous mode: auto-accept the split
|
|
710
|
-
const updatedSubHUs = buildSplitDependencies(proposal.subHUs, story);
|
|
711
|
-
const storyIndex = batch.stories.findIndex(s => s.id === story.id);
|
|
712
|
-
const newStories = updatedSubHUs.map(sub => ({
|
|
713
|
-
id: sub.id, status: "pending",
|
|
714
|
-
original: { text: `${sub.title}\n\n${sub.text}\n\nAcceptance Criteria:\n${(sub.acceptanceCriteria || []).map(ac => `- ${ac}`).join("\n")}` },
|
|
715
|
-
blocked_by: sub.blocked_by || [], certified: null, quality: null,
|
|
716
|
-
context_requests: [], created_at: new Date().toISOString(), updated_at: new Date().toISOString()
|
|
717
|
-
}));
|
|
718
|
-
batch.stories.splice(storyIndex, 1, ...newStories);
|
|
719
|
-
splitAccepted = true;
|
|
720
|
-
emitProgress(emitter, makeEvent("hu-reviewer:split-accepted", { ...eventBase, stage: "hu-reviewer" }, {
|
|
721
|
-
message: `Auto-accepted split of ${story.id} into ${updatedSubHUs.length} sub-HUs`,
|
|
722
|
-
detail: { originalId: story.id, subHUs: updatedSubHUs.map(s => s.id), heuristic }
|
|
723
|
-
}));
|
|
724
|
-
} else {
|
|
725
|
-
// Interactive mode: ask FDE for confirmation
|
|
726
|
-
const formatted = formatSplitProposalForFDE(proposal);
|
|
727
|
-
const question = `Split proposed for ${story.id}:\n${formatted}\nAccept? (yes/no/try another)`;
|
|
728
|
-
emitProgress(emitter, makeEvent("hu-reviewer:split-proposal", { ...eventBase, stage: "hu-reviewer" }, {
|
|
729
|
-
message: `Split proposal for ${story.id} using heuristic: ${heuristic}`,
|
|
730
|
-
detail: { originalId: story.id, heuristic, subHUCount: proposal.subHUs.length }
|
|
731
|
-
}));
|
|
732
|
-
const answer = await askQuestion(question, { iteration: 0, stage: "hu-reviewer" });
|
|
733
|
-
if (!answer || answer.toLowerCase().startsWith("yes")) {
|
|
734
|
-
const updatedSubHUs = buildSplitDependencies(proposal.subHUs, story);
|
|
735
|
-
const storyIndex = batch.stories.findIndex(s => s.id === story.id);
|
|
736
|
-
const newStories = updatedSubHUs.map(sub => ({
|
|
737
|
-
id: sub.id, status: "pending",
|
|
738
|
-
original: { text: `${sub.title}\n\n${sub.text}\n\nAcceptance Criteria:\n${(sub.acceptanceCriteria || []).map(ac => `- ${ac}`).join("\n")}` },
|
|
739
|
-
blocked_by: sub.blocked_by || [], certified: null, quality: null,
|
|
740
|
-
context_requests: [], created_at: new Date().toISOString(), updated_at: new Date().toISOString()
|
|
741
|
-
}));
|
|
742
|
-
batch.stories.splice(storyIndex, 1, ...newStories);
|
|
743
|
-
splitAccepted = true;
|
|
744
|
-
emitProgress(emitter, makeEvent("hu-reviewer:split-accepted", { ...eventBase, stage: "hu-reviewer" }, {
|
|
745
|
-
message: `FDE accepted split of ${story.id} into ${updatedSubHUs.length} sub-HUs`,
|
|
746
|
-
detail: { originalId: story.id, subHUs: updatedSubHUs.map(s => s.id), heuristic }
|
|
747
|
-
}));
|
|
748
|
-
} else if (answer.toLowerCase().includes("try") || answer.toLowerCase().startsWith("no")) {
|
|
749
|
-
triedHeuristics.push(heuristic);
|
|
750
|
-
heuristic = selectHeuristic(indicators, triedHeuristics);
|
|
751
|
-
if (!heuristic) {
|
|
752
|
-
updateStoryStatus(batch, story.id, "needs_context");
|
|
753
|
-
}
|
|
754
|
-
continue;
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
break;
|
|
758
|
-
}
|
|
759
|
-
await saveHuBatch(batchSessionId, batch);
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
// --- Evaluate loop (re-evaluate entire batch until all certified or needs_context with no askQuestion) ---
|
|
763
|
-
const huReviewerOnOutput = ({ stream, line }) => {
|
|
764
|
-
emitProgress(emitter, makeEvent("agent:output", { ...eventBase, stage: "hu-reviewer" }, {
|
|
765
|
-
message: line,
|
|
766
|
-
detail: { stream, agent: huReviewerProvider }
|
|
767
|
-
}));
|
|
768
|
-
};
|
|
769
|
-
|
|
770
|
-
let maxRounds = 5;
|
|
771
|
-
let round = 0;
|
|
772
|
-
|
|
773
|
-
while (round < maxRounds) {
|
|
774
|
-
round += 1;
|
|
775
|
-
|
|
776
|
-
const pendingStories = batch.stories.filter(s => s.status === "pending" || s.status === "needs_context");
|
|
777
|
-
if (pendingStories.length === 0) break;
|
|
778
|
-
|
|
779
|
-
const storiesToEvaluate = pendingStories.map(s => ({ id: s.id, text: s.original.text }));
|
|
780
|
-
|
|
781
|
-
const stall = createStallDetector({
|
|
782
|
-
onOutput: huReviewerOnOutput, emitter, eventBase, stage: "hu-reviewer", provider: huReviewerProvider
|
|
783
|
-
});
|
|
784
|
-
|
|
785
|
-
const huReviewer = new HuReviewerRole({ config, logger, emitter, createAgentFn: createAgent });
|
|
786
|
-
await huReviewer.init({ task: session.task, sessionId: session.id, iteration: 0 });
|
|
787
|
-
const reviewStart = Date.now();
|
|
788
|
-
let reviewOutput;
|
|
789
|
-
try {
|
|
790
|
-
reviewOutput = await huReviewer.run({ stories: storiesToEvaluate, onOutput: stall.onOutput });
|
|
791
|
-
} catch (err) {
|
|
792
|
-
logger.warn(`HU Reviewer threw: ${err.message}`);
|
|
793
|
-
reviewOutput = { ok: false, summary: `HU Reviewer error: ${err.message}`, result: { error: err.message } };
|
|
794
|
-
} finally {
|
|
795
|
-
stall.stop();
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
trackBudget({
|
|
799
|
-
role: "hu-reviewer",
|
|
800
|
-
provider: huReviewerProvider,
|
|
801
|
-
model: config?.roles?.hu_reviewer?.model || coderRole.model,
|
|
802
|
-
result: reviewOutput,
|
|
803
|
-
duration_ms: Date.now() - reviewStart
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
if (!reviewOutput.ok || !reviewOutput.result?.evaluations) {
|
|
807
|
-
break;
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
// --- Process evaluations ---
|
|
811
|
-
for (const evaluation of reviewOutput.result.evaluations) {
|
|
812
|
-
const storyId = evaluation.story_id;
|
|
813
|
-
try {
|
|
814
|
-
updateStoryQuality(batch, storyId, evaluation.scores);
|
|
815
|
-
} catch {
|
|
816
|
-
continue; // story not found in batch, skip
|
|
817
|
-
}
|
|
818
|
-
|
|
819
|
-
if (evaluation.verdict === "certified") {
|
|
820
|
-
updateStoryCertified(batch, storyId, evaluation.certified_hu);
|
|
821
|
-
} else if (evaluation.verdict === "needs_context" && evaluation.context_needed) {
|
|
822
|
-
addContextRequest(batch, storyId, {
|
|
823
|
-
fields_needed: evaluation.context_needed.fields_needed || [],
|
|
824
|
-
question: evaluation.context_needed.question_to_fde || ""
|
|
825
|
-
});
|
|
826
|
-
} else if (evaluation.verdict === "needs_rewrite" && evaluation.rewritten) {
|
|
827
|
-
// Accept the rewrite and re-certify
|
|
828
|
-
updateStoryCertified(batch, storyId, evaluation.rewritten);
|
|
829
|
-
} else {
|
|
830
|
-
updateStoryStatus(batch, storyId, "pending");
|
|
831
|
-
}
|
|
832
|
-
}
|
|
833
|
-
|
|
834
|
-
await saveHuBatch(batchSessionId, batch);
|
|
835
|
-
|
|
836
|
-
// --- Check if any need context ---
|
|
837
|
-
const needsContext = batch.stories.filter(s => s.status === "needs_context");
|
|
838
|
-
if (needsContext.length > 0) {
|
|
839
|
-
const consolidatedQuestions = reviewOutput.result.batch_summary?.consolidated_questions
|
|
840
|
-
|| needsContext.map(s => {
|
|
841
|
-
const pending = s.context_requests.find(r => !r.answered_at);
|
|
842
|
-
return pending ? `[${s.id}] ${pending.question_to_fde}` : null;
|
|
843
|
-
}).filter(Boolean).join("\n");
|
|
844
|
-
|
|
845
|
-
if (!askQuestion) {
|
|
846
|
-
// No interactive input — pause session
|
|
847
|
-
break;
|
|
848
|
-
}
|
|
849
|
-
|
|
850
|
-
emitProgress(emitter, makeEvent("hu-reviewer:needs-context", { ...eventBase, stage: "hu-reviewer" }, {
|
|
851
|
-
message: `${needsContext.length} story(ies) need context from FDE`,
|
|
852
|
-
detail: { questions: consolidatedQuestions }
|
|
853
|
-
}));
|
|
854
|
-
|
|
855
|
-
const answer = await askQuestion(
|
|
856
|
-
`The HU Reviewer needs additional context:\n\n${consolidatedQuestions}\n\nPlease provide your answers:`,
|
|
857
|
-
{ iteration: 0, stage: "hu-reviewer" }
|
|
858
|
-
);
|
|
859
|
-
|
|
860
|
-
if (!answer) break;
|
|
861
|
-
|
|
862
|
-
// --- Incorporate FDE answers and re-evaluate ---
|
|
863
|
-
for (const s of needsContext) {
|
|
864
|
-
answerContextRequest(batch, s.id, answer);
|
|
865
|
-
}
|
|
866
|
-
await saveHuBatch(batchSessionId, batch);
|
|
867
|
-
// Loop will re-evaluate entire batch
|
|
868
|
-
}
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
await addCheckpoint(session, {
|
|
872
|
-
stage: "hu-reviewer",
|
|
873
|
-
iteration: 0,
|
|
874
|
-
ok: true,
|
|
875
|
-
certified: batch.stories.filter(s => s.status === "certified").length,
|
|
876
|
-
total: batch.stories.length
|
|
877
|
-
});
|
|
878
|
-
|
|
879
|
-
// --- Return certified stories in topological order ---
|
|
880
|
-
const certifiedStories = batch.stories.filter(s => s.status === "certified");
|
|
881
|
-
let orderedIds;
|
|
882
|
-
try {
|
|
883
|
-
orderedIds = topologicalSort(certifiedStories);
|
|
884
|
-
} catch { /* cyclic dependency */
|
|
885
|
-
orderedIds = certifiedStories.map(s => s.id);
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
const orderedStories = orderedIds.map(id => batch.stories.find(s => s.id === id)).filter(Boolean);
|
|
889
|
-
|
|
890
|
-
const stageResult = {
|
|
891
|
-
ok: true,
|
|
892
|
-
certified: certifiedStories.length,
|
|
893
|
-
total: batch.stories.length,
|
|
894
|
-
needsContext: batch.stories.filter(s => s.status === "needs_context").length,
|
|
895
|
-
stories: orderedStories,
|
|
896
|
-
batchSessionId
|
|
897
|
-
};
|
|
898
|
-
|
|
899
|
-
emitProgress(
|
|
900
|
-
emitter,
|
|
901
|
-
makeEvent("hu-reviewer:end", { ...eventBase, stage: "hu-reviewer" }, {
|
|
902
|
-
status: "ok",
|
|
903
|
-
message: `HU Review complete: ${certifiedStories.length}/${batch.stories.length} certified`,
|
|
904
|
-
detail: { ...stageResult, provider: huReviewerProvider, executorType: "agent" }
|
|
905
|
-
})
|
|
906
|
-
);
|
|
907
6
|
|
|
908
|
-
|
|
909
|
-
}
|
|
7
|
+
export { runTriageStage } from "./stages/triage-stage.js";
|
|
8
|
+
export { runResearcherStage } from "./stages/research-stage.js";
|
|
9
|
+
export { runArchitectStage } from "./stages/architect-stage.js";
|
|
10
|
+
export { runPlannerStage } from "./stages/planner-stage.js";
|
|
11
|
+
export { runDiscoverStage, runHuReviewerStage } from "./stages/hu-reviewer-stage.js";
|