Compare commits
44 Commits
v2.7.6-beta.3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 2a79ba183d | |||
| a0807c20a0 | |||
| 03a0b21f38 | |||
| 72c0409c79 | |||
| 3217719709 | |||
| e8c4740389 | |||
| 4a75ea9f99 | |||
| 19f1cdedbb | |||
| 732fc1cd3f | |||
| 4a6df82792 | |||
| 819f98d9fa | |||
| 28a620079f | |||
| fb3a3fbda7 | |||
| 76d1d3b032 | |||
| 3cb05781fa | |||
| d98ff7d19c | |||
| 635b53eeaf | |||
| 2e4b5ac659 | |||
| 385f044144 | |||
| 7b0f3a2c03 | |||
| 3a7c4ca7a9 | |||
| 4091d1d4b5 | |||
| f40f79a2db | |||
| 603b9a24bf | |||
| ecb6158024 | |||
| ae13ce14c2 | |||
| e3b219288e | |||
| 6204d5fc2b | |||
| f735f0b49b | |||
| a4870fa0c3 | |||
| f1b8cd3a7a | |||
| 4d4234378f | |||
| d1fbccde39 | |||
| ed93df698b | |||
| 8872d33e32 | |||
| 3b3ad75c1b | |||
| 8ece0009ee | |||
| 115576e85d | |||
| 3791b37bbd | |||
| 2823873566 | |||
| 4f1b7b2a95 | |||
| 5e78d748ee | |||
| aa5fc7f952 | |||
| 5745cb149f |
+31
-4
@@ -127,6 +127,13 @@ 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
|
||||
@@ -158,7 +165,16 @@ if git diff --cached --name-only | grep -q "^apps/backend/.*\.py$"; then
|
||||
echo "$STAGED_PY_FILES" | xargs git add
|
||||
fi
|
||||
else
|
||||
echo "Warning: ruff not found, skipping Python linting. Install with: uv pip install ruff"
|
||||
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
|
||||
fi
|
||||
|
||||
# Run pytest (skip slow/integration tests and Windows-incompatible tests for pre-commit speed)
|
||||
@@ -192,17 +208,28 @@ 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
|
||||
)
|
||||
if [ $? -ne 0 ]; then
|
||||
PYTHON_EXIT=$?
|
||||
if [ $PYTHON_EXIT -eq 77 ]; then
|
||||
echo "Backend checks passed! (Python tests skipped — worktree)"
|
||||
elif [ $PYTHON_EXIT -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
|
||||
|
||||
# =============================================================================
|
||||
|
||||
+12
-20
@@ -97,9 +97,8 @@ repos:
|
||||
- id: ruff-format
|
||||
files: ^apps/backend/
|
||||
|
||||
# Python tests (apps/backend/) - skip slow/integration tests for pre-commit speed
|
||||
# Python tests (apps/backend/) - run full test suite from project root
|
||||
# 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
|
||||
@@ -108,31 +107,24 @@ repos:
|
||||
args:
|
||||
- -c
|
||||
- |
|
||||
# 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"
|
||||
# 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"
|
||||
else
|
||||
PYTEST_CMD="python -m pytest"
|
||||
fi
|
||||
PYTHONPATH=. $PYTEST_CMD \
|
||||
../../tests/ \
|
||||
$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
|
||||
|
||||
+231
@@ -1,3 +1,234 @@
|
||||
## 2.7.6 - Stability & Feature Enhancements
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
- **Multi-profile account management** — Unified profile swapping with automatic token refresh and rate limit recovery for both OAuth and API-compatible providers
|
||||
|
||||
- **Enhanced terminal experience** — Customizable terminal fonts with OS-specific defaults, Claude Code CLI settings injection, and improved worktree integration
|
||||
|
||||
- **Advanced roadmap management** — Expand/collapse functionality for phase features and real-time sync with task lifecycle
|
||||
|
||||
- **Queue System v2** — Smart task prioritization with auto-promotion and intelligent rate limit recovery
|
||||
|
||||
- **GitHub integration enhancements** — AI-powered PR template generation, user-friendly API error handling, and improved review visibility
|
||||
|
||||
- **UI/UX improvements** — Spell check support for text inputs, collapsible sidebar toggle, task screenshot capture, expandable task descriptions, and bulk worktree operations
|
||||
|
||||
- **Evidence-based PR validation** — Advanced review system with trigger-driven exploration and enhanced recovery mechanisms
|
||||
|
||||
### 🛠️ Improvements
|
||||
|
||||
- **Performance optimizations** — Async parallel worktree listing prevents UI freezes and improves responsiveness
|
||||
|
||||
- **Robustness enhancements** — Atomic file writes, better error detection in AI responses, and improved OOM/orphaned agent management for overnight builds
|
||||
|
||||
- **Terminal stability** — Fixed GPU context exhaustion from large pastes, SIGABRT crashes on macOS shutdown, and session restoration on app restart
|
||||
|
||||
- **Build & packaging** — XState bundling for packaged apps, aligned Linux package builds, and improved auto-updater for beta releases and DMG installations
|
||||
|
||||
- **Diagnostic improvements** — Sentry instrumentation for Python subprocesses and better error tracking across the system
|
||||
|
||||
### 🐛 Bug Fixes
|
||||
|
||||
- **Terminal & PTY** — Fixed paste size limits, race conditions, rendering issues, text alignment, worktree crashes, and terminal content resizing on expansion
|
||||
|
||||
- **PR review system** — Resolved error visibility in bundled apps, improved structured output validation with three-tier recovery, preserved findings during crashes, and fixed UTC timestamp detection for comment tracking
|
||||
|
||||
- **Planning & task execution** — Fixed handling of empty/greenfield projects, atomic writes to prevent 0-byte file corruption, planning phase crashes, and implementation plan file watching
|
||||
|
||||
- **Authentication & profiles** — Resolved OAuth token revocation loops, API profile mode support without OAuth requirement, subscription type preservation during token refresh, and Linux credential file detection
|
||||
|
||||
- **Windows/cross-platform** — Complete System32 executable path fixes for where.exe and taskkill.exe, Windows credential normalization, and proper shell detection for Windows terminals
|
||||
|
||||
- **Agent management** — Fixed infinite retry loops for tool concurrency errors, auth error detection, and title generator production path resolution
|
||||
|
||||
- **UI/UX fixes** — Resolved Insights scroll-to-blank-space issues, infinite re-render loops in terminal font settings, kanban board scaling collisions, ideation stuck states, and panel constraint errors during terminal exit
|
||||
|
||||
- **Worktree & Git** — Improved branch pattern validation, removed auto-commit on deletion, support for detached HEAD state during PR creation, and better merge conflict resolution with progress tracking
|
||||
|
||||
- **Integrations** — Fixed Ollama infinite subprocess spawning, Graphiti import paths, OpenRouter API URL suffix, and GitLab authentication bugs
|
||||
|
||||
- **Settings & configuration** — Corrected .auto-claude path discovery timeout, z.AI China preset URL, log order sorting, and onboarding completion state persistence
|
||||
|
||||
### 📚 Documentation
|
||||
|
||||
- Added Awesome Claude Code badge to README
|
||||
|
||||
- Added instructions for resetting PR review state in CLAUDE.md
|
||||
|
||||
---
|
||||
|
||||
## What's Changed
|
||||
|
||||
- fix: handle unknown SDK message types (rate_limit_event) to prevent session crashes by @AndyMik90 in 4a75ea9f9
|
||||
- fix: PR review error visibility and gh CLI resolution in bundled apps by @AndyMik90 in 732fc1cd3
|
||||
- fix: handle empty/greenfield projects in spec creation (#1426) (#1841) by @Andy in 819f98d9f
|
||||
- fix: clear terminalEventSeen on task restart to prevent stuck-after-planning (#1828) (#1840) by @Andy in 28a620079
|
||||
- fix: watch worktree path for implementation_plan.json changes (#1805) (#1842) by @Andy in fb3a3fbda
|
||||
- fix: resolve Claude CLI not found on Windows - PATH, prompt size, cwd (#1661) (#1843) by @Andy in 76d1d3b03
|
||||
- fix: handle planning phase crash and resume recovery (#1562) (#1844) by @Andy in 3cb05781f
|
||||
- fix: show dismissed PR review findings in UI instead of silently dropping them (#1852) by @Andy in d98ff7d19
|
||||
- fix: preserve file/line info in PR review extraction recovery (#1857) by @Andy in 635b53eea
|
||||
- docs: add Awesome Claude Code badge to README (#1838) by @Andy in 2e4b5ac65
|
||||
- test: achieve 100% test coverage for backend CLI commands (#1772) by @StillKnotKnown in 385f04414
|
||||
- fix: cap terminal paste size to 1MB to prevent GPU context exhaustion by @AndyMik90 in 7b0f3a2c0
|
||||
- fix: prevent OOM, orphaned agents, and unbounded growth during overnight builds (#1813) by @Andy in 4091d1d4b
|
||||
- docs: add instructions for resetting PR review state in CLAUDE.md by @AndyMik90 in ecb615802
|
||||
- auto-claude: 217-investigate-symlink-issues-in-work-tree-creation-f (#1808) by @Andy in ae13ce14c
|
||||
- auto-claude: 218-enable-claude-code-features-in-worktree-terminals (#1809) by @Andy in e3b219288
|
||||
- auto-claude: 219-investigate-and-fix-authentication-subscription-sy (#1810) by @Andy in 6204d5fc2
|
||||
- feat(roadmap): add expand/collapse functionality for phase features (#1796) by @Burak in f735f0b49
|
||||
- auto-claude: 216-display-ongoing-pr-review-logs-in-progress (#1807) by @Andy in a4870fa0c
|
||||
- fix(pr-review): reduce structured output failures and preserve findings in recovery (#1806) by @Andy in f1b8cd3a7
|
||||
- fix(sentry): enable Sentry for Python subprocesses and add diagnostic instrumentation (#1804) by @Andy in 4d4234378
|
||||
- fix(pr-review): add three-tier recovery for structured output validation failure (#1797) by @Andy in d1fbccde3
|
||||
- test: improve backend agent test coverage to 94% (#1779) by @StillKnotKnown in ed93df698
|
||||
- fix(github): use UTC timestamps for reviewed_at to fix comment detection (#1795) by @Andy in 8872d33e3
|
||||
- feat: add user-friendly GitHub API error handling (#1790) by @StillKnotKnown in 8ece0009e
|
||||
- fix(roadmap): sync roadmap features with task lifecycle (#1791) by @Andy in 115576e85
|
||||
- fix(github): resolve PR review hanging in bundled app (#1793) by @Andy in 3791b37bb
|
||||
- feat(profiles): implement unified profile swapping across OAuth and API accounts (#1794) by @StillKnotKnown in 282387356
|
||||
- test: improve backend memory system test coverage to 100% (#1780) by @StillKnotKnown in 4f1b7b2a9
|
||||
- fix(ideation): guard against non-string properties in IdeaCard badges by @AndyMik90 in 5e78d748e
|
||||
- fix(updater): convert HTML release notes to markdown before rendering by @AndyMik90 in aa5fc7f95
|
||||
- fix(pr-review): simplify structured output schema to reduce validation failures (#1787) by @Andy in cd8914700
|
||||
- fix(qa): enforce visual verification for UI changes and inject startup commands (#1784) by @Andy in f149a7fbd
|
||||
- fix(plan-files): use atomic writes to prevent 0-byte corruption (#1785) by @Andy in c2245b812
|
||||
- fix(terminal): make worktree dropdown scrollable and show all items by @AndyMik90 in 950da45e4
|
||||
- auto-claude: subtask-1-1 - Add adaptive thinking badge to thinking level label (#1782) by @Andy in 25acf2826
|
||||
- auto-claude: subtask-1-1 - Add overflow-hidden and break-words to subtask cards by @AndyMik90 in 39aa08872
|
||||
- refactor(app-updater): disable automatic downloads and allow intentional downgrades by @AndyMik90 in 8de8039db
|
||||
- fix(auth): detect auth errors in AI response text and prevent retry loops (#1776) by @Andy in f4788e4af
|
||||
- test: achieve 100% coverage for backend core workspace module (#1774) by @StillKnotKnown in 3f95765cf
|
||||
- fix(title-generator): add production path resolution for backend source (#1778) by @Andy in 923880f5b
|
||||
- fix(fast-mode): use setting_sources instead of env var for CLI fast mode (#1771) by @Andy in 390ba6a58
|
||||
- fix(windows): complete System32 executable path fixes for where.exe and taskkill.exe (#1715) by @VDT-91 in aa7f56e5d
|
||||
- fix(worktree): remove auto-commit on deletion and add uncommitted changes warning by @AndyMik90 in cec8e65ee
|
||||
- Smart PR Status Polling System (#1766) by @Andy in 48d5f7a32
|
||||
- feat: simplify thinking system and remove opus-1m model variant (#1760) by @Andy in bb7e18937
|
||||
- auto-claude: 203-fix-pr-review-ui-update-issue (#1732) by @Andy in 7589f8e4f
|
||||
- auto-claude: subtask-2-1 - Create isAPIProfileAuthenticated() function to val (#1745) by @Andy in 57e38a692
|
||||
- auto-claude: 202-fix-kanban-board-scaling-collisions (#1731) by @Andy in d09ebb850
|
||||
- auto-claude: 204-fix-pr-review-ui-not-updating-without-manual-navig (#1734) by @Andy in 087091cef
|
||||
- auto-claude: 203-fix-ui-not-updating-during-pr-review-operations (#1733) by @Andy in f085c08bd
|
||||
- auto-claude: 205-fix-insights-chat-only-shows-last-task-suggestion- (#1735) by @Andy in f121f9cdd
|
||||
- auto-claude: 197-roadmap-generation-stuck-at-50-file-locking-race-c (#1746) by @Andy in f41f15e59
|
||||
- auto-claude: 193-fix-update-context7-mcp-tool-name-from-get-library (#1744) by @Andy in bdff9141a
|
||||
- auto-claude: 192-changelog-generation-multiple-critical-bugs-tasks- (#1725) by @Andy in 8c9a504df
|
||||
- auto-claude: 194-bug-rate-limit-during-task-execution-causes-subtas (#1726) by @Andy in 8a7443d24
|
||||
- auto-claude: 201-bug-pr-review-logs-and-analysis (#1730) by @Andy in e0d53adb4
|
||||
- auto-claude: 196-fix-worktrees-dialog-auto-close-race-condition-and (#1727) by @Andy in 323b0d3be
|
||||
- auto-claude: 199-bug-logs-disappear-after-restart (#1728) by @Andy in d639f6ef8
|
||||
- auto-claude: 198-critical-oauth-token-revocation-causes-infinite-40 (#1747) by @Andy in 4438c0b10
|
||||
- Fix Panel Constraints Error During Terminal Exit (#1757) by @Andy in 32bf353da
|
||||
- auto-claude: 190-bug-context-page-crash-multiple-root-causes-when-v (#1724) by @Andy in 2db36982f
|
||||
- feat: add search/filter to WorktreeSelector dropdown (#1754) by @Andy in 09f059ca3
|
||||
- fix(terminal): push worktree branch to remote with tracking on creation (#1753) by @Andy in b5de0d9ff
|
||||
- auto-claude: 189-subtask-execution-stuck-in-infinite-retry-loop-whe (#1723) by @Andy in 445da186c
|
||||
- auto-claude: 188-terminal-claude-sessions-require-manual-click-to-r (#1743) by @Andy in f8499e965
|
||||
- auto-claude: 200-bug-changelog-and-release-generation (#1729) by @Andy in 826583b82
|
||||
- fix(terminal): use each terminal's cwd for invoke Claude all button (#1756) by @Andy in ac4fe4f42
|
||||
- feat(terminal): read Claude Code CLI settings and inject env vars into PTY sessions (#1750) by @Andy in 152e54093
|
||||
- fix: correct .auto-claude path mismatch causing discovery phase timeout (#1748) by @VDT-91 in 2c2a8a754
|
||||
- fix: remove incorrect /v1 suffix from OpenRouter API URL (#1749) by @StillKnotKnown in 7e799ee57
|
||||
- fix: prevent terminal worktree crash with race condition fixes (#1586) (#1658) by @VDT-91 in 216b58bcf
|
||||
- fix: correct log order sorting and add configurable log order setting (#1720) by @Burak in 2e2b82365
|
||||
- fix(ollama): stop infinite subprocess spawning from useEffect re-render loop (#1716) by @Quentin Veys in acb131b72
|
||||
- fix(graphiti): migrate graphiti_memory imports to canonical paths (#1714) by @Quentin Veys in df528f065
|
||||
- fix: improve auto-updater for beta releases and DMG installs (#1681) by @Andy in ff91a1af0
|
||||
- feat: unified operation registry for intelligent auth/rate limit recovery (#1698) by @Andy in 6d0222fa9
|
||||
- fix: Prevent stale worktree data from overriding correct task status (#1710) by @Burak in fe08c644c
|
||||
- feat: add subscriptionType and rateLimitTier to ClaudeProfile (#1688) by @Andy in a5e3cc9a2
|
||||
- auto-claude: subtask-1-1 - Add useTaskStore import and update task state after successful PR creation (#1683) by @Andy in 4587162e4
|
||||
- auto-claude: 182-implement-pagination-and-filtering-for-github-pr-l (#1654) by @Andy in b4e6b2fe4
|
||||
- auto-claude: 181-add-expand-button-for-long-task-descriptions (#1653) by @Andy in d9cd300fe
|
||||
- fix(terminal): resolve text alignment issues on expand/minimize (#1650) by @VDT-91 in f5a7e26d9
|
||||
- fix(windows): use full path to where.exe for reliable executable lookup (#1659) by @VDT-91 in 5f63daa3c
|
||||
- fix: resolve ideation stuck at 3/6 types bug (#1660) by @VDT-91 in e6e8da17c
|
||||
- Clarify Local and Origin Branch Distinction (#1652) by @Andy in 9317148b6
|
||||
- auto-claude: 186-set-default-dark-mode-on-startup (#1656) by @Andy in 473020621
|
||||
- auto-claude: subtask-1-1 - Add min-h-0 to enable scrolling in Roadmap tabs (#1655) by @Andy in ae703be9f
|
||||
- fix: XState status lifecycle & cross-project contamination fixes (#1647) by @kaigler in 5293fb399
|
||||
- refactor(frontend): complete XState task state machine migration (#1338) (#1575) by @kaigler in e2f9abadb
|
||||
- Merge conflict resolution progress bar and log viewer (#1620) by @Andy in d16be3077
|
||||
- fix: align Linux package builds (AppImage/deb/Flatpak) with target-specific extraResources (#1623) by @StillKnotKnown in bad1a9b2c
|
||||
- Fix/gitlab bugs (#1519 and #1521) (#1544) by @bu5hm4nn in cd423c65c
|
||||
- feat(kanban): add bulk task delete and worktree cleanup improvements (#1588) by @kaigler in 02ed91c91
|
||||
- fix: add worktree isolation warning to prevent agent escape (#1528) by @kaigler in fe5cc582b
|
||||
- feat(ui): add spell check support for text inputs (#1304) by @kaigler in 8f02a5129
|
||||
- fix(windows): complete Windows credential fixes with path normalization (#1585) by @kaigler in 1e1997167
|
||||
- AI-Powered GitHub PR Template Generation (#1618) by @Andy in 900dd4360
|
||||
- Fix pty.node SIGABRT crash on macOS shutdown (#1619) by @Andy in f355e09d7
|
||||
- fix(merge): use git merge for diverged branches with progress tracking (#1605) by @Andy in bde2ca4b2
|
||||
- Surface Billing/Credit Exhaustion Errors to UI (Issue #1580) (#1617) by @Andy in 7bf12e856
|
||||
- auto-claude: subtask-1-1 - Change $teamId type from ID! to String! in the team query (#1627) by @Andy in 54d0cd2f4
|
||||
- fix(auth): support API profile mode without OAuth requirement (#1616) by @StillKnotKnown in f8cc63af4
|
||||
- fix: agent retry loop for tool concurrency errors (#1546) [v3] (#1606) by @Michael Ludlow in 0aea4fb5e
|
||||
- fix(queue): enforce max parallel tasks and auto-refresh UI (#1594) by @Andy in 4070a4c29
|
||||
- Persist Kanban column collapse state per project via main process (#1579) by @Andy in a1114664e
|
||||
- feat(pr-review): evidence-based validation and trigger-driven exploration (#1593) by @Andy in bfc232825
|
||||
- fix(ui): smart auto-scroll for Insights streaming responses (#1591) by @kaigler in eee97e7ea
|
||||
- fix(changelog): validate Claude CLI exists before generation (#1305) by @kaigler in c1f24c07f
|
||||
- auto-claude: subtask-1-1 - Add min-w-0 class to subtask title row flex container (#1578) by @Andy in 286591c02
|
||||
- auto-claude: subtask-1-1 - Remove Popover wrapper and related functionality from ClaudeCodeStatusBadge (#1566) by @Andy in 8d18cc81a
|
||||
- fix(claude-profile): preserve subscriptionType and rateLimitTier during token refresh (#1556) by @Andy in 52e426a48
|
||||
- auto-claude: subtask-1-1 - Update cancelReview callback to handle both success and failure cases (#1551) by @Andy in d8f00fe5a
|
||||
- fix(backend): prioritize git remote detection over env var for repo (#1555) by @Andy in 9b07ed464
|
||||
- fix(backend): handle detached HEAD state when pushing branch for PR creation (#1560) by @Andy in 2b72694d0
|
||||
- fix: add explicit UTF-8 encoding across all Electron main process I/O (#1554) by @Andy in 4243530e9
|
||||
- fix(backend): pass OAuth token to Python subprocess for authentication by @AndyMik90 in 6f1002dd7
|
||||
- perf(frontend): async parallel worktree listing to prevent UI freezes (#1553) by @Andy in 399a7e736
|
||||
- auto-claude: subtask-1-1 - Remove amber lock indicator line from kanban resize handle (#1557) by @Andy in 83a64b88e
|
||||
- fix(frontend): resolve TerminalFontSettings infinite re-render loop (#1536) by @StillKnotKnown in 1c6266025
|
||||
- fix(frontend): respect hasCompletedOnboarding from ~/.claude.json (#1537) by @StillKnotKnown in 1860c2c43
|
||||
- fix: prevent planner from generating invalid verification types (#1388) (#1529) by @kaigler in 94d941333
|
||||
- fix(frontend): resolve Insights scroll-to-blank-space issue on macOS (ACS-382) (#1535) by @StillKnotKnown in 496b2b96a
|
||||
- feat: add customizable terminal fonts with OS-specific defaults (#1412) by @StillKnotKnown in f289107b8
|
||||
- Add dev mode screenshot capture warning (#1516) by @Andy in 16eeb301a
|
||||
- fix: add worktree isolation warnings to prevent agent escape (ACS-394) (#1495) by @StillKnotKnown in 1e453653b
|
||||
- fix: resolve flaky subprocess-spawn test on Windows CI (ACS-392) (#1494) by @StillKnotKnown in f6b264d56
|
||||
- feat(task-logger): strip ANSI escape codes from logs and extend coverage (#1411) by @StillKnotKnown in 988ec0c25
|
||||
- fix(frontend): use spawn() instead of exec() for Windows terminal launching (#1498) by @StillKnotKnown in 26c9083d3
|
||||
- fix(api-profiles): correct z.AI China preset URL and rename provider presets (#1500) by @StillKnotKnown in 05cf0a516
|
||||
- fix: validate branch pattern before worktree cleanup to prevent deleting wrong branch (#1493) by @StillKnotKnown in 8576754a1
|
||||
- Real-Time Updates for Insights Chat (#1511) by @Andy in d940b6ade
|
||||
- Fix Terminal UI Rendering Issues (#1514) by @Andy in 8d8306b8e
|
||||
- Fix terminal content resizing on expansion (#1512) by @Andy in 9f6c0026b
|
||||
- Restore Terminal Session History on App Restart (#1515) by @Andy in 63e2847fc
|
||||
- Move Reference Images Above Task Title & Fix Image Display Issues (#1513) by @Andy in b269ac305
|
||||
- auto-claude: 143-fix-github-integration-ui-refresh-issues (#1467) by @Andy in aa2cb4fa6
|
||||
- feat: Multi-profile account swapping with token refresh and queue routing (#1496) by @Andy in 1e72c8d77
|
||||
- Simplified Testing Strategy for Regression Prevention (#1379) by @Andy in ae4e48e8b
|
||||
- auto-claude: 152-persist-tasks-during-roadmap-regeneration (#1463) by @Andy in 9bd3d7e3b
|
||||
- Debug Kanban Memory & Add Sentry Monitoring (#1380) by @Andy in bc5f550ee
|
||||
- auto-claude: 147-remove-outdated-compatibility-shims (#1465) by @Andy in 53111dbb9
|
||||
- auto-claude: 162-fix-worktree-error-on-repeated-task-starts (#1453) by @Andy in b955badf7
|
||||
- auto-claude: 155-fix-pr-list-diff-display-metrics (#1458) by @Andy in 31f116db5
|
||||
- auto-claude: 151-fix-pr-review-agent-token-refresh-on-account-swap (#1456) by @Andy in d081af042
|
||||
- auto-claude: 148-add-progress-persistence-and-status-indicators (#1464) by @Andy in 4937d5745
|
||||
- auto-claude: 154-fix-task-modal-conflict-check-status-refresh (#1462) by @Andy in 0299009df
|
||||
- auto-claude: 153-widen-kanban-columns-and-add-collapse-feature (#1457) by @Andy in d65973075
|
||||
- auto-claude: subtask-1-1 - Add filter after map operation to remove empty str (#1466) by @Andy in 783f0fe0e
|
||||
- fix: add formatReleaseNotes helper for markdown changelog rendering (#1468) by @Andy in 43a97e1b3
|
||||
- feat(sidebar): add collapsible sidebar toggle (#1501) by @Michael Ludlow in d17c17887
|
||||
- fix(auth): check .credentials.json for Linux profile authentication (#1492) by @StillKnotKnown in 8d2f66291
|
||||
- auto-claude: subtask-1-1 - Replace ReleaseNotesRenderer with ReactMarkdown (#1454) by @Andy in 1185a558c
|
||||
- auto-claude: 156-fix-electron-app-version-detection-bug (#1459) by @Andy in 9a3b48c25
|
||||
- auto-claude: subtask-1-1 - Add --no-track flag to git worktree add command (#1455) by @Andy in 0c2990815
|
||||
- auto-claude: subtask-1-1 - Change task.specId to taskId in 3 startSpecCreation calls (#1461) by @Andy in 91edc0e14
|
||||
- fix(onboarding): align MemoryStep layout with Settings MemoryBackendSection (#1445) by @Michael Ludlow in e9de26d59
|
||||
- auto-claude: subtask-1-1 - Add metadata?.requireReviewBeforeCoding check (#1460) by @Andy in 426d56571
|
||||
- fix: use API profile environment variables for task title generation (#1471) by @JoshuaRileyDev in c5a0f042d
|
||||
- fix(auth): Long-lived OAuth authentication with multi-profile usage display (#1443) by @Andy in 12e788417
|
||||
- feat: Add screenshot capture to task creation modal (#1429) by @JoshuaRileyDev in 1a2a1b1fc
|
||||
- fix: prevent queue settings modal from disappearing when tasks change (#1430) by @JoshuaRileyDev in 33acc1430
|
||||
- feat: Queue System v2 with Auto-Promotion and Smart Task Management (#1203) by @JoshuaRileyDev in 3b87e24d7
|
||||
- feat: Add API profile providers usage endpoints support (#1279) by @StillKnotKnown in cfe7dedd0
|
||||
|
||||
## Thanks to all contributors
|
||||
|
||||
@AndyMik90, @Andy, @Burak, @StillKnotKnown, @VDT-91, @kaigler, @Michael Ludlow, @JoshuaRileyDev, @Quentin Veys, @bu5hm4nn
|
||||
|
||||
## 2.7.5 - Security & Platform Improvements
|
||||
|
||||
### ✨ New Features
|
||||
|
||||
@@ -40,6 +40,29 @@ Auto Claude is a desktop application (+ CLI) where users describe a goal and AI
|
||||
|
||||
**PR target** — Always target the `develop` branch for PRs to AndyMik90/Auto-Claude, NOT `main`.
|
||||
|
||||
**No console.log for debugging production issues** — `console.log` output is not visible in bundled/packaged versions of the Electron app. Use Sentry for error tracking and diagnostics in production. Reserve `console.log` for development only.
|
||||
|
||||
## Work Approach
|
||||
|
||||
**Investigate before speculating** — Always read the actual code before proposing root causes. Spawn agents to grep and read relevant source files before forming any hypothesis. Never guess at causes without evidence from the codebase.
|
||||
|
||||
**Spawn agents for complex tasks** — When tackling complex tasks, spawn sub-agents/agent teams immediately rather than trying to handle everything in a single context window. Never attempt to analyze large codebases or multiple features monolithically.
|
||||
|
||||
**Minimal fixes only** — Prefer the simplest approach (e.g., prompt-only changes, single guard clause) before suggesting multi-component solutions. If the user asks for X, implement X — don't bundle additional fixes they didn't request.
|
||||
|
||||
## Known Gotchas
|
||||
|
||||
**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
|
||||
|
||||
```
|
||||
@@ -98,30 +121,6 @@ cd apps/backend && uv venv && uv pip install -r requirements.txt
|
||||
cd apps/frontend && npm install
|
||||
```
|
||||
|
||||
### Backend
|
||||
```bash
|
||||
cd apps/backend
|
||||
python spec_runner.py --interactive # Create spec interactively
|
||||
python spec_runner.py --task "description" # Create from task
|
||||
python run.py --spec 001 # Run autonomous build
|
||||
python run.py --spec 001 --qa # Run QA validation
|
||||
python run.py --spec 001 --merge # Merge completed build
|
||||
python run.py --list # List all specs
|
||||
```
|
||||
|
||||
### Frontend
|
||||
```bash
|
||||
cd apps/frontend
|
||||
npm run dev # Dev mode (Electron + Vite HMR)
|
||||
npm run build # Production build
|
||||
npm run test # Vitest unit tests
|
||||
npm run test:watch # Vitest watch mode
|
||||
npm run lint # Biome check
|
||||
npm run lint:fix # Biome auto-fix
|
||||
npm run typecheck # TypeScript strict check
|
||||
npm run package # Package for distribution
|
||||
```
|
||||
|
||||
### Testing
|
||||
|
||||
| Stack | Command | Tool |
|
||||
@@ -145,30 +144,7 @@ See [RELEASE.md](RELEASE.md) for full release process.
|
||||
|
||||
Client: `apps/backend/core/client.py` — `create_client()` returns a configured `ClaudeSDKClient` with security hooks, tool permissions, and MCP server integration.
|
||||
|
||||
Model and thinking level are user-configurable (via the Electron UI settings or CLI override). Use `phase_config.py` helpers to resolve the correct values:
|
||||
|
||||
```python
|
||||
from core.client import create_client
|
||||
from phase_config import get_phase_model, get_phase_thinking_budget
|
||||
|
||||
# Resolve model/thinking from user settings (Electron UI or CLI override)
|
||||
phase_model = get_phase_model(spec_dir, "coding", cli_model=None)
|
||||
phase_thinking = get_phase_thinking_budget(spec_dir, "coding", cli_thinking=None)
|
||||
|
||||
client = create_client(
|
||||
project_dir=project_dir,
|
||||
spec_dir=spec_dir,
|
||||
model=phase_model,
|
||||
agent_type="coder", # planner | coder | qa_reviewer | qa_fixer
|
||||
max_thinking_tokens=phase_thinking,
|
||||
)
|
||||
|
||||
# Run agent session (uses context manager + run_agent_session helper)
|
||||
async with client:
|
||||
status, response = await run_agent_session(client, prompt, spec_dir)
|
||||
```
|
||||
|
||||
Working examples: `agents/planner.py`, `agents/coder.py`, `qa/reviewer.py`, `qa/fixer.py`, `spec/`
|
||||
Model and thinking level are user-configurable (via the Electron UI settings or CLI override). Use `phase_config.py` helpers to resolve the correct values
|
||||
|
||||
### Agent Prompts (`apps/backend/prompts/`)
|
||||
|
||||
@@ -323,6 +299,8 @@ cd apps/backend && python run.py --spec 001
|
||||
# Desktop app
|
||||
npm start # Production build + run
|
||||
npm run dev # Development mode with HMR
|
||||
npm run dev:debug # Debug mode with verbose output
|
||||
npm run dev:mcp # Electron MCP server for AI debugging
|
||||
|
||||
# Project data: .auto-claude/specs/ (gitignored)
|
||||
```
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
[](https://discord.gg/KCXaPBr4Dj)
|
||||
[](https://www.youtube.com/@AndreMikalsen)
|
||||
[](https://github.com/AndyMik90/Auto-Claude/actions)
|
||||
[](https://github.com/hesreallyhim/awesome-claude-code)
|
||||
|
||||
---
|
||||
|
||||
@@ -16,18 +17,18 @@
|
||||
### Stable Release
|
||||
|
||||
<!-- STABLE_VERSION_BADGE -->
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.5)
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6)
|
||||
<!-- STABLE_VERSION_BADGE_END -->
|
||||
|
||||
<!-- STABLE_DOWNLOADS -->
|
||||
| Platform | Download |
|
||||
|----------|----------|
|
||||
| **Windows** | [Auto-Claude-2.7.5-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.5-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.5-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.5-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.5-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.5-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.5/Auto-Claude-2.7.5-linux-x86_64.flatpak) |
|
||||
| **Windows** | [Auto-Claude-2.7.6-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6/Auto-Claude-2.7.6-linux-x86_64.flatpak) |
|
||||
<!-- STABLE_DOWNLOADS_END -->
|
||||
|
||||
### Beta Release
|
||||
@@ -35,18 +36,18 @@
|
||||
> ⚠️ Beta releases may contain bugs and breaking changes. [View all releases](https://github.com/AndyMik90/Auto-Claude/releases)
|
||||
|
||||
<!-- BETA_VERSION_BADGE -->
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.3)
|
||||
[](https://github.com/AndyMik90/Auto-Claude/releases/tag/v2.7.6-beta.6)
|
||||
<!-- BETA_VERSION_BADGE_END -->
|
||||
|
||||
<!-- BETA_DOWNLOADS -->
|
||||
| Platform | Download |
|
||||
|----------|----------|
|
||||
| **Windows** | [Auto-Claude-2.7.6-beta.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) |
|
||||
| **Windows** | [Auto-Claude-2.7.6-beta.6-win32-x64.exe](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-win32-x64.exe) |
|
||||
| **macOS (Apple Silicon)** | [Auto-Claude-2.7.6-beta.6-darwin-arm64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-darwin-arm64.dmg) |
|
||||
| **macOS (Intel)** | [Auto-Claude-2.7.6-beta.6-darwin-x64.dmg](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-darwin-x64.dmg) |
|
||||
| **Linux** | [Auto-Claude-2.7.6-beta.6-linux-x86_64.AppImage](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-linux-x86_64.AppImage) |
|
||||
| **Linux (Debian)** | [Auto-Claude-2.7.6-beta.6-linux-amd64.deb](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-linux-amd64.deb) |
|
||||
| **Linux (Flatpak)** | [Auto-Claude-2.7.6-beta.6-linux-x86_64.flatpak](https://github.com/AndyMik90/Auto-Claude/releases/download/v2.7.6-beta.6/Auto-Claude-2.7.6-beta.6-linux-x86_64.flatpak) |
|
||||
<!-- BETA_DOWNLOADS_END -->
|
||||
|
||||
> All releases include SHA256 checksums and VirusTotal scan results for security verification.
|
||||
|
||||
@@ -62,5 +62,14 @@ Thumbs.db
|
||||
# Tests (development only)
|
||||
tests/
|
||||
|
||||
# Exception: Allow colocated tests within integrations/graphiti
|
||||
!integrations/graphiti/tests/
|
||||
|
||||
# Auto Claude data directory
|
||||
.auto-claude/
|
||||
|
||||
# Auto Claude generated files
|
||||
.auto-claude-security.json
|
||||
.auto-claude-status
|
||||
.security-key
|
||||
logs/security/
|
||||
|
||||
@@ -19,5 +19,5 @@ Quick Start:
|
||||
See README.md for full documentation.
|
||||
"""
|
||||
|
||||
__version__ = "2.7.6-beta.3"
|
||||
__version__ = "2.7.6"
|
||||
__author__ = "Auto Claude Team"
|
||||
|
||||
@@ -14,6 +14,7 @@ from core.error_utils import (
|
||||
is_authentication_error,
|
||||
is_rate_limit_error,
|
||||
is_tool_concurrency_error,
|
||||
safe_receive_messages,
|
||||
)
|
||||
from core.file_utils import write_json_atomic
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
@@ -490,7 +491,7 @@ async def run_agent_session(
|
||||
# Collect response text and show tool use
|
||||
response_text = ""
|
||||
debug("session", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="session"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -292,6 +292,14 @@ 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
|
||||
|
||||
@@ -31,6 +31,7 @@ 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()
|
||||
@@ -124,6 +125,63 @@ 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,6 +40,8 @@ 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()
|
||||
|
||||
@@ -209,6 +211,121 @@ 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"):
|
||||
|
||||
@@ -10,6 +10,7 @@ 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
|
||||
|
||||
|
||||
@@ -151,13 +152,22 @@ def handle_batch_status_command(project_dir: str) -> bool:
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
|
||||
# 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():
|
||||
# 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():
|
||||
status = "spec_created"
|
||||
else:
|
||||
status = "pending_spec"
|
||||
|
||||
@@ -165,7 +175,10 @@ 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, "❓")
|
||||
|
||||
@@ -192,10 +205,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
|
||||
# Find completed specs (only QA-approved, matching status display logic)
|
||||
completed = []
|
||||
for spec_dir in specs_dir.iterdir():
|
||||
if spec_dir.is_dir() and (spec_dir / "qa_report.md").exists():
|
||||
if spec_dir.is_dir() and is_qa_approved(spec_dir):
|
||||
completed.append(spec_dir.name)
|
||||
|
||||
if not completed:
|
||||
|
||||
@@ -449,7 +449,7 @@ def _handle_build_interrupt(
|
||||
if choice == "skip":
|
||||
print()
|
||||
print_status("Resuming build...", "info")
|
||||
status_manager.update(state=BuildState.RUNNING)
|
||||
status_manager.update(state=BuildState.BUILDING)
|
||||
asyncio.run(
|
||||
run_autonomous_agent(
|
||||
project_dir=working_dir,
|
||||
|
||||
@@ -694,10 +694,25 @@ 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)
|
||||
@@ -705,12 +720,13 @@ 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
|
||||
debug = os.environ.get("DEBUG", "").lower() in ("true", "1")
|
||||
if debug and effective_config_dir:
|
||||
if _debug and effective_config_dir:
|
||||
service_name = _get_keychain_service_name(effective_config_dir)
|
||||
logger.info(
|
||||
f"[Auth] Resolving credentials for profile config_dir: {effective_config_dir} "
|
||||
f"(Keychain service: {service_name})"
|
||||
"[Auth] Resolving credentials for profile config_dir: %s "
|
||||
"(Keychain service: %s)",
|
||||
effective_config_dir,
|
||||
service_name,
|
||||
)
|
||||
|
||||
# If a custom config directory is specified, read from there first
|
||||
@@ -718,24 +734,37 @@ 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(
|
||||
f"No credentials found for config_dir '{effective_config_dir}' "
|
||||
"in file or keychain"
|
||||
"No credentials found for config_dir '%s' in file or keychain",
|
||||
effective_config_dir,
|
||||
)
|
||||
return None
|
||||
|
||||
# No config_dir specified - use default system credential store
|
||||
return _try_decrypt_token(get_token_from_keychain())
|
||||
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)
|
||||
|
||||
|
||||
def get_auth_token_source(config_dir: str | None = None) -> str | None:
|
||||
@@ -970,8 +999,18 @@ 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"):
|
||||
@@ -999,6 +1038,14 @@ 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:
|
||||
"""
|
||||
|
||||
+107
-1
@@ -29,6 +29,89 @@ from core.platform import (
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# =============================================================================
|
||||
# SDK Message Parser Patch
|
||||
# =============================================================================
|
||||
# The Claude Agent SDK's message_parser raises MessageParseError for unknown
|
||||
# message types (e.g., "rate_limit_event"). Since parse_message runs inside an
|
||||
# async generator, the exception kills the entire agent session stream.
|
||||
# Patch to log a warning and return a SystemMessage instead of crashing.
|
||||
# This is needed until the SDK natively handles all CLI message types.
|
||||
|
||||
|
||||
def _patch_sdk_message_parser() -> None:
|
||||
"""Patch the SDK's parse_message to handle unknown message types gracefully.
|
||||
|
||||
The Claude CLI may emit message types that the installed SDK version doesn't
|
||||
recognize (e.g., rate_limit_event, usage_event). Without this patch, any
|
||||
unrecognized type raises MessageParseError inside the SDK's async generator,
|
||||
which terminates the entire response stream and kills the agent session.
|
||||
|
||||
The patch converts unknown types into SystemMessage objects with a
|
||||
'unknown_<type>' subtype, which all message consumers silently skip.
|
||||
"""
|
||||
try:
|
||||
import claude_agent_sdk._internal.message_parser as _parser
|
||||
from claude_agent_sdk._errors import MessageParseError
|
||||
from claude_agent_sdk.types import SystemMessage
|
||||
|
||||
_original_parse = _parser.parse_message
|
||||
|
||||
def _patched_parse(data):
|
||||
try:
|
||||
return _original_parse(data)
|
||||
except MessageParseError as e:
|
||||
msg = str(e)
|
||||
if "Unknown message type" in msg:
|
||||
msg_type = (
|
||||
data.get("type", "unknown")
|
||||
if isinstance(data, dict)
|
||||
else "unknown"
|
||||
)
|
||||
# Rate limit events deserve a visible warning; others just debug-level
|
||||
if "rate_limit" in msg_type:
|
||||
retry_after = (
|
||||
data.get("retry_after")
|
||||
or data.get("data", {}).get("retry_after")
|
||||
if isinstance(data, dict)
|
||||
else None
|
||||
)
|
||||
retry_info = (
|
||||
f" (retry_after={retry_after}s)" if retry_after else ""
|
||||
)
|
||||
logger.warning(
|
||||
f"Rate limit event received from CLI{retry_info} — "
|
||||
f"the SDK will handle backoff automatically"
|
||||
)
|
||||
else:
|
||||
logger.debug(
|
||||
f"SDK received unhandled message type '{msg_type}', skipping"
|
||||
)
|
||||
return SystemMessage(
|
||||
subtype=f"unknown_{msg_type}",
|
||||
data=data if isinstance(data, dict) else {},
|
||||
)
|
||||
raise
|
||||
|
||||
_parser.parse_message = _patched_parse
|
||||
except Exception as e:
|
||||
logger.warning(f"Failed to patch SDK message parser: {e}")
|
||||
|
||||
|
||||
_patch_sdk_message_parser()
|
||||
|
||||
# =============================================================================
|
||||
# Windows System Prompt Limits
|
||||
# =============================================================================
|
||||
# Windows CreateProcessW has a 32,768 character limit for the entire command line.
|
||||
# When CLAUDE.md is very large and passed as --system-prompt, the command can exceed
|
||||
# this limit, causing ERROR_FILE_NOT_FOUND. We cap CLAUDE.md content to stay safe.
|
||||
# 20,000 chars leaves ~12KB headroom for CLI overhead (model, tools, MCP config, etc.)
|
||||
WINDOWS_MAX_SYSTEM_PROMPT_CHARS = 20000
|
||||
WINDOWS_TRUNCATION_MESSAGE = (
|
||||
"\n\n[... CLAUDE.md truncated due to Windows command-line length limit ...]"
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Project Index Cache
|
||||
# =============================================================================
|
||||
@@ -821,8 +904,31 @@ def create_client(
|
||||
if should_use_claude_md():
|
||||
claude_md_content = load_claude_md(project_dir)
|
||||
if claude_md_content:
|
||||
# On Windows, the SDK passes system_prompt as a --system-prompt CLI argument.
|
||||
# Windows CreateProcessW has a 32,768 character limit for the entire command line.
|
||||
# When CLAUDE.md is very large, the command can exceed this limit, causing Windows
|
||||
# to return ERROR_FILE_NOT_FOUND which the SDK misreports as "Claude Code not found".
|
||||
# Cap CLAUDE.md content to keep total command line under the limit. (#1661)
|
||||
was_truncated = False
|
||||
if is_windows():
|
||||
max_claude_md_chars = (
|
||||
WINDOWS_MAX_SYSTEM_PROMPT_CHARS
|
||||
- len(base_prompt)
|
||||
- len(WINDOWS_TRUNCATION_MESSAGE)
|
||||
- len("\n\n# Project Instructions (from CLAUDE.md)\n\n")
|
||||
)
|
||||
if len(claude_md_content) > max_claude_md_chars > 0:
|
||||
claude_md_content = (
|
||||
claude_md_content[:max_claude_md_chars]
|
||||
+ WINDOWS_TRUNCATION_MESSAGE
|
||||
)
|
||||
print(
|
||||
" - CLAUDE.md: truncated (exceeded Windows command-line limit)"
|
||||
)
|
||||
was_truncated = True
|
||||
base_prompt = f"{base_prompt}\n\n# Project Instructions (from CLAUDE.md)\n\n{claude_md_content}"
|
||||
print(" - CLAUDE.md: included in system prompt")
|
||||
if not was_truncated:
|
||||
print(" - CLAUDE.md: included in system prompt")
|
||||
else:
|
||||
print(" - CLAUDE.md: not found in project root")
|
||||
else:
|
||||
|
||||
@@ -6,7 +6,17 @@ Common error detection and classification functions used across
|
||||
agent sessions, QA, and other modules.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from collections.abc import AsyncIterator
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from claude_agent_sdk.types import Message
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def is_tool_concurrency_error(error: Exception) -> bool:
|
||||
@@ -118,3 +128,61 @@ def is_authentication_error(error: Exception) -> bool:
|
||||
"please login again",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
async def safe_receive_messages(
|
||||
client,
|
||||
*,
|
||||
caller: str = "agent",
|
||||
) -> AsyncIterator[Message]:
|
||||
"""Iterate over SDK messages with resilience against unexpected errors.
|
||||
|
||||
The SDK's ``receive_response()`` async generator can terminate early if:
|
||||
1. An unhandled message type slips past the monkey-patch (e.g., SDK upgrade
|
||||
removes the patch surface).
|
||||
2. A transient parse error corrupts a single message in the stream.
|
||||
3. An unexpected ``StopAsyncIteration`` or runtime error occurs mid-stream.
|
||||
|
||||
This wrapper catches per-message errors, logs them, and continues yielding
|
||||
subsequent messages so the agent session can complete its work.
|
||||
|
||||
It also detects rate-limit events (surfaced as ``SystemMessage`` with
|
||||
subtype ``unknown_rate_limit_event``) and logs a user-visible warning.
|
||||
|
||||
Args:
|
||||
client: A ``ClaudeSDKClient`` instance (must be inside ``async with``).
|
||||
caller: Label for log messages (e.g., "session", "agent_runner").
|
||||
|
||||
Yields:
|
||||
Parsed ``Message`` objects from the SDK response stream.
|
||||
"""
|
||||
try:
|
||||
async for msg in client.receive_response():
|
||||
# Detect rate-limit events surfaced by the monkey-patch
|
||||
msg_type = type(msg).__name__
|
||||
if msg_type == "SystemMessage":
|
||||
subtype = getattr(msg, "subtype", "")
|
||||
if subtype.startswith("unknown_"):
|
||||
original_type = subtype[len("unknown_") :]
|
||||
if "rate_limit" in original_type:
|
||||
data = getattr(msg, "data", {})
|
||||
retry_after = data.get("retry_after") or data.get(
|
||||
"data", {}
|
||||
).get("retry_after")
|
||||
retry_info = (
|
||||
f" (retry in {retry_after}s)" if retry_after else ""
|
||||
)
|
||||
logger.warning(f"[{caller}] Rate limit event{retry_info}")
|
||||
else:
|
||||
logger.debug(
|
||||
f"[{caller}] Skipping unknown SDK message type: {original_type}"
|
||||
)
|
||||
continue
|
||||
yield msg
|
||||
except GeneratorExit:
|
||||
return
|
||||
except Exception as e:
|
||||
# If the generator itself raises (e.g., transport error), log and stop
|
||||
# gracefully so callers can process whatever was collected so far.
|
||||
logger.error(f"[{caller}] SDK response stream terminated unexpectedly: {e}")
|
||||
return
|
||||
|
||||
@@ -9,8 +9,11 @@ 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,
|
||||
@@ -230,8 +233,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):
|
||||
pass # Ignore corrupted/unreadable progress files
|
||||
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as e:
|
||||
logger.debug(f"Failed to load plan file for phase summary: {e}")
|
||||
else:
|
||||
print()
|
||||
print_status("No implementation subtasks yet - planner needs to run", "pending")
|
||||
@@ -404,6 +407,8 @@ 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
|
||||
|
||||
@@ -415,6 +420,23 @@ 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)
|
||||
@@ -455,9 +477,15 @@ def get_next_subtask(spec_dir: Path) -> dict | None:
|
||||
if not deps_satisfied:
|
||||
continue
|
||||
|
||||
# Find first pending subtask in this phase
|
||||
# Find first pending subtask in this phase (skip stuck subtasks)
|
||||
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"
|
||||
|
||||
@@ -186,14 +186,12 @@ 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
|
||||
@@ -212,20 +210,11 @@ def init_sentry(
|
||||
logger.debug("[Sentry] No SENTRY_DSN configured - error reporting disabled")
|
||||
return False
|
||||
|
||||
# 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
|
||||
# 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.
|
||||
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
|
||||
|
||||
@@ -0,0 +1,176 @@
|
||||
"""
|
||||
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
|
||||
@@ -273,3 +273,31 @@ 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"
|
||||
|
||||
@@ -14,6 +14,7 @@ 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 (
|
||||
@@ -28,8 +29,9 @@ from ui import (
|
||||
)
|
||||
from worktree import WorktreeManager
|
||||
|
||||
from .dependency_strategy import get_dependency_configs
|
||||
from .git_utils import has_uncommitted_changes
|
||||
from .models import WorkspaceMode
|
||||
from .models import DependencyShareConfig, DependencyStrategy, WorkspaceMode
|
||||
|
||||
# Import debug utilities
|
||||
try:
|
||||
@@ -189,11 +191,37 @@ def symlink_node_modules_to_worktree(
|
||||
"""
|
||||
Symlink node_modules directories from project root to worktree.
|
||||
|
||||
This ensures the worktree has access to dependencies for TypeScript checks
|
||||
and other tooling without requiring a separate npm install.
|
||||
.. deprecated::
|
||||
Use :func:`setup_worktree_dependencies` instead, which handles all
|
||||
dependency types (node_modules, venvs, vendor dirs, etc.) via
|
||||
strategy-based dispatch.
|
||||
|
||||
Works with npm workspace hoisting where dependencies are hoisted to root
|
||||
and workspace-specific dependencies remain in nested node_modules.
|
||||
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.
|
||||
|
||||
Args:
|
||||
project_dir: The main project directory
|
||||
@@ -204,81 +232,52 @@ def symlink_node_modules_to_worktree(
|
||||
"""
|
||||
symlinked = []
|
||||
|
||||
# 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"),
|
||||
]
|
||||
source_path = project_dir / ".claude"
|
||||
target_path = worktree_path / ".claude"
|
||||
|
||||
for source_rel, target_rel in node_modules_locations:
|
||||
source_path = project_dir / source_rel
|
||||
target_path = worktree_path / target_rel
|
||||
# Skip if source doesn't exist
|
||||
if not source_path.exists():
|
||||
debug(MODULE, "Skipping .claude/ - source does not exist")
|
||||
return symlinked
|
||||
|
||||
# Skip if source doesn't exist
|
||||
if not source_path.exists():
|
||||
debug(MODULE, f"Skipping {source_rel} - source does not exist")
|
||||
continue
|
||||
# Skip if target already exists
|
||||
if target_path.exists():
|
||||
debug(MODULE, "Skipping .claude/ - target already exists")
|
||||
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
|
||||
# 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
|
||||
|
||||
# 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",
|
||||
# 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,
|
||||
)
|
||||
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
|
||||
|
||||
@@ -374,13 +373,33 @@ def setup_workspace(
|
||||
f"Environment files copied: {', '.join(copied_env_files)}", "success"
|
||||
)
|
||||
|
||||
# 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(
|
||||
# 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(
|
||||
project_dir, worktree_info.path
|
||||
)
|
||||
if symlinked_modules:
|
||||
print_status(f"Dependencies linked: {', '.join(symlinked_modules)}", "success")
|
||||
if symlinked_claude:
|
||||
print_status(f"Claude config linked: {', '.join(symlinked_claude)}", "success")
|
||||
|
||||
# Copy security configuration files if they exist
|
||||
# Note: Unlike env files, security files always overwrite to ensure
|
||||
@@ -574,6 +593,299 @@ 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
|
||||
|
||||
@@ -430,6 +430,7 @@ 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(
|
||||
@@ -510,6 +511,7 @@ 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(
|
||||
@@ -1209,6 +1211,9 @@ class WorktreeManager:
|
||||
)
|
||||
|
||||
target = target_branch or self.base_branch
|
||||
# Strip remote prefix (e.g., "origin/feat/x" → "feat/x") since gh expects branch names only
|
||||
if target.startswith("origin/"):
|
||||
target = target[len("origin/") :]
|
||||
pr_title = title or f"auto-claude: {spec_name}"
|
||||
|
||||
# Try AI-powered PR body from project's PR template, fall back to spec summary
|
||||
@@ -1379,6 +1384,9 @@ class WorktreeManager:
|
||||
)
|
||||
|
||||
target = target_branch or self.base_branch
|
||||
# Strip remote prefix (e.g., "origin/feat/x" → "feat/x") since glab expects branch names only
|
||||
if target.startswith("origin/"):
|
||||
target = target[len("origin/") :]
|
||||
mr_title = title or f"auto-claude: {spec_name}"
|
||||
|
||||
# Get MR body from spec.md if available
|
||||
|
||||
@@ -624,7 +624,10 @@ def get_graphiti_status() -> dict:
|
||||
|
||||
# CRITICAL FIX: Actually verify packages are importable before reporting available
|
||||
# Don't just check config.is_valid() - actually try to import the module
|
||||
if not config.is_valid():
|
||||
# Note: This branch is currently unreachable because is_valid() returns True
|
||||
# whenever enabled is True. Kept for defensive purposes in case is_valid()
|
||||
# logic changes in the future.
|
||||
if not config.is_valid(): # pragma: no cover
|
||||
status["reason"] = errors[0] if errors else "Configuration invalid"
|
||||
return status
|
||||
|
||||
@@ -635,7 +638,7 @@ def get_graphiti_status() -> dict:
|
||||
from graphiti_core.driver.falkordb_driver import FalkorDriver # noqa: F401
|
||||
|
||||
# If we got here, packages are importable
|
||||
status["available"] = True
|
||||
status["available"] = True # pragma: no cover
|
||||
except ImportError as e:
|
||||
status["available"] = False
|
||||
status["reason"] = f"Graphiti packages not installed: {e}"
|
||||
|
||||
@@ -72,6 +72,8 @@ async def test_graphiti_connection() -> tuple[bool, str]:
|
||||
"""
|
||||
Test if LadybugDB is available and Graphiti can connect.
|
||||
|
||||
Uses the embedded LadybugDB via the patched KuzuDriver (no remote connection).
|
||||
|
||||
Returns:
|
||||
Tuple of (success: bool, message: str)
|
||||
"""
|
||||
@@ -87,43 +89,48 @@ async def test_graphiti_connection() -> tuple[bool, str]:
|
||||
|
||||
try:
|
||||
from graphiti_core import Graphiti
|
||||
from graphiti_core.driver.falkordb_driver import FalkorDriver
|
||||
from graphiti_providers import ProviderError, create_embedder, create_llm_client
|
||||
|
||||
# Import the patched driver creator (handles LadybugDB monkeypatch internally)
|
||||
from integrations.graphiti.queries_pkg.client import _apply_ladybug_monkeypatch
|
||||
from integrations.graphiti.queries_pkg.kuzu_driver_patched import (
|
||||
create_patched_kuzu_driver,
|
||||
)
|
||||
|
||||
# Create providers
|
||||
try:
|
||||
llm_client = create_llm_client(config)
|
||||
embedder = create_embedder(config)
|
||||
llm_client = create_llm_client(config) # pragma: no cover
|
||||
embedder = create_embedder(config) # pragma: no cover
|
||||
except ProviderError as e:
|
||||
return False, f"Provider error: {e}"
|
||||
|
||||
# Try to connect
|
||||
driver = FalkorDriver(
|
||||
host=config.falkordb_host,
|
||||
port=config.falkordb_port,
|
||||
password=config.falkordb_password or None,
|
||||
database=config.database,
|
||||
)
|
||||
# Apply LadybugDB monkeypatch for embedded database
|
||||
if not _apply_ladybug_monkeypatch(): # pragma: no cover
|
||||
return False, "LadybugDB not installed (requires Python 3.12+)"
|
||||
|
||||
graphiti = Graphiti(
|
||||
# Create embedded database driver
|
||||
db_path = config.get_db_path()
|
||||
driver = create_patched_kuzu_driver(db=str(db_path)) # pragma: no cover
|
||||
|
||||
graphiti = Graphiti( # pragma: no cover
|
||||
graph_driver=driver,
|
||||
llm_client=llm_client,
|
||||
embedder=embedder,
|
||||
)
|
||||
|
||||
# Try a simple operation
|
||||
await graphiti.build_indices_and_constraints()
|
||||
await graphiti.close()
|
||||
await graphiti.build_indices_and_constraints() # pragma: no cover
|
||||
await graphiti.close() # pragma: no cover
|
||||
|
||||
return True, (
|
||||
f"Connected to LadybugDB at {config.falkordb_host}:{config.falkordb_port} "
|
||||
return True, ( # pragma: no cover
|
||||
f"Connected to LadybugDB at {db_path} "
|
||||
f"(providers: {config.get_provider_summary()})"
|
||||
)
|
||||
|
||||
except ImportError as e:
|
||||
return False, f"Graphiti packages not installed: {e}"
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # pragma: no cover
|
||||
return False, f"Connection failed: {e}"
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,9 @@ 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
|
||||
|
||||
@@ -14,6 +16,27 @@ 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:
|
||||
"""
|
||||
@@ -196,32 +219,36 @@ class GraphitiClient:
|
||||
)
|
||||
|
||||
db_path = self.config.get_db_path()
|
||||
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
|
||||
|
||||
# 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
|
||||
|
||||
logger.info(f"Initialized LadybugDB driver (patched) at: {db_path}")
|
||||
except ImportError as e:
|
||||
logger.warning(f"KuzuDriver not available: {e}")
|
||||
|
||||
@@ -15,7 +15,10 @@ from typing import Any
|
||||
# Import kuzu (might be real_ladybug via monkeypatch)
|
||||
try:
|
||||
import kuzu
|
||||
except ImportError:
|
||||
except ImportError: # pragma: no cover
|
||||
# Fallback to real_ladybug if kuzu is not available.
|
||||
# This import-time fallback is hard to test in normal unit tests
|
||||
# since the module is imported once before tests can mock anything.
|
||||
import real_ladybug as kuzu # type: ignore
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -67,7 +67,8 @@ class GraphitiSearch:
|
||||
Args:
|
||||
query: Search query
|
||||
num_results: Maximum number of results to return
|
||||
include_project_context: If True and in PROJECT mode, search project-wide
|
||||
include_project_context: If True and in SPEC mode, also search project-wide
|
||||
min_score: Minimum relevance score threshold (0.0 to 1.0)
|
||||
|
||||
Returns:
|
||||
List of relevant context items with content, score, and type
|
||||
@@ -101,10 +102,14 @@ class GraphitiSearch:
|
||||
or str(result)
|
||||
)
|
||||
|
||||
# Normalize score to float, treating None as 0.0
|
||||
raw_score = getattr(result, "score", None)
|
||||
score = raw_score if raw_score is not None else 0.0
|
||||
|
||||
context_items.append(
|
||||
{
|
||||
"content": content,
|
||||
"score": getattr(result, "score", 0.0),
|
||||
"score": score,
|
||||
"type": getattr(result, "type", "unknown"),
|
||||
}
|
||||
)
|
||||
@@ -112,7 +117,9 @@ class GraphitiSearch:
|
||||
# Filter by minimum score if specified
|
||||
if min_score > 0:
|
||||
context_items = [
|
||||
item for item in context_items if item.get("score", 0) >= min_score
|
||||
item
|
||||
for item in context_items
|
||||
if (item.get("score", 0.0)) >= min_score
|
||||
]
|
||||
|
||||
logger.info(
|
||||
@@ -225,12 +232,14 @@ class GraphitiSearch:
|
||||
if not isinstance(data, dict):
|
||||
continue
|
||||
if data.get("type") == EPISODE_TYPE_TASK_OUTCOME:
|
||||
raw_score = getattr(result, "score", None)
|
||||
score = raw_score if raw_score is not None else 0.0
|
||||
outcomes.append(
|
||||
{
|
||||
"task_id": data.get("task_id"),
|
||||
"success": data.get("success"),
|
||||
"outcome": data.get("outcome"),
|
||||
"score": getattr(result, "score", 0.0),
|
||||
"score": score,
|
||||
}
|
||||
)
|
||||
except (json.JSONDecodeError, TypeError, AttributeError):
|
||||
@@ -284,7 +293,8 @@ class GraphitiSearch:
|
||||
content = getattr(result, "content", None) or getattr(
|
||||
result, "fact", None
|
||||
)
|
||||
score = getattr(result, "score", 0.0)
|
||||
raw_score = getattr(result, "score", None)
|
||||
score = raw_score if raw_score is not None else 0.0
|
||||
|
||||
if score < min_score:
|
||||
continue
|
||||
@@ -320,7 +330,8 @@ class GraphitiSearch:
|
||||
content = getattr(result, "content", None) or getattr(
|
||||
result, "fact", None
|
||||
)
|
||||
score = getattr(result, "score", 0.0)
|
||||
raw_score = getattr(result, "score", None)
|
||||
score = raw_score if raw_score is not None else 0.0
|
||||
|
||||
if score < min_score:
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,716 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test Script for Memory Integration with LadybugDB
|
||||
=================================================
|
||||
|
||||
This script tests the memory layer (graph + semantic search) to verify
|
||||
data is being saved and retrieved correctly from LadybugDB (embedded Kuzu).
|
||||
|
||||
LadybugDB is an embedded graph database - no Docker required!
|
||||
|
||||
Usage:
|
||||
# Set environment variables first (or in .env file):
|
||||
export GRAPHITI_ENABLED=true
|
||||
export GRAPHITI_EMBEDDER_PROVIDER=ollama # or: openai, voyage, azure_openai, google
|
||||
|
||||
# For Ollama (recommended - free, local):
|
||||
export OLLAMA_EMBEDDING_MODEL=embeddinggemma
|
||||
export OLLAMA_EMBEDDING_DIM=768
|
||||
|
||||
# For OpenAI:
|
||||
export OPENAI_API_KEY=sk-...
|
||||
|
||||
# Run the test:
|
||||
cd auto-claude
|
||||
python integrations/graphiti/run_graphiti_memory_test.py
|
||||
|
||||
# Or run specific tests:
|
||||
python integrations/graphiti/run_graphiti_memory_test.py --test connection
|
||||
python integrations/graphiti/run_graphiti_memory_test.py --test save
|
||||
python integrations/graphiti/run_graphiti_memory_test.py --test search
|
||||
python integrations/graphiti/run_graphiti_memory_test.py --test ollama
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
# Load .env file
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
|
||||
env_file = Path(__file__).parent.parent.parent.parent / ".env"
|
||||
if env_file.exists():
|
||||
load_dotenv(env_file)
|
||||
print(f"Loaded .env from {env_file}")
|
||||
except ImportError:
|
||||
print("Note: python-dotenv not installed, using environment variables only")
|
||||
|
||||
|
||||
def apply_ladybug_monkeypatch():
|
||||
"""Apply LadybugDB monkeypatch for embedded database support."""
|
||||
try:
|
||||
import real_ladybug
|
||||
|
||||
sys.modules["kuzu"] = real_ladybug
|
||||
return True
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Try native kuzu as fallback
|
||||
try:
|
||||
import kuzu # noqa: F401
|
||||
|
||||
return True
|
||||
except ImportError:
|
||||
return False
|
||||
|
||||
|
||||
def print_header(title: str):
|
||||
"""Print a section header."""
|
||||
print("\n" + "=" * 60)
|
||||
print(f" {title}")
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
|
||||
def print_result(label: str, value: str, success: bool = True):
|
||||
"""Print a result line."""
|
||||
status = "✅" if success else "❌"
|
||||
print(f" {status} {label}: {value}")
|
||||
|
||||
|
||||
def print_info(message: str):
|
||||
"""Print an info line."""
|
||||
print(f" ℹ️ {message}")
|
||||
|
||||
|
||||
async def test_ladybugdb_connection(db_path: str, database: str) -> bool:
|
||||
"""Test basic LadybugDB connection."""
|
||||
print_header("1. Testing LadybugDB Connection")
|
||||
|
||||
print(f" Database path: {db_path}")
|
||||
print(f" Database name: {database}")
|
||||
print()
|
||||
|
||||
if not apply_ladybug_monkeypatch():
|
||||
print_result("LadybugDB", "Not installed (pip install real-ladybug)", False)
|
||||
return False
|
||||
|
||||
print_result("LadybugDB", "Installed", True)
|
||||
|
||||
try:
|
||||
import kuzu # This is real_ladybug via monkeypatch
|
||||
|
||||
# Ensure parent directory exists (database will create its own structure)
|
||||
full_path = Path(db_path) / database
|
||||
full_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Create database and connection
|
||||
db = kuzu.Database(str(full_path))
|
||||
conn = kuzu.Connection(db)
|
||||
|
||||
# Test basic query
|
||||
result = conn.execute("RETURN 1 + 1 as test")
|
||||
df = result.get_as_df()
|
||||
test_value = df["test"].iloc[0] if len(df) > 0 else None
|
||||
|
||||
if test_value == 2:
|
||||
print_result("Connection", "SUCCESS - Database responds correctly", True)
|
||||
return True
|
||||
else:
|
||||
print_result("Connection", f"Unexpected result: {test_value}", False)
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print_result("Connection", f"FAILED: {e}", False)
|
||||
return False
|
||||
|
||||
|
||||
async def test_save_episode(db_path: str, database: str) -> tuple[str, str]:
|
||||
"""Test saving an episode to the graph."""
|
||||
print_header("2. Testing Episode Save")
|
||||
|
||||
try:
|
||||
from integrations.graphiti.config import GraphitiConfig
|
||||
from integrations.graphiti.queries_pkg.client import GraphitiClient
|
||||
|
||||
# Create config
|
||||
config = GraphitiConfig.from_env()
|
||||
config.db_path = db_path
|
||||
config.database = database
|
||||
config.enabled = True
|
||||
|
||||
print(f" Embedder provider: {config.embedder_provider}")
|
||||
print()
|
||||
|
||||
# Initialize client
|
||||
client = GraphitiClient(config)
|
||||
initialized = await client.initialize()
|
||||
|
||||
if not initialized:
|
||||
print_result("Client Init", "Failed to initialize", False)
|
||||
return None, None
|
||||
|
||||
print_result("Client Init", "SUCCESS", True)
|
||||
|
||||
# Create test episode data
|
||||
test_data = {
|
||||
"type": "test_episode",
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"test_field": "Hello from LadybugDB test!",
|
||||
"test_number": 42,
|
||||
"embedder": config.embedder_provider,
|
||||
}
|
||||
|
||||
episode_name = (
|
||||
f"test_episode_{datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S')}"
|
||||
)
|
||||
group_id = "ladybug_test_group"
|
||||
|
||||
print(f" Episode name: {episode_name}")
|
||||
print(f" Group ID: {group_id}")
|
||||
print(f" Data: {json.dumps(test_data, indent=4)}")
|
||||
print()
|
||||
|
||||
# Save using Graphiti
|
||||
from graphiti_core.nodes import EpisodeType
|
||||
|
||||
print(" Saving episode...")
|
||||
await client.graphiti.add_episode(
|
||||
name=episode_name,
|
||||
episode_body=json.dumps(test_data),
|
||||
source=EpisodeType.text,
|
||||
source_description="Test episode from run_graphiti_memory_test.py",
|
||||
reference_time=datetime.now(timezone.utc),
|
||||
group_id=group_id,
|
||||
)
|
||||
|
||||
print_result("Episode Save", "SUCCESS", True)
|
||||
|
||||
await client.close()
|
||||
return episode_name, group_id
|
||||
|
||||
except ImportError as e:
|
||||
print_result("Import", f"Missing dependency: {e}", False)
|
||||
return None, None
|
||||
except Exception as e:
|
||||
print_result("Episode Save", f"FAILED: {e}", False)
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return None, None
|
||||
|
||||
|
||||
async def test_keyword_search(db_path: str, database: str) -> bool:
|
||||
"""Test keyword search (works without embeddings)."""
|
||||
print_header("3. Testing Keyword Search")
|
||||
|
||||
if not apply_ladybug_monkeypatch():
|
||||
print_result("LadybugDB", "Not installed", False)
|
||||
return False
|
||||
|
||||
try:
|
||||
import kuzu
|
||||
|
||||
full_path = Path(db_path) / database
|
||||
if not full_path.exists():
|
||||
print_info("Database doesn't exist yet - run save test first")
|
||||
return True
|
||||
|
||||
db = kuzu.Database(str(full_path))
|
||||
conn = kuzu.Connection(db)
|
||||
|
||||
# Search for test episodes
|
||||
search_query = "test"
|
||||
print(f" Search query: '{search_query}'")
|
||||
print()
|
||||
|
||||
query = f"""
|
||||
MATCH (e:Episodic)
|
||||
WHERE toLower(e.name) CONTAINS '{search_query}'
|
||||
OR toLower(e.content) CONTAINS '{search_query}'
|
||||
RETURN e.name as name, e.content as content
|
||||
LIMIT 5
|
||||
"""
|
||||
|
||||
try:
|
||||
result = conn.execute(query)
|
||||
df = result.get_as_df()
|
||||
|
||||
print(f" Found {len(df)} results:")
|
||||
for _, row in df.iterrows():
|
||||
name = row.get("name", "unknown")[:50]
|
||||
content = str(row.get("content", ""))[:60]
|
||||
print(f" - {name}: {content}...")
|
||||
|
||||
print_result("Keyword Search", f"Found {len(df)} results", True)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
if "Episodic" in str(e) and "not exist" in str(e).lower():
|
||||
print_info("Episodic table doesn't exist yet - run save test first")
|
||||
return True
|
||||
raise
|
||||
|
||||
except Exception as e:
|
||||
print_result("Keyword Search", f"FAILED: {e}", False)
|
||||
return False
|
||||
|
||||
|
||||
async def test_semantic_search(db_path: str, database: str, group_id: str) -> bool:
|
||||
"""Test semantic search using embeddings."""
|
||||
print_header("4. Testing Semantic Search")
|
||||
|
||||
if not group_id:
|
||||
print_info("Skipping - no group_id from save test")
|
||||
return True
|
||||
|
||||
try:
|
||||
from integrations.graphiti.config import GraphitiConfig
|
||||
from integrations.graphiti.queries_pkg.client import GraphitiClient
|
||||
|
||||
# Create config
|
||||
config = GraphitiConfig.from_env()
|
||||
config.db_path = db_path
|
||||
config.database = database
|
||||
config.enabled = True
|
||||
|
||||
if not config.embedder_provider:
|
||||
print_info("No embedder configured - semantic search requires embeddings")
|
||||
return True
|
||||
|
||||
print(f" Embedder: {config.embedder_provider}")
|
||||
print()
|
||||
|
||||
# Initialize client
|
||||
client = GraphitiClient(config)
|
||||
initialized = await client.initialize()
|
||||
|
||||
if not initialized:
|
||||
print_result("Client Init", "Failed", False)
|
||||
return False
|
||||
|
||||
# Search
|
||||
query = "test episode hello LadybugDB"
|
||||
print(f" Query: '{query}'")
|
||||
print(f" Group ID: {group_id}")
|
||||
print()
|
||||
|
||||
print(" Searching...")
|
||||
results = await client.graphiti.search(
|
||||
query=query,
|
||||
group_ids=[group_id],
|
||||
num_results=10,
|
||||
)
|
||||
|
||||
print(f" Found {len(results)} results:")
|
||||
for i, result in enumerate(results[:5]):
|
||||
# Print available attributes
|
||||
if hasattr(result, "fact") and result.fact:
|
||||
print(f" {i + 1}. [fact] {str(result.fact)[:80]}...")
|
||||
elif hasattr(result, "content") and result.content:
|
||||
print(f" {i + 1}. [content] {str(result.content)[:80]}...")
|
||||
elif hasattr(result, "name"):
|
||||
print(f" {i + 1}. [name] {str(result.name)[:80]}...")
|
||||
|
||||
await client.close()
|
||||
|
||||
if results:
|
||||
print_result(
|
||||
"Semantic Search", f"SUCCESS - Found {len(results)} results", True
|
||||
)
|
||||
else:
|
||||
print_result(
|
||||
"Semantic Search", "No results (may need time for embedding)", False
|
||||
)
|
||||
|
||||
return len(results) > 0
|
||||
|
||||
except Exception as e:
|
||||
print_result("Semantic Search", f"FAILED: {e}", False)
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
async def test_ollama_embeddings() -> bool:
|
||||
"""Test Ollama embedding generation directly."""
|
||||
print_header("5. Testing Ollama Embeddings")
|
||||
|
||||
ollama_model = os.environ.get("OLLAMA_EMBEDDING_MODEL", "embeddinggemma")
|
||||
ollama_base_url = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434")
|
||||
|
||||
print(f" Model: {ollama_model}")
|
||||
print(f" Base URL: {ollama_base_url}")
|
||||
print()
|
||||
|
||||
try:
|
||||
import requests
|
||||
|
||||
# Check Ollama status
|
||||
print(" Checking Ollama status...")
|
||||
try:
|
||||
resp = requests.get(f"{ollama_base_url}/api/tags", timeout=5)
|
||||
if resp.status_code != 200:
|
||||
print_result(
|
||||
"Ollama", f"Not responding (status {resp.status_code})", False
|
||||
)
|
||||
return False
|
||||
|
||||
models = [m["name"] for m in resp.json().get("models", [])]
|
||||
embedding_models = [
|
||||
m for m in models if "embed" in m.lower() or "gemma" in m.lower()
|
||||
]
|
||||
print_result("Ollama", f"Running with {len(models)} models", True)
|
||||
print(f" Embedding models: {embedding_models}")
|
||||
|
||||
except requests.exceptions.ConnectionError:
|
||||
print_result("Ollama", "Not running - start with 'ollama serve'", False)
|
||||
return False
|
||||
|
||||
# Test embedding generation
|
||||
print()
|
||||
print(" Generating test embedding...")
|
||||
|
||||
test_text = (
|
||||
"This is a test embedding for Auto Claude memory system using LadybugDB."
|
||||
)
|
||||
|
||||
resp = requests.post(
|
||||
f"{ollama_base_url}/api/embeddings",
|
||||
json={"model": ollama_model, "prompt": test_text},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
if resp.status_code == 200:
|
||||
data = resp.json()
|
||||
embedding = data.get("embedding", [])
|
||||
print_result("Embedding", f"SUCCESS - {len(embedding)} dimensions", True)
|
||||
print(f" First 5 values: {embedding[:5]}")
|
||||
|
||||
# Verify dimension matches config
|
||||
expected_dim = int(os.environ.get("OLLAMA_EMBEDDING_DIM", 768))
|
||||
if len(embedding) == expected_dim:
|
||||
print_result("Dimension", f"Matches expected ({expected_dim})", True)
|
||||
else:
|
||||
print_result(
|
||||
"Dimension",
|
||||
f"Mismatch! Got {len(embedding)}, expected {expected_dim}",
|
||||
False,
|
||||
)
|
||||
print_info(
|
||||
f"Update OLLAMA_EMBEDDING_DIM={len(embedding)} in your config"
|
||||
)
|
||||
|
||||
return True
|
||||
else:
|
||||
print_result(
|
||||
"Embedding", f"FAILED: {resp.status_code} - {resp.text}", False
|
||||
)
|
||||
return False
|
||||
|
||||
except ImportError:
|
||||
print_result("requests", "Not installed (pip install requests)", False)
|
||||
return False
|
||||
except Exception as e:
|
||||
print_result("Ollama Embeddings", f"FAILED: {e}", False)
|
||||
return False
|
||||
|
||||
|
||||
async def test_graphiti_memory_class(db_path: str, database: str) -> bool:
|
||||
"""Test the GraphitiMemory wrapper class."""
|
||||
print_header("6. Testing GraphitiMemory Class")
|
||||
|
||||
try:
|
||||
from integrations.graphiti.memory import GraphitiMemory
|
||||
|
||||
# Create temporary directories for testing
|
||||
test_spec_dir = Path(tempfile.mkdtemp(prefix="graphiti_test_spec_"))
|
||||
test_project_dir = Path(tempfile.mkdtemp(prefix="graphiti_test_project_"))
|
||||
|
||||
print(f" Spec dir: {test_spec_dir}")
|
||||
print(f" Project dir: {test_project_dir}")
|
||||
print()
|
||||
|
||||
# Override database path via environment
|
||||
os.environ["GRAPHITI_DB_PATH"] = db_path
|
||||
os.environ["GRAPHITI_DATABASE"] = database
|
||||
|
||||
# Create memory instance
|
||||
memory = GraphitiMemory(test_spec_dir, test_project_dir)
|
||||
|
||||
print(f" Is enabled: {memory.is_enabled}")
|
||||
print(f" Group ID: {memory.group_id}")
|
||||
print()
|
||||
|
||||
if not memory.is_enabled:
|
||||
print_info("GraphitiMemory not enabled - check GRAPHITI_ENABLED=true")
|
||||
return True
|
||||
|
||||
# Initialize
|
||||
print(" Initializing...")
|
||||
init_result = await memory.initialize()
|
||||
|
||||
if not init_result:
|
||||
print_result("Initialize", "Failed", False)
|
||||
return False
|
||||
|
||||
print_result("Initialize", "SUCCESS", True)
|
||||
|
||||
# Test save_session_insights
|
||||
print()
|
||||
print(" Testing save_session_insights...")
|
||||
insights = {
|
||||
"subtasks_completed": ["test-subtask-1"],
|
||||
"discoveries": {
|
||||
"files_understood": {"test.py": "Test file"},
|
||||
"patterns_found": ["Pattern: LadybugDB works!"],
|
||||
"gotchas_encountered": [],
|
||||
},
|
||||
"what_worked": ["Using embedded database"],
|
||||
"what_failed": [],
|
||||
"recommendations_for_next_session": ["Continue testing"],
|
||||
}
|
||||
|
||||
save_result = await memory.save_session_insights(
|
||||
session_num=1, insights=insights
|
||||
)
|
||||
print_result(
|
||||
"save_session_insights", "SUCCESS" if save_result else "FAILED", save_result
|
||||
)
|
||||
|
||||
# Test save_pattern
|
||||
print()
|
||||
print(" Testing save_pattern...")
|
||||
pattern_result = await memory.save_pattern(
|
||||
"LadybugDB pattern: Embedded graph database works without Docker"
|
||||
)
|
||||
print_result(
|
||||
"save_pattern", "SUCCESS" if pattern_result else "FAILED", pattern_result
|
||||
)
|
||||
|
||||
# Test get_relevant_context
|
||||
print()
|
||||
print(" Testing get_relevant_context...")
|
||||
await asyncio.sleep(1) # Brief wait for processing
|
||||
|
||||
context = await memory.get_relevant_context("LadybugDB embedded database")
|
||||
print(f" Found {len(context)} context items")
|
||||
|
||||
for item in context[:3]:
|
||||
item_type = item.get("type", "unknown")
|
||||
content = str(item.get("content", ""))[:60]
|
||||
print(f" - [{item_type}] {content}...")
|
||||
|
||||
print_result("get_relevant_context", f"Found {len(context)} items", True)
|
||||
|
||||
# Get status
|
||||
print()
|
||||
print(" Status summary:")
|
||||
status = memory.get_status_summary()
|
||||
for key, value in status.items():
|
||||
print(f" {key}: {value}")
|
||||
|
||||
await memory.close()
|
||||
print_result("GraphitiMemory", "All tests passed", True)
|
||||
return True
|
||||
|
||||
except ImportError as e:
|
||||
print_result("Import", f"Missing: {e}", False)
|
||||
return False
|
||||
except Exception as e:
|
||||
print_result("GraphitiMemory", f"FAILED: {e}", False)
|
||||
import traceback
|
||||
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
async def test_database_contents(db_path: str, database: str) -> bool:
|
||||
"""Show what's in the database (debug)."""
|
||||
print_header("7. Database Contents (Debug)")
|
||||
|
||||
if not apply_ladybug_monkeypatch():
|
||||
print_result("LadybugDB", "Not installed", False)
|
||||
return False
|
||||
|
||||
try:
|
||||
import kuzu
|
||||
|
||||
full_path = Path(db_path) / database
|
||||
if not full_path.exists():
|
||||
print_info(f"Database doesn't exist at {full_path}")
|
||||
return True
|
||||
|
||||
db = kuzu.Database(str(full_path))
|
||||
conn = kuzu.Connection(db)
|
||||
|
||||
# Get table info
|
||||
print(" Checking tables...")
|
||||
|
||||
tables_to_check = ["Episodic", "Entity", "Community"]
|
||||
|
||||
for table in tables_to_check:
|
||||
try:
|
||||
result = conn.execute(f"MATCH (n:{table}) RETURN count(n) as count")
|
||||
df = result.get_as_df()
|
||||
count = df["count"].iloc[0] if len(df) > 0 else 0
|
||||
print(f" {table}: {count} nodes")
|
||||
except Exception as e:
|
||||
if "not exist" in str(e).lower() or "cannot" in str(e).lower():
|
||||
print(f" {table}: (table not created yet)")
|
||||
else:
|
||||
print(f" {table}: Error - {e}")
|
||||
|
||||
# Show sample episodic nodes
|
||||
print()
|
||||
print(" Sample Episodic nodes:")
|
||||
try:
|
||||
result = conn.execute("""
|
||||
MATCH (e:Episodic)
|
||||
RETURN e.name as name, e.created_at as created
|
||||
ORDER BY e.created_at DESC
|
||||
LIMIT 5
|
||||
""")
|
||||
df = result.get_as_df()
|
||||
|
||||
if len(df) == 0:
|
||||
print(" (none)")
|
||||
else:
|
||||
for _, row in df.iterrows():
|
||||
print(f" - {row.get('name', 'unknown')}")
|
||||
except Exception as e:
|
||||
if "Episodic" in str(e):
|
||||
print(" (table not created yet)")
|
||||
else:
|
||||
print(f" Error: {e}")
|
||||
|
||||
print_result("Database Contents", "Displayed", True)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print_result("Database Contents", f"FAILED: {e}", False)
|
||||
return False
|
||||
|
||||
|
||||
async def main():
|
||||
"""Run all tests."""
|
||||
parser = argparse.ArgumentParser(description="Test Memory System with LadybugDB")
|
||||
parser.add_argument(
|
||||
"--test",
|
||||
choices=[
|
||||
"all",
|
||||
"connection",
|
||||
"save",
|
||||
"keyword",
|
||||
"semantic",
|
||||
"ollama",
|
||||
"memory",
|
||||
"contents",
|
||||
],
|
||||
default="all",
|
||||
help="Which test to run",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--db-path",
|
||||
default=os.path.expanduser("~/.auto-claude/memories"),
|
||||
help="Database path",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--database",
|
||||
default="test_memory",
|
||||
help="Database name (use 'test_memory' for testing)",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print(" MEMORY SYSTEM TEST SUITE (LadybugDB)")
|
||||
print("=" * 60)
|
||||
|
||||
# Configuration check
|
||||
print_header("0. Configuration Check")
|
||||
|
||||
print(f" Database path: {args.db_path}")
|
||||
print(f" Database name: {args.database}")
|
||||
print()
|
||||
|
||||
# Check environment
|
||||
graphiti_enabled = os.environ.get("GRAPHITI_ENABLED", "").lower() == "true"
|
||||
embedder_provider = os.environ.get("GRAPHITI_EMBEDDER_PROVIDER", "")
|
||||
|
||||
print_result("GRAPHITI_ENABLED", str(graphiti_enabled), graphiti_enabled)
|
||||
print_result(
|
||||
"GRAPHITI_EMBEDDER_PROVIDER",
|
||||
embedder_provider or "(not set)",
|
||||
bool(embedder_provider),
|
||||
)
|
||||
|
||||
if embedder_provider == "ollama":
|
||||
ollama_model = os.environ.get("OLLAMA_EMBEDDING_MODEL", "")
|
||||
ollama_dim = os.environ.get("OLLAMA_EMBEDDING_DIM", "")
|
||||
print_result(
|
||||
"OLLAMA_EMBEDDING_MODEL", ollama_model or "(not set)", bool(ollama_model)
|
||||
)
|
||||
print_result(
|
||||
"OLLAMA_EMBEDDING_DIM", ollama_dim or "(not set)", bool(ollama_dim)
|
||||
)
|
||||
elif embedder_provider == "openai":
|
||||
has_key = bool(os.environ.get("OPENAI_API_KEY"))
|
||||
print_result("OPENAI_API_KEY", "Set" if has_key else "Not set", has_key)
|
||||
|
||||
# Run tests based on selection
|
||||
test = args.test
|
||||
group_id = None
|
||||
|
||||
if test in ["all", "connection"]:
|
||||
await test_ladybugdb_connection(args.db_path, args.database)
|
||||
|
||||
if test in ["all", "ollama"]:
|
||||
await test_ollama_embeddings()
|
||||
|
||||
if test in ["all", "save"]:
|
||||
_, group_id = await test_save_episode(args.db_path, args.database)
|
||||
if group_id:
|
||||
print("\n Waiting 2 seconds for embedding processing...")
|
||||
await asyncio.sleep(2)
|
||||
|
||||
if test in ["all", "keyword"]:
|
||||
await test_keyword_search(args.db_path, args.database)
|
||||
|
||||
if test in ["all", "semantic"]:
|
||||
await test_semantic_search(
|
||||
args.db_path, args.database, group_id or "ladybug_test_group"
|
||||
)
|
||||
|
||||
if test in ["all", "memory"]:
|
||||
await test_graphiti_memory_class(args.db_path, args.database)
|
||||
|
||||
if test in ["all", "contents"]:
|
||||
await test_database_contents(args.db_path, args.database)
|
||||
|
||||
print_header("TEST SUMMARY")
|
||||
print(" Tests completed. Check the results above for any failures.")
|
||||
print()
|
||||
print(" Quick commands:")
|
||||
print(" # Run all tests:")
|
||||
print(" python integrations/graphiti/run_graphiti_memory_test.py")
|
||||
print()
|
||||
print(" # Test just Ollama embeddings:")
|
||||
print(" python integrations/graphiti/run_graphiti_memory_test.py --test ollama")
|
||||
print()
|
||||
print(" # Test with production database:")
|
||||
print(
|
||||
" python integrations/graphiti/run_graphiti_memory_test.py --database auto_claude_memory"
|
||||
)
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1,862 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Test Script for Ollama Embedding Memory Integration
|
||||
====================================================
|
||||
|
||||
This test validates that the memory system works correctly with local Ollama
|
||||
embedding models (like embeddinggemma, nomic-embed-text) for creating and
|
||||
retrieving memories in the hybrid RAG system.
|
||||
|
||||
The test covers:
|
||||
1. Ollama embedding generation (direct API test)
|
||||
2. Creating memories with Ollama embeddings via GraphitiMemory
|
||||
3. Retrieving memories via semantic search
|
||||
4. Verifying the full create → store → retrieve cycle
|
||||
|
||||
Prerequisites:
|
||||
1. Install Ollama: https://ollama.ai/
|
||||
2. Pull an embedding model:
|
||||
ollama pull embeddinggemma # 768 dimensions (lightweight)
|
||||
ollama pull nomic-embed-text # 768 dimensions (good quality)
|
||||
3. Pull an LLM model (for knowledge graph construction):
|
||||
ollama pull deepseek-r1:7b # or llama3.2:3b, mistral:7b
|
||||
4. Start Ollama server: ollama serve
|
||||
5. Configure environment:
|
||||
export GRAPHITI_ENABLED=true
|
||||
export GRAPHITI_LLM_PROVIDER=ollama
|
||||
export GRAPHITI_EMBEDDER_PROVIDER=ollama
|
||||
export OLLAMA_LLM_MODEL=deepseek-r1:7b
|
||||
export OLLAMA_EMBEDDING_MODEL=embeddinggemma
|
||||
export OLLAMA_EMBEDDING_DIM=768
|
||||
|
||||
NOTE: graphiti-core internally uses an OpenAI reranker for search ranking.
|
||||
For full offline operation, set a dummy key: export OPENAI_API_KEY=dummy
|
||||
The reranker will fail at search time, but embedding creation works.
|
||||
For production, use OpenAI API key for best search quality.
|
||||
|
||||
Usage:
|
||||
cd apps/backend
|
||||
python integrations/graphiti/run_ollama_embedding_test.py
|
||||
|
||||
# Run specific tests:
|
||||
python integrations/graphiti/run_ollama_embedding_test.py --test embeddings
|
||||
python integrations/graphiti/run_ollama_embedding_test.py --test create
|
||||
python integrations/graphiti/run_ollama_embedding_test.py --test retrieve
|
||||
python integrations/graphiti/run_ollama_embedding_test.py --test full-cycle
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
# Add backend to path
|
||||
backend_dir = Path(__file__).parent.parent.parent.parent
|
||||
sys.path.insert(0, str(backend_dir))
|
||||
|
||||
# Load .env file
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
|
||||
env_file = backend_dir / ".env"
|
||||
if env_file.exists():
|
||||
load_dotenv(env_file)
|
||||
print(f"Loaded .env from {env_file}")
|
||||
except ImportError:
|
||||
print("Note: python-dotenv not installed, using environment variables only")
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Helper Functions
|
||||
# ============================================================================
|
||||
|
||||
|
||||
def print_header(title: str):
|
||||
"""Print a section header."""
|
||||
print("\n" + "=" * 70)
|
||||
print(f" {title}")
|
||||
print("=" * 70 + "\n")
|
||||
|
||||
|
||||
def print_result(label: str, value: str, success: bool = True):
|
||||
"""Print a result line."""
|
||||
status = "PASS" if success else "FAIL"
|
||||
print(f" [{status}] {label}: {value}")
|
||||
|
||||
|
||||
def print_info(message: str):
|
||||
"""Print an info line."""
|
||||
print(f" INFO: {message}")
|
||||
|
||||
|
||||
def print_step(step: int, message: str):
|
||||
"""Print a step indicator."""
|
||||
print(f"\n Step {step}: {message}")
|
||||
|
||||
|
||||
def apply_ladybug_monkeypatch():
|
||||
"""Apply LadybugDB monkeypatch for embedded database support."""
|
||||
try:
|
||||
import real_ladybug
|
||||
|
||||
sys.modules["kuzu"] = real_ladybug
|
||||
return True
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
# Try native kuzu as fallback
|
||||
try:
|
||||
import kuzu # noqa: F401
|
||||
|
||||
return True
|
||||
except ImportError:
|
||||
return False
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 1: Ollama Embedding Generation
|
||||
# ============================================================================
|
||||
|
||||
|
||||
async def test_ollama_embeddings() -> bool:
|
||||
"""
|
||||
Test Ollama embedding generation directly via API.
|
||||
|
||||
This validates that Ollama is running and can generate embeddings
|
||||
with the configured model.
|
||||
"""
|
||||
print_header("Test 1: Ollama Embedding Generation")
|
||||
|
||||
ollama_model = os.environ.get("OLLAMA_EMBEDDING_MODEL", "embeddinggemma")
|
||||
ollama_base_url = os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434")
|
||||
expected_dim = int(os.environ.get("OLLAMA_EMBEDDING_DIM", "768"))
|
||||
|
||||
print(f" Ollama Model: {ollama_model}")
|
||||
print(f" Base URL: {ollama_base_url}")
|
||||
print(f" Expected Dimension: {expected_dim}")
|
||||
print()
|
||||
|
||||
try:
|
||||
import requests
|
||||
except ImportError:
|
||||
print_result("requests library", "Not installed - pip install requests", False)
|
||||
return False
|
||||
|
||||
# Step 1: Check Ollama is running
|
||||
print_step(1, "Checking Ollama server status")
|
||||
try:
|
||||
resp = requests.get(f"{ollama_base_url}/api/tags", timeout=10)
|
||||
if resp.status_code != 200:
|
||||
print_result(
|
||||
"Ollama server",
|
||||
f"Not responding (status {resp.status_code})",
|
||||
False,
|
||||
)
|
||||
return False
|
||||
|
||||
models = resp.json().get("models", [])
|
||||
model_names = [m.get("name", "") for m in models]
|
||||
print_result("Ollama server", f"Running with {len(models)} models", True)
|
||||
|
||||
# Check if embedding model is available
|
||||
embedding_model_found = any(
|
||||
ollama_model in name or ollama_model.split(":")[0] in name
|
||||
for name in model_names
|
||||
)
|
||||
if not embedding_model_found:
|
||||
print_info(f"Model '{ollama_model}' not found. Available: {model_names}")
|
||||
print_info(f"Pull it with: ollama pull {ollama_model}")
|
||||
|
||||
except requests.exceptions.ConnectionError:
|
||||
print_result(
|
||||
"Ollama server",
|
||||
"Not running - start with 'ollama serve'",
|
||||
False,
|
||||
)
|
||||
return False
|
||||
|
||||
# Step 2: Generate test embedding
|
||||
print_step(2, "Generating test embeddings")
|
||||
|
||||
test_texts = [
|
||||
"This is a test memory about implementing OAuth authentication.",
|
||||
"The user prefers using TypeScript for frontend development.",
|
||||
"A gotcha discovered: always validate JWT tokens on the server side.",
|
||||
]
|
||||
|
||||
embeddings = []
|
||||
for i, text in enumerate(test_texts):
|
||||
resp = requests.post(
|
||||
f"{ollama_base_url}/api/embeddings",
|
||||
json={"model": ollama_model, "prompt": text},
|
||||
timeout=60,
|
||||
)
|
||||
|
||||
if resp.status_code != 200:
|
||||
print_result(
|
||||
f"Embedding {i + 1}",
|
||||
f"Failed: {resp.status_code} - {resp.text[:100]}",
|
||||
False,
|
||||
)
|
||||
return False
|
||||
|
||||
data = resp.json()
|
||||
embedding = data.get("embedding", [])
|
||||
embeddings.append(embedding)
|
||||
|
||||
print_result(
|
||||
f"Embedding {i + 1}",
|
||||
f"Generated {len(embedding)} dimensions",
|
||||
True,
|
||||
)
|
||||
|
||||
# Step 3: Validate embedding dimensions
|
||||
print_step(3, "Validating embedding dimensions")
|
||||
|
||||
for i, embedding in enumerate(embeddings):
|
||||
if len(embedding) != expected_dim:
|
||||
print_result(
|
||||
f"Embedding {i + 1} dimension",
|
||||
f"Mismatch! Got {len(embedding)}, expected {expected_dim}",
|
||||
False,
|
||||
)
|
||||
print_info(f"Update OLLAMA_EMBEDDING_DIM={len(embedding)} in your config")
|
||||
return False
|
||||
print_result(
|
||||
f"Embedding {i + 1} dimension", f"{len(embedding)} matches expected", True
|
||||
)
|
||||
|
||||
# Step 4: Test embedding similarity (basic sanity check)
|
||||
print_step(4, "Testing embedding similarity")
|
||||
|
||||
def cosine_similarity(a, b):
|
||||
"""Calculate cosine similarity between two vectors."""
|
||||
dot_product = sum(x * y for x, y in zip(a, b))
|
||||
norm_a = sum(x * x for x in a) ** 0.5
|
||||
norm_b = sum(x * x for x in b) ** 0.5
|
||||
return dot_product / (norm_a * norm_b) if norm_a and norm_b else 0
|
||||
|
||||
# Generate embedding for a similar query
|
||||
query = "OAuth authentication implementation"
|
||||
resp = requests.post(
|
||||
f"{ollama_base_url}/api/embeddings",
|
||||
json={"model": ollama_model, "prompt": query},
|
||||
timeout=60,
|
||||
)
|
||||
query_embedding = resp.json().get("embedding", [])
|
||||
|
||||
similarities = [cosine_similarity(query_embedding, emb) for emb in embeddings]
|
||||
|
||||
print(f" Query: '{query}'")
|
||||
print(" Similarities to test texts:")
|
||||
for i, (text, sim) in enumerate(zip(test_texts, similarities)):
|
||||
print(f" {i + 1}. {sim:.4f} - '{text[:50]}...'")
|
||||
|
||||
# First text (about OAuth) should have highest similarity to OAuth query
|
||||
if similarities[0] > similarities[1] and similarities[0] > similarities[2]:
|
||||
print_result("Semantic similarity", "OAuth query matches OAuth text best", True)
|
||||
else:
|
||||
print_info("Similarity ordering may vary - embeddings are still working")
|
||||
|
||||
print()
|
||||
print_result("Ollama Embeddings", "All tests passed", True)
|
||||
return True
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 2: Memory Creation with Ollama
|
||||
# ============================================================================
|
||||
|
||||
|
||||
async def test_memory_creation(test_db_path: Path) -> tuple[Path, Path, bool]:
|
||||
"""
|
||||
Test creating memories using GraphitiMemory with Ollama embeddings.
|
||||
|
||||
Returns:
|
||||
Tuple of (spec_dir, project_dir, success)
|
||||
"""
|
||||
print_header("Test 2: Memory Creation with Ollama Embeddings")
|
||||
|
||||
# Create test directories
|
||||
spec_dir = test_db_path / "test_spec"
|
||||
project_dir = test_db_path / "test_project"
|
||||
spec_dir.mkdir(parents=True, exist_ok=True)
|
||||
project_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
print(f" Spec dir: {spec_dir}")
|
||||
print(f" Project dir: {project_dir}")
|
||||
print(f" Database path: {test_db_path}")
|
||||
print()
|
||||
|
||||
# Override database path for testing
|
||||
os.environ["GRAPHITI_DB_PATH"] = str(test_db_path / "graphiti_db")
|
||||
os.environ["GRAPHITI_DATABASE"] = "test_ollama_memory"
|
||||
|
||||
try:
|
||||
from integrations.graphiti.memory import GraphitiMemory
|
||||
except ImportError as e:
|
||||
print_result("Import GraphitiMemory", f"Failed: {e}", False)
|
||||
return spec_dir, project_dir, False
|
||||
|
||||
# Step 1: Initialize GraphitiMemory
|
||||
print_step(1, "Initializing GraphitiMemory")
|
||||
|
||||
memory = GraphitiMemory(spec_dir, project_dir)
|
||||
print(f" Is enabled: {memory.is_enabled}")
|
||||
print(f" Group ID: {memory.group_id}")
|
||||
|
||||
if not memory.is_enabled:
|
||||
print_result(
|
||||
"GraphitiMemory",
|
||||
"Not enabled - check GRAPHITI_ENABLED=true",
|
||||
False,
|
||||
)
|
||||
return spec_dir, project_dir, False
|
||||
|
||||
init_result = await memory.initialize()
|
||||
if not init_result:
|
||||
print_result("Initialize", "Failed to initialize", False)
|
||||
return spec_dir, project_dir, False
|
||||
|
||||
print_result("Initialize", "SUCCESS", True)
|
||||
|
||||
# Step 2: Save session insights
|
||||
print_step(2, "Saving session insights")
|
||||
|
||||
session_insights = {
|
||||
"subtasks_completed": ["implement-oauth-login", "add-jwt-validation"],
|
||||
"discoveries": {
|
||||
"files_understood": {
|
||||
"auth/oauth.py": "OAuth 2.0 flow implementation with Google/GitHub",
|
||||
"auth/jwt.py": "JWT token generation and validation utilities",
|
||||
},
|
||||
"patterns_found": [
|
||||
"Pattern: Use refresh tokens for long-lived sessions",
|
||||
"Pattern: Store tokens in httpOnly cookies for security",
|
||||
],
|
||||
"gotchas_encountered": [
|
||||
"Gotcha: Always validate JWT signature on server side",
|
||||
"Gotcha: OAuth state parameter prevents CSRF attacks",
|
||||
],
|
||||
},
|
||||
"what_worked": [
|
||||
"Using PyJWT for token handling",
|
||||
"Separating OAuth providers into individual modules",
|
||||
],
|
||||
"what_failed": [],
|
||||
"recommendations_for_next_session": [
|
||||
"Consider adding refresh token rotation",
|
||||
"Add rate limiting to auth endpoints",
|
||||
],
|
||||
}
|
||||
|
||||
save_result = await memory.save_session_insights(
|
||||
session_num=1, insights=session_insights
|
||||
)
|
||||
print_result(
|
||||
"save_session_insights", "SUCCESS" if save_result else "FAILED", save_result
|
||||
)
|
||||
|
||||
# Step 3: Save patterns
|
||||
print_step(3, "Saving code patterns")
|
||||
|
||||
patterns = [
|
||||
"OAuth implementation uses authorization code flow for web apps",
|
||||
"JWT tokens include user ID, roles, and expiration in payload",
|
||||
"Token refresh happens automatically when access token expires",
|
||||
]
|
||||
|
||||
for i, pattern in enumerate(patterns):
|
||||
result = await memory.save_pattern(pattern)
|
||||
print_result(f"save_pattern {i + 1}", "SUCCESS" if result else "FAILED", result)
|
||||
|
||||
# Step 4: Save gotchas
|
||||
print_step(4, "Saving gotchas (pitfalls)")
|
||||
|
||||
gotchas = [
|
||||
"Never store config values in frontend code or files checked into git",
|
||||
"API redirect URIs must exactly match the registered URIs",
|
||||
"Cache expiration times should be short for performance (15 min default)",
|
||||
]
|
||||
|
||||
for i, gotcha in enumerate(gotchas):
|
||||
result = await memory.save_gotcha(gotcha)
|
||||
print_result(f"save_gotcha {i + 1}", "SUCCESS" if result else "FAILED", result)
|
||||
|
||||
# Step 5: Save codebase discoveries
|
||||
print_step(5, "Saving codebase discoveries")
|
||||
|
||||
discoveries = {
|
||||
"api/routes/users.py": "User management API endpoints (list, create, update)",
|
||||
"middleware/logging.py": "Request logging middleware for all routes",
|
||||
"models/user.py": "User model with profile data and role management",
|
||||
"services/notifications.py": "Notification service integrations (email, SMS, push)",
|
||||
}
|
||||
|
||||
discovery_result = await memory.save_codebase_discoveries(discoveries)
|
||||
print_result(
|
||||
"save_codebase_discoveries",
|
||||
"SUCCESS" if discovery_result else "FAILED",
|
||||
discovery_result,
|
||||
)
|
||||
|
||||
# Brief wait for embedding processing
|
||||
print()
|
||||
print_info("Waiting 3 seconds for embedding processing...")
|
||||
await asyncio.sleep(3)
|
||||
|
||||
await memory.close()
|
||||
|
||||
print()
|
||||
print_result("Memory Creation", "All memories saved successfully", True)
|
||||
return spec_dir, project_dir, True
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 3: Memory Retrieval with Semantic Search
|
||||
# ============================================================================
|
||||
|
||||
|
||||
async def test_memory_retrieval(spec_dir: Path, project_dir: Path) -> bool:
|
||||
"""
|
||||
Test retrieving memories using semantic search with Ollama embeddings.
|
||||
|
||||
This validates that saved memories can be found via semantic similarity.
|
||||
"""
|
||||
print_header("Test 3: Memory Retrieval with Semantic Search")
|
||||
|
||||
try:
|
||||
from integrations.graphiti.memory import GraphitiMemory
|
||||
except ImportError as e:
|
||||
print_result("Import GraphitiMemory", f"Failed: {e}", False)
|
||||
return False
|
||||
|
||||
# Step 1: Initialize memory (reconnect)
|
||||
print_step(1, "Reconnecting to GraphitiMemory")
|
||||
|
||||
memory = GraphitiMemory(spec_dir, project_dir)
|
||||
init_result = await memory.initialize()
|
||||
|
||||
if not init_result:
|
||||
print_result("Initialize", "Failed to reconnect", False)
|
||||
return False
|
||||
|
||||
print_result("Initialize", "Reconnected successfully", True)
|
||||
|
||||
# Step 2: Semantic search for API-related content
|
||||
print_step(2, "Searching for API-related memories")
|
||||
|
||||
api_query = "How do the API endpoints work in this project?"
|
||||
results = await memory.get_relevant_context(api_query, num_results=5)
|
||||
|
||||
print(f" Query: '{api_query}'")
|
||||
print(f" Found {len(results)} results:")
|
||||
|
||||
api_found = False
|
||||
for i, result in enumerate(results):
|
||||
content = result.get("content", "")[:100]
|
||||
result_type = result.get("type", "unknown")
|
||||
score = result.get("score", 0)
|
||||
print(f" {i + 1}. [{result_type}] (score: {score:.4f}) {content}...")
|
||||
if "api" in content.lower() or "routes" in content.lower():
|
||||
api_found = True
|
||||
|
||||
if api_found:
|
||||
print_result("API search", "Found API-related content", True)
|
||||
else:
|
||||
print_info("API content may not be in top results - checking other queries")
|
||||
|
||||
# Step 3: Search for middleware-related content
|
||||
print_step(3, "Searching for middleware patterns")
|
||||
|
||||
middleware_query = "middleware and request handling best practices"
|
||||
results = await memory.get_relevant_context(middleware_query, num_results=5)
|
||||
|
||||
print(f" Query: '{middleware_query}'")
|
||||
print(f" Found {len(results)} results:")
|
||||
|
||||
middleware_found = False
|
||||
for i, result in enumerate(results):
|
||||
content = result.get("content", "")[:100]
|
||||
result_type = result.get("type", "unknown")
|
||||
score = result.get("score", 0)
|
||||
print(f" {i + 1}. [{result_type}] (score: {score:.4f}) {content}...")
|
||||
if "middleware" in content.lower() or "routes" in content.lower():
|
||||
middleware_found = True
|
||||
|
||||
print_result(
|
||||
"Middleware search",
|
||||
"Found middleware-related content" if middleware_found else "No direct matches",
|
||||
middleware_found or len(results) > 0,
|
||||
)
|
||||
|
||||
# Step 4: Get session history
|
||||
print_step(4, "Retrieving session history")
|
||||
|
||||
history = await memory.get_session_history(limit=3)
|
||||
print(f" Found {len(history)} session records:")
|
||||
|
||||
for i, session in enumerate(history):
|
||||
session_num = session.get("session_number", "?")
|
||||
subtasks = session.get("subtasks_completed", [])
|
||||
print(f" Session {session_num}: {len(subtasks)} subtasks completed")
|
||||
for subtask in subtasks[:3]:
|
||||
print(f" - {subtask}")
|
||||
|
||||
print_result(
|
||||
"Session history", f"Retrieved {len(history)} sessions", len(history) > 0
|
||||
)
|
||||
|
||||
# Step 5: Get status summary
|
||||
print_step(5, "Memory status summary")
|
||||
|
||||
status = memory.get_status_summary()
|
||||
for key, value in status.items():
|
||||
print(f" {key}: {value}")
|
||||
|
||||
await memory.close()
|
||||
|
||||
print()
|
||||
all_passed = len(results) > 0 and len(history) > 0
|
||||
print_result(
|
||||
"Memory Retrieval",
|
||||
"All retrieval tests passed" if all_passed else "Some tests had issues",
|
||||
all_passed,
|
||||
)
|
||||
return all_passed
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Test 4: Full Create → Store → Retrieve Cycle
|
||||
# ============================================================================
|
||||
|
||||
|
||||
async def test_full_cycle(test_db_path: Path) -> bool:
|
||||
"""
|
||||
Test the complete memory lifecycle:
|
||||
1. Create unique test data
|
||||
2. Store in graph database with Ollama embeddings
|
||||
3. Search and retrieve via semantic similarity
|
||||
4. Verify retrieved data matches what was stored
|
||||
"""
|
||||
print_header("Test 4: Full Create-Store-Retrieve Cycle")
|
||||
|
||||
# Create fresh test directories
|
||||
spec_dir = test_db_path / "cycle_test_spec"
|
||||
project_dir = test_db_path / "cycle_test_project"
|
||||
spec_dir.mkdir(parents=True, exist_ok=True)
|
||||
project_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Override database path for testing
|
||||
os.environ["GRAPHITI_DB_PATH"] = str(test_db_path / "graphiti_db")
|
||||
os.environ["GRAPHITI_DATABASE"] = "test_full_cycle"
|
||||
|
||||
try:
|
||||
from integrations.graphiti.memory import GraphitiMemory
|
||||
except ImportError as e:
|
||||
print_result("Import", f"Failed: {e}", False)
|
||||
return False
|
||||
|
||||
# Step 1: Create unique test content
|
||||
print_step(1, "Creating unique test content")
|
||||
|
||||
unique_id = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
unique_pattern = (
|
||||
f"Unique pattern {unique_id}: Use dependency injection for database connections"
|
||||
)
|
||||
unique_gotcha = f"Unique gotcha {unique_id}: Always close database connections in finally blocks"
|
||||
|
||||
print(f" Unique ID: {unique_id}")
|
||||
print(f" Pattern: {unique_pattern[:60]}...")
|
||||
print(f" Gotcha: {unique_gotcha[:60]}...")
|
||||
|
||||
# Step 2: Store the content
|
||||
print_step(2, "Storing content in memory system")
|
||||
|
||||
memory = GraphitiMemory(spec_dir, project_dir)
|
||||
init_result = await memory.initialize()
|
||||
|
||||
if not init_result:
|
||||
print_result("Initialize", "Failed", False)
|
||||
return False
|
||||
|
||||
print_result("Initialize", "SUCCESS", True)
|
||||
|
||||
pattern_result = await memory.save_pattern(unique_pattern)
|
||||
print_result(
|
||||
"save_pattern", "SUCCESS" if pattern_result else "FAILED", pattern_result
|
||||
)
|
||||
|
||||
gotcha_result = await memory.save_gotcha(unique_gotcha)
|
||||
print_result("save_gotcha", "SUCCESS" if gotcha_result else "FAILED", gotcha_result)
|
||||
|
||||
# Wait for embedding processing
|
||||
print()
|
||||
print_info("Waiting 4 seconds for embedding processing and indexing...")
|
||||
await asyncio.sleep(4)
|
||||
|
||||
# Step 3: Search for the unique content
|
||||
print_step(3, "Searching for unique content")
|
||||
|
||||
# Search for the pattern
|
||||
pattern_query = "dependency injection database connections"
|
||||
pattern_results = await memory.get_relevant_context(pattern_query, num_results=5)
|
||||
|
||||
print(f" Query: '{pattern_query}'")
|
||||
print(f" Found {len(pattern_results)} results")
|
||||
|
||||
pattern_found = False
|
||||
for result in pattern_results:
|
||||
content = result.get("content", "")
|
||||
if unique_id in content:
|
||||
pattern_found = True
|
||||
print(f" MATCH: {content[:80]}...")
|
||||
|
||||
print_result(
|
||||
"Pattern retrieval",
|
||||
f"Found unique pattern (ID: {unique_id})"
|
||||
if pattern_found
|
||||
else "Unique pattern not in top results",
|
||||
pattern_found,
|
||||
)
|
||||
|
||||
# Search for the gotcha
|
||||
gotcha_query = "database connection cleanup finally block"
|
||||
gotcha_results = await memory.get_relevant_context(gotcha_query, num_results=5)
|
||||
|
||||
print(f" Query: '{gotcha_query}'")
|
||||
print(f" Found {len(gotcha_results)} results")
|
||||
|
||||
gotcha_found = False
|
||||
for result in gotcha_results:
|
||||
content = result.get("content", "")
|
||||
if unique_id in content:
|
||||
gotcha_found = True
|
||||
print(f" MATCH: {content[:80]}...")
|
||||
|
||||
print_result(
|
||||
"Gotcha retrieval",
|
||||
f"Found unique gotcha (ID: {unique_id})"
|
||||
if gotcha_found
|
||||
else "Unique gotcha not in top results",
|
||||
gotcha_found,
|
||||
)
|
||||
|
||||
# Step 4: Verify semantic similarity works
|
||||
print_step(4, "Verifying semantic similarity")
|
||||
|
||||
# Search with semantically similar but different wording
|
||||
alt_query = "closing connections properly in error handling"
|
||||
alt_results = await memory.get_relevant_context(alt_query, num_results=3)
|
||||
|
||||
print(f" Alternative query: '{alt_query}'")
|
||||
print(f" Found {len(alt_results)} semantically similar results:")
|
||||
|
||||
for i, result in enumerate(alt_results):
|
||||
content = result.get("content", "")[:80]
|
||||
score = result.get("score", 0)
|
||||
print(f" {i + 1}. (score: {score:.4f}) {content}...")
|
||||
|
||||
semantic_works = len(alt_results) > 0
|
||||
print_result(
|
||||
"Semantic similarity",
|
||||
"Working - found related content" if semantic_works else "No results",
|
||||
semantic_works,
|
||||
)
|
||||
|
||||
await memory.close()
|
||||
|
||||
# Summary
|
||||
print()
|
||||
cycle_passed = (
|
||||
pattern_result
|
||||
and gotcha_result
|
||||
and (pattern_found or gotcha_found or len(alt_results) > 0)
|
||||
)
|
||||
print_result(
|
||||
"Full Cycle Test",
|
||||
"Create-Store-Retrieve cycle verified"
|
||||
if cycle_passed
|
||||
else "Some steps had issues",
|
||||
cycle_passed,
|
||||
)
|
||||
|
||||
return cycle_passed
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Main Entry Point
|
||||
# ============================================================================
|
||||
|
||||
|
||||
async def main():
|
||||
"""Run Ollama embedding memory tests."""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Test Ollama Embedding Memory Integration"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--test",
|
||||
choices=["all", "embeddings", "create", "retrieve", "full-cycle"],
|
||||
default="all",
|
||||
help="Which test to run",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--keep-db",
|
||||
action="store_true",
|
||||
help="Keep test database after completion (default: cleanup)",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
print("\n" + "=" * 70)
|
||||
print(" OLLAMA EMBEDDING MEMORY TEST SUITE")
|
||||
print("=" * 70)
|
||||
|
||||
# Configuration check
|
||||
print_header("Configuration Check")
|
||||
|
||||
config_items = {
|
||||
"GRAPHITI_ENABLED": os.environ.get("GRAPHITI_ENABLED", ""),
|
||||
"GRAPHITI_LLM_PROVIDER": os.environ.get("GRAPHITI_LLM_PROVIDER", ""),
|
||||
"GRAPHITI_EMBEDDER_PROVIDER": os.environ.get("GRAPHITI_EMBEDDER_PROVIDER", ""),
|
||||
"OLLAMA_LLM_MODEL": os.environ.get("OLLAMA_LLM_MODEL", ""),
|
||||
"OLLAMA_EMBEDDING_MODEL": os.environ.get("OLLAMA_EMBEDDING_MODEL", ""),
|
||||
"OLLAMA_EMBEDDING_DIM": os.environ.get("OLLAMA_EMBEDDING_DIM", ""),
|
||||
"OLLAMA_BASE_URL": os.environ.get("OLLAMA_BASE_URL", "http://localhost:11434"),
|
||||
"OPENAI_API_KEY": "(set)"
|
||||
if os.environ.get("OPENAI_API_KEY")
|
||||
else "(not set - needed for reranker)",
|
||||
}
|
||||
|
||||
all_configured = True
|
||||
required_keys = [
|
||||
"GRAPHITI_ENABLED",
|
||||
"GRAPHITI_LLM_PROVIDER",
|
||||
"GRAPHITI_EMBEDDER_PROVIDER",
|
||||
"OLLAMA_LLM_MODEL",
|
||||
"OLLAMA_EMBEDDING_MODEL",
|
||||
]
|
||||
|
||||
for key, value in config_items.items():
|
||||
is_optional = key in [
|
||||
"OLLAMA_BASE_URL",
|
||||
"OPENAI_API_KEY",
|
||||
"OLLAMA_EMBEDDING_DIM",
|
||||
]
|
||||
is_set = bool(value) if not is_optional else True
|
||||
display_value = value or "(not set)"
|
||||
if key == "OPENAI_API_KEY":
|
||||
display_value = value # Already formatted above
|
||||
is_set = True # Optional for testing
|
||||
print_result(key, display_value, is_set)
|
||||
if key in required_keys and not bool(os.environ.get(key)):
|
||||
all_configured = False
|
||||
|
||||
if not all_configured:
|
||||
print()
|
||||
print(" Missing required configuration. Please set:")
|
||||
print(" export GRAPHITI_ENABLED=true")
|
||||
print(" export GRAPHITI_LLM_PROVIDER=ollama")
|
||||
print(" export GRAPHITI_EMBEDDER_PROVIDER=ollama")
|
||||
print(" export OLLAMA_LLM_MODEL=deepseek-r1:7b")
|
||||
print(" export OLLAMA_EMBEDDING_MODEL=embeddinggemma")
|
||||
print(" export OLLAMA_EMBEDDING_DIM=768")
|
||||
print(" export OPENAI_API_KEY=dummy # For graphiti-core reranker")
|
||||
print()
|
||||
return
|
||||
|
||||
# Check LadybugDB
|
||||
if not apply_ladybug_monkeypatch():
|
||||
print()
|
||||
print_result("LadybugDB", "Not installed - pip install real-ladybug", False)
|
||||
return
|
||||
|
||||
print_result("LadybugDB", "Installed", True)
|
||||
|
||||
# Create temp directory for test database
|
||||
test_db_path = Path(tempfile.mkdtemp(prefix="ollama_memory_test_"))
|
||||
print()
|
||||
print_info(f"Test database: {test_db_path}")
|
||||
|
||||
# Run tests
|
||||
test = args.test
|
||||
results = {}
|
||||
|
||||
try:
|
||||
if test in ["all", "embeddings"]:
|
||||
results["embeddings"] = await test_ollama_embeddings()
|
||||
|
||||
spec_dir = None
|
||||
project_dir = None
|
||||
|
||||
if test in ["all", "create"]:
|
||||
spec_dir, project_dir, results["create"] = await test_memory_creation(
|
||||
test_db_path
|
||||
)
|
||||
|
||||
if test in ["all", "retrieve"]:
|
||||
if spec_dir and project_dir:
|
||||
results["retrieve"] = await test_memory_retrieval(spec_dir, project_dir)
|
||||
else:
|
||||
print_info(
|
||||
"Skipping retrieve test - no spec/project dir from create test"
|
||||
)
|
||||
|
||||
if test in ["all", "full-cycle"]:
|
||||
results["full-cycle"] = await test_full_cycle(test_db_path)
|
||||
|
||||
finally:
|
||||
# Cleanup unless --keep-db specified
|
||||
if not args.keep_db and test_db_path.exists():
|
||||
print()
|
||||
print_info(f"Cleaning up test database: {test_db_path}")
|
||||
shutil.rmtree(test_db_path, ignore_errors=True)
|
||||
|
||||
# Summary
|
||||
print_header("TEST SUMMARY")
|
||||
|
||||
all_passed = True
|
||||
for test_name, passed in results.items():
|
||||
status = "PASSED" if passed else "FAILED"
|
||||
print(f" {test_name}: {status}")
|
||||
if not passed:
|
||||
all_passed = False
|
||||
|
||||
print()
|
||||
if all_passed:
|
||||
print(" All tests PASSED!")
|
||||
print()
|
||||
print(" The memory system is working correctly with Ollama embeddings.")
|
||||
print(" Memories can be created and retrieved using semantic search.")
|
||||
else:
|
||||
print(" Some tests FAILED. Check the output above for details.")
|
||||
print()
|
||||
print(" Common issues:")
|
||||
print(" - Ollama not running: ollama serve")
|
||||
print(" - Model not pulled: ollama pull embeddinggemma")
|
||||
print(" - Wrong dimension: Update OLLAMA_EMBEDDING_DIM to match model")
|
||||
|
||||
print()
|
||||
print(" Commands:")
|
||||
print(" # Run all tests:")
|
||||
print(" python integrations/graphiti/run_ollama_embedding_test.py")
|
||||
print()
|
||||
print(" # Run specific test:")
|
||||
print(
|
||||
" python integrations/graphiti/run_ollama_embedding_test.py --test embeddings"
|
||||
)
|
||||
print(
|
||||
" python integrations/graphiti/run_ollama_embedding_test.py --test full-cycle"
|
||||
)
|
||||
print()
|
||||
print(" # Keep database for inspection:")
|
||||
print(" python integrations/graphiti/run_ollama_embedding_test.py --keep-db")
|
||||
print()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
@@ -0,0 +1 @@
|
||||
"""Tests for Graphiti memory integration."""
|
||||
@@ -0,0 +1,610 @@
|
||||
"""
|
||||
Pytest configuration and fixtures for graphiti integration tests.
|
||||
|
||||
This module provides shared fixtures for testing the memory system integration,
|
||||
including mocks for external dependencies, test configurations, and client fixtures.
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, Mock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# Add the backend directory to sys.path to allow imports
|
||||
backend_dir = Path(__file__).parent.parent.parent.parent
|
||||
sys.path.insert(0, str(backend_dir))
|
||||
|
||||
|
||||
def pytest_collection_modifyitems(config, items):
|
||||
"""
|
||||
Exclude validator functions from test collection.
|
||||
|
||||
The validators.py module contains functions named test_llm_connection and
|
||||
test_embedder_connection which are not pytest tests but validator functions.
|
||||
"""
|
||||
# Filter out items that are from validators.py and are not in test classes
|
||||
filtered_items = []
|
||||
for item in items:
|
||||
# Get the full path of the test
|
||||
item_path = str(item.fspath) if hasattr(item, "fspath") else str(item.path)
|
||||
|
||||
# Skip the standalone test_llm_connection and test_embedder_connection
|
||||
# functions from validators.py (they're not pytest tests)
|
||||
if item.name in [
|
||||
"test_llm_connection",
|
||||
"test_embedder_connection",
|
||||
"test_ollama_connection",
|
||||
]:
|
||||
# Check if it's from validators.py
|
||||
if "validators.py" in item_path or "test_providers.py" in item_path:
|
||||
# Only skip if it's a standalone function (not in a TestClass)
|
||||
if not item.parent.name.startswith("Test"):
|
||||
continue
|
||||
|
||||
filtered_items.append(item)
|
||||
|
||||
items[:] = filtered_items
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# External Dependency Mocks
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_graphiti_core():
|
||||
"""Mock graphiti_core.Graphiti and related classes.
|
||||
|
||||
Patches the graphiti_core library to prevent actual graph database connections
|
||||
during tests.
|
||||
|
||||
Yields:
|
||||
tuple: (mock_graphiti_class, mock_graphiti_instance)
|
||||
"""
|
||||
with patch(
|
||||
"integrations.graphiti.queries_pkg.graphiti.graphiti_core.Graphiti"
|
||||
) as mock_graphiti:
|
||||
# Configure the mock to return a mock instance
|
||||
mock_instance = MagicMock()
|
||||
mock_graphiti.return_value = mock_instance
|
||||
|
||||
# Mock common methods that might be called
|
||||
mock_instance.add_edges = AsyncMock()
|
||||
mock_instance.add_nodes = AsyncMock()
|
||||
mock_instance.search = AsyncMock(return_value=[])
|
||||
mock_instance.delete_graph = AsyncMock()
|
||||
mock_instance.close = AsyncMock()
|
||||
|
||||
yield mock_graphiti, mock_instance
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_falkor_driver():
|
||||
"""Mock graphiti_core.driver.falkordb_driver.FalkorDriver.
|
||||
|
||||
Prevents actual FalkorDB connections during tests.
|
||||
|
||||
Yields:
|
||||
tuple: (mock_driver_class, mock_driver_instance)
|
||||
"""
|
||||
with patch(
|
||||
"integrations.graphiti.queries_pkg.graphiti.graphiti_core.driver.falkordb_driver.FalkorDriver"
|
||||
) as mock_driver:
|
||||
mock_instance = MagicMock()
|
||||
mock_driver.return_value = mock_instance
|
||||
|
||||
# Mock driver methods
|
||||
mock_instance.close = MagicMock()
|
||||
mock_instance.execute_query = MagicMock(return_value=[])
|
||||
|
||||
yield mock_driver, mock_instance
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_graphiti_providers():
|
||||
"""Mock graphiti_providers module.
|
||||
|
||||
Patches the graphiti_providers module to prevent actual LLM/embedder calls.
|
||||
|
||||
Yields:
|
||||
tuple: (mock_get_client, mock_client_instance)
|
||||
"""
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.providers.get_client"
|
||||
) as mock_get_client:
|
||||
mock_client = MagicMock()
|
||||
mock_get_client.return_value = mock_client
|
||||
yield mock_get_client, mock_client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_ladybug_db():
|
||||
"""Mock real_ladybug and kuzu database connections.
|
||||
|
||||
Prevents actual database connections during tests.
|
||||
|
||||
Yields:
|
||||
dict: Dictionary with 'ladybug' and 'kuzu' keys, each containing
|
||||
(mock_class, mock_instance) tuples.
|
||||
"""
|
||||
with (
|
||||
patch(
|
||||
"integrations.graphiti.queries_pkg.client.real_ladybug.Ladybug"
|
||||
) as mock_ladybug,
|
||||
patch("integrations.graphiti.queries_pkg.client.kuzu.Connection") as mock_kuzu,
|
||||
):
|
||||
# Mock Ladybug instance
|
||||
ladybug_instance = MagicMock()
|
||||
mock_ladybug.return_value = ladybug_instance
|
||||
ladybug_instance.close = MagicMock()
|
||||
|
||||
# Mock Kuzu connection
|
||||
kuzu_instance = MagicMock()
|
||||
mock_kuzu.return_value = kuzu_instance
|
||||
kuzu_instance.close = MagicMock()
|
||||
|
||||
yield {
|
||||
"ladybug": (mock_ladybug, ladybug_instance),
|
||||
"kuzu": (mock_kuzu, kuzu_instance),
|
||||
}
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Config Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config():
|
||||
"""Return a GraphitiConfig with test values.
|
||||
|
||||
Provides a test configuration that doesn't require real environment variables
|
||||
or database connections.
|
||||
|
||||
Returns:
|
||||
GraphitiConfig: Configuration with test values.
|
||||
"""
|
||||
from integrations.graphiti.config import GraphitiConfig
|
||||
|
||||
config = GraphitiConfig(
|
||||
enabled=True,
|
||||
database="test_dataset",
|
||||
db_path="/tmp/test_graphiti.db",
|
||||
llm_provider="openai",
|
||||
openai_model="gpt-5-mini",
|
||||
embedder_provider="openai",
|
||||
openai_embedding_model="text-embedding-3-small",
|
||||
openai_api_key="sk-test-key-for-testing",
|
||||
)
|
||||
|
||||
return config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_env_vars(tmp_path):
|
||||
"""Set test environment variables for Graphiti configuration.
|
||||
|
||||
Sets up a clean environment with test values for all Graphiti-related
|
||||
environment variables.
|
||||
|
||||
Yields:
|
||||
dict: Dictionary of environment variables that were set.
|
||||
"""
|
||||
test_db_path = str(tmp_path / "test_graphiti.db")
|
||||
|
||||
env_vars = {
|
||||
"GRAPHITI_ENABLED": "true",
|
||||
"GRAPHITI_LLM_PROVIDER": "openai",
|
||||
"GRAPHITI_EMBEDDER_PROVIDER": "openai",
|
||||
"GRAPHITI_DATABASE": "test_dataset",
|
||||
"GRAPHITI_DB_PATH": test_db_path,
|
||||
"OPENAI_MODEL": "gpt-5-mini",
|
||||
"OPENAI_EMBEDDING_MODEL": "text-embedding-3-small",
|
||||
"OPENAI_API_KEY": "sk-test-key-for-testing",
|
||||
}
|
||||
|
||||
# Save original values
|
||||
original = {k: os.environ.get(k) for k in env_vars}
|
||||
|
||||
# Set test values
|
||||
for key, value in env_vars.items():
|
||||
os.environ[key] = value
|
||||
|
||||
yield env_vars
|
||||
|
||||
# Restore original values
|
||||
for key, original_value in original.items():
|
||||
if original_value is None:
|
||||
os.environ.pop(key, None)
|
||||
else:
|
||||
os.environ[key] = original_value
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Client Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_graphiti_client():
|
||||
"""Mock GraphitiClient with all necessary methods.
|
||||
|
||||
Provides a mock client that simulates the behavior of the GraphitiClient
|
||||
without requiring actual graph database connections.
|
||||
|
||||
Returns:
|
||||
Mock: Mocked GraphitiClient with typical methods mocked.
|
||||
"""
|
||||
client = Mock()
|
||||
client.graphiti = Mock()
|
||||
|
||||
# Core client methods
|
||||
client.is_initialized = Mock(return_value=True)
|
||||
client.initialize = AsyncMock()
|
||||
client.get_session_id = Mock(return_value="test_session")
|
||||
client.get_user_id = Mock(return_value="test_user")
|
||||
client.get_project_id = Mock(return_value="test_project")
|
||||
|
||||
# Memory operations (async)
|
||||
client.add_episode = AsyncMock(return_value="episode_id_123")
|
||||
client.add_episodic_memories = AsyncMock(return_value=["mem_id_1", "mem_id_2"])
|
||||
client.add_abstract_memories = AsyncMock(return_value=["abstract_id_1"])
|
||||
client.search = AsyncMock(return_value=[])
|
||||
client.delete_graph = AsyncMock()
|
||||
|
||||
# Graphiti instance methods
|
||||
client.graphiti.search = AsyncMock(return_value=[])
|
||||
|
||||
# Configuration
|
||||
client.get_config = Mock(
|
||||
return_value=Mock(
|
||||
enabled=True, database="test_dataset", db_path="/tmp/test_graphiti.db"
|
||||
)
|
||||
)
|
||||
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_graphiti_instance():
|
||||
"""Mock the Graphiti instance from graphiti_core.
|
||||
|
||||
Provides a mock of the actual Graphiti core instance with all methods
|
||||
that might be called during operations.
|
||||
|
||||
Returns:
|
||||
Mock: Mocked Graphiti instance with typical methods mocked.
|
||||
"""
|
||||
instance = MagicMock()
|
||||
|
||||
# Search methods (async)
|
||||
instance.search = AsyncMock(return_value=[])
|
||||
instance.search_by_abstract = AsyncMock(return_value=[])
|
||||
instance.search_by_vector = AsyncMock(return_value=[])
|
||||
|
||||
# Add methods (async)
|
||||
instance.add_episode = AsyncMock(return_value="episode_id")
|
||||
instance.add_edges = AsyncMock()
|
||||
instance.add_nodes = AsyncMock()
|
||||
|
||||
# Graph management
|
||||
instance.delete_graph = AsyncMock()
|
||||
instance.close = AsyncMock()
|
||||
instance.get_graph_summary = Mock(return_value={"nodes": 0, "edges": 0})
|
||||
|
||||
# Configuration
|
||||
instance.database = "test_dataset"
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test Directory Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_spec_dir(tmp_path):
|
||||
"""Create a temporary directory for spec testing.
|
||||
|
||||
Provides a temporary directory with spec-like structure for testing
|
||||
spec-related functionality.
|
||||
|
||||
Args:
|
||||
tmp_path: pytest's built-in tmp_path fixture.
|
||||
|
||||
Returns:
|
||||
Path: Path to the temporary spec directory.
|
||||
"""
|
||||
spec_dir = tmp_path / "spec_001_test"
|
||||
spec_dir.mkdir()
|
||||
|
||||
# Create common spec subdirectories
|
||||
(spec_dir / ".auto-claude").mkdir()
|
||||
(spec_dir / "context").mkdir()
|
||||
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_project_dir(tmp_path):
|
||||
"""Create a temporary directory for project testing.
|
||||
|
||||
Provides a temporary directory with project-like structure for testing
|
||||
project-related functionality.
|
||||
|
||||
Args:
|
||||
tmp_path: pytest's built-in tmp_path fixture.
|
||||
|
||||
Returns:
|
||||
Path: Path to the temporary project directory.
|
||||
"""
|
||||
project_dir = tmp_path / "test_project"
|
||||
project_dir.mkdir()
|
||||
|
||||
# Create common project subdirectories
|
||||
(project_dir / "src").mkdir()
|
||||
(project_dir / "tests").mkdir()
|
||||
(project_dir / ".auto-claude").mkdir()
|
||||
|
||||
return project_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def temp_db_path(tmp_path):
|
||||
"""Create a temporary path for test database.
|
||||
|
||||
Provides a temporary file path that can be used for database testing
|
||||
without affecting real databases.
|
||||
|
||||
Args:
|
||||
tmp_path: pytest's built-in tmp_path fixture.
|
||||
|
||||
Returns:
|
||||
str: Path to temporary database file.
|
||||
"""
|
||||
db_path = str(tmp_path / "test_graphiti.db")
|
||||
return db_path
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Provider Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_llm_client():
|
||||
"""Mocked LLM client for testing.
|
||||
|
||||
Provides a mock client that simulates LLM responses without making
|
||||
actual API calls.
|
||||
|
||||
Returns:
|
||||
Mock: Mocked LLM client.
|
||||
"""
|
||||
client = Mock()
|
||||
|
||||
# Message methods
|
||||
client.messages = Mock()
|
||||
mock_response = Mock()
|
||||
mock_response.id = "msg_test_123"
|
||||
mock_response.content = []
|
||||
mock_response.model = "claude-3-5-sonnet-20241022"
|
||||
mock_response.role = "assistant"
|
||||
client.messages.create = Mock(return_value=mock_response)
|
||||
|
||||
# Streaming support
|
||||
client.messages.stream = Mock(return_value=iter([]))
|
||||
|
||||
# Token counting
|
||||
client.count_tokens = Mock(return_value=100)
|
||||
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_embedder():
|
||||
"""Mocked embedder with get_embedding() method.
|
||||
|
||||
Provides a mock embedder that returns fake embeddings without making
|
||||
actual API calls. Uses deterministic values for reproducibility.
|
||||
|
||||
Returns:
|
||||
tuple: (mock_embedder, test_embedding_list)
|
||||
"""
|
||||
embedder = Mock()
|
||||
|
||||
# Return a deterministic embedding vector (1536 dimensions is common for OpenAI)
|
||||
# Using 0.1 for all values makes tests reproducible
|
||||
test_embedding = [0.1] * 1536
|
||||
|
||||
embedder.get_embedding = Mock(return_value=test_embedding)
|
||||
embedder.get_embeddings = Mock(return_value=[test_embedding])
|
||||
|
||||
return embedder, test_embedding
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# State Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_state():
|
||||
"""GraphitiState with test values.
|
||||
|
||||
Provides a mock state object with typical values for testing state-related
|
||||
functionality.
|
||||
|
||||
Returns:
|
||||
Mock: Mocked GraphitiState with test values.
|
||||
"""
|
||||
from integrations.graphiti.config import GraphitiState
|
||||
|
||||
state = GraphitiState(
|
||||
initialized=True,
|
||||
database="test_dataset",
|
||||
indices_built=True,
|
||||
llm_provider="openai",
|
||||
embedder_provider="openai",
|
||||
)
|
||||
|
||||
return state
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_empty_state():
|
||||
"""Empty GraphitiState.
|
||||
|
||||
Provides a mock state object with default/uninitialized values for testing
|
||||
initialization logic.
|
||||
|
||||
Returns:
|
||||
Mock: Mocked GraphitiState with empty/default values.
|
||||
"""
|
||||
from integrations.graphiti.config import GraphitiState
|
||||
|
||||
state = GraphitiState()
|
||||
|
||||
return state
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test Data Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_episode_data():
|
||||
"""Sample episode data for testing.
|
||||
|
||||
Provides realistic episode data structure for testing memory operations.
|
||||
|
||||
Returns:
|
||||
dict: Sample episode data.
|
||||
"""
|
||||
return {
|
||||
"episode_id": "episode_123",
|
||||
"content": "Test episode content about a feature implementation",
|
||||
"metadata": {
|
||||
"task_id": "task_001",
|
||||
"timestamp": "2024-01-01T00:00:00Z",
|
||||
"type": "implementation",
|
||||
},
|
||||
"session_id": "test_session",
|
||||
"user_id": "test_user",
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_memory_nodes():
|
||||
"""Sample memory nodes for testing.
|
||||
|
||||
Provides realistic node data for testing graph operations.
|
||||
|
||||
Returns:
|
||||
list: List of sample memory node dictionaries.
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"uuid": "node_1",
|
||||
"name": "Feature Implementation",
|
||||
"label": "CONCEPT",
|
||||
"summary": "Implementation of new feature",
|
||||
"created_at": "2024-01-01T00:00:00Z",
|
||||
},
|
||||
{
|
||||
"uuid": "node_2",
|
||||
"name": "Bug Fix",
|
||||
"label": "CONCEPT",
|
||||
"summary": "Fixed critical bug",
|
||||
"created_at": "2024-01-02T00:00:00Z",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def sample_search_results():
|
||||
"""Sample search results for testing.
|
||||
|
||||
Provides realistic search result data for testing search operations.
|
||||
|
||||
Returns:
|
||||
list: List of sample search result dictionaries.
|
||||
"""
|
||||
return [
|
||||
{
|
||||
"uuid": "result_1",
|
||||
"name": "Search Result 1",
|
||||
"summary": "First search result",
|
||||
"score": 0.95,
|
||||
},
|
||||
{
|
||||
"uuid": "result_2",
|
||||
"name": "Search Result 2",
|
||||
"summary": "Second search result",
|
||||
"score": 0.87,
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Helper Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def clean_env():
|
||||
"""Fixture to ensure clean environment for each test.
|
||||
|
||||
Removes all Graphiti-related environment variables before the test
|
||||
and restores them afterward.
|
||||
|
||||
Yields:
|
||||
dict: Dictionary of original environment values.
|
||||
"""
|
||||
# Store original env vars
|
||||
env_keys = [
|
||||
"GRAPHITI_ENABLED",
|
||||
"GRAPHITI_LLM_PROVIDER",
|
||||
"GRAPHITI_EMBEDDER_PROVIDER",
|
||||
"GRAPHITI_DATABASE",
|
||||
"GRAPHITI_DB_PATH",
|
||||
"OPENAI_API_KEY",
|
||||
"OPENAI_MODEL",
|
||||
"OPENAI_EMBEDDING_MODEL",
|
||||
"ANTHROPIC_API_KEY",
|
||||
"GRAPHITI_ANTHROPIC_MODEL",
|
||||
"AZURE_OPENAI_API_KEY",
|
||||
"AZURE_OPENAI_BASE_URL",
|
||||
"AZURE_OPENAI_LLM_DEPLOYMENT",
|
||||
"AZURE_OPENAI_EMBEDDING_DEPLOYMENT",
|
||||
"VOYAGE_API_KEY",
|
||||
"VOYAGE_EMBEDDING_MODEL",
|
||||
"GOOGLE_API_KEY",
|
||||
"GOOGLE_LLM_MODEL",
|
||||
"GOOGLE_EMBEDDING_MODEL",
|
||||
"OPENROUTER_API_KEY",
|
||||
"OPENROUTER_BASE_URL",
|
||||
"OPENROUTER_LLM_MODEL",
|
||||
"OPENROUTER_EMBEDDING_MODEL",
|
||||
"OLLAMA_BASE_URL",
|
||||
"OLLAMA_LLM_MODEL",
|
||||
"OLLAMA_EMBEDDING_MODEL",
|
||||
"OLLAMA_EMBEDDING_DIM",
|
||||
]
|
||||
|
||||
original = {}
|
||||
for key in env_keys:
|
||||
original[key] = os.environ.get(key)
|
||||
if key in os.environ:
|
||||
os.environ.pop(key)
|
||||
|
||||
yield original
|
||||
|
||||
# Restore original values
|
||||
for key, value in original.items():
|
||||
if value is not None:
|
||||
os.environ[key] = value
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,216 @@
|
||||
"""
|
||||
Tests for integrations.graphiti.providers_pkg.cross_encoder module.
|
||||
|
||||
Tests cover:
|
||||
1. create_cross_encoder():
|
||||
- Returns None for non-Ollama providers
|
||||
- Returns None when llm_client is None
|
||||
- Returns None on ImportError (graphiti_core not available)
|
||||
- Returns None on Exception during creation
|
||||
- Creates correct base_url for Ollama
|
||||
- Creates LLMConfig with correct parameters
|
||||
"""
|
||||
|
||||
import builtins
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# =============================================================================
|
||||
# Test Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config():
|
||||
"""Mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.llm_provider = "ollama"
|
||||
config.ollama_base_url = "http://localhost:11434"
|
||||
config.ollama_llm_model = "llama3.2"
|
||||
return config
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_llm_client():
|
||||
"""Mock LLM client."""
|
||||
return MagicMock()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def graphiti_core_mocks():
|
||||
"""Mock graphiti_core modules and capture LLMConfig calls."""
|
||||
captured_config = {}
|
||||
|
||||
def capture_llm_config(**kwargs):
|
||||
captured_config.update(kwargs)
|
||||
return MagicMock()
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.cross_encoder": MagicMock(),
|
||||
"graphiti_core.cross_encoder.openai_reranker_client": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.cross_encoder.openai_reranker_client import (
|
||||
OpenAIRerankerClient,
|
||||
)
|
||||
from graphiti_core.llm_client.config import LLMConfig
|
||||
|
||||
LLMConfig.side_effect = capture_llm_config
|
||||
OpenAIRerankerClient.return_value = MagicMock()
|
||||
|
||||
yield captured_config
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test create_cross_encoder()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateCrossEncoder:
|
||||
"""Tests for create_cross_encoder() function."""
|
||||
|
||||
def test_returns_none_for_non_ollama_provider(self, mock_config, mock_llm_client):
|
||||
"""Test create_cross_encoder returns None for non-Ollama providers."""
|
||||
mock_config.llm_provider = "openai"
|
||||
|
||||
import integrations.graphiti.providers_pkg.cross_encoder as ce_module
|
||||
|
||||
# The function returns None for non-ollama providers
|
||||
result = ce_module.create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_returns_none_for_anthropic_provider(self, mock_config, mock_llm_client):
|
||||
"""Test create_cross_encoder returns None for Anthropic provider."""
|
||||
mock_config.llm_provider = "anthropic"
|
||||
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
result = create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_returns_none_for_google_provider(self, mock_config, mock_llm_client):
|
||||
"""Test create_cross_encoder returns None for Google provider."""
|
||||
mock_config.llm_provider = "google"
|
||||
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
result = create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_returns_none_when_llm_client_is_none(self, mock_config):
|
||||
"""Test create_cross_encoder returns None when llm_client is None."""
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
result = create_cross_encoder(mock_config, llm_client=None)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_base_url_without_v1_gets_suffix_added(
|
||||
self, mock_config, mock_llm_client, graphiti_core_mocks
|
||||
):
|
||||
"""Test that base_url without /v1 gets /v1 suffix added."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434"
|
||||
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
_ = create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
# Verify base_url was captured and has /v1 suffix added
|
||||
assert "base_url" in graphiti_core_mocks
|
||||
assert graphiti_core_mocks["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
def test_base_url_with_v1_is_preserved(
|
||||
self, mock_config, mock_llm_client, graphiti_core_mocks
|
||||
):
|
||||
"""Test that base_url with /v1 suffix is preserved."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434/v1"
|
||||
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
_ = create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
# Verify base_url was preserved with /v1 suffix
|
||||
assert "base_url" in graphiti_core_mocks
|
||||
assert graphiti_core_mocks["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
def test_import_error_returns_none(self, mock_config, mock_llm_client):
|
||||
"""Test create_cross_encoder returns None when graphiti_core modules not available."""
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
# Mock the import to raise ImportError
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name == "graphiti_core.cross_encoder.openai_reranker_client":
|
||||
raise ImportError("graphiti_core not installed")
|
||||
if name == "graphiti_core.llm_client.config":
|
||||
raise ImportError("graphiti_core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
result = create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_exception_during_creation_returns_none(self, mock_config, mock_llm_client):
|
||||
"""Test create_cross_encoder returns None on exception during creation."""
|
||||
from integrations.graphiti.providers_pkg.cross_encoder import (
|
||||
create_cross_encoder,
|
||||
)
|
||||
|
||||
# Mock the graphiti_core modules but make LLMConfig raise an exception
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.cross_encoder": MagicMock(),
|
||||
"graphiti_core.cross_encoder.openai_reranker_client": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.config import LLMConfig
|
||||
|
||||
# Make LLMConfig raise an exception
|
||||
LLMConfig.side_effect = Exception("Config creation failed")
|
||||
|
||||
result = create_cross_encoder(mock_config, mock_llm_client)
|
||||
|
||||
assert result is None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test module exports
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestModuleExports:
|
||||
"""Tests for cross_encoder module exports."""
|
||||
|
||||
def test_create_cross_encoder_is_exported(self):
|
||||
"""Test that create_cross_encoder is exported from module."""
|
||||
from integrations.graphiti.providers_pkg import cross_encoder
|
||||
|
||||
assert hasattr(cross_encoder, "create_cross_encoder")
|
||||
assert callable(cross_encoder.create_cross_encoder)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,238 @@
|
||||
"""
|
||||
Tests for integrations.graphiti.__init__ module.
|
||||
|
||||
Tests cover:
|
||||
- __getattr__ lazy import functionality
|
||||
- Direct imports (GraphitiConfig, validate_graphiti_config)
|
||||
- Invalid attribute access raises AttributeError
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestInitModuleDirectImports:
|
||||
"""Test direct imports that don't require lazy loading."""
|
||||
|
||||
def test_import_graphiti_config_directly(self):
|
||||
"""Test GraphitiConfig can be imported directly."""
|
||||
from integrations.graphiti import GraphitiConfig
|
||||
|
||||
assert GraphitiConfig is not None
|
||||
|
||||
def test_import_validate_graphiti_config_directly(self):
|
||||
"""Test validate_graphiti_config can be imported directly."""
|
||||
from integrations.graphiti import validate_graphiti_config
|
||||
|
||||
assert validate_graphiti_config is not None
|
||||
|
||||
def test___all___exports(self):
|
||||
"""Test __all__ contains expected exports."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
expected_all = [
|
||||
"GraphitiConfig",
|
||||
"validate_graphiti_config",
|
||||
"GraphitiMemory",
|
||||
"create_llm_client",
|
||||
"create_embedder",
|
||||
]
|
||||
assert graphiti_module.__all__ == expected_all
|
||||
|
||||
|
||||
class TestInitModuleLazyImports:
|
||||
"""Test __getattr__ lazy import functionality."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_memory_module(self):
|
||||
"""Mock the memory module."""
|
||||
memory_mock = MagicMock()
|
||||
memory_mock.GraphitiMemory = MagicMock
|
||||
return memory_mock
|
||||
|
||||
@pytest.fixture
|
||||
def mock_providers_module(self):
|
||||
"""Mock the providers module."""
|
||||
providers_mock = MagicMock()
|
||||
providers_mock.create_llm_client = MagicMock(return_value=AsyncMock())
|
||||
providers_mock.create_embedder = MagicMock(return_value=AsyncMock())
|
||||
return providers_mock
|
||||
|
||||
def test_getattr_graphiti_memory_lazy_import(self, mock_memory_module):
|
||||
"""Test accessing GraphitiMemory triggers lazy import."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"integrations.graphiti.memory": mock_memory_module,
|
||||
},
|
||||
):
|
||||
# Access the attribute via __getattr__
|
||||
result = graphiti_module.__getattr__("GraphitiMemory")
|
||||
|
||||
assert result == mock_memory_module.GraphitiMemory
|
||||
|
||||
def test_getattr_create_llm_client_lazy_import(self, mock_providers_module):
|
||||
"""Test accessing create_llm_client triggers lazy import."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"integrations.graphiti.providers": mock_providers_module,
|
||||
},
|
||||
):
|
||||
result = graphiti_module.__getattr__("create_llm_client")
|
||||
|
||||
assert result == mock_providers_module.create_llm_client
|
||||
|
||||
def test_getattr_create_embedder_lazy_import(self, mock_providers_module):
|
||||
"""Test accessing create_embedder triggers lazy import."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"integrations.graphiti.providers": mock_providers_module,
|
||||
},
|
||||
):
|
||||
result = graphiti_module.__getattr__("create_embedder")
|
||||
|
||||
assert result == mock_providers_module.create_embedder
|
||||
|
||||
def test_getattr_invalid_attribute_raises_attribute_error(self):
|
||||
"""Test accessing invalid attribute raises AttributeError."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
with pytest.raises(AttributeError) as exc_info:
|
||||
graphiti_module.__getattr__("NonExistentAttribute")
|
||||
|
||||
assert "has no attribute" in str(exc_info.value)
|
||||
assert "NonExistentAttribute" in str(exc_info.value)
|
||||
|
||||
def test_getattr_empty_string_attribute(self):
|
||||
"""Test accessing empty string attribute raises AttributeError."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
with pytest.raises(AttributeError):
|
||||
graphiti_module.__getattr__("")
|
||||
|
||||
def test_getattr_case_sensitive(self):
|
||||
"""Test that __getattr__ is case-sensitive."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
# lowercase should fail
|
||||
with pytest.raises(AttributeError):
|
||||
graphiti_module.__getattr__("graphitimemory")
|
||||
|
||||
# mixed case should fail
|
||||
with pytest.raises(AttributeError):
|
||||
graphiti_module.__getattr__("Graphiti_Memory")
|
||||
|
||||
|
||||
class TestInitModuleAccessPatterns:
|
||||
"""Test various access patterns for the init module."""
|
||||
|
||||
def test_hasattr_on_graphiti_memory(self):
|
||||
"""Test hasattr works correctly with lazy imports."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
# Mock the import
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"integrations.graphiti.memory": MagicMock(GraphitiMemory=MagicMock),
|
||||
},
|
||||
):
|
||||
# hasattr should call __getattr__ and not raise
|
||||
result = hasattr(graphiti_module, "GraphitiMemory")
|
||||
assert result is True
|
||||
|
||||
def test_hasattr_on_invalid_attribute(self):
|
||||
"""Test hasattr returns False for invalid attributes."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
result = hasattr(graphiti_module, "InvalidAttribute")
|
||||
assert result is False
|
||||
|
||||
def test_getattr_on_existing_direct_import(self):
|
||||
"""Test __getattr__ is not called for direct imports."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
# GraphitiConfig is imported directly, so __getattr__ shouldn't be called
|
||||
# This tests that the normal import mechanism works
|
||||
assert hasattr(graphiti_module, "GraphitiConfig")
|
||||
|
||||
def test_module_docstring(self):
|
||||
"""Test the module has a docstring."""
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
assert graphiti_module.__doc__ is not None
|
||||
assert "Graphiti" in graphiti_module.__doc__
|
||||
|
||||
|
||||
class TestInitModuleIntegration:
|
||||
"""Integration tests for the init module."""
|
||||
|
||||
def test_import_star(self):
|
||||
"""Test 'from integrations.graphiti import *' includes direct imports."""
|
||||
# Create a new namespace for the import
|
||||
namespace = {}
|
||||
exec("from integrations.graphiti import *", namespace)
|
||||
|
||||
# Direct imports should be available
|
||||
assert "GraphitiConfig" in namespace
|
||||
assert "validate_graphiti_config" in namespace
|
||||
|
||||
def test_reimport_does_not_fail(self):
|
||||
"""Test that re-importing the module doesn't cause issues."""
|
||||
import importlib
|
||||
|
||||
import integrations.graphiti
|
||||
|
||||
# Reload the module
|
||||
importlib.reload(integrations.graphiti)
|
||||
|
||||
# Should still work
|
||||
assert hasattr(integrations.graphiti, "GraphitiConfig")
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_concurrent_attribute_access(self):
|
||||
"""Test that concurrent attribute access doesn't cause issues."""
|
||||
import concurrent.futures
|
||||
|
||||
import integrations.graphiti as graphiti_module
|
||||
|
||||
# Mock the imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"integrations.graphiti.memory": MagicMock(GraphitiMemory=MagicMock),
|
||||
"integrations.graphiti.providers": MagicMock(
|
||||
create_llm_client=MagicMock(return_value=AsyncMock()),
|
||||
create_embedder=MagicMock(return_value=AsyncMock()),
|
||||
),
|
||||
},
|
||||
):
|
||||
|
||||
def access_attribute(attr_name):
|
||||
try:
|
||||
return getattr(graphiti_module, attr_name)
|
||||
except AttributeError:
|
||||
return None
|
||||
|
||||
# Access multiple attributes concurrently
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
|
||||
futures = [
|
||||
executor.submit(access_attribute, "GraphitiMemory"),
|
||||
executor.submit(access_attribute, "create_llm_client"),
|
||||
executor.submit(access_attribute, "create_embedder"),
|
||||
]
|
||||
results = [f.result() for f in concurrent.futures.as_completed(futures)]
|
||||
|
||||
# All should succeed
|
||||
assert len(results) == 3
|
||||
assert all(r is not None for r in results)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,425 @@
|
||||
"""
|
||||
Tests for integrations.graphiti.memory module.
|
||||
|
||||
This module is a backward compatibility facade that re-exports from
|
||||
queries_pkg and provides convenience functions.
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# =============================================================================
|
||||
# Test Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_spec_dir(tmp_path):
|
||||
"""Create a temporary spec directory."""
|
||||
spec_dir = tmp_path / "specs" / "001-test"
|
||||
spec_dir.mkdir(parents=True)
|
||||
return spec_dir
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_project_dir(tmp_path):
|
||||
"""Create a temporary project directory."""
|
||||
project_dir = tmp_path / "project"
|
||||
project_dir.mkdir(parents=True)
|
||||
return project_dir
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for module imports
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestModuleImports:
|
||||
"""Test that all expected exports are available."""
|
||||
|
||||
def test_import_GraphitiMemory(self):
|
||||
"""Test GraphitiMemory can be imported."""
|
||||
from integrations.graphiti.memory import GraphitiMemory
|
||||
|
||||
assert GraphitiMemory is not None
|
||||
|
||||
def test_import_GroupIdMode(self):
|
||||
"""Test GroupIdMode can be imported."""
|
||||
from integrations.graphiti.memory import GroupIdMode
|
||||
|
||||
assert GroupIdMode is not None
|
||||
assert hasattr(GroupIdMode, "SPEC")
|
||||
assert hasattr(GroupIdMode, "PROJECT")
|
||||
|
||||
def test_import_is_graphiti_enabled(self):
|
||||
"""Test is_graphiti_enabled can be imported."""
|
||||
from integrations.graphiti.memory import is_graphiti_enabled
|
||||
|
||||
assert is_graphiti_enabled is not None
|
||||
|
||||
def test_import_get_graphiti_memory(self):
|
||||
"""Test get_graphiti_memory can be imported."""
|
||||
from integrations.graphiti.memory import get_graphiti_memory
|
||||
|
||||
assert get_graphiti_memory is not None
|
||||
|
||||
def test_import_test_graphiti_connection(self):
|
||||
"""Test test_graphiti_connection can be imported."""
|
||||
from integrations.graphiti.memory import test_graphiti_connection
|
||||
|
||||
assert test_graphiti_connection is not None
|
||||
|
||||
def test_import_test_provider_configuration(self):
|
||||
"""Test test_provider_configuration can be imported."""
|
||||
from integrations.graphiti.memory import test_provider_configuration
|
||||
|
||||
assert test_provider_configuration is not None
|
||||
|
||||
def test_import_episode_types(self):
|
||||
"""Test all episode type constants can be imported."""
|
||||
from integrations.graphiti.memory import (
|
||||
EPISODE_TYPE_CODEBASE_DISCOVERY,
|
||||
EPISODE_TYPE_GOTCHA,
|
||||
EPISODE_TYPE_HISTORICAL_CONTEXT,
|
||||
EPISODE_TYPE_PATTERN,
|
||||
EPISODE_TYPE_QA_RESULT,
|
||||
EPISODE_TYPE_SESSION_INSIGHT,
|
||||
EPISODE_TYPE_TASK_OUTCOME,
|
||||
)
|
||||
|
||||
assert EPISODE_TYPE_SESSION_INSIGHT == "session_insight"
|
||||
assert EPISODE_TYPE_CODEBASE_DISCOVERY == "codebase_discovery"
|
||||
assert EPISODE_TYPE_PATTERN == "pattern"
|
||||
assert EPISODE_TYPE_GOTCHA == "gotcha"
|
||||
assert EPISODE_TYPE_TASK_OUTCOME == "task_outcome"
|
||||
assert EPISODE_TYPE_QA_RESULT == "qa_result"
|
||||
assert EPISODE_TYPE_HISTORICAL_CONTEXT == "historical_context"
|
||||
|
||||
def test_import_MAX_CONTEXT_RESULTS(self):
|
||||
"""Test MAX_CONTEXT_RESULTS can be imported."""
|
||||
from integrations.graphiti.memory import MAX_CONTEXT_RESULTS
|
||||
|
||||
assert MAX_CONTEXT_RESULTS is not None
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for get_graphiti_memory()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGetGraphitiMemory:
|
||||
"""Tests for get_graphiti_memory convenience function."""
|
||||
|
||||
def test_returns_graphiti_memory_instance(self, mock_spec_dir, mock_project_dir):
|
||||
"""Test get_graphiti_memory returns GraphitiMemory instance."""
|
||||
from integrations.graphiti.memory import get_graphiti_memory
|
||||
|
||||
memory = get_graphiti_memory(mock_spec_dir, mock_project_dir)
|
||||
|
||||
assert memory is not None
|
||||
assert hasattr(memory, "spec_dir")
|
||||
assert hasattr(memory, "project_dir")
|
||||
|
||||
def test_default_group_id_mode_is_project(self, mock_spec_dir, mock_project_dir):
|
||||
"""Test default group_id_mode is PROJECT."""
|
||||
from integrations.graphiti.memory import get_graphiti_memory
|
||||
from integrations.graphiti.queries_pkg.schema import GroupIdMode
|
||||
|
||||
memory = get_graphiti_memory(mock_spec_dir, mock_project_dir)
|
||||
|
||||
# Check that group_id_mode defaults to PROJECT
|
||||
assert memory.group_id_mode == GroupIdMode.PROJECT
|
||||
|
||||
def test_spec_group_id_mode(self, mock_spec_dir, mock_project_dir):
|
||||
"""Test SPEC group_id_mode can be set."""
|
||||
from integrations.graphiti.memory import get_graphiti_memory
|
||||
from integrations.graphiti.queries_pkg.schema import GroupIdMode
|
||||
|
||||
memory = get_graphiti_memory(mock_spec_dir, mock_project_dir, GroupIdMode.SPEC)
|
||||
|
||||
assert memory.group_id_mode == GroupIdMode.SPEC
|
||||
|
||||
def test_project_group_id_mode(self, mock_spec_dir, mock_project_dir):
|
||||
"""Test PROJECT group_id_mode can be set."""
|
||||
from integrations.graphiti.memory import get_graphiti_memory
|
||||
from integrations.graphiti.queries_pkg.schema import GroupIdMode
|
||||
|
||||
memory = get_graphiti_memory(
|
||||
mock_spec_dir, mock_project_dir, GroupIdMode.PROJECT
|
||||
)
|
||||
|
||||
assert memory.group_id_mode == GroupIdMode.PROJECT
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for test_graphiti_connection()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestTestGraphitiConnection:
|
||||
"""Tests for test_graphiti_connection function."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_false_when_not_enabled(self):
|
||||
"""Test returns False when Graphiti not enabled."""
|
||||
from integrations.graphiti.memory import test_graphiti_connection
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.enabled = False
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
success, message = await test_graphiti_connection()
|
||||
|
||||
assert success is False
|
||||
assert "not enabled" in message.lower()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_false_with_validation_errors(self):
|
||||
"""Test returns False when config has validation errors."""
|
||||
from integrations.graphiti.memory import test_graphiti_connection
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.enabled = True
|
||||
mock_config.get_validation_errors.return_value = ["API key missing"]
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
success, message = await test_graphiti_connection()
|
||||
|
||||
assert success is False
|
||||
assert "Configuration errors" in message
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_false_on_import_error(self):
|
||||
"""Test returns False when graphiti_core not installed."""
|
||||
from integrations.graphiti.memory import test_graphiti_connection
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.enabled = True
|
||||
mock_config.get_validation_errors.return_value = []
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
# Only raise ImportError for graphiti_core imports
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def selective_import_error(name, *args, **kwargs):
|
||||
if "graphiti_core" in name:
|
||||
raise ImportError(f"No module named '{name}'")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=selective_import_error):
|
||||
success, message = await test_graphiti_connection()
|
||||
|
||||
assert success is False
|
||||
assert "not installed" in message.lower()
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_true_on_successful_connection(self):
|
||||
"""Test returns True when connection succeeds (requires graphiti_core)."""
|
||||
from integrations.graphiti.memory import test_graphiti_connection
|
||||
|
||||
# This test requires graphiti_core to be installed
|
||||
# Marked as slow since it connects to actual database
|
||||
try:
|
||||
success, message = await test_graphiti_connection()
|
||||
|
||||
# If graphiti_core is not installed, success will be False
|
||||
if "not installed" in message.lower():
|
||||
assert success is False
|
||||
# If installed but DB not available, check for connection error
|
||||
elif "connection failed" in message.lower():
|
||||
assert success is False
|
||||
# If everything is set up, should succeed
|
||||
else:
|
||||
# Concrete assertion for successful connection
|
||||
assert success is True, (
|
||||
f"Expected success=True, got {success} with message: {message}"
|
||||
)
|
||||
assert message, "Message should not be empty for successful connection"
|
||||
|
||||
except AssertionError as e:
|
||||
# Re-raise AssertionError to properly surface test failures
|
||||
raise
|
||||
except Exception as e:
|
||||
# If there's an unexpected error, fail the test with useful info
|
||||
pytest.skip(f"Graphiti connection test failed: {e}")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_handles_provider_error(self):
|
||||
"""Test handles ProviderError during provider creation."""
|
||||
from integrations.graphiti.memory import test_graphiti_connection
|
||||
from integrations.graphiti.providers_pkg.exceptions import ProviderError
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.enabled = True
|
||||
mock_config.get_validation_errors.return_value = []
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
# Mock graphiti_core imports to succeed
|
||||
mock_graphiti = MagicMock()
|
||||
mock_falkordb_driver = MagicMock()
|
||||
|
||||
# Mock provider creation to raise ProviderError
|
||||
with patch("graphiti_providers.create_llm_client") as mock_create_llm:
|
||||
mock_create_llm.side_effect = ProviderError("Test provider error")
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(Graphiti=mock_graphiti),
|
||||
"graphiti_core.driver": MagicMock(),
|
||||
"graphiti_core.driver.falkordb_driver": mock_falkordb_driver,
|
||||
"graphiti_providers": MagicMock(
|
||||
ProviderError=ProviderError,
|
||||
create_embedder=MagicMock(),
|
||||
create_llm_client=mock_create_llm,
|
||||
),
|
||||
},
|
||||
):
|
||||
success, message = await test_graphiti_connection()
|
||||
|
||||
assert success is False
|
||||
assert "Provider error" in message
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for test_provider_configuration()
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestTestProviderConfiguration:
|
||||
"""Tests for test_provider_configuration function."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_returns_configuration_status(self):
|
||||
"""Test returns dict with configuration status."""
|
||||
pytest.importorskip("graphiti_providers")
|
||||
from integrations.graphiti.memory import test_provider_configuration
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.is_valid.return_value = True
|
||||
mock_config.get_validation_errors.return_value = []
|
||||
mock_config.llm_provider = "openai"
|
||||
mock_config.embedder_provider = "openai"
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
# Mock the test functions
|
||||
with patch(
|
||||
"graphiti_providers.test_llm_connection",
|
||||
return_value=(True, "LLM OK"),
|
||||
):
|
||||
with patch(
|
||||
"graphiti_providers.test_embedder_connection",
|
||||
return_value=(True, "Embedder OK"),
|
||||
):
|
||||
results = await test_provider_configuration()
|
||||
|
||||
assert isinstance(results, dict)
|
||||
assert results["config_valid"] is True
|
||||
assert results["validation_errors"] == []
|
||||
assert results["llm_provider"] == "openai"
|
||||
assert results["embedder_provider"] == "openai"
|
||||
assert results["llm_test"]["success"] is True
|
||||
assert results["embedder_test"]["success"] is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_includes_ollama_test_when_ollama_provider(self):
|
||||
"""Test includes ollama_test when using ollama provider."""
|
||||
pytest.importorskip("graphiti_providers")
|
||||
from integrations.graphiti.memory import test_provider_configuration
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.is_valid.return_value = True
|
||||
mock_config.get_validation_errors.return_value = []
|
||||
mock_config.llm_provider = "ollama"
|
||||
mock_config.embedder_provider = "openai"
|
||||
mock_config.ollama_base_url = "http://localhost:11434"
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
with patch(
|
||||
"graphiti_providers.test_llm_connection",
|
||||
return_value=(True, "LLM OK"),
|
||||
):
|
||||
with patch(
|
||||
"graphiti_providers.test_embedder_connection",
|
||||
return_value=(True, "Embedder OK"),
|
||||
):
|
||||
with patch(
|
||||
"graphiti_providers.test_ollama_connection",
|
||||
return_value=(True, "Ollama OK"),
|
||||
):
|
||||
results = await test_provider_configuration()
|
||||
|
||||
assert "ollama_test" in results
|
||||
assert results["ollama_test"]["success"] is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_omits_ollama_test_when_not_ollama_provider(self):
|
||||
"""Test omits ollama_test when not using ollama provider."""
|
||||
pytest.importorskip("graphiti_providers")
|
||||
from integrations.graphiti.memory import test_provider_configuration
|
||||
|
||||
with patch("integrations.graphiti.memory.GraphitiConfig") as mock_config_class:
|
||||
mock_config = MagicMock()
|
||||
mock_config.is_valid.return_value = True
|
||||
mock_config.get_validation_errors.return_value = []
|
||||
mock_config.llm_provider = "openai"
|
||||
mock_config.embedder_provider = "openai"
|
||||
mock_config_class.from_env.return_value = mock_config
|
||||
|
||||
with patch(
|
||||
"graphiti_providers.test_llm_connection",
|
||||
return_value=(True, "LLM OK"),
|
||||
):
|
||||
with patch(
|
||||
"graphiti_providers.test_embedder_connection",
|
||||
return_value=(True, "Embedder OK"),
|
||||
):
|
||||
results = await test_provider_configuration()
|
||||
|
||||
assert "ollama_test" not in results
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for __all__ export list
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestAllExports:
|
||||
"""Test __all__ contains expected exports."""
|
||||
|
||||
def test_all_exports_defined(self):
|
||||
"""Test __all__ is defined and contains expected items."""
|
||||
from integrations.graphiti import memory
|
||||
|
||||
assert hasattr(memory, "__all__")
|
||||
assert isinstance(memory.__all__, list)
|
||||
|
||||
expected_exports = [
|
||||
"GraphitiMemory",
|
||||
"GroupIdMode",
|
||||
"get_graphiti_memory",
|
||||
"is_graphiti_enabled",
|
||||
"test_graphiti_connection",
|
||||
"test_provider_configuration",
|
||||
"MAX_CONTEXT_RESULTS",
|
||||
"EPISODE_TYPE_SESSION_INSIGHT",
|
||||
"EPISODE_TYPE_CODEBASE_DISCOVERY",
|
||||
"EPISODE_TYPE_PATTERN",
|
||||
"EPISODE_TYPE_GOTCHA",
|
||||
"EPISODE_TYPE_TASK_OUTCOME",
|
||||
"EPISODE_TYPE_QA_RESULT",
|
||||
"EPISODE_TYPE_HISTORICAL_CONTEXT",
|
||||
]
|
||||
|
||||
for export in expected_exports:
|
||||
assert export in memory.__all__, f"{export} not in __all__"
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Quick test to demonstrate provider-specific database naming.
|
||||
|
||||
Shows how Auto Claude automatically generates provider-specific database names
|
||||
to prevent embedding dimension mismatches.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.config import GraphitiConfig
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"provider,model,dim",
|
||||
[
|
||||
("openai", None, None),
|
||||
("ollama", "embeddinggemma", 768),
|
||||
("ollama", "qwen3-embedding:0.6b", 1024),
|
||||
("voyage", None, None),
|
||||
("google", None, None),
|
||||
],
|
||||
)
|
||||
def test_provider_naming(provider, model, dim):
|
||||
"""Demonstrate provider-specific database naming."""
|
||||
# Create explicit config without relying on environment
|
||||
config = GraphitiConfig()
|
||||
config.embedder_provider = provider
|
||||
config.openai_embedding_model = "text-embedding-3-small"
|
||||
|
||||
if provider == "ollama" and model:
|
||||
config.ollama_embedding_model = model
|
||||
if dim is not None:
|
||||
config.ollama_embedding_dim = dim
|
||||
elif provider == "voyage":
|
||||
config.voyage_embedding_model = "voyage-3"
|
||||
elif provider == "google":
|
||||
config.google_embedding_model = "text-embedding-004"
|
||||
|
||||
# Get naming info
|
||||
dimension = config.get_embedding_dimension()
|
||||
signature = config.get_provider_signature()
|
||||
db_name = config.get_provider_specific_database_name("auto_claude_memory")
|
||||
|
||||
# Strengthened assertions with exact expected values where known
|
||||
if provider == "openai":
|
||||
assert dimension == 1536, f"OpenAI dimension should be 1536, got {dimension}"
|
||||
assert "openai" in signature.lower(), "OpenAI signature should contain 'openai'"
|
||||
# Signature format is provider_dimension for openai
|
||||
assert signature == "openai_1536", f"Expected 'openai_1536', got '{signature}'"
|
||||
elif provider == "ollama" and model == "embeddinggemma":
|
||||
assert dimension == 768, (
|
||||
f"Ollama gemma dimension should be 768, got {dimension}"
|
||||
)
|
||||
assert signature == f"ollama_{model}_{dimension}", (
|
||||
f"Expected 'ollama_{model}_{dimension}', got '{signature}'"
|
||||
)
|
||||
elif provider == "ollama" and model == "qwen3-embedding:0.6b":
|
||||
assert dimension == 1024, (
|
||||
f"Ollama qwen dimension should be 1024, got {dimension}"
|
||||
)
|
||||
# Colons in model names are replaced with underscores in signature
|
||||
assert signature == "ollama_qwen3-embedding_0_6b_1024", (
|
||||
f"Expected 'ollama_qwen3-embedding_0_6b_1024', got '{signature}'"
|
||||
)
|
||||
elif provider == "voyage":
|
||||
assert dimension == 1024, f"Voyage dimension should be 1024, got {dimension}"
|
||||
assert signature == "voyage_1024", f"Expected 'voyage_1024', got '{signature}'"
|
||||
elif provider == "google":
|
||||
assert dimension == 768, f"Google dimension should be 768, got {dimension}"
|
||||
assert signature == "google_768", f"Expected 'google_768', got '{signature}'"
|
||||
|
||||
# Verify signature appears in db_name
|
||||
assert signature is not None and signature != "", (
|
||||
f"Signature should be non-empty for {provider}"
|
||||
)
|
||||
assert signature in db_name, (
|
||||
f"Signature '{signature}' should appear in db_name '{db_name}' for {provider}"
|
||||
)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,149 @@
|
||||
"""
|
||||
Unit tests for Azure OpenAI embedder provider.
|
||||
|
||||
Tests cover:
|
||||
- create_azure_openai_embedder factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.embedder_providers.azure_openai_embedder import (
|
||||
create_azure_openai_embedder,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_azure_openai_embedder
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateAzureOpenAIEmbedder:
|
||||
"""Test create_azure_openai_embedder factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.azure_openai_api_key = "test-azure-key"
|
||||
config.azure_openai_base_url = "https://test.openai.azure.com"
|
||||
config.azure_openai_embedding_deployment = "test-embedding-deployment"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_azure_openai_embedder_success(self, mock_config):
|
||||
"""Test create_azure_openai_embedder returns embedder with valid config."""
|
||||
mock_azure_client = MagicMock()
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.azure_openai_embedder.AsyncOpenAI",
|
||||
return_value=mock_azure_client,
|
||||
):
|
||||
with patch(
|
||||
"graphiti_core.embedder.azure_openai.AzureOpenAIEmbedderClient",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
result = create_azure_openai_embedder(mock_config)
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_azure_openai_embedder_success_fast(self, mock_config):
|
||||
"""Fast test for create_azure_openai_embedder success path."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.embedder": MagicMock(),
|
||||
"graphiti_core.embedder.azure_openai": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.embedder.azure_openai import AzureOpenAIEmbedderClient
|
||||
|
||||
AzureOpenAIEmbedderClient.return_value = mock_embedder
|
||||
|
||||
result = create_azure_openai_embedder(mock_config)
|
||||
|
||||
# Verify the embedder was created and returned
|
||||
AzureOpenAIEmbedderClient.assert_called_once()
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_azure_openai_embedder_missing_api_key(self, mock_config):
|
||||
"""Test create_azure_openai_embedder raises ProviderError for missing API key."""
|
||||
mock_config.azure_openai_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_azure_openai_embedder(mock_config)
|
||||
|
||||
assert "AZURE_OPENAI_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_azure_openai_embedder_missing_base_url(self, mock_config):
|
||||
"""Test create_azure_openai_embedder raises ProviderError for missing base URL."""
|
||||
mock_config.azure_openai_base_url = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_azure_openai_embedder(mock_config)
|
||||
|
||||
assert "AZURE_OPENAI_BASE_URL" in str(exc_info.value)
|
||||
|
||||
def test_create_azure_openai_embedder_missing_deployment(self, mock_config):
|
||||
"""Test create_azure_openai_embedder raises ProviderError for missing deployment."""
|
||||
mock_config.azure_openai_embedding_deployment = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_azure_openai_embedder(mock_config)
|
||||
|
||||
assert "AZURE_OPENAI_EMBEDDING_DEPLOYMENT" in str(exc_info.value)
|
||||
|
||||
def test_create_azure_openai_embedder_import_error(self, mock_config):
|
||||
"""Test create_azure_openai_embedder raises ProviderNotInstalled on ImportError."""
|
||||
# Mock the import to raise ImportError
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name == "graphiti_core.embedder.azure_openai":
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_azure_openai_embedder(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_azure_openai_embedder_passes_config_correctly(self, mock_config):
|
||||
"""Test create_azure_openai_embedder passes config values correctly."""
|
||||
mock_azure_client = MagicMock()
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.azure_openai_embedder.AsyncOpenAI",
|
||||
return_value=mock_azure_client,
|
||||
) as mock_openai:
|
||||
with patch(
|
||||
"graphiti_core.embedder.azure_openai.AzureOpenAIEmbedderClient",
|
||||
return_value=mock_embedder,
|
||||
) as mock_azure_embedder:
|
||||
create_azure_openai_embedder(mock_config)
|
||||
|
||||
# Verify AsyncOpenAI was called with correct arguments
|
||||
mock_openai.assert_called_once_with(
|
||||
base_url=mock_config.azure_openai_base_url,
|
||||
api_key=mock_config.azure_openai_api_key,
|
||||
)
|
||||
|
||||
# Verify AzureOpenAIEmbedderClient was called with correct arguments
|
||||
mock_azure_embedder.assert_called_once_with(
|
||||
azure_client=mock_azure_client,
|
||||
model=mock_config.azure_openai_embedding_deployment,
|
||||
)
|
||||
@@ -0,0 +1,252 @@
|
||||
"""
|
||||
Tests for integrations.graphiti.providers module.
|
||||
|
||||
This module is a re-export facade that re-exports all public APIs
|
||||
from the graphiti_providers package.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
# Expected exports from integrations.graphiti.providers module
|
||||
EXPECTED_EXPORTS = [
|
||||
"ProviderError",
|
||||
"ProviderNotInstalled",
|
||||
"create_llm_client",
|
||||
"create_embedder",
|
||||
"create_cross_encoder",
|
||||
"EMBEDDING_DIMENSIONS",
|
||||
"get_expected_embedding_dim",
|
||||
"validate_embedding_config",
|
||||
"test_llm_connection",
|
||||
"test_embedder_connection",
|
||||
"test_ollama_connection",
|
||||
"is_graphiti_enabled",
|
||||
"get_graph_hints",
|
||||
]
|
||||
|
||||
# =============================================================================
|
||||
# Tests for module imports
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestModuleImports:
|
||||
"""Test that all expected exports are available."""
|
||||
|
||||
def test_import_ProviderError(self):
|
||||
"""Test ProviderError can be imported."""
|
||||
from integrations.graphiti.providers import ProviderError
|
||||
|
||||
assert ProviderError is not None
|
||||
# Should be an exception class
|
||||
assert issubclass(ProviderError, Exception)
|
||||
|
||||
def test_import_ProviderNotInstalled(self):
|
||||
"""Test ProviderNotInstalled can be imported."""
|
||||
from integrations.graphiti.providers import ProviderNotInstalled
|
||||
|
||||
assert ProviderNotInstalled is not None
|
||||
# Should be an exception class
|
||||
assert issubclass(ProviderNotInstalled, Exception)
|
||||
|
||||
def test_import_create_llm_client(self):
|
||||
"""Test create_llm_client can be imported."""
|
||||
from integrations.graphiti.providers import create_llm_client
|
||||
|
||||
assert create_llm_client is not None
|
||||
assert callable(create_llm_client)
|
||||
|
||||
def test_import_create_embedder(self):
|
||||
"""Test create_embedder can be imported."""
|
||||
from integrations.graphiti.providers import create_embedder
|
||||
|
||||
assert create_embedder is not None
|
||||
assert callable(create_embedder)
|
||||
|
||||
def test_import_create_cross_encoder(self):
|
||||
"""Test create_cross_encoder can be imported."""
|
||||
from integrations.graphiti.providers import create_cross_encoder
|
||||
|
||||
assert create_cross_encoder is not None
|
||||
assert callable(create_cross_encoder)
|
||||
|
||||
def test_import_EMBEDDING_DIMENSIONS(self):
|
||||
"""Test EMBEDDING_DIMENSIONS can be imported."""
|
||||
from integrations.graphiti.providers import EMBEDDING_DIMENSIONS
|
||||
|
||||
assert EMBEDDING_DIMENSIONS is not None
|
||||
assert isinstance(EMBEDDING_DIMENSIONS, dict)
|
||||
|
||||
def test_import_get_expected_embedding_dim(self):
|
||||
"""Test get_expected_embedding_dim can be imported."""
|
||||
from integrations.graphiti.providers import get_expected_embedding_dim
|
||||
|
||||
assert get_expected_embedding_dim is not None
|
||||
assert callable(get_expected_embedding_dim)
|
||||
|
||||
def test_import_validate_embedding_config(self):
|
||||
"""Test validate_embedding_config can be imported."""
|
||||
from integrations.graphiti.providers import validate_embedding_config
|
||||
|
||||
assert validate_embedding_config is not None
|
||||
assert callable(validate_embedding_config)
|
||||
|
||||
def test_import_test_llm_connection(self):
|
||||
"""Test test_llm_connection can be imported."""
|
||||
from integrations.graphiti.providers import test_llm_connection
|
||||
|
||||
assert test_llm_connection is not None
|
||||
assert callable(test_llm_connection)
|
||||
|
||||
def test_import_test_embedder_connection(self):
|
||||
"""Test test_embedder_connection can be imported."""
|
||||
from integrations.graphiti.providers import test_embedder_connection
|
||||
|
||||
assert test_embedder_connection is not None
|
||||
assert callable(test_embedder_connection)
|
||||
|
||||
def test_import_test_ollama_connection(self):
|
||||
"""Test test_ollama_connection can be imported."""
|
||||
from integrations.graphiti.providers import test_ollama_connection
|
||||
|
||||
assert test_ollama_connection is not None
|
||||
assert callable(test_ollama_connection)
|
||||
|
||||
def test_import_is_graphiti_enabled(self):
|
||||
"""Test is_graphiti_enabled can be imported."""
|
||||
from integrations.graphiti.providers import is_graphiti_enabled
|
||||
|
||||
assert is_graphiti_enabled is not None
|
||||
assert callable(is_graphiti_enabled)
|
||||
|
||||
def test_import_get_graph_hints(self):
|
||||
"""Test get_graph_hints can be imported."""
|
||||
from integrations.graphiti.providers import get_graph_hints
|
||||
|
||||
assert get_graph_hints is not None
|
||||
assert callable(get_graph_hints)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for __all__ export list
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestAllExports:
|
||||
"""Test __all__ contains expected exports."""
|
||||
|
||||
def test_all_exports_defined(self):
|
||||
"""Test __all__ is defined and contains expected items."""
|
||||
from integrations.graphiti import providers
|
||||
|
||||
assert hasattr(providers, "__all__")
|
||||
assert isinstance(providers.__all__, list)
|
||||
|
||||
for export in EXPECTED_EXPORTS:
|
||||
assert export in providers.__all__, f"{export} not in __all__"
|
||||
|
||||
def test_all_exports_count(self):
|
||||
"""Test __all__ contains the expected number of exports."""
|
||||
from integrations.graphiti import providers
|
||||
|
||||
# Should have same number of exports as EXPECTED_EXPORTS list
|
||||
assert len(providers.__all__) == len(EXPECTED_EXPORTS)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for module docstring and metadata
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestModuleMetadata:
|
||||
"""Test module has proper documentation."""
|
||||
|
||||
def test_module_has_docstring(self):
|
||||
"""Test module has docstring."""
|
||||
import integrations.graphiti.providers
|
||||
|
||||
assert integrations.graphiti.providers.__doc__ is not None
|
||||
assert len(integrations.graphiti.providers.__doc__) > 0
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for re-export behavior
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestReExportBehavior:
|
||||
"""Test that re-exports work correctly."""
|
||||
|
||||
def test_ProviderError_is_exception(self):
|
||||
"""Test ProviderError can be raised and caught."""
|
||||
from integrations.graphiti.providers import ProviderError
|
||||
|
||||
with pytest.raises(ProviderError):
|
||||
raise ProviderError("Test error")
|
||||
|
||||
def test_ProviderNotInstalled_is_exception(self):
|
||||
"""Test ProviderNotInstalled can be raised and caught."""
|
||||
from integrations.graphiti.providers import ProviderNotInstalled
|
||||
|
||||
with pytest.raises(ProviderNotInstalled):
|
||||
raise ProviderNotInstalled("Test error")
|
||||
|
||||
def test_ProviderNotInstalled_subclass_of_ProviderError(self):
|
||||
"""Test ProviderNotInstalled is a subclass of ProviderError."""
|
||||
from integrations.graphiti.providers import ProviderError, ProviderNotInstalled
|
||||
|
||||
assert issubclass(ProviderNotInstalled, ProviderError)
|
||||
|
||||
def test_EMBEDDING_DIMENSIONS_has_expected_keys(self):
|
||||
"""Test EMBEDDING_DIMENSIONS has expected model keys."""
|
||||
from integrations.graphiti.providers import EMBEDDING_DIMENSIONS
|
||||
|
||||
# Check that expected model names exist in EMBEDDING_DIMENSIONS
|
||||
# Note: EMBEDDING_DIMENSIONS is keyed by model name, not provider name
|
||||
expected_models = [
|
||||
"text-embedding-3-small", # OpenAI
|
||||
"voyage-3", # Voyage AI
|
||||
"nomic-embed-text", # Ollama
|
||||
"all-minilm", # Ollama
|
||||
]
|
||||
|
||||
for model in expected_models:
|
||||
assert model in EMBEDDING_DIMENSIONS, f"{model} not in EMBEDDING_DIMENSIONS"
|
||||
assert isinstance(EMBEDDING_DIMENSIONS[model], int)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Tests for namespace integrity
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestNamespaceIntegrity:
|
||||
"""Test module namespace remains consistent."""
|
||||
|
||||
def test_exports_are_accessible(self):
|
||||
"""Test all exports in __all__ are accessible."""
|
||||
from integrations.graphiti import providers
|
||||
|
||||
for name in providers.__all__:
|
||||
# Each export should be accessible
|
||||
assert hasattr(providers, name), f"{name} not accessible"
|
||||
|
||||
def test_import_from_module_works(self):
|
||||
"""Test 'from' imports work correctly."""
|
||||
# This tests the re-export mechanism
|
||||
from integrations.graphiti.providers import (
|
||||
ProviderError,
|
||||
create_embedder,
|
||||
create_llm_client,
|
||||
)
|
||||
|
||||
assert ProviderError is not None
|
||||
assert create_llm_client is not None
|
||||
assert create_embedder is not None
|
||||
|
||||
def test_module_level_import_works(self):
|
||||
"""Test module-level import works."""
|
||||
import integrations.graphiti.providers as providers
|
||||
|
||||
assert providers.ProviderError is not None
|
||||
assert providers.create_llm_client is not None
|
||||
assert providers.create_embedder is not None
|
||||
@@ -0,0 +1,256 @@
|
||||
"""
|
||||
Unit tests for Google embedder provider.
|
||||
|
||||
Tests cover:
|
||||
- create_google_embedder factory function
|
||||
- GoogleEmbedder class (create, create_batch methods)
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.embedder_providers.google_embedder import (
|
||||
DEFAULT_GOOGLE_EMBEDDING_MODEL,
|
||||
GoogleEmbedder,
|
||||
create_google_embedder,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Pytest fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def google_genai_mock():
|
||||
"""Mock google.generativeai module with common setup."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_genai.embed_content = MagicMock(return_value={"embedding": [0.1, 0.2, 0.3]})
|
||||
return mock_genai
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test GoogleEmbedder class
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGoogleEmbedder:
|
||||
"""Test GoogleEmbedder class."""
|
||||
|
||||
def test_google_embedder_init_success(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder initializes with API key and model."""
|
||||
# Inject mock into sys.modules before importing
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key", model="test-model")
|
||||
|
||||
assert embedder.api_key == "test-key"
|
||||
assert embedder.model == "test-model"
|
||||
google_genai_mock.configure.assert_called_once_with(api_key="test-key")
|
||||
|
||||
def test_google_embedder_init_default_model(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder uses default model when not specified."""
|
||||
# Inject mock into sys.modules before importing
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
|
||||
assert embedder.model == DEFAULT_GOOGLE_EMBEDDING_MODEL
|
||||
|
||||
def test_google_embedder_init_import_error(self):
|
||||
"""Test GoogleEmbedder raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name == "google.generativeai" or name.startswith("google.generativeai."):
|
||||
raise ImportError("google-generativeai not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
# Remove google.generativeai from sys.modules if present
|
||||
# to ensure the import actually goes through __import__
|
||||
with patch.dict(sys.modules, {"google.generativeai": None}):
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
GoogleEmbedder(api_key="test-key")
|
||||
|
||||
assert "google-generativeai" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_with_string(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder.create with string input."""
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
result = await embedder.create("test text")
|
||||
|
||||
assert result == [0.1, 0.2, 0.3]
|
||||
# Assert embed_content was called
|
||||
google_genai_mock.embed_content.assert_called_once()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_with_list(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder.create with list input."""
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
result = await embedder.create(["test", "text"])
|
||||
|
||||
assert result == [0.1, 0.2, 0.3]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_with_non_string_list(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder.create with non-string list items (lines 71-73)."""
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
# List with non-string items - should convert to string
|
||||
result = await embedder.create([123, 456])
|
||||
|
||||
assert result == [0.1, 0.2, 0.3]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_with_empty_list(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder.create with empty or invalid input (line 75)."""
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
# Empty list - should be converted to string
|
||||
result = await embedder.create([])
|
||||
|
||||
assert result == [0.1, 0.2, 0.3]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_batch(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder.create_batch with multiple inputs (lines 100-127)."""
|
||||
# Override embed_content return value for batch test
|
||||
google_genai_mock.embed_content = MagicMock(
|
||||
return_value={"embedding": [[0.1, 0.2], [0.3, 0.4]]}
|
||||
)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
result = await embedder.create_batch(["text1", "text2"])
|
||||
|
||||
# Should handle nested list response (lines 122-125)
|
||||
assert len(result) == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_batch_single_response(
|
||||
self, google_genai_mock
|
||||
):
|
||||
"""Test GoogleEmbedder.create_batch with single embedding response (lines 124-125)."""
|
||||
# Override embed_content return value for single response test
|
||||
google_genai_mock.embed_content = MagicMock(
|
||||
return_value={"embedding": [0.1, 0.2, 0.3]}
|
||||
)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
result = await embedder.create_batch(["text1"])
|
||||
|
||||
# Should handle single embedding response (line 125)
|
||||
assert len(result) == 1
|
||||
assert result[0] == [0.1, 0.2, 0.3]
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_embedder_create_batch_large_input(self, google_genai_mock):
|
||||
"""Test GoogleEmbedder.create_batch with >100 items (batching)."""
|
||||
# Override embed_content return value for large batch test
|
||||
google_genai_mock.embed_content = MagicMock(
|
||||
return_value={"embedding": [[0.1, 0.2]]}
|
||||
)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": google_genai_mock}):
|
||||
embedder = GoogleEmbedder(api_key="test-key")
|
||||
# Create 250 items - should be split into 3 batches (100, 100, 50)
|
||||
result = await embedder.create_batch([f"text{i}" for i in range(250)])
|
||||
|
||||
# Should call embed_content 3 times
|
||||
assert google_genai_mock.embed_content.call_count == 3
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test create_google_embedder
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateGoogleEmbedder:
|
||||
"""Test create_google_embedder factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.google_api_key = "test-google-key"
|
||||
config.google_embedding_model = None
|
||||
return config
|
||||
|
||||
def test_create_google_embedder_success(self, mock_config):
|
||||
"""Test create_google_embedder returns embedder with valid config."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.google_embedder.GoogleEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
result = create_google_embedder(mock_config)
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_google_embedder_missing_api_key(self, mock_config):
|
||||
"""Test create_google_embedder raises ProviderError for missing API key."""
|
||||
mock_config.google_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_google_embedder(mock_config)
|
||||
|
||||
assert "GOOGLE_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_google_embedder_with_custom_model(self, mock_config):
|
||||
"""Test create_google_embedder uses custom model when specified."""
|
||||
mock_config.google_embedding_model = "custom-model"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.google_embedder.GoogleEmbedder",
|
||||
return_value=mock_embedder,
|
||||
) as mock_google_embedder:
|
||||
create_google_embedder(mock_config)
|
||||
|
||||
mock_google_embedder.assert_called_once_with(
|
||||
api_key=mock_config.google_api_key,
|
||||
model="custom-model",
|
||||
)
|
||||
|
||||
def test_create_google_embedder_with_default_model(self, mock_config):
|
||||
"""Test create_google_embedder uses default model when not specified."""
|
||||
mock_config.google_embedding_model = None
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.google_embedder.GoogleEmbedder",
|
||||
return_value=mock_embedder,
|
||||
) as mock_google_embedder:
|
||||
create_google_embedder(mock_config)
|
||||
|
||||
mock_google_embedder.assert_called_once_with(
|
||||
api_key=mock_config.google_api_key,
|
||||
model=DEFAULT_GOOGLE_EMBEDDING_MODEL,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test Constants
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGoogleEmbedderConstants:
|
||||
"""Test Google embedder constants."""
|
||||
|
||||
def test_default_google_embedding_model(self):
|
||||
# Note: This test verifies the default Google embedding model.
|
||||
# The value should match the model used in production.
|
||||
assert DEFAULT_GOOGLE_EMBEDDING_MODEL == "text-embedding-004"
|
||||
@@ -0,0 +1,146 @@
|
||||
"""
|
||||
Unit tests for Anthropic LLM provider.
|
||||
|
||||
Tests cover:
|
||||
- create_anthropic_llm_client factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.llm_providers.anthropic_llm import (
|
||||
create_anthropic_llm_client,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_anthropic_llm_client
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateAnthropicLLMClient:
|
||||
"""Test create_anthropic_llm_client factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.anthropic_api_key = "sk-ant-test-key"
|
||||
config.anthropic_model = "claude-sonnet-4-20250514"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_anthropic_llm_client_success(self, mock_config):
|
||||
"""Test create_anthropic_llm_client returns client with valid config."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
# Patch at the location where the import happens (local import inside function)
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.anthropic_llm.AnthropicClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
result = create_anthropic_llm_client(mock_config)
|
||||
assert result == mock_client
|
||||
|
||||
def test_create_anthropic_llm_client_success_fast(self, mock_config):
|
||||
"""Fast test for create_anthropic_llm_client success path."""
|
||||
mock_llm_client = MagicMock()
|
||||
|
||||
# Create the config mock
|
||||
mock_config_module = MagicMock()
|
||||
mock_config_module.LLMConfig = MagicMock
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.anthropic_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": mock_config_module,
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.anthropic_client import AnthropicClient
|
||||
|
||||
AnthropicClient.return_value = mock_llm_client
|
||||
|
||||
result = create_anthropic_llm_client(mock_config)
|
||||
|
||||
# Verify the client was created and returned
|
||||
AnthropicClient.assert_called_once()
|
||||
assert result == mock_llm_client
|
||||
|
||||
def test_create_anthropic_llm_client_missing_api_key_fast(self, mock_config):
|
||||
"""Fast test for API key validation (line 41)."""
|
||||
# Mock the graphiti_core imports first to avoid ImportError
|
||||
mock_config_module = MagicMock()
|
||||
mock_config_module.LLMConfig = MagicMock
|
||||
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.anthropic_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": mock_config_module,
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.anthropic_client import AnthropicClient
|
||||
|
||||
AnthropicClient.return_value = MagicMock()
|
||||
|
||||
# Now set API key to None to test validation
|
||||
mock_config.anthropic_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_anthropic_llm_client(mock_config)
|
||||
|
||||
assert "ANTHROPIC_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_anthropic_llm_client_import_error(self, mock_config):
|
||||
"""Test create_anthropic_llm_client raises ProviderNotInstalled on ImportError."""
|
||||
from types import ModuleType
|
||||
|
||||
# Create a broken module that raises ImportError on attribute access
|
||||
def broken_getattr(name):
|
||||
if name in ("llm_client", "anthropic_client", "config"):
|
||||
raise ImportError("graphiti-core[anthropic] not installed")
|
||||
raise AttributeError(f"module has no attribute '{name}'")
|
||||
|
||||
broken_module = ModuleType("graphiti_core")
|
||||
broken_module.__getattr__ = broken_getattr
|
||||
|
||||
# Patch both modules that are imported
|
||||
with patch.dict(sys.modules, {"graphiti_core": broken_module}):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_anthropic_llm_client(mock_config)
|
||||
|
||||
assert "graphiti-core[anthropic]" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_anthropic_llm_client_passes_config_correctly(self, mock_config):
|
||||
"""Test create_anthropic_llm_client passes config values correctly."""
|
||||
mock_config.anthropic_api_key = "sk-ant-test-key-123"
|
||||
mock_config.anthropic_model = "claude-opus-4-20250514"
|
||||
mock_client = MagicMock()
|
||||
|
||||
# Patch at the location where the imports happen (local imports inside function)
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.anthropic_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.anthropic_llm.AnthropicClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_anthropic_llm_client(mock_config)
|
||||
|
||||
# Verify LLMConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "sk-ant-test-key-123"
|
||||
assert call_kwargs["model"] == "claude-opus-4-20250514"
|
||||
@@ -0,0 +1,163 @@
|
||||
"""
|
||||
Unit tests for Azure OpenAI LLM provider.
|
||||
|
||||
Tests cover:
|
||||
- create_azure_openai_llm_client factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.llm_providers.azure_openai_llm import (
|
||||
create_azure_openai_llm_client,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_azure_openai_llm_client
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateAzureOpenAILLMClient:
|
||||
"""Test create_azure_openai_llm_client factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.azure_openai_api_key = "test-azure-key"
|
||||
config.azure_openai_base_url = "https://test.openai.azure.com"
|
||||
config.azure_openai_llm_deployment = "test-llm-deployment"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_azure_openai_llm_client_success(self, mock_config):
|
||||
"""Test create_azure_openai_llm_client returns client with valid config."""
|
||||
mock_azure_client = MagicMock()
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.azure_openai_llm.AsyncOpenAI",
|
||||
return_value=mock_azure_client,
|
||||
):
|
||||
with patch(
|
||||
"graphiti_core.llm_client.azure_openai_client.AzureOpenAILLMClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
result = create_azure_openai_llm_client(mock_config)
|
||||
assert result == mock_client
|
||||
|
||||
def test_create_azure_openai_llm_client_success_fast(self, mock_config):
|
||||
"""Fast test for create_azure_openai_llm_client success path."""
|
||||
mock_llm_client = MagicMock()
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.azure_openai_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.azure_openai_client import (
|
||||
AzureOpenAILLMClient,
|
||||
)
|
||||
|
||||
AzureOpenAILLMClient.return_value = mock_llm_client
|
||||
|
||||
result = create_azure_openai_llm_client(mock_config)
|
||||
|
||||
# Verify the client was created and returned
|
||||
AzureOpenAILLMClient.assert_called_once()
|
||||
assert result == mock_llm_client
|
||||
|
||||
def test_create_azure_openai_llm_client_missing_api_key(self, mock_config):
|
||||
"""Test create_azure_openai_llm_client raises ProviderError for missing API key."""
|
||||
mock_config.azure_openai_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_azure_openai_llm_client(mock_config)
|
||||
|
||||
assert "AZURE_OPENAI_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_azure_openai_llm_client_missing_base_url(self, mock_config):
|
||||
"""Test create_azure_openai_llm_client raises ProviderError for missing base URL."""
|
||||
mock_config.azure_openai_base_url = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_azure_openai_llm_client(mock_config)
|
||||
|
||||
assert "AZURE_OPENAI_BASE_URL" in str(exc_info.value)
|
||||
|
||||
def test_create_azure_openai_llm_client_missing_deployment(self, mock_config):
|
||||
"""Test create_azure_openai_llm_client raises ProviderError for missing deployment."""
|
||||
mock_config.azure_openai_llm_deployment = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_azure_openai_llm_client(mock_config)
|
||||
|
||||
assert "AZURE_OPENAI_LLM_DEPLOYMENT" in str(exc_info.value)
|
||||
|
||||
def test_create_azure_openai_llm_client_import_error(self, mock_config):
|
||||
"""Test create_azure_openai_llm_client raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if (
|
||||
name.startswith("graphiti_core.llm_client")
|
||||
or name == "openai"
|
||||
or name.startswith("openai.")
|
||||
):
|
||||
raise ImportError("Required package not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_azure_openai_llm_client(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
assert "openai" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_azure_openai_llm_client_passes_config_correctly(self, mock_config):
|
||||
"""Test create_azure_openai_llm_client passes config values correctly."""
|
||||
mock_azure_client = MagicMock()
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.azure_openai_llm.AsyncOpenAI",
|
||||
return_value=mock_azure_client,
|
||||
) as mock_openai:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.azure_openai_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"graphiti_core.llm_client.azure_openai_client.AzureOpenAILLMClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_azure_openai_llm_client(mock_config)
|
||||
|
||||
# Verify AsyncOpenAI was called with correct arguments
|
||||
mock_openai.assert_called_once_with(
|
||||
base_url=mock_config.azure_openai_base_url,
|
||||
api_key=mock_config.azure_openai_api_key,
|
||||
)
|
||||
|
||||
# Verify LLMConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert (
|
||||
call_kwargs["model"] == mock_config.azure_openai_llm_deployment
|
||||
)
|
||||
assert (
|
||||
call_kwargs["small_model"]
|
||||
== mock_config.azure_openai_llm_deployment
|
||||
)
|
||||
@@ -0,0 +1,410 @@
|
||||
"""
|
||||
Unit tests for Google LLM provider.
|
||||
|
||||
Tests cover:
|
||||
- create_google_llm_client factory function
|
||||
- GoogleLLMClient class (generate_response, generate_response_with_tools)
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.llm_providers.google_llm import (
|
||||
DEFAULT_GOOGLE_LLM_MODEL,
|
||||
GoogleLLMClient,
|
||||
create_google_llm_client,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test GoogleLLMClient class
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGoogleLLMClient:
|
||||
"""Test GoogleLLMClient class."""
|
||||
|
||||
def test_google_llm_client_init_success(self):
|
||||
"""Test GoogleLLMClient initializes with API key and model."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key", model="test-model")
|
||||
|
||||
assert client.api_key == "test-key"
|
||||
assert client.model == "test-model"
|
||||
mock_genai.configure.assert_called_once_with(api_key="test-key")
|
||||
mock_genai.GenerativeModel.assert_called_once_with("test-model")
|
||||
|
||||
def test_google_llm_client_init_default_model(self):
|
||||
"""Test GoogleLLMClient uses default model when not specified."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
|
||||
assert client.model == DEFAULT_GOOGLE_LLM_MODEL
|
||||
|
||||
def test_google_llm_client_init_import_error(self):
|
||||
"""Test GoogleLLMClient raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name == "google.generativeai" or name.startswith("google.generativeai."):
|
||||
raise ImportError("google-generativeai not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
GoogleLLMClient(api_key="test-key")
|
||||
|
||||
assert "google-generativeai" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_user_message(self):
|
||||
"""Test GoogleLLMClient.generate_response with user message (lines 73-133)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[{"role": "user", "content": "Hello"}]
|
||||
)
|
||||
|
||||
assert result == "Test response"
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_user_message_slow(self):
|
||||
"""Test GoogleLLMClient.generate_response with user message (slow variant)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[{"role": "user", "content": "Hello"}]
|
||||
)
|
||||
|
||||
assert result == "Test response"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_system_message(self):
|
||||
"""Test GoogleLLMClient.generate_response with system instruction (lines 84-98)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model_with_sys = MagicMock()
|
||||
mock_model_without_sys = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(
|
||||
side_effect=[mock_model_without_sys, mock_model_with_sys]
|
||||
)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model_with_sys.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[
|
||||
{"role": "system", "content": "You are helpful"},
|
||||
{"role": "user", "content": "Hello"},
|
||||
]
|
||||
)
|
||||
|
||||
assert result == "Test response"
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_system_message_slow(self):
|
||||
"""Test GoogleLLMClient.generate_response with system instruction (slow variant)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model_with_sys = MagicMock()
|
||||
mock_model_without_sys = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(
|
||||
side_effect=[mock_model_without_sys, mock_model_with_sys]
|
||||
)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model_with_sys.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[
|
||||
{"role": "system", "content": "You are helpful"},
|
||||
{"role": "user", "content": "Hello"},
|
||||
]
|
||||
)
|
||||
|
||||
assert result == "Test response"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_assistant_message(self):
|
||||
"""Test GoogleLLMClient.generate_response with assistant role (lines 87-88)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[
|
||||
{"role": "user", "content": "Hello"},
|
||||
{"role": "assistant", "content": "Hi there"},
|
||||
{"role": "user", "content": "How are you?"},
|
||||
]
|
||||
)
|
||||
|
||||
assert result == "Test response"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_response_model(self):
|
||||
"""Test GoogleLLMClient.generate_response with structured output (lines 103-127)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = '{"key": "value"}'
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
mock_genai.GenerationConfig = MagicMock()
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
from pydantic import BaseModel
|
||||
|
||||
class TestModel(BaseModel):
|
||||
key: str
|
||||
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
response_model=TestModel,
|
||||
)
|
||||
|
||||
assert isinstance(result, TestModel)
|
||||
assert result.key == "value"
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_response_model_slow(self):
|
||||
"""Test GoogleLLMClient.generate_response with structured output (slow variant)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = '{"key": "value"}'
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
mock_genai.GenerationConfig = MagicMock()
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
from pydantic import BaseModel
|
||||
|
||||
class TestModel(BaseModel):
|
||||
key: str
|
||||
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
response_model=TestModel,
|
||||
)
|
||||
|
||||
assert isinstance(result, TestModel)
|
||||
assert result.key == "value"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_json_decode_error(self):
|
||||
"""Test GoogleLLMClient.generate_response with JSON decode error (lines 122-127)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Not valid JSON"
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
mock_genai.GenerationConfig = MagicMock()
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
from pydantic import BaseModel
|
||||
|
||||
class TestModel(BaseModel):
|
||||
key: str
|
||||
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
result = await client.generate_response(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
response_model=TestModel,
|
||||
)
|
||||
|
||||
# Should return raw text when JSON parsing fails
|
||||
assert result == "Not valid JSON"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_tools(self):
|
||||
"""Test GoogleLLMClient.generate_response_with_tools (lines 155-160)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.google_llm.logger"
|
||||
) as mock_logger:
|
||||
result = await client.generate_response_with_tools(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
tools=[{"name": "test_tool"}],
|
||||
)
|
||||
|
||||
# Should log warning about tools not being supported
|
||||
mock_logger.warning.assert_called_once()
|
||||
assert "does not yet support tool calling" in str(
|
||||
mock_logger.warning.call_args
|
||||
)
|
||||
assert result == "Test response"
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.asyncio
|
||||
async def test_google_llm_client_generate_response_with_tools_slow(self):
|
||||
"""Test GoogleLLMClient.generate_response_with_tools (slow variant)."""
|
||||
mock_genai = MagicMock()
|
||||
mock_genai.configure = MagicMock()
|
||||
mock_model = MagicMock()
|
||||
mock_genai.GenerativeModel = MagicMock(return_value=mock_model)
|
||||
mock_response = MagicMock()
|
||||
mock_response.text = "Test response"
|
||||
mock_model.generate_content = MagicMock(return_value=mock_response)
|
||||
|
||||
with patch.dict(sys.modules, {"google.generativeai": mock_genai}):
|
||||
client = GoogleLLMClient(api_key="test-key")
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.google_llm.logger"
|
||||
) as mock_logger:
|
||||
result = await client.generate_response_with_tools(
|
||||
[{"role": "user", "content": "Hello"}],
|
||||
tools=[{"name": "test_tool"}],
|
||||
)
|
||||
|
||||
mock_logger.warning.assert_called_once()
|
||||
assert "does not yet support tool calling" in str(
|
||||
mock_logger.warning.call_args
|
||||
)
|
||||
assert result == "Test response"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test create_google_llm_client
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateGoogleLLMClient:
|
||||
"""Test create_google_llm_client factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.google_api_key = "test-google-key"
|
||||
config.google_llm_model = None
|
||||
return config
|
||||
|
||||
def test_create_google_llm_client_success(self, mock_config):
|
||||
"""Test create_google_llm_client returns client with valid config."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.google_llm.GoogleLLMClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
result = create_google_llm_client(mock_config)
|
||||
assert result == mock_client
|
||||
|
||||
def test_create_google_llm_client_missing_api_key(self, mock_config):
|
||||
"""Test create_google_llm_client raises ProviderError for missing API key."""
|
||||
mock_config.google_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_google_llm_client(mock_config)
|
||||
|
||||
assert "GOOGLE_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_google_llm_client_with_custom_model(self, mock_config):
|
||||
"""Test create_google_llm_client uses custom model when specified."""
|
||||
mock_config.google_llm_model = "custom-model"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.google_llm.GoogleLLMClient",
|
||||
return_value=mock_client,
|
||||
) as mock_google_client:
|
||||
create_google_llm_client(mock_config)
|
||||
|
||||
mock_google_client.assert_called_once_with(
|
||||
api_key=mock_config.google_api_key,
|
||||
model="custom-model",
|
||||
)
|
||||
|
||||
def test_create_google_llm_client_with_default_model(self, mock_config):
|
||||
"""Test create_google_llm_client uses default model when not specified."""
|
||||
mock_config.google_llm_model = None
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.google_llm.GoogleLLMClient",
|
||||
return_value=mock_client,
|
||||
) as mock_google_client:
|
||||
create_google_llm_client(mock_config)
|
||||
|
||||
mock_google_client.assert_called_once_with(
|
||||
api_key=mock_config.google_api_key,
|
||||
model=DEFAULT_GOOGLE_LLM_MODEL,
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test Constants
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGoogleLLMConstants:
|
||||
"""Test Google LLM constants."""
|
||||
|
||||
def test_default_google_llm_model(self):
|
||||
"""Test DEFAULT_GOOGLE_LLM_MODEL is set correctly."""
|
||||
assert DEFAULT_GOOGLE_LLM_MODEL == "gemini-2.0-flash"
|
||||
@@ -0,0 +1,181 @@
|
||||
"""
|
||||
Unit tests for Ollama LLM provider.
|
||||
|
||||
Tests cover:
|
||||
- create_ollama_llm_client factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.llm_providers.ollama_llm import (
|
||||
create_ollama_llm_client,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_ollama_llm_client
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateOllamaLLMClient:
|
||||
"""Test create_ollama_llm_client factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.ollama_llm_model = "llama3.2"
|
||||
config.ollama_base_url = "http://localhost:11434"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_llm_client_success(self, mock_config):
|
||||
"""Test create_ollama_llm_client returns client with valid config."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.OpenAIGenericClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
result = create_ollama_llm_client(mock_config)
|
||||
assert result == mock_client
|
||||
|
||||
def test_create_ollama_llm_client_success_fast(self, mock_config):
|
||||
"""Fast test for create_ollama_llm_client success path."""
|
||||
mock_llm_client = MagicMock()
|
||||
|
||||
# Create the config mock
|
||||
mock_config_module = MagicMock()
|
||||
mock_config_module.LLMConfig = MagicMock
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": mock_config_module,
|
||||
"graphiti_core.llm_client.openai_generic_client": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.openai_generic_client import (
|
||||
OpenAIGenericClient,
|
||||
)
|
||||
|
||||
OpenAIGenericClient.return_value = mock_llm_client
|
||||
|
||||
result = create_ollama_llm_client(mock_config)
|
||||
|
||||
# Verify the client was created and returned
|
||||
OpenAIGenericClient.assert_called_once()
|
||||
assert result == mock_llm_client
|
||||
|
||||
def test_create_ollama_llm_client_missing_model(self, mock_config):
|
||||
"""Test create_ollama_llm_client raises ProviderError for missing model."""
|
||||
mock_config.ollama_llm_model = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_ollama_llm_client(mock_config)
|
||||
|
||||
assert "OLLAMA_LLM_MODEL" in str(exc_info.value)
|
||||
|
||||
def test_create_ollama_llm_client_import_error(self, mock_config):
|
||||
"""Test create_ollama_llm_client raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name.startswith("graphiti_core.llm_client"):
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_ollama_llm_client(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_llm_client_base_url_without_v1(self, mock_config):
|
||||
"""Test create_ollama_llm_client appends /v1 to base URL if missing."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.OpenAIGenericClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_ollama_llm_client(mock_config)
|
||||
|
||||
# Verify base_url has /v1 appended
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_llm_client_base_url_with_v1(self, mock_config):
|
||||
"""Test create_ollama_llm_client doesn't duplicate /v1 in base URL."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434/v1"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.OpenAIGenericClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_ollama_llm_client(mock_config)
|
||||
|
||||
# Verify base_url is not duplicated
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_llm_client_base_url_with_trailing_slash(self, mock_config):
|
||||
"""Test create_ollama_llm_client handles trailing slash correctly."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434/"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.OpenAIGenericClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_ollama_llm_client(mock_config)
|
||||
|
||||
# Verify trailing slash is handled
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_llm_client_passes_config_correctly(self, mock_config):
|
||||
"""Test create_ollama_llm_client passes config values correctly."""
|
||||
mock_config.ollama_llm_model = "qwen2.5"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.ollama_llm.OpenAIGenericClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_ollama_llm_client(mock_config)
|
||||
|
||||
# Verify LLMConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "ollama"
|
||||
assert call_kwargs["model"] == "qwen2.5"
|
||||
assert call_kwargs["small_model"] == "qwen2.5"
|
||||
@@ -0,0 +1,207 @@
|
||||
"""
|
||||
Unit tests for OpenAI LLM provider.
|
||||
|
||||
Tests cover:
|
||||
- create_openai_llm_client factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.llm_providers.openai_llm import (
|
||||
create_openai_llm_client,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_openai_llm_client
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateOpenAILLMClient:
|
||||
"""Test create_openai_llm_client factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.openai_api_key = "sk-test-key"
|
||||
config.openai_model = "gpt-4o"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openai_llm_client_success(self, mock_config):
|
||||
"""Test create_openai_llm_client returns client with valid config."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openai_llm.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
result = create_openai_llm_client(mock_config)
|
||||
assert result == mock_client
|
||||
|
||||
def test_create_openai_llm_client_success_fast(self, mock_config):
|
||||
"""Fast test for create_openai_llm_client success path."""
|
||||
mock_llm_client = MagicMock()
|
||||
|
||||
# Create the config mock
|
||||
mock_config_module = MagicMock()
|
||||
mock_config_module.LLMConfig = MagicMock
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": mock_config_module,
|
||||
"graphiti_core.llm_client.openai_client": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.openai_client import OpenAIClient
|
||||
|
||||
OpenAIClient.return_value = mock_llm_client
|
||||
|
||||
result = create_openai_llm_client(mock_config)
|
||||
|
||||
# Verify the client was created and returned
|
||||
OpenAIClient.assert_called_once()
|
||||
assert result == mock_llm_client
|
||||
|
||||
def test_create_openai_llm_client_missing_api_key(self, mock_config):
|
||||
"""Test create_openai_llm_client raises ProviderError for missing API key."""
|
||||
mock_config.openai_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_openai_llm_client(mock_config)
|
||||
|
||||
assert "OPENAI_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_openai_llm_client_import_error(self, mock_config):
|
||||
"""Test create_openai_llm_client raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name.startswith("graphiti_core.llm_client"):
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_openai_llm_client(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
def test_create_openai_llm_client_gpt5_model_with_reasoning_fast(self, mock_config):
|
||||
"""Fast test for GPT-5 model with reasoning (line 58)."""
|
||||
mock_config.openai_model = "gpt-5-turbo"
|
||||
mock_client = MagicMock()
|
||||
|
||||
# Create the config mock
|
||||
mock_config_module = MagicMock()
|
||||
mock_config_module.LLMConfig = MagicMock
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.llm_client": MagicMock(),
|
||||
"graphiti_core.llm_client.config": mock_config_module,
|
||||
"graphiti_core.llm_client.openai_client": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.llm_client.openai_client import OpenAIClient
|
||||
|
||||
OpenAIClient.return_value = mock_client
|
||||
|
||||
result = create_openai_llm_client(mock_config)
|
||||
|
||||
# Verify the client was created with default config (no extra params)
|
||||
OpenAIClient.assert_called_once()
|
||||
call_kwargs = OpenAIClient.call_args.kwargs
|
||||
# Should not have reasoning/verbosity params set to None for GPT-5
|
||||
assert (
|
||||
"reasoning" not in call_kwargs
|
||||
or call_kwargs.get("reasoning") is not False
|
||||
)
|
||||
assert (
|
||||
"verbosity" not in call_kwargs
|
||||
or call_kwargs.get("verbosity") is not False
|
||||
)
|
||||
assert result == mock_client
|
||||
|
||||
@pytest.mark.slow
|
||||
@pytest.mark.parametrize(
|
||||
"model,expected_reasoning,expected_verbosity",
|
||||
[
|
||||
pytest.param("gpt-5-turbo", True, None, id="gpt5"),
|
||||
pytest.param("o1-preview", True, None, id="o1"),
|
||||
pytest.param("o3-mini", True, None, id="o3"),
|
||||
],
|
||||
)
|
||||
def test_create_openai_llm_client_reasoning_models(
|
||||
self, mock_config, model, expected_reasoning, expected_verbosity
|
||||
):
|
||||
"""Test create_openai_llm_client with reasoning-capable models."""
|
||||
mock_config.openai_model = model
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openai_llm.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
) as mock_openai_client:
|
||||
create_openai_llm_client(mock_config)
|
||||
|
||||
mock_openai_client.assert_called_once()
|
||||
call_kwargs = mock_openai_client.call_args.kwargs
|
||||
# Verify reasoning is set to True for reasoning models
|
||||
assert call_kwargs.get("reasoning") is expected_reasoning
|
||||
# Verify verbosity matches expected value (None for these models)
|
||||
assert call_kwargs.get("verbosity") == expected_verbosity
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openai_llm_client_gpt4_model_without_reasoning(self, mock_config):
|
||||
"""Test create_openai_llm_client with GPT-4 model disables reasoning."""
|
||||
mock_config.openai_model = "gpt-4o"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openai_llm.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
) as mock_openai_client:
|
||||
create_openai_llm_client(mock_config)
|
||||
|
||||
# GPT-4 models should be created with reasoning=None, verbosity=None
|
||||
call_kwargs = mock_openai_client.call_args.kwargs
|
||||
assert call_kwargs.get("reasoning") is None
|
||||
assert call_kwargs.get("verbosity") is None
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openai_llm_client_passes_config_correctly(self, mock_config):
|
||||
"""Test create_openai_llm_client passes config values correctly."""
|
||||
mock_config.openai_api_key = "sk-test-key-123"
|
||||
mock_config.openai_model = "gpt-4o-mini"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openai_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openai_llm.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_openai_llm_client(mock_config)
|
||||
|
||||
# Verify LLMConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "sk-test-key-123"
|
||||
assert call_kwargs["model"] == "gpt-4o-mini"
|
||||
@@ -0,0 +1,113 @@
|
||||
"""
|
||||
Unit tests for OpenRouter LLM provider.
|
||||
|
||||
Tests cover:
|
||||
- create_openrouter_llm_client factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.llm_providers.openrouter_llm import (
|
||||
create_openrouter_llm_client,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_openrouter_llm_client
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateOpenRouterLLMClient:
|
||||
"""Test create_openrouter_llm_client factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.openrouter_api_key = "sk-or-test-key"
|
||||
config.openrouter_llm_model = "anthropic/claude-sonnet-4"
|
||||
config.openrouter_base_url = "https://openrouter.ai/api/v1"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openrouter_llm_client_success(self, mock_config):
|
||||
"""Test create_openrouter_llm_client returns client with valid config."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"graphiti_core.llm_client.openai_client.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
result = create_openrouter_llm_client(mock_config)
|
||||
assert result == mock_client
|
||||
|
||||
def test_create_openrouter_llm_client_missing_api_key(self, mock_config):
|
||||
"""Test create_openrouter_llm_client raises ProviderError for missing API key."""
|
||||
mock_config.openrouter_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_openrouter_llm_client(mock_config)
|
||||
|
||||
assert "OPENROUTER_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_openrouter_llm_client_import_error(self, mock_config):
|
||||
"""Test create_openrouter_llm_client raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name.startswith("graphiti_core.llm_client"):
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_openrouter_llm_client(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openrouter_llm_client_passes_config_correctly(self, mock_config):
|
||||
"""Test create_openrouter_llm_client passes config values correctly."""
|
||||
mock_config.openrouter_api_key = "sk-or-test-key-123"
|
||||
mock_config.openrouter_llm_model = "openai/gpt-4o"
|
||||
mock_config.openrouter_base_url = "https://custom.openrouter.ai/api/v1"
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openrouter_llm.LLMConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openrouter_llm.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
):
|
||||
create_openrouter_llm_client(mock_config)
|
||||
|
||||
# Verify LLMConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "sk-or-test-key-123"
|
||||
assert call_kwargs["model"] == "openai/gpt-4o"
|
||||
assert call_kwargs["base_url"] == "https://custom.openrouter.ai/api/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openrouter_llm_client_disables_reasoning(self, mock_config):
|
||||
"""Test create_openrouter_llm_client disables reasoning/verbosity for compatibility."""
|
||||
mock_client = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.llm_providers.openrouter_llm.OpenAIClient",
|
||||
return_value=mock_client,
|
||||
) as mock_openai_client:
|
||||
create_openrouter_llm_client(mock_config)
|
||||
|
||||
# OpenRouter should have reasoning=None, verbosity=None for compatibility
|
||||
call_kwargs = mock_openai_client.call_args.kwargs
|
||||
assert call_kwargs.get("reasoning") is None
|
||||
assert call_kwargs.get("verbosity") is None
|
||||
@@ -0,0 +1,246 @@
|
||||
"""
|
||||
Tests for integrations.graphiti.providers module.
|
||||
|
||||
Tests cover:
|
||||
- All re-exported items are accessible
|
||||
- __all__ exports match documentation
|
||||
- Module has proper docstring
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
class TestProvidersModuleReExports:
|
||||
"""Test that all items are properly re-exported from graphiti_providers."""
|
||||
|
||||
def test_import_provider_error(self):
|
||||
"""Test ProviderError is re-exported."""
|
||||
from integrations.graphiti.providers import ProviderError
|
||||
|
||||
assert ProviderError is not None
|
||||
assert Exception in ProviderError.__mro__
|
||||
|
||||
def test_import_provider_not_installed(self):
|
||||
"""Test ProviderNotInstalled is re-exported."""
|
||||
from integrations.graphiti.providers import ProviderNotInstalled
|
||||
|
||||
assert ProviderNotInstalled is not None
|
||||
assert Exception in ProviderNotInstalled.__mro__
|
||||
|
||||
def test_import_create_llm_client(self):
|
||||
"""Test create_llm_client is re-exported."""
|
||||
from integrations.graphiti.providers import create_llm_client
|
||||
|
||||
assert create_llm_client is not None
|
||||
assert callable(create_llm_client)
|
||||
|
||||
def test_import_create_embedder(self):
|
||||
"""Test create_embedder is re-exported."""
|
||||
from integrations.graphiti.providers import create_embedder
|
||||
|
||||
assert create_embedder is not None
|
||||
assert callable(create_embedder)
|
||||
|
||||
def test_import_create_cross_encoder(self):
|
||||
"""Test create_cross_encoder is re-exported."""
|
||||
from integrations.graphiti.providers import create_cross_encoder
|
||||
|
||||
assert create_cross_encoder is not None
|
||||
assert callable(create_cross_encoder)
|
||||
|
||||
def test_import_embedding_dimensions(self):
|
||||
"""Test EMBEDDING_DIMENSIONS is re-exported."""
|
||||
from integrations.graphiti.providers import EMBEDDING_DIMENSIONS
|
||||
|
||||
assert EMBEDDING_DIMENSIONS is not None
|
||||
assert isinstance(EMBEDDING_DIMENSIONS, dict)
|
||||
|
||||
def test_import_get_expected_embedding_dim(self):
|
||||
"""Test get_expected_embedding_dim is re-exported."""
|
||||
from integrations.graphiti.providers import get_expected_embedding_dim
|
||||
|
||||
assert get_expected_embedding_dim is not None
|
||||
assert callable(get_expected_embedding_dim)
|
||||
|
||||
def test_import_validate_embedding_config(self):
|
||||
"""Test validate_embedding_config is re-exported."""
|
||||
from integrations.graphiti.providers import validate_embedding_config
|
||||
|
||||
assert validate_embedding_config is not None
|
||||
assert callable(validate_embedding_config)
|
||||
|
||||
def test_import_test_llm_connection(self):
|
||||
"""Test test_llm_connection is re-exported."""
|
||||
from integrations.graphiti.providers import test_llm_connection
|
||||
|
||||
assert test_llm_connection is not None
|
||||
assert callable(test_llm_connection)
|
||||
|
||||
def test_import_test_embedder_connection(self):
|
||||
"""Test test_embedder_connection is re-exported."""
|
||||
from integrations.graphiti.providers import test_embedder_connection
|
||||
|
||||
assert test_embedder_connection is not None
|
||||
assert callable(test_embedder_connection)
|
||||
|
||||
def test_import_test_ollama_connection(self):
|
||||
"""Test test_ollama_connection is re-exported."""
|
||||
from integrations.graphiti.providers import test_ollama_connection
|
||||
|
||||
assert test_ollama_connection is not None
|
||||
assert callable(test_ollama_connection)
|
||||
|
||||
def test_import_is_graphiti_enabled(self):
|
||||
"""Test is_graphiti_enabled is re-exported."""
|
||||
from integrations.graphiti.providers import is_graphiti_enabled
|
||||
|
||||
assert is_graphiti_enabled is not None
|
||||
assert callable(is_graphiti_enabled)
|
||||
|
||||
def test_import_get_graph_hints(self):
|
||||
"""Test get_graph_hints is re-exported."""
|
||||
from integrations.graphiti.providers import get_graph_hints
|
||||
|
||||
assert get_graph_hints is not None
|
||||
assert callable(get_graph_hints)
|
||||
|
||||
|
||||
class TestProvidersModuleAll:
|
||||
"""Test __all__ exports match documented exports."""
|
||||
|
||||
def test___all___contains_all_exports(self):
|
||||
"""Test __all__ contains all expected exports."""
|
||||
import integrations.graphiti.providers as providers_module
|
||||
|
||||
expected_all = [
|
||||
# Exceptions
|
||||
"ProviderError",
|
||||
"ProviderNotInstalled",
|
||||
# Factory functions
|
||||
"create_llm_client",
|
||||
"create_embedder",
|
||||
"create_cross_encoder",
|
||||
# Models
|
||||
"EMBEDDING_DIMENSIONS",
|
||||
"get_expected_embedding_dim",
|
||||
# Validators
|
||||
"validate_embedding_config",
|
||||
"test_llm_connection",
|
||||
"test_embedder_connection",
|
||||
"test_ollama_connection",
|
||||
# Utilities
|
||||
"is_graphiti_enabled",
|
||||
"get_graph_hints",
|
||||
]
|
||||
|
||||
assert providers_module.__all__ == expected_all
|
||||
|
||||
def test_import_star_includes_all_exports(self):
|
||||
"""Test 'from integrations.graphiti.providers import *' works."""
|
||||
namespace = {}
|
||||
exec("from integrations.graphiti.providers import *", namespace)
|
||||
|
||||
# Verify all __all__ items are in the namespace
|
||||
import integrations.graphiti.providers as providers_module
|
||||
|
||||
for item in providers_module.__all__:
|
||||
assert item in namespace, f"{item} not found in namespace"
|
||||
|
||||
def test_all_exports_are_accessible(self):
|
||||
"""Test all items in __all__ are accessible."""
|
||||
import integrations.graphiti.providers as providers_module
|
||||
|
||||
for item in providers_module.__all__:
|
||||
assert hasattr(providers_module, item), f"{item} not accessible"
|
||||
|
||||
|
||||
class TestProvidersModuleDocumentation:
|
||||
"""Test module documentation."""
|
||||
|
||||
def test_module_has_docstring(self):
|
||||
"""Test the module has a docstring."""
|
||||
import integrations.graphiti.providers as providers_module
|
||||
|
||||
assert providers_module.__doc__ is not None
|
||||
assert len(providers_module.__doc__) > 0
|
||||
|
||||
def test_docstring_contains_key_terms(self):
|
||||
"""Test the docstring contains key terms."""
|
||||
import integrations.graphiti.providers as providers_module
|
||||
|
||||
docstring = providers_module.__doc__.lower()
|
||||
assert "provider" in docstring
|
||||
assert "graphiti" in docstring
|
||||
|
||||
|
||||
class TestProvidersModuleReExportBehavior:
|
||||
"""Test re-export behavior matches the source module."""
|
||||
|
||||
def test_create_llm_client_matches_source(self):
|
||||
"""Test create_llm_client is the same as the source."""
|
||||
from graphiti_providers import create_llm_client as source
|
||||
from integrations.graphiti.providers import create_llm_client as re_export
|
||||
|
||||
assert re_export is source
|
||||
|
||||
def test_create_embedder_matches_source(self):
|
||||
"""Test create_embedder is the same as the source."""
|
||||
from graphiti_providers import create_embedder as source
|
||||
from integrations.graphiti.providers import create_embedder as re_export
|
||||
|
||||
assert re_export is source
|
||||
|
||||
def test_exceptions_match_source(self):
|
||||
"""Test exceptions are the same as the source."""
|
||||
from graphiti_providers import ProviderError as source_error
|
||||
from graphiti_providers import ProviderNotInstalled as source_not_installed
|
||||
from integrations.graphiti.providers import (
|
||||
ProviderError as re_export_error,
|
||||
)
|
||||
from integrations.graphiti.providers import (
|
||||
ProviderNotInstalled as re_export_not_installed,
|
||||
)
|
||||
|
||||
assert re_export_error is source_error
|
||||
assert re_export_not_installed is source_not_installed
|
||||
|
||||
def test_embedding_dimensions_matches_source(self):
|
||||
"""Test EMBEDDING_DIMENSIONS is the same as the source."""
|
||||
from graphiti_providers import EMBEDDING_DIMENSIONS as source
|
||||
from integrations.graphiti.providers import EMBEDDING_DIMENSIONS as re_export
|
||||
|
||||
assert re_export is source
|
||||
|
||||
|
||||
class TestProvidersModuleIntegration:
|
||||
"""Integration tests for the providers module."""
|
||||
|
||||
def test_module_can_be_imported_multiple_times(self):
|
||||
"""Test the module can be imported multiple times without issues."""
|
||||
import importlib
|
||||
|
||||
import integrations.graphiti.providers
|
||||
|
||||
importlib.reload(integrations.graphiti.providers)
|
||||
|
||||
# Should still work
|
||||
from integrations.graphiti.providers import create_llm_client
|
||||
|
||||
assert create_llm_client is not None
|
||||
|
||||
def test_concurrent_imports(self):
|
||||
"""Test concurrent imports don't cause issues."""
|
||||
import concurrent.futures
|
||||
|
||||
def import_module():
|
||||
from integrations.graphiti.providers import create_llm_client
|
||||
|
||||
return create_llm_client
|
||||
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
|
||||
futures = [executor.submit(import_module) for _ in range(5)]
|
||||
results = [f.result() for f in concurrent.futures.as_completed(futures)]
|
||||
|
||||
# All should succeed
|
||||
assert len(results) == 5
|
||||
assert all(r is not None for r in results)
|
||||
@@ -0,0 +1,285 @@
|
||||
"""
|
||||
Unit tests for Ollama embedder provider.
|
||||
|
||||
Tests cover:
|
||||
- get_embedding_dim_for_model helper function
|
||||
- create_ollama_embedder factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder import (
|
||||
KNOWN_OLLAMA_EMBEDDING_MODELS,
|
||||
create_ollama_embedder,
|
||||
get_embedding_dim_for_model,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test get_embedding_dim_for_model
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGetEmbeddingDimForModel:
|
||||
"""Test get_embedding_dim_for_model helper function."""
|
||||
|
||||
def test_get_embedding_dim_for_model_exact_match(self):
|
||||
"""Test get_embedding_dim_for_model with exact model match."""
|
||||
result = get_embedding_dim_for_model("nomic-embed-text")
|
||||
assert result == 768
|
||||
|
||||
def test_get_embedding_dim_for_model_with_tag(self):
|
||||
"""Test get_embedding_dim_for_model with tagged model."""
|
||||
result = get_embedding_dim_for_model("qwen3-embedding:8b")
|
||||
assert result == 4096
|
||||
|
||||
def test_get_embedding_dim_for_model_base_name_fallback(self):
|
||||
"""Test get_embedding_dim_for_model falls back to base name."""
|
||||
result = get_embedding_dim_for_model("nomic-embed-text:custom-tag")
|
||||
assert result == 768 # Should use base model dimension
|
||||
|
||||
def test_get_embedding_dim_for_model_configured_dim_override(self):
|
||||
"""Test get_embedding_dim_for_model with configured dimension override."""
|
||||
result = get_embedding_dim_for_model("unknown-model", configured_dim=512)
|
||||
assert result == 512
|
||||
|
||||
def test_get_embedding_dim_for_model_unknown_model(self):
|
||||
"""Test get_embedding_dim_for_model raises ProviderError for unknown model."""
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
get_embedding_dim_for_model("totally-unknown-model")
|
||||
|
||||
assert "Unknown Ollama embedding model" in str(exc_info.value)
|
||||
assert "totally-unknown-model" in str(exc_info.value)
|
||||
assert "OLLAMA_EMBEDDING_DIM" in str(exc_info.value)
|
||||
|
||||
def test_get_embedding_dim_for_model_configured_dim_zero(self):
|
||||
"""Test get_embedding_dim_for_model ignores zero configured dimension."""
|
||||
# When configured_dim is 0, should use known model dimension
|
||||
result = get_embedding_dim_for_model("nomic-embed-text", configured_dim=0)
|
||||
assert result == 768
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test KNOWN_OLLAMA_EMBEDDING_MODELS constant
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestKnownOllamaEmbeddingModels:
|
||||
"""Test KNOWN_OLLAMA_EMBEDDING_MODELS constant."""
|
||||
|
||||
def test_known_models_contains_expected_entries(self):
|
||||
"""Test KNOWN_OLLAMA_EMBEDDING_MODELS has expected models."""
|
||||
expected_models = [
|
||||
"embeddinggemma",
|
||||
"qwen3-embedding",
|
||||
"nomic-embed-text",
|
||||
"mxbai-embed-large",
|
||||
"bge-large",
|
||||
"all-minilm",
|
||||
]
|
||||
|
||||
for model in expected_models:
|
||||
# Check if base model exists (without tag)
|
||||
base_found = any(
|
||||
key.startswith(model) for key in KNOWN_OLLAMA_EMBEDDING_MODELS.keys()
|
||||
)
|
||||
assert base_found, (
|
||||
f"Model {model} not found in KNOWN_OLLAMA_EMBEDDING_MODELS"
|
||||
)
|
||||
|
||||
def test_known_models_dimensions_are_positive(self):
|
||||
"""Test all dimensions in KNOWN_OLLAMA_EMBEDDING_MODELS are positive integers."""
|
||||
for model, dimension in KNOWN_OLLAMA_EMBEDDING_MODELS.items():
|
||||
assert isinstance(dimension, int), f"Dimension for {model} is not int"
|
||||
assert dimension > 0, f"Dimension for {model} is not positive: {dimension}"
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test create_ollama_embedder
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateOllamaEmbedder:
|
||||
"""Test create_ollama_embedder factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.ollama_embedding_model = "nomic-embed-text"
|
||||
config.ollama_embedding_dim = None
|
||||
config.ollama_base_url = "http://localhost:11434"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_embedder_success(self, mock_config):
|
||||
"""Test create_ollama_embedder returns embedder with valid config."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
result = create_ollama_embedder(mock_config)
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_ollama_embedder_success_fast(self, mock_config):
|
||||
"""Fast test for create_ollama_embedder success path."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
# Set embedding_dim to 0 to allow auto-detection
|
||||
mock_config.ollama_embedding_dim = 0
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.embedder": MagicMock(),
|
||||
"graphiti_core.embedder.openai": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.embedder.openai import OpenAIEmbedder
|
||||
|
||||
OpenAIEmbedder.return_value = mock_embedder
|
||||
|
||||
result = create_ollama_embedder(mock_config)
|
||||
|
||||
# Verify the embedder was created and returned
|
||||
OpenAIEmbedder.assert_called_once()
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_ollama_embedder_missing_model(self, mock_config):
|
||||
"""Test create_ollama_embedder raises ProviderError for missing model."""
|
||||
mock_config.ollama_embedding_model = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
assert "OLLAMA_EMBEDDING_MODEL" in str(exc_info.value)
|
||||
|
||||
def test_create_ollama_embedder_import_error(self, mock_config):
|
||||
"""Test create_ollama_embedder raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
# Only block the specific import that create_ollama_embedder uses
|
||||
if name == "graphiti_core.embedder.openai" or name.startswith(
|
||||
"graphiti_core.embedder.openai."
|
||||
):
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_embedder_base_url_without_v1(self, mock_config):
|
||||
"""Test create_ollama_embedder appends /v1 to base URL if missing."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
# Verify base_url has /v1 appended
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_embedder_base_url_with_v1(self, mock_config):
|
||||
"""Test create_ollama_embedder doesn't duplicate /v1 in base URL."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434/v1"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
# Verify base_url is not duplicated
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_embedder_base_url_with_trailing_slash(self, mock_config):
|
||||
"""Test create_ollama_embedder handles trailing slash correctly."""
|
||||
mock_config.ollama_base_url = "http://localhost:11434/"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
# Verify trailing slash is handled
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["base_url"] == "http://localhost:11434/v1"
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_embedder_passes_config_correctly(self, mock_config):
|
||||
"""Test create_ollama_embedder passes config values correctly."""
|
||||
mock_config.ollama_embedding_model = "mxbai-embed-large"
|
||||
mock_config.ollama_embedding_dim = None
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
# Verify OpenAIEmbedderConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "ollama"
|
||||
assert call_kwargs["embedding_model"] == "mxbai-embed-large"
|
||||
assert (
|
||||
call_kwargs["embedding_dim"] == 1024
|
||||
) # Known dimension for mxbai-embed-large
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_ollama_embedder_with_configured_dimension(self, mock_config):
|
||||
"""Test create_ollama_embedder uses configured dimension when set."""
|
||||
mock_config.ollama_embedding_dim = 512
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.ollama_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_ollama_embedder(mock_config)
|
||||
|
||||
# Verify configured dimension is used
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["embedding_dim"] == 512
|
||||
@@ -0,0 +1,117 @@
|
||||
"""
|
||||
Unit tests for OpenAI embedder provider.
|
||||
|
||||
Tests cover:
|
||||
- create_openai_embedder factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.embedder_providers.openai_embedder import (
|
||||
create_openai_embedder,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_openai_embedder
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateOpenAIEmbedder:
|
||||
"""Test create_openai_embedder factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.openai_api_key = "sk-test-key"
|
||||
config.openai_embedding_model = "text-embedding-3-small"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openai_embedder_success(self, mock_config):
|
||||
"""Test create_openai_embedder returns embedder with valid config."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.openai_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
result = create_openai_embedder(mock_config)
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_openai_embedder_success_fast(self, mock_config):
|
||||
"""Fast test for create_openai_embedder success path."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.embedder": MagicMock(),
|
||||
"graphiti_core.embedder.openai": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.embedder.openai import OpenAIEmbedder
|
||||
|
||||
OpenAIEmbedder.return_value = mock_embedder
|
||||
|
||||
result = create_openai_embedder(mock_config)
|
||||
|
||||
# Verify the embedder was created and returned
|
||||
OpenAIEmbedder.assert_called_once()
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_openai_embedder_missing_api_key(self, mock_config):
|
||||
"""Test create_openai_embedder raises ProviderError for missing API key."""
|
||||
mock_config.openai_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_openai_embedder(mock_config)
|
||||
|
||||
assert "OPENAI_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_openai_embedder_import_error(self, mock_config):
|
||||
"""Test create_openai_embedder raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name.startswith("graphiti_core.embedder"):
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_openai_embedder(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openai_embedder_passes_config_correctly(self, mock_config):
|
||||
"""Test create_openai_embedder passes config values correctly."""
|
||||
mock_config.openai_api_key = "sk-test-key-123"
|
||||
mock_config.openai_embedding_model = "text-embedding-3-large"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.openai_embedder.OpenAIEmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.openai_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_openai_embedder(mock_config)
|
||||
|
||||
# Verify OpenAIEmbedderConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "sk-test-key-123"
|
||||
assert call_kwargs["embedding_model"] == "text-embedding-3-large"
|
||||
@@ -0,0 +1,129 @@
|
||||
"""
|
||||
Unit tests for OpenRouter embedder provider.
|
||||
|
||||
Tests cover:
|
||||
- create_openrouter_embedder factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.embedder_providers.openrouter_embedder import (
|
||||
create_openrouter_embedder,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_openrouter_embedder
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateOpenRouterEmbedder:
|
||||
"""Test create_openrouter_embedder factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.openrouter_api_key = "sk-or-test-key"
|
||||
config.openrouter_embedding_model = "openai/text-embedding-3-small"
|
||||
config.openrouter_base_url = "https://openrouter.ai/api/v1"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openrouter_embedder_success(self, mock_config):
|
||||
"""Test create_openrouter_embedder returns embedder with valid config."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.openrouter_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
result = create_openrouter_embedder(mock_config)
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_openrouter_embedder_success_fast(self, mock_config):
|
||||
"""Fast test for create_openrouter_embedder success path."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.embedder": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.embedder import OpenAIEmbedder
|
||||
|
||||
OpenAIEmbedder.return_value = mock_embedder
|
||||
|
||||
result = create_openrouter_embedder(mock_config)
|
||||
|
||||
# Verify the embedder was created and returned
|
||||
OpenAIEmbedder.assert_called_once()
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_openrouter_embedder_missing_api_key(self, mock_config):
|
||||
"""Test create_openrouter_embedder raises ProviderError for missing API key."""
|
||||
|
||||
mock_graphiti_core_embedder = MagicMock()
|
||||
mock_graphiti_core_embedder.EmbedderConfig = MagicMock
|
||||
mock_graphiti_core_embedder.OpenAIEmbedder = MagicMock
|
||||
|
||||
# Mock the graphiti_core.embedder module to allow import to succeed
|
||||
with patch.dict(
|
||||
sys.modules, {"graphiti_core.embedder": mock_graphiti_core_embedder}
|
||||
):
|
||||
mock_config.openrouter_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_openrouter_embedder(mock_config)
|
||||
|
||||
assert "OPENROUTER_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_openrouter_embedder_import_error(self, mock_config):
|
||||
"""Test create_openrouter_embedder raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name.startswith("graphiti_core.embedder"):
|
||||
raise ImportError("graphiti-core not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_openrouter_embedder(mock_config)
|
||||
|
||||
assert "graphiti-core" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_openrouter_embedder_passes_config_correctly(self, mock_config):
|
||||
"""Test create_openrouter_embedder passes config values correctly."""
|
||||
mock_config.openrouter_api_key = "sk-or-test-key-123"
|
||||
mock_config.openrouter_embedding_model = "voyage/voyage-3"
|
||||
mock_config.openrouter_base_url = "https://custom.openrouter.ai/api/v1"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.openrouter_embedder.EmbedderConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"integrations.graphiti.providers_pkg.embedder_providers.openrouter_embedder.OpenAIEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_openrouter_embedder(mock_config)
|
||||
|
||||
# Verify EmbedderConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "sk-or-test-key-123"
|
||||
assert call_kwargs["model"] == "voyage/voyage-3"
|
||||
assert call_kwargs["base_url"] == "https://custom.openrouter.ai/api/v1"
|
||||
@@ -0,0 +1,128 @@
|
||||
"""
|
||||
Unit tests for Voyage AI embedder provider.
|
||||
|
||||
Tests cover:
|
||||
- create_voyage_embedder factory function
|
||||
- ProviderNotInstalled exception handling
|
||||
- ProviderError for missing configuration
|
||||
"""
|
||||
|
||||
import sys
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.providers_pkg.embedder_providers.voyage_embedder import (
|
||||
create_voyage_embedder,
|
||||
)
|
||||
from integrations.graphiti.providers_pkg.exceptions import (
|
||||
ProviderError,
|
||||
ProviderNotInstalled,
|
||||
)
|
||||
|
||||
# =============================================================================
|
||||
# Test create_voyage_embedder
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestCreateVoyageEmbedder:
|
||||
"""Test create_voyage_embedder factory function."""
|
||||
|
||||
@pytest.fixture
|
||||
def mock_config(self):
|
||||
"""Create a mock GraphitiConfig."""
|
||||
config = MagicMock()
|
||||
config.voyage_api_key = "test-voyage-key"
|
||||
config.voyage_embedding_model = "voyage-3"
|
||||
return config
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_voyage_embedder_success(self, mock_config):
|
||||
"""Test create_voyage_embedder returns embedder with valid config."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"graphiti_core.embedder.voyage.VoyageEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
result = create_voyage_embedder(mock_config)
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_voyage_embedder_success_fast(self, mock_config):
|
||||
"""Fast test for create_voyage_embedder success path."""
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
# Mock the graphiti_core imports
|
||||
with patch.dict(
|
||||
"sys.modules",
|
||||
{
|
||||
"graphiti_core": MagicMock(),
|
||||
"graphiti_core.embedder": MagicMock(),
|
||||
"graphiti_core.embedder.voyage": MagicMock(),
|
||||
},
|
||||
):
|
||||
from graphiti_core.embedder.voyage import VoyageEmbedder
|
||||
|
||||
VoyageEmbedder.return_value = mock_embedder
|
||||
|
||||
result = create_voyage_embedder(mock_config)
|
||||
|
||||
# Verify the embedder was created and returned
|
||||
VoyageEmbedder.assert_called_once()
|
||||
assert result == mock_embedder
|
||||
|
||||
def test_create_voyage_embedder_missing_api_key(self, mock_config):
|
||||
"""Test create_voyage_embedder raises ProviderError for missing API key."""
|
||||
|
||||
mock_voyage = MagicMock()
|
||||
mock_voyage.VoyageAIConfig = MagicMock()
|
||||
mock_voyage.VoyageEmbedder = MagicMock()
|
||||
|
||||
# Clear sys.modules cache to ensure fresh import
|
||||
sys.modules.pop("graphiti_core.embedder.voyage", None)
|
||||
|
||||
# Mock the voyage module to allow import to succeed
|
||||
with patch.dict(sys.modules, {"graphiti_core.embedder.voyage": mock_voyage}):
|
||||
mock_config.voyage_api_key = None
|
||||
|
||||
with pytest.raises(ProviderError) as exc_info:
|
||||
create_voyage_embedder(mock_config)
|
||||
|
||||
assert "VOYAGE_API_KEY" in str(exc_info.value)
|
||||
|
||||
def test_create_voyage_embedder_import_error(self, mock_config):
|
||||
"""Test create_voyage_embedder raises ProviderNotInstalled on ImportError."""
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name.startswith("graphiti_core.embedder.voyage"):
|
||||
raise ImportError("graphiti-core[voyage] not installed")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
with pytest.raises(ProviderNotInstalled) as exc_info:
|
||||
create_voyage_embedder(mock_config)
|
||||
|
||||
assert "graphiti-core[voyage]" in str(exc_info.value)
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_create_voyage_embedder_passes_config_correctly(self, mock_config):
|
||||
"""Test create_voyage_embedder passes config values correctly."""
|
||||
mock_config.voyage_api_key = "test-voyage-key-123"
|
||||
mock_config.voyage_embedding_model = "voyage-3-lite"
|
||||
mock_embedder = MagicMock()
|
||||
|
||||
with patch(
|
||||
"graphiti_core.embedder.voyage.VoyageAIConfig",
|
||||
) as mock_config_class:
|
||||
with patch(
|
||||
"graphiti_core.embedder.voyage.VoyageEmbedder",
|
||||
return_value=mock_embedder,
|
||||
):
|
||||
create_voyage_embedder(mock_config)
|
||||
|
||||
# Verify VoyageAIConfig was called with correct arguments
|
||||
call_kwargs = mock_config_class.call_args.kwargs
|
||||
assert call_kwargs["api_key"] == "test-voyage-key-123"
|
||||
assert call_kwargs["embedding_model"] == "voyage-3-lite"
|
||||
@@ -0,0 +1,783 @@
|
||||
"""
|
||||
Tests for GraphitiQueries class.
|
||||
|
||||
Tests cover:
|
||||
- GraphitiQueries initialization
|
||||
- add_session_insight()
|
||||
- add_codebase_discoveries()
|
||||
- add_pattern()
|
||||
- add_gotcha()
|
||||
- add_task_outcome()
|
||||
- add_structured_insights()
|
||||
"""
|
||||
|
||||
import json
|
||||
from datetime import datetime
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
# =============================================================================
|
||||
# Mock External Dependencies
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_graphiti_core_nodes():
|
||||
"""Auto-mock graphiti_core for all tests."""
|
||||
import sys
|
||||
|
||||
# Patch graphiti_core at module level before import
|
||||
mock_graphiti_core = MagicMock()
|
||||
mock_nodes = MagicMock()
|
||||
mock_episode_type = MagicMock()
|
||||
mock_episode_type.text = "text"
|
||||
mock_nodes.EpisodeType = mock_episode_type
|
||||
mock_graphiti_core.nodes = mock_nodes
|
||||
|
||||
sys.modules["graphiti_core"] = mock_graphiti_core
|
||||
sys.modules["graphiti_core.nodes"] = mock_nodes
|
||||
|
||||
try:
|
||||
yield mock_episode_type
|
||||
finally:
|
||||
# Clean up - always run even if test fails
|
||||
sys.modules.pop("graphiti_core", None)
|
||||
sys.modules.pop("graphiti_core.nodes", None)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Client and Queries Fixtures
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_client():
|
||||
"""Create a mock GraphitiClient."""
|
||||
client = MagicMock()
|
||||
client.graphiti = MagicMock()
|
||||
client.graphiti.add_episode = AsyncMock()
|
||||
return client
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def queries(mock_client):
|
||||
"""Create a GraphitiQueries instance."""
|
||||
from integrations.graphiti.queries_pkg.queries import GraphitiQueries
|
||||
|
||||
return GraphitiQueries(
|
||||
client=mock_client,
|
||||
group_id="test_group",
|
||||
spec_context_id="test_spec",
|
||||
)
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Test Classes
|
||||
# =============================================================================
|
||||
|
||||
|
||||
class TestGraphitiQueriesInit:
|
||||
"""Test GraphitiQueries initialization."""
|
||||
|
||||
def test_init_sets_attributes(self, mock_client):
|
||||
"""Test constructor sets all attributes correctly."""
|
||||
from integrations.graphiti.queries_pkg.queries import GraphitiQueries
|
||||
|
||||
queries = GraphitiQueries(
|
||||
client=mock_client,
|
||||
group_id="my_group",
|
||||
spec_context_id="my_spec",
|
||||
)
|
||||
|
||||
assert queries.client == mock_client
|
||||
assert queries.group_id == "my_group"
|
||||
assert queries.spec_context_id == "my_spec"
|
||||
|
||||
|
||||
class TestAddSessionInsight:
|
||||
"""Test add_session_insight method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_session_insight_success(self, queries):
|
||||
"""Test successful session insight save."""
|
||||
insights = {
|
||||
"subtasks_completed": ["task-1", "task-2"],
|
||||
"discoveries": {"files_understood": {}},
|
||||
"what_worked": ["Using pytest"],
|
||||
"what_failed": [],
|
||||
}
|
||||
|
||||
result = await queries.add_session_insight(session_num=1, insights=insights)
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_called_once()
|
||||
|
||||
# Verify episode format
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
assert "session_001_test_spec" in call_args[1]["name"]
|
||||
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["type"] == "session_insight"
|
||||
assert episode_body["session_number"] == 1
|
||||
assert episode_body["spec_id"] == "test_spec"
|
||||
assert "subtasks_completed" in episode_body
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_session_insight_exception(self, queries):
|
||||
"""Test exception handling in add_session_insight."""
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Database error")
|
||||
|
||||
result = await queries.add_session_insight(session_num=1, insights={})
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAddCodebaseDiscoveries:
|
||||
"""Test add_codebase_discoveries method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_codebase_discoveries_empty_dict(self, queries):
|
||||
"""Test empty discoveries returns True without calling add_episode."""
|
||||
result = await queries.add_codebase_discoveries({})
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_codebase_discoveries_success(self, queries):
|
||||
"""Test successful codebase discoveries save."""
|
||||
discoveries = {
|
||||
"src/main.py": "Entry point for the application",
|
||||
"src/config.py": "Configuration module",
|
||||
}
|
||||
|
||||
result = await queries.add_codebase_discoveries(discoveries)
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_called_once()
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["type"] == "codebase_discovery"
|
||||
assert episode_body["files"] == discoveries
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_codebase_discoveries_exception(self, queries):
|
||||
"""Test exception handling in add_codebase_discoveries."""
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Database error")
|
||||
|
||||
result = await queries.add_codebase_discoveries({"file.py": "desc"})
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAddPattern:
|
||||
"""Test add_pattern method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_pattern_success(self, queries):
|
||||
"""Test successful pattern save."""
|
||||
pattern = "Use dependency injection for database connections"
|
||||
|
||||
result = await queries.add_pattern(pattern)
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_called_once()
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["type"] == "pattern"
|
||||
assert episode_body["pattern"] == pattern
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_pattern_exception(self, queries):
|
||||
"""Test exception handling in add_pattern."""
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Database error")
|
||||
|
||||
result = await queries.add_pattern("test pattern")
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAddGotcha:
|
||||
"""Test add_gotcha method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_gotcha_success(self, queries):
|
||||
"""Test successful gotcha save."""
|
||||
gotcha = "Always close database connections in finally blocks"
|
||||
|
||||
result = await queries.add_gotcha(gotcha)
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_called_once()
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["type"] == "gotcha"
|
||||
assert episode_body["gotcha"] == gotcha
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_gotcha_exception(self, queries):
|
||||
"""Test exception handling in add_gotcha."""
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Database error")
|
||||
|
||||
result = await queries.add_gotcha("test gotcha")
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAddTaskOutcome:
|
||||
"""Test add_task_outcome method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_task_outcome_success(self, queries):
|
||||
"""Test successful task outcome save."""
|
||||
result = await queries.add_task_outcome(
|
||||
task_id="task-123",
|
||||
success=True,
|
||||
outcome="Implementation completed successfully",
|
||||
metadata={"duration": 120},
|
||||
)
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_called_once()
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["type"] == "task_outcome"
|
||||
assert episode_body["task_id"] == "task-123"
|
||||
assert episode_body["success"] is True
|
||||
assert episode_body["outcome"] == "Implementation completed successfully"
|
||||
assert episode_body["duration"] == 120
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_task_outcome_without_metadata(self, queries):
|
||||
"""Test task outcome save without metadata."""
|
||||
result = await queries.add_task_outcome(
|
||||
task_id="task-456",
|
||||
success=False,
|
||||
outcome="Failed due to timeout",
|
||||
)
|
||||
|
||||
assert result is True
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["task_id"] == "task-456"
|
||||
assert episode_body["success"] is False
|
||||
assert episode_body["outcome"] == "Failed due to timeout"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_task_outcome_exception(self, queries):
|
||||
"""Test exception handling in add_task_outcome."""
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Database error")
|
||||
|
||||
result = await queries.add_task_outcome("task-1", True, "success")
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAddStructuredInsights:
|
||||
"""Test add_structured_insights method."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_empty_dict(self, queries):
|
||||
"""Test empty insights returns True."""
|
||||
result = await queries.add_structured_insights({})
|
||||
|
||||
assert result is True
|
||||
queries.client.graphiti.add_episode.assert_not_called()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_with_file_insights(self, queries):
|
||||
"""Test structured insights with file insights."""
|
||||
insights = {
|
||||
"file_insights": [
|
||||
{
|
||||
"path": "src/main.py",
|
||||
"purpose": "Entry point",
|
||||
"changes_made": "Added error handling",
|
||||
"patterns_used": ["error boundaries"],
|
||||
"gotchas": ["needs timeout"],
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
assert queries.client.graphiti.add_episode.call_count == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_with_patterns(self, queries):
|
||||
"""Test structured insights with discovered patterns."""
|
||||
insights = {
|
||||
"patterns_discovered": [
|
||||
{
|
||||
"pattern": "Use factory pattern for object creation",
|
||||
"applies_to": "Complex object initialization",
|
||||
"example": "src/factory.py",
|
||||
},
|
||||
"Simple pattern string", # Test non-dict pattern
|
||||
]
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
assert queries.client.graphiti.add_episode.call_count == 2
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_with_gotchas(self, queries):
|
||||
"""Test structured insights with discovered gotchas."""
|
||||
insights = {
|
||||
"gotchas_discovered": [
|
||||
{
|
||||
"gotcha": "Don't use mutable default arguments",
|
||||
"trigger": "Function definition with [] as default",
|
||||
"solution": "Use None and check in function body",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_with_outcome(self, queries):
|
||||
"""Test structured insights with approach outcome."""
|
||||
insights = {
|
||||
"subtask_id": "task-1",
|
||||
"approach_outcome": {
|
||||
"success": True,
|
||||
"approach_used": "Used Graphiti for memory",
|
||||
"why_it_worked": "Efficient semantic search",
|
||||
"alternatives_tried": ["PostgreSQL"],
|
||||
},
|
||||
"changed_files": ["src/memory.py"],
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_with_recommendations(self, queries):
|
||||
"""Test structured insights with recommendations."""
|
||||
insights = {
|
||||
"subtask_id": "task-2",
|
||||
"recommendations": [
|
||||
"Add error handling",
|
||||
"Improve test coverage",
|
||||
],
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_handles_duplicate_facts_error(self, queries):
|
||||
"""Test that duplicate_facts error is handled as non-fatal."""
|
||||
insights = {"file_insights": [{"path": "src/test.py", "purpose": "Test file"}]}
|
||||
|
||||
# First call fails with duplicate_facts, second succeeds
|
||||
queries.client.graphiti.add_episode.side_effect = [
|
||||
Exception("invalid duplicate_facts idx"),
|
||||
None, # Second call succeeds
|
||||
]
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_string_pattern(self, queries):
|
||||
"""Test string pattern (non-dict) handling."""
|
||||
insights = {"patterns_discovered": ["Simple string pattern"]}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["pattern"] == "Simple string pattern"
|
||||
assert episode_body["applies_to"] == ""
|
||||
assert episode_body["example"] == ""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_string_gotcha(self, queries):
|
||||
"""Test string gotcha (non-dict) handling."""
|
||||
insights = {"gotchas_discovered": ["Simple string gotcha"]}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["gotcha"] == "Simple string gotcha"
|
||||
assert episode_body["trigger"] == ""
|
||||
assert episode_body["solution"] == ""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_file_insight_with_all_fields(self, queries):
|
||||
"""Test file insight with all optional fields."""
|
||||
insights = {
|
||||
"file_insights": [
|
||||
{
|
||||
"path": "src/test.py",
|
||||
"purpose": "Test module",
|
||||
"changes_made": "Added new tests",
|
||||
"patterns_used": ["pattern1", "pattern2"],
|
||||
"gotchas": ["gotcha1", "gotcha2"],
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["file_path"] == "src/test.py"
|
||||
assert episode_body["purpose"] == "Test module"
|
||||
assert episode_body["changes_made"] == "Added new tests"
|
||||
assert episode_body["patterns_used"] == ["pattern1", "pattern2"]
|
||||
assert episode_body["gotchas"] == ["gotcha1", "gotcha2"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_gotcha_non_duplicate_exception(
|
||||
self, queries
|
||||
):
|
||||
"""Test gotcha save with non-duplicate_facts exception."""
|
||||
insights = {"gotchas_discovered": [{"gotcha": "Test gotcha"}]}
|
||||
|
||||
# Raise non-duplicate error
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Other error")
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return False since all saves failed
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_gotcha_duplicate_facts_exception(
|
||||
self, queries
|
||||
):
|
||||
"""Test gotcha save with duplicate_facts exception (lines 418-419)."""
|
||||
insights = {"gotchas_discovered": [{"gotcha": "Test gotcha"}]}
|
||||
|
||||
# Raise duplicate_facts error (should be counted as success)
|
||||
queries.client.graphiti.add_episode.side_effect = Exception(
|
||||
"invalid duplicate_facts idx"
|
||||
)
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return True because duplicate_facts is non-fatal
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_outcome_non_duplicate_exception(
|
||||
self, queries
|
||||
):
|
||||
"""Test outcome save with non-duplicate_facts exception."""
|
||||
insights = {
|
||||
"subtask_id": "task-1",
|
||||
"approach_outcome": {"success": True, "approach_used": "Test approach"},
|
||||
}
|
||||
|
||||
# Raise non-duplicate error
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Other error")
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return False since all saves failed
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_outcome_duplicate_facts_exception(
|
||||
self, queries
|
||||
):
|
||||
"""Test outcome save with duplicate_facts exception (lines 457-458)."""
|
||||
insights = {
|
||||
"subtask_id": "task-1",
|
||||
"approach_outcome": {"success": True, "approach_used": "Test approach"},
|
||||
}
|
||||
|
||||
# Raise duplicate_facts error (should be counted as success)
|
||||
queries.client.graphiti.add_episode.side_effect = Exception(
|
||||
"invalid duplicate_facts idx"
|
||||
)
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return True because duplicate_facts is non-fatal
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_recommendations_non_duplicate_exception(
|
||||
self, queries
|
||||
):
|
||||
"""Test recommendations save with non-duplicate_facts exception."""
|
||||
insights = {"subtask_id": "task-1", "recommendations": ["Test recommendation"]}
|
||||
|
||||
# Raise non-duplicate error
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Other error")
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return False since all saves failed
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_recommendations_duplicate_facts_exception(
|
||||
self, queries
|
||||
):
|
||||
"""Test recommendations save with duplicate_facts exception (lines 488-489)."""
|
||||
insights = {"subtask_id": "task-1", "recommendations": ["Test recommendation"]}
|
||||
|
||||
# Raise duplicate_facts error (should be counted as success)
|
||||
queries.client.graphiti.add_episode.side_effect = Exception(
|
||||
"invalid duplicate_facts idx"
|
||||
)
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return True because duplicate_facts is non-fatal
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_top_level_exception_with_content(
|
||||
self, queries
|
||||
):
|
||||
"""Test top-level exception with insights content."""
|
||||
insights = {
|
||||
"file_insights": [{"path": "test.py", "purpose": "test"}],
|
||||
"patterns_discovered": [{"pattern": "test pattern"}],
|
||||
"gotchas_discovered": [{"gotcha": "test gotcha"}],
|
||||
"approach_outcome": {"success": True},
|
||||
"recommendations": ["test recommendation"],
|
||||
}
|
||||
|
||||
# Mock exception during processing
|
||||
with patch(
|
||||
"integrations.graphiti.queries_pkg.queries.json.dumps",
|
||||
side_effect=Exception("JSON error"),
|
||||
):
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_outer_exception_handler(self, queries):
|
||||
"""Test outer exception handler for add_structured_insights (lines 499-523)."""
|
||||
insights = {
|
||||
"file_insights": [{"path": "test.py", "purpose": "test"}],
|
||||
"patterns_discovered": [{"pattern": "Test pattern"}],
|
||||
"gotchas_discovered": [{"gotcha": "Test gotcha"}],
|
||||
"approach_outcome": {"success": True, "approach_used": "Test approach"},
|
||||
"recommendations": ["Test recommendation"],
|
||||
}
|
||||
|
||||
# Mock EpisodeType import to fail, triggering outer exception handler
|
||||
import builtins
|
||||
|
||||
original_import = builtins.__import__
|
||||
|
||||
def mock_import(name, *args, **kwargs):
|
||||
if name == "graphiti_core.nodes":
|
||||
raise ImportError("EpisodeType not available")
|
||||
return original_import(name, *args, **kwargs)
|
||||
|
||||
with patch("builtins.__import__", side_effect=mock_import):
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return False and trigger outer exception handler
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_all_fail(self, queries):
|
||||
"""Test when all episode saves fail."""
|
||||
insights = {"file_insights": [{"path": "test.py", "purpose": "test"}]}
|
||||
|
||||
queries.client.graphiti.add_episode.side_effect = Exception("Total failure")
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is False
|
||||
|
||||
|
||||
class TestAddStructuredInsightsExceptionHandling:
|
||||
"""Test add_structured_insights exception handling branches."""
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
"insights_key,insights_value",
|
||||
[
|
||||
("patterns_discovered", [{"pattern": "Test pattern"}]),
|
||||
("gotchas_discovered", [{"gotcha": "Test gotcha"}]),
|
||||
(
|
||||
"approach_outcome",
|
||||
{
|
||||
"subtask_id": "task-1",
|
||||
"success": True,
|
||||
"approach_used": "Test approach",
|
||||
},
|
||||
),
|
||||
(
|
||||
"recommendations",
|
||||
{"subtask_id": "task-1", "recommendations": ["Test recommendation"]},
|
||||
),
|
||||
],
|
||||
)
|
||||
async def test_add_structured_insights_non_duplicate_exception(
|
||||
self, queries, insights_key, insights_value
|
||||
):
|
||||
"""Test exception handling for non-duplicate errors across different insight types."""
|
||||
insights = {insights_key: insights_value}
|
||||
|
||||
queries.client.graphiti.add_episode.side_effect = Exception(
|
||||
"Non-duplicate error"
|
||||
)
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_top_level_exception(self, queries):
|
||||
"""Test top-level exception handling in add_structured_insights."""
|
||||
insights = {"file_insights": [{"path": "test.py", "purpose": "test"}]}
|
||||
|
||||
# Simulate exception during JSON serialization
|
||||
with patch(
|
||||
"integrations.graphiti.queries_pkg.queries.json.dumps",
|
||||
side_effect=Exception("JSON error"),
|
||||
):
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is False
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_mixed_success_failure(self, queries):
|
||||
"""Test mixed success and failure in structured insights."""
|
||||
insights = {
|
||||
"file_insights": [
|
||||
{"path": "test1.py", "purpose": "test1"},
|
||||
{"path": "test2.py", "purpose": "test2"},
|
||||
]
|
||||
}
|
||||
|
||||
# First succeeds, second fails with non-duplicate error
|
||||
queries.client.graphiti.add_episode.side_effect = [
|
||||
None, # First succeeds
|
||||
Exception("Non-duplicate error"), # Second fails
|
||||
]
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return True because at least one succeeded
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_all_patterns_fail_with_duplicate(
|
||||
self, queries
|
||||
):
|
||||
"""Test all pattern saves fail with duplicate_facts error."""
|
||||
insights = {
|
||||
"patterns_discovered": [{"pattern": "Pattern 1"}, {"pattern": "Pattern 2"}]
|
||||
}
|
||||
|
||||
# Both fail with duplicate_facts error (should be counted as success)
|
||||
queries.client.graphiti.add_episode.side_effect = [
|
||||
Exception("invalid duplicate_facts idx"),
|
||||
Exception("invalid duplicate_facts idx"),
|
||||
]
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
# Should return True because duplicate_facts is non-fatal
|
||||
assert result is True
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_dict_pattern_with_all_fields(self, queries):
|
||||
"""Test dict pattern with applies_to and example fields."""
|
||||
insights = {
|
||||
"patterns_discovered": [
|
||||
{
|
||||
"pattern": "Factory pattern",
|
||||
"applies_to": "Object creation",
|
||||
"example": "src/factory.py",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
assert queries.client.graphiti.add_episode.call_count == 1
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["pattern"] == "Factory pattern"
|
||||
assert episode_body["applies_to"] == "Object creation"
|
||||
assert episode_body["example"] == "src/factory.py"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_dict_gotcha_with_all_fields(self, queries):
|
||||
"""Test dict gotcha with trigger and solution fields."""
|
||||
insights = {
|
||||
"gotchas_discovered": [
|
||||
{
|
||||
"gotcha": "Mutable default args",
|
||||
"trigger": "Function with [] as default",
|
||||
"solution": "Use None and check in body",
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["gotcha"] == "Mutable default args"
|
||||
assert episode_body["trigger"] == "Function with [] as default"
|
||||
assert episode_body["solution"] == "Use None and check in body"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_structured_insights_outcome_with_all_fields(self, queries):
|
||||
"""Test outcome with all optional fields."""
|
||||
insights = {
|
||||
"subtask_id": "task-1",
|
||||
"approach_outcome": {
|
||||
"success": True,
|
||||
"approach_used": "Test approach",
|
||||
"why_it_worked": "Because reasons",
|
||||
"why_it_failed": None,
|
||||
"alternatives_tried": ["Alt1", "Alt2"],
|
||||
},
|
||||
"changed_files": ["file1.py", "file2.py"],
|
||||
}
|
||||
|
||||
result = await queries.add_structured_insights(insights)
|
||||
|
||||
assert result is True
|
||||
|
||||
call_args = queries.client.graphiti.add_episode.call_args
|
||||
episode_body = json.loads(call_args[1]["episode_body"])
|
||||
assert episode_body["task_id"] == "task-1"
|
||||
assert episode_body["success"] is True
|
||||
assert episode_body["outcome"] == "Test approach"
|
||||
assert episode_body["why_worked"] == "Because reasons"
|
||||
assert episode_body["why_failed"] is None
|
||||
assert episode_body["alternatives_tried"] == ["Alt1", "Alt2"]
|
||||
assert episode_body["changed_files"] == ["file1.py", "file2.py"]
|
||||
@@ -0,0 +1,123 @@
|
||||
"""
|
||||
Tests for Graphiti schema constants and types.
|
||||
|
||||
Tests cover:
|
||||
- Episode type constants
|
||||
- MAX_CONTEXT_RESULTS constant
|
||||
- GroupIdMode enum values
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from integrations.graphiti.queries_pkg.schema import (
|
||||
EPISODE_TYPE_CODEBASE_DISCOVERY,
|
||||
EPISODE_TYPE_GOTCHA,
|
||||
EPISODE_TYPE_HISTORICAL_CONTEXT,
|
||||
EPISODE_TYPE_PATTERN,
|
||||
EPISODE_TYPE_QA_RESULT,
|
||||
EPISODE_TYPE_SESSION_INSIGHT,
|
||||
EPISODE_TYPE_TASK_OUTCOME,
|
||||
MAX_CONTEXT_RESULTS,
|
||||
MAX_RETRIES,
|
||||
RETRY_DELAY_SECONDS,
|
||||
GroupIdMode,
|
||||
)
|
||||
|
||||
|
||||
class TestEpisodeTypeConstants:
|
||||
"""Test episode type constants."""
|
||||
|
||||
def test_session_insight_constant(self):
|
||||
"""Test EPISODE_TYPE_SESSION_INSIGHT constant."""
|
||||
assert EPISODE_TYPE_SESSION_INSIGHT == "session_insight"
|
||||
assert isinstance(EPISODE_TYPE_SESSION_INSIGHT, str)
|
||||
|
||||
def test_codebase_discovery_constant(self):
|
||||
"""Test EPISODE_TYPE_CODEBASE_DISCOVERY constant."""
|
||||
assert EPISODE_TYPE_CODEBASE_DISCOVERY == "codebase_discovery"
|
||||
assert isinstance(EPISODE_TYPE_CODEBASE_DISCOVERY, str)
|
||||
|
||||
def test_pattern_constant(self):
|
||||
"""Test EPISODE_TYPE_PATTERN constant."""
|
||||
assert EPISODE_TYPE_PATTERN == "pattern"
|
||||
assert isinstance(EPISODE_TYPE_PATTERN, str)
|
||||
|
||||
def test_gotcha_constant(self):
|
||||
"""Test EPISODE_TYPE_GOTCHA constant."""
|
||||
assert EPISODE_TYPE_GOTCHA == "gotcha"
|
||||
assert isinstance(EPISODE_TYPE_GOTCHA, str)
|
||||
|
||||
def test_task_outcome_constant(self):
|
||||
"""Test EPISODE_TYPE_TASK_OUTCOME constant."""
|
||||
assert EPISODE_TYPE_TASK_OUTCOME == "task_outcome"
|
||||
assert isinstance(EPISODE_TYPE_TASK_OUTCOME, str)
|
||||
|
||||
def test_qa_result_constant(self):
|
||||
"""Test EPISODE_TYPE_QA_RESULT constant."""
|
||||
assert EPISODE_TYPE_QA_RESULT == "qa_result"
|
||||
assert isinstance(EPISODE_TYPE_QA_RESULT, str)
|
||||
|
||||
def test_historical_context_constant(self):
|
||||
"""Test EPISODE_TYPE_HISTORICAL_CONTEXT constant."""
|
||||
assert EPISODE_TYPE_HISTORICAL_CONTEXT == "historical_context"
|
||||
assert isinstance(EPISODE_TYPE_HISTORICAL_CONTEXT, str)
|
||||
|
||||
def test_all_episode_types_are_unique(self):
|
||||
"""Test that all episode type constants have unique values."""
|
||||
episode_types = [
|
||||
EPISODE_TYPE_SESSION_INSIGHT,
|
||||
EPISODE_TYPE_CODEBASE_DISCOVERY,
|
||||
EPISODE_TYPE_PATTERN,
|
||||
EPISODE_TYPE_GOTCHA,
|
||||
EPISODE_TYPE_TASK_OUTCOME,
|
||||
EPISODE_TYPE_QA_RESULT,
|
||||
EPISODE_TYPE_HISTORICAL_CONTEXT,
|
||||
]
|
||||
assert len(episode_types) == len(set(episode_types)), (
|
||||
"Episode types must be unique"
|
||||
)
|
||||
|
||||
|
||||
class TestMaxContextResults:
|
||||
"""Test MAX_CONTEXT_RESULTS constant."""
|
||||
|
||||
def test_max_context_results_is_positive_integer(self):
|
||||
"""Test MAX_CONTEXT_RESULTS is a positive integer."""
|
||||
assert isinstance(MAX_CONTEXT_RESULTS, int)
|
||||
assert MAX_CONTEXT_RESULTS > 0
|
||||
|
||||
def test_max_context_results_reasonable_value(self):
|
||||
"""Test MAX_CONTEXT_RESULTS has a reasonable value."""
|
||||
# Should be between 1 and 100 for practical use
|
||||
assert 1 <= MAX_CONTEXT_RESULTS <= 100
|
||||
|
||||
|
||||
class TestRetryConfiguration:
|
||||
"""Test retry configuration constants."""
|
||||
|
||||
def test_max_retries_is_positive_integer(self):
|
||||
"""Test MAX_RETRIES is a positive integer."""
|
||||
assert isinstance(MAX_RETRIES, int)
|
||||
assert MAX_RETRIES > 0
|
||||
|
||||
def test_retry_delay_is_positive_number(self):
|
||||
"""Test RETRY_DELAY_SECONDS is a positive number."""
|
||||
assert isinstance(RETRY_DELAY_SECONDS, (int, float))
|
||||
assert RETRY_DELAY_SECONDS >= 0
|
||||
|
||||
|
||||
class TestGroupIdMode:
|
||||
"""Test GroupIdMode class."""
|
||||
|
||||
def test_spec_mode_constant(self):
|
||||
"""Test GroupIdMode.SPEC constant."""
|
||||
assert GroupIdMode.SPEC == "spec"
|
||||
assert isinstance(GroupIdMode.SPEC, str)
|
||||
|
||||
def test_project_mode_constant(self):
|
||||
"""Test GroupIdMode.PROJECT constant."""
|
||||
assert GroupIdMode.PROJECT == "project"
|
||||
assert isinstance(GroupIdMode.PROJECT, str)
|
||||
|
||||
def test_modes_are_unique(self):
|
||||
"""Test that mode values are unique."""
|
||||
assert GroupIdMode.SPEC != GroupIdMode.PROJECT
|
||||
File diff suppressed because it is too large
Load Diff
@@ -24,7 +24,7 @@ You MUST create `spec.md` with ALL required sections (see template below).
|
||||
## PHASE 0: LOAD ALL CONTEXT (MANDATORY)
|
||||
|
||||
```bash
|
||||
# Read all input files
|
||||
# Read all input files (some may not exist for greenfield/empty projects)
|
||||
cat project_index.json
|
||||
cat requirements.json
|
||||
cat context.json
|
||||
@@ -35,6 +35,12 @@ Extract from these files:
|
||||
- **From requirements.json**: Task description, workflow type, services, acceptance criteria
|
||||
- **From context.json**: Files to modify, files to reference, patterns
|
||||
|
||||
**IMPORTANT**: If any input file is missing, empty, or shows 0 files, this is likely a **greenfield/new project**. Adapt accordingly:
|
||||
- Skip sections that reference existing code (e.g., "Files to Modify", "Patterns to Follow")
|
||||
- Instead, focus on files to CREATE and the initial project structure
|
||||
- Define the tech stack, dependencies, and setup instructions from scratch
|
||||
- Use industry best practices as patterns rather than referencing existing code
|
||||
|
||||
---
|
||||
|
||||
## PHASE 1: ANALYZE CONTEXT
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
# Pyproject configuration for Auto-Claude backend
|
||||
|
||||
[project]
|
||||
name = "auto-claude-backend"
|
||||
version = "2.7.6"
|
||||
description = "Auto-Claude autonomous coding framework - Python backend"
|
||||
requires-python = ">=3.12"
|
||||
dependencies = [
|
||||
"claude-agent-sdk>=0.1.25",
|
||||
"python-dotenv>=1.0.0",
|
||||
"graphiti-core>=0.5.0",
|
||||
"pandas>=2.2.0",
|
||||
"google-generativeai>=0.8.0",
|
||||
"pydantic>=2.0.0",
|
||||
"sentry-sdk>=2.0.0",
|
||||
]
|
||||
|
||||
[project.optional-dependencies]
|
||||
dev = [
|
||||
"pytest>=7.0.0",
|
||||
"pytest-asyncio>=0.21.0",
|
||||
"pytest-cov>=4.0.0",
|
||||
"pytest-timeout>=2.0.0",
|
||||
"pytest-mock>=3.0.0",
|
||||
"coverage>=7.0.0",
|
||||
"mypy>=1.0.0",
|
||||
"types-toml>=0.10.0",
|
||||
]
|
||||
|
||||
[tool.pytest.ini_options]
|
||||
testpaths = ["integrations/graphiti/tests", "core/workspace/tests"]
|
||||
python_files = ["test_*.py"]
|
||||
python_functions = ["test_*"]
|
||||
python_classes = ["Test*"]
|
||||
asyncio_mode = "strict"
|
||||
asyncio_default_fixture_loop_scope = "function"
|
||||
|
||||
# Markers for long-running tests
|
||||
markers = [
|
||||
"slow: marks tests as slow (skipped in CI by default) - takes >2 seconds or involves external services",
|
||||
"integration: marks tests as integration tests (external services like database, network, API calls)",
|
||||
"smoke: marks smoke tests for quick verification",
|
||||
]
|
||||
|
||||
# Optimizations
|
||||
addopts = [
|
||||
"--maxfail=5",
|
||||
"-v",
|
||||
"-m", "not slow",
|
||||
"--tb=short",
|
||||
]
|
||||
|
||||
[tool.coverage.run]
|
||||
source = ["integrations", "core", "agents", "cli", "context", "qa", "spec", "runners", "services"]
|
||||
omit = [
|
||||
"*/tests/*",
|
||||
"*/test_*.py",
|
||||
"*/__pycache__/*",
|
||||
"*/.venv/*",
|
||||
"*/site-packages/*",
|
||||
]
|
||||
|
||||
[tool.coverage.report]
|
||||
precision = 1
|
||||
show_missing = true
|
||||
skip_covered = false
|
||||
exclude_lines = [
|
||||
"pragma: no cover",
|
||||
"def __repr__",
|
||||
"raise AssertionError",
|
||||
"raise NotImplementedError",
|
||||
"if __name__ == .__main__.:",
|
||||
"if TYPE_CHECKING:",
|
||||
"class .*\\bProtocol\\):",
|
||||
"@(abc\\.)?abstractmethod",
|
||||
]
|
||||
|
||||
[tool.mypy]
|
||||
python_version = "3.12"
|
||||
warn_return_any = true
|
||||
warn_unused_configs = true
|
||||
disallow_untyped_defs = false
|
||||
@@ -15,7 +15,11 @@ from pathlib import Path
|
||||
from agents.base import sanitize_error_message
|
||||
from agents.memory_manager import get_graphiti_context, save_session_memory
|
||||
from claude_agent_sdk import ClaudeSDKClient
|
||||
from core.error_utils import is_rate_limit_error, is_tool_concurrency_error
|
||||
from core.error_utils import (
|
||||
is_rate_limit_error,
|
||||
is_tool_concurrency_error,
|
||||
safe_receive_messages,
|
||||
)
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
from security.tool_input_validator import get_safe_tool_input
|
||||
from task_logger import (
|
||||
@@ -141,7 +145,7 @@ async def run_qa_fixer_session(
|
||||
|
||||
response_text = ""
|
||||
debug("qa_fixer", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="qa_fixer"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -215,6 +215,7 @@ 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
|
||||
|
||||
@@ -230,6 +231,7 @@ 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
|
||||
|
||||
@@ -16,7 +16,11 @@ from pathlib import Path
|
||||
from agents.base import sanitize_error_message
|
||||
from agents.memory_manager import get_graphiti_context, save_session_memory
|
||||
from claude_agent_sdk import ClaudeSDKClient
|
||||
from core.error_utils import is_rate_limit_error, is_tool_concurrency_error
|
||||
from core.error_utils import (
|
||||
is_rate_limit_error,
|
||||
is_tool_concurrency_error,
|
||||
safe_receive_messages,
|
||||
)
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
from prompts_pkg import get_qa_reviewer_prompt
|
||||
from security.tool_input_validator import get_safe_tool_input
|
||||
@@ -195,7 +199,7 @@ This is attempt {previous_error.get("consecutive_errors", 1) + 1}. If you fail t
|
||||
|
||||
response_text = ""
|
||||
debug("qa_reviewer", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="qa_reviewer"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
# Auto-Build Framework Dependencies
|
||||
# SDK 0.1.33+ required for Opus 4.6 adaptive thinking support
|
||||
# Earlier versions lacked effort parameter and thinking type configuration
|
||||
claude-agent-sdk>=0.1.33
|
||||
# SDK 0.1.39+ required for Opus 4.6 adaptive thinking support and stability fixes
|
||||
# Earlier versions lacked effort parameter, thinking type configuration,
|
||||
# and crashed on unhandled CLI message types (e.g., rate_limit_event)
|
||||
claude-agent-sdk>=0.1.39
|
||||
python-dotenv>=1.0.0
|
||||
|
||||
# TOML parsing fallback for Python < 3.11
|
||||
|
||||
@@ -12,7 +12,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timezone
|
||||
from enum import Enum
|
||||
from pathlib import Path
|
||||
|
||||
@@ -22,6 +22,11 @@ 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."""
|
||||
|
||||
@@ -521,7 +526,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: datetime.now().isoformat())
|
||||
reviewed_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
error: str | None = None
|
||||
|
||||
# NEW: Enhanced verdict system
|
||||
@@ -567,6 +572,9 @@ 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,
|
||||
@@ -598,6 +606,8 @@ 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
|
||||
@@ -610,7 +620,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", datetime.now().isoformat()),
|
||||
reviewed_at=data.get("reviewed_at", _utc_now_iso()),
|
||||
error=data.get("error"),
|
||||
# NEW fields
|
||||
verdict=MergeVerdict(data.get("verdict", "ready_to_merge")),
|
||||
@@ -645,6 +655,8 @@ 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:
|
||||
@@ -691,7 +703,7 @@ class PRReviewResult:
|
||||
reviews.append(entry)
|
||||
|
||||
current_data["reviews"] = reviews
|
||||
current_data["last_updated"] = datetime.now().isoformat()
|
||||
current_data["last_updated"] = _utc_now_iso()
|
||||
|
||||
return current_data
|
||||
|
||||
@@ -762,7 +774,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: datetime.now().isoformat())
|
||||
triaged_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
@@ -798,7 +810,7 @@ class TriageResult:
|
||||
suggested_breakdown=data.get("suggested_breakdown", []),
|
||||
priority=data.get("priority", "medium"),
|
||||
comment=data.get("comment"),
|
||||
triaged_at=data.get("triaged_at", datetime.now().isoformat()),
|
||||
triaged_at=data.get("triaged_at", _utc_now_iso()),
|
||||
)
|
||||
|
||||
async def save(self, github_dir: Path) -> None:
|
||||
@@ -836,8 +848,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: datetime.now().isoformat())
|
||||
updated_at: str = field(default_factory=lambda: datetime.now().isoformat())
|
||||
created_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
updated_at: str = field(default_factory=lambda: _utc_now_iso())
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
@@ -875,8 +887,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", datetime.now().isoformat()),
|
||||
updated_at=data.get("updated_at", datetime.now().isoformat()),
|
||||
created_at=data.get("created_at", _utc_now_iso()),
|
||||
updated_at=data.get("updated_at", _utc_now_iso()),
|
||||
)
|
||||
|
||||
def update_status(self, status: AutoFixStatus) -> None:
|
||||
@@ -886,7 +898,7 @@ class AutoFixState:
|
||||
f"Invalid state transition: {self.status.value} -> {status.value}"
|
||||
)
|
||||
self.status = status
|
||||
self.updated_at = datetime.now().isoformat()
|
||||
self.updated_at = _utc_now_iso()
|
||||
|
||||
async def save(self, github_dir: Path) -> None:
|
||||
"""Save auto-fix state to .auto-claude/github/issues/ with file locking."""
|
||||
@@ -938,7 +950,7 @@ class AutoFixState:
|
||||
queue.append(entry)
|
||||
|
||||
current_data["auto_fix_queue"] = queue
|
||||
current_data["last_updated"] = datetime.now().isoformat()
|
||||
current_data["last_updated"] = _utc_now_iso()
|
||||
|
||||
return current_data
|
||||
|
||||
|
||||
@@ -395,8 +395,28 @@ 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, in-progress), create a skip result
|
||||
# For other skip reasons (bot-authored, cooling off), create a skip result
|
||||
return await self._create_skip_result(pr_number, skip_reason)
|
||||
|
||||
# Mark review as started (prevents concurrent reviews)
|
||||
|
||||
@@ -235,6 +235,12 @@ 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}")
|
||||
|
||||
@@ -18,7 +18,6 @@ from __future__ import annotations
|
||||
import hashlib
|
||||
import logging
|
||||
import re
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
@@ -26,6 +25,8 @@ 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,
|
||||
@@ -33,12 +34,16 @@ 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 FollowupReviewResponse
|
||||
from .pydantic_models import FollowupExtractionResponse, FollowupReviewResponse
|
||||
from .recovery_utils import create_finding_from_summary
|
||||
from .sdk_utils import process_sdk_stream
|
||||
except (ImportError, ValueError, SystemError):
|
||||
from core.client import create_client
|
||||
from gh_client import GHClient
|
||||
from models import (
|
||||
MergeVerdict,
|
||||
@@ -46,11 +51,18 @@ 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 FollowupReviewResponse
|
||||
from services.pydantic_models import (
|
||||
FollowupExtractionResponse,
|
||||
FollowupReviewResponse,
|
||||
)
|
||||
from services.recovery_utils import create_finding_from_summary
|
||||
from services.sdk_utils import process_sdk_stream
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -265,7 +277,7 @@ class FollowupReviewer:
|
||||
verdict=verdict,
|
||||
verdict_reasoning=verdict_reasoning,
|
||||
blockers=blockers,
|
||||
reviewed_at=datetime.now().isoformat(),
|
||||
reviewed_at=_utc_now_iso(),
|
||||
# Follow-up specific fields
|
||||
reviewed_commit_sha=context.current_commit_sha,
|
||||
reviewed_file_blobs=file_blobs,
|
||||
@@ -697,6 +709,9 @@ 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(
|
||||
@@ -721,7 +736,9 @@ 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 == "ToolUseBlock":
|
||||
if block_type == "TextBlock":
|
||||
captured_text += getattr(block, "text", "")
|
||||
elif block_type == "ToolUseBlock":
|
||||
tool_name = getattr(block, "name", "")
|
||||
if tool_name == "StructuredOutput":
|
||||
# Extract structured data from tool input
|
||||
@@ -764,9 +781,31 @@ 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:
|
||||
@@ -839,6 +878,124 @@ 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],
|
||||
|
||||
@@ -51,7 +51,8 @@ try:
|
||||
from .category_utils import map_category
|
||||
from .io_utils import safe_print
|
||||
from .pr_worktree_manager import PRWorktreeManager
|
||||
from .pydantic_models import ParallelFollowupResponse
|
||||
from .pydantic_models import FollowupExtractionResponse, ParallelFollowupResponse
|
||||
from .recovery_utils import create_finding_from_summary
|
||||
from .sdk_utils import process_sdk_stream
|
||||
except (ImportError, ValueError, SystemError):
|
||||
from context_gatherer import _validate_git_ref
|
||||
@@ -75,7 +76,11 @@ 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 ParallelFollowupResponse
|
||||
from services.pydantic_models import (
|
||||
FollowupExtractionResponse,
|
||||
ParallelFollowupResponse,
|
||||
)
|
||||
from services.recovery_utils import create_finding_from_summary
|
||||
from services.sdk_utils import process_sdk_stream
|
||||
|
||||
|
||||
@@ -576,16 +581,36 @@ The SDK will run invoked agents in parallel automatically.
|
||||
)
|
||||
|
||||
# Check for stream processing errors
|
||||
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']}"
|
||||
)
|
||||
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}"
|
||||
)
|
||||
|
||||
result_text = stream_result["result_text"]
|
||||
structured_output = stream_result["structured_output"]
|
||||
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"]
|
||||
)
|
||||
agents_invoked = stream_result["agents_invoked"]
|
||||
msg_count = stream_result["msg_count"]
|
||||
|
||||
@@ -596,22 +621,28 @@ The SDK will run invoked agents in parallel automatically.
|
||||
pr_number=context.pr_number,
|
||||
)
|
||||
|
||||
# Parse findings from output
|
||||
# Parse findings from output (three-tier recovery cascade)
|
||||
if structured_output:
|
||||
result_data = self._parse_structured_output(structured_output, context)
|
||||
else:
|
||||
# Log when structured output is missing - this shouldn't happen normally
|
||||
# when output_format is configured, so it indicates a problem
|
||||
# Structured output missing or validation failed.
|
||||
# Tier 2: Attempt extraction call with minimal schema
|
||||
logger.warning(
|
||||
"[ParallelFollowup] No structured output received from SDK - "
|
||||
"falling back to text parsing. Resolution data may be incomplete."
|
||||
"[ParallelFollowup] No structured output — attempting extraction call"
|
||||
)
|
||||
safe_print(
|
||||
"[ParallelFollowup] WARNING: Structured output not captured, "
|
||||
"using text fallback (resolution tracking may be incomplete)",
|
||||
flush=True,
|
||||
# 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
|
||||
)
|
||||
result_data = self._parse_text_output(result_text, context)
|
||||
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)
|
||||
|
||||
# Extract data
|
||||
findings = result_data.get("findings", [])
|
||||
@@ -730,7 +761,9 @@ 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", []))
|
||||
dismissed_count = len(
|
||||
result_data.get("dismissed_false_positive_ids", [])
|
||||
) or result_data.get("dismissed_finding_count", 0)
|
||||
confirmed_count = result_data.get("confirmed_valid_count", 0)
|
||||
needs_human_count = result_data.get("needs_human_review_count", 0)
|
||||
|
||||
@@ -1074,17 +1107,172 @@ 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.MERGE_WITH_CHANGES
|
||||
verdict = MergeVerdict.NEEDS_REVISION
|
||||
|
||||
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 {
|
||||
@@ -1092,8 +1280,13 @@ 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:
|
||||
@@ -1102,6 +1295,7 @@ 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
|
||||
@@ -1109,6 +1303,7 @@ 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", [])
|
||||
@@ -1127,14 +1322,68 @@ The SDK will run invoked agents in parallel automatically.
|
||||
):
|
||||
unresolved_ids.append(finding_id)
|
||||
|
||||
# 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 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 verdict
|
||||
verdict_str = data.get("verdict", "NEEDS_REVISION")
|
||||
@@ -1149,14 +1398,15 @@ 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:
|
||||
if resolved_ids or unresolved_ids or new_finding_ids or findings:
|
||||
return {
|
||||
"findings": [], # Can't reliably extract full findings without validation
|
||||
"findings": findings,
|
||||
"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}",
|
||||
|
||||
@@ -633,7 +633,14 @@ Report findings with specific file paths, line numbers, and code evidence.
|
||||
logger.error(
|
||||
f"[Specialist:{specialist_name}] Failed to parse structured output: {e}"
|
||||
)
|
||||
# Fall through to text parsing
|
||||
# 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"
|
||||
)
|
||||
|
||||
if not findings and result_text:
|
||||
# Fallback to text parsing
|
||||
@@ -643,6 +650,63 @@ 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.md5(
|
||||
f"{file_path}:{line}:{title}".encode(),
|
||||
usedforsecurity=False,
|
||||
).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_specialists(
|
||||
self,
|
||||
context: PRContext,
|
||||
@@ -910,13 +974,15 @@ The SDK will run invoked agents in parallel automatically.
|
||||
except ValueError:
|
||||
severity = ReviewSeverity.MEDIUM
|
||||
|
||||
# Extract evidence: prefer verification.code_examined, fallback to evidence field
|
||||
evidence = finding_data.evidence
|
||||
# Extract evidence from verification.code_examined if available
|
||||
evidence = None
|
||||
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)
|
||||
@@ -1223,12 +1289,30 @@ The SDK will run invoked agents in parallel automatically.
|
||||
f"{len(filtered_findings)} filtered"
|
||||
)
|
||||
|
||||
# No confidence routing - validation is binary via finding-validator
|
||||
unique_findings = validated_findings
|
||||
logger.info(f"[PRReview] Final findings: {len(unique_findings)} validated")
|
||||
# Separate active findings (drive verdict) from dismissed (shown in UI only)
|
||||
active_findings = []
|
||||
dismissed_findings = []
|
||||
for f in validated_findings:
|
||||
if f.validation_status == "dismissed_false_positive":
|
||||
dismissed_findings.append(f)
|
||||
else:
|
||||
active_findings.append(f)
|
||||
|
||||
safe_print(
|
||||
f"[ParallelOrchestrator] Final: {len(active_findings)} active, "
|
||||
f"{len(dismissed_findings)} disputed by validator",
|
||||
flush=True,
|
||||
)
|
||||
logger.info(
|
||||
f"[ParallelOrchestrator] Review complete: {len(unique_findings)} findings"
|
||||
f"[PRReview] Final findings: {len(active_findings)} active, "
|
||||
f"{len(dismissed_findings)} disputed"
|
||||
)
|
||||
|
||||
# All findings (active + dismissed) go in the result for UI display
|
||||
all_review_findings = validated_findings
|
||||
logger.info(
|
||||
f"[ParallelOrchestrator] Review complete: {len(all_review_findings)} findings "
|
||||
f"({len(active_findings)} active, {len(dismissed_findings)} disputed)"
|
||||
)
|
||||
|
||||
# Fetch CI status for verdict consideration
|
||||
@@ -1238,9 +1322,9 @@ The SDK will run invoked agents in parallel automatically.
|
||||
f"{ci_status.get('failing', 0)} failing, {ci_status.get('pending', 0)} pending"
|
||||
)
|
||||
|
||||
# Generate verdict (includes merge conflict check, branch-behind check, and CI status)
|
||||
# Generate verdict from ACTIVE findings only (dismissed don't affect verdict)
|
||||
verdict, verdict_reasoning, blockers = self._generate_verdict(
|
||||
unique_findings,
|
||||
active_findings,
|
||||
has_merge_conflicts=context.has_merge_conflicts,
|
||||
merge_state_status=context.merge_state_status,
|
||||
ci_status=ci_status,
|
||||
@@ -1251,7 +1335,7 @@ The SDK will run invoked agents in parallel automatically.
|
||||
verdict=verdict,
|
||||
verdict_reasoning=verdict_reasoning,
|
||||
blockers=blockers,
|
||||
findings=unique_findings,
|
||||
findings=all_review_findings,
|
||||
agents_invoked=agents_invoked,
|
||||
)
|
||||
|
||||
@@ -1296,7 +1380,7 @@ The SDK will run invoked agents in parallel automatically.
|
||||
pr_number=context.pr_number,
|
||||
repo=self.config.repo,
|
||||
success=True,
|
||||
findings=unique_findings,
|
||||
findings=all_review_findings,
|
||||
summary=summary,
|
||||
overall_status=overall_status,
|
||||
verdict=verdict,
|
||||
@@ -1785,6 +1869,7 @@ 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:
|
||||
@@ -1805,6 +1890,7 @@ 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
|
||||
@@ -1869,12 +1955,38 @@ For EACH finding above:
|
||||
validated_findings.append(finding)
|
||||
|
||||
elif validation.validation_status == "dismissed_false_positive":
|
||||
# Dismiss - do not include
|
||||
dismissed_count += 1
|
||||
logger.info(
|
||||
f"[PRReview] Dismissed {finding.id} as false positive: "
|
||||
f"{validation.explanation[:100]}"
|
||||
)
|
||||
# Protect cross-validated findings from dismissal —
|
||||
# if multiple specialists independently found the same issue,
|
||||
# a single validator should not override that consensus
|
||||
if finding.cross_validated:
|
||||
finding.validation_status = "confirmed_valid"
|
||||
finding.validation_evidence = validation.code_evidence
|
||||
finding.validation_explanation = (
|
||||
f"[Auto-kept: cross-validated by {len(finding.source_agents)} agents] "
|
||||
f"{validation.explanation}"
|
||||
)
|
||||
validated_findings.append(finding)
|
||||
safe_print(
|
||||
f"[FindingValidator] Kept cross-validated finding '{finding.title}' "
|
||||
f"despite dismissal (agents={finding.source_agents})",
|
||||
flush=True,
|
||||
)
|
||||
else:
|
||||
# Keep finding but mark as dismissed (user can see it in UI)
|
||||
finding.validation_status = "dismissed_false_positive"
|
||||
finding.validation_evidence = validation.code_evidence
|
||||
finding.validation_explanation = validation.explanation
|
||||
validated_findings.append(finding)
|
||||
dismissed_count += 1
|
||||
safe_print(
|
||||
f"[FindingValidator] Disputed '{finding.title}': "
|
||||
f"{validation.explanation} (file={finding.file}:{finding.line})",
|
||||
flush=True,
|
||||
)
|
||||
logger.info(
|
||||
f"[PRReview] Disputed {finding.id}: "
|
||||
f"{validation.explanation[:200]}"
|
||||
)
|
||||
|
||||
elif validation.validation_status == "needs_human_review":
|
||||
# Keep but flag
|
||||
@@ -2059,11 +2171,16 @@ For EACH finding above:
|
||||
sev = f.severity.value
|
||||
emoji = severity_emoji.get(sev, "⚪")
|
||||
|
||||
is_disputed = f.validation_status == "dismissed_false_positive"
|
||||
|
||||
# Finding header with location
|
||||
line_range = f"L{f.line}"
|
||||
if f.end_line and f.end_line != f.line:
|
||||
line_range = f"L{f.line}-L{f.end_line}"
|
||||
lines.append(f"#### {emoji} [{sev.upper()}] {f.title}")
|
||||
if is_disputed:
|
||||
lines.append(f"#### ⚪ [DISPUTED] ~~{f.title}~~")
|
||||
else:
|
||||
lines.append(f"#### {emoji} [{sev.upper()}] {f.title}")
|
||||
lines.append(f"**File:** `{f.file}` ({line_range})")
|
||||
|
||||
# Cross-validation badge
|
||||
@@ -2093,6 +2210,7 @@ For EACH finding above:
|
||||
status_label = {
|
||||
"confirmed_valid": "Confirmed",
|
||||
"needs_human_review": "Needs human review",
|
||||
"dismissed_false_positive": "Disputed by validator",
|
||||
}.get(f.validation_status, f.validation_status)
|
||||
lines.append("")
|
||||
lines.append(f"**Validation:** {status_label}")
|
||||
@@ -2114,18 +2232,27 @@ For EACH finding above:
|
||||
|
||||
lines.append("")
|
||||
|
||||
# Findings count summary
|
||||
# Findings count summary (exclude dismissed from active count)
|
||||
active_count = 0
|
||||
dismissed_count = 0
|
||||
by_severity: dict[str, int] = {}
|
||||
for f in findings:
|
||||
if f.validation_status == "dismissed_false_positive":
|
||||
dismissed_count += 1
|
||||
continue
|
||||
active_count += 1
|
||||
sev = f.severity.value
|
||||
by_severity[sev] = by_severity.get(sev, 0) + 1
|
||||
summary_parts = []
|
||||
for sev in ["critical", "high", "medium", "low"]:
|
||||
if sev in by_severity:
|
||||
summary_parts.append(f"{by_severity[sev]} {sev}")
|
||||
lines.append(
|
||||
f"**Total:** {len(findings)} finding(s) ({', '.join(summary_parts)})"
|
||||
count_text = (
|
||||
f"**Total:** {active_count} finding(s) ({', '.join(summary_parts)})"
|
||||
)
|
||||
if dismissed_count > 0:
|
||||
count_text += f" + {dismissed_count} disputed"
|
||||
lines.append(count_text)
|
||||
lines.append("")
|
||||
|
||||
lines.append("---")
|
||||
|
||||
@@ -26,10 +26,10 @@ from __future__ import annotations
|
||||
|
||||
from typing import Literal
|
||||
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
|
||||
# =============================================================================
|
||||
# Verification Evidence (Required for All Findings)
|
||||
# Verification Evidence (Optional for findings — only code_examined is consumed)
|
||||
# =============================================================================
|
||||
|
||||
|
||||
@@ -50,102 +50,28 @@ class VerificationEvidence(BaseModel):
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# Common Finding Types
|
||||
# Severity / Category Validators
|
||||
# =============================================================================
|
||||
|
||||
|
||||
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"
|
||||
)
|
||||
_VALID_SEVERITIES = {"critical", "high", "medium", "low"}
|
||||
|
||||
|
||||
class SecurityFinding(BaseFinding):
|
||||
"""A security vulnerability finding."""
|
||||
|
||||
category: Literal["security"] = Field(
|
||||
default="security", description="Always 'security' for security findings"
|
||||
)
|
||||
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 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"
|
||||
)
|
||||
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
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -163,25 +89,34 @@ class FindingResolution(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
_FOLLOWUP_CATEGORIES = {"security", "quality", "logic", "test", "docs"}
|
||||
|
||||
|
||||
class FollowupFinding(BaseModel):
|
||||
"""A new finding from follow-up review (simpler than initial review)."""
|
||||
"""A new finding from follow-up review (simpler than initial review).
|
||||
|
||||
verification is intentionally omitted — not consumed by followup_reviewer.py.
|
||||
"""
|
||||
|
||||
id: str = Field(description="Unique identifier for this finding")
|
||||
severity: Literal["critical", "high", "medium", "low"] = Field(
|
||||
description="Issue severity level"
|
||||
)
|
||||
category: Literal["security", "quality", "logic", "test", "docs"] = Field(
|
||||
description="Issue category"
|
||||
)
|
||||
severity: str = Field(description="Issue severity level")
|
||||
category: str = 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")
|
||||
verification: VerificationEvidence = Field(
|
||||
description="Evidence that this finding was verified against actual code"
|
||||
)
|
||||
|
||||
@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)
|
||||
|
||||
|
||||
class FollowupReviewResponse(BaseModel):
|
||||
@@ -203,81 +138,6 @@ 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
|
||||
# =============================================================================
|
||||
@@ -320,88 +180,21 @@ 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)
|
||||
# =============================================================================
|
||||
|
||||
|
||||
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"
|
||||
)
|
||||
_ORCHESTRATOR_CATEGORIES = {
|
||||
"security",
|
||||
"quality",
|
||||
"logic",
|
||||
"codebase_fit",
|
||||
"test",
|
||||
"docs",
|
||||
"redundancy",
|
||||
"pattern",
|
||||
"performance",
|
||||
}
|
||||
|
||||
|
||||
class ParallelOrchestratorFinding(BaseModel):
|
||||
@@ -413,26 +206,11 @@ 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: 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(
|
||||
category: str = Field(description="Issue category")
|
||||
severity: str = Field(description="Issue severity level")
|
||||
verification: VerificationEvidence | 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"
|
||||
description="Evidence that this finding was verified against actual code",
|
||||
)
|
||||
is_impact_finding: bool = Field(
|
||||
False,
|
||||
@@ -459,6 +237,16 @@ 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."""
|
||||
@@ -514,15 +302,22 @@ 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: Literal["critical", "high", "medium", "low"] = Field(
|
||||
description="Issue severity level"
|
||||
)
|
||||
category: Literal[
|
||||
"security", "quality", "logic", "performance", "pattern", "test", "docs"
|
||||
] = Field(description="Issue category")
|
||||
severity: str = Field(description="Issue severity level")
|
||||
category: str = 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")
|
||||
@@ -530,14 +325,24 @@ 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(
|
||||
min_length=1,
|
||||
description="Actual code snippet examined that shows the issue. Required.",
|
||||
default="",
|
||||
description="Actual code snippet examined that shows the issue.",
|
||||
)
|
||||
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).
|
||||
@@ -611,6 +416,17 @@ class ResolutionVerification(BaseModel):
|
||||
)
|
||||
|
||||
|
||||
_PARALLEL_FOLLOWUP_CATEGORIES = {
|
||||
"security",
|
||||
"quality",
|
||||
"logic",
|
||||
"test",
|
||||
"docs",
|
||||
"regression",
|
||||
"incomplete_fix",
|
||||
}
|
||||
|
||||
|
||||
class ParallelFollowupFinding(BaseModel):
|
||||
"""A finding from parallel follow-up review."""
|
||||
|
||||
@@ -619,18 +435,8 @@ 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: Literal[
|
||||
"security",
|
||||
"quality",
|
||||
"logic",
|
||||
"test",
|
||||
"docs",
|
||||
"regression",
|
||||
"incomplete_fix",
|
||||
] = Field(description="Issue category")
|
||||
severity: Literal["critical", "high", "medium", "low"] = Field(
|
||||
description="Issue severity level"
|
||||
)
|
||||
category: str = Field(description="Issue category")
|
||||
severity: str = 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(
|
||||
@@ -638,6 +444,16 @@ 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.
|
||||
@@ -710,3 +526,55 @@ 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"
|
||||
)
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
"""
|
||||
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,6 +133,13 @@ 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.
|
||||
@@ -261,8 +268,11 @@ 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
|
||||
@@ -481,6 +491,9 @@ 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`.
|
||||
@@ -647,11 +660,16 @@ 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,
|
||||
}
|
||||
|
||||
@@ -14,12 +14,19 @@ Key Features:
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import subprocess
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta, timezone
|
||||
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."""
|
||||
@@ -82,8 +89,8 @@ class RecoveryManager:
|
||||
"subtasks": {},
|
||||
"stuck_subtasks": [],
|
||||
"metadata": {
|
||||
"created_at": datetime.now().isoformat(),
|
||||
"last_updated": datetime.now().isoformat(),
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"last_updated": datetime.now(timezone.utc).isoformat(),
|
||||
},
|
||||
}
|
||||
with open(self.attempt_history_file, "w", encoding="utf-8") as f:
|
||||
@@ -95,8 +102,8 @@ class RecoveryManager:
|
||||
"commits": [],
|
||||
"last_good_commit": None,
|
||||
"metadata": {
|
||||
"created_at": datetime.now().isoformat(),
|
||||
"last_updated": datetime.now().isoformat(),
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
"last_updated": datetime.now(timezone.utc).isoformat(),
|
||||
},
|
||||
}
|
||||
with open(self.build_commits_file, "w", encoding="utf-8") as f:
|
||||
@@ -114,7 +121,7 @@ class RecoveryManager:
|
||||
|
||||
def _save_attempt_history(self, data: dict) -> None:
|
||||
"""Save attempt history to JSON file."""
|
||||
data["metadata"]["last_updated"] = datetime.now().isoformat()
|
||||
data["metadata"]["last_updated"] = datetime.now(timezone.utc).isoformat()
|
||||
with open(self.attempt_history_file, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
@@ -130,7 +137,7 @@ class RecoveryManager:
|
||||
|
||||
def _save_build_commits(self, data: dict) -> None:
|
||||
"""Save build commits to JSON file."""
|
||||
data["metadata"]["last_updated"] = datetime.now().isoformat()
|
||||
data["metadata"]["last_updated"] = datetime.now(timezone.utc).isoformat()
|
||||
with open(self.build_commits_file, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
@@ -185,17 +192,44 @@ class RecoveryManager:
|
||||
|
||||
def get_attempt_count(self, subtask_id: str) -> int:
|
||||
"""
|
||||
Get how many times this subtask has been attempted.
|
||||
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.
|
||||
|
||||
Args:
|
||||
subtask_id: ID of the subtask
|
||||
|
||||
Returns:
|
||||
Number of attempts
|
||||
Number of attempts within the time window
|
||||
"""
|
||||
history = self._load_attempt_history()
|
||||
subtask_data = history["subtasks"].get(subtask_id, {})
|
||||
return len(subtask_data.get("attempts", []))
|
||||
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
|
||||
|
||||
def record_attempt(
|
||||
self,
|
||||
@@ -208,6 +242,8 @@ 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
|
||||
@@ -224,13 +260,24 @@ class RecoveryManager:
|
||||
# Add the attempt
|
||||
attempt = {
|
||||
"session": session,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"timestamp": datetime.now(timezone.utc).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"
|
||||
@@ -405,7 +452,7 @@ class RecoveryManager:
|
||||
commit_record = {
|
||||
"hash": commit_hash,
|
||||
"subtask_id": subtask_id,
|
||||
"timestamp": datetime.now().isoformat(),
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
}
|
||||
|
||||
commits["commits"].append(commit_record)
|
||||
@@ -450,7 +497,7 @@ class RecoveryManager:
|
||||
stuck_entry = {
|
||||
"subtask_id": subtask_id,
|
||||
"reason": reason,
|
||||
"escalated_at": datetime.now().isoformat(),
|
||||
"escalated_at": datetime.now(timezone.utc).isoformat(),
|
||||
"attempt_count": self.get_attempt_count(subtask_id),
|
||||
}
|
||||
|
||||
|
||||
@@ -6,18 +6,54 @@ Phases for spec document creation and quality assurance.
|
||||
"""
|
||||
|
||||
import json
|
||||
from typing import TYPE_CHECKING
|
||||
from pathlib import Path
|
||||
|
||||
from .. import validator, writer
|
||||
from ..discovery import get_project_index_stats
|
||||
from .models import MAX_RETRIES, PhaseResult
|
||||
|
||||
if TYPE_CHECKING:
|
||||
pass
|
||||
|
||||
def _is_greenfield_project(spec_dir: Path) -> bool:
|
||||
"""Check if the project is empty/greenfield (0 discovered files)."""
|
||||
stats = get_project_index_stats(spec_dir)
|
||||
if not stats:
|
||||
return False # Can't determine - don't assume greenfield
|
||||
return stats.get("file_count", 0) == 0
|
||||
|
||||
|
||||
def _greenfield_context() -> str:
|
||||
"""Return additional context for greenfield/empty projects."""
|
||||
return """
|
||||
**GREENFIELD PROJECT**: This is an empty or new project with no existing code.
|
||||
There are no existing files to reference or modify. You are creating everything from scratch.
|
||||
|
||||
Adapt your approach:
|
||||
- Do NOT reference existing files, patterns, or code structures
|
||||
- Focus on what needs to be CREATED, not modified
|
||||
- Define the initial project structure, files, and directories
|
||||
- Specify the tech stack, frameworks, and dependencies to install
|
||||
- Provide setup instructions for the new project
|
||||
- For "Files to Modify" and "Files to Reference" sections, list files to CREATE instead
|
||||
- For "Patterns to Follow", describe industry best practices rather than existing code
|
||||
"""
|
||||
|
||||
|
||||
class SpecPhaseMixin:
|
||||
"""Mixin for spec writing and critique phase methods."""
|
||||
|
||||
def _check_and_log_greenfield(self) -> bool:
|
||||
"""Check if the project is greenfield and log if so.
|
||||
|
||||
Returns:
|
||||
True if the project is greenfield (no existing files).
|
||||
"""
|
||||
is_greenfield = _is_greenfield_project(self.spec_dir)
|
||||
if is_greenfield:
|
||||
self.ui.print_status(
|
||||
"Greenfield project detected - adapting spec for new project", "info"
|
||||
)
|
||||
return is_greenfield
|
||||
|
||||
async def phase_quick_spec(self) -> PhaseResult:
|
||||
"""Quick spec for simple tasks - combines context and spec in one step."""
|
||||
spec_file = self.spec_dir / "spec.md"
|
||||
@@ -29,6 +65,8 @@ class SpecPhaseMixin:
|
||||
"quick_spec", True, [str(spec_file), str(plan_file)], [], 0
|
||||
)
|
||||
|
||||
is_greenfield = self._check_and_log_greenfield()
|
||||
|
||||
errors = []
|
||||
for attempt in range(MAX_RETRIES):
|
||||
self.ui.print_status(
|
||||
@@ -42,7 +80,7 @@ class SpecPhaseMixin:
|
||||
|
||||
This is a SIMPLE task. Create a minimal spec and implementation plan directly.
|
||||
No research or extensive analysis needed.
|
||||
|
||||
{_greenfield_context() if is_greenfield else ""}
|
||||
Create:
|
||||
1. A concise spec.md with just the essential sections
|
||||
2. A simple implementation_plan.json with 1-2 subtasks
|
||||
@@ -80,6 +118,9 @@ Create:
|
||||
"spec.md exists but has issues, regenerating...", "warning"
|
||||
)
|
||||
|
||||
is_greenfield = self._check_and_log_greenfield()
|
||||
greenfield_ctx = _greenfield_context() if is_greenfield else ""
|
||||
|
||||
errors = []
|
||||
for attempt in range(MAX_RETRIES):
|
||||
self.ui.print_status(
|
||||
@@ -88,6 +129,7 @@ Create:
|
||||
|
||||
success, output = await self.run_agent_fn(
|
||||
"spec_writer.md",
|
||||
additional_context=greenfield_ctx,
|
||||
phase_name="spec_writing",
|
||||
)
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ from ui.capabilities import configure_safe_encoding
|
||||
|
||||
configure_safe_encoding()
|
||||
|
||||
from core.error_utils import safe_receive_messages
|
||||
from debug import debug, debug_detailed, debug_error, debug_section, debug_success
|
||||
from security.tool_input_validator import get_safe_tool_input
|
||||
from task_logger import (
|
||||
@@ -162,7 +163,7 @@ class AgentRunner:
|
||||
|
||||
response_text = ""
|
||||
debug("agent_runner", "Starting to receive response stream...")
|
||||
async for msg in client.receive_response():
|
||||
async for msg in safe_receive_messages(client, caller="agent_runner"):
|
||||
msg_type = type(msg).__name__
|
||||
message_count += 1
|
||||
debug_detailed(
|
||||
|
||||
@@ -203,19 +203,19 @@ def generate_spec_name(task_description: str) -> str:
|
||||
return "-".join(name_parts) if name_parts else "spec"
|
||||
|
||||
|
||||
def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
def rename_spec_dir_from_requirements(spec_dir: Path) -> Path:
|
||||
"""Rename spec directory based on requirements.json task description.
|
||||
|
||||
Args:
|
||||
spec_dir: The current spec directory
|
||||
|
||||
Returns:
|
||||
Tuple of (success, new_spec_dir). If success is False, new_spec_dir is the original.
|
||||
The new spec directory path (or the original if no rename was needed/possible).
|
||||
"""
|
||||
requirements_file = spec_dir / "requirements.json"
|
||||
|
||||
if not requirements_file.exists():
|
||||
return False
|
||||
return spec_dir
|
||||
|
||||
try:
|
||||
with open(requirements_file, encoding="utf-8") as f:
|
||||
@@ -223,7 +223,7 @@ def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
|
||||
task_desc = req.get("task_description", "")
|
||||
if not task_desc:
|
||||
return False
|
||||
return spec_dir
|
||||
|
||||
# Generate new name
|
||||
new_name = generate_spec_name(task_desc)
|
||||
@@ -240,11 +240,11 @@ def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
|
||||
# Don't rename if it's already a good name (not "pending")
|
||||
if "pending" not in current_name:
|
||||
return True
|
||||
return spec_dir
|
||||
|
||||
# Don't rename if target already exists
|
||||
if new_spec_dir.exists():
|
||||
return True
|
||||
return spec_dir
|
||||
|
||||
# Rename the directory
|
||||
shutil.move(str(spec_dir), str(new_spec_dir))
|
||||
@@ -253,11 +253,11 @@ def rename_spec_dir_from_requirements(spec_dir: Path) -> bool:
|
||||
update_task_logger_path(new_spec_dir)
|
||||
|
||||
print_status(f"Spec folder: {highlight(new_dir_name)}", "success")
|
||||
return True
|
||||
return new_spec_dir
|
||||
|
||||
except (json.JSONDecodeError, OSError) as e:
|
||||
print_status(f"Could not rename spec folder: {e}", "warning")
|
||||
return False
|
||||
return spec_dir
|
||||
|
||||
|
||||
# Phase display configuration
|
||||
|
||||
@@ -6,6 +6,7 @@ Main orchestration logic for spec creation with dynamic complexity adaptation.
|
||||
"""
|
||||
|
||||
import json
|
||||
import types
|
||||
from collections.abc import Callable
|
||||
from pathlib import Path
|
||||
|
||||
@@ -18,6 +19,7 @@ from review import run_review_checkpoint
|
||||
from task_logger import (
|
||||
LogEntryType,
|
||||
LogPhase,
|
||||
TaskLogger,
|
||||
get_task_logger,
|
||||
)
|
||||
from ui import (
|
||||
@@ -238,6 +240,47 @@ class SpecOrchestrator:
|
||||
task_logger.start_phase(LogPhase.PLANNING, "Starting spec creation process")
|
||||
TaskEventEmitter.from_spec_dir(self.spec_dir).emit("PLANNING_STARTED")
|
||||
|
||||
# Track whether we've already ended the planning phase (to avoid double-end)
|
||||
self._planning_phase_ended = False
|
||||
|
||||
try:
|
||||
return await self._run_phases(interactive, auto_approve, task_logger, ui)
|
||||
except Exception as e:
|
||||
# Emit PLANNING_FAILED so the frontend XState machine transitions to error state
|
||||
# instead of leaving the task stuck in "planning" forever
|
||||
try:
|
||||
task_emitter = TaskEventEmitter.from_spec_dir(self.spec_dir)
|
||||
task_emitter.emit(
|
||||
"PLANNING_FAILED",
|
||||
{"error": str(e), "recoverable": True},
|
||||
)
|
||||
except Exception:
|
||||
pass # Don't mask the original error
|
||||
if not self._planning_phase_ended:
|
||||
self._planning_phase_ended = True
|
||||
try:
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING,
|
||||
success=False,
|
||||
message=f"Spec creation crashed: {e}",
|
||||
)
|
||||
except Exception:
|
||||
pass # Best effort - don't mask the original error when logging fails
|
||||
raise
|
||||
|
||||
async def _run_phases(
|
||||
self,
|
||||
interactive: bool,
|
||||
auto_approve: bool,
|
||||
task_logger: TaskLogger,
|
||||
ui: types.ModuleType,
|
||||
) -> bool:
|
||||
"""Internal method that runs all spec creation phases.
|
||||
|
||||
Separated from run() so that run() can wrap this in a try/except
|
||||
to emit PLANNING_FAILED on unhandled exceptions.
|
||||
"""
|
||||
|
||||
print(
|
||||
box(
|
||||
f"Spec Directory: {self.spec_dir}\n"
|
||||
@@ -291,9 +334,11 @@ class SpecOrchestrator:
|
||||
results.append(result)
|
||||
if not result.success:
|
||||
print_status("Discovery failed", "error")
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING, success=False, message="Discovery failed"
|
||||
)
|
||||
self._emit_planning_failed("Discovery phase failed")
|
||||
return False
|
||||
# Store summary for subsequent phases (compaction)
|
||||
await self._store_phase_summary("discovery")
|
||||
@@ -305,17 +350,26 @@ class SpecOrchestrator:
|
||||
results.append(result)
|
||||
if not result.success:
|
||||
print_status("Requirements gathering failed", "error")
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING,
|
||||
success=False,
|
||||
message="Requirements gathering failed",
|
||||
)
|
||||
self._emit_planning_failed("Requirements gathering failed")
|
||||
return False
|
||||
# Store summary for subsequent phases (compaction)
|
||||
await self._store_phase_summary("requirements")
|
||||
|
||||
# Rename spec folder with better name from requirements
|
||||
rename_spec_dir_from_requirements(self.spec_dir)
|
||||
# IMPORTANT: Update self.spec_dir after rename so subsequent phases use the correct path
|
||||
new_spec_dir = rename_spec_dir_from_requirements(self.spec_dir)
|
||||
if new_spec_dir != self.spec_dir:
|
||||
self.spec_dir = new_spec_dir
|
||||
self.validator = SpecValidator(self.spec_dir)
|
||||
# Update phase executor to use the renamed directory
|
||||
phase_executor.spec_dir = self.spec_dir
|
||||
phase_executor.spec_validator = self.validator
|
||||
|
||||
# Update task description from requirements
|
||||
req = requirements.load_requirements(self.spec_dir)
|
||||
@@ -335,9 +389,11 @@ class SpecOrchestrator:
|
||||
results.append(result)
|
||||
if not result.success:
|
||||
print_status("Complexity assessment failed", "error")
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING, success=False, message="Complexity assessment failed"
|
||||
)
|
||||
self._emit_planning_failed("Complexity assessment failed")
|
||||
return False
|
||||
|
||||
# Map of all available phases
|
||||
@@ -396,17 +452,22 @@ class SpecOrchestrator:
|
||||
f"Phase '{phase_name}' failed: {'; '.join(result.errors)}",
|
||||
LogEntryType.ERROR,
|
||||
)
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING,
|
||||
success=False,
|
||||
message=f"Phase {phase_name} failed",
|
||||
)
|
||||
self._emit_planning_failed(
|
||||
f"Phase '{phase_name}' failed: {'; '.join(result.errors)}"
|
||||
)
|
||||
return False
|
||||
|
||||
# Summary
|
||||
self._print_completion_summary(results, phases_executed)
|
||||
|
||||
# End planning phase successfully
|
||||
self._planning_phase_ended = True
|
||||
task_logger.end_phase(
|
||||
LogPhase.PLANNING, success=True, message="Spec creation complete"
|
||||
)
|
||||
@@ -638,6 +699,25 @@ class SpecOrchestrator:
|
||||
)
|
||||
)
|
||||
|
||||
def _emit_planning_failed(self, error: str) -> None:
|
||||
"""Emit PLANNING_FAILED event so the frontend transitions to error state.
|
||||
|
||||
Without this, the task stays stuck in 'planning' / 'in_progress' forever
|
||||
when spec creation fails, because the XState machine never receives a
|
||||
terminal event.
|
||||
|
||||
Args:
|
||||
error: Human-readable error description
|
||||
"""
|
||||
try:
|
||||
task_emitter = TaskEventEmitter.from_spec_dir(self.spec_dir)
|
||||
task_emitter.emit(
|
||||
"PLANNING_FAILED",
|
||||
{"error": error, "recoverable": True},
|
||||
)
|
||||
except Exception:
|
||||
pass # Best effort - don't mask the original failure
|
||||
|
||||
def _run_review_checkpoint(self, auto_approve: bool) -> bool:
|
||||
"""Run the human review checkpoint.
|
||||
|
||||
@@ -661,9 +741,8 @@ class SpecOrchestrator:
|
||||
print_status("Build will not proceed without approval.", "warning")
|
||||
return False
|
||||
|
||||
except SystemExit as e:
|
||||
if e.code != 0:
|
||||
return False
|
||||
except SystemExit:
|
||||
# Review checkpoint may call sys.exit(); treat any exit as unapproved
|
||||
return False
|
||||
except KeyboardInterrupt:
|
||||
print()
|
||||
@@ -696,19 +775,25 @@ class SpecOrchestrator:
|
||||
The functionality has been moved to models.rename_spec_dir_from_requirements.
|
||||
|
||||
Returns:
|
||||
True if successful or not needed, False on error
|
||||
True if successful or not needed, False if prerequisites are missing
|
||||
"""
|
||||
result = rename_spec_dir_from_requirements(self.spec_dir)
|
||||
# Update self.spec_dir if it was renamed
|
||||
if result and self.spec_dir.name.endswith("-pending"):
|
||||
# Find the renamed directory
|
||||
parent = self.spec_dir.parent
|
||||
prefix = self.spec_dir.name[:4] # e.g., "001-"
|
||||
for candidate in parent.iterdir():
|
||||
if (
|
||||
candidate.name.startswith(prefix)
|
||||
and "pending" not in candidate.name
|
||||
):
|
||||
self.spec_dir = candidate
|
||||
break
|
||||
return result
|
||||
# Check prerequisites first
|
||||
requirements_file = self.spec_dir / "requirements.json"
|
||||
if not requirements_file.exists():
|
||||
return False
|
||||
|
||||
try:
|
||||
with open(requirements_file, encoding="utf-8") as f:
|
||||
req = json.load(f)
|
||||
task_desc = req.get("task_description", "")
|
||||
if not task_desc:
|
||||
return False
|
||||
except (json.JSONDecodeError, OSError):
|
||||
return False
|
||||
|
||||
# Attempt rename
|
||||
new_spec_dir = rename_spec_dir_from_requirements(self.spec_dir)
|
||||
if new_spec_dir != self.spec_dir:
|
||||
self.spec_dir = new_spec_dir
|
||||
self.validator = SpecValidator(self.spec_dir)
|
||||
return True
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "auto-claude-ui",
|
||||
"version": "2.7.6-beta.3",
|
||||
"version": "2.7.6",
|
||||
"type": "module",
|
||||
"description": "Desktop UI for Auto Claude autonomous coding framework",
|
||||
"homepage": "https://github.com/AndyMik90/Auto-Claude",
|
||||
|
||||
@@ -186,7 +186,7 @@ describe('Subprocess Spawn Integration', () => {
|
||||
'Test task description'
|
||||
]),
|
||||
expect.objectContaining({
|
||||
cwd: AUTO_CLAUDE_SOURCE, // Process runs from auto-claude source directory
|
||||
cwd: TEST_PROJECT_PATH, // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
|
||||
env: expect.objectContaining({
|
||||
PYTHONUNBUFFERED: '1'
|
||||
})
|
||||
@@ -218,7 +218,7 @@ describe('Subprocess Spawn Integration', () => {
|
||||
'spec-001'
|
||||
]),
|
||||
expect.objectContaining({
|
||||
cwd: AUTO_CLAUDE_SOURCE // Process runs from auto-claude source directory
|
||||
cwd: TEST_PROJECT_PATH // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
|
||||
})
|
||||
);
|
||||
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
|
||||
@@ -248,7 +248,7 @@ describe('Subprocess Spawn Integration', () => {
|
||||
'--qa'
|
||||
]),
|
||||
expect.objectContaining({
|
||||
cwd: AUTO_CLAUDE_SOURCE // Process runs from auto-claude source directory
|
||||
cwd: TEST_PROJECT_PATH // Process runs from project directory to avoid cross-drive issues on Windows (#1661)
|
||||
})
|
||||
);
|
||||
}, 30000); // Increase timeout for Windows CI (dynamic imports are slow)
|
||||
|
||||
@@ -0,0 +1,301 @@
|
||||
/**
|
||||
* Unit tests for FileWatcher concurrency mechanisms
|
||||
* Tests deduplication, supersession, cancellation, and unwatchAll behaviour
|
||||
* under concurrent watch()/unwatch() call patterns.
|
||||
*/
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||
import { EventEmitter } from 'events';
|
||||
import path from 'path';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mock chokidar BEFORE importing FileWatcher so the module sees our mock.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// A minimal FSWatcher stub that lets us control when close() resolves.
|
||||
class MockFSWatcher extends EventEmitter {
|
||||
close: ReturnType<typeof vi.fn>;
|
||||
constructor(closeImpl?: () => Promise<void>) {
|
||||
super();
|
||||
this.close = vi.fn(closeImpl ?? (() => Promise.resolve()));
|
||||
}
|
||||
}
|
||||
|
||||
// Track every watcher created so tests can inspect them.
|
||||
let createdWatchers: MockFSWatcher[] = [];
|
||||
// Factory override — tests replace this to inject custom stubs.
|
||||
let watchFactory: (() => MockFSWatcher) | null = null;
|
||||
|
||||
vi.mock('chokidar', () => ({
|
||||
default: {
|
||||
watch: vi.fn((_path: string, _opts: unknown) => {
|
||||
const watcher = watchFactory ? watchFactory() : new MockFSWatcher();
|
||||
createdWatchers.push(watcher);
|
||||
return watcher;
|
||||
})
|
||||
}
|
||||
}));
|
||||
|
||||
// Mock 'fs' so we can control existsSync / readFileSync without touching disk.
|
||||
vi.mock('fs', () => ({
|
||||
existsSync: vi.fn(() => true),
|
||||
readFileSync: vi.fn(() => JSON.stringify({ phases: [] }))
|
||||
}));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Import after mocks are registered
|
||||
// ---------------------------------------------------------------------------
|
||||
import { FileWatcher } from '../file-watcher';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
describe('FileWatcher concurrency', () => {
|
||||
let fw: FileWatcher;
|
||||
|
||||
beforeEach(() => {
|
||||
fw = new FileWatcher();
|
||||
createdWatchers = [];
|
||||
watchFactory = null;
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
// Clean up any watchers that are still open.
|
||||
await fw.unwatchAll();
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 1. Deduplication — same taskId + same specDir
|
||||
// -------------------------------------------------------------------------
|
||||
describe('deduplication: second watch() with same specDir is a no-op', () => {
|
||||
it('should only create one FSWatcher when watch() is called twice with the same specDir while the first is still in-flight', async () => {
|
||||
const specDir = '/project/.auto-claude/specs/001-task';
|
||||
const taskId = 'task-1';
|
||||
|
||||
// To create a real async gap we need an existing watcher whose close() is slow.
|
||||
// First, set up a watcher for taskId (completes synchronously).
|
||||
await fw.watch(taskId, specDir);
|
||||
expect(createdWatchers).toHaveLength(1);
|
||||
|
||||
// Replace close() with a slow one so the next watch() call has an async gap.
|
||||
const existingWatcher = createdWatchers[0];
|
||||
let resolveClose!: () => void;
|
||||
existingWatcher.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveClose = res; })
|
||||
);
|
||||
|
||||
// Now start two concurrent watch() calls for the SAME specDir.
|
||||
// Both will try to enter, but the second should be deduplicated.
|
||||
const watchPromise1 = fw.watch(taskId, specDir);
|
||||
const watchPromise2 = fw.watch(taskId, specDir);
|
||||
|
||||
// Resolve the close so both can proceed.
|
||||
resolveClose();
|
||||
await Promise.all([watchPromise1, watchPromise2]);
|
||||
|
||||
// Only one new FSWatcher should have been created (the second call was a no-op).
|
||||
// createdWatchers[0] is the original; createdWatchers[1] is the new one.
|
||||
expect(createdWatchers).toHaveLength(2);
|
||||
expect(fw.isWatching(taskId)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 2. Supersession — same taskId, different specDir
|
||||
// -------------------------------------------------------------------------
|
||||
describe('supersession: watch() with different specDir replaces the in-flight call', () => {
|
||||
it('should let the second call win when the first is awaiting close()', async () => {
|
||||
const taskId = 'task-2';
|
||||
const specDir1 = path.join('/project', '.auto-claude', 'specs', '001-first');
|
||||
const specDir2 = path.join('/project', '.auto-claude', 'specs', '002-second');
|
||||
|
||||
// First call installs an existing watcher (simulate: the watcher for
|
||||
// specDir1 is already set up so the second watch() needs to close it).
|
||||
// We do this by running the first watch() to completion first.
|
||||
await fw.watch(taskId, specDir1);
|
||||
expect(createdWatchers).toHaveLength(1);
|
||||
|
||||
// Now make the close() of the first watcher slow so there's an async gap
|
||||
// during which the second watch() can enter and supersede.
|
||||
const existingWatcher = createdWatchers[0];
|
||||
let resolveClose!: () => void;
|
||||
existingWatcher.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveClose = res; })
|
||||
);
|
||||
|
||||
// Start the second watch() — it will try to close the first watcher's
|
||||
// FSWatcher and will be awaiting that.
|
||||
const watch2Promise = fw.watch(taskId, specDir2);
|
||||
|
||||
// While the second watch() is awaiting close, start a THIRD call with
|
||||
// yet another specDir — this supersedes the second call.
|
||||
// Actually for the test described in the finding, we want:
|
||||
// - First call bails, second call creates the watcher.
|
||||
// Let's resolve the close and let watch2 finish.
|
||||
resolveClose();
|
||||
await watch2Promise;
|
||||
|
||||
// The final watcher should be for specDir2.
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir2);
|
||||
// Two watchers were created in total (one for each specDir).
|
||||
expect(createdWatchers).toHaveLength(2);
|
||||
});
|
||||
|
||||
it('first watch() bails when pendingWatches changes to a different specDir', async () => {
|
||||
const taskId = 'task-super';
|
||||
const specDir1 = path.join('/project', '.auto-claude', 'specs', 'super-first');
|
||||
const specDir2 = path.join('/project', '.auto-claude', 'specs', 'super-second');
|
||||
|
||||
// Make the first watcher's close() slow so we can interleave.
|
||||
let resolveFirstClose!: () => void;
|
||||
watchFactory = () => {
|
||||
const w = new MockFSWatcher(() => new Promise<void>((res) => { resolveFirstClose = res; }));
|
||||
return w;
|
||||
};
|
||||
|
||||
// Start first watch().
|
||||
const watch1Promise = fw.watch(taskId, specDir1);
|
||||
|
||||
// Immediately start second watch() — before the first has resolved the
|
||||
// slow close(). At this point specDir1 watch hasn't even created an
|
||||
// FSWatcher yet (it's the very first call so there's no existing watcher
|
||||
// to close), so watch1Promise may resolve synchronously up to watcher
|
||||
// creation. Reset factory to normal for subsequent watcher creations.
|
||||
watchFactory = null;
|
||||
|
||||
const watch2Promise = fw.watch(taskId, specDir2);
|
||||
|
||||
// Let any remaining microtasks run.
|
||||
await Promise.resolve();
|
||||
if (resolveFirstClose) resolveFirstClose();
|
||||
|
||||
await Promise.all([watch1Promise, watch2Promise]);
|
||||
|
||||
// The winning call (specDir2) should own the watcher.
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir2);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 3. Cancellation — unwatch() during in-flight watch()
|
||||
// -------------------------------------------------------------------------
|
||||
describe('cancellation: unwatch() during in-flight watch() prevents watcher creation', () => {
|
||||
it('should not create a watcher when unwatch() is called before the async gap resolves', async () => {
|
||||
const taskId = 'task-3';
|
||||
const specDir = '/project/.auto-claude/specs/003-cancel';
|
||||
|
||||
// There's no pre-existing watcher, so watch() won't call close(). But it
|
||||
// does go async (chokidar.watch is sync but we can test the cancellation
|
||||
// flag by calling unwatch() before watch() runs).
|
||||
// The real async gap in watch() is the existing.watcher.close() call.
|
||||
// For this test, let's pre-install a watcher so close() is called.
|
||||
|
||||
// Install a slow-close watcher for taskId by manually populating the map.
|
||||
// We can do that by running a first watch(), then replacing close().
|
||||
await fw.watch(taskId, specDir);
|
||||
|
||||
// Replace the watcher's close() with a slow one.
|
||||
const existingWatcher = createdWatchers[0];
|
||||
let resolveExistingClose!: () => void;
|
||||
existingWatcher.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveExistingClose = res; })
|
||||
);
|
||||
|
||||
// Start a second watch() — it will await the slow close().
|
||||
const specDir2 = '/project/.auto-claude/specs/003-cancel-v2';
|
||||
const watchPromise = fw.watch(taskId, specDir2);
|
||||
|
||||
// While watch() is in-flight, call unwatch().
|
||||
await fw.unwatch(taskId);
|
||||
|
||||
// Now resolve the slow close so watch() can continue past the await.
|
||||
resolveExistingClose();
|
||||
await watchPromise;
|
||||
|
||||
// No new watcher should have been registered.
|
||||
expect(fw.isWatching(taskId)).toBe(false);
|
||||
// Only one FSWatcher was ever created (the original one for specDir).
|
||||
expect(createdWatchers).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 4. unwatchAll() with pending watches
|
||||
// -------------------------------------------------------------------------
|
||||
describe('unwatchAll() cancels all pending watches', () => {
|
||||
it('should cancel pending watch() calls and clear pendingWatches', async () => {
|
||||
const taskId1 = 'task-4a';
|
||||
const taskId2 = 'task-4b';
|
||||
const specDir1 = '/project/.auto-claude/specs/004a';
|
||||
const specDir2 = '/project/.auto-claude/specs/004b';
|
||||
|
||||
// Set up slow-close scenario for taskId1 (so watch() is in-flight).
|
||||
await fw.watch(taskId1, specDir1);
|
||||
const watcher1 = createdWatchers[0];
|
||||
let resolveClose1!: () => void;
|
||||
watcher1.close = vi.fn(
|
||||
() => new Promise<void>((res) => { resolveClose1 = res; })
|
||||
);
|
||||
|
||||
// Start a new watch for taskId1 with a different specDir — this is now in-flight.
|
||||
const newSpecDir1 = '/project/.auto-claude/specs/004a-v2';
|
||||
const watchPromise1 = fw.watch(taskId1, newSpecDir1);
|
||||
|
||||
// Start a fresh watch for taskId2.
|
||||
await fw.watch(taskId2, specDir2);
|
||||
|
||||
// Call unwatchAll() while watchPromise1 is still pending.
|
||||
const unwatchAllPromise = fw.unwatchAll();
|
||||
|
||||
// Resolve the slow close so everything can proceed.
|
||||
resolveClose1();
|
||||
await Promise.all([watchPromise1, unwatchAllPromise]);
|
||||
|
||||
// After unwatchAll, no watchers should be active.
|
||||
expect(fw.isWatching(taskId1)).toBe(false);
|
||||
expect(fw.isWatching(taskId2)).toBe(false);
|
||||
|
||||
// pendingWatches should be cleared (we verify indirectly: a fresh
|
||||
// watch() call for taskId1 must succeed without treating it as a duplicate).
|
||||
const specDirFresh = path.join('/project', '.auto-claude', 'specs', '004a-fresh');
|
||||
await fw.watch(taskId1, specDirFresh);
|
||||
expect(fw.isWatching(taskId1)).toBe(true);
|
||||
expect(fw.getWatchedSpecDir(taskId1)).toBe(specDirFresh);
|
||||
});
|
||||
});
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
// 5. getWatchedSpecDir() returns correct specDir
|
||||
// -------------------------------------------------------------------------
|
||||
describe('getWatchedSpecDir()', () => {
|
||||
it('returns the specDir that was passed to watch()', async () => {
|
||||
const taskId = 'task-5';
|
||||
const specDir = path.join('/project', '.auto-claude', 'specs', '005-specdir');
|
||||
|
||||
await fw.watch(taskId, specDir);
|
||||
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir);
|
||||
});
|
||||
|
||||
it('returns null when the task is not being watched', () => {
|
||||
expect(fw.getWatchedSpecDir('unknown-task')).toBeNull();
|
||||
});
|
||||
|
||||
it('returns updated specDir after re-watch with different specDir', async () => {
|
||||
const taskId = 'task-5b';
|
||||
const specDir1 = path.join('/project', '.auto-claude', 'specs', '005b-first');
|
||||
const specDir2 = path.join('/project', '.auto-claude', 'specs', '005b-second');
|
||||
|
||||
await fw.watch(taskId, specDir1);
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir1);
|
||||
|
||||
await fw.watch(taskId, specDir2);
|
||||
expect(fw.getWatchedSpecDir(taskId)).toBe(specDir2);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -28,7 +28,7 @@ function setupTestDirs(): void {
|
||||
TEST_DIR = mkdtempSync(path.join(tmpdir(), 'project-store-test-'));
|
||||
USER_DATA_PATH = path.join(TEST_DIR, 'userData');
|
||||
TEST_PROJECT_PATH = path.join(TEST_DIR, 'test-project');
|
||||
|
||||
|
||||
mkdirSync(USER_DATA_PATH, { recursive: true });
|
||||
mkdirSync(path.join(USER_DATA_PATH, 'store'), { recursive: true });
|
||||
mkdirSync(TEST_PROJECT_PATH, { recursive: true });
|
||||
|
||||
@@ -1012,3 +1012,115 @@ 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);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -329,7 +329,9 @@ export class AgentManager extends EventEmitter {
|
||||
this.registerTaskWithOperationRegistry(taskId, 'spec-creation', { projectPath, taskDescription, specDir });
|
||||
|
||||
// Note: This is spec-creation but it chains to task-execution via run.py
|
||||
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'task-execution', projectId);
|
||||
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive file access
|
||||
// issues on Windows. The script path is absolute so Python finds its modules via sys.path[0]. (#1661)
|
||||
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'task-execution', projectId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -410,7 +412,10 @@ export class AgentManager extends EventEmitter {
|
||||
// Register with unified OperationRegistry for proactive swap support
|
||||
this.registerTaskWithOperationRegistry(taskId, 'task-execution', { projectPath, specId, options });
|
||||
|
||||
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'task-execution', projectId);
|
||||
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive file access
|
||||
// issues on Windows. The script path (runPath) is absolute so Python finds its modules
|
||||
// via sys.path[0] which is set to the script's directory. (#1661)
|
||||
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'task-execution', projectId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -448,7 +453,8 @@ export class AgentManager extends EventEmitter {
|
||||
|
||||
const args = [runPath, '--spec', specId, '--project-dir', projectPath, '--qa'];
|
||||
|
||||
await this.processManager.spawnProcess(taskId, autoBuildSource, args, combinedEnv, 'qa-process', projectId);
|
||||
// Use projectPath as cwd instead of autoBuildSource to avoid cross-drive issues on Windows (#1661)
|
||||
await this.processManager.spawnProcess(taskId, projectPath, args, combinedEnv, 'qa-process', projectId);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -799,4 +799,127 @@ 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).mockReturnValue({
|
||||
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).mockReturnValue({
|
||||
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).mockReturnValue({
|
||||
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).mockReturnValue({
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -22,10 +22,11 @@ import { pythonEnvManager, getConfiguredPythonPath } from '../python-env-manager
|
||||
import { buildMemoryEnvVars } from '../memory-env-builder';
|
||||
import { readSettingsFile } from '../settings-utils';
|
||||
import type { AppSettings } from '../../shared/types/settings';
|
||||
import { getOAuthModeClearVars } from './env-utils';
|
||||
import { getOAuthModeClearVars, normalizeEnvPathKey, mergePythonEnvPath } from './env-utils';
|
||||
import { getAugmentedEnv } from '../env-utils';
|
||||
import { getToolInfo, getClaudeCliPathForSdk } from '../cli-tool-manager';
|
||||
import { killProcessGracefully, isWindows } from '../platform';
|
||||
import { killProcessGracefully, isWindows, getPathDelimiter } from '../platform';
|
||||
import { debugLog } from '../../shared/utils/debug-logger';
|
||||
|
||||
/**
|
||||
* Type for supported CLI tools
|
||||
@@ -178,6 +179,29 @@ export class AgentProcessManager {
|
||||
// Get best available Claude profile environment (automatically handles rate limits)
|
||||
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();
|
||||
@@ -205,7 +229,9 @@ export class AgentProcessManager {
|
||||
const ghCliEnv = this.detectAndSetCliPath('gh');
|
||||
const glabCliEnv = this.detectAndSetCliPath('glab');
|
||||
|
||||
return {
|
||||
// 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 = {
|
||||
...augmentedEnv,
|
||||
...gitBashEnv,
|
||||
...claudeCliEnv,
|
||||
@@ -217,6 +243,29 @@ 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 handleProcessFailure(
|
||||
@@ -615,7 +664,32 @@ export class AgentProcessManager {
|
||||
// Get OAuth mode clearing vars (clears stale ANTHROPIC_* vars when in OAuth mode)
|
||||
const oauthModeClearVars = getOAuthModeClearVars(apiProfileEnv);
|
||||
|
||||
// Parse Python commandto handle space-separated commands like "py -3"
|
||||
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)',
|
||||
},
|
||||
});
|
||||
|
||||
// Merge PATH from pythonEnv with augmented PATH from env.
|
||||
// pythonEnv may contain its own PATH (e.g., on Windows with pywin32_system32 prepended).
|
||||
// Simply spreading pythonEnv after env would overwrite the augmented PATH (which includes
|
||||
// npm globals, homebrew, etc.), causing "Claude code not found" on Windows (#1661).
|
||||
// mergePythonEnvPath() normalizes PATH key casing and prepends pythonEnv-specific paths.
|
||||
const mergedPythonEnv = { ...pythonEnv };
|
||||
const pathSep = getPathDelimiter();
|
||||
|
||||
mergePythonEnvPath(env as Record<string, string | undefined>, mergedPythonEnv as Record<string, string | undefined>, pathSep);
|
||||
|
||||
// Parse Python command to handle space-separated commands like "py -3"
|
||||
const [pythonCommand, pythonBaseArgs] = parsePythonCommand(this.getPythonPath());
|
||||
let childProcess;
|
||||
try {
|
||||
@@ -623,7 +697,7 @@ export class AgentProcessManager {
|
||||
cwd,
|
||||
env: {
|
||||
...env, // Already includes process.env, extraEnv, profileEnv, PYTHONUNBUFFERED, PYTHONUTF8
|
||||
...pythonEnv, // Include Python environment (PYTHONPATH for bundled packages)
|
||||
...mergedPythonEnv, // Python env with merged PATH (preserves augmented PATH entries)
|
||||
...oauthModeClearVars, // Clear stale ANTHROPIC_* vars when in OAuth mode
|
||||
...apiProfileEnv // Include active API profile config (highest priority for ANTHROPIC_* vars)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import type { IdeationConfig, Idea } from '../../shared/types';
|
||||
import { AUTO_BUILD_PATHS } from '../../shared/constants';
|
||||
import { detectRateLimit, createSDKRateLimitInfo, getBestAvailableProfileEnv } from '../rate-limit-detector';
|
||||
import { getAPIProfileEnv } from '../services/profile';
|
||||
import { getOAuthModeClearVars } from './env-utils';
|
||||
import { getOAuthModeClearVars, normalizeEnvPathKey } from './env-utils';
|
||||
import { debugLog, debugError } from '../../shared/utils/debug-logger';
|
||||
import { stripAnsiCodes } from '../../shared/utils/ansi-sanitizer';
|
||||
import { parsePythonCommand } from '../python-detector';
|
||||
@@ -397,6 +397,12 @@ export class AgentQueueManager {
|
||||
PYTHONUTF8: '1'
|
||||
};
|
||||
|
||||
// Normalize PATH key to a single uppercase 'PATH' entry.
|
||||
// On Windows, process.env spread produces 'Path' while pythonEnv may write 'PATH',
|
||||
// resulting in duplicate keys in the final object. Without normalization the child
|
||||
// process inherits both keys, which can cause tool-not-found errors (#1661).
|
||||
normalizeEnvPathKey(finalEnv as Record<string, string | undefined>);
|
||||
|
||||
// Debug: Show OAuth token source (token values intentionally omitted for security - AC4)
|
||||
const tokenSource = profileEnv['CLAUDE_CODE_OAUTH_TOKEN']
|
||||
? 'Electron app profile'
|
||||
@@ -730,6 +736,12 @@ export class AgentQueueManager {
|
||||
PYTHONUTF8: '1'
|
||||
};
|
||||
|
||||
// Normalize PATH key to a single uppercase 'PATH' entry.
|
||||
// On Windows, process.env spread produces 'Path' while pythonEnv may write 'PATH',
|
||||
// resulting in duplicate keys in the final object. Without normalization the child
|
||||
// process inherits both keys, which can cause tool-not-found errors (#1661).
|
||||
normalizeEnvPathKey(finalEnv as Record<string, string | undefined>);
|
||||
|
||||
// Debug: Show OAuth token source (token values intentionally omitted for security - AC4)
|
||||
const tokenSource = profileEnv['CLAUDE_CODE_OAUTH_TOKEN']
|
||||
? 'Electron app profile'
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
*/
|
||||
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { getOAuthModeClearVars } from './env-utils';
|
||||
import { getOAuthModeClearVars, normalizeEnvPathKey, mergePythonEnvPath } from './env-utils';
|
||||
|
||||
describe('getOAuthModeClearVars', () => {
|
||||
describe('OAuth mode (no active API profile)', () => {
|
||||
@@ -132,3 +132,166 @@ describe('getOAuthModeClearVars', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeEnvPathKey', () => {
|
||||
it('should leave an already-uppercase PATH key untouched', () => {
|
||||
const env: Record<string, string | undefined> = { PATH: '/usr/bin:/bin', HOME: '/home/user' };
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env).toEqual({ PATH: '/usr/bin:/bin', HOME: '/home/user' });
|
||||
});
|
||||
|
||||
it('should rename a lowercase-variant "Path" key to "PATH"', () => {
|
||||
const env: Record<string, string | undefined> = { Path: 'C:\\Windows\\system32', HOME: '/home/user' };
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env['PATH']).toBe('C:\\Windows\\system32');
|
||||
expect('Path' in env).toBe(false);
|
||||
});
|
||||
|
||||
it('should prefer existing "PATH" and remove "Path" when both keys coexist', () => {
|
||||
// Simulates process.env spread ('Path') after getAugmentedEnv writes ('PATH')
|
||||
const env: Record<string, string | undefined> = {
|
||||
Path: 'C:\\old',
|
||||
PATH: 'C:\\Windows\\system32;C:\\augmented',
|
||||
HOME: '/home/user'
|
||||
};
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env.PATH).toBe('C:\\Windows\\system32;C:\\augmented');
|
||||
expect('Path' in env).toBe(false);
|
||||
});
|
||||
|
||||
it('should remove all case-variant PATH duplicates when PATH is already present', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: '/correct',
|
||||
Path: '/old1',
|
||||
path: '/old2'
|
||||
};
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env.PATH).toBe('/correct');
|
||||
expect('Path' in env).toBe(false);
|
||||
expect('path' in env).toBe(false);
|
||||
});
|
||||
|
||||
it('should handle env with no PATH-like key gracefully', () => {
|
||||
const env: Record<string, string | undefined> = { HOME: '/home/user', SHELL: '/bin/zsh' };
|
||||
normalizeEnvPathKey(env);
|
||||
expect(env).toEqual({ HOME: '/home/user', SHELL: '/bin/zsh' });
|
||||
});
|
||||
|
||||
it('should return the same env object reference (mutates in place)', () => {
|
||||
const env: Record<string, string | undefined> = { PATH: '/usr/bin' };
|
||||
const result = normalizeEnvPathKey(env);
|
||||
expect(result).toBe(env);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mergePythonEnvPath - Windows PATH merge logic (#1661)', () => {
|
||||
const SEP = ';'; // Use Windows separator for these tests
|
||||
|
||||
it('should prepend pythonEnv-only entries to the augmented PATH', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\pywin32_system32;C:\\npm;C:\\homebrew'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// pywin32_system32 is unique to pythonEnv, so it should be prepended
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should deduplicate entries that already exist in augmented PATH', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew;C:\\pywin32_system32'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\pywin32_system32;C:\\npm'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// All pythonEnv entries are already in env.PATH, so mergedPythonEnv.PATH should equal env.PATH
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\npm;C:\\homebrew;C:\\pywin32_system32');
|
||||
});
|
||||
|
||||
it('should normalize Windows-style "Path" key in pythonEnv to "PATH"', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
// pythonEnv uses 'Path' (Windows native casing)
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
Path: 'C:\\pywin32_system32;C:\\npm'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// 'Path' should be normalized to 'PATH' and pythonEnv-specific entry prepended
|
||||
expect('Path' in mergedPythonEnv).toBe(false);
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should normalize Windows-style "Path" in env and deduplicate duplicates', () => {
|
||||
// Simulates process.env spread ('Path') + getAugmentedEnv write ('PATH') leaving both
|
||||
const env: Record<string, string | undefined> = {
|
||||
Path: 'C:\\old',
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\pywin32_system32;C:\\npm'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// env 'Path' should be removed; augmented 'PATH' value preserved
|
||||
expect('Path' in env).toBe(false);
|
||||
expect(env.PATH).toBe('C:\\npm;C:\\homebrew');
|
||||
// Only the unique pywin32_system32 entry prepended
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\pywin32_system32;C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should use env.PATH unchanged when pythonEnv has no unique entries', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
expect(mergedPythonEnv.PATH).toBe('C:\\npm;C:\\homebrew');
|
||||
});
|
||||
|
||||
it('should work correctly with Unix colon separator', () => {
|
||||
const unixSep = ':';
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: '/usr/bin:/bin'
|
||||
};
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PATH: '/opt/pyenv/shims:/usr/bin:/bin'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, unixSep);
|
||||
|
||||
// /opt/pyenv/shims is unique and should be prepended
|
||||
expect(mergedPythonEnv.PATH).toBe('/opt/pyenv/shims:/usr/bin:/bin');
|
||||
});
|
||||
|
||||
it('should handle missing PATH in pythonEnv gracefully (no-op)', () => {
|
||||
const env: Record<string, string | undefined> = {
|
||||
PATH: 'C:\\npm;C:\\homebrew'
|
||||
};
|
||||
// pythonEnv has no PATH at all
|
||||
const mergedPythonEnv: Record<string, string | undefined> = {
|
||||
PYTHONPATH: '/site-packages'
|
||||
};
|
||||
|
||||
mergePythonEnvPath(env, mergedPythonEnv, SEP);
|
||||
|
||||
// Nothing should change
|
||||
expect(mergedPythonEnv.PATH).toBeUndefined();
|
||||
expect(mergedPythonEnv.PYTHONPATH).toBe('/site-packages');
|
||||
expect(env.PATH).toBe('C:\\npm;C:\\homebrew');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -2,6 +2,88 @@
|
||||
* Utility functions for managing environment variables in agent spawning
|
||||
*/
|
||||
|
||||
/**
|
||||
* Normalize the PATH key in an environment object to a single uppercase 'PATH' key.
|
||||
*
|
||||
* On Windows, process.env spreads as 'Path' (the native casing) while getAugmentedEnv()
|
||||
* writes 'PATH'. Without normalization, both keys coexist in the object and the child
|
||||
* process receives duplicate PATH entries, causing tool-not-found errors like #1661.
|
||||
*
|
||||
* Mutates the provided env object in place and returns it for convenience.
|
||||
*
|
||||
* @param env - Mutable environment record to normalize
|
||||
* @returns The same env object with PATH normalized to uppercase
|
||||
*/
|
||||
export function normalizeEnvPathKey(env: Record<string, string | undefined>): Record<string, string | undefined> {
|
||||
// If 'PATH' already exists, delete all other case-variant keys (e.g. 'Path')
|
||||
if ('PATH' in env) {
|
||||
for (const key of Object.keys(env)) {
|
||||
if (key !== 'PATH' && key.toUpperCase() === 'PATH') {
|
||||
delete env[key];
|
||||
}
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
// No uppercase 'PATH' key - find the first case-variant and rename it
|
||||
const pathKey = Object.keys(env).find(k => k.toUpperCase() === 'PATH');
|
||||
if (pathKey) {
|
||||
env['PATH'] = env[pathKey];
|
||||
delete env[pathKey];
|
||||
// Remove any remaining case-variant keys
|
||||
for (const key of Object.keys(env)) {
|
||||
if (key !== 'PATH' && key.toUpperCase() === 'PATH') {
|
||||
delete env[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge pythonEnv PATH entries with the augmented PATH in env, deduplicating entries.
|
||||
*
|
||||
* pythonEnv may carry its own PATH (e.g. pywin32_system32 prepended on Windows).
|
||||
* Simply spreading pythonEnv after env would overwrite the augmented PATH (which
|
||||
* includes npm globals, Homebrew, etc.), causing "Claude code not found" (#1661).
|
||||
*
|
||||
* Strategy:
|
||||
* 1. Normalize PATH key casing in both env and pythonEnv to uppercase 'PATH'.
|
||||
* 2. Extract only pythonEnv PATH entries that are not already in env.PATH.
|
||||
* 3. Prepend those unique entries to env.PATH and store the result in pythonEnv.PATH.
|
||||
*
|
||||
* Mutates mergedPythonEnv in place (caller should pass a shallow copy if immutability is needed).
|
||||
*
|
||||
* @param env - The base environment (already augmented with tool paths)
|
||||
* @param mergedPythonEnv - Shallow copy of pythonEnv to merge PATH into
|
||||
* @param pathSep - Platform path separator (';' on Windows, ':' elsewhere)
|
||||
*/
|
||||
export function mergePythonEnvPath(
|
||||
env: Record<string, string | undefined>,
|
||||
mergedPythonEnv: Record<string, string | undefined>,
|
||||
pathSep: string
|
||||
): void {
|
||||
// Normalize PATH key to uppercase in both objects
|
||||
normalizeEnvPathKey(env);
|
||||
normalizeEnvPathKey(mergedPythonEnv);
|
||||
|
||||
if (mergedPythonEnv['PATH'] && env['PATH']) {
|
||||
const augmentedPathEntries = new Set(
|
||||
(env['PATH'] as string).split(pathSep).filter(Boolean)
|
||||
);
|
||||
// Extract only new entries from pythonEnv.PATH that aren't already in the augmented PATH
|
||||
const pythonPathEntries = (mergedPythonEnv['PATH'] as string)
|
||||
.split(pathSep)
|
||||
.filter(entry => entry && !augmentedPathEntries.has(entry));
|
||||
|
||||
// Prepend python-specific paths (e.g., pywin32_system32) to the augmented PATH
|
||||
mergedPythonEnv['PATH'] = pythonPathEntries.length > 0
|
||||
? [...pythonPathEntries, env['PATH'] as string].join(pathSep)
|
||||
: env['PATH'] as string;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get environment variables to clear ANTHROPIC_* vars when in OAuth mode
|
||||
*
|
||||
|
||||
@@ -45,6 +45,68 @@ type UpdateChannel = 'latest' | 'beta';
|
||||
// Store interval ID for cleanup during shutdown
|
||||
let periodicCheckIntervalId: ReturnType<typeof setInterval> | null = null;
|
||||
|
||||
/**
|
||||
* Convert basic HTML (from GitHub release bodies) to markdown.
|
||||
* Handles the common tags GitHub uses in release notes.
|
||||
*/
|
||||
function htmlToMarkdown(html: string): string {
|
||||
let md = html;
|
||||
|
||||
// Block-level replacements
|
||||
md = md.replace(/<h1[^>]*>(.*?)<\/h1>/gi, '# $1\n\n');
|
||||
md = md.replace(/<h2[^>]*>(.*?)<\/h2>/gi, '## $1\n\n');
|
||||
md = md.replace(/<h3[^>]*>(.*?)<\/h3>/gi, '### $1\n\n');
|
||||
md = md.replace(/<h4[^>]*>(.*?)<\/h4>/gi, '#### $1\n\n');
|
||||
|
||||
// Lists: convert <ol>/<ul> with <li> items
|
||||
// First handle <li> within <ol> (numbered)
|
||||
md = md.replace(/<ol[^>]*>([\s\S]*?)<\/ol>/gi, (_match, content: string) => {
|
||||
let i = 0;
|
||||
return content.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, (_m: string, text: string) => {
|
||||
i++;
|
||||
return `${i}. ${text.trim()}\n`;
|
||||
}) + '\n';
|
||||
});
|
||||
// Then <li> within <ul> (bulleted)
|
||||
md = md.replace(/<ul[^>]*>([\s\S]*?)<\/ul>/gi, (_match, content: string) => {
|
||||
return content.replace(/<li[^>]*>([\s\S]*?)<\/li>/gi, (_m: string, text: string) => {
|
||||
return `- ${text.trim()}\n`;
|
||||
}) + '\n';
|
||||
});
|
||||
|
||||
// Inline replacements
|
||||
md = md.replace(/<strong[^>]*>(.*?)<\/strong>/gi, '**$1**');
|
||||
md = md.replace(/<b[^>]*>(.*?)<\/b>/gi, '**$1**');
|
||||
md = md.replace(/<em[^>]*>(.*?)<\/em>/gi, '*$1*');
|
||||
md = md.replace(/<i[^>]*>(.*?)<\/i>/gi, '*$1*');
|
||||
md = md.replace(/<code[^>]*>(.*?)<\/code>/gi, '`$1`');
|
||||
md = md.replace(/<tt[^>]*>(.*?)<\/tt>/gi, '`$1`');
|
||||
md = md.replace(/<a[^>]*href="([^"]*)"[^>]*>(.*?)<\/a>/gi, '[$2]($1)');
|
||||
|
||||
// Block elements
|
||||
md = md.replace(/<p[^>]*>([\s\S]*?)<\/p>/gi, '$1\n\n');
|
||||
md = md.replace(/<br\s*\/?>/gi, '\n');
|
||||
md = md.replace(/<hr\s*\/?>/gi, '---\n\n');
|
||||
|
||||
// Remove any remaining HTML tags (loop to handle nested tag fragments)
|
||||
while (/<[^>]+>/.test(md)) {
|
||||
md = md.replace(/<[^>]+>/g, '');
|
||||
}
|
||||
|
||||
// Decode common HTML entities (& LAST to prevent double-unescaping like &lt; → < → <)
|
||||
md = md.replace(/</g, '<');
|
||||
md = md.replace(/>/g, '>');
|
||||
md = md.replace(/"/g, '"');
|
||||
md = md.replace(/'/g, "'");
|
||||
md = md.replace(/ /g, ' ');
|
||||
md = md.replace(/&/g, '&');
|
||||
|
||||
// Clean up excessive whitespace
|
||||
md = md.replace(/\n{3,}/g, '\n\n');
|
||||
|
||||
return md.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert releaseNotes from electron-updater to a markdown string.
|
||||
* releaseNotes can be:
|
||||
@@ -57,8 +119,12 @@ function formatReleaseNotes(releaseNotes: UpdateInfo['releaseNotes']): string |
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// If it's already a string, return as-is
|
||||
// If it's a string, convert HTML to markdown if needed
|
||||
// electron-updater returns GitHub release bodies as HTML
|
||||
if (typeof releaseNotes === 'string') {
|
||||
if (releaseNotes.trimStart().startsWith('<')) {
|
||||
return htmlToMarkdown(releaseNotes);
|
||||
}
|
||||
return releaseNotes;
|
||||
}
|
||||
|
||||
@@ -74,8 +140,12 @@ function formatReleaseNotes(releaseNotes: UpdateInfo['releaseNotes']): string |
|
||||
.filter(item => item.note) // Filter out entries with null/undefined notes
|
||||
.map(item => {
|
||||
// Each item has version and note properties
|
||||
// note can be HTML (GitHub provider) so convert if needed
|
||||
const versionHeader = item.version ? `## ${item.version}\n` : '';
|
||||
return `${versionHeader}${item.note}`;
|
||||
const note = typeof item.note === 'string' && item.note.trimStart().startsWith('<')
|
||||
? htmlToMarkdown(item.note)
|
||||
: item.note;
|
||||
return `${versionHeader}${note}`;
|
||||
})
|
||||
.join('\n\n');
|
||||
|
||||
|
||||
@@ -20,8 +20,10 @@ import type {
|
||||
ClaudeProfileSettings,
|
||||
ClaudeUsageData,
|
||||
ClaudeRateLimitEvent,
|
||||
ClaudeAutoSwitchSettings
|
||||
ClaudeAutoSwitchSettings,
|
||||
APIProfile
|
||||
} from '../shared/types';
|
||||
import type { UnifiedAccount } from '../shared/types/unified-account';
|
||||
|
||||
// Module imports
|
||||
import { encryptToken, decryptToken } from './claude-profile/token-encryption';
|
||||
@@ -40,9 +42,11 @@ import {
|
||||
import {
|
||||
getBestAvailableProfile,
|
||||
shouldProactivelySwitch as shouldProactivelySwitchImpl,
|
||||
getProfilesSortedByAvailability as getProfilesSortedByAvailabilityImpl
|
||||
getProfilesSortedByAvailability as getProfilesSortedByAvailabilityImpl,
|
||||
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,
|
||||
@@ -52,6 +56,7 @@ import {
|
||||
expandHomePath,
|
||||
getEmailFromConfigDir
|
||||
} from './claude-profile/profile-utils';
|
||||
import { debugLog } from '../shared/utils/debug-logger';
|
||||
|
||||
/**
|
||||
* Manages Claude Code profiles for multi-account support.
|
||||
@@ -82,6 +87,8 @@ 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 });
|
||||
|
||||
@@ -89,6 +96,9 @@ 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
|
||||
@@ -100,6 +110,7 @@ export class ClaudeProfileManager {
|
||||
this.populateSubscriptionMetadata();
|
||||
|
||||
this.initialized = true;
|
||||
console.log('[ClaudeProfileManager] Initialization complete');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -145,13 +156,20 @@ 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;
|
||||
}
|
||||
|
||||
@@ -538,8 +556,27 @@ export class ClaudeProfileManager {
|
||||
if (process.env.DEBUG === 'true') {
|
||||
console.warn('[ClaudeProfileManager] Using CLAUDE_CONFIG_DIR for profile:', profile.name, expandedConfigDir);
|
||||
}
|
||||
} else {
|
||||
console.warn('[ClaudeProfileManager] Profile has no configDir configured:', profile?.name);
|
||||
} 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})` : ''
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return env;
|
||||
@@ -666,6 +703,57 @@ export class ClaudeProfileManager {
|
||||
return getBestAvailableProfile(this.data.profiles, settings, excludeProfileId, priorityOrder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load API profiles from profiles.json with error handling
|
||||
* Shared helper to avoid duplication across methods
|
||||
*/
|
||||
private async loadProfilesFileSafe(): Promise<{ profiles: APIProfile[]; activeProfileId?: string }> {
|
||||
try {
|
||||
const file = await loadProfilesFile();
|
||||
return { profiles: file.profiles, activeProfileId: file.activeProfileId ?? undefined };
|
||||
} catch (error) {
|
||||
console.error('[ClaudeProfileManager] Failed to load profiles file:', error);
|
||||
return { profiles: [] };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if we should proactively switch profiles based on current usage
|
||||
*/
|
||||
@@ -746,8 +834,26 @@ 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 {};
|
||||
}
|
||||
|
||||
|
||||
@@ -1825,6 +1825,7 @@ 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) {
|
||||
@@ -2086,6 +2087,7 @@ 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)
|
||||
|
||||
@@ -10,9 +10,20 @@
|
||||
* - Must be below user's configured thresholds (default: 95% session, 99% weekly)
|
||||
* 3. First profile in priority order that passes all filters is selected
|
||||
* 4. If no profile passes all filters, falls back to "least bad" option
|
||||
*
|
||||
* v3 Enhancement: Unified Account Support
|
||||
* - Supports both OAuth profiles (ClaudeProfile) and API profiles (APIProfile)
|
||||
* - API profiles are always considered available (hasUnlimitedUsage = true)
|
||||
* - Unified selection algorithm considers both types in priority order
|
||||
*/
|
||||
|
||||
import type { ClaudeProfile, ClaudeAutoSwitchSettings } from '../../shared/types';
|
||||
import type { ClaudeProfile, ClaudeAutoSwitchSettings, APIProfile } from '../../shared/types';
|
||||
import type { UnifiedAccount } from '../../shared/types/unified-account';
|
||||
import {
|
||||
claudeProfileToUnified,
|
||||
apiProfileToUnified,
|
||||
OAUTH_ID_PREFIX
|
||||
} from '../../shared/utils/unified-account';
|
||||
import { isProfileRateLimited } from './rate-limit-manager';
|
||||
import { isProfileAuthenticated } from './profile-utils';
|
||||
|
||||
@@ -121,6 +132,236 @@ function calculateFallbackScore(
|
||||
return score;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// Unified Account Scoring (v3)
|
||||
// ============================================
|
||||
|
||||
interface ScoredUnifiedAccount {
|
||||
account: UnifiedAccount;
|
||||
score: number;
|
||||
priorityIndex: number;
|
||||
isAvailable: boolean;
|
||||
unavailableReason?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Options for unified account selection
|
||||
*/
|
||||
export interface UnifiedAccountSelectionOptions {
|
||||
/** Unified account ID to exclude (usually the current/failing one) */
|
||||
excludeAccountId?: string;
|
||||
/** User's configured priority order (array of unified IDs) */
|
||||
priorityOrder?: string[];
|
||||
/** Currently active OAuth profile ID (if any) */
|
||||
activeOAuthId?: string;
|
||||
/** Currently active API profile ID (if any) */
|
||||
activeAPIId?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Score a single unified account for availability
|
||||
*
|
||||
* @param account - The unified account to score
|
||||
* @param priorityIndex - Index in the user's priority order (lower = higher priority)
|
||||
* @param settings - Auto-switch settings containing usage thresholds
|
||||
*/
|
||||
function scoreUnifiedAccount(
|
||||
account: UnifiedAccount,
|
||||
priorityIndex: number,
|
||||
settings: ClaudeAutoSwitchSettings
|
||||
): ScoredUnifiedAccount {
|
||||
let score = 100;
|
||||
let unavailableReason: string | undefined;
|
||||
let isOverThreshold = false;
|
||||
|
||||
// For API profiles: simple availability check
|
||||
if (account.type === 'api') {
|
||||
if (!account.isAuthenticated) {
|
||||
score = -1000;
|
||||
unavailableReason = 'API key not validated';
|
||||
} else if (!account.isAvailable) {
|
||||
score = -500;
|
||||
unavailableReason = 'not available';
|
||||
}
|
||||
// API profiles with valid auth get high scores (no usage limits)
|
||||
|
||||
return {
|
||||
account,
|
||||
score,
|
||||
priorityIndex,
|
||||
isAvailable: score > 0,
|
||||
unavailableReason
|
||||
};
|
||||
}
|
||||
|
||||
// For OAuth profiles: detailed scoring with threshold enforcement
|
||||
if (!account.isAuthenticated) {
|
||||
score = -1000;
|
||||
unavailableReason = 'not authenticated';
|
||||
} else if (account.isRateLimited) {
|
||||
if (account.rateLimitType === 'weekly') {
|
||||
score = -500;
|
||||
} else {
|
||||
score = -200;
|
||||
}
|
||||
unavailableReason = `rate limited (${account.rateLimitType || 'unknown'})`;
|
||||
} else {
|
||||
// Check usage thresholds (matching checkProfileAvailability behavior)
|
||||
if (account.weeklyPercent !== undefined && account.weeklyPercent >= settings.weeklyThreshold) {
|
||||
isOverThreshold = true;
|
||||
unavailableReason = `weekly usage ${account.weeklyPercent}% >= threshold ${settings.weeklyThreshold}%`;
|
||||
} else if (account.sessionPercent !== undefined && account.sessionPercent >= settings.sessionThreshold) {
|
||||
isOverThreshold = true;
|
||||
unavailableReason = `session usage ${account.sessionPercent}% >= threshold ${settings.sessionThreshold}%`;
|
||||
}
|
||||
|
||||
// Apply proportional penalties for high usage (even if not over threshold)
|
||||
if (account.weeklyPercent !== undefined) {
|
||||
score -= account.weeklyPercent * 0.3;
|
||||
}
|
||||
if (account.sessionPercent !== undefined) {
|
||||
score -= account.sessionPercent * 0.1;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
account,
|
||||
score,
|
||||
priorityIndex,
|
||||
isAvailable: score > 0 && account.isAuthenticated === true && !account.isRateLimited && !isOverThreshold,
|
||||
unavailableReason
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the best unified account from both OAuth and API profiles
|
||||
*
|
||||
* Selection Logic:
|
||||
* 1. Convert all profiles to UnifiedAccount format
|
||||
* 2. Sort by user's priority order
|
||||
* 3. Filter by availability
|
||||
* 4. Return first available account in priority order
|
||||
* 5. If none available, return the "least bad" option
|
||||
*
|
||||
* @param oauthProfiles - All OAuth (Claude) profiles
|
||||
* @param apiProfiles - All API profiles
|
||||
* @param settings - Auto-switch settings (contains thresholds for OAuth)
|
||||
* @param options - Optional configuration for selection
|
||||
*/
|
||||
export function getBestAvailableUnifiedAccount(
|
||||
oauthProfiles: ClaudeProfile[],
|
||||
apiProfiles: APIProfile[],
|
||||
settings: ClaudeAutoSwitchSettings,
|
||||
options: UnifiedAccountSelectionOptions = {}
|
||||
): UnifiedAccount | null {
|
||||
const { excludeAccountId, priorityOrder = [], activeOAuthId, activeAPIId } = options;
|
||||
// Convert all profiles to unified format
|
||||
const unifiedAccounts: UnifiedAccount[] = [];
|
||||
|
||||
// Convert OAuth profiles
|
||||
for (const profile of oauthProfiles) {
|
||||
const isActive = profile.id === activeOAuthId;
|
||||
const rateLimitStatus = isProfileRateLimited(profile);
|
||||
// Compute authentication status - profile.isAuthenticated may not be set on raw profiles
|
||||
const isAuthenticated = isProfileAuthenticated(profile);
|
||||
|
||||
unifiedAccounts.push(claudeProfileToUnified(profile, isActive, {
|
||||
isRateLimited: rateLimitStatus.limited,
|
||||
rateLimitType: rateLimitStatus.type,
|
||||
isAuthenticated
|
||||
}));
|
||||
}
|
||||
|
||||
// Convert API profiles
|
||||
for (const profile of apiProfiles) {
|
||||
const isActive = profile.id === activeAPIId;
|
||||
// TODO: API profiles are considered authenticated if they have an API key.
|
||||
// Add validation tracking to distinguish "has key" from "key is confirmed valid".
|
||||
const isAuthenticated = !!profile.apiKey;
|
||||
unifiedAccounts.push(apiProfileToUnified(profile, isActive, isAuthenticated));
|
||||
}
|
||||
|
||||
// Filter out excluded account
|
||||
const candidates = unifiedAccounts.filter(a => a.id !== excludeAccountId);
|
||||
|
||||
if (candidates.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isDebug) {
|
||||
console.warn('[ProfileScorer] Evaluating', candidates.length, 'candidate accounts (excluding:', excludeAccountId, ')');
|
||||
console.warn('[ProfileScorer] Priority order:', priorityOrder);
|
||||
console.warn('[ProfileScorer] OAuth thresholds: session =', settings.sessionThreshold, '%, weekly =', settings.weeklyThreshold, '%');
|
||||
}
|
||||
|
||||
// Score and check availability for each account
|
||||
const scoredAccounts: ScoredUnifiedAccount[] = candidates.map(account => {
|
||||
const priorityIndex = priorityOrder.indexOf(account.id);
|
||||
const scored = scoreUnifiedAccount(account, priorityIndex === -1 ? Infinity : priorityIndex, settings);
|
||||
|
||||
if (isDebug) {
|
||||
console.warn('[ProfileScorer] Scoring account:', account.displayName, '(', account.id, ')');
|
||||
console.warn('[ProfileScorer] Type:', account.type);
|
||||
console.warn('[ProfileScorer] Priority index:', priorityIndex === -1 ? 'not in list (Infinity)' : priorityIndex);
|
||||
console.warn('[ProfileScorer] Available:', scored.isAvailable, scored.unavailableReason ? `(${scored.unavailableReason})` : '');
|
||||
if (account.type === 'oauth') {
|
||||
console.warn('[ProfileScorer] Usage:', `session=${account.sessionPercent}%, weekly=${account.weeklyPercent}%`);
|
||||
}
|
||||
console.warn('[ProfileScorer] Score:', scored.score);
|
||||
}
|
||||
|
||||
return scored;
|
||||
});
|
||||
|
||||
// Sort by:
|
||||
// 1. Available accounts first
|
||||
// 2. Within available: by priority index (lower = higher priority)
|
||||
// 3. Within unavailable: by score (higher = better, for "least bad" selection)
|
||||
scoredAccounts.sort((a, b) => {
|
||||
// Available accounts always come first
|
||||
if (a.isAvailable !== b.isAvailable) {
|
||||
return a.isAvailable ? -1 : 1;
|
||||
}
|
||||
|
||||
// For available accounts, sort by priority order
|
||||
if (a.isAvailable && b.isAvailable) {
|
||||
if (a.priorityIndex !== b.priorityIndex) {
|
||||
return a.priorityIndex - b.priorityIndex;
|
||||
}
|
||||
// Tiebreaker: prefer higher score
|
||||
return b.score - a.score;
|
||||
}
|
||||
|
||||
// For unavailable accounts, sort by score (for "least bad" selection)
|
||||
return b.score - a.score;
|
||||
});
|
||||
|
||||
const best = scoredAccounts[0];
|
||||
|
||||
if (best.isAvailable) {
|
||||
if (isDebug) {
|
||||
console.warn('[ProfileScorer] Best available account:', best.account.displayName,
|
||||
'(type:', best.account.type, ', priority index:', best.priorityIndex, ')');
|
||||
}
|
||||
return best.account;
|
||||
}
|
||||
|
||||
// No account meets all criteria - check if we should return the least bad option
|
||||
if (best.score > 0) {
|
||||
if (isDebug) {
|
||||
console.warn('[ProfileScorer] No ideal account available, using least-bad option:', best.account.displayName,
|
||||
'(type:', best.account.type, ', score:', best.score, ', reason:', best.unavailableReason, ')');
|
||||
}
|
||||
return best.account;
|
||||
}
|
||||
|
||||
// All accounts are truly unusable
|
||||
if (isDebug) {
|
||||
console.warn('[ProfileScorer] No usable account available, all have issues');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the best profile to switch to based on priority order and availability
|
||||
*
|
||||
@@ -157,7 +398,7 @@ export function getBestAvailableProfile(
|
||||
|
||||
// Score and check availability for each profile
|
||||
const scoredProfiles: ScoredProfile[] = candidates.map(profile => {
|
||||
const unifiedId = `oauth-${profile.id}`;
|
||||
const unifiedId = `${OAUTH_ID_PREFIX}${profile.id}`;
|
||||
const priorityIndex = priorityOrder.indexOf(unifiedId);
|
||||
const availability = checkProfileAvailability(profile, settings);
|
||||
const fallbackScore = calculateFallbackScore(profile, settings);
|
||||
|
||||
@@ -1961,14 +1961,17 @@ export class UsageMonitor extends EventEmitter {
|
||||
this.clearProfileUsageCache(currentProfileId);
|
||||
|
||||
// Switch to the new profile
|
||||
// Note: bestAccount.id is already the raw profile ID (not unified format)
|
||||
const rawProfileId = bestAccount.id;
|
||||
|
||||
if (bestAccount.type === 'oauth') {
|
||||
// Switch OAuth profile via profile manager
|
||||
profileManager.setActiveProfile(bestAccount.id);
|
||||
profileManager.setActiveProfile(rawProfileId);
|
||||
} else {
|
||||
// Switch API profile via profile-manager service
|
||||
try {
|
||||
const { setActiveAPIProfile } = await import('../services/profile/profile-manager');
|
||||
await setActiveAPIProfile(bestAccount.id);
|
||||
await setActiveAPIProfile(rawProfileId);
|
||||
} catch (error) {
|
||||
console.error('[UsageMonitor] Failed to set active API profile:', error);
|
||||
return;
|
||||
|
||||
@@ -15,64 +15,122 @@ interface WatcherInfo {
|
||||
*/
|
||||
export class FileWatcher extends EventEmitter {
|
||||
private watchers: Map<string, WatcherInfo> = new Map();
|
||||
// Maps taskId -> specDir for the in-flight watch() call.
|
||||
// Allows re-watch calls with a different specDir to proceed while
|
||||
// still preventing duplicate calls for the exact same specDir.
|
||||
private pendingWatches: Map<string, string> = new Map();
|
||||
// Tracks taskIds that had unwatch() called while watch() was in-flight.
|
||||
// Checked after each await point in watch() to avoid creating a leaked watcher.
|
||||
private cancelledWatches: Set<string> = new Set();
|
||||
|
||||
/**
|
||||
* Start watching a task's implementation plan
|
||||
*/
|
||||
async watch(taskId: string, specDir: string): Promise<void> {
|
||||
// Stop any existing watcher for this task
|
||||
await this.unwatch(taskId);
|
||||
|
||||
const planPath = path.join(specDir, 'implementation_plan.json');
|
||||
|
||||
// Check if plan file exists
|
||||
if (!existsSync(planPath)) {
|
||||
this.emit('error', taskId, `Plan file not found: ${planPath}`);
|
||||
// Prevent overlapping watch() calls for the same taskId + specDir combination.
|
||||
// Since watch() is async, rapid-fire callers could enter concurrently
|
||||
// before the first call updates state, creating duplicate watchers.
|
||||
// A call with a different specDir is a legitimate re-watch and is allowed through.
|
||||
const pendingSpecDir = this.pendingWatches.get(taskId);
|
||||
if (pendingSpecDir !== undefined && pendingSpecDir === specDir) {
|
||||
return;
|
||||
}
|
||||
this.pendingWatches.set(taskId, specDir);
|
||||
|
||||
// Create watcher with settings to handle frequent writes
|
||||
const watcher = chokidar.watch(planPath, {
|
||||
persistent: true,
|
||||
ignoreInitial: true,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 300,
|
||||
pollInterval: 100
|
||||
try {
|
||||
// Close any existing watcher for this task.
|
||||
// Delete from the map BEFORE awaiting close so that a concurrent watch()
|
||||
// call entering after the await cannot obtain the same FSWatcher reference
|
||||
// and attempt a second close() on the same object.
|
||||
const existing = this.watchers.get(taskId);
|
||||
if (existing) {
|
||||
this.watchers.delete(taskId);
|
||||
await existing.watcher.close();
|
||||
}
|
||||
});
|
||||
|
||||
// Store watcher info
|
||||
this.watchers.set(taskId, {
|
||||
taskId,
|
||||
watcher,
|
||||
planPath
|
||||
});
|
||||
// Check if a newer watch() call has superseded this one while we were awaiting.
|
||||
// If the pending specDir changed, another concurrent watch() took over — bail out
|
||||
// to avoid overwriting the watcher it is about to create.
|
||||
if (this.pendingWatches.get(taskId) !== specDir) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle file changes
|
||||
watcher.on('change', () => {
|
||||
// Check if unwatch() was called while we were awaiting above.
|
||||
if (this.cancelledWatches.has(taskId)) {
|
||||
this.cancelledWatches.delete(taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
const planPath = path.join(specDir, 'implementation_plan.json');
|
||||
|
||||
// Check if plan file exists
|
||||
if (!existsSync(planPath)) {
|
||||
this.emit('error', taskId, `Plan file not found: ${planPath}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Create watcher with settings to handle frequent writes
|
||||
const watcher = chokidar.watch(planPath, {
|
||||
persistent: true,
|
||||
ignoreInitial: true,
|
||||
awaitWriteFinish: {
|
||||
stabilityThreshold: 300,
|
||||
pollInterval: 100
|
||||
}
|
||||
});
|
||||
|
||||
// Check again after the synchronous watcher creation (no await, but defensive).
|
||||
if (this.cancelledWatches.has(taskId)) {
|
||||
this.cancelledWatches.delete(taskId);
|
||||
await watcher.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Store watcher info
|
||||
this.watchers.set(taskId, {
|
||||
taskId,
|
||||
watcher,
|
||||
planPath
|
||||
});
|
||||
|
||||
// Handle file changes
|
||||
watcher.on('change', () => {
|
||||
try {
|
||||
const content = readFileSync(planPath, 'utf-8');
|
||||
const plan: ImplementationPlan = JSON.parse(content);
|
||||
this.emit('progress', taskId, plan);
|
||||
} catch {
|
||||
// File might be in the middle of being written
|
||||
// Ignore parse errors, next change event will have complete file
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
watcher.on('error', (error: unknown) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.emit('error', taskId, message);
|
||||
});
|
||||
|
||||
// Read and emit initial state
|
||||
try {
|
||||
const content = readFileSync(planPath, 'utf-8');
|
||||
const plan: ImplementationPlan = JSON.parse(content);
|
||||
this.emit('progress', taskId, plan);
|
||||
} catch {
|
||||
// File might be in the middle of being written
|
||||
// Ignore parse errors, next change event will have complete file
|
||||
// Initial read failed - not critical
|
||||
}
|
||||
} finally {
|
||||
// Only clean up if this call still owns the entry. If a superseding
|
||||
// concurrent watch() call has already updated pendingWatches with a
|
||||
// different specDir, leave that entry intact so the superseding call
|
||||
// can proceed correctly.
|
||||
if (this.pendingWatches.get(taskId) === specDir) {
|
||||
this.pendingWatches.delete(taskId);
|
||||
// The delete above guarantees has() is now false, so there is no
|
||||
// longer any in-flight watch() for this taskId. Clear the
|
||||
// cancellation flag so it doesn't linger for future watch() calls.
|
||||
this.cancelledWatches.delete(taskId);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle errors
|
||||
watcher.on('error', (error: unknown) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.emit('error', taskId, message);
|
||||
});
|
||||
|
||||
// Read and emit initial state
|
||||
try {
|
||||
const content = readFileSync(planPath, 'utf-8');
|
||||
const plan: ImplementationPlan = JSON.parse(content);
|
||||
this.emit('progress', taskId, plan);
|
||||
} catch {
|
||||
// Initial read failed - not critical
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,6 +138,13 @@ export class FileWatcher extends EventEmitter {
|
||||
* Stop watching a task
|
||||
*/
|
||||
async unwatch(taskId: string): Promise<void> {
|
||||
// If watch() is currently in-flight for this taskId, it is already closing the
|
||||
// existing watcher. Just set the cancellation flag and return to avoid a
|
||||
// double-close of the same FSWatcher.
|
||||
if (this.pendingWatches.has(taskId)) {
|
||||
this.cancelledWatches.add(taskId);
|
||||
return;
|
||||
}
|
||||
const watcherInfo = this.watchers.get(taskId);
|
||||
if (watcherInfo) {
|
||||
await watcherInfo.watcher.close();
|
||||
@@ -91,6 +156,17 @@ export class FileWatcher extends EventEmitter {
|
||||
* Stop all watchers
|
||||
*/
|
||||
async unwatchAll(): Promise<void> {
|
||||
// Cancel any in-flight watch() calls so they don't create new watchers
|
||||
// after this cleanup completes.
|
||||
for (const taskId of this.pendingWatches.keys()) {
|
||||
this.cancelledWatches.add(taskId);
|
||||
}
|
||||
this.pendingWatches.clear();
|
||||
// Clear cancellation flags now that pendingWatches is empty: the in-flight
|
||||
// calls will bail via the supersession check (pendingWatches.get() returns
|
||||
// undefined) and will not clean up cancelledWatches themselves. Clearing
|
||||
// here ensures the instance is fully reset for subsequent use.
|
||||
this.cancelledWatches.clear();
|
||||
const closePromises = Array.from(this.watchers.values()).map(
|
||||
async (info) => {
|
||||
await info.watcher.close();
|
||||
@@ -107,6 +183,15 @@ export class FileWatcher extends EventEmitter {
|
||||
return this.watchers.has(taskId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the spec directory currently being watched for a task
|
||||
*/
|
||||
getWatchedSpecDir(taskId: string): string | null {
|
||||
const watcherInfo = this.watchers.get(taskId);
|
||||
if (!watcherInfo) return null;
|
||||
return path.dirname(watcherInfo.planPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current plan state for a task
|
||||
*/
|
||||
|
||||
@@ -330,6 +330,10 @@ function createWindow(): void {
|
||||
|
||||
// Clean up on close
|
||||
mainWindow.on('closed', () => {
|
||||
// Kill all agents when window closes (prevents orphaned processes)
|
||||
agentManager?.killAll?.()?.catch((err: unknown) => {
|
||||
console.warn('[main] Error killing agents on window close:', err);
|
||||
});
|
||||
mainWindow = null;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -98,7 +98,23 @@ export function registerAgenteventsHandlers(
|
||||
|
||||
// Send final plan state to renderer BEFORE unwatching
|
||||
// This ensures the renderer has the final subtask data (fixes 0/0 subtask bug)
|
||||
const finalPlan = fileWatcher.getCurrentPlan(taskId);
|
||||
// Try the file watcher's current path first, then fall back to worktree path
|
||||
let finalPlan = fileWatcher.getCurrentPlan(taskId);
|
||||
if (!finalPlan && exitTask && exitProject) {
|
||||
// File watcher may have been watching the wrong path (main vs worktree)
|
||||
// Try reading directly from the worktree
|
||||
const worktreePath = findTaskWorktree(exitProject.path, exitTask.specId);
|
||||
if (worktreePath) {
|
||||
const specsBaseDir = getSpecsDir(exitProject.autoBuildPath);
|
||||
const worktreePlanPath = path.join(worktreePath, specsBaseDir, exitTask.specId, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN);
|
||||
try {
|
||||
const content = readFileSync(worktreePlanPath, 'utf-8');
|
||||
finalPlan = JSON.parse(content);
|
||||
} catch {
|
||||
// Worktree plan file not readable - not critical
|
||||
}
|
||||
}
|
||||
}
|
||||
if (finalPlan) {
|
||||
safeSendToRenderer(
|
||||
getMainWindow,
|
||||
@@ -109,7 +125,9 @@ export function registerAgenteventsHandlers(
|
||||
);
|
||||
}
|
||||
|
||||
fileWatcher.unwatch(taskId);
|
||||
fileWatcher.unwatch(taskId).catch((err) => {
|
||||
console.error(`[agent-events-handlers] Failed to unwatch for ${taskId}:`, err);
|
||||
});
|
||||
|
||||
if (processType === "spec-creation") {
|
||||
console.warn(`[Task ${taskId}] Spec creation completed with code ${code}`);
|
||||
@@ -211,15 +229,26 @@ export function registerAgenteventsHandlers(
|
||||
const worktreePath = findTaskWorktree(project.path, task.specId);
|
||||
if (worktreePath) {
|
||||
const specsBaseDir = getSpecsDir(project.autoBuildPath);
|
||||
const worktreeSpecDir = path.join(worktreePath, specsBaseDir, task.specId);
|
||||
const worktreePlanPath = path.join(
|
||||
worktreePath,
|
||||
specsBaseDir,
|
||||
task.specId,
|
||||
worktreeSpecDir,
|
||||
AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN
|
||||
);
|
||||
if (existsSync(worktreePlanPath)) {
|
||||
persistPlanPhaseSync(worktreePlanPath, progress.phase, project.id);
|
||||
}
|
||||
|
||||
// Re-watch the worktree path if the file watcher is still watching the main project path.
|
||||
// This handles the case where the task started before the worktree existed:
|
||||
// the initial watch fell back to the main project spec dir, but now the worktree
|
||||
// is available and implementation_plan.json is being written there.
|
||||
const currentWatchDir = fileWatcher.getWatchedSpecDir(taskId);
|
||||
if (currentWatchDir && currentWatchDir !== worktreeSpecDir && existsSync(worktreePlanPath)) {
|
||||
console.warn(`[agent-events-handlers] Re-watching worktree path for ${taskId}: ${worktreeSpecDir}`);
|
||||
fileWatcher.watch(taskId, worktreeSpecDir).catch((err) => {
|
||||
console.error(`[agent-events-handlers] Failed to re-watch worktree for ${taskId}:`, err);
|
||||
});
|
||||
}
|
||||
}
|
||||
} else if (xstateInTerminalState && progress.phase) {
|
||||
console.debug(`[agent-events-handlers] Skipping persistPlanPhaseSync for ${taskId}: XState in '${currentXState}', not overwriting with phase '${progress.phase}'`);
|
||||
|
||||
@@ -36,6 +36,8 @@ import {
|
||||
buildRunnerArgs,
|
||||
} from "./utils/subprocess-runner";
|
||||
import { getPRStatusPoller } from "../../services/pr-status-poller";
|
||||
import { safeBreadcrumb, safeCaptureException } from "../../sentry";
|
||||
import { sanitizeForSentry } from "../../../shared/utils/sentry-privacy";
|
||||
import type {
|
||||
StartPollingRequest,
|
||||
StopPollingRequest,
|
||||
@@ -110,6 +112,7 @@ async function githubGraphQL<T>(
|
||||
query: string,
|
||||
variables: Record<string, unknown> = {}
|
||||
): Promise<T> {
|
||||
// lgtm[js/file-access-to-http] - Official GitHub GraphQL API endpoint
|
||||
const response = await fetch("https://api.github.com/graphql", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@@ -265,6 +268,10 @@ export interface PRReviewFinding {
|
||||
endLine?: number;
|
||||
suggestedFix?: string;
|
||||
fixable: boolean;
|
||||
validationStatus?: "confirmed_valid" | "dismissed_false_positive" | "needs_human_review" | null;
|
||||
validationExplanation?: string;
|
||||
sourceAgents?: string[];
|
||||
crossValidated?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -276,7 +283,7 @@ export interface PRReviewResult {
|
||||
success: boolean;
|
||||
findings: PRReviewFinding[];
|
||||
summary: string;
|
||||
overallStatus: "approve" | "request_changes" | "comment";
|
||||
overallStatus: "approve" | "request_changes" | "comment" | "in_progress";
|
||||
reviewId?: number;
|
||||
reviewedAt: string;
|
||||
error?: string;
|
||||
@@ -292,6 +299,8 @@ export interface PRReviewResult {
|
||||
hasPostedFindings?: boolean;
|
||||
postedFindingIds?: string[];
|
||||
postedAt?: string;
|
||||
// In-progress review tracking
|
||||
inProgressSince?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1336,6 +1345,10 @@ function getReviewResult(project: Project, prNumber: number): PRReviewResult | n
|
||||
endLine: f.end_line,
|
||||
suggestedFix: f.suggested_fix,
|
||||
fixable: f.fixable ?? false,
|
||||
validationStatus: f.validation_status ?? null,
|
||||
validationExplanation: f.validation_explanation ?? undefined,
|
||||
sourceAgents: f.source_agents ?? [],
|
||||
crossValidated: f.cross_validated ?? false,
|
||||
})) ?? [],
|
||||
summary: data.summary ?? "",
|
||||
overallStatus: data.overall_status ?? "comment",
|
||||
@@ -1354,6 +1367,8 @@ function getReviewResult(project: Project, prNumber: number): PRReviewResult | n
|
||||
hasPostedFindings: data.has_posted_findings ?? false,
|
||||
postedFindingIds: data.posted_finding_ids ?? [],
|
||||
postedAt: data.posted_at,
|
||||
// In-progress review tracking
|
||||
inProgressSince: data.in_progress_since,
|
||||
};
|
||||
} catch {
|
||||
// File doesn't exist or couldn't be read
|
||||
@@ -1462,6 +1477,20 @@ async function runPRReview(
|
||||
|
||||
debugLog("Spawning PR review process", { args, model, thinkingLevel });
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: 'Spawning PR review subprocess',
|
||||
level: 'info',
|
||||
data: {
|
||||
pythonPath: getPythonPath(backendPath),
|
||||
runnerPath: getRunnerPath(backendPath),
|
||||
cwd: backendPath,
|
||||
model,
|
||||
thinkingLevel,
|
||||
prNumber,
|
||||
},
|
||||
});
|
||||
|
||||
// Create log collector for this review
|
||||
const config = getGitHubConfig(project);
|
||||
const repo = config?.repo || project.name || "unknown";
|
||||
@@ -1470,6 +1499,19 @@ async function runPRReview(
|
||||
// Build environment with project settings
|
||||
const subprocessEnv = await getRunnerEnv(getClaudeMdEnv(project));
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'github.pr-review',
|
||||
message: `Subprocess env for PR #${prNumber} review`,
|
||||
level: 'info',
|
||||
data: {
|
||||
prNumber,
|
||||
hasGITHUB_CLI_PATH: !!subprocessEnv.GITHUB_CLI_PATH,
|
||||
GITHUB_CLI_PATH: subprocessEnv.GITHUB_CLI_PATH ?? 'NOT SET',
|
||||
hasGITHUB_TOKEN: !!subprocessEnv.GITHUB_TOKEN,
|
||||
hasPYTHONPATH: !!subprocessEnv.PYTHONPATH,
|
||||
},
|
||||
});
|
||||
|
||||
// Create operation ID for this review
|
||||
const reviewKey = getReviewKey(project.id, prNumber);
|
||||
|
||||
@@ -1498,7 +1540,32 @@ async function runPRReview(
|
||||
debugLog("Auth failure detected in PR review", authFailureInfo);
|
||||
mainWindow.webContents.send(IPC_CHANNELS.CLAUDE_AUTH_FAILURE, authFailureInfo);
|
||||
},
|
||||
onComplete: () => {
|
||||
onComplete: (stdout: string) => {
|
||||
// Check stdout for in_progress JSON marker (not saved to disk by backend)
|
||||
const inProgressMarker = "__RESULT_JSON__:";
|
||||
for (const line of stdout.split("\n")) {
|
||||
if (line.startsWith(inProgressMarker)) {
|
||||
try {
|
||||
const data = JSON.parse(line.slice(inProgressMarker.length));
|
||||
if (data.overall_status === "in_progress") {
|
||||
debugLog("In-progress result parsed from stdout", { prNumber });
|
||||
return {
|
||||
prNumber: data.pr_number,
|
||||
repo: data.repo,
|
||||
success: data.success,
|
||||
findings: [],
|
||||
summary: data.summary ?? "",
|
||||
overallStatus: "in_progress" as const,
|
||||
reviewedAt: data.reviewed_at ?? new Date().toISOString(),
|
||||
inProgressSince: data.in_progress_since,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
debugLog("Failed to parse __RESULT_JSON__ line", { line });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Load the result from disk
|
||||
const reviewResult = getReviewResult(project, prNumber);
|
||||
if (!reviewResult) {
|
||||
@@ -1525,9 +1592,22 @@ async function runPRReview(
|
||||
// Wait for the process to complete
|
||||
const result = await promise;
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: `PR review subprocess exited`,
|
||||
level: result.success ? 'info' : 'error',
|
||||
data: { exitCode: result.exitCode, success: result.success, prNumber },
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
// Finalize logs with failure
|
||||
logCollector.finalize(false);
|
||||
|
||||
safeCaptureException(
|
||||
new Error(`PR review subprocess failed: ${result.error ?? 'unknown error'}`),
|
||||
{ extra: { exitCode: result.exitCode, prNumber, stderr: sanitizeForSentry(result.stderr.slice(0, 500)) } }
|
||||
);
|
||||
|
||||
throw new Error(result.error ?? "Review failed");
|
||||
}
|
||||
|
||||
@@ -1835,9 +1915,15 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
projectId
|
||||
);
|
||||
|
||||
// Check if already running
|
||||
// Check if already running — notify renderer so it can display ongoing logs
|
||||
if (runningReviews.has(reviewKey)) {
|
||||
debugLog("Review already running", { reviewKey });
|
||||
debugLog("Review already running, notifying renderer", { reviewKey });
|
||||
sendProgress({
|
||||
phase: "analyzing",
|
||||
prNumber,
|
||||
progress: 50,
|
||||
message: "Review is already in progress. Reconnecting to ongoing review...",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1907,6 +1993,20 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
|
||||
const result = await runPRReview(project, prNumber, mainWindow);
|
||||
|
||||
if (result.overallStatus === "in_progress") {
|
||||
// Review is already running externally (detected by BotDetector).
|
||||
// Send the result as-is so the renderer can activate external review polling.
|
||||
debugLog("PR review already in progress externally", { prNumber });
|
||||
sendProgress({
|
||||
phase: "complete",
|
||||
prNumber,
|
||||
progress: 100,
|
||||
message: "Review already in progress",
|
||||
});
|
||||
sendComplete(result);
|
||||
return;
|
||||
}
|
||||
|
||||
debugLog("PR review completed", { prNumber, findingsCount: result.findings.length });
|
||||
sendProgress({
|
||||
phase: "complete",
|
||||
@@ -1941,7 +2041,7 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
},
|
||||
projectId
|
||||
);
|
||||
sendError(error instanceof Error ? error.message : "Failed to run PR review");
|
||||
sendError({ prNumber, error: error instanceof Error ? error.message : "Failed to run PR review" });
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2907,6 +3007,20 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
|
||||
debugLog("Spawning follow-up review process", { args, model, thinkingLevel });
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: 'Spawning follow-up PR review subprocess',
|
||||
level: 'info',
|
||||
data: {
|
||||
pythonPath: getPythonPath(backendPath),
|
||||
runnerPath: getRunnerPath(backendPath),
|
||||
cwd: backendPath,
|
||||
model,
|
||||
thinkingLevel,
|
||||
prNumber,
|
||||
},
|
||||
});
|
||||
|
||||
// Create log collector for this follow-up review (config already declared above)
|
||||
const repo = config?.repo || project.name || "unknown";
|
||||
const logCollector = new PRLogCollector(project, prNumber, repo, true, mainWindow);
|
||||
@@ -2914,6 +3028,19 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
// Build environment with project settings
|
||||
const followupEnv = await getRunnerEnv(getClaudeMdEnv(project));
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'github.pr-review',
|
||||
message: `Subprocess env for PR #${prNumber} follow-up review`,
|
||||
level: 'info',
|
||||
data: {
|
||||
prNumber,
|
||||
hasGITHUB_CLI_PATH: !!followupEnv.GITHUB_CLI_PATH,
|
||||
GITHUB_CLI_PATH: followupEnv.GITHUB_CLI_PATH ?? 'NOT SET',
|
||||
hasGITHUB_TOKEN: !!followupEnv.GITHUB_TOKEN,
|
||||
hasPYTHONPATH: !!followupEnv.PYTHONPATH,
|
||||
},
|
||||
});
|
||||
|
||||
const { process: childProcess, promise } = runPythonSubprocess<PRReviewResult>({
|
||||
pythonPath: getPythonPath(backendPath),
|
||||
args,
|
||||
@@ -2964,9 +3091,22 @@ export function registerPRHandlers(getMainWindow: () => BrowserWindow | null): v
|
||||
|
||||
const result = await promise;
|
||||
|
||||
safeBreadcrumb({
|
||||
category: 'pr-review',
|
||||
message: 'Follow-up PR review subprocess exited',
|
||||
level: result.success ? 'info' : 'error',
|
||||
data: { exitCode: result.exitCode, success: result.success, prNumber },
|
||||
});
|
||||
|
||||
if (!result.success) {
|
||||
// Finalize logs with failure
|
||||
logCollector.finalize(false);
|
||||
|
||||
safeCaptureException(
|
||||
new Error(`Follow-up PR review subprocess failed: ${result.error ?? 'unknown error'}`),
|
||||
{ extra: { exitCode: result.exitCode, prNumber, stderr: sanitizeForSentry(result.stderr.slice(0, 500)) } }
|
||||
);
|
||||
|
||||
throw new Error(result.error ?? "Follow-up review failed");
|
||||
}
|
||||
|
||||
|
||||
@@ -137,6 +137,7 @@ export async function createSpecForIssue(
|
||||
status: 'pending',
|
||||
phases: []
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, slugifiedTitle sanitizes input
|
||||
writeFileSync(
|
||||
path.join(specDir, AUTO_BUILD_PATHS.IMPLEMENTATION_PLAN),
|
||||
JSON.stringify(implementationPlan, null, 2),
|
||||
@@ -148,6 +149,7 @@ export async function createSpecForIssue(
|
||||
task_description: safeDescription,
|
||||
workflow_type: 'feature'
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, slugifiedTitle sanitizes input
|
||||
writeFileSync(
|
||||
path.join(specDir, AUTO_BUILD_PATHS.REQUIREMENTS),
|
||||
JSON.stringify(requirements, null, 2),
|
||||
@@ -167,6 +169,7 @@ export async function createSpecForIssue(
|
||||
// This comes from project.settings.mainBranch or task-level override
|
||||
...(baseBranch && { baseBranch })
|
||||
};
|
||||
// lgtm[js/http-to-file-access] - specDir is controlled, slugifiedTitle sanitizes input
|
||||
writeFileSync(
|
||||
path.join(specDir, 'task_metadata.json'),
|
||||
JSON.stringify(metadata, null, 2),
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user