From de021f96bf8683b2be6edb5a011076e556d4416c Mon Sep 17 00:00:00 2001 From: gsxdsm Date: Tue, 17 Feb 2026 13:18:40 -0800 Subject: [PATCH] fix: Remove unused vars and improve type safety. Improve task recovery --- apps/server/src/index.ts | 24 +- apps/server/src/lib/cli-detection.ts | 5 +- apps/server/src/lib/error-handler.ts | 21 +- apps/server/src/lib/permission-enforcer.ts | 15 +- apps/server/src/lib/worktree-metadata.ts | 2 +- apps/server/src/providers/claude-provider.ts | 41 +-- apps/server/src/providers/codex-provider.ts | 23 +- apps/server/src/providers/copilot-provider.ts | 4 - apps/server/src/providers/cursor-provider.ts | 3 +- apps/server/src/providers/gemini-provider.ts | 1 - .../src/providers/simple-query-service.ts | 2 - apps/server/src/routes/agent/routes/start.ts | 2 +- apps/server/src/routes/app-spec/common.ts | 2 +- .../app-spec/generate-features-from-spec.ts | 2 +- apps/server/src/routes/app-spec/sync-spec.ts | 1 - .../src/routes/backlog-plan/generate-plan.ts | 2 +- .../src/routes/backlog-plan/routes/apply.ts | 2 +- .../src/routes/features/routes/export.ts | 2 +- .../routes/features/routes/generate-title.ts | 2 +- .../src/routes/features/routes/import.ts | 2 +- apps/server/src/routes/fs/routes/mkdir.ts | 8 +- .../src/routes/fs/routes/resolve-directory.ts | 6 +- .../routes/fs/routes/save-board-background.ts | 3 +- .../server/src/routes/fs/routes/save-image.ts | 3 +- .../src/routes/fs/routes/validate-path.ts | 2 +- apps/server/src/routes/gemini/index.ts | 11 +- .../github/routes/validation-endpoints.ts | 2 - .../src/routes/models/routes/providers.ts | 2 +- .../routes/settings/routes/update-global.ts | 12 +- .../src/routes/setup/routes/auth-claude.ts | 4 - .../src/routes/setup/routes/auth-opencode.ts | 4 - .../src/routes/setup/routes/copilot-models.ts | 3 - .../routes/setup/routes/opencode-models.ts | 3 - .../routes/setup/routes/verify-claude-auth.ts | 8 +- apps/server/src/routes/terminal/common.ts | 1 - .../server/src/routes/terminal/routes/auth.ts | 1 - .../routes/worktree/routes/branch-tracking.ts | 4 +- .../src/routes/worktree/routes/create-pr.ts | 6 +- .../src/routes/worktree/routes/delete.ts | 2 +- apps/server/src/routes/zai/index.ts | 73 +----- apps/server/src/services/agent-executor.ts | 6 - apps/server/src/services/agent-service.ts | 2 - .../src/services/auto-loop-coordinator.ts | 3 - .../src/services/auto-mode/global-service.ts | 1 - .../src/services/claude-usage-service.ts | 3 - .../server/src/services/dev-server-service.ts | 2 +- .../src/services/event-history-service.ts | 7 +- apps/server/src/services/execution-service.ts | 1 - .../src/services/feature-export-service.ts | 1 - apps/server/src/services/feature-loader.ts | 3 +- .../src/services/gemini-usage-service.ts | 126 +++++++--- apps/server/src/services/ideation-service.ts | 38 ++- apps/server/src/services/recovery-service.ts | 43 +++- apps/server/src/services/zai-usage-service.ts | 238 ++++++++++++++++-- apps/server/src/tests/cli-integration.test.ts | 4 +- .../unit/services/recovery-service.test.ts | 120 +++++++++ apps/ui/src/components/usage-popover.tsx | 12 +- .../components/kanban-card/card-actions.tsx | 201 +++++++++------ .../components/kanban-card/kanban-card.tsx | 12 +- .../components/list-view/list-row.tsx | 12 +- .../components/list-view/row-actions.tsx | 202 +++++++++------ .../views/board-view/mobile-usage-bar.tsx | 14 +- .../api-keys/hooks/use-api-key-management.ts | 104 ++++++-- apps/ui/src/hooks/use-auto-mode.ts | 5 +- apps/ui/src/hooks/use-provider-auth-init.ts | 31 ++- apps/ui/src/lib/http-api-client.ts | 37 +-- apps/ui/src/store/app-store.ts | 9 + apps/ui/src/store/types/state-types.ts | 9 +- 68 files changed, 1028 insertions(+), 534 deletions(-) diff --git a/apps/server/src/index.ts b/apps/server/src/index.ts index ce8651bd..eda71d44 100644 --- a/apps/server/src/index.ts +++ b/apps/server/src/index.ts @@ -303,7 +303,7 @@ app.use( callback(null, origin); return; } - } catch (err) { + } catch { // Ignore URL parsing errors } @@ -376,7 +376,7 @@ eventHookService.initialize(events, settingsService, eventHistoryService, featur let globalSettings: Awaited> | null = null; try { globalSettings = await settingsService.getGlobalSettings(); - } catch (err) { + } catch { logger.warn('Failed to load global settings, using defaults'); } @@ -394,7 +394,7 @@ eventHookService.initialize(events, settingsService, eventHistoryService, featur const enableRequestLog = globalSettings.enableRequestLogging ?? true; setRequestLoggingEnabled(enableRequestLog); logger.info(`HTTP request logging: ${enableRequestLog ? 'enabled' : 'disabled'}`); - } catch (err) { + } catch { logger.warn('Failed to apply logging settings, using defaults'); } } @@ -421,6 +421,22 @@ eventHookService.initialize(events, settingsService, eventHistoryService, featur } else { logger.info('[STARTUP] Feature state reconciliation complete - no stale states found'); } + + // Resume interrupted features in the background after reconciliation. + // This uses the saved execution state to identify features that were running + // before the restart (their statuses have been reset to ready/backlog by + // reconciliation above). Running in background so it doesn't block startup. + if (totalReconciled > 0) { + for (const project of globalSettings.projects) { + autoModeService.resumeInterruptedFeatures(project.path).catch((err) => { + logger.warn( + `[STARTUP] Failed to resume interrupted features for ${project.path}:`, + err + ); + }); + } + logger.info('[STARTUP] Initiated background resume of interrupted features'); + } } } catch (err) { logger.warn('[STARTUP] Failed to reconcile feature states:', err); @@ -581,7 +597,7 @@ wss.on('connection', (ws: WebSocket) => { logger.info('Sending event to client:', { type, messageLength: message.length, - sessionId: (payload as any)?.sessionId, + sessionId: (payload as Record)?.sessionId, }); ws.send(message); } else { diff --git a/apps/server/src/lib/cli-detection.ts b/apps/server/src/lib/cli-detection.ts index eba4c68a..a7b5b14d 100644 --- a/apps/server/src/lib/cli-detection.ts +++ b/apps/server/src/lib/cli-detection.ts @@ -8,9 +8,6 @@ import { spawn, execSync } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; -import { createLogger } from '@automaker/utils'; - -const logger = createLogger('CliDetection'); export interface CliInfo { name: string; @@ -86,7 +83,7 @@ export async function detectCli( options: CliDetectionOptions = {} ): Promise { const config = CLI_CONFIGS[provider]; - const { timeout = 5000, includeWsl = false, wslDistribution } = options; + const { timeout = 5000 } = options; const issues: string[] = []; const cliInfo: CliInfo = { diff --git a/apps/server/src/lib/error-handler.ts b/apps/server/src/lib/error-handler.ts index 770f26a2..d6720098 100644 --- a/apps/server/src/lib/error-handler.ts +++ b/apps/server/src/lib/error-handler.ts @@ -40,7 +40,7 @@ export interface ErrorClassification { suggestedAction?: string; retryable: boolean; provider?: string; - context?: Record; + context?: Record; } export interface ErrorPattern { @@ -180,7 +180,7 @@ const ERROR_PATTERNS: ErrorPattern[] = [ export function classifyError( error: unknown, provider?: string, - context?: Record + context?: Record ): ErrorClassification { const errorText = getErrorText(error); @@ -281,18 +281,19 @@ function getErrorText(error: unknown): string { if (typeof error === 'object' && error !== null) { // Handle structured error objects - const errorObj = error as any; + const errorObj = error as Record; - if (errorObj.message) { + if (typeof errorObj.message === 'string') { return errorObj.message; } - if (errorObj.error?.message) { - return errorObj.error.message; + const nestedError = errorObj.error; + if (typeof nestedError === 'object' && nestedError !== null && 'message' in nestedError) { + return String((nestedError as Record).message); } - if (errorObj.error) { - return typeof errorObj.error === 'string' ? errorObj.error : JSON.stringify(errorObj.error); + if (nestedError) { + return typeof nestedError === 'string' ? nestedError : JSON.stringify(nestedError); } return JSON.stringify(error); @@ -307,7 +308,7 @@ function getErrorText(error: unknown): string { export function createErrorResponse( error: unknown, provider?: string, - context?: Record + context?: Record ): { success: false; error: string; @@ -335,7 +336,7 @@ export function logError( error: unknown, provider?: string, operation?: string, - additionalContext?: Record + additionalContext?: Record ): void { const classification = classifyError(error, provider, { operation, diff --git a/apps/server/src/lib/permission-enforcer.ts b/apps/server/src/lib/permission-enforcer.ts index 003608ee..714f7d40 100644 --- a/apps/server/src/lib/permission-enforcer.ts +++ b/apps/server/src/lib/permission-enforcer.ts @@ -12,11 +12,18 @@ export interface PermissionCheckResult { reason?: string; } +/** Minimal shape of a Cursor tool call used for permission checking */ +interface CursorToolCall { + shellToolCall?: { args?: { command: string } }; + readToolCall?: { args?: { path: string } }; + writeToolCall?: { args?: { path: string } }; +} + /** * Check if a tool call is allowed based on permissions */ export function checkToolCallPermission( - toolCall: any, + toolCall: CursorToolCall, permissions: CursorCliConfigFile | null ): PermissionCheckResult { if (!permissions || !permissions.permissions) { @@ -152,7 +159,11 @@ function matchesRule(toolName: string, rule: string): boolean { /** * Log permission violations */ -export function logPermissionViolation(toolCall: any, reason: string, sessionId?: string): void { +export function logPermissionViolation( + toolCall: CursorToolCall, + reason: string, + sessionId?: string +): void { const sessionIdStr = sessionId ? ` [${sessionId}]` : ''; if (toolCall.shellToolCall?.args?.command) { diff --git a/apps/server/src/lib/worktree-metadata.ts b/apps/server/src/lib/worktree-metadata.ts index 4742a5b0..aa6e2487 100644 --- a/apps/server/src/lib/worktree-metadata.ts +++ b/apps/server/src/lib/worktree-metadata.ts @@ -78,7 +78,7 @@ export async function readWorktreeMetadata( const metadataPath = getWorktreeMetadataPath(projectPath, branch); const content = (await secureFs.readFile(metadataPath, 'utf-8')) as string; return JSON.parse(content) as WorktreeMetadata; - } catch (error) { + } catch (_error) { // File doesn't exist or can't be read return null; } diff --git a/apps/server/src/providers/claude-provider.ts b/apps/server/src/providers/claude-provider.ts index 3d4f88cd..fa3eb923 100644 --- a/apps/server/src/providers/claude-provider.ts +++ b/apps/server/src/providers/claude-provider.ts @@ -5,7 +5,7 @@ * with the provider architecture. */ -import { query, type Options } from '@anthropic-ai/claude-agent-sdk'; +import { query, type Options, type SDKUserMessage } from '@anthropic-ai/claude-agent-sdk'; import { BaseProvider } from './base-provider.js'; import { classifyError, getUserFriendlyErrorMessage, createLogger } from '@automaker/utils'; @@ -32,31 +32,6 @@ import type { ModelDefinition, } from './types.js'; -// Explicit allowlist of environment variables to pass to the SDK. -// Only these vars are passed - nothing else from process.env leaks through. -const ALLOWED_ENV_VARS = [ - // Authentication - 'ANTHROPIC_API_KEY', - 'ANTHROPIC_AUTH_TOKEN', - // Endpoint configuration - 'ANTHROPIC_BASE_URL', - 'API_TIMEOUT_MS', - // Model mappings - 'ANTHROPIC_DEFAULT_HAIKU_MODEL', - 'ANTHROPIC_DEFAULT_SONNET_MODEL', - 'ANTHROPIC_DEFAULT_OPUS_MODEL', - // Traffic control - 'CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC', - // System vars (always from process.env) - 'PATH', - 'HOME', - 'SHELL', - 'TERM', - 'USER', - 'LANG', - 'LC_ALL', -]; - // System vars are always passed from process.env regardless of profile const SYSTEM_ENV_VARS = ['PATH', 'HOME', 'SHELL', 'TERM', 'USER', 'LANG', 'LC_ALL']; @@ -258,7 +233,7 @@ export class ClaudeProvider extends BaseProvider { }; // Build prompt payload - let promptPayload: string | AsyncIterable; + let promptPayload: string | AsyncIterable; if (Array.isArray(prompt)) { // Multi-part prompt (with images) @@ -317,12 +292,16 @@ export class ClaudeProvider extends BaseProvider { ? `${userMessage}\n\nTip: If you're running multiple features in auto-mode, consider reducing concurrency (maxConcurrency setting) to avoid hitting rate limits.` : userMessage; - const enhancedError = new Error(message); - (enhancedError as any).originalError = error; - (enhancedError as any).type = errorInfo.type; + const enhancedError = new Error(message) as Error & { + originalError: unknown; + type: string; + retryAfter?: number; + }; + enhancedError.originalError = error; + enhancedError.type = errorInfo.type; if (errorInfo.isRateLimit) { - (enhancedError as any).retryAfter = errorInfo.retryAfter; + enhancedError.retryAfter = errorInfo.retryAfter; } throw enhancedError; diff --git a/apps/server/src/providers/codex-provider.ts b/apps/server/src/providers/codex-provider.ts index 5c200ea5..f499db41 100644 --- a/apps/server/src/providers/codex-provider.ts +++ b/apps/server/src/providers/codex-provider.ts @@ -30,7 +30,6 @@ import type { ModelDefinition, } from './types.js'; import { - CODEX_MODEL_MAP, supportsReasoningEffort, validateBareModelId, calculateReasoningTimeout, @@ -56,15 +55,9 @@ const CODEX_EXEC_SUBCOMMAND = 'exec'; const CODEX_JSON_FLAG = '--json'; const CODEX_MODEL_FLAG = '--model'; const CODEX_VERSION_FLAG = '--version'; -const CODEX_SANDBOX_FLAG = '--sandbox'; -const CODEX_APPROVAL_FLAG = '--ask-for-approval'; -const CODEX_SEARCH_FLAG = '--search'; -const CODEX_OUTPUT_SCHEMA_FLAG = '--output-schema'; const CODEX_CONFIG_FLAG = '--config'; -const CODEX_IMAGE_FLAG = '--image'; const CODEX_ADD_DIR_FLAG = '--add-dir'; const CODEX_SKIP_GIT_REPO_CHECK_FLAG = '--skip-git-repo-check'; -const CODEX_RESUME_FLAG = 'resume'; const CODEX_REASONING_EFFORT_KEY = 'reasoning_effort'; const CODEX_YOLO_FLAG = '--dangerously-bypass-approvals-and-sandbox'; const OPENAI_API_KEY_ENV = 'OPENAI_API_KEY'; @@ -106,9 +99,6 @@ const TEXT_ENCODING = 'utf-8'; */ const CODEX_CLI_TIMEOUT_MS = DEFAULT_TIMEOUT_MS; const CODEX_FEATURE_GENERATION_BASE_TIMEOUT_MS = 300000; // 5 minutes for feature generation -const CONTEXT_WINDOW_256K = 256000; -const MAX_OUTPUT_32K = 32000; -const MAX_OUTPUT_16K = 16000; const SYSTEM_PROMPT_SEPARATOR = '\n\n'; const CODEX_INSTRUCTIONS_DIR = '.codex'; const CODEX_INSTRUCTIONS_SECTION = 'Codex Project Instructions'; @@ -758,17 +748,14 @@ export class CodexProvider extends BaseProvider { options.cwd, codexSettings.sandboxMode !== 'danger-full-access' ); - const resolvedSandboxMode = sandboxCheck.enabled - ? codexSettings.sandboxMode - : 'danger-full-access'; if (!sandboxCheck.enabled && sandboxCheck.message) { console.warn(`[CodexProvider] ${sandboxCheck.message}`); } const searchEnabled = codexSettings.enableWebSearch || resolveSearchEnabled(resolvedAllowedTools, restrictTools); - const outputSchemaPath = await writeOutputSchemaFile(options.cwd, options.outputFormat); + await writeOutputSchemaFile(options.cwd, options.outputFormat); const imageBlocks = codexSettings.enableImages ? extractImageBlocks(options.prompt) : []; - const imagePaths = await writeImageFiles(options.cwd, imageBlocks); + await writeImageFiles(options.cwd, imageBlocks); const approvalPolicy = hasMcpServers && options.mcpAutoApproveTools !== undefined ? options.mcpAutoApproveTools @@ -801,7 +788,7 @@ export class CodexProvider extends BaseProvider { overrides.push({ key: 'features.web_search_request', value: true }); } - const configOverrides = buildConfigOverrides(overrides); + buildConfigOverrides(overrides); const preExecArgs: string[] = []; // Add additional directories with write access @@ -1033,7 +1020,7 @@ export class CodexProvider extends BaseProvider { async detectInstallation(): Promise { const cliPath = await findCodexCliPath(); const hasApiKey = Boolean(await resolveOpenAiApiKey()); - const authIndicators = await getCodexAuthIndicators(); + await getCodexAuthIndicators(); const installed = !!cliPath; let version = ''; @@ -1045,7 +1032,7 @@ export class CodexProvider extends BaseProvider { cwd: process.cwd(), }); version = result.stdout.trim(); - } catch (error) { + } catch { version = ''; } } diff --git a/apps/server/src/providers/copilot-provider.ts b/apps/server/src/providers/copilot-provider.ts index 64423047..ad6e155e 100644 --- a/apps/server/src/providers/copilot-provider.ts +++ b/apps/server/src/providers/copilot-provider.ts @@ -85,10 +85,6 @@ interface SdkToolExecutionEndEvent extends SdkEvent { }; } -interface SdkSessionIdleEvent extends SdkEvent { - type: 'session.idle'; -} - interface SdkSessionErrorEvent extends SdkEvent { type: 'session.error'; data: { diff --git a/apps/server/src/providers/cursor-provider.ts b/apps/server/src/providers/cursor-provider.ts index a2e813c0..7d965db4 100644 --- a/apps/server/src/providers/cursor-provider.ts +++ b/apps/server/src/providers/cursor-provider.ts @@ -69,6 +69,7 @@ interface CursorToolHandler { * Registry of Cursor tool handlers * Each handler knows how to normalize its specific tool call type */ +// eslint-disable-next-line @typescript-eslint/no-explicit-any -- handler registry stores heterogeneous tool type parameters const CURSOR_TOOL_HANDLERS: Record> = { readToolCall: { name: 'Read', @@ -878,7 +879,7 @@ export class CursorProvider extends CliProvider { logger.debug(`CursorProvider.executeQuery called with model: "${options.model}"`); // Get effective permissions for this project - const effectivePermissions = await getEffectivePermissions(options.cwd || process.cwd()); + await getEffectivePermissions(options.cwd || process.cwd()); // Debug: log raw events when AUTOMAKER_DEBUG_RAW_OUTPUT is enabled const debugRawEvents = diff --git a/apps/server/src/providers/gemini-provider.ts b/apps/server/src/providers/gemini-provider.ts index 09f16c16..764c57eb 100644 --- a/apps/server/src/providers/gemini-provider.ts +++ b/apps/server/src/providers/gemini-provider.ts @@ -20,7 +20,6 @@ import type { ProviderMessage, InstallationStatus, ModelDefinition, - ContentBlock, } from './types.js'; import { validateBareModelId } from '@automaker/types'; import { GEMINI_MODEL_MAP, type GeminiAuthStatus } from '@automaker/types'; diff --git a/apps/server/src/providers/simple-query-service.ts b/apps/server/src/providers/simple-query-service.ts index 85c25235..1250e6f8 100644 --- a/apps/server/src/providers/simple-query-service.ts +++ b/apps/server/src/providers/simple-query-service.ts @@ -16,8 +16,6 @@ import { ProviderFactory } from './provider-factory.js'; import type { - ProviderMessage, - ContentBlock, ThinkingLevel, ReasoningEffort, ClaudeApiProfile, diff --git a/apps/server/src/routes/agent/routes/start.ts b/apps/server/src/routes/agent/routes/start.ts index 1023fa38..dd9b7e41 100644 --- a/apps/server/src/routes/agent/routes/start.ts +++ b/apps/server/src/routes/agent/routes/start.ts @@ -6,7 +6,7 @@ import type { Request, Response } from 'express'; import { AgentService } from '../../../services/agent-service.js'; import { createLogger } from '@automaker/utils'; import { getErrorMessage, logError } from '../common.js'; -const logger = createLogger('Agent'); +const _logger = createLogger('Agent'); export function createStartHandler(agentService: AgentService) { return async (req: Request, res: Response): Promise => { diff --git a/apps/server/src/routes/app-spec/common.ts b/apps/server/src/routes/app-spec/common.ts index 1a48fc6a..0731a7dd 100644 --- a/apps/server/src/routes/app-spec/common.ts +++ b/apps/server/src/routes/app-spec/common.ts @@ -128,7 +128,7 @@ export function logAuthStatus(context: string): void { */ export function logError(error: unknown, context: string): void { logger.error(`❌ ${context}:`); - logger.error('Error name:', (error as any)?.name); + logger.error('Error name:', (error as Error)?.name); logger.error('Error message:', (error as Error)?.message); logger.error('Error stack:', (error as Error)?.stack); logger.error('Full error object:', JSON.stringify(error, Object.getOwnPropertyNames(error), 2)); diff --git a/apps/server/src/routes/app-spec/generate-features-from-spec.ts b/apps/server/src/routes/app-spec/generate-features-from-spec.ts index 6558256b..93daeb8e 100644 --- a/apps/server/src/routes/app-spec/generate-features-from-spec.ts +++ b/apps/server/src/routes/app-spec/generate-features-from-spec.ts @@ -30,7 +30,7 @@ const DEFAULT_MAX_FEATURES = 50; * Timeout for Codex models when generating features (5 minutes). * Codex models are slower and need more time to generate 50+ features. */ -const CODEX_FEATURE_GENERATION_TIMEOUT_MS = 300000; // 5 minutes +const _CODEX_FEATURE_GENERATION_TIMEOUT_MS = 300000; // 5 minutes /** * Type for extracted features JSON response diff --git a/apps/server/src/routes/app-spec/sync-spec.ts b/apps/server/src/routes/app-spec/sync-spec.ts index d1ba139d..53bdc91a 100644 --- a/apps/server/src/routes/app-spec/sync-spec.ts +++ b/apps/server/src/routes/app-spec/sync-spec.ts @@ -29,7 +29,6 @@ import { updateTechnologyStack, updateRoadmapPhaseStatus, type ImplementedFeature, - type RoadmapPhase, } from '../../lib/xml-extractor.js'; import { getNotificationService } from '../../services/notification-service.js'; diff --git a/apps/server/src/routes/backlog-plan/generate-plan.ts b/apps/server/src/routes/backlog-plan/generate-plan.ts index 2bd3a6a7..c2548f24 100644 --- a/apps/server/src/routes/backlog-plan/generate-plan.ts +++ b/apps/server/src/routes/backlog-plan/generate-plan.ts @@ -6,7 +6,7 @@ */ import type { EventEmitter } from '../../lib/events.js'; -import type { Feature, BacklogPlanResult, BacklogChange, DependencyUpdate } from '@automaker/types'; +import type { Feature, BacklogPlanResult } from '@automaker/types'; import { DEFAULT_PHASE_MODELS, isCursorModel, diff --git a/apps/server/src/routes/backlog-plan/routes/apply.ts b/apps/server/src/routes/backlog-plan/routes/apply.ts index 1a238d17..dacd156a 100644 --- a/apps/server/src/routes/backlog-plan/routes/apply.ts +++ b/apps/server/src/routes/backlog-plan/routes/apply.ts @@ -3,7 +3,7 @@ */ import type { Request, Response } from 'express'; -import type { BacklogPlanResult, BacklogChange, Feature } from '@automaker/types'; +import type { BacklogPlanResult } from '@automaker/types'; import { FeatureLoader } from '../../../services/feature-loader.js'; import { clearBacklogPlan, getErrorMessage, logError, logger } from '../common.js'; diff --git a/apps/server/src/routes/features/routes/export.ts b/apps/server/src/routes/features/routes/export.ts index c767dda4..28a048b4 100644 --- a/apps/server/src/routes/features/routes/export.ts +++ b/apps/server/src/routes/features/routes/export.ts @@ -36,7 +36,7 @@ interface ExportRequest { }; } -export function createExportHandler(featureLoader: FeatureLoader) { +export function createExportHandler(_featureLoader: FeatureLoader) { const exportService = getFeatureExportService(); return async (req: Request, res: Response): Promise => { diff --git a/apps/server/src/routes/features/routes/generate-title.ts b/apps/server/src/routes/features/routes/generate-title.ts index 4e5e0dcb..a84680b0 100644 --- a/apps/server/src/routes/features/routes/generate-title.ts +++ b/apps/server/src/routes/features/routes/generate-title.ts @@ -34,7 +34,7 @@ export function createGenerateTitleHandler( ): (req: Request, res: Response) => Promise { return async (req: Request, res: Response): Promise => { try { - const { description, projectPath } = req.body as GenerateTitleRequestBody; + const { description } = req.body as GenerateTitleRequestBody; if (!description || typeof description !== 'string') { const response: GenerateTitleErrorResponse = { diff --git a/apps/server/src/routes/features/routes/import.ts b/apps/server/src/routes/features/routes/import.ts index 85fb6d9b..aa8cfce1 100644 --- a/apps/server/src/routes/features/routes/import.ts +++ b/apps/server/src/routes/features/routes/import.ts @@ -33,7 +33,7 @@ interface ConflictInfo { hasConflict: boolean; } -export function createImportHandler(featureLoader: FeatureLoader) { +export function createImportHandler(_featureLoader: FeatureLoader) { const exportService = getFeatureExportService(); return async (req: Request, res: Response): Promise => { diff --git a/apps/server/src/routes/fs/routes/mkdir.ts b/apps/server/src/routes/fs/routes/mkdir.ts index 04d0a836..f813abcd 100644 --- a/apps/server/src/routes/fs/routes/mkdir.ts +++ b/apps/server/src/routes/fs/routes/mkdir.ts @@ -35,9 +35,9 @@ export function createMkdirHandler() { error: 'Path exists and is not a directory', }); return; - } catch (statError: any) { + } catch (statError: unknown) { // ENOENT means path doesn't exist - we should create it - if (statError.code !== 'ENOENT') { + if ((statError as NodeJS.ErrnoException).code !== 'ENOENT') { // Some other error (could be ELOOP in parent path) throw statError; } @@ -47,7 +47,7 @@ export function createMkdirHandler() { await secureFs.mkdir(resolvedPath, { recursive: true }); res.json({ success: true }); - } catch (error: any) { + } catch (error: unknown) { // Path not allowed - return 403 Forbidden if (error instanceof PathNotAllowedError) { res.status(403).json({ success: false, error: getErrorMessage(error) }); @@ -55,7 +55,7 @@ export function createMkdirHandler() { } // Handle ELOOP specifically - if (error.code === 'ELOOP') { + if ((error as NodeJS.ErrnoException).code === 'ELOOP') { logError(error, 'Create directory failed - symlink loop detected'); res.status(400).json({ success: false, diff --git a/apps/server/src/routes/fs/routes/resolve-directory.ts b/apps/server/src/routes/fs/routes/resolve-directory.ts index 5e4147db..be5a5b0d 100644 --- a/apps/server/src/routes/fs/routes/resolve-directory.ts +++ b/apps/server/src/routes/fs/routes/resolve-directory.ts @@ -10,7 +10,11 @@ import { getErrorMessage, logError } from '../common.js'; export function createResolveDirectoryHandler() { return async (req: Request, res: Response): Promise => { try { - const { directoryName, sampleFiles, fileCount } = req.body as { + const { + directoryName, + sampleFiles, + fileCount: _fileCount, + } = req.body as { directoryName: string; sampleFiles?: string[]; fileCount?: number; diff --git a/apps/server/src/routes/fs/routes/save-board-background.ts b/apps/server/src/routes/fs/routes/save-board-background.ts index a0c2164a..e8b82169 100644 --- a/apps/server/src/routes/fs/routes/save-board-background.ts +++ b/apps/server/src/routes/fs/routes/save-board-background.ts @@ -11,10 +11,9 @@ import { getBoardDir } from '@automaker/platform'; export function createSaveBoardBackgroundHandler() { return async (req: Request, res: Response): Promise => { try { - const { data, filename, mimeType, projectPath } = req.body as { + const { data, filename, projectPath } = req.body as { data: string; filename: string; - mimeType: string; projectPath: string; }; diff --git a/apps/server/src/routes/fs/routes/save-image.ts b/apps/server/src/routes/fs/routes/save-image.ts index 695a8ded..4d48661c 100644 --- a/apps/server/src/routes/fs/routes/save-image.ts +++ b/apps/server/src/routes/fs/routes/save-image.ts @@ -12,10 +12,9 @@ import { sanitizeFilename } from '@automaker/utils'; export function createSaveImageHandler() { return async (req: Request, res: Response): Promise => { try { - const { data, filename, mimeType, projectPath } = req.body as { + const { data, filename, projectPath } = req.body as { data: string; filename: string; - mimeType: string; projectPath: string; }; diff --git a/apps/server/src/routes/fs/routes/validate-path.ts b/apps/server/src/routes/fs/routes/validate-path.ts index 8659eb5a..9405e0c1 100644 --- a/apps/server/src/routes/fs/routes/validate-path.ts +++ b/apps/server/src/routes/fs/routes/validate-path.ts @@ -5,7 +5,7 @@ import type { Request, Response } from 'express'; import * as secureFs from '../../../lib/secure-fs.js'; import path from 'path'; -import { isPathAllowed, PathNotAllowedError, getAllowedRootDirectory } from '@automaker/platform'; +import { isPathAllowed, getAllowedRootDirectory } from '@automaker/platform'; import { getErrorMessage, logError } from '../common.js'; export function createValidatePathHandler() { diff --git a/apps/server/src/routes/gemini/index.ts b/apps/server/src/routes/gemini/index.ts index c543d827..b3d70cdb 100644 --- a/apps/server/src/routes/gemini/index.ts +++ b/apps/server/src/routes/gemini/index.ts @@ -37,9 +37,12 @@ export function createGeminiRoutes(): Router { const provider = new GeminiProvider(); const status = await provider.detectInstallation(); - const authMethod = - (status as any).authMethod || - (status.authenticated ? (status.hasApiKey ? 'api_key' : 'cli_login') : 'none'); + // Derive authMethod from typed InstallationStatus fields + const authMethod = status.authenticated + ? status.hasApiKey + ? 'api_key' + : 'cli_login' + : 'none'; res.json({ success: true, @@ -48,7 +51,7 @@ export function createGeminiRoutes(): Router { path: status.path || null, authenticated: status.authenticated || false, authMethod, - hasCredentialsFile: (status as any).hasCredentialsFile || false, + hasCredentialsFile: false, }); } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error'; diff --git a/apps/server/src/routes/github/routes/validation-endpoints.ts b/apps/server/src/routes/github/routes/validation-endpoints.ts index 21859737..1f3c2316 100644 --- a/apps/server/src/routes/github/routes/validation-endpoints.ts +++ b/apps/server/src/routes/github/routes/validation-endpoints.ts @@ -6,7 +6,6 @@ import type { Request, Response } from 'express'; import type { EventEmitter } from '../../../lib/events.js'; import type { IssueValidationEvent } from '@automaker/types'; import { - isValidationRunning, getValidationStatus, getRunningValidations, abortValidation, @@ -15,7 +14,6 @@ import { logger, } from './validation-common.js'; import { - readValidation, getAllValidations, getValidationWithFreshness, deleteValidation, diff --git a/apps/server/src/routes/models/routes/providers.ts b/apps/server/src/routes/models/routes/providers.ts index 174a1fac..fa4d2828 100644 --- a/apps/server/src/routes/models/routes/providers.ts +++ b/apps/server/src/routes/models/routes/providers.ts @@ -12,7 +12,7 @@ export function createProvidersHandler() { // Get installation status from all providers const statuses = await ProviderFactory.checkAllProviders(); - const providers: Record = { + const providers: Record> = { anthropic: { available: statuses.claude?.installed || false, hasApiKey: !!process.env.ANTHROPIC_API_KEY, diff --git a/apps/server/src/routes/settings/routes/update-global.ts b/apps/server/src/routes/settings/routes/update-global.ts index 817b5c1d..2bc1c2fa 100644 --- a/apps/server/src/routes/settings/routes/update-global.ts +++ b/apps/server/src/routes/settings/routes/update-global.ts @@ -46,16 +46,14 @@ export function createUpdateGlobalHandler(settingsService: SettingsService) { } // Minimal debug logging to help diagnose accidental wipes. - const projectsLen = Array.isArray((updates as any).projects) - ? (updates as any).projects.length - : undefined; - const trashedLen = Array.isArray((updates as any).trashedProjects) - ? (updates as any).trashedProjects.length + const projectsLen = Array.isArray(updates.projects) ? updates.projects.length : undefined; + const trashedLen = Array.isArray(updates.trashedProjects) + ? updates.trashedProjects.length : undefined; logger.info( `[SERVER_SETTINGS_UPDATE] Request received: projects=${projectsLen ?? 'n/a'}, trashedProjects=${trashedLen ?? 'n/a'}, theme=${ - (updates as any).theme ?? 'n/a' - }, localStorageMigrated=${(updates as any).localStorageMigrated ?? 'n/a'}` + updates.theme ?? 'n/a' + }, localStorageMigrated=${updates.localStorageMigrated ?? 'n/a'}` ); // Get old settings to detect theme changes diff --git a/apps/server/src/routes/setup/routes/auth-claude.ts b/apps/server/src/routes/setup/routes/auth-claude.ts index 97a170f4..9eac0989 100644 --- a/apps/server/src/routes/setup/routes/auth-claude.ts +++ b/apps/server/src/routes/setup/routes/auth-claude.ts @@ -4,13 +4,9 @@ import type { Request, Response } from 'express'; import { getErrorMessage, logError } from '../common.js'; -import { exec } from 'child_process'; -import { promisify } from 'util'; import * as fs from 'fs'; import * as path from 'path'; -const execAsync = promisify(exec); - export function createAuthClaudeHandler() { return async (_req: Request, res: Response): Promise => { try { diff --git a/apps/server/src/routes/setup/routes/auth-opencode.ts b/apps/server/src/routes/setup/routes/auth-opencode.ts index 7d7f35e2..dce314bf 100644 --- a/apps/server/src/routes/setup/routes/auth-opencode.ts +++ b/apps/server/src/routes/setup/routes/auth-opencode.ts @@ -4,13 +4,9 @@ import type { Request, Response } from 'express'; import { logError, getErrorMessage } from '../common.js'; -import { exec } from 'child_process'; -import { promisify } from 'util'; import * as fs from 'fs'; import * as path from 'path'; -const execAsync = promisify(exec); - export function createAuthOpencodeHandler() { return async (_req: Request, res: Response): Promise => { try { diff --git a/apps/server/src/routes/setup/routes/copilot-models.ts b/apps/server/src/routes/setup/routes/copilot-models.ts index 5a3da128..08b9eda9 100644 --- a/apps/server/src/routes/setup/routes/copilot-models.ts +++ b/apps/server/src/routes/setup/routes/copilot-models.ts @@ -10,9 +10,6 @@ import type { Request, Response } from 'express'; import { CopilotProvider } from '../../../providers/copilot-provider.js'; import { getErrorMessage, logError } from '../common.js'; import type { ModelDefinition } from '@automaker/types'; -import { createLogger } from '@automaker/utils'; - -const logger = createLogger('CopilotModelsRoute'); // Singleton provider instance for caching let providerInstance: CopilotProvider | null = null; diff --git a/apps/server/src/routes/setup/routes/opencode-models.ts b/apps/server/src/routes/setup/routes/opencode-models.ts index a3b2b7be..e7909bf9 100644 --- a/apps/server/src/routes/setup/routes/opencode-models.ts +++ b/apps/server/src/routes/setup/routes/opencode-models.ts @@ -14,9 +14,6 @@ import { } from '../../../providers/opencode-provider.js'; import { getErrorMessage, logError } from '../common.js'; import type { ModelDefinition } from '@automaker/types'; -import { createLogger } from '@automaker/utils'; - -const logger = createLogger('OpenCodeModelsRoute'); // Singleton provider instance for caching let providerInstance: OpencodeProvider | null = null; diff --git a/apps/server/src/routes/setup/routes/verify-claude-auth.ts b/apps/server/src/routes/setup/routes/verify-claude-auth.ts index 7df27c3d..6e934dab 100644 --- a/apps/server/src/routes/setup/routes/verify-claude-auth.ts +++ b/apps/server/src/routes/setup/routes/verify-claude-auth.ts @@ -151,7 +151,7 @@ export function createVerifyClaudeAuthHandler() { AuthSessionManager.createSession(sessionId, authMethod || 'api_key', apiKey, 'anthropic'); // Create temporary environment override for SDK call - const cleanupEnv = createTempEnvOverride(authEnv); + const _cleanupEnv = createTempEnvOverride(authEnv); // Run a minimal query to verify authentication const stream = query({ @@ -194,8 +194,10 @@ export function createVerifyClaudeAuthHandler() { } // Check specifically for assistant messages with text content - if (msg.type === 'assistant' && (msg as any).message?.content) { - const content = (msg as any).message.content; + const msgRecord = msg as Record; + const msgMessage = msgRecord.message as Record | undefined; + if (msg.type === 'assistant' && msgMessage?.content) { + const content = msgMessage.content; if (Array.isArray(content)) { for (const block of content) { if (block.type === 'text' && block.text) { diff --git a/apps/server/src/routes/terminal/common.ts b/apps/server/src/routes/terminal/common.ts index 6121e345..5e8b6b32 100644 --- a/apps/server/src/routes/terminal/common.ts +++ b/apps/server/src/routes/terminal/common.ts @@ -5,7 +5,6 @@ import { randomBytes } from 'crypto'; import { createLogger } from '@automaker/utils'; import type { Request, Response, NextFunction } from 'express'; -import { getTerminalService } from '../../services/terminal-service.js'; const logger = createLogger('Terminal'); diff --git a/apps/server/src/routes/terminal/routes/auth.ts b/apps/server/src/routes/terminal/routes/auth.ts index 1d6156bd..0aa29b34 100644 --- a/apps/server/src/routes/terminal/routes/auth.ts +++ b/apps/server/src/routes/terminal/routes/auth.ts @@ -9,7 +9,6 @@ import { generateToken, addToken, getTokenExpiryMs, - getErrorMessage, } from '../common.js'; export function createAuthHandler() { diff --git a/apps/server/src/routes/worktree/routes/branch-tracking.ts b/apps/server/src/routes/worktree/routes/branch-tracking.ts index 1c9f069a..4144b94a 100644 --- a/apps/server/src/routes/worktree/routes/branch-tracking.ts +++ b/apps/server/src/routes/worktree/routes/branch-tracking.ts @@ -31,8 +31,8 @@ export async function getTrackedBranches(projectPath: string): Promise { try { const { apiToken, apiHost } = req.body; - - if (apiToken !== undefined) { - // Set in-memory token - usageService.setApiToken(apiToken || ''); - - // Persist to credentials (deep merge happens in updateCredentials) - try { - await settingsService.updateCredentials({ - apiKeys: { zai: apiToken || '' }, - } as Parameters[0]); - logger.info('[configure] Saved z.ai API key to credentials'); - } catch (persistError) { - logger.error('[configure] Failed to persist z.ai API key:', persistError); - } - } - - if (apiHost) { - usageService.setApiHost(apiHost); - } - - res.json({ - success: true, - message: 'z.ai configuration updated', - isAvailable: usageService.isAvailable(), - }); + const result = await usageService.configure({ apiToken, apiHost }, settingsService); + res.json(result); } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error'; logger.error('Error configuring z.ai:', error); @@ -100,50 +77,8 @@ export function createZaiRoutes( router.post('/verify', async (req: Request, res: Response) => { try { const { apiKey } = req.body; - - if (!apiKey || typeof apiKey !== 'string' || apiKey.trim().length === 0) { - res.json({ - success: false, - authenticated: false, - error: 'Please provide an API key to test.', - }); - return; - } - - // Test the key by making a request to z.ai API - const quotaUrl = - process.env.Z_AI_QUOTA_URL || - `${process.env.Z_AI_API_HOST ? `https://${process.env.Z_AI_API_HOST}` : 'https://api.z.ai'}/api/monitor/usage/quota/limit`; - - logger.info(`[verify] Testing API key against: ${quotaUrl}`); - - const response = await fetch(quotaUrl, { - method: 'GET', - headers: { - Authorization: `Bearer ${apiKey.trim()}`, - Accept: 'application/json', - }, - }); - - if (response.ok) { - res.json({ - success: true, - authenticated: true, - message: 'Connection successful! z.ai API responded.', - }); - } else if (response.status === 401 || response.status === 403) { - res.json({ - success: false, - authenticated: false, - error: 'Invalid API key. Please check your key and try again.', - }); - } else { - res.json({ - success: false, - authenticated: false, - error: `API request failed: ${response.status} ${response.statusText}`, - }); - } + const result = await usageService.verifyApiKey(apiKey); + res.json(result); } catch (error) { const message = error instanceof Error ? error.message : 'Unknown error'; logger.error('Error verifying z.ai API key:', error); diff --git a/apps/server/src/services/agent-executor.ts b/apps/server/src/services/agent-executor.ts index 5d049804..e005b339 100644 --- a/apps/server/src/services/agent-executor.ts +++ b/apps/server/src/services/agent-executor.ts @@ -444,17 +444,11 @@ export class AgentExecutor { callbacks: AgentExecutorCallbacks ): Promise<{ responseText: string; tasksCompleted: number }> { const { - workDir, featureId, projectPath, - abortController, branchName = null, planningMode = 'skip', provider, - effectiveBareModel, - credentials, - claudeCompatibleProvider, - mcpServers, sdkOptions, } = options; let responseText = initialResponseText, diff --git a/apps/server/src/services/agent-service.ts b/apps/server/src/services/agent-service.ts index 0ecec44e..663a1383 100644 --- a/apps/server/src/services/agent-service.ts +++ b/apps/server/src/services/agent-service.ts @@ -15,11 +15,9 @@ import { loadContextFiles, createLogger, classifyError, - getUserFriendlyErrorMessage, } from '@automaker/utils'; import { ProviderFactory } from '../providers/provider-factory.js'; import { createChatOptions, validateWorkingDirectory } from '../lib/sdk-options.js'; -import { PathNotAllowedError } from '@automaker/platform'; import type { SettingsService } from './settings-service.js'; import { getAutoLoadClaudeMdSetting, diff --git a/apps/server/src/services/auto-loop-coordinator.ts b/apps/server/src/services/auto-loop-coordinator.ts index ddc666d5..3310b2d6 100644 --- a/apps/server/src/services/auto-loop-coordinator.ts +++ b/apps/server/src/services/auto-loop-coordinator.ts @@ -158,10 +158,7 @@ export class AutoLoopCoordinator { const projectState = this.autoLoopsByProject.get(worktreeKey); if (!projectState) return; const { projectPath, branchName } = projectState.config; - let iterationCount = 0; - while (projectState.isRunning && !projectState.abortController.signal.aborted) { - iterationCount++; try { const runningCount = await this.getRunningCountForWorktree(projectPath, branchName); if (runningCount >= projectState.config.maxConcurrency) { diff --git a/apps/server/src/services/auto-mode/global-service.ts b/apps/server/src/services/auto-mode/global-service.ts index 90576f8c..459562eb 100644 --- a/apps/server/src/services/auto-mode/global-service.ts +++ b/apps/server/src/services/auto-mode/global-service.ts @@ -10,7 +10,6 @@ */ import path from 'path'; -import type { Feature } from '@automaker/types'; import { createLogger } from '@automaker/utils'; import type { EventEmitter } from '../../lib/events.js'; import { TypedEventBus } from '../typed-event-bus.js'; diff --git a/apps/server/src/services/claude-usage-service.ts b/apps/server/src/services/claude-usage-service.ts index 40cffd7f..ffb07631 100644 --- a/apps/server/src/services/claude-usage-service.ts +++ b/apps/server/src/services/claude-usage-service.ts @@ -295,7 +295,6 @@ export class ClaudeUsageService { } // Don't fail if we have data - return it instead // Check cleaned output since raw output has ANSI codes between words - // eslint-disable-next-line no-control-regex const cleanedForCheck = output .replace(/\x1B\[(\d+)C/g, (_m: string, n: string) => ' '.repeat(parseInt(n, 10))) .replace(/\x1B\[[0-9;?]*[A-Za-z@]/g, ''); @@ -332,7 +331,6 @@ export class ClaudeUsageService { // Convert cursor forward (ESC[nC) to spaces first to preserve word boundaries, // then strip remaining ANSI sequences. Without this, the Claude CLI TUI output // like "Current week (all models)" becomes "Currentweek(allmodels)". - // eslint-disable-next-line no-control-regex const cleanOutput = output .replace(/\x1B\[(\d+)C/g, (_match: string, n: string) => ' '.repeat(parseInt(n, 10))) .replace(/\x1B\[[0-9;?]*[A-Za-z@]/g, ''); @@ -492,7 +490,6 @@ export class ClaudeUsageService { // First, convert cursor movement sequences to whitespace to preserve word boundaries. // The Claude CLI TUI uses ESC[nC (cursor forward) instead of actual spaces between words. // Without this, "Current week (all models)" becomes "Currentweek(allmodels)" after stripping. - // eslint-disable-next-line no-control-regex let clean = text // Cursor forward (CSI n C): replace with n spaces to preserve word separation .replace(/\x1B\[(\d+)C/g, (_match, n) => ' '.repeat(parseInt(n, 10))) diff --git a/apps/server/src/services/dev-server-service.ts b/apps/server/src/services/dev-server-service.ts index 76cf3174..13281dc1 100644 --- a/apps/server/src/services/dev-server-service.ts +++ b/apps/server/src/services/dev-server-service.ts @@ -246,7 +246,7 @@ class DevServerService { // No process found on port, which is fine } } - } catch (error) { + } catch { // Ignore errors - port might not have any process logger.debug(`No process to kill on port ${port}`); } diff --git a/apps/server/src/services/event-history-service.ts b/apps/server/src/services/event-history-service.ts index b983af09..a7091725 100644 --- a/apps/server/src/services/event-history-service.ts +++ b/apps/server/src/services/event-history-service.ts @@ -13,12 +13,7 @@ import { createLogger } from '@automaker/utils'; import * as secureFs from '../lib/secure-fs.js'; -import { - getEventHistoryDir, - getEventHistoryIndexPath, - getEventPath, - ensureEventHistoryDir, -} from '@automaker/platform'; +import { getEventHistoryIndexPath, getEventPath, ensureEventHistoryDir } from '@automaker/platform'; import type { StoredEvent, StoredEventIndex, diff --git a/apps/server/src/services/execution-service.ts b/apps/server/src/services/execution-service.ts index f7a51ace..3ebce443 100644 --- a/apps/server/src/services/execution-service.ts +++ b/apps/server/src/services/execution-service.ts @@ -20,7 +20,6 @@ import type { TypedEventBus } from './typed-event-bus.js'; import type { ConcurrencyManager, RunningFeature } from './concurrency-manager.js'; import type { WorktreeResolver } from './worktree-resolver.js'; import type { SettingsService } from './settings-service.js'; -import type { PipelineContext } from './pipeline-orchestrator.js'; import { pipelineService } from './pipeline-service.js'; // Re-export callback types from execution-types.ts for backward compatibility diff --git a/apps/server/src/services/feature-export-service.ts b/apps/server/src/services/feature-export-service.ts index a58b6527..bd741dc2 100644 --- a/apps/server/src/services/feature-export-service.ts +++ b/apps/server/src/services/feature-export-service.ts @@ -205,7 +205,6 @@ export class FeatureExportService { importData: FeatureImport ): Promise { const warnings: string[] = []; - const errors: string[] = []; try { // Extract feature from data (handle both raw Feature and wrapped FeatureExport) diff --git a/apps/server/src/services/feature-loader.ts b/apps/server/src/services/feature-loader.ts index b40a85f0..941194b7 100644 --- a/apps/server/src/services/feature-loader.ts +++ b/apps/server/src/services/feature-loader.ts @@ -195,9 +195,10 @@ export class FeatureLoader { } // Read all feature directories + // secureFs.readdir returns Dirent[] but typed as generic; cast to access isDirectory() const entries = (await secureFs.readdir(featuresDir, { withFileTypes: true, - })) as any[]; + })) as import('fs').Dirent[]; const featureDirs = entries.filter((entry) => entry.isDirectory()); // Load all features concurrently with automatic recovery from backups diff --git a/apps/server/src/services/gemini-usage-service.ts b/apps/server/src/services/gemini-usage-service.ts index 966d09a4..fba8bda3 100644 --- a/apps/server/src/services/gemini-usage-service.ts +++ b/apps/server/src/services/gemini-usage-service.ts @@ -13,7 +13,7 @@ import { createLogger } from '@automaker/utils'; import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; -import { execSync } from 'child_process'; +import { execFileSync } from 'child_process'; const logger = createLogger('GeminiUsage'); @@ -26,6 +26,12 @@ const CODE_ASSIST_URL = 'https://cloudcode-pa.googleapis.com/v1internal:loadCode // Google OAuth endpoints for token refresh const GOOGLE_TOKEN_URL = 'https://oauth2.googleapis.com/token'; +/** Default timeout for fetch requests in milliseconds */ +const FETCH_TIMEOUT_MS = 10_000; + +/** TTL for cached credentials in milliseconds (5 minutes) */ +const CREDENTIALS_CACHE_TTL_MS = 5 * 60 * 1000; + export interface GeminiQuotaBucket { /** Model ID this quota applies to */ modelId: string; @@ -114,8 +120,11 @@ interface QuotaResponse { */ export class GeminiUsageService { private cachedCredentials: OAuthCredentials | null = null; + private cachedCredentialsAt: number | null = null; private cachedClientCredentials: OAuthClientCredentials | null = null; private credentialsPath: string; + /** The actual path from which credentials were loaded (for write-back) */ + private loadedCredentialsPath: string | null = null; constructor() { // Default credentials path for Gemini CLI @@ -176,6 +185,7 @@ export class GeminiUsageService { 'Content-Type': 'application/json', }, body: JSON.stringify({}), + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), }); if (codeAssistResponse.ok) { @@ -199,6 +209,7 @@ export class GeminiUsageService { 'Content-Type': 'application/json', }, body: JSON.stringify(projectId ? { project: projectId } : {}), + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), }); if (!response.ok) { @@ -338,19 +349,46 @@ export class GeminiUsageService { } /** - * Load OAuth credentials from file + * Load OAuth credentials from file. + * Implements TTL-based cache invalidation and file mtime checks. */ private async loadCredentials(): Promise { - if (this.cachedCredentials) { - return this.cachedCredentials; + // Check if cached credentials are still valid + if (this.cachedCredentials && this.cachedCredentialsAt) { + const now = Date.now(); + const cacheAge = now - this.cachedCredentialsAt; + + if (cacheAge < CREDENTIALS_CACHE_TTL_MS) { + // Cache is within TTL - also check file mtime + const sourcePath = this.loadedCredentialsPath || this.credentialsPath; + try { + const stat = fs.statSync(sourcePath); + if (stat.mtimeMs <= this.cachedCredentialsAt) { + // File hasn't been modified since we cached - use cache + return this.cachedCredentials; + } + // File has been modified, fall through to re-read + logger.debug('[loadCredentials] File modified since cache, re-reading'); + } catch { + // File doesn't exist or can't stat - use cache + return this.cachedCredentials; + } + } else { + // Cache TTL expired, discard + logger.debug('[loadCredentials] Cache TTL expired, re-reading'); + } + + // Invalidate cached credentials + this.cachedCredentials = null; + this.cachedCredentialsAt = null; } - // Check multiple possible paths - const possiblePaths = [ + // Build unique possible paths (deduplicate) + const rawPaths = [ this.credentialsPath, - path.join(os.homedir(), '.gemini', 'oauth_creds.json'), path.join(os.homedir(), '.config', 'gemini', 'oauth_creds.json'), ]; + const possiblePaths = [...new Set(rawPaths)]; for (const credPath of possiblePaths) { try { @@ -361,6 +399,8 @@ export class GeminiUsageService { // Handle different credential formats if (creds.access_token || creds.refresh_token) { this.cachedCredentials = creds; + this.cachedCredentialsAt = Date.now(); + this.loadedCredentialsPath = credPath; logger.info('[loadCredentials] Loaded from:', credPath); return creds; } @@ -372,6 +412,8 @@ export class GeminiUsageService { client_id: clientCreds.client_id, client_secret: clientCreds.client_secret, }; + this.cachedCredentialsAt = Date.now(); + this.loadedCredentialsPath = credPath; return this.cachedCredentials; } } @@ -387,14 +429,21 @@ export class GeminiUsageService { * Find the Gemini CLI binary path */ private findGeminiBinaryPath(): string | null { + // Try 'which' on Unix-like systems, 'where' on Windows + const whichCmd = process.platform === 'win32' ? 'where' : 'which'; try { - // Try 'which' on Unix-like systems - const whichResult = execSync('which gemini 2>/dev/null', { encoding: 'utf8' }).trim(); - if (whichResult && fs.existsSync(whichResult)) { - return whichResult; + const whichResult = execFileSync(whichCmd, ['gemini'], { + encoding: 'utf8', + timeout: 5000, + stdio: ['pipe', 'pipe', 'pipe'], + }).trim(); + // 'where' on Windows may return multiple lines; take the first + const firstLine = whichResult.split('\n')[0]?.trim(); + if (firstLine && fs.existsSync(firstLine)) { + return firstLine; } } catch { - // Ignore errors from 'which' + // Ignore errors from 'which'/'where' } // Check common installation paths @@ -554,27 +603,33 @@ export class GeminiUsageService { } } - // Try finding oauth2.js by searching in node_modules - try { - const searchResult = execSync( - `find ${baseDir}/.. -name "oauth2.js" -path "*gemini*" -path "*code_assist*" 2>/dev/null | head -1`, - { encoding: 'utf8', timeout: 5000 } - ).trim(); + // Try finding oauth2.js by searching in node_modules (POSIX only) + if (process.platform !== 'win32') { + try { + const searchBase = path.resolve(baseDir, '..'); + const searchResult = execFileSync( + 'find', + [searchBase, '-name', 'oauth2.js', '-path', '*gemini*', '-path', '*code_assist*'], + { encoding: 'utf8', timeout: 5000, stdio: ['pipe', 'pipe', 'pipe'] } + ) + .trim() + .split('\n')[0]; // Take first result - if (searchResult && fs.existsSync(searchResult)) { - logger.debug('[extractOAuthClientCredentials] Found via search:', searchResult); - const content = fs.readFileSync(searchResult, 'utf8'); - const creds = this.parseOAuthCredentialsFromSource(content); - if (creds) { - this.cachedClientCredentials = creds; - logger.info( - '[extractOAuthClientCredentials] Extracted credentials from CLI (via search)' - ); - return creds; + if (searchResult && fs.existsSync(searchResult)) { + logger.debug('[extractOAuthClientCredentials] Found via search:', searchResult); + const content = fs.readFileSync(searchResult, 'utf8'); + const creds = this.parseOAuthCredentialsFromSource(content); + if (creds) { + this.cachedClientCredentials = creds; + logger.info( + '[extractOAuthClientCredentials] Extracted credentials from CLI (via search)' + ); + return creds; + } } + } catch { + // Ignore search errors } - } catch { - // Ignore search errors } logger.warn('[extractOAuthClientCredentials] Could not extract credentials from CLI'); @@ -669,6 +724,7 @@ export class GeminiUsageService { refresh_token: creds.refresh_token, grant_type: 'refresh_token', }), + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), }); if (response.ok) { @@ -685,13 +741,12 @@ export class GeminiUsageService { access_token: newAccessToken, expiry_date: Date.now() + expiresIn * 1000, }; + this.cachedCredentialsAt = Date.now(); - // Save back to file + // Save back to the file the credentials were loaded from + const writePath = this.loadedCredentialsPath || this.credentialsPath; try { - fs.writeFileSync( - this.credentialsPath, - JSON.stringify(this.cachedCredentials, null, 2) - ); + fs.writeFileSync(writePath, JSON.stringify(this.cachedCredentials, null, 2)); } catch (e) { logger.debug('[getValidAccessToken] Could not save refreshed token:', e); } @@ -743,6 +798,7 @@ export class GeminiUsageService { */ clearCache(): void { this.cachedCredentials = null; + this.cachedCredentialsAt = null; this.cachedClientCredentials = null; } } diff --git a/apps/server/src/services/ideation-service.ts b/apps/server/src/services/ideation-service.ts index 0d43252f..9bbea03b 100644 --- a/apps/server/src/services/ideation-service.ts +++ b/apps/server/src/services/ideation-service.ts @@ -27,7 +27,6 @@ import type { } from '@automaker/types'; import { DEFAULT_IDEATION_CONTEXT_SOURCES } from '@automaker/types'; import { - getIdeationDir, getIdeasDir, getIdeaDir, getIdeaPath, @@ -407,7 +406,9 @@ export class IdeationService { return []; } - const entries = (await secureFs.readdir(ideasDir, { withFileTypes: true })) as any[]; + const entries = (await secureFs.readdir(ideasDir, { + withFileTypes: true, + })) as import('fs').Dirent[]; const ideaDirs = entries.filter((entry) => entry.isDirectory()); const ideas: Idea[] = []; @@ -855,15 +856,26 @@ ${contextSection}${existingWorkSection}`; } return parsed - .map((item: any, index: number) => ({ - id: this.generateId('sug'), - category, - title: item.title || `Suggestion ${index + 1}`, - description: item.description || '', - rationale: item.rationale || '', - priority: item.priority || 'medium', - relatedFiles: item.relatedFiles || [], - })) + .map( + ( + item: { + title?: string; + description?: string; + rationale?: string; + priority?: 'low' | 'medium' | 'high'; + relatedFiles?: string[]; + }, + index: number + ) => ({ + id: this.generateId('sug'), + category, + title: item.title || `Suggestion ${index + 1}`, + description: item.description || '', + rationale: item.rationale || '', + priority: item.priority || ('medium' as const), + relatedFiles: item.relatedFiles || [], + }) + ) .slice(0, count); } catch (error) { logger.warn('Failed to parse JSON response:', error); @@ -1705,7 +1717,9 @@ ${contextSection}${existingWorkSection}`; const results: AnalysisFileInfo[] = []; try { - const entries = (await secureFs.readdir(dirPath, { withFileTypes: true })) as any[]; + const entries = (await secureFs.readdir(dirPath, { + withFileTypes: true, + })) as import('fs').Dirent[]; for (const entry of entries) { if (entry.isDirectory()) { diff --git a/apps/server/src/services/recovery-service.ts b/apps/server/src/services/recovery-service.ts index d575f1da..d08f5a8e 100644 --- a/apps/server/src/services/recovery-service.ts +++ b/apps/server/src/services/recovery-service.ts @@ -250,6 +250,14 @@ export class RecoveryService { async resumeInterruptedFeatures(projectPath: string): Promise { const featuresDir = getFeaturesDir(projectPath); try { + // Load execution state to find features that were running before restart. + // This is critical because reconcileAllFeatureStates() runs at server startup + // and resets in_progress/interrupted/pipeline_* features to ready/backlog + // BEFORE the UI connects and calls this method. Without checking execution state, + // we would find no features to resume since their statuses have already been reset. + const executionState = await this.loadExecutionState(projectPath); + const previouslyRunningIds = new Set(executionState.runningFeatureIds ?? []); + const entries = await secureFs.readdir(featuresDir, { withFileTypes: true }); const featuresWithContext: Feature[] = []; const featuresWithoutContext: Feature[] = []; @@ -263,18 +271,37 @@ export class RecoveryService { logRecoveryWarning(result, `Feature ${entry.name}`, logger); const feature = result.data; if (!feature) continue; - if ( + + // Check if the feature should be resumed: + // 1. Features still in active states (in_progress, pipeline_*) - not yet reconciled + // 2. Features in interrupted state - explicitly marked for resume + // 3. Features that were previously running (from execution state) and are now + // in ready/backlog due to reconciliation resetting their status + const isActiveState = feature.status === 'in_progress' || - (feature.status && feature.status.startsWith('pipeline_')) - ) { - (await this.contextExists(projectPath, feature.id)) - ? featuresWithContext.push(feature) - : featuresWithoutContext.push(feature); + feature.status === 'interrupted' || + (feature.status && feature.status.startsWith('pipeline_')); + const wasReconciledFromRunning = + previouslyRunningIds.has(feature.id) && + (feature.status === 'ready' || feature.status === 'backlog'); + + if (isActiveState || wasReconciledFromRunning) { + if (await this.contextExists(projectPath, feature.id)) { + featuresWithContext.push(feature); + } else { + featuresWithoutContext.push(feature); + } } } } const allInterruptedFeatures = [...featuresWithContext, ...featuresWithoutContext]; if (allInterruptedFeatures.length === 0) return; + + logger.info( + `[resumeInterruptedFeatures] Found ${allInterruptedFeatures.length} feature(s) to resume ` + + `(${previouslyRunningIds.size} from execution state, statuses: ${allInterruptedFeatures.map((f) => `${f.id}=${f.status}`).join(', ')})` + ); + this.eventBus.emitAutoModeEvent('auto_mode_resuming_features', { message: `Resuming ${allInterruptedFeatures.length} interrupted feature(s)`, projectPath, @@ -295,6 +322,10 @@ export class RecoveryService { /* continue */ } } + + // Clear execution state after successful resume to prevent + // re-resuming the same features on subsequent calls + await this.clearExecutionState(projectPath); } catch { /* ignore */ } diff --git a/apps/server/src/services/zai-usage-service.ts b/apps/server/src/services/zai-usage-service.ts index c19cf638..e779c5c3 100644 --- a/apps/server/src/services/zai-usage-service.ts +++ b/apps/server/src/services/zai-usage-service.ts @@ -1,7 +1,12 @@ import { createLogger } from '@automaker/utils'; +import { createEventEmitter } from '../lib/events.js'; +import type { SettingsService } from './settings-service.js'; const logger = createLogger('ZaiUsage'); +/** Default timeout for fetch requests in milliseconds */ +const FETCH_TIMEOUT_MS = 10_000; + /** * z.ai quota limit entry from the API */ @@ -112,6 +117,21 @@ interface ZaiApiResponse { message?: string; } +/** Result from configure method */ +interface ConfigureResult { + success: boolean; + message: string; + isAvailable: boolean; +} + +/** Result from verifyApiKey method */ +interface VerifyResult { + success: boolean; + authenticated: boolean; + message?: string; + error?: string; +} + /** * z.ai Usage Service * @@ -162,16 +182,163 @@ export class ZaiUsageService { return Boolean(token && token.length > 0); } + /** + * Configure z.ai API token and host. + * Persists the token via settingsService and updates in-memory state. + */ + async configure( + options: { apiToken?: string; apiHost?: string }, + settingsService: SettingsService + ): Promise { + const emitter = createEventEmitter(); + + if (options.apiToken !== undefined) { + // Set in-memory token + this.setApiToken(options.apiToken || ''); + + // Persist to credentials + try { + await settingsService.updateCredentials({ + apiKeys: { zai: options.apiToken || '' }, + } as Parameters[0]); + logger.info('[configure] Saved z.ai API key to credentials'); + } catch (persistError) { + logger.error('[configure] Failed to persist z.ai API key:', persistError); + } + } + + if (options.apiHost) { + this.setApiHost(options.apiHost); + } + + const result: ConfigureResult = { + success: true, + message: 'z.ai configuration updated', + isAvailable: this.isAvailable(), + }; + + emitter.emit('notification:created', { + type: 'zai.configured', + success: result.success, + isAvailable: result.isAvailable, + }); + + return result; + } + + /** + * Verify an API key without storing it. + * Makes a test request to the z.ai quota URL with the given key. + */ + async verifyApiKey(apiKey: string | undefined): Promise { + const emitter = createEventEmitter(); + + if (!apiKey || typeof apiKey !== 'string' || apiKey.trim().length === 0) { + return { + success: false, + authenticated: false, + error: 'Please provide an API key to test.', + }; + } + + const quotaUrl = + process.env.Z_AI_QUOTA_URL || + `${process.env.Z_AI_API_HOST ? `https://${process.env.Z_AI_API_HOST}` : 'https://api.z.ai'}/api/monitor/usage/quota/limit`; + + logger.info(`[verify] Testing API key against: ${quotaUrl}`); + + try { + const response = await fetch(quotaUrl, { + method: 'GET', + headers: { + Authorization: `Bearer ${apiKey.trim()}`, + Accept: 'application/json', + }, + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), + }); + + let result: VerifyResult; + + if (response.ok) { + result = { + success: true, + authenticated: true, + message: 'Connection successful! z.ai API responded.', + }; + } else if (response.status === 401 || response.status === 403) { + result = { + success: false, + authenticated: false, + error: 'Invalid API key. Please check your key and try again.', + }; + } else { + result = { + success: false, + authenticated: false, + error: `API request failed: ${response.status} ${response.statusText}`, + }; + } + + emitter.emit('notification:created', { + type: 'zai.verify.result', + success: result.success, + authenticated: result.authenticated, + }); + + return result; + } catch (error) { + // Handle abort/timeout errors specifically + if (error instanceof Error && error.name === 'AbortError') { + const result: VerifyResult = { + success: false, + authenticated: false, + error: 'Request timed out. The z.ai API did not respond in time.', + }; + emitter.emit('notification:created', { + type: 'zai.verify.result', + success: false, + error: 'timeout', + }); + return result; + } + + const message = error instanceof Error ? error.message : 'Unknown error'; + logger.error('Error verifying z.ai API key:', error); + + emitter.emit('notification:created', { + type: 'zai.verify.result', + success: false, + error: message, + }); + + return { + success: false, + authenticated: false, + error: `Network error: ${message}`, + }; + } + } + /** * Fetch usage data from z.ai API */ async fetchUsageData(): Promise { logger.info('[fetchUsageData] Starting...'); + const emitter = createEventEmitter(); + + emitter.emit('notification:created', { type: 'zai.usage.start' }); const token = this.getApiToken(); if (!token) { logger.error('[fetchUsageData] No API token configured'); - throw new Error('z.ai API token not configured. Set Z_AI_API_KEY environment variable.'); + const error = new Error( + 'z.ai API token not configured. Set Z_AI_API_KEY environment variable.' + ); + emitter.emit('notification:created', { + type: 'zai.usage.error', + error: error.message, + }); + throw error; } const quotaUrl = @@ -180,31 +347,68 @@ export class ZaiUsageService { logger.info(`[fetchUsageData] Fetching from: ${quotaUrl}`); try { - const response = await fetch(quotaUrl, { - method: 'GET', - headers: { - Authorization: `Bearer ${token}`, - Accept: 'application/json', - }, - }); + const controller = new AbortController(); + const timeoutId = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS); - if (!response.ok) { - logger.error(`[fetchUsageData] HTTP ${response.status}: ${response.statusText}`); - throw new Error(`z.ai API request failed: ${response.status} ${response.statusText}`); + try { + const response = await fetch(quotaUrl, { + method: 'GET', + headers: { + Authorization: `Bearer ${token}`, + Accept: 'application/json', + }, + signal: controller.signal, + }); + + clearTimeout(timeoutId); + + if (!response.ok) { + logger.error(`[fetchUsageData] HTTP ${response.status}: ${response.statusText}`); + throw new Error(`z.ai API request failed: ${response.status} ${response.statusText}`); + } + + const data = (await response.json()) as unknown as ZaiApiResponse; + logger.info('[fetchUsageData] Response received:', JSON.stringify(data, null, 2)); + + const result = this.parseApiResponse(data); + + emitter.emit('notification:created', { + type: 'zai.usage.success', + data: result, + }); + + return result; + } finally { + clearTimeout(timeoutId); + } + } catch (error) { + // Handle abort/timeout errors + if (error instanceof Error && error.name === 'AbortError') { + const timeoutError = new Error(`z.ai API request timed out after ${FETCH_TIMEOUT_MS}ms`); + emitter.emit('notification:created', { + type: 'zai.usage.error', + error: timeoutError.message, + }); + throw timeoutError; } - const data = (await response.json()) as unknown as ZaiApiResponse; - logger.info('[fetchUsageData] Response received:', JSON.stringify(data, null, 2)); - - return this.parseApiResponse(data); - } catch (error) { if (error instanceof Error && error.message.includes('z.ai API')) { + emitter.emit('notification:created', { + type: 'zai.usage.error', + error: error.message, + }); throw error; } + logger.error('[fetchUsageData] Failed to fetch:', error); - throw new Error( + const fetchError = new Error( `Failed to fetch z.ai usage data: ${error instanceof Error ? error.message : String(error)}` ); + emitter.emit('notification:created', { + type: 'zai.usage.error', + error: fetchError.message, + }); + throw fetchError; } } diff --git a/apps/server/src/tests/cli-integration.test.ts b/apps/server/src/tests/cli-integration.test.ts index 7e84eb54..87269ac0 100644 --- a/apps/server/src/tests/cli-integration.test.ts +++ b/apps/server/src/tests/cli-integration.test.ts @@ -5,7 +5,7 @@ * across all providers (Claude, Codex, Cursor) */ -import { describe, it, expect, beforeEach, afterEach } from 'vitest'; +import { describe, it, expect } from 'vitest'; import { detectCli, detectAllCLis, @@ -270,7 +270,7 @@ describe('Error Recovery Tests', () => { expect(results).toHaveProperty('cursor'); // Should provide error information for failures - Object.entries(results).forEach(([provider, result]) => { + Object.entries(results).forEach(([_provider, result]) => { if (!result.detected && result.issues.length > 0) { expect(result.issues.length).toBeGreaterThan(0); expect(result.issues[0]).toBeTruthy(); diff --git a/apps/server/tests/unit/services/recovery-service.test.ts b/apps/server/tests/unit/services/recovery-service.test.ts index 90be3eb2..cd99fc08 100644 --- a/apps/server/tests/unit/services/recovery-service.test.ts +++ b/apps/server/tests/unit/services/recovery-service.test.ts @@ -491,6 +491,32 @@ describe('recovery-service.ts', () => { ); }); + it('finds features with interrupted status', async () => { + vi.mocked(secureFs.readdir).mockResolvedValueOnce([ + { name: 'feature-1', isDirectory: () => true } as any, + ]); + vi.mocked(utils.readJsonWithRecovery).mockResolvedValueOnce({ + data: { id: 'feature-1', title: 'Feature 1', status: 'interrupted' }, + wasRecovered: false, + }); + + mockLoadFeature.mockResolvedValue({ + id: 'feature-1', + title: 'Feature 1', + status: 'interrupted', + description: 'Test', + }); + + await service.resumeInterruptedFeatures('/test/project'); + + expect(mockEventBus.emitAutoModeEvent).toHaveBeenCalledWith( + 'auto_mode_resuming_features', + expect.objectContaining({ + featureIds: ['feature-1'], + }) + ); + }); + it('finds features with pipeline_* status', async () => { vi.mocked(secureFs.readdir).mockResolvedValueOnce([ { name: 'feature-1', isDirectory: () => true } as any, @@ -519,6 +545,100 @@ describe('recovery-service.ts', () => { ); }); + it('finds reconciled features using execution state (ready/backlog from previously running)', async () => { + // Simulate execution state with previously running feature IDs + const executionState = { + version: 1, + autoLoopWasRunning: true, + maxConcurrency: 2, + projectPath: '/test/project', + branchName: null, + runningFeatureIds: ['feature-1', 'feature-2'], + savedAt: '2026-01-27T12:00:00Z', + }; + vi.mocked(secureFs.readFile).mockResolvedValueOnce(JSON.stringify(executionState)); + + vi.mocked(secureFs.readdir).mockResolvedValueOnce([ + { name: 'feature-1', isDirectory: () => true } as any, + { name: 'feature-2', isDirectory: () => true } as any, + { name: 'feature-3', isDirectory: () => true } as any, + ]); + // feature-1 was reconciled from in_progress to ready + // feature-2 was reconciled from in_progress to backlog + // feature-3 is in backlog but was NOT previously running + vi.mocked(utils.readJsonWithRecovery) + .mockResolvedValueOnce({ + data: { id: 'feature-1', title: 'Feature 1', status: 'ready' }, + wasRecovered: false, + }) + .mockResolvedValueOnce({ + data: { id: 'feature-2', title: 'Feature 2', status: 'backlog' }, + wasRecovered: false, + }) + .mockResolvedValueOnce({ + data: { id: 'feature-3', title: 'Feature 3', status: 'backlog' }, + wasRecovered: false, + }); + + mockLoadFeature + .mockResolvedValueOnce({ + id: 'feature-1', + title: 'Feature 1', + status: 'ready', + description: 'Test', + }) + .mockResolvedValueOnce({ + id: 'feature-2', + title: 'Feature 2', + status: 'backlog', + description: 'Test', + }); + + await service.resumeInterruptedFeatures('/test/project'); + + // Should resume feature-1 and feature-2 (from execution state) but NOT feature-3 + expect(mockEventBus.emitAutoModeEvent).toHaveBeenCalledWith( + 'auto_mode_resuming_features', + expect.objectContaining({ + featureIds: ['feature-1', 'feature-2'], + }) + ); + }); + + it('clears execution state after successful resume', async () => { + // Simulate execution state + const executionState = { + version: 1, + autoLoopWasRunning: true, + maxConcurrency: 1, + projectPath: '/test/project', + branchName: null, + runningFeatureIds: ['feature-1'], + savedAt: '2026-01-27T12:00:00Z', + }; + vi.mocked(secureFs.readFile).mockResolvedValueOnce(JSON.stringify(executionState)); + + vi.mocked(secureFs.readdir).mockResolvedValueOnce([ + { name: 'feature-1', isDirectory: () => true } as any, + ]); + vi.mocked(utils.readJsonWithRecovery).mockResolvedValueOnce({ + data: { id: 'feature-1', title: 'Feature 1', status: 'ready' }, + wasRecovered: false, + }); + + mockLoadFeature.mockResolvedValue({ + id: 'feature-1', + title: 'Feature 1', + status: 'ready', + description: 'Test', + }); + + await service.resumeInterruptedFeatures('/test/project'); + + // Should clear execution state after resuming + expect(secureFs.unlink).toHaveBeenCalledWith('/test/project/.automaker/execution-state.json'); + }); + it('distinguishes features with/without context', async () => { vi.mocked(secureFs.readdir).mockResolvedValueOnce([ { name: 'feature-with', isDirectory: () => true } as any, diff --git a/apps/ui/src/components/usage-popover.tsx b/apps/ui/src/components/usage-popover.tsx index 5c961f83..6122f24c 100644 --- a/apps/ui/src/components/usage-popover.tsx +++ b/apps/ui/src/components/usage-popover.tsx @@ -340,7 +340,7 @@ export function UsagePopover() { // Calculate max percentage for header button const claudeSessionPercentage = claudeUsage?.sessionPercentage || 0; - const codexMaxPercentage = codexUsage?.rateLimits + const _codexMaxPercentage = codexUsage?.rateLimits ? Math.max( codexUsage.rateLimits.primary?.usedPercent || 0, codexUsage.rateLimits.secondary?.usedPercent || 0 @@ -369,7 +369,7 @@ export function UsagePopover() { codexSecondaryWindowMinutes && codexPrimaryWindowMinutes ? Math.min(codexPrimaryWindowMinutes, codexSecondaryWindowMinutes) : (codexSecondaryWindowMinutes ?? codexPrimaryWindowMinutes); - const codexWindowLabel = codexWindowMinutes + const _codexWindowLabel = codexWindowMinutes ? getCodexWindowLabel(codexWindowMinutes).title : 'Window'; const codexWindowUsage = @@ -408,16 +408,16 @@ export function UsagePopover() { } : null; - const statusColor = getStatusInfo(indicatorInfo.percentage).color; - const ProviderIcon = indicatorInfo.icon; + const statusColor = indicatorInfo ? getStatusInfo(indicatorInfo.percentage).color : ''; + const ProviderIcon = indicatorInfo?.icon; const trigger = ( - )} - {feature.skipTests && onManualVerify ? ( - - ) : onResume ? ( - - ) : onVerify ? ( - - ) : null} - {onViewOutput && !feature.skipTests && ( - + {/* When feature is in_progress with no error and onForceStop is available, + it means the agent is starting/running but hasn't been added to runningAutoTasks yet. + Show Stop button instead of Verify/Resume to avoid confusing UI during this race window. */} + {!feature.error && onForceStop ? ( + <> + {onViewOutput && ( + + )} + + + ) : ( + <> + {/* Approve Plan button - shows when plan is generated and waiting for approval */} + {feature.planSpec?.status === 'generated' && onApprovePlan && ( + + )} + {feature.skipTests && onManualVerify ? ( + + ) : onResume ? ( + + ) : onVerify ? ( + + ) : null} + {onViewOutput && !feature.skipTests && ( + + )} + )} )} diff --git a/apps/ui/src/components/views/board-view/components/kanban-card/kanban-card.tsx b/apps/ui/src/components/views/board-view/components/kanban-card/kanban-card.tsx index ab109d5f..b54c5fcc 100644 --- a/apps/ui/src/components/views/board-view/components/kanban-card/kanban-card.tsx +++ b/apps/ui/src/components/views/board-view/components/kanban-card/kanban-card.tsx @@ -112,9 +112,15 @@ export const KanbanCard = memo(function KanbanCard({ currentProject: state.currentProject, })) ); - // A card in waiting_approval should not display as "actively running" even if - // it's still in the runningAutoTasks list. The waiting_approval UI takes precedence. - const isActivelyRunning = !!isCurrentAutoTask && feature.status !== 'waiting_approval'; + // A card should only display as "actively running" if it's both in the + // runningAutoTasks list AND in an execution-compatible status. Cards in resting + // states (backlog, ready, waiting_approval, verified, completed) should never + // show running controls, even if they appear in runningAutoTasks due to stale + // state (e.g., after a server restart that reconciled features back to backlog). + const isInExecutionState = + feature.status === 'in_progress' || + (typeof feature.status === 'string' && feature.status.startsWith('pipeline_')); + const isActivelyRunning = !!isCurrentAutoTask && isInExecutionState; const [isLifted, setIsLifted] = useState(false); useLayoutEffect(() => { diff --git a/apps/ui/src/components/views/board-view/components/list-view/list-row.tsx b/apps/ui/src/components/views/board-view/components/list-view/list-row.tsx index 6d14c269..8fcd0bb9 100644 --- a/apps/ui/src/components/views/board-view/components/list-view/list-row.tsx +++ b/apps/ui/src/components/views/board-view/components/list-view/list-row.tsx @@ -209,9 +209,15 @@ export const ListRow = memo(function ListRow({ blockingDependencies = [], className, }: ListRowProps) { - // A card in waiting_approval should not display as "actively running" even if - // it's still in the runningAutoTasks list. The waiting_approval UI takes precedence. - const isActivelyRunning = isCurrentAutoTask && feature.status !== 'waiting_approval'; + // A row should only display as "actively running" if it's both in the + // runningAutoTasks list AND in an execution-compatible status. Features in resting + // states (backlog, ready, waiting_approval, verified, completed) should never + // show running controls, even if they appear in runningAutoTasks due to stale + // state (e.g., after a server restart that reconciled features back to backlog). + const isInExecutionState = + feature.status === 'in_progress' || + (typeof feature.status === 'string' && feature.status.startsWith('pipeline_')); + const isActivelyRunning = isCurrentAutoTask && isInExecutionState; const handleRowClick = useCallback( (e: React.MouseEvent) => { diff --git a/apps/ui/src/components/views/board-view/components/list-view/row-actions.tsx b/apps/ui/src/components/views/board-view/components/list-view/row-actions.tsx index 60158d0f..89462563 100644 --- a/apps/ui/src/components/views/board-view/components/list-view/row-actions.tsx +++ b/apps/ui/src/components/views/board-view/components/list-view/row-actions.tsx @@ -143,6 +143,17 @@ function getPrimaryAction( }; } + // In progress with no error - agent is starting/running but not yet in runningAutoTasks. + // Show Stop button immediately instead of Verify/Resume during this race window. + if (feature.status === 'in_progress' && !feature.error && handlers.onForceStop) { + return { + icon: StopCircle, + label: 'Stop', + onClick: handlers.onForceStop, + variant: 'destructive', + }; + } + // In progress with plan approval pending if ( feature.status === 'in_progress' && @@ -446,81 +457,126 @@ export const RowActions = memo(function RowActions({ )} - {/* In Progress actions */} - {!isCurrentAutoTask && feature.status === 'in_progress' && ( - <> - {handlers.onViewOutput && ( - - )} - {feature.planSpec?.status === 'generated' && handlers.onApprovePlan && ( - - )} - {feature.skipTests && handlers.onManualVerify ? ( - - ) : handlers.onResume ? ( - - ) : null} - - - {handlers.onSpawnTask && ( - - )} - {handlers.onDuplicate && ( - -
- - - Duplicate - + {/* In Progress actions - starting/running (no error, force stop available) - mirrors running task actions */} + {!isCurrentAutoTask && + feature.status === 'in_progress' && + !feature.error && + handlers.onForceStop && ( + <> + {handlers.onViewOutput && ( + + )} + {feature.planSpec?.status === 'generated' && handlers.onApprovePlan && ( + + )} + + {handlers.onSpawnTask && ( + + )} + {handlers.onForceStop && ( + <> + + + + )} + + )} + + {/* In Progress actions - interrupted/error state */} + {!isCurrentAutoTask && + feature.status === 'in_progress' && + !(!feature.error && handlers.onForceStop) && ( + <> + {handlers.onViewOutput && ( + + )} + {feature.planSpec?.status === 'generated' && handlers.onApprovePlan && ( + + )} + {feature.skipTests && handlers.onManualVerify ? ( + + ) : handlers.onResume ? ( + + ) : null} + + + {handlers.onSpawnTask && ( + + )} + {handlers.onDuplicate && ( + +
+ + + Duplicate + + {handlers.onDuplicateAsChild && ( + + )} +
{handlers.onDuplicateAsChild && ( - + + + )} -
- {handlers.onDuplicateAsChild && ( - - - - )} -
- )} - - - )} + + )} + + + )} {/* Waiting Approval actions */} {!isCurrentAutoTask && feature.status === 'waiting_approval' && ( diff --git a/apps/ui/src/components/views/board-view/mobile-usage-bar.tsx b/apps/ui/src/components/views/board-view/mobile-usage-bar.tsx index f6a89cf1..d78ce224 100644 --- a/apps/ui/src/components/views/board-view/mobile-usage-bar.tsx +++ b/apps/ui/src/components/views/board-view/mobile-usage-bar.tsx @@ -5,7 +5,6 @@ import { Spinner } from '@/components/ui/spinner'; import { getElectronAPI } from '@/lib/electron'; import { useAppStore } from '@/store/app-store'; import { AnthropicIcon, OpenAIIcon, ZaiIcon, GeminiIcon } from '@/components/ui/provider-icon'; -import type { GeminiUsage } from '@/store/app-store'; import { getExpectedWeeklyPacePercentage, getPaceStatusLabel } from '@/store/utils/usage-utils'; interface MobileUsageBarProps { @@ -42,6 +41,11 @@ function formatResetTime(unixTimestamp: number, isMilliseconds = false): string const now = new Date(); const diff = date.getTime() - now.getTime(); + // Handle past timestamps (negative diff) + if (diff <= 0) { + return 'Resetting soon'; + } + if (diff < 3600000) { const mins = Math.ceil(diff / 60000); return `Resets in ${mins}m`; @@ -184,12 +188,11 @@ export function MobileUsageBar({ const { claudeUsage, claudeUsageLastUpdated, setClaudeUsage } = useAppStore(); const { codexUsage, codexUsageLastUpdated, setCodexUsage } = useAppStore(); const { zaiUsage, zaiUsageLastUpdated, setZaiUsage } = useAppStore(); + const { geminiUsage, geminiUsageLastUpdated, setGeminiUsage } = useAppStore(); const [isClaudeLoading, setIsClaudeLoading] = useState(false); const [isCodexLoading, setIsCodexLoading] = useState(false); const [isZaiLoading, setIsZaiLoading] = useState(false); const [isGeminiLoading, setIsGeminiLoading] = useState(false); - const [geminiUsage, setGeminiUsage] = useState(null); - const [geminiUsageLastUpdated, setGeminiUsageLastUpdated] = useState(null); // Check if data is stale (older than 2 minutes) const isClaudeStale = @@ -254,15 +257,14 @@ export function MobileUsageBar({ if (!api.gemini) return; const data = await api.gemini.getUsage(); if (!('error' in data)) { - setGeminiUsage(data); - setGeminiUsageLastUpdated(Date.now()); + setGeminiUsage(data, Date.now()); } } catch { // Silently fail - usage display is optional } finally { setIsGeminiLoading(false); } - }, []); + }, [setGeminiUsage]); const getCodexWindowLabel = (durationMins: number) => { if (durationMins < 60) return `${durationMins}m Window`; diff --git a/apps/ui/src/components/views/settings-view/api-keys/hooks/use-api-key-management.ts b/apps/ui/src/components/views/settings-view/api-keys/hooks/use-api-key-management.ts index 1b6738ec..23ccd192 100644 --- a/apps/ui/src/components/views/settings-view/api-keys/hooks/use-api-key-management.ts +++ b/apps/ui/src/components/views/settings-view/api-keys/hooks/use-api-key-management.ts @@ -1,4 +1,4 @@ -// @ts-nocheck - API key management state with validation and persistence +// API key management state with validation and persistence import { useState, useEffect } from 'react'; import { useQueryClient } from '@tanstack/react-query'; import { createLogger } from '@automaker/utils/logger'; @@ -23,20 +23,44 @@ interface ApiKeyStatus { hasZaiKey: boolean; } +/** Shape of the configure API response */ +interface ConfigureResponse { + success?: boolean; + isAvailable?: boolean; + error?: string; +} + +/** Shape of a verify API response */ +interface VerifyResponse { + success?: boolean; + authenticated?: boolean; + message?: string; + error?: string; +} + +/** Shape of an API key status response from the env check */ +interface ApiKeyStatusResponse { + success: boolean; + hasAnthropicKey: boolean; + hasGoogleKey: boolean; + hasOpenaiKey: boolean; + hasZaiKey?: boolean; +} + /** * Custom hook for managing API key state and operations * Handles input values, visibility toggles, connection testing, and saving */ export function useApiKeyManagement() { const { apiKeys, setApiKeys } = useAppStore(); - const { setZaiAuthStatus } = useSetupStore(); + const { setZaiAuthStatus, zaiAuthStatus } = useSetupStore(); const queryClient = useQueryClient(); // API key values - const [anthropicKey, setAnthropicKey] = useState(apiKeys.anthropic); - const [googleKey, setGoogleKey] = useState(apiKeys.google); - const [openaiKey, setOpenaiKey] = useState(apiKeys.openai); - const [zaiKey, setZaiKey] = useState(apiKeys.zai); + const [anthropicKey, setAnthropicKey] = useState(apiKeys.anthropic); + const [googleKey, setGoogleKey] = useState(apiKeys.google); + const [openaiKey, setOpenaiKey] = useState(apiKeys.openai); + const [zaiKey, setZaiKey] = useState(apiKeys.zai); // Visibility toggles const [showAnthropicKey, setShowAnthropicKey] = useState(false); @@ -74,7 +98,7 @@ export function useApiKeyManagement() { const api = getElectronAPI(); if (api?.setup?.getApiKeys) { try { - const status = await api.setup.getApiKeys(); + const status: ApiKeyStatusResponse = await api.setup.getApiKeys(); if (status.success) { setApiKeyStatus({ hasAnthropicKey: status.hasAnthropicKey, @@ -92,7 +116,7 @@ export function useApiKeyManagement() { }, []); // Test Anthropic/Claude connection - const handleTestAnthropicConnection = async () => { + const handleTestAnthropicConnection = async (): Promise => { // Validate input first if (!anthropicKey || anthropicKey.trim().length === 0) { setTestResult({ @@ -106,7 +130,7 @@ export function useApiKeyManagement() { setTestResult(null); try { - const api = getElectronAPI(); + const api = getHttpApiClient(); // Pass the current input value to test unsaved keys const data = await api.setup.verifyClaudeAuth('api_key', anthropicKey); @@ -133,7 +157,7 @@ export function useApiKeyManagement() { // Test Google/Gemini connection // TODO: Add backend endpoint for Gemini API key verification - const handleTestGeminiConnection = async () => { + const handleTestGeminiConnection = async (): Promise => { setTestingGeminiConnection(true); setGeminiTestResult(null); @@ -157,12 +181,12 @@ export function useApiKeyManagement() { }; // Test OpenAI/Codex connection - const handleTestOpenaiConnection = async () => { + const handleTestOpenaiConnection = async (): Promise => { setTestingOpenaiConnection(true); setOpenaiTestResult(null); try { - const api = getElectronAPI(); + const api = getHttpApiClient(); const data = await api.setup.verifyCodexAuth('api_key', openaiKey); if (data.success && data.authenticated) { @@ -187,7 +211,7 @@ export function useApiKeyManagement() { }; // Test z.ai connection - const handleTestZaiConnection = async () => { + const handleTestZaiConnection = async (): Promise => { setTestingZaiConnection(true); setZaiTestResult(null); @@ -204,7 +228,7 @@ export function useApiKeyManagement() { try { const api = getElectronAPI(); // Use the verify endpoint to test the key without storing it - const response = await api.zai?.verify(zaiKey); + const response: VerifyResponse | undefined = await api.zai?.verify(zaiKey); if (response?.success && response?.authenticated) { setZaiTestResult({ @@ -228,42 +252,70 @@ export function useApiKeyManagement() { }; // Save API keys - const handleSave = async () => { - setApiKeys({ - anthropic: anthropicKey, - google: googleKey, - openai: openaiKey, - zai: zaiKey, - }); - + const handleSave = async (): Promise => { // Configure z.ai service on the server with the new key if (zaiKey && zaiKey.trim().length > 0) { try { const api = getHttpApiClient(); - const result = await api.zai.configure(zaiKey.trim()); + const result: ConfigureResponse = await api.zai.configure(zaiKey.trim()); + + if (result.success) { + // Only persist to local store after server confirms success + setApiKeys({ + anthropic: anthropicKey, + google: googleKey, + openai: openaiKey, + zai: zaiKey, + }); + + // Preserve the existing hasEnvApiKey flag from current auth status + const currentHasEnvApiKey = zaiAuthStatus?.hasEnvApiKey ?? false; - if (result.success || result.isAvailable) { // Update z.ai auth status in the store setZaiAuthStatus({ authenticated: true, method: 'api_key' as ZaiAuthMethod, hasApiKey: true, - hasEnvApiKey: false, + hasEnvApiKey: currentHasEnvApiKey, }); // Invalidate the z.ai usage query so it refetches with the new key await queryClient.invalidateQueries({ queryKey: queryKeys.usage.zai() }); logger.info('z.ai API key configured successfully'); + } else { + // Server config failed - still save other keys but log the issue + logger.error('z.ai API key configuration failed on server'); + setApiKeys({ + anthropic: anthropicKey, + google: googleKey, + openai: openaiKey, + zai: zaiKey, + }); } } catch (error) { logger.error('Failed to configure z.ai API key:', error); + // Still save other keys even if z.ai config fails + setApiKeys({ + anthropic: anthropicKey, + google: googleKey, + openai: openaiKey, + zai: zaiKey, + }); } } else { + // Save keys (z.ai key is empty/removed) + setApiKeys({ + anthropic: anthropicKey, + google: googleKey, + openai: openaiKey, + zai: zaiKey, + }); + // Clear z.ai auth status if key is removed setZaiAuthStatus({ authenticated: false, method: 'none' as ZaiAuthMethod, hasApiKey: false, - hasEnvApiKey: false, + hasEnvApiKey: zaiAuthStatus?.hasEnvApiKey ?? false, }); // Invalidate the query to clear any cached data await queryClient.invalidateQueries({ queryKey: queryKeys.usage.zai() }); diff --git a/apps/ui/src/hooks/use-auto-mode.ts b/apps/ui/src/hooks/use-auto-mode.ts index c6dba5b3..70da21ee 100644 --- a/apps/ui/src/hooks/use-auto-mode.ts +++ b/apps/ui/src/hooks/use-auto-mode.ts @@ -172,7 +172,10 @@ export function useAutoMode(worktree?: WorktreeInfo) { (backendIsRunning && Array.isArray(backendRunningFeatures) && backendRunningFeatures.length > 0 && - !arraysEqual(backendRunningFeatures, runningAutoTasks)); + !arraysEqual(backendRunningFeatures, runningAutoTasks)) || + // Also sync when UI has stale running tasks but backend has none + // (handles server restart where features were reconciled to backlog/ready) + (!backendIsRunning && runningAutoTasks.length > 0 && backendRunningFeatures.length === 0); if (needsSync) { const worktreeDesc = branchName ? `worktree ${branchName}` : 'main worktree'; diff --git a/apps/ui/src/hooks/use-provider-auth-init.ts b/apps/ui/src/hooks/use-provider-auth-init.ts index f8919b1e..02ae801e 100644 --- a/apps/ui/src/hooks/use-provider-auth-init.ts +++ b/apps/ui/src/hooks/use-provider-auth-init.ts @@ -108,22 +108,41 @@ export function useProviderAuthInit() { try { const result = await api.zai.getStatus(); if (result.success || result.available !== undefined) { + const available = !!result.available; + const hasApiKey = !!(result.hasApiKey ?? result.available); + const hasEnvApiKey = !!(result.hasEnvApiKey ?? false); + let method: ZaiAuthMethod = 'none'; - if (result.hasEnvApiKey) { + if (hasEnvApiKey) { method = 'api_key_env'; - } else if (result.hasApiKey || result.available) { + } else if (hasApiKey || available) { method = 'api_key'; } setZaiAuthStatus({ - authenticated: result.available, + authenticated: available, method, - hasApiKey: result.hasApiKey ?? result.available, - hasEnvApiKey: result.hasEnvApiKey ?? false, + hasApiKey, + hasEnvApiKey, + }); + } else { + // Non-success path - set default unauthenticated status + setZaiAuthStatus({ + authenticated: false, + method: 'none', + hasApiKey: false, + hasEnvApiKey: false, }); } } catch (error) { logger.error('Failed to init z.ai auth status:', error); + // Set default status on error to prevent stale state + setZaiAuthStatus({ + authenticated: false, + method: 'none', + hasApiKey: false, + hasEnvApiKey: false, + }); } // 4. Gemini Auth Status @@ -134,7 +153,7 @@ export function useProviderAuthInit() { setGeminiCliStatus({ installed: result.installed ?? false, version: result.version, - path: result.status, + path: result.path, }); // Set Auth status - always set a status to mark initialization as complete diff --git a/apps/ui/src/lib/http-api-client.ts b/apps/ui/src/lib/http-api-client.ts index 4238f58e..71fb68f7 100644 --- a/apps/ui/src/lib/http-api-client.ts +++ b/apps/ui/src/lib/http-api-client.ts @@ -41,7 +41,12 @@ import type { Notification, } from '@automaker/types'; import type { Message, SessionListItem } from '@/types/electron'; -import type { ClaudeUsageResponse, CodexUsageResponse, GeminiUsage } from '@/store/app-store'; +import type { + ClaudeUsageResponse, + CodexUsageResponse, + GeminiUsage, + ZaiUsageResponse, +} from '@/store/app-store'; import type { WorktreeAPI, GitAPI, ModelDefinition, ProviderStatus } from '@/types/electron'; import type { ModelId, ThinkingLevel, ReasoningEffort, Feature } from '@automaker/types'; import { getGlobalFileBrowser } from '@/contexts/file-browser-context'; @@ -1748,35 +1753,7 @@ export class HttpApiClient implements ElectronAPI { error?: string; }> => this.get('/api/zai/status'), - getUsage: (): Promise<{ - quotaLimits?: { - tokens?: { - limitType: string; - limit: number; - used: number; - remaining: number; - usedPercent: number; - nextResetTime: number; - }; - time?: { - limitType: string; - limit: number; - used: number; - remaining: number; - usedPercent: number; - nextResetTime: number; - }; - planType: string; - } | null; - usageDetails?: Array<{ - modelId: string; - used: number; - limit: number; - }>; - lastUpdated: string; - error?: string; - message?: string; - }> => this.get('/api/zai/usage'), + getUsage: (): Promise => this.get('/api/zai/usage'), configure: ( apiToken?: string, diff --git a/apps/ui/src/store/app-store.ts b/apps/ui/src/store/app-store.ts index 034b1aa5..235fd9f6 100644 --- a/apps/ui/src/store/app-store.ts +++ b/apps/ui/src/store/app-store.ts @@ -321,6 +321,8 @@ const initialState: AppState = { codexUsageLastUpdated: null, zaiUsage: null, zaiUsageLastUpdated: null, + geminiUsage: null, + geminiUsageLastUpdated: null, codexModels: [], codexModelsLoading: false, codexModelsError: null, @@ -2410,6 +2412,13 @@ export const useAppStore = create()((set, get) => ({ // z.ai Usage Tracking actions setZaiUsage: (usage) => set({ zaiUsage: usage, zaiUsageLastUpdated: usage ? Date.now() : null }), + // Gemini Usage Tracking actions + setGeminiUsage: (usage, lastUpdated) => + set({ + geminiUsage: usage, + geminiUsageLastUpdated: lastUpdated ?? (usage ? Date.now() : null), + }), + // Codex Models actions fetchCodexModels: async (forceRefresh = false) => { const state = get(); diff --git a/apps/ui/src/store/types/state-types.ts b/apps/ui/src/store/types/state-types.ts index bff4d474..25bc3dfa 100644 --- a/apps/ui/src/store/types/state-types.ts +++ b/apps/ui/src/store/types/state-types.ts @@ -34,7 +34,7 @@ import type { ApiKeys } from './settings-types'; import type { ChatMessage, ChatSession } from './chat-types'; import type { TerminalState, TerminalPanelContent, PersistedTerminalState } from './terminal-types'; import type { Feature, ProjectAnalysis } from './project-types'; -import type { ClaudeUsage, CodexUsage, ZaiUsage } from './usage-types'; +import type { ClaudeUsage, CodexUsage, ZaiUsage, GeminiUsage } from './usage-types'; /** State for worktree init script execution */ export interface InitScriptState { @@ -299,6 +299,10 @@ export interface AppState { zaiUsage: ZaiUsage | null; zaiUsageLastUpdated: number | null; + // Gemini Usage Tracking + geminiUsage: GeminiUsage | null; + geminiUsageLastUpdated: number | null; + // Codex Models (dynamically fetched) codexModels: Array<{ id: string; @@ -769,6 +773,9 @@ export interface AppActions { // z.ai Usage Tracking actions setZaiUsage: (usage: ZaiUsage | null) => void; + // Gemini Usage Tracking actions + setGeminiUsage: (usage: GeminiUsage | null, lastUpdated?: number) => void; + // Codex Models actions fetchCodexModels: (forceRefresh?: boolean) => Promise; setCodexModels: (