From 8355eb7172c7f792d8bba64422afcc7cebc1f07a Mon Sep 17 00:00:00 2001 From: Shirone Date: Fri, 30 Jan 2026 21:31:45 +0100 Subject: [PATCH] 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). --- .../routes/auto-mode/routes/context-exists.ts | 19 +++++++++++- .../src/routes/auto-mode/routes/status.ts | 31 ++++++++++++++++++- .../src/routes/running-agents/routes/index.ts | 18 +++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/apps/server/src/routes/auto-mode/routes/context-exists.ts b/apps/server/src/routes/auto-mode/routes/context-exists.ts index ef028f3f..8e761094 100644 --- a/apps/server/src/routes/auto-mode/routes/context-exists.ts +++ b/apps/server/src/routes/auto-mode/routes/context-exists.ts @@ -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 => { 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) { diff --git a/apps/server/src/routes/auto-mode/routes/status.ts b/apps/server/src/routes/auto-mode/routes/status.ts index 73c77945..8d519d38 100644 --- a/apps/server/src/routes/auto-mode/routes/status.ts +++ b/apps/server/src/routes/auto-mode/routes/status.ts @@ -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 => { 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(); diff --git a/apps/server/src/routes/running-agents/routes/index.ts b/apps/server/src/routes/running-agents/routes/index.ts index 1eeb4ae6..d5b394f7 100644 --- a/apps/server/src/routes/running-agents/routes/index.ts +++ b/apps/server/src/routes/running-agents/routes/index.ts @@ -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 => { 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();