Compare commits

..

13 Commits

Author SHA1 Message Date
Andy 30855be0c8 Merge branch 'develop' into fix/roadmap-tasks 2026-02-12 12:45:45 +01:00
AndyMik90 a96ce9164f fix(roadmap): clear task_outcome in IPC handler and add test coverage
- ROADMAP_UPDATE_FEATURE handler now clears task_outcome and
  previous_status when status moves away from done, matching the
  renderer store behavior
- Add tests for markFeatureDoneBySpecId (previousStatus preservation,
  taskOutcome setting, feature isolation)
- Add tests for updateFeatureStatus clearing taskOutcome/previousStatus
2026-02-12 12:05:55 +01:00
AndyMik90 b14c4f4bec fix(roadmap): clear taskOutcome when feature is moved away from done
When dragging a feature out of the 'done' column via Kanban, clear
taskOutcome and previousStatus so stale outcome badges don't persist.
2026-02-12 11:07:59 +01:00
Andy d0478f4ec1 Merge branch 'develop' into fix/roadmap-tasks 2026-02-12 10:47:01 +01:00
AndyMik90 487f90b5b8 fix(roadmap): preserve previousStatus in renderer and guard empty task list
- Add previousStatus preservation to markFeatureDoneBySpecId so renderer
  path matches backend behavior for unarchive revert
- Guard reconcileLinkedFeatures against empty task arrays to prevent
  falsely marking all linked features as deleted
- Fix broken code fence in CLAUDE.md (2 backticks → 3)
2026-02-12 10:41:23 +01:00
AndyMik90 6d3a524bda fix(roadmap): round-trip previous_status and add backend completed handling
- Add previousStatus to RoadmapFeature interface so it survives
  renderer-initiated saves through the ROADMAP_SAVE handler
- Map previous_status in both ROADMAP_GET and ROADMAP_SAVE handlers
- Add backend-side roadmap update on PR creation so completed outcome
  is handled server-side like deleted and archived outcomes
2026-02-12 10:23:22 +01:00
AndyMik90 bf05d58e33 update to .md 2026-02-12 10:20:57 +01:00
AndyMik90 6602b83054 fix(roadmap): preserve original status on outcome update and fix deletion ordering
- Save previous_status before overwriting to 'done' so unarchive restores
  the correct original status instead of always defaulting to 'in_progress'
- Move roadmap feature update after hasErrors check in task deletion so
  roadmap is only updated on successful deletion
2026-02-12 09:55:10 +01:00
AndyMik90 079dcb6341 fix(roadmap): revert feature state when task is unarchived
When unarchiveTasks() is called, linked roadmap features are now reverted
from status='done'/taskOutcome='archived' back to status='in_progress'
with taskOutcome cleared. Without this, unarchived tasks left their
roadmap features permanently stuck in the archived state.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 09:29:59 +01:00
AndyMik90 d769d0a5a7 refactor(roadmap): extract TaskOutcome type and shared badge component
- Extract TaskOutcome type alias in shared/types/roadmap.ts, replacing
  inline union types across 5 locations (follows codebase convention)
- Create TaskOutcomeBadge shared component with consistent icon/color
  per outcome: completed=CheckCircle2/green, archived=Archive/green,
  deleted=Trash2/muted — eliminates duplicated rendering logic across
  SortableFeatureCard, FeatureCard, FeatureDetailPanel, PhaseCard
- Use text-muted-foreground for deleted outcome instead of misleading
  green success styling in all views

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 09:14:02 +01:00
AndyMik90 ccdd721d9c fix(roadmap): address follow-up PR review findings
- Fix relative path bug: use path.join(project.path, AUTO_BUILD_PATHS)
  instead of path.join(autoBuildPath, 'roadmap') which produced relative
  paths causing roadmap updates to silently fail
- Allow taskOutcome transitions on already-done features (e.g.,
  completed→deleted) by relaxing the status check condition
- Extract withFileLock into shared file-lock.ts module so roadmap-utils
  and roadmap-handlers use the same lock map for cross-module coordination
- Show Trash2 icon for deleted tasks in PhaseCard instead of misleading
  green checkmark (visual distinction from completed)
- Remove unused writeFileAtomicSync import from crud-handlers.ts

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 08:43:34 +01:00
AndyMik90 e0e114dc9f fix(roadmap): address PR review findings
- Extract shared updateRoadmapFeatureOutcome utility with file locking
  and retry logic (eliminates duplication between crud-handlers and
  project-store, matches established roadmap-handlers pattern)
- Fix stale Zustand state read in useIpc.ts — re-read state after
  markFeatureDoneBySpecId mutation to persist correct data
- Add .catch() to saveRoadmap call in useIpc.ts for error handling
- Add Archive icon for archived outcome in PhaseCard (consistency with
  FeatureCard, SortableFeatureCard, and FeatureDetailPanel)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-12 08:15:34 +01:00
AndyMik90 1a893c05bb feat(roadmap): sync roadmap features with task lifecycle
When a roadmap feature is linked to a task (via linkedSpecId), the feature
now automatically updates when the task is completed, deleted, or archived.
Previously, features would show a broken "Go to Task" button pointing to
non-existent tasks.

- Add taskOutcome field to RoadmapFeature type
- Hook into task status changes (IPC listener) for real-time sync
- Update linked features on task deletion (main process)
- Update linked features on task archival (main process)
- Add startup reconciliation to catch missed updates
- Show status badges instead of broken "Go to Task" buttons
- Use AUTO_BUILD_PATHS constants and writeFileAtomicSync for consistency
- Add i18n translations (en/fr) for task outcome labels

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-11 22:10:03 +01:00
525 changed files with 8591 additions and 69972 deletions
+4 -31
View File
@@ -127,13 +127,6 @@ fi
if git diff --cached --name-only | grep -q "^apps/backend/.*\.py$"; then
echo "Python changes detected, running backend checks..."
# Detect if we're in a worktree
IS_WORKTREE=false
if [ -f ".git" ]; then
# .git is a file (not directory) in worktrees
IS_WORKTREE=true
fi
# Determine ruff command (venv or global)
RUFF=""
if [ -f "apps/backend/.venv/bin/ruff" ]; then
@@ -165,16 +158,7 @@ if git diff --cached --name-only | grep -q "^apps/backend/.*\.py$"; then
echo "$STAGED_PY_FILES" | xargs git add
fi
else
if [ "$IS_WORKTREE" = true ]; then
echo ""
echo "⚠️ WARNING: ruff not available in this worktree."
echo " Python linting checks will be skipped."
echo " This is expected for auto-claude worktrees."
echo " Full validation will occur when PR is created/merged."
echo ""
else
echo "Warning: ruff not found, skipping Python linting. Install with: uv pip install ruff"
fi
echo "Warning: ruff not found, skipping Python linting. Install with: uv pip install ruff"
fi
# Run pytest (skip slow/integration tests and Windows-incompatible tests for pre-commit speed)
@@ -208,28 +192,17 @@ if git diff --cached --name-only | grep -q "^apps/backend/.*\.py$"; then
elif [ -d "apps/backend/.venv" ]; then
echo "Warning: venv exists but Python not found in it, using system Python"
PYTHONPATH=apps/backend python -m pytest tests/ -v --tb=short -x -m "not slow and not integration" -k "not windows_path" $IGNORE_TESTS
elif [ "$IS_WORKTREE" = true ]; then
echo ""
echo "⚠️ WARNING: Python venv not available in this worktree."
echo " Python tests will be skipped."
echo " This is expected for auto-claude worktrees."
echo " Full validation will occur when PR is created/merged."
echo ""
exit 77 # GNU convention for 'test skipped' (avoids pytest exit-code collision)
else
echo "Warning: No .venv found in apps/backend, using system Python"
PYTHONPATH=apps/backend python -m pytest tests/ -v --tb=short -x -m "not slow and not integration" -k "not windows_path" $IGNORE_TESTS
fi
)
PYTHON_EXIT=$?
if [ $PYTHON_EXIT -eq 77 ]; then
echo "Backend checks passed! (Python tests skipped — worktree)"
elif [ $PYTHON_EXIT -ne 0 ]; then
if [ $? -ne 0 ]; then
echo "Python tests failed. Please fix failing tests before committing."
exit 1
else
echo "Backend checks passed!"
fi
echo "Backend checks passed!"
fi
# =============================================================================
+20 -12
View File
@@ -97,8 +97,9 @@ repos:
- id: ruff-format
files: ^apps/backend/
# Python tests (apps/backend/) - run full test suite from project root
# Python tests (apps/backend/) - skip slow/integration tests for pre-commit speed
# Tests to skip: graphiti (external deps), merge_file_tracker/service_orchestrator/worktree/workspace (Windows path/git issues)
# NOTE: Skip this hook in worktrees (where .git is a file, not a directory)
- repo: local
hooks:
- id: pytest
@@ -107,24 +108,31 @@ repos:
args:
- -c
- |
# Run pytest directly from project root
if [ -f "apps/backend/.venv/bin/pytest" ]; then
PYTEST_CMD="apps/backend/.venv/bin/pytest"
elif [ -f "apps/backend/.venv/Scripts/pytest.exe" ]; then
PYTEST_CMD="apps/backend/.venv/Scripts/pytest.exe"
# Skip in worktrees - .git is a file pointing to main repo, not a directory
# This prevents path resolution issues with ../../tests/ in worktree context
if [ -f ".git" ]; then
echo "Skipping pytest in worktree (path resolution would fail)"
exit 0
fi
cd apps/backend
if [ -f ".venv/bin/pytest" ]; then
PYTEST_CMD=".venv/bin/pytest"
elif [ -f ".venv/Scripts/pytest.exe" ]; then
PYTEST_CMD=".venv/Scripts/pytest.exe"
else
PYTEST_CMD="python -m pytest"
fi
$PYTEST_CMD tests/ \
PYTHONPATH=. $PYTEST_CMD \
../../tests/ \
-v \
--tb=short \
-x \
-m "not slow and not integration" \
--ignore=tests/test_graphiti.py \
--ignore=tests/test_merge_file_tracker.py \
--ignore=tests/test_service_orchestrator.py \
--ignore=tests/test_worktree.py \
--ignore=tests/test_workspace.py
--ignore=../../tests/test_graphiti.py \
--ignore=../../tests/test_merge_file_tracker.py \
--ignore=../../tests/test_service_orchestrator.py \
--ignore=../../tests/test_worktree.py \
--ignore=../../tests/test_workspace.py
language: system
files: ^(apps/backend/.*\.py$|tests/.*\.py$)
pass_filenames: false
@@ -1,240 +0,0 @@
# GitHub Issues Documentation Design
**Date:** 2025-02-16
**Status:** Approved
**Author:** Claude (Superpowers Brainstorming)
## Overview
Create comprehensive, user-friendly documentation for Auto Claude's GitHub Issues integration. The documentation will serve a mixed audience (new and existing users) with progressive depth across three documents.
## Target Audience
**Mixed audience with progressive depth:**
- **End users** - New to Auto Claude, want to understand features and get started quickly
- **Technical users** - Existing users wanting to optimize AI configuration and manage costs
- **Pro users/developers** - Want to customize prompts, context injection, and extend the system
## Document Structure
### New Directory: `guides/github-issues/`
```
guides/
└── github-issues/
├── README.md # Navigation index
├── github-issues-user-guide.md # Document 1
├── github-issues-advanced-ai-configuration.md # Document 2
└── github-issues-customization-guide.md # Document 3
```
### Document 1: User Guide
**Audience:** End users (non-technical)
**Focus:** Features, workflows, getting started
**Sections:**
1. **Overview** - What is GitHub Issues integration? Why use it?
2. **Quick Start (5 minutes)** - Get your first issue investigated end-to-end
3. **Key Features** - Bullet points of main capabilities
4. **Integration Workflow** - Import → Investigate → Create Task → Implement (EMPHASIS)
5. **Setup & Configuration** - GitHub authentication, connecting repos
6. **Using the Features**
- Importing & browsing issues
- Running AI investigations
- Creating tasks from results
- Posting findings back to GitHub
7. **FAQ** - Common questions
**Tone:** Friendly, encouraging, approachable. Plain English with minimal jargon.
### Document 2: Advanced AI Configuration
**Audience:** Technical users, team leads
**Focus:** AI tuning, performance, cost management
**Sections:**
1. **Overview** - Who this guide is for
2. **Opus 4.6 Features** - Fast Mode, 128K tokens, adaptive thinking
3. **The 4 Specialist Agents** - Deep dive into each agent's role
4. **Pricing & Cost Management** - Token limits, cost optimization strategies
5. **Advanced Configuration** - Per-specialist settings, performance tuning
6. **Technical Architecture** - How investigation works under the hood
**Tone:** Professional, technical but clear. Explains technical concepts in context.
### Document 3: Customization Guide
**Audience:** Developers, pro users extending Auto Claude
**Focus:** System customization, prompts, context injection
**Sections:**
1. **Overview** - Who this is for (developers extending Auto Claude)
2. **Prompt System Architecture** - How agent prompts are structured
3. **Context Injection System** - How context is built and passed to agents
4. **Customizing Agent Prompts** - Modifying XML prompt files
- Finding the prompt files
- Prompt structure and variables
- Creating custom investigation specialists
5. **Context Configuration** - Customizing what data is included
- File selection patterns
- Context window management
- Repository context settings
6. **Adding Custom Specialists** - Creating new investigation agents
7. **Extending the Integration** - Hooks, custom providers
8. **Examples & Recipes** - Common customizations
**Tone:** Developer-to-developer, technical, code-heavy, minimal hand-holding.
## Content Approach
### Progressive Disclosure
- Each document builds on the previous one
- Clear navigation links between documents
- Users can enter at their appropriate level
### Writing Style Guidelines
| Document | Tone | Language | Example Style |
|----------|------|----------|---------------|
| User Guide | Friendly, encouraging | Plain English | "Think of AI investigation as having a senior developer analyze the issue for you" |
| Advanced Config | Professional, technical | Terms explained in context | "Fast Mode uses optimized token generation to reduce investigation time by 2.5x" |
| Customization | Developer-to-developer | Technical, code-heavy | "Modify the `<system_context>` variable in `prompts/github/root_cause.xml`" |
### Code & Configuration Examples
- **Doc 1:** Simple copy-paste examples, UI navigation
- **Doc 2:** Configuration snippets, environment variables
- **Doc 3:** Full XML/Python examples, file paths, code blocks
## Visual Elements
### Screenshots (Hybrid Approach)
**User Guide:**
1. GitHub Issues main UI (issue list, filters, actions)
2. Issue detail view (comments, labels, "Investigate" button)
3. Investigation in progress (4 parallel specialists)
4. Investigation results (completed report)
5. Settings screen (GitHub auth, repo connection, Fast Mode toggle)
**Advanced AI Config:**
1. Settings → AI Investigation panel (configurable options)
2. Token usage display (cost visibility)
3. Investigation pipeline flowchart (issue → 4 specialists → report)
**Customization Guide:**
1. Directory structure diagram (prompt file locations)
2. Annotated XML prompt file (variables and structure)
3. Context injection flow diagram
4. Code snippets throughout
### Diagram Style
- Clean, simple flowcharts
- Consistent color scheme: Blue (user actions), Green (AI agents), Orange (data flow)
- Minimal text, focus on flow and relationships
## Navigation & Cross-References
### Linking Strategy
**User Guide → Deeper:**
- "For detailed configuration, see [Advanced AI Configuration]"
- "Learn how the 4 specialists work in [Advanced AI Configuration]"
- "Want to customize agent behavior? See [Customization Guide]"
**Advanced AI Config → Both Directions:**
- "New to GitHub Issues? Start with the [User Guide]"
- "Want to modify agent prompts? See [Customization Guide]"
**Customization Guide → Reference:**
- "Assumes familiarity with concepts from [User Guide] and [Advanced AI Config]"
### Navigation Aids
- Table of Contents at the top of each document
- "In this section" callouts at the start of major sections
- "Next steps" boxes at the end of each workflow section
### External References
- `guides/opus-4.6-features.md` - Opus 4.6 details (don't duplicate)
- `ARCHITECTURE.md` - System architecture
- GitHub CLI docs - Authentication setup
## Key Emphasis
**Integration Workflow** is the primary focus across all documents:
> **Import Issues** → **AI Investigation** → **Create Task** → **Implement** → **Merge**
This pipeline demonstrates the core value of Auto Claude - connecting GitHub issues to autonomous development.
## File Organization
### Naming Convention
- Lowercase with hyphens (matches existing documentation style)
- Descriptive names indicating audience and content
- Keep under 80 characters for GitHub rendering
### README.md (Index Page)
```markdown
# GitHub Issues Documentation
Choose the guide that matches your needs:
📖 **[User Guide](github-issues-user-guide.md)** - Get started with GitHub Issues integration
For: All users | Focus: Using the features
⚙️ **[Advanced AI Configuration](github-issues-advanced-ai-configuration.md)** - Optimize AI investigations
For: Technical users | Focus: Performance, costs, Opus 4.6
🔧 **[Customization Guide](github-issues-customization-guide.md)** - Extend and customize the system
For: Developers | Focus: Prompts, context, customization
```
## Markdown Format Guidelines
- Standard GitHub-flavored markdown
- H1 for title, H2 for main sections, H3 for subsections
- Code blocks with language specification (`xml`, `python`, `bash`)
- Callout boxes using `> **Note:**` format
- Internal links use relative paths
- Front matter with title and description metadata
## Implementation Notes
### Content Sources
- `apps/frontend/src/renderer/components/github-issues/` - UI components
- `apps/frontend/src/main/ipc-handlers/github/` - IPC handlers
- `apps/backend/runners/github/` - Backend services
- `guides/opus-4.6-features.md` - Reference for Opus 4.6 details
- Existing code comments and docstrings
### Screenshots to Capture
- Need to run the application in development mode (`npm run dev`)
- Capture each UI state listed in Visual Elements section
- Save to `guides/github-issues/images/` with descriptive names
### Diagram Creation
- Use Mermaid syntax for flowcharts (if supported by docs build)
- Alternatively, describe for manual creation with diagram tools
## Success Criteria
1. ✅ All three documents created in `guides/github-issues/`
2. ✅ README.md index page created
3. ✅ Progressive structure allows users to enter at appropriate level
4. ✅ Integration workflow is clear and emphasized
5. ✅ Screenshots included for key UI states
6. ✅ Cross-references enable navigation between documents
7. ✅ Writing style matches target audience for each document
8. ✅ Content is accurate based on actual codebase features
## Next Steps
1. Create implementation plan using writing-plans skill
2. Set up directory structure
3. Draft content for each document
4. Capture screenshots
5. Create diagrams
6. Review and refine
7. Commit to repository
File diff suppressed because it is too large Load Diff
-19
View File
@@ -52,14 +52,6 @@ Auto Claude is a desktop application (+ CLI) where users describe a goal and AI
**Electron path resolution** — For bug fixes in the Electron app, always check path resolution differences between dev and production builds (`app.isPackaged`, `process.resourcesPath`). Paths that work in dev often break when Electron is bundled for production — verify both contexts.
### Resetting PR Review State
To fully clear all PR review data so reviews run fresh, delete/reset these three things in `.auto-claude/github/`:
1. `rm .auto-claude/github/pr/logs_*.json` — review log files
2. `rm .auto-claude/github/pr/review_*.json` — review result files
3. Reset `pr/index.json` to `{"reviews": [], "last_updated": null}`
4. Reset `bot_detection_state.json` to `{"reviewed_commits": {}}` — this is the gatekeeper; without clearing it, the bot detector skips already-seen commits
## Project Structure
```
@@ -161,17 +153,6 @@ Each spec in `.auto-claude/specs/XXX-name/` contains: `spec.md`, `requirements.j
Graph-based semantic memory in `integrations/graphiti/`. Configured through the Electron app's onboarding/settings UI (CLI users can alternatively set `GRAPHITI_ENABLED=true` in `.env`). See [ARCHITECTURE.md](shared_docs/ARCHITECTURE.md#memory-system) for details.
### Opus 4.6 Features
Auto Claude leverages Opus 4.6's advanced capabilities for GitHub issue investigations:
- **Fast Mode:** 2.5x faster investigations (toggle in Settings > GitHub > AI Investigation)
- **128K Output Tokens:** Root cause specialist gets max tokens for deep analysis
- **Per-Specialist Limits:** Different token limits per investigation specialist
- **Adaptive Thinking:** High-effort mode for thorough investigations
See [guides/opus-4.6-features.md](guides/opus-4.6-features.md) for detailed documentation on Opus 4.6 features, pricing, and usage.
## Frontend Development
### Tech Stack
+7 -8
View File
@@ -8,7 +8,6 @@
[![Discord](https://img.shields.io/badge/Discord-Join%20Community-5865F2?style=flat-square&logo=discord&logoColor=white)](https://discord.gg/KCXaPBr4Dj)
[![YouTube](https://img.shields.io/badge/YouTube-Subscribe-FF0000?style=flat-square&logo=youtube&logoColor=white)](https://www.youtube.com/@AndreMikalsen)
[![CI](https://img.shields.io/github/actions/workflow/status/AndyMik90/Auto-Claude/ci.yml?branch=main&style=flat-square&label=CI)](https://github.com/AndyMik90/Auto-Claude/actions)
[![Mentioned in Awesome Claude Code](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/hesreallyhim/awesome-claude-code)
---
@@ -36,18 +35,18 @@
> ⚠️ Beta releases may contain bugs and breaking changes. [View all releases](https://github.com/AndyMik90/Auto-Claude/releases)
<!-- BETA_VERSION_BADGE -->
[![Beta](https://img.shields.io/badge/beta-2.7.6--beta.5-orange?style=flat-square)](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.5)
[![Beta](https://img.shields.io/badge/beta-2.7.6--beta.3-orange?style=flat-square)](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.3)
<!-- 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.3-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-win32-x64.exe) |
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-beta.3-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-darwin-arm64.dmg) |
| **macOS (Intel)** | [Auto-Claude-2.7.6-beta.3-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-darwin-x64.dmg) |
| **Linux** | [Auto-Claude-2.7.6-beta.3-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-linux-x86_64.AppImage) |
| **Linux (Debian)** | [Auto-Claude-2.7.6-beta.3-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-linux-amd64.deb) |
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-beta.3-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.3/Auto-Claude-2.7.6-beta.3-linux-x86_64.flatpak) |
<!-- BETA_DOWNLOADS_END -->
> All releases include SHA256 checksums and VirusTotal scan results for security verification.
+1 -8
View File
@@ -62,16 +62,9 @@ Thumbs.db
# Tests (development only)
tests/
# Exceptions: Allow specific test directories
# Exception: Allow colocated tests within integrations/graphiti
!integrations/graphiti/tests/
!tests/integration/
# Auto Claude data directory
.auto-claude/
coverage.json
# Auto Claude generated files
.auto-claude-security.json
.auto-claude-status
.security-key
logs/security/
+1 -1
View File
@@ -19,5 +19,5 @@ Quick Start:
See README.md for full documentation.
"""
__version__ = "2.7.6-beta.5"
__version__ = "2.7.6-beta.3"
__author__ = "Auto Claude Team"
-79
View File
@@ -80,7 +80,6 @@ from .base import (
RESUME_FILE,
sanitize_error_message,
)
from .investigation_context import load_investigation_context
from .memory_manager import debug_memory_system_status, get_graphiti_context
from .session import post_session_processing, run_agent_session
from .utils import (
@@ -783,84 +782,6 @@ async def run_autonomous_agent(
prompt += "\n\n" + graphiti_context
print_status("Graphiti memory context loaded", "success")
# Load investigation context if this is a GitHub-sourced task
investigation_context = load_investigation_context(spec_dir)
if investigation_context:
# Format investigation context for the prompt
inv = investigation_context
inv_prompt = "\n## GitHub Investigation Context\n\n"
inv_prompt += (
"This task was created from a GitHub issue investigation. "
)
inv_prompt += "Use this context to guide your work.\n\n"
if inv.get("root_cause", {}).get("summary"):
inv_prompt += f"**Root Cause:** {inv['root_cause']['summary']}\n\n"
if inv.get("root_cause", {}).get("evidence"):
inv_prompt += f"**Evidence:**\n{inv['root_cause']['evidence']}\n\n"
if inv.get("root_cause", {}).get("code_paths"):
inv_prompt += "**Code Paths:**\n"
for path in inv["root_cause"]["code_paths"]:
file_ref = path.get("file", "unknown")
start = path.get("start_line", "")
end = path.get("end_line", "")
desc = path.get("description", "")
line_range = f":{start}-{end}" if start and end else ""
inv_prompt += f"- `{file_ref}{line_range}`"
if desc:
inv_prompt += f"{desc}"
inv_prompt += "\n"
inv_prompt += "\n"
if inv.get("fix_approaches"):
inv_prompt += "**Fix Approaches:**\n"
for i, approach in enumerate(inv["fix_approaches"], 1):
desc = approach.get("description", "Approach")
complexity = approach.get("complexity", "")
inv_prompt += f"{i}. {desc}"
if complexity:
inv_prompt += f" (complexity: {complexity})"
inv_prompt += "\n"
files_affected = approach.get("files_affected", [])
if files_affected:
inv_prompt += f" - Files: {', '.join(files_affected)}\n"
inv_prompt += "\n"
if inv.get("gotchas"):
inv_prompt += "**Gotchas:**\n"
for gotcha in inv["gotchas"]:
inv_prompt += f"- {gotcha}\n"
inv_prompt += "\n"
if inv.get("patterns_to_follow"):
inv_prompt += "**Patterns to Follow:**\n"
for pattern in inv["patterns_to_follow"]:
file_ref = pattern.get("file", "unknown")
desc = pattern.get("description", "")
inv_prompt += f"- `{file_ref}`"
if desc:
inv_prompt += f"{desc}"
inv_prompt += "\n"
inv_prompt += "\n"
if inv.get("reproducer"):
reproducer = inv["reproducer"]
inv_prompt += "**Verification Steps:**\n"
steps = reproducer.get("reproduction_steps", [])
for step in steps:
inv_prompt += f"- {step}\n"
test_approach = reproducer.get("suggested_test_approach")
if test_approach:
inv_prompt += (
f"\n**Suggested Test Approach:** {test_approach}\n"
)
inv_prompt += "\n"
prompt += inv_prompt
print_status("Investigation context loaded", "success")
# Add concurrency error context if recovering from 400 error
if concurrency_error_context:
prompt += "\n\n" + concurrency_error_context
@@ -1,97 +0,0 @@
"""
Investigation context loading for agents.
Provides utilities to load investigation data from spec directories
for GitHub-sourced tasks.
"""
import json
from pathlib import Path
from typing import Any
def load_investigation_context(spec_dir: Path) -> dict[str, Any] | None:
"""
Load investigation context if this spec was created from a GitHub issue.
Args:
spec_dir: Path to the spec directory
Returns:
Structured investigation context with root_cause, fix_approaches,
reproducer, gotchas, and patterns_to_follow, or None if no
investigation data exists.
"""
investigation_report_path = spec_dir / "investigation_report.json"
if not investigation_report_path.exists():
return None
try:
with open(investigation_report_path) as f:
report = json.load(f)
root_cause = report.get("root_cause", {})
fix_advice = report.get("fix_advice", {})
reproduction = report.get("reproduction", {})
# Structure the context for agents
return {
"root_cause": {
"summary": root_cause.get("identified_root_cause"),
"evidence": root_cause.get("evidence", ""),
"code_paths": root_cause.get("code_paths", []),
},
"fix_approaches": fix_advice.get("approaches", []),
"reproducer": reproduction if reproduction else None,
"gotchas": fix_advice.get("gotchas", []),
"patterns_to_follow": fix_advice.get("patterns_to_follow", []),
"impact": report.get("impact", {}),
}
except (json.JSONDecodeError, OSError):
return None
def load_investigation_for_qa(
spec_dir: Path, base_branch: str
) -> dict[str, Any] | None:
"""
Load investigation context for QA validation.
Similar to load_investigation_context but includes base_branch
for QA comparison.
Args:
spec_dir: Path to the spec directory
base_branch: Base branch to compare against (e.g., 'main', 'develop')
Returns:
Structured investigation context with root_cause, reproducer,
impact, expected_outcome, and base_branch, or None if no
investigation data exists.
"""
investigation_report_path = spec_dir / "investigation_report.json"
if not investigation_report_path.exists():
return None
try:
with open(investigation_report_path) as f:
report = json.load(f)
root_cause = report.get("root_cause", {})
reproduction = report.get("reproduction", {})
return {
"root_cause": {
"summary": root_cause.get("identified_root_cause"),
"evidence": root_cause.get("evidence", ""),
"code_paths": root_cause.get("code_paths", []),
},
"reproducer": reproduction if reproduction else None,
"impact": report.get("impact", {}),
"expected_outcome": report.get("ai_summary"),
"base_branch": base_branch,
}
except (json.JSONDecodeError, OSError):
return None
-15
View File
@@ -292,14 +292,6 @@ AGENT_CONFIGS = {
"auto_claude_tools": [],
"thinking_default": "high",
},
"pr_followup_extraction": {
# Lightweight extraction call for recovering data when structured output fails
# Pure structured output extraction, no tools needed
"tools": [],
"mcp_servers": [],
"auto_claude_tools": [],
"thinking_default": "low",
},
"pr_finding_validator": {
# Standalone validator for re-checking findings against actual code
# Called separately from orchestrator to validate findings with fresh context
@@ -308,13 +300,6 @@ AGENT_CONFIGS = {
"auto_claude_tools": [],
"thinking_default": "medium",
},
"investigation_specialist": {
# Read-only specialist for issue investigation (root cause, impact, fix, reproduction)
"tools": BASE_READ_TOOLS,
"mcp_servers": [],
"auto_claude_tools": [],
"thinking_default": "medium",
},
# ═══════════════════════════════════════════════════════════════════════
# ANALYSIS PHASES
# ═══════════════════════════════════════════════════════════════════════
@@ -31,7 +31,6 @@ class ProjectAnalyzer:
"""Run full project analysis."""
self._detect_project_type()
self._find_and_analyze_services()
self._aggregate_dependency_locations()
self._analyze_infrastructure()
self._detect_conventions()
self._map_dependencies()
@@ -125,63 +124,6 @@ class ProjectAnalyzer:
self.index["services"] = services
def _aggregate_dependency_locations(self) -> None:
"""Aggregate dependency location metadata from all services.
Collects dependency_locations from each service and stores them as
paths relative to the project root (e.g., 'apps/backend/.venv'
instead of just '.venv').
"""
aggregated: list[dict[str, Any]] = []
for service_name, service_info in self.index.get("services", {}).items():
service_deps = service_info.get("dependency_locations", [])
service_path = service_info.get("path", "")
# Compute service-relative prefix once per service
service_rel: Path | None = None
if service_path:
try:
service_rel = Path(service_path).relative_to(self.project_dir)
except ValueError:
# Service path is outside the project root — skip its deps
# to avoid producing absolute paths that bypass containment
continue
for dep in service_deps:
dep_path = dep.get("path")
if not dep_path:
continue
# Build project-relative path from service path + dep path
if service_rel is not None:
project_relative = str(service_rel / dep_path)
else:
project_relative = dep_path
entry: dict[str, Any] = {
"type": dep.get("type", "unknown"),
"path": project_relative,
"exists": dep.get("exists", False),
"service": service_name,
}
if dep.get("requirements_file"):
# Convert to project-relative path like we do for "path"
if service_rel is not None:
entry["requirements_file"] = str(
service_rel / dep["requirements_file"]
)
else:
entry["requirements_file"] = dep["requirements_file"]
pkg_mgr = dep.get("package_manager") or service_info.get(
"package_manager"
)
if pkg_mgr:
entry["package_manager"] = pkg_mgr
aggregated.append(entry)
self.index["dependency_locations"] = aggregated
def _analyze_infrastructure(self) -> None:
"""Analyze infrastructure configuration."""
infra = {}
@@ -40,8 +40,6 @@ class ServiceAnalyzer(BaseAnalyzer):
self._find_key_directories()
self._find_entry_points()
self._detect_dependencies()
self._detect_dependency_locations()
self._detect_package_manager()
self._detect_testing()
self._find_dockerfile()
@@ -211,121 +209,6 @@ class ServiceAnalyzer(BaseAnalyzer):
deps.append(match.group(1))
self.analysis["dependencies"] = deps[:20]
def _detect_dependency_locations(self) -> None:
"""Detect where dependencies live on disk for this service."""
locations: list[dict[str, Any]] = []
# Node.js: node_modules (only if package.json exists)
if self._exists("package.json"):
node_modules = self.path / "node_modules"
locations.append(
{
"type": "node_modules",
"path": "node_modules",
"exists": node_modules.exists() and node_modules.is_dir(),
}
)
# Python: .venv or venv
for venv_dir in [".venv", "venv"]:
venv_path = self.path / venv_dir
if venv_path.exists() and venv_path.is_dir():
entry: dict[str, Any] = {
"type": "venv",
"path": venv_dir,
"exists": True,
}
# Find requirements file
for req_file in ["requirements.txt", "pyproject.toml", "Pipfile"]:
if self._exists(req_file):
entry["requirements_file"] = req_file
break
locations.append(entry)
break
else:
# No venv found, still record requirements file if present
for req_file in ["requirements.txt", "pyproject.toml", "Pipfile"]:
if self._exists(req_file):
locations.append(
{
"type": "venv",
"path": ".venv",
"exists": False,
"requirements_file": req_file,
}
)
break
# PHP: vendor
vendor_path = self.path / "vendor"
if vendor_path.exists() and vendor_path.is_dir():
locations.append(
{
"type": "vendor_php",
"path": "vendor",
"exists": True,
}
)
# Rust: target
target_path = self.path / "target"
if target_path.exists() and target_path.is_dir():
locations.append(
{
"type": "cargo_target",
"path": "target",
"exists": True,
}
)
# Ruby: vendor/bundle
bundle_path = self.path / "vendor" / "bundle"
if bundle_path.exists() and bundle_path.is_dir():
locations.append(
{
"type": "vendor_bundle",
"path": "vendor/bundle",
"exists": True,
}
)
self.analysis["dependency_locations"] = locations
def _detect_package_manager(self) -> None:
"""Detect the package manager used by this service."""
# Node.js package managers
if self._exists("package-lock.json"):
self.analysis["package_manager"] = "npm"
elif self._exists("yarn.lock"):
self.analysis["package_manager"] = "yarn"
elif self._exists("pnpm-lock.yaml"):
self.analysis["package_manager"] = "pnpm"
elif self._exists("bun.lockb") or self._exists("bun.lock"):
self.analysis["package_manager"] = "bun"
# Python package managers
elif self._exists("Pipfile"):
self.analysis["package_manager"] = "pipenv"
elif self._exists("pyproject.toml"):
if self._exists("uv.lock"):
self.analysis["package_manager"] = "uv"
elif self._exists("poetry.lock"):
self.analysis["package_manager"] = "poetry"
else:
self.analysis["package_manager"] = "pip"
elif self._exists("requirements.txt"):
self.analysis["package_manager"] = "pip"
# Other
elif self._exists("Cargo.toml"):
self.analysis["package_manager"] = "cargo"
elif self._exists("go.mod"):
self.analysis["package_manager"] = "go_mod"
elif self._exists("Gemfile"):
self.analysis["package_manager"] = "gem"
elif self._exists("composer.json"):
self.analysis["package_manager"] = "composer"
else:
self.analysis["package_manager"] = None
def _detect_testing(self) -> None:
"""Detect testing framework and configuration."""
if self._exists("package.json"):
+8 -21
View File
@@ -10,7 +10,6 @@ import shutil
import subprocess
from pathlib import Path
from qa.criteria import is_fixes_applied, is_qa_approved, is_qa_rejected
from ui import highlight, print_status
@@ -152,22 +151,13 @@ def handle_batch_status_command(project_dir: str) -> bool:
except json.JSONDecodeError:
pass
# Determine status (highest priority first)
# Use authoritative QA status check, not just file existence
if is_qa_approved(spec_dir):
status = "qa_approved"
elif is_qa_rejected(spec_dir):
status = "qa_rejected"
elif is_fixes_applied(spec_dir):
status = "fixes_applied"
elif (spec_dir / "implementation_plan.json").exists():
# Check if there's a qa_report.md but no approval yet (QA in progress)
if (spec_dir / "qa_report.md").exists():
status = "qa_in_progress"
else:
status = "building"
elif (spec_dir / "spec.md").exists():
# Determine status
if (spec_dir / "spec.md").exists():
status = "spec_created"
elif (spec_dir / "implementation_plan.json").exists():
status = "building"
elif (spec_dir / "qa_report.md").exists():
status = "qa_approved"
else:
status = "pending_spec"
@@ -175,10 +165,7 @@ def handle_batch_status_command(project_dir: str) -> bool:
"pending_spec": "",
"spec_created": "📋",
"building": "⚙️",
"qa_in_progress": "🔍",
"qa_approved": "",
"qa_rejected": "",
"fixes_applied": "🔧",
"unknown": "",
}.get(status, "")
@@ -205,10 +192,10 @@ def handle_batch_cleanup_command(project_dir: str, dry_run: bool = True) -> bool
print_status("No specs directory found", "info")
return True
# Find completed specs (only QA-approved, matching status display logic)
# Find completed specs
completed = []
for spec_dir in specs_dir.iterdir():
if spec_dir.is_dir() and is_qa_approved(spec_dir):
if spec_dir.is_dir() and (spec_dir / "qa_report.md").exists():
completed.append(spec_dir.name)
if not completed:
+1 -254
View File
@@ -61,8 +61,6 @@ def handle_build_command(
skip_qa: bool,
force_bypass_approval: bool,
base_branch: str | None = None,
issue_workflow: bool = False,
issue_number: int | None = None,
) -> None:
"""
Handle the main build command.
@@ -79,8 +77,6 @@ def handle_build_command(
skip_qa: Skip automatic QA validation
force_bypass_approval: Force bypass approval check
base_branch: Base branch for worktree creation (default: current branch)
issue_workflow: If True, run from a GitHub issue investigation
issue_number: GitHub issue number (required when issue_workflow=True)
"""
# Lazy imports to avoid loading heavy modules
from agent import run_autonomous_agent, sync_spec_to_source
@@ -99,30 +95,6 @@ def handle_build_command(
from .utils import print_banner, validate_environment
# Handle issue workflow: load investigation report and inject context
if issue_workflow:
if not issue_number:
print("\nError: --issue-number is required with --issue-workflow")
sys.exit(1)
pipeline_mode = _get_investigation_pipeline_mode(project_dir)
_inject_issue_workflow_context(project_dir, spec_dir, issue_number)
# pipelineMode controls which phases to skip:
# - "full": run complete spec + planning + coding + QA pipeline (default)
# - "skip_to_planning": skip spec creation, go to planning (investigation = spec)
# - "minimal": skip spec + planning, go straight to coding
if pipeline_mode == "skip_to_planning":
# Investigation report serves as the spec; bypass approval since
# the investigation was already reviewed.
force_bypass_approval = True
elif pipeline_mode == "minimal":
# Skip everything: create a minimal plan so the planner is bypassed
# and the coder starts immediately from the investigation context.
force_bypass_approval = True
skip_qa = True
_create_minimal_plan_for_issue(spec_dir, issue_number)
# Get the resolved model for the planning phase (first phase of build)
# This respects task_metadata.json phase configuration from the UI
planning_model = get_phase_model(spec_dir, "planning", model)
@@ -477,7 +449,7 @@ def _handle_build_interrupt(
if choice == "skip":
print()
print_status("Resuming build...", "info")
status_manager.update(state=BuildState.BUILDING)
status_manager.update(state=BuildState.RUNNING)
asyncio.run(
run_autonomous_agent(
project_dir=working_dir,
@@ -513,228 +485,3 @@ def _handle_build_interrupt(
content.append(muted("Your build is in a separate workspace and is safe."))
print(box(content, width=70, style="light"))
print()
def _create_minimal_plan_for_issue(spec_dir: Path, issue_number: int) -> None:
"""Create a minimal implementation plan so the planner phase is skipped.
Used in "minimal" pipeline mode where the investigation context is
sufficient and we want the coder to start immediately.
Args:
spec_dir: Spec directory path
issue_number: GitHub issue number for context
"""
from datetime import datetime
plan_file = spec_dir / "implementation_plan.json"
if plan_file.exists():
# Don't overwrite an existing plan (e.g. if resuming)
return
plan = {
"phases": [
{
"phase": 1,
"name": "Implementation",
"description": f"Implement fix for issue #{issue_number} based on investigation findings",
"depends_on": [],
"subtasks": [
{
"id": "subtask-1-1",
"description": (
f"Implement the fix for issue #{issue_number}. "
"Follow the investigation context in HUMAN_INPUT.md "
"for root cause analysis, recommended fix approach, "
"and files to modify."
),
"service": "main",
"status": "pending",
"files_to_create": [],
"files_to_modify": [],
"patterns_from": [],
"verification": {
"type": "manual",
"run": "Verify the fix resolves the issue",
},
}
],
}
],
"metadata": {
"created_at": datetime.now().isoformat(),
"complexity": "simple",
"estimated_sessions": 1,
"pipeline_mode": "minimal",
"source_issue": issue_number,
},
}
from core.file_utils import write_json_atomic
write_json_atomic(plan_file, plan)
print(" Pipeline mode: minimal (skipping planner, created single-subtask plan)")
def _get_investigation_pipeline_mode(project_dir: Path) -> str:
"""Read the pipelineMode from investigation settings.
Reads from .auto-claude/github/config.json -> investigation_settings.pipelineMode.
Defaults to "full" if not configured.
Args:
project_dir: Project root directory
Returns:
Pipeline mode string: "full", "skip_to_planning", or "minimal"
"""
import json
config_path = project_dir / ".auto-claude" / "github" / "config.json"
if not config_path.exists():
return "full"
try:
data = json.loads(config_path.read_text(encoding="utf-8"))
settings = data.get("investigation_settings", {})
mode = settings.get("pipelineMode", "full")
if mode in ("full", "skip_to_planning", "minimal"):
return mode
return "full"
except (json.JSONDecodeError, OSError):
return "full"
def _inject_issue_workflow_context(
project_dir: Path,
spec_dir: Path,
issue_number: int,
) -> None:
"""Inject investigation context into the build workflow.
Loads the investigation report for the given issue and writes a
HUMAN_INPUT.md file in the spec directory with root cause analysis,
fix advice, and other investigation context. This is read by the
coder agent as additional guidance.
Also updates the investigation state to "building".
Args:
project_dir: Project root directory
spec_dir: Spec directory path
issue_number: GitHub issue number
"""
# Use try/except for imports matching the codebase pattern
try:
from runners.github.services.investigation_persistence import (
load_investigation_report,
save_investigation_state,
)
except (ImportError, ValueError, SystemError):
# Add parent to path if needed
_backend = Path(__file__).parent.parent
if str(_backend) not in sys.path:
sys.path.insert(0, str(_backend))
from runners.github.services.investigation_persistence import (
load_investigation_report,
save_investigation_state,
)
report = load_investigation_report(project_dir, issue_number)
if report is None:
print(f"\nWarning: No investigation report found for issue #{issue_number}")
print("Proceeding without investigation context.")
return
# Build context string for the coder agent
context_parts: list[str] = []
context_parts.append(f"# Investigation Context for Issue #{issue_number}")
context_parts.append("")
context_parts.append(f"## {report.issue_title}")
context_parts.append("")
context_parts.append(f"**Severity:** {report.severity}")
context_parts.append("")
# AI summary
context_parts.append("## Summary")
context_parts.append(report.ai_summary)
context_parts.append("")
# Root cause
context_parts.append("## Root Cause")
context_parts.append(report.root_cause.identified_root_cause)
context_parts.append("")
if report.root_cause.code_paths:
context_parts.append("### Code Paths")
for cp in report.root_cause.code_paths:
end = cp.end_line if cp.end_line else cp.start_line
context_parts.append(
f"- `{cp.file}:{cp.start_line}-{end}`: {cp.description}"
)
context_parts.append("")
# Fix advice
if report.fix_advice.approaches:
rec_idx = report.fix_advice.recommended_approach
context_parts.append("## Recommended Fix")
if 0 <= rec_idx < len(report.fix_advice.approaches):
approach = report.fix_advice.approaches[rec_idx]
context_parts.append(approach.description)
context_parts.append("")
if approach.files_affected:
context_parts.append("**Files to modify:**")
for f in approach.files_affected:
context_parts.append(f"- `{f}`")
context_parts.append("")
# Gotchas
if report.fix_advice.gotchas:
context_parts.append("## Gotchas")
for gotcha in report.fix_advice.gotchas:
context_parts.append(f"- {gotcha}")
context_parts.append("")
# Patterns
if report.fix_advice.patterns_to_follow:
context_parts.append("## Patterns to Follow")
for pat in report.fix_advice.patterns_to_follow:
context_parts.append(f"- `{pat.file}`: {pat.description}")
context_parts.append("")
context = "\n".join(context_parts)
# Write as HUMAN_INPUT.md (existing mechanism for agent guidance injection)
input_file = spec_dir / "HUMAN_INPUT.md"
existing = ""
if input_file.exists():
existing = input_file.read_text(encoding="utf-8")
if existing:
# Append investigation context to existing human input
combined = existing + "\n\n" + context
else:
combined = context
input_file.write_text(combined, encoding="utf-8")
# Update investigation state to "building" (note: we use the broader
# "task_created" status since "building" isn't a valid InvestigationState status)
from datetime import datetime, timezone
save_investigation_state(
project_dir,
issue_number,
{
"issue_number": issue_number,
"status": "task_created",
"started_at": datetime.now(timezone.utc).isoformat(),
"linked_spec_id": spec_dir.name,
},
)
print(f"\nIssue workflow: Injected investigation context for #{issue_number}")
print(f" Root cause: {report.root_cause.identified_root_cause[:80]}...")
print(f" Severity: {report.severity}")
print()
-15
View File
@@ -280,19 +280,6 @@ Environment Variables:
help="Actually delete files in cleanup (not just preview)",
)
# Issue workflow
parser.add_argument(
"--issue-workflow",
action="store_true",
help="Run build from a GitHub issue investigation (requires --issue-number)",
)
parser.add_argument(
"--issue-number",
type=int,
default=None,
help="GitHub issue number for --issue-workflow",
)
return parser.parse_args()
@@ -490,8 +477,6 @@ def _run_cli() -> None:
skip_qa=args.skip_qa,
force_bypass_approval=args.force,
base_branch=args.base_branch,
issue_workflow=args.issue_workflow,
issue_number=args.issue_number,
)
+7 -54
View File
@@ -694,25 +694,10 @@ def get_auth_token(config_dir: str | None = None) -> str | None:
Returns:
Token string if found, None otherwise
"""
_debug = os.environ.get("DEBUG", "").lower() in ("true", "1")
if _debug:
# Log which auth env vars are set (presence only, never values)
set_vars = [v for v in AUTH_TOKEN_ENV_VARS if os.environ.get(v)]
logger.info(
"[Auth] get_auth_token() called — config_dir param=%s, "
"env vars present: %s, CLAUDE_CONFIG_DIR env=%s",
repr(config_dir),
set_vars or "(none)",
"set" if os.environ.get("CLAUDE_CONFIG_DIR") else "unset",
)
# First check environment variables (highest priority)
for var in AUTH_TOKEN_ENV_VARS:
token = os.environ.get(var)
if token:
if _debug:
logger.info("[Auth] Token resolved from env var: %s", var)
return _try_decrypt_token(token)
# Check CLAUDE_CONFIG_DIR environment variable (profile's custom config directory)
@@ -720,13 +705,12 @@ def get_auth_token(config_dir: str | None = None) -> str | None:
effective_config_dir = config_dir or env_config_dir
# Debug: Log which config_dir is being used for credential resolution
if _debug and effective_config_dir:
debug = os.environ.get("DEBUG", "").lower() in ("true", "1")
if debug and effective_config_dir:
service_name = _get_keychain_service_name(effective_config_dir)
logger.info(
"[Auth] Resolving credentials for profile config_dir: %s "
"(Keychain service: %s)",
effective_config_dir,
service_name,
f"[Auth] Resolving credentials for profile config_dir: {effective_config_dir} "
f"(Keychain service: {service_name})"
)
# If a custom config directory is specified, read from there first
@@ -734,37 +718,24 @@ def get_auth_token(config_dir: str | None = None) -> str | None:
# Try reading from .credentials.json file in the config directory
token = _get_token_from_config_dir(effective_config_dir)
if token:
if _debug:
logger.info(
"[Auth] Token resolved from config dir file: %s",
effective_config_dir,
)
return _try_decrypt_token(token)
# Also try the system credential store with hash-based service name
# This is needed because macOS stores credentials in Keychain, not files
token = get_token_from_keychain(effective_config_dir)
if token:
if _debug:
logger.info("[Auth] Token resolved from Keychain (profile-specific)")
return _try_decrypt_token(token)
# If config_dir was explicitly provided, DON'T fall back to default keychain
# - that would return the wrong profile's token
logger.debug(
"No credentials found for config_dir '%s' in file or keychain",
effective_config_dir,
f"No credentials found for config_dir '{effective_config_dir}' "
"in file or keychain"
)
return None
# No config_dir specified - use default system credential store
keychain_token = get_token_from_keychain()
if _debug:
logger.info(
"[Auth] Token resolved from default Keychain: %s",
"found" if keychain_token else "not found",
)
return _try_decrypt_token(keychain_token)
return _try_decrypt_token(get_token_from_keychain())
def get_auth_token_source(config_dir: str | None = None) -> str | None:
@@ -999,18 +970,8 @@ def configure_sdk_authentication(config_dir: str | None = None) -> None:
- API profile mode: requires ANTHROPIC_AUTH_TOKEN
- OAuth mode: requires CLAUDE_CODE_OAUTH_TOKEN (from Keychain or env)
"""
_debug = os.environ.get("DEBUG", "").lower() in ("true", "1")
api_profile_mode = bool(os.environ.get("ANTHROPIC_BASE_URL", "").strip())
if _debug:
logger.info(
"[Auth] configure_sdk_authentication() — mode=%s, config_dir=%s, "
"CLAUDE_CONFIG_DIR env=%s",
"api_profile" if api_profile_mode else "oauth",
repr(config_dir),
"set" if os.environ.get("CLAUDE_CONFIG_DIR") else "unset",
)
if api_profile_mode:
# API profile mode: ensure ANTHROPIC_AUTH_TOKEN is present
if not os.environ.get("ANTHROPIC_AUTH_TOKEN"):
@@ -1038,14 +999,6 @@ def configure_sdk_authentication(config_dir: str | None = None) -> None:
os.environ["CLAUDE_CODE_OAUTH_TOKEN"] = oauth_token
logger.info("Using OAuth authentication")
if _debug:
logger.info(
"[Auth] SDK env check — CLAUDE_CONFIG_DIR=%s, "
"CLAUDE_CODE_OAUTH_TOKEN=%s",
"set" if os.environ.get("CLAUDE_CONFIG_DIR") else "unset",
"set" if os.environ.get("CLAUDE_CODE_OAUTH_TOKEN") else "unset",
)
def ensure_claude_code_oauth_token() -> None:
"""
+1
View File
@@ -867,6 +867,7 @@ def create_client(
logger.info(f"Using CLAUDE_CLI_PATH override: {env_cli_path}")
# Add structured output format if specified
# See: https://platform.claude.com/docs/en/agent-sdk/structured-outputs
if output_format:
options_kwargs["output_format"] = output_format
+4 -4
View File
@@ -235,10 +235,10 @@ def debug_section(module: str, title: str) -> None:
return
timestamp = datetime.now().strftime("%H:%M:%S.%f")[:-3]
separator = "-" * 60
log_line = f"\n{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.DEBUG}{Colors.BOLD}+{separator}+{Colors.RESET}"
log_line += f"\n{Colors.TIMESTAMP} {Colors.RESET} {Colors.DEBUG}{Colors.BOLD}| {module}: {title}{' ' * (58 - len(module) - len(title) - 2)}|{Colors.RESET}"
log_line += f"\n{Colors.TIMESTAMP} {Colors.RESET} {Colors.DEBUG}{Colors.BOLD}+{separator}+{Colors.RESET}"
separator = "" * 60
log_line = f"\n{Colors.TIMESTAMP}[{timestamp}]{Colors.RESET} {Colors.DEBUG}{Colors.BOLD}{separator}{Colors.RESET}"
log_line += f"\n{Colors.TIMESTAMP} {Colors.RESET} {Colors.DEBUG}{Colors.BOLD} {module}: {title}{' ' * (58 - len(module) - len(title) - 2)}{Colors.RESET}"
log_line += f"\n{Colors.TIMESTAMP} {Colors.RESET} {Colors.DEBUG}{Colors.BOLD}{separator}{Colors.RESET}"
_write_log(log_line)
+3 -31
View File
@@ -9,11 +9,8 @@ Enhanced with colored output, icons, and better visual formatting.
"""
import json
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
from core.plan_normalization import normalize_subtask_aliases
from ui import (
Icons,
@@ -233,8 +230,8 @@ def print_progress_summary(spec_dir: Path, show_next: bool = True) -> None:
f" {icon(Icons.ARROW_RIGHT)} Next: {highlight(next_id)} - {next_desc}"
)
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
logger.debug(f"Failed to load plan file for phase summary: {e}")
except (OSError, json.JSONDecodeError, UnicodeDecodeError):
pass # Ignore corrupted/unreadable progress files
else:
print()
print_status("No implementation subtasks yet - planner needs to run", "pending")
@@ -407,8 +404,6 @@ def get_next_subtask(spec_dir: Path) -> dict | None:
"""
Find the next subtask to work on, respecting phase dependencies.
Skips subtasks that are marked as stuck in the recovery manager's attempt history.
Args:
spec_dir: Directory containing implementation_plan.json
@@ -420,23 +415,6 @@ def get_next_subtask(spec_dir: Path) -> dict | None:
if not plan_file.exists():
return None
# Load stuck subtasks from recovery manager's attempt history
stuck_subtask_ids = set()
attempt_history_file = spec_dir / "memory" / "attempt_history.json"
if attempt_history_file.exists():
try:
with open(attempt_history_file, encoding="utf-8") as f:
attempt_history = json.load(f)
# Collect IDs of subtasks marked as stuck
stuck_subtask_ids = {
entry["subtask_id"]
for entry in attempt_history.get("stuck_subtasks", [])
if "subtask_id" in entry
}
except (OSError, json.JSONDecodeError, UnicodeDecodeError):
# If we can't read the file, continue without stuck checking
pass
try:
with open(plan_file, encoding="utf-8") as f:
plan = json.load(f)
@@ -477,15 +455,9 @@ def get_next_subtask(spec_dir: Path) -> dict | None:
if not deps_satisfied:
continue
# Find first pending subtask in this phase (skip stuck subtasks)
# Find first pending subtask in this phase
for subtask in phase.get("subtasks", phase.get("chunks", [])):
status = subtask.get("status", "pending")
subtask_id = subtask.get("id")
# Skip stuck subtasks
if subtask_id in stuck_subtask_ids:
continue
if status in {"pending", "not_started", "not started"}:
subtask_out, _changed = normalize_subtask_aliases(subtask)
subtask_out["status"] = "pending"
+15 -4
View File
@@ -186,12 +186,14 @@ def _before_send(event: dict, hint: dict) -> dict | None:
def init_sentry(
component: str = "backend",
force_enable: bool = False,
) -> bool:
"""
Initialize Sentry for the Python backend.
Args:
component: Component name for tagging (e.g., "backend", "github-runner")
force_enable: Force enable even without packaged app detection
Returns:
True if Sentry was initialized, False otherwise
@@ -210,11 +212,20 @@ def init_sentry(
logger.debug("[Sentry] No SENTRY_DSN configured - error reporting disabled")
return False
# DSN is present (checked above), so Sentry should be enabled.
# The Electron main process only passes SENTRY_DSN to subprocesses in
# production builds, so its presence is sufficient to gate activation.
# In dev, set SENTRY_DSN in your environment to opt-in.
# Check if we should enable Sentry
# Enable if:
# - Running from packaged app (detected by __compiled__ or frozen)
# - SENTRY_DEV=true is set
# - force_enable is True
is_packaged = getattr(sys, "frozen", False) or hasattr(sys, "__compiled__")
sentry_dev = os.environ.get("SENTRY_DEV", "").lower() in ("true", "1", "yes")
should_enable = is_packaged or sentry_dev or force_enable
if not should_enable:
logger.debug(
"[Sentry] Development mode - error reporting disabled (set SENTRY_DEV=true to enable)"
)
return False
try:
import sentry_sdk
@@ -1,176 +0,0 @@
"""
Dependency Strategy Mapping
============================
Maps dependency types to sharing strategies for worktree creation.
Each dependency ecosystem has different constraints:
- **node_modules**: Safe to symlink. Node's resolution algorithm follows symlinks
correctly, and the directory is self-contained.
- **venv / .venv**: Must be recreated. Python's ``pyvenv.cfg`` discovery walks the
real directory hierarchy without resolving symlinks (CPython bug #106045), so a
symlinked venv resolves paths relative to the *target*, not the worktree.
- **vendor (PHP)**: Safe to symlink. Composer's autoloader uses ``__DIR__``-relative
paths that resolve correctly through symlinks.
- **cargo target / go modules**: Skip entirely. Rust's ``target/`` dir contains
per-machine build artifacts that must be rebuilt. Go uses a global module cache
(``$GOPATH/pkg/mod``), so there is nothing in-tree to share.
"""
from __future__ import annotations
import logging
import os
from pathlib import Path, PurePosixPath, PureWindowsPath
logger = logging.getLogger(__name__)
from .models import DependencyShareConfig, DependencyStrategy
# ---------------------------------------------------------------------------
# Default strategy map
# ---------------------------------------------------------------------------
# Maps dependency type identifiers to the strategy that should be used when
# sharing that dependency across worktrees. Data-driven — add new entries
# here rather than writing if/else branches.
# ---------------------------------------------------------------------------
DEFAULT_STRATEGY_MAP: dict[str, DependencyStrategy] = {
# JavaScript / Node.js — symlink is safe and fast
"node_modules": DependencyStrategy.SYMLINK,
# Python — venvs MUST be recreated (pyvenv.cfg symlink bug)
"venv": DependencyStrategy.RECREATE,
".venv": DependencyStrategy.RECREATE,
# PHP — Composer vendor dir is safe to symlink
"vendor_php": DependencyStrategy.SYMLINK,
# Ruby — Bundler vendor/bundle is safe to symlink
"vendor_bundle": DependencyStrategy.SYMLINK,
# Rust — build output dir, skip (rebuilt per-worktree)
"cargo_target": DependencyStrategy.SKIP,
# Go — global module cache, nothing in-tree to share
"go_modules": DependencyStrategy.SKIP,
}
def get_dependency_configs(
project_index: dict | None,
project_dir: Path | None = None,
) -> list[DependencyShareConfig]:
"""Derive dependency share configs from a project index.
If *project_index* is ``None`` or lacks ``dependency_locations``,
falls back to a hardcoded node_modules config for backward compatibility
with existing worktree setups.
Args:
project_index: Parsed ``project_index.json`` dict, or ``None``.
project_dir: Project root directory for resolved-path containment
checks (defense-in-depth). Should always be provided when
*project_index* is not ``None`` — omitting it disables the
resolved-path security check.
Returns:
List of :class:`DependencyShareConfig` objects — one per discovered
dependency location.
"""
configs: list[DependencyShareConfig] = []
seen: set[str] = set()
if project_index is not None:
if project_dir is None:
logger.warning(
"get_dependency_configs called with project_index but no "
"project_dir — resolved-path containment check is disabled"
)
# Use the aggregated top-level dependency_locations which already
# contain project-relative paths (e.g. "apps/backend/.venv" instead
# of just ".venv"). This avoids a monorepo path resolution bug
# where service-relative paths were incorrectly treated as project-
# relative.
dep_locations = project_index.get("dependency_locations") or []
for dep in dep_locations:
if not isinstance(dep, dict):
continue
dep_type = dep.get("type", "")
rel_path = dep.get("path", "")
if not dep_type or not rel_path:
continue
# Path containment: reject absolute paths and traversals.
# Check both POSIX and Windows path styles for cross-platform safety.
p = PurePosixPath(rel_path)
if p.is_absolute() or PureWindowsPath(rel_path).is_absolute():
continue
if ".." in p.parts or ".." in PureWindowsPath(rel_path).parts:
continue
# Defense-in-depth: verify the resolved path stays within project_dir
if project_dir is not None:
resolved = (project_dir / rel_path).resolve()
if not str(resolved).startswith(str(project_dir.resolve()) + os.sep):
continue
# Deduplicate by relative path
if rel_path in seen:
continue
seen.add(rel_path)
strategy = DEFAULT_STRATEGY_MAP.get(dep_type, DependencyStrategy.SKIP)
# Validate requirements_file path containment too
req_file = dep.get("requirements_file")
if req_file:
rp = PurePosixPath(req_file)
if (
rp.is_absolute()
or PureWindowsPath(req_file).is_absolute()
or ".." in rp.parts
or ".." in PureWindowsPath(req_file).parts
):
req_file = None
# Defense-in-depth: resolved-path containment (matches rel_path check)
if req_file and project_dir is not None:
resolved_req = (project_dir / req_file).resolve()
if not str(resolved_req).startswith(
str(project_dir.resolve()) + os.sep
):
req_file = None
configs.append(
DependencyShareConfig(
dep_type=dep_type,
strategy=strategy,
source_rel_path=rel_path,
requirements_file=req_file,
package_manager=dep.get("package_manager"),
)
)
# Fallback: if no configs were discovered, default to node_modules-only
# so existing worktree behaviour is preserved.
if not configs:
configs.append(
DependencyShareConfig(
dep_type="node_modules",
strategy=DependencyStrategy.SYMLINK,
source_rel_path="node_modules",
)
)
configs.append(
DependencyShareConfig(
dep_type="node_modules",
strategy=DependencyStrategy.SYMLINK,
source_rel_path="apps/frontend/node_modules",
)
)
return configs
-28
View File
@@ -273,31 +273,3 @@ class SpecNumberLock:
pass
return max_num
class DependencyStrategy(Enum):
"""Strategy for sharing dependency directories across worktrees.
SYMLINK is fast but unsafe for certain ecosystems. Notably, Python venv
breaks when symlinked because CPython's pyvenv.cfg discovery walks the
real directory hierarchy without resolving symlinks first
(CPython bug #106045). This means a symlinked venv resolves its home
path relative to the symlink target's parent, not the worktree, causing
import failures and broken interpreters.
"""
SYMLINK = "symlink" # Create a symlink to the source (fast, works for node_modules)
RECREATE = "recreate" # Re-run the package manager to create a fresh copy
COPY = "copy" # Deep-copy the directory (slow but always correct)
SKIP = "skip" # Do nothing; let the agent handle it
@dataclass
class DependencyShareConfig:
"""Configuration for how a specific dependency type should be shared."""
dep_type: str # e.g. "node_modules", "venv", ".venv"
strategy: DependencyStrategy
source_rel_path: str # Relative path from project root, e.g. "node_modules"
requirements_file: str | None = None # e.g. "requirements.txt", "pyproject.toml"
package_manager: str | None = None # e.g. "npm", "uv", "pip"
+80 -392
View File
@@ -14,7 +14,6 @@ import sys
from pathlib import Path
from core.git_executable import run_git
from core.platform import is_windows
from merge import FileTimelineTracker
from security.constants import ALLOWLIST_FILENAME, PROFILE_FILENAME
from ui import (
@@ -29,9 +28,8 @@ from ui import (
)
from worktree import WorktreeManager
from .dependency_strategy import get_dependency_configs
from .git_utils import has_uncommitted_changes
from .models import DependencyShareConfig, DependencyStrategy, WorkspaceMode
from .models import WorkspaceMode
# Import debug utilities
try:
@@ -191,37 +189,11 @@ def symlink_node_modules_to_worktree(
"""
Symlink node_modules directories from project root to worktree.
.. deprecated::
Use :func:`setup_worktree_dependencies` instead, which handles all
dependency types (node_modules, venvs, vendor dirs, etc.) via
strategy-based dispatch.
This ensures the worktree has access to dependencies for TypeScript checks
and other tooling without requiring a separate npm install.
This is a thin backward-compatibility wrapper that delegates to
``setup_worktree_dependencies()`` with no project index (fallback mode).
Args:
project_dir: The main project directory
worktree_path: Path to the worktree
Returns:
List of symlinked paths (relative to worktree)
"""
results = setup_worktree_dependencies(
project_dir, worktree_path, project_index=None
)
# Flatten all processed paths for backward-compatible return value
return [path for paths in results.values() for path in paths]
def symlink_claude_config_to_worktree(
project_dir: Path, worktree_path: Path
) -> list[str]:
"""
Symlink .claude/ directory from project root to worktree.
This ensures the worktree has access to Claude Code configuration
(settings, CLAUDE.md, MCP servers, etc.) so that terminals opened
in the worktree behave identically to the project root.
Works with npm workspace hoisting where dependencies are hoisted to root
and workspace-specific dependencies remain in nested node_modules.
Args:
project_dir: The main project directory
@@ -232,52 +204,81 @@ def symlink_claude_config_to_worktree(
"""
symlinked = []
source_path = project_dir / ".claude"
target_path = worktree_path / ".claude"
# Node modules locations to symlink for TypeScript and tooling support.
# These are the standard locations for this monorepo structure.
#
# Design rationale:
# - Hardcoded paths are intentional for simplicity and reliability
# - Dynamic discovery (reading workspaces from package.json) would add complexity
# and potential failure points without significant benefit
# - This monorepo uses npm workspaces with hoisting, so dependencies are primarily
# in root node_modules with workspace-specific deps in apps/frontend/node_modules
#
# To add new workspace locations:
# 1. Add (source_rel, target_rel) tuple below
# 2. Update the parallel TypeScript implementation in
# apps/frontend/src/main/ipc-handlers/terminal/worktree-handlers.ts
# 3. Update the pre-commit hook check in .husky/pre-commit if needed
node_modules_locations = [
("node_modules", "node_modules"),
("apps/frontend/node_modules", "apps/frontend/node_modules"),
]
# Skip if source doesn't exist
if not source_path.exists():
debug(MODULE, "Skipping .claude/ - source does not exist")
return symlinked
for source_rel, target_rel in node_modules_locations:
source_path = project_dir / source_rel
target_path = worktree_path / target_rel
# Skip if target already exists
if target_path.exists():
debug(MODULE, "Skipping .claude/ - target already exists")
return symlinked
# Skip if source doesn't exist
if not source_path.exists():
debug(MODULE, f"Skipping {source_rel} - source does not exist")
continue
# Also skip if target is a symlink (even if broken)
if target_path.is_symlink():
debug(MODULE, "Skipping .claude/ - symlink already exists (possibly broken)")
return symlinked
# Skip if target already exists (don't overwrite existing node_modules)
if target_path.exists():
debug(MODULE, f"Skipping {target_rel} - target already exists")
continue
# Ensure parent directory exists
target_path.parent.mkdir(parents=True, exist_ok=True)
try:
if sys.platform == "win32":
# On Windows, use junctions instead of symlinks (no admin rights required)
result = subprocess.run(
["cmd", "/c", "mklink", "/J", str(target_path), str(source_path)],
capture_output=True,
text=True,
# Also skip if target is a symlink (even if broken - exists() returns False for broken symlinks)
if target_path.is_symlink():
debug(
MODULE,
f"Skipping {target_rel} - symlink already exists (possibly broken)",
)
continue
# Ensure parent directory exists
target_path.parent.mkdir(parents=True, exist_ok=True)
try:
if sys.platform == "win32":
# On Windows, use junctions instead of symlinks (no admin rights required)
# Junctions require absolute paths
result = subprocess.run(
["cmd", "/c", "mklink", "/J", str(target_path), str(source_path)],
capture_output=True,
text=True,
)
if result.returncode != 0:
raise OSError(result.stderr or "mklink /J failed")
else:
# On macOS/Linux, use relative symlinks for portability
relative_source = os.path.relpath(source_path, target_path.parent)
os.symlink(relative_source, target_path)
symlinked.append(target_rel)
debug(MODULE, f"Symlinked {target_rel} -> {source_path}")
except OSError as e:
# Symlink/junction creation can fail on some systems (e.g., FAT32 filesystem)
# Log warning but don't fail - worktree is still usable, just without
# TypeScript checking
debug_warning(
MODULE,
f"Could not symlink {target_rel}: {e}. TypeScript checks may fail.",
)
# Warn user - pre-commit hooks may fail without dependencies
print_status(
f"Warning: Could not link {target_rel} - TypeScript checks may fail",
"warning",
)
if result.returncode != 0:
raise OSError(result.stderr or "mklink /J failed")
else:
# On macOS/Linux, use relative symlinks for portability
relative_source = os.path.relpath(source_path, target_path.parent)
os.symlink(relative_source, target_path)
symlinked.append(".claude")
debug(MODULE, f"Symlinked .claude/ -> {source_path}")
except OSError as e:
debug_warning(
MODULE,
f"Could not symlink .claude/: {e}. Claude Code features may not work in worktree terminals.",
)
print_status(
"Warning: Could not link .claude/ - Claude Code features may not work in terminals",
"warning",
)
return symlinked
@@ -373,33 +374,13 @@ def setup_workspace(
f"Environment files copied: {', '.join(copied_env_files)}", "success"
)
# Set up dependencies in worktree using strategy-based dispatch
# Load project index if available for ecosystem-aware dependency handling
project_index = None
project_index_path = project_dir / ".auto-claude" / "project_index.json"
if project_index_path.is_file():
try:
with open(project_index_path, encoding="utf-8") as f:
project_index = json.load(f)
debug(MODULE, "Loaded project_index.json for dependency setup")
except (OSError, json.JSONDecodeError) as e:
debug_warning(MODULE, f"Could not load project_index.json: {e}")
dep_results = setup_worktree_dependencies(
project_dir, worktree_info.path, project_index=project_index
)
for strategy_name, paths in dep_results.items():
if paths:
print_status(
f"Dependencies ({strategy_name}): {', '.join(paths)}", "success"
)
# Symlink .claude/ config to worktree for Claude Code features (settings, commands, etc.)
symlinked_claude = symlink_claude_config_to_worktree(
# Symlink node_modules to worktree for TypeScript and tooling support
# This allows pre-commit hooks to run typecheck without npm install in worktree
symlinked_modules = symlink_node_modules_to_worktree(
project_dir, worktree_info.path
)
if symlinked_claude:
print_status(f"Claude config linked: {', '.join(symlinked_claude)}", "success")
if symlinked_modules:
print_status(f"Dependencies linked: {', '.join(symlinked_modules)}", "success")
# Copy security configuration files if they exist
# Note: Unlike env files, security files always overwrite to ensure
@@ -593,299 +574,6 @@ def initialize_timeline_tracking(
print(muted(f" Note: Timeline tracking could not be initialized: {e}"))
def setup_worktree_dependencies(
project_dir: Path,
worktree_path: Path,
project_index: dict | None = None,
) -> dict[str, list[str]]:
"""
Set up dependencies in a worktree using strategy-based dispatch.
Reads dependency configs from the project index and applies the correct
strategy for each: symlink, recreate, copy, or skip.
All operations are non-blocking — failures produce warnings but do not
prevent worktree creation.
Args:
project_dir: The main project directory
worktree_path: Path to the worktree
project_index: Parsed project_index.json dict, or None
Returns:
Dict mapping strategy names to lists of paths that were processed.
"""
configs = get_dependency_configs(project_index, project_dir=project_dir)
results: dict[str, list[str]] = {}
for config in configs:
strategy_name = config.strategy.value
if strategy_name not in results:
results[strategy_name] = []
try:
performed = True
if config.strategy == DependencyStrategy.SYMLINK:
performed = _apply_symlink_strategy(project_dir, worktree_path, config)
elif config.strategy == DependencyStrategy.RECREATE:
performed = _apply_recreate_strategy(project_dir, worktree_path, config)
elif config.strategy == DependencyStrategy.COPY:
performed = _apply_copy_strategy(project_dir, worktree_path, config)
elif config.strategy == DependencyStrategy.SKIP:
_apply_skip_strategy(config)
# Don't record skipped entries — only report actual work
continue
if performed:
results[strategy_name].append(config.source_rel_path)
except Exception as e:
debug_warning(
MODULE,
f"Failed to apply {strategy_name} strategy for "
f"{config.source_rel_path}: {e}",
)
return results
def _apply_symlink_strategy(
project_dir: Path,
worktree_path: Path,
config: DependencyShareConfig,
) -> bool:
"""Create a symlink (or Windows junction) from worktree to project source.
Returns True if a symlink was created, False if skipped.
"""
source_path = project_dir / config.source_rel_path
target_path = worktree_path / config.source_rel_path
if not source_path.exists():
debug(MODULE, f"Skipping symlink {config.source_rel_path} - source missing")
return False
if target_path.exists() or target_path.is_symlink():
debug(MODULE, f"Skipping symlink {config.source_rel_path} - target exists")
return False
target_path.parent.mkdir(parents=True, exist_ok=True)
try:
if is_windows():
# Windows: use directory junctions (no admin rights required).
# os.symlink creates a directory symlink that needs admin/DevMode,
# so we use mklink /J which creates a junction without privileges.
result = subprocess.run(
["cmd", "/c", "mklink", "/J", str(target_path), str(source_path)],
capture_output=True,
text=True,
timeout=30,
)
if result.returncode != 0:
raise OSError(result.stderr or "mklink /J failed")
else:
# macOS/Linux: relative symlinks for portability
relative_source = os.path.relpath(source_path, target_path.parent)
os.symlink(relative_source, target_path)
debug(MODULE, f"Symlinked {config.source_rel_path} -> {source_path}")
return True
except subprocess.TimeoutExpired:
debug_warning(
MODULE,
f"Symlink creation timed out for {config.source_rel_path}",
)
print_status(
f"Warning: Symlink creation timed out for {config.source_rel_path}",
"warning",
)
return False
except OSError as e:
debug_warning(
MODULE,
f"Could not symlink {config.source_rel_path}: {e}",
)
print_status(f"Warning: Could not link {config.source_rel_path}", "warning")
return False
def _apply_recreate_strategy(
project_dir: Path,
worktree_path: Path,
config: DependencyShareConfig,
) -> bool:
"""Create a fresh virtual environment in the worktree and install deps.
Returns True if the venv was successfully created, False if skipped or failed.
"""
venv_path = worktree_path / config.source_rel_path
if venv_path.exists():
debug(MODULE, f"Skipping recreate {config.source_rel_path} - already exists")
return False
# Detect Python executable from the source venv or fall back to sys.executable
source_venv = project_dir / config.source_rel_path
python_exec = sys.executable
if source_venv.exists():
# Try to use the same Python version as the source venv
for candidate in ("bin/python", "Scripts/python.exe"):
candidate_path = source_venv / candidate
if candidate_path.exists():
python_exec = str(candidate_path.resolve())
break
# Create the venv
try:
debug(MODULE, f"Creating venv at {venv_path}")
result = subprocess.run(
[python_exec, "-m", "venv", str(venv_path)],
capture_output=True,
text=True,
timeout=120,
)
if result.returncode != 0:
debug_warning(MODULE, f"venv creation failed: {result.stderr}")
print_status(
f"Warning: Could not create venv at {config.source_rel_path}",
"warning",
)
# Clean up partial venv so retries aren't blocked
if venv_path.exists():
shutil.rmtree(venv_path, ignore_errors=True)
return False
except subprocess.TimeoutExpired:
debug_warning(MODULE, f"venv creation timed out for {config.source_rel_path}")
print_status(
f"Warning: venv creation timed out for {config.source_rel_path}",
"warning",
)
# Clean up partial venv so retries aren't blocked
if venv_path.exists():
shutil.rmtree(venv_path, ignore_errors=True)
return False
# Install from requirements file if specified
req_file = config.requirements_file
if req_file:
req_path = project_dir / req_file
if req_path.is_file():
# Determine pip executable inside the new venv
if is_windows():
pip_exec = str(venv_path / "Scripts" / "pip.exe")
else:
pip_exec = str(venv_path / "bin" / "pip")
# Build install command based on file type
req_basename = Path(req_file).name
if req_basename == "pyproject.toml":
# pyproject.toml: snapshot-install from the worktree copy.
# Non-editable so the venv doesn't symlink back to the source.
worktree_req = worktree_path / req_file
install_dir = str(
worktree_req.parent if worktree_req.is_file() else req_path.parent
)
install_cmd = [pip_exec, "install", install_dir]
elif req_basename == "Pipfile":
# Pipfile: not directly installable via pip, skip
debug(
MODULE,
f"Skipping Pipfile-based install for {req_file} "
"(use pipenv in the worktree)",
)
install_cmd = None
else:
# requirements.txt or similar: pip install -r
install_cmd = [pip_exec, "install", "-r", str(req_path)]
if install_cmd:
try:
debug(MODULE, f"Installing deps from {req_file}")
pip_result = subprocess.run(
install_cmd,
capture_output=True,
text=True,
timeout=120,
)
if pip_result.returncode != 0:
debug_warning(
MODULE,
f"pip install failed (exit {pip_result.returncode}): "
f"{pip_result.stderr}",
)
print_status(
f"Warning: Dependency install failed for {req_file}",
"warning",
)
# Clean up broken venv so retries aren't blocked
if venv_path.exists():
shutil.rmtree(venv_path, ignore_errors=True)
return False
except subprocess.TimeoutExpired:
debug_warning(
MODULE,
f"pip install timed out for {req_file}",
)
print_status(
f"Warning: Dependency install timed out for {req_file}",
"warning",
)
# Clean up broken venv so retries aren't blocked
if venv_path.exists():
shutil.rmtree(venv_path, ignore_errors=True)
return False
except OSError as e:
debug_warning(MODULE, f"pip install failed: {e}")
# Clean up broken venv so retries aren't blocked
if venv_path.exists():
shutil.rmtree(venv_path, ignore_errors=True)
return False
debug(MODULE, f"Recreated venv at {config.source_rel_path}")
return True
def _apply_copy_strategy(
project_dir: Path,
worktree_path: Path,
config: DependencyShareConfig,
) -> bool:
"""Deep-copy a dependency directory from project to worktree.
Returns True if the copy was performed, False if skipped.
"""
source_path = project_dir / config.source_rel_path
target_path = worktree_path / config.source_rel_path
if not source_path.exists():
debug(MODULE, f"Skipping copy {config.source_rel_path} - source missing")
return False
if target_path.exists():
debug(MODULE, f"Skipping copy {config.source_rel_path} - target exists")
return False
target_path.parent.mkdir(parents=True, exist_ok=True)
try:
if source_path.is_file():
shutil.copy2(source_path, target_path)
else:
shutil.copytree(source_path, target_path)
debug(MODULE, f"Copied {config.source_rel_path} to worktree")
return True
except (OSError, shutil.Error) as e:
debug_warning(MODULE, f"Could not copy {config.source_rel_path}: {e}")
print_status(f"Warning: Could not copy {config.source_rel_path}", "warning")
return False
def _apply_skip_strategy(config: DependencyShareConfig) -> None:
"""Skip — nothing to do for this dependency type."""
debug(
MODULE, f"Skipping {config.dep_type} ({config.source_rel_path}) - skip strategy"
)
# Export private functions for backward compatibility
_ensure_timeline_hook_installed = ensure_timeline_hook_installed
_initialize_timeline_tracking = initialize_timeline_tracking
-2
View File
@@ -430,7 +430,6 @@ class WorktreeManager:
if os.path.samefile(resolved_path, current_path):
return line[len("branch refs/heads/") :]
except OSError:
# File system comparison errors are handled by fallback below
pass
# Fallback to normalized case comparison
if os.path.normcase(str(resolved_path)) == os.path.normcase(
@@ -511,7 +510,6 @@ class WorktreeManager:
if os.path.samefile(resolved_path, registered_path):
return True
except OSError:
# File system errors handled by fallback comparison below
pass
# Fallback to normalized case comparison for non-existent paths
if os.path.normcase(str(resolved_path)) == os.path.normcase(
@@ -5,9 +5,7 @@ Handles database connection, initialization, and lifecycle management.
Uses LadybugDB as the embedded graph database (no Docker required, Python 3.12+).
"""
import asyncio
import logging
import random
import sys
from datetime import datetime, timezone
@@ -16,27 +14,6 @@ from graphiti_config import GraphitiConfig, GraphitiState
logger = logging.getLogger(__name__)
# Retry configuration for LadybugDB lock contention
MAX_LOCK_RETRIES = 5
INITIAL_BACKOFF_SECONDS = 0.5
MAX_BACKOFF_SECONDS = 8.0
JITTER_PERCENT = 0.2
def _is_lock_error(error: Exception) -> bool:
"""Check if an error indicates database lock contention."""
error_msg = str(error).lower()
return "could not set lock" in error_msg or (
"lock" in error_msg and ("file" in error_msg or "database" in error_msg)
)
def _backoff_with_jitter(attempt: int) -> float:
"""Calculate exponential backoff with jitter for retry delays."""
backoff = min(INITIAL_BACKOFF_SECONDS * (2**attempt), MAX_BACKOFF_SECONDS)
jitter = backoff * JITTER_PERCENT * (2 * random.random() - 1)
return max(0.01, backoff + jitter)
def _apply_ladybug_monkeypatch() -> bool:
"""
@@ -219,36 +196,32 @@ class GraphitiClient:
)
db_path = self.config.get_db_path()
# Retry with exponential backoff for lock contention
for attempt in range(MAX_LOCK_RETRIES + 1):
try:
self._driver = create_patched_kuzu_driver(db=str(db_path))
if attempt > 0:
logger.info(
f"LadybugDB lock acquired after {attempt} retries"
)
break # Success
except Exception as e:
if _is_lock_error(e) and attempt < MAX_LOCK_RETRIES:
wait_time = _backoff_with_jitter(attempt)
logger.debug(
f"LadybugDB lock contention (attempt {attempt + 1}/{MAX_LOCK_RETRIES}), retrying in {wait_time:.2f}s"
)
await asyncio.sleep(wait_time)
continue
logger.warning(
f"Failed to initialize LadybugDB driver at {db_path}: {e}"
)
capture_exception(
e,
error_type=type(e).__name__,
db_path=str(db_path),
llm_provider=self.config.llm_provider,
embedder_provider=self.config.embedder_provider,
)
return False
try:
self._driver = create_patched_kuzu_driver(db=str(db_path))
except (OSError, PermissionError) as e:
logger.warning(
f"Failed to initialize LadybugDB driver at {db_path}: {e}"
)
capture_exception(
e,
error_type=type(e).__name__,
db_path=str(db_path),
llm_provider=self.config.llm_provider,
embedder_provider=self.config.embedder_provider,
)
return False
except Exception as e:
logger.warning(
f"Unexpected error initializing LadybugDB driver at {db_path}: {e}"
)
capture_exception(
e,
error_type=type(e).__name__,
db_path=str(db_path),
llm_provider=self.config.llm_provider,
embedder_provider=self.config.embedder_provider,
)
return False
logger.info(f"Initialized LadybugDB driver (patched) at: {db_path}")
except ImportError as e:
logger.warning(f"KuzuDriver not available: {e}")
@@ -1,100 +0,0 @@
<role>
You are a fix strategy specialist. You have been spawned to provide concrete, actionable fix approaches for a reported GitHub issue.
</role>
<mission>
Analyze the codebase and provide concrete fix approaches with specific files to modify, pros/cons for each approach, and a recommended solution that follows existing codebase patterns.
</mission>
<available_context>
The issue context below includes:
- Issue title, description, labels, and comments
- Recent git commits (last 20 commits) - use these to understand recent changes and patterns
</available_context>
<root_context_integration>
If a "Root Cause Analysis" section is provided below the issue context, use it as the foundation for your fix approaches. The root cause agent has already identified the exact code location and cause — your job is to design fix strategies that address that specific root cause.
This means you can skip Step 1 (understanding the problem space) when root cause context is available, and instead focus on designing fixes that target the identified code paths.
</root_context_integration>
<investigation_process>
<step_1>
<title>Understand the Problem Space</title>
- Read the issue description to understand what needs fixing
- Use Grep and Glob to locate the relevant code
- Read the affected files to understand the current implementation
</step_1>
<step_2>
<title>Study Existing Patterns</title>
- Search for similar patterns in the codebase using Grep
- Look at how related features or modules handle similar logic
- Identify coding conventions (naming, error handling, state management)
- Note any utility functions or shared abstractions that should be reused
</step_2>
<step_3>
<title>Design Fix Approaches</title>
For each approach, specify:
- What to change: Exact files and the nature of the modification
- Complexity: simple (< 1 hour), moderate (1-4 hours), complex (> 4 hours)
- Pros: Why this approach is good
- Cons: Risks or downsides of this approach
Provide at least 2 approaches when possible:
1. The minimal fix - smallest change that resolves the issue
2. The proper fix - addresses underlying design issues if applicable
</step_3>
<step_4>
<title>Identify Files to Modify</title>
- List ALL files that need changes across all approaches
- Include test files that need updating
- Include configuration or schema files if relevant
</step_4>
<step_5>
<title>Document Gotchas</title>
- Identify edge cases the fix must handle
- Note platform-specific concerns (Windows/macOS/Linux)
- Flag any migration or backward compatibility considerations
- Warn about tightly coupled code that could break
</step_5>
</investigation_process>
<pattern_discovery>
When exploring the codebase for patterns, look for:
- How similar bugs were fixed in the past (git log, related files)
- Existing utility functions that could be reused
- Framework-level patterns (e.g., error handling middleware, validation layers)
- Test patterns for the affected module
</pattern_discovery>
<evidence_requirements>
Every fix approach MUST include:
1. Specific file paths - Not "the auth module" but "src/auth/login.ts"
2. Nature of change - What code to add, modify, or remove
3. Pattern references - Links to existing code that demonstrates the approach
4. Complexity assessment - Realistic effort estimate
</evidence_requirements>
<constraints>
- Do not provide vague advice like "improve error handling"
- Do not suggest rewriting large portions of code unless necessary
- Do not ignore existing patterns in favor of "better" approaches
- Do not recommend approaches that conflict with the project's architecture
- Do not assess impact or severity (that is the Impact Assessor's job)
- Do not analyze root cause (that is the Root Cause Analyzer's job)
</constraints>
<output_format>
Provide your analysis as structured output with:
- approaches: List of fix approaches ordered by recommendation
- recommended_approach: Index of the recommended approach
- files_to_modify: All files that need modification across all approaches
- patterns_to_follow: Existing codebase patterns the fix should follow
- gotchas: Potential pitfalls when implementing the fix
</output_format>
@@ -1,89 +0,0 @@
<role>
You are an impact assessment specialist. You have been spawned to evaluate the blast radius and severity of a reported GitHub issue.
</role>
<mission>
Assess how far-reaching the reported issue is: which components are affected, how users are impacted, and what risks exist if the issue is fixed incorrectly.
</mission>
<available_context>
The issue context below includes:
- Issue title, description, labels, and comments
- Recent git commits (last 20 commits) - use these to identify recent changes that may affect impact assessment
</available_context>
<root_context_integration>
If a "Root Cause Analysis" section is provided below the issue context, use it as the starting point for your impact assessment. The root cause agent has already identified the problematic code — your job is to trace outward from those code paths to determine blast radius and severity.
This means you can skip Steps 1-2 (identifying affected code) when root cause context is available, and instead focus on mapping dependencies outward from the identified code paths.
</root_context_integration>
<investigation_process>
<step_1>
<title>Identify Affected Code</title>
- Use Grep to find all references to the functions/modules mentioned in the issue
- Use Glob to find related files by naming patterns
- Read each affected file to understand its role
</step_1>
<step_2>
<title>Map the Dependency Graph</title>
- Trace imports and call chains from the affected code outward
- Identify which features, views, or API endpoints depend on the buggy code
- Categorize each affected component as:
- direct - Code that directly uses the buggy function/module
- indirect - Code that depends on code that uses the buggy code
- dependency - External consumers or downstream systems affected
</step_2>
<step_3>
<title>Assess User Impact</title>
- Determine which user-facing features are affected
- Evaluate if data integrity is at risk
- Check if the issue causes crashes, data loss, or security exposure
- Consider the frequency: does this affect all users or edge cases only?
</step_3>
<step_4>
<title>Evaluate Severity</title>
- critical - Data loss, security vulnerability, system crash for most users
- high - Major feature broken, significant workflow disruption
- medium - Feature partially broken, workaround exists
- low - Minor inconvenience, cosmetic issue, edge case only
</step_4>
<step_5>
<title>Assess Regression Risk</title>
- Identify what could break if the issue is fixed
- Look for tightly coupled code that might be affected by a fix
- Check if the affected code has test coverage
- Evaluate whether a fix could introduce new issues
</step_5>
</investigation_process>
<evidence_requirements>
Every impact assessment MUST include:
1. File paths - Exact locations of affected components
2. Component names - Clear identification of what is affected
3. Impact type classification - direct/indirect/dependency for each component
4. User-facing description - How end users experience the problem
</evidence_requirements>
<constraints>
- Do not identify the root cause (that is the Root Cause Analyzer's job)
- Do not suggest fixes (that is the Fix Advisor's job)
- Do not speculate about impact without reading the actual code
- Do not inflate severity; be objective and evidence-based
- Do not report components as affected unless you verified the dependency
</constraints>
<output_format>
Provide your analysis as structured output with:
- severity: Overall severity (critical/high/medium/low)
- affected_components: List of affected components with file paths and impact types
- blast_radius: Description of how far-reaching the impact is
- user_impact: How end users are affected
- regression_risk: Risk assessment for potential fixes
</output_format>
@@ -1,80 +0,0 @@
<role>
You are a reproduction and testing specialist. You have been spawned to determine if a reported GitHub issue is reproducible and assess the test coverage of the affected code.
</role>
<mission>
Determine whether the issue can be reproduced, document reproduction steps, assess existing test coverage for the affected code paths, and suggest how to write a test that verifies the fix.
</mission>
<available_context>
The issue context below includes:
- Issue title, description, labels, and comments
- Recent git commits (last 20 commits) - use these to check if tests were recently added or modified
</available_context>
<investigation_process>
<step_1>
<title>Analyze Reproducibility</title>
- Read the issue description for any reproduction steps provided
- Identify the conditions under which the bug manifests
- Determine if the issue depends on specific state, timing, or environment
- Classify reproducibility:
- yes - Clear, deterministic steps to reproduce
- likely - High probability of reproduction with the right conditions
- unlikely - Depends on rare conditions or timing
- no - Cannot be reproduced (e.g., already fixed, environment-specific)
</step_1>
<step_2>
<title>Document Reproduction Steps</title>
- Write clear, numbered steps from a clean starting state
- Include any required configuration or environment setup
- Specify expected vs actual behavior at each step
- Note any prerequisites (specific data, user state, feature flags)
</step_2>
<step_3>
<title>Assess Test Coverage</title>
- Use Glob to find test files related to the affected code
- Common patterns: *test*, *spec*, __tests__/, tests/
- Read the test files to understand what is covered
- Identify gaps: which code paths lack tests?
- Check if the specific scenario described in the issue is tested
</step_3>
<step_4>
<title>Suggest Test Approach</title>
- Recommend what type of test to write (unit, integration, e2e)
- Describe what the test should verify
- Reference existing test patterns in the codebase
- Include any mocking or setup requirements
</step_4>
</investigation_process>
<evidence_requirements>
Every reproduction analysis MUST include:
1. Test file paths - Existing test files for the affected code
2. Coverage assessment - What is tested and what is not
3. Concrete reproduction steps - Not vague descriptions
4. Test approach - Specific enough for a developer to implement
</evidence_requirements>
<constraints>
- Do not write the actual test code (just describe the approach)
- Do not identify the root cause (that is the Root Cause Analyzer's job)
- Do not suggest fixes (that is the Fix Advisor's job)
- Do not assess impact (that is the Impact Assessor's job)
- Do not claim an issue is unreproducible without checking the code
- Do not list test files you haven't actually read
</constraints>
<output_format>
Provide your analysis as structured output with:
- reproducible: Whether the issue can be reproduced (yes/likely/unlikely/no)
- reproduction_steps: Numbered steps to reproduce the issue
- test_coverage: Assessment of existing test coverage (has_existing_tests, test_files, coverage_assessment)
- related_test_files: Test files related to the affected code
- suggested_test_approach: How to write a test that verifies the fix
</output_format>
@@ -1,114 +0,0 @@
<role>
You are a root cause analysis specialist. You have been spawned to trace the source of a bug or issue reported in a GitHub issue.
</role>
<mission>
Identify the root cause of the reported issue by tracing through the codebase. Find the exact code path from entry point to the underlying problem.
</mission>
<available_context>
The issue context below includes:
- Issue title, description, labels, and comments
- Recent git commits (last 20 commits) - USE THESE to identify recent changes that may have introduced the bug
</available_context>
<image_analysis>
If the issue context includes an "Images" section with screenshot URLs:
- Treat these as PRIMARY EVIDENCE - screenshots often show the actual bug manifestation
- Use image URLs to understand visual bugs, UI issues, error messages, or crash screens
- When the issue description is vague but screenshots are provided, prioritize the visual evidence
- Describe what you see in the images in your evidence section (e.g., "Screenshot shows X component displaying incorrectly")
- If the screenshot shows an error message, trace that error in the codebase
</image_analysis>
<investigation_process>
<step_1>
<title>Understand the Issue</title>
- Check the Images section first - screenshots may show the actual bug
- Read the issue title and description carefully
- Identify the reported symptoms (error messages, unexpected behavior, crashes)
- Note any file paths, stack traces, or code references mentioned
- If screenshots are provided, describe the visual evidence you observe
</step_1>
<step_2>
<title>Review Recent Commits</title>
- Check the Recent Commits section in the issue context
- Look for commits that modified files related to the issue
- Identify recent changes that might have introduced the bug
- If a commit message mentions the issue or related symptoms, investigate that commit first
</step_2>
<step_3>
<title>Locate Entry Points</title>
- Use Grep to find relevant functions, classes, or files mentioned in the issue
- Identify the user-facing entry point where the problem manifests
- Read the entry point code with surrounding context
</step_3>
<step_4>
<title>Trace the Code Path</title>
- Follow the execution flow from the entry point inward
- Use Grep to find function definitions, callers, and imports
- Read each file in the chain to understand data flow
- Identify where the logic diverges from expected behavior
</step_4>
<step_5>
<title>Identify the Root Cause</title>
- Pinpoint the exact code location where the bug originates
- Distinguish between the symptom (where the error appears) and the cause (where the logic is wrong)
- Check if the issue is in recently changed code or a pre-existing problem
- Cross-reference with recent commits - did a recent change introduce this issue?
</step_5>
<step_6>
<title>Check If Already Fixed</title>
- Search for recent changes to the affected files using git log or git show
- Look for commits that might have addressed this issue
- Check if the problematic code pattern still exists in the current codebase
</step_6>
</investigation_process>
<evidence_requirements>
Every root cause identification MUST include:
1. File paths and line numbers - Exact locations in the codebase
2. Code snippets - Copy-paste the actual problematic code (not descriptions)
3. Execution trace - How the code flows from entry point to the bug
4. Confidence level - How certain you are about the root cause
</evidence_requirements>
<confidence_levels>
- high - You found the exact code causing the issue, verified with code evidence
- medium - You identified a likely cause but could not fully verify (e.g., depends on runtime state)
- low - You found a plausible explanation but other causes are equally likely
</confidence_levels>
<constraints>
- Do not speculate without reading the actual code
- Do not report multiple unrelated potential causes; identify the MOST LIKELY one
- Do not suggest fixes (that is the Fix Advisor's job)
- Do not assess impact (that is the Impact Assessor's job)
- Do not explore code paths unrelated to the reported issue
</constraints>
<depth_requirements>
- You MUST trace at least 3 levels deep in the call chain (entry point → intermediate → root cause location) before concluding
- You MUST explore at least 2 competing hypotheses before settling on a root cause — read both code paths and explain why one is more likely
- Do NOT conclude with "medium" or "low" confidence if you still have unexplored code paths you could Read or Grep
- If the issue mentions a UI behavior, trace it from the React component through the store, IPC handler, and into the backend
- If you find the likely cause early, keep investigating to VERIFY it — read callers, check edge cases, look for related patterns
- Use your full tool budget. Read more files, run more greps. Thoroughness is more valuable than speed for root cause analysis
</depth_requirements>
<output_format>
Provide your analysis as structured output with:
- identified_root_cause: Clear description of what causes the issue
- code_paths: Ordered list of code locations from entry point to root cause
- confidence: Your confidence level (high/medium/low)
- evidence: Code snippets and traces supporting your analysis
- related_issues: Known issue patterns this matches (e.g., "race condition", "null reference")
- likely_already_fixed: True if evidence suggests the issue is already resolved
</output_format>
-34
View File
@@ -13,7 +13,6 @@ from pathlib import Path
# Memory integration for cross-session learning
from agents.base import sanitize_error_message
from agents.investigation_context import load_investigation_context
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
@@ -128,39 +127,6 @@ async def run_qa_fixer_session(
print("✓ Memory context loaded for QA fixer")
debug_success("qa_fixer", "Graphiti memory context loaded for fixer")
# Load investigation context for GitHub-sourced tasks
investigation_context = load_investigation_context(spec_dir)
if investigation_context:
# Format investigation context for the fixer
inv = investigation_context
inv_prompt = "\n## Investigation Context\n\n"
inv_prompt += "The original investigation identified:\n\n"
if inv.get("root_cause", {}).get("summary"):
inv_prompt += f"- **Root cause**: {inv['root_cause']['summary']}\n"
if inv.get("reproducer"):
inv_prompt += f"- **Reproducer**: {inv['reproducer']}\n"
if inv.get("fix_approaches"):
inv_prompt += "\n**Recommended fix approaches:**\n"
for i, approach in enumerate(inv["fix_approaches"][:3], 1):
inv_prompt += f"{i}. {approach.get('name', 'Approach')}: "
if approach.get("description"):
inv_prompt += approach["description"]
inv_prompt += "\n"
inv_prompt += (
"\n**Guidance**: Ensure your fix actually addresses these findings. "
)
inv_prompt += (
"Don't just make the QA errors go away — fix the underlying issue.\n"
)
prompt += inv_prompt
print("✓ Investigation context loaded for QA fixer")
debug_success("qa_fixer", "Investigation context loaded for fixer")
# Add session context - use full path so agent can find files
prompt += f"\n\n---\n\n**Fix Session**: {fix_session}\n"
prompt += f"**Spec Directory**: {spec_dir}\n"
-2
View File
@@ -215,7 +215,6 @@ async def run_qa_validation_loop(
"Removed QA_FIX_REQUEST.md after permanent fixer error",
)
except OSError:
# File removal failure is not critical here
pass
return False
@@ -231,7 +230,6 @@ async def run_qa_validation_loop(
fix_request_file.unlink()
debug("qa_loop", "Removed processed QA_FIX_REQUEST.md")
except OSError:
# File removal failure is not critical here
pass # Ignore if file removal fails
# Check for no-test projects
-93
View File
@@ -8,17 +8,12 @@ acceptance criteria.
Memory Integration:
- Retrieves past patterns, gotchas, and insights before QA session
- Saves QA findings (bugs, patterns, validation outcomes) after session
Investigation Integration:
- Loads GitHub investigation context for issue-based validation
"""
import json
from pathlib import Path
# Memory integration for cross-session learning
from agents.base import sanitize_error_message
from agents.investigation_context import load_investigation_for_qa
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
@@ -112,94 +107,6 @@ async def run_qa_agent_session(
print("✓ Memory context loaded for QA reviewer")
debug_success("qa_reviewer", "Graphiti memory context loaded for QA")
# Load investigation context for GitHub-sourced tasks
task_metadata_path = spec_dir / "task_metadata.json"
base_branch = "main" # Default base branch
# Try to read base_branch from task_metadata.json
if task_metadata_path.exists():
try:
with open(task_metadata_path) as f:
task_metadata = json.load(f)
base_branch = task_metadata.get("baseBranch", "main")
except (json.JSONDecodeError, OSError):
pass
investigation_context = load_investigation_for_qa(spec_dir, base_branch)
if investigation_context:
# Format investigation context for the prompt
inv = investigation_context
inv_prompt = "\n## GitHub Issue Validation\n\n"
inv_prompt += "This task was created from a GitHub issue investigation. "
inv_prompt += "You must verify that the issue is actually fixed.\n\n"
if inv.get("root_cause", {}).get("summary"):
inv_prompt += f"**Root Cause:** {inv['root_cause']['summary']}\n\n"
if inv.get("root_cause", {}).get("evidence"):
inv_prompt += (
f"**Evidence of the Issue:**\n{inv['root_cause']['evidence']}\n\n"
)
if inv.get("root_cause", {}).get("code_paths"):
inv_prompt += "**Affected Code Paths:**\n"
for path in inv["root_cause"]["code_paths"]:
file_ref = path.get("file", "unknown")
start = path.get("start_line", "")
end = path.get("end_line", "")
desc = path.get("description", "")
line_range = f":{start}-{end}" if start and end else ""
inv_prompt += f"- `{file_ref}{line_range}`"
if desc:
inv_prompt += f"{desc}"
inv_prompt += "\n"
inv_prompt += "\n"
if inv.get("reproducer"):
reproducer = inv["reproducer"]
inv_prompt += "**Verification Steps:**\n"
steps = reproducer.get("reproduction_steps", [])
for step in steps:
inv_prompt += f"- {step}\n"
test_approach = reproducer.get("suggested_test_approach")
if test_approach:
inv_prompt += f"\n**Suggested Test Approach:** {test_approach}\n"
inv_prompt += "\n"
inv_prompt += "**ACTION REQUIRED:** Attempt to reproduce the issue following these steps. "
inv_prompt += "If a reproducer script or test is mentioned, run it to confirm the issue is fixed.\n\n"
if inv.get("impact"):
inv_prompt += "**Expected Impact:**\n"
if inv["impact"].get("severity"):
inv_prompt += f"- **Severity:** {inv['impact']['severity']}\n"
if inv["impact"].get("user_impact"):
inv_prompt += f"- **User Impact:** {inv['impact']['user_impact']}\n"
if inv["impact"].get("blast_radius"):
inv_prompt += f"- **Blast Radius:** {inv['impact']['blast_radius']}\n"
if inv["impact"].get("regression_risk"):
inv_prompt += (
f"- **Regression Risk:** {inv['impact']['regression_risk']}\n"
)
inv_prompt += "\n"
inv_prompt += "**Validation Checklist:**\n"
inv_prompt += "In your review, you MUST:\n"
inv_prompt += "1. Verify the root cause is addressed\n"
inv_prompt += "2. Validate the issue is resolved"
if inv.get("reproducer"):
inv_prompt += " (run the reproducer)\n"
else:
inv_prompt += "\n"
inv_prompt += "3. Check for regressions\n"
inv_prompt += "4. Verify minimal changes\n\n"
inv_prompt += f"You are comparing changes from branch `{inv.get('base_branch', 'main')}` to the current worktree.\n"
inv_prompt += "Focus your review on whether these changes actually fix the described issue.\n"
prompt += inv_prompt
print("✓ Investigation context loaded for QA reviewer")
debug_success("qa_reviewer", "Investigation context loaded for QA validation")
# Add session context
prompt += f"\n\n---\n\n**QA Session**: {qa_session}\n"
prompt += f"**Max Iterations**: {max_iterations}\n"
+6 -4
View File
@@ -153,8 +153,9 @@ class BotDetector:
# Identify bot username from token
self.bot_username = self._get_bot_username()
logger.debug(
f"[BotDetector] Initialized: bot_user={self.bot_username}, review_own_prs={review_own_prs}"
print(
f"[BotDetector] Initialized: bot_user={self.bot_username}, review_own_prs={review_own_prs}",
file=sys.stderr,
)
def _get_bot_username(self) -> str | None:
@@ -165,8 +166,9 @@ class BotDetector:
Bot username or None if token not provided or invalid
"""
if not self.bot_token:
logger.debug(
"[BotDetector] No bot token provided, cannot identify bot user"
print(
"[BotDetector] No bot token provided, cannot identify bot user",
file=sys.stderr,
)
return None
+9 -171
View File
@@ -331,12 +331,7 @@ class GHClient:
args = self._add_repo_flag(args)
result = await self.run(args)
try:
return json.loads(result.stdout) if result.stdout.strip() else []
except json.JSONDecodeError:
logger.warning("Failed to parse PR list response as JSON")
logger.debug("Raw stdout (truncated): %s", result.stdout[:200])
return []
return json.loads(result.stdout)
async def pr_get(
self, pr_number: int, json_fields: list[str] | None = None
@@ -376,12 +371,7 @@ class GHClient:
args = self._add_repo_flag(args)
result = await self.run(args)
try:
return json.loads(result.stdout) if result.stdout.strip() else {}
except json.JSONDecodeError:
logger.warning("Failed to parse PR #%d response as JSON", pr_number)
logger.debug("Raw stdout (truncated): %s", result.stdout[:200])
return {}
return json.loads(result.stdout)
async def pr_diff(self, pr_number: int) -> str:
"""
@@ -486,15 +476,9 @@ class GHClient:
"--json",
",".join(json_fields),
]
args = self._add_repo_flag(args)
result = await self.run(args)
try:
return json.loads(result.stdout) if result.stdout.strip() else []
except json.JSONDecodeError:
logger.warning("Failed to parse issue list response as JSON")
logger.debug("Raw stdout (truncated): %s", result.stdout[:200])
return []
return json.loads(result.stdout)
async def issue_get(
self, issue_number: int, json_fields: list[str] | None = None
@@ -529,135 +513,20 @@ class GHClient:
"--json",
",".join(json_fields),
]
args = self._add_repo_flag(args)
result = await self.run(args)
try:
return json.loads(result.stdout) if result.stdout.strip() else {}
except json.JSONDecodeError:
logger.warning("Failed to parse issue #%d response as JSON", issue_number)
logger.debug("Raw stdout (truncated): %s", result.stdout[:200])
return {}
return json.loads(result.stdout)
async def issue_comment(self, issue_number: int, body: str) -> int:
async def issue_comment(self, issue_number: int, body: str) -> None:
"""
Post a comment to an issue.
Args:
issue_number: Issue number
body: Comment body
Returns:
The ID of the created comment
Raises:
GHCommandError: If the gh CLI command fails
ValueError: If the comment ID is not returned
"""
# Use GitHub API directly via 'gh api' to post the comment
# This allows us to get JSON response with the comment ID
# Note: gh issue comment doesn't support --json flag, but gh api does
endpoint = f"repos/{{owner}}/{{repo}}/issues/{issue_number}/comments"
args = [
"api",
"--method",
"POST",
endpoint,
"-f",
f"body={body}",
"--jq",
".id",
]
# Note: gh api doesn't use -R flag, the repo is already in the endpoint URL
# Only add -R if we have a custom repo configured (not the default from git)
if self.repo:
# For gh api, we need to use --hostname to specify a different repo
# But since the endpoint already has {{owner}}/{{repo}}, it will be
# expanded correctly by gh CLI using the default git remote
pass
result = await self.run(args, raise_on_error=False)
# Check for gh CLI errors
if result.returncode != 0:
stderr_lower = result.stderr.lower()
error_msg = (
result.stderr.strip()
or f"gh CLI failed with exit code {result.returncode}"
)
# Provide user-friendly error messages for common failure modes
if (
"401" in result.stderr
or "unauthenticated" in stderr_lower
or "not logged in" in stderr_lower
):
raise GHCommandError(
"GitHub authentication failed. Please run 'gh auth login' to authenticate."
)
elif (
"403" in result.stderr
or "429" in result.stderr
or "rate limit" in stderr_lower
):
raise GHCommandError(
"GitHub API rate limit exceeded. Please wait a few minutes before trying again."
)
elif "404" in result.stderr or "not found" in stderr_lower:
if self.repo:
raise GHCommandError(
f"Repository or issue not found. Please verify that the repository '{self.repo}' exists and you have access to it."
)
else:
raise GHCommandError(
f"Issue #{issue_number} not found. Please verify that the issue exists in the repository."
)
elif "permission" in stderr_lower or "forbidden" in stderr_lower:
raise GHCommandError(
"You do not have permission to comment on this issue. Please ensure you have write access to the repository."
)
# Generic error with stderr output
raise GHCommandError(f"Failed to post comment to GitHub: {error_msg}")
# Parse the comment ID from the output (gh api --jq .id returns just the ID)
try:
stdout = result.stdout.strip()
if not stdout:
raise ValueError("Empty response from GitHub API")
comment_id = int(stdout)
if comment_id:
return comment_id
else:
raise ValueError(f"Invalid comment ID returned: {stdout}")
except (ValueError, json.JSONDecodeError) as e:
logger.error(
"Failed to parse comment ID response for issue #%d: %s", issue_number, e
)
logger.debug("Raw stdout (first 500 chars): %s", result.stdout[:500])
logger.debug("Raw stderr (first 500 chars): %s", result.stderr[:500])
raise ValueError(f"Failed to parse GitHub response: {str(e)}")
async def issue_comments(self, issue_number: int) -> list[dict]:
"""
Fetch comments on an issue.
Args:
issue_number: Issue number
Returns:
List of comment dicts from the GitHub API
"""
endpoint = f"repos/{{owner}}/{{repo}}/issues/{issue_number}/comments"
args = ["api", "--method", "GET", endpoint]
result = await self.run(args, raise_on_error=False)
if result.returncode == 0:
try:
return json.loads(result.stdout)
except json.JSONDecodeError:
logger.warning(f"Failed to parse comments for issue #{issue_number}")
return []
args = ["issue", "comment", str(issue_number), "--body", body]
await self.run(args)
async def issue_add_labels(self, issue_number: int, labels: list[str]) -> None:
"""
@@ -677,7 +546,6 @@ class GHClient:
"--add-label",
",".join(labels),
]
args = self._add_repo_flag(args)
await self.run(args)
async def issue_remove_labels(self, issue_number: int, labels: list[str]) -> None:
@@ -698,7 +566,6 @@ class GHClient:
"--remove-label",
",".join(labels),
]
args = self._add_repo_flag(args)
# Don't raise on error - labels might not exist
await self.run(args, raise_on_error=False)
@@ -720,12 +587,7 @@ class GHClient:
args.extend(["-f", f"{key}={value}"])
result = await self.run(args)
try:
return json.loads(result.stdout) if result.stdout.strip() else {}
except json.JSONDecodeError:
logger.warning("Failed to parse API response for %s as JSON", endpoint)
logger.debug("Raw stdout (truncated): %s", result.stdout[:200])
return {}
return json.loads(result.stdout)
async def pr_merge(
self,
@@ -765,25 +627,6 @@ class GHClient:
args = self._add_repo_flag(args)
await self.run(args)
async def pr_comment_reply(
self, pr_number: int, comment_id: int, body: str
) -> None:
"""
Reply to a specific review comment on a pull request.
Uses: POST /repos/{owner}/{repo}/pulls/{pull_number}/comments/{comment_id}/replies
Args:
pr_number: PR number (used for the API endpoint)
comment_id: The ID of the review comment to reply to
body: Reply body text
"""
endpoint = (
f"repos/{{owner}}/{{repo}}/pulls/{pr_number}/comments/{comment_id}/replies"
)
args = ["api", "--method", "POST", endpoint, "-f", f"body={body}"]
await self.run(args)
async def pr_get_assignees(self, pr_number: int) -> list[str]:
"""
Get assignees for a pull request.
@@ -843,12 +686,7 @@ class GHClient:
args = ["api", endpoint]
result = await self.run(args, timeout=60.0) # Longer timeout for large diffs
try:
return json.loads(result.stdout) if result.stdout.strip() else {}
except json.JSONDecodeError:
logger.warning("Failed to parse commit comparison response as JSON")
logger.debug("Raw stdout (truncated): %s", result.stdout[:200])
return {}
return json.loads(result.stdout)
async def get_comments_since(
self, pr_number: int, since_timestamp: str
+12 -56
View File
@@ -12,7 +12,7 @@ from __future__ import annotations
import json
from dataclasses import dataclass, field
from datetime import datetime, timezone
from datetime import datetime
from enum import Enum
from pathlib import Path
@@ -22,11 +22,6 @@ except (ImportError, ValueError, SystemError):
from file_lock import locked_json_update, locked_json_write
def _utc_now_iso() -> str:
"""Return current UTC time as ISO 8601 string with timezone info."""
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
class ReviewSeverity(str, Enum):
"""Severity levels for PR review findings."""
@@ -526,7 +521,7 @@ class PRReviewResult:
summary: str = ""
overall_status: str = "comment" # approve, request_changes, comment
review_id: int | None = None
reviewed_at: str = field(default_factory=lambda: _utc_now_iso())
reviewed_at: str = field(default_factory=lambda: datetime.now().isoformat())
error: str | None = None
# NEW: Enhanced verdict system
@@ -572,9 +567,6 @@ class PRReviewResult:
) # IDs of posted findings
posted_at: str | None = None # Timestamp when findings were posted
# In-progress review tracking
in_progress_since: str | None = None # ISO timestamp when active review started
def to_dict(self) -> dict:
return {
"pr_number": self.pr_number,
@@ -606,8 +598,6 @@ class PRReviewResult:
"has_posted_findings": self.has_posted_findings,
"posted_finding_ids": self.posted_finding_ids,
"posted_at": self.posted_at,
# In-progress review tracking
"in_progress_since": self.in_progress_since,
}
@classmethod
@@ -620,7 +610,7 @@ class PRReviewResult:
summary=data.get("summary", ""),
overall_status=data.get("overall_status", "comment"),
review_id=data.get("review_id"),
reviewed_at=data.get("reviewed_at", _utc_now_iso()),
reviewed_at=data.get("reviewed_at", datetime.now().isoformat()),
error=data.get("error"),
# NEW fields
verdict=MergeVerdict(data.get("verdict", "ready_to_merge")),
@@ -655,8 +645,6 @@ class PRReviewResult:
has_posted_findings=data.get("has_posted_findings", False),
posted_finding_ids=data.get("posted_finding_ids", []),
posted_at=data.get("posted_at"),
# In-progress review tracking
in_progress_since=data.get("in_progress_since"),
)
async def save(self, github_dir: Path) -> None:
@@ -703,7 +691,7 @@ class PRReviewResult:
reviews.append(entry)
current_data["reviews"] = reviews
current_data["last_updated"] = _utc_now_iso()
current_data["last_updated"] = datetime.now().isoformat()
return current_data
@@ -774,7 +762,7 @@ class TriageResult:
suggested_breakdown: list[str] = field(default_factory=list)
priority: str = "medium" # high, medium, low
comment: str | None = None
triaged_at: str = field(default_factory=lambda: _utc_now_iso())
triaged_at: str = field(default_factory=lambda: datetime.now().isoformat())
def to_dict(self) -> dict:
return {
@@ -810,7 +798,7 @@ class TriageResult:
suggested_breakdown=data.get("suggested_breakdown", []),
priority=data.get("priority", "medium"),
comment=data.get("comment"),
triaged_at=data.get("triaged_at", _utc_now_iso()),
triaged_at=data.get("triaged_at", datetime.now().isoformat()),
)
async def save(self, github_dir: Path) -> None:
@@ -848,8 +836,8 @@ class AutoFixState:
pr_url: str | None = None
bot_comments: list[str] = field(default_factory=list)
error: str | None = None
created_at: str = field(default_factory=lambda: _utc_now_iso())
updated_at: str = field(default_factory=lambda: _utc_now_iso())
created_at: str = field(default_factory=lambda: datetime.now().isoformat())
updated_at: str = field(default_factory=lambda: datetime.now().isoformat())
def to_dict(self) -> dict:
return {
@@ -887,8 +875,8 @@ class AutoFixState:
pr_url=data.get("pr_url"),
bot_comments=data.get("bot_comments", []),
error=data.get("error"),
created_at=data.get("created_at", _utc_now_iso()),
updated_at=data.get("updated_at", _utc_now_iso()),
created_at=data.get("created_at", datetime.now().isoformat()),
updated_at=data.get("updated_at", datetime.now().isoformat()),
)
def update_status(self, status: AutoFixStatus) -> None:
@@ -898,7 +886,7 @@ class AutoFixState:
f"Invalid state transition: {self.status.value} -> {status.value}"
)
self.status = status
self.updated_at = _utc_now_iso()
self.updated_at = datetime.now().isoformat()
async def save(self, github_dir: Path) -> None:
"""Save auto-fix state to .auto-claude/github/issues/ with file locking."""
@@ -950,7 +938,7 @@ class AutoFixState:
queue.append(entry)
current_data["auto_fix_queue"] = queue
current_data["last_updated"] = _utc_now_iso()
current_data["last_updated"] = datetime.now().isoformat()
return current_data
@@ -1004,12 +992,6 @@ class GitHubRunnerConfig:
True # Use SDK subagent parallel orchestrator (default)
)
# Investigation settings
investigation_auto_post: bool = False
investigation_auto_close: bool = False
investigation_max_parallel: int = 3
investigation_pipeline_mode: str = "full"
# Model settings
# Note: Default uses shorthand "sonnet" which gets resolved via resolve_model_id()
# to respect environment variable overrides (e.g., ANTHROPIC_DEFAULT_SONNET_MODEL)
@@ -1017,10 +999,6 @@ class GitHubRunnerConfig:
thinking_level: str = "medium"
fast_mode: bool = False
# Per-specialist investigation config (overrides model/thinking_level for each specialist)
# Dict mapping specialist name → {"model": str, "thinking": str}
specialist_config: dict[str, dict[str, str]] | None = None
def to_dict(self) -> dict:
return {
"token": "***", # Never save token
@@ -1043,10 +1021,6 @@ class GitHubRunnerConfig:
"model": self.model,
"thinking_level": self.thinking_level,
"fast_mode": self.fast_mode,
"investigation_auto_post": self.investigation_auto_post,
"investigation_auto_close": self.investigation_auto_close,
"investigation_max_parallel": self.investigation_max_parallel,
"investigation_pipeline_mode": self.investigation_pipeline_mode,
}
def save_settings(self, github_dir: Path) -> None:
@@ -1075,9 +1049,6 @@ class GitHubRunnerConfig:
else:
settings = {}
# Load investigation_settings from nested structure if present
investigation_settings = settings.get("investigation_settings", {})
return cls(
token=token,
repo=repo,
@@ -1103,19 +1074,4 @@ class GitHubRunnerConfig:
# Note: model is stored as shorthand and resolved via resolve_model_id()
model=settings.get("model", "sonnet"),
thinking_level=settings.get("thinking_level", "medium"),
# Load fast_mode from investigation_settings (front-end uses camelCase "fastInvestigations")
fast_mode=investigation_settings.get("fastInvestigations", False),
# Load investigation settings from nested structure
investigation_auto_post=investigation_settings.get(
"autoPostToGitHub", False
),
investigation_auto_close=investigation_settings.get(
"autoCloseIssues", False
),
investigation_max_parallel=investigation_settings.get(
"maxParallelInvestigations", 3
),
investigation_pipeline_mode=investigation_settings.get(
"pipelineMode", "full"
),
)
@@ -53,6 +53,7 @@ class RepoConfig:
relationship: Relationship to other repos
upstream_repo: Upstream repo if this is a fork
labels: Label configuration overrides
trust_level: Trust level for this repo
"""
repo: str # owner/repo format
@@ -63,6 +64,7 @@ class RepoConfig:
labels: dict[str, list[str]] = field(
default_factory=dict
) # e.g., {"auto_fix": ["fix-me"]}
trust_level: int = 0 # 0-4 trust level
display_name: str | None = None # Human-readable name
# Feature toggles per repo
@@ -123,6 +125,7 @@ class RepoConfig:
"relationship": self.relationship.value,
"upstream_repo": self.upstream_repo,
"labels": self.labels,
"trust_level": self.trust_level,
"display_name": self.display_name,
"auto_fix_enabled": self.auto_fix_enabled,
"pr_review_enabled": self.pr_review_enabled,
@@ -138,6 +141,7 @@ class RepoConfig:
relationship=RepoRelationship(data.get("relationship", "standalone")),
upstream_repo=data.get("upstream_repo"),
labels=data.get("labels", {}),
trust_level=data.get("trust_level", 0),
display_name=data.get("display_name"),
auto_fix_enabled=data.get("auto_fix_enabled", True),
pr_review_enabled=data.get("pr_review_enabled", True),
+4 -409
View File
@@ -43,12 +43,9 @@ try:
from .services import (
AutoFixProcessor,
BatchProcessor,
EnrichmentEngine,
PRReviewEngine,
SplitEngine,
TriageEngine,
)
from .services.investigation_label_manager import InvestigationLabelManager
from .services.io_utils import safe_print
except (ImportError, ValueError, SystemError):
# When imported directly (runner.py adds github dir to path)
@@ -75,12 +72,9 @@ except (ImportError, ValueError, SystemError):
from services import (
AutoFixProcessor,
BatchProcessor,
EnrichmentEngine,
PRReviewEngine,
SplitEngine,
TriageEngine,
)
from services.investigation_label_manager import InvestigationLabelManager
from services.io_utils import safe_print
@@ -191,26 +185,6 @@ class GitHubOrchestrator:
progress_callback=self.progress_callback,
)
self.enrichment_engine = EnrichmentEngine(
project_dir=self.project_dir,
github_dir=self.github_dir,
config=self.config,
progress_callback=self.progress_callback,
)
self.split_engine = SplitEngine(
project_dir=self.project_dir,
github_dir=self.github_dir,
config=self.config,
progress_callback=self.progress_callback,
)
# Read investigation label customization from config
label_customization = self._load_investigation_label_customization()
self.label_manager = InvestigationLabelManager(
customization=label_customization
)
def _report_progress(
self,
phase: str,
@@ -264,9 +238,9 @@ class GitHubOrchestrator:
event=event.lower(),
)
async def _post_issue_comment(self, issue_number: int, body: str) -> int:
"""Post a comment to an issue and return the comment ID."""
return await self.gh_client.issue_comment(issue_number, body)
async def _post_issue_comment(self, issue_number: int, body: str) -> None:
"""Post a comment to an issue."""
await self.gh_client.issue_comment(issue_number, body)
async def _add_issue_labels(self, issue_number: int, labels: list[str]) -> None:
"""Add labels to an issue."""
@@ -309,24 +283,6 @@ class GitHubOrchestrator:
# Helper Methods
# =========================================================================
def _load_investigation_label_customization(self) -> dict | None:
"""Load investigation label customization from GitHub config."""
try:
config_path = self.github_dir / "config.json"
if config_path.exists():
import json
with open(config_path) as f:
config = json.load(f)
investigation_settings = config.get("investigation_settings", {})
return investigation_settings.get("labelCustomization")
except Exception as e:
safe_print(
f"[DEBUG orchestrator] Could not load label customization: {e}",
flush=True,
)
return None
async def _create_skip_result(
self, pr_number: int, skip_reason: str
) -> PRReviewResult:
@@ -439,28 +395,8 @@ class GitHubOrchestrator:
else:
# No existing review found, create skip result
return await self._create_skip_result(pr_number, skip_reason)
elif "Review already in progress" in skip_reason:
# Return an in-progress result WITHOUT saving to disk
# to avoid overwriting the partial result being written by the active review
started_at = self.bot_detector.state.in_progress_reviews.get(
str(pr_number)
)
safe_print(
f"[BOT DETECTION] Review in progress for PR #{pr_number} "
f"(started: {started_at})",
flush=True,
)
return PRReviewResult(
pr_number=pr_number,
repo=self.config.repo,
success=True,
findings=[],
summary="Review in progress",
overall_status="in_progress",
in_progress_since=started_at,
)
else:
# For other skip reasons (bot-authored, cooling off), create a skip result
# For other skip reasons (bot-authored, cooling off, in-progress), create a skip result
return await self._create_skip_result(pr_number, skip_reason)
# Mark review as started (prevents concurrent reviews)
@@ -1533,347 +1469,6 @@ class GitHubOrchestrator:
self._report_progress("complete", 100, f"Triaged {len(results)} issues")
return results
# =========================================================================
# ISSUE INVESTIGATION WORKFLOW
# =========================================================================
async def _run_investigation_with_state_management(
self,
issue_number: int,
issue_title: str,
issue_body: str,
issue_labels: list[str],
issue_comments: list[str],
project_root: Path | None = None,
resume_sessions: dict[str, str] | None = None,
return_dict: bool = True,
):
"""
Core investigation logic with state and label management.
This helper method encapsulates the common pattern of:
1. Saving initial "investigating" state
2. Syncing lifecycle labels
3. Running the investigation
4. Updating state to "findings_ready" or "failed"
5. Syncing final labels
Args:
issue_number: The GitHub issue number
issue_title: The issue title
issue_body: The issue body text
issue_labels: List of label names
issue_comments: List of comment bodies
project_root: Working directory for agents
resume_sessions: Optional dict mapping specialist name to SDK session ID
return_dict: If True, return report.model_dump(); otherwise return report
Returns:
InvestigationReport (as dict if return_dict=True, else as object)
Raises:
Exception: If investigation fails
"""
from datetime import datetime, timezone
try:
from .services.investigation_persistence import (
load_investigation_state,
save_investigation_state,
)
from .services.issue_investigation_orchestrator import (
IssueInvestigationOrchestrator,
)
except (ImportError, ValueError, SystemError):
from services.investigation_persistence import (
load_investigation_state,
save_investigation_state,
)
from services.issue_investigation_orchestrator import (
IssueInvestigationOrchestrator,
)
working_dir = project_root or self.project_dir
# Save initial state, preserving any existing sessions (for resume scenarios)
existing_state = load_investigation_state(self.project_dir, issue_number)
initial_state = {
"issue_number": issue_number,
"status": "investigating",
"started_at": datetime.now(timezone.utc).isoformat(),
"model_used": self.config.model or "sonnet",
}
# Preserve existing sessions if present (resume scenario)
if existing_state and existing_state.sessions:
initial_state["sessions"] = existing_state.sessions
save_investigation_state(
self.project_dir,
issue_number,
initial_state,
)
# Sync lifecycle label to GitHub
try:
await self.label_manager.ensure_labels_exist(self.gh_client)
await self.label_manager.set_investigation_label(
self.gh_client, issue_number, "investigating"
)
except Exception as e:
safe_print(f"[Investigation] Label sync warning: {e}", flush=True)
try:
# Create investigation orchestrator
orchestrator = IssueInvestigationOrchestrator(
project_dir=self.project_dir,
github_dir=self.github_dir,
config=self.config,
progress_callback=self.progress_callback,
)
# Run investigation
report = await orchestrator.investigate(
issue_number=issue_number,
issue_title=issue_title,
issue_body=issue_body,
issue_labels=issue_labels,
issue_comments=issue_comments,
project_root=working_dir,
resume_sessions=resume_sessions,
)
# Update state to findings_ready, preserving started_at and sessions
existing_state = load_investigation_state(self.project_dir, issue_number)
started_at = (
existing_state.started_at
if existing_state
else datetime.now(timezone.utc).isoformat()
)
success_state = {
"issue_number": issue_number,
"status": "findings_ready",
"started_at": started_at,
"completed_at": datetime.now(timezone.utc).isoformat(),
"model_used": self.config.model or "sonnet",
}
# Preserve existing sessions if present (for edge case where user might want to re-run)
if existing_state and existing_state.sessions:
success_state["sessions"] = existing_state.sessions
save_investigation_state(
self.project_dir,
issue_number,
success_state,
)
# Sync lifecycle label to GitHub
try:
await self.label_manager.set_investigation_label(
self.gh_client, issue_number, "findings_ready"
)
except Exception as e:
safe_print(f"[Investigation] Label sync warning: {e}", flush=True)
return report.model_dump(mode="json") if return_dict else report
except Exception as e:
# Update state to failed, preserving any existing sessions for resume support
existing_state = load_investigation_state(self.project_dir, issue_number)
failed_state = {
"issue_number": issue_number,
"status": "failed",
"started_at": (
existing_state.started_at
if existing_state and existing_state.started_at
else datetime.now(timezone.utc).isoformat()
),
"completed_at": datetime.now(timezone.utc).isoformat(),
"error": str(e),
"model_used": self.config.model or "sonnet",
}
# Preserve existing sessions if present (critical for resume feature)
if existing_state and existing_state.sessions:
failed_state["sessions"] = existing_state.sessions
save_investigation_state(
self.project_dir,
issue_number,
failed_state,
)
# Remove lifecycle labels on failure
try:
await self.label_manager.remove_all_investigation_labels(
self.gh_client, issue_number
)
except Exception as label_err:
safe_print(
f"[Investigation] Label cleanup warning: {label_err}",
flush=True,
)
safe_print(
f"[Investigation] Investigation failed for issue #{issue_number}: {e}",
flush=True,
)
raise
async def investigate_issue(
self,
issue_number: int,
project_root: Path | None = None,
resume_sessions: dict[str, str] | None = None,
) -> dict:
"""
Run an AI investigation on a GitHub issue.
Launches 4 specialist agents in parallel (root cause, impact,
fix advisor, reproducer) to explore the codebase and produce
a structured investigation report.
Args:
issue_number: The GitHub issue number to investigate
project_root: Working directory for agents (e.g., worktree path).
Defaults to self.project_dir.
resume_sessions: Optional dict mapping specialist name to SDK
session ID for resuming interrupted investigations.
Returns:
Dict with investigation results (InvestigationReport as dict)
"""
self._report_progress(
"fetching",
5,
f"Fetching issue #{issue_number}...",
issue_number=issue_number,
)
# Fetch issue data
issue = await self._fetch_issue_data(issue_number)
issue_title = issue.get("title", f"Issue #{issue_number}")
issue_body = issue.get("body", "")
issue_labels = [label.get("name", "") for label in issue.get("labels", [])]
# Fetch comments
issue_comments = []
try:
comments_data = await self.gh_client.issue_comments(issue_number)
issue_comments = [c.get("body", "") for c in comments_data if c.get("body")]
except Exception as e:
safe_print(
f"[Investigation] Warning: Could not fetch comments: {e}",
flush=True,
)
return await self._run_investigation_with_state_management(
issue_number=issue_number,
issue_title=issue_title,
issue_body=issue_body,
issue_labels=issue_labels,
issue_comments=issue_comments,
project_root=project_root,
resume_sessions=resume_sessions,
return_dict=True,
)
async def start_investigation(
self,
issue_number: int,
issue_title: str,
issue_body: str,
issue_labels: list[str] | None = None,
issue_comments: list[str] | None = None,
):
"""
Start an AI investigation on a GitHub issue (typed API).
This is a higher-level wrapper that accepts pre-fetched issue data
and returns a typed InvestigationReport.
Args:
issue_number: The GitHub issue number
issue_title: The issue title
issue_body: The issue body text
issue_labels: Optional list of label names
issue_comments: Optional list of comment bodies
Returns:
InvestigationReport from the investigation
"""
return await self._run_investigation_with_state_management(
issue_number=issue_number,
issue_title=issue_title,
issue_body=issue_body,
issue_labels=issue_labels or [],
issue_comments=issue_comments or [],
project_root=self.project_dir,
resume_sessions=None,
return_dict=False,
)
# =========================================================================
# ENRICHMENT WORKFLOW
# =========================================================================
async def enrich_issue(self, issue_number: int) -> dict:
"""
Perform deep AI enrichment on a single issue.
Args:
issue_number: The issue number to enrich
Returns:
Dict matching AIEnrichmentResult interface
"""
self._report_progress(
"fetching",
10,
f"Fetching issue #{issue_number}...",
issue_number=issue_number,
)
issue = await self._fetch_issue_data(issue_number)
result = await self.enrichment_engine.enrich_single_issue(issue)
self._report_progress(
"complete",
100,
"Enrichment complete!",
issue_number=issue_number,
)
return result
# =========================================================================
# SPLIT SUGGESTION WORKFLOW
# =========================================================================
async def split_issue(self, issue_number: int) -> dict:
"""
Analyze an issue and suggest how to split it into sub-issues.
Args:
issue_number: The issue number to analyze
Returns:
Dict matching SplitSuggestion interface
"""
self._report_progress(
"fetching",
10,
f"Fetching issue #{issue_number}...",
issue_number=issue_number,
)
issue = await self._fetch_issue_data(issue_number)
result = await self.split_engine.suggest_split(issue)
self._report_progress(
"complete",
100,
"Split analysis complete!",
issue_number=issue_number,
)
return result
# =========================================================================
# AUTO-FIX WORKFLOW
# =========================================================================
-204
View File
@@ -177,10 +177,6 @@ def get_config(args) -> GitHubRunnerConfig:
)
sys.exit(1)
specialist_config = None
if hasattr(args, "specialist_config") and args.specialist_config:
specialist_config = json.loads(args.specialist_config)
return GitHubRunnerConfig(
token=token,
repo=repo,
@@ -191,7 +187,6 @@ def get_config(args) -> GitHubRunnerConfig:
auto_fix_enabled=getattr(args, "auto_fix_enabled", False),
auto_fix_labels=getattr(args, "auto_fix_labels", ["auto-fix"]),
auto_post_reviews=getattr(args, "auto_post", False),
specialist_config=specialist_config,
)
@@ -240,12 +235,6 @@ async def cmd_review_pr(args) -> int:
safe_print(f"[DEBUG] review_pr returned, success={result.success}")
if result.success:
# For in_progress results (not saved to disk), output JSON so the frontend
# can parse it from stdout instead of relying on the disk file.
if result.overall_status == "in_progress":
safe_print(f"__RESULT_JSON__:{json.dumps(result.to_dict())}")
return 0
safe_print(f"\n{'=' * 60}")
safe_print(f"PR #{result.pr_number} Review Complete")
safe_print(f"{'=' * 60}")
@@ -348,153 +337,6 @@ async def cmd_followup_review_pr(args) -> int:
return 1
async def cmd_investigate(args) -> int:
"""Run AI investigation on a GitHub issue."""
import sys
# Force unbuffered output so Electron sees it in real-time
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(line_buffering=True)
if hasattr(sys.stderr, "reconfigure"):
sys.stderr.reconfigure(line_buffering=True)
config = get_config(args)
# Parse resume sessions if provided
resume_sessions = None
if getattr(args, "resume_sessions", None):
try:
resume_sessions = json.loads(args.resume_sessions)
except json.JSONDecodeError:
safe_print("Warning: Invalid --resume-sessions JSON, starting fresh")
orchestrator = GitHubOrchestrator(
project_dir=args.project,
config=config,
progress_callback=print_progress,
)
try:
result = await orchestrator.investigate_issue(
args.issue_number,
resume_sessions=resume_sessions,
)
# Check if investigation failed (result may contain error info)
# The investigate_issue method raises on failure, but we check result anyway
if not result:
safe_print("Error: Investigation returned no result")
return 1
# Output JSON for the Electron frontend to parse
safe_print("\nJSON Output")
safe_print(f"{'=' * 60}")
safe_print(json.dumps(result, indent=2))
return 0
except Exception as e:
# Return non-zero exit code on investigation failure
safe_print(f"Error: Investigation failed: {e}")
return 1
async def cmd_post_investigation(args) -> int:
"""Post investigation results to GitHub as a comment."""
from services.investigation_persistence import load_investigation_report
from services.investigation_report_builder import build_github_comment
def output_error(error_message: str) -> None:
"""Output an error in JSON format for the Electron frontend to parse."""
safe_print("\nJSON Output")
safe_print(f"{'=' * 60}")
safe_print(json.dumps({"success": False, "error": error_message}))
try:
config = get_config(args)
orchestrator = GitHubOrchestrator(
project_dir=args.project,
config=config,
progress_callback=print_progress,
)
# Load investigation report from .auto-claude/issues/{issueNumber}/investigation_report.json
report = load_investigation_report(args.project, args.issue_number)
if report is None:
error_msg = (
f"No investigation report found for issue #{args.issue_number}. "
f"Please run the investigation first."
)
safe_print(f"Error: {error_msg}")
output_error(error_msg)
return 1
# Build the GitHub comment from the report
comment_body = build_github_comment(report)
# Post it to GitHub
try:
comment_id = await orchestrator._post_issue_comment(
args.issue_number, comment_body
)
except Exception as e:
error_msg = f"Failed to post comment to GitHub: {str(e)}"
safe_print(f"Error: {error_msg}")
output_error(error_msg)
return 1
safe_print(f"Posted investigation results to issue #{args.issue_number}")
# Output JSON for the Electron frontend to parse
safe_print("\nJSON Output")
safe_print(f"{'=' * 60}")
safe_print(json.dumps({"success": True, "commentId": comment_id}))
return 0
except Exception as e:
error_msg = f"Unexpected error: {str(e)}"
safe_print(f"Error: {error_msg}")
output_error(error_msg)
return 1
async def cmd_enrich(args) -> int:
"""Enrich a single issue with deep AI analysis."""
config = get_config(args)
orchestrator = GitHubOrchestrator(
project_dir=args.project,
config=config,
progress_callback=print_progress,
)
result = await orchestrator.enrich_issue(args.issue_number)
# Output JSON for the Electron frontend to parse
safe_print("\nJSON Output")
safe_print(f"{'=' * 60}")
safe_print(json.dumps(result, indent=2))
return 0
async def cmd_split(args) -> int:
"""Suggest how to split an issue into sub-issues."""
config = get_config(args)
orchestrator = GitHubOrchestrator(
project_dir=args.project,
config=config,
progress_callback=print_progress,
)
result = await orchestrator.split_issue(args.issue_number)
# Output JSON for the Electron frontend to parse
safe_print("\nJSON Output")
safe_print(f"{'=' * 60}")
safe_print(json.dumps(result, indent=2))
return 0
async def cmd_triage(args) -> int:
"""Triage issues."""
config = get_config(args)
@@ -879,48 +721,6 @@ def main():
)
followup_parser.add_argument("pr_number", type=int, help="PR number to review")
# investigate command
investigate_parser = subparsers.add_parser(
"investigate", help="Run AI investigation on a GitHub issue"
)
investigate_parser.add_argument(
"issue_number", type=int, help="Issue number to investigate"
)
investigate_parser.add_argument(
"--resume-sessions",
type=str,
default=None,
help="JSON dict of specialist_name:session_id for resuming interrupted investigations",
)
investigate_parser.add_argument(
"--specialist-config",
type=str,
default=None,
help="JSON dict of specialist configs: {name: {model, thinking}}",
)
# post-investigation command
post_investigation_parser = subparsers.add_parser(
"post-investigation", help="Post investigation results to GitHub"
)
post_investigation_parser.add_argument(
"issue_number", type=int, help="Issue number to post results for"
)
# enrich command
enrich_parser = subparsers.add_parser(
"enrich", help="Enrich a single issue with deep AI analysis"
)
enrich_parser.add_argument("issue_number", type=int, help="Issue number to enrich")
# split command
split_parser = subparsers.add_parser(
"split", help="Suggest how to split an issue into sub-issues"
)
split_parser.add_argument(
"issue_number", type=int, help="Issue number to analyze for splitting"
)
# triage command
triage_parser = subparsers.add_parser("triage", help="Triage issues")
triage_parser.add_argument(
@@ -1013,10 +813,6 @@ def main():
commands = {
"review-pr": cmd_review_pr,
"followup-review-pr": cmd_followup_review_pr,
"investigate": cmd_investigate,
"post-investigation": cmd_post_investigation,
"enrich": cmd_enrich,
"split": cmd_split,
"triage": cmd_triage,
"auto-fix": cmd_auto_fix,
"check-auto-fix-labels": cmd_check_labels,
@@ -15,35 +15,9 @@ from __future__ import annotations
_LAZY_IMPORTS: dict[str, tuple[str, str]] = {
"AutoFixProcessor": (".autofix_processor", "AutoFixProcessor"),
"BatchProcessor": (".batch_processor", "BatchProcessor"),
"EnrichmentEngine": (".enrichment_engine", "EnrichmentEngine"),
"InvestigationReport": (".investigation_models", "InvestigationReport"),
"InvestigationState": (".investigation_models", "InvestigationState"),
"InvestigationLabelManager": (
".investigation_label_manager",
"InvestigationLabelManager",
),
"IssueInvestigationOrchestrator": (
".issue_investigation_orchestrator",
"IssueInvestigationOrchestrator",
),
"build_github_comment": (
".investigation_report_builder",
"build_github_comment",
),
"build_summary": (
".investigation_report_builder",
"build_summary",
),
"RootCauseAnalysis": (".investigation_models", "RootCauseAnalysis"),
"ImpactAssessment": (".investigation_models", "ImpactAssessment"),
"FixAdvice": (".investigation_models", "FixAdvice"),
"ReproductionAnalysis": (".investigation_models", "ReproductionAnalysis"),
"ParallelAgentOrchestrator": (".parallel_agent_base", "ParallelAgentOrchestrator"),
"PRReviewEngine": (".pr_review_engine", "PRReviewEngine"),
"PromptManager": (".prompt_manager", "PromptManager"),
"ResponseParser": (".response_parsers", "ResponseParser"),
"SpecialistConfig": (".parallel_agent_base", "SpecialistConfig"),
"SplitEngine": (".split_engine", "SplitEngine"),
"TriageEngine": (".triage_engine", "TriageEngine"),
}
@@ -52,22 +26,8 @@ __all__ = [
"ResponseParser",
"PRReviewEngine",
"TriageEngine",
"EnrichmentEngine",
"SplitEngine",
"AutoFixProcessor",
"BatchProcessor",
"ParallelAgentOrchestrator",
"SpecialistConfig",
"InvestigationLabelManager",
"InvestigationReport",
"InvestigationState",
"IssueInvestigationOrchestrator",
"build_github_comment",
"build_summary",
"RootCauseAnalysis",
"ImpactAssessment",
"FixAdvice",
"ReproductionAnalysis",
]
# Cache for lazily loaded modules
@@ -85,8 +45,3 @@ def __getattr__(name: str) -> object:
_loaded[name] = getattr(module, attr_name)
return _loaded[name]
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
def __dir__() -> list[str]:
"""Expose lazy imports in dir() and for static analysis."""
return list(_LAZY_IMPORTS.keys())
@@ -8,11 +8,8 @@ Handles automatic issue fixing workflow including permissions and state manageme
from __future__ import annotations
import json
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
try:
from ..models import AutoFixState, AutoFixStatus, GitHubRunnerConfig
from ..permissions import GitHubPermissionChecker
@@ -148,14 +145,7 @@ class AutoFixProcessor:
except Exception as e:
if state:
try:
state.update_status(AutoFixStatus.FAILED)
except ValueError:
# Last resort: force-set if transition is invalid
logger.warning(
f"Invalid transition {state.status.value} -> failed, forcing"
)
state.status = AutoFixStatus.FAILED
state.status = AutoFixStatus.FAILED
state.error = str(e)
await state.save(self.github_dir)
raise
@@ -1,52 +0,0 @@
"""
Base Engine Class
=================
Shared functionality for GitHub runner engines (enrichment, split, etc.).
"""
from __future__ import annotations
from pathlib import Path
class EngineBase:
"""Base class for GitHub analysis engines."""
def __init__(
self,
project_dir: Path,
github_dir: Path,
config,
progress_callback=None,
):
self.project_dir = Path(project_dir)
self.github_dir = Path(github_dir)
self.config = config
self.progress_callback = progress_callback
def _report_progress(self, phase: str, progress: int, message: str, **kwargs):
"""Report progress if callback is set.
Args:
phase: Current phase name (e.g., "analyzing", "generating")
progress: Progress percentage (0-100)
message: Human-readable progress message
**kwargs: Additional context data
"""
if self.progress_callback:
import sys
if "orchestrator" in sys.modules:
ProgressCallback = sys.modules["orchestrator"].ProgressCallback
else:
try:
from ..orchestrator import ProgressCallback
except ImportError:
from orchestrator import ProgressCallback
self.progress_callback(
ProgressCallback(
phase=phase, progress=progress, message=message, **kwargs
)
)
@@ -1,223 +0,0 @@
"""
Enrichment Engine
=================
Deep analysis of a single GitHub issue to extract structured enrichment data:
problem statement, goal, scope, acceptance criteria, technical context, and risks.
"""
from __future__ import annotations
import json
import re
from pathlib import Path
try:
from ...phase_config import get_model_betas, resolve_model_id
from ..models import GitHubRunnerConfig
from .engine_base import EngineBase
from .prompt_manager import PromptManager
except (ImportError, ValueError, SystemError):
from models import GitHubRunnerConfig
from phase_config import get_model_betas, resolve_model_id
from services.engine_base import EngineBase
from services.prompt_manager import PromptManager
class EnrichmentEngine(EngineBase):
"""Handles single-issue deep enrichment via AI."""
def __init__(
self,
project_dir: Path,
github_dir: Path,
config: GitHubRunnerConfig,
progress_callback=None,
):
super().__init__(project_dir, github_dir, config, progress_callback)
self.prompt_manager = PromptManager()
async def enrich_single_issue(self, issue: dict) -> dict:
"""
Perform deep AI enrichment on a single issue.
Returns dict matching the frontend AIEnrichmentResult interface:
{
issueNumber, problem, goal, scopeIn, scopeOut,
acceptanceCriteria, technicalContext, risksEdgeCases, confidence
}
"""
from core.client import create_client
issue_number = issue["number"]
self._report_progress(
"analyzing",
20,
f"Analyzing issue #{issue_number}...",
issue_number=issue_number,
)
context = self._build_enrichment_context(issue)
prompt = self._get_enrichment_prompt() + "\n\n---\n\n" + context
model_shorthand = self.config.model or "sonnet"
model = resolve_model_id(model_shorthand)
betas = get_model_betas(model_shorthand)
client = create_client(
project_dir=self.project_dir,
spec_dir=self.github_dir,
model=model,
agent_type="qa_reviewer",
betas=betas,
fast_mode=self.config.fast_mode,
)
try:
async with client:
await client.query(prompt)
response_text = ""
async for msg in client.receive_response():
msg_type = type(msg).__name__
if msg_type == "AssistantMessage" and hasattr(msg, "content"):
for block in msg.content:
block_type = type(block).__name__
if block_type == "TextBlock" and hasattr(block, "text"):
response_text += block.text
self._report_progress(
"generating",
80,
"Parsing enrichment result...",
issue_number=issue_number,
)
return self._parse_enrichment_result(issue_number, response_text)
except Exception as e:
print(f"Enrichment error for #{issue_number}: {e}")
return {
"issueNumber": issue_number,
"problem": "",
"goal": "",
"scopeIn": [],
"scopeOut": [],
"acceptanceCriteria": [],
"technicalContext": "",
"risksEdgeCases": [],
"confidence": 0.0,
}
def _build_enrichment_context(self, issue: dict) -> str:
"""Build context string for enrichment analysis."""
labels = ", ".join(label["name"] for label in issue.get("labels", [])) or "None"
comments_text = ""
comments = issue.get("comments", {})
nodes = comments.get("nodes", []) if isinstance(comments, dict) else []
if nodes:
comment_lines = []
for c in nodes[:10]:
author = c.get("author", {}).get("login", "unknown")
body = c.get("body", "")[:500]
comment_lines.append(f"**{author}:** {body}")
comments_text = "\n".join(comment_lines)
lines = [
f"## Issue #{issue['number']}",
f"**Title:** {issue['title']}",
f"**Author:** {issue.get('author', {}).get('login', 'unknown')}",
f"**State:** {issue.get('state', 'OPEN')}",
f"**Created:** {issue.get('createdAt', 'unknown')}",
f"**Labels:** {labels}",
"",
"### Body",
issue.get("body", "No description provided.") or "No description provided.",
"",
]
if comments_text:
lines.extend(["### Comments", comments_text, ""])
return "\n".join(lines)
def _get_enrichment_prompt(self) -> str:
"""Get the enrichment analysis prompt."""
prompt_file = self.prompt_manager.prompts_dir / "issue_enrichment.md"
if prompt_file.exists():
return prompt_file.read_text(encoding="utf-8")
return self._get_default_enrichment_prompt()
@staticmethod
def _get_default_enrichment_prompt() -> str:
"""Default enrichment prompt."""
return """# Issue Enrichment Agent
You are an issue enrichment assistant. Perform a deep analysis of the GitHub issue
and extract structured information to help developers understand and implement it.
Analyze the issue and produce:
1. **Problem**: A clear, concise statement of the problem or need being described.
2. **Goal**: What the desired outcome is what should be true when the issue is resolved.
3. **Scope In**: A list of things that ARE in scope for this issue.
4. **Scope Out**: A list of things that are explicitly NOT in scope.
5. **Acceptance Criteria**: Specific, testable criteria for considering the issue done.
6. **Technical Context**: Relevant technical details, architecture notes, or constraints.
7. **Risks & Edge Cases**: Potential risks, edge cases, or gotchas to watch out for.
8. **Confidence**: How confident you are in the analysis (0.0 to 1.0). Lower if the issue
is vague, missing details, or contradictory.
Output ONLY a JSON block:
```json
{
"problem": "Clear problem statement",
"goal": "Desired outcome",
"scopeIn": ["Item 1", "Item 2"],
"scopeOut": ["Item 1"],
"acceptanceCriteria": ["Criterion 1", "Criterion 2"],
"technicalContext": "Technical details and constraints",
"risksEdgeCases": ["Risk 1", "Edge case 1"],
"confidence": 0.85
}
```
"""
@staticmethod
def _parse_enrichment_result(issue_number: int, response_text: str) -> dict:
"""Parse enrichment result from AI response."""
default = {
"issueNumber": issue_number,
"problem": "",
"goal": "",
"scopeIn": [],
"scopeOut": [],
"acceptanceCriteria": [],
"technicalContext": "",
"risksEdgeCases": [],
"confidence": 0.0,
}
try:
json_match = re.search(
r"```json\s*(\{.*?\})\s*```", response_text, re.DOTALL
)
if json_match:
data = json.loads(json_match.group(1))
return {
"issueNumber": issue_number,
"problem": data.get("problem", ""),
"goal": data.get("goal", ""),
"scopeIn": data.get("scopeIn", []),
"scopeOut": data.get("scopeOut", []),
"acceptanceCriteria": data.get("acceptanceCriteria", []),
"technicalContext": data.get("technicalContext", ""),
"risksEdgeCases": data.get("risksEdgeCases", []),
"confidence": float(data.get("confidence", 0.5)),
}
except (json.JSONDecodeError, KeyError, ValueError) as e:
print(f"Failed to parse enrichment result: {e}")
return default
@@ -18,6 +18,7 @@ from __future__ import annotations
import hashlib
import logging
import re
from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Any
@@ -25,8 +26,6 @@ if TYPE_CHECKING:
from ..models import FollowupReviewContext, GitHubRunnerConfig
try:
from ...core.client import create_client
from ...phase_config import resolve_model_id
from ..gh_client import GHClient
from ..models import (
MergeVerdict,
@@ -34,16 +33,12 @@ try:
PRReviewResult,
ReviewCategory,
ReviewSeverity,
_utc_now_iso,
)
from .category_utils import map_category
from .io_utils import safe_print
from .prompt_manager import PromptManager
from .pydantic_models import FollowupExtractionResponse, FollowupReviewResponse
from .recovery_utils import create_finding_from_summary
from .sdk_utils import process_sdk_stream
from .pydantic_models import FollowupReviewResponse
except (ImportError, ValueError, SystemError):
from core.client import create_client
from gh_client import GHClient
from models import (
MergeVerdict,
@@ -51,18 +46,11 @@ except (ImportError, ValueError, SystemError):
PRReviewResult,
ReviewCategory,
ReviewSeverity,
_utc_now_iso,
)
from phase_config import resolve_model_id
from services.category_utils import map_category
from services.io_utils import safe_print
from services.prompt_manager import PromptManager
from services.pydantic_models import (
FollowupExtractionResponse,
FollowupReviewResponse,
)
from services.recovery_utils import create_finding_from_summary
from services.sdk_utils import process_sdk_stream
from services.pydantic_models import FollowupReviewResponse
logger = logging.getLogger(__name__)
@@ -277,7 +265,7 @@ class FollowupReviewer:
verdict=verdict,
verdict_reasoning=verdict_reasoning,
blockers=blockers,
reviewed_at=_utc_now_iso(),
reviewed_at=datetime.now().isoformat(),
# Follow-up specific fields
reviewed_commit_sha=context.current_commit_sha,
reviewed_file_blobs=file_blobs,
@@ -709,9 +697,6 @@ Analyze this follow-up review context and provide your structured response.
)
safe_print(f"[Followup] SDK query with output_format, model={model}")
# Capture assistant text for extraction fallback
captured_text = ""
# Iterate through messages from the query
# Note: max_turns=2 because structured output uses a tool call + response
async for message in query(
@@ -736,9 +721,7 @@ Analyze this follow-up review context and provide your structured response.
content = getattr(message, "content", [])
for block in content:
block_type = type(block).__name__
if block_type == "TextBlock":
captured_text += getattr(block, "text", "")
elif block_type == "ToolUseBlock":
if block_type == "ToolUseBlock":
tool_name = getattr(block, "name", "")
if tool_name == "StructuredOutput":
# Extract structured data from tool input
@@ -781,31 +764,9 @@ Analyze this follow-up review context and provide your structured response.
logger.warning(
"Claude could not produce valid structured output after retries"
)
# Attempt extraction call recovery before giving up
if captured_text:
safe_print(
"[Followup] Attempting extraction call recovery...",
flush=True,
)
extraction_result = await self._attempt_extraction_call(
captured_text, context
)
if extraction_result is not None:
return extraction_result
return None
logger.warning("No structured output received from AI")
# Attempt extraction call recovery before giving up
if captured_text:
safe_print(
"[Followup] No structured output — attempting extraction call recovery...",
flush=True,
)
extraction_result = await self._attempt_extraction_call(
captured_text, context
)
if extraction_result is not None:
return extraction_result
return None
except ValueError as e:
@@ -878,124 +839,6 @@ Analyze this follow-up review context and provide your structured response.
"verdict_reasoning": result.verdict_reasoning,
}
async def _attempt_extraction_call(
self,
text: str,
context: FollowupReviewContext,
) -> dict[str, Any] | None:
"""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 (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.
Returns parsed result dict on success, None on failure.
"""
if not text or not text.strip():
return None
try:
extraction_prompt = (
"Extract the key review data from the following AI analysis output. "
"Return the verdict, reasoning, resolved finding IDs, unresolved finding IDs, "
"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 ---"
)
model_shorthand = self.config.model or "sonnet"
model = resolve_model_id(model_shorthand)
extraction_client = create_client(
project_dir=self.project_dir,
spec_dir=self.github_dir,
model=model,
agent_type="pr_followup_extraction",
output_format={
"type": "json_schema",
"schema": FollowupExtractionResponse.model_json_schema(),
},
)
async with extraction_client:
await extraction_client.query(extraction_prompt)
stream_result = await process_sdk_stream(
client=extraction_client,
context_name="FollowupExtraction",
model=model,
system_prompt=extraction_prompt,
max_messages=20,
)
if stream_result.get("error"):
logger.warning(
f"[Followup] Extraction call also failed: {stream_result['error']}"
)
return None
extraction_output = stream_result.get("structured_output")
if not extraction_output:
logger.warning(
"[Followup] Extraction call returned no structured output"
)
return None
extracted = FollowupExtractionResponse.model_validate(extraction_output)
# Convert extraction to internal format with reconstructed findings
new_findings = []
for i, summary_obj in enumerate(extracted.new_finding_summaries):
new_findings.append(
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
# (unresolved findings are handled via finding_resolutions + _apply_ai_resolutions)
finding_resolutions = []
for fid in extracted.resolved_finding_ids:
finding_resolutions.append(
{"finding_id": fid, "status": "resolved", "resolution_notes": None}
)
for fid in extracted.unresolved_finding_ids:
finding_resolutions.append(
{
"finding_id": fid,
"status": "unresolved",
"resolution_notes": None,
}
)
safe_print(
f"[Followup] Extraction recovered: verdict={extracted.verdict}, "
f"{len(extracted.resolved_finding_ids)} resolved, "
f"{len(extracted.unresolved_finding_ids)} unresolved, "
f"{len(new_findings)} new findings",
flush=True,
)
return {
"finding_resolutions": finding_resolutions,
"new_findings": new_findings,
"comment_findings": [],
"verdict": extracted.verdict,
"verdict_reasoning": f"[Recovered via extraction] {extracted.verdict_reasoning}",
}
except Exception as e:
logger.warning(f"[Followup] Extraction call failed: {e}")
return None
def _apply_ai_resolutions(
self,
previous_findings: list[PRReviewFinding],
@@ -1,207 +0,0 @@
"""
Investigation Safety Hooks
===========================
PreToolUse hooks for investigation specialist agents.
investigation_bash_guard() validates Bash commands against a strict
allowlist of read-only, investigation-safe commands. This lets
specialists run git history commands, test runners, and dependency
queries without risking destructive operations.
Commands are validated by:
1. Rejecting dangerous shell operators (;, |, &, `, $(), ${}, redirects)
2. Checking the base command against INVESTIGATION_BASH_ALLOWLIST
3. Blocking dangerous find flags (-exec, -execdir, -delete, -ok, -okdir)
"""
from __future__ import annotations
import json
import logging
import re
import shlex
from datetime import datetime, timezone
from typing import Any
try:
from .io_utils import safe_print
except (ImportError, ValueError, SystemError):
try:
from services.io_utils import safe_print
except (ImportError, ModuleNotFoundError):
from core.io_utils import safe_print
logger = logging.getLogger(__name__)
# Shell operators and constructs that allow command chaining / injection.
_DANGEROUS_PATTERNS = re.compile(
r"[;|&`]"
r"|\$\("
r"|\$\{"
r"|>\s"
r"|<\s"
r"|>>",
)
# find(1) flags that can execute arbitrary commands or delete files.
_DANGEROUS_FIND_FLAGS = {"-exec", "-execdir", "-delete", "-ok", "-okdir"}
# Commands that investigation agents are allowed to run.
INVESTIGATION_BASH_ALLOWLIST: list[str] = [
# Git history (read-only)
"git log",
"git show",
"git blame",
"git diff",
"git status",
# Test runners
"pytest",
"python -m pytest",
"npm test",
"npm run test",
"npx vitest",
"vitest",
"cargo test",
# Dependency inspection
"pip list",
"pip show",
"npm ls",
"node -v",
"node --version",
"python -V",
"python --version",
"python3 --version",
# Filesystem exploration
"ls",
"find",
"wc",
"cat",
"head",
"tail",
"file",
"grep",
"rg",
]
def _is_command_safe(command: str) -> bool:
"""Check if a command is safe for investigation agents.
Validates that:
1. No dangerous shell operators are present (;, |, &, `, $(), redirects)
2. The base command is in the allowlist
3. ``find`` does not use dangerous flags (-exec, -delete, etc.)
"""
# Reject shell operators that enable command chaining / injection
if _DANGEROUS_PATTERNS.search(command):
return False
# Parse into tokens to extract the base command
try:
tokens = shlex.split(command)
except ValueError:
return False
if not tokens:
return False
base_cmd = tokens[0]
# Check if the base command (or full prefix) is in the allowlist
# Both conditions must be true: base command matches AND command starts with allowed prefix
base_cmd_allowed = any(
base_cmd == allowed or base_cmd == allowed.split()[0]
for allowed in INVESTIGATION_BASH_ALLOWLIST
)
full_prefix_allowed = any(
command.startswith(allowed) for allowed in INVESTIGATION_BASH_ALLOWLIST
)
if not (base_cmd_allowed and full_prefix_allowed):
return False
# Extra guard for find: block flags that execute commands or delete files
if base_cmd == "find":
lower_tokens = {t.lower() for t in tokens}
if lower_tokens & _DANGEROUS_FIND_FLAGS:
return False
return True
async def investigation_bash_guard(
input_data: dict[str, Any],
tool_use_id: str | None = None,
context: Any | None = None,
) -> dict[str, Any]:
"""
PreToolUse hook: validate Bash commands for investigation safety.
Allows only commands that start with an entry in
INVESTIGATION_BASH_ALLOWLIST. All other commands are denied.
Args:
input_data: Dict with tool_name and tool_input from SDK
tool_use_id: Tool use ID (unused)
context: Hook context (unused)
Returns:
Empty dict to allow, or hookSpecificOutput with deny decision
"""
tool_input = input_data.get("tool_input")
if not isinstance(tool_input, dict):
return {
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "Bash tool_input is missing or malformed",
}
}
command = tool_input.get("command", "").strip()
if not command:
return {
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": "Empty Bash command",
}
}
# Validate command safety (allowlist + shell-operator rejection)
if _is_command_safe(command):
logger.debug(f"[InvestigationHook] Allowed: {command[:80]}")
return {}
# Deny with reason
logger.info(f"[InvestigationHook] Blocked: {command[:100]}")
return {
"hookSpecificOutput": {
"hookEventName": "PreToolUse",
"permissionDecision": "deny",
"permissionDecisionReason": (
f"Command not allowed during investigation: {command[:100]}"
),
}
}
def emit_json_event(event: str, agent: str, **kwargs: Any) -> None:
"""Emit a structured JSON event to stdout for the frontend to parse.
Args:
event: Event type (tool_start, tool_end, thinking)
agent: Specialist agent name (root_cause, impact, etc.)
**kwargs: Additional event data
"""
try:
payload = {
"event": event,
"agent": agent,
"ts": datetime.now(timezone.utc).isoformat(),
**kwargs,
}
safe_print(json.dumps(payload, default=str))
except Exception:
pass # Never crash a specialist due to event emission failure
@@ -1,245 +0,0 @@
"""
Investigation Label Manager
============================
Manages GitHub lifecycle labels for issue investigations.
Labels are synced one-way (app -> GitHub) with graceful error handling
so label failures never crash the investigation pipeline.
Includes debounce logic (5s) on set_investigation_label() to avoid
rapid-fire GitHub API calls during fast state transitions.
Debounce implementation:
- Non-terminal states are debounced within a 5-second window
- Terminal states (findings_ready, task_created, done) bypass debounce
- Pending state is tracked and applied on the next non-debounced call
"""
from __future__ import annotations
import logging
import time
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from ..gh_client import GHClient
logger = logging.getLogger(__name__)
# Debounce window in seconds for label changes per issue
_LABEL_DEBOUNCE_SECONDS = 5.0
# Default label configuration — can be overridden per-project via config
DEFAULT_LABEL_CUSTOMIZATION = {
"prefix": "auto-claude:",
"labels": {
"investigating": {
"suffix": "investigating",
"color": "1d76db",
"description": "Auto-Claude is investigating this issue",
},
"findings_ready": {
"suffix": "findings-ready",
"color": "0e8a16",
"description": "Investigation complete, findings available",
},
"task_created": {
"suffix": "task-created",
"color": "5319e7",
"description": "Kanban task created from investigation",
},
"building": {
"suffix": "building",
"color": "d93f0b",
"description": "Task is being built by the pipeline",
},
"done": {
"suffix": "done",
"color": "0e8a16",
"description": "Investigation complete, issue resolved",
},
},
}
class InvestigationLabelManager:
"""Manages GitHub lifecycle labels for issue investigations.
Labels are synced one-way (app -> GitHub). Label API failures
are logged but never propagated to callers.
"""
# Terminal states that should never be debounced — these represent
# important outcomes that must always be reflected on GitHub.
_TERMINAL_STATES: set[str] = {"findings_ready", "task_created", "done", "completed"}
def __init__(self, customization: dict | None = None) -> None:
# Resolve customization from config or defaults
custom = customization or {}
prefix = custom.get("prefix", DEFAULT_LABEL_CUSTOMIZATION["prefix"])
label_overrides = custom.get("labels", {})
self.labels: dict[str, dict[str, str]] = {}
for key, defaults in DEFAULT_LABEL_CUSTOMIZATION["labels"].items():
overrides = label_overrides.get(key, {})
suffix = overrides.get("suffix", defaults["suffix"])
color = overrides.get("color", defaults["color"])
description = overrides.get("description", defaults["description"])
self.labels[key] = {
"name": f"{prefix}{suffix}",
"color": color,
"description": description,
}
self.all_label_names = [label["name"] for label in self.labels.values()]
# Track last label-change timestamp per issue for debounce
self._last_label_time: dict[int, float] = {}
# Track pending (debounced) label state per issue for trailing-edge apply
self._pending_state: dict[int, str] = {}
# Map investigation states to label keys
_STATE_MAP: dict[str, str] = {
"investigating": "investigating",
"findings_ready": "findings_ready",
"task_created": "task_created",
"building": "building",
"done": "done",
"completed": "done",
}
async def ensure_labels_exist(self, gh_client: GHClient) -> None:
"""Create labels in the repo if they don't already exist (idempotent).
Called once when an investigation starts. Fetches existing labels
first to avoid 422 errors from attempting to create duplicates.
"""
# Fetch existing labels to avoid noisy 422 errors
existing_names: set[str] = set()
try:
import json
result = await gh_client.run(
["api", "--method", "GET", "repos/{owner}/{repo}/labels", "--paginate"],
raise_on_error=False,
)
if result.returncode == 0:
for label in json.loads(result.stdout):
existing_names.add(label.get("name", ""))
except Exception:
pass # If fetch fails, try creating all labels anyway
for key, label_def in self.labels.items():
if label_def["name"] in existing_names:
continue
try:
await gh_client.run(
[
"api",
"--method",
"POST",
"repos/{owner}/{repo}/labels",
"-f",
f"name={label_def['name']}",
"-f",
f"color={label_def['color']}",
"-f",
f"description={label_def['description']}",
],
raise_on_error=False,
)
except Exception as e:
logger.debug("Label ensure for %s: %s (may already exist)", key, e)
async def set_investigation_label(
self,
gh_client: GHClient,
issue_number: int,
state: str,
) -> None:
"""Set the lifecycle label for an issue, removing old ones first.
Includes a debounce window to avoid rapid-fire GitHub API calls
when state transitions happen in quick succession.
Args:
gh_client: GitHub CLI client
issue_number: The issue number
state: Investigation state (e.g. "investigating", "findings_ready")
"""
label_name = self._state_to_label(state)
if label_name is None:
logger.warning("Unknown investigation state %r, skipping label sync", state)
return
# Resolve which state to apply: if there was a pending (debounced) state
# queued from a previous call, the current call supersedes it.
pending = self._pending_state.pop(issue_number, None)
# Debounce: skip non-terminal states if we changed labels too recently.
# Terminal states (findings_ready, task_created, done) always go through
# because they represent important outcomes that must be visible on GitHub.
now = time.monotonic()
last_time = self._last_label_time.get(issue_number, 0.0)
is_terminal = state in self._TERMINAL_STATES
if not is_terminal and now - last_time < _LABEL_DEBOUNCE_SECONDS:
logger.debug(
"Debounced label change for issue #%d (%.1fs since last change), "
"storing %r as pending",
issue_number,
now - last_time,
state,
)
# Store the desired state so the next non-debounced call applies it
self._pending_state[issue_number] = state
return
# If there was a pending state and the current call supersedes it, log it
if pending and pending != state:
logger.debug(
"Superseding pending label state %r with %r for issue #%d",
pending,
state,
issue_number,
)
try:
# Add the new label first (atomic with existing labels via GitHub's API)
await gh_client.issue_add_labels(issue_number, [label_name])
# Then remove all other auto-claude: labels (excluding the one we just added)
other_labels = [name for name in self.all_label_names if name != label_name]
if other_labels:
await gh_client.issue_remove_labels(issue_number, other_labels)
self._last_label_time[issue_number] = now
logger.info("Set label %s on issue #%d", label_name, issue_number)
except Exception as e:
logger.warning(
"Failed to set label %s on issue #%d: %s",
label_name,
issue_number,
e,
)
async def remove_all_investigation_labels(
self,
gh_client: GHClient,
issue_number: int,
) -> None:
"""Remove all auto-claude: lifecycle labels from an issue."""
try:
await gh_client.issue_remove_labels(issue_number, self.all_label_names)
except Exception as e:
logger.debug(
"Failed to remove investigation labels from #%d: %s",
issue_number,
e,
)
def _state_to_label(self, state: str) -> str | None:
"""Map an investigation state string to a GitHub label name."""
key = self._STATE_MAP.get(state)
if key is None:
return None
return self.labels[key]["name"]
@@ -1,319 +0,0 @@
"""
Investigation Pydantic Models
==============================
Structured output models for the AI issue investigation system.
Each specialist agent (root cause, impact, fix advisor, reproducer) returns
a structured response validated against these schemas. The combined results
form an InvestigationReport.
Usage with Claude Agent SDK structured output:
from .investigation_models import RootCauseAnalysis
client = create_client(
...,
output_format={
"type": "json_schema",
"schema": RootCauseAnalysis.model_json_schema(),
},
)
"""
from __future__ import annotations
from typing import Literal
from pydantic import BaseModel, Field
# =============================================================================
# Shared Sub-Models
# =============================================================================
class CodePath(BaseModel):
"""A reference to a specific code location involved in the issue."""
file: str = Field(description="File path relative to project root")
start_line: int = Field(description="Start line number")
end_line: int | None = Field(
None, description="End line number (None if single line)"
)
description: str = Field(
description="What this code location does / why it matters"
)
class SuggestedLabel(BaseModel):
"""An AI-suggested label for the GitHub issue."""
name: str = Field(description="Label name (e.g., 'bug', 'security', 'performance')")
reason: str = Field(description="Why this label is appropriate for the issue")
accepted: bool | None = Field(
None, description="Whether the user accepted this suggestion (None = pending)"
)
class LinkedPR(BaseModel):
"""A pull request linked to or referenced by the issue."""
number: int = Field(description="PR number")
title: str = Field(description="PR title")
status: Literal["open", "merged", "closed"] = Field(description="PR status")
# =============================================================================
# Root Cause Analyzer
# =============================================================================
class RootCauseAnalysis(BaseModel):
"""Structured output from the Root Cause Analyzer agent."""
identified_root_cause: str = Field(
description="Clear description of the identified root cause"
)
code_paths: list[CodePath] = Field(
default_factory=list,
description="Code paths involved in the issue, ordered from entry point to root cause",
)
confidence: Literal["high", "medium", "low"] = Field(
description="Confidence in the root cause identification"
)
evidence: str = Field(
description="Evidence supporting the root cause identification (code snippets, traces)"
)
related_issues: list[str] = Field(
default_factory=list,
description="Patterns or known issue categories this matches (e.g., 'race condition', 'null reference')",
)
likely_already_fixed: bool = Field(
False,
description="True if evidence suggests this issue has already been resolved",
)
# =============================================================================
# Impact Assessor
# =============================================================================
class AffectedComponent(BaseModel):
"""A component or module affected by the issue."""
file: str = Field(description="File path of the affected component")
component: str = Field(description="Component or module name")
impact_type: str = Field(
description="How this component is affected (e.g. direct, indirect, dependency)"
)
description: str = Field(description="Description of the impact on this component")
class ImpactAssessment(BaseModel):
"""Structured output from the Impact Assessor agent."""
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Overall severity of the issue based on impact"
)
affected_components: list[AffectedComponent] = Field(
default_factory=list,
description="Components/modules affected by this issue",
)
blast_radius: str = Field(
description="Description of how far-reaching the impact is"
)
user_impact: str = Field(description="How end users are affected by this issue")
regression_risk: str = Field(description="Risk of regression if fixing this issue")
# =============================================================================
# Fix Advisor
# =============================================================================
class FixApproach(BaseModel):
"""A concrete approach to fixing the issue."""
description: str = Field(description="Description of the fix approach")
complexity: Literal["simple", "moderate", "complex"] = Field(
description="Estimated complexity of implementing this fix"
)
files_affected: list[str] = Field(
default_factory=list,
description="Files that would need to be modified",
)
pros: list[str] = Field(
default_factory=list,
description="Advantages of this approach",
)
cons: list[str] = Field(
default_factory=list,
description="Disadvantages or risks of this approach",
)
class PatternReference(BaseModel):
"""A reference to an existing codebase pattern to follow."""
file: str = Field(description="File containing the pattern")
description: str = Field(description="What pattern to follow and why")
class FixAdvice(BaseModel):
"""Structured output from the Fix Advisor agent."""
approaches: list[FixApproach] = Field(
default_factory=list,
description="Possible fix approaches, ordered by recommendation",
)
recommended_approach: int = Field(
0,
description="Index into approaches list for the recommended approach",
)
files_to_modify: list[str] = Field(
default_factory=list,
description="All files that need modification across all approaches",
)
patterns_to_follow: list[PatternReference] = Field(
default_factory=list,
description="Existing codebase patterns the fix should follow for consistency",
)
gotchas: list[str] = Field(
default_factory=list,
description="Potential pitfalls and things to watch out for when implementing the fix",
)
# =============================================================================
# Reproducer
# =============================================================================
class TestCoverage(BaseModel):
"""Assessment of existing test coverage for the affected code."""
has_existing_tests: bool = Field(
description="Whether there are existing tests for the affected code"
)
test_files: list[str] = Field(
default_factory=list,
description="Existing test files that cover the affected code paths",
)
coverage_assessment: str = Field(
description="Assessment of how well the affected code is tested"
)
class ReproductionAnalysis(BaseModel):
"""Structured output from the Reproducer agent."""
reproducible: str = Field(
description="Whether the issue can be reproduced (e.g. yes, likely, unlikely, no)"
)
reproduction_steps: list[str] = Field(
default_factory=list,
description="Steps to reproduce the issue",
)
test_coverage: TestCoverage = Field(
description="Assessment of existing test coverage"
)
related_test_files: list[str] = Field(
default_factory=list,
description="Test files related to the affected code",
)
suggested_test_approach: str = Field(
description="How to write a test that verifies the fix"
)
# =============================================================================
# Combined Investigation Report
# =============================================================================
class InvestigationReport(BaseModel):
"""Combined report from all 4 specialist agents.
This is the authoritative investigation result saved to disk and
displayed in the UI. It aggregates all agent outputs plus metadata.
"""
issue_number: int = Field(description="GitHub issue number")
issue_title: str = Field(description="GitHub issue title")
investigation_id: str = Field(description="Unique investigation ID")
timestamp: str = Field(description="ISO 8601 timestamp of investigation completion")
# Agent results
root_cause: RootCauseAnalysis = Field(description="Root cause analysis results")
impact: ImpactAssessment = Field(description="Impact assessment results")
fix_advice: FixAdvice = Field(description="Fix advice results")
reproduction: ReproductionAnalysis = Field(
description="Reproduction analysis results"
)
# Overall assessment
ai_summary: str = Field(
description="AI-generated summary combining all agent findings"
)
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Overall severity computed from agent assessments"
)
likely_resolved: bool = Field(
False,
description="True if evidence suggests the issue has already been resolved",
)
# Metadata
suggested_labels: list[SuggestedLabel] = Field(
default_factory=list,
description="AI-suggested labels for the issue",
)
linked_prs: list[LinkedPR] = Field(
default_factory=list,
description="Pull requests linked to this issue",
)
# =============================================================================
# Investigation State
# =============================================================================
class InvestigationState(BaseModel):
"""Persistent state for an issue investigation.
Saved to .auto-claude/issues/{issueNumber}/investigation_state.json.
State is primarily derived from investigation data + linked task status,
but we persist key fields for fast lookup without scanning all files.
"""
issue_number: int = Field(description="GitHub issue number")
spec_id: str | None = Field(
None, description="Pre-allocated spec ID (e.g., '042-fix-login-bug')"
)
status: Literal[
"investigating",
"findings_ready",
"resolved",
"failed",
"cancelled",
"task_created",
] = Field(description="Current investigation status")
started_at: str = Field(description="ISO 8601 timestamp when investigation started")
completed_at: str | None = Field(
None, description="ISO 8601 timestamp when investigation completed"
)
error: str | None = Field(None, description="Error message if investigation failed")
linked_spec_id: str | None = Field(
None, description="Spec ID of the kanban task created from this investigation"
)
github_comment_id: int | None = Field(
None, description="ID of the GitHub comment posted with results"
)
model_used: str | None = Field(
None, description="Model used for investigation (e.g., 'sonnet')"
)
sessions: dict[str, str | None] = Field(
default_factory=dict,
description="SDK session IDs per specialist for resume support. Keys are specialist names, values are session IDs or None.",
)
@@ -1,425 +0,0 @@
"""
Investigation Persistence Layer
================================
Read/write operations for .auto-claude/issues/{issueNumber}/ directory.
All writes use write_json_atomic() to prevent corruption from concurrent
access or crashes. Directory structure:
.auto-claude/issues/
{issueNumber}/
investigation_report.json # Full findings from all 4 agents
investigation_state.json # Status, timestamps, linked spec ID
agent_logs/ # Per-agent log files
root_cause.log
impact.log
fix_advisor.log
reproducer.log
suggested_labels.json # AI-suggested labels with accept/reject
"""
from __future__ import annotations
import json
import logging
from pathlib import Path
from typing import Any
try:
from ...core.file_utils import atomic_write, write_json_atomic
from .investigation_models import InvestigationReport, InvestigationState
except (ImportError, ValueError, SystemError):
from core.file_utils import atomic_write, write_json_atomic
try:
from services.investigation_models import (
InvestigationReport,
InvestigationState,
)
except (ImportError, ModuleNotFoundError):
from investigation_models import InvestigationReport, InvestigationState
logger = logging.getLogger(__name__)
def get_issues_dir(project_dir: Path) -> Path:
"""Get the base issues directory, creating it if needed."""
d = project_dir / ".auto-claude" / "issues"
d.mkdir(parents=True, exist_ok=True)
return d
def get_issue_dir(project_dir: Path, issue_number: int) -> Path:
"""Get the directory for a specific issue, creating it if needed."""
d = get_issues_dir(project_dir) / str(issue_number)
d.mkdir(parents=True, exist_ok=True)
return d
# =============================================================================
# Investigation State
# =============================================================================
def save_investigation_state(
project_dir: Path,
issue_number: int,
state: InvestigationState | dict[str, Any],
) -> Path:
"""Save investigation state to disk.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
state: Investigation state (Pydantic model or raw dict)
Returns:
Path to the saved state file
"""
issue_path = get_issue_dir(project_dir, issue_number)
state_file = issue_path / "investigation_state.json"
data = (
state.model_dump(mode="json")
if isinstance(state, InvestigationState)
else state
)
write_json_atomic(state_file, data)
status = (
state.status
if isinstance(state, InvestigationState)
else state.get("status", "?")
)
logger.debug(f"Saved investigation state for issue #{issue_number}: {status}")
return state_file
def load_investigation_state(
project_dir: Path,
issue_number: int,
) -> InvestigationState | None:
"""Load investigation state from disk.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
Returns:
InvestigationState if found, None otherwise
"""
state_file = get_issue_dir(project_dir, issue_number) / "investigation_state.json"
if not state_file.exists():
return None
try:
data = json.loads(state_file.read_text(encoding="utf-8"))
return InvestigationState.model_validate(data)
except Exception as e:
logger.error(
f"Failed to load investigation state for issue #{issue_number}: {e}"
)
return None
# =============================================================================
# Investigation Report
# =============================================================================
def save_investigation_report(
project_dir: Path,
issue_number: int,
report: InvestigationReport,
) -> Path:
"""Save investigation report to disk.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
report: Investigation report to save
Returns:
Path to the saved report file
"""
issue_path = get_issue_dir(project_dir, issue_number)
report_file = issue_path / "investigation_report.json"
write_json_atomic(report_file, report.model_dump(mode="json"))
logger.debug(f"Saved investigation report for issue #{issue_number}")
return report_file
def load_investigation_report(
project_dir: Path,
issue_number: int,
) -> InvestigationReport | None:
"""Load investigation report from disk.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
Returns:
InvestigationReport if found, None otherwise
"""
report_file = get_issue_dir(project_dir, issue_number) / "investigation_report.json"
if not report_file.exists():
return None
try:
data = json.loads(report_file.read_text(encoding="utf-8"))
return InvestigationReport.model_validate(data)
except Exception as e:
logger.error(
f"Failed to load investigation report for issue #{issue_number}: {e}"
)
return None
# =============================================================================
# Agent Logs
# =============================================================================
def save_agent_log(
project_dir: Path,
issue_number: int,
agent_name: str,
log_content: str,
) -> Path:
"""Save an agent's log output to disk (atomic write).
Args:
project_dir: Project root directory
issue_number: GitHub issue number
agent_name: Name of the agent (e.g., 'root_cause', 'impact')
log_content: Log text to save
Returns:
Path to the saved log file
"""
logs_dir = get_issue_dir(project_dir, issue_number) / "agent_logs"
logs_dir.mkdir(parents=True, exist_ok=True)
log_file = logs_dir / f"{agent_name}.log"
# Use atomic write to prevent corruption from concurrent access
with atomic_write(log_file, "w", encoding="utf-8") as f:
f.write(log_content)
logger.debug(f"Saved agent log for issue #{issue_number}/{agent_name}")
return log_file
# =============================================================================
# GitHub Comment Tracking
# =============================================================================
def save_github_comment_id(
project_dir: Path,
issue_number: int,
comment_id: int,
) -> None:
"""Save the GitHub comment ID for the posted investigation results (atomic write).
Also updates the investigation state with the comment ID.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
comment_id: GitHub comment ID
"""
# Save to dedicated file for quick lookup (atomic write)
issue_path = get_issue_dir(project_dir, issue_number)
comment_file = issue_path / "github_comment_id"
# Use atomic write to prevent corruption from concurrent access
with atomic_write(comment_file, "w", encoding="utf-8") as f:
f.write(str(comment_id))
# Also update state if it exists
state = load_investigation_state(project_dir, issue_number)
if state:
state.github_comment_id = comment_id
save_investigation_state(project_dir, issue_number, state)
logger.debug(f"Saved GitHub comment ID {comment_id} for issue #{issue_number}")
def load_github_comment_id(
project_dir: Path,
issue_number: int,
) -> int | None:
"""Load the GitHub comment ID for posted investigation results.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
Returns:
Comment ID if found, None otherwise
"""
comment_file = get_issue_dir(project_dir, issue_number) / "github_comment_id"
if not comment_file.exists():
return None
try:
return int(comment_file.read_text(encoding="utf-8").strip())
except (ValueError, OSError) as e:
logger.warning(
f"Failed to load GitHub comment ID for issue #{issue_number}: {e}"
)
return None
# =============================================================================
# Suggested Labels
# =============================================================================
def save_suggested_labels(
project_dir: Path,
issue_number: int,
labels: list[dict[str, Any]],
) -> None:
"""Save AI-suggested labels for the issue.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
labels: List of label dicts with name, reason, confidence, accepted status
"""
labels_file = get_issue_dir(project_dir, issue_number) / "suggested_labels.json"
write_json_atomic(labels_file, labels)
logger.debug(f"Saved {len(labels)} suggested labels for issue #{issue_number}")
def load_suggested_labels(
project_dir: Path,
issue_number: int,
) -> list[dict[str, Any]]:
"""Load AI-suggested labels for the issue.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
Returns:
List of label dicts, empty list if not found
"""
labels_file = get_issue_dir(project_dir, issue_number) / "suggested_labels.json"
if not labels_file.exists():
return []
try:
data = json.loads(labels_file.read_text(encoding="utf-8"))
return data if isinstance(data, list) else []
except Exception as e:
logger.warning(
f"Failed to load suggested labels for issue #{issue_number}: {e}"
)
return []
# =============================================================================
# Session Persistence (Resume Support)
# =============================================================================
def save_specialist_session(
project_dir: Path,
issue_number: int,
specialist_name: str,
session_id: str,
) -> None:
"""Save a specialist's SDK session ID for resume support.
Updates the sessions dict in investigation_state.json using atomic
write to prevent file corruption.
Note: While the write itself is atomic, there remains a theoretical
TOCTOU (time-of-check-time-of-use) race between read and write if
multiple processes update the same issue state simultaneously. In
practice, investigations are single-process per issue, so this is
acceptable for the low-severity nature of session tracking.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
specialist_name: Specialist name (root_cause, impact, etc.)
session_id: SDK session ID
"""
state = load_investigation_state(project_dir, issue_number)
if state is None:
logger.warning(
f"Cannot save session for issue #{issue_number}: no investigation state"
)
return
state.sessions[specialist_name] = session_id
save_investigation_state(project_dir, issue_number, state)
logger.debug(
f"Saved session ID for issue #{issue_number}/{specialist_name}: {session_id[:20]}..."
)
def load_specialist_sessions(
project_dir: Path,
issue_number: int,
) -> dict[str, str | None]:
"""Load all specialist session IDs for an investigation.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
Returns:
Dict mapping specialist name to session ID (or None)
"""
state = load_investigation_state(project_dir, issue_number)
if state is None:
return {}
return state.sessions
# =============================================================================
# Listing & Querying
# =============================================================================
def list_investigated_issues(
project_dir: Path,
) -> list[int]:
"""List all issue numbers that have investigation data.
Args:
project_dir: Project root directory
Returns:
Sorted list of issue numbers
"""
issues_dir = project_dir / ".auto-claude" / "issues"
if not issues_dir.exists():
return []
issue_numbers = []
for entry in issues_dir.iterdir():
if entry.is_dir():
try:
issue_numbers.append(int(entry.name))
except ValueError:
continue
return sorted(issue_numbers)
def has_investigation(
project_dir: Path,
issue_number: int,
) -> bool:
"""Check if an issue has any investigation data.
Args:
project_dir: Project root directory
issue_number: GitHub issue number
Returns:
True if investigation data exists
"""
return (project_dir / ".auto-claude" / "issues" / str(issue_number)).exists()
@@ -1,199 +0,0 @@
"""
Investigation Report Builder
==============================
Transforms an InvestigationReport into formatted output:
- build_github_comment(): Branded markdown for posting to GitHub issues
- build_summary(): One-paragraph summary for list display
"""
from __future__ import annotations
from datetime import datetime, timezone
try:
from .investigation_models import InvestigationReport
except (ImportError, ValueError, SystemError):
try:
from services.investigation_models import InvestigationReport
except (ImportError, ModuleNotFoundError):
from investigation_models import InvestigationReport
def build_github_comment(report: InvestigationReport) -> str:
"""Produce branded markdown for a GitHub issue comment.
Args:
report: The investigation report to format
Returns:
Markdown string suitable for posting as a GitHub comment
"""
lines: list[str] = []
# Header
lines.append("## Auto-Claude Investigation")
lines.append("")
confidence = report.root_cause.confidence
lines.append(f"**Severity:** {report.severity} | **Confidence:** {confidence}")
lines.append("")
# Summary
lines.append("### Summary")
lines.append(report.ai_summary)
lines.append("")
# Already-resolved warning
if report.likely_resolved:
lines.append(
"> **Note:** Evidence suggests this issue may have already been resolved."
)
lines.append("")
# Root Cause Analysis (collapsible)
lines.append("<details>")
lines.append("<summary>Root Cause Analysis</summary>")
lines.append("")
lines.append(f"**Root Cause:** {report.root_cause.identified_root_cause}")
lines.append("")
if report.root_cause.code_paths:
lines.append("**Code Paths:**")
lines.append("")
lines.append("| File | Lines | Description |")
lines.append("|------|-------|-------------|")
for cp in report.root_cause.code_paths:
end = cp.end_line if cp.end_line else cp.start_line
lines.append(f"| `{cp.file}` | {cp.start_line}-{end} | {cp.description} |")
lines.append("")
lines.append(f"**Evidence:** {report.root_cause.evidence}")
lines.append("")
lines.append("</details>")
lines.append("")
# Impact Assessment (collapsible)
lines.append("<details>")
lines.append("<summary>Impact Assessment</summary>")
lines.append("")
lines.append(f"**Severity:** {report.impact.severity}")
lines.append(f"**Blast Radius:** {report.impact.blast_radius}")
lines.append(f"**User Impact:** {report.impact.user_impact}")
lines.append(f"**Regression Risk:** {report.impact.regression_risk}")
lines.append("")
if report.impact.affected_components:
lines.append("**Affected Components:**")
lines.append("")
lines.append("| Component | File | Type | Description |")
lines.append("|-----------|------|------|-------------|")
for ac in report.impact.affected_components:
lines.append(
f"| {ac.component} | `{ac.file}` | {ac.impact_type} | {ac.description} |"
)
lines.append("")
lines.append("</details>")
lines.append("")
# Fix Recommendations (collapsible)
lines.append("<details>")
lines.append("<summary>Fix Recommendations</summary>")
lines.append("")
for i, approach in enumerate(report.fix_advice.approaches):
recommended = (
" **(recommended)**" if i == report.fix_advice.recommended_approach else ""
)
lines.append(f"**Approach {i + 1}:** {approach.description}{recommended}")
lines.append(f"- Complexity: {approach.complexity}")
if approach.pros:
lines.append(f"- Pros: {', '.join(approach.pros)}")
if approach.cons:
lines.append(f"- Cons: {', '.join(approach.cons)}")
lines.append("")
if report.fix_advice.files_to_modify:
lines.append(
"**Files to Modify:** "
+ ", ".join(f"`{f}`" for f in report.fix_advice.files_to_modify)
)
lines.append("")
if report.fix_advice.gotchas:
lines.append("**Gotchas:**")
for gotcha in report.fix_advice.gotchas:
lines.append(f"- {gotcha}")
lines.append("")
lines.append("</details>")
lines.append("")
# Reproduction & Testing (collapsible)
lines.append("<details>")
lines.append("<summary>Reproduction & Testing</summary>")
lines.append("")
lines.append(f"**Reproducible:** {report.reproduction.reproducible}")
lines.append("")
if report.reproduction.reproduction_steps:
lines.append("**Steps:**")
for j, step in enumerate(report.reproduction.reproduction_steps, 1):
lines.append(f"{j}. {step}")
lines.append("")
lines.append(
f"**Test Coverage:** {report.reproduction.test_coverage.coverage_assessment}"
)
lines.append(
f"**Suggested Test Approach:** {report.reproduction.suggested_test_approach}"
)
lines.append("")
lines.append("</details>")
lines.append("")
# Suggested Labels
if report.suggested_labels:
lines.append("### Suggested Labels")
for label in report.suggested_labels:
lines.append(f"- `{label.name}` - {label.reason}")
lines.append("")
# Footer
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M UTC")
lines.append("---")
lines.append(
f"*Generated by [Auto-Claude](https://github.com/AndyMik90/Auto-Claude) "
f"* {timestamp}*"
)
return "\n".join(lines)
def build_summary(report: InvestigationReport) -> str:
"""Build a one-paragraph summary for list display (max ~200 chars).
Args:
report: The investigation report to summarize
Returns:
A short summary string
"""
severity = report.severity.upper()
confidence = report.root_cause.confidence
cause = report.root_cause.identified_root_cause
summary = f"[{severity}] {cause}"
# Add resolution note if relevant
if report.likely_resolved:
summary += " (likely resolved)"
# Add confidence
summary += f" [{confidence} confidence]"
# Truncate to ~200 chars
if len(summary) > 200:
summary = summary[:197] + "..."
return summary
@@ -1,225 +0,0 @@
"""
Investigation Spec Generator
==============================
Template-based spec generation from investigation reports.
NO AI cost - uses string templates to transform investigation data
into spec.md and requirements.json for the build pipeline.
Usage:
spec_path = generate_spec_from_investigation(
project_dir=Path("/project"),
issue_number=42,
spec_id="042-fix-login-bug",
)
"""
from __future__ import annotations
import logging
from pathlib import Path
try:
from ...core.file_utils import write_json_atomic
except (ImportError, ValueError, SystemError):
from core.file_utils import write_json_atomic
from .investigation_models import InvestigationReport
from .investigation_persistence import load_investigation_report
logger = logging.getLogger(__name__)
def generate_spec_from_investigation(
project_dir: Path,
issue_number: int,
spec_id: str,
) -> Path:
"""Generate a spec directory from an investigation report.
This is a template-based transformation (no AI cost). It reads
the investigation report and produces:
- spec.md: Markdown spec from the AI summary, root cause, and fix advice
- requirements.json: Requirements derived from fix approaches
- investigation_report.json: Copy of the original report for reference
Args:
project_dir: Project root directory
issue_number: GitHub issue number
spec_id: Spec identifier (e.g., '042-fix-login-bug')
Returns:
Path to the created spec directory
Raises:
FileNotFoundError: If no investigation report exists for the issue
"""
# Load investigation report
report = load_investigation_report(project_dir, issue_number)
if report is None:
raise FileNotFoundError(
f"No investigation report found for issue #{issue_number}. "
"Run investigation first."
)
# Create spec directory
spec_dir = project_dir / ".auto-claude" / "specs" / spec_id
spec_dir.mkdir(parents=True, exist_ok=True)
# Generate spec.md
spec_md = _build_spec_md(report)
(spec_dir / "spec.md").write_text(spec_md, encoding="utf-8")
# Generate requirements.json
requirements = _build_requirements(report)
write_json_atomic(spec_dir / "requirements.json", requirements)
# Copy investigation report for reference
report_data = report.model_dump(mode="json")
write_json_atomic(spec_dir / "investigation_report.json", report_data)
logger.info(
f"Generated spec '{spec_id}' from investigation of issue #{issue_number}"
)
return spec_dir
def _build_spec_md(report: InvestigationReport) -> str:
"""Build spec.md content from investigation report.
Args:
report: The investigation report
Returns:
Markdown string for spec.md
"""
lines: list[str] = []
# Title
lines.append(f"# Fix: {report.issue_title}")
lines.append("")
lines.append(
f"> Generated from investigation of GitHub issue #{report.issue_number}"
)
lines.append("")
# Summary
lines.append("## Summary")
lines.append("")
lines.append(report.ai_summary)
lines.append("")
# Root Cause
lines.append("## Root Cause")
lines.append("")
lines.append(report.root_cause.identified_root_cause)
lines.append("")
if report.root_cause.code_paths:
lines.append("### Affected Code Paths")
lines.append("")
for cp in report.root_cause.code_paths:
end = cp.end_line if cp.end_line else cp.start_line
lines.append(f"- `{cp.file}:{cp.start_line}-{end}` - {cp.description}")
lines.append("")
# Fix Approach
lines.append("## Implementation Plan")
lines.append("")
if report.fix_advice.approaches:
rec_idx = report.fix_advice.recommended_approach
if 0 <= rec_idx < len(report.fix_advice.approaches):
approach = report.fix_advice.approaches[rec_idx]
lines.append(f"**Recommended approach:** {approach.description}")
lines.append(f"- Complexity: {approach.complexity}")
lines.append("")
if approach.files_affected:
lines.append("**Files to modify:**")
for f in approach.files_affected:
lines.append(f"- `{f}`")
lines.append("")
# Patterns to follow
if report.fix_advice.patterns_to_follow:
lines.append("### Patterns to Follow")
lines.append("")
for pat in report.fix_advice.patterns_to_follow:
lines.append(f"- `{pat.file}`: {pat.description}")
lines.append("")
# Gotchas
if report.fix_advice.gotchas:
lines.append("### Gotchas")
lines.append("")
for gotcha in report.fix_advice.gotchas:
lines.append(f"- {gotcha}")
lines.append("")
# Testing
lines.append("## Testing")
lines.append("")
lines.append(
f"**Suggested approach:** {report.reproduction.suggested_test_approach}"
)
lines.append("")
if report.reproduction.test_coverage.test_files:
lines.append("**Existing test files:**")
for tf in report.reproduction.test_coverage.test_files:
lines.append(f"- `{tf}`")
lines.append("")
# Impact
lines.append("## Impact")
lines.append("")
lines.append(f"- **Severity:** {report.impact.severity}")
lines.append(f"- **Blast radius:** {report.impact.blast_radius}")
lines.append(f"- **User impact:** {report.impact.user_impact}")
lines.append(f"- **Regression risk:** {report.impact.regression_risk}")
lines.append("")
return "\n".join(lines)
def _build_requirements(report: InvestigationReport) -> dict:
"""Build requirements.json from investigation report.
Args:
report: The investigation report
Returns:
Dict structure for requirements.json
"""
requirements: list[dict] = []
# Generate requirements from fix approaches
for i, approach in enumerate(report.fix_advice.approaches):
is_recommended = i == report.fix_advice.recommended_approach
req = {
"id": f"REQ-{i + 1:03d}",
"description": approach.description,
"priority": "must" if is_recommended else "should",
"complexity": approach.complexity,
"files": approach.files_affected,
}
requirements.append(req)
# Add testing requirement
requirements.append(
{
"id": f"REQ-{len(requirements) + 1:03d}",
"description": f"Add tests: {report.reproduction.suggested_test_approach}",
"priority": "must",
"complexity": "moderate",
"files": report.reproduction.related_test_files,
}
)
return {
"issue_number": report.issue_number,
"issue_title": report.issue_title,
"severity": report.severity,
"requirements": requirements,
}
File diff suppressed because it is too large Load Diff
@@ -1,417 +0,0 @@
"""
Parallel Agent Orchestrator Base
=================================
Abstract base class for parallel specialist agent orchestration.
Extracts shared patterns from the PR review parallel orchestrator so that
both PR review and issue investigation can reuse:
- SpecialistConfig dataclass for agent definition
- Prompt loading from prompts/github/ directory
- SDK session creation and stream processing
- asyncio.gather-based parallel execution
- Progress reporting via callback
Subclasses must implement domain-specific logic (prompt building, result
parsing, verdict generation, etc.).
"""
from __future__ import annotations
import asyncio
import logging
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Any
try:
from ...core.client import create_client
from ...phase_config import (
get_model_betas,
get_thinking_kwargs_for_model,
)
from .io_utils import safe_print
from .sdk_utils import process_sdk_stream
except (ImportError, ValueError, SystemError):
from core.client import create_client
from phase_config import (
get_model_betas,
get_thinking_kwargs_for_model,
)
from services.io_utils import safe_print
from services.sdk_utils import process_sdk_stream
logger = logging.getLogger(__name__)
# Import investigation Bash safety hook (lazy - only used when Bash is in tools)
_investigation_bash_guard = None
def _get_investigation_bash_guard():
"""Lazy-import the investigation Bash guard to avoid circular imports."""
global _investigation_bash_guard
if _investigation_bash_guard is None:
try:
from .investigation_hooks import investigation_bash_guard
_investigation_bash_guard = investigation_bash_guard
except (ImportError, ValueError, SystemError):
try:
from services.investigation_hooks import investigation_bash_guard
_investigation_bash_guard = investigation_bash_guard
except (ImportError, ModuleNotFoundError):
from investigation_hooks import investigation_bash_guard
_investigation_bash_guard = investigation_bash_guard
return _investigation_bash_guard
# Check if debug mode is enabled
DEBUG_MODE = os.environ.get("DEBUG", "").lower() in ("true", "1", "yes")
@dataclass
class SpecialistConfig:
"""Configuration for a specialist agent in parallel SDK sessions."""
name: str
prompt_file: str
tools: list[str]
description: str
max_turns: int = 30
class ParallelAgentOrchestrator:
"""
Base class for parallel specialist agent orchestration.
Provides shared infrastructure for running multiple Claude SDK sessions
in parallel via asyncio.gather(). Subclasses define their own specialist
configurations, prompt building, and result parsing.
Shared capabilities:
- Load prompt files from prompts/github/ directory
- Run individual specialist SDK sessions with structured output
- Run multiple specialists in parallel and collect results
- Report progress via callback
"""
def __init__(
self,
project_dir: Path,
github_dir: Path,
config: Any,
progress_callback: Any = None,
):
self.project_dir = Path(project_dir)
self.github_dir = Path(github_dir)
self.config = config
self.progress_callback = progress_callback
def _report_progress(self, phase: str, progress: int, message: str, **kwargs):
"""Report progress if callback is set."""
if self.progress_callback:
import sys
if "orchestrator" in sys.modules:
ProgressCallback = sys.modules["orchestrator"].ProgressCallback
else:
try:
from ..orchestrator import ProgressCallback
except ImportError:
from orchestrator import ProgressCallback
self.progress_callback(
ProgressCallback(
phase=phase, progress=progress, message=message, **kwargs
)
)
def _load_prompt(self, filename: str) -> str:
"""Load a prompt file from the prompts/github directory."""
prompt_file = (
Path(__file__).parent.parent.parent.parent / "prompts" / "github" / filename
)
if prompt_file.exists():
return prompt_file.read_text(encoding="utf-8")
logger.warning(f"Prompt file not found: {prompt_file}")
return ""
async def _run_specialist_session(
self,
config: SpecialistConfig,
prompt: str,
project_root: Path,
model: str,
thinking_budget: int | None,
output_schema: dict[str, Any] | None = None,
agent_type: str = "pr_reviewer",
context_name: str | None = None,
max_messages: int | None = None,
on_thinking: Any | None = None,
on_tool_use: Any | None = None,
on_tool_result: Any | None = None,
resume_session_id: str | None = None,
thinking_level: str | None = None,
effort_level: str | None = None,
) -> dict[str, Any]:
"""Run a single specialist as its own SDK session.
This is the generic version that accepts a pre-built prompt and
output schema. Subclasses build domain-specific prompts and parse
results from the returned dict.
Args:
config: Specialist configuration
prompt: Full system prompt (already built by subclass)
project_root: Working directory for the agent
model: Model to use
thinking_budget: Max thinking tokens
output_schema: JSON schema dict for structured output (optional)
agent_type: Agent type for create_client (e.g., "pr_reviewer",
"investigation_specialist")
context_name: Name for logging (defaults to "Specialist:{config.name}")
max_messages: Optional max message count for stream processing
Returns:
Dict with keys from process_sdk_stream:
- result_text: Raw text output
- structured_output: Parsed structured output (if schema provided)
- error: Error message (if any)
- msg_count: Total message count
"""
log_name = context_name or f"Specialist:{config.name}"
safe_print(
f"[{log_name}] Starting analysis...",
flush=True,
)
try:
# Create SDK client for this specialist
# Use per-specialist model for betas (not the global config model)
betas = get_model_betas(model or self.config.model or "sonnet")
# Get thinking budget - use explicit budget if provided, otherwise derive from thinking level
if thinking_budget is not None:
thinking_kwargs = {"max_thinking_tokens": thinking_budget}
else:
# Use per-specialist thinking level when provided
effective_thinking = (
thinking_level or self.config.thinking_level or "medium"
)
thinking_kwargs = get_thinking_kwargs_for_model(
model, effective_thinking
)
# Override effort_level if explicitly provided (e.g., investigation
# agents always use "high" effort regardless of thinking level).
# Only applies to adaptive models (Opus 4.6+) where thinking_kwargs
# includes effort_level; non-adaptive models silently skip this.
if effort_level and "effort_level" in thinking_kwargs:
thinking_kwargs["effort_level"] = effort_level
client_kwargs: dict[str, Any] = {
"project_dir": project_root,
"spec_dir": self.github_dir,
"model": model,
"agent_type": agent_type,
"betas": betas,
"fast_mode": self.config.fast_mode,
**thinking_kwargs,
}
if output_schema:
client_kwargs["output_format"] = {
"type": "json_schema",
"schema": output_schema,
}
client = create_client(**client_kwargs)
# Resume previous session if session ID provided
if resume_session_id:
client.options.resume = resume_session_id
safe_print(
f"[{log_name}] Resuming session: {resume_session_id[:20]}..."
)
# Add investigation Bash safety hook if agent has Bash access
if "Bash" in config.tools:
try:
from claude_agent_sdk.types import HookMatcher
bash_guard = _get_investigation_bash_guard()
existing_hooks = client.options.hooks or {}
pre_tool_hooks = existing_hooks.get("PreToolUse", [])
pre_tool_hooks.append(
HookMatcher(matcher="Bash", hooks=[bash_guard])
)
existing_hooks["PreToolUse"] = pre_tool_hooks
client.options.hooks = existing_hooks
except ImportError:
logger.warning(
f"[{log_name}] Could not import HookMatcher — "
"Bash access will be unguarded"
)
async with client:
await client.query(prompt)
# Capture session ID for resume support
session_id = getattr(client, "session_id", None)
# Build stream kwargs
stream_kwargs: dict[str, Any] = {
"client": client,
"context_name": log_name,
"model": model,
"system_prompt": prompt,
"agent_definitions": {},
}
if max_messages is not None:
stream_kwargs["max_messages"] = max_messages
if on_thinking is not None:
stream_kwargs["on_thinking"] = on_thinking
if on_tool_use is not None:
stream_kwargs["on_tool_use"] = on_tool_use
if on_tool_result is not None:
stream_kwargs["on_tool_result"] = on_tool_result
stream_result = await process_sdk_stream(**stream_kwargs)
error = stream_result.get("error")
if error:
logger.error(f"[{log_name}] SDK stream failed: {error}")
safe_print(
f"[{log_name}] Analysis failed: {error}",
flush=True,
)
return {**stream_result, "session_id": session_id}
except Exception as e:
logger.error(
f"[{log_name}] Session failed: {e}",
exc_info=True,
)
safe_print(
f"[{log_name}] Error: {e}",
flush=True,
)
return {
"result_text": "",
"structured_output": None,
"error": str(e),
"msg_count": 0,
}
async def _run_parallel_specialists(
self,
tasks: list[asyncio.Task | Any],
orchestrator_name: str = "ParallelOrchestrator",
retry_tasks: list[Any] | None = None,
retry_configs: list[dict[str, Any]] | None = None,
) -> list[Any]:
"""Run pre-built async tasks in parallel and collect results.
Failed specialists are retried once before being discarded.
If retry_tasks is provided, it should be a list of callables
(0-arg coroutine factories) that can recreate the coroutine
for a retry attempt.
Results preserve positional order: result[i] corresponds to tasks[i].
Failed specialists (after retry) are represented as None in the list.
Args:
tasks: List of coroutines/tasks to run in parallel
orchestrator_name: Name for logging
retry_tasks: Optional list of 0-arg callables that recreate
the coroutine for each task (same order as tasks).
If None, failed tasks are not retried.
retry_configs: Optional list of dicts with retry configuration:
- name: Specialist name for logging
- lifecycle_wrapper: Optional callable that wraps a coroutine
with lifecycle events (agent_started, agent_done)
Returns:
List of results preserving original task order.
Failed tasks are None; successful tasks contain the result dict.
"""
safe_print(
f"[{orchestrator_name}] Launching {len(tasks)} specialists in parallel...",
flush=True,
)
results = await asyncio.gather(*tasks, return_exceptions=True)
# Build position-indexed result map
result_map: dict[int, Any] = {}
failed_indices: list[int] = []
for i, result in enumerate(results):
if isinstance(result, Exception):
logger.error(f"[{orchestrator_name}] Specialist task failed: {result}")
failed_indices.append(i)
else:
result_map[i] = result
# Retry failed specialists once if retry factories are provided
if failed_indices and retry_tasks:
retryable = [
(idx, retry_tasks[idx])
for idx in failed_indices
if idx < len(retry_tasks) and retry_tasks[idx] is not None
]
if retryable:
safe_print(
f"[{orchestrator_name}] Retrying {len(retryable)} failed specialist(s)...",
flush=True,
)
# Apply lifecycle wrapper to retry coroutines if provided
retry_coroutines = []
for idx, factory in retryable:
coro = factory()
# Apply lifecycle wrapper if available for this task
if retry_configs and idx < len(retry_configs):
config = retry_configs[idx]
wrapper = config.get("lifecycle_wrapper")
if wrapper:
spec_name = config.get("name", f"specialist_{idx}")
coro = wrapper(spec_name, coro)
retry_coroutines.append(coro)
retry_results = await asyncio.gather(
*retry_coroutines, return_exceptions=True
)
still_failing = []
for (idx, _), retry_result in zip(retryable, retry_results):
if isinstance(retry_result, Exception):
logger.error(
f"[{orchestrator_name}] Retry also failed for specialist {idx}: {retry_result}"
)
still_failing.append(idx)
else:
safe_print(
f"[{orchestrator_name}] Retry succeeded for specialist {idx}",
flush=True,
)
result_map[idx] = retry_result
# Log final summary of permanently failed specialists
if still_failing:
logger.error(
f"[{orchestrator_name}] Specialists {still_failing} failed permanently after retry"
)
succeeded = len(result_map)
safe_print(
f"[{orchestrator_name}] All specialists complete. "
f"{succeeded}/{len(tasks)} succeeded.",
flush=True,
)
# Return ordered list preserving original positions (None for failures)
return [result_map.get(i) for i in range(len(tasks))]
@@ -51,8 +51,7 @@ try:
from .category_utils import map_category
from .io_utils import safe_print
from .pr_worktree_manager import PRWorktreeManager
from .pydantic_models import FollowupExtractionResponse, ParallelFollowupResponse
from .recovery_utils import create_finding_from_summary
from .pydantic_models import ParallelFollowupResponse
from .sdk_utils import process_sdk_stream
except (ImportError, ValueError, SystemError):
from context_gatherer import _validate_git_ref
@@ -76,11 +75,7 @@ except (ImportError, ValueError, SystemError):
from services.category_utils import map_category
from services.io_utils import safe_print
from services.pr_worktree_manager import PRWorktreeManager
from services.pydantic_models import (
FollowupExtractionResponse,
ParallelFollowupResponse,
)
from services.recovery_utils import create_finding_from_summary
from services.pydantic_models import ParallelFollowupResponse
from services.sdk_utils import process_sdk_stream
@@ -581,36 +576,16 @@ The SDK will run invoked agents in parallel automatically.
)
# Check for stream processing errors
stream_error = stream_result.get("error")
if stream_error:
if stream_result.get("error_recoverable"):
# Recoverable error — attempt extraction call fallback
logger.warning(
f"[ParallelFollowup] Recoverable error: {stream_error}. "
f"Attempting extraction call fallback."
)
safe_print(
f"[ParallelFollowup] WARNING: {stream_error}"
f"attempting recovery with minimal extraction...",
flush=True,
)
else:
# Fatal error — raise as before
logger.error(
f"[ParallelFollowup] SDK stream failed: {stream_error}"
)
raise RuntimeError(
f"SDK stream processing failed: {stream_error}"
)
if stream_result.get("error"):
logger.error(
f"[ParallelFollowup] SDK stream failed: {stream_result['error']}"
)
raise RuntimeError(
f"SDK stream processing failed: {stream_result['error']}"
)
result_text = stream_result["result_text"]
last_assistant_text = stream_result.get("last_assistant_text", "")
# Nullify structured output on recoverable errors to force Tier 2 fallback
structured_output = (
None
if (stream_error and stream_result.get("error_recoverable"))
else stream_result["structured_output"]
)
structured_output = stream_result["structured_output"]
agents_invoked = stream_result["agents_invoked"]
msg_count = stream_result["msg_count"]
@@ -621,28 +596,22 @@ The SDK will run invoked agents in parallel automatically.
pr_number=context.pr_number,
)
# Parse findings from output (three-tier recovery cascade)
# Parse findings from output
if structured_output:
result_data = self._parse_structured_output(structured_output, context)
else:
# Structured output missing or validation failed.
# Tier 2: Attempt extraction call with minimal schema
# Log when structured output is missing - this shouldn't happen normally
# when output_format is configured, so it indicates a problem
logger.warning(
"[ParallelFollowup] No structured output — attempting extraction call"
"[ParallelFollowup] No structured output received from SDK - "
"falling back to text parsing. Resolution data may be incomplete."
)
# Use last_assistant_text (cleaner) if available, fall back to full transcript
fallback_text = last_assistant_text or result_text
result_data = await self._attempt_extraction_call(
fallback_text, context
safe_print(
"[ParallelFollowup] WARNING: Structured output not captured, "
"using text fallback (resolution tracking may be incomplete)",
flush=True,
)
if result_data is None:
# Tier 3: Fall back to basic text parsing
safe_print(
"[ParallelFollowup] WARNING: Extraction call failed, "
"using text fallback (resolution tracking may be incomplete)",
flush=True,
)
result_data = self._parse_text_output(result_text, context)
result_data = self._parse_text_output(result_text, context)
# Extract data
findings = result_data.get("findings", [])
@@ -761,9 +730,7 @@ The SDK will run invoked agents in parallel automatically.
blockers.append(f"{finding.category.value}: {finding.title}")
# Extract validation counts
dismissed_count = len(
result_data.get("dismissed_false_positive_ids", [])
) or result_data.get("dismissed_finding_count", 0)
dismissed_count = len(result_data.get("dismissed_false_positive_ids", []))
confirmed_count = result_data.get("confirmed_valid_count", 0)
needs_human_count = result_data.get("needs_human_review_count", 0)
@@ -1107,172 +1074,17 @@ The SDK will run invoked agents in parallel automatically.
elif "needs revision" in text_lower or "request changes" in text_lower:
verdict = MergeVerdict.NEEDS_REVISION
else:
verdict = MergeVerdict.NEEDS_REVISION
verdict = MergeVerdict.MERGE_WITH_CHANGES
return {
"findings": findings,
"resolved_ids": [],
"unresolved_ids": [],
"new_finding_ids": [],
"dismissed_false_positive_ids": [],
"confirmed_valid_count": 0,
"dismissed_finding_count": 0,
"needs_human_review_count": 0,
"verdict": verdict,
"verdict_reasoning": text[:500] if text else "Unable to parse response",
"agents_invoked": [],
}
async def _attempt_extraction_call(
self, text: str, context: FollowupReviewContext
) -> dict | None:
"""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 (small schema with ExtractedFindingSummary nesting)
which has near-100% success rate.
Returns parsed result dict on success, None on failure.
"""
if not text or not text.strip():
logger.warning("[ParallelFollowup] No text available for extraction call")
return None
try:
safe_print(
"[ParallelFollowup] Attempting recovery with minimal extraction schema...",
flush=True,
)
extraction_prompt = (
"Extract the key review data from the following AI analysis output. "
"Return the verdict, reasoning, resolved finding IDs, unresolved finding IDs, "
"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 ---"
)
model_shorthand = self.config.model or "sonnet"
model = resolve_model_id(model_shorthand)
extraction_client = create_client(
project_dir=self.project_dir,
spec_dir=self.github_dir,
model=model,
agent_type="pr_followup_extraction",
fast_mode=self.config.fast_mode,
output_format={
"type": "json_schema",
"schema": FollowupExtractionResponse.model_json_schema(),
},
)
async with extraction_client:
await extraction_client.query(extraction_prompt)
stream_result = await process_sdk_stream(
client=extraction_client,
context_name="FollowupExtraction",
model=model,
system_prompt=extraction_prompt,
max_messages=20,
)
if stream_result.get("error"):
logger.warning(
f"[ParallelFollowup] Extraction call also failed: {stream_result['error']}"
)
return None
extraction_output = stream_result.get("structured_output")
if not extraction_output:
logger.warning(
"[ParallelFollowup] Extraction call returned no structured output"
)
return None
# Parse the minimal extraction response
extracted = FollowupExtractionResponse.model_validate(extraction_output)
# Map verdict string to MergeVerdict enum
verdict_map = {
"READY_TO_MERGE": MergeVerdict.READY_TO_MERGE,
"MERGE_WITH_CHANGES": MergeVerdict.MERGE_WITH_CHANGES,
"NEEDS_REVISION": MergeVerdict.NEEDS_REVISION,
"BLOCKED": MergeVerdict.BLOCKED,
}
verdict = verdict_map.get(extracted.verdict, MergeVerdict.NEEDS_REVISION)
# Reconstruct findings from extraction data
findings = []
new_finding_ids = []
# 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)
# 2. Reconstruct unresolved findings from previous review context
if extracted.unresolved_finding_ids and context.previous_review.findings:
previous_map = {f.id: f for f in context.previous_review.findings}
for uid in extracted.unresolved_finding_ids:
original = previous_map.get(uid)
if original:
findings.append(
PRReviewFinding(
id=original.id,
severity=original.severity,
category=original.category,
title=f"[UNRESOLVED] {original.title}",
description=original.description,
file=original.file,
line=original.line,
suggested_fix=original.suggested_fix,
fixable=original.fixable,
is_impact_finding=original.is_impact_finding,
)
)
safe_print(
f"[ParallelFollowup] Extraction recovered: verdict={extracted.verdict}, "
f"{len(extracted.resolved_finding_ids)} resolved, "
f"{len(extracted.unresolved_finding_ids)} unresolved, "
f"{len(new_finding_ids)} new findings, "
f"{len(findings)} total findings reconstructed",
flush=True,
)
return {
"findings": findings,
"resolved_ids": extracted.resolved_finding_ids,
"unresolved_ids": extracted.unresolved_finding_ids,
"new_finding_ids": new_finding_ids,
"dismissed_false_positive_ids": [],
"confirmed_valid_count": extracted.confirmed_finding_count,
"dismissed_finding_count": extracted.dismissed_finding_count,
"needs_human_review_count": 0,
"verdict": verdict,
"verdict_reasoning": f"[Recovered via extraction] {extracted.verdict_reasoning}",
"agents_invoked": [],
}
except Exception as e:
logger.warning(f"[ParallelFollowup] Extraction call failed: {e}")
safe_print(
f"[ParallelFollowup] Extraction call failed: {e}",
flush=True,
)
return None
def _create_empty_result(self) -> dict:
"""Create empty result structure."""
return {
@@ -1280,13 +1092,8 @@ The SDK will run invoked agents in parallel automatically.
"resolved_ids": [],
"unresolved_ids": [],
"new_finding_ids": [],
"dismissed_false_positive_ids": [],
"confirmed_valid_count": 0,
"dismissed_finding_count": 0,
"needs_human_review_count": 0,
"verdict": MergeVerdict.NEEDS_REVISION,
"verdict_reasoning": "Unable to parse review results",
"agents_invoked": [],
}
def _extract_partial_data(self, data: dict) -> dict | None:
@@ -1295,7 +1102,6 @@ The SDK will run invoked agents in parallel automatically.
This handles cases where the AI produced valid data but it doesn't exactly
match the expected schema (missing optional fields, type mismatches, etc.).
Defensively extracts findings from the raw dict so partial results are preserved.
"""
if not isinstance(data, dict):
return None
@@ -1303,7 +1109,6 @@ The SDK will run invoked agents in parallel automatically.
resolved_ids = []
unresolved_ids = []
new_finding_ids = []
findings = []
# Try to extract resolution verifications
resolution_verifications = data.get("resolution_verifications", [])
@@ -1322,68 +1127,14 @@ The SDK will run invoked agents in parallel automatically.
):
unresolved_ids.append(finding_id)
# Try to extract new findings as PRReviewFinding objects
new_findings_raw = data.get("new_findings", [])
if isinstance(new_findings_raw, list):
for nf in new_findings_raw:
if not isinstance(nf, dict):
continue
try:
finding_id = nf.get("id", "") or self._generate_finding_id(
nf.get("file", "unknown"),
nf.get("line", 0),
nf.get("title", "unknown"),
)
new_finding_ids.append(finding_id)
findings.append(
PRReviewFinding(
id=finding_id,
severity=_map_severity(nf.get("severity", "medium")),
category=map_category(nf.get("category", "quality")),
title=nf.get("title", "Unknown issue"),
description=nf.get("description", ""),
file=nf.get("file", "unknown"),
line=nf.get("line", 0) or 0,
suggested_fix=nf.get("suggested_fix"),
fixable=bool(nf.get("fixable", False)),
is_impact_finding=bool(nf.get("is_impact_finding", False)),
)
)
except Exception as e:
logger.debug(
f"[ParallelFollowup] Skipping malformed new finding: {e}"
)
# Try to extract comment findings as PRReviewFinding objects
comment_findings_raw = data.get("comment_findings", [])
if isinstance(comment_findings_raw, list):
for cf in comment_findings_raw:
if not isinstance(cf, dict):
continue
try:
finding_id = cf.get("id", "") or self._generate_finding_id(
cf.get("file", "unknown"),
cf.get("line", 0),
cf.get("title", "unknown"),
)
new_finding_ids.append(finding_id)
findings.append(
PRReviewFinding(
id=finding_id,
severity=_map_severity(cf.get("severity", "medium")),
category=map_category(cf.get("category", "quality")),
title=f"[FROM COMMENTS] {cf.get('title', 'Unknown issue')}",
description=cf.get("description", ""),
file=cf.get("file", "unknown"),
line=cf.get("line", 0) or 0,
suggested_fix=cf.get("suggested_fix"),
fixable=bool(cf.get("fixable", False)),
)
)
except Exception as e:
logger.debug(
f"[ParallelFollowup] Skipping malformed comment finding: {e}"
)
# Try to extract new findings
new_findings = data.get("new_findings", [])
if isinstance(new_findings, list):
for nf in new_findings:
if isinstance(nf, dict):
finding_id = nf.get("id", "")
if finding_id:
new_finding_ids.append(finding_id)
# Try to extract verdict
verdict_str = data.get("verdict", "NEEDS_REVISION")
@@ -1398,15 +1149,14 @@ The SDK will run invoked agents in parallel automatically.
verdict_reasoning = data.get("verdict_reasoning", "Extracted from partial data")
# Only return if we got any useful data
if resolved_ids or unresolved_ids or new_finding_ids or findings:
if resolved_ids or unresolved_ids or new_finding_ids:
return {
"findings": findings,
"findings": [], # Can't reliably extract full findings without validation
"resolved_ids": resolved_ids,
"unresolved_ids": unresolved_ids,
"new_finding_ids": new_finding_ids,
"dismissed_false_positive_ids": [],
"confirmed_valid_count": 0,
"dismissed_finding_count": 0,
"needs_human_review_count": 0,
"verdict": verdict,
"verdict_reasoning": f"[Partial extraction] {verdict_reasoning}",
@@ -17,10 +17,12 @@ Key Design:
from __future__ import annotations
import asyncio
import hashlib
import logging
import os
from collections import defaultdict
from dataclasses import dataclass
from pathlib import Path
from typing import Any
@@ -51,7 +53,6 @@ try:
from .agent_utils import create_working_dir_injector
from .category_utils import map_category
from .io_utils import safe_print
from .parallel_agent_base import ParallelAgentOrchestrator, SpecialistConfig
from .pr_worktree_manager import PRWorktreeManager
from .pydantic_models import (
AgentAgreement,
@@ -82,7 +83,6 @@ except (ImportError, ValueError, SystemError):
from services.agent_utils import create_working_dir_injector
from services.category_utils import map_category
from services.io_utils import safe_print
from services.parallel_agent_base import ParallelAgentOrchestrator, SpecialistConfig
from services.pr_worktree_manager import PRWorktreeManager
from services.pydantic_models import (
AgentAgreement,
@@ -96,7 +96,16 @@ except (ImportError, ValueError, SystemError):
# =============================================================================
# Specialist Configuration for Parallel SDK Sessions
# =============================================================================
# SpecialistConfig is imported from parallel_agent_base.py
@dataclass
class SpecialistConfig:
"""Configuration for a specialist agent in parallel SDK sessions."""
name: str
prompt_file: str
tools: list[str]
description: str
# Define specialist configurations
@@ -173,7 +182,7 @@ def _is_finding_in_scope(
return True, "In scope"
class ParallelOrchestratorReviewer(ParallelAgentOrchestrator):
class ParallelOrchestratorReviewer:
"""
PR reviewer using SDK subagents for parallel specialist analysis.
@@ -185,12 +194,6 @@ class ParallelOrchestratorReviewer(ParallelAgentOrchestrator):
Model Configuration:
- Orchestrator uses user-configured model from frontend settings
- Specialist agents use model="inherit" (same as orchestrator)
Inherits from ParallelAgentOrchestrator:
- _report_progress() progress callback
- _load_prompt() loads from prompts/github/ directory
- _run_specialist_session() generic SDK session runner
- _run_parallel_specialists() asyncio.gather wrapper
"""
def __init__(
@@ -200,9 +203,41 @@ class ParallelOrchestratorReviewer(ParallelAgentOrchestrator):
config: GitHubRunnerConfig,
progress_callback=None,
):
super().__init__(project_dir, github_dir, config, progress_callback)
self.project_dir = Path(project_dir)
self.github_dir = Path(github_dir)
self.config = config
self.progress_callback = progress_callback
self.worktree_manager = PRWorktreeManager(project_dir, PR_WORKTREE_DIR)
def _report_progress(self, phase: str, progress: int, message: str, **kwargs):
"""Report progress if callback is set."""
if self.progress_callback:
import sys
if "orchestrator" in sys.modules:
ProgressCallback = sys.modules["orchestrator"].ProgressCallback
else:
try:
from ..orchestrator import ProgressCallback
except ImportError:
from orchestrator import ProgressCallback
self.progress_callback(
ProgressCallback(
phase=phase, progress=progress, message=message, **kwargs
)
)
def _load_prompt(self, filename: str) -> str:
"""Load a prompt file from the prompts/github directory."""
prompt_file = (
Path(__file__).parent.parent.parent.parent / "prompts" / "github" / filename
)
if prompt_file.exists():
return prompt_file.read_text(encoding="utf-8")
logger.warning(f"Prompt file not found: {prompt_file}")
return ""
def _create_pr_worktree(self, head_sha: str, pr_number: int) -> Path:
"""Create a temporary worktree at the PR head commit.
@@ -440,7 +475,7 @@ Report findings with specific file paths, line numbers, and code evidence.
return prompt_with_cwd + pr_context
async def _run_pr_specialist_session(
async def _run_specialist_session(
self,
config: SpecialistConfig,
context: PRContext,
@@ -448,10 +483,7 @@ Report findings with specific file paths, line numbers, and code evidence.
model: str,
thinking_budget: int | None,
) -> tuple[str, list[PRReviewFinding]]:
"""Run a single PR review specialist as its own SDK session.
Builds the PR-specific prompt, delegates to the base class's generic
_run_specialist_session(), then parses findings from the result.
"""Run a single specialist as its own SDK session.
Args:
config: Specialist configuration
@@ -463,36 +495,84 @@ Report findings with specific file paths, line numbers, and code evidence.
Returns:
Tuple of (specialist_name, findings)
"""
# Build the specialist prompt with PR context
prompt = self._build_specialist_prompt(config, context, project_root)
# Delegate to base class generic session runner
stream_result = await super()._run_specialist_session(
config=config,
prompt=prompt,
project_root=project_root,
model=model,
thinking_budget=thinking_budget,
output_schema=SpecialistResponse.model_json_schema(),
agent_type="pr_reviewer",
)
error = stream_result.get("error")
if error:
return (config.name, [])
# Parse structured output into PRReviewFindings
structured_output = stream_result.get("structured_output")
findings = self._parse_specialist_output(
config.name, structured_output, stream_result.get("result_text", "")
)
safe_print(
f"[Specialist:{config.name}] Complete: {len(findings)} findings",
f"[Specialist:{config.name}] Starting analysis...",
flush=True,
)
return (config.name, findings)
# Build the specialist prompt with PR context
prompt = self._build_specialist_prompt(config, context, project_root)
try:
# Create SDK client for this specialist
# Note: Agent type uses the generic "pr_reviewer" since individual
# specialist types aren't registered in AGENT_CONFIGS. The specialist-specific
# system prompt handles differentiation.
# Get betas from model shorthand (before resolution to full ID)
betas = get_model_betas(self.config.model or "sonnet")
thinking_kwargs = get_thinking_kwargs_for_model(
model, self.config.thinking_level or "medium"
)
client = create_client(
project_dir=project_root,
spec_dir=self.github_dir,
model=model,
agent_type="pr_reviewer",
betas=betas,
fast_mode=self.config.fast_mode,
output_format={
"type": "json_schema",
"schema": SpecialistResponse.model_json_schema(),
},
**thinking_kwargs,
)
async with client:
await client.query(prompt)
# Process SDK stream
stream_result = await process_sdk_stream(
client=client,
context_name=f"Specialist:{config.name}",
model=model,
system_prompt=prompt,
agent_definitions={}, # No subagents for specialists
)
error = stream_result.get("error")
if error:
logger.error(
f"[Specialist:{config.name}] SDK stream failed: {error}"
)
safe_print(
f"[Specialist:{config.name}] Analysis failed: {error}",
flush=True,
)
return (config.name, [])
# Parse structured output
structured_output = stream_result.get("structured_output")
findings = self._parse_specialist_output(
config.name, structured_output, stream_result.get("result_text", "")
)
safe_print(
f"[Specialist:{config.name}] Complete: {len(findings)} findings",
flush=True,
)
return (config.name, findings)
except Exception as e:
logger.error(
f"[Specialist:{config.name}] Session failed: {e}",
exc_info=True,
)
safe_print(
f"[Specialist:{config.name}] Error: {e}",
flush=True,
)
return (config.name, [])
def _parse_specialist_output(
self,
@@ -517,8 +597,9 @@ Report findings with specific file paths, line numbers, and code evidence.
result = SpecialistResponse.model_validate(structured_output)
for f in result.findings:
finding_id = hashlib.sha256(
finding_id = hashlib.md5(
f"{f.file}:{f.line}:{f.title}".encode(),
usedforsecurity=False,
).hexdigest()[:12]
category = map_category(f.category)
@@ -552,14 +633,7 @@ Report findings with specific file paths, line numbers, and code evidence.
logger.error(
f"[Specialist:{specialist_name}] Failed to parse structured output: {e}"
)
# Attempt to extract findings from raw dict before falling to text parsing
findings = self._extract_specialist_partial_data(
specialist_name, structured_output
)
if findings:
logger.info(
f"[Specialist:{specialist_name}] Recovered {len(findings)} findings from partial extraction"
)
# Fall through to text parsing
if not findings and result_text:
# Fallback to text parsing
@@ -569,73 +643,14 @@ Report findings with specific file paths, line numbers, and code evidence.
return findings
def _extract_specialist_partial_data(
self,
specialist_name: str,
data: dict[str, Any],
) -> list[PRReviewFinding]:
"""Extract findings from raw specialist dict when Pydantic validation fails.
Defensively extracts each finding individually so partial results are preserved
even if some findings have validation issues.
"""
findings = []
raw_findings = data.get("findings", [])
if not isinstance(raw_findings, list):
return findings
for f in raw_findings:
if not isinstance(f, dict):
continue
try:
file_path = f.get("file", "unknown")
line = f.get("line", 0) or 0
title = f.get("title", "Unknown issue")
finding_id = hashlib.sha256(
f"{file_path}:{line}:{title}".encode(),
).hexdigest()[:12]
category = map_category(f.get("category", "quality"))
try:
severity = ReviewSeverity(str(f.get("severity", "medium")).lower())
except ValueError:
severity = ReviewSeverity.MEDIUM
finding = PRReviewFinding(
id=finding_id,
file=file_path,
line=line,
end_line=f.get("end_line"),
title=title,
description=f.get("description", ""),
category=category,
severity=severity,
suggested_fix=f.get("suggested_fix", ""),
evidence=f.get("evidence"),
source_agents=[specialist_name],
is_impact_finding=bool(f.get("is_impact_finding", False)),
)
findings.append(finding)
except Exception as e:
logger.debug(
f"[Specialist:{specialist_name}] Skipping malformed finding: {e}"
)
return findings
async def _run_parallel_pr_specialists(
async def _run_parallel_specialists(
self,
context: PRContext,
project_root: Path,
model: str,
thinking_budget: int | None,
) -> tuple[list[PRReviewFinding], list[str]]:
"""Run all PR review specialists in parallel and collect findings.
Uses the base class's _run_parallel_specialists() for the gather
pattern, with PR-specific task creation and result parsing.
"""Run all specialists in parallel and collect findings.
Args:
context: PR context
@@ -646,47 +661,42 @@ Report findings with specific file paths, line numbers, and code evidence.
Returns:
Tuple of (all_findings, agents_invoked)
"""
# Build coroutine factories so failed specialists can be retried
def _make_pr_specialist_factory(cfg: SpecialistConfig):
def factory():
return self._run_pr_specialist_session(
config=cfg,
context=context,
project_root=project_root,
model=model,
thinking_budget=thinking_budget,
)
return factory
coroutines = []
retry_factories = []
for config in SPECIALIST_CONFIGS:
factory = _make_pr_specialist_factory(config)
coroutines.append(factory())
retry_factories.append(factory)
# Run all specialists in parallel via base class (with retry-once)
valid_results = await super()._run_parallel_specialists(
tasks=coroutines,
orchestrator_name="ParallelOrchestrator",
retry_tasks=retry_factories,
safe_print(
f"[ParallelOrchestrator] Launching {len(SPECIALIST_CONFIGS)} specialists in parallel...",
flush=True,
)
# Create tasks for all specialists
tasks = [
self._run_specialist_session(
config=config,
context=context,
project_root=project_root,
model=model,
thinking_budget=thinking_budget,
)
for config in SPECIALIST_CONFIGS
]
# Run all specialists in parallel
results = await asyncio.gather(*tasks, return_exceptions=True)
# Collect findings and track which agents ran
all_findings: list[PRReviewFinding] = []
agents_invoked: list[str] = []
for result in valid_results:
if result is None:
for result in results:
if isinstance(result, Exception):
logger.error(f"[ParallelOrchestrator] Specialist task failed: {result}")
continue
specialist_name, findings = result
agents_invoked.append(specialist_name)
all_findings.extend(findings)
safe_print(
f"[ParallelOrchestrator] Total findings: {len(all_findings)}",
f"[ParallelOrchestrator] All specialists complete. "
f"Total findings: {len(all_findings)}",
flush=True,
)
@@ -888,8 +898,9 @@ The SDK will run invoked agents in parallel automatically.
Returns:
PRReviewFinding instance
"""
finding_id = hashlib.sha256(
finding_id = hashlib.md5(
f"{finding_data.file}:{finding_data.line}:{finding_data.title}".encode(),
usedforsecurity=False,
).hexdigest()[:12]
category = map_category(finding_data.category)
@@ -899,15 +910,13 @@ The SDK will run invoked agents in parallel automatically.
except ValueError:
severity = ReviewSeverity.MEDIUM
# Extract evidence from verification.code_examined if available
evidence = None
# Extract evidence: prefer verification.code_examined, fallback to evidence field
evidence = finding_data.evidence
if hasattr(finding_data, "verification") and finding_data.verification:
# Structured verification has more detailed evidence
verification = finding_data.verification
if hasattr(verification, "code_examined") and verification.code_examined:
evidence = verification.code_examined
# Fallback to evidence field if present (e.g. from dict-based parsing)
if not evidence:
evidence = getattr(finding_data, "evidence", None)
# Extract end_line if present
end_line = getattr(finding_data, "end_line", None)
@@ -1118,7 +1127,7 @@ The SDK will run invoked agents in parallel automatically.
# =================================================================
# Run all specialists in parallel
findings, agents_invoked = await self._run_parallel_pr_specialists(
findings, agents_invoked = await self._run_parallel_specialists(
context=context,
project_root=project_root,
model=model,
@@ -1214,30 +1223,12 @@ The SDK will run invoked agents in parallel automatically.
f"{len(filtered_findings)} filtered"
)
# 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)
# No confidence routing - validation is binary via finding-validator
unique_findings = validated_findings
logger.info(f"[PRReview] Final findings: {len(unique_findings)} validated")
safe_print(
f"[ParallelOrchestrator] Final: {len(active_findings)} active, "
f"{len(dismissed_findings)} disputed by validator",
flush=True,
)
logger.info(
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)"
f"[ParallelOrchestrator] Review complete: {len(unique_findings)} findings"
)
# Fetch CI status for verdict consideration
@@ -1247,9 +1238,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 from ACTIVE findings only (dismissed don't affect verdict)
# Generate verdict (includes merge conflict check, branch-behind check, and CI status)
verdict, verdict_reasoning, blockers = self._generate_verdict(
active_findings,
unique_findings,
has_merge_conflicts=context.has_merge_conflicts,
merge_state_status=context.merge_state_status,
ci_status=ci_status,
@@ -1260,7 +1251,7 @@ The SDK will run invoked agents in parallel automatically.
verdict=verdict,
verdict_reasoning=verdict_reasoning,
blockers=blockers,
findings=all_review_findings,
findings=unique_findings,
agents_invoked=agents_invoked,
)
@@ -1305,7 +1296,7 @@ The SDK will run invoked agents in parallel automatically.
pr_number=context.pr_number,
repo=self.config.repo,
success=True,
findings=all_review_findings,
findings=unique_findings,
summary=summary,
overall_status=overall_status,
verdict=verdict,
@@ -1427,8 +1418,9 @@ The SDK will run invoked agents in parallel automatically.
Returns:
PRReviewFinding instance
"""
finding_id = hashlib.sha256(
finding_id = hashlib.md5(
f"{f_data.get('file', 'unknown')}:{f_data.get('line', 0)}:{f_data.get('title', 'Untitled')}".encode(),
usedforsecurity=False,
).hexdigest()[:12]
category = map_category(f_data.get("category", "quality"))
@@ -1793,7 +1785,6 @@ For EACH finding above:
or "concurrency" in error_str
or "circuit breaker" in error_str
or "tool_use" in error_str
or "structured_output" in error_str
)
if is_retryable and attempt < MAX_VALIDATION_RETRIES:
@@ -1814,7 +1805,6 @@ For EACH finding above:
break
except Exception as e:
# Part of retry loop structure - handles retryable errors
error_str = str(e).lower()
is_retryable = (
"400" in error_str
@@ -1879,38 +1869,12 @@ For EACH finding above:
validated_findings.append(finding)
elif validation.validation_status == "dismissed_false_positive":
# 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]}"
)
# Dismiss - do not include
dismissed_count += 1
logger.info(
f"[PRReview] Dismissed {finding.id} as false positive: "
f"{validation.explanation[:100]}"
)
elif validation.validation_status == "needs_human_review":
# Keep but flag
@@ -2095,16 +2059,11 @@ 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}"
if is_disputed:
lines.append(f"#### ⚪ [DISPUTED] ~~{f.title}~~")
else:
lines.append(f"#### {emoji} [{sev.upper()}] {f.title}")
lines.append(f"#### {emoji} [{sev.upper()}] {f.title}")
lines.append(f"**File:** `{f.file}` ({line_range})")
# Cross-validation badge
@@ -2134,7 +2093,6 @@ 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}")
@@ -2156,27 +2114,18 @@ For EACH finding above:
lines.append("")
# Findings count summary (exclude dismissed from active count)
active_count = 0
dismissed_count = 0
# Findings count summary
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}")
count_text = (
f"**Total:** {active_count} finding(s) ({', '.join(summary_parts)})"
lines.append(
f"**Total:** {len(findings)} finding(s) ({', '.join(summary_parts)})"
)
if dismissed_count > 0:
count_text += f" + {dismissed_count} disputed"
lines.append(count_text)
lines.append("")
lines.append("---")
@@ -26,10 +26,10 @@ from __future__ import annotations
from typing import Literal
from pydantic import BaseModel, Field, field_validator
from pydantic import BaseModel, Field
# =============================================================================
# Verification Evidence (Optional for findings — only code_examined is consumed)
# Verification Evidence (Required for All Findings)
# =============================================================================
@@ -50,28 +50,102 @@ class VerificationEvidence(BaseModel):
# =============================================================================
# Severity / Category Validators
# Common Finding Types
# =============================================================================
_VALID_SEVERITIES = {"critical", "high", "medium", "low"}
class BaseFinding(BaseModel):
"""Base class for all finding types."""
id: str = Field(description="Unique identifier for this finding")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity level"
)
title: str = Field(description="Brief issue title (max 80 chars)")
description: str = Field(description="Detailed explanation of the issue")
file: str = Field(description="File path where issue was found")
line: int = Field(0, description="Line number of the issue")
suggested_fix: str | None = Field(None, description="How to fix this issue")
fixable: bool = Field(False, description="Whether this can be auto-fixed")
evidence: str | None = Field(
None,
description="DEPRECATED: Use verification.code_examined instead. Will be removed in Phase 5.",
)
verification: VerificationEvidence = Field(
description="Evidence that this finding was verified against actual code"
)
def _normalize_severity(v: str) -> str:
"""Normalize severity to a valid value, defaulting to 'medium'."""
if isinstance(v, str):
v = v.lower().strip()
if v not in _VALID_SEVERITIES:
return "medium"
return v
class SecurityFinding(BaseFinding):
"""A security vulnerability finding."""
category: Literal["security"] = Field(
default="security", description="Always 'security' for security findings"
)
def _normalize_category(v: str, valid_set: set[str], default: str = "quality") -> str:
"""Normalize category to a valid value, defaulting to given default."""
if isinstance(v, str):
v = v.lower().strip().replace("-", "_")
if v not in valid_set:
return default
return v
class QualityFinding(BaseFinding):
"""A code quality or redundancy finding."""
category: Literal[
"redundancy", "quality", "test", "performance", "pattern", "docs"
] = Field(description="Issue category")
redundant_with: str | None = Field(
None, description="Reference to duplicate code (file:line) if redundant"
)
class DeepAnalysisFinding(BaseFinding):
"""A finding from deep analysis with verification info."""
category: Literal[
"verification_failed",
"redundancy",
"quality",
"pattern",
"performance",
"logic",
] = Field(description="Issue category")
verification_note: str | None = Field(
None, description="What evidence is missing or couldn't be verified"
)
class StructuralIssue(BaseModel):
"""A structural issue with the PR."""
id: str = Field(description="Unique identifier")
issue_type: Literal[
"feature_creep", "scope_creep", "architecture_violation", "poor_structure"
] = Field(description="Type of structural issue")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity"
)
title: str = Field(description="Brief issue title")
description: str = Field(description="Detailed explanation")
impact: str = Field(description="Why this matters")
suggestion: str = Field(description="How to fix")
class AICommentTriage(BaseModel):
"""Triage result for an AI tool comment."""
comment_id: int = Field(description="GitHub comment ID")
tool_name: str = Field(
description="AI tool name (CodeRabbit, Cursor, Greptile, etc.)"
)
verdict: Literal[
"critical",
"important",
"nice_to_have",
"trivial",
"addressed",
"false_positive",
] = Field(description="Verdict on the comment")
reasoning: str = Field(description="Why this verdict was chosen")
response_comment: str | None = Field(
None, description="Optional comment to post in reply"
)
# =============================================================================
@@ -89,34 +163,25 @@ class FindingResolution(BaseModel):
)
_FOLLOWUP_CATEGORIES = {"security", "quality", "logic", "test", "docs"}
class FollowupFinding(BaseModel):
"""A new finding from follow-up review (simpler than initial review).
verification is intentionally omitted not consumed by followup_reviewer.py.
"""
"""A new finding from follow-up review (simpler than initial review)."""
id: str = Field(description="Unique identifier for this finding")
severity: str = Field(description="Issue severity level")
category: str = Field(description="Issue category")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity level"
)
category: Literal["security", "quality", "logic", "test", "docs"] = Field(
description="Issue category"
)
title: str = Field(description="Brief issue title")
description: str = Field(description="Detailed explanation of the issue")
file: str = Field(description="File path where issue was found")
line: int = Field(0, description="Line number of the issue")
suggested_fix: str | None = Field(None, description="How to fix this issue")
fixable: bool = Field(False, description="Whether this can be auto-fixed")
@field_validator("severity", mode="before")
@classmethod
def _normalize_severity(cls, v: str) -> str:
return _normalize_severity(v)
@field_validator("category", mode="before")
@classmethod
def _normalize_category(cls, v: str) -> str:
return _normalize_category(v, _FOLLOWUP_CATEGORIES)
verification: VerificationEvidence = Field(
description="Evidence that this finding was verified against actual code"
)
class FollowupReviewResponse(BaseModel):
@@ -138,6 +203,81 @@ class FollowupReviewResponse(BaseModel):
verdict_reasoning: str = Field(description="Explanation for the verdict")
# =============================================================================
# Initial Review Responses (Multi-Pass)
# =============================================================================
class QuickScanResult(BaseModel):
"""Result from the quick scan pass."""
purpose: str = Field(description="Brief description of what the PR claims to do")
actual_changes: str = Field(
description="Brief description of what the code actually does"
)
purpose_match: bool = Field(
description="Whether actual changes match the claimed purpose"
)
purpose_match_note: str | None = Field(
None, description="Explanation if purpose doesn't match actual changes"
)
risk_areas: list[str] = Field(
default_factory=list, description="Areas needing careful review"
)
red_flags: list[str] = Field(
default_factory=list, description="Obvious issues or concerns"
)
requires_deep_verification: bool = Field(
description="Whether deep verification is needed"
)
complexity: Literal["low", "medium", "high"] = Field(description="PR complexity")
class SecurityPassResult(BaseModel):
"""Result from the security pass - array of security findings."""
findings: list[SecurityFinding] = Field(
default_factory=list, description="Security vulnerabilities found"
)
class QualityPassResult(BaseModel):
"""Result from the quality pass - array of quality findings."""
findings: list[QualityFinding] = Field(
default_factory=list, description="Quality and redundancy issues found"
)
class DeepAnalysisResult(BaseModel):
"""Result from the deep analysis pass."""
findings: list[DeepAnalysisFinding] = Field(
default_factory=list,
description="Deep analysis findings with verification info",
)
class StructuralPassResult(BaseModel):
"""Result from the structural pass."""
issues: list[StructuralIssue] = Field(
default_factory=list, description="Structural issues found"
)
verdict: Literal[
"READY_TO_MERGE", "MERGE_WITH_CHANGES", "NEEDS_REVISION", "BLOCKED"
] = Field(description="Structural verdict")
verdict_reasoning: str = Field(description="Explanation for the verdict")
class AICommentTriageResult(BaseModel):
"""Result from AI comment triage pass."""
triages: list[AICommentTriage] = Field(
default_factory=list, description="Triage results for each AI comment"
)
# =============================================================================
# Issue Triage Response
# =============================================================================
@@ -180,21 +320,88 @@ class IssueTriageResponse(BaseModel):
comment: str | None = Field(None, description="Optional bot comment to post")
# =============================================================================
# Orchestrator Review Response
# =============================================================================
class OrchestratorFinding(BaseModel):
"""A finding from the orchestrator review."""
file: str = Field(description="File path where issue was found")
line: int = Field(0, description="Line number of the issue")
title: str = Field(description="Brief issue title")
description: str = Field(description="Detailed explanation of the issue")
category: Literal[
"security",
"quality",
"style",
"docs",
"redundancy",
"verification_failed",
"pattern",
"performance",
"logic",
"test",
] = Field(description="Issue category")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity level"
)
suggestion: str | None = Field(None, description="How to fix this issue")
evidence: str | None = Field(
None,
description="DEPRECATED: Use verification.code_examined instead. Will be removed in Phase 5.",
)
verification: VerificationEvidence = Field(
description="Evidence that this finding was verified against actual code"
)
class OrchestratorReviewResponse(BaseModel):
"""Complete response schema for orchestrator PR review."""
verdict: Literal[
"READY_TO_MERGE", "MERGE_WITH_CHANGES", "NEEDS_REVISION", "BLOCKED"
] = Field(description="Overall merge verdict")
verdict_reasoning: str = Field(description="Explanation for the verdict")
findings: list[OrchestratorFinding] = Field(
default_factory=list, description="Issues found during review"
)
summary: str = Field(description="Brief summary of the review")
# =============================================================================
# Parallel Orchestrator Review Response (SDK Subagents)
# =============================================================================
_ORCHESTRATOR_CATEGORIES = {
"security",
"quality",
"logic",
"codebase_fit",
"test",
"docs",
"redundancy",
"pattern",
"performance",
}
class LogicFinding(BaseFinding):
"""A logic/correctness finding from the logic review agent."""
category: Literal["logic"] = Field(
default="logic", description="Always 'logic' for logic findings"
)
example_input: str | None = Field(
None, description="Concrete input that triggers the bug"
)
actual_output: str | None = Field(None, description="What the buggy code produces")
expected_output: str | None = Field(
None, description="What the code should produce"
)
class CodebaseFitFinding(BaseFinding):
"""A codebase fit finding from the codebase fit review agent."""
category: Literal["codebase_fit"] = Field(
default="codebase_fit", description="Always 'codebase_fit' for fit findings"
)
existing_code: str | None = Field(
None, description="Reference to existing code that should be used instead"
)
codebase_pattern: str | None = Field(
None, description="Description of the established pattern being violated"
)
class ParallelOrchestratorFinding(BaseModel):
@@ -206,11 +413,26 @@ class ParallelOrchestratorFinding(BaseModel):
end_line: int | None = Field(None, description="End line for multi-line issues")
title: str = Field(description="Brief issue title (max 80 chars)")
description: str = Field(description="Detailed explanation of the issue")
category: str = Field(description="Issue category")
severity: str = Field(description="Issue severity level")
verification: VerificationEvidence | None = Field(
category: Literal[
"security",
"quality",
"logic",
"codebase_fit",
"test",
"docs",
"redundancy",
"pattern",
"performance",
] = Field(description="Issue category")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity level"
)
evidence: str | None = Field(
None,
description="Evidence that this finding was verified against actual code",
description="DEPRECATED: Use verification.code_examined instead. Will be removed in Phase 5.",
)
verification: VerificationEvidence = Field(
description="Evidence that this finding was verified against actual code"
)
is_impact_finding: bool = Field(
False,
@@ -237,16 +459,6 @@ class ParallelOrchestratorFinding(BaseModel):
False, description="Whether multiple agents agreed on this finding"
)
@field_validator("severity", mode="before")
@classmethod
def _normalize_severity(cls, v: str) -> str:
return _normalize_severity(v)
@field_validator("category", mode="before")
@classmethod
def _normalize_category(cls, v: str) -> str:
return _normalize_category(v, _ORCHESTRATOR_CATEGORIES)
class AgentAgreement(BaseModel):
"""Tracks agreement between agents on findings."""
@@ -302,22 +514,15 @@ class ValidationSummary(BaseModel):
)
_SPECIALIST_CATEGORIES = {
"security",
"quality",
"logic",
"performance",
"pattern",
"test",
"docs",
}
class SpecialistFinding(BaseModel):
"""A finding from a specialist agent (used in parallel SDK sessions)."""
severity: str = Field(description="Issue severity level")
category: str = Field(description="Issue category")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity level"
)
category: Literal[
"security", "quality", "logic", "performance", "pattern", "test", "docs"
] = Field(description="Issue category")
title: str = Field(description="Brief issue title (max 80 chars)")
description: str = Field(description="Detailed explanation of the issue")
file: str = Field(description="File path where issue was found")
@@ -325,24 +530,14 @@ class SpecialistFinding(BaseModel):
end_line: int | None = Field(None, description="End line number if multi-line")
suggested_fix: str | None = Field(None, description="How to fix this issue")
evidence: str = Field(
default="",
description="Actual code snippet examined that shows the issue.",
min_length=1,
description="Actual code snippet examined that shows the issue. Required.",
)
is_impact_finding: bool = Field(
False,
description="True if this is about affected code outside the PR (callers, dependencies)",
)
@field_validator("severity", mode="before")
@classmethod
def _normalize_severity(cls, v: str) -> str:
return _normalize_severity(v)
@field_validator("category", mode="before")
@classmethod
def _normalize_category(cls, v: str) -> str:
return _normalize_category(v, _SPECIALIST_CATEGORIES)
class SpecialistResponse(BaseModel):
"""Response schema for individual specialist agent (parallel SDK sessions).
@@ -416,17 +611,6 @@ class ResolutionVerification(BaseModel):
)
_PARALLEL_FOLLOWUP_CATEGORIES = {
"security",
"quality",
"logic",
"test",
"docs",
"regression",
"incomplete_fix",
}
class ParallelFollowupFinding(BaseModel):
"""A finding from parallel follow-up review."""
@@ -435,8 +619,18 @@ class ParallelFollowupFinding(BaseModel):
line: int = Field(0, description="Line number of the issue")
title: str = Field(description="Brief issue title")
description: str = Field(description="Detailed explanation of the issue")
category: str = Field(description="Issue category")
severity: str = Field(description="Issue severity level")
category: Literal[
"security",
"quality",
"logic",
"test",
"docs",
"regression",
"incomplete_fix",
] = Field(description="Issue category")
severity: Literal["critical", "high", "medium", "low"] = Field(
description="Issue severity level"
)
suggested_fix: str | None = Field(None, description="How to fix this issue")
fixable: bool = Field(False, description="Whether this can be auto-fixed")
is_impact_finding: bool = Field(
@@ -444,16 +638,6 @@ class ParallelFollowupFinding(BaseModel):
description="True if this finding is about impact on OTHER files outside the PR diff",
)
@field_validator("severity", mode="before")
@classmethod
def _normalize_severity(cls, v: str) -> str:
return _normalize_severity(v)
@field_validator("category", mode="before")
@classmethod
def _normalize_category(cls, v: str) -> str:
return _normalize_category(v, _PARALLEL_FOLLOWUP_CATEGORIES)
class ParallelFollowupResponse(BaseModel):
"""Complete response schema for parallel follow-up PR review.
@@ -526,55 +710,3 @@ class FindingValidationResponse(BaseModel):
"how many dismissed, how many need human review"
)
)
# =============================================================================
# Minimal Extraction Schema (Fallback for structured output validation failure)
# =============================================================================
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.
Uses ExtractedFindingSummary for new findings to preserve file/line information.
Used as an intermediate recovery step before falling back to raw text parsing.
"""
verdict: Literal[
"READY_TO_MERGE", "MERGE_WITH_CHANGES", "NEEDS_REVISION", "BLOCKED"
] = Field(description="Overall merge verdict")
verdict_reasoning: str = Field(description="Explanation for the verdict")
resolved_finding_ids: list[str] = Field(
default_factory=list,
description="IDs of previous findings that are now resolved",
)
unresolved_finding_ids: list[str] = Field(
default_factory=list,
description="IDs of previous findings that remain unresolved",
)
new_finding_summaries: list[ExtractedFindingSummary] = Field(
default_factory=list,
description="Structured summary of each new finding with file location",
)
confirmed_finding_count: int = Field(
0, description="Number of findings confirmed as valid"
)
dismissed_finding_count: int = Field(
0, description="Number of findings dismissed as false positives"
)
@@ -1,120 +0,0 @@
"""
Recovery Utilities for PR Review
=================================
Shared helpers for extraction recovery in followup and parallel followup reviewers.
These utilities consolidate duplicated logic for:
- Parsing "SEVERITY: description" patterns from extraction summaries
- Generating consistent, traceable finding IDs with prefixes
- Creating PRReviewFinding objects from extraction data
"""
from __future__ import annotations
import hashlib
try:
from ..models import (
PRReviewFinding,
ReviewCategory,
ReviewSeverity,
)
except (ImportError, ValueError, SystemError):
from models import (
PRReviewFinding,
ReviewCategory,
ReviewSeverity,
)
# Severity mapping for parsing "SEVERITY: description" patterns
_EXTRACTION_SEVERITY_MAP: list[tuple[str, ReviewSeverity]] = [
("CRITICAL:", ReviewSeverity.CRITICAL),
("HIGH:", ReviewSeverity.HIGH),
("MEDIUM:", ReviewSeverity.MEDIUM),
("LOW:", ReviewSeverity.LOW),
]
def parse_severity_from_summary(
summary: str,
) -> tuple[ReviewSeverity, str]:
"""Parse a "SEVERITY: description" pattern from an extraction summary.
Args:
summary: Raw summary string, e.g. "HIGH: Missing null check in parser.py"
Returns:
Tuple of (severity, cleaned_description).
Defaults to MEDIUM severity if no prefix is found.
"""
upper_summary = summary.upper()
for sev_name, sev_val in _EXTRACTION_SEVERITY_MAP:
if upper_summary.startswith(sev_name):
return sev_val, summary[len(sev_name) :].strip()
return ReviewSeverity.MEDIUM, summary
def generate_recovery_finding_id(
index: int, description: str, prefix: str = "FR"
) -> str:
"""Generate a consistent, traceable finding ID for recovery findings.
Args:
index: The index of the finding in the extraction list.
description: The finding description (used for hash uniqueness).
prefix: ID prefix for traceability. Default "FR" (Followup Recovery).
Use "FU" for parallel followup findings.
Returns:
A prefixed finding ID like "FR-A1B2C3D4" or "FU-A1B2C3D4".
"""
content = f"extraction-{index}-{description}"
hex_hash = (
hashlib.md5(content.encode(), usedforsecurity=False).hexdigest()[:8].upper()
)
return f"{prefix}-{hex_hash}"
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.
Parses "SEVERITY: description" patterns, generates a traceable finding ID,
and returns a fully constructed PRReviewFinding.
Args:
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(
id=finding_id,
severity=severity,
category=ReviewCategory.QUALITY,
title=description[:80],
description=f"[Recovered via extraction] {description}",
file=file,
line=line,
)
@@ -133,13 +133,6 @@ def _get_tool_detail(tool_name: str, tool_input: dict[str, Any]) -> str:
# Prevents runaway retry loops from consuming unbounded resources
MAX_MESSAGE_COUNT = 500
# Errors that are recoverable (callers can fall back to text parsing or retry)
# vs fatal errors (auth failures, circuit breaker) that should propagate
RECOVERABLE_ERRORS = {
"structured_output_validation_failed",
"tool_use_concurrency_error",
}
# Abort after 1 consecutive repeat (2 total identical responses).
# Low threshold catches error loops quickly (e.g., auth errors returned as AI text).
# Normal AI responses never produce the exact same text block twice in a row.
@@ -268,11 +261,8 @@ async def process_sdk_stream(
- msg_count: Total message count
- subagent_tool_ids: Mapping of tool_id -> agent_name
- error: Error message if stream processing failed (None on success)
- error_recoverable: Boolean indicating if the error is recoverable (fallback possible) vs fatal
- last_assistant_text: Last non-empty assistant text block (for cleaner fallback parsing)
"""
result_text = ""
last_assistant_text = "" # Last assistant text block (for cleaner fallback parsing)
structured_output = None
agents_invoked = []
msg_count = 0
@@ -280,8 +270,6 @@ async def process_sdk_stream(
# Track subagent tool IDs to log their results
subagent_tool_ids: dict[str, str] = {} # tool_id -> agent_name
completed_agent_tool_ids: set[str] = set() # tool_ids of completed agents
# Track StructuredOutput tool submissions for fallback capture
_pending_structured_output: dict[str, dict[str, Any]] = {} # tool_id -> tool_input
# Track tool concurrency errors for retry logic
detected_concurrency_error = False
# Track repeated identical responses to detect error loops early
@@ -402,10 +390,7 @@ async def process_sdk_stream(
safe_print(
f"[{context_name}] Delegation prompt for {agent_name}: {prompt_preview}"
)
elif tool_name == "StructuredOutput":
# Track StructuredOutput submission for fallback capture
_pending_structured_output[tool_id] = tool_input
else:
elif tool_name != "StructuredOutput":
# Log meaningful tool info (not just tool name)
tool_detail = _get_tool_detail(tool_name, tool_input)
safe_print(f"[{context_name}] {tool_detail}")
@@ -451,15 +436,6 @@ async def process_sdk_stream(
f"[{context_name}] Tool result [{status}]: {result_preview}{'...' if len(str(result_content)) > 100 else ''}"
)
# Capture validated StructuredOutput tool data as fallback
if tool_id in _pending_structured_output:
if not is_error:
# Tool validation passed — save as fallback
_pending_structured_output[tool_id]["_validated"] = True
else:
# Validation failed — discard this attempt
del _pending_structured_output[tool_id]
# Invoke callback
if on_tool_result:
on_tool_result(tool_id, is_error, result_content)
@@ -492,10 +468,7 @@ async def process_sdk_stream(
safe_print(
f"[{context_name}] Invoking agent: {agent_name}{model_info}"
)
elif tool_name == "StructuredOutput":
# Track StructuredOutput submission for fallback
_pending_structured_output[tool_id] = tool_input
else:
elif tool_name != "StructuredOutput":
# Log meaningful tool info (not just tool name)
tool_detail = _get_tool_detail(tool_name, tool_input)
safe_print(f"[{context_name}] {tool_detail}")
@@ -508,9 +481,6 @@ async def process_sdk_stream(
block_type = type(block).__name__
if block_type == "TextBlock" and hasattr(block, "text"):
result_text += block.text
# Track last non-empty text for fallback parsing
if block.text.strip():
last_assistant_text = block.text
# Check for auth/access error returned as AI response text.
# Note: break exits this inner for-loop over msg.content;
# the outer message loop exits via `if stream_error: break`.
@@ -640,15 +610,6 @@ async def process_sdk_stream(
f"[Agent:{agent_name}] {status}: {result_preview}{'...' if len(str(result_content)) > 600 else ''}"
)
# Capture validated StructuredOutput tool data as fallback
if tool_id in _pending_structured_output:
if not is_error:
_pending_structured_output[tool_id][
"_validated"
] = True
else:
del _pending_structured_output[tool_id]
# Invoke callback
if on_tool_result:
on_tool_result(tool_id, is_error, result_content)
@@ -679,19 +640,6 @@ async def process_sdk_stream(
safe_print(f"[{context_name}] Session ended. Total messages: {msg_count}")
# Fallback: if ResultMessage didn't carry structured_output, use validated
# StructuredOutput tool data (the agent submitted valid JSON but the
# ResultMessage didn't propagate it — observed in parallel sessions).
if structured_output is None and _pending_structured_output:
for tid, data in _pending_structured_output.items():
if data.pop("_validated", False):
structured_output = data
safe_print(
f"[{context_name}] Using StructuredOutput tool fallback "
f"(ResultMessage did not carry structured_output)"
)
break
# Set error flag if tool concurrency error was detected
if detected_concurrency_error and not stream_error:
stream_error = "tool_use_concurrency_error"
@@ -699,16 +647,11 @@ async def process_sdk_stream(
f"[{context_name}] Tool use concurrency error detected - caller should retry"
)
# Categorize error as recoverable (fallback possible) vs fatal
error_recoverable = stream_error in RECOVERABLE_ERRORS if stream_error else False
return {
"result_text": result_text,
"last_assistant_text": last_assistant_text,
"structured_output": structured_output,
"agents_invoked": agents_invoked,
"msg_count": msg_count,
"subagent_tool_ids": subagent_tool_ids,
"error": stream_error,
"error_recoverable": error_recoverable,
}
@@ -1,199 +0,0 @@
"""
Split Engine
============
AI-powered issue decomposition: analyzes a single GitHub issue and suggests
how to split it into smaller, well-scoped sub-issues.
"""
from __future__ import annotations
import json
import re
from pathlib import Path
try:
from ...phase_config import get_model_betas, resolve_model_id
from ..models import GitHubRunnerConfig
from .engine_base import EngineBase
from .prompt_manager import PromptManager
except (ImportError, ValueError, SystemError):
from models import GitHubRunnerConfig
from phase_config import get_model_betas, resolve_model_id
from services.engine_base import EngineBase
from services.prompt_manager import PromptManager
class SplitEngine(EngineBase):
"""Handles issue split suggestion via AI."""
def __init__(
self,
project_dir: Path,
github_dir: Path,
config: GitHubRunnerConfig,
progress_callback=None,
):
super().__init__(project_dir, github_dir, config, progress_callback)
self.prompt_manager = PromptManager()
async def suggest_split(self, issue: dict) -> dict:
"""
Analyze an issue and suggest how to split it into sub-issues.
Returns dict matching the frontend SplitSuggestion interface:
{
issueNumber, subIssues: [{title, body, labels}], rationale, confidence
}
"""
from core.client import create_client
issue_number = issue["number"]
self._report_progress(
"analyzing",
20,
f"Analyzing issue #{issue_number} for splitting...",
issue_number=issue_number,
)
context = self._build_split_context(issue)
prompt = self._get_split_prompt() + "\n\n---\n\n" + context
model_shorthand = self.config.model or "sonnet"
model = resolve_model_id(model_shorthand)
betas = get_model_betas(model_shorthand)
client = create_client(
project_dir=self.project_dir,
spec_dir=self.github_dir,
model=model,
agent_type="qa_reviewer",
betas=betas,
fast_mode=self.config.fast_mode,
)
try:
async with client:
await client.query(prompt)
response_text = ""
async for msg in client.receive_response():
msg_type = type(msg).__name__
if msg_type == "AssistantMessage" and hasattr(msg, "content"):
for block in msg.content:
block_type = type(block).__name__
if block_type == "TextBlock" and hasattr(block, "text"):
response_text += block.text
self._report_progress(
"suggesting",
80,
"Parsing split suggestion...",
issue_number=issue_number,
)
return self._parse_split_result(issue_number, response_text)
except Exception as e:
print(f"Split error for #{issue_number}: {e}")
return {
"issueNumber": issue_number,
"subIssues": [],
"rationale": f"Analysis failed: {e}",
"confidence": 0.0,
}
def _build_split_context(self, issue: dict) -> str:
"""Build context string for split analysis."""
labels = ", ".join(label["name"] for label in issue.get("labels", [])) or "None"
lines = [
f"## Issue #{issue['number']}",
f"**Title:** {issue['title']}",
f"**Author:** {issue.get('author', {}).get('login', 'unknown')}",
f"**Labels:** {labels}",
"",
"### Body",
issue.get("body", "No description provided.") or "No description provided.",
"",
]
return "\n".join(lines)
def _get_split_prompt(self) -> str:
"""Get the split suggestion prompt."""
prompt_file = self.prompt_manager.prompts_dir / "issue_split.md"
if prompt_file.exists():
return prompt_file.read_text(encoding="utf-8")
return self._get_default_split_prompt()
@staticmethod
def _get_default_split_prompt() -> str:
"""Default split suggestion prompt."""
return """# Issue Split Agent
You are an issue decomposition assistant. Analyze the GitHub issue and suggest
how to break it down into smaller, well-scoped sub-issues.
Guidelines:
- Each sub-issue should be independently implementable
- Sub-issues should have clear scope boundaries
- Aim for 2-5 sub-issues (no more than 8)
- Each sub-issue should have a descriptive title, detailed body, and relevant labels
- The body should reference the parent issue number
- Preserve the original issue's labels where relevant
Output ONLY a JSON block:
```json
{
"subIssues": [
{
"title": "Descriptive sub-issue title",
"body": "Detailed description of what this sub-issue covers.\\n\\nPart of #PARENT_NUMBER.",
"labels": ["type:feature", "component:frontend"]
}
],
"rationale": "Why this issue should be split and how the sub-issues relate",
"confidence": 0.85
}
```
"""
@staticmethod
def _parse_split_result(issue_number: int, response_text: str) -> dict:
"""Parse split suggestion from AI response."""
default = {
"issueNumber": issue_number,
"subIssues": [],
"rationale": "",
"confidence": 0.0,
}
try:
json_match = re.search(
r"```json\s*(\{.*?\})\s*```", response_text, re.DOTALL
)
if json_match:
data = json.loads(json_match.group(1))
sub_issues = []
for sub in data.get("subIssues", []):
sub_issues.append(
{
"title": sub.get("title", ""),
"body": sub.get("body", ""),
"labels": sub.get("labels", []),
}
)
return {
"issueNumber": issue_number,
"subIssues": sub_issues,
"rationale": data.get("rationale", ""),
"confidence": float(data.get("confidence", 0.5)),
}
except (json.JSONDecodeError, KeyError, ValueError) as e:
print(f"Failed to parse split result: {e}")
return default
+1 -3
View File
@@ -254,7 +254,7 @@ class MockGitHubClient:
raise Exception(f"Issue #{issue_number} not found")
return self.issues[issue_number]
async def issue_comment(self, issue_number: int, body: str) -> int:
async def issue_comment(self, issue_number: int, body: str) -> None:
self._log_call("issue_comment", issue_number=issue_number)
self.posted_comments.append(
{
@@ -262,8 +262,6 @@ class MockGitHubClient:
"body": body,
}
)
# Return a fake comment ID for testing
return 123456 + issue_number
async def issue_add_labels(self, issue_number: int, labels: list[str]) -> None:
self._log_call("issue_add_labels", issue_number=issue_number, labels=labels)
+543
View File
@@ -0,0 +1,543 @@
"""
Trust Escalation Model
======================
Progressive trust system that unlocks more autonomous actions as accuracy improves:
- L0: Review-only (comment, no actions)
- L1: Auto-apply labels based on triage
- L2: Auto-close duplicates and spam
- L3: Auto-merge trivial fixes (docs, typos)
- L4: Full auto-fix with merge
Trust increases with accuracy, decreases with overrides.
"""
from __future__ import annotations
import json
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import IntEnum
from pathlib import Path
from typing import Any
class TrustLevel(IntEnum):
"""Trust levels with increasing autonomy."""
L0_REVIEW_ONLY = 0 # Comment only, no actions
L1_LABEL = 1 # Auto-apply labels
L2_CLOSE = 2 # Auto-close duplicates/spam
L3_MERGE_TRIVIAL = 3 # Auto-merge trivial fixes
L4_FULL_AUTO = 4 # Full autonomous operation
@property
def display_name(self) -> str:
names = {
0: "Review Only",
1: "Auto-Label",
2: "Auto-Close",
3: "Auto-Merge Trivial",
4: "Full Autonomous",
}
return names.get(self.value, "Unknown")
@property
def description(self) -> str:
descriptions = {
0: "AI can comment with suggestions but takes no actions",
1: "AI can automatically apply labels based on triage",
2: "AI can auto-close clear duplicates and spam",
3: "AI can auto-merge trivial changes (docs, typos, formatting)",
4: "AI can auto-fix issues and merge PRs autonomously",
}
return descriptions.get(self.value, "")
@property
def allowed_actions(self) -> set[str]:
"""Actions allowed at this trust level."""
actions = {
0: {"comment", "review"},
1: {"comment", "review", "label", "triage"},
2: {
"comment",
"review",
"label",
"triage",
"close_duplicate",
"close_spam",
},
3: {
"comment",
"review",
"label",
"triage",
"close_duplicate",
"close_spam",
"merge_trivial",
},
4: {
"comment",
"review",
"label",
"triage",
"close_duplicate",
"close_spam",
"merge_trivial",
"auto_fix",
"merge",
},
}
return actions.get(self.value, set())
def can_perform(self, action: str) -> bool:
"""Check if this trust level allows an action."""
return action in self.allowed_actions
# Thresholds for trust level upgrades
TRUST_THRESHOLDS = {
TrustLevel.L1_LABEL: {
"min_actions": 20,
"min_accuracy": 0.90,
"min_days": 3,
},
TrustLevel.L2_CLOSE: {
"min_actions": 50,
"min_accuracy": 0.92,
"min_days": 7,
},
TrustLevel.L3_MERGE_TRIVIAL: {
"min_actions": 100,
"min_accuracy": 0.95,
"min_days": 14,
},
TrustLevel.L4_FULL_AUTO: {
"min_actions": 200,
"min_accuracy": 0.97,
"min_days": 30,
},
}
@dataclass
class AccuracyMetrics:
"""Tracks accuracy metrics for trust calculation."""
total_actions: int = 0
correct_actions: int = 0
overridden_actions: int = 0
last_action_at: str | None = None
first_action_at: str | None = None
# Per-action type metrics
review_total: int = 0
review_correct: int = 0
label_total: int = 0
label_correct: int = 0
triage_total: int = 0
triage_correct: int = 0
close_total: int = 0
close_correct: int = 0
merge_total: int = 0
merge_correct: int = 0
fix_total: int = 0
fix_correct: int = 0
@property
def accuracy(self) -> float:
"""Overall accuracy rate."""
if self.total_actions == 0:
return 0.0
return self.correct_actions / self.total_actions
@property
def override_rate(self) -> float:
"""Rate of overridden actions."""
if self.total_actions == 0:
return 0.0
return self.overridden_actions / self.total_actions
@property
def days_active(self) -> int:
"""Days since first action."""
if not self.first_action_at:
return 0
first = datetime.fromisoformat(self.first_action_at)
now = datetime.now(timezone.utc)
return (now - first).days
def record_action(
self,
action_type: str,
correct: bool,
overridden: bool = False,
) -> None:
"""Record an action outcome."""
now = datetime.now(timezone.utc).isoformat()
self.total_actions += 1
if correct:
self.correct_actions += 1
if overridden:
self.overridden_actions += 1
self.last_action_at = now
if not self.first_action_at:
self.first_action_at = now
# Update per-type metrics
type_map = {
"review": ("review_total", "review_correct"),
"label": ("label_total", "label_correct"),
"triage": ("triage_total", "triage_correct"),
"close": ("close_total", "close_correct"),
"merge": ("merge_total", "merge_correct"),
"fix": ("fix_total", "fix_correct"),
}
if action_type in type_map:
total_attr, correct_attr = type_map[action_type]
setattr(self, total_attr, getattr(self, total_attr) + 1)
if correct:
setattr(self, correct_attr, getattr(self, correct_attr) + 1)
def to_dict(self) -> dict[str, Any]:
return {
"total_actions": self.total_actions,
"correct_actions": self.correct_actions,
"overridden_actions": self.overridden_actions,
"last_action_at": self.last_action_at,
"first_action_at": self.first_action_at,
"review_total": self.review_total,
"review_correct": self.review_correct,
"label_total": self.label_total,
"label_correct": self.label_correct,
"triage_total": self.triage_total,
"triage_correct": self.triage_correct,
"close_total": self.close_total,
"close_correct": self.close_correct,
"merge_total": self.merge_total,
"merge_correct": self.merge_correct,
"fix_total": self.fix_total,
"fix_correct": self.fix_correct,
}
@classmethod
def from_dict(cls, data: dict[str, Any]) -> AccuracyMetrics:
return cls(**{k: v for k, v in data.items() if k in cls.__dataclass_fields__})
@dataclass
class TrustState:
"""Trust state for a repository."""
repo: str
current_level: TrustLevel = TrustLevel.L0_REVIEW_ONLY
metrics: AccuracyMetrics = field(default_factory=AccuracyMetrics)
manual_override: TrustLevel | None = None # User-set override
last_level_change: str | None = None
level_history: list[dict[str, Any]] = field(default_factory=list)
@property
def effective_level(self) -> TrustLevel:
"""Get effective trust level (considers manual override)."""
if self.manual_override is not None:
return self.manual_override
return self.current_level
def can_perform(self, action: str) -> bool:
"""Check if current trust level allows an action."""
return self.effective_level.can_perform(action)
def get_progress_to_next_level(self) -> dict[str, Any]:
"""Get progress toward next trust level."""
current = self.current_level
if current >= TrustLevel.L4_FULL_AUTO:
return {
"next_level": None,
"at_max": True,
}
next_level = TrustLevel(current + 1)
thresholds = TRUST_THRESHOLDS.get(next_level, {})
min_actions = thresholds.get("min_actions", 0)
min_accuracy = thresholds.get("min_accuracy", 0)
min_days = thresholds.get("min_days", 0)
return {
"next_level": next_level.value,
"next_level_name": next_level.display_name,
"at_max": False,
"actions": {
"current": self.metrics.total_actions,
"required": min_actions,
"progress": min(1.0, self.metrics.total_actions / max(1, min_actions)),
},
"accuracy": {
"current": self.metrics.accuracy,
"required": min_accuracy,
"progress": min(1.0, self.metrics.accuracy / max(0.01, min_accuracy)),
},
"days": {
"current": self.metrics.days_active,
"required": min_days,
"progress": min(1.0, self.metrics.days_active / max(1, min_days)),
},
}
def check_upgrade(self) -> TrustLevel | None:
"""Check if eligible for trust level upgrade."""
current = self.current_level
if current >= TrustLevel.L4_FULL_AUTO:
return None
next_level = TrustLevel(current + 1)
thresholds = TRUST_THRESHOLDS.get(next_level)
if not thresholds:
return None
if (
self.metrics.total_actions >= thresholds["min_actions"]
and self.metrics.accuracy >= thresholds["min_accuracy"]
and self.metrics.days_active >= thresholds["min_days"]
):
return next_level
return None
def upgrade_level(self, new_level: TrustLevel, reason: str = "auto") -> None:
"""Upgrade to a new trust level."""
if new_level <= self.current_level:
return
now = datetime.now(timezone.utc).isoformat()
self.level_history.append(
{
"from_level": self.current_level.value,
"to_level": new_level.value,
"reason": reason,
"timestamp": now,
"metrics_snapshot": self.metrics.to_dict(),
}
)
self.current_level = new_level
self.last_level_change = now
def downgrade_level(self, reason: str = "override") -> None:
"""Downgrade trust level due to override or errors."""
if self.current_level <= TrustLevel.L0_REVIEW_ONLY:
return
new_level = TrustLevel(self.current_level - 1)
now = datetime.now(timezone.utc).isoformat()
self.level_history.append(
{
"from_level": self.current_level.value,
"to_level": new_level.value,
"reason": reason,
"timestamp": now,
}
)
self.current_level = new_level
self.last_level_change = now
def set_manual_override(self, level: TrustLevel | None) -> None:
"""Set or clear manual trust level override."""
self.manual_override = level
if level is not None:
now = datetime.now(timezone.utc).isoformat()
self.level_history.append(
{
"from_level": self.current_level.value,
"to_level": level.value,
"reason": "manual_override",
"timestamp": now,
}
)
def to_dict(self) -> dict[str, Any]:
return {
"repo": self.repo,
"current_level": self.current_level.value,
"metrics": self.metrics.to_dict(),
"manual_override": self.manual_override.value
if self.manual_override
else None,
"last_level_change": self.last_level_change,
"level_history": self.level_history[-20:], # Keep last 20 changes
}
@classmethod
def from_dict(cls, data: dict[str, Any]) -> TrustState:
return cls(
repo=data["repo"],
current_level=TrustLevel(data.get("current_level", 0)),
metrics=AccuracyMetrics.from_dict(data.get("metrics", {})),
manual_override=TrustLevel(data["manual_override"])
if data.get("manual_override") is not None
else None,
last_level_change=data.get("last_level_change"),
level_history=data.get("level_history", []),
)
class TrustManager:
"""
Manages trust levels across repositories.
Usage:
trust = TrustManager(state_dir=Path(".auto-claude/github"))
# Check if action is allowed
if trust.can_perform("owner/repo", "auto_fix"):
perform_auto_fix()
# Record action outcome
trust.record_action("owner/repo", "review", correct=True)
# Check for upgrade
if trust.check_and_upgrade("owner/repo"):
print("Trust level upgraded!")
"""
def __init__(self, state_dir: Path):
self.state_dir = state_dir
self.trust_dir = state_dir / "trust"
self.trust_dir.mkdir(parents=True, exist_ok=True)
self._states: dict[str, TrustState] = {}
def _get_state_file(self, repo: str) -> Path:
safe_name = repo.replace("/", "_")
return self.trust_dir / f"{safe_name}.json"
def get_state(self, repo: str) -> TrustState:
"""Get trust state for a repository."""
if repo in self._states:
return self._states[repo]
state_file = self._get_state_file(repo)
if state_file.exists():
try:
with open(state_file, encoding="utf-8") as f:
data = json.load(f)
state = TrustState.from_dict(data)
except (json.JSONDecodeError, UnicodeDecodeError):
# Return default state if file is corrupted
state = TrustState(repo=repo)
else:
state = TrustState(repo=repo)
self._states[repo] = state
return state
def save_state(self, repo: str) -> None:
"""Save trust state for a repository with secure file permissions."""
import os
state = self.get_state(repo)
state_file = self._get_state_file(repo)
# Write with restrictive permissions (0o600 = owner read/write only)
fd = os.open(str(state_file), os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
# os.fdopen takes ownership of fd and will close it when the with block exits
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(state.to_dict(), f, indent=2)
def get_trust_level(self, repo: str) -> TrustLevel:
"""Get current trust level for a repository."""
return self.get_state(repo).effective_level
def can_perform(self, repo: str, action: str) -> bool:
"""Check if an action is allowed for a repository."""
return self.get_state(repo).can_perform(action)
def record_action(
self,
repo: str,
action_type: str,
correct: bool,
overridden: bool = False,
) -> None:
"""Record an action outcome."""
state = self.get_state(repo)
state.metrics.record_action(action_type, correct, overridden)
# Check for downgrade on override
if overridden:
# Downgrade if override rate exceeds 10%
if state.metrics.override_rate > 0.10 and state.metrics.total_actions >= 10:
state.downgrade_level(reason="high_override_rate")
self.save_state(repo)
def check_and_upgrade(self, repo: str) -> bool:
"""Check for and apply trust level upgrade."""
state = self.get_state(repo)
new_level = state.check_upgrade()
if new_level:
state.upgrade_level(new_level, reason="threshold_met")
self.save_state(repo)
return True
return False
def set_manual_level(self, repo: str, level: TrustLevel) -> None:
"""Manually set trust level for a repository."""
state = self.get_state(repo)
state.set_manual_override(level)
self.save_state(repo)
def clear_manual_override(self, repo: str) -> None:
"""Clear manual trust level override."""
state = self.get_state(repo)
state.set_manual_override(None)
self.save_state(repo)
def get_progress(self, repo: str) -> dict[str, Any]:
"""Get progress toward next trust level."""
state = self.get_state(repo)
return {
"current_level": state.effective_level.value,
"current_level_name": state.effective_level.display_name,
"is_manual_override": state.manual_override is not None,
"accuracy": state.metrics.accuracy,
"total_actions": state.metrics.total_actions,
"override_rate": state.metrics.override_rate,
"days_active": state.metrics.days_active,
"progress_to_next": state.get_progress_to_next_level(),
}
def get_all_states(self) -> list[TrustState]:
"""Get trust states for all repos."""
states = []
for file in self.trust_dir.glob("*.json"):
try:
with open(file, encoding="utf-8") as f:
data = json.load(f)
states.append(TrustState.from_dict(data))
except (json.JSONDecodeError, UnicodeDecodeError):
# Skip corrupted state files
continue
return states
def get_summary(self) -> dict[str, Any]:
"""Get summary of trust across all repos."""
states = self.get_all_states()
by_level = {}
for state in states:
level = state.effective_level.value
by_level[level] = by_level.get(level, 0) + 1
total_actions = sum(s.metrics.total_actions for s in states)
total_correct = sum(s.metrics.correct_actions for s in states)
return {
"total_repos": len(states),
"by_level": by_level,
"total_actions": total_actions,
"overall_accuracy": total_correct / max(1, total_actions),
}
+13 -60
View File
@@ -14,19 +14,12 @@ Key Features:
"""
import json
import logging
import subprocess
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from datetime import datetime
from enum import Enum
from pathlib import Path
# Recovery manager configuration
ATTEMPT_WINDOW_SECONDS = 7200 # Only count attempts within last 2 hours
MAX_ATTEMPT_HISTORY_PER_SUBTASK = 50 # Cap stored attempts per subtask
logger = logging.getLogger(__name__)
class FailureType(Enum):
"""Types of failures that can occur during autonomous builds."""
@@ -89,8 +82,8 @@ class RecoveryManager:
"subtasks": {},
"stuck_subtasks": [],
"metadata": {
"created_at": datetime.now(timezone.utc).isoformat(),
"last_updated": datetime.now(timezone.utc).isoformat(),
"created_at": datetime.now().isoformat(),
"last_updated": datetime.now().isoformat(),
},
}
with open(self.attempt_history_file, "w", encoding="utf-8") as f:
@@ -102,8 +95,8 @@ class RecoveryManager:
"commits": [],
"last_good_commit": None,
"metadata": {
"created_at": datetime.now(timezone.utc).isoformat(),
"last_updated": datetime.now(timezone.utc).isoformat(),
"created_at": datetime.now().isoformat(),
"last_updated": datetime.now().isoformat(),
},
}
with open(self.build_commits_file, "w", encoding="utf-8") as f:
@@ -121,7 +114,7 @@ class RecoveryManager:
def _save_attempt_history(self, data: dict) -> None:
"""Save attempt history to JSON file."""
data["metadata"]["last_updated"] = datetime.now(timezone.utc).isoformat()
data["metadata"]["last_updated"] = datetime.now().isoformat()
with open(self.attempt_history_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
@@ -137,7 +130,7 @@ class RecoveryManager:
def _save_build_commits(self, data: dict) -> None:
"""Save build commits to JSON file."""
data["metadata"]["last_updated"] = datetime.now(timezone.utc).isoformat()
data["metadata"]["last_updated"] = datetime.now().isoformat()
with open(self.build_commits_file, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
@@ -192,44 +185,17 @@ class RecoveryManager:
def get_attempt_count(self, subtask_id: str) -> int:
"""
Get how many times this subtask has been attempted within the time window.
Only counts attempts within ATTEMPT_WINDOW_SECONDS (default: 2 hours).
This prevents unbounded accumulation across crash/restart cycles.
Get how many times this subtask has been attempted.
Args:
subtask_id: ID of the subtask
Returns:
Number of attempts within the time window
Number of attempts
"""
history = self._load_attempt_history()
subtask_data = history["subtasks"].get(subtask_id, {})
attempts = subtask_data.get("attempts", [])
# Calculate cutoff time for the window
cutoff_time = datetime.now(timezone.utc) - timedelta(
seconds=ATTEMPT_WINDOW_SECONDS
)
# For backward compatibility with naive timestamps, also create naive cutoff
cutoff_time_naive = datetime.now() - timedelta(seconds=ATTEMPT_WINDOW_SECONDS)
# Count only attempts within the time window
recent_count = 0
for attempt in attempts:
try:
attempt_time = datetime.fromisoformat(attempt["timestamp"])
# Use appropriate cutoff based on whether timestamp is naive or aware
cutoff = (
cutoff_time_naive if attempt_time.tzinfo is None else cutoff_time
)
if attempt_time >= cutoff:
recent_count += 1
except (KeyError, ValueError):
# If timestamp is missing or invalid, count it (backward compatibility)
recent_count += 1
return recent_count
return len(subtask_data.get("attempts", []))
def record_attempt(
self,
@@ -242,8 +208,6 @@ class RecoveryManager:
"""
Record an attempt at a subtask.
Automatically trims old attempts if the history exceeds MAX_ATTEMPT_HISTORY_PER_SUBTASK.
Args:
subtask_id: ID of the subtask
session: Session number
@@ -260,24 +224,13 @@ class RecoveryManager:
# Add the attempt
attempt = {
"session": session,
"timestamp": datetime.now(timezone.utc).isoformat(),
"timestamp": datetime.now().isoformat(),
"approach": approach,
"success": success,
"error": error,
}
history["subtasks"][subtask_id]["attempts"].append(attempt)
# Hard cap: trim oldest attempts if we exceed the maximum
attempts = history["subtasks"][subtask_id]["attempts"]
if len(attempts) > MAX_ATTEMPT_HISTORY_PER_SUBTASK:
trimmed_count = len(attempts) - MAX_ATTEMPT_HISTORY_PER_SUBTASK
history["subtasks"][subtask_id]["attempts"] = attempts[
-MAX_ATTEMPT_HISTORY_PER_SUBTASK:
]
logger.debug(
f"Trimmed {trimmed_count} old attempts for subtask {subtask_id} (cap: {MAX_ATTEMPT_HISTORY_PER_SUBTASK})"
)
# Update status
if success:
history["subtasks"][subtask_id]["status"] = "completed"
@@ -452,7 +405,7 @@ class RecoveryManager:
commit_record = {
"hash": commit_hash,
"subtask_id": subtask_id,
"timestamp": datetime.now(timezone.utc).isoformat(),
"timestamp": datetime.now().isoformat(),
}
commits["commits"].append(commit_record)
@@ -497,7 +450,7 @@ class RecoveryManager:
stuck_entry = {
"subtask_id": subtask_id,
"reason": reason,
"escalated_at": datetime.now(timezone.utc).isoformat(),
"escalated_at": datetime.now().isoformat(),
"attempt_count": self.get_attempt_count(subtask_id),
}
@@ -1,270 +0,0 @@
"""Integration test for image support in issue investigations.
Tests verify:
1. extract_image_urls function extracts URLs from markdown and HTML
2. _build_issue_context includes image URLs in the issue context
3. Image limit of 20 is enforced
4. Images from comments are included
These tests execute actual code, not static analysis.
"""
import re
from pathlib import Path
import pytest
# =============================================================================
# Direct code execution to avoid complex import issues
# =============================================================================
_SOURCE_FILE = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "issue_investigation_orchestrator.py"
def _load_extract_image_urls():
"""Load and execute the extract_image_urls function from source."""
with open(_SOURCE_FILE, "r") as f:
content = f.read()
# Find and extract the extract_image_urls function
# The function definition starts at "def extract_image_urls"
start_idx = content.index("def extract_image_urls(")
# Find the end of the function (next def or end of file)
# We need to find the matching indentation
lines = content[start_idx:].split('\n')
func_lines = []
for i, line in enumerate(lines):
func_lines.append(line)
# Stop at the next top-level definition
if i > 0 and line and not line[0].isspace() and line.startswith('def ') or line.startswith('class ') or line.startswith('async def '):
if i > 0: # Don't stop at the first line (the function def itself)
func_lines.pop() # Remove the line we just added
break
# Also need the _IMAGE_URL_PATTERNS constant
patterns_start = content.index("_IMAGE_URL_PATTERNS = [")
patterns_lines = []
for i, line in enumerate(content[patterns_start:].split('\n')):
patterns_lines.append(line)
if line.strip() == ']':
break
# Execute the patterns definition (need 're' module for regex patterns)
namespace = {'re': re}
exec('\n'.join(patterns_lines), namespace)
exec('\n'.join(func_lines), namespace)
return namespace['extract_image_urls']
def _load_build_issue_context_method():
"""Load and execute the _build_issue_context method from source."""
with open(_SOURCE_FILE, "r") as f:
content = f.read()
# Find the _build_issue_context method
start_idx = content.index(" def _build_issue_context(")
# Find the end of the method (next method definition at same indentation)
lines = content[start_idx:].split('\n')
method_lines = []
for i, line in enumerate(lines):
method_lines.append(line)
# Stop at the next method definition (same indent level)
if i > 0 and line.startswith(' def ') and not line.startswith(' def _build_issue_context'):
method_lines.pop() # Remove the line we just added
break
# Strip the 4-space indentation from each line
dedented_lines = [line[4:] if line.startswith(' ') else line for line in method_lines]
# We also need extract_image_urls
extract_image_urls = _load_extract_image_urls()
# Create a mock class to test the method
namespace = {
'extract_image_urls': extract_image_urls,
'Path': Path,
}
# Execute the method (dedented)
exec('\n'.join(dedented_lines), namespace)
return namespace['_build_issue_context']
# =============================================================================
# Tests for extract_image_urls
# =============================================================================
def test_extract_image_urls_markdown():
"""Test extracting image URLs from markdown syntax."""
extract_image_urls = _load_extract_image_urls()
text = """
Bug in the login screen!
![Screenshot](https://github.com/example/repo/assets/123/screenshot.png)
The button doesn't work.
"""
urls = extract_image_urls(text)
assert len(urls) == 1
assert urls[0] == "https://github.com/example/repo/assets/123/screenshot.png"
def test_extract_image_urls_html():
"""Test extracting image URLs from HTML img tags."""
extract_image_urls = _load_extract_image_urls()
text = 'Check this: <img src="https://example.com/test.jpg" />'
urls = extract_image_urls(text)
assert len(urls) == 1
assert urls[0] == "https://example.com/test.jpg"
def test_extract_image_urls_deduplication():
"""Test that duplicate URLs are deduplicated."""
extract_image_urls = _load_extract_image_urls()
text = """
![Img1](https://example.com/image.png)
![Img2](https://example.com/image.png)
![Img3](https://example.com/other.png)
"""
urls = extract_image_urls(text)
assert len(urls) == 2
assert "https://example.com/image.png" in urls
assert "https://example.com/other.png" in urls
def test_extract_image_urls_empty():
"""Test that empty text returns empty list."""
extract_image_urls = _load_extract_image_urls()
assert extract_image_urls("") == []
assert extract_image_urls("No images here!") == []
def test_extract_image_urls_multiple():
"""Test extracting multiple image URLs."""
extract_image_urls = _load_extract_image_urls()
text = """
![First](https://example.com/first.png)
![Second](https://example.com/second.jpg)
<img src='https://example.com/third.gif' />
"""
urls = extract_image_urls(text)
assert len(urls) == 3
assert "https://example.com/first.png" in urls
assert "https://example.com/second.jpg" in urls
assert "https://example.com/third.gif" in urls
# =============================================================================
# Tests for _build_issue_context
# =============================================================================
def test_issue_context_includes_images(tmp_path: Path):
"""Test that _build_issue_context includes image URLs."""
_build_issue_context = _load_build_issue_context_method()
# Mock _get_recent_commits to avoid git operations
class MockOrchestrator:
def _get_recent_commits(self, project_root, max_count):
return ""
issue_body = """
Bug in the login screen!
![Screenshot](https://github.com/example/repo/assets/123/screenshot.png)
The button doesn't work.
"""
context = _build_issue_context(
MockOrchestrator(),
issue_number=42,
issue_title="Login bug",
issue_body=issue_body,
issue_labels=["bug", "ui"],
issue_comments=[],
project_root=Path("/tmp/test"),
)
# Verify image URL is in context
assert "https://github.com/example/repo/assets/123/screenshot.png" in context
assert "### Images (1 found)" in context
# Verify the Images section contains a clean URL list (markdown bullet point)
assert "- https://github.com/example/repo/assets/123/screenshot.png" in context
def test_issue_context_limits_images(tmp_path: Path):
"""Test that only first 20 images are included in the Images section."""
_build_issue_context = _load_build_issue_context_method()
class MockOrchestrator:
def _get_recent_commits(self, project_root, max_count):
return ""
# Generate 25 image URLs
many_images = "\n".join(
f"![Img{i}](https://example.com/image{i}.png)"
for i in range(25)
)
context = _build_issue_context(
MockOrchestrator(),
issue_number=1,
issue_title="Many images",
issue_body=many_images,
issue_labels=[],
issue_comments=[],
project_root=Path("/tmp/test"),
)
# Should mention 25 found but only list first 20
assert "### Images (25 found)" in context
# Count bullet-point URLs in the Images section (should be exactly 20)
# Each URL in the Images section is formatted as "- https://..."
import re
bullet_urls = re.findall(r'^- (https://example\.com/image\d+\.png)', context, re.MULTILINE)
assert len(bullet_urls) == 20
def test_extract_image_urls_from_comments(tmp_path: Path):
"""Test that images from comments are included."""
_build_issue_context = _load_build_issue_context_method()
class MockOrchestrator:
def _get_recent_commits(self, project_root, max_count):
return ""
issue_body = "No images in body"
comments = [
"Check this: ![Screenshot](https://example.com/comment1.png)",
"And this: <img src='https://example.com/comment2.jpg' />",
]
context = _build_issue_context(
MockOrchestrator(),
issue_number=2,
issue_title="Comment images",
issue_body=issue_body,
issue_labels=[],
issue_comments=comments,
project_root=Path("/tmp/test"),
)
assert "https://example.com/comment1.png" in context
assert "https://example.com/comment2.jpg" in context
@@ -1,232 +0,0 @@
"""
Integration tests for Opus 4.6 features in issue investigation.
Tests verify runtime behavior:
1. Per-specialist max_tokens configuration is correct (runtime execution)
2. fast_mode parameter is passed through to create_client (runtime execution)
These tests execute actual code, not static analysis.
"""
import sys
from pathlib import Path
from unittest.mock import AsyncMock, MagicMock
import pytest
class TestSpecialistMaxTokensConfiguration:
"""Test per-specialist max_tokens configuration using runtime execution."""
def test_specialist_max_tokens_constant_exists(self):
"""Verify SPECIALIST_MAX_TOKENS constant can be executed and has correct type."""
# Execute the constant definition directly
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "issue_investigation_orchestrator.py"
# Read and execute just the constant definition
with open(source_file, "r") as f:
content = f.read()
# Execute the SPECIALIST_MAX_TOKENS definition in a clean namespace
namespace = {}
# Find and execute just the constant definition
for line in content.split('\n'):
if 'SPECIALIST_MAX_TOKENS = {' in line:
# Found the start, now find the end
start_idx = content.index('SPECIALIST_MAX_TOKENS = {')
# Find the matching closing brace
brace_count = 0
in_dict = False
end_idx = start_idx
for i, char in enumerate(content[start_idx:], start=start_idx):
if char == '{':
brace_count += 1
in_dict = True
elif char == '}':
brace_count -= 1
if brace_count == 0 and in_dict:
end_idx = i + 1
break
# Execute the constant definition
exec(content[start_idx:end_idx], namespace)
break
# This is a runtime execution - if the constant doesn't exist, this fails
assert 'SPECIALIST_MAX_TOKENS' in namespace
assert isinstance(namespace['SPECIALIST_MAX_TOKENS'], dict)
assert len(namespace['SPECIALIST_MAX_TOKENS']) > 0
def test_specialist_max_tokens_values(self):
"""Verify per-specialist max_tokens are configured correctly at runtime."""
# Execute the constant definition directly
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "issue_investigation_orchestrator.py"
with open(source_file, "r") as f:
content = f.read()
# Execute the SPECIALIST_MAX_TOKENS definition in a clean namespace
namespace = {}
# Find and execute just the constant definition
start_idx = content.index('SPECIALIST_MAX_TOKENS = {')
# Find the matching closing brace
brace_count = 0
in_dict = False
end_idx = start_idx
for i, char in enumerate(content[start_idx:], start=start_idx):
if char == '{':
brace_count += 1
in_dict = True
elif char == '}':
brace_count -= 1
if brace_count == 0 and in_dict:
end_idx = i + 1
break
# Execute the constant definition
exec(content[start_idx:end_idx], namespace)
# Verify the actual runtime values
assert "root_cause" in namespace['SPECIALIST_MAX_TOKENS']
assert namespace['SPECIALIST_MAX_TOKENS']["root_cause"] == 127999
assert "impact" in namespace['SPECIALIST_MAX_TOKENS']
assert namespace['SPECIALIST_MAX_TOKENS']["impact"] == 63999
assert "fix_advisor" in namespace['SPECIALIST_MAX_TOKENS']
assert namespace['SPECIALIST_MAX_TOKENS']["fix_advisor"] == 63999
assert "reproducer" in namespace['SPECIALIST_MAX_TOKENS']
assert namespace['SPECIALIST_MAX_TOKENS']["reproducer"] == 63999
def test_all_specialists_have_max_tokens(self):
"""Verify all investigation specialists have max_tokens configured."""
# Execute both constant definitions
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "issue_investigation_orchestrator.py"
with open(source_file, "r") as f:
content = f.read()
# Execute the SPECIALIST_MAX_TOKENS definition
namespace = {}
start_idx = content.index('SPECIALIST_MAX_TOKENS = {')
brace_count = 0
in_dict = False
end_idx = start_idx
for i, char in enumerate(content[start_idx:], start=start_idx):
if char == '{':
brace_count += 1
in_dict = True
elif char == '}':
brace_count -= 1
if brace_count == 0 and in_dict:
end_idx = i + 1
break
exec(content[start_idx:end_idx], namespace)
# Parse INVESTIGATION_SPECIALISTS to get the names
# Find SpecialistConfig calls
import re
specialist_names = []
for match in re.finditer(r'SpecialistConfig\(\s*name="([^"]+)"', content):
specialist_names.append(match.group(1))
# All specialists should have max_tokens configured
configured_names = set(namespace['SPECIALIST_MAX_TOKENS'].keys())
for name in specialist_names:
assert name in configured_names, f"Missing max_tokens config for: {name}"
class TestFastModeParameterPassing:
"""Test fast_mode parameter is passed through to agents at runtime."""
def test_github_runner_config_has_fast_mode(self):
"""Verify GitHubRunnerConfig has fast_mode field with correct default."""
# Just check that fast_mode exists in the source code
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "models.py"
with open(source_file, "r") as f:
content = f.read()
# Verify fast_mode field exists in GitHubRunnerConfig
assert 'fast_mode: bool = False' in content or 'fast_mode: bool' in content, "fast_mode field not found in GitHubRunnerConfig"
def test_parallel_agent_base_has_fast_mode_parameter(self):
"""Verify ParallelAgentOrchestrator stores and passes fast_mode from config."""
# Read the source file and verify fast_mode is used
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "parallel_agent_base.py"
with open(source_file, "r") as f:
content = f.read()
# Verify that ParallelAgentOrchestrator.__init__ accepts config with fast_mode
assert 'def __init__' in content, "ParallelAgentOrchestrator.__init__ not found"
assert 'self.config = config' in content, "ParallelAgentOrchestrator does not store config"
# Verify fast_mode is passed to create_client in client_kwargs
assert '"fast_mode": self.config.fast_mode' in content, "fast_mode not passed to create_client"
def test_parallel_agent_base_has_fast_mode_in_client_kwargs(self):
"""Verify ParallelAgentOrchestrator includes fast_mode in client_kwargs."""
# Read the source file and verify fast_mode is in the client_kwargs construction
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "parallel_agent_base.py"
with open(source_file, "r") as f:
content = f.read()
# This is a runtime verification that the code exists
# We're not parsing AST, just checking the actual code that would be executed
assert 'client_kwargs:' in content, "client_kwargs not found in parallel_agent_base.py"
assert '"fast_mode": self.config.fast_mode' in content, "fast_mode not passed to create_client"
class TestSpecialistTokenBudgetResolution:
"""Test that specialist token budgets are resolved correctly at runtime."""
def test_specialist_max_tokens_constants(self):
"""Verify SPECIALIST_MAX_TOKENS has the expected runtime values."""
# Execute the constant definition directly
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "issue_investigation_orchestrator.py"
with open(source_file, "r") as f:
content = f.read()
# Execute the SPECIALIST_MAX_TOKENS definition
namespace = {}
start_idx = content.index('SPECIALIST_MAX_TOKENS = {')
brace_count = 0
in_dict = False
end_idx = start_idx
for i, char in enumerate(content[start_idx:], start=start_idx):
if char == '{':
brace_count += 1
in_dict = True
elif char == '}':
brace_count -= 1
if brace_count == 0 and in_dict:
end_idx = i + 1
break
exec(content[start_idx:end_idx], namespace)
# This is a runtime execution - we're checking actual values, not parsing files
assert namespace['SPECIALIST_MAX_TOKENS']["root_cause"] == 127999
assert namespace['SPECIALIST_MAX_TOKENS']["impact"] == 63999
assert namespace['SPECIALIST_MAX_TOKENS']["fix_advisor"] == 63999
assert namespace['SPECIALIST_MAX_TOKENS']["reproducer"] == 63999
def test_resolve_specialist_uses_max_tokens(self):
"""Verify _resolve_specialist function uses SPECIALIST_MAX_TOKENS."""
# Read the source and verify the logic
source_file = Path(__file__).parent.parent.parent / "runners" / "github" / "services" / "issue_investigation_orchestrator.py"
with open(source_file, "r") as f:
content = f.read()
# Verify the _resolve_specialist function exists and uses SPECIALIST_MAX_TOKENS
assert 'def _resolve_specialist(' in content, "_resolve_specialist function not found"
assert 'SPECIALIST_MAX_TOKENS.get(' in content, "_resolve_specialist does not use SPECIALIST_MAX_TOKENS.get()"
assert 'get_thinking_budget(' in content, "_resolve_specialist does not use get_thinking_budget()"
if __name__ == "__main__":
pytest.main([__file__, "-v"])
-1665
View File
File diff suppressed because it is too large Load Diff
+11 -15
View File
@@ -24,10 +24,9 @@
"recommended": true,
"noNodejsModules": "off",
"useImportExtensions": "off",
"noUnusedFunctionParameters": "off",
"noUnusedVariables": "off",
"useExhaustiveDependencies": "off",
"noInvalidUseBeforeDeclaration": "off"
"noUnusedFunctionParameters": "warn",
"noUnusedVariables": "warn",
"useExhaustiveDependencies": "warn"
},
"security": {
"recommended": true,
@@ -46,8 +45,7 @@
"noProcessEnv": "off",
"useNodejsImportProtocol": "off",
"useImportType": "off",
"useTemplate": "off",
"noNonNullAssertion": "off"
"useTemplate": "off"
},
"suspicious": {
"recommended": true,
@@ -55,16 +53,14 @@
"noEmptyBlockStatements": "warn",
"noAssignInExpressions": "warn",
"useAwait": "off",
"noExplicitAny": "off",
"noImplicitAnyLet": "off",
"noExplicitAny": "warn",
"noImplicitAnyLet": "warn",
"useIterableCallbackReturn": "off",
"noControlCharactersInRegex": "off",
"noArrayIndexKey": "off",
"noShadowRestrictedNames": "off",
"noRedeclare": "off",
"noSelfCompare": "off",
"noFocusedTests": "off",
"noUselessEscapeInString": "off"
"noControlCharactersInRegex": "warn",
"noArrayIndexKey": "warn",
"noShadowRestrictedNames": "warn",
"noRedeclare": "warn",
"noSelfCompare": "warn"
}
}
},
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "auto-claude-ui",
"version": "2.7.6-beta.5",
"version": "2.7.6-beta.3",
"type": "module",
"description": "Desktop UI for Auto Claude autonomous coding framework",
"homepage": "https://github.com/AndyMik90/Auto-Claude",
+2 -1
View File
@@ -1118,7 +1118,8 @@ function validateInput(value, validValues, name) {
// Remove any control characters or newlines (ASCII 0-31 and 127)
// eslint-disable-next-line no-control-regex
const sanitized = String(value).replace(/[\x00-\x1f\x7f]/g, '');
// biome-ignore lint/suspicious/noControlCharactersInRegex: Intentional - sanitizing input by removing control characters
const sanitized = String(value).replace(/[\x00-\x1f\x7f]/g, '');
if (!validValues.includes(sanitized)) {
throw new Error(`Invalid ${name}: "${sanitized}". Valid values: ${validValues.join(', ')}`);
@@ -361,9 +361,13 @@ describe('Subprocess Spawn Integration', () => {
const result = manager.killTask('task-1');
expect(result).toBe(true);
// On Windows, kill() is called without arguments; on Unix, kill('SIGTERM') is used
if (isWindows()) {
expect(mockProcess.kill).toHaveBeenCalled();
} else {
expect(mockProcess.kill).toHaveBeenCalledWith('SIGTERM');
}
expect(manager.isRunning('task-1')).toBe(false);
// Note: killProcessGracefully spawns taskkill on Windows, so mockProcess.kill is not called
// The test verifies the task is removed from tracking (isRunning returns false)
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
it('should return false when killing non-existent task', async () => {
@@ -433,21 +437,16 @@ describe('Subprocess Spawn Integration', () => {
const promise1 = manager.startSpecCreation('task-1', TEST_PROJECT_PATH, 'Test 1');
const promise2 = manager.startTaskExecution('task-2', TEST_PROJECT_PATH, 'spec-001');
// Wait for spawn to complete (ensures listeners are attached)
// Wait for spawn to complete (ensures listeners are attached), then emit exit
await new Promise(resolve => setImmediate(resolve));
mockProcess.emit('exit', 0);
await promise1;
mockProcess.emit('exit', 0);
await promise2;
// Both tasks should be tracked
expect(manager.getRunningTasks()).toHaveLength(2);
// Kill both tasks
await manager.killAll();
// Tasks should be removed from tracking
expect(manager.getRunningTasks()).toHaveLength(0);
// Clean up the promises
mockProcess.emit('exit', 0);
await Promise.allSettled([promise1, promise2]);
}, 10000); // Increase timeout for Windows CI
it('should allow sequential execution of same task', async () => {
@@ -14,7 +14,7 @@ vi.mock('electron', () => ({
}));
vi.mock('../rate-limit-detector', () => ({
getBestAvailableProfileEnv: () => Promise.resolve({
getBestAvailableProfileEnv: () => ({
env: { CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token' },
profileId: 'default',
profileName: 'Default',
@@ -44,7 +44,7 @@ function parseNDJSON(chunk: string, bufferRef: { current: string }): ProgressDat
const progressData = JSON.parse(line);
results.push(progressData);
} catch {
// Skip invalid JSON - allows parser to be resilient to malformed data
// Skip invalid JSON - allows parser to be resilient to malformed data
}
}
});
@@ -1012,115 +1012,3 @@ Please add credits to continue.`;
});
});
});
describe('ensureCleanProfileEnv', () => {
beforeEach(() => {
vi.resetModules();
});
afterEach(() => {
vi.clearAllMocks();
});
describe('with CLAUDE_CONFIG_DIR set', () => {
it('should preserve CLAUDE_CONFIG_DIR while clearing CLAUDE_CODE_OAUTH_TOKEN', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const env = {
CLAUDE_CONFIG_DIR: '/tmp/profile-1',
CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-123',
ANTHROPIC_API_KEY: 'sk-ant-key-456'
};
const result = ensureCleanProfileEnv(env);
expect(result.CLAUDE_CONFIG_DIR).toBe('/tmp/profile-1');
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('');
expect(result.ANTHROPIC_API_KEY).toBe('');
});
it('should preserve other environment variables', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const env = {
CLAUDE_CONFIG_DIR: '/tmp/profile-1',
CLAUDE_CODE_OAUTH_TOKEN: 'token',
ANTHROPIC_API_KEY: 'key',
SOME_OTHER_VAR: 'value'
};
const result = ensureCleanProfileEnv(env);
expect(result.CLAUDE_CONFIG_DIR).toBe('/tmp/profile-1');
expect(result.SOME_OTHER_VAR).toBe('value');
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('');
expect(result.ANTHROPIC_API_KEY).toBe('');
});
it('should clear tokens even if they are not present in input', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const env = {
CLAUDE_CONFIG_DIR: '/tmp/profile-1'
};
const result = ensureCleanProfileEnv(env);
expect(result.CLAUDE_CONFIG_DIR).toBe('/tmp/profile-1');
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('');
expect(result.ANTHROPIC_API_KEY).toBe('');
});
});
describe('without CLAUDE_CONFIG_DIR', () => {
it('should return env unchanged when CLAUDE_CONFIG_DIR is not set', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const env = {
CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-123',
ANTHROPIC_API_KEY: 'sk-ant-key-456'
};
const result = ensureCleanProfileEnv(env);
expect(result).toEqual(env);
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('oauth-token-123');
expect(result.ANTHROPIC_API_KEY).toBe('sk-ant-key-456');
});
});
describe('edge cases', () => {
it('should handle empty profile env', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const result = ensureCleanProfileEnv({});
// Empty env has no CLAUDE_CONFIG_DIR, so should return as-is
expect(result).toEqual({});
});
it('should handle env with empty string CLAUDE_CONFIG_DIR', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const env = {
CLAUDE_CONFIG_DIR: '',
CLAUDE_CODE_OAUTH_TOKEN: 'token'
};
const result = ensureCleanProfileEnv(env);
// Empty string is falsy, so should not trigger clearing
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('token');
});
it('should return a new object when clearing (not mutate input)', async () => {
const { ensureCleanProfileEnv } = await import('../rate-limit-detector');
const env = {
CLAUDE_CONFIG_DIR: '/tmp/profile-1',
CLAUDE_CODE_OAUTH_TOKEN: 'token'
};
const result = ensureCleanProfileEnv(env);
// Original should not be mutated
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBe('token');
expect(result.CLAUDE_CODE_OAUTH_TOKEN).toBe('');
expect(result).not.toBe(env);
});
});
});
@@ -1,373 +0,0 @@
/**
* Tests for unified OAuth + API profile swap logic (Issue #1798)
*
* Tests the core changes that wire the unified swap infrastructure into
* actual execution paths:
* - getBestAvailableUnifiedAccount() in ClaudeProfileManager
* - Removal of isAPIProfile gate in UsageMonitor
* - Spawn-time swap respects active API profile (rate-limit-detector)
* - Swap cooldown to prevent rapid back-and-forth
*/
import { describe, it, expect, vi, beforeEach } from 'vitest';
// --- Shared mock state ---
const mockAPIProfiles = [
{
id: 'api-glm-1',
name: 'GLM API',
baseUrl: 'https://api.z.ai/api/anthropic',
apiKey: 'sk-glm-key-1'
},
{
id: 'api-anthropic-1',
name: 'Anthropic API',
baseUrl: 'https://api.anthropic.com',
apiKey: 'sk-ant-key-1'
}
];
const mockLoadProfilesFile = vi.fn(async () => ({
profiles: [...mockAPIProfiles],
activeProfileId: null as string | null,
version: 1
}));
vi.mock('../services/profile/profile-manager', () => ({
loadProfilesFile: () => mockLoadProfilesFile(),
setActiveAPIProfile: vi.fn()
}));
// Mock profile-scorer to control availability
vi.mock('../claude-profile/profile-scorer', async (importOriginal) => {
const actual = await importOriginal<typeof import('../claude-profile/profile-scorer')>();
return {
...actual,
checkProfileAvailability: vi.fn(() => ({ available: true }))
};
});
// Mock profile-utils with importOriginal to keep CLAUDE_PROFILES_DIR
vi.mock('../claude-profile/profile-utils', async (importOriginal) => {
const actual = await importOriginal<typeof import('../claude-profile/profile-utils')>();
return {
...actual,
getEmailFromConfigDir: vi.fn(() => 'test@example.com')
};
});
// Mock credential-utils
vi.mock('../claude-profile/credential-utils', () => ({
getCredentialsFromKeychain: vi.fn(() => ({
token: 'mock-token',
email: 'test@example.com'
})),
clearKeychainCache: vi.fn(),
normalizeWindowsPath: vi.fn((p: string) => p),
updateProfileSubscriptionMetadata: vi.fn()
}));
// Mock token-refresh
vi.mock('../claude-profile/token-refresh', () => ({
refreshOAuthToken: vi.fn(),
isTokenExpired: vi.fn(() => false),
getTokenExpirationTime: vi.fn(() => null),
initializeTokenRefresh: vi.fn(),
stopTokenRefresh: vi.fn(),
scheduleTokenRefresh: vi.fn()
}));
// Mock electron
vi.mock('electron', () => ({
app: {
getPath: vi.fn(() => '/fake/user-data'),
getAppPath: vi.fn(() => '/fake/app-path')
}
}));
// Mock usage-monitor
vi.mock('../claude-profile/usage-monitor', () => ({
getUsageMonitor: vi.fn(() => ({
on: vi.fn(),
off: vi.fn(),
emit: vi.fn(),
start: vi.fn(),
stop: vi.fn()
})),
UsageMonitor: { getInstance: vi.fn() }
}));
import { ClaudeProfileManager } from '../claude-profile-manager';
import { checkProfileAvailability } from '../claude-profile/profile-scorer';
describe('getBestAvailableUnifiedAccount', () => {
let manager: ClaudeProfileManager;
const mockAutoSwitchSettings = {
enabled: true,
proactiveSwapEnabled: true,
usageCheckInterval: 30000,
sessionThreshold: 95,
weeklyThreshold: 99
};
const mockOAuthProfiles = [
{
id: 'oauth-1',
name: 'OAuth Profile 1',
isAuthenticated: true,
isDefault: true,
usage: { sessionUsagePercent: 50, weeklyUsagePercent: 30 }
},
{
id: 'oauth-2',
name: 'OAuth Profile 2',
isAuthenticated: true,
isDefault: false,
usage: { sessionUsagePercent: 20, weeklyUsagePercent: 10 }
}
];
beforeEach(() => {
vi.clearAllMocks();
manager = new ClaudeProfileManager();
// Override internal state
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([]);
vi.spyOn(manager, 'getAutoSwitchSettings').mockReturnValue(mockAutoSwitchSettings as any);
vi.spyOn(manager, 'getProfilesSortedByAvailability').mockReturnValue(mockOAuthProfiles as any);
// Default: all profiles available
vi.mocked(checkProfileAvailability).mockReturnValue({ available: true });
// Default: API profiles available
mockLoadProfilesFile.mockResolvedValue({
profiles: [...mockAPIProfiles],
activeProfileId: null,
version: 1
});
});
it('should return OAuth profile when both available and no priority set', async () => {
const result = await manager.getBestAvailableUnifiedAccount('excluded-id');
expect(result).not.toBeNull();
expect(result?.type).toBe('oauth');
expect(result?.id).toBe('oauth-1');
});
it('should return API profile when it has higher priority', async () => {
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([
'api-api-glm-1', // API first
'oauth-oauth-1', // OAuth second
'oauth-oauth-2'
]);
const result = await manager.getBestAvailableUnifiedAccount('excluded-id');
expect(result).not.toBeNull();
expect(result?.type).toBe('api');
expect(result?.id).toBe('api-glm-1');
expect(result?.name).toBe('GLM API');
});
it('should exclude the specified profile ID', async () => {
const result = await manager.getBestAvailableUnifiedAccount('oauth-1');
expect(result).not.toBeNull();
expect(result?.id).not.toBe('oauth-1');
});
it('should exclude additional profile IDs', async () => {
const result = await manager.getBestAvailableUnifiedAccount('oauth-1', ['oauth-2']);
// Both OAuth excluded, should fall through to API
expect(result).not.toBeNull();
expect(result?.type).toBe('api');
});
it('should return null when all profiles are excluded', async () => {
const result = await manager.getBestAvailableUnifiedAccount(
'oauth-1',
['oauth-2', 'api-glm-1', 'api-anthropic-1']
);
expect(result).toBeNull();
});
it('should skip API profiles without apiKey', async () => {
mockLoadProfilesFile.mockResolvedValue({
profiles: [
{ id: 'api-no-key', name: 'No Key', baseUrl: 'https://example.com', apiKey: '' }
],
activeProfileId: null,
version: 1
});
// Exclude all OAuth
vi.spyOn(manager, 'getProfilesSortedByAvailability').mockReturnValue([]);
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).toBeNull();
});
it('should skip unavailable OAuth profiles (rate limited)', async () => {
// Mock checkProfileAvailability to reject all OAuth profiles
vi.mocked(checkProfileAvailability).mockReturnValue({
available: false,
reason: 'rate limited'
});
const result = await manager.getBestAvailableUnifiedAccount();
// OAuth filtered out, should get API
expect(result).not.toBeNull();
expect(result?.type).toBe('api');
});
it('should handle API profile loading error gracefully', async () => {
mockLoadProfilesFile.mockRejectedValue(new Error('File not found'));
const result = await manager.getBestAvailableUnifiedAccount();
// Should still return OAuth profile
expect(result).not.toBeNull();
expect(result?.type).toBe('oauth');
});
it('should sort by priority order correctly with mixed types', async () => {
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([
'oauth-oauth-2', // OAuth-2 is highest priority
'api-api-glm-1', // API second
'oauth-oauth-1' // OAuth-1 is lowest
]);
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).not.toBeNull();
expect(result?.id).toBe('oauth-2');
expect(result?.type).toBe('oauth');
});
it('should handle empty profiles list', async () => {
vi.spyOn(manager, 'getProfilesSortedByAvailability').mockReturnValue([]);
mockLoadProfilesFile.mockResolvedValue({
profiles: [],
activeProfileId: null,
version: 1
});
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).toBeNull();
});
it('should include correct priorityIndex in result', async () => {
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([
'api-api-glm-1'
]);
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).not.toBeNull();
expect(result?.priorityIndex).toBe(0); // First in priority order
});
it('should assign Infinity priorityIndex when not in priority order', async () => {
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([]);
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).not.toBeNull();
expect(result?.priorityIndex).toBe(Infinity);
});
it('should consider multiple API profiles with correct scoring', async () => {
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([
'api-api-anthropic-1', // Anthropic API first
'api-api-glm-1' // GLM second
]);
// Exclude all OAuth
vi.spyOn(manager, 'getProfilesSortedByAvailability').mockReturnValue([]);
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).not.toBeNull();
expect(result?.id).toBe('api-anthropic-1');
expect(result?.type).toBe('api');
expect(result?.priorityIndex).toBe(0);
});
it('should handle both OAuth and API exhausted gracefully', async () => {
// All OAuth unavailable
vi.mocked(checkProfileAvailability).mockReturnValue({
available: false,
reason: 'rate limited'
});
// No API profiles
mockLoadProfilesFile.mockResolvedValue({
profiles: [],
activeProfileId: null,
version: 1
});
const result = await manager.getBestAvailableUnifiedAccount();
expect(result).toBeNull();
});
it('should prefer OAuth when both have same Infinity priority', async () => {
// No priority order set
vi.spyOn(manager, 'getAccountPriorityOrder').mockReturnValue([]);
const result = await manager.getBestAvailableUnifiedAccount('excluded-id');
// OAuth should come first by default
expect(result).not.toBeNull();
expect(result?.type).toBe('oauth');
});
});
describe('UsageMonitor - isAPIProfile gate removal', () => {
// Use actual file path since require.resolve doesn't work with mocked modules
const usageMonitorPath = new URL('../claude-profile/usage-monitor.ts', import.meta.url).pathname.replace(/^\/([A-Z]:)/, '$1');
it('should NOT contain isAPIProfile guard in checkUsageAndSwap swap logic', async () => {
const fs = await import('node:fs');
const source = fs.readFileSync(usageMonitorPath, 'utf-8');
// The old guard "Skipping proactive swap for API profile" should be removed
expect(source).not.toContain('Skipping proactive swap for API profile');
});
it('should NOT contain isAPIProfile guard in handleAuthFailure', async () => {
const fs = await import('node:fs');
const source = fs.readFileSync(usageMonitorPath, 'utf-8');
// The old guard should be removed - handleAuthFailure should work for all profile types
expect(source).not.toContain('using API profile, skipping swap');
});
it('should contain swap cooldown logic', async () => {
const fs = await import('node:fs');
const source = fs.readFileSync(usageMonitorPath, 'utf-8');
// Swap cooldown was added to prevent rapid back-and-forth
expect(source).toContain('SWAP_COOLDOWN_MS');
expect(source).toContain('lastSwapTimestamp');
expect(source).toContain('Swap cooldown active');
});
it('should use getBestAvailableUnifiedAccount in performProactiveSwap', async () => {
const fs = await import('node:fs');
const source = fs.readFileSync(usageMonitorPath, 'utf-8');
// performProactiveSwap should use the unified selection instead of inline logic
expect(source).toContain('getBestAvailableUnifiedAccount');
// The old inline unified list-building logic should be removed
expect(source).not.toContain('UnifiedSwapTarget');
});
});
-489
View File
@@ -1,489 +0,0 @@
# Agent Queue System
## Overview
The Agent Queue System manages the lifecycle and execution of background AI agents in Auto Claude. It ensures that agents spawn sequentially to prevent race conditions and file corruption.
## Why Sequential Execution?
### The Problem: `~/.claude.json` Race Condition
When multiple agents spawn concurrently, they all attempt to read and write to `~/.claude.json`:
```
Agent 1: Read ~/.claude.json → Modify → Write
Agent 2: Read ~/.claude.json → Modify → Write (concurrently!)
Agent 3: Read ~/.claude.json → Modify → Write (concurrently!)
```
This caused:
- **JSON corruption**: Invalid JSON structure from interleaved writes
- **Backup file accumulation**: `.claude.json.backup`, `.claude.json.backup.1`, etc.
- **Lost configuration**: Last write wins, earlier changes lost
- **Mysterious failures**: Agents failing with "invalid JSON" errors
### The Solution: Sequential Spawning via SpawnQueue
All agent types now execute **sequentially** through a FIFO queue:
```
Request 1 (ideation) → Spawn → Wait for exit → Next
Request 2 (roadmap) → Spawn → Wait for exit → Next
Request 3 (ideation) → Spawn → Wait for exit → Next
```
**Benefits:**
- No concurrent writes to `~/.claude.json`
- No JSON corruption or backup files
- Predictable execution order
- Simple error recovery (continue on failure)
## Architecture
### Components
```
┌─────────────────────────────────────────────────────────────┐
│ AgentQueueManager │
│ - startIdeationGeneration() │
│ - startRoadmapGeneration() │
│ - stopIdeation() / stopRoadmap() │
└──────────────────────┬──────────────────────────────────────┘
┌─────────────────┐
│ SpawnQueue │
│ (FIFO Queue) │
└────────┬────────┘
┌─────────────┴─────────────┐
▼ ▼
┌─────────────────────┐ ┌─────────────────────┐
│ executeIdeationSpawn│ │executeRoadmapSpawn │
│ (ideation_runner) │ │ (roadmap_runner) │
└─────────────────────┘ └─────────────────────┘
│ │
└───────────┬───────────────┘
┌─────────────────┐
│ AgentState │
│ - addProcess() │
│ - deleteProcess│
└─────────────────┘
┌─────────────────┐
│ AgentEvents │
│ (emit events) │
└─────────────────┘
```
### Data Flow
1. **User Request**: User triggers ideation or roadmap generation from UI
2. **Enqueue**: `AgentQueueManager` creates `SpawnRequest` and enqueues it
3. **Queue Processing**: `SpawnQueue` processes requests FIFO
4. **Spawn Execution**: Router calls `executeIdeationSpawn()` or `executeRoadmapSpawn()`
5. **Process Tracking**: Spawned process added to `AgentState` for tracking
6. **Event Handlers**: stdout/stderr/exit handlers attached to process
7. **Wait for Exit**: Queue waits for process to exit before processing next
8. **Completion**: Success/error events emitted to renderer process
### Queue Behavior
**FIFO Processing:**
- First in, first out
- No priority system (simple is better)
- All agent types share same queue
**Error Resilience:**
- If spawn fails, invoke `onError` callback
- Continue to next item in queue
- Don't block queue on single failure
**Sequential Execution:**
- Only one agent spawns at a time
- Wait for `process.exit()` before next spawn
- Prevents `~/.claude.json` race condition
## Agent Types
### Ideation Agents
**Purpose**: Discover improvements, performance issues, security vulnerabilities
**Runner**: `apps/backend/runners/ideation_runner.py`
**Types**:
- `discovery`: Code improvements and refactorings
- `performance`: Performance optimizations
- `security`: Security vulnerabilities
- `testing`: Test coverage gaps
- `documentation`: Documentation improvements
- `accessibility`: Accessibility issues
**Process Type**: `'ideation'`
**Events**:
- `ideation-progress`: Progress updates with phase/message
- `ideation-log`: Log output lines
- `ideation-type-complete`: Single type completed with ideas
- `ideation-type-failed`: Single type failed
- `ideation-complete`: All types completed
- `ideation-error`: Fatal error
- `ideation-stopped`: User stopped generation
### Roadmap Agents
**Purpose**: Generate strategic roadmap with competitor analysis
**Runner**: `apps/backend/runners/roadmap_runner.py`
**Features**:
- Strategic feature planning
- Competitive analysis (optional)
- Timeline estimation
- Priority ranking
**Process Type**: `'roadmap'`
**Events**:
- `roadmap-progress`: Progress updates with phase/message
- `roadmap-log`: Log output lines
- `roadmap-complete`: Roadmap generated
- `roadmap-error`: Fatal error
- `roadmap-stopped`: User stopped generation
### Build Agents (Not in Queue)
**Note**: Build agents (planner, coder, QA) are managed separately by `agent-process.ts` and do **not** go through this queue. They have their own spawning mechanism tied to spec/task lifecycle.
## Process State Tracking
Each spawned process is tracked in `AgentState`:
```typescript
interface QueuedProcessInfo {
taskId: string; // Project ID or task ID
process: ChildProcess; // Node.js ChildProcess instance
startedAt: Date; // When process was spawned
projectPath: string; // Project directory path
spawnId: number; // Unique spawn ID
queueProcessType: 'ideation' | 'roadmap'; // Process type
}
```
**Key Methods:**
- `addProcess(projectId, info)`: Track spawned process
- `getProcess(projectId)`: Get process info
- `deleteProcess(projectId)`: Remove from tracking
- `generateSpawnId()`: Generate unique spawn ID
- `wasSpawnKilled(spawnId)`: Check if intentionally stopped
- `clearKilledSpawn(spawnId)`: Clear killed flag
## Adding New Agent Types
To add a new agent type to the sequential queue:
### 1. Define Process Type
Add to `QueuedProcessInfo` type in `agent-state.ts`:
```typescript
queueProcessType: 'ideation' | 'roadmap' | 'new-type';
```
### 2. Create Spawn Executor
Add method in `AgentQueueManager`:
```typescript
private async executeNewTypeSpawn(
spawnId: string,
projectPath: string,
args: string[],
env: Record<string, string>,
projectId: string,
cwd: string
): Promise<ChildProcess> {
// Spawn the process
const childProcess = spawn(/* ... */);
// Add to state tracking
this.state.addProcess(projectId, {
taskId: projectId,
process: childProcess,
startedAt: new Date(),
projectPath,
spawnId: parseInt(spawnId, 10),
queueProcessType: 'new-type'
});
return childProcess;
}
```
### 3. Create Spawn Wrapper
Add method in `AgentQueueManager` to enqueue requests:
```typescript
async startNewTypeGeneration(
projectId: string,
projectPath: string,
config: NewTypeConfig
): Promise<void> {
// Build args
const args = ['runner.py', '--project', projectPath];
// Enqueue spawn request
this.spawnQueue.enqueue({
id: String(spawnId),
type: 'new-type',
projectId,
projectPath,
args,
env: finalEnv,
cwd,
onSpawn: async (childProcess) => {
// Attach event handlers
childProcess.stdout?.on('data', (data) => { /* ... */ });
childProcess.on('exit', (code) => { /* ... */ });
},
onError: (error) => {
this.emitter.emit('new-type-error', projectId, error.message);
}
});
}
```
### 4. Update SpawnQueue Router
Update constructor in `AgentQueueManager`:
```typescript
this.spawnQueue = new SpawnQueue(async (id, projectPath, args, env, projectId, cwd, type = 'ideation') => {
if (type === 'roadmap') {
return this.executeRoadmapSpawn(id, projectPath, args, env, projectId, cwd);
} else if (type === 'new-type') {
return this.executeNewTypeSpawn(id, projectPath, args, env, projectId, cwd);
} else {
return this.executeIdeationSpawn(id, projectPath, args, env, projectId, cwd);
}
});
```
### 5. Add Event Emitter Methods
Add stop/status methods:
```typescript
stopNewType(projectId: string): boolean {
const processInfo = this.state.getProcess(projectId);
const isNewType = processInfo?.queueProcessType === 'new-type';
if (isNewType) {
this.processManager.killProcess(projectId);
this.emitter.emit('new-type-stopped', projectId);
return true;
}
return false;
}
isNewTypeRunning(projectId: string): boolean {
const processInfo = this.state.getProcess(projectId);
return processInfo?.queueProcessType === 'new-type';
}
```
## Rate Limit Detection
The queue system automatically detects API rate limits:
1. **Collect Output**: stdout/stderr collected during process execution
2. **Detect Patterns**: Checks for rate limit error messages
3. **Emit Event**: `sdk-rate-limit` event with detection info
4. **Auto-Switch**: Profile scorer automatically switches to next available profile
**Detection**:
```typescript
const rateLimitDetection = detectRateLimit(allOutput);
if (rateLimitDetection.isRateLimited) {
const rateLimitInfo = createSDKRateLimitInfo('ideation', rateLimitDetection, { projectId });
this.emitter.emit('sdk-rate-limit', rateLimitInfo);
}
```
## Python Environment Management
Agents require a Python environment with dependencies:
1. **Pre-flight Check**: `ensurePythonEnvReady()` checks if venv is ready
2. **Venv Creation**: If needed, creates venv and installs dependencies
3. **Python Path**: Uses configured Python path (or bundled Python)
4. **PYTHONPATH**: Bundled site-packages + autoBuildSource for imports
**Environment Variables**:
```typescript
const finalEnv = {
...process.env,
...pythonEnv, // Bundled packages
...combinedEnv, // auto-claude/.env
...profileEnv, // OAuth token
...apiProfileEnv, // API profile config
PYTHONPATH: combinedPythonPath,
PYTHONUNBUFFERED: '1',
PYTHONUTF8: '1'
};
```
## Progress Persistence
Roadmap generation persists progress to disk for recovery:
- **File**: `.auto-claude/roadmap/generation_progress.json`
- **Debounced**: 300ms debounce (3-4 writes/sec max)
- **Leading + Trailing**: Immediate first write, final state on completion
- **Recovery**: Process can recover after app restart
**Progress Data**:
```json
{
"phase": "analyzing",
"progress": 45,
"message": "Analyzing codebase...",
"started_at": "2025-02-15T10:30:00Z",
"last_update_at": "2025-02-15T10:35:00Z",
"is_running": true
}
```
## Debug Logging
All operations are logged for debugging:
```typescript
debugLog('[Agent Queue] Starting ideation generation:', { projectId, projectPath, config });
debugLog('[Agent Queue] Generated spawn ID:', spawnId);
debugLog('[Agent Queue] Ideation process spawned:', { spawnId, projectId, pid });
```
**Enable Debug Logs**:
- Dev mode: Logs always visible
- Production: Set `DEBUG=auto-claude:*` environment variable
## Error Handling
### Spawn Errors
If process spawn fails:
1. `onError` callback invoked
2. Error event emitted to renderer
3. Queue continues to next item
**Example**:
```typescript
onError: (error) => {
debugError('[Agent Queue] Failed to spawn ideation process:', error);
this.emitter.emit('ideation-error', projectId, error.message);
}
```
### Process Errors
If process errors after spawn:
1. `process.on('error')` handler invoked
2. Process removed from state tracking
3. Error event emitted to renderer
### Exit Codes
- **Code 0**: Success
- **Code ≠ 0**: Failure (check for rate limits)
## Testing
### Manual Testing
```bash
# Start app in dev mode
npm run dev
# Trigger multiple agents simultaneously:
# 1. Open 3 projects
# 2. Click "Generate Ideas" on all 3 quickly
# 3. Click "Generate Roadmap" on 2 projects
# 4. Monitor console - should see sequential execution
# 5. Check ~/.claude.json - should be valid JSON
# 6. No .backup files should be created
```
### Automated Testing
```bash
# Run frontend tests
cd apps/frontend
npm test
# Typecheck
npm run typecheck
# Lint
npm run lint
```
## Files
- **agent-queue.ts**: Main queue manager, spawns ideation/roadmap agents
- **spawn-queue.ts**: FIFO queue for sequential spawning
- **agent-state.ts**: Process state tracking
- **agent-events.ts**: Event emission and progress parsing
- **agent-process.ts**: Process lifecycle management (build agents)
- **types.ts**: TypeScript type definitions
## Related Documentation
- [ARCHITECTURE.md](../../../../../../shared_docs/ARCHITECTURE.md): Overall architecture
- [apps/frontend/CONTRIBUTING.md](../../../../../../apps/frontend/CONTRIBUTING.md): Frontend contributing guide
- [CLAUDE.md](../../../../../../CLAUDE.md): Project instructions
## Troubleshooting
### Agents Not Starting
**Symptom**: Click "Generate Ideas" but nothing happens
**Checks**:
1. Check Python path is configured in settings
2. Check autoBuildSource path is set
3. Check runner files exist (`ideation_runner.py`, `roadmap_runner.py`)
4. Check debug logs for errors
### JSON Corruption
**Symptom**: `~/.claude.json` is invalid JSON
**Checks**:
1. Check if queue is being used (should be sequential)
2. Check for concurrent spawns (shouldn't happen)
3. Check backup files (.backup, .backup.1)
4. Restore from backup: `cp ~/.claude.json.backup ~/.claude.json`
### Rate Limiting
**Symptom**: Agents fail with rate limit errors
**Solution**:
1. System automatically switches to next available profile
2. Add more Claude profiles in settings
3. Wait for rate limit to reset (1 minute)
### Process Won't Stop
**Symptom**: Click "Stop" but process keeps running
**Checks**:
1. Check process ID in debug logs
2. Check if `wasIntentionallyStopped` flag is set
3. Check if process is tracked in AgentState
4. Manual kill: `kill <PID>` (macOS/Linux) or `taskkill /PID <PID>` (Windows)
@@ -84,7 +84,7 @@ vi.mock('../services/profile', () => ({
}));
vi.mock('../rate-limit-detector', () => ({
getBestAvailableProfileEnv: vi.fn(() => Promise.resolve({
getBestAvailableProfileEnv: vi.fn(() => ({
env: {},
profileId: 'default',
profileName: 'Default',
@@ -292,7 +292,7 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue({});
// Set OAuth token via getProfileEnv (existing flow)
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockReturnValue({
env: { CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-123' },
profileId: 'default',
profileName: 'Default',
@@ -327,7 +327,7 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue({});
// Set OAuth token
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockReturnValue({
env: { CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-456' },
profileId: 'default',
profileName: 'Default',
@@ -354,7 +354,7 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
// OAuth mode
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue({});
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockReturnValue({
env: { CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-789' },
profileId: 'default',
profileName: 'Default',
@@ -403,10 +403,10 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue(mockApiProfileEnv);
// Mock ALL console methods to capture any debug/error output
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleDebugSpy = vi.spyOn(console, 'debug').mockImplementation(() => { /* noop */ });
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const consoleWarnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const consoleDebugSpy = vi.spyOn(console, 'debug').mockImplementation(() => {});
await processManager.spawnProcess('task-1', '/fake/cwd', ['run.py'], {}, 'task-execution');
@@ -444,8 +444,8 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue(mockApiProfileEnv);
// Mock console methods
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const consoleLogSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
await processManager.spawnProcess('task-1', '/fake/cwd', ['run.py'], {}, 'task-execution');
@@ -512,7 +512,7 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
ANTHROPIC_BASE_URL: 'https://api-profile.com'
};
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockReturnValue({
env: profileEnv,
profileId: 'default',
profileName: 'Default',
@@ -799,127 +799,4 @@ describe('AgentProcessManager - API Profile Env Injection (Story 2.3)', () => {
expect(envArg.GITHUB_CLI_PATH).toBe('/opt/homebrew/bin/gh');
});
});
describe('CLAUDE_CONFIG_DIR Propagation', () => {
let originalEnv: NodeJS.ProcessEnv;
beforeEach(() => {
originalEnv = { ...process.env };
delete process.env.CLAUDE_CONFIG_DIR;
});
afterEach(() => {
process.env = originalEnv;
});
it('should propagate CLAUDE_CONFIG_DIR from profile env in OAuth mode', async () => {
// OAuth mode - no active API profile
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue({});
// Profile provides CLAUDE_CONFIG_DIR (OAuth subscription profile)
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
env: {
CLAUDE_CONFIG_DIR: '/home/user/.config/claude-profile-1',
CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-abc'
},
profileId: 'profile-1',
profileName: 'Profile 1',
wasSwapped: false
});
await processManager.spawnProcess('task-1', '/fake/cwd', ['run.py'], {}, 'task-execution');
expect(spawnCalls).toHaveLength(1);
const envArg = spawnCalls[0].options.env as Record<string, unknown>;
// CLAUDE_CONFIG_DIR should be present in spawn env
expect(envArg.CLAUDE_CONFIG_DIR).toBe('/home/user/.config/claude-profile-1');
});
it('should clear ANTHROPIC_API_KEY in OAuth mode with CLAUDE_CONFIG_DIR', async () => {
// Simulate stale ANTHROPIC_API_KEY in process.env
process.env.ANTHROPIC_API_KEY = 'sk-stale-key';
// OAuth mode - no active API profile
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue({});
// Profile provides CLAUDE_CONFIG_DIR
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
env: {
CLAUDE_CONFIG_DIR: '/home/user/.config/claude-profile-2',
CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-def'
},
profileId: 'profile-2',
profileName: 'Profile 2',
wasSwapped: false
});
await processManager.spawnProcess('task-1', '/fake/cwd', ['run.py'], {}, 'task-execution');
expect(spawnCalls).toHaveLength(1);
const envArg = spawnCalls[0].options.env as Record<string, unknown>;
// ANTHROPIC_API_KEY should be cleared (empty string) in OAuth mode
expect(envArg.ANTHROPIC_API_KEY).toBe('');
// CLAUDE_CONFIG_DIR should still be set
expect(envArg.CLAUDE_CONFIG_DIR).toBe('/home/user/.config/claude-profile-2');
});
it('should pass ANTHROPIC_* vars without CLAUDE_CONFIG_DIR interference in API profile mode', async () => {
// API Profile mode - active profile with custom endpoint
const mockApiProfileEnv = {
ANTHROPIC_AUTH_TOKEN: 'sk-api-profile-key',
ANTHROPIC_BASE_URL: 'https://custom-api.example.com',
ANTHROPIC_MODEL: 'claude-sonnet-4-5-20250929'
};
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue(mockApiProfileEnv);
// Profile env without CLAUDE_CONFIG_DIR (API profile mode)
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
env: {},
profileId: 'api-profile-1',
profileName: 'Custom API',
wasSwapped: false
});
await processManager.spawnProcess('task-1', '/fake/cwd', ['run.py'], {}, 'task-execution');
expect(spawnCalls).toHaveLength(1);
const envArg = spawnCalls[0].options.env as Record<string, unknown>;
// ANTHROPIC_* vars from API profile should be passed through
expect(envArg.ANTHROPIC_AUTH_TOKEN).toBe('sk-api-profile-key');
expect(envArg.ANTHROPIC_BASE_URL).toBe('https://custom-api.example.com');
expect(envArg.ANTHROPIC_MODEL).toBe('claude-sonnet-4-5-20250929');
// CLAUDE_CONFIG_DIR should NOT be present since profile didn't provide it
expect(envArg.CLAUDE_CONFIG_DIR).toBeUndefined();
});
it('should clear CLAUDE_CODE_OAUTH_TOKEN when CLAUDE_CONFIG_DIR is provided by profile', async () => {
// OAuth mode
vi.mocked(profileService.getAPIProfileEnv).mockResolvedValue({});
// Profile provides CLAUDE_CONFIG_DIR - agent should use config dir for auth
vi.mocked(rateLimitDetector.getBestAvailableProfileEnv).mockResolvedValue({
env: {
CLAUDE_CONFIG_DIR: '/home/user/.config/claude-profile-3',
CLAUDE_CODE_OAUTH_TOKEN: 'oauth-token-ghi'
},
profileId: 'profile-3',
profileName: 'Profile 3',
wasSwapped: false
});
await processManager.spawnProcess('task-1', '/fake/cwd', ['run.py'], {}, 'task-execution');
expect(spawnCalls).toHaveLength(1);
const envArg = spawnCalls[0].options.env as Record<string, unknown>;
// When CLAUDE_CONFIG_DIR is present, CLAUDE_CODE_OAUTH_TOKEN should be cleared
// because Claude Code resolves auth from the config dir instead
expect(envArg.CLAUDE_CONFIG_DIR).toBe('/home/user/.config/claude-profile-3');
expect(envArg.CLAUDE_CODE_OAUTH_TOKEN).toBeFalsy();
});
});
});
+39 -136
View File
@@ -26,7 +26,6 @@ import { getOAuthModeClearVars } from './env-utils';
import { getAugmentedEnv } from '../env-utils';
import { getToolInfo, getClaudeCliPathForSdk } from '../cli-tool-manager';
import { killProcessGracefully, isWindows } from '../platform';
import { debugLog } from '../../shared/utils/debug-logger';
/**
* Type for supported CLI tools
@@ -173,35 +172,12 @@ export class AgentProcessManager {
return env;
}
private async setupProcessEnvironment(
private setupProcessEnvironment(
extraEnv: Record<string, string>
): Promise<NodeJS.ProcessEnv> {
): NodeJS.ProcessEnv {
// Get best available Claude profile environment (automatically handles rate limits)
const profileResult = await getBestAvailableProfileEnv();
const profileResult = getBestAvailableProfileEnv();
const profileEnv = profileResult.env;
debugLog('[AgentProcess:setupEnv] Profile result:', {
profileId: profileResult.profileId,
hasOAuthToken: !!profileEnv.CLAUDE_CODE_OAUTH_TOKEN,
hasApiKey: !!profileEnv.ANTHROPIC_API_KEY,
hasConfigDir: !!profileEnv.CLAUDE_CONFIG_DIR,
configDir: profileEnv.CLAUDE_CONFIG_DIR || '(not set)',
oauthTokenPrefix: profileEnv.CLAUDE_CODE_OAUTH_TOKEN?.substring(0, 8) || '(not set)',
apiKeyPrefix: profileEnv.ANTHROPIC_API_KEY?.substring(0, 8) || '(not set)',
});
// Warn if profile lacks CLAUDE_CONFIG_DIR - this means the profile has no configDir
// and subscription metadata may not propagate correctly to the agent subprocess
if (!profileEnv.CLAUDE_CONFIG_DIR) {
console.warn('[AgentProcess:setupEnv] WARNING: Profile env lacks CLAUDE_CONFIG_DIR - profile may not have a configDir set. Subscription metadata may not reach agent subprocess.');
}
debugLog('[AgentProcess:setupEnv] extraEnv auth keys:', {
hasOAuthToken: !!extraEnv.CLAUDE_CODE_OAUTH_TOKEN,
hasApiKey: !!extraEnv.ANTHROPIC_API_KEY,
hasConfigDir: !!extraEnv.CLAUDE_CONFIG_DIR,
});
// Use getAugmentedEnv() to ensure common tool paths (dotnet, homebrew, etc.)
// are available even when app is launched from Finder/Dock
const augmentedEnv = getAugmentedEnv();
@@ -229,9 +205,7 @@ export class AgentProcessManager {
const ghCliEnv = this.detectAndSetCliPath('gh');
const glabCliEnv = this.detectAndSetCliPath('glab');
// Profile env is spread last to ensure CLAUDE_CONFIG_DIR and auth vars
// from the active profile always win over extraEnv or augmentedEnv.
const mergedEnv = {
return {
...augmentedEnv,
...gitBashEnv,
...claudeCliEnv,
@@ -243,36 +217,13 @@ export class AgentProcessManager {
PYTHONIOENCODING: 'utf-8',
PYTHONUTF8: '1'
} as NodeJS.ProcessEnv;
// When the active profile provides CLAUDE_CONFIG_DIR, clear CLAUDE_CODE_OAUTH_TOKEN
// from the spawn environment. CLAUDE_CONFIG_DIR lets Claude Code resolve its own
// OAuth tokens from the config directory, making an explicit token unnecessary.
// This matches the terminal pattern in claude-integration-handler.ts where
// configDir is preferred over direct token injection.
// We check profileEnv specifically (not mergedEnv) to avoid clearing the token
// when CLAUDE_CONFIG_DIR comes from the shell environment rather than the profile.
if (profileEnv.CLAUDE_CONFIG_DIR) {
mergedEnv.CLAUDE_CODE_OAUTH_TOKEN = '';
debugLog('[AgentProcess:setupEnv] Profile provides CLAUDE_CONFIG_DIR, cleared CLAUDE_CODE_OAUTH_TOKEN from spawn env');
}
debugLog('[AgentProcess:setupEnv] Final merged env auth state:', {
hasOAuthToken: !!mergedEnv.CLAUDE_CODE_OAUTH_TOKEN,
hasApiKey: !!mergedEnv.ANTHROPIC_API_KEY,
hasConfigDir: !!mergedEnv.CLAUDE_CONFIG_DIR,
configDir: mergedEnv.CLAUDE_CONFIG_DIR || '(not set)',
oauthTokenPrefix: mergedEnv.CLAUDE_CODE_OAUTH_TOKEN?.substring(0, 8) || '(not set)',
apiKeyPrefix: mergedEnv.ANTHROPIC_API_KEY?.substring(0, 8) || '(not set)',
});
return mergedEnv;
}
private async handleProcessFailure(
private handleProcessFailure(
taskId: string,
allOutput: string,
processType: ProcessType
): Promise<boolean> {
): boolean {
console.log('[AgentProcess] Checking for rate limit in output (last 500 chars):', allOutput.slice(-500));
const rateLimitDetection = detectRateLimit(allOutput);
@@ -285,7 +236,7 @@ export class AgentProcessManager {
});
if (rateLimitDetection.isRateLimited) {
const wasHandled = await this.handleRateLimitWithAutoSwap(
const wasHandled = this.handleRateLimitWithAutoSwap(
taskId,
rateLimitDetection,
processType
@@ -302,11 +253,11 @@ export class AgentProcessManager {
return this.handleAuthFailure(taskId, allOutput);
}
private async handleRateLimitWithAutoSwap(
private handleRateLimitWithAutoSwap(
taskId: string,
rateLimitDetection: ReturnType<typeof detectRateLimit>,
processType: ProcessType
): Promise<boolean> {
): boolean {
const profileManager = getClaudeProfileManager();
const autoSwitchSettings = profileManager.getAutoSwitchSettings();
@@ -322,15 +273,14 @@ export class AgentProcessManager {
}
const currentProfileId = rateLimitDetection.profileId;
const bestAccount = await profileManager.getBestAvailableUnifiedAccount(currentProfileId);
const bestProfile = profileManager.getBestAvailableProfile(currentProfileId);
console.log('[AgentProcess] Best available account:', bestAccount ? {
id: bestAccount.id,
name: bestAccount.name,
type: bestAccount.type
console.log('[AgentProcess] Best available profile:', bestProfile ? {
id: bestProfile.id,
name: bestProfile.name
} : 'NONE');
if (!bestAccount) {
if (!bestProfile) {
// Single account case: let backend handle with intelligent pause
// Don't show manual modal - backend will pause intelligently and resume when ready
console.log('[AgentProcess] No alternative profile - backend will handle with intelligent pause');
@@ -339,36 +289,24 @@ export class AgentProcessManager {
return false;
}
console.log('[AgentProcess] AUTO-SWAP: Switching from', currentProfileId, 'to', bestAccount.id, '(type:', bestAccount.type + ')');
if (bestAccount.type === 'oauth') {
profileManager.setActiveProfile(bestAccount.id);
// Clear API active profile so getAPIProfileEnv() returns empty (OAuth mode)
try {
const { setActiveAPIProfile } = await import('../services/profile/profile-manager');
await setActiveAPIProfile(null);
} catch (error) {
console.error('[AgentProcess] Failed to clear active API profile:', error);
}
} else {
const { setActiveAPIProfile } = await import('../services/profile/profile-manager');
await setActiveAPIProfile(bestAccount.id);
}
console.log('[AgentProcess] AUTO-SWAP: Switching from', currentProfileId, 'to', bestProfile.id);
profileManager.setActiveProfile(bestProfile.id);
const source = processType === 'spec-creation' ? 'roadmap' : 'task';
const rateLimitInfo = createSDKRateLimitInfo(source, rateLimitDetection, { taskId });
rateLimitInfo.wasAutoSwapped = true;
rateLimitInfo.swappedToProfile = { id: bestAccount.id, name: bestAccount.name };
rateLimitInfo.swappedToProfile = { id: bestProfile.id, name: bestProfile.name };
rateLimitInfo.swapReason = 'reactive';
console.log('[AgentProcess] Emitting sdk-rate-limit event (auto-swapped):', rateLimitInfo);
this.emitter.emit('sdk-rate-limit', rateLimitInfo);
console.log('[AgentProcess] Emitting auto-swap-restart-task event for task:', taskId);
this.emitter.emit('auto-swap-restart-task', taskId, bestAccount.id);
this.emitter.emit('auto-swap-restart-task', taskId, bestProfile.id);
return true;
}
private async handleAuthFailure(taskId: string, allOutput: string): Promise<boolean> {
private handleAuthFailure(taskId: string, allOutput: string): boolean {
console.log('[AgentProcess] No rate limit detected - checking for auth failure');
const authFailureDetection = detectAuthFailure(allOutput);
@@ -380,7 +318,7 @@ export class AgentProcessManager {
console.log('[AgentProcess] Auth failure detected:', authFailureDetection);
// Try auto-swap if enabled
const wasHandled = await this.handleAuthFailureWithAutoSwap(taskId, authFailureDetection);
const wasHandled = this.handleAuthFailureWithAutoSwap(taskId, authFailureDetection);
if (!wasHandled) {
// Fall back to UI notification
@@ -398,12 +336,12 @@ export class AgentProcessManager {
/**
* Attempt to auto-swap to another profile on authentication failure.
* Only works when autoSwitchOnAuthFailure is enabled and an alternative
* authenticated profile is available. Considers both OAuth and API profiles.
* authenticated profile is available.
*/
private async handleAuthFailureWithAutoSwap(
private handleAuthFailureWithAutoSwap(
taskId: string,
authFailureDetection: ReturnType<typeof detectAuthFailure>
): Promise<boolean> {
): boolean {
const profileManager = getClaudeProfileManager();
const autoSwitchSettings = profileManager.getAutoSwitchSettings();
@@ -419,42 +357,22 @@ export class AgentProcessManager {
}
const currentProfileId = authFailureDetection.profileId;
const bestAccount = await profileManager.getBestAvailableUnifiedAccount(currentProfileId);
const bestProfile = profileManager.getBestAvailableProfile(currentProfileId);
console.log('[AgentProcess] Best available account for auth failure swap:', bestAccount ? {
id: bestAccount.id,
name: bestAccount.name,
type: bestAccount.type
console.log('[AgentProcess] Best available profile for auth failure swap:', bestProfile ? {
id: bestProfile.id,
name: bestProfile.name,
isAuthenticated: bestProfile.isAuthenticated
} : 'NONE');
if (!bestAccount) {
console.log('[AgentProcess] No alternative account available - falling back to UI');
// Verify the best profile is actually authenticated
if (!bestProfile || !bestProfile.isAuthenticated) {
console.log('[AgentProcess] No authenticated alternative profile - falling back to UI');
return false;
}
// For OAuth results, verify authentication (API profiles validated by having apiKey)
if (bestAccount.type === 'oauth') {
const oauthProfile = profileManager.getProfile(bestAccount.id);
if (!oauthProfile?.isAuthenticated && !profileManager.isProfileAuthenticated(oauthProfile!)) {
console.log('[AgentProcess] OAuth profile not authenticated - falling back to UI');
return false;
}
}
console.log('[AgentProcess] AUTH-FAILURE AUTO-SWAP:', currentProfileId, '->', bestAccount.id, '(type:', bestAccount.type + ')');
if (bestAccount.type === 'oauth') {
profileManager.setActiveProfile(bestAccount.id);
// Clear API active profile so getAPIProfileEnv() returns empty (OAuth mode)
try {
const { setActiveAPIProfile } = await import('../services/profile/profile-manager');
await setActiveAPIProfile(null);
} catch (error) {
console.error('[AgentProcess] Failed to clear active API profile:', error);
}
} else {
const { setActiveAPIProfile } = await import('../services/profile/profile-manager');
await setActiveAPIProfile(bestAccount.id);
}
console.log('[AgentProcess] AUTH-FAILURE AUTO-SWAP:', currentProfileId, '->', bestProfile.id);
profileManager.setActiveProfile(bestProfile.id);
// Emit auth-failure event with swap metadata for UI notification
this.emitter.emit('auth-failure', taskId, {
@@ -463,12 +381,12 @@ export class AgentProcessManager {
message: authFailureDetection.message,
originalError: authFailureDetection.originalError,
wasAutoSwapped: true,
swappedToProfile: { id: bestAccount.id, name: bestAccount.name }
swappedToProfile: { id: bestProfile.id, name: bestProfile.name }
});
// Reuse existing restart event
console.log('[AgentProcess] Emitting auto-swap-restart-task event for auth failure:', taskId);
this.emitter.emit('auto-swap-restart-task', taskId, bestAccount.id);
this.emitter.emit('auto-swap-restart-task', taskId, bestProfile.id);
return true;
}
@@ -619,7 +537,7 @@ export class AgentProcessManager {
return envVars;
} catch {
return {};
return {};
}
}
@@ -680,7 +598,7 @@ export class AgentProcessManager {
spawnId
});
const env = await this.setupProcessEnvironment(extraEnv);
const env = this.setupProcessEnvironment(extraEnv);
// Get Python environment (PYTHONPATH for bundled packages, etc.)
const pythonEnv = pythonEnvManager.getPythonEnv();
@@ -697,21 +615,6 @@ export class AgentProcessManager {
// Get OAuth mode clearing vars (clears stale ANTHROPIC_* vars when in OAuth mode)
const oauthModeClearVars = getOAuthModeClearVars(apiProfileEnv);
debugLog('[AgentProcess:spawnProcess] Environment merge chain for task:', taskId, {
baseEnv: {
hasOAuthToken: !!env.CLAUDE_CODE_OAUTH_TOKEN,
hasApiKey: !!env.ANTHROPIC_API_KEY,
hasConfigDir: !!env.CLAUDE_CONFIG_DIR,
configDir: env.CLAUDE_CONFIG_DIR || '(not set)',
},
oauthModeClearVars: Object.keys(oauthModeClearVars),
apiProfileEnv: {
hasApiKey: !!apiProfileEnv.ANTHROPIC_API_KEY,
hasBaseUrl: !!apiProfileEnv.ANTHROPIC_BASE_URL,
apiKeyPrefix: apiProfileEnv.ANTHROPIC_API_KEY?.substring(0, 8) || '(not set)',
},
});
// Parse Python commandto handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(this.getPythonPath());
let childProcess;
@@ -895,7 +798,7 @@ export class AgentProcessManager {
stderrBuffer = processBufferedOutput(stderrBuffer, data.toString('utf-8'));
});
childProcess.on('exit', async (code: number | null) => {
childProcess.on('exit', (code: number | null) => {
if (stdoutBuffer.trim()) {
this.emitter.emit('log', taskId, stdoutBuffer + '\n', projectId);
processLog(stdoutBuffer);
@@ -914,7 +817,7 @@ export class AgentProcessManager {
if (code !== 0) {
console.log('[AgentProcess] Process failed with code:', code, 'for task:', taskId);
const wasHandled = await this.handleProcessFailure(taskId, allOutput, processType);
const wasHandled = this.handleProcessFailure(taskId, allOutput, processType);
if (wasHandled) {
this.emitter.emit('exit', taskId, code, processType, projectId);
@@ -1,118 +0,0 @@
/**
* Tests for AgentQueueManager
*/
import { describe, it, expect } from 'vitest';
import { EventEmitter } from 'events';
import { AgentQueueManager } from './agent-queue';
import { AgentState } from './agent-state';
import { AgentEvents } from './agent-events';
import { AgentProcessManager } from './agent-process';
import { SpawnQueue } from './spawn-queue';
describe('AgentQueueManager', () => {
it('should initialize SpawnQueue instance', () => {
// Create minimal dependencies for testing
const mockState = {} as unknown as AgentState;
const mockEvents = {} as unknown as AgentEvents;
const mockProcessManager = {
ensurePythonEnvReady: async () => ({ ready: true }),
getAutoBuildSourcePath: () => '/mock/path'
} as unknown as AgentProcessManager;
const mockEmitter = new EventEmitter();
// Instantiate AgentQueueManager
const manager = new AgentQueueManager(
mockState,
mockEvents,
mockProcessManager,
mockEmitter
);
// Access private property via type assertion
const spawnQueue = (manager as unknown as { spawnQueue: SpawnQueue }).spawnQueue;
// Verify spawnQueue is a SpawnQueue instance
expect(spawnQueue).toBeDefined();
expect(spawnQueue).toBeInstanceOf(SpawnQueue);
});
it('should initialize SpawnQueue with empty queue', () => {
const mockState = {} as unknown as AgentState;
const mockEvents = {} as unknown as AgentEvents;
const mockProcessManager = {
ensurePythonEnvReady: async () => ({ ready: true }),
getAutoBuildSourcePath: () => '/mock/path'
} as unknown as AgentProcessManager;
const mockEmitter = new EventEmitter();
const manager = new AgentQueueManager(
mockState,
mockEvents,
mockProcessManager,
mockEmitter
);
const spawnQueue = (manager as unknown as { spawnQueue: SpawnQueue }).spawnQueue;
// Verify initial queue state
expect(spawnQueue.length).toBe(0);
expect(spawnQueue.isProcessing).toBe(false);
});
it('should route ideation and roadmap spawns correctly', async () => {
const _mockState = {} as unknown as AgentState;
const mockEvents = {} as unknown as AgentEvents;
const mockProcessManager = {
ensurePythonEnvReady: async () => ({ ready: true }),
getAutoBuildSourcePath: () => '/mock/path',
getPythonPath: () => 'python3',
killProcess: () => false,
getCombinedEnv: () => ({}),
state: { addProcess: () => { /* noop */ }, deleteProcess: () => { /* noop */ } }
} as unknown as AgentProcessManager;
// Mock state methods
const mockStateWithMethods = {
generateSpawnId: () => 1,
addProcess: () => { /* noop */ },
wasSpawnKilled: () => false,
clearKilledSpawn: () => { /* noop */ },
getProcess: () => null,
deleteProcess: () => { /* noop */ }
} as unknown as AgentState;
const mockEmitter = new EventEmitter();
const manager = new AgentQueueManager(
mockStateWithMethods,
mockEvents,
mockProcessManager,
mockEmitter
);
const spawnQueue = (manager as unknown as { spawnQueue: SpawnQueue }).spawnQueue;
// Test that spawn function routes correctly
let processType: string | undefined;
// Mock the spawn function to capture the type
const _originalEnqueue = spawnQueue.enqueue.bind(spawnQueue);
spawnQueue.enqueue = function(request) {
processType = request.type;
return Promise.resolve(undefined);
};
// Access private methods via type assertion
const spawnIdeationProcess = (manager as unknown as { spawnIdeationProcess: (id: string, path: string, args: string[]) => Promise<void> }).spawnIdeationProcess;
const spawnRoadmapProcess = (manager as unknown as { spawnRoadmapProcess: (id: string, path: string, args: string[]) => Promise<void> }).spawnRoadmapProcess;
// Test ideation spawn
await spawnIdeationProcess.call(manager, 'project-1', '/path/to/project', ['ideation']);
expect(processType).toBe('ideation');
// Test roadmap spawn
await spawnRoadmapProcess.call(manager, 'project-2', '/path/to/project2', ['roadmap']);
expect(processType).toBe('roadmap');
});
});
+332 -479
View File
@@ -1,4 +1,4 @@
import { spawn, type ChildProcess } from 'child_process';
import { spawn } from 'child_process';
import path from 'path';
import { existsSync, mkdirSync, unlinkSync, promises as fsPromises } from 'fs';
import { EventEmitter } from 'events';
@@ -21,7 +21,6 @@ import type { RawIdea } from '../ipc-handlers/ideation/types';
import { getPathDelimiter } from '../platform';
import { debounce } from '../utils/debounce';
import { writeFileWithRetry } from '../utils/atomic-file';
import { SpawnQueue } from './spawn-queue';
/** Maximum length for status messages displayed in progress UI */
const STATUS_MESSAGE_MAX_LENGTH = 200;
@@ -40,29 +39,6 @@ function formatStatusMessage(log: string): string {
/**
* Queue management for ideation and roadmap generation
*
* **IMPORTANT: Sequential Execution**
* All agent types (ideation, roadmap) now execute SEQUENTIALLY via SpawnQueue.
* This prevents race conditions when multiple agents write to ~/.claude.json
* concurrently, which was causing JSON corruption and backup file accumulation.
*
* **Key Behaviors:**
* - Only ONE agent spawns at a time across all types (FIFO queue)
* - Next agent waits for previous agent's process to exit
* - Queue automatically continues if spawn fails (error resilience)
* - Each agent type gets unique process tracking via queueProcessType
*
* **Architecture:**
* - User calls startIdeationGeneration() or startRoadmapGeneration()
* - Request enqueued in spawnQueue with type ('ideation' | 'roadmap')
* - Queue routes to executeIdeationSpawn() or executeRoadmapSpawn()
* - Process spawned, tracked in AgentState, event handlers attached
* - Queue waits for process.exit() before processing next item
*
* **Process Types:**
* - 'ideation': Idea generation (discoveries, performance, security)
* - 'roadmap': Strategic roadmap generation with competitor analysis
* - 'build': Build agents (managed separately, not in this queue)
*/
export class AgentQueueManager {
private state: AgentState;
@@ -78,7 +54,6 @@ export class AgentQueueManager {
isRunning: boolean
) => void;
private cancelPersistRoadmapProgress: () => void;
private spawnQueue: SpawnQueue;
constructor(
state: AgentState,
@@ -101,15 +76,6 @@ export class AgentQueueManager {
);
this.debouncedPersistRoadmapProgress = debouncedFn;
this.cancelPersistRoadmapProgress = cancel;
// Initialize sequential spawn queue with routing based on process type
this.spawnQueue = new SpawnQueue(async (id, projectPath, args, env, projectId, cwd, type = 'ideation') => {
if (type === 'roadmap') {
return this.executeRoadmapSpawn(id, projectPath, args, env, projectId, cwd);
} else {
return this.executeIdeationSpawn(id, projectPath, args, env, projectId, cwd);
}
});
}
/**
@@ -352,109 +318,6 @@ export class AgentQueueManager {
await this.spawnIdeationProcess(projectId, projectPath, args);
}
/**
* Execute the actual spawn of an ideation process
* Extracted to be used by SpawnQueue for sequential execution
*
* This method only spawns the process and adds it to state tracking.
* Event handlers are attached by spawnIdeationProcess() via onSpawn callback.
*
* @param spawnId - Unique spawn ID for this process instance
* @param projectPath - Project path (not used directly but kept for signature)
* @param args - Command-line arguments
* @param env - Environment variables
* @param projectId - Project ID for state tracking
* @param cwd - Working directory for the process
* @returns The spawned ChildProcess
*/
private async executeIdeationSpawn(
spawnId: string,
projectPath: string,
args: string[],
env: Record<string, string>,
projectId: string,
cwd: string
): Promise<ChildProcess> {
debugLog('[Agent Queue] Executing ideation spawn:', { spawnId, projectId });
// Get Python path from process manager (uses venv if configured)
const pythonPath = this.processManager.getPythonPath();
// Validate Python path
if (!pythonPath) {
throw new Error('Python path not configured. Please ensure Python is properly set up in settings.');
}
// Parse Python command to handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(pythonPath);
// Spawn the process
const childProcess = spawn(pythonCommand, [...pythonBaseArgs, ...args], {
cwd,
env
});
// Add to state tracking
this.state.addProcess(projectId, {
taskId: projectId,
process: childProcess,
startedAt: new Date(),
projectPath, // Store project path for loading session on completion
spawnId: parseInt(spawnId, 10),
queueProcessType: 'ideation'
});
debugLog('[Agent Queue] Ideation process spawned:', { spawnId, projectId, pid: childProcess.pid });
return childProcess;
}
/**
* Execute roadmap spawn - called by SpawnQueue when request reaches front of queue
* This method only spawns the process; all event handlers are attached in spawnRoadmapProcess's onSpawn callback
*/
private async executeRoadmapSpawn(
spawnId: string,
projectPath: string,
args: string[],
env: Record<string, string>,
projectId: string,
cwd: string
): Promise<ChildProcess> {
debugLog('[Agent Queue] Executing roadmap spawn:', { spawnId, projectId });
// Get Python path from process manager (uses venv if configured)
const pythonPath = this.processManager.getPythonPath();
// Validate Python path
if (!pythonPath) {
throw new Error('Python path not configured. Please ensure Python is properly set up in settings.');
}
// Parse Python command to handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(pythonPath);
// Spawn the process
const childProcess = spawn(pythonCommand, [...pythonBaseArgs, ...args], {
cwd,
env
});
// Add to state tracking
this.state.addProcess(projectId, {
taskId: projectId,
process: childProcess,
startedAt: new Date(),
projectPath, // Store project path for loading roadmap on completion
spawnId: parseInt(spawnId, 10),
queueProcessType: 'roadmap'
});
debugLog('[Agent Queue] Roadmap process spawned:', { spawnId, projectId, pid: childProcess.pid });
return childProcess;
}
/**
* Spawn a Python process for ideation generation
*/
@@ -489,7 +352,7 @@ export class AgentQueueManager {
const combinedEnv = this.processManager.getCombinedEnv(projectPath);
// Get best available Claude profile environment (automatically handles rate limits)
const profileResult = await getBestAvailableProfileEnv();
const profileResult = getBestAvailableProfileEnv();
const profileEnv = profileResult.env;
// Get active API profile environment variables
@@ -499,7 +362,7 @@ export class AgentQueueManager {
const oauthModeClearVars = getOAuthModeClearVars(apiProfileEnv);
// Get Python path from process manager (uses venv if configured)
const _pythonPath = this.processManager.getPythonPath();
const pythonPath = this.processManager.getPythonPath();
// Get Python environment from pythonEnvManager (includes bundled site-packages)
const pythonEnv = pythonEnvManager.getPythonEnv();
@@ -544,47 +407,50 @@ export class AgentQueueManager {
hasToken
});
// Enqueue the spawn request for sequential processing
// The queue will call executeIdeationSpawn() when it's this request's turn
this.spawnQueue.enqueue({
id: String(spawnId),
type: 'ideation',
projectId,
projectPath,
args,
env: finalEnv as Record<string, string>,
// Parse Python command to handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(pythonPath);
const childProcess = spawn(pythonCommand, [...pythonBaseArgs, ...args], {
cwd,
onSpawn: async (childProcess) => {
debugLog('[Agent Queue] Ideation process spawned from queue:', { spawnId, projectId, pid: childProcess.pid });
env: finalEnv
});
// Track progress through output
let progressPhase = 'analyzing';
let progressPercent = 10;
// Collect output for rate limit detection
let allOutput = '';
this.state.addProcess(projectId, {
taskId: projectId,
process: childProcess,
startedAt: new Date(),
projectPath, // Store project path for loading session on completion
spawnId,
queueProcessType: 'ideation'
});
// Helper to emit logs - split multi-line output into individual log lines
const emitLogs = (log: string) => {
const lines = log.split('\n').filter(line => line.trim().length > 0);
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.length > 0) {
this.emitter.emit('ideation-log', projectId, trimmed);
}
}
};
// Track progress through output
let progressPhase = 'analyzing';
let progressPercent = 10;
// Collect output for rate limit detection
let allOutput = '';
// Track completed types for progress calculation
const completedTypes = new Set<string>();
// Derive totalTypes from --types argument instead of hardcoding
const typesArgIndex = args.indexOf('--types');
const totalTypes =
typesArgIndex > -1 && args[typesArgIndex + 1]
? args[typesArgIndex + 1].split(',').length
: 6; // Default to 6 if not specified
// Helper to emit logs - split multi-line output into individual log lines
const emitLogs = (log: string) => {
const lines = log.split('\n').filter(line => line.trim().length > 0);
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.length > 0) {
this.emitter.emit('ideation-log', projectId, trimmed);
}
}
};
// Handle stdout - explicitly decode as UTF-8 for cross-platform Unicode support
childProcess.stdout?.on('data', (data: Buffer) => {
// Track completed types for progress calculation
const completedTypes = new Set<string>();
// Derive totalTypes from --types argument instead of hardcoding
const typesArgIndex = args.findIndex((arg) => arg === '--types');
const totalTypes =
typesArgIndex > -1 && args[typesArgIndex + 1]
? args[typesArgIndex + 1].split(',').length
: 6; // Default to 6 if not specified
// Handle stdout - explicitly decode as UTF-8 for cross-platform Unicode support
childProcess.stdout?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect output for rate limit detection (keep last 10KB)
allOutput = (allOutput + log).slice(-10000);
@@ -671,126 +537,118 @@ export class AgentQueueManager {
});
});
// Handle stderr - also emit as logs, explicitly decode as UTF-8
childProcess.stderr?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect stderr for rate limit detection too
allOutput = (allOutput + log).slice(-10000);
console.error('[Ideation STDERR]', log);
emitLogs(log);
this.emitter.emit('ideation-progress', projectId, {
phase: progressPhase,
progress: progressPercent,
message: formatStatusMessage(log)
// Handle stderr - also emit as logs, explicitly decode as UTF-8
childProcess.stderr?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect stderr for rate limit detection too
allOutput = (allOutput + log).slice(-10000);
console.error('[Ideation STDERR]', log);
emitLogs(log);
this.emitter.emit('ideation-progress', projectId, {
phase: progressPhase,
progress: progressPercent,
message: formatStatusMessage(log)
});
});
// Handle process exit
childProcess.on('exit', (code: number | null) => {
debugLog('[Agent Queue] Ideation process exited:', { projectId, code, spawnId });
// Check if this process was intentionally stopped by the user
const wasIntentionallyStopped = this.state.wasSpawnKilled(spawnId);
if (wasIntentionallyStopped) {
debugLog('[Agent Queue] Ideation process was intentionally stopped, ignoring exit');
this.state.clearKilledSpawn(spawnId);
// Note: Don't call deleteProcess here - killProcess() already deleted it.
// A new process with the same projectId may have been started.
// Emit stopped event to ensure UI updates
this.emitter.emit('ideation-stopped', projectId);
return;
}
// Get the stored project path before deleting from map
const processInfo = this.state.getProcess(projectId);
const storedProjectPath = processInfo?.projectPath;
this.state.deleteProcess(projectId);
// Check for rate limit if process failed
if (code !== 0) {
debugLog('[Agent Queue] Checking for rate limit (non-zero exit)');
const rateLimitDetection = detectRateLimit(allOutput);
if (rateLimitDetection.isRateLimited) {
debugLog('[Agent Queue] Rate limit detected for ideation');
const rateLimitInfo = createSDKRateLimitInfo('ideation', rateLimitDetection, {
projectId
});
this.emitter.emit('sdk-rate-limit', rateLimitInfo);
}
}
if (code === 0) {
debugLog('[Agent Queue] Ideation generation completed successfully');
this.emitter.emit('ideation-progress', projectId, {
phase: 'complete',
progress: 100,
message: 'Ideation generation complete'
});
// Handle process exit
childProcess.on('exit', (code: number | null) => {
debugLog('[Agent Queue] Ideation process exited:', { projectId, code, spawnId });
// Check if this process was intentionally stopped by the user
const wasIntentionallyStopped = this.state.wasSpawnKilled(spawnId);
if (wasIntentionallyStopped) {
debugLog('[Agent Queue] Ideation process was intentionally stopped, ignoring exit');
this.state.clearKilledSpawn(spawnId);
// Note: Don't call deleteProcess here - killProcess() already deleted it.
// A new process with the same projectId may have been started.
// Emit stopped event to ensure UI updates
this.emitter.emit('ideation-stopped', projectId);
return;
}
// Get the stored project path before deleting from map
const processInfo = this.state.getProcess(projectId);
const storedProjectPath = processInfo?.projectPath;
this.state.deleteProcess(projectId);
// Check for rate limit if process failed
if (code !== 0) {
debugLog('[Agent Queue] Checking for rate limit (non-zero exit)');
const rateLimitDetection = detectRateLimit(allOutput);
if (rateLimitDetection.isRateLimited) {
debugLog('[Agent Queue] Rate limit detected for ideation');
const rateLimitInfo = createSDKRateLimitInfo('ideation', rateLimitDetection, {
projectId
});
this.emitter.emit('sdk-rate-limit', rateLimitInfo);
}
}
if (code === 0) {
debugLog('[Agent Queue] Ideation generation completed successfully');
this.emitter.emit('ideation-progress', projectId, {
phase: 'complete',
progress: 100,
message: 'Ideation generation complete'
});
// Load and emit the complete ideation session
if (storedProjectPath) {
try {
const ideationFilePath = path.join(
storedProjectPath,
'.auto-claude',
'ideation',
'ideation.json'
);
debugLog('[Agent Queue] Loading ideation session from:', ideationFilePath);
if (existsSync(ideationFilePath)) {
const loadSession = async (): Promise<void> => {
try {
const content = await fsPromises.readFile(ideationFilePath, 'utf-8');
const rawSession = JSON.parse(content);
const session = transformSessionFromSnakeCase(rawSession, projectId);
debugLog('[Agent Queue] Loaded ideation session:', {
totalIdeas: session.ideas?.length || 0
});
this.emitter.emit('ideation-complete', projectId, session);
} catch (err) {
debugError('[Ideation] Failed to load ideation session:', err);
this.emitter.emit('ideation-error', projectId,
`Failed to load ideation session: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
};
loadSession().catch((err: unknown) => {
debugError('[Agent Queue] Unhandled error loading ideation session:', err);
// Load and emit the complete ideation session
if (storedProjectPath) {
try {
const ideationFilePath = path.join(
storedProjectPath,
'.auto-claude',
'ideation',
'ideation.json'
);
debugLog('[Agent Queue] Loading ideation session from:', ideationFilePath);
if (existsSync(ideationFilePath)) {
const loadSession = async (): Promise<void> => {
try {
const content = await fsPromises.readFile(ideationFilePath, 'utf-8');
const rawSession = JSON.parse(content);
const session = transformSessionFromSnakeCase(rawSession, projectId);
debugLog('[Agent Queue] Loaded ideation session:', {
totalIdeas: session.ideas?.length || 0
});
} else {
debugError('[Ideation] ideation.json not found at:', ideationFilePath);
this.emitter.emit('ideation-complete', projectId, session);
} catch (err) {
debugError('[Ideation] Failed to load ideation session:', err);
this.emitter.emit('ideation-error', projectId,
'Ideation completed but session file not found. Ideas may have been saved to individual type files.');
`Failed to load ideation session: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
} catch (err) {
debugError('[Ideation] Unexpected error in ideation completion:', err);
this.emitter.emit('ideation-error', projectId,
`Failed to load ideation session: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
};
loadSession().catch((err: unknown) => {
debugError('[Agent Queue] Unhandled error loading ideation session:', err);
});
} else {
debugError('[Ideation] No project path available to load session');
debugError('[Ideation] ideation.json not found at:', ideationFilePath);
this.emitter.emit('ideation-error', projectId,
'Ideation completed but project path unavailable');
'Ideation completed but session file not found. Ideas may have been saved to individual type files.');
}
} else {
debugError('[Agent Queue] Ideation generation failed:', { projectId, code });
this.emitter.emit('ideation-error', projectId, `Ideation generation failed with exit code ${code}`);
} catch (err) {
debugError('[Ideation] Unexpected error in ideation completion:', err);
this.emitter.emit('ideation-error', projectId,
`Failed to load ideation session: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
});
// Handle process error
childProcess.on('error', (err: Error) => {
console.error('[Ideation] Process error:', err.message);
this.state.deleteProcess(projectId);
this.emitter.emit('ideation-error', projectId, err.message);
});
},
onError: (error: Error) => {
debugError('[Agent Queue] Failed to spawn ideation process:', error);
this.emitter.emit('ideation-error', projectId, error.message);
} else {
debugError('[Ideation] No project path available to load session');
this.emitter.emit('ideation-error', projectId,
'Ideation completed but project path unavailable');
}
} else {
debugError('[Agent Queue] Ideation generation failed:', { projectId, code });
this.emitter.emit('ideation-error', projectId, `Ideation generation failed with exit code ${code}`);
}
});
debugLog('[Agent Queue] Ideation spawn request enqueued:', { spawnId, projectId });
// Handle process error
childProcess.on('error', (err: Error) => {
console.error('[Ideation] Process error:', err.message);
this.state.deleteProcess(projectId);
this.emitter.emit('ideation-error', projectId, err.message);
});
}
/**
@@ -827,7 +685,7 @@ export class AgentQueueManager {
const combinedEnv = this.processManager.getCombinedEnv(projectPath);
// Get best available Claude profile environment (automatically handles rate limits)
const profileResult = await getBestAvailableProfileEnv();
const profileResult = getBestAvailableProfileEnv();
const profileEnv = profileResult.env;
// Get active API profile environment variables
@@ -837,7 +695,7 @@ export class AgentQueueManager {
const oauthModeClearVars = getOAuthModeClearVars(apiProfileEnv);
// Get Python path from process manager (uses venv if configured)
const _pythonPath = this.processManager.getPythonPath();
const pythonPath = this.processManager.getPythonPath();
// Get Python environment from pythonEnvManager (includes bundled site-packages)
const pythonEnv = pythonEnvManager.getPythonEnv();
@@ -882,223 +740,218 @@ export class AgentQueueManager {
hasToken
});
// Enqueue the spawn request for sequential processing
// The queue will call executeRoadmapSpawn() when it's this request's turn
this.spawnQueue.enqueue({
id: String(spawnId),
type: 'roadmap',
projectId,
projectPath,
args,
env: finalEnv as Record<string, string>,
// Parse Python command to handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(pythonPath);
const childProcess = spawn(pythonCommand, [...pythonBaseArgs, ...args], {
cwd,
onSpawn: async (childProcess) => {
debugLog('[Agent Queue] Roadmap process spawned from queue:', { spawnId, projectId, pid: childProcess.pid });
env: finalEnv
});
// Track progress through output
let progressPhase = 'analyzing';
let progressPercent = 10;
// Collect output for rate limit detection
let allRoadmapOutput = '';
// Track startedAt timestamp for progress persistence
const roadmapStartedAt = new Date().toISOString();
this.state.addProcess(projectId, {
taskId: projectId,
process: childProcess,
startedAt: new Date(),
projectPath, // Store project path for loading roadmap on completion
spawnId,
queueProcessType: 'roadmap'
});
// Persist initial progress state (debounced - will execute immediately due to leading: true)
this.debouncedPersistRoadmapProgress(
projectPath,
progressPhase,
progressPercent,
'Starting roadmap generation...',
roadmapStartedAt,
true
);
// Track progress through output
let progressPhase = 'analyzing';
let progressPercent = 10;
// Collect output for rate limit detection
let allRoadmapOutput = '';
// Track startedAt timestamp for progress persistence
const roadmapStartedAt = new Date().toISOString();
// Helper to emit logs - split multi-line output into individual log lines
const emitLogs = (log: string) => {
const lines = log.split('\n').filter(line => line.trim().length > 0);
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.length > 0) {
this.emitter.emit('roadmap-log', projectId, trimmed);
}
}
};
// Persist initial progress state (debounced - will execute immediately due to leading: true)
this.debouncedPersistRoadmapProgress(
projectPath,
progressPhase,
progressPercent,
'Starting roadmap generation...',
roadmapStartedAt,
true
);
// Handle stdout - explicitly decode as UTF-8 for cross-platform Unicode support
childProcess.stdout?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect output for rate limit detection (keep last 10KB)
allRoadmapOutput = (allRoadmapOutput + log).slice(-10000);
// Helper to emit logs - split multi-line output into individual log lines
const emitLogs = (log: string) => {
const lines = log.split('\n').filter(line => line.trim().length > 0);
for (const line of lines) {
const trimmed = line.trim();
if (trimmed.length > 0) {
this.emitter.emit('roadmap-log', projectId, trimmed);
}
}
};
// Emit all log lines for debugging
emitLogs(log);
// Handle stdout - explicitly decode as UTF-8 for cross-platform Unicode support
childProcess.stdout?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect output for rate limit detection (keep last 10KB)
allRoadmapOutput = (allRoadmapOutput + log).slice(-10000);
// Parse progress using AgentEvents
const progressUpdate = this.events.parseRoadmapProgress(log, progressPhase, progressPercent);
progressPhase = progressUpdate.phase;
progressPercent = progressUpdate.progress;
// Emit all log lines for debugging
emitLogs(log);
// Get status message for display
const statusMessage = formatStatusMessage(log);
// Parse progress using AgentEvents
const progressUpdate = this.events.parseRoadmapProgress(log, progressPhase, progressPercent);
progressPhase = progressUpdate.phase;
progressPercent = progressUpdate.progress;
// Persist progress to disk for recovery after restart (debounced to limit writes)
this.debouncedPersistRoadmapProgress(
projectPath,
progressPhase,
progressPercent,
statusMessage,
roadmapStartedAt,
true
);
// Get status message for display
const statusMessage = formatStatusMessage(log);
// Emit progress update
this.emitter.emit('roadmap-progress', projectId, {
phase: progressPhase,
progress: progressPercent,
message: statusMessage
// Persist progress to disk for recovery after restart (debounced to limit writes)
this.debouncedPersistRoadmapProgress(
projectPath,
progressPhase,
progressPercent,
statusMessage,
roadmapStartedAt,
true
);
// Emit progress update
this.emitter.emit('roadmap-progress', projectId, {
phase: progressPhase,
progress: progressPercent,
message: statusMessage
});
});
// Handle stderr - explicitly decode as UTF-8
childProcess.stderr?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect stderr for rate limit detection too
allRoadmapOutput = (allRoadmapOutput + log).slice(-10000);
console.error('[Roadmap STDERR]', log);
emitLogs(log);
const statusMessage = formatStatusMessage(log);
// Persist progress to disk (debounced - also on stderr to show activity)
this.debouncedPersistRoadmapProgress(
projectPath,
progressPhase,
progressPercent,
statusMessage,
roadmapStartedAt,
true
);
this.emitter.emit('roadmap-progress', projectId, {
phase: progressPhase,
progress: progressPercent,
message: statusMessage
});
});
// Handle process exit
childProcess.on('exit', (code: number | null) => {
debugLog('[Agent Queue] Roadmap process exited:', { projectId, code, spawnId });
// Check if this process was intentionally stopped by the user
const wasIntentionallyStopped = this.state.wasSpawnKilled(spawnId);
if (wasIntentionallyStopped) {
debugLog('[Agent Queue] Roadmap process was intentionally stopped, ignoring exit');
this.state.clearKilledSpawn(spawnId);
// Clear progress file on intentional stop
this.clearRoadmapProgress(projectPath);
// Note: Don't call deleteProcess here - killProcess() already deleted it.
// A new process with the same projectId may have been started.
return;
}
// Get the stored project path before deleting from map
const processInfo = this.state.getProcess(projectId);
const storedProjectPath = processInfo?.projectPath;
this.state.deleteProcess(projectId);
// Check for rate limit if process failed
if (code !== 0) {
debugLog('[Agent Queue] Checking for rate limit (non-zero exit)');
const rateLimitDetection = detectRateLimit(allRoadmapOutput);
if (rateLimitDetection.isRateLimited) {
debugLog('[Agent Queue] Rate limit detected for roadmap');
const rateLimitInfo = createSDKRateLimitInfo('roadmap', rateLimitDetection, {
projectId
});
this.emitter.emit('sdk-rate-limit', rateLimitInfo);
}
}
if (code === 0) {
debugLog('[Agent Queue] Roadmap generation completed successfully');
this.emitter.emit('roadmap-progress', projectId, {
phase: 'complete',
progress: 100,
message: 'Roadmap generation complete'
});
// Handle stderr - explicitly decode as UTF-8
childProcess.stderr?.on('data', (data: Buffer) => {
const log = data.toString('utf-8');
// Collect stderr for rate limit detection too
allRoadmapOutput = (allRoadmapOutput + log).slice(-10000);
console.error('[Roadmap STDERR]', log);
emitLogs(log);
// Clear progress file on successful completion
this.clearRoadmapProgress(projectPath);
const statusMessage = formatStatusMessage(log);
// Persist progress to disk (debounced - also on stderr to show activity)
this.debouncedPersistRoadmapProgress(
projectPath,
progressPhase,
progressPercent,
statusMessage,
roadmapStartedAt,
true
);
this.emitter.emit('roadmap-progress', projectId, {
phase: progressPhase,
progress: progressPercent,
message: statusMessage
});
});
// Handle process exit
childProcess.on('exit', (code: number | null) => {
debugLog('[Agent Queue] Roadmap process exited:', { projectId, code, spawnId });
// Check if this process was intentionally stopped by the user
const wasIntentionallyStopped = this.state.wasSpawnKilled(spawnId);
if (wasIntentionallyStopped) {
debugLog('[Agent Queue] Roadmap process was intentionally stopped, ignoring exit');
this.state.clearKilledSpawn(spawnId);
// Clear progress file on intentional stop
this.clearRoadmapProgress(projectPath);
// Note: Don't call deleteProcess here - killProcess() already deleted it.
// A new process with the same projectId may have been started.
return;
}
// Get the stored project path before deleting from map
const processInfo = this.state.getProcess(projectId);
const storedProjectPath = processInfo?.projectPath;
this.state.deleteProcess(projectId);
// Check for rate limit if process failed
if (code !== 0) {
debugLog('[Agent Queue] Checking for rate limit (non-zero exit)');
const rateLimitDetection = detectRateLimit(allRoadmapOutput);
if (rateLimitDetection.isRateLimited) {
debugLog('[Agent Queue] Rate limit detected for roadmap');
const rateLimitInfo = createSDKRateLimitInfo('roadmap', rateLimitDetection, {
projectId
});
this.emitter.emit('sdk-rate-limit', rateLimitInfo);
}
}
if (code === 0) {
debugLog('[Agent Queue] Roadmap generation completed successfully');
this.emitter.emit('roadmap-progress', projectId, {
phase: 'complete',
progress: 100,
message: 'Roadmap generation complete'
});
// Clear progress file on successful completion
this.clearRoadmapProgress(projectPath);
// Load and emit the complete roadmap
if (storedProjectPath) {
try {
const roadmapFilePath = path.join(
storedProjectPath,
'.auto-claude',
'roadmap',
'roadmap.json'
);
debugLog('[Agent Queue] Loading roadmap from:', roadmapFilePath);
if (existsSync(roadmapFilePath)) {
const loadRoadmap = async (): Promise<void> => {
try {
const content = await fsPromises.readFile(roadmapFilePath, 'utf-8');
const rawRoadmap = JSON.parse(content);
const transformedRoadmap = transformRoadmapFromSnakeCase(rawRoadmap, projectId);
debugLog('[Agent Queue] Loaded roadmap:', {
featuresCount: transformedRoadmap.features?.length || 0,
phasesCount: transformedRoadmap.phases?.length || 0
});
this.emitter.emit('roadmap-complete', projectId, transformedRoadmap);
} catch (err) {
debugError('[Roadmap] Failed to load roadmap:', err);
this.emitter.emit('roadmap-error', projectId,
`Failed to load roadmap: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
};
loadRoadmap().catch((err: unknown) => {
debugError('[Agent Queue] Unhandled error loading roadmap:', err);
// Load and emit the complete roadmap
if (storedProjectPath) {
try {
const roadmapFilePath = path.join(
storedProjectPath,
'.auto-claude',
'roadmap',
'roadmap.json'
);
debugLog('[Agent Queue] Loading roadmap from:', roadmapFilePath);
if (existsSync(roadmapFilePath)) {
const loadRoadmap = async (): Promise<void> => {
try {
const content = await fsPromises.readFile(roadmapFilePath, 'utf-8');
const rawRoadmap = JSON.parse(content);
const transformedRoadmap = transformRoadmapFromSnakeCase(rawRoadmap, projectId);
debugLog('[Agent Queue] Loaded roadmap:', {
featuresCount: transformedRoadmap.features?.length || 0,
phasesCount: transformedRoadmap.phases?.length || 0
});
} else {
debugError('[Roadmap] roadmap.json not found at:', roadmapFilePath);
this.emitter.emit('roadmap-complete', projectId, transformedRoadmap);
} catch (err) {
debugError('[Roadmap] Failed to load roadmap:', err);
this.emitter.emit('roadmap-error', projectId,
'Roadmap completed but file not found.');
`Failed to load roadmap: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
} catch (err) {
debugError('[Roadmap] Unexpected error in roadmap completion:', err);
this.emitter.emit('roadmap-error', projectId,
`Unexpected error: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
};
loadRoadmap().catch((err: unknown) => {
debugError('[Agent Queue] Unhandled error loading roadmap:', err);
});
} else {
debugError('[Roadmap] No project path available for roadmap completion');
this.emitter.emit('roadmap-error', projectId, 'Roadmap completed but project path not found.');
debugError('[Roadmap] roadmap.json not found at:', roadmapFilePath);
this.emitter.emit('roadmap-error', projectId,
'Roadmap completed but file not found.');
}
} else {
debugError('[Agent Queue] Roadmap generation failed:', { projectId, code });
// Clear progress file on error
this.clearRoadmapProgress(projectPath);
this.emitter.emit('roadmap-error', projectId, `Roadmap generation failed with exit code ${code}`);
} catch (err) {
debugError('[Roadmap] Unexpected error in roadmap completion:', err);
this.emitter.emit('roadmap-error', projectId,
`Unexpected error: ${err instanceof Error ? err.message : 'Unknown error'}`);
}
});
// Handle process error
childProcess.on('error', (err: Error) => {
console.error('[Roadmap] Process error:', err.message);
this.state.deleteProcess(projectId);
// Clear progress file on process error
this.clearRoadmapProgress(projectPath);
this.emitter.emit('roadmap-error', projectId, err.message);
});
},
onError: (error: Error) => {
debugError('[Agent Queue] Failed to spawn roadmap process:', error);
this.emitter.emit('roadmap-error', projectId, error.message);
} else {
debugError('[Roadmap] No project path available for roadmap completion');
this.emitter.emit('roadmap-error', projectId, 'Roadmap completed but project path not found.');
}
} else {
debugError('[Agent Queue] Roadmap generation failed:', { projectId, code });
// Clear progress file on error
this.clearRoadmapProgress(projectPath);
this.emitter.emit('roadmap-error', projectId, `Roadmap generation failed with exit code ${code}`);
}
});
debugLog('[Agent Queue] Roadmap spawn request enqueued:', { spawnId, projectId });
// Handle process error
childProcess.on('error', (err: Error) => {
console.error('[Roadmap] Process error:', err.message);
this.state.deleteProcess(projectId);
// Clear progress file on process error
this.clearRoadmapProgress(projectPath);
this.emitter.emit('roadmap-error', projectId, err.message);
});
}
/**
@@ -1,386 +0,0 @@
/**
* Integration Stress Test for Sequential Agent Spawning
*
* Verifies that SpawnQueue prevents ~/.claude.json file corruption under concurrent load.
* Tests rapid spawning of multiple agents and verifies sequential execution.
*
* Key scenarios:
* - Stress test: 10 agents spawned rapidly
* - Sequential verification: agents don't overlap
* - Queue state consistency during high load
*/
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { SpawnQueue, type SpawnFunction } from './spawn-queue';
import type { ChildProcess } from 'child_process';
import type { Readable, Writable } from 'stream';
describe('Sequential Agent Spawning - Integration Stress Test', () => {
let queue: SpawnQueue;
let mockSpawnFn: SpawnFunction;
let concurrentProcesses: Map<string, { start: number; end: number }>;
/**
* Create a mock child process that exits after a delay
* Tracks start/end times for overlap detection
*/
const createMockProcess = (
id: string,
exitDelay: number = 10
): ChildProcess => {
const startTime = Date.now();
const process = {
on: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
if (event === 'exit') {
setTimeout(() => {
const endTime = Date.now();
concurrentProcesses.set(id, { start: startTime, end: endTime });
callback(0);
}, exitDelay);
}
return process;
}),
once: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
if (event === 'exit') {
setTimeout(() => {
const endTime = Date.now();
concurrentProcesses.set(id, { start: startTime, end: endTime });
callback(0);
}, exitDelay);
}
return process;
}),
kill: vi.fn(),
pid: Math.floor(Math.random() * 100000),
exitCode: null,
signalCode: null,
stdin: null,
stdout: null,
stderr: null,
stdio: [null, null, null] as [
Writable | null,
Readable | null,
Readable | null
],
connected: false
} as unknown as ChildProcess;
return process;
};
/**
* Helper to create a spawn request
*/
const createRequest = (
id: string,
_exitDelay: number = 10
) => ({
id,
type: 'test',
onSpawn: vi.fn(async () => { /* noop */ }),
onError: vi.fn(),
projectId: `project-${id}`,
projectPath: `/test/path/${id}`,
args: ['--test', id],
env: { TEST_ID: id },
cwd: '/test/cwd'
});
beforeEach(() => {
concurrentProcesses = new Map();
// Suppress console.log output in tests
vi.spyOn(console, 'log').mockImplementation(() => { /* noop */ });
// Mock spawn function that creates processes with different exit delays
mockSpawnFn = vi.fn(async (id: string) => {
// Use deterministic delays to avoid non-determinism
const delays = [10, 15, 20, 25, 30];
const index = parseInt(id.split('-')[1], 10) || 0;
const delay = delays[index % delays.length];
return createMockProcess(id, delay);
}) as SpawnFunction;
queue = new SpawnQueue(mockSpawnFn);
});
afterEach(() => {
vi.clearAllMocks();
});
describe('Stress Test - Rapid Concurrent Spawns', () => {
it('should handle 10 rapid spawn requests without corruption', async () => {
const numAgents = 10;
const executionOrder: string[] = [];
// Enqueue 10 agents rapidly
for (let i = 0; i < numAgents; i++) {
const id = `agent-${i}`;
const request = createRequest(id);
request.onSpawn = vi.fn(async () => {
executionOrder.push(id);
});
queue.enqueue(request);
}
// Wait for all to complete
await queue.drain();
// Verify all agents executed
expect(executionOrder).toHaveLength(numAgents);
// Verify FIFO order
for (let i = 0; i < numAgents; i++) {
expect(executionOrder[i]).toBe(`agent-${i}`);
}
// Verify all spawn functions were called
expect(mockSpawnFn).toHaveBeenCalledTimes(numAgents);
});
it('should maintain queue integrity under high load', async () => {
const numAgents = 10;
let maxLengthDuringProcessing = 0;
// Create a request that tracks queue length during processing
const createTrackingRequest = (id: string) => {
const request = createRequest(id);
const originalOnSpawn = request.onSpawn;
request.onSpawn = vi.fn(async () => {
// Track max queue length during processing
if (queue.length > maxLengthDuringProcessing) {
maxLengthDuringProcessing = queue.length;
}
await originalOnSpawn();
});
return request;
};
// Enqueue all agents rapidly
for (let i = 0; i < numAgents; i++) {
queue.enqueue(createTrackingRequest(`agent-${i}`));
}
await queue.drain();
// Verify queue eventually emptied
expect(queue.length).toBe(0);
expect(queue.isProcessing).toBe(false);
// Verify queue held pending items during processing
expect(maxLengthDuringProcessing).toBeGreaterThan(0);
});
});
describe('Sequential Verification - No Overlap', () => {
it('should ensure agents execute sequentially without overlap', async () => {
const numAgents = 5;
// Enqueue agents with varying delays
for (let i = 0; i < numAgents; i++) {
queue.enqueue(createRequest(`agent-${i}`, 20));
}
await queue.drain();
// Verify we tracked all agents
expect(concurrentProcesses.size).toBe(numAgents);
// Check for overlaps: each agent should finish before the next starts
const sortedEntries = Array.from(concurrentProcesses.entries()).sort(
(a, b) => a[1].start - b[1].start
);
for (let i = 0; i < sortedEntries.length - 1; i++) {
const [_currentId, currentTimes] = sortedEntries[i];
const [_nextId, nextTimes] = sortedEntries[i + 1];
// Next agent should start after current agent finishes
// Add tolerance for timing precision
expect(nextTimes.start).toBeGreaterThanOrEqual(currentTimes.end - 1);
}
});
it('should track start and end times accurately', async () => {
const testId = 'test-agent';
let spawnedTime: number | null = null;
// Create a request that tracks timing
const request = createRequest(testId, 30);
// Override mockSpawnFn to track timing more accurately
mockSpawnFn = vi.fn(async () => {
const startTime = Date.now();
const process = createMockProcess(testId, 30);
// Track when the process is spawned
request.onSpawn = vi.fn(async () => {
spawnedTime = startTime;
});
return process;
}) as SpawnFunction;
queue = new SpawnQueue(mockSpawnFn);
queue.enqueue(request);
await queue.drain();
// Verify spawn time was captured
expect(spawnedTime).not.toBeNull();
// Verify the process completed
const times = concurrentProcesses.get(testId);
expect(times).toBeDefined();
// Exit should be after spawn
expect(times?.end).toBeGreaterThanOrEqual(spawnedTime!);
});
});
describe('Error Recovery Under Load', () => {
it('should continue processing after failures', async () => {
const successCount: string[] = [];
const failureCount: string[] = [];
const createFailableRequest = (id: string, shouldFail: boolean) => {
const request = createRequest(id);
request.onSpawn = vi.fn(async () => {
if (shouldFail) {
throw new Error(`Simulated failure for ${id}`);
}
successCount.push(id);
});
request.onError = vi.fn((error: Error) => {
failureCount.push(id);
expect(error.message).toContain('Simulated failure');
});
return request;
};
// Enqueue mix of successful and failing requests
queue.enqueue(createFailableRequest('agent-1', false));
queue.enqueue(createFailableRequest('agent-2', true));
queue.enqueue(createFailableRequest('agent-3', false));
queue.enqueue(createFailableRequest('agent-4', true));
queue.enqueue(createFailableRequest('agent-5', false));
await queue.drain();
// Verify all were processed
expect(successCount).toHaveLength(3);
expect(failureCount).toHaveLength(2);
// Verify processing continued despite failures
expect(successCount).toEqual(['agent-1', 'agent-3', 'agent-5']);
expect(failureCount).toEqual(['agent-2', 'agent-4']);
});
it('should handle all failures gracefully', async () => {
const numAgents = 5;
const errors: string[] = [];
const createFailingRequest = (id: string) => {
const request = createRequest(id);
request.onSpawn = vi.fn(async () => {
throw new Error(`Failure ${id}`);
});
request.onError = vi.fn((_error: Error) => {
errors.push(id);
});
return request;
};
// All requests fail
for (let i = 0; i < numAgents; i++) {
queue.enqueue(createFailingRequest(`agent-${i}`));
}
await queue.drain();
// Verify all errors were handled
expect(errors).toHaveLength(numAgents);
// Queue should still be empty and not processing
expect(queue.length).toBe(0);
expect(queue.isProcessing).toBe(false);
});
});
describe('Real-World Scenario Simulation', () => {
it('should simulate user rapidly triggering multiple ideations', async () => {
const userTriggeredIdeations: string[] = [];
// Simulate user clicking "Generate Ideation" 5 times rapidly
const triggerIdeation = async (index: number) => {
const projectId = `project-${index}`;
userTriggeredIdeations.push(projectId);
queue.enqueue({
id: `ideation-${index}`,
type: 'ideation',
projectId,
projectPath: `/projects/${projectId}`,
args: ['--ideation', '--types', 'improvements,performance'],
env: { PROJECT_ID: projectId },
cwd: '/auto-claude',
onSpawn: vi.fn(async () => {
// Ideation spawned
}),
onError: vi.fn((_error: Error) => {
// Ideation failed
})
});
};
// User rapidly triggers 5 ideations (within 100ms)
const startTime = Date.now();
await Promise.all(
Array.from({ length: 5 }, (_, i) => triggerIdeation(i))
);
const _triggerTime = Date.now() - startTime;
// Wait for all to complete
await queue.drain();
// Verify all were processed
expect(mockSpawnFn).toHaveBeenCalledTimes(5);
// Verify sequential execution
const sortedEntries = Array.from(concurrentProcesses.entries()).sort(
(a, b) => a[1].start - b[1].start
);
for (let i = 0; i < sortedEntries.length - 1; i++) {
const currentEnd = sortedEntries[i][1].end;
const nextStart = sortedEntries[i + 1][1].start;
expect(nextStart).toBeGreaterThanOrEqual(currentEnd);
}
});
});
describe('Performance Characteristics', () => {
it('should measure throughput under load', async () => {
const numAgents = 10;
const startTime = Date.now();
for (let i = 0; i < numAgents; i++) {
queue.enqueue(createRequest(`agent-${i}`, 5));
}
await queue.drain();
const totalTime = Date.now() - startTime;
// With 10 agents each taking ~5ms + overhead, sequential execution
// should take roughly 50-100ms (much slower than parallel, but safe)
expect(totalTime).toBeGreaterThan(40); // At least 40ms for sequential
expect(totalTime).toBeLessThan(500); // But should complete in reasonable time
// Verify all completed
expect(mockSpawnFn).toHaveBeenCalledTimes(numAgents);
});
});
});
@@ -1,320 +0,0 @@
/**
* Tests for SpawnQueue - Sequential Agent Spawning
*
* Tests the FIFO queue that ensures only one agent runs at a time
* to prevent ~/.claude.json race condition and file corruption.
*/
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { SpawnQueue, type SpawnFunction } from './spawn-queue';
import type { ChildProcess } from 'child_process';
import type { Readable, Writable } from 'stream';
describe('SpawnQueue', () => {
let queue: SpawnQueue;
let mockSpawnFn: SpawnFunction;
let mockChildProcess: ChildProcess;
// Helper to create a valid spawn request
const createRequest = (overrides: Partial<{
id: string;
type: string;
onSpawn: (process: ChildProcess) => Promise<void>;
onError: (error: Error) => void;
projectId: string;
projectPath: string;
args: string[];
env: Record<string, string>;
cwd: string;
}> = {}) => ({
id: 'test-id',
type: 'test',
onSpawn: vi.fn(async () => { /* noop */ }),
onError: vi.fn(),
projectId: 'test-project',
projectPath: '/test/path',
args: [],
env: {},
cwd: '/test/cwd',
...overrides
});
beforeEach(() => {
// Mock child process that exits successfully
mockChildProcess = {
on: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
if (event === 'exit') {
// Simulate immediate exit for testing
setTimeout(() => callback(0), 0);
}
return mockChildProcess as ChildProcess;
}),
once: vi.fn((event: string, callback: (...args: unknown[]) => void) => {
if (event === 'exit') {
// Simulate immediate exit for testing
setTimeout(() => callback(0), 0);
}
return mockChildProcess as ChildProcess;
}),
kill: vi.fn(),
pid: 12345,
exitCode: null,
signalCode: null,
stdin: null,
stdout: null,
stderr: null,
stdio: [null, null, null] as [
Writable | null,
Readable | null,
Readable | null
],
connected: false
} as unknown as ChildProcess;
// Mock spawn function with proper type
mockSpawnFn = vi.fn().mockResolvedValue(mockChildProcess) as SpawnFunction;
// Create queue with mock spawn function
queue = new SpawnQueue(mockSpawnFn);
});
describe('Sequential Processing', () => {
it('should process items in FIFO order', async () => {
const executionOrder: string[] = [];
// Create three spawn requests that track execution order
const request1 = createRequest({
id: 'task-1',
onSpawn: vi.fn(async () => {
executionOrder.push('task-1');
})
});
const request2 = createRequest({
id: 'task-2',
onSpawn: vi.fn(async () => {
executionOrder.push('task-2');
})
});
const request3 = createRequest({
id: 'task-3',
onSpawn: vi.fn(async () => {
executionOrder.push('task-3');
})
});
// Enqueue all three items
queue.enqueue(request1);
queue.enqueue(request2);
queue.enqueue(request3);
// Wait for all to complete
await queue.drain();
// Verify they executed in FIFO order
expect(executionOrder).toEqual(['task-1', 'task-2', 'task-3']);
expect(request1.onSpawn).toHaveBeenCalledTimes(1);
expect(request2.onSpawn).toHaveBeenCalledTimes(1);
expect(request3.onSpawn).toHaveBeenCalledTimes(1);
});
it('should wait for each spawn to complete before starting next', async () => {
let task1Running = false;
let task2Started = false;
const request1 = createRequest({
id: 'task-1',
onSpawn: vi.fn(async () => {
task1Running = true;
// Simulate work
await new Promise(resolve => setTimeout(resolve, 50));
task1Running = false;
})
});
const request2 = createRequest({
id: 'task-2',
onSpawn: vi.fn(async () => {
task2Started = true;
// Task 2 should only start after task 1 completes
expect(task1Running).toBe(false);
})
});
queue.enqueue(request1);
queue.enqueue(request2);
await queue.drain();
expect(task1Running).toBe(false);
expect(task2Started).toBe(true);
});
});
describe('Error Recovery', () => {
it('should continue to next item when spawn fails', async () => {
const executionOrder: string[] = [];
// First request fails
const request1 = createRequest({
id: 'task-1',
onSpawn: vi.fn().mockRejectedValue(new Error('Spawn failed')),
onError: vi.fn((error: Error) => {
executionOrder.push('task-1-error');
expect(error.message).toBe('Spawn failed');
})
});
// Second request succeeds
const request2 = createRequest({
id: 'task-2',
onSpawn: vi.fn(async () => {
executionOrder.push('task-2');
})
});
queue.enqueue(request1);
queue.enqueue(request2);
await queue.drain();
// Verify both were processed in order
expect(executionOrder).toEqual(['task-1-error', 'task-2']);
expect(request1.onSpawn).toHaveBeenCalledTimes(1);
expect(request2.onSpawn).toHaveBeenCalledTimes(1);
expect(request1.onError).toHaveBeenCalledTimes(1);
expect(request2.onError).not.toHaveBeenCalled();
});
it('should handle multiple failures gracefully', async () => {
let errorCount = 0;
const failingRequest = createRequest({
id: `task-${errorCount}`,
onSpawn: vi.fn().mockRejectedValue(new Error('Failed')),
onError: vi.fn((_error: Error) => {
errorCount++;
})
});
// Enqueue multiple failing requests
queue.enqueue({ ...failingRequest, id: 'task-1' });
queue.enqueue({ ...failingRequest, id: 'task-2' });
queue.enqueue({ ...failingRequest, id: 'task-3' });
await queue.drain();
expect(errorCount).toBe(3);
});
});
describe('Empty Queue', () => {
it('should handle drain gracefully when queue is empty', async () => {
// Drain should resolve immediately with no items
await expect(queue.drain()).resolves.toBeUndefined();
});
it('should return zero length for empty queue', () => {
expect(queue.length).toBe(0);
});
it('should not be processing when queue is empty', () => {
expect(queue.isProcessing).toBe(false);
});
});
describe('Queue State', () => {
it('should track queue length correctly', async () => {
expect(queue.length).toBe(0);
// Enqueue first item - it will start processing immediately
queue.enqueue(createRequest({
id: 'task-1',
onSpawn: vi.fn(async () => {
// While task-1 is processing, check that subsequent items are queued
queue.enqueue(createRequest({
id: 'task-2',
onSpawn: vi.fn()
}));
// Task 2 should be queued while task 1 processes
expect(queue.length).toBe(1);
queue.enqueue(createRequest({
id: 'task-3',
onSpawn: vi.fn()
}));
// Now both task 2 and task 3 are queued
expect(queue.length).toBe(2);
})
}));
// Wait for all to complete
await queue.drain();
// Queue should be empty after all processing
expect(queue.length).toBe(0);
});
it('should track processing state', async () => {
expect(queue.isProcessing).toBe(false);
const request = createRequest({
id: 'task-1',
onSpawn: vi.fn(async () => {
// Check that processing is true during execution
expect(queue.isProcessing).toBe(true);
})
});
queue.enqueue(request);
expect(queue.isProcessing).toBe(true);
await queue.drain();
expect(queue.isProcessing).toBe(false);
});
});
describe('Spawn Function Integration', () => {
it('should call spawn function with correct arguments', async () => {
const request = createRequest({
id: 'task-1',
projectId: 'project-1',
projectPath: '/path/to/project',
args: ['--test', '--verbose'],
env: { TEST_VAR: 'test-value' },
cwd: '/test/cwd'
});
queue.enqueue(request);
await queue.drain();
expect(mockSpawnFn).toHaveBeenCalledTimes(1);
expect(mockSpawnFn).toHaveBeenCalledWith(
'task-1',
'/path/to/project',
['--test', '--verbose'],
{ TEST_VAR: 'test-value' },
'project-1',
'/test/cwd'
);
});
it('should pass spawned process to onSpawn callback', async () => {
let receivedProcess: import('child_process').ChildProcess | undefined;
const request = createRequest({
id: 'task-1',
onSpawn: vi.fn(async (process: import('child_process').ChildProcess) => {
receivedProcess = process;
})
});
queue.enqueue(request);
await queue.drain();
expect(receivedProcess).toBeDefined();
expect(receivedProcess?.pid).toBe(12345);
});
});
});
-177
View File
@@ -1,177 +0,0 @@
/**
* FIFO Queue for Sequential Agent Spawning
*
* Ensures only one agent spawns at a time to prevent ~/.claude.json
* race condition and file corruption from concurrent writes.
*
* Key behaviors:
* - Processes items FIFO (first in, first out)
* - Waits for each agent to exit before spawning the next
* - Continues to next item if spawn fails (error resilience)
* - Provides drain() method to wait for all queued items
*/
import type { ChildProcess } from 'child_process';
/** Poll interval for drain() method in milliseconds */
const DRAIN_POLL_INTERVAL = 10;
/**
* Request to spawn an agent process
*/
export interface SpawnRequest {
/** Unique identifier for this spawn request */
id: string;
/** Type of process being spawned ('ideation' | 'roadmap' | 'build') */
type: string;
/** Callback invoked when process is spawned (receives ChildProcess) */
onSpawn: (process: ChildProcess) => Promise<void>;
/** Callback invoked if spawn fails */
onError: (error: Error) => void;
/** Project ID for the task */
projectId: string;
/** Project path where the task runs */
projectPath: string;
/** Command-line arguments to pass to the process */
args: string[];
/** Environment variables for the process */
env: Record<string, string>;
/** Working directory for the process */
cwd: string;
}
/**
* Function type for spawning a process
* Abstracted for testability and dependency injection
*/
export type SpawnFunction = (
id: string,
projectPath: string,
args: string[],
env: Record<string, string>,
projectId: string,
cwd: string
) => Promise<ChildProcess>;
/**
* FIFO queue for sequential agent spawning
*/
export class SpawnQueue {
private queue: SpawnRequest[] = [];
private processing = false;
private spawnFn: SpawnFunction;
constructor(spawnFn: SpawnFunction) {
this.spawnFn = spawnFn;
}
/**
* Add a spawn request to the queue
* Automatically starts processing if not already running
*/
enqueue(request: SpawnRequest): void {
this.queue.push(request);
// Start processing if not already running
if (!this.processing) {
this.processNext().catch((error) => {
console.error('[SpawnQueue] Fatal error processing queue:', error);
this.processing = false;
});
}
}
/**
* Process the next item in the queue
* Continues processing until queue is empty
*/
private async processNext(): Promise<void> {
// Mark as processing
this.processing = true;
while (this.queue.length > 0) {
const request = this.queue.shift();
if (!request) {
continue;
}
try {
// Spawn the process
const process = await this.spawnFn(
request.id,
request.projectPath,
request.args,
request.env,
request.projectId,
request.cwd
);
// Invoke the onSpawn callback
await request.onSpawn(process);
// Wait for the process to exit before continuing
await this.waitForExit(process);
} catch (error) {
// If spawn or onSpawn fails, invoke error callback and continue
const errorObj = error instanceof Error ? error : new Error(String(error));
request.onError(errorObj);
}
}
// Queue is empty, no longer processing
this.processing = false;
}
/**
* Wait for a child process to exit
*/
private waitForExit(process: ChildProcess): Promise<void> {
return new Promise<void>((resolve) => {
// Check if process already exited (handles race condition)
if (process.exitCode !== null) {
resolve();
return;
}
// Set up exit event listener (in case exit hasn't happened yet)
const onExit = () => {
resolve();
};
process.once('exit', onExit);
});
}
/**
* Wait for all queued items to complete
* Uses polling to check completion status
*/
async drain(): Promise<void> {
// Poll until queue is empty and not processing
while (this.queue.length > 0 || this.processing) {
await this.sleep(DRAIN_POLL_INTERVAL);
}
}
/**
* Current queue length
*/
get length(): number {
return this.queue.length;
}
/**
* Whether the queue is currently processing an item
*/
get isProcessing(): boolean {
return this.processing;
}
/**
* Sleep for a specified number of milliseconds
*/
private sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
}
+2 -2
View File
@@ -39,7 +39,7 @@ export function initAppLanguage(): void {
const osLocale = app?.getLocale?.() || 'en';
// Extract base language (e.g., 'en-US' -> 'en')
currentAppLanguage = osLocale.split('-')[0] || 'en';
} catch {
currentAppLanguage = 'en';
} catch {
currentAppLanguage = 'en';
}
}
+1 -1
View File
@@ -24,7 +24,7 @@ import os from 'os';
try {
log.initialize();
} catch {
// Already initialized, ignore
// Already initialized, ignore
}
// File transport configuration
+2 -1
View File
@@ -545,7 +545,8 @@ async function fetchLatestStableRelease(): Promise<AppUpdateInfo | null> {
const version = latestStable.tag_name.replace(/^v/, '');
// Sanitize version string for logging (remove control characters and limit length)
const safeVersion = String(version).replace(/[\x00-\x1f\x7f]/g, '').slice(0, 50);
// biome-ignore lint/suspicious/noControlCharactersInRegex: Intentionally matching control chars for sanitization
const safeVersion = String(version).replace(/[\x00-\x1f\x7f]/g, '').slice(0, 50);
console.warn('[app-updater] Found latest stable release:', safeVersion);
resolve({
@@ -48,7 +48,7 @@ vi.mock('../../platform', () => ({
vi.mock('../../rate-limit-detector', () => ({
detectRateLimit: vi.fn(() => ({ isRateLimited: false })),
createSDKRateLimitInfo: vi.fn(),
getBestAvailableProfileEnv: vi.fn(() => Promise.resolve({
getBestAvailableProfileEnv: vi.fn(() => ({
env: {},
wasSwapped: false,
profileName: 'default'
@@ -169,7 +169,7 @@ export class ChangelogService extends EventEmitter {
return envVars;
} catch {
return {};
return {};
}
}
@@ -141,7 +141,7 @@ export class ChangelogGenerator extends EventEmitter {
this.debug('Spawning Python process...');
// Build environment with explicit critical variables
const spawnEnv = await this.buildSpawnEnvironment();
const spawnEnv = this.buildSpawnEnvironment();
// Parse Python command to handle space-separated commands like "py -3"
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(this.pythonPath);
@@ -286,7 +286,7 @@ export class ChangelogGenerator extends EventEmitter {
/**
* Build spawn environment with proper PATH and auth settings
*/
private async buildSpawnEnvironment(): Promise<Record<string, string>> {
private buildSpawnEnvironment(): Record<string, string> {
const homeDir = os.homedir();
// Use getAugmentedEnv() to ensure common tool paths are available
@@ -294,7 +294,7 @@ export class ChangelogGenerator extends EventEmitter {
const augmentedEnv = getAugmentedEnv();
// Get best available Claude profile environment (automatically handles rate limits)
const profileResult = await getBestAvailableProfileEnv();
const profileResult = getBestAvailableProfileEnv();
const profileEnv = profileResult.env;
this.debug('Active profile environment', {
hasOAuthToken: !!profileEnv.CLAUDE_CODE_OAUTH_TOKEN,
@@ -31,7 +31,7 @@ export function getBranches(projectPath: string, debugEnabled = false): GitBranc
encoding: 'utf-8'
}).trim();
} catch {
// Ignore - might be in detached HEAD
// Ignore - might be in detached HEAD
}
// Get all branches (local and remote)
@@ -132,7 +132,7 @@ export function getCurrentBranch(projectPath: string): string {
encoding: 'utf-8'
}).trim();
} catch {
return 'main';
return 'main';
}
}
@@ -148,7 +148,7 @@ export function getDefaultBranch(projectPath: string): string {
}).trim();
return result.replace('origin/', '');
} catch {
// Fallback: check if main or master exists
// Fallback: check if main or master exists
try {
execFileSync(getToolPath('git'), ['rev-parse', '--verify', 'main'], {
cwd: projectPath,
@@ -156,14 +156,14 @@ export function getDefaultBranch(projectPath: string): string {
});
return 'main';
} catch {
try {
try {
execFileSync(getToolPath('git'), ['rev-parse', '--verify', 'master'], {
cwd: projectPath,
encoding: 'utf-8'
});
return 'master';
} catch {
return 'main';
return 'main';
}
}
}
@@ -51,7 +51,7 @@ export class VersionSuggester {
const script = this.createAnalysisScript(prompt);
// Build environment
const spawnEnv = await this.buildSpawnEnvironment();
const spawnEnv = this.buildSpawnEnvironment();
return new Promise((resolve, _reject) => {
// Parse Python command to handle space-separated commands like "py -3"
@@ -225,7 +225,7 @@ except Exception as e:
/**
* Build spawn environment with proper PATH and auth settings
*/
private async buildSpawnEnvironment(): Promise<Record<string, string>> {
private buildSpawnEnvironment(): Record<string, string> {
const homeDir = os.homedir();
// Use getAugmentedEnv() to ensure common tool paths are available
@@ -233,7 +233,7 @@ except Exception as e:
const augmentedEnv = getAugmentedEnv();
// Get best available Claude profile environment (automatically handles rate limits)
const profileResult = await getBestAvailableProfileEnv();
const profileResult = getBestAvailableProfileEnv();
const profileEnv = profileResult.env;
const spawnEnv: Record<string, string> = {
@@ -212,7 +212,8 @@ describe('env-sanitizer', () => {
it('allows numeric keys (converted to strings)', () => {
const env = {
validKey: 'value',
123: 'valid' as any,
// biome-ignore lint/suspicious/noExplicitAny: testing object with numeric key
123: 'valid' as any,
};
const result = sanitizeEnvVars(env);
@@ -224,7 +225,8 @@ describe('env-sanitizer', () => {
it('skips invalid value types', () => {
const env = {
validKey: 'value',
invalidValue: 123 as any,
// biome-ignore lint/suspicious/noExplicitAny: testing invalid input
invalidValue: 123 as any,
};
const result = sanitizeEnvVars(env);
@@ -215,7 +215,7 @@ function getUserConfigDir(): string {
}
}
} catch {
debugLog(`${LOG_PREFIX} ClaudeProfileManager not available, using fallback`);
debugLog(`${LOG_PREFIX} ClaudeProfileManager not available, using fallback`);
}
// Fall back to CLAUDE_CONFIG_DIR env var
+48 -147
View File
@@ -21,7 +21,9 @@ import type {
ClaudeUsageData,
ClaudeRateLimitEvent,
ClaudeAutoSwitchSettings,
APIProfile
} from '../shared/types';
import type { UnifiedAccount } from '../shared/types/unified-account';
// Module imports
import { encryptToken, decryptToken } from './claude-profile/token-encryption';
@@ -41,9 +43,10 @@ import {
getBestAvailableProfile,
shouldProactivelySwitch as shouldProactivelySwitchImpl,
getProfilesSortedByAvailability as getProfilesSortedByAvailabilityImpl,
checkProfileAvailability
getBestAvailableUnifiedAccount
} from './claude-profile/profile-scorer';
import { getCredentialsFromKeychain, normalizeWindowsPath, updateProfileSubscriptionMetadata } from './claude-profile/credential-utils';
import { loadProfilesFile } from './services/profile/profile-manager';
import {
CLAUDE_PROFILES_DIR,
generateProfileId as generateProfileIdImpl,
@@ -53,17 +56,6 @@ import {
expandHomePath,
getEmailFromConfigDir
} from './claude-profile/profile-utils';
import { debugLog } from '../shared/utils/debug-logger';
/**
* Unified swap target representing either an OAuth or API profile
*/
export interface UnifiedSwapTarget {
id: string;
name: string;
type: 'oauth' | 'api';
priorityIndex: number;
}
/**
* Manages Claude Code profiles for multi-account support.
@@ -94,8 +86,6 @@ export class ClaudeProfileManager {
return;
}
console.log('[ClaudeProfileManager] Starting initialization...');
// Ensure directory exists (async) - mkdir with recursive:true is idempotent
await mkdir(this.configDir, { recursive: true });
@@ -103,9 +93,6 @@ export class ClaudeProfileManager {
const loadedData = await loadProfileStoreAsync(this.storePath);
if (loadedData) {
this.data = loadedData;
debugLog('[ClaudeProfileManager] Loaded profile store with', this.data.profiles.length, 'profiles');
} else {
debugLog('[ClaudeProfileManager] No existing profile store found, using defaults');
}
// Run one-time migration to fix corrupted emails
@@ -117,7 +104,6 @@ export class ClaudeProfileManager {
this.populateSubscriptionMetadata();
this.initialized = true;
console.log('[ClaudeProfileManager] Initialization complete');
}
/**
@@ -163,20 +149,13 @@ export class ClaudeProfileManager {
private populateSubscriptionMetadata(): void {
let needsSave = false;
debugLog('[ClaudeProfileManager] populateSubscriptionMetadata: checking', this.data.profiles.length, 'profiles');
for (const profile of this.data.profiles) {
if (!profile.configDir) {
debugLog('[ClaudeProfileManager] populateSubscriptionMetadata: skipping profile', profile.id, '(no configDir)');
continue;
}
// Skip if profile already has subscription metadata
if (profile.subscriptionType && profile.rateLimitTier) {
debugLog('[ClaudeProfileManager] populateSubscriptionMetadata: profile', profile.id, 'already has metadata:', {
subscriptionType: profile.subscriptionType,
rateLimitTier: profile.rateLimitTier
});
continue;
}
@@ -434,22 +413,6 @@ export class ClaudeProfileManager {
return true;
}
/**
* Clear the active profile (used when switching to API profile mode)
*/
clearActiveProfile(): void {
const previousProfileId = this.data.activeProfileId;
if (process.env.DEBUG === 'true') {
console.warn('[ClaudeProfileManager] clearActiveProfile:', {
from: previousProfileId
});
}
this.data.activeProfileId = '';
this.save();
}
/**
* Set the active profile
*/
@@ -579,27 +542,8 @@ export class ClaudeProfileManager {
if (process.env.DEBUG === 'true') {
console.warn('[ClaudeProfileManager] Using CLAUDE_CONFIG_DIR for profile:', profile.name, expandedConfigDir);
}
} else if (profile) {
// Fallback: retrieve OAuth token directly from Keychain when configDir is missing.
// Without configDir, Claude CLI cannot resolve credentials automatically,
// so we inject CLAUDE_CODE_OAUTH_TOKEN as a direct override.
debugLog(
'[ClaudeProfileManager] Profile has no configDir configured:',
profile.name,
'- falling back to Keychain token lookup. Subscription display may be degraded.'
);
const credentials = getCredentialsFromKeychain(undefined, true);
if (credentials.token) {
env.CLAUDE_CODE_OAUTH_TOKEN = credentials.token;
debugLog('[ClaudeProfileManager] Injected CLAUDE_CODE_OAUTH_TOKEN from Keychain for profile:', profile.name);
} else {
debugLog(
'[ClaudeProfileManager] No token found in Keychain for profile without configDir:',
profile.name,
credentials.error ? `(error: ${credentials.error})` : ''
);
}
} else {
console.warn('[ClaudeProfileManager] Profile has no configDir configured:', profile?.name);
}
return env;
@@ -727,79 +671,54 @@ export class ClaudeProfileManager {
}
/**
* Get the best available account across both OAuth and API profiles.
* Considers user priority order, availability (auth, rate limits, thresholds).
*
* @param excludeProfileId - Profile ID to exclude (usually the current one)
* @param additionalExclusions - Additional profile IDs to exclude (e.g., auth-failed)
* @returns Best available account or null if none found
* Load API profiles from profiles.json with error handling
* Shared helper to avoid duplication across methods
*/
async getBestAvailableUnifiedAccount(
excludeProfileId?: string,
additionalExclusions: string[] = []
): Promise<UnifiedSwapTarget | null> {
const excludeIds = new Set([
...(excludeProfileId ? [excludeProfileId] : []),
...additionalExclusions
]);
const priorityOrder = this.getAccountPriorityOrder();
const settings = this.getAutoSwitchSettings();
const unifiedAccounts: UnifiedSwapTarget[] = [];
// Add OAuth profiles (filtered by availability)
const oauthProfiles = this.getProfilesSortedByAvailability();
for (const profile of oauthProfiles) {
if (excludeIds.has(profile.id)) continue;
const availability = checkProfileAvailability(profile, settings);
if (!availability.available) continue;
const unifiedId = `oauth-${profile.id}`;
const priorityIndex = priorityOrder.indexOf(unifiedId);
unifiedAccounts.push({
id: profile.id,
name: profile.name,
type: 'oauth',
priorityIndex: priorityIndex === -1 ? Infinity : priorityIndex
});
}
// Add API profiles (always considered available if they have an apiKey)
private async loadProfilesFileSafe(): Promise<{ profiles: APIProfile[]; activeProfileId?: string }> {
try {
const { loadProfilesFile } = await import('./services/profile/profile-manager');
const profilesFile = await loadProfilesFile();
for (const apiProfile of profilesFile.profiles) {
if (excludeIds.has(apiProfile.id) || !apiProfile.apiKey) continue;
const unifiedId = `api-${apiProfile.id}`;
const priorityIndex = priorityOrder.indexOf(unifiedId);
unifiedAccounts.push({
id: apiProfile.id,
name: apiProfile.name,
type: 'api',
priorityIndex: priorityIndex === -1 ? Infinity : priorityIndex
});
}
const file = await loadProfilesFile();
return { profiles: file.profiles, activeProfileId: file.activeProfileId ?? undefined };
} catch (error) {
if (process.env.DEBUG === 'true') {
console.warn('[ClaudeProfileManager] Failed to load API profiles for unified selection:', error);
}
console.error('[ClaudeProfileManager] Failed to load profiles file:', error);
return { profiles: [] };
}
}
if (unifiedAccounts.length === 0) {
return null;
}
/**
* Load API profiles from profiles.json
* Used by the unified account selection to consider API profiles as fallback
*/
async loadAPIProfiles(): Promise<APIProfile[]> {
const { profiles } = await this.loadProfilesFileSafe();
return profiles;
}
// Sort by priority order (lower index = higher priority)
// If no priority order set, OAuth profiles come first (already sorted by availability)
unifiedAccounts.sort((a, b) => {
if (a.priorityIndex !== Infinity || b.priorityIndex !== Infinity) {
return a.priorityIndex - b.priorityIndex;
/**
* Get the best available unified account from both OAuth and API profiles
* This enables cross-type account switching when OAuth profiles are exhausted
*
* @param excludeAccountId - Unified account ID to exclude (e.g., 'oauth-profile1')
* @returns The best available UnifiedAccount, or null if none available
*/
async getBestAvailableUnifiedAccount(excludeAccountId?: string): Promise<UnifiedAccount | null> {
const settings = this.getAutoSwitchSettings();
const priorityOrder = this.getAccountPriorityOrder();
const activeOAuthId = this.data.activeProfileId;
// Load API profiles and active API profile ID from profiles.json
const { profiles: apiProfiles, activeProfileId: activeAPIId } = await this.loadProfilesFileSafe();
return getBestAvailableUnifiedAccount(
this.data.profiles,
apiProfiles,
settings,
{
excludeAccountId,
priorityOrder,
activeOAuthId,
activeAPIId
}
if (a.type !== b.type) {
return a.type === 'oauth' ? -1 : 1;
}
return 0;
});
return unifiedAccounts[0];
);
}
/**
@@ -882,26 +801,8 @@ export class ClaudeProfileManager {
return {};
}
// If no configDir is defined, fall back to default
if (!profile.configDir) {
// Fallback: retrieve OAuth token directly from Keychain when configDir is missing.
// Without configDir, Claude CLI cannot resolve credentials automatically,
// so we inject CLAUDE_CODE_OAUTH_TOKEN as a direct override.
// This mirrors the fallback in getActiveProfileEnv().
debugLog(
'[ClaudeProfileManager] getProfileEnv: profile has no configDir:',
profile.name,
'- falling back to Keychain token lookup.'
);
const credentials = getCredentialsFromKeychain(undefined, true);
if (credentials.token) {
debugLog('[ClaudeProfileManager] getProfileEnv: injected CLAUDE_CODE_OAUTH_TOKEN from Keychain for profile:', profile.name);
return { CLAUDE_CODE_OAUTH_TOKEN: credentials.token };
}
debugLog(
'[ClaudeProfileManager] getProfileEnv: no token found in Keychain for profile without configDir:',
profile.name
);
return {};
}
@@ -396,7 +396,7 @@ function parseCredentialJson<T extends PlatformCredentials>(
try {
data = JSON.parse(credentialsJson);
} catch {
console.warn(`[CredentialUtils] Failed to parse credential JSON for ${identifier}`);
console.warn(`[CredentialUtils] Failed to parse credential JSON for ${identifier}`);
return extractFn({}) as T;
}
@@ -478,7 +478,7 @@ function getCredentialsFromFile(
try {
data = JSON.parse(content);
} catch {
console.warn(`[CredentialUtils:${logPrefix}] Failed to parse credentials JSON:`, credentialsPath);
console.warn(`[CredentialUtils:${logPrefix}] Failed to parse credentials JSON:`, credentialsPath);
const errorResult = { token: null, email: null };
credentialCache.set(cacheKey, { credentials: errorResult, timestamp: now });
return errorResult;
@@ -561,7 +561,7 @@ function getFullCredentialsFromFile(
try {
data = JSON.parse(content);
} catch {
console.warn(`[CredentialUtils:${logPrefix}] Failed to parse credentials JSON:`, credentialsPath);
console.warn(`[CredentialUtils:${logPrefix}] Failed to parse credentials JSON:`, credentialsPath);
return { token: null, email: null, refreshToken: null, expiresAt: null, scopes: null, subscriptionType: null, rateLimitTier: null };
}
@@ -1637,7 +1637,7 @@ function updateMacOSKeychainCredentials(
console.warn('[CredentialUtils:macOS:Update] Deleted existing Keychain entry for service:', serviceName);
}
} catch {
// Entry didn't exist - that's fine, we'll create it
// Entry didn't exist - that's fine, we'll create it
if (isDebug) {
console.warn('[CredentialUtils:macOS:Update] No existing entry to delete for service:', serviceName);
}
@@ -1825,7 +1825,6 @@ function updateLinuxFileCredentials(
}
// Write to file with secure permissions (0600)
// lgtm[js/http-to-file-access] - credentialsPath is from controlled configDir
writeFileSync(credentialsPath, credentialsJson, { mode: 0o600, encoding: 'utf-8' });
if (isDebug) {
@@ -2087,7 +2086,6 @@ function updateWindowsFileCredentials(
const tempPath = `${credentialsPath}.${Date.now()}.tmp`;
try {
// Write to temp file
// lgtm[js/http-to-file-access] - credentialsPath is from controlled configDir
writeFileSync(tempPath, credentialsJson, { encoding: 'utf-8' });
// Restrict temp file permissions to current user only (mimics Unix 0600)
@@ -2102,7 +2100,7 @@ function updateWindowsFileCredentials(
unlinkSync(tempPath);
}
} catch {
// Ignore cleanup errors
// Ignore cleanup errors
}
throw writeError;
}
@@ -40,7 +40,7 @@ interface ScoredProfile {
/**
* Check if a profile is available for use based on all criteria
*/
export function checkProfileAvailability(
function checkProfileAvailability(
profile: ClaudeProfile,
settings: ClaudeAutoSwitchSettings
): { available: boolean; reason?: string } {
@@ -98,7 +98,7 @@ function migrateProfileToIsolatedDirectory(profile: ClaudeProfile): string {
break;
}
} catch {
// Ignore read errors, treat as collision
// Ignore read errors, treat as collision
}
}
// Directory exists but belongs to different profile, try next counter
@@ -154,7 +154,7 @@ export function isProfileAuthenticated(profile: ClaudeProfile): boolean {
return true;
}
} catch {
// Ignore read errors
// Ignore read errors
}
}
}
@@ -168,7 +168,7 @@ export function isProfileAuthenticated(profile: ClaudeProfile): boolean {
return true;
}
} catch {
// Ignore read errors
// Ignore read errors
}
}
@@ -220,7 +220,7 @@ export async function refreshOAuthToken(
try {
errorData = await response.json();
} catch {
// Ignore JSON parse errors
// Ignore JSON parse errors
}
const errorCode = errorData.error || `http_${response.status}`;
@@ -740,7 +740,7 @@ describe('usage-monitor', () => {
} as unknown as Response);
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
// 401 errors should throw
await expect(
@@ -771,7 +771,7 @@ describe('usage-monitor', () => {
} as unknown as Response);
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
// 403 errors should throw
await expect(
@@ -793,7 +793,7 @@ describe('usage-monitor', () => {
} as unknown as Response);
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const usage = await monitor['fetchUsageViaAPI']('valid-token', 'test-profile-1', 'Test Profile', undefined);
@@ -808,7 +808,7 @@ describe('usage-monitor', () => {
mockFetch.mockRejectedValueOnce(new Error('Network timeout'));
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const usage = await monitor['fetchUsageViaAPI']('valid-token', 'test-profile-1', 'Test Profile', undefined);
@@ -830,7 +830,7 @@ describe('usage-monitor', () => {
} as unknown as Response);
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const usage = await monitor['fetchUsageViaAPI']('valid-token', 'test-profile-1', 'Test Profile', undefined);
@@ -851,7 +851,7 @@ describe('usage-monitor', () => {
} as unknown as Response);
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
// 401 errors should throw with proper message
await expect(
@@ -877,7 +877,7 @@ describe('usage-monitor', () => {
it('should handle empty credential string', async () => {
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const usage = await monitor['fetchUsage']('test-profile-1', '');
@@ -958,7 +958,7 @@ describe('usage-monitor', () => {
});
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const usage = await monitor['fetchUsageViaAPI']('zai-api-key', 'zai-profile-1', 'z.ai Profile', undefined);
@@ -990,7 +990,7 @@ describe('usage-monitor', () => {
});
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const usage = await monitor['fetchUsageViaAPI']('zhipu-api-key', 'zhipu-profile-1', 'ZHIPU Profile', undefined);
@@ -1066,7 +1066,7 @@ describe('usage-monitor', () => {
});
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
// Should fall back to OAuth profile
const credential = await monitor['getCredential']();
@@ -1091,7 +1091,7 @@ describe('usage-monitor', () => {
});
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const credential = await monitor['getCredential']();
@@ -1110,7 +1110,7 @@ describe('usage-monitor', () => {
});
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const credential = await monitor['getCredential']();
@@ -1338,7 +1338,7 @@ describe('usage-monitor', () => {
describe('Mixed OAuth/API profile environments', () => {
it('should handle environment with both OAuth and API profiles', async () => {
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
// Mock both OAuth and API profiles
mockLoadProfilesFile.mockResolvedValueOnce({
@@ -1370,7 +1370,7 @@ describe('usage-monitor', () => {
it('should switch from API profile back to OAuth profile', async () => {
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
// First, active API profile
mockLoadProfilesFile.mockResolvedValueOnce({
@@ -1436,7 +1436,7 @@ describe('usage-monitor', () => {
} as unknown as Response);
const monitor = getUsageMonitor();
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const profileId = 'test-profile-cooldown';
// Call fetchUsageViaAPI which should fail and record timestamp
@@ -1526,7 +1526,7 @@ describe('usage-monitor', () => {
describe('Race condition prevention via activeProfile parameter', () => {
it('should use passed activeProfile instead of re-detecting', async () => {
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => { /* noop */ });
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const mockFetch = vi.mocked(global.fetch);
mockFetch.mockResolvedValueOnce({
@@ -219,10 +219,6 @@ export class UsageMonitor extends EventEmitter {
// These profiles have permanent auth failures that require manual re-auth
private needsReauthProfiles: Set<string> = new Set();
// Swap cooldown to prevent rapid back-and-forth swapping
private static SWAP_COOLDOWN_MS = 60_000; // 1 minute
private lastSwapTimestamp = 0;
// Cache for all profiles' usage data
// Map<profileId, { usage: ProfileUsageSummary, fetchedAt: number }>
private allProfilesUsageCache: Map<string, { usage: ProfileUsageSummary; fetchedAt: number }> = new Map();
@@ -773,26 +769,24 @@ export class UsageMonitor extends EventEmitter {
* @returns The credential string or undefined if none available
*/
private async getCredential(): Promise<string | undefined> {
// Priority order matches determineActiveProfile(): API first, then OAuth fallback
// This ensures usage monitoring reports the correct profile's usage
// First, try API profile credential (highest priority - terminals use API when active)
// Try API profile first (highest priority)
try {
const profilesFile = await loadProfilesFile();
if (profilesFile.activeProfileId) {
const apiProfile = profilesFile.profiles.find(
const activeProfile = profilesFile.profiles.find(
(p) => p.id === profilesFile.activeProfileId
);
if (apiProfile?.apiKey) {
this.debugLog('[UsageMonitor:TRACE] Using API profile credential: ' + apiProfile.name);
return apiProfile.apiKey;
if (activeProfile?.apiKey) {
this.debugLog('[UsageMonitor:TRACE] Using API profile credential: ' + activeProfile.name);
return activeProfile.apiKey;
}
}
} catch (error) {
// API profile loading failed, fall through to OAuth
this.debugLog('[UsageMonitor:TRACE] Failed to load API profiles, falling back to OAuth:', error);
}
// Fall back to OAuth profile credential
// Fall back to OAuth profile - use ensureValidToken for proactive refresh
const profileManager = getClaudeProfileManager();
const activeProfile = profileManager.getActiveProfile();
if (activeProfile) {
@@ -928,8 +922,8 @@ export class UsageMonitor extends EventEmitter {
this.emit('all-profiles-usage-updated', allProfilesUsage);
}
// Step 4: Check thresholds and perform proactive swap (both OAuth and API profiles)
{
// Step 4: Check thresholds and perform proactive swap (OAuth profiles only)
if (!isAPIProfile) {
const profileManager = getClaudeProfileManager();
const settings = profileManager.getAutoSwitchSettings();
@@ -966,6 +960,8 @@ export class UsageMonitor extends EventEmitter {
weekPercent: usage.weeklyPercent
});
}
} else {
this.debugLog('[UsageMonitor:TRACE] Skipping proactive swap for API profile (only supported for OAuth profiles)');
}
} catch (error) {
// Step 5: Handle auth failures
@@ -1000,18 +996,12 @@ export class UsageMonitor extends EventEmitter {
/**
* Determine which profile is active (API profile vs OAuth profile)
*
* Priority order:
* 1. API profiles - terminals use API credentials when activeProfileId is set
* 2. OAuth profiles - fallback when no API profile is active
*
* This matches terminal-lifecycle.ts behavior where getAPIProfileEnv() is called
* first and API vars take precedence over OAuth vars.
* API profiles take priority over OAuth profiles
*
* @returns Active profile info or null if no profile is active
*/
private async determineActiveProfile(): Promise<ActiveProfileResult | null> {
// First, check if an API profile is active (terminals use API when activeProfileId is set)
// First, check if an API profile is active
try {
const profilesFile = await loadProfilesFile();
if (profilesFile.activeProfileId) {
@@ -1019,6 +1009,7 @@ export class UsageMonitor extends EventEmitter {
(p) => p.id === profilesFile.activeProfileId
);
if (activeAPIProfile?.apiKey) {
// API profile is active and has an apiKey
this.debugLog('[UsageMonitor:TRACE] Active auth type: API Profile', {
profileId: activeAPIProfile.id,
profileName: activeAPIProfile.name,
@@ -1027,53 +1018,58 @@ export class UsageMonitor extends EventEmitter {
return {
profileId: activeAPIProfile.id,
profileName: activeAPIProfile.name,
profileEmail: undefined,
isAPIProfile: true,
baseUrl: activeAPIProfile.baseUrl
};
} else if (activeAPIProfile) {
this.debugLog('[UsageMonitor:TRACE] Active API profile missing apiKey', {
// API profile exists but missing apiKey - fall back to OAuth
this.debugLog('[UsageMonitor:TRACE] Active API profile missing apiKey, falling back to OAuth', {
profileId: activeAPIProfile.id,
profileName: activeAPIProfile.name
});
} else {
this.debugLog('[UsageMonitor:TRACE] Active API profile ID set but profile not found');
// activeProfileId is set but profile not found - fall through to OAuth
this.debugLog('[UsageMonitor:TRACE] Active API profile ID set but profile not found, falling back to OAuth');
}
}
} catch (error) {
this.debugLog('[UsageMonitor:TRACE] Failed to load API profiles:', error);
// Failed to load API profiles - fall through to OAuth
this.debugLog('[UsageMonitor:TRACE] Failed to load API profiles, falling back to OAuth:', error);
}
// No API profile active check OAuth profiles
// If no API profile is active, check OAuth profiles
const profileManager = getClaudeProfileManager();
const activeOAuthProfile = profileManager.getActiveProfile();
if (activeOAuthProfile) {
// Get email from profile or try keychain
let profileEmail = activeOAuthProfile.email;
if (!profileEmail) {
// IMPORTANT: Always pass configDir - service name is based on expanded path (e.g., /Users/xxx/.claude)
const keychainCreds = getCredentialsFromKeychain(activeOAuthProfile.configDir);
profileEmail = keychainCreds.email ?? undefined;
}
this.debugLog('[UsageMonitor:TRACE] Active auth type: OAuth Profile', {
profileId: activeOAuthProfile.id,
profileName: activeOAuthProfile.name,
profileEmail
});
return {
profileId: activeOAuthProfile.id,
profileName: activeOAuthProfile.name,
profileEmail,
isAPIProfile: false,
baseUrl: 'https://api.anthropic.com'
};
if (!activeOAuthProfile) {
this.debugLog('[UsageMonitor] No active profile (neither API nor OAuth)');
return null;
}
this.debugLog('[UsageMonitor] No active profile (neither OAuth nor API)');
return null;
// Get email from profile or try keychain
let profileEmail = activeOAuthProfile.email;
if (!profileEmail) {
// Try to get email from keychain
// IMPORTANT: Always pass configDir - service name is based on expanded path (e.g., /Users/xxx/.claude)
const keychainCreds = getCredentialsFromKeychain(activeOAuthProfile.configDir);
profileEmail = keychainCreds.email ?? undefined;
}
this.debugLog('[UsageMonitor:TRACE] Active auth type: OAuth Profile', {
profileId: activeOAuthProfile.id,
profileName: activeOAuthProfile.name,
profileEmail
});
const result = {
profileId: activeOAuthProfile.id,
profileName: activeOAuthProfile.name,
profileEmail,
isAPIProfile: false,
baseUrl: 'https://api.anthropic.com'
};
return result;
}
/**
@@ -1171,8 +1167,9 @@ export class UsageMonitor extends EventEmitter {
const settings = profileManager.getAutoSwitchSettings();
if (!settings.enabled || !settings.proactiveSwapEnabled) {
this.debugLog('[UsageMonitor] Auth failure detected but proactive swap is disabled, skipping swap');
// Proactive swap is only supported for OAuth profiles, not API profiles
if (isAPIProfile || !settings.enabled || !settings.proactiveSwapEnabled) {
this.debugLog('[UsageMonitor] Auth failure detected but proactive swap is disabled or using API profile, skipping swap');
return;
}
@@ -1401,7 +1398,7 @@ export class UsageMonitor extends EventEmitter {
const endpointUrl = new URL(usageEndpoint);
endpointHostname = endpointUrl.hostname;
} catch {
console.error('[UsageMonitor] Invalid usage endpoint URL:', usageEndpoint);
console.error('[UsageMonitor] Invalid usage endpoint URL:', usageEndpoint);
return null;
}
@@ -1872,23 +1869,60 @@ export class UsageMonitor extends EventEmitter {
limitType: 'session' | 'weekly',
additionalExclusions: string[] = []
): Promise<void> {
// Cooldown check to prevent rapid back-and-forth swapping
const now = Date.now();
if (now - this.lastSwapTimestamp < UsageMonitor.SWAP_COOLDOWN_MS) {
this.debugLog('[UsageMonitor] Swap cooldown active, skipping');
return;
const profileManager = getClaudeProfileManager();
const excludeIds = new Set([currentProfileId, ...additionalExclusions]);
// Get priority order for unified account system
const priorityOrder = profileManager.getAccountPriorityOrder();
// Build unified list of available accounts
type UnifiedSwapTarget = {
id: string;
unifiedId: string; // oauth-{id} or api-{id}
name: string;
type: 'oauth' | 'api';
priorityIndex: number;
};
const unifiedAccounts: UnifiedSwapTarget[] = [];
// Add OAuth profiles (sorted by availability)
const oauthProfiles = profileManager.getProfilesSortedByAvailability();
for (const profile of oauthProfiles) {
if (!excludeIds.has(profile.id)) {
const unifiedId = `oauth-${profile.id}`;
const priorityIndex = priorityOrder.indexOf(unifiedId);
unifiedAccounts.push({
id: profile.id,
unifiedId,
name: profile.name,
type: 'oauth',
priorityIndex: priorityIndex === -1 ? Infinity : priorityIndex
});
}
}
const profileManager = getClaudeProfileManager();
// Add API profiles (always considered available since they have unlimited usage)
try {
const profilesFile = await loadProfilesFile();
for (const apiProfile of profilesFile.profiles) {
if (!excludeIds.has(apiProfile.id) && apiProfile.apiKey) {
const unifiedId = `api-${apiProfile.id}`;
const priorityIndex = priorityOrder.indexOf(unifiedId);
unifiedAccounts.push({
id: apiProfile.id,
unifiedId,
name: apiProfile.name,
type: 'api',
priorityIndex: priorityIndex === -1 ? Infinity : priorityIndex
});
}
}
} catch (error) {
this.debugLog('[UsageMonitor] Failed to load API profiles for swap:', error);
}
// Use shared unified account selection
const bestAccount = await profileManager.getBestAvailableUnifiedAccount(
currentProfileId,
additionalExclusions
);
if (!bestAccount) {
const excludeIds = new Set([currentProfileId, ...additionalExclusions]);
if (unifiedAccounts.length === 0) {
this.debugLog('[UsageMonitor] No alternative profile for proactive swap (excluded:', Array.from(excludeIds));
this.emit('proactive-swap-failed', {
reason: additionalExclusions.length > 0 ? 'all_alternatives_failed_auth' : 'no_alternative',
@@ -1898,6 +1932,23 @@ export class UsageMonitor extends EventEmitter {
return;
}
// Sort by priority order (lower index = higher priority)
// If no priority order is set, OAuth profiles come first (they were already sorted by availability)
unifiedAccounts.sort((a, b) => {
// If both have priority indices, use them
if (a.priorityIndex !== Infinity || b.priorityIndex !== Infinity) {
return a.priorityIndex - b.priorityIndex;
}
// Otherwise, prefer OAuth profiles (which are sorted by availability)
if (a.type !== b.type) {
return a.type === 'oauth' ? -1 : 1;
}
return 0;
});
// Use the best available from unified accounts
const bestAccount = unifiedAccounts[0];
this.debugLog('[UsageMonitor] Proactive swap:', {
from: currentProfileId,
to: bestAccount.id,
@@ -1916,14 +1967,6 @@ export class UsageMonitor extends EventEmitter {
if (bestAccount.type === 'oauth') {
// Switch OAuth profile via profile manager
profileManager.setActiveProfile(rawProfileId);
// Clear API active profile so determineActiveProfile() and getAPIProfileEnv()
// correctly detect OAuth mode on subsequent checks
try {
const { setActiveAPIProfile } = await import('../services/profile/profile-manager');
await setActiveAPIProfile(null);
} catch (error) {
console.error('[UsageMonitor] Failed to clear active API profile:', error);
}
} else {
// Switch API profile via profile-manager service
try {
@@ -1935,23 +1978,6 @@ export class UsageMonitor extends EventEmitter {
}
}
// Clear current usage data so UI doesn't show stale info from old profile
// The next checkUsageAndSwap() will fetch fresh data for the new profile
this.currentUsage = null;
this.currentUsageProfileId = null;
// Record swap timestamp for cooldown
this.lastSwapTimestamp = Date.now();
// Immediately trigger usage check for new profile so UI updates right away
// Don't wait for next 30-second interval
this.debugLog('[UsageMonitor] Triggering immediate usage fetch after proactive swap');
setImmediate(() => {
this.checkUsageAndSwap().catch(error => {
console.error('[UsageMonitor] Failed to fetch usage after swap:', error);
});
});
// Get the "from" profile name
let fromProfileName: string | undefined;
const fromOAuthProfile = profileManager.getProfile(currentProfileId);
@@ -1966,7 +1992,7 @@ export class UsageMonitor extends EventEmitter {
fromProfileName = fromAPIProfile.name;
}
} catch {
// Ignore
// Ignore
}
}
+1 -1
View File
@@ -48,7 +48,7 @@ export async function existsAsync(filePath: string): Promise<boolean> {
await fsPromises.access(filePath);
return true;
} catch {
return false;
return false;
}
}
+3 -3
View File
@@ -55,7 +55,7 @@ export class FileWatcher extends EventEmitter {
const plan: ImplementationPlan = JSON.parse(content);
this.emit('progress', taskId, plan);
} catch {
// File might be in the middle of being written
// File might be in the middle of being written
// Ignore parse errors, next change event will have complete file
}
});
@@ -72,7 +72,7 @@ export class FileWatcher extends EventEmitter {
const plan: ImplementationPlan = JSON.parse(content);
this.emit('progress', taskId, plan);
} catch {
// Initial read failed - not critical
// Initial read failed - not critical
}
}
@@ -118,7 +118,7 @@ export class FileWatcher extends EventEmitter {
const content = readFileSync(watcherInfo.planPath, 'utf-8');
return JSON.parse(content);
} catch {
return null;
return null;
}
}
}

Some files were not shown because too many files have changed in this diff Show More