# Execution Plan: Intentional Patterns Validation Actions **Created:** 2026-01-13 **Source:** `docs/reports/intentional-patterns-validation.md` **Target:** `src/services/worker-service.ts` and related files --- ## Phase 0: Documentation Discovery (COMPLETED) ### Evidence Gathered **Files Analyzed:** - `docs/reports/intentional-patterns-validation.md` - Pattern verdicts and recommendations - `docs/reports/nonsense-logic.md` - Original 23 issues identified - `.claude/plans/cleanup-worker-service-nonsense-logic.md` - Existing cleanup plan - `src/services/worker-service.ts` (813 lines) - Current state **Current State:** - File has been reduced from 1445 lines to 813 lines in prior refactoring - `runInteractiveSetup` still exists at line 439 (~200 lines of dead code) - Re-export at line 78: `export { updateCursorContextForProject };` - MCP version hardcoded "1.0.0" at line 159 - Fallback agents set at lines 144-146 without verification - Unused imports: `fs`, `spawn`, `homedir`, `readline` at lines 13-17 **Allowed APIs (from validation report):** - Exit code 0 pattern: **KEEP** (documented Windows Terminal workaround) - `as Error` casts: **KEEP** (documented project policy) - Dual init tracking: **KEEP** (serves async + sync callers) - Signal handler ref pattern: **KEEP** (standard JS mutable state sharing) - Empty MCP capabilities: **KEEP** (correct per MCP spec) **Actions Required:** | Pattern | Action | Priority | |---------|--------|----------| | Re-export for circular import | Remove (no actual circular dep) | LOW | | Fallback agent without check | Add availability verification | HIGH | | MCP version hardcoded | Update to use package.json | LOW | | Dead code `runInteractiveSetup` | Delete (~200 lines) | HIGH | | Unused imports | Delete | LOW | --- ## Phase 1: Delete Dead Code (HIGH PRIORITY) ### 1.1 Delete `runInteractiveSetup` Function **What:** Delete lines 435-639 (approximately 200 lines) **File:** `src/services/worker-service.ts` **Location confirmed:** Line 439 starts `async function runInteractiveSetup(): Promise` **Steps:** 1. Read worker-service.ts lines 435-650 to find exact boundaries 2. Delete the section comment and entire function 3. Run build to verify no compile errors **Verification:** ```bash grep -n "runInteractiveSetup" src/services/worker-service.ts # Expected: No output (function deleted) npm run build # Expected: No errors ``` ### 1.2 Remove Unused Imports **What:** Delete imports only used by dead code **Lines to delete:** 13-17 (check each) **Current imports to remove:** ```typescript import * as fs from 'fs'; // Line 13 - UNUSED (namespace never accessed) import { spawn } from 'child_process'; // Line 14 - UNUSED (MCP uses StdioClientTransport) import { homedir } from 'os'; // Line 15 - Only in dead code import * as readline from 'readline'; // Line 17 - Only in dead code ``` **Keep:** ```typescript import { existsSync, writeFileSync, readFileSync, mkdirSync } from 'fs'; // Line 16 - CHECK ``` **Steps:** 1. After deleting `runInteractiveSetup`, grep each import 2. Delete any with zero usages 3. Run build to verify **Verification:** ```bash grep -n "^import \* as fs" src/services/worker-service.ts grep -n "import { spawn }" src/services/worker-service.ts # Expected: No output npm run build ``` ### 1.3 Remove Unused CursorHooksInstaller Imports **After deleting dead code, check:** ```typescript import { updateCursorContextForProject, // KEEP (re-exported) handleCursorCommand, // KEEP (used in main) detectClaudeCode, // DELETE (only in dead code) findCursorHooksDir, // DELETE (only in dead code) installCursorHooks, // DELETE (only in dead code) configureCursorMcp // DELETE (only in dead code) } from './integrations/CursorHooksInstaller.js'; ``` **Verification:** ```bash grep "detectClaudeCode\|findCursorHooksDir\|installCursorHooks\|configureCursorMcp" src/services/worker-service.ts # Expected: Only import line (which gets trimmed) ``` --- ## Phase 2: Fix Fallback Agent Oversight (HIGH PRIORITY) ### 2.1 Add SDKAgent Availability Check **Problem:** Lines 144-146 set Claude SDK as fallback without verifying it's configured ```typescript this.geminiAgent.setFallbackAgent(this.sdkAgent); this.openRouterAgent.setFallbackAgent(this.sdkAgent); ``` **Risk:** User chooses Gemini because they lack Claude credentials → transient Gemini error → fallback to Claude SDK → cascading failure **Solution Options:** **Option A: Add isConfigured() method to SDKAgent** 1. Add method to SDKAgent that checks for valid Claude SDK credentials 2. Only set fallback if `sdkAgent.isConfigured()` returns true 3. Log warning when fallback unavailable **Pattern to follow (from SDKAgent.ts constructor):** ```typescript // Check if Claude SDK can be initialized public isConfigured(): boolean { // Claude SDK uses subprocess, check if claude command exists try { // Check for ANTHROPIC_API_KEY or claude CLI availability return !!process.env.ANTHROPIC_API_KEY || this.checkClaudeCliAvailable(); } catch { return false; } } ``` **Option B: Document limitation (minimal fix)** Add comment explaining the risk: ```typescript // NOTE: Fallback to Claude SDK may fail if user lacks Claude credentials // Consider adding availability check in future (Issue #XXX) this.geminiAgent.setFallbackAgent(this.sdkAgent); ``` **Recommended: Option A** **Steps:** 1. Read SDKAgent.ts to understand initialization pattern 2. Add `isConfigured()` method that checks Claude CLI/credentials 3. Update worker-service.ts to conditionally set fallback 4. Add warning log when fallback unavailable 5. Run tests **Verification:** ```bash grep -n "isConfigured" src/services/worker/SDKAgent.ts # Expected: Method definition grep -n "setFallbackAgent" src/services/worker-service.ts # Expected: Conditional calls with isConfigured check npm test ``` --- ## Phase 3: Remove Unnecessary Re-Export (LOW PRIORITY) ### 3.1 Fix Misleading Re-Export **Current (worker-service.ts:77-78):** ```typescript // Re-export updateCursorContextForProject for SDK agents export { updateCursorContextForProject }; ``` **Issue:** Comment implies avoiding circular import, but investigation found NO circular dependency exists. **Import chain:** ``` CursorHooksInstaller.ts (defines) → worker-service.ts (imports, re-exports) → ResponseProcessor.ts (imports) ``` **ResponseProcessor.ts could import directly from CursorHooksInstaller.ts** **Options:** 1. **Remove re-export entirely** - Update ResponseProcessor.ts to import from CursorHooksInstaller directly 2. **Fix comment** - Update to reflect actual reason (API surface simplification) **Recommended: Option 1 (cleaner)** **Steps:** 1. Update `src/services/worker/agents/ResponseProcessor.ts`: - Change: `import { updateCursorContextForProject } from '../../worker-service.js';` - To: `import { updateCursorContextForProject } from '../../integrations/CursorHooksInstaller.js';` 2. Delete re-export from worker-service.ts (lines 77-78) 3. Run build to verify **Verification:** ```bash grep -n "export { updateCursorContextForProject" src/services/worker-service.ts # Expected: No output grep -n "updateCursorContextForProject" src/services/worker/agents/ResponseProcessor.ts # Expected: Import from CursorHooksInstaller npm run build ``` --- ## Phase 4: Update MCP Version (LOW PRIORITY) ### 4.1 Use Package Version for MCP Client **Current (worker-service.ts:157-160):** ```typescript this.mcpClient = new Client({ name: 'worker-search-proxy', version: '1.0.0' // Hardcoded, should match package.json (9.0.4) }, { capabilities: {} }); ``` **Also affects (from report):** - `src/services/sync/ChromaSync.ts:126-131` - MCP server (separate file) **Pattern to follow:** ```typescript import { version } from '../../package.json' assert { type: 'json' }; this.mcpClient = new Client({ name: 'worker-search-proxy', version: version }, { capabilities: {} }); ``` **Alternative (if JSON import not supported):** ```typescript import { readFileSync } from 'fs'; const pkg = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url), 'utf-8')); this.mcpClient = new Client({ name: 'worker-search-proxy', version: pkg.version }, { capabilities: {} }); ``` **Steps:** 1. Check if JSON import assertion works in project 2. Update worker-service.ts MCP client initialization 3. Update ChromaSync.ts similarly 4. Run build to verify **Verification:** ```bash grep -n "version: '1.0.0'" src/services/worker-service.ts src/services/sync/ChromaSync.ts # Expected: No output npm run build ``` ### 4.2 Add MCP Capabilities Comment **Current:** ```typescript }, { capabilities: {} }); ``` **Add clarifying comment:** ```typescript }, { // MCP spec: Clients accept all server capabilities; no declaration needed capabilities: {} }); ``` --- ## Phase 5: Verification ### 5.1 Build Check ```bash npm run build ``` **Expected:** No TypeScript errors ### 5.2 Test Suite ```bash npm test ``` **Expected:** All tests pass ### 5.3 Grep for Anti-Patterns ```bash # Verify dead code removed grep -r "runInteractiveSetup" src/ # Expected: No matches # Verify unused imports removed grep "import \* as fs from 'fs'" src/services/worker-service.ts # Expected: No match # Verify re-export removed grep "export { updateCursorContextForProject" src/services/worker-service.ts # Expected: No match # Verify fallback has check grep -A2 "setFallbackAgent" src/services/worker-service.ts # Expected: Conditional with isConfigured check ``` ### 5.4 Runtime Check ```bash npm run build-and-sync # Manually verify worker starts and basic operations work ``` --- ## Summary | Phase | Description | Lines Changed | Priority | |-------|-------------|---------------|----------| | Phase 1 | Delete dead code + imports | ~200 deleted | HIGH | | Phase 2 | Add fallback verification | ~10 added | HIGH | | Phase 3 | Remove re-export | ~5 changed | LOW | | Phase 4 | Update MCP version | ~3 changed | LOW | | Phase 5 | Verification | N/A | N/A | **Execution Order:** Phase 1 → Phase 2 → Phase 3 → Phase 4 → Phase 5 **Note:** Each phase should be followed by verification (build + test) before proceeding. --- ## Patterns Confirmed KEEP (No Action) These patterns were validated as intentional: 1. **Exit code 0 always** - Windows Terminal tab accumulation workaround (commit 222a73da) 2. **`as Error` casts** - Documented project policy with anti-pattern detection 3. **Dual init tracking** - Promise for async, flag for sync callers 4. **Signal handler ref pattern** - Standard JS mutable state sharing 5. **Empty MCP capabilities** - Correct per MCP client spec