feat: wire quality review into orchestrator handleAgentStopped

When an agent stops, check whether a quality review should run before
auto-completing the task. If shouldRunQualityReview returns run:true,
delegate to runQualityReview (which transitions task to quality_review
and spawns a review agent) instead of calling completeTask directly.

Falls back to completeTask when agentRepository or agentManager are
not injected, or when the task lacks phaseId/initiativeId context.

- Add agentManager optional param to ExecutionOrchestrator constructor
- Extract tryQualityReview() private method to compute branch names and
  repo path before delegating to the quality-review service
- Pass agentManager to ExecutionOrchestrator in container.ts
- Add orchestrator integration tests for the agent:stopped quality hook

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lukas May
2026-03-06 22:05:42 +01:00
parent 9200891a5d
commit 4bc65bfe3d
3 changed files with 203 additions and 6 deletions

View File

@@ -18,12 +18,14 @@ import type { TaskRepository } from '../db/repositories/task-repository.js';
import type { InitiativeRepository } from '../db/repositories/initiative-repository.js';
import type { ProjectRepository } from '../db/repositories/project-repository.js';
import type { AgentRepository } from '../db/repositories/agent-repository.js';
import type { AgentManager } from '../agent/types.js';
import type { DispatchManager, PhaseDispatchManager } from '../dispatch/types.js';
import type { ConflictResolutionService } from '../coordination/conflict-resolution-service.js';
import { phaseBranchName, taskBranchName } from '../git/branch-naming.js';
import { ensureProjectClone } from '../git/project-clones.js';
import { createModuleLogger } from '../logger/index.js';
import { phaseMetaCache, fileDiffCache } from '../review/diff-cache.js';
import { shouldRunQualityReview, runQualityReview } from './quality-review.js';
const log = createModuleLogger('execution-orchestrator');
@@ -50,6 +52,7 @@ export class ExecutionOrchestrator {
private eventBus: EventBus,
private workspaceRoot: string,
private agentRepository?: AgentRepository,
private agentManager?: AgentManager,
) {}
/**
@@ -108,15 +111,17 @@ export class ExecutionOrchestrator {
private async handleAgentStopped(event: AgentStoppedEvent): Promise<void> {
const { taskId, reason, agentId } = event.payload;
// Auto-complete task for successful agent completions, not manual stops
if (taskId && reason !== 'user_requested') {
try {
await this.dispatchManager.completeTask(taskId, agentId);
log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop');
const result = await this.tryQualityReview(taskId, agentId, reason);
if (!result.reviewStarted) {
await this.dispatchManager.completeTask(taskId, agentId);
log.info({ taskId, agentId, reason }, 'task auto-completed on agent stop');
}
} catch (err) {
log.warn(
{ taskId, agentId, reason, err: err instanceof Error ? err.message : String(err) },
'failed to auto-complete task on agent stop',
'failed to handle agent stop',
);
}
}
@@ -124,6 +129,72 @@ export class ExecutionOrchestrator {
this.scheduleDispatch();
}
/**
* Attempt to run quality review for a stopping agent.
* Returns { reviewStarted: true } if quality review was initiated (callers must NOT call completeTask).
* Returns { reviewStarted: false } if no review needed (caller should call completeTask).
*/
private async tryQualityReview(taskId: string, agentId: string, reason: string): Promise<{ reviewStarted: boolean }> {
if (!this.agentRepository || !this.agentManager) {
return { reviewStarted: false };
}
const task = await this.taskRepository.findById(taskId);
if (!task?.phaseId || !task.initiativeId) {
return { reviewStarted: false };
}
const initiative = await this.initiativeRepository.findById(task.initiativeId);
if (!initiative?.branch) {
return { reviewStarted: false };
}
const phase = await this.phaseRepository.findById(task.phaseId);
if (!phase) {
return { reviewStarted: false };
}
const taskBranch = taskBranchName(initiative.branch, taskId);
const baseBranch = phaseBranchName(initiative.branch, phase.name);
const projects = await this.projectRepository.findProjectsByInitiativeId(task.initiativeId);
if (projects.length === 0) {
return { reviewStarted: false };
}
const repoPath = await ensureProjectClone(projects[0], this.workspaceRoot);
const result = await shouldRunQualityReview({
agentId,
taskId,
stopReason: reason,
repoPath,
taskBranch,
baseBranch,
agentRepository: this.agentRepository,
taskRepository: this.taskRepository,
initiativeRepository: this.initiativeRepository,
branchManager: this.branchManager,
});
if (!result.run) {
return { reviewStarted: false };
}
await runQualityReview({
taskId,
taskBranch,
baseBranch,
initiativeId: task.initiativeId,
qualifyingFiles: result.qualifyingFiles,
taskRepository: this.taskRepository,
agentManager: this.agentManager,
log,
});
return { reviewStarted: true };
}
private async handleAgentCrashed(event: AgentCrashedEvent): Promise<void> {
const { taskId, agentId, error } = event.payload;
if (!taskId) return;