mirror of
https://github.com/leonvanzyl/autocoder.git
synced 2026-02-02 07:23:35 +00:00
refactor: optimize token usage, deduplicate code, fix bugs across agents
Token reduction (~40% per session, ~2.3M fewer tokens per 200-feature project): - Agent-type-specific tool lists: coding 9, testing 5, init 5 (was 19 for all) - Right-sized max_turns: coding 300, testing 100 (was 1000 for all) - Trimmed coding prompt template (~150 lines removed) - Streamlined testing prompt with batch support - YOLO mode now strips browser testing instructions from prompt - Added Grep, WebFetch, WebSearch to expand project session Performance improvements: - Rate limit retries start at ~15s with jitter (was fixed 60s) - Post-spawn delay reduced to 0.5s (was 2s) - Orchestrator consolidated to 1 DB query per loop (was 5-7) - Testing agents batch 3 features per session (was 1) - Smart context compaction preserves critical state, discards noise Bug fixes: - Removed ghost feature_release_testing MCP tool (wasted tokens every test session) - Forward all 9 Vertex AI env vars to chat sessions (was missing 3) - Fix DetachedInstanceError risk in test batch ORM access - Prevent duplicate testing of same features in parallel mode Code deduplication: - _get_project_path(): 9 copies -> 1 shared utility (project_helpers.py) - validate_project_name(): 9 copies -> 2 variants in 1 file (validation.py) - ROOT_DIR: 10 copies -> 1 definition (chat_constants.py) - API_ENV_VARS: 4 copies -> 1 source of truth (env_constants.py) Security hardening: - Unified sensitive directory blocklist (14 dirs, was two divergent lists) - Cached get_blocked_paths() for O(1) directory listing checks - Terminal security warning when ALLOW_REMOTE=1 exposes WebSocket - 20 new security tests for EXTRA_READ_PATHS blocking - Extracted _validate_command_list() and _validate_pkill_processes() helpers Type safety: - 87 mypy errors -> 0 across 58 source files - Installed types-PyYAML for proper yaml stub types - Fixed SQLAlchemy Column[T] coercions across all routers Dead code removed: - 13 files deleted (~2,679 lines): unused UI components, debug logs, outdated docs - 7 unused npm packages removed (Radix UI components with 0 imports) - AgentAvatar.tsx reduced from 615 -> 119 lines (SVGs extracted to mascotData.tsx) New CLI options: - --testing-batch-size (1-5) for parallel mode test batching - --testing-feature-ids for direct multi-feature testing Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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.
|
||||
@@ -102,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]
|
||||
)
|
||||
|
||||
|
||||
@@ -190,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)
|
||||
@@ -259,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,
|
||||
)
|
||||
@@ -280,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)
|
||||
@@ -334,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)
|
||||
|
||||
Reference in New Issue
Block a user