refactor(06-02): migrate Batch 1 query-only routes to facade pattern

- status.ts: Add facadeFactory parameter for per-project status
- context-exists.ts: Add facadeFactory parameter for context checks
- running-agents/index.ts: Add facade parameter for getRunningAgents

All routes maintain backward compatibility by accepting both
autoModeService (legacy) and facade/facadeFactory (new).
This commit is contained in:
Shirone
2026-01-30 21:31:45 +01:00
parent 4ea35e1743
commit 8355eb7172
3 changed files with 64 additions and 4 deletions

View File

@@ -4,9 +4,17 @@
import type { Request, Response } from 'express';
import type { AutoModeService } from '../../../services/auto-mode-service.js';
import type { AutoModeServiceFacade } from '../../../services/auto-mode/index.js';
import { getErrorMessage, logError } from '../common.js';
export function createContextExistsHandler(autoModeService: AutoModeService) {
/**
* Create context exists handler with transition compatibility.
* Accepts either autoModeService (legacy) or facadeFactory (new).
*/
export function createContextExistsHandler(
autoModeService: AutoModeService,
facadeFactory?: (projectPath: string) => AutoModeServiceFacade
) {
return async (req: Request, res: Response): Promise<void> => {
try {
const { projectPath, featureId } = req.body as {
@@ -22,6 +30,15 @@ export function createContextExistsHandler(autoModeService: AutoModeService) {
return;
}
// Use facade if factory is provided, otherwise fall back to autoModeService
if (facadeFactory) {
const facade = facadeFactory(projectPath);
const exists = await facade.contextExists(featureId);
res.json({ success: true, exists });
return;
}
// Legacy path: use autoModeService directly
const exists = await autoModeService.contextExists(projectPath, featureId);
res.json({ success: true, exists });
} catch (error) {

View File

@@ -7,9 +7,18 @@
import type { Request, Response } from 'express';
import type { AutoModeService } from '../../../services/auto-mode-service.js';
import type { AutoModeServiceFacade } from '../../../services/auto-mode/index.js';
import { getErrorMessage, logError } from '../common.js';
export function createStatusHandler(autoModeService: AutoModeService) {
/**
* Create status handler with transition compatibility.
* Accepts either autoModeService (legacy) or facade (new).
* When facade is provided, creates a per-project facade for the request.
*/
export function createStatusHandler(
autoModeService: AutoModeService,
facadeFactory?: (projectPath: string) => AutoModeServiceFacade
) {
return async (req: Request, res: Response): Promise<void> => {
try {
const { projectPath, branchName } = req.body as {
@@ -21,6 +30,25 @@ export function createStatusHandler(autoModeService: AutoModeService) {
if (projectPath) {
// Normalize branchName: undefined becomes null
const normalizedBranchName = branchName ?? null;
// Use facade if factory is provided, otherwise fall back to autoModeService
if (facadeFactory) {
const facade = facadeFactory(projectPath);
const projectStatus = facade.getStatusForProject(normalizedBranchName);
res.json({
success: true,
isRunning: projectStatus.runningCount > 0,
isAutoLoopRunning: projectStatus.isAutoLoopRunning,
runningFeatures: projectStatus.runningFeatures,
runningCount: projectStatus.runningCount,
maxConcurrency: projectStatus.maxConcurrency,
projectPath,
branchName: normalizedBranchName,
});
return;
}
// Legacy path: use autoModeService directly
const projectStatus = autoModeService.getStatusForProject(
projectPath,
normalizedBranchName
@@ -39,6 +67,7 @@ export function createStatusHandler(autoModeService: AutoModeService) {
}
// Fall back to global status for backward compatibility
// Global status uses autoModeService (facade is per-project)
const status = autoModeService.getStatus();
const activeProjects = autoModeService.getActiveAutoLoopProjects();
const activeWorktrees = autoModeService.getActiveAutoLoopWorktrees();

View File

@@ -4,15 +4,29 @@
import type { Request, Response } from 'express';
import type { AutoModeService } from '../../../services/auto-mode-service.js';
import type { AutoModeServiceFacade } from '../../../services/auto-mode/index.js';
import { getBacklogPlanStatus, getRunningDetails } from '../../backlog-plan/common.js';
import { getAllRunningGenerations } from '../../app-spec/common.js';
import path from 'path';
import { getErrorMessage, logError } from '../common.js';
export function createIndexHandler(autoModeService: AutoModeService) {
/**
* Create index handler with transition compatibility.
* Accepts either autoModeService (legacy) or facade (new).
* Note: getRunningAgents is global (not per-project), so facade is created
* with an empty path for global queries.
*/
export function createIndexHandler(
autoModeService: AutoModeService,
facade?: AutoModeServiceFacade
) {
return async (_req: Request, res: Response): Promise<void> => {
try {
const runningAgents = [...(await autoModeService.getRunningAgents())];
// Use facade if provided, otherwise fall back to autoModeService
const runningAgents = facade
? [...(await facade.getRunningAgents())]
: [...(await autoModeService.getRunningAgents())];
const backlogPlanStatus = getBacklogPlanStatus();
const backlogPlanDetails = getRunningDetails();