Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ EMBEDDING_BATCH_SIZE=32
# EMBEDDING_API_CONCURRENCY=4
# Estimated token cap per API batch request (default: 100000).
# EMBEDDING_MAX_TOKENS_PER_REQUEST=100000
# Estimated token cap for a single input chunk before truncation (default: 8000).
# EMBEDDING_MAX_TOKENS_PER_INPUT=8000
# Optional tokens-per-minute pacing limit for strict quotas (default: 0 / unlimited).
# EMBEDDING_TPM_LIMIT=0

Expand Down
13 changes: 11 additions & 2 deletions REQUIREMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> **Note:** This document is automatically generated and verified against the live test suite by `scripts/generate_requirements.py` and `tests/backend/test_requirements_sync.py`.

**Test Verification Baseline:** **1357 Automated Tests** (993 Pytest Backend + 314 Vitest Frontend + 50 Playwright E2E).
**Test Verification Baseline:** **1361 Automated Tests** (997 Pytest Backend + 314 Vitest Frontend + 50 Playwright E2E).

---

Expand Down Expand Up @@ -593,13 +593,17 @@ classDiagram
- `test_sse_stream_live_events_and_unsubscription`
- `test_sse_stream_keep_alive_ping`

#### `tests/backend/test_git_syncer_progress.py` (6 tests)
#### `tests/backend/test_git_syncer_progress.py` (7 tests)
- `test_git_syncer_reports_stages_up_to_date`
- `test_git_syncer_clone_failure`
- `test_git_syncer_full_5_stage_sync`
- `test_git_syncer_cancellation_during_stage_4`
- `test_git_syncer_nonexistent_repo`
- `test_git_progress_tracker_pending_cancellation`
- `test_git_syncer_failure_stops_producer_keeps_error_status_and_only_counts_persisted_files` - _Issues #179 & #180: Verifies that when batch 2 fails in the persist stage:
1. The producer stops before reading all remaining files and logs no 'No such file or directory' after temp_dir cleanup.
2. Only batch 1 files (actually saved to DB) are counted in processed_files and logged as 'Ingested'.
3. sync-status remains 'error' matching the repository table record._

#### `tests/backend/test_health_check_status.py` (12 tests)
- `test_api_stats_reports_healthy_when_store_is_healthy`
Expand Down Expand Up @@ -1132,6 +1136,11 @@ code results are enriched with signature/kind/ast_symbol_id from SQLite._
- `test_embedding_cache_invalidation`
- `test_embedding_cache_empty_inputs`

#### `tests/test_embedding_input_limits_and_pipeline_recovery.py` (3 tests)
- `test_per_input_token_limit_truncates_oversized_chunk_before_batching` - _Issue #178: Verifies oversized inputs exceeding max_tokens_per_input are truncated before batching and configurable via EMBEDDING_MAX_TOKENS_PER_INPUT._
- `test_http_400_oversized_input_splits_batch_skips_bad_chunk_and_continues_sync` - _Issue #178: Verifies that on an HTTP 400 error for a batch, the batcher splits and retries halves, skips only the bad input, logs its file path, and completes the repo sync._
- `test_pipeline_producer_stops_when_persist_stage_raises` - _Issue #179: Verifies that when the persist stage raises, the producer stops before reading all remaining files and threads are joined before executor shutdown._

#### `tests/test_embedding_model_transition.py` (6 tests)
- `test_update_embedding_config_rejection_when_index_has_content` - _Verifies that changing dense model/dimension without rebuild_index=True raises ValueError if index has content._
- `test_api_save_embedding_settings_rejection_returns_400` - _Verifies POST /admin/api/settings/embedding returns HTTP 400 when change is rejected._
Expand Down
4 changes: 4 additions & 0 deletions app/api/routers/ai_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ async def api_save_embedding_settings(payload: EmbeddingSettingsRequest):
sparse_model=payload.sparse_model,
threads=payload.threads,
batch_size=payload.batch_size,
api_concurrency=payload.api_concurrency,
max_tokens_per_request=payload.max_tokens_per_request,
max_tokens_per_input=payload.max_tokens_per_input,
tpm_limit=payload.tpm_limit,
litellm_url=payload.litellm_url,
litellm_api_key=payload.litellm_api_key,
vision_ocr_model=payload.vision_ocr_model,
Expand Down
4 changes: 4 additions & 0 deletions app/models/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ class EmbeddingSettingsRequest(BaseModel):
sparse_model: Optional[str] = None
threads: Optional[int] = Field(default=None, ge=1, le=128)
batch_size: Optional[int] = Field(default=None, ge=1, le=1024)
api_concurrency: Optional[int] = Field(default=None, ge=1, le=128)
max_tokens_per_request: Optional[int] = Field(default=None, ge=1)
max_tokens_per_input: Optional[int] = Field(default=None, ge=1)
tpm_limit: Optional[int] = Field(default=None, ge=0)
litellm_url: Optional[str] = None
litellm_api_key: Optional[str] = None
vision_ocr_model: Optional[str] = None
Expand Down
62 changes: 27 additions & 35 deletions app/services/database/config_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,10 @@ def detect_system_resources() -> Dict[str, Any]:

class _MEMORYSTATUSEX(ctypes.Structure):
_fields_ = [
("dwLength", ctypes.c_ulong),
("dwMemoryLoad", ctypes.c_ulong),
("ullTotalPhys", ctypes.c_ulonglong),
("ullAvailPhys", ctypes.c_ulonglong),
("ullTotalPageFile", ctypes.c_ulonglong),
("ullAvailPageFile", ctypes.c_ulonglong),
("ullTotalVirtual", ctypes.c_ulonglong),
("ullAvailVirtual", ctypes.c_ulonglong),
("dwLength", ctypes.c_ulong), ("dwMemoryLoad", ctypes.c_ulong),
("ullTotalPhys", ctypes.c_ulonglong), ("ullAvailPhys", ctypes.c_ulonglong),
("ullTotalPageFile", ctypes.c_ulonglong), ("ullAvailPageFile", ctypes.c_ulonglong),
("ullTotalVirtual", ctypes.c_ulonglong), ("ullAvailVirtual", ctypes.c_ulonglong),
("sullAvailExtendedVirtual", ctypes.c_ulonglong),
]

Expand Down Expand Up @@ -217,45 +213,31 @@ def _resolve_default_embedding_config(conn: Optional[Any] = None) -> Dict[str, A
env_max_tokens = os.getenv("EMBEDDING_MAX_TOKENS_PER_REQUEST")
max_tokens_per_request = int(env_max_tokens) if env_max_tokens and env_max_tokens.isdigit() else 100000

env_max_input_tokens = os.getenv("EMBEDDING_MAX_TOKENS_PER_INPUT")
max_tokens_per_input = int(env_max_input_tokens) if env_max_input_tokens and env_max_input_tokens.isdigit() else 8000

env_tpm = os.getenv("EMBEDDING_TPM_LIMIT")
tpm_limit = int(env_tpm) if env_tpm and env_tpm.isdigit() else 0

dense_model = os.getenv("EMBEDDING_MODEL", "BAAI/bge-small-en-v1.5").strip()
sparse_model = os.getenv("SPARSE_MODEL", "Qdrant/bm25").strip()
litellm_url = (
os.getenv("OPENAI_API_URL")
or os.getenv("OPENAI_BASE_URL")
or os.getenv("LITELLM_URL")
or "http://litellm:4000/v1"
).strip()
litellm_url = (os.getenv("OPENAI_API_URL") or os.getenv("OPENAI_BASE_URL") or os.getenv("LITELLM_URL") or "http://litellm:4000/v1").strip()
raw_api_key = (os.getenv("OPENAI_API_KEY") or os.getenv("LITELLM_API_KEY") or "").strip()
litellm_api_key = "" if _is_placeholder_api_key(raw_api_key) else raw_api_key

vision_ocr_model = os.getenv("VISION_OCR_MODEL", "").strip()
chat_model = os.getenv("CHAT_MODEL", "").strip()

allow_fallback_env = os.getenv("EMBEDDING_ALLOW_FALLBACK", "false").lower().strip()
allow_fallback = allow_fallback_env in ("1", "true", "yes")
allow_fallback = os.getenv("EMBEDDING_ALLOW_FALLBACK", "false").lower().strip() in ("1", "true", "yes")
fallback_dense_model = os.getenv("EMBEDDING_FALLBACK_DENSE_MODEL", "").strip()

return {
"provider": provider,
"dense_model": dense_model,
"sparse_model": sparse_model,
"threads": max(1, threads),
"batch_size": max(1, batch_size),
"api_concurrency": max(1, api_concurrency),
"max_tokens_per_request": max(1, max_tokens_per_request),
"tpm_limit": max(0, tpm_limit),
"openai_api_url": litellm_url,
"litellm_url": litellm_url,
"litellm_api_key": litellm_api_key,
"vision_ocr_model": vision_ocr_model,
"chat_model": chat_model,
"allow_fallback": allow_fallback,
"fallback_dense_model": fallback_dense_model,
"system_cpus": sys_res["cpus"],
"system_memory_gb": sys_res["memory_gb"],
"provider": provider, "dense_model": dense_model, "sparse_model": sparse_model,
"threads": max(1, threads), "batch_size": max(1, batch_size),
"api_concurrency": max(1, api_concurrency), "max_tokens_per_request": max(1, max_tokens_per_request),
"max_tokens_per_input": max(1, max_tokens_per_input), "tpm_limit": max(0, tpm_limit),
"openai_api_url": litellm_url, "litellm_url": litellm_url, "litellm_api_key": litellm_api_key,
"vision_ocr_model": vision_ocr_model, "chat_model": chat_model,
"allow_fallback": allow_fallback, "fallback_dense_model": fallback_dense_model,
"system_cpus": sys_res["cpus"], "system_memory_gb": sys_res["memory_gb"],
}


Expand All @@ -267,6 +249,7 @@ def get_embedding_db_config() -> Dict[str, Any]:
batch_size_str = _get_meta("embedding_batch_size")
concurrency_str = _get_meta("embedding_api_concurrency")
max_tokens_str = _get_meta("embedding_max_tokens_per_request")
max_input_tokens_str = _get_meta("embedding_max_tokens_per_input")
tpm_limit_str = _get_meta("embedding_tpm_limit")
litellm_url = _get_meta("openai_api_url") or _get_meta("embedding_litellm_url")
litellm_api_key = _get_meta("openai_api_key") or _get_meta("embedding_litellm_api_key")
Expand All @@ -291,6 +274,11 @@ def get_embedding_db_config() -> Dict[str, Any]:

api_concurrency = int(concurrency_str) if concurrency_str and concurrency_str.isdigit() else default_cfg["api_concurrency"]
max_tokens = int(max_tokens_str) if max_tokens_str and max_tokens_str.isdigit() else default_cfg["max_tokens_per_request"]
max_input_tokens = (
int(max_input_tokens_str)
if max_input_tokens_str and max_input_tokens_str.isdigit()
else default_cfg["max_tokens_per_input"]
)
tpm_limit = int(tpm_limit_str) if tpm_limit_str and tpm_limit_str.isdigit() else default_cfg["tpm_limit"]
allow_fallback = (allow_fallback_str == "1") if allow_fallback_str is not None else default_cfg["allow_fallback"]

Expand All @@ -305,6 +293,7 @@ def get_embedding_db_config() -> Dict[str, Any]:
"batch_size": max(1, batch_size),
"api_concurrency": max(1, api_concurrency),
"max_tokens_per_request": max(1, max_tokens),
"max_tokens_per_input": max(1, max_input_tokens),
"tpm_limit": max(0, tpm_limit),
"openai_api_url": resolved_url,
"litellm_url": resolved_url,
Expand All @@ -328,6 +317,7 @@ def set_embedding_db_config(
batch_size: Optional[int] = None,
api_concurrency: Optional[int] = None,
max_tokens_per_request: Optional[int] = None,
max_tokens_per_input: Optional[int] = None,
tpm_limit: Optional[int] = None,
litellm_url: Optional[str] = None,
litellm_api_key: Optional[str] = None,
Expand All @@ -351,6 +341,8 @@ def set_embedding_db_config(
_set_meta("embedding_api_concurrency", str(max(1, api_concurrency)))
if max_tokens_per_request is not None:
_set_meta("embedding_max_tokens_per_request", str(max(1, max_tokens_per_request)))
if max_tokens_per_input is not None:
_set_meta("embedding_max_tokens_per_input", str(max(1, max_tokens_per_input)))
if tpm_limit is not None:
_set_meta("embedding_tpm_limit", str(max(0, tpm_limit)))
if litellm_url is not None:
Expand Down
Loading
Loading