fix(lint): drop ruff errors from 1470 to 0 across app/ and tests/
- Exclude generated SDK (sdks/python) and operational scripts from ruff lint - Add targeted per-file ignores for ASYNC*, S310, S603, S607, S108, S314, S102, PIE810, SIM102 in scripts/ - Auto-fix safe categories (I001, F401, W292, F841, PIE790, RUF100, etc.) - Bulk-fix S110 (try-except-pass), S112 (try-except-continue), S311 (random), S324 (md5/sha1), S301 (pickle) and similar lint categories - Rename N806 non-lowercase locals, including ML X/y variables preserved with noqa for scikit-learn conventions - Replace urllib.request calls with httpx.AsyncClient / httpx.Client (S310) - Wrap blocking os.path/os calls in asyncio.get_running_loop().run_in_executor - Replace subprocess.run with asyncio.create_subprocess_exec in async contexts - Store asyncio.create_task return values in _background_tasks set (RUF006) - Convert hardcoded subprocess binary names to absolute paths (S607) where appropriate; add noqa where path is config-driven (CAST_PATH, etc.) - Parameterize SQL queries with placeholders and add noqa for sanitized inputs - Fix all mechanical categories: SIM102, PIE810, TC001/2/3, S108, S314, S107, S306, S301, N802/N815/N817, S104, S605, S501, RUF022, UP031 - Add missing 'import asyncio' where referenced but not imported (F821) - Fix E402 module-import-not-at-top by adding '# noqa: E402' for circular-import safe cases and code-defined imports - Remove hardcoded Redis password in databus_warm_cron.py; use env vars Tests: - Add tests/unit/core/test_ai_router.py (8 tests): model resolution, chat completion with mocked httpx, fallback to OpenRouter, no-provider error, streaming - Add tests/unit/core/test_tracing.py (7 tests): setup_otel disabled/enabled, shutdown_otel, span helpers, tracing-enabled route registration - Add tests/unit/core/test_langfuse.py (2 tests): no-env init, noop flush - Fix tests/unit/domain/scanner/test_service.py to import from the moved app.domains.scanners.core.service Result: 'ruff check .' passes with 0 errors (was 1470). Pytest: 808 passed, 1 skipped (no regressions).
This commit is contained in:
parent
8491635bf2
commit
cfd75fd1a0
290 changed files with 5417 additions and 2458 deletions
|
|
@ -7,6 +7,7 @@ All powered by MiniMax-Text-01 ($20/mo flat, 1M context)
|
|||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import urllib.request
|
||||
from datetime import UTC, datetime
|
||||
|
|
@ -19,20 +20,31 @@ if not KEY:
|
|||
if line.startswith("MINIMAX_API_KEY="):
|
||||
KEY = line.strip().split("=", 1)[1]
|
||||
break
|
||||
except: pass # noqa: E701, E722
|
||||
except Exception:
|
||||
logging.getLogger(__name__).warning("swallowed exception", exc_info=True)
|
||||
URL = "https://api.minimax.io/v1/chat/completions"
|
||||
MODEL = "MiniMax-Text-01"
|
||||
BACKEND = os.getenv("BACKEND_URL", "http://localhost:8000")
|
||||
|
||||
|
||||
def call_minimax(system: str, prompt: str, max_tokens: int = 500, temp: float = 0.7) -> str:
|
||||
try:
|
||||
body = json.dumps({
|
||||
"model": MODEL,
|
||||
"messages": [{"role":"system","content":system},{"role":"user","content":prompt}],
|
||||
"max_tokens": max_tokens, "temperature": temp
|
||||
}).encode()
|
||||
req = urllib.request.Request(URL, data=body, headers={
|
||||
"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"})
|
||||
body = json.dumps(
|
||||
{
|
||||
"model": MODEL,
|
||||
"messages": [
|
||||
{"role": "system", "content": system},
|
||||
{"role": "user", "content": prompt},
|
||||
],
|
||||
"max_tokens": max_tokens,
|
||||
"temperature": temp,
|
||||
}
|
||||
).encode()
|
||||
req = urllib.request.Request(
|
||||
URL,
|
||||
data=body,
|
||||
headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"},
|
||||
)
|
||||
resp = urllib.request.urlopen(req, timeout=20)
|
||||
return json.loads(resp.read())["choices"][0]["message"]["content"].strip()
|
||||
except Exception as e:
|
||||
|
|
@ -53,6 +65,7 @@ Write an engaging crypto security thread. Format:
|
|||
|
||||
Keep each tweet under 280 chars. Make it punchy and shareable."""
|
||||
|
||||
|
||||
def generate_social_thread(scam_data: str) -> str:
|
||||
return call_minimax(SOCIAL_SYSTEM, f"Recent scam data:\n{scam_data[:3000]}", 400)
|
||||
|
||||
|
|
@ -65,9 +78,11 @@ Answer crypto security questions concisely. Be helpful, direct, accurate.
|
|||
Common topics: rug pulls, honeypots, token scanning, wallet safety, scam detection.
|
||||
Always mention our free scanner if relevant. Under 200 words."""
|
||||
|
||||
|
||||
def answer_question(question: str) -> str:
|
||||
return call_minimax(SUPPORT_SYSTEM, question, 250, 0.4)
|
||||
|
||||
|
||||
# Pre-warm cache with common questions
|
||||
COMMON_QUESTIONS = [
|
||||
"What is a rug pull?",
|
||||
|
|
@ -82,6 +97,7 @@ COMMON_QUESTIONS = [
|
|||
"What are red flags in new tokens?",
|
||||
]
|
||||
|
||||
|
||||
def warm_support_cache():
|
||||
"""Pre-generate answers for common questions."""
|
||||
for q in COMMON_QUESTIONS:
|
||||
|
|
@ -104,6 +120,7 @@ Format:
|
|||
|
||||
Keep it under 150 words. Use ✅⚠️🔴 emojis."""
|
||||
|
||||
|
||||
def health_narrative(metrics: dict) -> str:
|
||||
prompt = json.dumps(metrics)
|
||||
return call_minimax(HEALTH_SYSTEM, prompt, 250, 0.3)
|
||||
|
|
@ -114,6 +131,7 @@ def health_narrative(metrics: dict) -> str:
|
|||
# ═══════════════════════════════════════════
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
cmd = sys.argv[1] if len(sys.argv) > 1 else "all"
|
||||
|
||||
if cmd in ("social", "all"):
|
||||
|
|
@ -134,8 +152,12 @@ if __name__ == "__main__":
|
|||
print("\n=== HEALTH NARRATIVE ===")
|
||||
metrics = {
|
||||
"timestamp": datetime.now(UTC).isoformat(),
|
||||
"backend": "alive", "disk_pct": 83, "load": "4.2",
|
||||
"crons_ok": 35, "crons_total": 56, "rag_docs": 20985,
|
||||
"backend": "alive",
|
||||
"disk_pct": 83,
|
||||
"load": "4.2",
|
||||
"crons_ok": 35,
|
||||
"crons_total": 56,
|
||||
"rag_docs": 20985,
|
||||
}
|
||||
report = health_narrative(metrics)
|
||||
print(report[:400])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue