mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-02-05 16:33:08 +00:00
Merge branch 'master' into master
This commit is contained in:
@@ -6,31 +6,22 @@ API endpoints for agent control (start/stop/pause/resume).
|
||||
Uses project registry for path lookups.
|
||||
"""
|
||||
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from ..schemas import AgentActionResponse, AgentStartRequest, AgentStatus
|
||||
from ..services.chat_constants import ROOT_DIR
|
||||
from ..services.process_manager import get_manager
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import validate_project_name
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Path:
|
||||
"""Get project path from registry."""
|
||||
import sys
|
||||
root = Path(__file__).parent.parent.parent
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
|
||||
from registry import get_project_path
|
||||
return get_project_path(project_name)
|
||||
|
||||
|
||||
def _get_settings_defaults() -> tuple[bool, str, int]:
|
||||
def _get_settings_defaults() -> tuple[bool, str, int, bool, int]:
|
||||
"""Get defaults from global settings.
|
||||
|
||||
Returns:
|
||||
Tuple of (yolo_mode, model, testing_agent_ratio)
|
||||
Tuple of (yolo_mode, model, testing_agent_ratio, playwright_headless, batch_size)
|
||||
"""
|
||||
import sys
|
||||
root = Path(__file__).parent.parent.parent
|
||||
@@ -49,24 +40,18 @@ def _get_settings_defaults() -> tuple[bool, str, int]:
|
||||
except (ValueError, TypeError):
|
||||
testing_agent_ratio = 1
|
||||
|
||||
return yolo_mode, model, testing_agent_ratio
|
||||
playwright_headless = (settings.get("playwright_headless") or "true").lower() == "true"
|
||||
|
||||
try:
|
||||
batch_size = int(settings.get("batch_size", "3"))
|
||||
except (ValueError, TypeError):
|
||||
batch_size = 3
|
||||
|
||||
return yolo_mode, model, testing_agent_ratio, playwright_headless, batch_size
|
||||
|
||||
|
||||
router = APIRouter(prefix="/api/projects/{project_name}/agent", tags=["agent"])
|
||||
|
||||
# Root directory for process manager
|
||||
ROOT_DIR = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
def validate_project_name(name: str) -> str:
|
||||
"""Validate and sanitize project name to prevent path traversal."""
|
||||
if not re.match(r'^[a-zA-Z0-9_-]{1,50}$', name):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Invalid project name"
|
||||
)
|
||||
return name
|
||||
|
||||
|
||||
def get_project_manager(project_name: str):
|
||||
"""Get the process manager for a project."""
|
||||
@@ -111,18 +96,22 @@ async def start_agent(
|
||||
manager = get_project_manager(project_name)
|
||||
|
||||
# Get defaults from global settings if not provided in request
|
||||
default_yolo, default_model, default_testing_ratio = _get_settings_defaults()
|
||||
default_yolo, default_model, default_testing_ratio, playwright_headless, default_batch_size = _get_settings_defaults()
|
||||
|
||||
yolo_mode = request.yolo_mode if request.yolo_mode is not None else default_yolo
|
||||
model = request.model if request.model else default_model
|
||||
max_concurrency = request.max_concurrency or 1
|
||||
testing_agent_ratio = request.testing_agent_ratio if request.testing_agent_ratio is not None else default_testing_ratio
|
||||
|
||||
batch_size = default_batch_size
|
||||
|
||||
success, message = await manager.start(
|
||||
yolo_mode=yolo_mode,
|
||||
model=model,
|
||||
max_concurrency=max_concurrency,
|
||||
testing_agent_ratio=testing_agent_ratio,
|
||||
playwright_headless=playwright_headless,
|
||||
batch_size=batch_size,
|
||||
)
|
||||
|
||||
# Notify scheduler of manual start (to prevent auto-stop during scheduled window)
|
||||
|
||||
@@ -7,8 +7,6 @@ WebSocket and REST endpoints for the read-only project assistant.
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, WebSocket, WebSocketDisconnect
|
||||
@@ -27,30 +25,13 @@ from ..services.assistant_database import (
|
||||
get_conversation,
|
||||
get_conversations,
|
||||
)
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import is_valid_project_name as validate_project_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/assistant", tags=["assistant-chat"])
|
||||
|
||||
# Root directory
|
||||
ROOT_DIR = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Optional[Path]:
|
||||
"""Get project path from registry."""
|
||||
import sys
|
||||
root = Path(__file__).parent.parent.parent
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
|
||||
from registry import get_project_path
|
||||
return get_project_path(project_name)
|
||||
|
||||
|
||||
def validate_project_name(name: str) -> bool:
|
||||
"""Validate project name to prevent path traversal."""
|
||||
return bool(re.match(r'^[a-zA-Z0-9_-]{1,50}$', name))
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# Pydantic Models
|
||||
@@ -145,9 +126,9 @@ async def create_project_conversation(project_name: str):
|
||||
|
||||
conversation = create_conversation(project_dir, project_name)
|
||||
return ConversationSummary(
|
||||
id=conversation.id,
|
||||
project_name=conversation.project_name,
|
||||
title=conversation.title,
|
||||
id=int(conversation.id),
|
||||
project_name=str(conversation.project_name),
|
||||
title=str(conversation.title) if conversation.title else None,
|
||||
created_at=conversation.created_at.isoformat() if conversation.created_at else None,
|
||||
updated_at=conversation.updated_at.isoformat() if conversation.updated_at else None,
|
||||
message_count=0,
|
||||
|
||||
@@ -8,7 +8,6 @@ Allows adding multiple features to existing projects via natural language.
|
||||
|
||||
import json
|
||||
import logging
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, WebSocket, WebSocketDisconnect
|
||||
@@ -22,27 +21,13 @@ from ..services.expand_chat_session import (
|
||||
list_expand_sessions,
|
||||
remove_expand_session,
|
||||
)
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import validate_project_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/expand", tags=["expand-project"])
|
||||
|
||||
# Root directory
|
||||
ROOT_DIR = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Path:
|
||||
"""Get project path from registry."""
|
||||
import sys
|
||||
root = Path(__file__).parent.parent.parent
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
|
||||
from registry import get_project_path
|
||||
return get_project_path(project_name)
|
||||
|
||||
|
||||
|
||||
|
||||
# ============================================================================
|
||||
@@ -136,7 +121,8 @@ async def expand_project_websocket(websocket: WebSocket, project_name: str):
|
||||
return
|
||||
|
||||
# Verify project has app_spec.txt
|
||||
spec_path = project_dir / "prompts" / "app_spec.txt"
|
||||
from autocoder_paths import get_prompts_dir
|
||||
spec_path = get_prompts_dir(project_dir) / "app_spec.txt"
|
||||
if not spec_path.exists():
|
||||
await websocket.close(code=4004, reason="Project has no spec. Create spec first.")
|
||||
return
|
||||
|
||||
@@ -8,10 +8,12 @@ API endpoints for feature/test case management.
|
||||
import logging
|
||||
from contextlib import contextmanager
|
||||
from pathlib import Path
|
||||
from typing import Literal
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from ..schemas import (
|
||||
DependencyGraphEdge,
|
||||
DependencyGraphNode,
|
||||
DependencyGraphResponse,
|
||||
DependencyUpdate,
|
||||
@@ -22,6 +24,7 @@ from ..schemas import (
|
||||
FeatureResponse,
|
||||
FeatureUpdate,
|
||||
)
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import validate_project_name
|
||||
|
||||
# Lazy imports to avoid circular dependencies
|
||||
@@ -31,17 +34,6 @@ _Feature = None
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Path:
|
||||
"""Get project path from registry."""
|
||||
import sys
|
||||
root = Path(__file__).parent.parent.parent
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
|
||||
from registry import get_project_path
|
||||
return get_project_path(project_name)
|
||||
|
||||
|
||||
def _get_db_classes():
|
||||
"""Lazy import of database classes."""
|
||||
global _create_database, _Feature
|
||||
@@ -71,6 +63,9 @@ def get_db_session(project_dir: Path):
|
||||
session = SessionLocal()
|
||||
try:
|
||||
yield session
|
||||
except Exception:
|
||||
session.rollback()
|
||||
raise
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
@@ -131,7 +126,8 @@ async def list_features(project_name: str):
|
||||
if not project_dir.exists():
|
||||
raise HTTPException(status_code=404, detail="Project directory not found")
|
||||
|
||||
db_file = project_dir / "features.db"
|
||||
from autocoder_paths import get_features_db_path
|
||||
db_file = get_features_db_path(project_dir)
|
||||
if not db_file.exists():
|
||||
return FeatureListResponse(pending=[], in_progress=[], done=[])
|
||||
|
||||
@@ -326,7 +322,8 @@ async def get_dependency_graph(project_name: str):
|
||||
if not project_dir.exists():
|
||||
raise HTTPException(status_code=404, detail="Project directory not found")
|
||||
|
||||
db_file = project_dir / "features.db"
|
||||
from autocoder_paths import get_features_db_path
|
||||
db_file = get_features_db_path(project_dir)
|
||||
if not db_file.exists():
|
||||
return DependencyGraphResponse(nodes=[], edges=[])
|
||||
|
||||
@@ -344,6 +341,7 @@ async def get_dependency_graph(project_name: str):
|
||||
deps = f.dependencies or []
|
||||
blocking = [d for d in deps if d not in passing_ids]
|
||||
|
||||
status: Literal["pending", "in_progress", "done", "blocked"]
|
||||
if f.passes:
|
||||
status = "done"
|
||||
elif blocking:
|
||||
@@ -363,7 +361,7 @@ async def get_dependency_graph(project_name: str):
|
||||
))
|
||||
|
||||
for dep_id in deps:
|
||||
edges.append({"source": dep_id, "target": f.id})
|
||||
edges.append(DependencyGraphEdge(source=dep_id, target=f.id))
|
||||
|
||||
return DependencyGraphResponse(nodes=nodes, edges=edges)
|
||||
except HTTPException:
|
||||
@@ -390,7 +388,8 @@ async def get_feature(project_name: str, feature_id: int):
|
||||
if not project_dir.exists():
|
||||
raise HTTPException(status_code=404, detail="Project directory not found")
|
||||
|
||||
db_file = project_dir / "features.db"
|
||||
from autocoder_paths import get_features_db_path
|
||||
db_file = get_features_db_path(project_dir)
|
||||
if not db_file.exists():
|
||||
raise HTTPException(status_code=404, detail="No features database found")
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ API endpoints for browsing the filesystem for project folder selection.
|
||||
Provides cross-platform support for Windows, macOS, and Linux.
|
||||
"""
|
||||
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
@@ -14,6 +15,8 @@ from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query
|
||||
|
||||
from security import SENSITIVE_DIRECTORIES
|
||||
|
||||
# Module logger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -77,17 +80,10 @@ LINUX_BLOCKED = {
|
||||
"/opt",
|
||||
}
|
||||
|
||||
# Universal blocked paths (relative to home directory)
|
||||
UNIVERSAL_BLOCKED_RELATIVE = {
|
||||
".ssh",
|
||||
".aws",
|
||||
".gnupg",
|
||||
".config/gh",
|
||||
".netrc",
|
||||
".docker",
|
||||
".kube",
|
||||
".terraform",
|
||||
}
|
||||
# Universal blocked paths (relative to home directory).
|
||||
# Delegates to the canonical SENSITIVE_DIRECTORIES set in security.py so that
|
||||
# the filesystem browser and the EXTRA_READ_PATHS validator share one source of truth.
|
||||
UNIVERSAL_BLOCKED_RELATIVE = SENSITIVE_DIRECTORIES
|
||||
|
||||
# Patterns for files that should not be shown
|
||||
HIDDEN_PATTERNS = [
|
||||
@@ -99,8 +95,14 @@ HIDDEN_PATTERNS = [
|
||||
]
|
||||
|
||||
|
||||
def get_blocked_paths() -> set[Path]:
|
||||
"""Get the set of blocked paths for the current platform."""
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_blocked_paths() -> frozenset[Path]:
|
||||
"""
|
||||
Get the set of blocked paths for the current platform.
|
||||
|
||||
Cached because the platform and home directory do not change at runtime,
|
||||
and this function is called once per directory entry in list_directory().
|
||||
"""
|
||||
home = Path.home()
|
||||
blocked = set()
|
||||
|
||||
@@ -119,7 +121,7 @@ def get_blocked_paths() -> set[Path]:
|
||||
for rel in UNIVERSAL_BLOCKED_RELATIVE:
|
||||
blocked.add((home / rel).resolve())
|
||||
|
||||
return blocked
|
||||
return frozenset(blocked)
|
||||
|
||||
|
||||
def is_path_blocked(path: Path) -> bool:
|
||||
|
||||
@@ -10,6 +10,7 @@ import re
|
||||
import shutil
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
@@ -24,11 +25,12 @@ from ..schemas import (
|
||||
)
|
||||
|
||||
# Lazy imports to avoid circular dependencies
|
||||
# These are initialized by _init_imports() before first use.
|
||||
_imports_initialized = False
|
||||
_check_spec_exists = None
|
||||
_scaffold_project_prompts = None
|
||||
_get_project_prompts_dir = None
|
||||
_count_passing_tests = None
|
||||
_check_spec_exists: Callable[..., Any] | None = None
|
||||
_scaffold_project_prompts: Callable[..., Any] | None = None
|
||||
_get_project_prompts_dir: Callable[..., Any] | None = None
|
||||
_count_passing_tests: Callable[..., Any] | None = None
|
||||
|
||||
|
||||
def _init_imports():
|
||||
@@ -99,6 +101,7 @@ def validate_project_name(name: str) -> str:
|
||||
def get_project_stats(project_dir: Path) -> ProjectStats:
|
||||
"""Get statistics for a project."""
|
||||
_init_imports()
|
||||
assert _count_passing_tests is not None # guaranteed by _init_imports()
|
||||
passing, in_progress, total = _count_passing_tests(project_dir)
|
||||
percentage = (passing / total * 100) if total > 0 else 0.0
|
||||
return ProjectStats(
|
||||
@@ -113,6 +116,7 @@ def get_project_stats(project_dir: Path) -> ProjectStats:
|
||||
async def list_projects():
|
||||
"""List all registered projects."""
|
||||
_init_imports()
|
||||
assert _check_spec_exists is not None # guaranteed by _init_imports()
|
||||
(_, _, _, list_registered_projects, validate_project_path,
|
||||
get_project_concurrency, _) = _get_registry_functions()
|
||||
|
||||
@@ -145,6 +149,7 @@ async def list_projects():
|
||||
async def create_project(project: ProjectCreate):
|
||||
"""Create a new project at the specified path."""
|
||||
_init_imports()
|
||||
assert _scaffold_project_prompts is not None # guaranteed by _init_imports()
|
||||
(register_project, _, get_project_path, list_registered_projects,
|
||||
_, _, _) = _get_registry_functions()
|
||||
|
||||
@@ -225,6 +230,8 @@ async def create_project(project: ProjectCreate):
|
||||
async def get_project(name: str):
|
||||
"""Get detailed information about a project."""
|
||||
_init_imports()
|
||||
assert _check_spec_exists is not None # guaranteed by _init_imports()
|
||||
assert _get_project_prompts_dir is not None # guaranteed by _init_imports()
|
||||
(_, _, get_project_path, _, _, get_project_concurrency, _) = _get_registry_functions()
|
||||
|
||||
name = validate_project_name(name)
|
||||
@@ -269,8 +276,8 @@ async def delete_project(name: str, delete_files: bool = False):
|
||||
raise HTTPException(status_code=404, detail=f"Project '{name}' not found")
|
||||
|
||||
# Check if agent is running
|
||||
lock_file = project_dir / ".agent.lock"
|
||||
if lock_file.exists():
|
||||
from autocoder_paths import has_agent_running
|
||||
if has_agent_running(project_dir):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="Cannot delete project while agent is running. Stop the agent first."
|
||||
@@ -296,6 +303,7 @@ async def delete_project(name: str, delete_files: bool = False):
|
||||
async def get_project_prompts(name: str):
|
||||
"""Get the content of project prompt files."""
|
||||
_init_imports()
|
||||
assert _get_project_prompts_dir is not None # guaranteed by _init_imports()
|
||||
(_, _, get_project_path, _, _, _, _) = _get_registry_functions()
|
||||
|
||||
name = validate_project_name(name)
|
||||
@@ -307,7 +315,7 @@ async def get_project_prompts(name: str):
|
||||
if not project_dir.exists():
|
||||
raise HTTPException(status_code=404, detail="Project directory not found")
|
||||
|
||||
prompts_dir = _get_project_prompts_dir(project_dir)
|
||||
prompts_dir: Path = _get_project_prompts_dir(project_dir)
|
||||
|
||||
def read_file(filename: str) -> str:
|
||||
filepath = prompts_dir / filename
|
||||
@@ -329,6 +337,7 @@ async def get_project_prompts(name: str):
|
||||
async def update_project_prompts(name: str, prompts: ProjectPromptsUpdate):
|
||||
"""Update project prompt files."""
|
||||
_init_imports()
|
||||
assert _get_project_prompts_dir is not None # guaranteed by _init_imports()
|
||||
(_, _, get_project_path, _, _, _, _) = _get_registry_functions()
|
||||
|
||||
name = validate_project_name(name)
|
||||
@@ -398,8 +407,8 @@ async def reset_project(name: str, full_reset: bool = False):
|
||||
raise HTTPException(status_code=404, detail="Project directory not found")
|
||||
|
||||
# Check if agent is running
|
||||
lock_file = project_dir / ".agent.lock"
|
||||
if lock_file.exists():
|
||||
from autocoder_paths import has_agent_running
|
||||
if has_agent_running(project_dir):
|
||||
raise HTTPException(
|
||||
status_code=409,
|
||||
detail="Cannot reset project while agent is running. Stop the agent first."
|
||||
@@ -415,36 +424,58 @@ async def reset_project(name: str, full_reset: bool = False):
|
||||
|
||||
deleted_files: list[str] = []
|
||||
|
||||
# Files to delete in quick reset
|
||||
quick_reset_files = [
|
||||
"features.db",
|
||||
"features.db-wal", # WAL mode journal file
|
||||
"features.db-shm", # WAL mode shared memory file
|
||||
"assistant.db",
|
||||
"assistant.db-wal",
|
||||
"assistant.db-shm",
|
||||
".claude_settings.json",
|
||||
".claude_assistant_settings.json",
|
||||
from autocoder_paths import (
|
||||
get_assistant_db_path,
|
||||
get_claude_assistant_settings_path,
|
||||
get_claude_settings_path,
|
||||
get_features_db_path,
|
||||
)
|
||||
|
||||
# Build list of files to delete using path helpers (finds files at current location)
|
||||
# Plus explicit old-location fallbacks for backward compatibility
|
||||
db_path = get_features_db_path(project_dir)
|
||||
asst_path = get_assistant_db_path(project_dir)
|
||||
reset_files: list[Path] = [
|
||||
db_path,
|
||||
db_path.with_suffix(".db-wal"),
|
||||
db_path.with_suffix(".db-shm"),
|
||||
asst_path,
|
||||
asst_path.with_suffix(".db-wal"),
|
||||
asst_path.with_suffix(".db-shm"),
|
||||
get_claude_settings_path(project_dir),
|
||||
get_claude_assistant_settings_path(project_dir),
|
||||
# Also clean old root-level locations if they exist
|
||||
project_dir / "features.db",
|
||||
project_dir / "features.db-wal",
|
||||
project_dir / "features.db-shm",
|
||||
project_dir / "assistant.db",
|
||||
project_dir / "assistant.db-wal",
|
||||
project_dir / "assistant.db-shm",
|
||||
project_dir / ".claude_settings.json",
|
||||
project_dir / ".claude_assistant_settings.json",
|
||||
]
|
||||
|
||||
for filename in quick_reset_files:
|
||||
file_path = project_dir / filename
|
||||
for file_path in reset_files:
|
||||
if file_path.exists():
|
||||
try:
|
||||
relative = file_path.relative_to(project_dir)
|
||||
file_path.unlink()
|
||||
deleted_files.append(filename)
|
||||
deleted_files.append(str(relative))
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to delete {filename}: {e}")
|
||||
raise HTTPException(status_code=500, detail=f"Failed to delete {file_path.name}: {e}")
|
||||
|
||||
# Full reset: also delete prompts directory
|
||||
if full_reset:
|
||||
prompts_dir = project_dir / "prompts"
|
||||
if prompts_dir.exists():
|
||||
try:
|
||||
shutil.rmtree(prompts_dir)
|
||||
deleted_files.append("prompts/")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to delete prompts/: {e}")
|
||||
from autocoder_paths import get_prompts_dir
|
||||
# Delete prompts from both possible locations
|
||||
for prompts_dir in [get_prompts_dir(project_dir), project_dir / "prompts"]:
|
||||
if prompts_dir.exists():
|
||||
try:
|
||||
relative = prompts_dir.relative_to(project_dir)
|
||||
shutil.rmtree(prompts_dir)
|
||||
deleted_files.append(f"{relative}/")
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"Failed to delete prompts: {e}")
|
||||
|
||||
return {
|
||||
"success": True,
|
||||
@@ -458,6 +489,8 @@ async def reset_project(name: str, full_reset: bool = False):
|
||||
async def update_project_settings(name: str, settings: ProjectSettingsUpdate):
|
||||
"""Update project-level settings (concurrency, etc.)."""
|
||||
_init_imports()
|
||||
assert _check_spec_exists is not None # guaranteed by _init_imports()
|
||||
assert _get_project_prompts_dir is not None # guaranteed by _init_imports()
|
||||
(_, _, get_project_path, _, _, get_project_concurrency,
|
||||
set_project_concurrency) = _get_registry_functions()
|
||||
|
||||
|
||||
@@ -6,12 +6,10 @@ API endpoints for managing agent schedules.
|
||||
Provides CRUD operations for time-based schedule configuration.
|
||||
"""
|
||||
|
||||
import re
|
||||
import sys
|
||||
from contextlib import contextmanager
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from pathlib import Path
|
||||
from typing import Generator, Tuple
|
||||
from typing import TYPE_CHECKING, Generator, Tuple
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
@@ -26,17 +24,21 @@ from ..schemas import (
|
||||
ScheduleResponse,
|
||||
ScheduleUpdate,
|
||||
)
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import validate_project_name
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from api.database import Schedule as ScheduleModel
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Path:
|
||||
"""Get project path from registry."""
|
||||
root = Path(__file__).parent.parent.parent
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
|
||||
from registry import get_project_path
|
||||
return get_project_path(project_name)
|
||||
def _schedule_to_response(schedule: "ScheduleModel") -> ScheduleResponse:
|
||||
"""Convert a Schedule ORM object to a ScheduleResponse Pydantic model.
|
||||
|
||||
SQLAlchemy Column descriptors resolve to Python types at instance access time,
|
||||
but mypy sees the Column[T] descriptor type. Using model_validate with
|
||||
from_attributes handles this conversion correctly.
|
||||
"""
|
||||
return ScheduleResponse.model_validate(schedule, from_attributes=True)
|
||||
|
||||
router = APIRouter(
|
||||
prefix="/api/projects/{project_name}/schedules",
|
||||
@@ -44,16 +46,6 @@ router = APIRouter(
|
||||
)
|
||||
|
||||
|
||||
def validate_project_name(name: str) -> str:
|
||||
"""Validate and sanitize project name to prevent path traversal."""
|
||||
if not re.match(r'^[a-zA-Z0-9_-]{1,50}$', name):
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="Invalid project name"
|
||||
)
|
||||
return name
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _get_db_session(project_name: str) -> Generator[Tuple[Session, Path], None, None]:
|
||||
"""Get database session for a project as a context manager.
|
||||
@@ -84,6 +76,9 @@ def _get_db_session(project_name: str) -> Generator[Tuple[Session, Path], None,
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db, project_path
|
||||
except Exception:
|
||||
db.rollback()
|
||||
raise
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
@@ -99,21 +94,7 @@ async def list_schedules(project_name: str):
|
||||
).order_by(Schedule.start_time).all()
|
||||
|
||||
return ScheduleListResponse(
|
||||
schedules=[
|
||||
ScheduleResponse(
|
||||
id=s.id,
|
||||
project_name=s.project_name,
|
||||
start_time=s.start_time,
|
||||
duration_minutes=s.duration_minutes,
|
||||
days_of_week=s.days_of_week,
|
||||
enabled=s.enabled,
|
||||
yolo_mode=s.yolo_mode,
|
||||
model=s.model,
|
||||
crash_count=s.crash_count,
|
||||
created_at=s.created_at,
|
||||
)
|
||||
for s in schedules
|
||||
]
|
||||
schedules=[_schedule_to_response(s) for s in schedules]
|
||||
)
|
||||
|
||||
|
||||
@@ -187,18 +168,7 @@ async def create_schedule(project_name: str, data: ScheduleCreate):
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to start agent for schedule {schedule.id}: {e}", exc_info=True)
|
||||
|
||||
return ScheduleResponse(
|
||||
id=schedule.id,
|
||||
project_name=schedule.project_name,
|
||||
start_time=schedule.start_time,
|
||||
duration_minutes=schedule.duration_minutes,
|
||||
days_of_week=schedule.days_of_week,
|
||||
enabled=schedule.enabled,
|
||||
yolo_mode=schedule.yolo_mode,
|
||||
model=schedule.model,
|
||||
crash_count=schedule.crash_count,
|
||||
created_at=schedule.created_at,
|
||||
)
|
||||
return _schedule_to_response(schedule)
|
||||
|
||||
|
||||
@router.get("/next", response_model=NextRunResponse)
|
||||
@@ -256,8 +226,8 @@ async def get_next_scheduled_run(project_name: str):
|
||||
|
||||
return NextRunResponse(
|
||||
has_schedules=True,
|
||||
next_start=next_start.isoformat() if (active_count == 0 and next_start) else None,
|
||||
next_end=latest_end.isoformat() if latest_end else None,
|
||||
next_start=next_start if active_count == 0 else None,
|
||||
next_end=latest_end,
|
||||
is_currently_running=active_count > 0,
|
||||
active_schedule_count=active_count,
|
||||
)
|
||||
@@ -277,18 +247,7 @@ async def get_schedule(project_name: str, schedule_id: int):
|
||||
if not schedule:
|
||||
raise HTTPException(status_code=404, detail="Schedule not found")
|
||||
|
||||
return ScheduleResponse(
|
||||
id=schedule.id,
|
||||
project_name=schedule.project_name,
|
||||
start_time=schedule.start_time,
|
||||
duration_minutes=schedule.duration_minutes,
|
||||
days_of_week=schedule.days_of_week,
|
||||
enabled=schedule.enabled,
|
||||
yolo_mode=schedule.yolo_mode,
|
||||
model=schedule.model,
|
||||
crash_count=schedule.crash_count,
|
||||
created_at=schedule.created_at,
|
||||
)
|
||||
return _schedule_to_response(schedule)
|
||||
|
||||
|
||||
@router.patch("/{schedule_id}", response_model=ScheduleResponse)
|
||||
@@ -331,18 +290,7 @@ async def update_schedule(
|
||||
# Was enabled, now disabled - remove jobs
|
||||
scheduler.remove_schedule(schedule_id)
|
||||
|
||||
return ScheduleResponse(
|
||||
id=schedule.id,
|
||||
project_name=schedule.project_name,
|
||||
start_time=schedule.start_time,
|
||||
duration_minutes=schedule.duration_minutes,
|
||||
days_of_week=schedule.days_of_week,
|
||||
enabled=schedule.enabled,
|
||||
yolo_mode=schedule.yolo_mode,
|
||||
model=schedule.model,
|
||||
crash_count=schedule.crash_count,
|
||||
created_at=schedule.created_at,
|
||||
)
|
||||
return _schedule_to_response(schedule)
|
||||
|
||||
|
||||
@router.delete("/{schedule_id}", status_code=204)
|
||||
|
||||
@@ -9,17 +9,16 @@ Settings are stored in the registry database and shared across all projects.
|
||||
import mimetypes
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter
|
||||
|
||||
from ..schemas import ModelInfo, ModelsResponse, SettingsResponse, SettingsUpdate
|
||||
from ..services.chat_constants import ROOT_DIR
|
||||
|
||||
# Mimetype fix for Windows - must run before StaticFiles is mounted
|
||||
mimetypes.add_type("text/javascript", ".js", True)
|
||||
|
||||
# Add root to path for registry import
|
||||
ROOT_DIR = Path(__file__).parent.parent.parent
|
||||
# Ensure root is on sys.path for registry import
|
||||
if str(ROOT_DIR) not in sys.path:
|
||||
sys.path.insert(0, str(ROOT_DIR))
|
||||
|
||||
@@ -92,6 +91,8 @@ async def get_settings():
|
||||
glm_mode=_is_glm_mode(),
|
||||
ollama_mode=_is_ollama_mode(),
|
||||
testing_agent_ratio=_parse_int(all_settings.get("testing_agent_ratio"), 1),
|
||||
playwright_headless=_parse_bool(all_settings.get("playwright_headless"), default=True),
|
||||
batch_size=_parse_int(all_settings.get("batch_size"), 3),
|
||||
)
|
||||
|
||||
|
||||
@@ -107,6 +108,12 @@ async def update_settings(update: SettingsUpdate):
|
||||
if update.testing_agent_ratio is not None:
|
||||
set_setting("testing_agent_ratio", str(update.testing_agent_ratio))
|
||||
|
||||
if update.playwright_headless is not None:
|
||||
set_setting("playwright_headless", "true" if update.playwright_headless else "false")
|
||||
|
||||
if update.batch_size is not None:
|
||||
set_setting("batch_size", str(update.batch_size))
|
||||
|
||||
# Return updated settings
|
||||
all_settings = get_all_settings()
|
||||
return SettingsResponse(
|
||||
@@ -115,4 +122,6 @@ async def update_settings(update: SettingsUpdate):
|
||||
glm_mode=_is_glm_mode(),
|
||||
ollama_mode=_is_ollama_mode(),
|
||||
testing_agent_ratio=_parse_int(all_settings.get("testing_agent_ratio"), 1),
|
||||
playwright_headless=_parse_bool(all_settings.get("playwright_headless"), default=True),
|
||||
batch_size=_parse_int(all_settings.get("batch_size"), 3),
|
||||
)
|
||||
|
||||
@@ -7,8 +7,6 @@ WebSocket and REST endpoints for interactive spec creation with Claude.
|
||||
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, WebSocket, WebSocketDisconnect
|
||||
@@ -22,30 +20,13 @@ from ..services.spec_chat_session import (
|
||||
list_sessions,
|
||||
remove_session,
|
||||
)
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import is_valid_project_name as validate_project_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/spec", tags=["spec-creation"])
|
||||
|
||||
# Root directory
|
||||
ROOT_DIR = Path(__file__).parent.parent.parent
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Path:
|
||||
"""Get project path from registry."""
|
||||
import sys
|
||||
root = Path(__file__).parent.parent.parent
|
||||
if str(root) not in sys.path:
|
||||
sys.path.insert(0, str(root))
|
||||
|
||||
from registry import get_project_path
|
||||
return get_project_path(project_name)
|
||||
|
||||
|
||||
def validate_project_name(name: str) -> bool:
|
||||
"""Validate project name to prevent path traversal."""
|
||||
return bool(re.match(r'^[a-zA-Z0-9_-]{1,50}$', name))
|
||||
|
||||
|
||||
# ============================================================================
|
||||
# REST Endpoints
|
||||
@@ -124,7 +105,8 @@ async def get_spec_file_status(project_name: str):
|
||||
if not project_dir.exists():
|
||||
raise HTTPException(status_code=404, detail="Project directory not found")
|
||||
|
||||
status_file = project_dir / "prompts" / ".spec_status.json"
|
||||
from autocoder_paths import get_prompts_dir
|
||||
status_file = get_prompts_dir(project_dir) / ".spec_status.json"
|
||||
|
||||
if not status_file.exists():
|
||||
return SpecFileStatus(
|
||||
|
||||
@@ -12,8 +12,6 @@ import base64
|
||||
import json
|
||||
import logging
|
||||
import re
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import APIRouter, HTTPException, WebSocket, WebSocketDisconnect
|
||||
from pydantic import BaseModel
|
||||
@@ -27,13 +25,8 @@ from ..services.terminal_manager import (
|
||||
rename_terminal,
|
||||
stop_terminal_session,
|
||||
)
|
||||
|
||||
# Add project root to path for registry import
|
||||
_root = Path(__file__).parent.parent.parent
|
||||
if str(_root) not in sys.path:
|
||||
sys.path.insert(0, str(_root))
|
||||
|
||||
from registry import get_project_path as registry_get_project_path
|
||||
from ..utils.project_helpers import get_project_path as _get_project_path
|
||||
from ..utils.validation import is_valid_project_name as validate_project_name
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -48,27 +41,6 @@ class TerminalCloseCode:
|
||||
FAILED_TO_START = 4500
|
||||
|
||||
|
||||
def _get_project_path(project_name: str) -> Path | None:
|
||||
"""Get project path from registry."""
|
||||
return registry_get_project_path(project_name)
|
||||
|
||||
|
||||
def validate_project_name(name: str) -> bool:
|
||||
"""
|
||||
Validate project name to prevent path traversal attacks.
|
||||
|
||||
Allows only alphanumeric characters, underscores, and hyphens.
|
||||
Maximum length of 50 characters.
|
||||
|
||||
Args:
|
||||
name: The project name to validate
|
||||
|
||||
Returns:
|
||||
True if valid, False otherwise
|
||||
"""
|
||||
return bool(re.match(r"^[a-zA-Z0-9_-]{1,50}$", name))
|
||||
|
||||
|
||||
def validate_terminal_id(terminal_id: str) -> bool:
|
||||
"""
|
||||
Validate terminal ID format.
|
||||
|
||||
Reference in New Issue
Block a user