Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a79ba183d | |||
| a0807c20a0 | |||
| 03a0b21f38 | |||
| 72c0409c79 | |||
| 3217719709 | |||
| e8c4740389 | |||
| 4a75ea9f99 | |||
| 19f1cdedbb | |||
| 732fc1cd3f | |||
| 4a6df82792 | |||
| 819f98d9fa | |||
| 28a620079f | |||
| fb3a3fbda7 | |||
| 76d1d3b032 | |||
| 3cb05781fa | |||
| d98ff7d19c | |||
| 635b53eeaf | |||
| 5745cb149f |
+231
@@ -1,3 +1,234 @@
|
||||
## 2.7.6 - Stability & Feature Enhancements
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Multi-profile account management** — Unified profile swapping with automatic token refresh and rate limit recovery for both OAuth and API-compatible providers
|
||||
|
||||
- **Enhanced terminal experience** — Customizable terminal fonts with OS-specific defaults, Claude Code CLI settings injection, and improved worktree integration
|
||||
|
||||
- **Advanced roadmap management** — Expand/collapse functionality for phase features and real-time sync with task lifecycle
|
||||
|
||||
- **Queue System v2** — Smart task prioritization with auto-promotion and intelligent rate limit recovery
|
||||
|
||||
- **GitHub integration enhancements** — AI-powered PR template generation, user-friendly API error handling, and improved review visibility
|
||||
|
||||
- **UI/UX improvements** — Spell check support for text inputs, collapsible sidebar toggle, task screenshot capture, expandable task descriptions, and bulk worktree operations
|
||||
|
||||
- **Evidence-based PR validation** — Advanced review system with trigger-driven exploration and enhanced recovery mechanisms
|
||||
|
||||
### 🛠️ Improvements
|
||||
|
||||
- **Performance optimizations** — Async parallel worktree listing prevents UI freezes and improves responsiveness
|
||||
|
||||
- **Robustness enhancements** — Atomic file writes, better error detection in AI responses, and improved OOM/orphaned agent management for overnight builds
|
||||
|
||||
- **Terminal stability** — Fixed GPU context exhaustion from large pastes, SIGABRT crashes on macOS shutdown, and session restoration on app restart
|
||||
|
||||
- **Build & packaging** — XState bundling for packaged apps, aligned Linux package builds, and improved auto-updater for beta releases and DMG installations
|
||||
|
||||
- **Diagnostic improvements** — Sentry instrumentation for Python subprocesses and better error tracking across the system
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Terminal & PTY** — Fixed paste size limits, race conditions, rendering issues, text alignment, worktree crashes, and terminal content resizing on expansion
|
||||
|
||||
- **PR review system** — Resolved error visibility in bundled apps, improved structured output validation with three-tier recovery, preserved findings during crashes, and fixed UTC timestamp detection for comment tracking
|
||||
|
||||
- **Planning & task execution** — Fixed handling of empty/greenfield projects, atomic writes to prevent 0-byte file corruption, planning phase crashes, and implementation plan file watching
|
||||
|
||||
- **Authentication & profiles** — Resolved OAuth token revocation loops, API profile mode support without OAuth requirement, subscription type preservation during token refresh, and Linux credential file detection
|
||||
|
||||
- **Windows/cross-platform** — Complete System32 executable path fixes for where.exe and taskkill.exe, Windows credential normalization, and proper shell detection for Windows terminals
|
||||
|
||||
- **Agent management** — Fixed infinite retry loops for tool concurrency errors, auth error detection, and title generator production path resolution
|
||||
|
||||
- **UI/UX fixes** — Resolved Insights scroll-to-blank-space issues, infinite re-render loops in terminal font settings, kanban board scaling collisions, ideation stuck states, and panel constraint errors during terminal exit
|
||||
|
||||
- **Worktree & Git** — Improved branch pattern validation, removed auto-commit on deletion, support for detached HEAD state during PR creation, and better merge conflict resolution with progress tracking
|
||||
|
||||
- **Integrations** — Fixed Ollama infinite subprocess spawning, Graphiti import paths, OpenRouter API URL suffix, and GitLab authentication bugs
|
||||
|
||||
- **Settings & configuration** — Corrected .auto-claude path discovery timeout, z.AI China preset URL, log order sorting, and onboarding completion state persistence
|
||||
|
||||
### 📚 Documentation
|
||||
|
||||
- Added Awesome Claude Code badge to README
|
||||
|
||||
- Added instructions for resetting PR review state in CLAUDE.md
|
||||
|
||||
---
|
||||
|
||||
## What's Changed
|
||||
|
||||
- fix: handle unknown SDK message types (rate_limit_event) to prevent session crashes by @AndyMik90 in 4a75ea9f9
|
||||
- fix: PR review error visibility and gh CLI resolution in bundled apps by @AndyMik90 in 732fc1cd3
|
||||
- fix: handle empty/greenfield projects in spec creation (#1426) (#1841) by @Andy in 819f98d9f
|
||||
- fix: clear terminalEventSeen on task restart to prevent stuck-after-planning (#1828) (#1840) by @Andy in 28a620079
|
||||
- fix: watch worktree path for implementation_plan.json changes (#1805) (#1842) by @Andy in fb3a3fbda
|
||||
- fix: resolve Claude CLI not found on Windows - PATH, prompt size, cwd (#1661) (#1843) by @Andy in 76d1d3b03
|
||||
- fix: handle planning phase crash and resume recovery (#1562) (#1844) by @Andy in 3cb05781f
|
||||
- fix: show dismissed PR review findings in UI instead of silently dropping them (#1852) by @Andy in d98ff7d19
|
||||
- fix: preserve file/line info in PR review extraction recovery (#1857) by @Andy in 635b53eea
|
||||
- docs: add Awesome Claude Code badge to README (#1838) by @Andy in 2e4b5ac65
|
||||
- test: achieve 100% test coverage for backend CLI commands (#1772) by @StillKnotKnown in 385f04414
|
||||
- fix: cap terminal paste size to 1MB to prevent GPU context exhaustion by @AndyMik90 in 7b0f3a2c0
|
||||
- fix: prevent OOM, orphaned agents, and unbounded growth during overnight builds (#1813) by @Andy in 4091d1d4b
|
||||
- docs: add instructions for resetting PR review state in CLAUDE.md by @AndyMik90 in ecb615802
|
||||
- auto-claude: 217-investigate-symlink-issues-in-work-tree-creation-f (#1808) by @Andy in ae13ce14c
|
||||
- auto-claude: 218-enable-claude-code-features-in-worktree-terminals (#1809) by @Andy in e3b219288
|
||||
- auto-claude: 219-investigate-and-fix-authentication-subscription-sy (#1810) by @Andy in 6204d5fc2
|
||||
- feat(roadmap): add expand/collapse functionality for phase features (#1796) by @Burak in f735f0b49
|
||||
- auto-claude: 216-display-ongoing-pr-review-logs-in-progress (#1807) by @Andy in a4870fa0c
|
||||
- fix(pr-review): reduce structured output failures and preserve findings in recovery (#1806) by @Andy in f1b8cd3a7
|
||||
- fix(sentry): enable Sentry for Python subprocesses and add diagnostic instrumentation (#1804) by @Andy in 4d4234378
|
||||
- fix(pr-review): add three-tier recovery for structured output validation failure (#1797) by @Andy in d1fbccde3
|
||||
- test: improve backend agent test coverage to 94% (#1779) by @StillKnotKnown in ed93df698
|
||||
- fix(github): use UTC timestamps for reviewed_at to fix comment detection (#1795) by @Andy in 8872d33e3
|
||||
- feat: add user-friendly GitHub API error handling (#1790) by @StillKnotKnown in 8ece0009e
|
||||
- fix(roadmap): sync roadmap features with task lifecycle (#1791) by @Andy in 115576e85
|
||||
- fix(github): resolve PR review hanging in bundled app (#1793) by @Andy in 3791b37bb
|
||||
- feat(profiles): implement unified profile swapping across OAuth and API accounts (#1794) by @StillKnotKnown in 282387356
|
||||
- test: improve backend memory system test coverage to 100% (#1780) by @StillKnotKnown in 4f1b7b2a9
|
||||
- fix(ideation): guard against non-string properties in IdeaCard badges by @AndyMik90 in 5e78d748e
|
||||
- fix(updater): convert HTML release notes to markdown before rendering by @AndyMik90 in aa5fc7f95
|
||||
- fix(pr-review): simplify structured output schema to reduce validation failures (#1787) by @Andy in cd8914700
|
||||
- fix(qa): enforce visual verification for UI changes and inject startup commands (#1784) by @Andy in f149a7fbd
|
||||
- fix(plan-files): use atomic writes to prevent 0-byte corruption (#1785) by @Andy in c2245b812
|
||||
- fix(terminal): make worktree dropdown scrollable and show all items by @AndyMik90 in 950da45e4
|
||||
- auto-claude: subtask-1-1 - Add adaptive thinking badge to thinking level label (#1782) by @Andy in 25acf2826
|
||||
- auto-claude: subtask-1-1 - Add overflow-hidden and break-words to subtask cards by @AndyMik90 in 39aa08872
|
||||
- refactor(app-updater): disable automatic downloads and allow intentional downgrades by @AndyMik90 in 8de8039db
|
||||
- fix(auth): detect auth errors in AI response text and prevent retry loops (#1776) by @Andy in f4788e4af
|
||||
- test: achieve 100% coverage for backend core workspace module (#1774) by @StillKnotKnown in 3f95765cf
|
||||
- fix(title-generator): add production path resolution for backend source (#1778) by @Andy in 923880f5b
|
||||
- fix(fast-mode): use setting_sources instead of env var for CLI fast mode (#1771) by @Andy in 390ba6a58
|
||||
- fix(windows): complete System32 executable path fixes for where.exe and taskkill.exe (#1715) by @VDT-91 in aa7f56e5d
|
||||
- fix(worktree): remove auto-commit on deletion and add uncommitted changes warning by @AndyMik90 in cec8e65ee
|
||||
- Smart PR Status Polling System (#1766) by @Andy in 48d5f7a32
|
||||
- feat: simplify thinking system and remove opus-1m model variant (#1760) by @Andy in bb7e18937
|
||||
- auto-claude: 203-fix-pr-review-ui-update-issue (#1732) by @Andy in 7589f8e4f
|
||||
- auto-claude: subtask-2-1 - Create isAPIProfileAuthenticated() function to val (#1745) by @Andy in 57e38a692
|
||||
- auto-claude: 202-fix-kanban-board-scaling-collisions (#1731) by @Andy in d09ebb850
|
||||
- auto-claude: 204-fix-pr-review-ui-not-updating-without-manual-navig (#1734) by @Andy in 087091cef
|
||||
- auto-claude: 203-fix-ui-not-updating-during-pr-review-operations (#1733) by @Andy in f085c08bd
|
||||
- auto-claude: 205-fix-insights-chat-only-shows-last-task-suggestion- (#1735) by @Andy in f121f9cdd
|
||||
- auto-claude: 197-roadmap-generation-stuck-at-50-file-locking-race-c (#1746) by @Andy in f41f15e59
|
||||
- auto-claude: 193-fix-update-context7-mcp-tool-name-from-get-library (#1744) by @Andy in bdff9141a
|
||||
- auto-claude: 192-changelog-generation-multiple-critical-bugs-tasks- (#1725) by @Andy in 8c9a504df
|
||||
- auto-claude: 194-bug-rate-limit-during-task-execution-causes-subtas (#1726) by @Andy in 8a7443d24
|
||||
- auto-claude: 201-bug-pr-review-logs-and-analysis (#1730) by @Andy in e0d53adb4
|
||||
- auto-claude: 196-fix-worktrees-dialog-auto-close-race-condition-and (#1727) by @Andy in 323b0d3be
|
||||
- auto-claude: 199-bug-logs-disappear-after-restart (#1728) by @Andy in d639f6ef8
|
||||
- auto-claude: 198-critical-oauth-token-revocation-causes-infinite-40 (#1747) by @Andy in 4438c0b10
|
||||
- Fix Panel Constraints Error During Terminal Exit (#1757) by @Andy in 32bf353da
|
||||
- auto-claude: 190-bug-context-page-crash-multiple-root-causes-when-v (#1724) by @Andy in 2db36982f
|
||||
- feat: add search/filter to WorktreeSelector dropdown (#1754) by @Andy in 09f059ca3
|
||||
- fix(terminal): push worktree branch to remote with tracking on creation (#1753) by @Andy in b5de0d9ff
|
||||
- auto-claude: 189-subtask-execution-stuck-in-infinite-retry-loop-whe (#1723) by @Andy in 445da186c
|
||||
- auto-claude: 188-terminal-claude-sessions-require-manual-click-to-r (#1743) by @Andy in f8499e965
|
||||
- auto-claude: 200-bug-changelog-and-release-generation (#1729) by @Andy in 826583b82
|
||||
- fix(terminal): use each terminal's cwd for invoke Claude all button (#1756) by @Andy in ac4fe4f42
|
||||
- feat(terminal): read Claude Code CLI settings and inject env vars into PTY sessions (#1750) by @Andy in 152e54093
|
||||
- fix: correct .auto-claude path mismatch causing discovery phase timeout (#1748) by @VDT-91 in 2c2a8a754
|
||||
- fix: remove incorrect /v1 suffix from OpenRouter API URL (#1749) by @StillKnotKnown in 7e799ee57
|
||||
- fix: prevent terminal worktree crash with race condition fixes (#1586) (#1658) by @VDT-91 in 216b58bcf
|
||||
- fix: correct log order sorting and add configurable log order setting (#1720) by @Burak in 2e2b82365
|
||||
- fix(ollama): stop infinite subprocess spawning from useEffect re-render loop (#1716) by @Quentin Veys in acb131b72
|
||||
- fix(graphiti): migrate graphiti_memory imports to canonical paths (#1714) by @Quentin Veys in df528f065
|
||||
- fix: improve auto-updater for beta releases and DMG installs (#1681) by @Andy in ff91a1af0
|
||||
- feat: unified operation registry for intelligent auth/rate limit recovery (#1698) by @Andy in 6d0222fa9
|
||||
- fix: Prevent stale worktree data from overriding correct task status (#1710) by @Burak in fe08c644c
|
||||
- feat: add subscriptionType and rateLimitTier to ClaudeProfile (#1688) by @Andy in a5e3cc9a2
|
||||
- auto-claude: subtask-1-1 - Add useTaskStore import and update task state after successful PR creation (#1683) by @Andy in 4587162e4
|
||||
- auto-claude: 182-implement-pagination-and-filtering-for-github-pr-l (#1654) by @Andy in b4e6b2fe4
|
||||
- auto-claude: 181-add-expand-button-for-long-task-descriptions (#1653) by @Andy in d9cd300fe
|
||||
- fix(terminal): resolve text alignment issues on expand/minimize (#1650) by @VDT-91 in f5a7e26d9
|
||||
- fix(windows): use full path to where.exe for reliable executable lookup (#1659) by @VDT-91 in 5f63daa3c
|
||||
- fix: resolve ideation stuck at 3/6 types bug (#1660) by @VDT-91 in e6e8da17c
|
||||
- Clarify Local and Origin Branch Distinction (#1652) by @Andy in 9317148b6
|
||||
- auto-claude: 186-set-default-dark-mode-on-startup (#1656) by @Andy in 473020621
|
||||
- auto-claude: subtask-1-1 - Add min-h-0 to enable scrolling in Roadmap tabs (#1655) by @Andy in ae703be9f
|
||||
- fix: XState status lifecycle & cross-project contamination fixes (#1647) by @kaigler in 5293fb399
|
||||
- refactor(frontend): complete XState task state machine migration (#1338) (#1575) by @kaigler in e2f9abadb
|
||||
- Merge conflict resolution progress bar and log viewer (#1620) by @Andy in d16be3077
|
||||
- fix: align Linux package builds (AppImage/deb/Flatpak) with target-specific extraResources (#1623) by @StillKnotKnown in bad1a9b2c
|
||||
- Fix/gitlab bugs (#1519 and #1521) (#1544) by @bu5hm4nn in cd423c65c
|
||||
- feat(kanban): add bulk task delete and worktree cleanup improvements (#1588) by @kaigler in 02ed91c91
|
||||
- fix: add worktree isolation warning to prevent agent escape (#1528) by @kaigler in fe5cc582b
|
||||
- feat(ui): add spell check support for text inputs (#1304) by @kaigler in 8f02a5129
|
||||
- fix(windows): complete Windows credential fixes with path normalization (#1585) by @kaigler in 1e1997167
|
||||
- AI-Powered GitHub PR Template Generation (#1618) by @Andy in 900dd4360
|
||||
- Fix pty.node SIGABRT crash on macOS shutdown (#1619) by @Andy in f355e09d7
|
||||
- fix(merge): use git merge for diverged branches with progress tracking (#1605) by @Andy in bde2ca4b2
|
||||
- Surface Billing/Credit Exhaustion Errors to UI (Issue #1580) (#1617) by @Andy in 7bf12e856
|
||||
- auto-claude: subtask-1-1 - Change $teamId type from ID! to String! in the team query (#1627) by @Andy in 54d0cd2f4
|
||||
- fix(auth): support API profile mode without OAuth requirement (#1616) by @StillKnotKnown in f8cc63af4
|
||||
- fix: agent retry loop for tool concurrency errors (#1546) [v3] (#1606) by @Michael Ludlow in 0aea4fb5e
|
||||
- fix(queue): enforce max parallel tasks and auto-refresh UI (#1594) by @Andy in 4070a4c29
|
||||
- Persist Kanban column collapse state per project via main process (#1579) by @Andy in a1114664e
|
||||
- feat(pr-review): evidence-based validation and trigger-driven exploration (#1593) by @Andy in bfc232825
|
||||
- fix(ui): smart auto-scroll for Insights streaming responses (#1591) by @kaigler in eee97e7ea
|
||||
- fix(changelog): validate Claude CLI exists before generation (#1305) by @kaigler in c1f24c07f
|
||||
- auto-claude: subtask-1-1 - Add min-w-0 class to subtask title row flex container (#1578) by @Andy in 286591c02
|
||||
- auto-claude: subtask-1-1 - Remove Popover wrapper and related functionality from ClaudeCodeStatusBadge (#1566) by @Andy in 8d18cc81a
|
||||
- fix(claude-profile): preserve subscriptionType and rateLimitTier during token refresh (#1556) by @Andy in 52e426a48
|
||||
- auto-claude: subtask-1-1 - Update cancelReview callback to handle both success and failure cases (#1551) by @Andy in d8f00fe5a
|
||||
- fix(backend): prioritize git remote detection over env var for repo (#1555) by @Andy in 9b07ed464
|
||||
- fix(backend): handle detached HEAD state when pushing branch for PR creation (#1560) by @Andy in 2b72694d0
|
||||
- fix: add explicit UTF-8 encoding across all Electron main process I/O (#1554) by @Andy in 4243530e9
|
||||
- fix(backend): pass OAuth token to Python subprocess for authentication by @AndyMik90 in 6f1002dd7
|
||||
- perf(frontend): async parallel worktree listing to prevent UI freezes (#1553) by @Andy in 399a7e736
|
||||
- auto-claude: subtask-1-1 - Remove amber lock indicator line from kanban resize handle (#1557) by @Andy in 83a64b88e
|
||||
- fix(frontend): resolve TerminalFontSettings infinite re-render loop (#1536) by @StillKnotKnown in 1c6266025
|
||||
- fix(frontend): respect hasCompletedOnboarding from ~/.claude.json (#1537) by @StillKnotKnown in 1860c2c43
|
||||
- fix: prevent planner from generating invalid verification types (#1388) (#1529) by @kaigler in 94d941333
|
||||
- fix(frontend): resolve Insights scroll-to-blank-space issue on macOS (ACS-382) (#1535) by @StillKnotKnown in 496b2b96a
|
||||
- feat: add customizable terminal fonts with OS-specific defaults (#1412) by @StillKnotKnown in f289107b8
|
||||
- Add dev mode screenshot capture warning (#1516) by @Andy in 16eeb301a
|
||||
- fix: add worktree isolation warnings to prevent agent escape (ACS-394) (#1495) by @StillKnotKnown in 1e453653b
|
||||
- fix: resolve flaky subprocess-spawn test on Windows CI (ACS-392) (#1494) by @StillKnotKnown in f6b264d56
|
||||
- feat(task-logger): strip ANSI escape codes from logs and extend coverage (#1411) by @StillKnotKnown in 988ec0c25
|
||||
- fix(frontend): use spawn() instead of exec() for Windows terminal launching (#1498) by @StillKnotKnown in 26c9083d3
|
||||
- fix(api-profiles): correct z.AI China preset URL and rename provider presets (#1500) by @StillKnotKnown in 05cf0a516
|
||||
- fix: validate branch pattern before worktree cleanup to prevent deleting wrong branch (#1493) by @StillKnotKnown in 8576754a1
|
||||
- Real-Time Updates for Insights Chat (#1511) by @Andy in d940b6ade
|
||||
- Fix Terminal UI Rendering Issues (#1514) by @Andy in 8d8306b8e
|
||||
- Fix terminal content resizing on expansion (#1512) by @Andy in 9f6c0026b
|
||||
- Restore Terminal Session History on App Restart (#1515) by @Andy in 63e2847fc
|
||||
- Move Reference Images Above Task Title & Fix Image Display Issues (#1513) by @Andy in b269ac305
|
||||
- auto-claude: 143-fix-github-integration-ui-refresh-issues (#1467) by @Andy in aa2cb4fa6
|
||||
- feat: Multi-profile account swapping with token refresh and queue routing (#1496) by @Andy in 1e72c8d77
|
||||
- Simplified Testing Strategy for Regression Prevention (#1379) by @Andy in ae4e48e8b
|
||||
- auto-claude: 152-persist-tasks-during-roadmap-regeneration (#1463) by @Andy in 9bd3d7e3b
|
||||
- Debug Kanban Memory & Add Sentry Monitoring (#1380) by @Andy in bc5f550ee
|
||||
- auto-claude: 147-remove-outdated-compatibility-shims (#1465) by @Andy in 53111dbb9
|
||||
- auto-claude: 162-fix-worktree-error-on-repeated-task-starts (#1453) by @Andy in b955badf7
|
||||
- auto-claude: 155-fix-pr-list-diff-display-metrics (#1458) by @Andy in 31f116db5
|
||||
- auto-claude: 151-fix-pr-review-agent-token-refresh-on-account-swap (#1456) by @Andy in d081af042
|
||||
- auto-claude: 148-add-progress-persistence-and-status-indicators (#1464) by @Andy in 4937d5745
|
||||
- auto-claude: 154-fix-task-modal-conflict-check-status-refresh (#1462) by @Andy in 0299009df
|
||||
- auto-claude: 153-widen-kanban-columns-and-add-collapse-feature (#1457) by @Andy in d65973075
|
||||
- auto-claude: subtask-1-1 - Add filter after map operation to remove empty str (#1466) by @Andy in 783f0fe0e
|
||||
- fix: add formatReleaseNotes helper for markdown changelog rendering (#1468) by @Andy in 43a97e1b3
|
||||
- feat(sidebar): add collapsible sidebar toggle (#1501) by @Michael Ludlow in d17c17887
|
||||
- fix(auth): check .credentials.json for Linux profile authentication (#1492) by @StillKnotKnown in 8d2f66291
|
||||
- auto-claude: subtask-1-1 - Replace ReleaseNotesRenderer with ReactMarkdown (#1454) by @Andy in 1185a558c
|
||||
- auto-claude: 156-fix-electron-app-version-detection-bug (#1459) by @Andy in 9a3b48c25
|
||||
- auto-claude: subtask-1-1 - Add --no-track flag to git worktree add command (#1455) by @Andy in 0c2990815
|
||||
- auto-claude: subtask-1-1 - Change task.specId to taskId in 3 startSpecCreation calls (#1461) by @Andy in 91edc0e14
|
||||
- fix(onboarding): align MemoryStep layout with Settings MemoryBackendSection (#1445) by @Michael Ludlow in e9de26d59
|
||||
- auto-claude: subtask-1-1 - Add metadata?.requireReviewBeforeCoding check (#1460) by @Andy in 426d56571
|
||||
- fix: use API profile environment variables for task title generation (#1471) by @JoshuaRileyDev in c5a0f042d
|
||||
- fix(auth): Long-lived OAuth authentication with multi-profile usage display (#1443) by @Andy in 12e788417
|
||||
- feat: Add screenshot capture to task creation modal (#1429) by @JoshuaRileyDev in 1a2a1b1fc
|
||||
- fix: prevent queue settings modal from disappearing when tasks change (#1430) by @JoshuaRileyDev in 33acc1430
|
||||
- feat: Queue System v2 with Auto-Promotion and Smart Task Management (#1203) by @JoshuaRileyDev in 3b87e24d7
|
||||
- feat: Add API profile providers usage endpoints support (#1279) by @StillKnotKnown in cfe7dedd0
|
||||
|
||||
## Thanks to all contributors
|
||||
|
||||
@AndyMik90, @Andy, @Burak, @StillKnotKnown, @VDT-91, @kaigler, @Michael Ludlow, @JoshuaRileyDev, @Quentin Veys, @bu5hm4nn
|
||||
|
||||
## 2.7.5 - Security & Platform Improvements
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
@@ -40,6 +40,8 @@ Auto Claude is a desktop application (+ CLI) where users describe a goal and AI
|
||||
|
||||
**PR target** — Always target the `develop` branch for PRs to AndyMik90/Auto-Claude, NOT `main`.
|
||||
|
||||
**No console.log for debugging production issues** — `console.log` output is not visible in bundled/packaged versions of the Electron app. Use Sentry for error tracking and diagnostics in production. Reserve `console.log` for development only.
|
||||
|
||||
## Work Approach
|
||||
|
||||
**Investigate before speculating** — Always read the actual code before proposing root causes. Spawn agents to grep and read relevant source files before forming any hypothesis. Never guess at causes without evidence from the codebase.
|
||||
|
||||
@@ -17,18 +17,18 @@
|
||||
### Stable Release
|
||||
|
||||
<!-- STABLE_VERSION_BADGE -->
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.5)
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6)
|
||||
<!-- STABLE_VERSION_BADGE_END -->
|
||||
|
||||
<!-- STABLE_DOWNLOADS -->
|
||||
| Platform | Download |
|
||||
|----------|----------|
|
||||
| **Windows** | [Auto-Claude-2.7.5-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.5-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.5-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.5-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.5-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.5-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-linux-x86_64.flatpak) |
|
||||
| **Windows** | [Auto-Claude-2.7.6-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-linux-x86_64.flatpak) |
|
||||
<!-- STABLE_DOWNLOADS_END -->
|
||||
|
||||
### Beta Release
|
||||
@@ -36,18 +36,18 @@
|
||||
> ⚠️ Beta releases may contain bugs and breaking changes. [View all releases](https://github.com/AndyMik90/Auto-Claude/releases)
|
||||
|
||||
<!-- BETA_VERSION_BADGE -->
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.5)
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.6)
|
||||
<!-- BETA_VERSION_BADGE_END -->
|
||||
|
||||
<!-- BETA_DOWNLOADS -->
|
||||
| Platform | Download |
|
||||
|----------|----------|
|
||||
| **Windows** | [Auto-Claude-2.7.6-beta.5-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.5/Auto-Claude-2.7.6-beta.5-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-beta.5-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.5/Auto-Claude-2.7.6-beta.5-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-beta.5-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.5/Auto-Claude-2.7.6-beta.5-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-beta.5-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.5/Auto-Claude-2.7.6-beta.5-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-beta.5-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.5/Auto-Claude-2.7.6-beta.5-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-beta.5-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.5/Auto-Claude-2.7.6-beta.5-linux-x86_64.flatpak) |
|
||||
| **Windows** | [Auto-Claude-2.7.6-beta.6-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-beta.6-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-beta.6-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-beta.6-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-beta.6-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-beta.6-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-linux-x86_64.flatpak) |
|
||||
<!-- BETA_DOWNLOADS_END -->
|
||||
|
||||
> All releases include SHA256 checksums and VirusTotal scan results for security verification.
|
||||
|
||||
@@ -19,5 +19,5 @@ Quick Start:
|
||||
See README.md for full documentation.
|
||||
"""
|
||||
|
||||
__version__ = "2.7.6-beta.5"
|
||||
__version__ = "2.7.6"
|
||||
__author__ = "Auto Claude Team"
|
||||
|
||||
@@ -14,6 +14,7 @@ from core.error_utils import (
|
||||
is_authentication_error,
|
||||
is_rate_limit_error,
|
||||
is_tool_concurrency_error,
|
||||
safe_receive_messages,
|
||||
)
|
||||
from core.file_utils import write_json_atomic
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
@@ -490,7 +491,7 @@ async def run_agent_session(
|
||||
# Collect response text and show tool use
|
||||
response_text = ""
|
||||
debug("session", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="session"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
+107
-1
@@ -29,6 +29,89 @@ from core.platform import (
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# =============================================================================
|
||||
# SDK Message Parser Patch
|
||||
# =============================================================================
|
||||
# The Claude Agent SDK's message_parser raises MessageParseError for unknown
|
||||
# message types (e.g., "rate_limit_event"). Since parse_message runs inside an
|
||||
# async generator, the exception kills the entire agent session stream.
|
||||
# Patch to log a warning and return a SystemMessage instead of crashing.
|
||||
# This is needed until the SDK natively handles all CLI message types.
|
||||
|
||||
|
||||
def _patch_sdk_message_parser() -> None:
|
||||
"""Patch the SDK's parse_message to handle unknown message types gracefully.
|
||||
|
||||
The Claude CLI may emit message types that the installed SDK version doesn't
|
||||
recognize (e.g., rate_limit_event, usage_event). Without this patch, any
|
||||
unrecognized type raises MessageParseError inside the SDK's async generator,
|
||||
which terminates the entire response stream and kills the agent session.
|
||||
|
||||
The patch converts unknown types into SystemMessage objects with a
|
||||
'unknown_<type>' subtype, which all message consumers silently skip.
|
||||
"""
|
||||
try:
|
||||
import claude_agent_sdk._internal.message_parser as _parser
|
||||
from claude_agent_sdk._errors import MessageParseError
|
||||
from claude_agent_sdk.types import SystemMessage
|
||||
|
||||
_original_parse = _parser.parse_message
|
||||
|
||||
def _patched_parse(data):
|
||||
try:
|
||||
return _original_parse(data)
|
||||
except MessageParseError as e:
|
||||
msg = str(e)
|
||||
if "Unknown message type" in msg:
|
||||
msg_type = (
|
||||
data.get("type", "unknown")
|
||||
if isinstance(data, dict)
|
||||
else "unknown"
|
||||
)
|
||||
# Rate limit events deserve a visible warning; others just debug-level
|
||||
if "rate_limit" in msg_type:
|
||||
retry_after = (
|
||||
data.get("retry_after")
|
||||
or data.get("data", {}).get("retry_after")
|
||||
if isinstance(data, dict)
|
||||
else None
|
||||
)
|
||||
retry_info = (
|
||||
f" (retry_after={retry_after}s)" if retry_after else ""
|
||||
)
|
||||
logger.warning(
|
||||
f"Rate limit event received from CLI{retry_info} — "
|
||||
f"the SDK will handle backoff automatically"
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"SDK received unhandled message type '{msg_type}', skipping"
|
||||
)
|
||||
return SystemMessage(
|
||||
subtype=f"unknown_{msg_type}",
|
||||
data=data if isinstance(data, dict) else {},
|
||||
)
|
||||
raise
|
||||
|
||||
_parser.parse_message = _patched_parse
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to patch SDK message parser: {e}")
|
||||
|
||||
|
||||
_patch_sdk_message_parser()
|
||||
|
||||
# =============================================================================
|
||||
# Windows System Prompt Limits
|
||||
# =============================================================================
|
||||
# Windows CreateProcessW has a 32,768 character limit for the entire command line.
|
||||
# When CLAUDE.md is very large and passed as --system-prompt, the command can exceed
|
||||
# this limit, causing ERROR_FILE_NOT_FOUND. We cap CLAUDE.md content to stay safe.
|
||||
# 20,000 chars leaves ~12KB headroom for CLI overhead (model, tools, MCP config, etc.)
|
||||
WINDOWS_MAX_SYSTEM_PROMPT_CHARS = 20000
|
||||
WINDOWS_TRUNCATION_MESSAGE = (
|
||||
"\n\n[... CLAUDE.md truncated due to Windows command-line length limit ...]"
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Project Index Cache
|
||||
# =============================================================================
|
||||
@@ -821,8 +904,31 @@ def create_client(
|
||||
if should_use_claude_md():
|
||||
claude_md_content = load_claude_md(project_dir)
|
||||
if claude_md_content:
|
||||
# On Windows, the SDK passes system_prompt as a --system-prompt CLI argument.
|
||||
# Windows CreateProcessW has a 32,768 character limit for the entire command line.
|
||||
# When CLAUDE.md is very large, the command can exceed this limit, causing Windows
|
||||
# to return ERROR_FILE_NOT_FOUND which the SDK misreports as "Claude Code not found".
|
||||
# Cap CLAUDE.md content to keep total command line under the limit. (#1661)
|
||||
was_truncated = False
|
||||
if is_windows():
|
||||
max_claude_md_chars = (
|
||||
WINDOWS_MAX_SYSTEM_PROMPT_CHARS
|
||||
- len(base_prompt)
|
||||
- len(WINDOWS_TRUNCATION_MESSAGE)
|
||||
- len("\n\n# Project Instructions (from CLAUDE.md)\n\n")
|
||||
)
|
||||
if len(claude_md_content) > max_claude_md_chars > 0:
|
||||
claude_md_content = (
|
||||
claude_md_content[:max_claude_md_chars]
|
||||
+ WINDOWS_TRUNCATION_MESSAGE
|
||||
)
|
||||
print(
|
||||
" - CLAUDE.md: truncated (exceeded Windows command-line limit)"
|
||||
)
|
||||
was_truncated = True
|
||||
base_prompt = f"{base_prompt}\n\n# Project Instructions (from CLAUDE.md)\n\n{claude_md_content}"
|
||||
print(" - CLAUDE.md: included in system prompt")
|
||||
if not was_truncated:
|
||||
print(" - CLAUDE.md: included in system prompt")
|
||||
else:
|
||||
print(" - CLAUDE.md: not found in project root")
|
||||
else:
|
||||
|
||||
@@ -6,7 +6,17 @@ Common error detection and classification functions used across
|
||||
agent sessions, QA, and other modules.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import AsyncIterator
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from claude_agent_sdk.types import Message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_tool_concurrency_error(error: Exception) -> bool:
|
||||
@@ -118,3 +128,61 @@ def is_authentication_error(error: Exception) -> bool:
|
||||
"please login again",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def safe_receive_messages(
|
||||
client,
|
||||
*,
|
||||
caller: str = "agent",
|
||||
) -> AsyncIterator[Message]:
|
||||
"""Iterate over SDK messages with resilience against unexpected errors.
|
||||
|
||||
The SDK's ``receive_response()`` async generator can terminate early if:
|
||||
1. An unhandled message type slips past the monkey-patch (e.g., SDK upgrade
|
||||
removes the patch surface).
|
||||
2. A transient parse error corrupts a single message in the stream.
|
||||
3. An unexpected ``StopAsyncIteration`` or runtime error occurs mid-stream.
|
||||
|
||||
This wrapper catches per-message errors, logs them, and continues yielding
|
||||
subsequent messages so the agent session can complete its work.
|
||||
|
||||
It also detects rate-limit events (surfaced as ``SystemMessage`` with
|
||||
subtype ``unknown_rate_limit_event``) and logs a user-visible warning.
|
||||
|
||||
Args:
|
||||
client: A ``ClaudeSDKClient`` instance (must be inside ``async with``).
|
||||
caller: Label for log messages (e.g., "session", "agent_runner").
|
||||
|
||||
Yields:
|
||||
Parsed ``Message`` objects from the SDK response stream.
|
||||
"""
|
||||
try:
|
||||
async for msg in client.receive_response():
|
||||
# Detect rate-limit events surfaced by the monkey-patch
|
||||
msg_type = type(msg).__name__
|
||||
if msg_type == "SystemMessage":
|
||||
subtype = getattr(msg, "subtype", "")
|
||||
if subtype.startswith("unknown_"):
|
||||
original_type = subtype[len("unknown_") :]
|
||||
if "rate_limit" in original_type:
|
||||
data = getattr(msg, "data", {})
|
||||
retry_after = data.get("retry_after") or data.get(
|
||||
"data", {}
|
||||
).get("retry_after")
|
||||
retry_info = (
|
||||
f" (retry in {retry_after}s)" if retry_after else ""
|
||||
)
|
||||
logger.warning(f"[{caller}] Rate limit event{retry_info}")
|
||||
else:
|
||||
logger.debug(
|
||||
f"[{caller}] Skipping unknown SDK message type: {original_type}"
|
||||
)
|
||||
continue
|
||||
yield msg
|
||||
except GeneratorExit:
|
||||
return
|
||||
except Exception as e:
|
||||
# If the generator itself raises (e.g., transport error), log and stop
|
||||
# gracefully so callers can process whatever was collected so far.
|
||||
logger.error(f"[{caller}] SDK response stream terminated unexpectedly: {e}")
|
||||
return
|
||||
|
||||
@@ -1211,6 +1211,9 @@ class WorktreeManager:
|
||||
)
|
||||
|
||||
target = target_branch or self.base_branch
|
||||
# Strip remote prefix (e.g., "origin/feat/x" → "feat/x") since gh expects branch names only
|
||||
if target.startswith("origin/"):
|
||||
target = target[len("origin/") :]
|
||||
pr_title = title or f"auto-claude: {spec_name}"
|
||||
|
||||
# Try AI-powered PR body from project's PR template, fall back to spec summary
|
||||
@@ -1381,6 +1384,9 @@ class WorktreeManager:
|
||||
)
|
||||
|
||||
target = target_branch or self.base_branch
|
||||
# Strip remote prefix (e.g., "origin/feat/x" → "feat/x") since glab expects branch names only
|
||||
if target.startswith("origin/"):
|
||||
target = target[len("origin/") :]
|
||||
mr_title = title or f"auto-claude: {spec_name}"
|
||||
|
||||
# Get MR body from spec.md if available
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
"""
|
||||
Auto Claude MCP Server
|
||||
======================
|
||||
|
||||
Control plane for the Auto Claude autonomous coding pipeline.
|
||||
Exposes all backend capabilities via the Model Context Protocol (MCP).
|
||||
"""
|
||||
|
||||
__version__ = "0.1.0"
|
||||
@@ -1,95 +0,0 @@
|
||||
"""
|
||||
Auto Claude MCP Server Entry Point
|
||||
===================================
|
||||
|
||||
Usage:
|
||||
python -m mcp_server --project-dir /path/to/project
|
||||
python -m mcp_server --project-dir /path/to/project --transport sse --port 8642
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import logging
|
||||
import sys
|
||||
|
||||
# Configure logging to stderr (stdout is reserved for MCP protocol over stdio)
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s",
|
||||
stream=sys.stderr,
|
||||
)
|
||||
logger = logging.getLogger("mcp_server")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Auto Claude MCP Server - control plane for the autonomous coding pipeline",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--project-dir",
|
||||
required=True,
|
||||
help="Path to the project directory to manage",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--transport",
|
||||
choices=["stdio", "sse", "streamable-http"],
|
||||
default="stdio",
|
||||
help="MCP transport to use (default: stdio)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--port",
|
||||
type=int,
|
||||
default=8642,
|
||||
help="Port for SSE/HTTP transport (default: 8642)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--host",
|
||||
default="127.0.0.1",
|
||||
help="Host for SSE/HTTP transport (default: 127.0.0.1)",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--debug",
|
||||
action="store_true",
|
||||
help="Enable debug logging",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.debug:
|
||||
logging.getLogger().setLevel(logging.DEBUG)
|
||||
|
||||
# Initialize project context (adds backend to sys.path, loads .env)
|
||||
from mcp_server.config import initialize
|
||||
|
||||
initialize(args.project_dir)
|
||||
|
||||
# Import server and register tools AFTER initialization
|
||||
# (tools need backend modules on sys.path)
|
||||
from mcp_server.server import mcp, register_all_tools
|
||||
|
||||
register_all_tools()
|
||||
|
||||
logger.info(
|
||||
"Starting Auto Claude MCP server (transport=%s, project=%s)",
|
||||
args.transport,
|
||||
args.project_dir,
|
||||
)
|
||||
|
||||
# For stdio transport, redirect any stray stdout prints to stderr
|
||||
# to prevent corrupting the MCP JSON-RPC protocol
|
||||
if args.transport == "stdio":
|
||||
# Capture any prints from backend modules that write to stdout
|
||||
_original_stdout = sys.stdout
|
||||
sys.stdout = sys.stderr
|
||||
|
||||
# Run the server
|
||||
if args.transport == "stdio":
|
||||
mcp.run(transport="stdio")
|
||||
elif args.transport == "sse":
|
||||
mcp.run(transport="sse", host=args.host, port=args.port)
|
||||
elif args.transport == "streamable-http":
|
||||
mcp.run(transport="streamable-http", host=args.host, port=args.port)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,114 +0,0 @@
|
||||
"""
|
||||
MCP Server Configuration
|
||||
========================
|
||||
|
||||
Manages project context and backend initialization for the MCP server.
|
||||
The project directory is set once at startup and used by all tools.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Global project context - set once at server startup
|
||||
_project_dir: Path | None = None
|
||||
_auto_claude_dir: Path | None = None
|
||||
|
||||
|
||||
def initialize(project_dir: str | Path) -> None:
|
||||
"""Initialize the MCP server with a project directory.
|
||||
|
||||
This sets up the Python path so backend modules can be imported,
|
||||
loads the .env file, and validates the project structure.
|
||||
|
||||
Args:
|
||||
project_dir: Path to the user's project directory
|
||||
"""
|
||||
global _project_dir, _auto_claude_dir
|
||||
|
||||
_project_dir = Path(project_dir).resolve()
|
||||
if not _project_dir.is_dir():
|
||||
raise ValueError(f"Project directory does not exist: {_project_dir}")
|
||||
|
||||
# Add backend to sys.path so existing modules can be imported
|
||||
backend_dir = Path(__file__).parent.parent
|
||||
if str(backend_dir) not in sys.path:
|
||||
sys.path.insert(0, str(backend_dir))
|
||||
|
||||
# Load .env if present
|
||||
try:
|
||||
from cli.utils import import_dotenv
|
||||
|
||||
load_dotenv = import_dotenv()
|
||||
env_file = backend_dir / ".env"
|
||||
if env_file.exists():
|
||||
load_dotenv(env_file)
|
||||
except Exception:
|
||||
logger.debug("Could not load .env file (non-critical)")
|
||||
|
||||
# Determine .auto-claude directory
|
||||
auto_claude = _project_dir / ".auto-claude"
|
||||
if not auto_claude.is_dir():
|
||||
# Also check legacy 'auto-claude' (no dot prefix)
|
||||
alt = _project_dir / "auto-claude"
|
||||
if alt.is_dir():
|
||||
auto_claude = alt
|
||||
else:
|
||||
logger.warning(
|
||||
"No .auto-claude directory found in %s. "
|
||||
"Some tools may not work until the project is initialized.",
|
||||
_project_dir,
|
||||
)
|
||||
_auto_claude_dir = auto_claude
|
||||
|
||||
logger.info("MCP server initialized for project: %s", _project_dir)
|
||||
|
||||
|
||||
def get_project_dir() -> Path:
|
||||
"""Get the active project directory. Raises if not initialized."""
|
||||
if _project_dir is None:
|
||||
raise RuntimeError(
|
||||
"MCP server not initialized. Call config.initialize() first."
|
||||
)
|
||||
return _project_dir
|
||||
|
||||
|
||||
def get_auto_claude_dir() -> Path:
|
||||
"""Get the .auto-claude directory for the active project."""
|
||||
if _auto_claude_dir is None:
|
||||
raise RuntimeError(
|
||||
"MCP server not initialized. Call config.initialize() first."
|
||||
)
|
||||
return _auto_claude_dir
|
||||
|
||||
|
||||
def get_specs_dir() -> Path:
|
||||
"""Get the specs directory for the active project."""
|
||||
return get_auto_claude_dir() / "specs"
|
||||
|
||||
|
||||
def get_project_index() -> dict:
|
||||
"""Load and return the project index if available."""
|
||||
index_path = get_auto_claude_dir() / "project_index.json"
|
||||
if not index_path.exists():
|
||||
return {}
|
||||
try:
|
||||
with open(index_path, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
logger.warning("Failed to load project index: %s", e)
|
||||
return {}
|
||||
|
||||
|
||||
def is_initialized() -> bool:
|
||||
"""Check if the project has been initialized with .auto-claude."""
|
||||
try:
|
||||
ac_dir = get_auto_claude_dir()
|
||||
return ac_dir.is_dir()
|
||||
except RuntimeError:
|
||||
return False
|
||||
@@ -1,158 +0,0 @@
|
||||
"""
|
||||
Long-Running Operation Tracker
|
||||
===============================
|
||||
|
||||
Tracks async operations (spec creation, builds, QA, etc.) so MCP clients
|
||||
can poll for progress. Tools that start long-running work return an
|
||||
operation_id immediately; clients poll operation_get_status() for updates.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import time
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OperationStatus(str, Enum):
|
||||
PENDING = "pending"
|
||||
RUNNING = "running"
|
||||
COMPLETED = "completed"
|
||||
FAILED = "failed"
|
||||
CANCELLED = "cancelled"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Operation:
|
||||
"""Represents a long-running operation."""
|
||||
|
||||
id: str
|
||||
type: str # e.g. "spec_create", "build", "qa_review"
|
||||
status: OperationStatus = OperationStatus.PENDING
|
||||
progress: int = 0 # 0-100
|
||||
message: str = ""
|
||||
result: Any = None
|
||||
error: str | None = None
|
||||
created_at: float = field(default_factory=time.time)
|
||||
updated_at: float = field(default_factory=time.time)
|
||||
_task: asyncio.Task | None = field(default=None, repr=False)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Serialize for MCP response."""
|
||||
return {
|
||||
"id": self.id,
|
||||
"type": self.type,
|
||||
"status": self.status.value,
|
||||
"progress": self.progress,
|
||||
"message": self.message,
|
||||
"result": self.result,
|
||||
"error": self.error,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"elapsed_seconds": round(time.time() - self.created_at, 1),
|
||||
}
|
||||
|
||||
|
||||
class OperationTracker:
|
||||
"""Manages the lifecycle of long-running operations."""
|
||||
|
||||
def __init__(self, max_completed: int = 100):
|
||||
self._operations: dict[str, Operation] = {}
|
||||
self._max_completed = max_completed
|
||||
|
||||
def create(self, operation_type: str, message: str = "") -> Operation:
|
||||
"""Create a new operation and return it."""
|
||||
op = Operation(
|
||||
id=str(uuid.uuid4()),
|
||||
type=operation_type,
|
||||
status=OperationStatus.PENDING,
|
||||
message=message or f"Starting {operation_type}...",
|
||||
)
|
||||
self._operations[op.id] = op
|
||||
self._cleanup_old()
|
||||
return op
|
||||
|
||||
def get(self, operation_id: str) -> Operation | None:
|
||||
"""Get an operation by ID."""
|
||||
return self._operations.get(operation_id)
|
||||
|
||||
def update(
|
||||
self,
|
||||
operation_id: str,
|
||||
*,
|
||||
status: OperationStatus | None = None,
|
||||
progress: int | None = None,
|
||||
message: str | None = None,
|
||||
result: Any = None,
|
||||
error: str | None = None,
|
||||
) -> Operation | None:
|
||||
"""Update an operation's state."""
|
||||
op = self._operations.get(operation_id)
|
||||
if op is None:
|
||||
return None
|
||||
|
||||
if status is not None:
|
||||
op.status = status
|
||||
if progress is not None:
|
||||
op.progress = max(0, min(100, progress))
|
||||
if message is not None:
|
||||
op.message = message
|
||||
if result is not None:
|
||||
op.result = result
|
||||
if error is not None:
|
||||
op.error = error
|
||||
op.updated_at = time.time()
|
||||
return op
|
||||
|
||||
def cancel(self, operation_id: str) -> bool:
|
||||
"""Cancel a running operation."""
|
||||
op = self._operations.get(operation_id)
|
||||
if op is None:
|
||||
return False
|
||||
if op.status in (OperationStatus.COMPLETED, OperationStatus.FAILED):
|
||||
return False
|
||||
|
||||
# Cancel the asyncio task if it exists
|
||||
if op._task and not op._task.done():
|
||||
op._task.cancel()
|
||||
|
||||
op.status = OperationStatus.CANCELLED
|
||||
op.message = "Operation cancelled by user"
|
||||
op.updated_at = time.time()
|
||||
return True
|
||||
|
||||
def list_active(self) -> list[Operation]:
|
||||
"""List all active (non-terminal) operations."""
|
||||
return [
|
||||
op
|
||||
for op in self._operations.values()
|
||||
if op.status in (OperationStatus.PENDING, OperationStatus.RUNNING)
|
||||
]
|
||||
|
||||
def _cleanup_old(self) -> None:
|
||||
"""Remove old completed operations to prevent memory growth."""
|
||||
completed = [
|
||||
op
|
||||
for op in self._operations.values()
|
||||
if op.status
|
||||
in (
|
||||
OperationStatus.COMPLETED,
|
||||
OperationStatus.FAILED,
|
||||
OperationStatus.CANCELLED,
|
||||
)
|
||||
]
|
||||
if len(completed) > self._max_completed:
|
||||
# Sort by created_at, remove oldest
|
||||
completed.sort(key=lambda o: o.created_at)
|
||||
for op in completed[: len(completed) - self._max_completed]:
|
||||
del self._operations[op.id]
|
||||
|
||||
|
||||
# Global singleton
|
||||
tracker = OperationTracker()
|
||||
@@ -1,56 +0,0 @@
|
||||
"""
|
||||
Auto Claude MCP Server
|
||||
======================
|
||||
|
||||
FastMCP server instance with all tool registrations.
|
||||
Tools are organized into modules under mcp_server/tools/.
|
||||
Each module's register() function adds tools to the server.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from fastmcp import FastMCP
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Create the FastMCP server instance
|
||||
mcp = FastMCP(
|
||||
"Auto Claude",
|
||||
instructions=(
|
||||
"Auto Claude is an autonomous multi-agent coding framework. "
|
||||
"Use these tools to manage tasks, create specs, run builds, "
|
||||
"perform QA reviews, manage workspaces, and more. "
|
||||
"Long-running operations return an operation_id - "
|
||||
"poll with operation_get_status() for progress."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def register_all_tools() -> None:
|
||||
"""Register all tool modules with the MCP server.
|
||||
|
||||
Each tool module defines functions decorated with @mcp.tool()
|
||||
that are imported here to trigger registration.
|
||||
"""
|
||||
# Phase 1: Project & Task management
|
||||
# Phase 2: Core autonomous pipeline
|
||||
# Phase 3: Feature tools
|
||||
# Operations management (poll long-running ops)
|
||||
from mcp_server.tools import (
|
||||
execution, # noqa: F401
|
||||
github, # noqa: F401
|
||||
ideation, # noqa: F401
|
||||
insights, # noqa: F401
|
||||
memory, # noqa: F401
|
||||
ops, # noqa: F401
|
||||
project, # noqa: F401
|
||||
qa, # noqa: F401
|
||||
roadmap, # noqa: F401
|
||||
specs, # noqa: F401
|
||||
tasks, # noqa: F401
|
||||
workspace, # noqa: F401
|
||||
)
|
||||
|
||||
logger.info("All MCP tools registered successfully")
|
||||
@@ -1 +0,0 @@
|
||||
"""Service layer - thin adapters wrapping existing backend modules."""
|
||||
@@ -1,337 +0,0 @@
|
||||
"""
|
||||
Execution Service
|
||||
==================
|
||||
|
||||
Service layer for spawning and managing build processes.
|
||||
Wraps the run.py subprocess and parses task events from stdout.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Matches core/task_event.py
|
||||
TASK_EVENT_PREFIX = "__TASK_EVENT__:"
|
||||
|
||||
# Module-level singleton
|
||||
_instance: ExecutionService | None = None
|
||||
|
||||
|
||||
def get_execution_service(project_dir: Path) -> ExecutionService:
|
||||
"""Return a lazily-created singleton ExecutionService.
|
||||
|
||||
If project_dir changes (e.g. user switches projects), a new instance
|
||||
is created so in-memory state matches the active project.
|
||||
"""
|
||||
global _instance
|
||||
if _instance is None or _instance.project_dir != project_dir:
|
||||
_instance = ExecutionService(project_dir)
|
||||
return _instance
|
||||
|
||||
|
||||
class ExecutionService:
|
||||
"""Manages build execution as a subprocess of run.py."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
self._processes: dict[str, asyncio.subprocess.Process] = {}
|
||||
self._logs: dict[str, list[str]] = {}
|
||||
self._events: dict[str, list[dict]] = {}
|
||||
|
||||
async def start_build(
|
||||
self,
|
||||
spec_id: str,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
) -> asyncio.subprocess.Process:
|
||||
"""Spawn a build subprocess for the given spec.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
model: Model shorthand
|
||||
thinking_level: Thinking level
|
||||
|
||||
Returns:
|
||||
The subprocess handle
|
||||
|
||||
Raises:
|
||||
RuntimeError: If a build is already running for this spec
|
||||
"""
|
||||
if spec_id in self._processes:
|
||||
proc = self._processes[spec_id]
|
||||
if proc.returncode is None:
|
||||
raise RuntimeError(
|
||||
f"Build already running for spec '{spec_id}'. "
|
||||
"Stop it first with build_stop()."
|
||||
)
|
||||
|
||||
backend_dir = Path(__file__).parent.parent.parent # apps/backend/
|
||||
run_py = backend_dir / "run.py"
|
||||
if not run_py.exists():
|
||||
raise FileNotFoundError(f"run.py not found at {run_py}")
|
||||
|
||||
cmd = [
|
||||
sys.executable,
|
||||
str(run_py),
|
||||
"--spec",
|
||||
spec_id,
|
||||
"--project-dir",
|
||||
str(self.project_dir),
|
||||
"--model",
|
||||
model,
|
||||
"--thinking",
|
||||
thinking_level,
|
||||
]
|
||||
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
*cmd,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=str(backend_dir),
|
||||
)
|
||||
|
||||
self._processes[spec_id] = proc
|
||||
self._logs[spec_id] = []
|
||||
self._events[spec_id] = []
|
||||
|
||||
# Start background reader for stdout
|
||||
asyncio.create_task(self._read_output(spec_id, proc))
|
||||
|
||||
return proc
|
||||
|
||||
async def _read_output(
|
||||
self, spec_id: str, proc: asyncio.subprocess.Process
|
||||
) -> None:
|
||||
"""Read stdout from the build process, parsing task events.
|
||||
|
||||
Args:
|
||||
spec_id: The spec being built
|
||||
proc: The subprocess to read from
|
||||
"""
|
||||
if proc.stdout is None:
|
||||
return
|
||||
|
||||
try:
|
||||
while True:
|
||||
line_bytes = await proc.stdout.readline()
|
||||
if not line_bytes:
|
||||
break
|
||||
line = line_bytes.decode("utf-8", errors="replace").rstrip("\n")
|
||||
|
||||
# Store the log line
|
||||
log_list = self._logs.get(spec_id)
|
||||
if log_list is not None:
|
||||
log_list.append(line)
|
||||
# Cap stored logs to prevent unbounded growth
|
||||
if len(log_list) > 5000:
|
||||
del log_list[:1000]
|
||||
|
||||
# Parse task events
|
||||
event = self.parse_event(line)
|
||||
if event is not None:
|
||||
events_list = self._events.get(spec_id)
|
||||
if events_list is not None:
|
||||
events_list.append(event)
|
||||
except Exception as e:
|
||||
logger.warning("Error reading build output for %s: %s", spec_id, e)
|
||||
|
||||
def parse_event(self, line: str) -> dict | None:
|
||||
"""Parse a task event line from build stdout.
|
||||
|
||||
Args:
|
||||
line: A line of stdout output
|
||||
|
||||
Returns:
|
||||
Parsed event dict or None if not an event line
|
||||
"""
|
||||
if not line.startswith(TASK_EVENT_PREFIX):
|
||||
return None
|
||||
try:
|
||||
return json.loads(line[len(TASK_EVENT_PREFIX) :])
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
return None
|
||||
|
||||
def stop_build(self, spec_id: str) -> dict:
|
||||
"""Stop a running build process.
|
||||
|
||||
Args:
|
||||
spec_id: The spec being built
|
||||
|
||||
Returns:
|
||||
Status dict
|
||||
"""
|
||||
proc = self._processes.get(spec_id)
|
||||
if proc is None:
|
||||
return {"success": False, "error": f"No build found for spec '{spec_id}'"}
|
||||
|
||||
if proc.returncode is not None:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Build for '{spec_id}' already finished (exit code {proc.returncode})",
|
||||
}
|
||||
|
||||
try:
|
||||
proc.terminate()
|
||||
return {"success": True, "message": f"Build for '{spec_id}' terminated"}
|
||||
except ProcessLookupError:
|
||||
return {"success": False, "error": "Process already exited"}
|
||||
|
||||
def get_progress(self, spec_id: str) -> dict:
|
||||
"""Get progress of a build by inspecting events and process state.
|
||||
|
||||
Args:
|
||||
spec_id: The spec being built
|
||||
|
||||
Returns:
|
||||
Dict with status, events, and process info
|
||||
"""
|
||||
proc = self._processes.get(spec_id)
|
||||
events = self._events.get(spec_id, [])
|
||||
|
||||
if proc is None:
|
||||
# Check if there's a completed implementation plan on disk
|
||||
return self._get_disk_progress(spec_id)
|
||||
|
||||
is_running = proc.returncode is None
|
||||
latest_event = events[-1] if events else None
|
||||
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"running": is_running,
|
||||
"exit_code": proc.returncode,
|
||||
"event_count": len(events),
|
||||
"latest_event": latest_event,
|
||||
"log_lines": len(self._logs.get(spec_id, [])),
|
||||
}
|
||||
|
||||
def get_logs(self, spec_id: str, tail: int = 50) -> dict:
|
||||
"""Get recent build logs.
|
||||
|
||||
Args:
|
||||
spec_id: The spec being built
|
||||
tail: Number of recent lines to return
|
||||
|
||||
Returns:
|
||||
Dict with log lines
|
||||
"""
|
||||
logs = self._logs.get(spec_id, [])
|
||||
if not logs:
|
||||
# Try to find logs on disk
|
||||
return self._get_disk_logs(spec_id, tail)
|
||||
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"total_lines": len(logs),
|
||||
"lines": logs[-tail:],
|
||||
}
|
||||
|
||||
def _get_disk_progress(self, spec_id: str) -> dict:
|
||||
"""Check on-disk state for build progress when no process is tracked.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
|
||||
Returns:
|
||||
Progress dict from disk state
|
||||
"""
|
||||
specs_dir = self.project_dir / ".auto-claude" / "specs"
|
||||
spec_dir = self._resolve_spec_dir(specs_dir, spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found"}
|
||||
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
if not plan_file.exists():
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"running": False,
|
||||
"status": "no_plan",
|
||||
"message": "No implementation plan found. Create a spec first.",
|
||||
}
|
||||
|
||||
try:
|
||||
with open(plan_file, encoding="utf-8") as f:
|
||||
plan = json.load(f)
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"running": False,
|
||||
"status": "error",
|
||||
"message": "Could not read implementation plan",
|
||||
}
|
||||
|
||||
subtasks = plan.get("subtasks", [])
|
||||
completed = sum(1 for s in subtasks if s.get("status") == "completed")
|
||||
total = len(subtasks)
|
||||
|
||||
qa_signoff = plan.get("qa_signoff")
|
||||
if qa_signoff and qa_signoff.get("status") == "approved":
|
||||
status = "qa_approved"
|
||||
elif qa_signoff and qa_signoff.get("status") == "rejected":
|
||||
status = "qa_rejected"
|
||||
elif completed == total and total > 0:
|
||||
status = "build_complete"
|
||||
elif completed > 0:
|
||||
status = "building"
|
||||
else:
|
||||
status = "not_started"
|
||||
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"running": False,
|
||||
"status": status,
|
||||
"subtasks_completed": completed,
|
||||
"subtasks_total": total,
|
||||
"qa_signoff": qa_signoff,
|
||||
}
|
||||
|
||||
def _get_disk_logs(self, spec_id: str, tail: int) -> dict:
|
||||
"""Try to find build logs on disk.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
tail: Number of lines to return
|
||||
|
||||
Returns:
|
||||
Dict with log content
|
||||
"""
|
||||
specs_dir = self.project_dir / ".auto-claude" / "specs"
|
||||
spec_dir = self._resolve_spec_dir(specs_dir, spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found", "lines": []}
|
||||
|
||||
# Check for task log file
|
||||
log_file = spec_dir / "task_log.jsonl"
|
||||
if not log_file.exists():
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"lines": [],
|
||||
"message": "No build logs found",
|
||||
}
|
||||
|
||||
try:
|
||||
lines = log_file.read_text(encoding="utf-8").strip().split("\n")
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"total_lines": len(lines),
|
||||
"lines": lines[-tail:],
|
||||
}
|
||||
except OSError as e:
|
||||
return {"error": str(e), "lines": []}
|
||||
|
||||
def _resolve_spec_dir(self, specs_dir: Path, spec_id: str) -> Path | None:
|
||||
"""Resolve spec_id to directory with prefix matching."""
|
||||
exact = specs_dir / spec_id
|
||||
if exact.is_dir():
|
||||
return exact
|
||||
|
||||
if specs_dir.is_dir():
|
||||
for item in specs_dir.iterdir():
|
||||
if item.is_dir() and item.name.startswith(spec_id):
|
||||
return item
|
||||
return None
|
||||
@@ -1,202 +0,0 @@
|
||||
"""
|
||||
GitHub Service
|
||||
==============
|
||||
|
||||
Wraps the backend GitHubOrchestrator for MCP tool access.
|
||||
Handles repo detection, config creation, and result serialization.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class GitHubService:
|
||||
"""Service layer for GitHub automation features."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
self.github_dir = project_dir / ".auto-claude" / "github"
|
||||
|
||||
def _detect_repo(self) -> str | None:
|
||||
"""Detect owner/repo from git remote origin."""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["git", "remote", "get-url", "origin"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=str(self.project_dir),
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
return None
|
||||
|
||||
url = result.stdout.strip()
|
||||
# Handle SSH: git@github.com:owner/repo.git
|
||||
if url.startswith("git@"):
|
||||
parts = url.split(":")[-1]
|
||||
return parts.removesuffix(".git")
|
||||
# Handle HTTPS: https://github.com/owner/repo.git
|
||||
if "github.com" in url:
|
||||
parts = url.split("github.com/")[-1]
|
||||
return parts.removesuffix(".git")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.warning("Failed to detect repo from git remote: %s", e)
|
||||
return None
|
||||
|
||||
def _get_repo(self, repo: str | None) -> str:
|
||||
"""Get repo string, falling back to auto-detection."""
|
||||
if repo:
|
||||
return repo
|
||||
detected = self._detect_repo()
|
||||
if not detected:
|
||||
raise ValueError(
|
||||
"Could not detect repository. Provide 'repo' parameter "
|
||||
"in owner/repo format, or ensure a GitHub remote is configured."
|
||||
)
|
||||
return detected
|
||||
|
||||
def _create_config(self, repo: str, model: str = "sonnet"):
|
||||
"""Create a GitHubRunnerConfig with sensible defaults."""
|
||||
# Get GitHub token from environment
|
||||
import os
|
||||
|
||||
from runners.github.models import GitHubRunnerConfig
|
||||
|
||||
token = os.environ.get("GITHUB_TOKEN", "")
|
||||
if not token:
|
||||
# Try gh CLI auth token
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["gh", "auth", "token"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=10,
|
||||
)
|
||||
if result.returncode == 0:
|
||||
token = result.stdout.strip()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return GitHubRunnerConfig(
|
||||
token=token,
|
||||
repo=repo,
|
||||
model=model,
|
||||
thinking_level="medium",
|
||||
pr_review_enabled=True,
|
||||
triage_enabled=True,
|
||||
)
|
||||
|
||||
async def review_pr(
|
||||
self, pr_number: int, repo: str | None = None, model: str = "sonnet"
|
||||
) -> dict:
|
||||
"""Review a pull request with AI."""
|
||||
try:
|
||||
from runners.github.orchestrator import GitHubOrchestrator
|
||||
|
||||
resolved_repo = self._get_repo(repo)
|
||||
config = self._create_config(resolved_repo, model)
|
||||
orchestrator = GitHubOrchestrator(
|
||||
project_dir=self.project_dir, config=config
|
||||
)
|
||||
result = await orchestrator.review_pr(pr_number)
|
||||
return {"success": True, "data": result.to_dict()}
|
||||
except ImportError:
|
||||
return {"error": "GitHub runner module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def list_issues(
|
||||
self, state: str = "open", limit: int = 30, repo: str | None = None
|
||||
) -> dict:
|
||||
"""List GitHub issues using gh CLI."""
|
||||
try:
|
||||
resolved_repo = self._get_repo(repo)
|
||||
cmd = [
|
||||
"gh",
|
||||
"issue",
|
||||
"list",
|
||||
"--repo",
|
||||
resolved_repo,
|
||||
"--state",
|
||||
state,
|
||||
"--limit",
|
||||
str(limit),
|
||||
"--json",
|
||||
"number,title,state,labels,author,createdAt,updatedAt",
|
||||
]
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
cwd=str(self.project_dir),
|
||||
timeout=30,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
return {"error": f"gh CLI failed: {result.stderr.strip()}"}
|
||||
issues = json.loads(result.stdout)
|
||||
return {"success": True, "issues": issues, "count": len(issues)}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def auto_fix_issue(self, issue_number: int, repo: str | None = None) -> dict:
|
||||
"""Auto-fix a GitHub issue."""
|
||||
try:
|
||||
from runners.github.orchestrator import GitHubOrchestrator
|
||||
|
||||
resolved_repo = self._get_repo(repo)
|
||||
config = self._create_config(resolved_repo)
|
||||
config.auto_fix_enabled = True
|
||||
orchestrator = GitHubOrchestrator(
|
||||
project_dir=self.project_dir, config=config
|
||||
)
|
||||
state = await orchestrator.auto_fix_issue(issue_number)
|
||||
return {"success": True, "data": state.to_dict()}
|
||||
except ImportError:
|
||||
return {"error": "GitHub runner module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
def get_review(self, pr_number: int) -> dict:
|
||||
"""Get the most recent review result for a PR."""
|
||||
try:
|
||||
from runners.github.models import PRReviewResult
|
||||
|
||||
result = PRReviewResult.load(self.github_dir, pr_number)
|
||||
if result is None:
|
||||
return {"error": f"No review found for PR #{pr_number}"}
|
||||
return {"success": True, "data": result.to_dict()}
|
||||
except ImportError:
|
||||
return {"error": "GitHub runner module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
async def triage_issues(
|
||||
self, issue_numbers: list[int], repo: str | None = None
|
||||
) -> dict:
|
||||
"""Triage and classify GitHub issues."""
|
||||
try:
|
||||
from runners.github.orchestrator import GitHubOrchestrator
|
||||
|
||||
resolved_repo = self._get_repo(repo)
|
||||
config = self._create_config(resolved_repo)
|
||||
config.triage_enabled = True
|
||||
orchestrator = GitHubOrchestrator(
|
||||
project_dir=self.project_dir, config=config
|
||||
)
|
||||
results = await orchestrator.triage_issues(issue_numbers=issue_numbers)
|
||||
return {
|
||||
"success": True,
|
||||
"data": [r.to_dict() for r in results],
|
||||
"count": len(results),
|
||||
}
|
||||
except ImportError:
|
||||
return {"error": "GitHub runner module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
@@ -1,82 +0,0 @@
|
||||
"""
|
||||
Ideation Service
|
||||
=================
|
||||
|
||||
Wraps the backend IdeationOrchestrator for MCP tool access.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Valid ideation types
|
||||
VALID_IDEATION_TYPES = [
|
||||
"low_hanging_fruit",
|
||||
"ui_ux_improvements",
|
||||
"high_value_features",
|
||||
]
|
||||
|
||||
|
||||
class IdeationService:
|
||||
"""Service layer for AI-powered ideation generation."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
self.ideation_dir = project_dir / ".auto-claude" / "ideation"
|
||||
|
||||
async def generate(
|
||||
self,
|
||||
types: list[str] | None = None,
|
||||
refresh: bool = False,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
) -> dict:
|
||||
"""Generate ideas for project improvements."""
|
||||
try:
|
||||
from ideation import IdeationOrchestrator
|
||||
|
||||
# Validate types
|
||||
enabled_types = types or VALID_IDEATION_TYPES
|
||||
invalid = [t for t in enabled_types if t not in VALID_IDEATION_TYPES]
|
||||
if invalid:
|
||||
return {
|
||||
"error": f"Invalid ideation types: {invalid}. "
|
||||
f"Valid types: {VALID_IDEATION_TYPES}"
|
||||
}
|
||||
|
||||
orchestrator = IdeationOrchestrator(
|
||||
project_dir=self.project_dir,
|
||||
enabled_types=enabled_types,
|
||||
model=model,
|
||||
thinking_level=thinking_level,
|
||||
refresh=refresh,
|
||||
)
|
||||
success = await orchestrator.run()
|
||||
|
||||
if success:
|
||||
return self.get_ideation()
|
||||
return {"error": "Ideation generation failed. Check logs for details."}
|
||||
except ImportError:
|
||||
return {"error": "Ideation module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
def get_ideation(self) -> dict:
|
||||
"""Get previously generated ideation results from disk."""
|
||||
ideation_file = self.ideation_dir / "ideation.json"
|
||||
if not ideation_file.exists():
|
||||
return {
|
||||
"success": True,
|
||||
"data": None,
|
||||
"message": "No ideation data yet. Use ideation_generate first.",
|
||||
}
|
||||
try:
|
||||
with open(ideation_file, encoding="utf-8") as f:
|
||||
ideation = json.load(f)
|
||||
return {"success": True, "data": ideation}
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
return {"error": f"Failed to load ideation data: {e}"}
|
||||
@@ -1,116 +0,0 @@
|
||||
"""
|
||||
Insights Service
|
||||
=================
|
||||
|
||||
Wraps the backend InsightsRunner for MCP tool access.
|
||||
Captures stdout output since run_with_sdk prints to stdout.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class InsightsService:
|
||||
"""Service layer for codebase insights / AI chat."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
|
||||
async def ask(
|
||||
self,
|
||||
question: str,
|
||||
history: list | None = None,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
) -> dict:
|
||||
"""Ask an AI question about the codebase.
|
||||
|
||||
IMPORTANT: run_with_sdk prints to stdout, so we capture it.
|
||||
"""
|
||||
try:
|
||||
from runners.insights_runner import run_with_sdk
|
||||
|
||||
history = history or []
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
await run_with_sdk(
|
||||
project_dir=str(self.project_dir),
|
||||
message=question,
|
||||
history=history,
|
||||
model=model,
|
||||
thinking_level=thinking_level,
|
||||
)
|
||||
|
||||
output = captured.getvalue()
|
||||
|
||||
# Parse out any task suggestions from the output
|
||||
task_suggestions = []
|
||||
response_lines = []
|
||||
for line in output.split("\n"):
|
||||
if line.startswith("__TASK_SUGGESTION__:"):
|
||||
try:
|
||||
suggestion_json = line.split("__TASK_SUGGESTION__:", 1)[1]
|
||||
task_suggestions.append(json.loads(suggestion_json))
|
||||
except (json.JSONDecodeError, IndexError):
|
||||
pass
|
||||
elif line.startswith("__TOOL_START__:") or line.startswith(
|
||||
"__TOOL_END__:"
|
||||
):
|
||||
# Skip tool markers - they're for the Electron UI
|
||||
pass
|
||||
else:
|
||||
response_lines.append(line)
|
||||
|
||||
response_text = "\n".join(response_lines).strip()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"response": response_text,
|
||||
"task_suggestions": task_suggestions,
|
||||
}
|
||||
except ImportError:
|
||||
return {"error": "Insights runner module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
def suggest_tasks(self) -> dict:
|
||||
"""Get AI-suggested tasks based on project analysis.
|
||||
|
||||
Reads the most recent ideation/insights data if available.
|
||||
"""
|
||||
try:
|
||||
ideation_file = (
|
||||
self.project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
)
|
||||
if ideation_file.exists():
|
||||
with open(ideation_file, encoding="utf-8") as f:
|
||||
ideation = json.load(f)
|
||||
ideas = ideation.get("ideas", [])
|
||||
# Convert top ideas to task suggestions
|
||||
suggestions = []
|
||||
for idea in ideas[:10]:
|
||||
suggestions.append(
|
||||
{
|
||||
"title": idea.get("title", ""),
|
||||
"description": idea.get("description", ""),
|
||||
"category": idea.get("type", "feature"),
|
||||
"impact": idea.get("impact", "medium"),
|
||||
"effort": idea.get("effort", "medium"),
|
||||
}
|
||||
)
|
||||
return {"success": True, "suggestions": suggestions}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"suggestions": [],
|
||||
"message": "No ideation data available. Run ideation_generate first.",
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
@@ -1,161 +0,0 @@
|
||||
"""
|
||||
Memory Service
|
||||
===============
|
||||
|
||||
Wraps the Graphiti memory system for MCP tool access.
|
||||
Gracefully handles the case where Graphiti is not enabled/configured.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Module-level singleton
|
||||
_instance: MemoryService | None = None
|
||||
|
||||
|
||||
def get_memory_service(project_dir: Path) -> MemoryService:
|
||||
"""Return a lazily-created singleton MemoryService.
|
||||
|
||||
Preserves the cached Graphiti connection across tool calls.
|
||||
If project_dir changes, a new instance is created.
|
||||
"""
|
||||
global _instance
|
||||
if _instance is None or _instance.project_dir != project_dir:
|
||||
_instance = MemoryService(project_dir)
|
||||
return _instance
|
||||
|
||||
|
||||
def _is_graphiti_enabled() -> bool:
|
||||
"""Check if Graphiti memory is enabled via environment variable."""
|
||||
return os.environ.get("GRAPHITI_ENABLED", "").lower() in ("true", "1")
|
||||
|
||||
|
||||
class MemoryService:
|
||||
"""Service layer for Graphiti-based semantic memory."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
self._memory = None
|
||||
|
||||
def _get_disabled_message(self) -> dict:
|
||||
"""Return a helpful error when Graphiti is not enabled."""
|
||||
return {
|
||||
"error": "Graphiti memory is not enabled. "
|
||||
"Set GRAPHITI_ENABLED=true in your .env file and configure "
|
||||
"the required provider settings (LLM and embedder). "
|
||||
"See the project documentation for setup instructions."
|
||||
}
|
||||
|
||||
async def _get_memory(self):
|
||||
"""Lazily initialize and return a GraphitiMemory instance."""
|
||||
if self._memory is not None:
|
||||
return self._memory
|
||||
|
||||
if not _is_graphiti_enabled():
|
||||
return None
|
||||
|
||||
try:
|
||||
from integrations.graphiti.memory import (
|
||||
GraphitiMemory,
|
||||
GroupIdMode,
|
||||
)
|
||||
|
||||
# Use a dummy spec_dir since we're in project-wide mode
|
||||
spec_dir = self.project_dir / ".auto-claude" / "mcp_memory"
|
||||
spec_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
memory = GraphitiMemory(
|
||||
spec_dir=spec_dir,
|
||||
project_dir=self.project_dir,
|
||||
group_id_mode=GroupIdMode.PROJECT,
|
||||
)
|
||||
|
||||
if not await memory.initialize():
|
||||
logger.warning("Failed to initialize Graphiti memory")
|
||||
return None
|
||||
|
||||
self._memory = memory
|
||||
return memory
|
||||
except ImportError:
|
||||
logger.warning("Graphiti modules not available")
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.warning("Failed to create Graphiti memory: %s", e)
|
||||
return None
|
||||
|
||||
async def search(self, query: str, limit: int = 10) -> dict:
|
||||
"""Search the project's semantic memory."""
|
||||
if not _is_graphiti_enabled():
|
||||
return self._get_disabled_message()
|
||||
|
||||
try:
|
||||
memory = await self._get_memory()
|
||||
if memory is None:
|
||||
return {"error": "Could not initialize Graphiti memory"}
|
||||
|
||||
results = await memory.get_relevant_context(
|
||||
query=query,
|
||||
num_results=limit,
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"results": results,
|
||||
"count": len(results),
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": f"Memory search failed: {e}"}
|
||||
|
||||
async def add_episode(self, content: str, source: str = "mcp") -> dict:
|
||||
"""Add a new episode/fact to the project's memory."""
|
||||
if not _is_graphiti_enabled():
|
||||
return self._get_disabled_message()
|
||||
|
||||
try:
|
||||
memory = await self._get_memory()
|
||||
if memory is None:
|
||||
return {"error": "Could not initialize Graphiti memory"}
|
||||
|
||||
success = await memory.save_session_insights(
|
||||
session_num=0,
|
||||
insights={
|
||||
"content": content,
|
||||
"source": source,
|
||||
"type": "mcp_episode",
|
||||
},
|
||||
)
|
||||
|
||||
if success:
|
||||
return {"success": True, "message": "Episode added to memory"}
|
||||
return {"error": "Failed to save episode to memory"}
|
||||
except Exception as e:
|
||||
return {"error": f"Failed to add episode: {e}"}
|
||||
|
||||
async def get_recent(self, limit: int = 10) -> dict:
|
||||
"""Get recent memory entries."""
|
||||
if not _is_graphiti_enabled():
|
||||
return self._get_disabled_message()
|
||||
|
||||
try:
|
||||
memory = await self._get_memory()
|
||||
if memory is None:
|
||||
return {"error": "Could not initialize Graphiti memory"}
|
||||
|
||||
# Use a broad search to get recent entries
|
||||
results = await memory.get_relevant_context(
|
||||
query="recent project activity and insights",
|
||||
num_results=limit,
|
||||
)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"results": results,
|
||||
"count": len(results),
|
||||
}
|
||||
except Exception as e:
|
||||
return {"error": f"Failed to get recent memory: {e}"}
|
||||
@@ -1,236 +0,0 @@
|
||||
"""
|
||||
QA Service
|
||||
===========
|
||||
|
||||
Service layer wrapping the backend QA reviewer for MCP tool consumption.
|
||||
Handles client creation, stdout isolation, and error management.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class QAService:
|
||||
"""Wraps QA review and approval operations for MCP server use."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
|
||||
async def start_review(
|
||||
self,
|
||||
spec_id: str,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
max_iterations: int = 3,
|
||||
) -> dict:
|
||||
"""Run a QA review session for a completed build.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
model: Model shorthand
|
||||
thinking_level: Thinking level
|
||||
max_iterations: Maximum QA loop iterations
|
||||
|
||||
Returns:
|
||||
Dict with review outcome (approved/rejected/error)
|
||||
"""
|
||||
spec_dir = self._resolve_spec_dir(spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found"}
|
||||
|
||||
# Verify the build is complete before starting QA
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
if not plan_file.exists():
|
||||
return {
|
||||
"error": "No implementation plan found. Build the spec first.",
|
||||
}
|
||||
|
||||
try:
|
||||
from core.client import create_client
|
||||
from qa.reviewer import run_qa_agent_session
|
||||
except ImportError as e:
|
||||
logger.error("Failed to import QA modules: %s", e)
|
||||
return {"error": f"Backend module not available: {e}"}
|
||||
|
||||
try:
|
||||
# Determine QA session number from existing state
|
||||
qa_session = self._get_next_qa_session(spec_dir)
|
||||
|
||||
# Create a Claude SDK client for the QA agent
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
client = create_client(
|
||||
project_dir=self.project_dir,
|
||||
spec_dir=spec_dir,
|
||||
model=model,
|
||||
phase="qa_reviewer",
|
||||
)
|
||||
|
||||
status, response_text, error_info = await run_qa_agent_session(
|
||||
client=client,
|
||||
project_dir=self.project_dir,
|
||||
spec_dir=spec_dir,
|
||||
qa_session=qa_session,
|
||||
max_iterations=max_iterations,
|
||||
)
|
||||
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"status": status,
|
||||
"qa_session": qa_session,
|
||||
"response_preview": response_text[:1000] if response_text else "",
|
||||
"error_info": error_info if error_info else None,
|
||||
"output": captured.getvalue()[-1000:] if captured.getvalue() else "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception("QA review failed for %s", spec_id)
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"status": "error",
|
||||
"error": str(e),
|
||||
}
|
||||
|
||||
def get_report(self, spec_id: str) -> dict:
|
||||
"""Get the QA report for a spec.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
|
||||
Returns:
|
||||
Dict with QA report content and status
|
||||
"""
|
||||
spec_dir = self._resolve_spec_dir(spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found"}
|
||||
|
||||
result: dict = {"spec_id": spec_id}
|
||||
|
||||
# Read qa_report.md
|
||||
qa_report = spec_dir / "qa_report.md"
|
||||
if qa_report.exists():
|
||||
try:
|
||||
result["report"] = qa_report.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
result["report_error"] = str(e)
|
||||
|
||||
# Read QA fix request if present
|
||||
fix_request = spec_dir / "QA_FIX_REQUEST.md"
|
||||
if fix_request.exists():
|
||||
try:
|
||||
result["fix_request"] = fix_request.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
result["fix_request_error"] = str(e)
|
||||
|
||||
# Read qa_signoff from implementation plan
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
if plan_file.exists():
|
||||
try:
|
||||
with open(plan_file, encoding="utf-8") as f:
|
||||
plan = json.load(f)
|
||||
qa_signoff = plan.get("qa_signoff")
|
||||
if qa_signoff:
|
||||
result["qa_signoff"] = qa_signoff
|
||||
except (json.JSONDecodeError, OSError):
|
||||
pass
|
||||
|
||||
if "report" not in result and "qa_signoff" not in result:
|
||||
result["message"] = "No QA report found. Run QA review first."
|
||||
|
||||
return result
|
||||
|
||||
def approve(self, spec_id: str) -> dict:
|
||||
"""Manually approve a spec's QA status.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
|
||||
Returns:
|
||||
Dict with approval result
|
||||
"""
|
||||
spec_dir = self._resolve_spec_dir(spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found"}
|
||||
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
if not plan_file.exists():
|
||||
return {"error": "No implementation plan found"}
|
||||
|
||||
try:
|
||||
with open(plan_file, encoding="utf-8") as f:
|
||||
plan = json.load(f)
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
return {"error": f"Could not read implementation plan: {e}"}
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
plan["qa_signoff"] = {
|
||||
"status": "approved",
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"qa_session": plan.get("qa_signoff", {}).get("qa_session", 0),
|
||||
"verified_by": "manual_approval",
|
||||
"note": "Manually approved via MCP tool",
|
||||
}
|
||||
|
||||
try:
|
||||
with open(plan_file, "w", encoding="utf-8") as f:
|
||||
json.dump(plan, f, indent=2)
|
||||
except OSError as e:
|
||||
return {"error": f"Could not write implementation plan: {e}"}
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"spec_id": spec_id,
|
||||
"message": "Spec manually approved",
|
||||
}
|
||||
|
||||
def _get_next_qa_session(self, spec_dir: Path) -> int:
|
||||
"""Get the next QA session number.
|
||||
|
||||
Args:
|
||||
spec_dir: Path to the spec directory
|
||||
|
||||
Returns:
|
||||
Next session number (1-based)
|
||||
"""
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
if not plan_file.exists():
|
||||
return 1
|
||||
try:
|
||||
with open(plan_file, encoding="utf-8") as f:
|
||||
plan = json.load(f)
|
||||
qa_signoff = plan.get("qa_signoff", {})
|
||||
current = qa_signoff.get("qa_session", 0)
|
||||
return current + 1
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return 1
|
||||
|
||||
def _resolve_spec_dir(self, spec_id: str) -> Path | None:
|
||||
"""Resolve spec_id to its directory path.
|
||||
|
||||
Args:
|
||||
spec_id: Full or prefix spec identifier
|
||||
|
||||
Returns:
|
||||
Path to spec directory or None
|
||||
"""
|
||||
specs_dir = self.project_dir / ".auto-claude" / "specs"
|
||||
|
||||
# Direct match
|
||||
exact = specs_dir / spec_id
|
||||
if exact.is_dir():
|
||||
return exact
|
||||
|
||||
# Prefix match
|
||||
if specs_dir.is_dir():
|
||||
for item in specs_dir.iterdir():
|
||||
if item.is_dir() and item.name.startswith(spec_id):
|
||||
return item
|
||||
|
||||
return None
|
||||
@@ -1,65 +0,0 @@
|
||||
"""
|
||||
Roadmap Service
|
||||
================
|
||||
|
||||
Wraps the backend RoadmapOrchestrator for MCP tool access.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class RoadmapService:
|
||||
"""Service layer for roadmap generation features."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
self.roadmap_dir = project_dir / ".auto-claude" / "roadmap"
|
||||
|
||||
async def generate(
|
||||
self,
|
||||
refresh: bool = False,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
) -> dict:
|
||||
"""Generate a strategic roadmap for the project."""
|
||||
try:
|
||||
from runners.roadmap.orchestrator import RoadmapOrchestrator
|
||||
|
||||
orchestrator = RoadmapOrchestrator(
|
||||
project_dir=self.project_dir,
|
||||
model=model,
|
||||
thinking_level=thinking_level,
|
||||
refresh=refresh,
|
||||
)
|
||||
success = await orchestrator.run()
|
||||
|
||||
if success:
|
||||
# Load and return the generated roadmap
|
||||
return self.get_roadmap()
|
||||
return {"error": "Roadmap generation failed. Check logs for details."}
|
||||
except ImportError:
|
||||
return {"error": "Roadmap runner module not available"}
|
||||
except Exception as e:
|
||||
return {"error": str(e)}
|
||||
|
||||
def get_roadmap(self) -> dict:
|
||||
"""Get the current roadmap data from disk."""
|
||||
roadmap_file = self.roadmap_dir / "roadmap.json"
|
||||
if not roadmap_file.exists():
|
||||
return {
|
||||
"success": True,
|
||||
"data": None,
|
||||
"message": "No roadmap generated yet. Use roadmap_generate first.",
|
||||
}
|
||||
try:
|
||||
with open(roadmap_file, encoding="utf-8") as f:
|
||||
roadmap = json.load(f)
|
||||
return {"success": True, "data": roadmap}
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
return {"error": f"Failed to load roadmap: {e}"}
|
||||
@@ -1,242 +0,0 @@
|
||||
"""
|
||||
Spec Service
|
||||
=============
|
||||
|
||||
Service layer wrapping the backend SpecOrchestrator for MCP tool consumption.
|
||||
Handles stdout isolation and error management.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class SpecService:
|
||||
"""Wraps backend spec creation pipeline for MCP server use."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
|
||||
async def create_spec(
|
||||
self,
|
||||
task_description: str,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
complexity_override: str | None = None,
|
||||
) -> dict:
|
||||
"""Create a spec using the SpecOrchestrator.
|
||||
|
||||
Redirects stdout to prevent protocol corruption when running
|
||||
under stdio transport.
|
||||
|
||||
Args:
|
||||
task_description: Description of the task to spec out
|
||||
model: Model shorthand (sonnet, opus, etc.)
|
||||
thinking_level: Thinking level (low, medium, high)
|
||||
complexity_override: Force a specific complexity level
|
||||
|
||||
Returns:
|
||||
Dict with success status, spec_dir, spec_id, and any captured output
|
||||
"""
|
||||
try:
|
||||
from spec.pipeline.orchestrator import SpecOrchestrator
|
||||
except ImportError as e:
|
||||
logger.error("Failed to import SpecOrchestrator: %s", e)
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Backend module not available: {e}",
|
||||
}
|
||||
|
||||
try:
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
orchestrator = SpecOrchestrator(
|
||||
project_dir=self.project_dir,
|
||||
task_description=task_description,
|
||||
model=model,
|
||||
thinking_level=thinking_level,
|
||||
complexity_override=complexity_override,
|
||||
use_ai_assessment=True,
|
||||
)
|
||||
# Run non-interactively with auto-approve for MCP
|
||||
success = await orchestrator.run(interactive=False, auto_approve=True)
|
||||
|
||||
spec_dir = orchestrator.spec_dir
|
||||
return {
|
||||
"success": success,
|
||||
"spec_dir": str(spec_dir),
|
||||
"spec_id": spec_dir.name,
|
||||
"output": captured.getvalue()[-2000:] if captured.getvalue() else "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception("Spec creation failed")
|
||||
return {
|
||||
"success": False,
|
||||
"error": str(e),
|
||||
}
|
||||
|
||||
def get_spec_status(self, spec_id: str) -> dict:
|
||||
"""Get the status of a spec by checking which phase files exist.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name (e.g. '001-my-feature')
|
||||
|
||||
Returns:
|
||||
Dict describing which phases are complete and current state
|
||||
"""
|
||||
specs_dir = self.project_dir / ".auto-claude" / "specs"
|
||||
spec_dir = self._resolve_spec_dir(specs_dir, spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found"}
|
||||
|
||||
phases = {
|
||||
"discovery": (spec_dir / "discovery.md").exists(),
|
||||
"requirements": (spec_dir / "requirements.json").exists(),
|
||||
"complexity_assessment": (spec_dir / "complexity_assessment.json").exists(),
|
||||
"spec": (spec_dir / "spec.md").exists(),
|
||||
"implementation_plan": (spec_dir / "implementation_plan.json").exists(),
|
||||
}
|
||||
|
||||
# Determine overall status
|
||||
if phases["implementation_plan"]:
|
||||
plan = self._load_json(spec_dir / "implementation_plan.json")
|
||||
qa_signoff = plan.get("qa_signoff") if plan else None
|
||||
if qa_signoff and qa_signoff.get("status") == "approved":
|
||||
status = "qa_approved"
|
||||
elif qa_signoff and qa_signoff.get("status") == "rejected":
|
||||
status = "qa_rejected"
|
||||
elif (spec_dir / "qa_report.md").exists():
|
||||
status = "qa_reviewed"
|
||||
else:
|
||||
status = "ready_to_build"
|
||||
elif phases["spec"]:
|
||||
status = "spec_complete"
|
||||
elif phases["requirements"]:
|
||||
status = "requirements_gathered"
|
||||
elif phases["discovery"]:
|
||||
status = "discovery_complete"
|
||||
else:
|
||||
status = "pending"
|
||||
|
||||
return {
|
||||
"spec_id": spec_dir.name,
|
||||
"spec_dir": str(spec_dir),
|
||||
"status": status,
|
||||
"phases": phases,
|
||||
}
|
||||
|
||||
def get_spec_content(self, spec_id: str) -> dict:
|
||||
"""Get the full content of a spec.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
|
||||
Returns:
|
||||
Dict with spec.md content, requirements, implementation plan, etc.
|
||||
"""
|
||||
specs_dir = self.project_dir / ".auto-claude" / "specs"
|
||||
spec_dir = self._resolve_spec_dir(specs_dir, spec_id)
|
||||
if spec_dir is None:
|
||||
return {"error": f"Spec '{spec_id}' not found"}
|
||||
|
||||
content: dict = {
|
||||
"spec_id": spec_dir.name,
|
||||
"spec_dir": str(spec_dir),
|
||||
}
|
||||
|
||||
# Read spec.md
|
||||
spec_md = spec_dir / "spec.md"
|
||||
if spec_md.exists():
|
||||
try:
|
||||
content["spec_md"] = spec_md.read_text(encoding="utf-8")
|
||||
except OSError as e:
|
||||
content["spec_md_error"] = str(e)
|
||||
|
||||
# Read requirements.json
|
||||
req = self._load_json(spec_dir / "requirements.json")
|
||||
if req is not None:
|
||||
content["requirements"] = req
|
||||
|
||||
# Read implementation_plan.json
|
||||
plan = self._load_json(spec_dir / "implementation_plan.json")
|
||||
if plan is not None:
|
||||
content["implementation_plan"] = plan
|
||||
|
||||
# Read complexity_assessment.json
|
||||
assessment = self._load_json(spec_dir / "complexity_assessment.json")
|
||||
if assessment is not None:
|
||||
content["complexity_assessment"] = assessment
|
||||
|
||||
# Read QA report if present
|
||||
qa_report = spec_dir / "qa_report.md"
|
||||
if qa_report.exists():
|
||||
try:
|
||||
content["qa_report"] = qa_report.read_text(encoding="utf-8")
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return content
|
||||
|
||||
def list_specs(self) -> list[dict]:
|
||||
"""List all specs in the project.
|
||||
|
||||
Returns:
|
||||
List of spec summary dicts
|
||||
"""
|
||||
specs_dir = self.project_dir / ".auto-claude" / "specs"
|
||||
if not specs_dir.is_dir():
|
||||
return []
|
||||
|
||||
specs = []
|
||||
for item in sorted(specs_dir.iterdir()):
|
||||
if item.is_dir() and not item.name.startswith("."):
|
||||
status_info = self.get_spec_status(item.name)
|
||||
specs.append(status_info)
|
||||
return specs
|
||||
|
||||
def _resolve_spec_dir(self, specs_dir: Path, spec_id: str) -> Path | None:
|
||||
"""Resolve a spec_id to its directory, supporting prefix matching.
|
||||
|
||||
Args:
|
||||
specs_dir: Parent specs directory
|
||||
spec_id: Full or prefix spec identifier
|
||||
|
||||
Returns:
|
||||
Path to spec directory or None
|
||||
"""
|
||||
# Direct match
|
||||
exact = specs_dir / spec_id
|
||||
if exact.is_dir():
|
||||
return exact
|
||||
|
||||
# Prefix match (e.g. '001' matches '001-my-feature')
|
||||
if specs_dir.is_dir():
|
||||
for item in specs_dir.iterdir():
|
||||
if item.is_dir() and item.name.startswith(spec_id):
|
||||
return item
|
||||
|
||||
return None
|
||||
|
||||
def _load_json(self, path: Path) -> dict | None:
|
||||
"""Safely load a JSON file.
|
||||
|
||||
Args:
|
||||
path: Path to the JSON file
|
||||
|
||||
Returns:
|
||||
Parsed dict or None
|
||||
"""
|
||||
if not path.exists():
|
||||
return None
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
logger.warning("Failed to load %s: %s", path, e)
|
||||
return None
|
||||
@@ -1,429 +0,0 @@
|
||||
"""
|
||||
Task Service
|
||||
=============
|
||||
|
||||
Loads, creates, updates, and deletes tasks by scanning spec directories.
|
||||
Ported from the TypeScript ProjectStore.loadTasksFromSpecsDir() logic.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import shutil
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Valid task statuses used by the backend pipeline
|
||||
VALID_STATUSES = frozenset(
|
||||
{
|
||||
"pending",
|
||||
"spec_creating",
|
||||
"planning",
|
||||
"in_progress",
|
||||
"qa_review",
|
||||
"qa_fixing",
|
||||
"human_review",
|
||||
"done",
|
||||
"failed",
|
||||
"cancelled",
|
||||
}
|
||||
)
|
||||
|
||||
# Status priority for deduplication (higher = more "complete")
|
||||
_STATUS_PRIORITY: dict[str, int] = {
|
||||
"done": 100,
|
||||
"human_review": 80,
|
||||
"qa_fixing": 70,
|
||||
"qa_review": 65,
|
||||
"in_progress": 50,
|
||||
"planning": 40,
|
||||
"spec_creating": 35,
|
||||
"pending": 20,
|
||||
"cancelled": 15,
|
||||
"failed": 10,
|
||||
}
|
||||
|
||||
|
||||
def _slugify(text: str) -> str:
|
||||
"""Convert a title into a filesystem-safe slug."""
|
||||
slug = text.lower().strip()
|
||||
slug = re.sub(r"[^\w\s-]", "", slug)
|
||||
slug = re.sub(r"[\s_]+", "-", slug)
|
||||
slug = re.sub(r"-+", "-", slug)
|
||||
return slug.strip("-")[:80]
|
||||
|
||||
|
||||
def _safe_read_json(path: Path) -> dict | None:
|
||||
"""Read a JSON file, returning None on any error."""
|
||||
try:
|
||||
return json.loads(path.read_text(encoding="utf-8"))
|
||||
except (json.JSONDecodeError, OSError, ValueError):
|
||||
return None
|
||||
|
||||
|
||||
def _extract_spec_heading(spec_path: Path) -> str | None:
|
||||
"""Extract the first markdown heading from a spec.md file."""
|
||||
try:
|
||||
content = spec_path.read_text(encoding="utf-8")
|
||||
match = re.search(
|
||||
r"^#\s+(?:Quick Spec:|Specification:)?\s*(.+)$", content, re.MULTILINE
|
||||
)
|
||||
if match:
|
||||
return match.group(1).strip()
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
def _extract_spec_overview(spec_path: Path) -> str | None:
|
||||
"""Extract the Overview section from a spec.md file."""
|
||||
try:
|
||||
content = spec_path.read_text(encoding="utf-8")
|
||||
match = re.search(r"## Overview\s*\n+([\s\S]*?)(?=\n#{1,6}\s|$)", content)
|
||||
if match:
|
||||
return match.group(1).strip()
|
||||
except OSError:
|
||||
pass
|
||||
return None
|
||||
|
||||
|
||||
class TaskService:
|
||||
"""Manages task lifecycle by reading/writing spec directories."""
|
||||
|
||||
def __init__(self, project_dir: Path) -> None:
|
||||
self.project_dir = project_dir
|
||||
self.specs_dir = project_dir / ".auto-claude" / "specs"
|
||||
self.worktrees_dir = project_dir / ".auto-claude" / "worktrees" / "tasks"
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Read operations
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def list_tasks(self) -> list[dict]:
|
||||
"""Scan spec directories and build a deduplicated task list.
|
||||
|
||||
Scans both the main project specs dir and worktree specs dirs.
|
||||
Main project tasks take priority over worktree duplicates.
|
||||
"""
|
||||
all_tasks: list[dict] = []
|
||||
main_spec_ids: set[str] = set()
|
||||
|
||||
# 1. Scan main project specs
|
||||
if self.specs_dir.is_dir():
|
||||
main_tasks = self._load_tasks_from_specs_dir(self.specs_dir, "main")
|
||||
all_tasks.extend(main_tasks)
|
||||
main_spec_ids = {t["spec_id"] for t in main_tasks}
|
||||
|
||||
# 2. Scan worktree specs (only include if spec exists in main)
|
||||
if self.worktrees_dir.is_dir():
|
||||
try:
|
||||
for worktree_dir in sorted(self.worktrees_dir.iterdir()):
|
||||
if not worktree_dir.is_dir():
|
||||
continue
|
||||
wt_specs = worktree_dir / ".auto-claude" / "specs"
|
||||
if wt_specs.is_dir():
|
||||
wt_tasks = self._load_tasks_from_specs_dir(wt_specs, "worktree")
|
||||
valid = [t for t in wt_tasks if t["spec_id"] in main_spec_ids]
|
||||
all_tasks.extend(valid)
|
||||
except OSError as exc:
|
||||
logger.warning("Error scanning worktrees: %s", exc)
|
||||
|
||||
# 3. Deduplicate — prefer main over worktree
|
||||
task_map: dict[str, dict] = {}
|
||||
for task in all_tasks:
|
||||
existing = task_map.get(task["spec_id"])
|
||||
if existing is None:
|
||||
task_map[task["spec_id"]] = task
|
||||
else:
|
||||
existing_is_main = existing.get("location") == "main"
|
||||
new_is_main = task.get("location") == "main"
|
||||
|
||||
if existing_is_main and not new_is_main:
|
||||
# Keep existing main
|
||||
continue
|
||||
elif not existing_is_main and new_is_main:
|
||||
# Replace worktree with main
|
||||
task_map[task["spec_id"]] = task
|
||||
else:
|
||||
# Same location — use status priority
|
||||
ep = _STATUS_PRIORITY.get(existing.get("status", ""), 0)
|
||||
np = _STATUS_PRIORITY.get(task.get("status", ""), 0)
|
||||
if np > ep:
|
||||
task_map[task["spec_id"]] = task
|
||||
|
||||
return list(task_map.values())
|
||||
|
||||
def get_task(self, spec_id: str) -> dict | None:
|
||||
"""Get full details for a single task by spec_id."""
|
||||
spec_dir = self.specs_dir / spec_id
|
||||
if not spec_dir.is_dir():
|
||||
# Try worktrees
|
||||
spec_dir = self._find_spec_dir_in_worktrees(spec_id)
|
||||
if spec_dir is None:
|
||||
return None
|
||||
return self._load_single_task(spec_dir, "main")
|
||||
|
||||
def create_task(self, title: str, description: str) -> dict:
|
||||
"""Create a new spec directory with initial files.
|
||||
|
||||
Returns the created task dict.
|
||||
"""
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
next_num = self._next_spec_number()
|
||||
slug = _slugify(title)
|
||||
dir_name = f"{next_num:03d}-{slug}" if slug else f"{next_num:03d}"
|
||||
spec_dir = self.specs_dir / dir_name
|
||||
|
||||
spec_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# Write requirements.json
|
||||
requirements = {"task_description": description}
|
||||
(spec_dir / "requirements.json").write_text(
|
||||
json.dumps(requirements, indent=2), encoding="utf-8"
|
||||
)
|
||||
|
||||
# Write implementation_plan.json
|
||||
plan = {
|
||||
"feature": title,
|
||||
"title": title,
|
||||
"description": description,
|
||||
"status": "pending",
|
||||
"phases": [],
|
||||
"created_at": now,
|
||||
"updated_at": now,
|
||||
}
|
||||
(spec_dir / "implementation_plan.json").write_text(
|
||||
json.dumps(plan, indent=2), encoding="utf-8"
|
||||
)
|
||||
|
||||
# Write task_metadata.json
|
||||
metadata = {
|
||||
"created_at": now,
|
||||
"source": "mcp",
|
||||
}
|
||||
(spec_dir / "task_metadata.json").write_text(
|
||||
json.dumps(metadata, indent=2), encoding="utf-8"
|
||||
)
|
||||
|
||||
return self._load_single_task(spec_dir, "main") or {
|
||||
"spec_id": dir_name,
|
||||
"title": title,
|
||||
"description": description,
|
||||
"status": "pending",
|
||||
}
|
||||
|
||||
def update_task(
|
||||
self,
|
||||
spec_id: str,
|
||||
*,
|
||||
title: str | None = None,
|
||||
description: str | None = None,
|
||||
status: str | None = None,
|
||||
) -> dict | None:
|
||||
"""Update task metadata/plan fields."""
|
||||
spec_dir = self.specs_dir / spec_id
|
||||
if not spec_dir.is_dir():
|
||||
return None
|
||||
|
||||
plan_path = spec_dir / "implementation_plan.json"
|
||||
plan = _safe_read_json(plan_path) or {}
|
||||
changed = False
|
||||
|
||||
if title is not None:
|
||||
plan["feature"] = title
|
||||
plan["title"] = title
|
||||
changed = True
|
||||
|
||||
if description is not None:
|
||||
plan["description"] = description
|
||||
# Also update requirements
|
||||
req_path = spec_dir / "requirements.json"
|
||||
reqs = _safe_read_json(req_path) or {}
|
||||
reqs["task_description"] = description
|
||||
req_path.write_text(json.dumps(reqs, indent=2), encoding="utf-8")
|
||||
changed = True
|
||||
|
||||
if status is not None:
|
||||
if status not in VALID_STATUSES:
|
||||
return None
|
||||
plan["status"] = status
|
||||
changed = True
|
||||
|
||||
if changed:
|
||||
plan["updated_at"] = datetime.now(timezone.utc).isoformat()
|
||||
plan_path.write_text(json.dumps(plan, indent=2), encoding="utf-8")
|
||||
|
||||
return self._load_single_task(spec_dir, "main")
|
||||
|
||||
def delete_task(self, spec_id: str) -> bool:
|
||||
"""Delete a spec directory. Returns True if deleted."""
|
||||
spec_dir = self.specs_dir / spec_id
|
||||
if not spec_dir.is_dir():
|
||||
return False
|
||||
|
||||
# Safety: ensure it's actually within specs_dir (prevent traversal)
|
||||
try:
|
||||
spec_dir.resolve().relative_to(self.specs_dir.resolve())
|
||||
except ValueError:
|
||||
logger.error("Path traversal detected for spec_id: %s", spec_id)
|
||||
return False
|
||||
|
||||
shutil.rmtree(spec_dir)
|
||||
return True
|
||||
|
||||
def update_status(self, spec_id: str, status: str) -> dict | None:
|
||||
"""Update just the status field in implementation_plan.json."""
|
||||
if status not in VALID_STATUSES:
|
||||
return None
|
||||
return self.update_task(spec_id, status=status)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Internal helpers
|
||||
# ------------------------------------------------------------------
|
||||
|
||||
def _next_spec_number(self) -> int:
|
||||
"""Find the highest existing spec number and return next."""
|
||||
max_num = 0
|
||||
if self.specs_dir.is_dir():
|
||||
for entry in self.specs_dir.iterdir():
|
||||
if entry.is_dir():
|
||||
match = re.match(r"^(\d{3})-", entry.name)
|
||||
if match:
|
||||
max_num = max(max_num, int(match.group(1)))
|
||||
return max_num + 1
|
||||
|
||||
def _find_spec_dir_in_worktrees(self, spec_id: str) -> Path | None:
|
||||
"""Search worktree directories for a spec."""
|
||||
if not self.worktrees_dir.is_dir():
|
||||
return None
|
||||
for wt_dir in self.worktrees_dir.iterdir():
|
||||
if not wt_dir.is_dir():
|
||||
continue
|
||||
candidate = wt_dir / ".auto-claude" / "specs" / spec_id
|
||||
if candidate.is_dir():
|
||||
return candidate
|
||||
return None
|
||||
|
||||
def _load_tasks_from_specs_dir(self, specs_dir: Path, location: str) -> list[dict]:
|
||||
"""Load all tasks from a specs directory."""
|
||||
tasks: list[dict] = []
|
||||
|
||||
try:
|
||||
entries = sorted(specs_dir.iterdir())
|
||||
except OSError as exc:
|
||||
logger.warning("Error reading specs directory %s: %s", specs_dir, exc)
|
||||
return []
|
||||
|
||||
for entry in entries:
|
||||
if not entry.is_dir() or entry.name == ".gitkeep":
|
||||
continue
|
||||
try:
|
||||
task = self._load_single_task(entry, location)
|
||||
if task:
|
||||
tasks.append(task)
|
||||
except Exception as exc:
|
||||
logger.warning("Error loading spec %s: %s", entry.name, exc)
|
||||
|
||||
return tasks
|
||||
|
||||
def _load_single_task(self, spec_dir: Path, location: str) -> dict | None:
|
||||
"""Load a single task from its spec directory."""
|
||||
dir_name = spec_dir.name
|
||||
|
||||
# Read implementation plan
|
||||
plan = _safe_read_json(spec_dir / "implementation_plan.json")
|
||||
|
||||
# Read requirements
|
||||
requirements = _safe_read_json(spec_dir / "requirements.json")
|
||||
|
||||
# Read metadata
|
||||
metadata = _safe_read_json(spec_dir / "task_metadata.json")
|
||||
|
||||
# Determine title (priority: plan.feature > plan.title > dir name)
|
||||
title = (plan or {}).get("feature") or (plan or {}).get("title") or dir_name
|
||||
|
||||
# If title looks like a spec ID (e.g. "054-some-slug"), try spec.md heading
|
||||
if re.match(r"^\d{3}-", title):
|
||||
spec_heading = _extract_spec_heading(spec_dir / "spec.md")
|
||||
if spec_heading:
|
||||
title = spec_heading
|
||||
|
||||
# Determine description (priority: plan.description > requirements.task_description > spec.md overview)
|
||||
description = ""
|
||||
if plan and plan.get("description"):
|
||||
description = plan["description"]
|
||||
if not description and requirements and requirements.get("task_description"):
|
||||
description = requirements["task_description"]
|
||||
if not description:
|
||||
overview = _extract_spec_overview(spec_dir / "spec.md")
|
||||
if overview:
|
||||
description = overview
|
||||
|
||||
# Determine status
|
||||
status = "pending"
|
||||
if plan and plan.get("status"):
|
||||
raw_status = plan["status"]
|
||||
# Map frontend-style statuses to valid backend statuses
|
||||
status_map: dict[str, str] = {
|
||||
"pending": "pending",
|
||||
"backlog": "pending",
|
||||
"queue": "pending",
|
||||
"queued": "pending",
|
||||
"spec_creating": "spec_creating",
|
||||
"planning": "planning",
|
||||
"coding": "in_progress",
|
||||
"in_progress": "in_progress",
|
||||
"review": "qa_review",
|
||||
"ai_review": "qa_review",
|
||||
"qa_review": "qa_review",
|
||||
"qa_fixing": "qa_fixing",
|
||||
"human_review": "human_review",
|
||||
"completed": "done",
|
||||
"done": "done",
|
||||
"pr_created": "done",
|
||||
"error": "failed",
|
||||
"failed": "failed",
|
||||
"cancelled": "cancelled",
|
||||
}
|
||||
status = status_map.get(raw_status, "pending")
|
||||
|
||||
# Extract subtasks from plan phases
|
||||
subtasks: list[dict] = []
|
||||
if plan and plan.get("phases"):
|
||||
for phase in plan["phases"]:
|
||||
items = phase.get("subtasks") or phase.get("chunks") or []
|
||||
for st in items:
|
||||
subtasks.append(
|
||||
{
|
||||
"id": st.get("id", ""),
|
||||
"title": st.get("description", ""),
|
||||
"status": st.get("status", "pending"),
|
||||
}
|
||||
)
|
||||
|
||||
# Build result
|
||||
created_at = (plan or {}).get("created_at", "")
|
||||
updated_at = (plan or {}).get("updated_at", "")
|
||||
|
||||
return {
|
||||
"spec_id": dir_name,
|
||||
"title": title,
|
||||
"description": description,
|
||||
"status": status,
|
||||
"subtasks": subtasks,
|
||||
"metadata": metadata,
|
||||
"location": location,
|
||||
"specs_path": str(spec_dir),
|
||||
"has_spec": (spec_dir / "spec.md").exists(),
|
||||
"has_plan": (spec_dir / "implementation_plan.json").exists(),
|
||||
"has_qa_report": (spec_dir / "qa_report.md").exists(),
|
||||
"created_at": created_at,
|
||||
"updated_at": updated_at,
|
||||
}
|
||||
@@ -1,245 +0,0 @@
|
||||
"""
|
||||
Workspace Service
|
||||
==================
|
||||
|
||||
Service layer wrapping the backend WorktreeManager for MCP tool consumption.
|
||||
Handles git worktree operations: list, diff, merge, discard, and PR creation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import contextlib
|
||||
import io
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class WorkspaceService:
|
||||
"""Wraps WorktreeManager operations for MCP server use."""
|
||||
|
||||
def __init__(self, project_dir: Path):
|
||||
self.project_dir = project_dir
|
||||
|
||||
def _get_manager(self):
|
||||
"""Lazily create a WorktreeManager instance.
|
||||
|
||||
Returns:
|
||||
WorktreeManager instance
|
||||
|
||||
Raises:
|
||||
ImportError: If backend module is not available
|
||||
"""
|
||||
from core.worktree import WorktreeManager
|
||||
|
||||
return WorktreeManager(self.project_dir)
|
||||
|
||||
def list_worktrees(self) -> dict:
|
||||
"""List all active git worktrees for the project.
|
||||
|
||||
Returns:
|
||||
Dict with list of worktree info dicts
|
||||
"""
|
||||
try:
|
||||
manager = self._get_manager()
|
||||
except ImportError as e:
|
||||
return {"error": f"Backend module not available: {e}"}
|
||||
|
||||
try:
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
worktrees = manager.list_all_worktrees()
|
||||
|
||||
result = []
|
||||
for wt in worktrees:
|
||||
entry = {
|
||||
"spec_name": wt.spec_name,
|
||||
"branch": wt.branch,
|
||||
"path": str(wt.path),
|
||||
"base_branch": wt.base_branch,
|
||||
"is_active": wt.is_active,
|
||||
"commit_count": wt.commit_count,
|
||||
"files_changed": wt.files_changed,
|
||||
"additions": wt.additions,
|
||||
"deletions": wt.deletions,
|
||||
}
|
||||
if wt.days_since_last_commit is not None:
|
||||
entry["days_since_last_commit"] = wt.days_since_last_commit
|
||||
if wt.last_commit_date is not None:
|
||||
entry["last_commit_date"] = wt.last_commit_date.isoformat()
|
||||
result.append(entry)
|
||||
|
||||
return {"worktrees": result, "count": len(result)}
|
||||
except Exception as e:
|
||||
logger.exception("Failed to list worktrees")
|
||||
return {"error": str(e)}
|
||||
|
||||
def get_diff(self, spec_id: str) -> dict:
|
||||
"""Get the git diff for a spec's worktree.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
|
||||
Returns:
|
||||
Dict with diff content and change summary
|
||||
"""
|
||||
try:
|
||||
manager = self._get_manager()
|
||||
except ImportError as e:
|
||||
return {"error": f"Backend module not available: {e}"}
|
||||
|
||||
try:
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
info = manager.get_worktree_info(spec_id)
|
||||
|
||||
if info is None:
|
||||
return {"error": f"No worktree found for spec '{spec_id}'"}
|
||||
|
||||
# Get changed files
|
||||
files = manager.get_changed_files(spec_id)
|
||||
summary = manager.get_change_summary(spec_id)
|
||||
|
||||
# Get actual diff content
|
||||
from core.git_executable import run_git
|
||||
|
||||
diff_result = run_git(
|
||||
["diff", f"{info.base_branch}...HEAD"],
|
||||
cwd=info.path,
|
||||
)
|
||||
diff_content = ""
|
||||
if diff_result.returncode == 0:
|
||||
diff_content = diff_result.stdout
|
||||
# Truncate very large diffs
|
||||
if len(diff_content) > 50000:
|
||||
diff_content = (
|
||||
diff_content[:50000]
|
||||
+ "\n\n... (diff truncated, total length: "
|
||||
+ str(len(diff_result.stdout))
|
||||
+ " chars)"
|
||||
)
|
||||
|
||||
return {
|
||||
"spec_id": spec_id,
|
||||
"branch": info.branch,
|
||||
"base_branch": info.base_branch,
|
||||
"changed_files": [
|
||||
{"status": status, "path": path} for status, path in files
|
||||
],
|
||||
"summary": summary,
|
||||
"diff": diff_content,
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception("Failed to get diff for %s", spec_id)
|
||||
return {"error": str(e)}
|
||||
|
||||
async def merge(self, spec_id: str, strategy: str = "auto") -> dict:
|
||||
"""Merge a spec's worktree changes back to the main branch.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
strategy: Merge strategy - 'auto' (git merge), 'no-commit' (stage only)
|
||||
|
||||
Returns:
|
||||
Dict with merge result
|
||||
"""
|
||||
try:
|
||||
manager = self._get_manager()
|
||||
except ImportError as e:
|
||||
return {"error": f"Backend module not available: {e}"}
|
||||
|
||||
try:
|
||||
no_commit = strategy == "no-commit"
|
||||
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
success = manager.merge_worktree(
|
||||
spec_id,
|
||||
delete_after=False,
|
||||
no_commit=no_commit,
|
||||
)
|
||||
|
||||
return {
|
||||
"success": success,
|
||||
"spec_id": spec_id,
|
||||
"strategy": strategy,
|
||||
"output": captured.getvalue()[-2000:] if captured.getvalue() else "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception("Failed to merge worktree for %s", spec_id)
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
def discard(self, spec_id: str) -> dict:
|
||||
"""Discard a spec's worktree and optionally its branch.
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
|
||||
Returns:
|
||||
Dict with discard result
|
||||
"""
|
||||
try:
|
||||
manager = self._get_manager()
|
||||
except ImportError as e:
|
||||
return {"error": f"Backend module not available: {e}"}
|
||||
|
||||
try:
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
manager.remove_worktree(spec_id, delete_branch=True)
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"spec_id": spec_id,
|
||||
"message": f"Worktree and branch for '{spec_id}' removed",
|
||||
"output": captured.getvalue() if captured.getvalue() else "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception("Failed to discard worktree for %s", spec_id)
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
async def create_pr(
|
||||
self,
|
||||
spec_id: str,
|
||||
title: str | None = None,
|
||||
body: str | None = None,
|
||||
) -> dict:
|
||||
"""Push branch and create a pull request from a spec's worktree.
|
||||
|
||||
Automatically detects the git provider (GitHub/GitLab).
|
||||
|
||||
Args:
|
||||
spec_id: The spec folder name
|
||||
title: PR title (defaults to spec name)
|
||||
body: PR body (defaults to spec summary)
|
||||
|
||||
Returns:
|
||||
Dict with PR URL and status
|
||||
"""
|
||||
try:
|
||||
manager = self._get_manager()
|
||||
except ImportError as e:
|
||||
return {"error": f"Backend module not available: {e}"}
|
||||
|
||||
try:
|
||||
captured = io.StringIO()
|
||||
with contextlib.redirect_stdout(captured):
|
||||
result = manager.push_and_create_pr(
|
||||
spec_name=spec_id,
|
||||
title=title,
|
||||
)
|
||||
|
||||
return {
|
||||
"success": result.get("success", False),
|
||||
"spec_id": spec_id,
|
||||
"pr_url": result.get("pr_url"),
|
||||
"branch": result.get("branch"),
|
||||
"provider": result.get("provider"),
|
||||
"already_exists": result.get("already_exists", False),
|
||||
"error": result.get("error"),
|
||||
"output": captured.getvalue()[-1000:] if captured.getvalue() else "",
|
||||
}
|
||||
except Exception as e:
|
||||
logger.exception("Failed to create PR for %s", spec_id)
|
||||
return {"success": False, "error": str(e)}
|
||||
@@ -1 +0,0 @@
|
||||
"""MCP tool modules - each module registers tools with the FastMCP server."""
|
||||
@@ -1,159 +0,0 @@
|
||||
"""
|
||||
Execution Tools
|
||||
================
|
||||
|
||||
MCP tools for starting, stopping, and monitoring builds.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def build_start(
|
||||
spec_id: str,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
) -> dict:
|
||||
"""Start building/implementing a spec. This is a long-running operation.
|
||||
|
||||
Spawns the autonomous coding pipeline which creates a worktree, runs
|
||||
the planner, then executes each subtask with parallel agents.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix (e.g. '001' or '001-my-feature')
|
||||
model: Model to use - 'sonnet' (fast), 'opus' (thorough)
|
||||
thinking_level: Reasoning depth - 'low', 'medium', 'high'
|
||||
|
||||
Returns:
|
||||
An operation_id to poll with operation_get_status() for progress
|
||||
"""
|
||||
op = tracker.create("build", f"Building spec: {spec_id}")
|
||||
|
||||
async def _run() -> None:
|
||||
try:
|
||||
from mcp_server.services.execution_service import get_execution_service
|
||||
|
||||
service = get_execution_service(get_project_dir())
|
||||
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=5,
|
||||
message="Spawning build process...",
|
||||
)
|
||||
|
||||
proc = await service.start_build(
|
||||
spec_id=spec_id,
|
||||
model=model,
|
||||
thinking_level=thinking_level,
|
||||
)
|
||||
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="Build process started, waiting for completion...",
|
||||
)
|
||||
|
||||
# Wait for the process to complete
|
||||
await proc.wait()
|
||||
|
||||
if proc.returncode == 0:
|
||||
progress_info = service.get_progress(spec_id)
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Build completed successfully",
|
||||
result=progress_info,
|
||||
)
|
||||
else:
|
||||
logs = service.get_logs(spec_id, tail=20)
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=f"Build exited with code {proc.returncode}",
|
||||
result={
|
||||
"exit_code": proc.returncode,
|
||||
"tail_logs": logs.get("lines", []),
|
||||
},
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("build_start operation failed")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "Build started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def build_stop(spec_id: str) -> dict:
|
||||
"""Stop a running build.
|
||||
|
||||
Terminates the build subprocess. The worktree and any partial changes
|
||||
are preserved so the build can be resumed later.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
|
||||
Returns:
|
||||
Whether the build was successfully stopped
|
||||
"""
|
||||
from mcp_server.services.execution_service import get_execution_service
|
||||
|
||||
service = get_execution_service(get_project_dir())
|
||||
return service.stop_build(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def build_get_progress(spec_id: str) -> dict:
|
||||
"""Get progress of a running or completed build.
|
||||
|
||||
Shows subtask completion status, QA state, and whether the build
|
||||
is still running.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
|
||||
Returns:
|
||||
Build progress including subtask completion and QA status
|
||||
"""
|
||||
from mcp_server.services.execution_service import get_execution_service
|
||||
|
||||
service = get_execution_service(get_project_dir())
|
||||
return service.get_progress(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def build_get_logs(spec_id: str, tail: int = 50) -> dict:
|
||||
"""Get recent build logs for a spec.
|
||||
|
||||
Returns the most recent log lines from the build process output.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
tail: Number of recent lines to return (default 50)
|
||||
|
||||
Returns:
|
||||
Recent log lines from the build
|
||||
"""
|
||||
from mcp_server.services.execution_service import get_execution_service
|
||||
|
||||
service = get_execution_service(get_project_dir())
|
||||
return service.get_logs(spec_id, tail=tail)
|
||||
@@ -1,208 +0,0 @@
|
||||
"""
|
||||
GitHub Tools
|
||||
=============
|
||||
|
||||
MCP tools for GitHub automation: PR review, issue triage, auto-fix.
|
||||
Long-running operations return an operation_id for polling.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
from mcp_server.services.github_service import GitHubService
|
||||
|
||||
|
||||
def _get_service() -> GitHubService:
|
||||
return GitHubService(get_project_dir())
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def github_review_pr(
|
||||
pr_number: int, repo: str | None = None, model: str = "sonnet"
|
||||
) -> dict:
|
||||
"""Review a pull request with AI. Long-running operation - returns operation_id.
|
||||
|
||||
Performs a multi-pass AI code review including security, quality,
|
||||
structural analysis, and AI comment triage.
|
||||
|
||||
Args:
|
||||
pr_number: The PR number to review
|
||||
repo: Repository in owner/repo format (auto-detected from git remote if omitted)
|
||||
model: Model to use (haiku, sonnet, opus)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create("github_review_pr", f"Starting review of PR #{pr_number}...")
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message=f"Reviewing PR #{pr_number}...",
|
||||
)
|
||||
result = await service.review_pr(pr_number, repo, model)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Review complete",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": f"PR #{pr_number} review started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def github_list_issues(
|
||||
state: str = "open", limit: int = 30, repo: str | None = None
|
||||
) -> dict:
|
||||
"""List GitHub issues for the project.
|
||||
|
||||
Args:
|
||||
state: Issue state filter: open, closed, or all
|
||||
limit: Maximum number of issues to return
|
||||
repo: Repository in owner/repo format (auto-detected if omitted)
|
||||
|
||||
Returns:
|
||||
List of issues with number, title, state, labels, author
|
||||
"""
|
||||
service = _get_service()
|
||||
return await service.list_issues(state, limit, repo)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def github_auto_fix(issue_number: int, repo: str | None = None) -> dict:
|
||||
"""Automatically fix a GitHub issue by creating a spec and building it. Long-running.
|
||||
|
||||
Creates a specification from the issue, builds it through the autonomous
|
||||
pipeline (planner -> coder -> QA), and optionally creates a PR.
|
||||
|
||||
Args:
|
||||
issue_number: The issue number to auto-fix
|
||||
repo: Repository in owner/repo format (auto-detected if omitted)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create(
|
||||
"github_auto_fix", f"Starting auto-fix for issue #{issue_number}..."
|
||||
)
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message=f"Auto-fixing issue #{issue_number}...",
|
||||
)
|
||||
result = await service.auto_fix_issue(issue_number, repo)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Auto-fix complete",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": f"Auto-fix for issue #{issue_number} started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def github_get_review(pr_number: int) -> dict:
|
||||
"""Get the most recent review result for a PR.
|
||||
|
||||
Returns the saved review data including findings, verdict, and summary.
|
||||
|
||||
Args:
|
||||
pr_number: The PR number to get the review for
|
||||
|
||||
Returns:
|
||||
Review result with findings, verdict, blockers, and summary
|
||||
"""
|
||||
service = _get_service()
|
||||
return service.get_review(pr_number)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def github_triage_issues(
|
||||
issue_numbers: list[int], repo: str | None = None
|
||||
) -> dict:
|
||||
"""Triage and classify GitHub issues. Long-running.
|
||||
|
||||
Analyzes issues for duplicates, spam, feature creep, and assigns
|
||||
categories, priority, and suggested labels.
|
||||
|
||||
Args:
|
||||
issue_numbers: List of issue numbers to triage
|
||||
repo: Repository in owner/repo format (auto-detected if omitted)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create(
|
||||
"github_triage_issues",
|
||||
f"Starting triage of {len(issue_numbers)} issues...",
|
||||
)
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message=f"Triaging {len(issue_numbers)} issues...",
|
||||
)
|
||||
result = await service.triage_issues(issue_numbers, repo)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message=f"Triaged {result.get('count', 0)} issues",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": f"Triage of {len(issue_numbers)} issues started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
"""
|
||||
Ideation Tools
|
||||
===============
|
||||
|
||||
MCP tools for AI-powered project ideation and improvement discovery.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
from mcp_server.services.ideation_service import IdeationService
|
||||
|
||||
|
||||
def _get_service() -> IdeationService:
|
||||
return IdeationService(get_project_dir())
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def ideation_generate(
|
||||
types: list[str] | None = None,
|
||||
refresh: bool = False,
|
||||
model: str = "sonnet",
|
||||
) -> dict:
|
||||
"""Generate ideas for project improvements. Long-running.
|
||||
|
||||
Analyzes the codebase and generates actionable improvement ideas
|
||||
across multiple categories.
|
||||
|
||||
Args:
|
||||
types: Ideation types to generate. Options: low_hanging_fruit,
|
||||
ui_ux_improvements, high_value_features. Defaults to all.
|
||||
refresh: Force regeneration of existing ideation data
|
||||
model: Model to use (haiku, sonnet, opus)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create("ideation_generate", "Starting ideation generation...")
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="Analyzing project for improvement ideas...",
|
||||
)
|
||||
result = await service.generate(types=types, refresh=refresh, model=model)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Ideation complete",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "Ideation generation started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def ideation_get() -> dict:
|
||||
"""Get previously generated ideation results.
|
||||
|
||||
Returns all generated ideas with their categories, priorities,
|
||||
effort estimates, and implementation suggestions.
|
||||
|
||||
Returns:
|
||||
Ideation data with ideas grouped by type and priority
|
||||
"""
|
||||
service = _get_service()
|
||||
return service.get_ideation()
|
||||
@@ -1,84 +0,0 @@
|
||||
"""
|
||||
Insights Tools
|
||||
===============
|
||||
|
||||
MCP tools for AI-powered codebase insights and Q&A.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
from mcp_server.services.insights_service import InsightsService
|
||||
|
||||
|
||||
def _get_service() -> InsightsService:
|
||||
return InsightsService(get_project_dir())
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def insights_ask(
|
||||
question: str, history: list | None = None, model: str = "sonnet"
|
||||
) -> dict:
|
||||
"""Ask an AI question about the codebase. Long-running operation.
|
||||
|
||||
The AI agent has access to the codebase and can read files, search,
|
||||
and explore to answer questions about architecture, patterns, bugs, etc.
|
||||
|
||||
Args:
|
||||
question: The question to ask about the codebase
|
||||
history: Optional conversation history as list of {role, content} dicts
|
||||
model: Model to use (haiku, sonnet, opus)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create("insights_ask", "Processing question...")
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="AI is exploring the codebase...",
|
||||
)
|
||||
result = await service.ask(question, history, model)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Question answered",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "Insights query started. Poll operation_get_status() for the answer.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def insights_suggest_tasks() -> dict:
|
||||
"""Get AI-suggested tasks based on recent insights conversations.
|
||||
|
||||
Returns task suggestions derived from ideation data or previous
|
||||
insights conversations.
|
||||
|
||||
Returns:
|
||||
List of task suggestions with title, description, category, impact
|
||||
"""
|
||||
service = _get_service()
|
||||
return service.suggest_tasks()
|
||||
@@ -1,71 +0,0 @@
|
||||
"""
|
||||
Memory Tools
|
||||
=============
|
||||
|
||||
MCP tools for Graphiti-based semantic memory (knowledge graph).
|
||||
Requires GRAPHITI_ENABLED=true in the environment.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.server import mcp
|
||||
from mcp_server.services.memory_service import get_memory_service
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def memory_search(query: str, limit: int = 10) -> dict:
|
||||
"""Search the project's semantic memory (Graphiti knowledge graph).
|
||||
|
||||
Finds relevant stored knowledge including codebase discoveries,
|
||||
session insights, patterns, gotchas, and task outcomes.
|
||||
|
||||
Requires GRAPHITI_ENABLED=true in environment.
|
||||
|
||||
Args:
|
||||
query: Search query describing what you're looking for
|
||||
limit: Maximum number of results to return (default 10)
|
||||
|
||||
Returns:
|
||||
List of relevant memory entries with content and relevance scores
|
||||
"""
|
||||
service = get_memory_service(get_project_dir())
|
||||
return await service.search(query, limit)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def memory_add_episode(content: str, source: str = "mcp") -> dict:
|
||||
"""Add a new episode/fact to the project's memory.
|
||||
|
||||
Stores information in the knowledge graph for future retrieval.
|
||||
Use this to record insights, patterns, or important findings.
|
||||
|
||||
Requires GRAPHITI_ENABLED=true in environment.
|
||||
|
||||
Args:
|
||||
content: The information to store (insight, pattern, discovery, etc.)
|
||||
source: Source identifier for the episode (default: mcp)
|
||||
|
||||
Returns:
|
||||
Confirmation of successful storage
|
||||
"""
|
||||
service = get_memory_service(get_project_dir())
|
||||
return await service.add_episode(content, source)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def memory_get_recent(limit: int = 10) -> dict:
|
||||
"""Get recent memory entries.
|
||||
|
||||
Retrieves the most recent entries from the project's knowledge graph.
|
||||
|
||||
Requires GRAPHITI_ENABLED=true in environment.
|
||||
|
||||
Args:
|
||||
limit: Maximum number of entries to return (default 10)
|
||||
|
||||
Returns:
|
||||
List of recent memory entries
|
||||
"""
|
||||
service = get_memory_service(get_project_dir())
|
||||
return await service.get_recent(limit)
|
||||
@@ -1,52 +0,0 @@
|
||||
"""
|
||||
Operations Management Tools
|
||||
============================
|
||||
|
||||
Tools for polling long-running operation status and cancelling operations.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from mcp_server.operations import tracker
|
||||
from mcp_server.server import mcp
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def operation_get_status(operation_id: str) -> dict:
|
||||
"""Get the status of a long-running operation.
|
||||
|
||||
Use this to poll for progress on operations started by tools like
|
||||
spec_create, build_start, qa_start_review, etc.
|
||||
|
||||
Args:
|
||||
operation_id: The operation ID returned by the tool that started the operation
|
||||
|
||||
Returns:
|
||||
Operation status including progress (0-100), message, and result when complete
|
||||
"""
|
||||
op = tracker.get(operation_id)
|
||||
if op is None:
|
||||
return {"error": f"Operation {operation_id} not found"}
|
||||
return op.to_dict()
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def operation_cancel(operation_id: str) -> dict:
|
||||
"""Cancel a running operation.
|
||||
|
||||
Args:
|
||||
operation_id: The operation ID to cancel
|
||||
|
||||
Returns:
|
||||
Whether the cancellation was successful
|
||||
"""
|
||||
success = tracker.cancel(operation_id)
|
||||
if not success:
|
||||
op = tracker.get(operation_id)
|
||||
if op is None:
|
||||
return {"success": False, "error": "Operation not found"}
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Cannot cancel operation in {op.status.value} state",
|
||||
}
|
||||
return {"success": True, "message": "Operation cancelled"}
|
||||
@@ -1,148 +0,0 @@
|
||||
"""
|
||||
Project Management Tools
|
||||
=========================
|
||||
|
||||
MCP tools for managing the active project: switching projects,
|
||||
getting status, listing specs, and reading the project index.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from mcp_server import config
|
||||
from mcp_server.server import mcp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def project_set_active(project_dir: str) -> dict:
|
||||
"""Switch the MCP server to a different project directory.
|
||||
|
||||
Re-initializes the server to point at a new project.
|
||||
All subsequent tool calls will operate on this project.
|
||||
|
||||
Args:
|
||||
project_dir: Absolute path to the project directory
|
||||
"""
|
||||
try:
|
||||
config.initialize(project_dir)
|
||||
project_path = config.get_project_dir()
|
||||
initialized = config.is_initialized()
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
"project_dir": str(project_path),
|
||||
"initialized": initialized,
|
||||
"message": f"Active project set to {project_path}",
|
||||
}
|
||||
except (ValueError, RuntimeError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def project_get_status() -> dict:
|
||||
"""Get the current project status.
|
||||
|
||||
Returns the active project directory, initialization state,
|
||||
specs count, and project index summary.
|
||||
"""
|
||||
try:
|
||||
project_dir = config.get_project_dir()
|
||||
except RuntimeError:
|
||||
return {
|
||||
"initialized": False,
|
||||
"error": "No project set. Use project_set_active() first.",
|
||||
}
|
||||
|
||||
initialized = config.is_initialized()
|
||||
specs_count = 0
|
||||
|
||||
if initialized:
|
||||
specs_dir = config.get_specs_dir()
|
||||
if specs_dir.is_dir():
|
||||
specs_count = sum(
|
||||
1
|
||||
for entry in specs_dir.iterdir()
|
||||
if entry.is_dir() and entry.name != ".gitkeep"
|
||||
)
|
||||
|
||||
index = config.get_project_index()
|
||||
|
||||
return {
|
||||
"project_dir": str(project_dir),
|
||||
"initialized": initialized,
|
||||
"specs_count": specs_count,
|
||||
"has_project_index": bool(index),
|
||||
"project_name": index.get("name", project_dir.name),
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def project_list_specs() -> dict:
|
||||
"""List all spec directories with their basic info.
|
||||
|
||||
Returns each spec's name, whether it has a plan/spec file,
|
||||
and status from the implementation plan.
|
||||
"""
|
||||
try:
|
||||
specs_dir = config.get_specs_dir()
|
||||
except RuntimeError:
|
||||
return {"error": "No project set. Use project_set_active() first.", "specs": []}
|
||||
|
||||
if not specs_dir.is_dir():
|
||||
return {"specs": [], "message": "No specs directory found."}
|
||||
|
||||
specs: list[dict] = []
|
||||
for entry in sorted(specs_dir.iterdir()):
|
||||
if not entry.is_dir() or entry.name == ".gitkeep":
|
||||
continue
|
||||
|
||||
has_plan = (entry / "implementation_plan.json").exists()
|
||||
has_spec = (entry / "spec.md").exists()
|
||||
|
||||
status = "pending"
|
||||
title = entry.name
|
||||
if has_plan:
|
||||
try:
|
||||
import json
|
||||
|
||||
plan = json.loads(
|
||||
(entry / "implementation_plan.json").read_text(encoding="utf-8")
|
||||
)
|
||||
status = plan.get("status", "pending")
|
||||
title = plan.get("feature") or plan.get("title") or entry.name
|
||||
except (json.JSONDecodeError, OSError):
|
||||
pass
|
||||
|
||||
specs.append(
|
||||
{
|
||||
"name": entry.name,
|
||||
"title": title,
|
||||
"has_plan": has_plan,
|
||||
"has_spec": has_spec,
|
||||
"has_qa_report": (entry / "qa_report.md").exists(),
|
||||
"status": status,
|
||||
}
|
||||
)
|
||||
|
||||
return {"specs": specs, "count": len(specs)}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def project_get_index() -> dict:
|
||||
"""Return the full project_index.json content.
|
||||
|
||||
The project index contains metadata about the project
|
||||
such as file summaries, dependency info, and analysis results.
|
||||
"""
|
||||
try:
|
||||
index = config.get_project_index()
|
||||
except RuntimeError:
|
||||
return {"error": "No project set. Use project_set_active() first."}
|
||||
|
||||
if not index:
|
||||
return {"message": "No project index found. Run indexing first.", "index": {}}
|
||||
|
||||
return {"index": index}
|
||||
@@ -1,125 +0,0 @@
|
||||
"""
|
||||
QA Tools
|
||||
=========
|
||||
|
||||
MCP tools for running QA reviews, getting reports, and manual approval.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def qa_start_review(spec_id: str) -> dict:
|
||||
"""Start QA review for a completed build. This is a long-running operation.
|
||||
|
||||
Runs the QA reviewer agent which validates the implementation against
|
||||
the spec's acceptance criteria. The agent reads code, runs tests, and
|
||||
produces a detailed QA report.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix (e.g. '001' or '001-my-feature')
|
||||
|
||||
Returns:
|
||||
An operation_id to poll with operation_get_status() for progress
|
||||
"""
|
||||
op = tracker.create("qa_review", f"QA review for: {spec_id}")
|
||||
|
||||
async def _run() -> None:
|
||||
try:
|
||||
from mcp_server.services.qa_service import QAService
|
||||
|
||||
service = QAService(get_project_dir())
|
||||
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="Starting QA review session...",
|
||||
)
|
||||
|
||||
result = await service.start_review(spec_id=spec_id)
|
||||
|
||||
status = result.get("status", "error")
|
||||
if status == "approved":
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="QA approved - all acceptance criteria validated",
|
||||
result=result,
|
||||
)
|
||||
elif status == "rejected":
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="QA rejected - issues found, see report",
|
||||
result=result,
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=result.get("error", "QA review failed"),
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("qa_start_review operation failed")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "QA review started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def qa_get_report(spec_id: str) -> dict:
|
||||
"""Get the QA report for a spec.
|
||||
|
||||
Returns the full QA report including validation results, issues found,
|
||||
and the qa_signoff status from the implementation plan.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
|
||||
Returns:
|
||||
QA report content, fix requests, and signoff status
|
||||
"""
|
||||
from mcp_server.services.qa_service import QAService
|
||||
|
||||
service = QAService(get_project_dir())
|
||||
return service.get_report(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def qa_approve(spec_id: str) -> dict:
|
||||
"""Manually approve a spec that's in QA review.
|
||||
|
||||
Use this to bypass the automated QA review and mark a spec as approved.
|
||||
This updates the implementation_plan.json qa_signoff status.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
|
||||
Returns:
|
||||
Whether the approval was successful
|
||||
"""
|
||||
from mcp_server.services.qa_service import QAService
|
||||
|
||||
service = QAService(get_project_dir())
|
||||
return service.approve(spec_id)
|
||||
@@ -1,128 +0,0 @@
|
||||
"""
|
||||
Roadmap Tools
|
||||
==============
|
||||
|
||||
MCP tools for AI-powered strategic roadmap generation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
from mcp_server.services.roadmap_service import RoadmapService
|
||||
|
||||
|
||||
def _get_service() -> RoadmapService:
|
||||
return RoadmapService(get_project_dir())
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def roadmap_generate(refresh: bool = False, model: str = "sonnet") -> dict:
|
||||
"""Generate a strategic roadmap for the project. Long-running.
|
||||
|
||||
Analyzes the project structure, existing features, and codebase to
|
||||
generate a phased roadmap with prioritized features.
|
||||
|
||||
Args:
|
||||
refresh: Force regeneration even if a roadmap already exists
|
||||
model: Model to use (haiku, sonnet, opus)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create("roadmap_generate", "Starting roadmap generation...")
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="Analyzing project for roadmap generation...",
|
||||
)
|
||||
result = await service.generate(refresh=refresh, model=model)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Roadmap generated",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "Roadmap generation started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def roadmap_get() -> dict:
|
||||
"""Get the current roadmap data.
|
||||
|
||||
Returns the previously generated roadmap including vision, phases,
|
||||
features with priorities, and implementation details.
|
||||
|
||||
Returns:
|
||||
Roadmap data with vision, phases, features, and priority breakdown
|
||||
"""
|
||||
service = _get_service()
|
||||
return service.get_roadmap()
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def roadmap_refresh(model: str = "sonnet") -> dict:
|
||||
"""Refresh/regenerate the roadmap. Long-running.
|
||||
|
||||
Forces a complete regeneration of the roadmap, analyzing current
|
||||
project state and creating updated phases and features.
|
||||
|
||||
Args:
|
||||
model: Model to use (haiku, sonnet, opus)
|
||||
|
||||
Returns:
|
||||
Operation ID to poll with operation_get_status()
|
||||
"""
|
||||
service = _get_service()
|
||||
op = tracker.create("roadmap_refresh", "Starting roadmap refresh...")
|
||||
|
||||
async def _run():
|
||||
try:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="Refreshing roadmap...",
|
||||
)
|
||||
result = await service.generate(refresh=True, model=model)
|
||||
if "error" in result:
|
||||
tracker.update(
|
||||
op.id, status=OperationStatus.FAILED, error=result["error"]
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Roadmap refreshed",
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error=str(e))
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "Roadmap refresh started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
"""
|
||||
Spec Tools
|
||||
===========
|
||||
|
||||
MCP tools for creating and inspecting specifications.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def spec_create(
|
||||
task_description: str,
|
||||
model: str = "sonnet",
|
||||
thinking_level: str = "medium",
|
||||
) -> dict:
|
||||
"""Create a specification for a task. This is a long-running operation.
|
||||
|
||||
The spec creation pipeline runs multiple AI phases (discovery, requirements,
|
||||
complexity assessment, spec writing, planning) to produce a complete
|
||||
implementation-ready specification.
|
||||
|
||||
Args:
|
||||
task_description: What you want to build (be specific and detailed)
|
||||
model: Model to use - 'sonnet' (fast), 'opus' (thorough)
|
||||
thinking_level: How much the AI reasons - 'low', 'medium', 'high'
|
||||
|
||||
Returns:
|
||||
An operation_id to poll with operation_get_status() for progress
|
||||
"""
|
||||
op = tracker.create("spec_create", f"Creating spec for: {task_description[:80]}")
|
||||
|
||||
async def _run() -> None:
|
||||
try:
|
||||
from mcp_server.services.spec_service import SpecService
|
||||
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=5,
|
||||
message="Initializing spec pipeline...",
|
||||
)
|
||||
|
||||
service = SpecService(get_project_dir())
|
||||
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=10,
|
||||
message="Running spec creation phases...",
|
||||
)
|
||||
|
||||
result = await service.create_spec(
|
||||
task_description=task_description,
|
||||
model=model,
|
||||
thinking_level=thinking_level,
|
||||
)
|
||||
|
||||
if result.get("success"):
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="Spec created successfully",
|
||||
result=result,
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
progress=100,
|
||||
message="Spec creation failed",
|
||||
error=result.get("error", "Unknown error"),
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("spec_create operation failed")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "Spec creation started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def spec_get_status(spec_id: str) -> dict:
|
||||
"""Get the current status of a spec (which phases have completed).
|
||||
|
||||
Shows whether discovery, requirements, spec writing, and planning
|
||||
phases are complete, and the overall readiness state.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix (e.g. '001' or '001-my-feature')
|
||||
|
||||
Returns:
|
||||
Status including completed phases and overall state
|
||||
"""
|
||||
from mcp_server.services.spec_service import SpecService
|
||||
|
||||
service = SpecService(get_project_dir())
|
||||
return service.get_spec_status(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def spec_get_content(spec_id: str) -> dict:
|
||||
"""Get the full content of a spec including spec.md, requirements, and plan.
|
||||
|
||||
Returns the complete specification content so you can understand what
|
||||
will be built and how.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix (e.g. '001' or '001-my-feature')
|
||||
|
||||
Returns:
|
||||
Full spec content including spec.md, requirements.json, implementation_plan.json
|
||||
"""
|
||||
from mcp_server.services.spec_service import SpecService
|
||||
|
||||
service = SpecService(get_project_dir())
|
||||
return service.get_spec_content(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def spec_list() -> dict:
|
||||
"""List all specs in the project with their status.
|
||||
|
||||
Returns:
|
||||
List of specs with their current status and phase completion
|
||||
"""
|
||||
from mcp_server.services.spec_service import SpecService
|
||||
|
||||
service = SpecService(get_project_dir())
|
||||
specs = service.list_specs()
|
||||
return {"specs": specs, "count": len(specs)}
|
||||
@@ -1,179 +0,0 @@
|
||||
"""
|
||||
Task Management Tools
|
||||
======================
|
||||
|
||||
MCP tools for CRUD operations on tasks (specs).
|
||||
Tasks are stored as spec directories under .auto-claude/specs/.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from mcp_server import config
|
||||
from mcp_server.server import mcp
|
||||
from mcp_server.services.task_service import VALID_STATUSES, TaskService
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_task_service() -> TaskService:
|
||||
"""Get a TaskService instance for the active project."""
|
||||
return TaskService(config.get_project_dir())
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def task_list() -> dict:
|
||||
"""List all tasks with id, title, status, and description preview.
|
||||
|
||||
Scans both the main project and worktree spec directories,
|
||||
deduplicating with main project taking priority.
|
||||
"""
|
||||
try:
|
||||
service = _get_task_service()
|
||||
except RuntimeError as exc:
|
||||
return {"error": str(exc), "tasks": []}
|
||||
|
||||
tasks = service.list_tasks()
|
||||
|
||||
# Return a concise view for listing
|
||||
summary = []
|
||||
for t in tasks:
|
||||
desc = t.get("description", "")
|
||||
preview = (desc[:200] + "...") if len(desc) > 200 else desc
|
||||
summary.append(
|
||||
{
|
||||
"spec_id": t["spec_id"],
|
||||
"title": t["title"],
|
||||
"status": t["status"],
|
||||
"description_preview": preview,
|
||||
"has_spec": t.get("has_spec", False),
|
||||
"has_plan": t.get("has_plan", False),
|
||||
"subtask_count": len(t.get("subtasks", [])),
|
||||
}
|
||||
)
|
||||
|
||||
return {"tasks": summary, "count": len(summary)}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def task_create(title: str, description: str) -> dict:
|
||||
"""Create a new task (spec directory) with initial files.
|
||||
|
||||
Generates the next spec number automatically and creates
|
||||
the directory with requirements.json, implementation_plan.json,
|
||||
and task_metadata.json.
|
||||
|
||||
Args:
|
||||
title: The task title (used for the directory name slug)
|
||||
description: Full description of what needs to be done
|
||||
"""
|
||||
try:
|
||||
service = _get_task_service()
|
||||
except RuntimeError as exc:
|
||||
return {"error": str(exc)}
|
||||
|
||||
if not title or not title.strip():
|
||||
return {"error": "Title is required"}
|
||||
if not description or not description.strip():
|
||||
return {"error": "Description is required"}
|
||||
|
||||
task = service.create_task(title.strip(), description.strip())
|
||||
return {"success": True, "task": task}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def task_get(spec_id: str) -> dict:
|
||||
"""Get full task details including subtasks, metadata, and file info.
|
||||
|
||||
Args:
|
||||
spec_id: The spec directory name (e.g., "001-my-feature")
|
||||
"""
|
||||
try:
|
||||
service = _get_task_service()
|
||||
except RuntimeError as exc:
|
||||
return {"error": str(exc)}
|
||||
|
||||
task = service.get_task(spec_id)
|
||||
if task is None:
|
||||
return {"error": f"Task '{spec_id}' not found"}
|
||||
|
||||
return {"task": task}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def task_update(
|
||||
spec_id: str,
|
||||
title: str | None = None,
|
||||
description: str | None = None,
|
||||
status: str | None = None,
|
||||
) -> dict:
|
||||
"""Update task metadata (title, description, and/or status).
|
||||
|
||||
Args:
|
||||
spec_id: The spec directory name (e.g., "001-my-feature")
|
||||
title: New title (optional)
|
||||
description: New description (optional)
|
||||
status: New status (optional) - must be a valid status
|
||||
"""
|
||||
try:
|
||||
service = _get_task_service()
|
||||
except RuntimeError as exc:
|
||||
return {"error": str(exc)}
|
||||
|
||||
if status is not None and status not in VALID_STATUSES:
|
||||
return {"error": f"Invalid status '{status}'. Valid: {sorted(VALID_STATUSES)}"}
|
||||
|
||||
task = service.update_task(
|
||||
spec_id, title=title, description=description, status=status
|
||||
)
|
||||
if task is None:
|
||||
return {"error": f"Task '{spec_id}' not found or invalid update"}
|
||||
|
||||
return {"success": True, "task": task}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def task_delete(spec_id: str) -> dict:
|
||||
"""Delete a task by removing its spec directory.
|
||||
|
||||
WARNING: This permanently deletes the spec directory and all its contents.
|
||||
|
||||
Args:
|
||||
spec_id: The spec directory name (e.g., "001-my-feature")
|
||||
"""
|
||||
try:
|
||||
service = _get_task_service()
|
||||
except RuntimeError as exc:
|
||||
return {"error": str(exc)}
|
||||
|
||||
deleted = service.delete_task(spec_id)
|
||||
if not deleted:
|
||||
return {"error": f"Task '{spec_id}' not found"}
|
||||
|
||||
return {"success": True, "message": f"Task '{spec_id}' deleted"}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def task_update_status(spec_id: str, status: str) -> dict:
|
||||
"""Update just the status field of a task.
|
||||
|
||||
Args:
|
||||
spec_id: The spec directory name (e.g., "001-my-feature")
|
||||
status: New status value. Valid statuses: pending, spec_creating,
|
||||
planning, in_progress, qa_review, qa_fixing, human_review,
|
||||
done, failed, cancelled
|
||||
"""
|
||||
try:
|
||||
service = _get_task_service()
|
||||
except RuntimeError as exc:
|
||||
return {"error": str(exc)}
|
||||
|
||||
if status not in VALID_STATUSES:
|
||||
return {"error": f"Invalid status '{status}'. Valid: {sorted(VALID_STATUSES)}"}
|
||||
|
||||
task = service.update_status(spec_id, status)
|
||||
if task is None:
|
||||
return {"error": f"Task '{spec_id}' not found"}
|
||||
|
||||
return {"success": True, "task": task}
|
||||
@@ -1,159 +0,0 @@
|
||||
"""
|
||||
Workspace Tools
|
||||
================
|
||||
|
||||
MCP tools for managing git worktrees: list, diff, merge, discard, and PR creation.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from mcp_server.config import get_project_dir
|
||||
from mcp_server.operations import OperationStatus, tracker
|
||||
from mcp_server.server import mcp
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def workspace_list() -> dict:
|
||||
"""List all active git worktrees for the project.
|
||||
|
||||
Each spec gets its own isolated worktree. This shows all active
|
||||
worktrees with their branch, change stats, and age.
|
||||
|
||||
Returns:
|
||||
List of worktrees with branch, stats, and age information
|
||||
"""
|
||||
from mcp_server.services.workspace_service import WorkspaceService
|
||||
|
||||
service = WorkspaceService(get_project_dir())
|
||||
return service.list_worktrees()
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def workspace_diff(spec_id: str) -> dict:
|
||||
"""Get the git diff for a spec's worktree.
|
||||
|
||||
Shows all changes made in the spec's branch compared to the base branch,
|
||||
including a file-level summary and the full diff content.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix (e.g. '001' or '001-my-feature')
|
||||
|
||||
Returns:
|
||||
Changed files summary and full diff content
|
||||
"""
|
||||
from mcp_server.services.workspace_service import WorkspaceService
|
||||
|
||||
service = WorkspaceService(get_project_dir())
|
||||
return service.get_diff(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def workspace_merge(spec_id: str, strategy: str = "auto") -> dict:
|
||||
"""Merge a spec's worktree changes back to the main branch.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
strategy: 'auto' for standard git merge, 'no-commit' to stage without committing
|
||||
|
||||
Returns:
|
||||
Whether the merge was successful
|
||||
"""
|
||||
from mcp_server.services.workspace_service import WorkspaceService
|
||||
|
||||
service = WorkspaceService(get_project_dir())
|
||||
return await service.merge(spec_id, strategy=strategy)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def workspace_discard(spec_id: str) -> dict:
|
||||
"""Discard a spec's worktree and its branch.
|
||||
|
||||
Permanently removes the worktree directory and deletes the associated
|
||||
git branch. This cannot be undone.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
|
||||
Returns:
|
||||
Whether the discard was successful
|
||||
"""
|
||||
from mcp_server.services.workspace_service import WorkspaceService
|
||||
|
||||
service = WorkspaceService(get_project_dir())
|
||||
return service.discard(spec_id)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
async def workspace_create_pr(
|
||||
spec_id: str,
|
||||
title: str | None = None,
|
||||
body: str | None = None,
|
||||
) -> dict:
|
||||
"""Create a pull request from a spec's worktree branch.
|
||||
|
||||
Pushes the branch to origin and creates a PR/MR on the detected
|
||||
git hosting provider (GitHub or GitLab). This is a long-running operation.
|
||||
|
||||
Args:
|
||||
spec_id: Spec folder name or prefix
|
||||
title: PR title (defaults to spec name)
|
||||
body: PR body (defaults to spec summary)
|
||||
|
||||
Returns:
|
||||
An operation_id to poll with operation_get_status() for progress
|
||||
"""
|
||||
op = tracker.create("create_pr", f"Creating PR for: {spec_id}")
|
||||
|
||||
async def _run() -> None:
|
||||
try:
|
||||
from mcp_server.services.workspace_service import WorkspaceService
|
||||
|
||||
service = WorkspaceService(get_project_dir())
|
||||
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=20,
|
||||
message="Pushing branch and creating PR...",
|
||||
)
|
||||
|
||||
result = await service.create_pr(
|
||||
spec_id=spec_id,
|
||||
title=title,
|
||||
body=body,
|
||||
)
|
||||
|
||||
if result.get("success"):
|
||||
pr_url = result.get("pr_url", "")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message=f"PR created: {pr_url}" if pr_url else "PR created",
|
||||
result=result,
|
||||
)
|
||||
else:
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=result.get("error", "PR creation failed"),
|
||||
result=result,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.exception("workspace_create_pr operation failed")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error=str(e),
|
||||
)
|
||||
|
||||
op._task = asyncio.create_task(_run())
|
||||
return {
|
||||
"operation_id": op.id,
|
||||
"message": "PR creation started. Poll operation_get_status() for progress.",
|
||||
}
|
||||
@@ -24,7 +24,7 @@ You MUST create `spec.md` with ALL required sections (see template below).
|
||||
## PHASE 0: LOAD ALL CONTEXT (MANDATORY)
|
||||
|
||||
```bash
|
||||
# Read all input files
|
||||
# Read all input files (some may not exist for greenfield/empty projects)
|
||||
cat project_index.json
|
||||
cat requirements.json
|
||||
cat context.json
|
||||
@@ -35,6 +35,12 @@ Extract from these files:
|
||||
- **From requirements.json**: Task description, workflow type, services, acceptance criteria
|
||||
- **From context.json**: Files to modify, files to reference, patterns
|
||||
|
||||
**IMPORTANT**: If any input file is missing, empty, or shows 0 files, this is likely a **greenfield/new project**. Adapt accordingly:
|
||||
- Skip sections that reference existing code (e.g., "Files to Modify", "Patterns to Follow")
|
||||
- Instead, focus on files to CREATE and the initial project structure
|
||||
- Define the tech stack, dependencies, and setup instructions from scratch
|
||||
- Use industry best practices as patterns rather than referencing existing code
|
||||
|
||||
---
|
||||
|
||||
## PHASE 1: ANALYZE CONTEXT
|
||||
|
||||
@@ -15,7 +15,11 @@ from pathlib import Path
|
||||
from agents.base import sanitize_error_message
|
||||
from agents.memory_manager import get_graphiti_context, save_session_memory
|
||||
from claude_agent_sdk import ClaudeSDKClient
|
||||
from core.error_utils import is_rate_limit_error, is_tool_concurrency_error
|
||||
from core.error_utils import (
|
||||
is_rate_limit_error,
|
||||
is_tool_concurrency_error,
|
||||
safe_receive_messages,
|
||||
)
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
from security.tool_input_validator import get_safe_tool_input
|
||||
from task_logger import (
|
||||
@@ -141,7 +145,7 @@ async def run_qa_fixer_session(
|
||||
|
||||
response_text = ""
|
||||
debug("qa_fixer", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="qa_fixer"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -16,7 +16,11 @@ from pathlib import Path
|
||||
from agents.base import sanitize_error_message
|
||||
from agents.memory_manager import get_graphiti_context, save_session_memory
|
||||
from claude_agent_sdk import ClaudeSDKClient
|
||||
from core.error_utils import is_rate_limit_error, is_tool_concurrency_error
|
||||
from core.error_utils import (
|
||||
is_rate_limit_error,
|
||||
is_tool_concurrency_error,
|
||||
safe_receive_messages,
|
||||
)
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
from prompts_pkg import get_qa_reviewer_prompt
|
||||
from security.tool_input_validator import get_safe_tool_input
|
||||
@@ -195,7 +199,7 @@ This is attempt {previous_error.get("consecutive_errors", 1) + 1}. If you fail t
|
||||
|
||||
response_text = ""
|
||||
debug("qa_reviewer", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="qa_reviewer"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
# Auto-Build Framework Dependencies
|
||||
# SDK 0.1.33+ required for Opus 4.6 adaptive thinking support
|
||||
# Earlier versions lacked effort parameter and thinking type configuration
|
||||
claude-agent-sdk>=0.1.33
|
||||
# SDK 0.1.39+ required for Opus 4.6 adaptive thinking support and stability fixes
|
||||
# Earlier versions lacked effort parameter, thinking type configuration,
|
||||
# and crashed on unhandled CLI message types (e.g., rate_limit_event)
|
||||
claude-agent-sdk>=0.1.39
|
||||
python-dotenv>=1.0.0
|
||||
|
||||
# TOML parsing fallback for Python < 3.11
|
||||
|
||||
@@ -886,7 +886,8 @@ Analyze this follow-up review context and provide your structured response.
|
||||
"""Attempt a short SDK call with minimal schema to recover review data.
|
||||
|
||||
This is the extraction recovery step when full structured output validation fails.
|
||||
Uses FollowupExtractionResponse (~6 flat fields) which has near-100% success rate.
|
||||
Uses FollowupExtractionResponse (small schema with ExtractedFindingSummary nesting)
|
||||
which has near-100% success rate.
|
||||
|
||||
Uses create_client() + process_sdk_stream() for proper OAuth handling,
|
||||
matching the pattern in parallel_followup_reviewer.py.
|
||||
@@ -900,7 +901,8 @@ Analyze this follow-up review context and provide your structured response.
|
||||
extraction_prompt = (
|
||||
"Extract the key review data from the following AI analysis output. "
|
||||
"Return the verdict, reasoning, resolved finding IDs, unresolved finding IDs, "
|
||||
"one-line summaries of any new findings, and counts of confirmed/dismissed findings.\n\n"
|
||||
"structured summaries of any new findings (including severity, description, file path, and line number), "
|
||||
"and counts of confirmed/dismissed findings.\n\n"
|
||||
f"--- AI ANALYSIS OUTPUT ---\n{text[:8000]}\n--- END ---"
|
||||
)
|
||||
|
||||
@@ -946,9 +948,16 @@ Analyze this follow-up review context and provide your structured response.
|
||||
|
||||
# Convert extraction to internal format with reconstructed findings
|
||||
new_findings = []
|
||||
for i, summary in enumerate(extracted.new_finding_summaries):
|
||||
for i, summary_obj in enumerate(extracted.new_finding_summaries):
|
||||
new_findings.append(
|
||||
create_finding_from_summary(summary, i, id_prefix="FR")
|
||||
create_finding_from_summary(
|
||||
summary=summary_obj.description,
|
||||
index=i,
|
||||
id_prefix="FR",
|
||||
severity_override=summary_obj.severity,
|
||||
file=summary_obj.file,
|
||||
line=summary_obj.line,
|
||||
)
|
||||
)
|
||||
|
||||
# Build finding_resolutions from extraction data for _apply_ai_resolutions
|
||||
|
||||
@@ -1129,7 +1129,8 @@ The SDK will run invoked agents in parallel automatically.
|
||||
"""Attempt a short SDK call with a minimal schema to recover review data.
|
||||
|
||||
This is the Tier 2 recovery step when full structured output validation fails.
|
||||
Uses FollowupExtractionResponse (~6 flat fields) which has near-100% success rate.
|
||||
Uses FollowupExtractionResponse (small schema with ExtractedFindingSummary nesting)
|
||||
which has near-100% success rate.
|
||||
|
||||
Returns parsed result dict on success, None on failure.
|
||||
"""
|
||||
@@ -1146,7 +1147,8 @@ The SDK will run invoked agents in parallel automatically.
|
||||
extraction_prompt = (
|
||||
"Extract the key review data from the following AI analysis output. "
|
||||
"Return the verdict, reasoning, resolved finding IDs, unresolved finding IDs, "
|
||||
"one-line summaries of any new findings, and counts of confirmed/dismissed findings.\n\n"
|
||||
"structured summaries of any new findings (including severity, description, file path, and line number), "
|
||||
"and counts of confirmed/dismissed findings.\n\n"
|
||||
f"--- AI ANALYSIS OUTPUT ---\n{text[:8000]}\n--- END ---"
|
||||
)
|
||||
|
||||
@@ -1205,10 +1207,17 @@ The SDK will run invoked agents in parallel automatically.
|
||||
findings = []
|
||||
new_finding_ids = []
|
||||
|
||||
# 1. Convert new_finding_summaries to minimal PRReviewFinding objects
|
||||
# Uses shared helper for "SEVERITY: description" parsing and ID generation
|
||||
for i, summary in enumerate(extracted.new_finding_summaries):
|
||||
finding = create_finding_from_summary(summary, i, id_prefix="FU")
|
||||
# 1. Convert new_finding_summaries to PRReviewFinding objects
|
||||
# ExtractedFindingSummary objects carry file/line from extraction
|
||||
for i, summary_obj in enumerate(extracted.new_finding_summaries):
|
||||
finding = create_finding_from_summary(
|
||||
summary=summary_obj.description,
|
||||
index=i,
|
||||
id_prefix="FU",
|
||||
severity_override=summary_obj.severity,
|
||||
file=summary_obj.file,
|
||||
line=summary_obj.line,
|
||||
)
|
||||
new_finding_ids.append(finding.id)
|
||||
findings.append(finding)
|
||||
|
||||
|
||||
@@ -1289,12 +1289,30 @@ The SDK will run invoked agents in parallel automatically.
|
||||
f"{len(filtered_findings)} filtered"
|
||||
)
|
||||
|
||||
# No confidence routing - validation is binary via finding-validator
|
||||
unique_findings = validated_findings
|
||||
logger.info(f"[PRReview] Final findings: {len(unique_findings)} validated")
|
||||
# Separate active findings (drive verdict) from dismissed (shown in UI only)
|
||||
active_findings = []
|
||||
dismissed_findings = []
|
||||
for f in validated_findings:
|
||||
if f.validation_status == "dismissed_false_positive":
|
||||
dismissed_findings.append(f)
|
||||
else:
|
||||
active_findings.append(f)
|
||||
|
||||
safe_print(
|
||||
f"[ParallelOrchestrator] Final: {len(active_findings)} active, "
|
||||
f"{len(dismissed_findings)} disputed by validator",
|
||||
flush=True,
|
||||
)
|
||||
logger.info(
|
||||
f"[ParallelOrchestrator] Review complete: {len(unique_findings)} findings"
|
||||
f"[PRReview] Final findings: {len(active_findings)} active, "
|
||||
f"{len(dismissed_findings)} disputed"
|
||||
)
|
||||
|
||||
# All findings (active + dismissed) go in the result for UI display
|
||||
all_review_findings = validated_findings
|
||||
logger.info(
|
||||
f"[ParallelOrchestrator] Review complete: {len(all_review_findings)} findings "
|
||||
f"({len(active_findings)} active, {len(dismissed_findings)} disputed)"
|
||||
)
|
||||
|
||||
# Fetch CI status for verdict consideration
|
||||
@@ -1304,9 +1322,9 @@ The SDK will run invoked agents in parallel automatically.
|
||||
f"{ci_status.get('failing', 0)} failing, {ci_status.get('pending', 0)} pending"
|
||||
)
|
||||
|
||||
# Generate verdict (includes merge conflict check, branch-behind check, and CI status)
|
||||
# Generate verdict from ACTIVE findings only (dismissed don't affect verdict)
|
||||
verdict, verdict_reasoning, blockers = self._generate_verdict(
|
||||
unique_findings,
|
||||
active_findings,
|
||||
has_merge_conflicts=context.has_merge_conflicts,
|
||||
merge_state_status=context.merge_state_status,
|
||||
ci_status=ci_status,
|
||||
@@ -1317,7 +1335,7 @@ The SDK will run invoked agents in parallel automatically.
|
||||
verdict=verdict,
|
||||
verdict_reasoning=verdict_reasoning,
|
||||
blockers=blockers,
|
||||
findings=unique_findings,
|
||||
findings=all_review_findings,
|
||||
agents_invoked=agents_invoked,
|
||||
)
|
||||
|
||||
@@ -1362,7 +1380,7 @@ The SDK will run invoked agents in parallel automatically.
|
||||
pr_number=context.pr_number,
|
||||
repo=self.config.repo,
|
||||
success=True,
|
||||
findings=unique_findings,
|
||||
findings=all_review_findings,
|
||||
summary=summary,
|
||||
overall_status=overall_status,
|
||||
verdict=verdict,
|
||||
@@ -1937,12 +1955,38 @@ For EACH finding above:
|
||||
validated_findings.append(finding)
|
||||
|
||||
elif validation.validation_status == "dismissed_false_positive":
|
||||
# Dismiss - do not include
|
||||
dismissed_count += 1
|
||||
logger.info(
|
||||
f"[PRReview] Dismissed {finding.id} as false positive: "
|
||||
f"{validation.explanation[:100]}"
|
||||
)
|
||||
# Protect cross-validated findings from dismissal —
|
||||
# if multiple specialists independently found the same issue,
|
||||
# a single validator should not override that consensus
|
||||
if finding.cross_validated:
|
||||
finding.validation_status = "confirmed_valid"
|
||||
finding.validation_evidence = validation.code_evidence
|
||||
finding.validation_explanation = (
|
||||
f"[Auto-kept: cross-validated by {len(finding.source_agents)} agents] "
|
||||
f"{validation.explanation}"
|
||||
)
|
||||
validated_findings.append(finding)
|
||||
safe_print(
|
||||
f"[FindingValidator] Kept cross-validated finding '{finding.title}' "
|
||||
f"despite dismissal (agents={finding.source_agents})",
|
||||
flush=True,
|
||||
)
|
||||
else:
|
||||
# Keep finding but mark as dismissed (user can see it in UI)
|
||||
finding.validation_status = "dismissed_false_positive"
|
||||
finding.validation_evidence = validation.code_evidence
|
||||
finding.validation_explanation = validation.explanation
|
||||
validated_findings.append(finding)
|
||||
dismissed_count += 1
|
||||
safe_print(
|
||||
f"[FindingValidator] Disputed '{finding.title}': "
|
||||
f"{validation.explanation} (file={finding.file}:{finding.line})",
|
||||
flush=True,
|
||||
)
|
||||
logger.info(
|
||||
f"[PRReview] Disputed {finding.id}: "
|
||||
f"{validation.explanation[:200]}"
|
||||
)
|
||||
|
||||
elif validation.validation_status == "needs_human_review":
|
||||
# Keep but flag
|
||||
@@ -2127,11 +2171,16 @@ For EACH finding above:
|
||||
sev = f.severity.value
|
||||
emoji = severity_emoji.get(sev, "⚪")
|
||||
|
||||
is_disputed = f.validation_status == "dismissed_false_positive"
|
||||
|
||||
# Finding header with location
|
||||
line_range = f"L{f.line}"
|
||||
if f.end_line and f.end_line != f.line:
|
||||
line_range = f"L{f.line}-L{f.end_line}"
|
||||
lines.append(f"#### {emoji} [{sev.upper()}] {f.title}")
|
||||
if is_disputed:
|
||||
lines.append(f"#### ⚪ [DISPUTED] ~~{f.title}~~")
|
||||
else:
|
||||
lines.append(f"#### {emoji} [{sev.upper()}] {f.title}")
|
||||
lines.append(f"**File:** `{f.file}` ({line_range})")
|
||||
|
||||
# Cross-validation badge
|
||||
@@ -2161,6 +2210,7 @@ For EACH finding above:
|
||||
status_label = {
|
||||
"confirmed_valid": "Confirmed",
|
||||
"needs_human_review": "Needs human review",
|
||||
"dismissed_false_positive": "Disputed by validator",
|
||||
}.get(f.validation_status, f.validation_status)
|
||||
lines.append("")
|
||||
lines.append(f"**Validation:** {status_label}")
|
||||
@@ -2182,18 +2232,27 @@ For EACH finding above:
|
||||
|
||||
lines.append("")
|
||||
|
||||
# Findings count summary
|
||||
# Findings count summary (exclude dismissed from active count)
|
||||
active_count = 0
|
||||
dismissed_count = 0
|
||||
by_severity: dict[str, int] = {}
|
||||
for f in findings:
|
||||
if f.validation_status == "dismissed_false_positive":
|
||||
dismissed_count += 1
|
||||
continue
|
||||
active_count += 1
|
||||
sev = f.severity.value
|
||||
by_severity[sev] = by_severity.get(sev, 0) + 1
|
||||
summary_parts = []
|
||||
for sev in ["critical", "high", "medium", "low"]:
|
||||
if sev in by_severity:
|
||||
summary_parts.append(f"{by_severity[sev]} {sev}")
|
||||
lines.append(
|
||||
f"**Total:** {len(findings)} finding(s) ({', '.join(summary_parts)})"
|
||||
count_text = (
|
||||
f"**Total:** {active_count} finding(s) ({', '.join(summary_parts)})"
|
||||
)
|
||||
if dismissed_count > 0:
|
||||
count_text += f" + {dismissed_count} disputed"
|
||||
lines.append(count_text)
|
||||
lines.append("")
|
||||
|
||||
lines.append("---")
|
||||
|
||||
@@ -533,10 +533,26 @@ class FindingValidationResponse(BaseModel):
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class ExtractedFindingSummary(BaseModel):
|
||||
"""Per-finding summary with file location for extraction recovery."""
|
||||
|
||||
severity: str = Field(description="Severity level: LOW, MEDIUM, HIGH, or CRITICAL")
|
||||
description: str = Field(description="One-line description of the finding")
|
||||
file: str = Field(
|
||||
default="unknown", description="File path where the issue was found"
|
||||
)
|
||||
line: int = Field(default=0, description="Line number in the file (0 if unknown)")
|
||||
|
||||
@field_validator("severity", mode="before")
|
||||
@classmethod
|
||||
def _normalize_severity(cls, v: str) -> str:
|
||||
return _normalize_severity(v)
|
||||
|
||||
|
||||
class FollowupExtractionResponse(BaseModel):
|
||||
"""Minimal extraction schema for recovering data when full structured output fails.
|
||||
|
||||
Deliberately kept small (~6 fields, no nesting) for near-100% validation success.
|
||||
Uses ExtractedFindingSummary for new findings to preserve file/line information.
|
||||
Used as an intermediate recovery step before falling back to raw text parsing.
|
||||
"""
|
||||
|
||||
@@ -552,9 +568,9 @@ class FollowupExtractionResponse(BaseModel):
|
||||
default_factory=list,
|
||||
description="IDs of previous findings that remain unresolved",
|
||||
)
|
||||
new_finding_summaries: list[str] = Field(
|
||||
new_finding_summaries: list[ExtractedFindingSummary] = Field(
|
||||
default_factory=list,
|
||||
description="One-line summary of each new finding (e.g. 'HIGH: cleanup deletes QA-rejected specs in batch_commands.py')",
|
||||
description="Structured summary of each new finding with file location",
|
||||
)
|
||||
confirmed_finding_count: int = Field(
|
||||
0, description="Number of findings confirmed as valid"
|
||||
|
||||
@@ -80,6 +80,9 @@ def create_finding_from_summary(
|
||||
summary: str,
|
||||
index: int,
|
||||
id_prefix: str = "FR",
|
||||
severity_override: str | None = None,
|
||||
file: str = "unknown",
|
||||
line: int = 0,
|
||||
) -> PRReviewFinding:
|
||||
"""Create a PRReviewFinding from an extraction summary string.
|
||||
|
||||
@@ -90,11 +93,20 @@ def create_finding_from_summary(
|
||||
summary: Raw summary string, e.g. "HIGH: Missing null check in parser.py"
|
||||
index: The index of the finding in the extraction list.
|
||||
id_prefix: ID prefix for traceability. Default "FR" (Followup Recovery).
|
||||
severity_override: If provided, use this severity instead of parsing from summary.
|
||||
file: File path where the issue was found (default "unknown").
|
||||
line: Line number in the file (default 0).
|
||||
|
||||
Returns:
|
||||
A PRReviewFinding with parsed severity, generated ID, and description.
|
||||
"""
|
||||
severity, description = parse_severity_from_summary(summary)
|
||||
|
||||
# Use severity_override if provided
|
||||
if severity_override is not None:
|
||||
severity_map = {k.rstrip(":"): v for k, v in _EXTRACTION_SEVERITY_MAP}
|
||||
severity = severity_map.get(severity_override.upper(), severity)
|
||||
|
||||
finding_id = generate_recovery_finding_id(index, description, prefix=id_prefix)
|
||||
|
||||
return PRReviewFinding(
|
||||
@@ -103,6 +115,6 @@ def create_finding_from_summary(
|
||||
category=ReviewCategory.QUALITY,
|
||||
title=description[:80],
|
||||
description=f"[Recovered via extraction] {description}",
|
||||
file="unknown",
|
||||
line=0,
|
||||
file=file,
|
||||
line=line,
|
||||
)
|
||||
|
||||
@@ -6,18 +6,54 @@ Phases for spec document creation and quality assurance.
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import TYPE_CHECKING
|
||||
from pathlib import Path
|
||||
|
||||
from .. import validator, writer
|
||||
from ..discovery import get_project_index_stats
|
||||
from .models import MAX_RETRIES, PhaseResult
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
||||
def _is_greenfield_project(spec_dir: Path) -> bool:
|
||||
"""Check if the project is empty/greenfield (0 discovered files)."""
|
||||
stats = get_project_index_stats(spec_dir)
|
||||
if not stats:
|
||||
return False # Can't determine - don't assume greenfield
|
||||
return stats.get("file_count", 0) == 0
|
||||
|
||||
|
||||
def _greenfield_context() -> str:
|
||||
"""Return additional context for greenfield/empty projects."""
|
||||
return """
|
||||
**GREENFIELD PROJECT**: This is an empty or new project with no existing code.
|
||||
There are no existing files to reference or modify. You are creating everything from scratch.
|
||||
|
||||
Adapt your approach:
|
||||
- Do NOT reference existing files, patterns, or code structures
|
||||
- Focus on what needs to be CREATED, not modified
|
||||
- Define the initial project structure, files, and directories
|
||||
- Specify the tech stack, frameworks, and dependencies to install
|
||||
- Provide setup instructions for the new project
|
||||
- For "Files to Modify" and "Files to Reference" sections, list files to CREATE instead
|
||||
- For "Patterns to Follow", describe industry best practices rather than existing code
|
||||
"""
|
||||
|
||||
|
||||
class SpecPhaseMixin:
|
||||
"""Mixin for spec writing and critique phase methods."""
|
||||
|
||||
def _check_and_log_greenfield(self) -> bool:
|
||||
"""Check if the project is greenfield and log if so.
|
||||
|
||||
Returns:
|
||||
True if the project is greenfield (no existing files).
|
||||
"""
|
||||
is_greenfield = _is_greenfield_project(self.spec_dir)
|
||||
if is_greenfield:
|
||||
self.ui.print_status(
|
||||
"Greenfield project detected - adapting spec for new project", "info"
|
||||
)
|
||||
return is_greenfield
|
||||
|
||||
async def phase_quick_spec(self) -> PhaseResult:
|
||||
"""Quick spec for simple tasks - combines context and spec in one step."""
|
||||
spec_file = self.spec_dir / "spec.md"
|
||||
@@ -29,6 +65,8 @@ class SpecPhaseMixin:
|
||||
"quick_spec", True, [str(spec_file), str(plan_file)], [], 0
|
||||
)
|
||||
|
||||
is_greenfield = self._check_and_log_greenfield()
|
||||
|
||||
errors = []
|
||||
for attempt in range(MAX_RETRIES):
|
||||
self.ui.print_status(
|
||||
@@ -42,7 +80,7 @@ class SpecPhaseMixin:
|
||||
|
||||
This is a SIMPLE task. Create a minimal spec and implementation plan directly.
|
||||
No research or extensive analysis needed.
|
||||
|
||||
{_greenfield_context() if is_greenfield else ""}
|
||||
Create:
|
||||
1. A concise spec.md with just the essential sections
|
||||
2. A simple implementation_plan.json with 1-2 subtasks
|
||||
@@ -80,6 +118,9 @@ Create:
|
||||
"spec.md exists but has issues, regenerating...", "warning"
|
||||
)
|
||||
|
||||
is_greenfield = self._check_and_log_greenfield()
|
||||
greenfield_ctx = _greenfield_context() if is_greenfield else ""
|
||||
|
||||
errors = []
|
||||
for attempt in range(MAX_RETRIES):
|
||||
self.ui.print_status(
|
||||
@@ -88,6 +129,7 @@ Create:
|
||||
|
||||
success, output = await self.run_agent_fn(
|
||||
"spec_writer.md",
|
||||
additional_context=greenfield_ctx,
|
||||
phase_name="spec_writing",
|
||||
)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ from ui.capabilities import configure_safe_encoding
|
||||
|
||||
configure_safe_encoding()
|
||||
|
||||
from core.error_utils import safe_receive_messages
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
from security.tool_input_validator import get_safe_tool_input
|
||||
from task_logger import (
|
||||
@@ -162,7 +163,7 @@ class AgentRunner:
|
||||
|
||||
response_text = ""
|
||||
debug("agent_runner", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="agent_runner"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -203,19 +203,19 @@ def generate_spec_name(task_description: str) -> str:
|
||||
return "-".join(name_parts) if name_parts else "spec"
|
||||
|
||||
|
||||
def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
def rename_spec_dir_from_requirements(spec_dir: Path) -> Path:
|
||||
"""Rename spec directory based on requirements.json task description.
|
||||
|
||||
Args:
|
||||
spec_dir: The current spec directory
|
||||
|
||||
Returns:
|
||||
Tuple of (success, new_spec_dir). If success is False, new_spec_dir is the original.
|
||||
The new spec directory path (or the original if no rename was needed/possible).
|
||||
"""
|
||||
requirements_file = spec_dir / "requirements.json"
|
||||
|
||||
if not requirements_file.exists():
|
||||
return False
|
||||
return spec_dir
|
||||
|
||||
try:
|
||||
with open(requirements_file, encoding="utf-8") as f:
|
||||
@@ -223,7 +223,7 @@ def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
|
||||
task_desc = req.get("task_description", "")
|
||||
if not task_desc:
|
||||
return False
|
||||
return spec_dir
|
||||
|
||||
# Generate new name
|
||||
new_name = generate_spec_name(task_desc)
|
||||
@@ -240,11 +240,11 @@ def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
|
||||
# Don't rename if it's already a good name (not "pending")
|
||||
if "pending" not in current_name:
|
||||
return True
|
||||
return spec_dir
|
||||
|
||||
# Don't rename if target already exists
|
||||
if new_spec_dir.exists():
|
||||
return True
|
||||
return spec_dir
|
||||
|
||||
# Rename the directory
|
||||
shutil.move(str(spec_dir), str(new_spec_dir))
|
||||
@@ -253,11 +253,11 @@ def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
update_task_logger_path(new_spec_dir)
|
||||
|
||||
print_status(f"Spec folder: {highlight(new_dir_name)}", "success")
|
||||
return True
|
||||
return new_spec_dir
|
||||
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
print_status(f"Could not rename spec folder: {e}", "warning")
|
||||
return False
|
||||
return spec_dir
|
||||
|
||||
|
||||
# Phase display configuration
|
||||
|
||||
@@ -6,6 +6,7 @@ Main orchestration logic for spec creation with dynamic complexity adaptation.
|
||||
"""
|
||||
|
||||
import json
|
||||
import types
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
@@ -18,6 +19,7 @@ from review import run_review_checkpoint
|
||||
from task_logger import (
|
||||
LogEntryType,
|
||||
LogPhase,
|
||||
TaskLogger,
|
||||
get_task_logger,
|
||||
)
|
||||
from ui import (
|
||||
@@ -238,6 +240,47 @@ class SpecOrchestrator:
|
||||
task_logger.start_phase(LogPhase.PLANNING, "Starting spec creation process")
|
||||
TaskEventEmitter.from_spec_dir(self.spec_dir).emit("PLANNING_STARTED")
|
||||
|
||||
# Track whether we've already ended the planning phase (to avoid double-end)
|
||||
self._planning_phase_ended = False
|
||||
|
||||
try:
|
||||
return await self._run_phases(interactive, auto_approve, task_logger, ui)
|
||||
except Exception as e:
|
||||
# Emit PLANNING_FAILED so the frontend XState machine transitions to error state
|
||||
# instead of leaving the task stuck in "planning" forever
|
||||
try:
|
||||
task_emitter = TaskEventEmitter.from_spec_dir(self.spec_dir)
|
||||
task_emitter.emit(
|
||||
"PLANNING_FAILED",
|
||||
{"error": str(e), "recoverable": True},
|
||||
)
|
||||
except Exception:
|
||||
pass # Don't mask the original error
|
||||
if not self._planning_phase_ended:
|
||||
self._planning_phase_ended = True
|
||||
try:
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING,
|
||||
success=False,
|
||||
message=f"Spec creation crashed: {e}",
|
||||
)
|
||||
except Exception:
|
||||
pass # Best effort - don't mask the original error when logging fails
|
||||
raise
|
||||
|
||||
async def _run_phases(
|
||||
self,
|
||||
interactive: bool,
|
||||
auto_approve: bool,
|
||||
task_logger: TaskLogger,
|
||||
ui: types.ModuleType,
|
||||
) -> bool:
|
||||
"""Internal method that runs all spec creation phases.
|
||||
|
||||
Separated from run() so that run() can wrap this in a try/except
|
||||
to emit PLANNING_FAILED on unhandled exceptions.
|
||||
"""
|
||||
|
||||
print(
|
||||
box(
|
||||
f"Spec Directory: {self.spec_dir}\n"
|
||||
@@ -291,9 +334,11 @@ class SpecOrchestrator:
|
||||
results.append(result)
|
||||
if not result.success:
|
||||
print_status("Discovery failed", "error")
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING, success=False, message="Discovery failed"
|
||||
)
|
||||
self._emit_planning_failed("Discovery phase failed")
|
||||
return False
|
||||
# Store summary for subsequent phases (compaction)
|
||||
await self._store_phase_summary("discovery")
|
||||
@@ -305,17 +350,26 @@ class SpecOrchestrator:
|
||||
results.append(result)
|
||||
if not result.success:
|
||||
print_status("Requirements gathering failed", "error")
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING,
|
||||
success=False,
|
||||
message="Requirements gathering failed",
|
||||
)
|
||||
self._emit_planning_failed("Requirements gathering failed")
|
||||
return False
|
||||
# Store summary for subsequent phases (compaction)
|
||||
await self._store_phase_summary("requirements")
|
||||
|
||||
# Rename spec folder with better name from requirements
|
||||
rename_spec_dir_from_requirements(self.spec_dir)
|
||||
# IMPORTANT: Update self.spec_dir after rename so subsequent phases use the correct path
|
||||
new_spec_dir = rename_spec_dir_from_requirements(self.spec_dir)
|
||||
if new_spec_dir != self.spec_dir:
|
||||
self.spec_dir = new_spec_dir
|
||||
self.validator = SpecValidator(self.spec_dir)
|
||||
# Update phase executor to use the renamed directory
|
||||
phase_executor.spec_dir = self.spec_dir
|
||||
phase_executor.spec_validator = self.validator
|
||||
|
||||
# Update task description from requirements
|
||||
req = requirements.load_requirements(self.spec_dir)
|
||||
@@ -335,9 +389,11 @@ class SpecOrchestrator:
|
||||
results.append(result)
|
||||
if not result.success:
|
||||
print_status("Complexity assessment failed", "error")
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING, success=False, message="Complexity assessment failed"
|
||||
)
|
||||
self._emit_planning_failed("Complexity assessment failed")
|
||||
return False
|
||||
|
||||
# Map of all available phases
|
||||
@@ -396,17 +452,22 @@ class SpecOrchestrator:
|
||||
f"Phase '{phase_name}' failed: {'; '.join(result.errors)}",
|
||||
LogEntryType.ERROR,
|
||||
)
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING,
|
||||
success=False,
|
||||
message=f"Phase {phase_name} failed",
|
||||
)
|
||||
self._emit_planning_failed(
|
||||
f"Phase '{phase_name}' failed: {'; '.join(result.errors)}"
|
||||
)
|
||||
return False
|
||||
|
||||
# Summary
|
||||
self._print_completion_summary(results, phases_executed)
|
||||
|
||||
# End planning phase successfully
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING, success=True, message="Spec creation complete"
|
||||
)
|
||||
@@ -638,6 +699,25 @@ class SpecOrchestrator:
|
||||
)
|
||||
)
|
||||
|
||||
def _emit_planning_failed(self, error: str) -> None:
|
||||
"""Emit PLANNING_FAILED event so the frontend transitions to error state.
|
||||
|
||||
Without this, the task stays stuck in 'planning' / 'in_progress' forever
|
||||
when spec creation fails, because the XState machine never receives a
|
||||
terminal event.
|
||||
|
||||
Args:
|
||||
error: Human-readable error description
|
||||
"""
|
||||
try:
|
||||
task_emitter = TaskEventEmitter.from_spec_dir(self.spec_dir)
|
||||
task_emitter.emit(
|
||||
"PLANNING_FAILED",
|
||||
{"error": error, "recoverable": True},
|
||||
)
|
||||
except Exception:
|
||||
pass # Best effort - don't mask the original failure
|
||||
|
||||
def _run_review_checkpoint(self, auto_approve: bool) -> bool:
|
||||
"""Run the human review checkpoint.
|
||||
|
||||
@@ -661,9 +741,8 @@ class SpecOrchestrator:
|
||||
print_status("Build will not proceed without approval.", "warning")
|
||||
return False
|
||||
|
||||
except SystemExit as e:
|
||||
if e.code != 0:
|
||||
return False
|
||||
except SystemExit:
|
||||
# Review checkpoint may call sys.exit(); treat any exit as unapproved
|
||||
return False
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
@@ -696,19 +775,25 @@ class SpecOrchestrator:
|
||||
The functionality has been moved to models.rename_spec_dir_from_requirements.
|
||||
|
||||
Returns:
|
||||
True if successful or not needed, False on error
|
||||
True if successful or not needed, False if prerequisites are missing
|
||||
"""
|
||||
result = rename_spec_dir_from_requirements(self.spec_dir)
|
||||
# Update self.spec_dir if it was renamed
|
||||
if result and self.spec_dir.name.endswith("-pending"):
|
||||
# Find the renamed directory
|
||||
parent = self.spec_dir.parent
|
||||
prefix = self.spec_dir.name[:4] # e.g., "001-"
|
||||
for candidate in parent.iterdir():
|
||||
if (
|
||||
candidate.name.startswith(prefix)
|
||||
and "pending" not in candidate.name
|
||||
):
|
||||
self.spec_dir = candidate
|
||||
break
|
||||
return result
|
||||
# Check prerequisites first
|
||||
requirements_file = self.spec_dir / "requirements.json"
|
||||
if not requirements_file.exists():
|
||||
return False
|
||||
|
||||
try:
|
||||
with open(requirements_file, encoding="utf-8") as f:
|
||||
req = json.load(f)
|
||||
task_desc = req.get("task_description", "")
|
||||
if not task_desc:
|
||||
return False
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return False
|
||||
|
||||
# Attempt rename
|
||||
new_spec_dir = rename_spec_dir_from_requirements(self.spec_dir)
|
||||
if new_spec_dir != self.spec_dir:
|
||||
self.spec_dir = new_spec_dir
|
||||
self.validator = SpecValidator(self.spec_dir)
|
||||
return True
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "auto-claude-ui",
|
||||
"version": "2.7.6-beta.5",
|
||||
"version": "2.7.6",
|
||||
"type": "module",
|
||||
"description": "Desktop UI for Auto Claude autonomous coding framework",
|
||||
"homepage": "https://github.com/AndyMik90/Auto-Claude",
|
||||
|
||||
@@ -186,7 +186,7 @@ describe('Subprocess Spawn Integration', () => {
|
||||
'Test task description'
|
||||
]),
|
||||
expect.objectContaining({
|
||||
cwd: AUTO_CLAUDE_SOURCE, // Process runs from auto-claude source directory
|
||||
cwd: TEST_PROJECT_PATH, // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
|
||||
env: expect.objectContaining({
|
||||
PYTHONUNBUFFERED: '1'
|
||||
})
|
||||
@@ -218,7 +218,7 @@ describe('Subprocess Spawn Integration', () => {
|
||||
'spec-001'
|
||||
]),
|
||||
expect.objectContaining({
|
||||
cwd: AUTO_CLAUDE_SOURCE // Process runs from auto-claude source directory
|
||||
cwd: TEST_PROJECT_PATH // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
|
||||
})
|
||||
);
|
||||
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
|
||||
@@ -248,7 +248,7 @@ describe('Subprocess Spawn Integration', () => {
|
||||
'--qa'
|
||||
]),
|
||||
expect.objectContaining({
|
||||
cwd: AUTO_CLAUDE_SOURCE // Process runs from auto-claude source directory
|
||||
cwd: TEST_PROJECT_PATH // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
|
||||
})
|
||||
);
|
||||
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
/**
|
||||
* Unit tests for FileWatcher concurrency mechanisms
|
||||
* Tests deduplication, supersession, cancellation, and unwatchAll behaviour
|
||||
* under concurrent watch()/unwatch() call patterns.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { EventEmitter } from 'events';
|
||||
import path from 'path';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock chokidar BEFORE importing FileWatcher so the module sees our mock.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// A minimal FSWatcher stub that lets us control when close() resolves.
|
||||
class MockFSWatcher extends EventEmitter {
|
||||
close: ReturnType<typeof vi.fn>;
|
||||
constructor(closeImpl?: () => Promise<void>) {
|
||||
super();
|
||||
this.close = vi.fn(closeImpl ?? (() => Promise.resolve()));
|
||||
}
|
||||
}
|
||||
|
||||
// Track every watcher created so tests can inspect them.
|
||||
let createdWatchers: MockFSWatcher[] = [];
|
||||
// Factory override — tests replace this to inject custom stubs.
|
||||
let watchFactory: (() => MockFSWatcher) | null = null;
|
||||
|
||||
vi.mock('chokidar', () => ({
|
||||
default: {
|
||||
watch: vi.fn((_path: string, _opts: unknown) => {
|
||||
const watcher = watchFactory ? watchFactory() : new MockFSWatcher();
|
||||
createdWatchers.push(watcher);
|
||||
return watcher;
|
||||
})
|
||||
}
|
||||
}));
|
||||
|
||||
// Mock 'fs' so we can control existsSync / readFileSync without touching disk.
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(() => true),
|
||||
readFileSync: vi.fn(() => JSON.stringify({ phases: [] }))
|
||||
}));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Import after mocks are registered
|
||||
// ---------------------------------------------------------------------------
|
||||
import { FileWatcher } from '../file-watcher';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('FileWatcher concurrency', () => {
|
||||
let fw: FileWatcher;
|
||||
|
||||
beforeEach(() => {
|
||||
fw = new FileWatcher();
|
||||
createdWatchers = [];
|
||||
watchFactory = null;
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean up any watchers that are still open.
|
||||
await fw.unwatchAll();
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 1. Deduplication — same taskId + same specDir
|
||||
// -------------------------------------------------------------------------
|
||||
describe('deduplication: second watch() with same specDir is a no-op', () => {
|
||||
it('should only create one FSWatcher when watch() is called twice with the same specDir while the first is still in-flight', async () => {
|
||||
const specDir = '/project/.auto-claude/specs/001-task';
|
||||
const taskId = 'task-1';
|
||||
|
||||
// To create a real async gap we need an existing watcher whose close() is slow.
|
||||
// First, set up a watcher for taskId (completes synchronously).
|
||||
await fw.watch(taskId, specDir);
|
||||
expect(createdWatchers).toHaveLength(1);
|
||||
|
||||
// Replace close() with a slow one so the next watch() call has an async gap.
|
||||
const existingWatcher = createdWatchers[0];
|
||||
let resolveClose!: () => void;
|
||||
existingWatcher.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveClose = res; })
|
||||
);
|
||||
|
||||
// Now start two concurrent watch() calls for the SAME specDir.
|
||||
// Both will try to enter, but the second should be deduplicated.
|
||||
const watchPromise1 = fw.watch(taskId, specDir);
|
||||
const watchPromise2 = fw.watch(taskId, specDir);
|
||||
|
||||
// Resolve the close so both can proceed.
|
||||
resolveClose();
|
||||
await Promise.all([watchPromise1, watchPromise2]);
|
||||
|
||||
// Only one new FSWatcher should have been created (the second call was a no-op).
|
||||
// createdWatchers[0] is the original; createdWatchers[1] is the new one.
|
||||
expect(createdWatchers).toHaveLength(2);
|
||||
expect(fw.isWatching(taskId)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 2. Supersession — same taskId, different specDir
|
||||
// -------------------------------------------------------------------------
|
||||
describe('supersession: watch() with different specDir replaces the in-flight call', () => {
|
||||
it('should let the second call win when the first is awaiting close()', async () => {
|
||||
const taskId = 'task-2';
|
||||
const specDir1 = path.join('/project', '.auto-claude', 'specs', '001-first');
|
||||
const specDir2 = path.join('/project', '.auto-claude', 'specs', '002-second');
|
||||
|
||||
// First call installs an existing watcher (simulate: the watcher for
|
||||
// specDir1 is already set up so the second watch() needs to close it).
|
||||
// We do this by running the first watch() to completion first.
|
||||
await fw.watch(taskId, specDir1);
|
||||
expect(createdWatchers).toHaveLength(1);
|
||||
|
||||
// Now make the close() of the first watcher slow so there's an async gap
|
||||
// during which the second watch() can enter and supersede.
|
||||
const existingWatcher = createdWatchers[0];
|
||||
let resolveClose!: () => void;
|
||||
existingWatcher.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveClose = res; })
|
||||
);
|
||||
|
||||
// Start the second watch() — it will try to close the first watcher's
|
||||
// FSWatcher and will be awaiting that.
|
||||
const watch2Promise = fw.watch(taskId, specDir2);
|
||||
|
||||
// While the second watch() is awaiting close, start a THIRD call with
|
||||
// yet another specDir — this supersedes the second call.
|
||||
// Actually for the test described in the finding, we want:
|
||||
// - First call bails, second call creates the watcher.
|
||||
// Let's resolve the close and let watch2 finish.
|
||||
resolveClose();
|
||||
await watch2Promise;
|
||||
|
||||
// The final watcher should be for specDir2.
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir2);
|
||||
// Two watchers were created in total (one for each specDir).
|
||||
expect(createdWatchers).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('first watch() bails when pendingWatches changes to a different specDir', async () => {
|
||||
const taskId = 'task-super';
|
||||
const specDir1 = path.join('/project', '.auto-claude', 'specs', 'super-first');
|
||||
const specDir2 = path.join('/project', '.auto-claude', 'specs', 'super-second');
|
||||
|
||||
// Make the first watcher's close() slow so we can interleave.
|
||||
let resolveFirstClose!: () => void;
|
||||
watchFactory = () => {
|
||||
const w = new MockFSWatcher(() => new Promise<void>((res) => { resolveFirstClose = res; }));
|
||||
return w;
|
||||
};
|
||||
|
||||
// Start first watch().
|
||||
const watch1Promise = fw.watch(taskId, specDir1);
|
||||
|
||||
// Immediately start second watch() — before the first has resolved the
|
||||
// slow close(). At this point specDir1 watch hasn't even created an
|
||||
// FSWatcher yet (it's the very first call so there's no existing watcher
|
||||
// to close), so watch1Promise may resolve synchronously up to watcher
|
||||
// creation. Reset factory to normal for subsequent watcher creations.
|
||||
watchFactory = null;
|
||||
|
||||
const watch2Promise = fw.watch(taskId, specDir2);
|
||||
|
||||
// Let any remaining microtasks run.
|
||||
await Promise.resolve();
|
||||
if (resolveFirstClose) resolveFirstClose();
|
||||
|
||||
await Promise.all([watch1Promise, watch2Promise]);
|
||||
|
||||
// The winning call (specDir2) should own the watcher.
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir2);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 3. Cancellation — unwatch() during in-flight watch()
|
||||
// -------------------------------------------------------------------------
|
||||
describe('cancellation: unwatch() during in-flight watch() prevents watcher creation', () => {
|
||||
it('should not create a watcher when unwatch() is called before the async gap resolves', async () => {
|
||||
const taskId = 'task-3';
|
||||
const specDir = '/project/.auto-claude/specs/003-cancel';
|
||||
|
||||
// There's no pre-existing watcher, so watch() won't call close(). But it
|
||||
// does go async (chokidar.watch is sync but we can test the cancellation
|
||||
// flag by calling unwatch() before watch() runs).
|
||||
// The real async gap in watch() is the existing.watcher.close() call.
|
||||
// For this test, let's pre-install a watcher so close() is called.
|
||||
|
||||
// Install a slow-close watcher for taskId by manually populating the map.
|
||||
// We can do that by running a first watch(), then replacing close().
|
||||
await fw.watch(taskId, specDir);
|
||||
|
||||
// Replace the watcher's close() with a slow one.
|
||||
const existingWatcher = createdWatchers[0];
|
||||
let resolveExistingClose!: () => void;
|
||||
existingWatcher.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveExistingClose = res; })
|
||||
);
|
||||
|
||||
// Start a second watch() — it will await the slow close().
|
||||
const specDir2 = '/project/.auto-claude/specs/003-cancel-v2';
|
||||
const watchPromise = fw.watch(taskId, specDir2);
|
||||
|
||||
// While watch() is in-flight, call unwatch().
|
||||
await fw.unwatch(taskId);
|
||||
|
||||
// Now resolve the slow close so watch() can continue past the await.
|
||||
resolveExistingClose();
|
||||
await watchPromise;
|
||||
|
||||
// No new watcher should have been registered.
|
||||
expect(fw.isWatching(taskId)).toBe(false);
|
||||
// Only one FSWatcher was ever created (the original one for specDir).
|
||||
expect(createdWatchers).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 4. unwatchAll() with pending watches
|
||||
// -------------------------------------------------------------------------
|
||||
describe('unwatchAll() cancels all pending watches', () => {
|
||||
it('should cancel pending watch() calls and clear pendingWatches', async () => {
|
||||
const taskId1 = 'task-4a';
|
||||
const taskId2 = 'task-4b';
|
||||
const specDir1 = '/project/.auto-claude/specs/004a';
|
||||
const specDir2 = '/project/.auto-claude/specs/004b';
|
||||
|
||||
// Set up slow-close scenario for taskId1 (so watch() is in-flight).
|
||||
await fw.watch(taskId1, specDir1);
|
||||
const watcher1 = createdWatchers[0];
|
||||
let resolveClose1!: () => void;
|
||||
watcher1.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveClose1 = res; })
|
||||
);
|
||||
|
||||
// Start a new watch for taskId1 with a different specDir — this is now in-flight.
|
||||
const newSpecDir1 = '/project/.auto-claude/specs/004a-v2';
|
||||
const watchPromise1 = fw.watch(taskId1, newSpecDir1);
|
||||
|
||||
// Start a fresh watch for taskId2.
|
||||
await fw.watch(taskId2, specDir2);
|
||||
|
||||
// Call unwatchAll() while watchPromise1 is still pending.
|
||||
const unwatchAllPromise = fw.unwatchAll();
|
||||
|
||||
// Resolve the slow close so everything can proceed.
|
||||
resolveClose1();
|
||||
await Promise.all([watchPromise1, unwatchAllPromise]);
|
||||
|
||||
// After unwatchAll, no watchers should be active.
|
||||
expect(fw.isWatching(taskId1)).toBe(false);
|
||||
expect(fw.isWatching(taskId2)).toBe(false);
|
||||
|
||||
// pendingWatches should be cleared (we verify indirectly: a fresh
|
||||
// watch() call for taskId1 must succeed without treating it as a duplicate).
|
||||
const specDirFresh = path.join('/project', '.auto-claude', 'specs', '004a-fresh');
|
||||
await fw.watch(taskId1, specDirFresh);
|
||||
expect(fw.isWatching(taskId1)).toBe(true);
|
||||
expect(fw.getWatchedSpecDir(taskId1)).toBe(specDirFresh);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 5. getWatchedSpecDir() returns correct specDir
|
||||
// -------------------------------------------------------------------------
|
||||
describe('getWatchedSpecDir()', () => {
|
||||
it('returns the specDir that was passed to watch()', async () => {
|
||||
const taskId = 'task-5';
|
||||
const specDir = path.join('/project', '.auto-claude', 'specs', '005-specdir');
|
||||
|
||||
await fw.watch(taskId, specDir);
|
||||
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir);
|
||||
});
|
||||
|
||||
it('returns null when the task is not being watched', () => {
|
||||
expect(fw.getWatchedSpecDir('unknown-task')).toBeNull();
|
||||
});
|
||||
|
||||
it('returns updated specDir after re-watch with different specDir', async () => {
|
||||
const taskId = 'task-5b';
|
||||
const specDir1 = path.join('/project', '.auto-claude', 'specs', '005b-first');
|
||||
const specDir2 = path.join('/project', '.auto-claude', 'specs', '005b-second');
|
||||
|
||||
await fw.watch(taskId, specDir1);
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir1);
|
||||
|
||||
await fw.watch(taskId, specDir2);
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -329,7 +329,9 @@ export class AgentManager extends EventEmitter {
|
||||
this.registerTaskWithOperationRegistry(taskId, 'spec-creation', { projectPath, taskDescription, specDir });
|
||||
|
||||
// Note: This is spec-creation but it chains to task-execution via run.py
|
||||
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'task-execution', projectId);
|
||||
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive file access
|
||||
// issues on Windows. The script path is absolute so Python finds its modules via sys.path[0]. (#1661)
|
||||
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'task-execution', projectId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +412,10 @@ export class AgentManager extends EventEmitter {
|
||||
// Register with unified OperationRegistry for proactive swap support
|
||||
this.registerTaskWithOperationRegistry(taskId, 'task-execution', { projectPath, specId, options });
|
||||
|
||||
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'task-execution', projectId);
|
||||
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive file access
|
||||
// issues on Windows. The script path (runPath) is absolute so Python finds its modules
|
||||
// via sys.path[0] which is set to the script's directory. (#1661)
|
||||
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'task-execution', projectId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -448,7 +453,8 @@ export class AgentManager extends EventEmitter {
|
||||
|
||||
const args = [runPath, '--spec', specId, '--project-dir', projectPath, '--qa'];
|
||||
|
||||
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'qa-process', projectId);
|
||||
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive issues on Windows (#1661)
|
||||
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'qa-process', projectId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,10 +22,10 @@ import { pythonEnvManager, getConfiguredPythonPath } from '../python-env-manager
|
||||
import { buildMemoryEnvVars } from '../memory-env-builder';
|
||||
import { readSettingsFile } from '../settings-utils';
|
||||
import type { AppSettings } from '../../shared/types/settings';
|
||||
import { getOAuthModeClearVars } from './env-utils';
|
||||
import { getOAuthModeClearVars, normalizeEnvPathKey, mergePythonEnvPath } from './env-utils';
|
||||
import { getAugmentedEnv } from '../env-utils';
|
||||
import { getToolInfo, getClaudeCliPathForSdk } from '../cli-tool-manager';
|
||||
import { killProcessGracefully, isWindows } from '../platform';
|
||||
import { killProcessGracefully, isWindows, getPathDelimiter } from '../platform';
|
||||
import { debugLog } from '../../shared/utils/debug-logger';
|
||||
|
||||
/**
|
||||
@@ -679,7 +679,17 @@ export class AgentProcessManager {
|
||||
},
|
||||
});
|
||||
|
||||
// Parse Python commandto handle space-separated commands like "py -3"
|
||||
// Merge PATH from pythonEnv with augmented PATH from env.
|
||||
// pythonEnv may contain its own PATH (e.g., on Windows with pywin32_system32 prepended).
|
||||
// Simply spreading pythonEnv after env would overwrite the augmented PATH (which includes
|
||||
// npm globals, homebrew, etc.), causing "Claude code not found" on Windows (#1661).
|
||||
// mergePythonEnvPath() normalizes PATH key casing and prepends pythonEnv-specific paths.
|
||||
const mergedPythonEnv = { ...pythonEnv };
|
||||
const pathSep = getPathDelimiter();
|
||||
|
||||
mergePythonEnvPath(env as Record<string, string | undefined>, mergedPythonEnv as Record<string, string | undefined>, pathSep);
|
||||
|
||||
// Parse Python command to handle space-separated commands like "py -3"
|
||||
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(this.getPythonPath());
|
||||
let childProcess;
|
||||
try {
|
||||
@@ -687,7 +697,7 @@ export class AgentProcessManager {
|
||||
cwd,
|
||||
env: {
|
||||
...env, // Already includes process.env, extraEnv, profileEnv, PYTHONUNBUFFERED, PYTHONUTF8
|
||||
...pythonEnv, // Include Python environment (PYTHONPATH for bundled packages)
|
||||
...mergedPythonEnv, // Python env with merged PATH (preserves augmented PATH entries)
|
||||
...oauthModeClearVars, // Clear stale ANTHROPIC_* vars when in OAuth mode
|
||||
...apiProfileEnv // Include active API profile config (highest priority for ANTHROPIC_* vars)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import type { IdeationConfig, Idea } from '../../shared/types';
|
||||
import { AUTO_BUILD_PATHS } from '../../shared/constants';
|
||||
import { detectRateLimit, createSDKRateLimitInfo, getBestAvailableProfileEnv } from '../rate-limit-detector';
|
||||
import { getAPIProfileEnv } from '../services/profile';
|
||||
import { getOAuthModeClearVars } from './env-utils';
|
||||
import { getOAuthModeClearVars, normalizeEnvPathKey } from './env-utils';
|
||||
import { debugLog, debugError } from '../../shared/utils/debug-logger';
|
||||
import { stripAnsiCodes } from '../../shared/utils/ansi-sanitizer';
|
||||
import { parsePythonCommand } from '../python-detector';
|
||||
@@ -397,6 +397,12 @@ export class AgentQueueManager {
|
||||
PYTHONUTF8: '1'
|
||||
};
|
||||
|
||||
// Normalize PATH key to a single uppercase 'PATH' entry.
|
||||
// On Windows, process.env spread produces 'Path' while pythonEnv may write 'PATH',
|
||||
// resulting in duplicate keys in the final object. Without normalization the child
|
||||
// process inherits both keys, which can cause tool-not-found errors (#1661).
|
||||
normalizeEnvPathKey(finalEnv as Record<string, string | undefined>);
|
||||
|
||||
// Debug: Show OAuth token source (token values intentionally omitted for security - AC4)
|
||||
const tokenSource = profileEnv['CLAUDE_CODE_OAUTH_TOKEN']
|
||||
? 'Electron app profile'
|
||||
@@ -730,6 +736,12 @@ export class AgentQueueManager {
|
||||
PYTHONUTF8: '1'
|
||||
};
|
||||
|
||||
// Normalize PATH key to a single uppercase 'PATH' entry.
|
||||
// On Windows, process.env spread produces 'Path' while pythonEnv may write 'PATH',
|
||||
// resulting in duplicate keys in the final object. Without normalization the child
|
||||
// process inherits both keys, which can cause tool-not-found errors (#1661).
|
||||
normalizeEnvPathKey(finalEnv as Record<string, string | undefined>);
|
||||
|
||||
// Debug: Show OAuth token source (token values intentionally omitted for security - AC4)
|
||||
const tokenSource = profileEnv['CLAUDE_CODE_OAUTH_TOKEN']
|
||||
? 'Electron app profile'
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { getOAuthModeClearVars } from './env-utils';
|
||||
import { getOAuthModeClearVars, normalizeEnvPathKey, mergePythonEnvPath } from './env-utils';
|
||||
|
||||
describe('getOAuthModeClearVars', () => {
|
||||
describe('OAuth mode (no active API profile)', () => {
|
||||
@@ -132,3 +132,166 @@ describe('getOAuthModeClearVars', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeEnvPathKey', () => {
|
||||
it('should leave an already-uppercase PATH key untouched', () => {
|
||||
const env: Record<string, string | undefined> = { PATH: '/usr/bin:/bin', HOME: '/home/user' };
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env).toEqual({ PATH: '/usr/bin:/bin', HOME: '/home/user' });
|
||||
});
|
||||
|
||||
it('should rename a lowercase-variant "Path" key to "PATH"', () => {
|
||||
const env: Record<string, string | undefined> = { Path: 'C:\\Windows\\system32', HOME: '/home/user' };
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env['PATH']).toBe('C:\\Windows\\system32');
|
||||
expect('Path' in env).toBe(false);
|
||||
});
|
||||
|
||||
it('should prefer existing "PATH" and remove "Path" when both keys coexist', () => {
|
||||
// Simulates process.env spread ('Path') after getAugmentedEnv writes ('PATH')
|
||||
const env: Record<string, string | undefined> = {
|
||||
Path: 'C:\\old',
|
||||
PATH: 'C:\\Windows\\system32;C:\\augmented',
|
||||
HOME: '/home/user'
|
||||
};
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env.PATH).toBe('C:\\Windows\\system32;C:\\augmented');
|
||||
expect('Path' in env).toBe(false);
|
||||
});
|
||||
|
||||
it('should remove all case-variant PATH duplicates when PATH is already present', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: '/correct',
|
||||
Path: '/old1',
|
||||
path: '/old2'
|
||||
};
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env.PATH).toBe('/correct');
|
||||
expect('Path' in env).toBe(false);
|
||||
expect('path' in env).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle env with no PATH-like key gracefully', () => {
|
||||
const env: Record<string, string | undefined> = { HOME: '/home/user', SHELL: '/bin/zsh' };
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env).toEqual({ HOME: '/home/user', SHELL: '/bin/zsh' });
|
||||
});
|
||||
|
||||
it('should return the same env object reference (mutates in place)', () => {
|
||||
const env: Record<string, string | undefined> = { PATH: '/usr/bin' };
|
||||
const result = normalizeEnvPathKey(env);
|
||||
expect(result).toBe(env);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergePythonEnvPath - Windows PATH merge logic (#1661)', () => {
|
||||
const SEP = ';'; // Use Windows separator for these tests
|
||||
|
||||
it('should prepend pythonEnv-only entries to the augmented PATH', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\pywin32_system32;C:\\npm;C:\\homebrew'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// pywin32_system32 is unique to pythonEnv, so it should be prepended
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should deduplicate entries that already exist in augmented PATH', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew;C:\\pywin32_system32'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\pywin32_system32;C:\\npm'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// All pythonEnv entries are already in env.PATH, so mergedPythonEnv.PATH should equal env.PATH
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\npm;C:\\homebrew;C:\\pywin32_system32');
|
||||
});
|
||||
|
||||
it('should normalize Windows-style "Path" key in pythonEnv to "PATH"', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
// pythonEnv uses 'Path' (Windows native casing)
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
Path: 'C:\\pywin32_system32;C:\\npm'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// 'Path' should be normalized to 'PATH' and pythonEnv-specific entry prepended
|
||||
expect('Path' in mergedPythonEnv).toBe(false);
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should normalize Windows-style "Path" in env and deduplicate duplicates', () => {
|
||||
// Simulates process.env spread ('Path') + getAugmentedEnv write ('PATH') leaving both
|
||||
const env: Record<string, string | undefined> = {
|
||||
Path: 'C:\\old',
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\pywin32_system32;C:\\npm'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// env 'Path' should be removed; augmented 'PATH' value preserved
|
||||
expect('Path' in env).toBe(false);
|
||||
expect(env.PATH).toBe('C:\\npm;C:\\homebrew');
|
||||
// Only the unique pywin32_system32 entry prepended
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should use env.PATH unchanged when pythonEnv has no unique entries', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should work correctly with Unix colon separator', () => {
|
||||
const unixSep = ':';
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: '/usr/bin:/bin'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: '/opt/pyenv/shims:/usr/bin:/bin'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, unixSep);
|
||||
|
||||
// /opt/pyenv/shims is unique and should be prepended
|
||||
expect(mergedPythonEnv.PATH).toBe('/opt/pyenv/shims:/usr/bin:/bin');
|
||||
});
|
||||
|
||||
it('should handle missing PATH in pythonEnv gracefully (no-op)', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
// pythonEnv has no PATH at all
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PYTHONPATH: '/site-packages'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// Nothing should change
|
||||
expect(mergedPythonEnv.PATH).toBeUndefined();
|
||||
expect(mergedPythonEnv.PYTHONPATH).toBe('/site-packages');
|
||||
expect(env.PATH).toBe('C:\\npm;C:\\homebrew');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,88 @@
|
||||
* Utility functions for managing environment variables in agent spawning
|
||||
*/
|
||||
|
||||
/**
|
||||
* Normalize the PATH key in an environment object to a single uppercase 'PATH' key.
|
||||
*
|
||||
* On Windows, process.env spreads as 'Path' (the native casing) while getAugmentedEnv()
|
||||
* writes 'PATH'. Without normalization, both keys coexist in the object and the child
|
||||
* process receives duplicate PATH entries, causing tool-not-found errors like #1661.
|
||||
*
|
||||
* Mutates the provided env object in place and returns it for convenience.
|
||||
*
|
||||
* @param env - Mutable environment record to normalize
|
||||
* @returns The same env object with PATH normalized to uppercase
|
||||
*/
|
||||
export function normalizeEnvPathKey(env: Record<string, string | undefined>): Record<string, string | undefined> {
|
||||
// If 'PATH' already exists, delete all other case-variant keys (e.g. 'Path')
|
||||
if ('PATH' in env) {
|
||||
for (const key of Object.keys(env)) {
|
||||
if (key !== 'PATH' && key.toUpperCase() === 'PATH') {
|
||||
delete env[key];
|
||||
}
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
// No uppercase 'PATH' key - find the first case-variant and rename it
|
||||
const pathKey = Object.keys(env).find(k => k.toUpperCase() === 'PATH');
|
||||
if (pathKey) {
|
||||
env['PATH'] = env[pathKey];
|
||||
delete env[pathKey];
|
||||
// Remove any remaining case-variant keys
|
||||
for (const key of Object.keys(env)) {
|
||||
if (key !== 'PATH' && key.toUpperCase() === 'PATH') {
|
||||
delete env[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge pythonEnv PATH entries with the augmented PATH in env, deduplicating entries.
|
||||
*
|
||||
* pythonEnv may carry its own PATH (e.g. pywin32_system32 prepended on Windows).
|
||||
* Simply spreading pythonEnv after env would overwrite the augmented PATH (which
|
||||
* includes npm globals, Homebrew, etc.), causing "Claude code not found" (#1661).
|
||||
*
|
||||
* Strategy:
|
||||
* 1. Normalize PATH key casing in both env and pythonEnv to uppercase 'PATH'.
|
||||
* 2. Extract only pythonEnv PATH entries that are not already in env.PATH.
|
||||
* 3. Prepend those unique entries to env.PATH and store the result in pythonEnv.PATH.
|
||||
*
|
||||
* Mutates mergedPythonEnv in place (caller should pass a shallow copy if immutability is needed).
|
||||
*
|
||||
* @param env - The base environment (already augmented with tool paths)
|
||||
* @param mergedPythonEnv - Shallow copy of pythonEnv to merge PATH into
|
||||
* @param pathSep - Platform path separator (';' on Windows, ':' elsewhere)
|
||||
*/
|
||||
export function mergePythonEnvPath(
|
||||
env: Record<string, string | undefined>,
|
||||
mergedPythonEnv: Record<string, string | undefined>,
|
||||
pathSep: string
|
||||
): void {
|
||||
// Normalize PATH key to uppercase in both objects
|
||||
normalizeEnvPathKey(env);
|
||||
normalizeEnvPathKey(mergedPythonEnv);
|
||||
|
||||
if (mergedPythonEnv['PATH'] && env['PATH']) {
|
||||
const augmentedPathEntries = new Set(
|
||||
(env['PATH'] as string).split(pathSep).filter(Boolean)
|
||||
);
|
||||
// Extract only new entries from pythonEnv.PATH that aren't already in the augmented PATH
|
||||
const pythonPathEntries = (mergedPythonEnv['PATH'] as string)
|
||||
.split(pathSep)
|
||||
.filter(entry => entry && !augmentedPathEntries.has(entry));
|
||||
|
||||
// Prepend python-specific paths (e.g., pywin32_system32) to the augmented PATH
|
||||
mergedPythonEnv['PATH'] = pythonPathEntries.length > 0
|
||||
? [...pythonPathEntries, env['PATH'] as string].join(pathSep)
|
||||
: env['PATH'] as string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variables to clear ANTHROPIC_* vars when in OAuth mode
|
||||
*
|
||||
|
||||
@@ -88,16 +88,18 @@ function htmlToMarkdown(html: string): string {
|
||||
md = md.replace(/<br\s*\/?>/gi, '\n');
|
||||
md = md.replace(/<hr\s*\/?>/gi, '---\n\n');
|
||||
|
||||
// Remove any remaining HTML tags
|
||||
md = md.replace(/<[^>]+>/g, '');
|
||||
// Remove any remaining HTML tags (loop to handle nested tag fragments)
|
||||
while (/<[^>]+>/.test(md)) {
|
||||
md = md.replace(/<[^>]+>/g, '');
|
||||
}
|
||||
|
||||
// Decode common HTML entities
|
||||
md = md.replace(/&/g, '&');
|
||||
// Decode common HTML entities (& LAST to prevent double-unescaping like &lt; → < → <)
|
||||
md = md.replace(/</g, '<');
|
||||
md = md.replace(/>/g, '>');
|
||||
md = md.replace(/"/g, '"');
|
||||
md = md.replace(/'/g, "'");
|
||||
md = md.replace(/ /g, ' ');
|
||||
md = md.replace(/&/g, '&');
|
||||
|
||||
// Clean up excessive whitespace
|
||||
md = md.replace(/\n{3,}/g, '\n\n');
|
||||
|
||||
@@ -15,64 +15,122 @@ interface WatcherInfo {
|
||||
*/
|
||||
export class FileWatcher extends EventEmitter {
|
||||
private watchers: Map<string, WatcherInfo> = new Map();
|
||||
// Maps taskId -> specDir for the in-flight watch() call.
|
||||
// Allows re-watch calls with a different specDir to proceed while
|
||||
// still preventing duplicate calls for the exact same specDir.
|
||||
private pendingWatches: Map<string, string> = new Map();
|
||||
// Tracks taskIds that had unwatch() called while watch() was in-flight.
|
||||
// Checked after each await point in watch() to avoid creating a leaked watcher.
|
||||
private cancelledWatches: Set<string> = new Set();
|
||||
|
||||
/**
|
||||
* Start watching a task's implementation plan
|
||||
*/
|
||||
async watch(taskId: string, specDir: string): Promise<void> {
|
||||
// Stop any existing watcher for this task
|
||||
await this.unwatch(taskId);
|
||||
|
||||
const planPath = path.join(specDir, 'implementation_plan.json');
|
||||
|
||||
// Check if plan file exists
|
||||
if (!existsSync(planPath)) {
|
||||
this.emit('error', taskId, `Plan file not found: ${planPath}`);
|
||||
// Prevent overlapping watch() calls for the same taskId + specDir combination.
|
||||
// Since watch() is async, rapid-fire callers could enter concurrently
|
||||
// before the first call updates state, creating duplicate watchers.
|
||||
// A call with a different specDir is a legitimate re-watch and is allowed through.
|
||||
const pendingSpecDir = this.pendingWatches.get(taskId);
|
||||
if (pendingSpecDir !== undefined && pendingSpecDir === specDir) {
|
||||
return;
|
||||
}
|
||||
this.pendingWatches.set(taskId, specDir);
|
||||
|
||||
// Create watcher with settings to handle frequent writes
|
||||
const watcher = chokidar.watch(planPath, {
|
||||
persistent: true,
|
||||
ignoreInitial: true,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 300,
|
||||
pollInterval: 100
|
||||
try {
|
||||
// Close any existing watcher for this task.
|
||||
// Delete from the map BEFORE awaiting close so that a concurrent watch()
|
||||
// call entering after the await cannot obtain the same FSWatcher reference
|
||||
// and attempt a second close() on the same object.
|
||||
const existing = this.watchers.get(taskId);
|
||||
if (existing) {
|
||||
this.watchers.delete(taskId);
|
||||
await existing.watcher.close();
|
||||
}
|
||||
});
|
||||
|
||||
// Store watcher info
|
||||
this.watchers.set(taskId, {
|
||||
taskId,
|
||||
watcher,
|
||||
planPath
|
||||
});
|
||||
// Check if a newer watch() call has superseded this one while we were awaiting.
|
||||
// If the pending specDir changed, another concurrent watch() took over — bail out
|
||||
// to avoid overwriting the watcher it is about to create.
|
||||
if (this.pendingWatches.get(taskId) !== specDir) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle file changes
|
||||
watcher.on('change', () => {
|
||||
// Check if unwatch() was called while we were awaiting above.
|
||||
if (this.cancelledWatches.has(taskId)) {
|
||||
this.cancelledWatches.delete(taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
const planPath = path.join(specDir, 'implementation_plan.json');
|
||||
|
||||
// Check if plan file exists
|
||||
if (!existsSync(planPath)) {
|
||||
this.emit('error', taskId, `Plan file not found: ${planPath}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create watcher with settings to handle frequent writes
|
||||
const watcher = chokidar.watch(planPath, {
|
||||
persistent: true,
|
||||
ignoreInitial: true,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 300,
|
||||
pollInterval: 100
|
||||
}
|
||||
});
|
||||
|
||||
// Check again after the synchronous watcher creation (no await, but defensive).
|
||||
if (this.cancelledWatches.has(taskId)) {
|
||||
this.cancelledWatches.delete(taskId);
|
||||
await watcher.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Store watcher info
|
||||
this.watchers.set(taskId, {
|
||||
taskId,
|
||||
watcher,
|
||||
planPath
|
||||
});
|
||||
|
||||
// Handle file changes
|
||||
watcher.on('change', () => {
|
||||
try {
|
||||
const content = readFileSync(planPath, 'utf-8');
|
||||
const plan: ImplementationPlan = JSON.parse(content);
|
||||
this.emit('progress', taskId, plan);
|
||||
} catch {
|
||||
// File might be in the middle of being written
|
||||
// Ignore parse errors, next change event will have complete file
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
watcher.on('error', (error: unknown) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.emit('error', taskId, message);
|
||||
});
|
||||
|
||||
// Read and emit initial state
|
||||
try {
|
||||
const content = readFileSync(planPath, 'utf-8');
|
||||
const plan: ImplementationPlan = JSON.parse(content);
|
||||
this.emit('progress', taskId, plan);
|
||||
} catch {
|
||||
// File might be in the middle of being written
|
||||
// Ignore parse errors, next change event will have complete file
|
||||
// Initial read failed - not critical
|
||||
}
|
||||
} finally {
|
||||
// Only clean up if this call still owns the entry. If a superseding
|
||||
// concurrent watch() call has already updated pendingWatches with a
|
||||
// different specDir, leave that entry intact so the superseding call
|
||||
// can proceed correctly.
|
||||
if (this.pendingWatches.get(taskId) === specDir) {
|
||||
this.pendingWatches.delete(taskId);
|
||||
// The delete above guarantees has() is now false, so there is no
|
||||
// longer any in-flight watch() for this taskId. Clear the
|
||||
// cancellation flag so it doesn't linger for future watch() calls.
|
||||
this.cancelledWatches.delete(taskId);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
watcher.on('error', (error: unknown) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.emit('error', taskId, message);
|
||||
});
|
||||
|
||||
// Read and emit initial state
|
||||
try {
|
||||
const content = readFileSync(planPath, 'utf-8');
|
||||
const plan: ImplementationPlan = JSON.parse(content);
|
||||
this.emit('progress', taskId, plan);
|
||||
} catch {
|
||||
// Initial read failed - not critical
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +138,13 @@ export class FileWatcher extends EventEmitter {
|
||||
* Stop watching a task
|
||||
*/
|
||||
async unwatch(taskId: string): Promise<void> {
|
||||
// If watch() is currently in-flight for this taskId, it is already closing the
|
||||
// existing watcher. Just set the cancellation flag and return to avoid a
|
||||
// double-close of the same FSWatcher.
|
||||
if (this.pendingWatches.has(taskId)) {
|
||||
this.cancelledWatches.add(taskId);
|
||||
return;
|
||||
}
|
||||
const watcherInfo = this.watchers.get(taskId);
|
||||
if (watcherInfo) {
|
||||
await watcherInfo.watcher.close();
|
||||
@@ -91,6 +156,17 @@ export class FileWatcher extends EventEmitter {
|
||||
* Stop all watchers
|
||||
*/
|
||||
async unwatchAll(): Promise<void> {
|
||||
// Cancel any in-flight watch() calls so they don't create new watchers
|
||||
// after this cleanup completes.
|
||||
for (const taskId of this.pendingWatches.keys()) {
|
||||
this.cancelledWatches.add(taskId);
|
||||
}
|
||||
this.pendingWatches.clear();
|
||||
// Clear cancellation flags now that pendingWatches is empty: the in-flight
|
||||
// calls will bail via the supersession check (pendingWatches.get() returns
|
||||
// undefined) and will not clean up cancelledWatches themselves. Clearing
|
||||
// here ensures the instance is fully reset for subsequent use.
|
||||
this.cancelledWatches.clear();
|
||||
const closePromises = Array.from(this.watchers.values()).map(
|
||||
async (info) => {
|
||||
await info.watcher.close();
|
||||
@@ -107,6 +183,15 @@ export class FileWatcher extends EventEmitter {
|
||||
return this.watchers.has(taskId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the spec directory currently being watched for a task
|
||||
*/
|
||||
getWatchedSpecDir(taskId: string): string | null {
|
||||
const watcherInfo = this.watchers.get(taskId);
|
||||
if (!watcherInfo) return null;
|
||||
return path.dirname(watcherInfo.planPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current plan state for a task
|
||||
*/
|
||||
|
||||
@@ -98,7 +98,23 @@ export function registerAgenteventsHandlers(
|
||||
|
||||
// Send final plan state to renderer BEFORE unwatching
|
||||
// This ensures the renderer has the final subtask data (fixes 0/0 subtask bug)
|
||||
const finalPlan = fileWatcher.getCurrentPlan(taskId);
|
||||
// Try the file watcher's current path first, then fall back to worktree path
|
||||
let finalPlan = fileWatcher.getCurrentPlan(taskId);
|
||||
if (!finalPlan && exitTask && exitProject) {
|
||||
// File watcher may have been watching the wrong path (main vs worktree)
|
||||
// Try reading directly from the worktree
|
||||
const worktreePath = findTaskWorktree(exitProject.path, exitTask.specId);
|
||||
if (worktreePath) {
|
||||
const specsBaseDir = getSpecsDir(exitProject.autoBuildPath);
|
||||
const worktreePlanPath = path.join(worktreePath, specsBaseDir, exitTask.specId, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN);
|
||||
try {
|
||||
const content = readFileSync(worktreePlanPath, 'utf-8');
|
||||
finalPlan = JSON.parse(content);
|
||||
} catch {
|
||||
// Worktree plan file not readable - not critical
|
||||
}
|
||||
}
|
||||
}
|
||||
if (finalPlan) {
|
||||
safeSendToRenderer(
|
||||
getMainWindow,
|
||||
@@ -109,7 +125,9 @@ export function registerAgenteventsHandlers(
|
||||
);
|
||||
}
|
||||
|
||||
fileWatcher.unwatch(taskId);
|
||||
fileWatcher.unwatch(taskId).catch((err) => {
|
||||
console.error(`[agent-events-handlers] Failed to unwatch for ${taskId}:`, err);
|
||||
});
|
||||
|
||||
if (processType === "spec-creation") {
|
||||
console.warn(`[Task ${taskId}] Spec creation completed with code ${code}`);
|
||||
@@ -211,15 +229,26 @@ export function registerAgenteventsHandlers(
|
||||
const worktreePath = findTaskWorktree(project.path, task.specId);
|
||||
if (worktreePath) {
|
||||
const specsBaseDir = getSpecsDir(project.autoBuildPath);
|
||||
const worktreeSpecDir = path.join(worktreePath, specsBaseDir, task.specId);
|
||||
const worktreePlanPath = path.join(
|
||||
worktreePath,
|
||||
specsBaseDir,
|
||||
task.specId,
|
||||
worktreeSpecDir,
|
||||
AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN
|
||||
);
|
||||
if (existsSync(worktreePlanPath)) {
|
||||
persistPlanPhaseSync(worktreePlanPath, progress.phase, project.id);
|
||||
}
|
||||
|
||||
// Re-watch the worktree path if the file watcher is still watching the main project path.
|
||||
// This handles the case where the task started before the worktree existed:
|
||||
// the initial watch fell back to the main project spec dir, but now the worktree
|
||||
// is available and implementation_plan.json is being written there.
|
||||
const currentWatchDir = fileWatcher.getWatchedSpecDir(taskId);
|
||||
if (currentWatchDir && currentWatchDir !== worktreeSpecDir && existsSync(worktreePlanPath)) {
|
||||
console.warn(`[agent-events-handlers] Re-watching worktree path for ${taskId}: ${worktreeSpecDir}`);
|
||||
fileWatcher.watch(taskId, worktreeSpecDir).catch((err) => {
|
||||
console.error(`[agent-events-handlers] Failed to re-watch worktree for ${taskId}:`, err);
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (xstateInTerminalState && progress.phase) {
|
||||
console.debug(`[agent-events-handlers] Skipping persistPlanPhaseSync for ${taskId}: XState in '${currentXState}', not overwriting with phase '${progress.phase}'`);
|
||||
|
||||
@@ -268,6 +268,10 @@ export interface PRReviewFinding {
|
||||
endLine?: number;
|
||||
suggestedFix?: string;
|
||||
fixable: boolean;
|
||||
validationStatus?: "confirmed_valid" | "dismissed_false_positive" | "needs_human_review" | null;
|
||||
validationExplanation?: string;
|
||||
sourceAgents?: string[];
|
||||
crossValidated?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1341,6 +1345,10 @@ function getReviewResult(project: Project, prNumber: number): PRReviewResult | n
|
||||
endLine: f.end_line,
|
||||
suggestedFix: f.suggested_fix,
|
||||
fixable: f.fixable ?? false,
|
||||
validationStatus: f.validation_status ?? null,
|
||||
validationExplanation: f.validation_explanation ?? undefined,
|
||||
sourceAgents: f.source_agents ?? [],
|
||||
crossValidated: f.cross_validated ?? false,
|
||||
})) ?? [],
|
||||
summary: data.summary ?? "",
|
||||
overallStatus: data.overall_status ?? "comment",
|
||||
@@ -1491,6 +1499,19 @@ async function runPRReview(
|
||||
// Build environment with project settings
|
||||
const subprocessEnv = await getRunnerEnv(getClaudeMdEnv(project));
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'github.pr-review',
|
||||
message: `Subprocess env for PR #${prNumber} review`,
|
||||
level: 'info',
|
||||
data: {
|
||||
prNumber,
|
||||
hasGITHUB_CLI_PATH: !!subprocessEnv.GITHUB_CLI_PATH,
|
||||
GITHUB_CLI_PATH: subprocessEnv.GITHUB_CLI_PATH ?? 'NOT SET',
|
||||
hasGITHUB_TOKEN: !!subprocessEnv.GITHUB_TOKEN,
|
||||
hasPYTHONPATH: !!subprocessEnv.PYTHONPATH,
|
||||
},
|
||||
});
|
||||
|
||||
// Create operation ID for this review
|
||||
const reviewKey = getReviewKey(project.id, prNumber);
|
||||
|
||||
@@ -2020,7 +2041,7 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
},
|
||||
projectId
|
||||
);
|
||||
sendError(error instanceof Error ? error.message : "Failed to run PR review");
|
||||
sendError({ prNumber, error: error instanceof Error ? error.message : "Failed to run PR review" });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3007,6 +3028,19 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
// Build environment with project settings
|
||||
const followupEnv = await getRunnerEnv(getClaudeMdEnv(project));
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'github.pr-review',
|
||||
message: `Subprocess env for PR #${prNumber} follow-up review`,
|
||||
level: 'info',
|
||||
data: {
|
||||
prNumber,
|
||||
hasGITHUB_CLI_PATH: !!followupEnv.GITHUB_CLI_PATH,
|
||||
GITHUB_CLI_PATH: followupEnv.GITHUB_CLI_PATH ?? 'NOT SET',
|
||||
hasGITHUB_TOKEN: !!followupEnv.GITHUB_TOKEN,
|
||||
hasPYTHONPATH: !!followupEnv.PYTHONPATH,
|
||||
},
|
||||
});
|
||||
|
||||
const { process: childProcess, promise } = runPythonSubprocess<PRReviewResult>({
|
||||
pythonPath: getPythonPath(backendPath),
|
||||
args,
|
||||
|
||||
@@ -30,6 +30,15 @@ vi.mock('../../utils', () => ({
|
||||
getGitHubTokenForSubprocess: () => mockGetGitHubTokenForSubprocess(),
|
||||
}));
|
||||
|
||||
vi.mock('../../../../cli-tool-manager', () => ({
|
||||
getToolInfo: () => ({ found: false, path: undefined, source: undefined }),
|
||||
}));
|
||||
|
||||
vi.mock('../../../../sentry', () => ({
|
||||
getSentryEnvForSubprocess: () => ({}),
|
||||
safeBreadcrumb: () => {},
|
||||
}));
|
||||
|
||||
import { getRunnerEnv } from '../runner-env';
|
||||
|
||||
describe('getRunnerEnv', () => {
|
||||
|
||||
@@ -3,7 +3,8 @@ import { getAPIProfileEnv } from '../../../services/profile';
|
||||
import { getBestAvailableProfileEnv } from '../../../rate-limit-detector';
|
||||
import { pythonEnvManager } from '../../../python-env-manager';
|
||||
import { getGitHubTokenForSubprocess } from '../utils';
|
||||
import { getSentryEnvForSubprocess } from '../../../sentry';
|
||||
import { getSentryEnvForSubprocess, safeBreadcrumb } from '../../../sentry';
|
||||
import { getToolInfo } from '../../../cli-tool-manager';
|
||||
|
||||
/**
|
||||
* Get environment variables for Python runner subprocesses.
|
||||
@@ -43,12 +44,30 @@ export async function getRunnerEnv(
|
||||
const githubToken = await getGitHubTokenForSubprocess();
|
||||
const githubEnv: Record<string, string> = githubToken ? { GITHUB_TOKEN: githubToken } : {};
|
||||
|
||||
// Resolve gh CLI path so Python subprocess can find it in bundled apps
|
||||
// (bundled Electron apps have a stripped PATH that doesn't include Homebrew etc.)
|
||||
const ghInfo = getToolInfo('gh');
|
||||
const ghCliEnv: Record<string, string> = ghInfo.found && ghInfo.path ? { GITHUB_CLI_PATH: ghInfo.path } : {};
|
||||
safeBreadcrumb({
|
||||
category: 'github.runner-env',
|
||||
message: `gh CLI for subprocess: found=${ghInfo.found}, path=${ghInfo.path ?? 'none'}, source=${ghInfo.source ?? 'none'}`,
|
||||
level: ghInfo.found ? 'info' : 'warning',
|
||||
data: {
|
||||
found: ghInfo.found,
|
||||
path: ghInfo.path ?? null,
|
||||
source: ghInfo.source ?? null,
|
||||
willSetGITHUB_CLI_PATH: !!(ghInfo.found && ghInfo.path),
|
||||
hasGITHUB_TOKEN: !!githubToken,
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
...pythonEnv, // Python environment including PYTHONPATH (fixes #139)
|
||||
...apiProfileEnv,
|
||||
...oauthModeClearVars,
|
||||
...profileEnv, // OAuth token from profile manager (fixes #563, rate-limit aware)
|
||||
...githubEnv, // Fresh GitHub token from gh CLI (fixes #151)
|
||||
...ghCliEnv, // gh CLI path for bundled apps (Python backend uses GITHUB_CLI_PATH)
|
||||
...getSentryEnvForSubprocess(), // Sentry DSN + sample rates for Python subprocess
|
||||
...extraEnv, // extraEnv last so callers can still override
|
||||
};
|
||||
|
||||
@@ -20,7 +20,8 @@ import { isWindows, isMacOS } from '../../../platform';
|
||||
import { getEffectiveSourcePath } from '../../../updater/path-resolver';
|
||||
import { pythonEnvManager, getConfiguredPythonPath } from '../../../python-env-manager';
|
||||
import { getTaskkillExePath, getWhereExePath } from '../../../utils/windows-paths';
|
||||
import { safeCaptureException } from '../../../sentry';
|
||||
import { safeCaptureException, safeBreadcrumb } from '../../../sentry';
|
||||
import { getToolInfo } from '../../../cli-tool-manager';
|
||||
|
||||
const execAsync = promisify(exec);
|
||||
const execFileAsync = promisify(execFile);
|
||||
@@ -559,6 +560,7 @@ export interface GitHubModuleValidation {
|
||||
pythonEnvValid: boolean;
|
||||
error?: string;
|
||||
backendPath?: string;
|
||||
ghCliPath?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -622,33 +624,36 @@ export async function validateGitHubModule(project: Project): Promise<GitHubModu
|
||||
return result;
|
||||
}
|
||||
|
||||
// 2. Check gh CLI installation (cross-platform)
|
||||
try {
|
||||
if (isWindows()) {
|
||||
await execFileAsync(getWhereExePath(), ['gh'], { timeout: 5000 });
|
||||
} else {
|
||||
await execAsync('which gh');
|
||||
}
|
||||
// 2. Check gh CLI installation (uses CLI tool manager for bundled app compatibility)
|
||||
const ghInfo = getToolInfo('gh');
|
||||
safeBreadcrumb({
|
||||
category: 'github.validation',
|
||||
message: `gh CLI lookup: found=${ghInfo.found}, path=${ghInfo.path ?? 'none'}, source=${ghInfo.source ?? 'none'}`,
|
||||
level: ghInfo.found ? 'info' : 'warning',
|
||||
data: { found: ghInfo.found, path: ghInfo.path ?? null, source: ghInfo.source ?? null },
|
||||
});
|
||||
if (ghInfo.found && ghInfo.path) {
|
||||
result.ghCliInstalled = true;
|
||||
} catch (error: unknown) {
|
||||
result.ghCliPath = ghInfo.path;
|
||||
} else {
|
||||
result.ghCliInstalled = false;
|
||||
const errCode = (error as NodeJS.ErrnoException).code;
|
||||
if (errCode === 'ENOENT' && isWindows()) {
|
||||
result.error = `System utility 'where.exe' not found. Check Windows installation.`;
|
||||
} else {
|
||||
const installInstructions = isWindows()
|
||||
? 'winget install --id GitHub.cli'
|
||||
: isMacOS()
|
||||
? 'brew install gh'
|
||||
: 'See https://cli.github.com/';
|
||||
result.error = `GitHub CLI (gh) is not installed. Install it with:\n ${installInstructions}`;
|
||||
}
|
||||
const installInstructions = isWindows()
|
||||
? 'winget install --id GitHub.cli'
|
||||
: isMacOS()
|
||||
? 'brew install gh'
|
||||
: 'See https://cli.github.com/';
|
||||
result.error = `GitHub CLI (gh) is not installed. Install it with:\n ${installInstructions}`;
|
||||
safeCaptureException(new Error('gh CLI not found in bundled app'), {
|
||||
tags: { component: 'github-validation' },
|
||||
extra: { ghInfo, isPackaged: require('electron').app?.isPackaged ?? 'unknown' },
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// 3. Check gh authentication
|
||||
// 3. Check gh authentication (use resolved path for bundled app compatibility)
|
||||
try {
|
||||
await execAsync('gh auth status 2>&1');
|
||||
const ghPath = result.ghCliPath || 'gh';
|
||||
await execAsync(`"${ghPath}" auth status 2>&1`);
|
||||
result.ghAuthenticated = true;
|
||||
} catch (error: any) {
|
||||
// gh auth status returns non-zero when not authenticated
|
||||
|
||||
@@ -153,16 +153,24 @@ function getGitBranchesWithInfo(projectPath: string): GitBranchDetail[] {
|
||||
.map(b => b.trim())
|
||||
// Remove HEAD pointer entries like "origin/HEAD"
|
||||
.filter(b => !b.endsWith('/HEAD'))
|
||||
.map(name => ({
|
||||
name,
|
||||
type: 'remote' as const,
|
||||
displayName: name,
|
||||
isCurrent: false
|
||||
}));
|
||||
.map(fullName => {
|
||||
// Strip "origin/" prefix so branch names are clean for PR targets etc.
|
||||
const name = fullName.replace(/^origin\//, '');
|
||||
return {
|
||||
name,
|
||||
type: 'remote' as const,
|
||||
displayName: name,
|
||||
isCurrent: false
|
||||
};
|
||||
});
|
||||
} catch {
|
||||
// Remote branches may not exist, continue with local only
|
||||
}
|
||||
|
||||
// Deduplicate: if a branch exists locally and remotely, keep only the local entry
|
||||
const localNames = new Set(localBranches.map(b => b.name));
|
||||
remoteBranches = remoteBranches.filter(b => !localNames.has(b.name));
|
||||
|
||||
// Combine and sort: local branches first, then remote branches, alphabetically within each group
|
||||
const allBranches = [...localBranches, ...remoteBranches];
|
||||
|
||||
|
||||
@@ -81,6 +81,22 @@ async function ensureProfileManagerInitialized(): Promise<
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the spec directory for file watching, preferring the worktree path if it exists.
|
||||
* When a task runs in a worktree, implementation_plan.json is written there,
|
||||
* not in the main project's spec directory.
|
||||
*/
|
||||
function getSpecDirForWatcher(projectPath: string, specsBaseDir: string, specId: string): string {
|
||||
const worktreePath = findTaskWorktree(projectPath, specId);
|
||||
if (worktreePath) {
|
||||
const worktreeSpecDir = path.join(worktreePath, specsBaseDir, specId);
|
||||
if (existsSync(path.join(worktreeSpecDir, 'implementation_plan.json'))) {
|
||||
return worktreeSpecDir;
|
||||
}
|
||||
}
|
||||
return path.join(projectPath, specsBaseDir, specId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register task execution handlers (start, stop, review, status management, recovery)
|
||||
*/
|
||||
@@ -161,6 +177,31 @@ export function registerTaskExecutionHandlers(
|
||||
|
||||
console.warn('[TASK_START] Found task:', task.specId, 'status:', task.status, 'reviewReason:', task.reviewReason, 'subtasks:', task.subtasks.length);
|
||||
|
||||
// Clear stale tracking state from any previous execution so that:
|
||||
// - terminalEventSeen doesn't suppress future PROCESS_EXITED events
|
||||
// - lastSequenceByTask doesn't drop events from the new process
|
||||
taskStateManager.prepareForRestart(taskId);
|
||||
|
||||
// Check if implementation_plan.json has valid subtasks BEFORE XState handling.
|
||||
// This is more reliable than task.subtasks.length which may not be loaded yet.
|
||||
const specsBaseDir = getSpecsDir(project.autoBuildPath);
|
||||
const specDir = path.join(
|
||||
project.path,
|
||||
specsBaseDir,
|
||||
task.specId
|
||||
);
|
||||
const planFilePath = path.join(specDir, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN);
|
||||
let planHasSubtasks = false;
|
||||
const planContent = safeReadFileSync(planFilePath);
|
||||
if (planContent) {
|
||||
try {
|
||||
const plan = JSON.parse(planContent);
|
||||
planHasSubtasks = checkSubtasksCompletion(plan).totalCount > 0;
|
||||
} catch {
|
||||
// Invalid/corrupt plan file - treat as no subtasks
|
||||
}
|
||||
}
|
||||
|
||||
// Immediately mark as started so the UI moves the card to In Progress.
|
||||
// Use XState actor state as source of truth (if actor exists), with task data as fallback.
|
||||
// - plan_review: User approved the plan, send PLAN_APPROVED to transition to coding
|
||||
@@ -173,6 +214,11 @@ export function registerTaskExecutionHandlers(
|
||||
// XState says plan_review - send PLAN_APPROVED
|
||||
console.warn('[TASK_START] XState: plan_review -> coding via PLAN_APPROVED');
|
||||
taskStateManager.handleUiEvent(taskId, { type: 'PLAN_APPROVED' }, task, project);
|
||||
} else if (currentXState === 'error' && !planHasSubtasks) {
|
||||
// FIX (#1562): Task crashed during planning (no subtasks yet).
|
||||
// Uses planHasSubtasks from implementation_plan.json (more reliable than task.subtasks.length).
|
||||
console.warn('[TASK_START] XState: error with no plan subtasks -> planning via PLANNING_STARTED');
|
||||
taskStateManager.handleUiEvent(taskId, { type: 'PLANNING_STARTED' }, task, project);
|
||||
} else if (currentXState === 'human_review' || currentXState === 'error') {
|
||||
// XState says human_review or error - send USER_RESUMED
|
||||
console.warn('[TASK_START] XState:', currentXState, '-> coding via USER_RESUMED');
|
||||
@@ -186,6 +232,11 @@ export function registerTaskExecutionHandlers(
|
||||
// No XState actor - fallback to task data (e.g., after app restart)
|
||||
console.warn('[TASK_START] No XState actor, task data: plan_review -> coding via PLAN_APPROVED');
|
||||
taskStateManager.handleUiEvent(taskId, { type: 'PLAN_APPROVED' }, task, project);
|
||||
} else if (task.status === 'error' && !planHasSubtasks) {
|
||||
// FIX (#1562): No XState actor, task crashed during planning (no subtasks).
|
||||
// Uses planHasSubtasks from implementation_plan.json (more reliable than task.subtasks.length).
|
||||
console.warn('[TASK_START] No XState actor, error with no plan subtasks -> planning via PLANNING_STARTED');
|
||||
taskStateManager.handleUiEvent(taskId, { type: 'PLANNING_STARTED' }, task, project);
|
||||
} else if (task.status === 'human_review' || task.status === 'error') {
|
||||
// No XState actor - fallback to task data for resuming
|
||||
console.warn('[TASK_START] No XState actor, task data:', task.status, '-> coding via USER_RESUMED');
|
||||
@@ -205,24 +256,27 @@ export function registerTaskExecutionHandlers(
|
||||
}
|
||||
|
||||
// Start file watcher for this task
|
||||
const specsBaseDir = getSpecsDir(project.autoBuildPath);
|
||||
const specDir = path.join(
|
||||
project.path,
|
||||
specsBaseDir,
|
||||
task.specId
|
||||
);
|
||||
fileWatcher.watch(taskId, specDir);
|
||||
// Use worktree path if it exists, since the backend writes implementation_plan.json there
|
||||
const watchSpecDir = getSpecDirForWatcher(project.path, specsBaseDir, task.specId);
|
||||
fileWatcher.watch(taskId, watchSpecDir).catch((err) => {
|
||||
console.error(`[TASK_START] Failed to watch spec dir for ${taskId}:`, err);
|
||||
});
|
||||
|
||||
// Check if spec.md exists (indicates spec creation was already done or in progress)
|
||||
// Check main project path for spec file (spec is created before worktree)
|
||||
const specFilePath = path.join(specDir, AUTO_BUILD_PATHS.SPEC_FILE);
|
||||
const hasSpec = existsSync(specFilePath);
|
||||
|
||||
// Check if this task needs spec creation first (no spec file = not yet created)
|
||||
// OR if it has a spec but no implementation plan subtasks (spec created, needs planning/building)
|
||||
const needsSpecCreation = !hasSpec;
|
||||
const needsImplementation = hasSpec && task.subtasks.length === 0;
|
||||
// FIX (#1562): Check actual plan file for subtasks, not just task.subtasks.length.
|
||||
// When a task crashes during planning, it may have spec.md but an empty/missing
|
||||
// implementation_plan.json. Previously, this path would call startTaskExecution
|
||||
// (run.py) which expects subtasks to exist. Now we check the actual plan file.
|
||||
const needsImplementation = hasSpec && !planHasSubtasks;
|
||||
|
||||
console.warn('[TASK_START] hasSpec:', hasSpec, 'needsSpecCreation:', needsSpecCreation, 'needsImplementation:', needsImplementation);
|
||||
console.warn('[TASK_START] hasSpec:', hasSpec, 'planHasSubtasks:', planHasSubtasks, 'needsSpecCreation:', needsSpecCreation, 'needsImplementation:', needsImplementation);
|
||||
|
||||
// Get base branch: task-level override takes precedence over project settings
|
||||
const baseBranch = task.metadata?.baseBranch || project.settings?.mainBranch;
|
||||
@@ -237,18 +291,12 @@ export function registerTaskExecutionHandlers(
|
||||
// Also pass baseBranch so worktrees are created from the correct branch
|
||||
agentManager.startSpecCreation(taskId, project.path, taskDescription, specDir, task.metadata, baseBranch, project.id);
|
||||
} else if (needsImplementation) {
|
||||
// Spec exists but no subtasks - run run.py to create implementation plan and execute
|
||||
// Read the spec.md to get the task description
|
||||
const _taskDescription = task.description || task.title;
|
||||
try {
|
||||
readFileSync(specFilePath, 'utf-8');
|
||||
} catch {
|
||||
// Use default description
|
||||
}
|
||||
|
||||
console.warn('[TASK_START] Starting task execution (no subtasks) for:', task.specId);
|
||||
// Start task execution which will create the implementation plan
|
||||
// Note: No parallel mode for planning phase - parallel only makes sense with multiple subtasks
|
||||
// Spec exists but no valid subtasks in implementation plan
|
||||
// FIX (#1562): Use startTaskExecution (run.py) which will create the planner
|
||||
// agent session to generate the implementation plan. run.py handles the case
|
||||
// where implementation_plan.json is missing or has no subtasks - the planner
|
||||
// agent will generate the plan before the coder starts.
|
||||
console.warn('[TASK_START] Starting task execution (no valid subtasks in plan) for:', task.specId);
|
||||
agentManager.startTaskExecution(
|
||||
taskId,
|
||||
project.path,
|
||||
@@ -289,7 +337,9 @@ export function registerTaskExecutionHandlers(
|
||||
*/
|
||||
ipcMain.on(IPC_CHANNELS.TASK_STOP, (_, taskId: string) => {
|
||||
agentManager.killTask(taskId);
|
||||
fileWatcher.unwatch(taskId);
|
||||
fileWatcher.unwatch(taskId).catch((err) => {
|
||||
console.error('[TASK_STOP] Failed to unwatch:', err);
|
||||
});
|
||||
|
||||
// Find task and project to emit USER_STOPPED with plan context
|
||||
const { task, project } = findTaskAndProject(taskId);
|
||||
@@ -315,6 +365,9 @@ export function registerTaskExecutionHandlers(
|
||||
task,
|
||||
project
|
||||
);
|
||||
|
||||
// Clear stale tracking state so a subsequent restart works correctly
|
||||
taskStateManager.prepareForRestart(taskId);
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -484,6 +537,9 @@ export function registerTaskExecutionHandlers(
|
||||
return { success: false, error: 'Failed to write QA fix request file' };
|
||||
}
|
||||
|
||||
// Clear stale tracking state before starting new QA process
|
||||
taskStateManager.prepareForRestart(taskId);
|
||||
|
||||
// Restart QA process - use worktree path if it exists, otherwise main project
|
||||
// The QA process needs to run where the implementation_plan.json with completed subtasks is
|
||||
const qaProjectPath = hasWorktree ? worktreePath : project.path;
|
||||
@@ -658,6 +714,8 @@ export function registerTaskExecutionHandlers(
|
||||
|
||||
// Auto-start task when status changes to 'in_progress' and no process is running
|
||||
if (status === 'in_progress' && !agentManager.isRunning(taskId)) {
|
||||
// Clear stale tracking state before starting a new process
|
||||
taskStateManager.prepareForRestart(taskId);
|
||||
const mainWindow = getMainWindow();
|
||||
|
||||
// Check git status before auto-starting
|
||||
@@ -710,13 +768,29 @@ export function registerTaskExecutionHandlers(
|
||||
}
|
||||
|
||||
// Start file watcher for this task
|
||||
fileWatcher.watch(taskId, specDir);
|
||||
// Use worktree path if it exists, since the backend writes implementation_plan.json there
|
||||
const watchSpecDir = getSpecDirForWatcher(project.path, specsBaseDir, task.specId);
|
||||
fileWatcher.watch(taskId, watchSpecDir).catch((err) => {
|
||||
console.error(`[TASK_UPDATE_STATUS] Failed to watch spec dir for ${taskId}:`, err);
|
||||
});
|
||||
|
||||
// Check if spec.md exists
|
||||
const specFilePath = path.join(specDir, AUTO_BUILD_PATHS.SPEC_FILE);
|
||||
const hasSpec = existsSync(specFilePath);
|
||||
const needsSpecCreation = !hasSpec;
|
||||
const needsImplementation = hasSpec && task.subtasks.length === 0;
|
||||
// FIX (#1562): Check actual plan file for subtasks, not just task.subtasks.length
|
||||
const updatePlanFilePath = path.join(specDir, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN);
|
||||
let updatePlanHasSubtasks = false;
|
||||
const updatePlanContent = safeReadFileSync(updatePlanFilePath);
|
||||
if (updatePlanContent) {
|
||||
try {
|
||||
const plan = JSON.parse(updatePlanContent);
|
||||
updatePlanHasSubtasks = checkSubtasksCompletion(plan).totalCount > 0;
|
||||
} catch {
|
||||
// Invalid/corrupt plan file - treat as no subtasks
|
||||
}
|
||||
}
|
||||
const needsImplementation = hasSpec && !updatePlanHasSubtasks;
|
||||
|
||||
console.warn('[TASK_UPDATE_STATUS] hasSpec:', hasSpec, 'needsSpecCreation:', needsSpecCreation, 'needsImplementation:', needsImplementation);
|
||||
|
||||
@@ -1065,11 +1139,15 @@ export function registerTaskExecutionHandlers(
|
||||
}
|
||||
|
||||
// Stop file watcher if it was watching this task
|
||||
fileWatcher.unwatch(taskId);
|
||||
fileWatcher.unwatch(taskId).catch((err) => {
|
||||
console.error('[TASK_RECOVER_STUCK] Failed to unwatch:', err);
|
||||
});
|
||||
|
||||
// Auto-restart the task if requested
|
||||
let autoRestarted = false;
|
||||
if (autoRestart) {
|
||||
// Clear stale tracking state before restarting
|
||||
taskStateManager.prepareForRestart(taskId);
|
||||
// Check git status before auto-restarting
|
||||
const gitStatusForRestart = checkGitStatus(project.path);
|
||||
if (!gitStatusForRestart.isGitRepo || !gitStatusForRestart.hasCommits) {
|
||||
@@ -1146,12 +1224,16 @@ export function registerTaskExecutionHandlers(
|
||||
|
||||
// Start the task execution
|
||||
// Start file watcher for this task
|
||||
const specsBaseDir = getSpecsDir(project.autoBuildPath);
|
||||
const specDirForWatcher = path.join(project.path, specsBaseDir, task.specId);
|
||||
fileWatcher.watch(taskId, specDirForWatcher);
|
||||
// Use worktree path if it exists, since the backend writes implementation_plan.json there
|
||||
const watchSpecDir = getSpecDirForWatcher(project.path, specsBaseDir, task.specId);
|
||||
fileWatcher.watch(taskId, watchSpecDir).catch((err) => {
|
||||
console.error(`[Recovery] Failed to watch spec dir for ${taskId}:`, err);
|
||||
});
|
||||
|
||||
// Check if spec.md exists to determine whether to run spec creation or task execution
|
||||
const specFilePath = path.join(specDirForWatcher, AUTO_BUILD_PATHS.SPEC_FILE);
|
||||
// Check main project path for spec file (spec is created before worktree)
|
||||
// mainSpecDir is declared earlier in the handler scope
|
||||
const specFilePath = path.join(mainSpecDir, AUTO_BUILD_PATHS.SPEC_FILE);
|
||||
const hasSpec = existsSync(specFilePath);
|
||||
const needsSpecCreation = !hasSpec;
|
||||
|
||||
@@ -1162,7 +1244,7 @@ export function registerTaskExecutionHandlers(
|
||||
// No spec file - need to run spec_runner.py to create the spec
|
||||
const taskDescription = task.description || task.title;
|
||||
console.warn(`[Recovery] Starting spec creation for: ${task.specId}`);
|
||||
agentManager.startSpecCreation(taskId, project.path, taskDescription, specDirForWatcher, task.metadata, baseBranchForRecovery, project.id);
|
||||
agentManager.startSpecCreation(taskId, project.path, taskDescription, mainSpecDir, task.metadata, baseBranchForRecovery, project.id);
|
||||
} else {
|
||||
// Spec exists - run task execution
|
||||
console.warn(`[Recovery] Starting task execution for: ${task.specId}`);
|
||||
|
||||
@@ -1370,7 +1370,9 @@ function getTaskBaseBranch(specDir: string): string | undefined {
|
||||
if (metadata.baseBranch &&
|
||||
metadata.baseBranch !== '__project_default__' &&
|
||||
GIT_BRANCH_REGEX.test(metadata.baseBranch)) {
|
||||
return metadata.baseBranch;
|
||||
// Strip remote prefix if present (e.g., "origin/feat/x" → "feat/x")
|
||||
const branch = metadata.baseBranch.replace(/^origin\//, '');
|
||||
return branch;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { app } from 'electron';
|
||||
import { findPythonCommand, getBundledPythonPath } from './python-detector';
|
||||
import { isLinux, isWindows, getPathDelimiter } from './platform';
|
||||
import { getIsolatedGitEnv } from './utils/git-isolation';
|
||||
import { normalizeEnvPathKey } from './agent/env-utils';
|
||||
|
||||
export interface PythonEnvStatus {
|
||||
ready: boolean;
|
||||
@@ -726,17 +727,10 @@ if sys.version_info >= (3, 12):
|
||||
const pywin32System32 = path.join(this.sitePackagesPath, 'pywin32_system32');
|
||||
|
||||
// Add pywin32_system32 to PATH for DLL loading
|
||||
// Fix PATH case sensitivity: On Windows, env vars are case-insensitive but Node.js
|
||||
// preserves case. If we have both 'PATH' and 'Path', Node.js lexicographically sorts
|
||||
// and uses the first match, causing issues. Normalize to single 'PATH' key.
|
||||
// See: https://github.com/nodejs/node/issues/9157
|
||||
const pathKey = Object.keys(baseEnv).find(k => k.toUpperCase() === 'PATH');
|
||||
const currentPath = pathKey ? baseEnv[pathKey] : '';
|
||||
|
||||
// Remove any existing PATH variants to avoid duplicates
|
||||
if (pathKey && pathKey !== 'PATH') {
|
||||
delete baseEnv[pathKey];
|
||||
}
|
||||
// Normalize to single 'PATH' key before reading/writing, using the shared utility.
|
||||
// This prevents duplicate 'Path'/'PATH' keys that cause DLL-load failures on Windows.
|
||||
normalizeEnvPathKey(baseEnv);
|
||||
const currentPath = baseEnv['PATH'] ?? '';
|
||||
|
||||
if (currentPath && !currentPath.includes(pywin32System32)) {
|
||||
windowsEnv['PATH'] = `${pywin32System32};${currentPath}`;
|
||||
|
||||
@@ -169,6 +169,18 @@ export class TaskStateManager {
|
||||
return this.getCurrentState(taskId) === 'plan_review';
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset tracking state for a task that is about to be restarted.
|
||||
* Clears terminalEventSeen (so process exits aren't swallowed) and
|
||||
* lastSequenceByTask (so events from the new process aren't dropped
|
||||
* as duplicates). Does NOT stop or remove the XState actor, since
|
||||
* the caller may still need to send events to it.
|
||||
*/
|
||||
prepareForRestart(taskId: string): void {
|
||||
this.terminalEventSeen.delete(taskId);
|
||||
this.lastSequenceByTask.delete(taskId);
|
||||
}
|
||||
|
||||
clearTask(taskId: string): void {
|
||||
this.lastSequenceByTask.delete(taskId);
|
||||
this.lastStateByTask.delete(taskId);
|
||||
|
||||
@@ -376,6 +376,10 @@ export interface PRReviewFinding {
|
||||
endLine?: number;
|
||||
suggestedFix?: string;
|
||||
fixable: boolean;
|
||||
validationStatus?: 'confirmed_valid' | 'dismissed_false_positive' | 'needs_human_review' | null;
|
||||
validationExplanation?: string;
|
||||
sourceAgents?: string[];
|
||||
crossValidated?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -68,6 +68,7 @@ export function GitHubPRs({ onOpenSettings, isActive = false }: GitHubPRsProps)
|
||||
isReviewing,
|
||||
isExternalReview,
|
||||
previousReviewResult,
|
||||
reviewError,
|
||||
hasMore,
|
||||
selectPR,
|
||||
runReview,
|
||||
@@ -271,6 +272,7 @@ export function GitHubPRs({ onOpenSettings, isActive = false }: GitHubPRsProps)
|
||||
startedAt={startedAt}
|
||||
isReviewing={isReviewing}
|
||||
isExternalReview={isExternalReview}
|
||||
reviewError={reviewError}
|
||||
initialNewCommitsCheck={storedNewCommitsCheck}
|
||||
isActive={isActive}
|
||||
isLoadingFiles={isLoadingPRDetails}
|
||||
|
||||
@@ -14,6 +14,7 @@ interface FindingItemProps {
|
||||
finding: PRReviewFinding;
|
||||
selected: boolean;
|
||||
posted?: boolean;
|
||||
disputed?: boolean;
|
||||
onToggle: () => void;
|
||||
}
|
||||
|
||||
@@ -33,7 +34,7 @@ function getCategoryTranslationKey(category: string): string {
|
||||
return categoryMap[category.toLowerCase()] || category;
|
||||
}
|
||||
|
||||
export function FindingItem({ finding, selected, posted = false, onToggle }: FindingItemProps) {
|
||||
export function FindingItem({ finding, selected, posted = false, disputed = false, onToggle }: FindingItemProps) {
|
||||
const { t } = useTranslation('common');
|
||||
const CategoryIcon = getCategoryIcon(finding.category);
|
||||
|
||||
@@ -45,8 +46,9 @@ export function FindingItem({ finding, selected, posted = false, onToggle }: Fin
|
||||
<div
|
||||
className={cn(
|
||||
"rounded-lg border bg-background p-3 space-y-2 transition-colors",
|
||||
selected && !posted && "ring-2 ring-primary/50",
|
||||
posted && "opacity-60"
|
||||
selected && !posted && !disputed && "ring-2 ring-primary/50",
|
||||
selected && disputed && "ring-2 ring-purple-500/50",
|
||||
(posted || (disputed && !selected)) && "opacity-60"
|
||||
)}
|
||||
>
|
||||
{/* Finding Header */}
|
||||
@@ -72,6 +74,16 @@ export function FindingItem({ finding, selected, posted = false, onToggle }: Fin
|
||||
{t('prReview.posted')}
|
||||
</Badge>
|
||||
)}
|
||||
{disputed && (
|
||||
<Badge variant="outline" className="text-xs shrink-0 bg-purple-500/10 text-purple-500 border-purple-500/30">
|
||||
{t('prReview.disputed')}
|
||||
</Badge>
|
||||
)}
|
||||
{finding.crossValidated && finding.sourceAgents && finding.sourceAgents.length > 1 && (
|
||||
<Badge variant="outline" className="text-xs shrink-0 bg-green-500/10 text-green-500 border-green-500/30">
|
||||
{t('prReview.crossValidatedBy', { count: finding.sourceAgents.length })}
|
||||
</Badge>
|
||||
)}
|
||||
<span className="font-medium text-sm break-words">
|
||||
{finding.title}
|
||||
</span>
|
||||
@@ -79,6 +91,11 @@ export function FindingItem({ finding, selected, posted = false, onToggle }: Fin
|
||||
<p className="text-sm text-muted-foreground break-words">
|
||||
{finding.description}
|
||||
</p>
|
||||
{disputed && finding.validationExplanation && (
|
||||
<p className="text-xs text-purple-500/80 italic break-words">
|
||||
{finding.validationExplanation}
|
||||
</p>
|
||||
)}
|
||||
<div className="text-xs text-muted-foreground">
|
||||
<code className="bg-muted px-1 py-0.5 rounded break-all">
|
||||
{finding.file}:{finding.line}
|
||||
|
||||
@@ -9,9 +9,10 @@ import type { PRReviewFinding } from '../hooks/useGitHubPRs';
|
||||
interface FindingsSummaryProps {
|
||||
findings: PRReviewFinding[];
|
||||
selectedCount: number;
|
||||
disputedCount?: number;
|
||||
}
|
||||
|
||||
export function FindingsSummary({ findings, selectedCount }: FindingsSummaryProps) {
|
||||
export function FindingsSummary({ findings, selectedCount, disputedCount = 0 }: FindingsSummaryProps) {
|
||||
const { t } = useTranslation('common');
|
||||
|
||||
// Count findings by severity
|
||||
@@ -46,6 +47,11 @@ export function FindingsSummary({ findings, selectedCount }: FindingsSummaryProp
|
||||
{counts.low} {t('prReview.severity.low')}
|
||||
</Badge>
|
||||
)}
|
||||
{disputedCount > 0 && (
|
||||
<Badge variant="outline" className="bg-purple-500/10 text-purple-500 border-purple-500/30">
|
||||
{disputedCount} {t('prReview.disputed')}
|
||||
</Badge>
|
||||
)}
|
||||
</div>
|
||||
<span className="text-xs text-muted-foreground">
|
||||
{t('prReview.selectedOfTotal', { selected: selectedCount, total: counts.total })}
|
||||
|
||||
@@ -46,6 +46,7 @@ interface PRDetailProps {
|
||||
startedAt: string | null;
|
||||
isReviewing: boolean;
|
||||
isExternalReview?: boolean;
|
||||
reviewError?: string | null;
|
||||
initialNewCommitsCheck?: NewCommitsCheck | null;
|
||||
isActive?: boolean;
|
||||
isLoadingFiles?: boolean;
|
||||
@@ -81,6 +82,7 @@ export function PRDetail({
|
||||
startedAt,
|
||||
isReviewing,
|
||||
isExternalReview = false,
|
||||
reviewError: reviewErrorProp,
|
||||
initialNewCommitsCheck,
|
||||
isActive: _isActive = false,
|
||||
isLoadingFiles = false,
|
||||
@@ -721,6 +723,16 @@ export function PRDetail({
|
||||
};
|
||||
}
|
||||
|
||||
if (reviewErrorProp && !reviewResult?.success) {
|
||||
return {
|
||||
status: 'not_reviewed',
|
||||
label: t('prReview.reviewFailed'),
|
||||
description: reviewErrorProp,
|
||||
icon: <AlertCircle className="h-5 w-5" />,
|
||||
color: 'bg-destructive/20 text-destructive border-destructive/50',
|
||||
};
|
||||
}
|
||||
|
||||
if (!reviewResult || !reviewResult.success) {
|
||||
return {
|
||||
status: 'not_reviewed',
|
||||
@@ -863,7 +875,7 @@ export function PRDetail({
|
||||
icon: <MessageSquare className="h-5 w-5" />,
|
||||
color: 'bg-primary/20 text-primary border-primary/50',
|
||||
};
|
||||
}, [isReviewing, reviewProgress, reviewResult, postedFindingIds, isReadyToMerge, newCommitsCheck, t]);
|
||||
}, [isReviewing, reviewProgress, reviewResult, reviewErrorProp, postedFindingIds, isReadyToMerge, newCommitsCheck, t]);
|
||||
|
||||
const handlePostReview = async () => {
|
||||
const idsToPost = Array.from(selectedFindingIds);
|
||||
@@ -1128,6 +1140,7 @@ ${t('prReview.blockedStatusMessageFooter')}`;
|
||||
reviewResult={reviewResult}
|
||||
previousReviewResult={previousReviewResult}
|
||||
postedCount={new Set([...postedFindingIds, ...(reviewResult?.postedFindingIds ?? [])]).size}
|
||||
reviewError={reviewErrorProp}
|
||||
onRunReview={onRunReview}
|
||||
onRunFollowupReview={onRunFollowupReview}
|
||||
onCancelReview={onCancelReview}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
* - Quick select actions (Critical/High, All, None)
|
||||
* - Collapsible sections for less important findings
|
||||
* - Visual summary of finding counts
|
||||
* - Disputed findings shown in a separate collapsible section
|
||||
*/
|
||||
|
||||
import { useState, useMemo } from 'react';
|
||||
@@ -16,6 +17,9 @@ import {
|
||||
CheckSquare,
|
||||
Square,
|
||||
Send,
|
||||
ChevronDown,
|
||||
ChevronRight,
|
||||
ShieldQuestion,
|
||||
} from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button } from '../../ui/button';
|
||||
@@ -47,6 +51,7 @@ export function ReviewFindings({
|
||||
const [expandedSections, setExpandedSections] = useState<Set<SeverityGroup>>(
|
||||
new Set<SeverityGroup>(['critical', 'high']) // Critical and High expanded by default
|
||||
);
|
||||
const [disputedExpanded, setDisputedExpanded] = useState(false);
|
||||
|
||||
// Filter out posted findings - only show unposted findings for selection
|
||||
const unpostedFindings = useMemo(() =>
|
||||
@@ -54,10 +59,24 @@ export function ReviewFindings({
|
||||
[findings, postedIds]
|
||||
);
|
||||
|
||||
// Split unposted findings into active vs disputed (single pass)
|
||||
const { activeFindings, disputedFindings } = useMemo(() => {
|
||||
const active: PRReviewFinding[] = [];
|
||||
const disputed: PRReviewFinding[] = [];
|
||||
for (const finding of unpostedFindings) {
|
||||
if (finding.validationStatus === 'dismissed_false_positive') {
|
||||
disputed.push(finding);
|
||||
} else {
|
||||
active.push(finding);
|
||||
}
|
||||
}
|
||||
return { activeFindings: active, disputedFindings: disputed };
|
||||
}, [unpostedFindings]);
|
||||
|
||||
// Check if all findings are posted
|
||||
const allFindingsPosted = findings.length > 0 && unpostedFindings.length === 0;
|
||||
|
||||
// Group unposted findings by severity (only show findings that haven't been posted)
|
||||
// Group ACTIVE unposted findings by severity (disputed go in their own section)
|
||||
const groupedFindings = useMemo(() => {
|
||||
const groups: Record<SeverityGroup, PRReviewFinding[]> = {
|
||||
critical: [],
|
||||
@@ -66,7 +85,7 @@ export function ReviewFindings({
|
||||
low: [],
|
||||
};
|
||||
|
||||
for (const finding of unpostedFindings) {
|
||||
for (const finding of activeFindings) {
|
||||
const severity = finding.severity as SeverityGroup;
|
||||
if (groups[severity]) {
|
||||
groups[severity].push(finding);
|
||||
@@ -74,20 +93,20 @@ export function ReviewFindings({
|
||||
}
|
||||
|
||||
return groups;
|
||||
}, [unpostedFindings]);
|
||||
}, [activeFindings]);
|
||||
|
||||
// Count by severity (unposted findings only)
|
||||
// Count by severity (active findings only)
|
||||
const counts = useMemo(() => ({
|
||||
critical: groupedFindings.critical.length,
|
||||
high: groupedFindings.high.length,
|
||||
medium: groupedFindings.medium.length,
|
||||
low: groupedFindings.low.length,
|
||||
total: unpostedFindings.length,
|
||||
total: activeFindings.length,
|
||||
important: groupedFindings.critical.length + groupedFindings.high.length,
|
||||
posted: postedIds.size,
|
||||
}), [groupedFindings, unpostedFindings.length, postedIds.size]);
|
||||
}), [groupedFindings, activeFindings.length, postedIds.size]);
|
||||
|
||||
// Selection hooks - use unposted findings only
|
||||
// Selection hooks - use ACTIVE unposted findings only (Select All excludes disputed)
|
||||
const {
|
||||
toggleFinding,
|
||||
selectAll,
|
||||
@@ -95,7 +114,7 @@ export function ReviewFindings({
|
||||
selectImportant,
|
||||
toggleSeverityGroup,
|
||||
} = useFindingSelection({
|
||||
findings: unpostedFindings,
|
||||
findings: activeFindings,
|
||||
selectedIds,
|
||||
onSelectionChange,
|
||||
groupedFindings,
|
||||
@@ -114,6 +133,12 @@ export function ReviewFindings({
|
||||
});
|
||||
};
|
||||
|
||||
// Count only active findings that are selected (excludes disputed from count)
|
||||
const selectedActiveCount = useMemo(
|
||||
() => activeFindings.filter(f => selectedIds.has(f.id)).length,
|
||||
[activeFindings, selectedIds]
|
||||
);
|
||||
|
||||
// When all findings have been posted, show a success message instead of the selection UI
|
||||
if (allFindingsPosted) {
|
||||
return (
|
||||
@@ -131,10 +156,11 @@ export function ReviewFindings({
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Summary Stats Bar - show unposted findings only */}
|
||||
{/* Summary Stats Bar - show active findings + disputed count */}
|
||||
<FindingsSummary
|
||||
findings={unpostedFindings}
|
||||
selectedCount={selectedIds.size}
|
||||
findings={activeFindings}
|
||||
selectedCount={selectedActiveCount}
|
||||
disputedCount={disputedFindings.length}
|
||||
/>
|
||||
|
||||
{/* Quick Select Actions */}
|
||||
@@ -170,7 +196,7 @@ export function ReviewFindings({
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Grouped Findings (unposted only) */}
|
||||
{/* Grouped Findings (active only) */}
|
||||
<div className="space-y-3">
|
||||
{SEVERITY_ORDER.map((severity) => {
|
||||
const group = groupedFindings[severity];
|
||||
@@ -220,6 +246,48 @@ export function ReviewFindings({
|
||||
})}
|
||||
</div>
|
||||
|
||||
{/* Disputed Findings Section */}
|
||||
{disputedFindings.length > 0 && (
|
||||
<div className="rounded-lg border border-purple-500/20 bg-purple-500/5">
|
||||
{/* Disputed Header */}
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setDisputedExpanded(!disputedExpanded)}
|
||||
aria-expanded={disputedExpanded}
|
||||
className="w-full flex items-center gap-2 p-3 text-left hover:bg-purple-500/10 transition-colors rounded-t-lg"
|
||||
>
|
||||
{disputedExpanded ? (
|
||||
<ChevronDown className="h-4 w-4 text-purple-500 shrink-0" />
|
||||
) : (
|
||||
<ChevronRight className="h-4 w-4 text-purple-500 shrink-0" />
|
||||
)}
|
||||
<ShieldQuestion className="h-4 w-4 text-purple-500 shrink-0" />
|
||||
<span className="text-sm font-medium text-purple-500">
|
||||
{t('prReview.disputedByValidator', { count: disputedFindings.length })}
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{/* Disputed Content */}
|
||||
{disputedExpanded && (
|
||||
<div className="p-3 pt-0 space-y-2">
|
||||
<p className="text-xs text-muted-foreground italic mb-2">
|
||||
{t('prReview.disputedSectionHint')}
|
||||
</p>
|
||||
{disputedFindings.map((finding) => (
|
||||
<FindingItem
|
||||
key={finding.id}
|
||||
finding={finding}
|
||||
selected={selectedIds.has(finding.id)}
|
||||
posted={false}
|
||||
disputed
|
||||
onToggle={() => toggleFinding(finding.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Empty State - no findings at all */}
|
||||
{findings.length === 0 && (
|
||||
<div className="text-center py-8 text-muted-foreground">
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState } from 'react';
|
||||
import { CheckCircle, Circle, CircleDot, Play, RefreshCw } from 'lucide-react';
|
||||
import { AlertCircle, CheckCircle, Circle, CircleDot, Play, RefreshCw } from 'lucide-react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Button } from '../../ui/button';
|
||||
import { cn } from '../../../lib/utils';
|
||||
@@ -26,6 +26,7 @@ export interface ReviewStatusTreeProps {
|
||||
reviewResult: PRReviewResult | null;
|
||||
previousReviewResult: PRReviewResult | null;
|
||||
postedCount: number;
|
||||
reviewError?: string | null;
|
||||
onRunReview: () => void;
|
||||
onRunFollowupReview: () => void;
|
||||
onCancelReview: () => void;
|
||||
@@ -45,6 +46,7 @@ export function ReviewStatusTree({
|
||||
reviewResult,
|
||||
previousReviewResult,
|
||||
postedCount,
|
||||
reviewError,
|
||||
onRunReview,
|
||||
onRunFollowupReview,
|
||||
onCancelReview,
|
||||
@@ -57,8 +59,26 @@ export function ReviewStatusTree({
|
||||
// Determine if this is a follow-up review in progress (for edge case handling)
|
||||
const isFollowupInProgress = isReviewing && (previousReviewResult !== null || reviewResult?.isFollowupReview);
|
||||
|
||||
// If not reviewed, show simple status
|
||||
// If not reviewed, show simple status (with error if present)
|
||||
if (status === 'not_reviewed' && !isReviewing) {
|
||||
if (reviewError) {
|
||||
return (
|
||||
<div className="flex flex-wrap items-center justify-between gap-y-3 p-4 border rounded-lg bg-card shadow-sm border-destructive/30">
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
<div className="h-2.5 w-2.5 shrink-0 rounded-full bg-destructive" />
|
||||
<div className="min-w-0">
|
||||
<span className="font-medium text-destructive truncate block">{t('prReview.reviewFailed')}</span>
|
||||
<span className="text-xs text-muted-foreground truncate block mt-0.5">{reviewError}</span>
|
||||
</div>
|
||||
</div>
|
||||
<Button onClick={onRunReview} size="sm" variant="outline" className="gap-2 shrink-0 ml-auto sm:ml-0">
|
||||
<RefreshCw className="h-3.5 w-3.5" />
|
||||
{t('prReview.retryReview')}
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex flex-wrap items-center justify-between gap-y-3 p-4 border rounded-lg bg-card shadow-sm">
|
||||
<div className="flex items-center gap-3 min-w-0">
|
||||
|
||||
@@ -30,21 +30,31 @@ export function useFindingSelection({
|
||||
onSelectionChange(next);
|
||||
}, [selectedIds, onSelectionChange]);
|
||||
|
||||
// Select all findings
|
||||
// Select all findings (preserving any disputed selections not in active findings)
|
||||
const selectAll = useCallback(() => {
|
||||
onSelectionChange(new Set(findings.map(f => f.id)));
|
||||
}, [findings, onSelectionChange]);
|
||||
const activeIds = new Set(findings.map(f => f.id));
|
||||
// Preserve selections for disputed findings (IDs not in active findings list)
|
||||
for (const id of selectedIds) {
|
||||
if (!findings.some(f => f.id === id)) activeIds.add(id);
|
||||
}
|
||||
onSelectionChange(activeIds);
|
||||
}, [findings, selectedIds, onSelectionChange]);
|
||||
|
||||
// Clear all selections
|
||||
const selectNone = useCallback(() => {
|
||||
onSelectionChange(new Set());
|
||||
}, [onSelectionChange]);
|
||||
|
||||
// Select only critical and high severity findings
|
||||
// Select only critical and high severity findings (preserving disputed selections)
|
||||
const selectImportant = useCallback(() => {
|
||||
const important = [...groupedFindings.critical, ...groupedFindings.high];
|
||||
onSelectionChange(new Set(important.map(f => f.id)));
|
||||
}, [groupedFindings, onSelectionChange]);
|
||||
const importantIds = new Set(important.map(f => f.id));
|
||||
// Preserve selections for disputed findings (IDs not in active findings list)
|
||||
for (const id of selectedIds) {
|
||||
if (!findings.some(f => f.id === id)) importantIds.add(id);
|
||||
}
|
||||
onSelectionChange(importantIds);
|
||||
}, [groupedFindings, findings, selectedIds, onSelectionChange]);
|
||||
|
||||
// Toggle entire severity group selection
|
||||
const toggleSeverityGroup = useCallback((severity: SeverityGroup) => {
|
||||
|
||||
@@ -34,6 +34,7 @@ interface UseGitHubPRsResult {
|
||||
isReviewing: boolean;
|
||||
isExternalReview: boolean;
|
||||
previousReviewResult: PRReviewResult | null;
|
||||
reviewError: string | null;
|
||||
isConnected: boolean;
|
||||
repoFullName: string | null;
|
||||
activePRReviews: number[]; // PR numbers currently being reviewed
|
||||
@@ -117,6 +118,7 @@ export function useGitHubPRs(
|
||||
const isExternalReview = selectedPRReviewState?.isExternalReview ?? false;
|
||||
const previousReviewResult = selectedPRReviewState?.previousResult ?? null;
|
||||
const startedAt = selectedPRReviewState?.startedAt ?? null;
|
||||
const reviewError = selectedPRReviewState?.error ?? null;
|
||||
|
||||
// Get list of PR numbers currently being reviewed
|
||||
const activePRReviews = useMemo(() => {
|
||||
@@ -731,6 +733,7 @@ export function useGitHubPRs(
|
||||
isReviewing,
|
||||
isExternalReview,
|
||||
previousReviewResult,
|
||||
reviewError,
|
||||
isConnected,
|
||||
repoFullName,
|
||||
activePRReviews,
|
||||
|
||||
@@ -362,6 +362,7 @@
|
||||
"followup": "Follow-up",
|
||||
"initial": "Initial",
|
||||
"rerunFollowup": "Re-run follow-up review",
|
||||
"retryReview": "Retry Review",
|
||||
"rerunReview": "Re-run review",
|
||||
"updateBranch": "Update Branch",
|
||||
"updatingBranch": "Updating...",
|
||||
@@ -402,6 +403,10 @@
|
||||
"verifyChanges": "Verify Changes",
|
||||
"verifyAnyway": "Verify",
|
||||
"runFollowupAnyway": "Run follow-up verification even though no files overlap",
|
||||
"disputed": "Disputed",
|
||||
"disputedByValidator": "Disputed by Validator ({{count}})",
|
||||
"crossValidatedBy": "Confirmed by {{count}} agents",
|
||||
"disputedSectionHint": "These findings were reported by specialists but disputed by the validator. You can still select and post them.",
|
||||
"logs": {
|
||||
"agentActivity": "Agent Activity",
|
||||
"showMore": "Show {{count}} more",
|
||||
|
||||
@@ -371,6 +371,7 @@
|
||||
"followup": "Suivi",
|
||||
"initial": "Initial",
|
||||
"rerunFollowup": "Relancer la revue de suivi",
|
||||
"retryReview": "Réessayer la revue",
|
||||
"rerunReview": "Relancer la revue",
|
||||
"updateBranch": "Mettre à jour la branche",
|
||||
"updatingBranch": "Mise à jour...",
|
||||
@@ -402,6 +403,10 @@
|
||||
"blockedStatusMessageTitle": "## 🤖 Auto Claude PR Review",
|
||||
"blockedStatusMessageFooter": "*This review identified blockers that must be resolved before merge. Generated by Auto Claude.*",
|
||||
"failedPostBlockedStatus": "Échec de la publication du statut",
|
||||
"disputed": "Contesté",
|
||||
"disputedByValidator": "Contesté par le validateur ({{count}})",
|
||||
"crossValidatedBy": "Confirmé par {{count}} agents",
|
||||
"disputedSectionHint": "Ces résultats ont été signalés par les spécialistes mais contestés par le validateur. Vous pouvez toujours les sélectionner et les publier.",
|
||||
"logs": {
|
||||
"agentActivity": "Activité des agents",
|
||||
"showMore": "Afficher {{count}} de plus",
|
||||
|
||||
@@ -126,12 +126,16 @@ export const taskMachine = createMachine(
|
||||
on: {
|
||||
CREATE_PR: 'creating_pr',
|
||||
MARK_DONE: 'done',
|
||||
USER_RESUMED: { target: 'coding', actions: 'clearReviewReason' }
|
||||
USER_RESUMED: { target: 'coding', actions: 'clearReviewReason' },
|
||||
// Allow restarting planning from human_review (e.g., incomplete task with no subtasks)
|
||||
PLANNING_STARTED: { target: 'planning', actions: 'clearReviewReason' }
|
||||
}
|
||||
},
|
||||
error: {
|
||||
on: {
|
||||
USER_RESUMED: { target: 'coding', actions: 'clearReviewReason' },
|
||||
// Allow restarting from error back to planning (e.g., spec creation crashed)
|
||||
PLANNING_STARTED: { target: 'planning', actions: 'clearReviewReason' },
|
||||
MARK_DONE: 'done'
|
||||
}
|
||||
},
|
||||
|
||||
Generated
+3
-3
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "auto-claude",
|
||||
"version": "2.7.6-beta.3",
|
||||
"version": "2.7.6-beta.6",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "auto-claude",
|
||||
"version": "2.7.6-beta.3",
|
||||
"version": "2.7.6-beta.6",
|
||||
"license": "AGPL-3.0",
|
||||
"workspaces": [
|
||||
"apps/*",
|
||||
@@ -25,7 +25,7 @@
|
||||
},
|
||||
"apps/frontend": {
|
||||
"name": "auto-claude-ui",
|
||||
"version": "2.7.6-beta.3",
|
||||
"version": "2.7.6-beta.6",
|
||||
"hasInstallScript": true,
|
||||
"license": "AGPL-3.0",
|
||||
"dependencies": {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "auto-claude",
|
||||
"version": "2.7.6-beta.5",
|
||||
"version": "2.7.6",
|
||||
"description": "Autonomous multi-agent coding framework powered by Claude AI",
|
||||
"license": "AGPL-3.0",
|
||||
"author": "Auto Claude Team",
|
||||
|
||||
@@ -337,6 +337,136 @@ class TestGitHubCLIInvocation:
|
||||
assert result["success"] is True
|
||||
|
||||
|
||||
class TestGitHubOriginPrefixStripping:
|
||||
"""Test that origin/ prefix is stripped from target_branch in create_pull_request."""
|
||||
|
||||
def test_origin_prefix_stripped_from_target_branch(self, tmp_path):
|
||||
"""Test that 'origin/develop' becomes 'develop' in --base argument to gh CLI."""
|
||||
# Setup
|
||||
project_dir = tmp_path / "project"
|
||||
project_dir.mkdir()
|
||||
spec_dir = tmp_path / "spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Create .auto-claude directories
|
||||
auto_claude_dir = project_dir / ".auto-claude"
|
||||
auto_claude_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Create WorktreeManager
|
||||
manager = WorktreeManager(
|
||||
project_dir=project_dir,
|
||||
base_branch="main",
|
||||
)
|
||||
|
||||
# Mock get_worktree_info to return a valid WorktreeInfo
|
||||
mock_worktree_info = WorktreeInfo(
|
||||
path=spec_dir,
|
||||
branch="auto-claude/001-test-spec",
|
||||
spec_name="001-test-spec",
|
||||
base_branch="main",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
# Mock subprocess result
|
||||
mock_subprocess_result = MagicMock(
|
||||
returncode=0,
|
||||
stdout="https://github.com/user/repo/pull/123\n",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
# Import the actual module to patch it directly
|
||||
import core.worktree as worktree_module
|
||||
|
||||
with (
|
||||
patch.object(manager, "get_worktree_info", return_value=mock_worktree_info),
|
||||
patch.object(
|
||||
worktree_module, "get_gh_executable", return_value="/usr/bin/gh"
|
||||
),
|
||||
patch.object(
|
||||
worktree_module.subprocess, "run", return_value=mock_subprocess_result
|
||||
) as mock_run,
|
||||
patch.object(manager, "_extract_spec_summary", return_value="Test PR body"),
|
||||
):
|
||||
result = manager.create_pull_request(
|
||||
spec_name="001-test-spec",
|
||||
target_branch="origin/develop",
|
||||
title="Test PR Title",
|
||||
draft=False,
|
||||
)
|
||||
|
||||
# Verify gh CLI received "develop" (not "origin/develop") as --base
|
||||
assert mock_run.called
|
||||
call_args = mock_run.call_args[0][0]
|
||||
base_idx = call_args.index("--base")
|
||||
assert call_args[base_idx + 1] == "develop", (
|
||||
f"Expected 'develop' after --base, got '{call_args[base_idx + 1]}'"
|
||||
)
|
||||
assert result["success"] is True
|
||||
|
||||
def test_target_branch_without_origin_prefix_unchanged(self, tmp_path):
|
||||
"""Test that 'develop' (no prefix) is passed through unchanged to gh CLI."""
|
||||
# Setup
|
||||
project_dir = tmp_path / "project"
|
||||
project_dir.mkdir()
|
||||
spec_dir = tmp_path / "spec"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Create .auto-claude directories
|
||||
auto_claude_dir = project_dir / ".auto-claude"
|
||||
auto_claude_dir.mkdir(exist_ok=True)
|
||||
|
||||
# Create WorktreeManager
|
||||
manager = WorktreeManager(
|
||||
project_dir=project_dir,
|
||||
base_branch="main",
|
||||
)
|
||||
|
||||
# Mock get_worktree_info to return a valid WorktreeInfo
|
||||
mock_worktree_info = WorktreeInfo(
|
||||
path=spec_dir,
|
||||
branch="auto-claude/001-test-spec",
|
||||
spec_name="001-test-spec",
|
||||
base_branch="main",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
# Mock subprocess result
|
||||
mock_subprocess_result = MagicMock(
|
||||
returncode=0,
|
||||
stdout="https://github.com/user/repo/pull/123\n",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
# Import the actual module to patch it directly
|
||||
import core.worktree as worktree_module
|
||||
|
||||
with (
|
||||
patch.object(manager, "get_worktree_info", return_value=mock_worktree_info),
|
||||
patch.object(
|
||||
worktree_module, "get_gh_executable", return_value="/usr/bin/gh"
|
||||
),
|
||||
patch.object(
|
||||
worktree_module.subprocess, "run", return_value=mock_subprocess_result
|
||||
) as mock_run,
|
||||
patch.object(manager, "_extract_spec_summary", return_value="Test PR body"),
|
||||
):
|
||||
result = manager.create_pull_request(
|
||||
spec_name="001-test-spec",
|
||||
target_branch="develop",
|
||||
title="Test PR Title",
|
||||
draft=False,
|
||||
)
|
||||
|
||||
# Verify gh CLI received "develop" as --base
|
||||
assert mock_run.called
|
||||
call_args = mock_run.call_args[0][0]
|
||||
base_idx = call_args.index("--base")
|
||||
assert call_args[base_idx + 1] == "develop", (
|
||||
f"Expected 'develop' after --base, got '{call_args[base_idx + 1]}'"
|
||||
)
|
||||
assert result["success"] is True
|
||||
|
||||
|
||||
class TestGitHubErrorHandling:
|
||||
"""Test that GitHub error handling still works correctly."""
|
||||
|
||||
|
||||
@@ -270,6 +270,118 @@ class TestCreateMergeRequest:
|
||||
assert result["pr_url"] == "https://gitlab.com/user/repo/-/merge_requests/44"
|
||||
|
||||
|
||||
class TestGitLabOriginPrefixStripping:
|
||||
"""Test that origin/ prefix is stripped from target_branch in create_merge_request."""
|
||||
|
||||
def test_origin_prefix_stripped_from_target_branch(
|
||||
self, worktree_manager, temp_project_dir
|
||||
):
|
||||
"""Test that 'origin/develop' becomes 'develop' in --target-branch argument to glab CLI."""
|
||||
import core.worktree as worktree_module
|
||||
|
||||
spec_name = "test-feature"
|
||||
|
||||
mock_worktree_info = WorktreeInfo(
|
||||
path=temp_project_dir / ".auto-claude" / "worktrees" / "tasks" / spec_name,
|
||||
branch=f"auto-claude/{spec_name}",
|
||||
spec_name=spec_name,
|
||||
base_branch="main",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
mock_subprocess_result = MagicMock(
|
||||
returncode=0,
|
||||
stdout="https://gitlab.com/user/repo/-/merge_requests/42\n",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
worktree_manager, "get_worktree_info", return_value=mock_worktree_info
|
||||
),
|
||||
patch.object(
|
||||
worktree_module,
|
||||
"get_glab_executable",
|
||||
return_value="/usr/local/bin/glab",
|
||||
),
|
||||
patch.object(
|
||||
worktree_module.subprocess, "run", return_value=mock_subprocess_result
|
||||
) as mock_run,
|
||||
patch.object(
|
||||
worktree_manager, "_extract_spec_summary", return_value="Test MR body"
|
||||
),
|
||||
):
|
||||
result = worktree_manager.create_merge_request(
|
||||
spec_name=spec_name,
|
||||
target_branch="origin/develop",
|
||||
title="Test MR",
|
||||
draft=False,
|
||||
)
|
||||
|
||||
# Verify glab CLI received "develop" (not "origin/develop") as --target-branch
|
||||
assert mock_run.called
|
||||
call_args = mock_run.call_args[0][0]
|
||||
target_idx = call_args.index("--target-branch")
|
||||
assert call_args[target_idx + 1] == "develop", (
|
||||
f"Expected 'develop' after --target-branch, got '{call_args[target_idx + 1]}'"
|
||||
)
|
||||
assert result["success"] is True
|
||||
|
||||
def test_target_branch_without_origin_prefix_unchanged(
|
||||
self, worktree_manager, temp_project_dir
|
||||
):
|
||||
"""Test that 'develop' (no prefix) is passed through unchanged to glab CLI."""
|
||||
import core.worktree as worktree_module
|
||||
|
||||
spec_name = "test-feature"
|
||||
|
||||
mock_worktree_info = WorktreeInfo(
|
||||
path=temp_project_dir / ".auto-claude" / "worktrees" / "tasks" / spec_name,
|
||||
branch=f"auto-claude/{spec_name}",
|
||||
spec_name=spec_name,
|
||||
base_branch="main",
|
||||
is_active=True,
|
||||
)
|
||||
|
||||
mock_subprocess_result = MagicMock(
|
||||
returncode=0,
|
||||
stdout="https://gitlab.com/user/repo/-/merge_requests/43\n",
|
||||
stderr="",
|
||||
)
|
||||
|
||||
with (
|
||||
patch.object(
|
||||
worktree_manager, "get_worktree_info", return_value=mock_worktree_info
|
||||
),
|
||||
patch.object(
|
||||
worktree_module,
|
||||
"get_glab_executable",
|
||||
return_value="/usr/local/bin/glab",
|
||||
),
|
||||
patch.object(
|
||||
worktree_module.subprocess, "run", return_value=mock_subprocess_result
|
||||
) as mock_run,
|
||||
patch.object(
|
||||
worktree_manager, "_extract_spec_summary", return_value="Test MR body"
|
||||
),
|
||||
):
|
||||
result = worktree_manager.create_merge_request(
|
||||
spec_name=spec_name,
|
||||
target_branch="develop",
|
||||
title="Test MR",
|
||||
draft=False,
|
||||
)
|
||||
|
||||
# Verify glab CLI received "develop" as --target-branch
|
||||
assert mock_run.called
|
||||
call_args = mock_run.call_args[0][0]
|
||||
target_idx = call_args.index("--target-branch")
|
||||
assert call_args[target_idx + 1] == "develop", (
|
||||
f"Expected 'develop' after --target-branch, got '{call_args[target_idx + 1]}'"
|
||||
)
|
||||
assert result["success"] is True
|
||||
|
||||
|
||||
class TestPushAndCreatePR:
|
||||
"""Test push_and_create_pr method with provider detection."""
|
||||
|
||||
|
||||
@@ -1,195 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/config.py
|
||||
==============================
|
||||
|
||||
Tests project configuration initialization, directory resolution,
|
||||
project index loading, and initialization state checks.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import json
|
||||
|
||||
import mcp_server.config as config
|
||||
|
||||
|
||||
class TestInitialize:
|
||||
"""Tests for config.initialize()."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Reset global state before each test."""
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
def test_initialize_valid_project_dir(self, tmp_path):
|
||||
"""Initialize with a valid directory sets the project dir."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.get_project_dir() == tmp_path.resolve()
|
||||
|
||||
def test_initialize_missing_dir_raises_value_error(self, tmp_path):
|
||||
"""Initialize with nonexistent directory raises ValueError."""
|
||||
import pytest
|
||||
|
||||
bad_path = tmp_path / "nonexistent"
|
||||
with pytest.raises(ValueError, match="does not exist"):
|
||||
config.initialize(bad_path)
|
||||
|
||||
def test_initialize_with_string_path(self, tmp_path):
|
||||
"""Initialize accepts a string path and resolves it."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(str(tmp_path))
|
||||
assert config.get_project_dir() == tmp_path.resolve()
|
||||
|
||||
def test_initialize_finds_auto_claude_dir(self, tmp_path):
|
||||
"""Initialize locates the .auto-claude directory."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.get_auto_claude_dir() == ac_dir
|
||||
|
||||
def test_initialize_finds_legacy_auto_claude_dir(self, tmp_path):
|
||||
"""Initialize falls back to legacy auto-claude (no dot prefix)."""
|
||||
legacy_dir = tmp_path / "auto-claude"
|
||||
legacy_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.get_auto_claude_dir() == legacy_dir
|
||||
|
||||
def test_initialize_warns_when_no_auto_claude_dir(self, tmp_path):
|
||||
"""Initialize still works but warns when no .auto-claude dir exists."""
|
||||
config.initialize(tmp_path)
|
||||
# Should point to the expected (but missing) .auto-claude path
|
||||
assert config._auto_claude_dir == tmp_path / ".auto-claude"
|
||||
|
||||
|
||||
class TestGetProjectDir:
|
||||
"""Tests for config.get_project_dir()."""
|
||||
|
||||
def setup_method(self):
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
def test_get_project_dir_before_initialize_raises(self):
|
||||
"""get_project_dir raises RuntimeError when not initialized."""
|
||||
import pytest
|
||||
|
||||
with pytest.raises(RuntimeError, match="not initialized"):
|
||||
config.get_project_dir()
|
||||
|
||||
def test_get_project_dir_after_initialize(self, tmp_path):
|
||||
"""get_project_dir returns the resolved project path."""
|
||||
(tmp_path / ".auto-claude").mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.get_project_dir() == tmp_path.resolve()
|
||||
|
||||
|
||||
class TestGetAutoClaudeDir:
|
||||
"""Tests for config.get_auto_claude_dir()."""
|
||||
|
||||
def setup_method(self):
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
def test_get_auto_claude_dir_before_initialize_raises(self):
|
||||
"""get_auto_claude_dir raises RuntimeError when not initialized."""
|
||||
import pytest
|
||||
|
||||
with pytest.raises(RuntimeError, match="not initialized"):
|
||||
config.get_auto_claude_dir()
|
||||
|
||||
def test_get_auto_claude_dir_after_initialize(self, tmp_path):
|
||||
"""get_auto_claude_dir returns the .auto-claude path."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.get_auto_claude_dir() == ac_dir
|
||||
|
||||
|
||||
class TestGetSpecsDir:
|
||||
"""Tests for config.get_specs_dir()."""
|
||||
|
||||
def setup_method(self):
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
def test_get_specs_dir_returns_specs_subdir(self, tmp_path):
|
||||
"""get_specs_dir returns .auto-claude/specs path."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.get_specs_dir() == ac_dir / "specs"
|
||||
|
||||
|
||||
class TestGetProjectIndex:
|
||||
"""Tests for config.get_project_index()."""
|
||||
|
||||
def setup_method(self):
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
def test_get_project_index_loads_valid_json(self, tmp_path):
|
||||
"""get_project_index loads and returns valid JSON data."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
index_data = {"files": ["main.py"], "language": "python"}
|
||||
(ac_dir / "project_index.json").write_text(json.dumps(index_data))
|
||||
config.initialize(tmp_path)
|
||||
result = config.get_project_index()
|
||||
assert result == index_data
|
||||
|
||||
def test_get_project_index_returns_empty_on_missing_file(self, tmp_path):
|
||||
"""get_project_index returns empty dict when file doesn't exist."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
result = config.get_project_index()
|
||||
assert result == {}
|
||||
|
||||
def test_get_project_index_returns_empty_on_invalid_json(self, tmp_path):
|
||||
"""get_project_index returns empty dict on malformed JSON."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
(ac_dir / "project_index.json").write_text("{invalid json!!")
|
||||
config.initialize(tmp_path)
|
||||
result = config.get_project_index()
|
||||
assert result == {}
|
||||
|
||||
|
||||
class TestIsInitialized:
|
||||
"""Tests for config.is_initialized()."""
|
||||
|
||||
def setup_method(self):
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
def test_is_initialized_returns_false_before_init(self):
|
||||
"""is_initialized returns False when config hasn't been initialized."""
|
||||
assert config.is_initialized() is False
|
||||
|
||||
def test_is_initialized_returns_true_with_existing_dir(self, tmp_path):
|
||||
"""is_initialized returns True when .auto-claude directory exists."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
config.initialize(tmp_path)
|
||||
assert config.is_initialized() is True
|
||||
|
||||
def test_is_initialized_returns_false_when_dir_missing(self, tmp_path):
|
||||
"""is_initialized returns False when .auto-claude dir doesn't exist on disk."""
|
||||
config.initialize(tmp_path)
|
||||
# _auto_claude_dir is set but directory doesn't exist on disk
|
||||
assert config.is_initialized() is False
|
||||
@@ -1,390 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/services/execution_service.py
|
||||
====================================================
|
||||
|
||||
Tests the ExecutionService singleton, event parsing, build lifecycle,
|
||||
progress tracking, and log retrieval.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
import mcp_server.services.execution_service as exec_mod
|
||||
from mcp_server.services.execution_service import ExecutionService, get_execution_service
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_singleton():
|
||||
"""Reset the module-level singleton between tests."""
|
||||
exec_mod._instance = None
|
||||
yield
|
||||
exec_mod._instance = None
|
||||
|
||||
|
||||
class TestGetExecutionService:
|
||||
"""Tests for the singleton factory function."""
|
||||
|
||||
def test_returns_instance(self, tmp_path):
|
||||
"""get_execution_service creates an ExecutionService."""
|
||||
svc = get_execution_service(tmp_path)
|
||||
assert isinstance(svc, ExecutionService)
|
||||
assert svc.project_dir == tmp_path
|
||||
|
||||
def test_returns_same_instance(self, tmp_path):
|
||||
"""Repeated calls with the same path return the cached singleton."""
|
||||
svc1 = get_execution_service(tmp_path)
|
||||
svc2 = get_execution_service(tmp_path)
|
||||
assert svc1 is svc2
|
||||
|
||||
def test_recreates_on_different_project(self, tmp_path):
|
||||
"""Switching project_dir creates a fresh instance."""
|
||||
dir_a = tmp_path / "a"
|
||||
dir_b = tmp_path / "b"
|
||||
dir_a.mkdir()
|
||||
dir_b.mkdir()
|
||||
|
||||
svc_a = get_execution_service(dir_a)
|
||||
svc_b = get_execution_service(dir_b)
|
||||
assert svc_a is not svc_b
|
||||
assert svc_b.project_dir == dir_b
|
||||
|
||||
|
||||
class TestParseEvent:
|
||||
"""Tests for ExecutionService.parse_event()."""
|
||||
|
||||
def test_parses_valid_event_line(self, tmp_path):
|
||||
"""parse_event returns dict for a valid __TASK_EVENT__: JSON line."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
payload = {"type": "subtask_complete", "index": 1}
|
||||
line = f"__TASK_EVENT__:{json.dumps(payload)}"
|
||||
result = svc.parse_event(line)
|
||||
assert result == payload
|
||||
|
||||
def test_returns_none_for_non_event_line(self, tmp_path):
|
||||
"""parse_event returns None for a regular log line."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc.parse_event("INFO: Starting build...") is None
|
||||
|
||||
def test_returns_none_for_empty_string(self, tmp_path):
|
||||
"""parse_event returns None for empty input."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc.parse_event("") is None
|
||||
|
||||
def test_returns_none_for_malformed_json(self, tmp_path):
|
||||
"""parse_event returns None when JSON after prefix is invalid."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc.parse_event("__TASK_EVENT__:{not valid json}") is None
|
||||
|
||||
def test_parses_nested_json(self, tmp_path):
|
||||
"""parse_event handles nested JSON structures."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
payload = {"type": "progress", "data": {"completed": [1, 2], "total": 5}}
|
||||
line = f"__TASK_EVENT__:{json.dumps(payload)}"
|
||||
assert svc.parse_event(line) == payload
|
||||
|
||||
|
||||
class TestStopBuild:
|
||||
"""Tests for ExecutionService.stop_build()."""
|
||||
|
||||
def test_stop_unknown_spec_returns_error(self, tmp_path):
|
||||
"""stop_build returns error dict for a spec not being tracked."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.stop_build("999-missing")
|
||||
assert result["success"] is False
|
||||
assert "No build found" in result["error"]
|
||||
|
||||
def test_stop_already_finished_process(self, tmp_path):
|
||||
"""stop_build returns error when process already exited."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
mock_proc = MagicMock()
|
||||
mock_proc.returncode = 0
|
||||
svc._processes["001-feat"] = mock_proc
|
||||
result = svc.stop_build("001-feat")
|
||||
assert result["success"] is False
|
||||
assert "already finished" in result["error"]
|
||||
|
||||
def test_stop_running_process(self, tmp_path):
|
||||
"""stop_build terminates a running process and returns success."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
mock_proc = MagicMock()
|
||||
mock_proc.returncode = None # still running
|
||||
svc._processes["001-feat"] = mock_proc
|
||||
result = svc.stop_build("001-feat")
|
||||
assert result["success"] is True
|
||||
mock_proc.terminate.assert_called_once()
|
||||
|
||||
def test_stop_process_lookup_error(self, tmp_path):
|
||||
"""stop_build handles ProcessLookupError (race condition)."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
mock_proc = MagicMock()
|
||||
mock_proc.returncode = None
|
||||
mock_proc.terminate.side_effect = ProcessLookupError
|
||||
svc._processes["001-feat"] = mock_proc
|
||||
result = svc.stop_build("001-feat")
|
||||
assert result["success"] is False
|
||||
assert "already exited" in result["error"]
|
||||
|
||||
|
||||
class TestGetProgress:
|
||||
"""Tests for ExecutionService.get_progress()."""
|
||||
|
||||
def test_progress_with_running_process(self, tmp_path):
|
||||
"""get_progress reports running status and event info."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
mock_proc = MagicMock()
|
||||
mock_proc.returncode = None
|
||||
svc._processes["001"] = mock_proc
|
||||
svc._logs["001"] = ["line1", "line2"]
|
||||
svc._events["001"] = [{"type": "start"}, {"type": "subtask_complete"}]
|
||||
|
||||
result = svc.get_progress("001")
|
||||
assert result["running"] is True
|
||||
assert result["event_count"] == 2
|
||||
assert result["latest_event"] == {"type": "subtask_complete"}
|
||||
assert result["log_lines"] == 2
|
||||
|
||||
def test_progress_with_finished_process(self, tmp_path):
|
||||
"""get_progress reports finished status with exit code."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
mock_proc = MagicMock()
|
||||
mock_proc.returncode = 1
|
||||
svc._processes["001"] = mock_proc
|
||||
svc._events["001"] = []
|
||||
svc._logs["001"] = []
|
||||
|
||||
result = svc.get_progress("001")
|
||||
assert result["running"] is False
|
||||
assert result["exit_code"] == 1
|
||||
|
||||
def test_progress_falls_back_to_disk(self, tmp_path):
|
||||
"""get_progress checks disk state when no process is tracked."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
plan = {"subtasks": [{"status": "completed"}, {"status": "pending"}]}
|
||||
(specs_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("001-feat")
|
||||
assert result["running"] is False
|
||||
assert result["status"] == "building"
|
||||
assert result["subtasks_completed"] == 1
|
||||
assert result["subtasks_total"] == 2
|
||||
|
||||
def test_progress_disk_no_plan(self, tmp_path):
|
||||
"""get_progress returns no_plan when spec dir has no plan file."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "002-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("002-feat")
|
||||
assert result["status"] == "no_plan"
|
||||
|
||||
def test_progress_disk_spec_not_found(self, tmp_path):
|
||||
"""get_progress returns error when spec directory doesn't exist."""
|
||||
(tmp_path / ".auto-claude" / "specs").mkdir(parents=True)
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("999-missing")
|
||||
assert "error" in result
|
||||
|
||||
def test_progress_disk_qa_approved(self, tmp_path):
|
||||
"""get_progress identifies qa_approved status from plan."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
plan = {
|
||||
"subtasks": [{"status": "completed"}],
|
||||
"qa_signoff": {"status": "approved"},
|
||||
}
|
||||
(specs_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("001-feat")
|
||||
assert result["status"] == "qa_approved"
|
||||
|
||||
def test_progress_disk_qa_rejected(self, tmp_path):
|
||||
"""get_progress identifies qa_rejected status."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
plan = {
|
||||
"subtasks": [{"status": "completed"}],
|
||||
"qa_signoff": {"status": "rejected"},
|
||||
}
|
||||
(specs_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("001-feat")
|
||||
assert result["status"] == "qa_rejected"
|
||||
|
||||
def test_progress_disk_build_complete(self, tmp_path):
|
||||
"""get_progress shows build_complete when all subtasks done, no QA."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
plan = {"subtasks": [{"status": "completed"}, {"status": "completed"}]}
|
||||
(specs_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("001-feat")
|
||||
assert result["status"] == "build_complete"
|
||||
|
||||
def test_progress_disk_not_started(self, tmp_path):
|
||||
"""get_progress shows not_started when no subtasks completed."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
plan = {"subtasks": [{"status": "pending"}, {"status": "pending"}]}
|
||||
(specs_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("001-feat")
|
||||
assert result["status"] == "not_started"
|
||||
|
||||
def test_progress_disk_invalid_plan_json(self, tmp_path):
|
||||
"""get_progress handles corrupt plan file gracefully."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "implementation_plan.json").write_text("not json!")
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_progress("001-feat")
|
||||
assert result["status"] == "error"
|
||||
|
||||
|
||||
class TestGetLogs:
|
||||
"""Tests for ExecutionService.get_logs()."""
|
||||
|
||||
def test_get_logs_from_memory(self, tmp_path):
|
||||
"""get_logs returns in-memory log lines."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
svc._logs["001"] = [f"line-{i}" for i in range(100)]
|
||||
result = svc.get_logs("001", tail=10)
|
||||
assert result["total_lines"] == 100
|
||||
assert len(result["lines"]) == 10
|
||||
assert result["lines"][0] == "line-90"
|
||||
|
||||
def test_get_logs_falls_back_to_disk(self, tmp_path):
|
||||
"""get_logs reads disk logs when in-memory is empty."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
log_lines = [f'{{"event": {i}}}' for i in range(20)]
|
||||
(specs_dir / "task_log.jsonl").write_text("\n".join(log_lines))
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_logs("001-feat", tail=5)
|
||||
assert result["total_lines"] == 20
|
||||
assert len(result["lines"]) == 5
|
||||
|
||||
def test_get_logs_disk_no_log_file(self, tmp_path):
|
||||
"""get_logs returns empty when no log file exists on disk."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / "001-feat"
|
||||
specs_dir.mkdir(parents=True)
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_logs("001-feat")
|
||||
assert result["lines"] == []
|
||||
assert "No build logs" in result.get("message", "")
|
||||
|
||||
def test_get_logs_disk_spec_not_found(self, tmp_path):
|
||||
"""get_logs returns error when spec doesn't exist."""
|
||||
(tmp_path / ".auto-claude" / "specs").mkdir(parents=True)
|
||||
svc = ExecutionService(tmp_path)
|
||||
result = svc.get_logs("999-missing")
|
||||
assert "error" in result
|
||||
|
||||
|
||||
class TestResolveSpecDir:
|
||||
"""Tests for ExecutionService._resolve_spec_dir()."""
|
||||
|
||||
def test_exact_match(self, tmp_path):
|
||||
"""_resolve_spec_dir finds exact directory match."""
|
||||
specs_dir = tmp_path / "specs"
|
||||
target = specs_dir / "001-feature"
|
||||
target.mkdir(parents=True)
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc._resolve_spec_dir(specs_dir, "001-feature") == target
|
||||
|
||||
def test_prefix_match(self, tmp_path):
|
||||
"""_resolve_spec_dir finds directory by prefix."""
|
||||
specs_dir = tmp_path / "specs"
|
||||
target = specs_dir / "001-my-feature"
|
||||
target.mkdir(parents=True)
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc._resolve_spec_dir(specs_dir, "001") == target
|
||||
|
||||
def test_no_match(self, tmp_path):
|
||||
"""_resolve_spec_dir returns None when no directory matches."""
|
||||
specs_dir = tmp_path / "specs"
|
||||
specs_dir.mkdir()
|
||||
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc._resolve_spec_dir(specs_dir, "999") is None
|
||||
|
||||
def test_no_specs_dir(self, tmp_path):
|
||||
"""_resolve_spec_dir returns None when specs dir doesn't exist."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
assert svc._resolve_spec_dir(tmp_path / "nonexistent", "001") is None
|
||||
|
||||
|
||||
class TestStartBuild:
|
||||
"""Tests for ExecutionService.start_build()."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_build_raises_if_already_running(self, tmp_path):
|
||||
"""start_build raises RuntimeError if build already in progress."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
mock_proc = MagicMock()
|
||||
mock_proc.returncode = None # still running
|
||||
svc._processes["001"] = mock_proc
|
||||
|
||||
with pytest.raises(RuntimeError, match="already running"):
|
||||
await svc.start_build("001")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_build_raises_if_run_py_missing(self, tmp_path, monkeypatch):
|
||||
"""start_build raises FileNotFoundError when run.py not found."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
# Patch Path.exists to return False for the run.py check
|
||||
original_exists = Path.exists
|
||||
def fake_exists(self):
|
||||
if self.name == "run.py":
|
||||
return False
|
||||
return original_exists(self)
|
||||
monkeypatch.setattr(Path, "exists", fake_exists)
|
||||
|
||||
with pytest.raises(FileNotFoundError, match="run.py not found"):
|
||||
await svc.start_build("001")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_start_build_allows_restart_after_completion(self, tmp_path, monkeypatch):
|
||||
"""start_build allows restarting after a previous process completed."""
|
||||
svc = ExecutionService(tmp_path)
|
||||
old_proc = MagicMock()
|
||||
old_proc.returncode = 0 # already finished
|
||||
svc._processes["001"] = old_proc
|
||||
|
||||
# Patch run.py to not exist so we get FileNotFoundError
|
||||
# (proving it got past the "already running" check)
|
||||
original_exists = Path.exists
|
||||
def fake_exists(self):
|
||||
if self.name == "run.py":
|
||||
return False
|
||||
return original_exists(self)
|
||||
monkeypatch.setattr(Path, "exists", fake_exists)
|
||||
|
||||
with pytest.raises(FileNotFoundError, match="run.py not found"):
|
||||
await svc.start_build("001")
|
||||
@@ -1,889 +0,0 @@
|
||||
"""
|
||||
Tests for MCP Feature Services: Insights, Roadmap, Ideation, Memory
|
||||
=====================================================================
|
||||
|
||||
Combined tests for InsightsService, RoadmapService, IdeationService,
|
||||
and MemoryService (singleton pattern).
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
# Setup: add backend to path and pre-mock SDK
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.services.insights_service import InsightsService
|
||||
from mcp_server.services.roadmap_service import RoadmapService
|
||||
from mcp_server.services.ideation_service import IdeationService, VALID_IDEATION_TYPES
|
||||
import mcp_server.services.memory_service as memory_mod
|
||||
from mcp_server.services.memory_service import MemoryService, get_memory_service
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_dir(tmp_path):
|
||||
"""Create a temp project dir with .auto-claude structure."""
|
||||
ac = tmp_path / ".auto-claude"
|
||||
ac.mkdir()
|
||||
(ac / "ideation").mkdir()
|
||||
(ac / "roadmap").mkdir()
|
||||
(ac / "github").mkdir()
|
||||
return tmp_path
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_memory_singleton():
|
||||
"""Reset the MemoryService singleton between every test."""
|
||||
memory_mod._instance = None
|
||||
yield
|
||||
memory_mod._instance = None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# InsightsService Tests
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestInsightsServiceAsk:
|
||||
"""Tests for InsightsService.ask() - AI codebase Q&A."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_ask_captures_stdout(self, project_dir):
|
||||
"""ask() captures stdout from run_with_sdk and returns it."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
mock_run = AsyncMock()
|
||||
|
||||
async def _fake_run(**kwargs):
|
||||
print("The answer is 42.")
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.insights_runner": MagicMock(
|
||||
run_with_sdk=_fake_run
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.ask("What is the meaning?")
|
||||
|
||||
assert result["success"] is True
|
||||
assert "42" in result["response"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_ask_parses_task_suggestions(self, project_dir):
|
||||
"""ask() extracts __TASK_SUGGESTION__ lines from output."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
suggestion = json.dumps({"title": "Add tests", "priority": "high"})
|
||||
|
||||
async def _fake_run(**kwargs):
|
||||
print("Some response text")
|
||||
print(f"__TASK_SUGGESTION__:{suggestion}")
|
||||
print("More text")
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.insights_runner": MagicMock(
|
||||
run_with_sdk=_fake_run
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.ask("Suggest improvements")
|
||||
|
||||
assert result["success"] is True
|
||||
assert len(result["task_suggestions"]) == 1
|
||||
assert result["task_suggestions"][0]["title"] == "Add tests"
|
||||
# Task suggestion lines should not appear in response
|
||||
assert "__TASK_SUGGESTION__" not in result["response"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_ask_strips_tool_markers(self, project_dir):
|
||||
"""ask() removes __TOOL_START__ and __TOOL_END__ markers."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
async def _fake_run(**kwargs):
|
||||
print("__TOOL_START__:search")
|
||||
print("Real content here")
|
||||
print("__TOOL_END__:search")
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.insights_runner": MagicMock(
|
||||
run_with_sdk=_fake_run
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.ask("Question")
|
||||
|
||||
assert "__TOOL_START__" not in result["response"]
|
||||
assert "__TOOL_END__" not in result["response"]
|
||||
assert "Real content here" in result["response"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_ask_passes_params_correctly(self, project_dir):
|
||||
"""ask() forwards all parameters to run_with_sdk."""
|
||||
service = InsightsService(project_dir)
|
||||
captured_kwargs = {}
|
||||
|
||||
async def _fake_run(**kwargs):
|
||||
captured_kwargs.update(kwargs)
|
||||
print("ok")
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.insights_runner": MagicMock(
|
||||
run_with_sdk=_fake_run
|
||||
)
|
||||
},
|
||||
):
|
||||
await service.ask(
|
||||
"Q",
|
||||
history=[{"role": "user", "content": "hi"}],
|
||||
model="opus",
|
||||
thinking_level="high",
|
||||
)
|
||||
|
||||
assert captured_kwargs["project_dir"] == str(project_dir)
|
||||
assert captured_kwargs["message"] == "Q"
|
||||
assert captured_kwargs["model"] == "opus"
|
||||
assert captured_kwargs["thinking_level"] == "high"
|
||||
assert len(captured_kwargs["history"]) == 1
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_ask_import_error(self, project_dir):
|
||||
"""When insights runner is unavailable, returns error."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
with patch.dict("sys.modules", {"runners.insights_runner": None}):
|
||||
result = await service.ask("Question")
|
||||
|
||||
assert "error" in result
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_ask_exception(self, project_dir):
|
||||
"""When run_with_sdk raises, returns error."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
async def _boom(**kwargs):
|
||||
raise RuntimeError("Model overloaded")
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.insights_runner": MagicMock(
|
||||
run_with_sdk=_boom
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.ask("Question")
|
||||
|
||||
assert "error" in result
|
||||
assert "Model overloaded" in result["error"]
|
||||
|
||||
|
||||
class TestInsightsServiceSuggestTasks:
|
||||
"""Tests for InsightsService.suggest_tasks() - reads ideation data."""
|
||||
|
||||
def test_suggest_tasks_with_ideation_data(self, project_dir):
|
||||
"""When ideation.json exists, returns task suggestions."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
ideation_data = {
|
||||
"ideas": [
|
||||
{
|
||||
"title": "Add caching",
|
||||
"description": "Implement Redis cache",
|
||||
"type": "performance",
|
||||
"impact": "high",
|
||||
"effort": "medium",
|
||||
},
|
||||
{
|
||||
"title": "Fix typos",
|
||||
"description": "Fix documentation typos",
|
||||
"type": "docs",
|
||||
"impact": "low",
|
||||
"effort": "low",
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text(json.dumps(ideation_data))
|
||||
|
||||
result = service.suggest_tasks()
|
||||
|
||||
assert result["success"] is True
|
||||
assert len(result["suggestions"]) == 2
|
||||
assert result["suggestions"][0]["title"] == "Add caching"
|
||||
assert result["suggestions"][0]["category"] == "performance"
|
||||
assert result["suggestions"][1]["impact"] == "low"
|
||||
|
||||
def test_suggest_tasks_no_ideation_data(self, project_dir):
|
||||
"""When ideation.json doesn't exist, returns empty with message."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
result = service.suggest_tasks()
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["suggestions"] == []
|
||||
assert "No ideation data" in result["message"]
|
||||
|
||||
def test_suggest_tasks_limits_to_10(self, project_dir):
|
||||
"""At most 10 suggestions are returned."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
ideation_data = {
|
||||
"ideas": [
|
||||
{"title": f"Idea {i}", "description": f"Desc {i}"}
|
||||
for i in range(20)
|
||||
]
|
||||
}
|
||||
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text(json.dumps(ideation_data))
|
||||
|
||||
result = service.suggest_tasks()
|
||||
|
||||
assert result["success"] is True
|
||||
assert len(result["suggestions"]) == 10
|
||||
|
||||
def test_suggest_tasks_corrupt_json(self, project_dir):
|
||||
"""When ideation.json is corrupt, returns error."""
|
||||
service = InsightsService(project_dir)
|
||||
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text("not valid json{{{")
|
||||
|
||||
result = service.suggest_tasks()
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# RoadmapService Tests
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestRoadmapServiceGetRoadmap:
|
||||
"""Tests for RoadmapService.get_roadmap() - reads roadmap from disk."""
|
||||
|
||||
def test_get_roadmap_exists(self, project_dir):
|
||||
"""When roadmap.json exists, returns it."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
roadmap_data = {
|
||||
"vision": "World domination",
|
||||
"phases": [{"name": "Phase 1", "features": []}],
|
||||
}
|
||||
|
||||
roadmap_file = project_dir / ".auto-claude" / "roadmap" / "roadmap.json"
|
||||
roadmap_file.write_text(json.dumps(roadmap_data))
|
||||
|
||||
result = service.get_roadmap()
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["data"]["vision"] == "World domination"
|
||||
|
||||
def test_get_roadmap_not_found(self, project_dir):
|
||||
"""When roadmap.json does not exist, returns None with message."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
result = service.get_roadmap()
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["data"] is None
|
||||
assert "No roadmap generated" in result["message"]
|
||||
|
||||
def test_get_roadmap_corrupt_json(self, project_dir):
|
||||
"""When roadmap.json is corrupt, returns error."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
roadmap_file = project_dir / ".auto-claude" / "roadmap" / "roadmap.json"
|
||||
roadmap_file.write_text("{bad json")
|
||||
|
||||
result = service.get_roadmap()
|
||||
|
||||
assert "error" in result
|
||||
assert "Failed to load roadmap" in result["error"]
|
||||
|
||||
|
||||
class TestRoadmapServiceGenerate:
|
||||
"""Tests for RoadmapService.generate() - wraps RoadmapOrchestrator."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_success(self, project_dir):
|
||||
"""Successful generation calls orchestrator and returns roadmap."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(return_value=True)
|
||||
|
||||
# Write roadmap data that get_roadmap() will read after generation
|
||||
roadmap_data = {"vision": "Built", "phases": []}
|
||||
roadmap_file = project_dir / ".auto-claude" / "roadmap" / "roadmap.json"
|
||||
roadmap_file.write_text(json.dumps(roadmap_data))
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.roadmap.orchestrator": MagicMock(
|
||||
RoadmapOrchestrator=MagicMock(return_value=mock_orchestrator)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.generate(refresh=True, model="opus")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["data"]["vision"] == "Built"
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_failure(self, project_dir):
|
||||
"""When orchestrator returns False, returns error."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(return_value=False)
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.roadmap.orchestrator": MagicMock(
|
||||
RoadmapOrchestrator=MagicMock(return_value=mock_orchestrator)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.generate()
|
||||
|
||||
assert "error" in result
|
||||
assert "failed" in result["error"].lower()
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_import_error(self, project_dir):
|
||||
"""When roadmap module is unavailable, returns error."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
with patch.dict("sys.modules", {"runners.roadmap.orchestrator": None}):
|
||||
result = await service.generate()
|
||||
|
||||
assert "error" in result
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_exception(self, project_dir):
|
||||
"""When generate raises, returns error."""
|
||||
service = RoadmapService(project_dir)
|
||||
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(side_effect=RuntimeError("boom"))
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.roadmap.orchestrator": MagicMock(
|
||||
RoadmapOrchestrator=MagicMock(return_value=mock_orchestrator)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.generate()
|
||||
|
||||
assert "error" in result
|
||||
assert "boom" in result["error"]
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# IdeationService Tests
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestIdeationServiceGetIdeation:
|
||||
"""Tests for IdeationService.get_ideation() - reads from disk."""
|
||||
|
||||
def test_get_ideation_exists(self, project_dir):
|
||||
"""When ideation.json exists, returns it."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
ideation_data = {
|
||||
"ideas": [{"title": "Cache everything", "type": "performance"}]
|
||||
}
|
||||
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text(json.dumps(ideation_data))
|
||||
|
||||
result = service.get_ideation()
|
||||
|
||||
assert result["success"] is True
|
||||
assert len(result["data"]["ideas"]) == 1
|
||||
|
||||
def test_get_ideation_not_found(self, project_dir):
|
||||
"""When ideation.json does not exist, returns None with message."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
result = service.get_ideation()
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["data"] is None
|
||||
assert "No ideation data" in result["message"]
|
||||
|
||||
def test_get_ideation_corrupt_json(self, project_dir):
|
||||
"""When ideation.json is corrupt, returns error."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text("{{bad")
|
||||
|
||||
result = service.get_ideation()
|
||||
|
||||
assert "error" in result
|
||||
assert "Failed to load" in result["error"]
|
||||
|
||||
|
||||
class TestIdeationServiceGenerate:
|
||||
"""Tests for IdeationService.generate() - wraps IdeationOrchestrator."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_success(self, project_dir):
|
||||
"""Successful generation calls orchestrator and returns ideation."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(return_value=True)
|
||||
|
||||
# Write data that get_ideation will read
|
||||
ideation_data = {"ideas": [{"title": "Idea 1"}]}
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text(json.dumps(ideation_data))
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"ideation": MagicMock(
|
||||
IdeationOrchestrator=MagicMock(
|
||||
return_value=mock_orchestrator
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.generate(
|
||||
types=["low_hanging_fruit"], refresh=True, model="opus"
|
||||
)
|
||||
|
||||
assert result["success"] is True
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_invalid_types(self, project_dir):
|
||||
"""Invalid ideation types return error before calling orchestrator."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
# Mock the import so we get past it to the validation logic
|
||||
mock_module = MagicMock()
|
||||
with patch.dict("sys.modules", {"ideation": mock_module}):
|
||||
result = await service.generate(types=["invalid_type", "also_bad"])
|
||||
|
||||
assert "error" in result
|
||||
assert "Invalid ideation types" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_defaults_to_all_types(self, project_dir):
|
||||
"""When types is None, all valid types are used."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
captured_types = []
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(return_value=True)
|
||||
|
||||
# Write data for get_ideation
|
||||
ideation_file = project_dir / ".auto-claude" / "ideation" / "ideation.json"
|
||||
ideation_file.write_text(json.dumps({"ideas": []}))
|
||||
|
||||
mock_cls = MagicMock(return_value=mock_orchestrator)
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"ideation": MagicMock(IdeationOrchestrator=mock_cls)},
|
||||
):
|
||||
await service.generate(types=None)
|
||||
|
||||
# Verify all valid types were passed
|
||||
call_kwargs = mock_cls.call_args[1]
|
||||
assert call_kwargs["enabled_types"] == VALID_IDEATION_TYPES
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_orchestrator_failure(self, project_dir):
|
||||
"""When orchestrator returns False, returns error."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(return_value=False)
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"ideation": MagicMock(
|
||||
IdeationOrchestrator=MagicMock(
|
||||
return_value=mock_orchestrator
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.generate()
|
||||
|
||||
assert "error" in result
|
||||
assert "failed" in result["error"].lower()
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_import_error(self, project_dir):
|
||||
"""When ideation module is unavailable, returns error."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
with patch.dict("sys.modules", {"ideation": None}):
|
||||
result = await service.generate()
|
||||
|
||||
assert "error" in result
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_generate_exception(self, project_dir):
|
||||
"""When generate raises, returns error."""
|
||||
service = IdeationService(project_dir)
|
||||
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_orchestrator.run = AsyncMock(side_effect=RuntimeError("boom"))
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"ideation": MagicMock(
|
||||
IdeationOrchestrator=MagicMock(
|
||||
return_value=mock_orchestrator
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.generate()
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# MemoryService Tests
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestMemoryServiceSingleton:
|
||||
"""Tests for the get_memory_service() singleton factory."""
|
||||
|
||||
def test_returns_same_instance(self, project_dir):
|
||||
"""get_memory_service returns the same instance for same dir."""
|
||||
svc1 = get_memory_service(project_dir)
|
||||
svc2 = get_memory_service(project_dir)
|
||||
assert svc1 is svc2
|
||||
|
||||
def test_creates_new_instance_for_different_dir(self, tmp_path):
|
||||
"""get_memory_service creates new instance when dir changes."""
|
||||
dir1 = tmp_path / "proj1"
|
||||
dir1.mkdir()
|
||||
dir2 = tmp_path / "proj2"
|
||||
dir2.mkdir()
|
||||
|
||||
svc1 = get_memory_service(dir1)
|
||||
svc2 = get_memory_service(dir2)
|
||||
assert svc1 is not svc2
|
||||
|
||||
def test_singleton_reset_between_tests(self):
|
||||
"""Verify autouse fixture resets the singleton."""
|
||||
assert memory_mod._instance is None
|
||||
|
||||
|
||||
class TestMemoryServiceDisabled:
|
||||
"""Tests for MemoryService when GRAPHITI_ENABLED is not set."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_search_disabled(self, project_dir):
|
||||
"""search() returns disabled message when Graphiti is off."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
result = await service.search("test query")
|
||||
|
||||
assert "error" in result
|
||||
assert "not enabled" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_add_episode_disabled(self, project_dir):
|
||||
"""add_episode() returns disabled message when Graphiti is off."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
result = await service.add_episode("some fact")
|
||||
|
||||
assert "error" in result
|
||||
assert "not enabled" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_get_recent_disabled(self, project_dir):
|
||||
"""get_recent() returns disabled message when Graphiti is off."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
result = await service.get_recent()
|
||||
|
||||
assert "error" in result
|
||||
assert "not enabled" in result["error"]
|
||||
|
||||
|
||||
class TestMemoryServiceEnabled:
|
||||
"""Tests for MemoryService when GRAPHITI_ENABLED=true."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_search_calls_get_relevant_context(self, project_dir):
|
||||
"""search() calls the public get_relevant_context() method."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.get_relevant_context = AsyncMock(
|
||||
return_value=["result1", "result2"]
|
||||
)
|
||||
mock_memory.initialize = AsyncMock(return_value=True)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
result = await service.search("test query", limit=5)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["count"] == 2
|
||||
mock_memory.get_relevant_context.assert_awaited_once_with(
|
||||
query="test query", num_results=5
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_search_caches_memory_instance(self, project_dir):
|
||||
"""_get_memory() caches the memory instance after first call."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.get_relevant_context = AsyncMock(return_value=[])
|
||||
mock_memory.initialize = AsyncMock(return_value=True)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
await service.search("first")
|
||||
await service.search("second")
|
||||
|
||||
# GraphitiMemory should be instantiated only once
|
||||
assert mock_graphiti_mod.GraphitiMemory.call_count == 1
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_add_episode_calls_save_session_insights(self, project_dir):
|
||||
"""add_episode() calls memory.save_session_insights."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.save_session_insights = AsyncMock(return_value=True)
|
||||
mock_memory.initialize = AsyncMock(return_value=True)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
result = await service.add_episode("Important fact", source="test")
|
||||
|
||||
assert result["success"] is True
|
||||
mock_memory.save_session_insights.assert_awaited_once()
|
||||
call_kwargs = mock_memory.save_session_insights.call_args[1]
|
||||
assert call_kwargs["session_num"] == 0
|
||||
assert call_kwargs["insights"]["content"] == "Important fact"
|
||||
assert call_kwargs["insights"]["source"] == "test"
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_add_episode_failure(self, project_dir):
|
||||
"""add_episode() returns error when save fails."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.save_session_insights = AsyncMock(return_value=False)
|
||||
mock_memory.initialize = AsyncMock(return_value=True)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
result = await service.add_episode("fact")
|
||||
|
||||
assert "error" in result
|
||||
assert "Failed to save" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_get_recent_uses_broad_search(self, project_dir):
|
||||
"""get_recent() uses get_relevant_context with a broad query."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.get_relevant_context = AsyncMock(return_value=["entry1"])
|
||||
mock_memory.initialize = AsyncMock(return_value=True)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
result = await service.get_recent(limit=5)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["count"] == 1
|
||||
mock_memory.get_relevant_context.assert_awaited_once_with(
|
||||
query="recent project activity and insights", num_results=5
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_get_memory_returns_none_on_init_failure(self, project_dir):
|
||||
"""_get_memory() returns None when initialize fails."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.initialize = AsyncMock(return_value=False)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
result = await service.search("query")
|
||||
|
||||
assert "error" in result
|
||||
assert "Could not initialize" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_get_memory_import_error(self, project_dir):
|
||||
"""_get_memory() returns None when graphiti import fails."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules", {"integrations.graphiti.memory": None}
|
||||
):
|
||||
result = await service.search("query")
|
||||
|
||||
assert "error" in result
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_search_exception(self, project_dir):
|
||||
"""search() returns error when memory search raises."""
|
||||
service = MemoryService(project_dir)
|
||||
|
||||
mock_memory = MagicMock()
|
||||
mock_memory.get_relevant_context = AsyncMock(
|
||||
side_effect=RuntimeError("connection lost")
|
||||
)
|
||||
mock_memory.initialize = AsyncMock(return_value=True)
|
||||
|
||||
mock_graphiti_mod = MagicMock()
|
||||
mock_graphiti_mod.GraphitiMemory = MagicMock(return_value=mock_memory)
|
||||
mock_graphiti_mod.GroupIdMode = MagicMock()
|
||||
mock_graphiti_mod.GroupIdMode.PROJECT = "project"
|
||||
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{"integrations.graphiti.memory": mock_graphiti_mod},
|
||||
):
|
||||
result = await service.search("query")
|
||||
|
||||
assert "error" in result
|
||||
assert "connection lost" in result["error"]
|
||||
|
||||
|
||||
class TestMemoryServiceIsGraphitiEnabled:
|
||||
"""Tests for the _is_graphiti_enabled() helper."""
|
||||
|
||||
def test_enabled_true(self):
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "true"}):
|
||||
assert memory_mod._is_graphiti_enabled() is True
|
||||
|
||||
def test_enabled_one(self):
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "1"}):
|
||||
assert memory_mod._is_graphiti_enabled() is True
|
||||
|
||||
def test_enabled_TRUE_uppercase(self):
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "TRUE"}):
|
||||
assert memory_mod._is_graphiti_enabled() is True
|
||||
|
||||
def test_disabled_false(self):
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": "false"}):
|
||||
assert memory_mod._is_graphiti_enabled() is False
|
||||
|
||||
def test_disabled_empty(self):
|
||||
with patch.dict("os.environ", {"GRAPHITI_ENABLED": ""}):
|
||||
assert memory_mod._is_graphiti_enabled() is False
|
||||
|
||||
def test_disabled_not_set(self):
|
||||
with patch.dict("os.environ", {}, clear=True):
|
||||
assert memory_mod._is_graphiti_enabled() is False
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# Valid Ideation Types Constant
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestIdeationConstants:
|
||||
"""Test that VALID_IDEATION_TYPES is correctly defined."""
|
||||
|
||||
def test_valid_types_list(self):
|
||||
assert "low_hanging_fruit" in VALID_IDEATION_TYPES
|
||||
assert "ui_ux_improvements" in VALID_IDEATION_TYPES
|
||||
assert "high_value_features" in VALID_IDEATION_TYPES
|
||||
assert len(VALID_IDEATION_TYPES) == 3
|
||||
@@ -1,467 +0,0 @@
|
||||
"""
|
||||
Tests for MCP GitHub Service and Tools
|
||||
=======================================
|
||||
|
||||
Tests GitHubService (repo detection, issue listing, PR review, triage, auto-fix)
|
||||
and the github tool wrappers.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
# Setup: add backend to path and pre-mock SDK
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.services.github_service import GitHubService
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fixtures
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def project_dir(tmp_path):
|
||||
"""Create a temp project dir with .auto-claude/github structure."""
|
||||
github_dir = tmp_path / ".auto-claude" / "github"
|
||||
github_dir.mkdir(parents=True)
|
||||
return tmp_path
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def service(project_dir):
|
||||
return GitHubService(project_dir)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _detect_repo
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestDetectRepo:
|
||||
"""Tests for _detect_repo() - parses git remote to owner/repo."""
|
||||
|
||||
def test_ssh_remote(self, service):
|
||||
"""SSH remote git@github.com:owner/repo.git is parsed correctly."""
|
||||
mock_result = MagicMock(returncode=0, stdout="git@github.com:owner/repo.git\n")
|
||||
with patch("subprocess.run", return_value=mock_result) as mock_run:
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result == "owner/repo"
|
||||
mock_run.assert_called_once()
|
||||
args = mock_run.call_args
|
||||
assert args[0][0] == ["git", "remote", "get-url", "origin"]
|
||||
|
||||
def test_ssh_remote_no_dot_git(self, service):
|
||||
"""SSH remote without .git suffix works."""
|
||||
mock_result = MagicMock(returncode=0, stdout="git@github.com:owner/repo\n")
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result == "owner/repo"
|
||||
|
||||
def test_https_remote(self, service):
|
||||
"""HTTPS remote https://github.com/owner/repo.git is parsed correctly."""
|
||||
mock_result = MagicMock(
|
||||
returncode=0, stdout="https://github.com/owner/repo.git\n"
|
||||
)
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result == "owner/repo"
|
||||
|
||||
def test_https_remote_no_dot_git(self, service):
|
||||
"""HTTPS remote without .git suffix works."""
|
||||
mock_result = MagicMock(
|
||||
returncode=0, stdout="https://github.com/owner/repo\n"
|
||||
)
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result == "owner/repo"
|
||||
|
||||
def test_non_github_remote_returns_none(self, service):
|
||||
"""Non-GitHub remote (e.g. GitLab) returns None."""
|
||||
mock_result = MagicMock(
|
||||
returncode=0, stdout="https://gitlab.com/owner/repo.git\n"
|
||||
)
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_git_command_fails_returns_none(self, service):
|
||||
"""If git command returns non-zero, returns None."""
|
||||
mock_result = MagicMock(returncode=1, stdout="", stderr="not a git repo")
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_exception_returns_none(self, service):
|
||||
"""If subprocess raises, returns None."""
|
||||
with patch("subprocess.run", side_effect=OSError("git not found")):
|
||||
result = service._detect_repo()
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _get_repo
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetRepo:
|
||||
"""Tests for _get_repo() - resolves repo from explicit arg or auto-detect."""
|
||||
|
||||
def test_explicit_repo_returned_directly(self, service):
|
||||
"""When repo is provided, it is returned without auto-detection."""
|
||||
result = service._get_repo("my-org/my-repo")
|
||||
assert result == "my-org/my-repo"
|
||||
|
||||
def test_auto_detects_when_no_repo_provided(self, service):
|
||||
"""When repo is None, falls back to _detect_repo."""
|
||||
with patch.object(service, "_detect_repo", return_value="detected/repo"):
|
||||
result = service._get_repo(None)
|
||||
|
||||
assert result == "detected/repo"
|
||||
|
||||
def test_raises_when_no_repo_and_no_detection(self, service):
|
||||
"""When repo is None and detection fails, raises ValueError."""
|
||||
with patch.object(service, "_detect_repo", return_value=None):
|
||||
with pytest.raises(ValueError, match="Could not detect repository"):
|
||||
service._get_repo(None)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# list_issues
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestListIssues:
|
||||
"""Tests for list_issues() - calls gh CLI to list GitHub issues."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_list_issues_success(self, service):
|
||||
"""Successful gh issue list returns parsed JSON."""
|
||||
issues_json = '[{"number": 1, "title": "Bug"}]'
|
||||
mock_result = MagicMock(returncode=0, stdout=issues_json)
|
||||
with patch.object(service, "_get_repo", return_value="owner/repo"):
|
||||
with patch("subprocess.run", return_value=mock_result) as mock_run:
|
||||
result = await service.list_issues(state="open", limit=10)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["count"] == 1
|
||||
assert result["issues"][0]["number"] == 1
|
||||
|
||||
# Verify gh CLI was called with correct args
|
||||
cmd = mock_run.call_args[0][0]
|
||||
assert "gh" in cmd
|
||||
assert "issue" in cmd
|
||||
assert "list" in cmd
|
||||
assert "--repo" in cmd
|
||||
assert "owner/repo" in cmd
|
||||
assert "--state" in cmd
|
||||
assert "open" in cmd
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_list_issues_gh_failure(self, service):
|
||||
"""When gh CLI fails, returns error dict."""
|
||||
mock_result = MagicMock(
|
||||
returncode=1, stdout="", stderr="authentication required"
|
||||
)
|
||||
with patch.object(service, "_get_repo", return_value="owner/repo"):
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
result = await service.list_issues()
|
||||
|
||||
assert "error" in result
|
||||
assert "gh CLI failed" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_list_issues_exception(self, service):
|
||||
"""When an exception occurs, returns error dict."""
|
||||
with patch.object(
|
||||
service, "_get_repo", side_effect=ValueError("no repo")
|
||||
):
|
||||
result = await service.list_issues()
|
||||
|
||||
assert "error" in result
|
||||
assert "no repo" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_list_issues_with_explicit_repo(self, service):
|
||||
"""Repo argument is forwarded to _get_repo."""
|
||||
issues_json = "[]"
|
||||
mock_result = MagicMock(returncode=0, stdout=issues_json)
|
||||
with patch.object(
|
||||
service, "_get_repo", return_value="explicit/repo"
|
||||
) as mock_get:
|
||||
with patch("subprocess.run", return_value=mock_result):
|
||||
await service.list_issues(repo="explicit/repo")
|
||||
|
||||
mock_get.assert_called_once_with("explicit/repo")
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# review_pr
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestReviewPR:
|
||||
"""Tests for review_pr() - wraps GitHubOrchestrator.review_pr."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_review_pr_success(self, service):
|
||||
"""Successful review returns serialized result."""
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_result = MagicMock()
|
||||
mock_result.to_dict.return_value = {"verdict": "approve", "findings": []}
|
||||
mock_orchestrator.review_pr = AsyncMock(return_value=mock_result)
|
||||
|
||||
with patch.object(service, "_get_repo", return_value="owner/repo"):
|
||||
with patch.object(service, "_create_config") as mock_config:
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.orchestrator": MagicMock(
|
||||
GitHubOrchestrator=MagicMock(
|
||||
return_value=mock_orchestrator
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.review_pr(42)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["data"]["verdict"] == "approve"
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_review_pr_import_error(self, service):
|
||||
"""When GitHubOrchestrator is not available, returns error."""
|
||||
with patch.object(service, "_get_repo", return_value="owner/repo"):
|
||||
with patch.dict("sys.modules", {"runners.github.orchestrator": None}):
|
||||
result = await service.review_pr(42)
|
||||
|
||||
assert "error" in result
|
||||
assert "not available" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_review_pr_get_repo_error(self, service):
|
||||
"""When _get_repo raises inside the method, returns error dict."""
|
||||
# The import of GitHubOrchestrator happens first in the try block,
|
||||
# so we need to provide a valid module, then let _get_repo fail.
|
||||
mock_orch_mod = MagicMock()
|
||||
|
||||
with patch.dict("sys.modules", {"runners.github.orchestrator": mock_orch_mod}):
|
||||
with patch.object(
|
||||
service, "_get_repo", side_effect=ValueError("no repo")
|
||||
):
|
||||
result = await service.review_pr(42)
|
||||
|
||||
assert "error" in result
|
||||
assert "no repo" in result["error"]
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# get_review
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestGetReview:
|
||||
"""Tests for get_review() - loads saved review from disk."""
|
||||
|
||||
def test_get_review_found(self, service):
|
||||
"""When a review exists on disk, returns it."""
|
||||
mock_result = MagicMock()
|
||||
mock_result.to_dict.return_value = {"verdict": "approve"}
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.models": MagicMock(
|
||||
PRReviewResult=MagicMock(
|
||||
load=MagicMock(return_value=mock_result)
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = service.get_review(42)
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["data"]["verdict"] == "approve"
|
||||
|
||||
def test_get_review_not_found(self, service):
|
||||
"""When no review exists, returns error."""
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.models": MagicMock(
|
||||
PRReviewResult=MagicMock(
|
||||
load=MagicMock(return_value=None)
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = service.get_review(42)
|
||||
|
||||
assert "error" in result
|
||||
assert "No review found" in result["error"]
|
||||
|
||||
def test_get_review_import_error(self, service):
|
||||
"""When models module is unavailable, returns error."""
|
||||
with patch.dict("sys.modules", {"runners.github.models": None}):
|
||||
result = service.get_review(42)
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# auto_fix_issue
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestAutoFixIssue:
|
||||
"""Tests for auto_fix_issue() - wraps GitHubOrchestrator.auto_fix_issue."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_auto_fix_success(self, service):
|
||||
"""Successful auto-fix returns serialized state."""
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_state = MagicMock()
|
||||
mock_state.to_dict.return_value = {"status": "completed"}
|
||||
mock_orchestrator.auto_fix_issue = AsyncMock(return_value=mock_state)
|
||||
|
||||
with patch.object(service, "_get_repo", return_value="owner/repo"):
|
||||
with patch.object(service, "_create_config") as mock_config:
|
||||
mock_config.return_value = MagicMock()
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.orchestrator": MagicMock(
|
||||
GitHubOrchestrator=MagicMock(
|
||||
return_value=mock_orchestrator
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.auto_fix_issue(10)
|
||||
|
||||
assert result["success"] is True
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_auto_fix_exception(self, service):
|
||||
"""When auto_fix_issue raises, returns error."""
|
||||
with patch.object(
|
||||
service, "_get_repo", side_effect=ValueError("fail")
|
||||
):
|
||||
result = await service.auto_fix_issue(10)
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# triage_issues
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestTriageIssues:
|
||||
"""Tests for triage_issues() - wraps GitHubOrchestrator.triage_issues."""
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_triage_success(self, service):
|
||||
"""Successful triage returns list of serialized results."""
|
||||
mock_orchestrator = MagicMock()
|
||||
mock_triage_result = MagicMock()
|
||||
mock_triage_result.to_dict.return_value = {"issue": 1, "category": "bug"}
|
||||
mock_orchestrator.triage_issues = AsyncMock(
|
||||
return_value=[mock_triage_result]
|
||||
)
|
||||
|
||||
with patch.object(service, "_get_repo", return_value="owner/repo"):
|
||||
with patch.object(service, "_create_config") as mock_config:
|
||||
mock_config.return_value = MagicMock()
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.orchestrator": MagicMock(
|
||||
GitHubOrchestrator=MagicMock(
|
||||
return_value=mock_orchestrator
|
||||
)
|
||||
)
|
||||
},
|
||||
):
|
||||
result = await service.triage_issues([1, 2])
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["count"] == 1
|
||||
|
||||
@pytest.mark.asyncio(loop_scope="function")
|
||||
async def test_triage_exception(self, service):
|
||||
"""When triage raises, returns error dict."""
|
||||
with patch.object(
|
||||
service, "_get_repo", side_effect=ValueError("fail")
|
||||
):
|
||||
result = await service.triage_issues([1])
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# _create_config
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
class TestCreateConfig:
|
||||
"""Tests for _create_config() - builds GitHubRunnerConfig."""
|
||||
|
||||
def test_creates_config_with_env_token(self, service):
|
||||
"""Uses GITHUB_TOKEN from environment if set."""
|
||||
mock_config_cls = MagicMock()
|
||||
mock_config_cls.return_value = MagicMock()
|
||||
|
||||
with patch.dict("os.environ", {"GITHUB_TOKEN": "test-token"}):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.models": MagicMock(
|
||||
GitHubRunnerConfig=mock_config_cls
|
||||
)
|
||||
},
|
||||
):
|
||||
config = service._create_config("owner/repo", "sonnet")
|
||||
|
||||
mock_config_cls.assert_called_once()
|
||||
call_kwargs = mock_config_cls.call_args[1]
|
||||
assert call_kwargs["token"] == "test-token"
|
||||
assert call_kwargs["repo"] == "owner/repo"
|
||||
assert call_kwargs["model"] == "sonnet"
|
||||
|
||||
def test_falls_back_to_gh_cli_token(self, service):
|
||||
"""When GITHUB_TOKEN is empty, tries gh auth token."""
|
||||
mock_config_cls = MagicMock()
|
||||
mock_config_cls.return_value = MagicMock()
|
||||
mock_gh_result = MagicMock(returncode=0, stdout="gh-cli-token\n")
|
||||
|
||||
with patch.dict("os.environ", {"GITHUB_TOKEN": ""}, clear=False):
|
||||
with patch("subprocess.run", return_value=mock_gh_result):
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"runners.github.models": MagicMock(
|
||||
GitHubRunnerConfig=mock_config_cls
|
||||
)
|
||||
},
|
||||
):
|
||||
config = service._create_config("owner/repo")
|
||||
|
||||
call_kwargs = mock_config_cls.call_args[1]
|
||||
assert call_kwargs["token"] == "gh-cli-token"
|
||||
@@ -1,425 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/operations.py
|
||||
===================================
|
||||
|
||||
Tests the OperationTracker for creating, updating, cancelling,
|
||||
listing, and cleaning up long-running operations.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
from mcp_server.operations import Operation, OperationStatus, OperationTracker
|
||||
|
||||
|
||||
class TestOperationCreation:
|
||||
"""Tests for Operation dataclass and OperationTracker.create()."""
|
||||
|
||||
def test_create_returns_operation_with_unique_id(self):
|
||||
"""create() returns an Operation with a unique UUID."""
|
||||
tracker = OperationTracker()
|
||||
op1 = tracker.create("spec_create")
|
||||
op2 = tracker.create("build")
|
||||
assert op1.id != op2.id
|
||||
assert len(op1.id) == 36 # UUID format
|
||||
|
||||
def test_create_sets_pending_status(self):
|
||||
"""create() initializes operation with PENDING status."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("spec_create")
|
||||
assert op.status == OperationStatus.PENDING
|
||||
|
||||
def test_create_sets_operation_type(self):
|
||||
"""create() records the operation type."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("qa_review")
|
||||
assert op.type == "qa_review"
|
||||
|
||||
def test_create_uses_default_message(self):
|
||||
"""create() generates a default message from the operation type."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
assert op.message == "Starting build..."
|
||||
|
||||
def test_create_uses_custom_message(self):
|
||||
"""create() uses a custom message when provided."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("spec_create", message="Creating spec for feature X")
|
||||
assert op.message == "Creating spec for feature X"
|
||||
|
||||
def test_create_stores_operation(self):
|
||||
"""create() stores the operation so get() can find it."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
assert tracker.get(op.id) is op
|
||||
|
||||
def test_create_sets_timestamps(self):
|
||||
"""create() records created_at and updated_at timestamps."""
|
||||
tracker = OperationTracker()
|
||||
before = time.time()
|
||||
op = tracker.create("build")
|
||||
after = time.time()
|
||||
assert before <= op.created_at <= after
|
||||
assert before <= op.updated_at <= after
|
||||
|
||||
|
||||
class TestOperationGet:
|
||||
"""Tests for OperationTracker.get()."""
|
||||
|
||||
def test_get_returns_none_for_unknown_id(self):
|
||||
"""get() returns None for a nonexistent operation ID."""
|
||||
tracker = OperationTracker()
|
||||
assert tracker.get("nonexistent-id") is None
|
||||
|
||||
def test_get_returns_operation_by_id(self):
|
||||
"""get() returns the correct operation."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
result = tracker.get(op.id)
|
||||
assert result is op
|
||||
assert result.type == "build"
|
||||
|
||||
|
||||
class TestOperationUpdate:
|
||||
"""Tests for OperationTracker.update()."""
|
||||
|
||||
def test_update_modifies_status(self):
|
||||
"""update() changes the operation status."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, status=OperationStatus.RUNNING)
|
||||
assert op.status == OperationStatus.RUNNING
|
||||
|
||||
def test_update_modifies_progress(self):
|
||||
"""update() changes the progress value."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, progress=50)
|
||||
assert op.progress == 50
|
||||
|
||||
def test_update_clamps_progress_to_zero(self):
|
||||
"""update() clamps negative progress to 0."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, progress=-10)
|
||||
assert op.progress == 0
|
||||
|
||||
def test_update_clamps_progress_to_hundred(self):
|
||||
"""update() clamps progress above 100 to 100."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, progress=150)
|
||||
assert op.progress == 100
|
||||
|
||||
def test_update_modifies_message(self):
|
||||
"""update() changes the message."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, message="Building subtask 3/5...")
|
||||
assert op.message == "Building subtask 3/5..."
|
||||
|
||||
def test_update_sets_result(self):
|
||||
"""update() stores the result payload."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
result_data = {"files_changed": 5, "tests_passed": True}
|
||||
tracker.update(op.id, result=result_data)
|
||||
assert op.result == result_data
|
||||
|
||||
def test_update_sets_error(self):
|
||||
"""update() stores an error message."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, error="Build failed: syntax error in main.py")
|
||||
assert op.error == "Build failed: syntax error in main.py"
|
||||
|
||||
def test_update_updates_timestamp(self):
|
||||
"""update() refreshes the updated_at timestamp."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
old_time = op.updated_at
|
||||
time.sleep(0.01)
|
||||
tracker.update(op.id, progress=10)
|
||||
assert op.updated_at > old_time
|
||||
|
||||
def test_update_returns_operation(self):
|
||||
"""update() returns the updated operation."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
result = tracker.update(op.id, status=OperationStatus.RUNNING)
|
||||
assert result is op
|
||||
|
||||
def test_update_returns_none_for_unknown_id(self):
|
||||
"""update() returns None for a nonexistent operation."""
|
||||
tracker = OperationTracker()
|
||||
result = tracker.update("nonexistent", progress=50)
|
||||
assert result is None
|
||||
|
||||
def test_update_multiple_fields_at_once(self):
|
||||
"""update() can modify multiple fields in a single call."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=75,
|
||||
message="Almost done",
|
||||
)
|
||||
assert op.status == OperationStatus.RUNNING
|
||||
assert op.progress == 75
|
||||
assert op.message == "Almost done"
|
||||
|
||||
|
||||
class TestOperationCancel:
|
||||
"""Tests for OperationTracker.cancel()."""
|
||||
|
||||
def test_cancel_pending_operation(self):
|
||||
"""cancel() sets a PENDING operation to CANCELLED."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
result = tracker.cancel(op.id)
|
||||
assert result is True
|
||||
assert op.status == OperationStatus.CANCELLED
|
||||
assert op.message == "Operation cancelled by user"
|
||||
|
||||
def test_cancel_running_operation(self):
|
||||
"""cancel() sets a RUNNING operation to CANCELLED."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, status=OperationStatus.RUNNING)
|
||||
result = tracker.cancel(op.id)
|
||||
assert result is True
|
||||
assert op.status == OperationStatus.CANCELLED
|
||||
|
||||
def test_cancel_completed_operation_returns_false(self):
|
||||
"""cancel() returns False for COMPLETED operations."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, status=OperationStatus.COMPLETED)
|
||||
result = tracker.cancel(op.id)
|
||||
assert result is False
|
||||
assert op.status == OperationStatus.COMPLETED
|
||||
|
||||
def test_cancel_failed_operation_returns_false(self):
|
||||
"""cancel() returns False for FAILED operations."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(op.id, status=OperationStatus.FAILED)
|
||||
result = tracker.cancel(op.id)
|
||||
assert result is False
|
||||
assert op.status == OperationStatus.FAILED
|
||||
|
||||
def test_cancel_unknown_id_returns_false(self):
|
||||
"""cancel() returns False for a nonexistent operation."""
|
||||
tracker = OperationTracker()
|
||||
result = tracker.cancel("nonexistent")
|
||||
assert result is False
|
||||
|
||||
def test_cancel_cancels_asyncio_task(self):
|
||||
"""cancel() calls task.cancel() on the associated asyncio task."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
mock_task = MagicMock(spec=asyncio.Task)
|
||||
mock_task.done.return_value = False
|
||||
op._task = mock_task
|
||||
tracker.cancel(op.id)
|
||||
mock_task.cancel.assert_called_once()
|
||||
|
||||
def test_cancel_does_not_cancel_done_task(self):
|
||||
"""cancel() skips task.cancel() when the asyncio task is already done."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
mock_task = MagicMock(spec=asyncio.Task)
|
||||
mock_task.done.return_value = True
|
||||
op._task = mock_task
|
||||
tracker.cancel(op.id)
|
||||
mock_task.cancel.assert_not_called()
|
||||
|
||||
def test_cancel_updates_timestamp(self):
|
||||
"""cancel() refreshes the updated_at timestamp."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
old_time = op.updated_at
|
||||
time.sleep(0.01)
|
||||
tracker.cancel(op.id)
|
||||
assert op.updated_at > old_time
|
||||
|
||||
|
||||
class TestListActive:
|
||||
"""Tests for OperationTracker.list_active()."""
|
||||
|
||||
def test_list_active_returns_pending_and_running(self):
|
||||
"""list_active() returns PENDING and RUNNING operations."""
|
||||
tracker = OperationTracker()
|
||||
op1 = tracker.create("build")
|
||||
op2 = tracker.create("qa_review")
|
||||
tracker.update(op2.id, status=OperationStatus.RUNNING)
|
||||
active = tracker.list_active()
|
||||
assert len(active) == 2
|
||||
assert op1 in active
|
||||
assert op2 in active
|
||||
|
||||
def test_list_active_excludes_terminal_states(self):
|
||||
"""list_active() excludes COMPLETED, FAILED, and CANCELLED."""
|
||||
tracker = OperationTracker()
|
||||
op1 = tracker.create("build")
|
||||
op2 = tracker.create("qa_review")
|
||||
op3 = tracker.create("spec_create")
|
||||
tracker.update(op1.id, status=OperationStatus.COMPLETED)
|
||||
tracker.update(op2.id, status=OperationStatus.FAILED)
|
||||
tracker.cancel(op3.id)
|
||||
|
||||
active = tracker.list_active()
|
||||
assert len(active) == 0
|
||||
|
||||
def test_list_active_empty_tracker(self):
|
||||
"""list_active() returns empty list when no operations exist."""
|
||||
tracker = OperationTracker()
|
||||
assert tracker.list_active() == []
|
||||
|
||||
|
||||
class TestCleanupOld:
|
||||
"""Tests for OperationTracker._cleanup_old()."""
|
||||
|
||||
def test_cleanup_removes_oldest_completed(self):
|
||||
"""_cleanup_old() removes oldest completed operations beyond max."""
|
||||
tracker = OperationTracker(max_completed=2)
|
||||
ops = []
|
||||
for i in range(4):
|
||||
op = tracker.create(f"build_{i}")
|
||||
op.created_at = i # deterministic ordering
|
||||
tracker.update(op.id, status=OperationStatus.COMPLETED)
|
||||
ops.append(op)
|
||||
|
||||
# Trigger cleanup by creating a new operation
|
||||
tracker.create("trigger")
|
||||
|
||||
# Oldest two should have been removed
|
||||
assert tracker.get(ops[0].id) is None
|
||||
assert tracker.get(ops[1].id) is None
|
||||
# Newest two should remain
|
||||
assert tracker.get(ops[2].id) is not None
|
||||
assert tracker.get(ops[3].id) is not None
|
||||
|
||||
def test_cleanup_does_not_remove_active_operations(self):
|
||||
"""_cleanup_old() only removes terminal-state operations."""
|
||||
tracker = OperationTracker(max_completed=1)
|
||||
active_op = tracker.create("build")
|
||||
tracker.update(active_op.id, status=OperationStatus.RUNNING)
|
||||
|
||||
completed_op = tracker.create("old_build")
|
||||
tracker.update(completed_op.id, status=OperationStatus.COMPLETED)
|
||||
|
||||
# Trigger cleanup
|
||||
tracker.create("trigger")
|
||||
|
||||
# Active operation should still exist
|
||||
assert tracker.get(active_op.id) is not None
|
||||
|
||||
def test_cleanup_under_limit_keeps_all(self):
|
||||
"""_cleanup_old() keeps all operations when under max_completed limit."""
|
||||
tracker = OperationTracker(max_completed=10)
|
||||
ops = []
|
||||
for i in range(3):
|
||||
op = tracker.create(f"build_{i}")
|
||||
tracker.update(op.id, status=OperationStatus.COMPLETED)
|
||||
ops.append(op)
|
||||
|
||||
# All should still exist
|
||||
for op in ops:
|
||||
assert tracker.get(op.id) is not None
|
||||
|
||||
|
||||
class TestOperationToDict:
|
||||
"""Tests for Operation.to_dict() serialization."""
|
||||
|
||||
def test_to_dict_contains_all_fields(self):
|
||||
"""to_dict() includes all expected fields."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("spec_create", message="Creating spec")
|
||||
result = op.to_dict()
|
||||
|
||||
expected_keys = {
|
||||
"id",
|
||||
"type",
|
||||
"status",
|
||||
"progress",
|
||||
"message",
|
||||
"result",
|
||||
"error",
|
||||
"created_at",
|
||||
"updated_at",
|
||||
"elapsed_seconds",
|
||||
}
|
||||
assert set(result.keys()) == expected_keys
|
||||
|
||||
def test_to_dict_serializes_status_as_string(self):
|
||||
"""to_dict() converts OperationStatus enum to its string value."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
result = op.to_dict()
|
||||
assert result["status"] == "pending"
|
||||
assert isinstance(result["status"], str)
|
||||
|
||||
def test_to_dict_computes_elapsed_seconds(self):
|
||||
"""to_dict() calculates elapsed time from creation."""
|
||||
op = Operation(id="test-id", type="build", created_at=time.time() - 5.0)
|
||||
result = op.to_dict()
|
||||
assert result["elapsed_seconds"] >= 4.9
|
||||
|
||||
def test_to_dict_serializes_result_and_error(self):
|
||||
"""to_dict() includes result and error fields."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error="Connection timeout",
|
||||
result={"partial": True},
|
||||
)
|
||||
result = op.to_dict()
|
||||
assert result["error"] == "Connection timeout"
|
||||
assert result["result"] == {"partial": True}
|
||||
assert result["status"] == "failed"
|
||||
|
||||
def test_to_dict_default_values(self):
|
||||
"""to_dict() includes correct defaults for a fresh operation."""
|
||||
tracker = OperationTracker()
|
||||
op = tracker.create("build")
|
||||
result = op.to_dict()
|
||||
assert result["progress"] == 0
|
||||
assert result["result"] is None
|
||||
assert result["error"] is None
|
||||
|
||||
|
||||
class TestOperationStatusEnum:
|
||||
"""Tests for OperationStatus enum values."""
|
||||
|
||||
def test_status_values(self):
|
||||
"""OperationStatus has the expected string values."""
|
||||
assert OperationStatus.PENDING.value == "pending"
|
||||
assert OperationStatus.RUNNING.value == "running"
|
||||
assert OperationStatus.COMPLETED.value == "completed"
|
||||
assert OperationStatus.FAILED.value == "failed"
|
||||
assert OperationStatus.CANCELLED.value == "cancelled"
|
||||
|
||||
def test_status_is_string_enum(self):
|
||||
"""OperationStatus values are strings."""
|
||||
assert isinstance(OperationStatus.PENDING, str)
|
||||
assert OperationStatus.PENDING == "pending"
|
||||
@@ -1,219 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/tools/ops.py
|
||||
==================================
|
||||
|
||||
Tests operation status polling and cancellation tools.
|
||||
Uses the OperationTracker directly.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.operations import OperationStatus, OperationTracker, tracker
|
||||
from mcp_server.tools.ops import (
|
||||
operation_cancel as _operation_cancel_tool,
|
||||
operation_get_status as _operation_get_status_tool,
|
||||
)
|
||||
|
||||
# @mcp.tool() wraps functions as FunctionTool; access .fn for the raw callable
|
||||
operation_get_status = _operation_get_status_tool.fn
|
||||
operation_cancel = _operation_cancel_tool.fn
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def clean_tracker():
|
||||
"""Clear the global tracker between tests."""
|
||||
tracker._operations.clear()
|
||||
yield
|
||||
tracker._operations.clear()
|
||||
|
||||
|
||||
class TestOperationGetStatus:
|
||||
"""Tests for operation_get_status()."""
|
||||
|
||||
def test_returns_operation_dict(self):
|
||||
"""Returns the operation as a dict for a valid ID."""
|
||||
op = tracker.create("build", "Building spec: 001")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.RUNNING,
|
||||
progress=50,
|
||||
message="Halfway done",
|
||||
)
|
||||
|
||||
result = operation_get_status(op.id)
|
||||
|
||||
assert result["id"] == op.id
|
||||
assert result["type"] == "build"
|
||||
assert result["status"] == "running"
|
||||
assert result["progress"] == 50
|
||||
assert result["message"] == "Halfway done"
|
||||
|
||||
def test_returns_error_for_unknown_id(self):
|
||||
"""Returns error dict for a nonexistent operation ID."""
|
||||
result = operation_get_status("nonexistent-uuid")
|
||||
assert "error" in result
|
||||
assert "not found" in result["error"]
|
||||
|
||||
def test_returns_completed_operation(self):
|
||||
"""Returns completed operation with result."""
|
||||
op = tracker.create("qa_review", "QA for 001")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
message="QA approved",
|
||||
result={"status": "approved"},
|
||||
)
|
||||
|
||||
result = operation_get_status(op.id)
|
||||
|
||||
assert result["status"] == "completed"
|
||||
assert result["progress"] == 100
|
||||
assert result["result"]["status"] == "approved"
|
||||
|
||||
def test_returns_failed_operation(self):
|
||||
"""Returns failed operation with error message."""
|
||||
op = tracker.create("build", "Building spec: 001")
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.FAILED,
|
||||
error="Process exited with code 1",
|
||||
)
|
||||
|
||||
result = operation_get_status(op.id)
|
||||
|
||||
assert result["status"] == "failed"
|
||||
assert result["error"] == "Process exited with code 1"
|
||||
|
||||
def test_includes_elapsed_time(self):
|
||||
"""Result includes elapsed_seconds field."""
|
||||
op = tracker.create("build", "test")
|
||||
result = operation_get_status(op.id)
|
||||
assert "elapsed_seconds" in result
|
||||
assert isinstance(result["elapsed_seconds"], float)
|
||||
|
||||
|
||||
class TestOperationCancel:
|
||||
"""Tests for operation_cancel()."""
|
||||
|
||||
def test_cancel_pending_operation(self):
|
||||
"""Cancels a pending operation successfully."""
|
||||
op = tracker.create("build", "Building spec: 001")
|
||||
result = operation_cancel(op.id)
|
||||
assert result["success"] is True
|
||||
|
||||
# Verify it's actually cancelled
|
||||
status = operation_get_status(op.id)
|
||||
assert status["status"] == "cancelled"
|
||||
|
||||
def test_cancel_running_operation(self):
|
||||
"""Cancels a running operation."""
|
||||
op = tracker.create("build", "Building spec: 001")
|
||||
tracker.update(op.id, status=OperationStatus.RUNNING)
|
||||
|
||||
result = operation_cancel(op.id)
|
||||
assert result["success"] is True
|
||||
|
||||
def test_cannot_cancel_completed_operation(self):
|
||||
"""Cannot cancel an already completed operation."""
|
||||
op = tracker.create("build", "Building spec: 001")
|
||||
tracker.update(op.id, status=OperationStatus.COMPLETED, progress=100)
|
||||
|
||||
result = operation_cancel(op.id)
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
def test_cannot_cancel_failed_operation(self):
|
||||
"""Cannot cancel an already failed operation."""
|
||||
op = tracker.create("build", "Building spec: 001")
|
||||
tracker.update(op.id, status=OperationStatus.FAILED, error="failed")
|
||||
|
||||
result = operation_cancel(op.id)
|
||||
assert result["success"] is False
|
||||
|
||||
def test_cancel_nonexistent_returns_error(self):
|
||||
"""Returns error when cancelling a nonexistent operation."""
|
||||
result = operation_cancel("nonexistent-uuid")
|
||||
assert result["success"] is False
|
||||
assert "not found" in result["error"]
|
||||
|
||||
def test_cancel_already_cancelled(self):
|
||||
"""Cancelling an already-cancelled operation still succeeds (idempotent within tracker)."""
|
||||
op = tracker.create("build", "test")
|
||||
# First cancel
|
||||
tracker.cancel(op.id)
|
||||
# Second cancel via the tool - should fail because it's in CANCELLED state
|
||||
# (CANCELLED is not COMPLETED/FAILED, but let's see if tracker.cancel handles it)
|
||||
result = operation_cancel(op.id)
|
||||
# The tracker.cancel checks for COMPLETED and FAILED states;
|
||||
# CANCELLED is neither, so it should succeed
|
||||
assert result["success"] is True
|
||||
|
||||
|
||||
class TestOperationTrackerIntegration:
|
||||
"""Integration tests exercising the tracker lifecycle through tools."""
|
||||
|
||||
def test_create_poll_complete_lifecycle(self):
|
||||
"""Full lifecycle: create -> poll -> update -> complete -> poll."""
|
||||
op = tracker.create("spec_create", "Creating spec 001")
|
||||
|
||||
# Poll pending
|
||||
result = operation_get_status(op.id)
|
||||
assert result["status"] == "pending"
|
||||
|
||||
# Move to running
|
||||
tracker.update(op.id, status=OperationStatus.RUNNING, progress=30)
|
||||
result = operation_get_status(op.id)
|
||||
assert result["status"] == "running"
|
||||
assert result["progress"] == 30
|
||||
|
||||
# Complete
|
||||
tracker.update(
|
||||
op.id,
|
||||
status=OperationStatus.COMPLETED,
|
||||
progress=100,
|
||||
result={"spec_id": "001-feat"},
|
||||
)
|
||||
result = operation_get_status(op.id)
|
||||
assert result["status"] == "completed"
|
||||
assert result["result"]["spec_id"] == "001-feat"
|
||||
|
||||
def test_create_cancel_lifecycle(self):
|
||||
"""Full lifecycle: create -> poll -> cancel -> poll."""
|
||||
op = tracker.create("build", "Building")
|
||||
tracker.update(op.id, status=OperationStatus.RUNNING)
|
||||
|
||||
result = operation_cancel(op.id)
|
||||
assert result["success"] is True
|
||||
|
||||
status = operation_get_status(op.id)
|
||||
assert status["status"] == "cancelled"
|
||||
|
||||
def test_multiple_concurrent_operations(self):
|
||||
"""Multiple operations can coexist independently."""
|
||||
op1 = tracker.create("build", "Build 001")
|
||||
op2 = tracker.create("qa_review", "QA 002")
|
||||
|
||||
tracker.update(op1.id, status=OperationStatus.RUNNING, progress=50)
|
||||
tracker.update(op2.id, status=OperationStatus.COMPLETED, progress=100)
|
||||
|
||||
r1 = operation_get_status(op1.id)
|
||||
r2 = operation_get_status(op2.id)
|
||||
|
||||
assert r1["status"] == "running"
|
||||
assert r2["status"] == "completed"
|
||||
@@ -1,304 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/tools/project.py
|
||||
=======================================
|
||||
|
||||
Tests project management tools: set active, get status, list specs,
|
||||
and get project index. Uses monkeypatch to mock config module.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
import mcp_server.config as config
|
||||
|
||||
# @mcp.tool() wraps functions as FunctionTool objects; access .fn for the raw callable
|
||||
from mcp_server.tools.project import (
|
||||
project_get_index as _project_get_index_tool,
|
||||
project_get_status as _project_get_status_tool,
|
||||
project_list_specs as _project_list_specs_tool,
|
||||
project_set_active as _project_set_active_tool,
|
||||
)
|
||||
|
||||
project_set_active = _project_set_active_tool.fn
|
||||
project_get_status = _project_get_status_tool.fn
|
||||
project_list_specs = _project_list_specs_tool.fn
|
||||
project_get_index = _project_get_index_tool.fn
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def reset_config():
|
||||
"""Reset config state between tests."""
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
yield
|
||||
config._project_dir = None
|
||||
config._auto_claude_dir = None
|
||||
|
||||
|
||||
class TestProjectSetActive:
|
||||
"""Tests for project_set_active()."""
|
||||
|
||||
def test_set_active_valid_project(self, tmp_path):
|
||||
"""Sets the active project directory."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
result = project_set_active(str(tmp_path))
|
||||
assert result["success"] is True
|
||||
assert result["initialized"] is True
|
||||
assert str(tmp_path.resolve()) in result["project_dir"]
|
||||
|
||||
def test_set_active_nonexistent_dir(self):
|
||||
"""Returns error for a nonexistent directory."""
|
||||
result = project_set_active("/nonexistent/path/to/project")
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
def test_set_active_without_auto_claude(self, tmp_path):
|
||||
"""Sets project even without .auto-claude dir (warns but works)."""
|
||||
result = project_set_active(str(tmp_path))
|
||||
assert result["success"] is True
|
||||
# initialized should be False since .auto-claude doesn't exist on disk
|
||||
assert result["initialized"] is False
|
||||
|
||||
|
||||
class TestProjectGetStatus:
|
||||
"""Tests for project_get_status()."""
|
||||
|
||||
def test_returns_error_when_not_initialized(self):
|
||||
"""Returns error when no project has been set."""
|
||||
result = project_get_status()
|
||||
assert result["initialized"] is False
|
||||
assert "error" in result
|
||||
|
||||
def test_returns_status_with_specs(self, tmp_path):
|
||||
"""Returns project status including specs count."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "001-feature-a").mkdir()
|
||||
(specs_dir / "002-feature-b").mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_status()
|
||||
|
||||
assert result["initialized"] is True
|
||||
assert result["specs_count"] == 2
|
||||
assert str(tmp_path.resolve()) in result["project_dir"]
|
||||
|
||||
def test_excludes_gitkeep_from_count(self, tmp_path):
|
||||
"""Specs count excludes .gitkeep entries."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "001-feat").mkdir()
|
||||
(specs_dir / ".gitkeep").mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_status()
|
||||
|
||||
assert result["specs_count"] == 1
|
||||
|
||||
def test_returns_project_name_from_index(self, tmp_path):
|
||||
"""Uses project name from project_index.json when available."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
index = {"name": "My Awesome Project"}
|
||||
(ac_dir / "project_index.json").write_text(json.dumps(index))
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_status()
|
||||
|
||||
assert result["project_name"] == "My Awesome Project"
|
||||
|
||||
def test_uses_dir_name_as_fallback_project_name(self, tmp_path):
|
||||
"""Falls back to directory name when no project_index exists."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_status()
|
||||
|
||||
assert result["project_name"] == tmp_path.resolve().name
|
||||
|
||||
def test_handles_no_specs_dir(self, tmp_path):
|
||||
"""Returns zero specs when specs directory doesn't exist."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_status()
|
||||
|
||||
assert result["specs_count"] == 0
|
||||
|
||||
|
||||
class TestProjectListSpecs:
|
||||
"""Tests for project_list_specs()."""
|
||||
|
||||
def test_returns_error_when_not_initialized(self):
|
||||
"""Returns error when no project set."""
|
||||
result = project_list_specs()
|
||||
assert "error" in result
|
||||
|
||||
def test_lists_specs_with_plan(self, tmp_path):
|
||||
"""Returns spec list with plan details."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
spec_dir = specs_dir / "001-auth"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
plan = {"feature": "User Authentication", "status": "building"}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
(spec_dir / "spec.md").write_text("# Auth Spec")
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
assert result["count"] == 1
|
||||
spec = result["specs"][0]
|
||||
assert spec["name"] == "001-auth"
|
||||
assert spec["title"] == "User Authentication"
|
||||
assert spec["has_plan"] is True
|
||||
assert spec["has_spec"] is True
|
||||
assert spec["status"] == "building"
|
||||
|
||||
def test_lists_specs_without_plan(self, tmp_path):
|
||||
"""Returns spec with default status when no plan exists."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
spec_dir = specs_dir / "001-feat"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
spec = result["specs"][0]
|
||||
assert spec["has_plan"] is False
|
||||
assert spec["status"] == "pending"
|
||||
assert spec["title"] == "001-feat"
|
||||
|
||||
def test_detects_qa_report(self, tmp_path):
|
||||
"""Returns has_qa_report=True when qa_report.md exists."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
spec_dir = specs_dir / "001-feat"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "qa_report.md").write_text("# QA Report")
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
assert result["specs"][0]["has_qa_report"] is True
|
||||
|
||||
def test_empty_specs_dir(self, tmp_path):
|
||||
"""Returns empty list when specs directory is empty."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
(ac_dir / "specs").mkdir(parents=True)
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
assert result["count"] == 0
|
||||
assert result["specs"] == []
|
||||
|
||||
def test_no_specs_dir(self, tmp_path):
|
||||
"""Returns message when specs directory doesn't exist."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
assert result["specs"] == []
|
||||
assert "message" in result
|
||||
|
||||
def test_excludes_gitkeep(self, tmp_path):
|
||||
"""Excludes .gitkeep from specs list."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / ".gitkeep").mkdir()
|
||||
(specs_dir / "001-feat").mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
assert result["count"] == 1
|
||||
assert result["specs"][0]["name"] == "001-feat"
|
||||
|
||||
def test_handles_corrupt_plan(self, tmp_path):
|
||||
"""Handles corrupt implementation_plan.json gracefully."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
spec_dir = specs_dir / "001-feat"
|
||||
spec_dir.mkdir(parents=True)
|
||||
(spec_dir / "implementation_plan.json").write_text("bad json!")
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
spec = result["specs"][0]
|
||||
assert spec["has_plan"] is True
|
||||
assert spec["status"] == "pending" # fallback
|
||||
|
||||
def test_specs_sorted_by_name(self, tmp_path):
|
||||
"""Specs are returned sorted by directory name."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
specs_dir = ac_dir / "specs"
|
||||
specs_dir.mkdir(parents=True)
|
||||
(specs_dir / "003-third").mkdir()
|
||||
(specs_dir / "001-first").mkdir()
|
||||
(specs_dir / "002-second").mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_list_specs()
|
||||
|
||||
names = [s["name"] for s in result["specs"]]
|
||||
assert names == ["001-first", "002-second", "003-third"]
|
||||
|
||||
|
||||
class TestProjectGetIndex:
|
||||
"""Tests for project_get_index()."""
|
||||
|
||||
def test_returns_error_when_not_initialized(self):
|
||||
"""Returns error when no project set."""
|
||||
result = project_get_index()
|
||||
assert "error" in result
|
||||
|
||||
def test_returns_index_data(self, tmp_path):
|
||||
"""Returns project index content."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
index_data = {"files": ["main.py"], "language": "python"}
|
||||
(ac_dir / "project_index.json").write_text(json.dumps(index_data))
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_index()
|
||||
|
||||
assert result["index"] == index_data
|
||||
|
||||
def test_returns_empty_when_no_index(self, tmp_path):
|
||||
"""Returns empty index when file doesn't exist."""
|
||||
ac_dir = tmp_path / ".auto-claude"
|
||||
ac_dir.mkdir()
|
||||
|
||||
config.initialize(tmp_path)
|
||||
result = project_get_index()
|
||||
|
||||
assert result["index"] == {}
|
||||
assert "message" in result
|
||||
@@ -1,267 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/services/qa_service.py
|
||||
=============================================
|
||||
|
||||
Tests QA report retrieval, manual approval, session number resolution,
|
||||
spec directory resolution, and start_review error paths.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.services.qa_service import QAService
|
||||
|
||||
|
||||
def _make_spec_dir(tmp_path: Path, spec_name: str = "001-feat") -> Path:
|
||||
"""Create a spec directory structure for testing."""
|
||||
specs_dir = tmp_path / ".auto-claude" / "specs" / spec_name
|
||||
specs_dir.mkdir(parents=True)
|
||||
return specs_dir
|
||||
|
||||
|
||||
class TestResolveSpecDir:
|
||||
"""Tests for QAService._resolve_spec_dir()."""
|
||||
|
||||
def test_exact_match(self, tmp_path):
|
||||
"""Resolves exact spec directory name."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feature")
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._resolve_spec_dir("001-feature") == spec_dir
|
||||
|
||||
def test_prefix_match(self, tmp_path):
|
||||
"""Resolves spec directory by prefix."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-my-feature")
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._resolve_spec_dir("001") == spec_dir
|
||||
|
||||
def test_no_match_returns_none(self, tmp_path):
|
||||
"""Returns None when no spec matches."""
|
||||
_make_spec_dir(tmp_path, "001-feat")
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._resolve_spec_dir("999") is None
|
||||
|
||||
def test_no_specs_dir_returns_none(self, tmp_path):
|
||||
"""Returns None when specs directory doesn't exist."""
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._resolve_spec_dir("001") is None
|
||||
|
||||
|
||||
class TestGetNextQaSession:
|
||||
"""Tests for QAService._get_next_qa_session()."""
|
||||
|
||||
def test_first_session_when_no_plan(self, tmp_path):
|
||||
"""Returns 1 when no implementation plan exists."""
|
||||
spec_dir = _make_spec_dir(tmp_path)
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._get_next_qa_session(spec_dir) == 1
|
||||
|
||||
def test_increments_from_existing_session(self, tmp_path):
|
||||
"""Increments the qa_session number from existing signoff."""
|
||||
spec_dir = _make_spec_dir(tmp_path)
|
||||
plan = {"qa_signoff": {"qa_session": 2, "status": "rejected"}}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._get_next_qa_session(spec_dir) == 3
|
||||
|
||||
def test_first_session_when_no_signoff(self, tmp_path):
|
||||
"""Returns 1 when plan exists but has no qa_signoff."""
|
||||
spec_dir = _make_spec_dir(tmp_path)
|
||||
plan = {"subtasks": []}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._get_next_qa_session(spec_dir) == 1
|
||||
|
||||
def test_handles_corrupt_plan(self, tmp_path):
|
||||
"""Returns 1 when plan file has invalid JSON."""
|
||||
spec_dir = _make_spec_dir(tmp_path)
|
||||
(spec_dir / "implementation_plan.json").write_text("not json!!")
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
assert svc._get_next_qa_session(spec_dir) == 1
|
||||
|
||||
|
||||
class TestStartReview:
|
||||
"""Tests for QAService.start_review()."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_error_for_missing_spec(self, tmp_path):
|
||||
"""start_review returns error when spec not found."""
|
||||
(tmp_path / ".auto-claude" / "specs").mkdir(parents=True)
|
||||
svc = QAService(tmp_path)
|
||||
result = await svc.start_review("999-missing")
|
||||
assert "error" in result
|
||||
assert "not found" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_error_for_missing_plan(self, tmp_path):
|
||||
"""start_review returns error when no implementation plan exists."""
|
||||
_make_spec_dir(tmp_path, "001-feat")
|
||||
svc = QAService(tmp_path)
|
||||
result = await svc.start_review("001-feat")
|
||||
assert "error" in result
|
||||
assert "implementation plan" in result["error"].lower() or "Build" in result["error"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_error_on_import_failure(self, tmp_path, monkeypatch):
|
||||
"""start_review returns error when backend QA modules unavailable."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
plan = {"subtasks": [{"status": "completed"}]}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
# The import of core.client / qa.reviewer will fail naturally
|
||||
# since we haven't set up the backend path properly for these modules
|
||||
result = await svc.start_review("001-feat")
|
||||
assert "error" in result
|
||||
|
||||
|
||||
class TestGetReport:
|
||||
"""Tests for QAService.get_report()."""
|
||||
|
||||
def test_returns_error_for_missing_spec(self, tmp_path):
|
||||
"""get_report returns error when spec not found."""
|
||||
(tmp_path / ".auto-claude" / "specs").mkdir(parents=True)
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("999-missing")
|
||||
assert "error" in result
|
||||
|
||||
def test_reads_qa_report(self, tmp_path):
|
||||
"""get_report returns qa_report.md content."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
report_text = "# QA Report\n\nAll tests passed."
|
||||
(spec_dir / "qa_report.md").write_text(report_text)
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("001-feat")
|
||||
assert result["report"] == report_text
|
||||
assert result["spec_id"] == "001-feat"
|
||||
|
||||
def test_reads_fix_request(self, tmp_path):
|
||||
"""get_report includes QA_FIX_REQUEST.md content when present."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
fix_text = "## Fix Required\n\nFix the error handling."
|
||||
(spec_dir / "QA_FIX_REQUEST.md").write_text(fix_text)
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("001-feat")
|
||||
assert result["fix_request"] == fix_text
|
||||
|
||||
def test_reads_qa_signoff(self, tmp_path):
|
||||
"""get_report includes qa_signoff from implementation plan."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
signoff = {"status": "approved", "qa_session": 2}
|
||||
plan = {"qa_signoff": signoff}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("001-feat")
|
||||
assert result["qa_signoff"] == signoff
|
||||
|
||||
def test_no_report_message(self, tmp_path):
|
||||
"""get_report returns 'no report' message when nothing exists."""
|
||||
_make_spec_dir(tmp_path, "001-feat")
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("001-feat")
|
||||
assert "No QA report" in result.get("message", "")
|
||||
|
||||
def test_reads_all_artifacts(self, tmp_path):
|
||||
"""get_report combines report, fix request, and signoff."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
(spec_dir / "qa_report.md").write_text("Report content")
|
||||
(spec_dir / "QA_FIX_REQUEST.md").write_text("Fix content")
|
||||
plan = {"qa_signoff": {"status": "rejected", "qa_session": 1}}
|
||||
(spec_dir / "implementation_plan.json").write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("001-feat")
|
||||
assert "report" in result
|
||||
assert "fix_request" in result
|
||||
assert "qa_signoff" in result
|
||||
|
||||
def test_handles_corrupt_plan_gracefully(self, tmp_path):
|
||||
"""get_report skips qa_signoff on corrupt plan file."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
(spec_dir / "qa_report.md").write_text("Report content")
|
||||
(spec_dir / "implementation_plan.json").write_text("bad json!")
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.get_report("001-feat")
|
||||
assert "report" in result
|
||||
assert "qa_signoff" not in result
|
||||
|
||||
|
||||
class TestApprove:
|
||||
"""Tests for QAService.approve()."""
|
||||
|
||||
def test_returns_error_for_missing_spec(self, tmp_path):
|
||||
"""approve returns error when spec not found."""
|
||||
(tmp_path / ".auto-claude" / "specs").mkdir(parents=True)
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.approve("999-missing")
|
||||
assert "error" in result
|
||||
|
||||
def test_returns_error_for_missing_plan(self, tmp_path):
|
||||
"""approve returns error when no implementation plan exists."""
|
||||
_make_spec_dir(tmp_path, "001-feat")
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.approve("001-feat")
|
||||
assert "error" in result
|
||||
assert "implementation plan" in result["error"].lower()
|
||||
|
||||
def test_writes_qa_signoff(self, tmp_path):
|
||||
"""approve writes qa_signoff to the implementation plan."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
plan = {"subtasks": [{"status": "completed"}]}
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
plan_file.write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.approve("001-feat")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["spec_id"] == "001-feat"
|
||||
|
||||
# Verify file was updated
|
||||
updated_plan = json.loads(plan_file.read_text())
|
||||
assert updated_plan["qa_signoff"]["status"] == "approved"
|
||||
assert updated_plan["qa_signoff"]["verified_by"] == "manual_approval"
|
||||
|
||||
def test_preserves_existing_session_number(self, tmp_path):
|
||||
"""approve preserves the existing qa_session number."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
plan = {"qa_signoff": {"qa_session": 3, "status": "rejected"}}
|
||||
plan_file = spec_dir / "implementation_plan.json"
|
||||
plan_file.write_text(json.dumps(plan))
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
svc.approve("001-feat")
|
||||
|
||||
updated_plan = json.loads(plan_file.read_text())
|
||||
assert updated_plan["qa_signoff"]["qa_session"] == 3
|
||||
|
||||
def test_returns_error_on_corrupt_plan(self, tmp_path):
|
||||
"""approve returns error when plan file contains invalid JSON."""
|
||||
spec_dir = _make_spec_dir(tmp_path, "001-feat")
|
||||
(spec_dir / "implementation_plan.json").write_text("not json!")
|
||||
|
||||
svc = QAService(tmp_path)
|
||||
result = svc.approve("001-feat")
|
||||
assert "error" in result
|
||||
@@ -1,107 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/server.py
|
||||
===============================
|
||||
|
||||
Tests that the MCP server instance is configured correctly and
|
||||
all tools are registered.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
# Mock heavy backend dependencies that tool modules import
|
||||
_modules_to_mock = [
|
||||
"cli.utils",
|
||||
"core.client",
|
||||
"core.auth",
|
||||
"core.worktree",
|
||||
"agents",
|
||||
"agents.planner",
|
||||
"agents.coder",
|
||||
"agents.session",
|
||||
"qa",
|
||||
"qa.reviewer",
|
||||
"qa.fixer",
|
||||
"qa.loop",
|
||||
"qa.criteria",
|
||||
"spec",
|
||||
"spec.pipeline",
|
||||
"context",
|
||||
"context.builder",
|
||||
"context.search",
|
||||
"runners",
|
||||
"runners.spec_runner",
|
||||
"runners.roadmap",
|
||||
"runners.insights",
|
||||
"runners.github",
|
||||
"runners.github.services",
|
||||
"runners.github.services.parallel_orchestrator_reviewer",
|
||||
"services",
|
||||
"services.recovery",
|
||||
"integrations",
|
||||
"integrations.graphiti",
|
||||
"integrations.github",
|
||||
"project",
|
||||
"project.analysis",
|
||||
"merge",
|
||||
]
|
||||
for mod_name in _modules_to_mock:
|
||||
if mod_name not in sys.modules:
|
||||
sys.modules[mod_name] = MagicMock()
|
||||
|
||||
|
||||
class TestMCPServerInstance:
|
||||
"""Tests for the MCP server instance."""
|
||||
|
||||
def test_mcp_instance_exists(self):
|
||||
"""The mcp server instance is created."""
|
||||
from mcp_server.server import mcp
|
||||
|
||||
assert mcp is not None
|
||||
|
||||
def test_mcp_instance_name(self):
|
||||
"""The mcp server has the correct name."""
|
||||
from mcp_server.server import mcp
|
||||
|
||||
assert mcp.name == "Auto Claude"
|
||||
|
||||
|
||||
class TestRegisterAllTools:
|
||||
"""Tests for register_all_tools()."""
|
||||
|
||||
def test_register_all_tools_registers_43_tools(self):
|
||||
"""register_all_tools() registers exactly 43 tools."""
|
||||
from mcp_server.server import mcp, register_all_tools
|
||||
|
||||
register_all_tools()
|
||||
|
||||
# FastMCP stores tools in _tool_manager._tools dict
|
||||
tool_count = len(mcp._tool_manager._tools)
|
||||
assert tool_count == 43, (
|
||||
f"Expected 43 tools, got {tool_count}. "
|
||||
f"Tools: {sorted(mcp._tool_manager._tools.keys())}"
|
||||
)
|
||||
|
||||
def test_register_all_tools_idempotent(self):
|
||||
"""Calling register_all_tools() multiple times doesn't duplicate tools."""
|
||||
from mcp_server.server import mcp, register_all_tools
|
||||
|
||||
register_all_tools()
|
||||
count_first = len(mcp._tool_manager._tools)
|
||||
|
||||
register_all_tools()
|
||||
count_second = len(mcp._tool_manager._tools)
|
||||
|
||||
assert count_first == count_second
|
||||
@@ -1,601 +0,0 @@
|
||||
"""
|
||||
Tests for MCP Spec Service and Spec Tools
|
||||
==========================================
|
||||
|
||||
Tests SpecService (spec status, content, listing) and the 4 spec_* MCP tools.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.services.spec_service import SpecService
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _make_spec_dir(
|
||||
specs_dir: Path,
|
||||
name: str,
|
||||
*,
|
||||
plan: dict | None = None,
|
||||
requirements: dict | None = None,
|
||||
complexity: dict | None = None,
|
||||
spec_md: str | None = None,
|
||||
discovery_md: str | None = None,
|
||||
qa_report: str | None = None,
|
||||
) -> Path:
|
||||
"""Create a spec directory with optional files."""
|
||||
d = specs_dir / name
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
if plan is not None:
|
||||
(d / "implementation_plan.json").write_text(json.dumps(plan), encoding="utf-8")
|
||||
if requirements is not None:
|
||||
(d / "requirements.json").write_text(
|
||||
json.dumps(requirements), encoding="utf-8"
|
||||
)
|
||||
if complexity is not None:
|
||||
(d / "complexity_assessment.json").write_text(
|
||||
json.dumps(complexity), encoding="utf-8"
|
||||
)
|
||||
if spec_md is not None:
|
||||
(d / "spec.md").write_text(spec_md, encoding="utf-8")
|
||||
if discovery_md is not None:
|
||||
(d / "discovery.md").write_text(discovery_md, encoding="utf-8")
|
||||
if qa_report is not None:
|
||||
(d / "qa_report.md").write_text(qa_report, encoding="utf-8")
|
||||
return d
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# SpecService - get_spec_status
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecServiceGetSpecStatus:
|
||||
def test_not_found(self, tmp_path):
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("999-nope")
|
||||
assert "error" in result
|
||||
|
||||
def test_pending_no_files(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "pending"
|
||||
assert result["phases"]["discovery"] is False
|
||||
assert result["phases"]["requirements"] is False
|
||||
assert result["phases"]["spec"] is False
|
||||
assert result["phases"]["implementation_plan"] is False
|
||||
|
||||
def test_discovery_complete(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", discovery_md="# Discovery\nFindings here")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "discovery_complete"
|
||||
assert result["phases"]["discovery"] is True
|
||||
|
||||
def test_requirements_gathered(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
discovery_md="# D",
|
||||
requirements={"task_description": "Do thing"},
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "requirements_gathered"
|
||||
assert result["phases"]["requirements"] is True
|
||||
|
||||
def test_spec_complete(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
discovery_md="# D",
|
||||
requirements={"task_description": "X"},
|
||||
spec_md="# Spec\nContent here",
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "spec_complete"
|
||||
assert result["phases"]["spec"] is True
|
||||
|
||||
def test_ready_to_build(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
discovery_md="# D",
|
||||
requirements={"task_description": "X"},
|
||||
spec_md="# Spec",
|
||||
plan={"feature": "F", "phases": []},
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "ready_to_build"
|
||||
assert result["phases"]["implementation_plan"] is True
|
||||
|
||||
def test_qa_reviewed(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
qa_report="# QA Report\nAll good",
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "qa_reviewed"
|
||||
|
||||
def test_qa_approved(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={
|
||||
"feature": "F",
|
||||
"qa_signoff": {"status": "approved"},
|
||||
},
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "qa_approved"
|
||||
|
||||
def test_qa_rejected(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={
|
||||
"feature": "F",
|
||||
"qa_signoff": {"status": "rejected"},
|
||||
},
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["status"] == "qa_rejected"
|
||||
|
||||
def test_complexity_assessment_phase(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
requirements={"task_description": "X"},
|
||||
complexity={"complexity": "standard"},
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert result["phases"]["complexity_assessment"] is True
|
||||
|
||||
def test_prefix_matching(self, tmp_path):
|
||||
"""get_spec_status supports prefix matching (e.g., '001' matches '001-feat')."""
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-my-feature", plan={"feature": "F"})
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001")
|
||||
assert result["spec_id"] == "001-my-feature"
|
||||
assert result["status"] == "ready_to_build"
|
||||
|
||||
def test_returns_spec_dir_path(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_status("001-feat")
|
||||
assert "spec_dir" in result
|
||||
assert result["spec_dir"].endswith("001-feat")
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# SpecService - get_spec_content
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecServiceGetSpecContent:
|
||||
def test_not_found(self, tmp_path):
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("999-nope")
|
||||
assert "error" in result
|
||||
|
||||
def test_reads_spec_md(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", spec_md="# My Spec\n\nContent here.")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-feat")
|
||||
assert "spec_md" in result
|
||||
assert "My Spec" in result["spec_md"]
|
||||
|
||||
def test_reads_requirements(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs, "001-feat", requirements={"task_description": "Build auth"}
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-feat")
|
||||
assert "requirements" in result
|
||||
assert result["requirements"]["task_description"] == "Build auth"
|
||||
|
||||
def test_reads_implementation_plan(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "Auth", "phases": [{"name": "Phase 1"}]},
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-feat")
|
||||
assert "implementation_plan" in result
|
||||
assert result["implementation_plan"]["feature"] == "Auth"
|
||||
|
||||
def test_reads_complexity_assessment(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs, "001-feat", complexity={"complexity": "standard", "confidence": 0.9}
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-feat")
|
||||
assert "complexity_assessment" in result
|
||||
assert result["complexity_assessment"]["complexity"] == "standard"
|
||||
|
||||
def test_reads_qa_report(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", qa_report="# QA Report\n\nAll tests pass.")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-feat")
|
||||
assert "qa_report" in result
|
||||
assert "All tests pass" in result["qa_report"]
|
||||
|
||||
def test_empty_spec_dir(self, tmp_path):
|
||||
"""A spec dir with no files returns only spec_id and spec_dir."""
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-empty")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-empty")
|
||||
assert result["spec_id"] == "001-empty"
|
||||
assert "spec_md" not in result
|
||||
assert "requirements" not in result
|
||||
assert "implementation_plan" not in result
|
||||
|
||||
def test_prefix_matching(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-my-feature", spec_md="# Content")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001")
|
||||
assert result["spec_id"] == "001-my-feature"
|
||||
assert "spec_md" in result
|
||||
|
||||
def test_all_files_present(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-full",
|
||||
plan={"feature": "Full"},
|
||||
requirements={"task_description": "Build it"},
|
||||
complexity={"complexity": "complex"},
|
||||
spec_md="# Full Spec",
|
||||
qa_report="# QA\nAll good",
|
||||
)
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.get_spec_content("001-full")
|
||||
assert "spec_md" in result
|
||||
assert "requirements" in result
|
||||
assert "implementation_plan" in result
|
||||
assert "complexity_assessment" in result
|
||||
assert "qa_report" in result
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# SpecService - list_specs
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecServiceListSpecs:
|
||||
def test_no_specs_dir(self, tmp_path):
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc.list_specs() == []
|
||||
|
||||
def test_empty_specs_dir(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc.list_specs() == []
|
||||
|
||||
def test_lists_multiple_specs(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-first", plan={"feature": "First"})
|
||||
_make_spec_dir(specs, "002-second", requirements={"task_description": "X"})
|
||||
_make_spec_dir(specs, "003-third")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.list_specs()
|
||||
assert len(result) == 3
|
||||
ids = [s["spec_id"] for s in result]
|
||||
assert "001-first" in ids
|
||||
assert "002-second" in ids
|
||||
assert "003-third" in ids
|
||||
|
||||
def test_skips_hidden_dirs(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-visible")
|
||||
(specs / ".hidden-dir").mkdir()
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.list_specs()
|
||||
assert len(result) == 1
|
||||
assert result[0]["spec_id"] == "001-visible"
|
||||
|
||||
def test_skips_files(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-visible")
|
||||
(specs / "some-file.txt").write_text("not a dir")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.list_specs()
|
||||
assert len(result) == 1
|
||||
|
||||
def test_sorted_order(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "003-c")
|
||||
_make_spec_dir(specs, "001-a")
|
||||
_make_spec_dir(specs, "002-b")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.list_specs()
|
||||
ids = [s["spec_id"] for s in result]
|
||||
assert ids == ["001-a", "002-b", "003-c"]
|
||||
|
||||
def test_each_entry_has_status(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F"})
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc.list_specs()
|
||||
assert "status" in result[0]
|
||||
assert "phases" in result[0]
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# SpecService - _resolve_spec_dir
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecServiceResolveSpecDir:
|
||||
def test_exact_match(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-exact-name")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc._resolve_spec_dir(specs, "001-exact-name")
|
||||
assert result is not None
|
||||
assert result.name == "001-exact-name"
|
||||
|
||||
def test_prefix_match(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-my-feature")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc._resolve_spec_dir(specs, "001")
|
||||
assert result is not None
|
||||
assert result.name == "001-my-feature"
|
||||
|
||||
def test_no_match(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc._resolve_spec_dir(specs, "999") is None
|
||||
|
||||
def test_prefers_exact_over_prefix(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001")
|
||||
_make_spec_dir(specs, "001-with-slug")
|
||||
svc = SpecService(tmp_path)
|
||||
result = svc._resolve_spec_dir(specs, "001")
|
||||
assert result.name == "001"
|
||||
|
||||
def test_no_specs_dir(self, tmp_path):
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc._resolve_spec_dir(tmp_path / "nonexistent", "001") is None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# SpecService - _load_json
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecServiceLoadJson:
|
||||
def test_valid_json(self, tmp_path):
|
||||
p = tmp_path / "data.json"
|
||||
p.write_text('{"key": "value"}')
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc._load_json(p) == {"key": "value"}
|
||||
|
||||
def test_invalid_json(self, tmp_path):
|
||||
p = tmp_path / "bad.json"
|
||||
p.write_text("not json")
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc._load_json(p) is None
|
||||
|
||||
def test_missing_file(self, tmp_path):
|
||||
svc = SpecService(tmp_path)
|
||||
assert svc._load_json(tmp_path / "nope.json") is None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# SpecService - create_spec (async, mocked)
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecServiceCreateSpec:
|
||||
async def test_import_error(self, tmp_path, monkeypatch):
|
||||
"""When SpecOrchestrator can't be imported, returns error."""
|
||||
svc = SpecService(tmp_path)
|
||||
# Ensure the import inside create_spec fails
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if "orchestrator" in name:
|
||||
raise ImportError("no module")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
monkeypatch.setattr(builtins, "__import__", mock_import)
|
||||
result = await svc.create_spec("build a thing")
|
||||
assert result["success"] is False
|
||||
assert "not available" in result["error"]
|
||||
|
||||
async def test_orchestrator_exception(self, tmp_path, monkeypatch):
|
||||
"""When orchestrator raises, returns error."""
|
||||
svc = SpecService(tmp_path)
|
||||
|
||||
mock_orch_cls = MagicMock()
|
||||
mock_orch_cls.side_effect = RuntimeError("boom")
|
||||
|
||||
monkeypatch.setitem(
|
||||
sys.modules,
|
||||
"spec.pipeline.orchestrator",
|
||||
MagicMock(SpecOrchestrator=mock_orch_cls),
|
||||
)
|
||||
result = await svc.create_spec("task")
|
||||
assert result["success"] is False
|
||||
assert "boom" in result["error"]
|
||||
|
||||
async def test_successful_spec_creation(self, tmp_path, monkeypatch):
|
||||
"""When orchestrator succeeds, returns success with spec info."""
|
||||
svc = SpecService(tmp_path)
|
||||
|
||||
spec_dir = tmp_path / ".auto-claude" / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
|
||||
mock_instance = MagicMock()
|
||||
mock_instance.spec_dir = spec_dir
|
||||
mock_instance.run = MagicMock(return_value=True)
|
||||
# Make run a coroutine
|
||||
import asyncio
|
||||
|
||||
async def mock_run(**kwargs):
|
||||
return True
|
||||
|
||||
mock_instance.run = mock_run
|
||||
|
||||
mock_orch_cls = MagicMock(return_value=mock_instance)
|
||||
|
||||
monkeypatch.setitem(
|
||||
sys.modules,
|
||||
"spec.pipeline.orchestrator",
|
||||
MagicMock(SpecOrchestrator=mock_orch_cls),
|
||||
)
|
||||
result = await svc.create_spec("build a thing")
|
||||
assert result["success"] is True
|
||||
assert result["spec_id"] == "001-test"
|
||||
assert "spec_dir" in result
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# Spec Tools (mcp_server.tools.specs)
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSpecTools:
|
||||
"""Tests for the MCP spec_* tool functions.
|
||||
|
||||
FastMCP's @mcp.tool() wraps functions in FunctionTool objects.
|
||||
We call the underlying function via .fn to test the tool logic directly.
|
||||
"""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _setup_config(self, tmp_path, monkeypatch):
|
||||
"""Mock get_project_dir for all tool tests."""
|
||||
import mcp_server.config as config_mod
|
||||
|
||||
monkeypatch.setattr(config_mod, "_project_dir", tmp_path)
|
||||
monkeypatch.setattr(config_mod, "_auto_claude_dir", tmp_path / ".auto-claude")
|
||||
self.project_dir = tmp_path
|
||||
self.specs_dir = tmp_path / ".auto-claude" / "specs"
|
||||
|
||||
def test_spec_get_status_success(self):
|
||||
from mcp_server.tools.specs import spec_get_status
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
spec_md="# Spec",
|
||||
)
|
||||
result = spec_get_status.fn("001-feat")
|
||||
assert result["status"] == "ready_to_build"
|
||||
assert result["spec_id"] == "001-feat"
|
||||
|
||||
def test_spec_get_status_not_found(self):
|
||||
from mcp_server.tools.specs import spec_get_status
|
||||
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
result = spec_get_status.fn("999-nope")
|
||||
assert "error" in result
|
||||
|
||||
def test_spec_get_status_prefix(self):
|
||||
from mcp_server.tools.specs import spec_get_status
|
||||
|
||||
_make_spec_dir(self.specs_dir, "001-my-feature", plan={"feature": "F"})
|
||||
result = spec_get_status.fn("001")
|
||||
assert result["spec_id"] == "001-my-feature"
|
||||
|
||||
def test_spec_get_content_success(self):
|
||||
from mcp_server.tools.specs import spec_get_content
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
spec_md="# The Spec",
|
||||
requirements={"task_description": "Build it"},
|
||||
plan={"feature": "Feature"},
|
||||
)
|
||||
result = spec_get_content.fn("001-feat")
|
||||
assert "spec_md" in result
|
||||
assert "requirements" in result
|
||||
assert "implementation_plan" in result
|
||||
|
||||
def test_spec_get_content_not_found(self):
|
||||
from mcp_server.tools.specs import spec_get_content
|
||||
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
result = spec_get_content.fn("999-nope")
|
||||
assert "error" in result
|
||||
|
||||
def test_spec_list_success(self):
|
||||
from mcp_server.tools.specs import spec_list
|
||||
|
||||
_make_spec_dir(self.specs_dir, "001-first", plan={"feature": "First"})
|
||||
_make_spec_dir(self.specs_dir, "002-second")
|
||||
result = spec_list.fn()
|
||||
assert result["count"] == 2
|
||||
assert len(result["specs"]) == 2
|
||||
|
||||
def test_spec_list_empty(self):
|
||||
from mcp_server.tools.specs import spec_list
|
||||
|
||||
result = spec_list.fn()
|
||||
assert result["count"] == 0
|
||||
assert result["specs"] == []
|
||||
|
||||
def test_spec_create_is_async(self):
|
||||
"""spec_create wraps an async function for long-running operations."""
|
||||
from mcp_server.tools.specs import spec_create
|
||||
import inspect
|
||||
|
||||
# The underlying fn is async (wrapped by FastMCP)
|
||||
assert inspect.iscoroutinefunction(spec_create.fn)
|
||||
@@ -1,937 +0,0 @@
|
||||
"""
|
||||
Tests for MCP Task Service and Task Tools
|
||||
==========================================
|
||||
|
||||
Tests TaskService (CRUD on spec directories) and the 6 task_* MCP tools.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import json
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.services.task_service import (
|
||||
VALID_STATUSES,
|
||||
TaskService,
|
||||
_extract_spec_heading,
|
||||
_extract_spec_overview,
|
||||
_safe_read_json,
|
||||
_slugify,
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Helpers
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _make_spec_dir(
|
||||
specs_dir: Path,
|
||||
name: str,
|
||||
*,
|
||||
plan: dict | None = None,
|
||||
requirements: dict | None = None,
|
||||
metadata: dict | None = None,
|
||||
spec_md: str | None = None,
|
||||
qa_report: str | None = None,
|
||||
) -> Path:
|
||||
"""Create a spec directory with optional files."""
|
||||
d = specs_dir / name
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
if plan is not None:
|
||||
(d / "implementation_plan.json").write_text(json.dumps(plan), encoding="utf-8")
|
||||
if requirements is not None:
|
||||
(d / "requirements.json").write_text(
|
||||
json.dumps(requirements), encoding="utf-8"
|
||||
)
|
||||
if metadata is not None:
|
||||
(d / "task_metadata.json").write_text(json.dumps(metadata), encoding="utf-8")
|
||||
if spec_md is not None:
|
||||
(d / "spec.md").write_text(spec_md, encoding="utf-8")
|
||||
if qa_report is not None:
|
||||
(d / "qa_report.md").write_text(qa_report, encoding="utf-8")
|
||||
return d
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _slugify
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSlugify:
|
||||
def test_basic_title(self):
|
||||
assert _slugify("My Feature") == "my-feature"
|
||||
|
||||
def test_special_characters(self):
|
||||
assert _slugify("Hello, World! (v2)") == "hello-world-v2"
|
||||
|
||||
def test_leading_trailing_whitespace(self):
|
||||
assert _slugify(" padded ") == "padded"
|
||||
|
||||
def test_multiple_spaces_and_dashes(self):
|
||||
assert _slugify("a b---c") == "a-b-c"
|
||||
|
||||
def test_truncates_to_80(self):
|
||||
result = _slugify("a" * 100)
|
||||
assert len(result) <= 80
|
||||
|
||||
def test_empty_string(self):
|
||||
assert _slugify("") == ""
|
||||
|
||||
def test_unicode_preserved(self):
|
||||
# Python \w matches unicode word chars, so accented letters stay
|
||||
assert _slugify("café crème") == "café-crème"
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _safe_read_json
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestSafeReadJson:
|
||||
def test_valid_json(self, tmp_path):
|
||||
p = tmp_path / "data.json"
|
||||
p.write_text('{"key": 1}')
|
||||
assert _safe_read_json(p) == {"key": 1}
|
||||
|
||||
def test_invalid_json(self, tmp_path):
|
||||
p = tmp_path / "bad.json"
|
||||
p.write_text("not json")
|
||||
assert _safe_read_json(p) is None
|
||||
|
||||
def test_missing_file(self, tmp_path):
|
||||
assert _safe_read_json(tmp_path / "nope.json") is None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _extract_spec_heading
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestExtractSpecHeading:
|
||||
def test_standard_heading(self, tmp_path):
|
||||
p = tmp_path / "spec.md"
|
||||
p.write_text("# My Cool Feature\n\nSome text")
|
||||
assert _extract_spec_heading(p) == "My Cool Feature"
|
||||
|
||||
def test_quick_spec_prefix(self, tmp_path):
|
||||
p = tmp_path / "spec.md"
|
||||
p.write_text("# Quick Spec: Login Page\n")
|
||||
assert _extract_spec_heading(p) == "Login Page"
|
||||
|
||||
def test_specification_prefix(self, tmp_path):
|
||||
p = tmp_path / "spec.md"
|
||||
p.write_text("# Specification: Auth Module\n")
|
||||
assert _extract_spec_heading(p) == "Auth Module"
|
||||
|
||||
def test_no_heading(self, tmp_path):
|
||||
p = tmp_path / "spec.md"
|
||||
p.write_text("No heading here\n")
|
||||
assert _extract_spec_heading(p) is None
|
||||
|
||||
def test_missing_file(self, tmp_path):
|
||||
assert _extract_spec_heading(tmp_path / "nope.md") is None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# _extract_spec_overview
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestExtractSpecOverview:
|
||||
def test_overview_section(self, tmp_path):
|
||||
p = tmp_path / "spec.md"
|
||||
p.write_text("# Title\n\n## Overview\n\nThis is the overview.\n\n## Details\n")
|
||||
assert _extract_spec_overview(p) == "This is the overview."
|
||||
|
||||
def test_no_overview(self, tmp_path):
|
||||
p = tmp_path / "spec.md"
|
||||
p.write_text("# Title\n\n## Details\nStuff\n")
|
||||
assert _extract_spec_overview(p) is None
|
||||
|
||||
def test_missing_file(self, tmp_path):
|
||||
assert _extract_spec_overview(tmp_path / "nope.md") is None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - list_tasks
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceListTasks:
|
||||
def test_empty_specs_dir(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks() == []
|
||||
|
||||
def test_no_specs_dir(self, tmp_path):
|
||||
svc = TaskService(tmp_path / "nonexistent")
|
||||
assert svc.list_tasks() == []
|
||||
|
||||
def test_loads_tasks_from_specs(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-hello",
|
||||
plan={"feature": "Hello Feature", "status": "pending"},
|
||||
)
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"002-world",
|
||||
plan={"feature": "World Feature", "status": "in_progress"},
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
assert len(tasks) == 2
|
||||
ids = {t["spec_id"] for t in tasks}
|
||||
assert ids == {"001-hello", "002-world"}
|
||||
|
||||
def test_title_from_plan_feature(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "My Feature Title"})
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
assert tasks[0]["title"] == "My Feature Title"
|
||||
|
||||
def test_title_from_plan_title_field(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"title": "Title Field"})
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
assert tasks[0]["title"] == "Title Field"
|
||||
|
||||
def test_title_fallback_to_spec_heading(self, tmp_path):
|
||||
"""When plan title looks like a spec ID, fall back to spec.md heading."""
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "001-feat"},
|
||||
spec_md="# The Real Title\n\nContent here",
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
assert tasks[0]["title"] == "The Real Title"
|
||||
|
||||
def test_title_fallback_to_dir_name(self, tmp_path):
|
||||
"""When no plan exists and no spec.md heading, use dir name."""
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-some-slug")
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
# Dir name is the fallback, but it matches ^\d{3}- so spec heading is tried
|
||||
# Since no spec.md, stays as dir name
|
||||
assert tasks[0]["title"] == "001-some-slug"
|
||||
|
||||
def test_description_from_plan(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs, "001-feat", plan={"feature": "F", "description": "Plan desc"}
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks()[0]["description"] == "Plan desc"
|
||||
|
||||
def test_description_fallback_to_requirements(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
requirements={"task_description": "Req desc"},
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks()[0]["description"] == "Req desc"
|
||||
|
||||
def test_description_fallback_to_overview(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
spec_md="# Title\n\n## Overview\n\nOverview text.\n\n## Other\n",
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks()[0]["description"] == "Overview text."
|
||||
|
||||
def test_status_mapping(self, tmp_path):
|
||||
"""Frontend-style statuses are mapped to valid backend statuses."""
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-a", plan={"feature": "A", "status": "coding"})
|
||||
_make_spec_dir(specs, "002-b", plan={"feature": "B", "status": "completed"})
|
||||
_make_spec_dir(specs, "003-c", plan={"feature": "C", "status": "backlog"})
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = {t["spec_id"]: t for t in svc.list_tasks()}
|
||||
assert tasks["001-a"]["status"] == "in_progress"
|
||||
assert tasks["002-b"]["status"] == "done"
|
||||
assert tasks["003-c"]["status"] == "pending"
|
||||
|
||||
def test_unknown_status_defaults_to_pending(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs, "001-x", plan={"feature": "X", "status": "totally_unknown"}
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks()[0]["status"] == "pending"
|
||||
|
||||
def test_subtasks_from_phases(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
plan = {
|
||||
"feature": "F",
|
||||
"phases": [
|
||||
{
|
||||
"subtasks": [
|
||||
{"id": "s1", "description": "Do thing 1", "status": "pending"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"chunks": [
|
||||
{
|
||||
"id": "c1",
|
||||
"description": "Do chunk 1",
|
||||
"status": "completed",
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
}
|
||||
_make_spec_dir(specs, "001-feat", plan=plan)
|
||||
svc = TaskService(tmp_path)
|
||||
task = svc.list_tasks()[0]
|
||||
assert len(task["subtasks"]) == 2
|
||||
assert task["subtasks"][0]["id"] == "s1"
|
||||
assert task["subtasks"][1]["id"] == "c1"
|
||||
|
||||
def test_has_spec_and_plan_flags(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
spec_md="# Spec",
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
task = svc.list_tasks()[0]
|
||||
assert task["has_spec"] is True
|
||||
assert task["has_plan"] is True
|
||||
|
||||
def test_has_qa_report_flag(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
qa_report="# QA passed",
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks()[0]["has_qa_report"] is True
|
||||
|
||||
def test_skips_gitkeep(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
(specs / ".gitkeep").mkdir() # Directory named .gitkeep
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.list_tasks() == []
|
||||
|
||||
def test_deduplication_main_wins_over_worktree(self, tmp_path):
|
||||
"""Main project task takes priority over worktree duplicate."""
|
||||
# Main specs
|
||||
main_specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
main_specs,
|
||||
"001-feat",
|
||||
plan={"feature": "Main Version", "status": "pending"},
|
||||
)
|
||||
|
||||
# Worktree with same spec id
|
||||
wt_dir = tmp_path / ".auto-claude" / "worktrees" / "tasks" / "wt1"
|
||||
wt_specs = wt_dir / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
wt_specs,
|
||||
"001-feat",
|
||||
plan={"feature": "Worktree Version", "status": "in_progress"},
|
||||
)
|
||||
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
assert len(tasks) == 1
|
||||
assert tasks[0]["title"] == "Main Version"
|
||||
assert tasks[0]["location"] == "main"
|
||||
|
||||
def test_worktree_only_included_if_exists_in_main(self, tmp_path):
|
||||
"""Worktree tasks are only included when their spec_id exists in main."""
|
||||
main_specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
main_specs,
|
||||
"001-feat",
|
||||
plan={"feature": "Main", "status": "pending"},
|
||||
)
|
||||
|
||||
wt_dir = tmp_path / ".auto-claude" / "worktrees" / "tasks" / "wt1"
|
||||
wt_specs = wt_dir / ".auto-claude" / "specs"
|
||||
# This spec doesn't exist in main
|
||||
_make_spec_dir(
|
||||
wt_specs,
|
||||
"099-orphan",
|
||||
plan={"feature": "Orphan", "status": "done"},
|
||||
)
|
||||
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
assert len(tasks) == 1
|
||||
assert tasks[0]["spec_id"] == "001-feat"
|
||||
|
||||
def test_dedup_same_location_higher_status_wins(self, tmp_path):
|
||||
"""When both are from same location, higher status priority wins."""
|
||||
# We can't easily have two entries from the same location for the same
|
||||
# spec_id via the normal scan since directories are unique. But we can
|
||||
# verify the dedup logic via worktree entries that pass the main_spec_ids filter.
|
||||
main_specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
main_specs,
|
||||
"001-feat",
|
||||
plan={"feature": "Feat", "status": "pending"},
|
||||
)
|
||||
|
||||
# Two worktrees with same spec
|
||||
for i, status in enumerate(["in_progress", "done"], 1):
|
||||
wt = tmp_path / ".auto-claude" / "worktrees" / "tasks" / f"wt{i}"
|
||||
wt_specs = wt / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
wt_specs,
|
||||
"001-feat",
|
||||
plan={"feature": "Feat", "status": status},
|
||||
)
|
||||
|
||||
svc = TaskService(tmp_path)
|
||||
tasks = svc.list_tasks()
|
||||
# Main should win regardless
|
||||
assert len(tasks) == 1
|
||||
assert tasks[0]["location"] == "main"
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - get_task
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceGetTask:
|
||||
def test_existing_task(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F", "status": "pending"})
|
||||
svc = TaskService(tmp_path)
|
||||
task = svc.get_task("001-feat")
|
||||
assert task is not None
|
||||
assert task["spec_id"] == "001-feat"
|
||||
assert task["title"] == "F"
|
||||
|
||||
def test_missing_task(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.get_task("999-nope") is None
|
||||
|
||||
def test_get_task_from_worktree(self, tmp_path):
|
||||
# No main spec, but exists in worktree
|
||||
(tmp_path / ".auto-claude" / "specs").mkdir(parents=True)
|
||||
wt = tmp_path / ".auto-claude" / "worktrees" / "tasks" / "wt1"
|
||||
wt_specs = wt / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
wt_specs,
|
||||
"001-feat",
|
||||
plan={"feature": "From WT", "status": "in_progress"},
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
task = svc.get_task("001-feat")
|
||||
assert task is not None
|
||||
assert task["title"] == "From WT"
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - create_task
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceCreateTask:
|
||||
def test_creates_directory_and_files(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("My New Task", "Build something cool")
|
||||
assert result["title"] == "My New Task"
|
||||
assert result["status"] == "pending"
|
||||
|
||||
spec_dir = tmp_path / ".auto-claude" / "specs" / result["spec_id"]
|
||||
assert spec_dir.is_dir()
|
||||
assert (spec_dir / "implementation_plan.json").exists()
|
||||
assert (spec_dir / "requirements.json").exists()
|
||||
assert (spec_dir / "task_metadata.json").exists()
|
||||
|
||||
def test_auto_numbering(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-existing", plan={"feature": "Existing"})
|
||||
_make_spec_dir(specs, "005-jump", plan={"feature": "Jump"})
|
||||
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("Next Task", "Desc")
|
||||
# Should be 006 (one after max existing = 005)
|
||||
assert result["spec_id"].startswith("006-")
|
||||
|
||||
def test_auto_numbering_empty(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("First Task", "Desc")
|
||||
assert result["spec_id"].startswith("001-")
|
||||
|
||||
def test_slug_in_dir_name(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("Add OAuth Support!", "Desc")
|
||||
assert "add-oauth-support" in result["spec_id"]
|
||||
|
||||
def test_plan_contents(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("Title", "Description text")
|
||||
spec_dir = tmp_path / ".auto-claude" / "specs" / result["spec_id"]
|
||||
plan = json.loads((spec_dir / "implementation_plan.json").read_text())
|
||||
assert plan["feature"] == "Title"
|
||||
assert plan["title"] == "Title"
|
||||
assert plan["description"] == "Description text"
|
||||
assert plan["status"] == "pending"
|
||||
assert "created_at" in plan
|
||||
assert "updated_at" in plan
|
||||
|
||||
def test_requirements_contents(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("Title", "My desc")
|
||||
spec_dir = tmp_path / ".auto-claude" / "specs" / result["spec_id"]
|
||||
reqs = json.loads((spec_dir / "requirements.json").read_text())
|
||||
assert reqs["task_description"] == "My desc"
|
||||
|
||||
def test_metadata_contents(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.create_task("Title", "Desc")
|
||||
spec_dir = tmp_path / ".auto-claude" / "specs" / result["spec_id"]
|
||||
meta = json.loads((spec_dir / "task_metadata.json").read_text())
|
||||
assert meta["source"] == "mcp"
|
||||
assert "created_at" in meta
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - update_task
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceUpdateTask:
|
||||
def test_update_title(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "Old", "status": "pending"})
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.update_task("001-feat", title="New Title")
|
||||
assert result is not None
|
||||
assert result["title"] == "New Title"
|
||||
# Verify on disk
|
||||
plan = json.loads(
|
||||
(specs / "001-feat" / "implementation_plan.json").read_text()
|
||||
)
|
||||
assert plan["feature"] == "New Title"
|
||||
assert plan["title"] == "New Title"
|
||||
|
||||
def test_update_description(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={"feature": "F", "status": "pending"},
|
||||
requirements={"task_description": "Old desc"},
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.update_task("001-feat", description="New desc")
|
||||
assert result is not None
|
||||
# Verify requirements.json updated too
|
||||
reqs = json.loads((specs / "001-feat" / "requirements.json").read_text())
|
||||
assert reqs["task_description"] == "New desc"
|
||||
|
||||
def test_update_status(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F", "status": "pending"})
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.update_task("001-feat", status="in_progress")
|
||||
assert result is not None
|
||||
assert result["status"] == "in_progress"
|
||||
|
||||
def test_update_invalid_status(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F", "status": "pending"})
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.update_task("001-feat", status="bogus")
|
||||
assert result is None
|
||||
|
||||
def test_update_nonexistent(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.update_task("999-nope", title="X") is None
|
||||
|
||||
def test_updated_at_changes(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(
|
||||
specs,
|
||||
"001-feat",
|
||||
plan={
|
||||
"feature": "F",
|
||||
"status": "pending",
|
||||
"updated_at": "2020-01-01T00:00:00",
|
||||
},
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
svc.update_task("001-feat", title="Changed")
|
||||
plan = json.loads(
|
||||
(specs / "001-feat" / "implementation_plan.json").read_text()
|
||||
)
|
||||
assert plan["updated_at"] != "2020-01-01T00:00:00"
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - delete_task
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceDeleteTask:
|
||||
def test_delete_existing(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F"})
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.delete_task("001-feat") is True
|
||||
assert not (specs / "001-feat").exists()
|
||||
|
||||
def test_delete_nonexistent(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.delete_task("999-nope") is False
|
||||
|
||||
def test_path_traversal_blocked(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
specs.mkdir(parents=True)
|
||||
# Create a dir outside specs that a traversal would target
|
||||
outside = tmp_path / "important"
|
||||
outside.mkdir()
|
||||
(outside / "data.txt").write_text("precious")
|
||||
|
||||
svc = TaskService(tmp_path)
|
||||
# Even if the traversal path resolves to an existing directory,
|
||||
# the relative_to check should block it
|
||||
result = svc.delete_task("../../important")
|
||||
# The directory should not be deleted
|
||||
assert outside.exists()
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - update_status
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceUpdateStatus:
|
||||
def test_valid_status(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F", "status": "pending"})
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.update_status("001-feat", "done")
|
||||
assert result is not None
|
||||
assert result["status"] == "done"
|
||||
|
||||
def test_invalid_status(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "001-feat", plan={"feature": "F", "status": "pending"})
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc.update_status("001-feat", "invalid") is None
|
||||
|
||||
def test_all_valid_statuses_accepted(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
for i, status in enumerate(sorted(VALID_STATUSES), 1):
|
||||
name = f"{i:03d}-test"
|
||||
_make_spec_dir(
|
||||
specs, name, plan={"feature": f"Task {i}", "status": "pending"}
|
||||
)
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc.update_status(name, status)
|
||||
assert result is not None, f"Status '{status}' should be valid"
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# TaskService - internal helpers
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskServiceInternalHelpers:
|
||||
def test_next_spec_number_empty(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc._next_spec_number() == 1
|
||||
|
||||
def test_next_spec_number_existing(self, tmp_path):
|
||||
specs = tmp_path / ".auto-claude" / "specs"
|
||||
_make_spec_dir(specs, "003-thing")
|
||||
_make_spec_dir(specs, "010-other")
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc._next_spec_number() == 11
|
||||
|
||||
def test_find_spec_dir_in_worktrees(self, tmp_path):
|
||||
wt = tmp_path / ".auto-claude" / "worktrees" / "tasks" / "wt1"
|
||||
wt_specs = wt / ".auto-claude" / "specs"
|
||||
_make_spec_dir(wt_specs, "001-feat", plan={"feature": "F"})
|
||||
svc = TaskService(tmp_path)
|
||||
result = svc._find_spec_dir_in_worktrees("001-feat")
|
||||
assert result is not None
|
||||
assert result.name == "001-feat"
|
||||
|
||||
def test_find_spec_dir_in_worktrees_not_found(self, tmp_path):
|
||||
svc = TaskService(tmp_path)
|
||||
assert svc._find_spec_dir_in_worktrees("999-nope") is None
|
||||
|
||||
|
||||
# ===========================================================================
|
||||
# Task Tools (mcp_server.tools.tasks)
|
||||
# ===========================================================================
|
||||
|
||||
|
||||
class TestTaskTools:
|
||||
"""Tests for the MCP task_* tool functions.
|
||||
|
||||
FastMCP's @mcp.tool() wraps functions in FunctionTool objects.
|
||||
We call the underlying function via .fn to test the tool logic directly.
|
||||
"""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _setup_config(self, tmp_path, monkeypatch):
|
||||
"""Mock get_project_dir for all tool tests."""
|
||||
import mcp_server.config as config_mod
|
||||
|
||||
monkeypatch.setattr(config_mod, "_project_dir", tmp_path)
|
||||
monkeypatch.setattr(config_mod, "_auto_claude_dir", tmp_path / ".auto-claude")
|
||||
self.project_dir = tmp_path
|
||||
self.specs_dir = tmp_path / ".auto-claude" / "specs"
|
||||
|
||||
def test_task_list_empty(self):
|
||||
from mcp_server.tools.tasks import task_list
|
||||
|
||||
result = task_list.fn()
|
||||
assert result["count"] == 0
|
||||
assert result["tasks"] == []
|
||||
|
||||
def test_task_list_with_tasks(self):
|
||||
from mcp_server.tools.tasks import task_list
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-hello",
|
||||
plan={"feature": "Hello", "status": "pending", "description": "A task"},
|
||||
)
|
||||
result = task_list.fn()
|
||||
assert result["count"] == 1
|
||||
assert result["tasks"][0]["spec_id"] == "001-hello"
|
||||
assert result["tasks"][0]["title"] == "Hello"
|
||||
assert result["tasks"][0]["status"] == "pending"
|
||||
|
||||
def test_task_list_description_preview_truncated(self):
|
||||
from mcp_server.tools.tasks import task_list
|
||||
|
||||
long_desc = "x" * 300
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F", "description": long_desc},
|
||||
)
|
||||
result = task_list.fn()
|
||||
preview = result["tasks"][0]["description_preview"]
|
||||
assert len(preview) <= 204 # 200 + "..."
|
||||
assert preview.endswith("...")
|
||||
|
||||
def test_task_create_success(self):
|
||||
from mcp_server.tools.tasks import task_create
|
||||
|
||||
result = task_create.fn("New Task", "Do something")
|
||||
assert result["success"] is True
|
||||
assert "task" in result
|
||||
assert result["task"]["title"] == "New Task"
|
||||
|
||||
def test_task_create_empty_title(self):
|
||||
from mcp_server.tools.tasks import task_create
|
||||
|
||||
result = task_create.fn("", "Desc")
|
||||
assert "error" in result
|
||||
assert "Title" in result["error"]
|
||||
|
||||
def test_task_create_empty_description(self):
|
||||
from mcp_server.tools.tasks import task_create
|
||||
|
||||
result = task_create.fn("Title", "")
|
||||
assert "error" in result
|
||||
assert "Description" in result["error"]
|
||||
|
||||
def test_task_create_whitespace_title(self):
|
||||
from mcp_server.tools.tasks import task_create
|
||||
|
||||
result = task_create.fn(" ", "Desc")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_create_whitespace_description(self):
|
||||
from mcp_server.tools.tasks import task_create
|
||||
|
||||
result = task_create.fn("Title", " ")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_get_success(self):
|
||||
from mcp_server.tools.tasks import task_get
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F", "status": "pending"},
|
||||
)
|
||||
result = task_get.fn("001-feat")
|
||||
assert "task" in result
|
||||
assert result["task"]["spec_id"] == "001-feat"
|
||||
|
||||
def test_task_get_not_found(self):
|
||||
from mcp_server.tools.tasks import task_get
|
||||
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
result = task_get.fn("999-nope")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_update_success(self):
|
||||
from mcp_server.tools.tasks import task_update
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "Old", "status": "pending"},
|
||||
)
|
||||
result = task_update.fn("001-feat", title="New Title", status="in_progress")
|
||||
assert result["success"] is True
|
||||
assert result["task"]["title"] == "New Title"
|
||||
assert result["task"]["status"] == "in_progress"
|
||||
|
||||
def test_task_update_invalid_status(self):
|
||||
from mcp_server.tools.tasks import task_update
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F", "status": "pending"},
|
||||
)
|
||||
result = task_update.fn("001-feat", status="bogus")
|
||||
assert "error" in result
|
||||
assert "Invalid status" in result["error"]
|
||||
|
||||
def test_task_update_not_found(self):
|
||||
from mcp_server.tools.tasks import task_update
|
||||
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
result = task_update.fn("999-nope", title="X")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_delete_success(self):
|
||||
from mcp_server.tools.tasks import task_delete
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F"},
|
||||
)
|
||||
result = task_delete.fn("001-feat")
|
||||
assert result["success"] is True
|
||||
assert not (self.specs_dir / "001-feat").exists()
|
||||
|
||||
def test_task_delete_not_found(self):
|
||||
from mcp_server.tools.tasks import task_delete
|
||||
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
result = task_delete.fn("999-nope")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_update_status_success(self):
|
||||
from mcp_server.tools.tasks import task_update_status
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F", "status": "pending"},
|
||||
)
|
||||
result = task_update_status.fn("001-feat", "done")
|
||||
assert result["success"] is True
|
||||
assert result["task"]["status"] == "done"
|
||||
|
||||
def test_task_update_status_invalid(self):
|
||||
from mcp_server.tools.tasks import task_update_status
|
||||
|
||||
_make_spec_dir(
|
||||
self.specs_dir,
|
||||
"001-feat",
|
||||
plan={"feature": "F", "status": "pending"},
|
||||
)
|
||||
result = task_update_status.fn("001-feat", "invalid")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_update_status_not_found(self):
|
||||
from mcp_server.tools.tasks import task_update_status
|
||||
|
||||
self.specs_dir.mkdir(parents=True, exist_ok=True)
|
||||
result = task_update_status.fn("999-nope", "done")
|
||||
assert "error" in result
|
||||
|
||||
|
||||
class TestTaskToolsProjectNotInitialized:
|
||||
"""Test that tools return errors when project is not initialized."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _setup_uninitialized(self, monkeypatch):
|
||||
"""Make get_project_dir raise RuntimeError."""
|
||||
import mcp_server.config as config_mod
|
||||
|
||||
monkeypatch.setattr(config_mod, "_project_dir", None)
|
||||
|
||||
def test_task_list_error(self):
|
||||
from mcp_server.tools.tasks import task_list
|
||||
|
||||
result = task_list.fn()
|
||||
assert "error" in result
|
||||
|
||||
def test_task_create_error(self):
|
||||
from mcp_server.tools.tasks import task_create
|
||||
|
||||
result = task_create.fn("Title", "Desc")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_get_error(self):
|
||||
from mcp_server.tools.tasks import task_get
|
||||
|
||||
result = task_get.fn("001")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_update_error(self):
|
||||
from mcp_server.tools.tasks import task_update
|
||||
|
||||
result = task_update.fn("001", title="X")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_delete_error(self):
|
||||
from mcp_server.tools.tasks import task_delete
|
||||
|
||||
result = task_delete.fn("001")
|
||||
assert "error" in result
|
||||
|
||||
def test_task_update_status_error(self):
|
||||
from mcp_server.tools.tasks import task_update_status
|
||||
|
||||
result = task_update_status.fn("001", "done")
|
||||
assert "error" in result
|
||||
@@ -1,345 +0,0 @@
|
||||
"""
|
||||
Tests for mcp_server/services/workspace_service.py
|
||||
====================================================
|
||||
|
||||
Tests worktree listing, diff retrieval (with truncation), merge strategies,
|
||||
discard, and PR creation. WorktreeManager is fully mocked.
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to sys.path
|
||||
_backend = Path(__file__).parent.parent / "apps" / "backend"
|
||||
if str(_backend) not in sys.path:
|
||||
sys.path.insert(0, str(_backend))
|
||||
|
||||
# Pre-mock SDK modules before any mcp_server imports
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
if "claude_agent_sdk" not in sys.modules:
|
||||
sys.modules["claude_agent_sdk"] = MagicMock()
|
||||
sys.modules["claude_agent_sdk.types"] = MagicMock()
|
||||
|
||||
import pytest
|
||||
|
||||
from mcp_server.services.workspace_service import WorkspaceService
|
||||
|
||||
|
||||
def _mock_worktree_info(
|
||||
spec_name="001-feat",
|
||||
branch="auto-claude/001-feat",
|
||||
path="/tmp/wt/001-feat",
|
||||
base_branch="main",
|
||||
is_active=True,
|
||||
commit_count=3,
|
||||
files_changed=5,
|
||||
additions=100,
|
||||
deletions=20,
|
||||
days_since_last_commit=1,
|
||||
last_commit_date=None,
|
||||
):
|
||||
"""Create a mock worktree info object."""
|
||||
info = MagicMock()
|
||||
info.spec_name = spec_name
|
||||
info.branch = branch
|
||||
info.path = Path(path)
|
||||
info.base_branch = base_branch
|
||||
info.is_active = is_active
|
||||
info.commit_count = commit_count
|
||||
info.files_changed = files_changed
|
||||
info.additions = additions
|
||||
info.deletions = deletions
|
||||
info.days_since_last_commit = days_since_last_commit
|
||||
info.last_commit_date = last_commit_date
|
||||
return info
|
||||
|
||||
|
||||
class TestListWorktrees:
|
||||
"""Tests for WorkspaceService.list_worktrees()."""
|
||||
|
||||
def test_returns_worktree_list(self, tmp_path):
|
||||
"""list_worktrees returns formatted worktree data."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
wt = _mock_worktree_info()
|
||||
mock_manager.list_all_worktrees.return_value = [wt]
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.list_worktrees()
|
||||
|
||||
assert result["count"] == 1
|
||||
assert result["worktrees"][0]["spec_name"] == "001-feat"
|
||||
assert result["worktrees"][0]["branch"] == "auto-claude/001-feat"
|
||||
assert result["worktrees"][0]["is_active"] is True
|
||||
|
||||
def test_returns_empty_list(self, tmp_path):
|
||||
"""list_worktrees returns empty list when no worktrees exist."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.list_all_worktrees.return_value = []
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.list_worktrees()
|
||||
|
||||
assert result["count"] == 0
|
||||
assert result["worktrees"] == []
|
||||
|
||||
def test_handles_import_error(self, tmp_path):
|
||||
"""list_worktrees returns error when backend unavailable."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
|
||||
with patch.object(svc, "_get_manager", side_effect=ImportError("no module")):
|
||||
result = svc.list_worktrees()
|
||||
|
||||
assert "error" in result
|
||||
|
||||
def test_handles_manager_exception(self, tmp_path):
|
||||
"""list_worktrees returns error on unexpected manager exception."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.list_all_worktrees.side_effect = RuntimeError("git error")
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.list_worktrees()
|
||||
|
||||
assert "error" in result
|
||||
|
||||
def test_includes_optional_fields(self, tmp_path):
|
||||
"""list_worktrees includes days_since_last_commit when available."""
|
||||
from datetime import datetime, timezone
|
||||
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
dt = datetime(2026, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
|
||||
wt = _mock_worktree_info(days_since_last_commit=5, last_commit_date=dt)
|
||||
mock_manager.list_all_worktrees.return_value = [wt]
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.list_worktrees()
|
||||
|
||||
entry = result["worktrees"][0]
|
||||
assert entry["days_since_last_commit"] == 5
|
||||
assert "last_commit_date" in entry
|
||||
|
||||
def test_omits_none_optional_fields(self, tmp_path):
|
||||
"""list_worktrees omits optional fields when None."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
wt = _mock_worktree_info(days_since_last_commit=None, last_commit_date=None)
|
||||
mock_manager.list_all_worktrees.return_value = [wt]
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.list_worktrees()
|
||||
|
||||
entry = result["worktrees"][0]
|
||||
assert "days_since_last_commit" not in entry
|
||||
assert "last_commit_date" not in entry
|
||||
|
||||
|
||||
class TestGetDiff:
|
||||
"""Tests for WorkspaceService.get_diff()."""
|
||||
|
||||
def test_returns_diff_content(self, tmp_path):
|
||||
"""get_diff returns diff data with changed files."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
info = _mock_worktree_info()
|
||||
mock_manager.get_worktree_info.return_value = info
|
||||
mock_manager.get_changed_files.return_value = [("M", "src/main.py")]
|
||||
mock_manager.get_change_summary.return_value = "1 file changed"
|
||||
|
||||
mock_git_result = MagicMock()
|
||||
mock_git_result.returncode = 0
|
||||
mock_git_result.stdout = "diff --git a/src/main.py b/src/main.py\n+new line"
|
||||
|
||||
# run_git is imported locally inside get_diff via `from core.git_executable import run_git`
|
||||
mock_git_module = MagicMock()
|
||||
mock_git_module.run_git = MagicMock(return_value=mock_git_result)
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager), \
|
||||
patch.dict(sys.modules, {"core.git_executable": mock_git_module}):
|
||||
result = svc.get_diff("001-feat")
|
||||
|
||||
assert result["spec_id"] == "001-feat"
|
||||
assert len(result["changed_files"]) == 1
|
||||
assert result["changed_files"][0]["status"] == "M"
|
||||
|
||||
def test_returns_error_for_missing_worktree(self, tmp_path):
|
||||
"""get_diff returns error when worktree not found."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.get_worktree_info.return_value = None
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.get_diff("999-missing")
|
||||
|
||||
assert "error" in result
|
||||
assert "No worktree" in result["error"]
|
||||
|
||||
def test_handles_import_error(self, tmp_path):
|
||||
"""get_diff returns error when backend unavailable."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
|
||||
with patch.object(svc, "_get_manager", side_effect=ImportError("no module")):
|
||||
result = svc.get_diff("001-feat")
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
class TestMerge:
|
||||
"""Tests for WorkspaceService.merge()."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_auto_strategy(self, tmp_path):
|
||||
"""merge with 'auto' strategy calls merge_worktree without no_commit."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.merge_worktree.return_value = True
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.merge("001-feat", strategy="auto")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["strategy"] == "auto"
|
||||
mock_manager.merge_worktree.assert_called_once_with(
|
||||
"001-feat", delete_after=False, no_commit=False
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_no_commit_strategy(self, tmp_path):
|
||||
"""merge with 'no-commit' strategy passes no_commit=True."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.merge_worktree.return_value = True
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.merge("001-feat", strategy="no-commit")
|
||||
|
||||
assert result["success"] is True
|
||||
mock_manager.merge_worktree.assert_called_once_with(
|
||||
"001-feat", delete_after=False, no_commit=True
|
||||
)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_failure(self, tmp_path):
|
||||
"""merge returns success=False when merge_worktree returns False."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.merge_worktree.return_value = False
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.merge("001-feat")
|
||||
|
||||
assert result["success"] is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_handles_exception(self, tmp_path):
|
||||
"""merge returns error on unexpected exception."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.merge_worktree.side_effect = RuntimeError("conflict")
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.merge("001-feat")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_handles_import_error(self, tmp_path):
|
||||
"""merge returns error when backend unavailable."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
|
||||
with patch.object(svc, "_get_manager", side_effect=ImportError("no module")):
|
||||
result = await svc.merge("001-feat")
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
class TestDiscard:
|
||||
"""Tests for WorkspaceService.discard()."""
|
||||
|
||||
def test_discard_success(self, tmp_path):
|
||||
"""discard calls remove_worktree and returns success."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.discard("001-feat")
|
||||
|
||||
assert result["success"] is True
|
||||
mock_manager.remove_worktree.assert_called_once_with(
|
||||
"001-feat", delete_branch=True
|
||||
)
|
||||
|
||||
def test_discard_handles_exception(self, tmp_path):
|
||||
"""discard returns error on unexpected exception."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.remove_worktree.side_effect = RuntimeError("git error")
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = svc.discard("001-feat")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
def test_discard_handles_import_error(self, tmp_path):
|
||||
"""discard returns error when backend unavailable."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
|
||||
with patch.object(svc, "_get_manager", side_effect=ImportError("no module")):
|
||||
result = svc.discard("001-feat")
|
||||
|
||||
assert "error" in result
|
||||
|
||||
|
||||
class TestCreatePR:
|
||||
"""Tests for WorkspaceService.create_pr()."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_pr_success(self, tmp_path):
|
||||
"""create_pr returns success with PR URL."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.push_and_create_pr.return_value = {
|
||||
"success": True,
|
||||
"pr_url": "https://github.com/org/repo/pull/42",
|
||||
"branch": "auto-claude/001-feat",
|
||||
"provider": "github",
|
||||
}
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.create_pr("001-feat", title="Add feature")
|
||||
|
||||
assert result["success"] is True
|
||||
assert result["pr_url"] == "https://github.com/org/repo/pull/42"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_pr_failure(self, tmp_path):
|
||||
"""create_pr returns error from manager."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.push_and_create_pr.return_value = {
|
||||
"success": False,
|
||||
"error": "Remote rejected push",
|
||||
}
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.create_pr("001-feat")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_pr_handles_exception(self, tmp_path):
|
||||
"""create_pr returns error on unexpected exception."""
|
||||
svc = WorkspaceService(tmp_path)
|
||||
mock_manager = MagicMock()
|
||||
mock_manager.push_and_create_pr.side_effect = RuntimeError("network error")
|
||||
|
||||
with patch.object(svc, "_get_manager", return_value=mock_manager):
|
||||
result = await svc.create_pr("001-feat")
|
||||
|
||||
assert result["success"] is False
|
||||
assert "error" in result
|
||||
@@ -18,17 +18,22 @@ import pytest
|
||||
# services/ package at both apps/backend/services/ and runners/github/services/.
|
||||
# To avoid collision, add the github services dir directly and import bare module names.
|
||||
_backend_dir = Path(__file__).parent.parent / "apps" / "backend"
|
||||
_github_services_dir = _backend_dir / "runners" / "github" / "services"
|
||||
_github_runner_dir = _backend_dir / "runners" / "github"
|
||||
_github_services_dir = _github_runner_dir / "services"
|
||||
if str(_backend_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_backend_dir))
|
||||
if str(_github_runner_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_github_runner_dir))
|
||||
if str(_github_services_dir) not in sys.path:
|
||||
sys.path.insert(0, str(_github_services_dir))
|
||||
|
||||
from agents.tools_pkg.models import AGENT_CONFIGS
|
||||
from pydantic_models import (
|
||||
ExtractedFindingSummary,
|
||||
FollowupExtractionResponse,
|
||||
ParallelFollowupResponse,
|
||||
)
|
||||
from recovery_utils import create_finding_from_summary
|
||||
from sdk_utils import RECOVERABLE_ERRORS
|
||||
|
||||
|
||||
@@ -53,20 +58,38 @@ class TestFollowupExtractionResponse:
|
||||
assert resp.dismissed_finding_count == 0
|
||||
|
||||
def test_full_valid_response(self):
|
||||
"""Accepts fully populated response."""
|
||||
"""Accepts fully populated response with ExtractedFindingSummary objects."""
|
||||
resp = FollowupExtractionResponse(
|
||||
verdict="READY_TO_MERGE",
|
||||
verdict_reasoning="All findings resolved",
|
||||
resolved_finding_ids=["NCR-001", "NCR-002"],
|
||||
unresolved_finding_ids=[],
|
||||
new_finding_summaries=["HIGH: potential cleanup issue in batch_commands.py"],
|
||||
new_finding_summaries=[
|
||||
ExtractedFindingSummary(
|
||||
severity="HIGH",
|
||||
description="potential cleanup issue in batch_commands.py",
|
||||
file="apps/backend/cli/batch_commands.py",
|
||||
line=42,
|
||||
)
|
||||
],
|
||||
confirmed_finding_count=1,
|
||||
dismissed_finding_count=1,
|
||||
)
|
||||
assert len(resp.resolved_finding_ids) == 2
|
||||
assert len(resp.new_finding_summaries) == 1
|
||||
assert resp.new_finding_summaries[0].file == "apps/backend/cli/batch_commands.py"
|
||||
assert resp.new_finding_summaries[0].line == 42
|
||||
assert resp.confirmed_finding_count == 1
|
||||
|
||||
def test_finding_summary_defaults(self):
|
||||
"""ExtractedFindingSummary defaults file='unknown' and line=0."""
|
||||
summary = ExtractedFindingSummary(
|
||||
severity="MEDIUM",
|
||||
description="Some issue without location",
|
||||
)
|
||||
assert summary.file == "unknown"
|
||||
assert summary.line == 0
|
||||
|
||||
def test_schema_is_small(self):
|
||||
"""Schema should be significantly smaller than ParallelFollowupResponse."""
|
||||
extraction_schema = json.dumps(
|
||||
@@ -75,10 +98,11 @@ class TestFollowupExtractionResponse:
|
||||
followup_schema = json.dumps(
|
||||
ParallelFollowupResponse.model_json_schema()
|
||||
)
|
||||
# Extraction schema should be less than half the size of the full schema
|
||||
assert len(extraction_schema) < len(followup_schema) / 2, (
|
||||
# Actual ratio is ~50.7% after adding ExtractedFindingSummary nesting.
|
||||
# Threshold at 55% gives headroom while still guarding against schema bloat.
|
||||
assert len(extraction_schema) < len(followup_schema) * 0.55, (
|
||||
f"Extraction schema ({len(extraction_schema)} chars) should be "
|
||||
f"less than half of full schema ({len(followup_schema)} chars)"
|
||||
f"less than 55% of full schema ({len(followup_schema)} chars)"
|
||||
)
|
||||
|
||||
def test_all_verdict_values_accepted(self):
|
||||
@@ -143,3 +167,81 @@ class TestAgentConfigRegistration:
|
||||
"""Extraction agent should use low thinking (lightweight call)."""
|
||||
config = AGENT_CONFIGS["pr_followup_extraction"]
|
||||
assert config["thinking_default"] == "low"
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test create_finding_from_summary with file/line params
|
||||
# ============================================================================
|
||||
|
||||
|
||||
class TestCreateFindingFromSummary:
|
||||
"""Tests for create_finding_from_summary with file/line support."""
|
||||
|
||||
def test_backward_compatible_defaults(self):
|
||||
"""Calling without file/line still produces file='unknown', line=0."""
|
||||
finding = create_finding_from_summary("HIGH: some issue", 0)
|
||||
assert finding.file == "unknown"
|
||||
assert finding.line == 0
|
||||
assert finding.severity.value == "high"
|
||||
|
||||
def test_file_and_line_passed_through(self):
|
||||
"""File and line params are used in the resulting finding."""
|
||||
finding = create_finding_from_summary(
|
||||
summary="Missing null check",
|
||||
index=0,
|
||||
file="src/parser.py",
|
||||
line=42,
|
||||
)
|
||||
assert finding.file == "src/parser.py"
|
||||
assert finding.line == 42
|
||||
|
||||
def test_severity_override(self):
|
||||
"""severity_override takes precedence over parsed severity."""
|
||||
finding = create_finding_from_summary(
|
||||
summary="HIGH: some issue",
|
||||
index=0,
|
||||
severity_override="CRITICAL",
|
||||
)
|
||||
assert finding.severity.value == "critical"
|
||||
|
||||
def test_severity_override_case_insensitive(self):
|
||||
"""severity_override works regardless of case."""
|
||||
finding = create_finding_from_summary(
|
||||
summary="some issue",
|
||||
index=0,
|
||||
severity_override="high",
|
||||
)
|
||||
assert finding.severity.value == "high"
|
||||
|
||||
def test_severity_override_invalid_falls_back(self):
|
||||
"""Invalid severity_override falls back to parsed severity."""
|
||||
finding = create_finding_from_summary(
|
||||
summary="LOW: minor issue",
|
||||
index=0,
|
||||
severity_override="UNKNOWN",
|
||||
)
|
||||
# Falls back to parsed "LOW" from summary
|
||||
assert finding.severity.value == "low"
|
||||
|
||||
def test_id_prefix(self):
|
||||
"""Custom id_prefix is used in the finding ID."""
|
||||
finding = create_finding_from_summary(
|
||||
summary="some issue", index=0, id_prefix="FU"
|
||||
)
|
||||
assert finding.id.startswith("FU-")
|
||||
|
||||
def test_all_params_together(self):
|
||||
"""All new params work together correctly."""
|
||||
finding = create_finding_from_summary(
|
||||
summary="Regex issue in subtask title truncation",
|
||||
index=3,
|
||||
id_prefix="FU",
|
||||
severity_override="MEDIUM",
|
||||
file="apps/backend/agents/planner.py",
|
||||
line=187,
|
||||
)
|
||||
assert finding.id.startswith("FU-")
|
||||
assert finding.severity.value == "medium"
|
||||
assert finding.file == "apps/backend/agents/planner.py"
|
||||
assert finding.line == 187
|
||||
assert "Regex issue" in finding.title
|
||||
|
||||
Reference in New Issue
Block a user