Code quality improvements and faster tunnel detection - #145
Merged
Conversation
Optimizations implemented: - Removed dead code (format_resource_str) - Moved STATUS_COLOR_MAP to module level - Extracted _is_git_repo() helper (2 duplicates → 1) - Created _build_job_info() helper (6 duplicates → 1) - Simplified verbose conditionals (-12 lines) - Extracted _should_skip_wandb_check() for clarity Results: - LOC: -94 lines (318 insertions, 412 deletions) - Tests: 40/42 passing ✓ - Type safety: 0 pyright errors ✓ Combined with Phase 1, achieved significant code quality improvements while reducing total codebase size. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Critical fixes: - Fix notification validation logic preventing silent job submission failures - Fix CPU job display inconsistency between preview and success messages High-priority fixes: - Add Literal type annotation to format_gpu_info style parameter - Extract magic numbers to named constants (WANDB_CHECK_*, JOB_INIT_MAX_ATTEMPTS, COMPLETED_JOB_LOG_TAIL_LINES) - Improve wandb check logic with minimum delay (skip jobs < 30s and > 12min) - Use frozenset for TERMINAL_STATUSES (O(1) vs O(n) lookups) Results: - LOC: -74 lines (340 insertions, 414 deletions) - Type safety: 0 pyright errors ✓ - Tests: 40/42 passing ✓ 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Set SCREENDIR to /tmp/nexus-screen with mode 700 to avoid permission errors in test environments where /run/screen is not accessible. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
- Created nexus/cli/ids.py to duplicate generate_job_id() (eliminate server dependency) - Created nexus/cli/constants.py with status constants and magic numbers - Removed all imports from nexus.server in CLI code (complete module separation) - Extracted _format_job_details() helper to consolidate duplicate formatting - Extracted _resolve_job_target() helper to consolidate job lookup logic - Extracted _format_gpu_status_part() helper to consolidate GPU status formatting - Replaced all hardcoded status strings with constants - Replaced all magic numbers with named constants - Added missing type annotations to inline functions Net impact: -31 LOC, improved maintainability, zero CLI-server coupling 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
## Phase 1: CLI Job Preparation Logic - Extract _build_notification_lists() - eliminates 20+ lines of duplicate list building - Extract _load_jobrc() - eliminates 10 lines of duplicate file reading - Extract _build_job_request() - eliminates 36 lines of duplicate job request construction - Replace verbose loops with comprehensions for cleaner code ## Phase 2: Display Logic - Add STATUS_ICONS dict - replace 7-line nested ternary with 1-line lookup - Improve readability in show_history() ## Phase 3: Server-Side Improvements - Extract _require_env() in notifications.py - reduce 32 lines to 7 (25-line reduction) - Extract _clear_cache_if_refresh() in system.py - reduce 15 lines to 4 across 4 functions ## Quality Metrics - Files Modified: 4 - Net Change: +3 lines (135 insertions, 132 deletions) - Eliminated ~90 lines of duplication, added ~87 lines of reusable helpers - DRY Compliance: Single source of truth for job preparation, env validation, cache clearing - Type Safety: 0 pyright errors with proper overloads - Tests: 42/42 passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Resolved conflicts in 7 files by keeping refactored code with: - Better type safety (Literal types) - Helper functions to reduce duplication - Named constants for maintainability - Clearer error messaging 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Reduces stale tunnel detection time from 3+ minutes to ~30-40 seconds and adds automatic recovery. **Phase 1: Fast Detection** - Aggressive SSH keepalive: 15s interval (was 60s), 2 attempts (was 3) - Detects dead connections in 30s instead of 180s - Fast-fail tunnel validation: 2.5s total (was 6s+) - Control socket check: 2s timeout (was 5s) - Port connectivity: 0.5s timeout (was 1s) - Add timeouts to ALL API requests (12 locations): - Short operations: 5s timeout - Medium operations: 10s timeout - Long operations: 30s timeout - Prevents infinite hangs on stale tunnels **Phase 2: Automatic Recovery** - Auto-retry with tunnel recreation on connection failures - Catches ConnectionError and Timeout exceptions - Stops stale control master and retries (up to 2 attempts) - User-friendly "recreating tunnel" progress messages **Impact:** - Before: 180s to detect, infinite hang, manual retry required - After: 30-40s to detect, 5-30s fail-fast, automatic recovery Files modified: - src/nexus/cli/tunnel_manager.py: SSH keepalive and validation timeouts - src/nexus/cli/api_client.py: Request timeouts and retry logic 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Remove unreachable code and fix type checking issue where last_error could be None. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR includes two major improvements:
Code Quality Improvements
Refactoring Changes
tp.LiteraltypesELLIPSIS_LENGTH,STATUS_ICONS)Bug Fixes
Net Result: -74 lines of code with better functionality
SSH Tunnel Detection Improvements
Problem
Users experiencing stale SSH tunnels faced:
Solution - Phase 1: Fast Detection
1. Aggressive SSH Keepalive (
tunnel_manager.py)2. Fast-Fail Validation (
tunnel_manager.py)3. Request Timeouts (
api_client.py- 12 locations)Solution - Phase 2: Automatic Recovery
Auto-Retry Logic (
api_client.py)ConnectionErrorandTimeoutexceptionsConnection failed, recreating tunnel (attempt 1/2)...Impact
Files Modified
Code Quality:
src/nexus/cli/api_client.py- Type improvements, helper functionsrc/nexus/cli/constants.py- AddedSTATUS_ICONSsrc/nexus/cli/jobs.py- Helper functions, better error messagessrc/nexus/cli/utils.py- Named constants, formattingsrc/nexus/server/api/router.py- Formattingsrc/nexus/server/core/job.py- Formattingsrc/nexus/server/external/system.py- FormattingTunnel Detection:
src/nexus/cli/tunnel_manager.py- SSH keepalive and validationsrc/nexus/cli/api_client.py- Timeouts and retry logicTest Plan
🤖 Generated with Claude Code