diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f1bc859 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,35 @@ +.git +.gitignore +.env* + +.venv/ +venv/ +env/ +**/.venv/ +**/venv/ +**/node_modules/ + +**/__pycache__/ +*.py[cod] +.pytest_cache/ +.ruff_cache/ +.coverage +coverage.xml +junit*.xml +htmlcov/ +test-results/ +tests/ + +data/*.db +*.sqlite +*.sqlite3 +.codex-* + +secrets/ +**/secrets/ +*.pem +*.key +*.p12 +*.pfx + +output/builds/build/ diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..09f0df1 --- /dev/null +++ b/.env.example @@ -0,0 +1,29 @@ +# SentinelForge Environment Configuration Example +# Copy this file to .env and populate with production values. Do NOT commit .env. + +# Cesium Ion personal access token for 3D globe tiles and imagery +CESIUM_ION_TOKEN=replace_with_cesium_ion_token + +# Internal API authentication key for SentinelForge REST endpoints +SF_API_KEY=replace_with_internal_api_key + +# API key required in the X-API-Key header for /api/nlp +NLP_API_KEY=replace_with_nlp_api_key + +# Google Gemini API key for natural language command interpretation +GEMINI_API_KEY=replace_with_gemini_api_key + +# Comma-separated list of allowed CORS origins for serverless endpoints +ALLOWED_ORIGINS=https://example.invalid + +# PostgreSQL / PostGIS database connection URL +DATABASE_URL=replace_with_database_connection_url + +# PostgreSQL password for catalog store +POSTGIS_PASSWORD=replace_with_database_password + +# Mutual TLS SSL private key content for Kafka transport +KAFKA_SSL_KEY=replace_with_kafka_private_key + +# Optional path to PyTorch weights for thermospheric density MLP +DENSITY_MODEL_PATH=/path/to/density_mlp.pth diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d56c154..8de7f8b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -25,18 +25,17 @@ jobs: run: | python -m pip install --upgrade pip pip install -r requirements.txt - pip install ruff + pip install pytest ruff - - name: Lint with ruff + - name: Lint repository for fatal errors run: | - ruff check --select=E,F,W --ignore=E501,E402 \ - main.py \ - end_to_end_test.py \ - output/builds/src/science/ \ - output/builds/src/orbit_propagator.py \ - output/builds/src/conjunction_screener.py \ - output/builds/src/multi_sensor_fusion.py \ - output/builds/src/observation_scheduler.py + ruff check . --select=E9,F63,F7,F82 \ + --exclude=output/builds/src/hardware/ascom_hal.py + + - name: Run test suite + env: + PYTHONIOENCODING: utf-8 + run: pytest -q - name: Run end-to-end pipeline test env: @@ -68,4 +67,12 @@ jobs: - name: Verify CMakeLists.txt parses run: | cd output/builds/src - cmake -B build -DCMAKE_CUDA_ARCHITECTURES=87 2>&1 || echo "CMake parse check complete (CUDA toolkit not installed — expected)" + if cmake -B build -DCMAKE_CUDA_ARCHITECTURES=87 >cmake.log 2>&1; then + cat cmake.log + elif grep -Eq "Failed to find nvcc|Could not find nvcc|No CMAKE_CUDA_COMPILER" cmake.log; then + cat cmake.log + echo "CMake parse check complete (CUDA toolkit not installed — expected)" + else + cat cmake.log + exit 1 + fi diff --git a/.gitignore b/.gitignore index 6328d2b..34a7439 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,8 @@ fix*.ps1 data/real_frame.fits data/synthetic_frame.raw benchmarks/benchmark_results.json + +# Environment variables and secrets +.env +.env.* +!.env.example diff --git a/Dockerfile b/Dockerfile index e35861c..61e23fb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,4 +51,6 @@ COPY . . EXPOSE 8001 ENV PYTHONIOENCODING=utf-8 -CMD ["uvicorn", "src.api.twin_ws:app", "--host", "0.0.0.0", "--port", "8001"] +ENV PYTHONPATH=/app/output/builds + +CMD ["uvicorn", "src.api.twin_ws:app", "--app-dir", "/app/output/builds", "--host", "0.0.0.0", "--port", "8001"] diff --git a/agents/tier4_deploy/docker_builder.py b/agents/tier4_deploy/docker_builder.py index 47c39c4..958d605 100644 --- a/agents/tier4_deploy/docker_builder.py +++ b/agents/tier4_deploy/docker_builder.py @@ -48,10 +48,8 @@ def generate_dockerfile(task): -shared -o libsentinel_gpu.so \\ -Xcompiler -fPIC -# Compile C++ drivers +# Compile the available C++ processing modules RUN g++ -O3 -std=c++17 \\ - src/camera_driver.cpp \\ - src/mount_control.cpp \\ src/plate_solver.cpp \\ src/photometry.cpp \\ -shared -o libsentinel_hw.so \\ @@ -179,7 +177,6 @@ def generate_compose(task): - WATCH_CONTAINER=sentinelforge-edge - ALERT_ENDPOINT=${{ALERT_ENDPOINT}} volumes: - - /var/run/docker.sock:/var/run/docker.sock:ro - ./logs:/logs updater: @@ -191,8 +188,6 @@ def generate_compose(task): - IMAGE={DEPLOY_SPEC["image_name"]} - CHECK_INTERVAL=3600 - AUTO_ROLLBACK=true - volumes: - - /var/run/docker.sock:/var/run/docker.sock ''' filepath.write_text(code, encoding="utf-8") diff --git a/api/config.js b/api/config.js new file mode 100644 index 0000000..d53dfe4 --- /dev/null +++ b/api/config.js @@ -0,0 +1,33 @@ +// Vercel Serverless Function — Public Configuration Provider +export default async function handler(req, res) { + const origin = req.headers.origin; + const allowedOrigins = (process.env.ALLOWED_ORIGINS || 'https://sentinelforge.vercel.app') + .split(',') + .map(s => s.trim()) + .filter(Boolean); + + if (origin) { + if (allowedOrigins.includes(origin)) { + res.setHeader('Access-Control-Allow-Origin', origin); + res.setHeader('Vary', 'Origin'); + } else { + return res.status(403).json({ error: 'Origin not allowed' }); + } + } + + res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS'); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); + res.setHeader('Cache-Control', 'no-store'); + + if (req.method === 'OPTIONS') { + return res.status(204).end(); + } + + if (req.method !== 'GET') { + return res.status(405).json({ error: 'GET only' }); + } + + return res.status(200).json({ + cesiumIonToken: process.env.CESIUM_ION_TOKEN || '' + }); +} diff --git a/api/nlp.js b/api/nlp.js index 11e4221..5cce40d 100644 --- a/api/nlp.js +++ b/api/nlp.js @@ -1,17 +1,106 @@ // Vercel Serverless Function — Gemini NLU for SentinelForge Terminal +import { timingSafeEqual } from 'node:crypto'; + +// Best-effort in-memory per-IP rate limit (20 req/min) +const ipHits = new Map(); + +function checkRateLimit(ip) { + const now = Date.now(); + const windowMs = 60 * 1000; + const maxReqs = 20; + const hits = (ipHits.get(ip) || []).filter(t => now - t < windowMs); + + if (hits.length >= maxReqs) { + return false; + } + hits.push(now); + ipHits.set(ip, hits); + + if (ipHits.size > 2000) { + for (const [k, v] of ipHits.entries()) { + if (v.length === 0 || now - v[v.length - 1] > windowMs) { + ipHits.delete(k); + } + } + } + return true; +} + +function validApiKey(provided, expected) { + if (typeof provided !== 'string' || typeof expected !== 'string') { + return false; + } + const providedBytes = Buffer.from(provided, 'utf8'); + const expectedBytes = Buffer.from(expected, 'utf8'); + return providedBytes.length === expectedBytes.length + && timingSafeEqual(providedBytes, expectedBytes); +} + +const ALLOWED_COMMANDS = new Set([ + 'ls', 'cat', 'cd', 'pwd', 'tree', 'find', 'head', 'grep', 'wc', + 'python', 'pytest', 'git', 'make', 'nano', 'ssh', 'slingshot-pipeline', + 'jetson_health', 'df', 'uptime', 'whoami', 'uname', 'date', 'echo', + 'clear', 'help', '_reply' +]); + export default async function handler(req, res) { - res.setHeader('Access-Control-Allow-Origin', '*'); + const origin = req.headers.origin; + const allowedOrigins = (process.env.ALLOWED_ORIGINS || 'https://sentinelforge.vercel.app') + .split(',') + .map(s => s.trim()) + .filter(Boolean); + + if (origin) { + if (allowedOrigins.includes(origin)) { + res.setHeader('Access-Control-Allow-Origin', origin); + res.setHeader('Vary', 'Origin'); + } else { + return res.status(403).json({ error: 'Origin not allowed' }); + } + } + res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS'); - res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); - if (req.method === 'OPTIONS') return res.status(200).end(); - if (req.method !== 'POST') return res.status(405).json({ error: 'POST only' }); + res.setHeader('Access-Control-Allow-Headers', 'Content-Type, X-API-Key'); - const { input } = req.body || {}; - if (!input || typeof input !== 'string' || input.length > 500) + if (req.method === 'OPTIONS') { + return res.status(204).end(); + } + + if (req.method !== 'POST') { + return res.status(405).json({ error: 'POST only' }); + } + + // Authentication is mandatory and fail-closed when the server is not configured. + const expectedApiKey = process.env.NLP_API_KEY; + if (!expectedApiKey) { + return res.status(503).json({ error: 'Service authentication is not configured' }); + } + if (!validApiKey(req.headers['x-api-key'], expectedApiKey)) { + return res.status(401).json({ error: 'Unauthorized' }); + } + + // Best-effort per-IP rate limiting. Serverless instances do not share memory, + // so this bounds bursts per warm instance rather than providing a global quota. + const ip = req.headers['x-forwarded-for']?.split(',')[0]?.trim() || req.socket?.remoteAddress || 'unknown'; + if (!checkRateLimit(ip)) { + return res.status(429).json({ error: 'Too many requests. Please wait a minute.' }); + } + + // Strict input validation + let input = req.body?.input; + if (!input || typeof input !== 'string') { return res.status(400).json({ error: 'Invalid input' }); + } + // Strip control characters (retain printable characters and whitespace) + input = input.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '').trim(); + if (input.length < 1 || input.length > 500) { + return res.status(400).json({ error: 'Invalid input length' }); + } const apiKey = process.env.GEMINI_API_KEY; - if (!apiKey) return res.status(500).json({ error: 'API key not configured' }); + if (!apiKey) { + return res.status(500).json({ error: 'API key not configured' }); + } const systemPrompt = `You are a command interpreter for the SentinelForge space surveillance terminal. The user types natural language. You must respond with ONLY a JSON object — no markdown, no explanation. @@ -64,9 +153,10 @@ Examples: "how's the gpu?" → {"cmd":"jetson_health","args":[],"narr":"Running GPU diagnostics..."} "hello" → {"cmd":"_reply","text":"Hello! I'm the SentinelForge terminal. Ask me anything about the codebase, or tell me what to run."}`; + // Strict fixed model allow-list (cannot be used as an open proxy) const models = ['gemini-2.5-flash', 'gemini-2.5-flash-lite', 'gemini-2.0-flash-lite']; const reqBody = JSON.stringify({ - contents: [{ parts: [{ text: `${systemPrompt}\n\nUser input: "${input}"` }] }], + contents: [{ parts: [{ text: `${systemPrompt}\n\nUser input: ${JSON.stringify(input)}` }] }], generationConfig: { temperature: 0.1, maxOutputTokens: 256, @@ -76,19 +166,25 @@ Examples: for (const model of models) { try { + // Pass API key via header, NEVER in URL query params const response = await fetch( - `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`, - { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: reqBody } + `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'x-goog-api-key': apiKey + }, + body: reqBody + } ); if (response.status === 503 || response.status === 429) { - console.log(`${model}: ${response.status}, trying next model...`); continue; // Try next model } if (!response.ok) { - const errText = await response.text(); - console.error(`${model} error:`, errText); + // Do not leak upstream error body to client continue; } @@ -96,13 +192,28 @@ Examples: const text = data?.candidates?.[0]?.content?.parts?.[0]?.text; if (!text) continue; - const parsed = JSON.parse(text); + let parsed; + try { + parsed = JSON.parse(text); + } catch { + continue; + } + + if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) { + return res.status(200).json({ cmd: '_reply', text: 'Command not recognized.' }); + } + + // Validate command is from allowed set + if (typeof parsed.cmd !== 'string' || !ALLOWED_COMMANDS.has(parsed.cmd)) { + return res.status(200).json({ cmd: '_reply', text: 'Command not recognized.' }); + } + return res.status(200).json(parsed); - } catch (err) { - console.error(`${model} failed:`, err.message); + } catch { continue; } } + // Generic failure response without leaking internal errors return res.status(502).json({ error: 'All models unavailable. Try again in a moment.' }); } diff --git a/db.py b/db.py index 4434c82..c7a67c7 100644 --- a/db.py +++ b/db.py @@ -5,6 +5,7 @@ """ import sqlite3 import json +from contextlib import contextmanager from datetime import datetime, timezone from pathlib import Path from config import DB_PATH, TASK_STATES, PRIORITIES @@ -18,10 +19,25 @@ def get_conn(): conn.row_factory = sqlite3.Row return conn + +@contextmanager +def get_db(): + """Yield a connection that always commits or rolls back and closes.""" + conn = get_conn() + try: + yield conn + conn.commit() + except Exception: + conn.rollback() + raise + finally: + conn.close() + + def init_db(): """Create all tables if they don't exist.""" - conn = get_conn() - conn.executescript(""" + with get_db() as conn: + conn.executescript(""" -- Sprints: 2-week planning cycles CREATE TABLE IF NOT EXISTS sprints ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -112,204 +128,187 @@ def init_db(): CREATE INDEX IF NOT EXISTS idx_log_task ON agent_log(task_id); CREATE INDEX IF NOT EXISTS idx_benchmarks_metric ON benchmarks(metric); CREATE INDEX IF NOT EXISTS idx_deployments_site ON deployments(site_id); - """) - conn.commit() - conn.close() + """) print(f"[DB] Initialized at {DB_PATH}") # ─── Task Operations ──────────────────────────────────────────── def create_task(sprint_id, title, description, agent_id, priority="P2_MEDIUM", deliverables=None, depends_on=None, estimated_hours=None): - conn = get_conn() - conn.execute(""" - INSERT INTO tasks (sprint_id, title, description, agent_id, priority, - deliverables, depends_on, estimated_hours) - VALUES (?, ?, ?, ?, ?, ?, ?, ?) - """, (sprint_id, title, description, agent_id, priority, - json.dumps(deliverables or []), - json.dumps(depends_on or []), - estimated_hours)) - conn.commit() - task_id = conn.execute("SELECT last_insert_rowid()").fetchone()[0] - conn.close() + with get_db() as conn: + conn.execute(""" + INSERT INTO tasks (sprint_id, title, description, agent_id, priority, + deliverables, depends_on, estimated_hours) + VALUES (?, ?, ?, ?, ?, ?, ?, ?) + """, (sprint_id, title, description, agent_id, priority, + json.dumps(deliverables or []), + json.dumps(depends_on or []), + estimated_hours)) + task_id = conn.execute("SELECT last_insert_rowid()").fetchone()[0] return task_id def update_task_state(task_id, new_state): - conn = get_conn() - updates = {"state": new_state} - if new_state == "IN_PROGRESS": - updates["started_at"] = datetime.now(timezone.utc).isoformat() - elif new_state == "DONE": - updates["completed_at"] = datetime.now(timezone.utc).isoformat() - - set_clause = ", ".join(f"{k} = ?" for k in updates) - conn.execute(f"UPDATE tasks SET {set_clause} WHERE id = ?", - list(updates.values()) + [task_id]) - conn.commit() - conn.close() + with get_db() as conn: + if new_state == "IN_PROGRESS": + conn.execute( + "UPDATE tasks SET state = ?, started_at = ? WHERE id = ?", + (new_state, datetime.now(timezone.utc).isoformat(), task_id), + ) + elif new_state == "DONE": + conn.execute( + "UPDATE tasks SET state = ?, completed_at = ? WHERE id = ?", + (new_state, datetime.now(timezone.utc).isoformat(), task_id), + ) + else: + conn.execute( + "UPDATE tasks SET state = ? WHERE id = ?", + (new_state, task_id), + ) def get_tasks_for_agent(agent_id, state=None): - conn = get_conn() - if state: - rows = conn.execute( - "SELECT * FROM tasks WHERE agent_id = ? AND state = ? ORDER BY priority", - (agent_id, state) - ).fetchall() - else: - rows = conn.execute( - "SELECT * FROM tasks WHERE agent_id = ? ORDER BY priority, state", - (agent_id,) - ).fetchall() - conn.close() + with get_db() as conn: + if state: + rows = conn.execute( + "SELECT * FROM tasks WHERE agent_id = ? AND state = ? ORDER BY priority", + (agent_id, state) + ).fetchall() + else: + rows = conn.execute( + "SELECT * FROM tasks WHERE agent_id = ? ORDER BY priority, state", + (agent_id,) + ).fetchall() return [dict(r) for r in rows] def get_blocked_tasks(): """Find tasks whose dependencies aren't DONE yet.""" - conn = get_conn() - tasks = conn.execute( - "SELECT * FROM tasks WHERE state != 'DONE' AND depends_on != '[]'" - ).fetchall() - result = [] - for t in tasks: - deps = json.loads(t["depends_on"]) - for dep_id in deps: - dep = conn.execute("SELECT state FROM tasks WHERE id = ?", (dep_id,)).fetchone() - if dep and dep["state"] != "DONE": - result.append(dict(t)) - break - conn.close() + with get_db() as conn: + tasks = conn.execute( + "SELECT * FROM tasks WHERE state != 'DONE' AND depends_on != '[]'" + ).fetchall() + result = [] + for t in tasks: + deps = json.loads(t["depends_on"]) + for dep_id in deps: + dep = conn.execute("SELECT state FROM tasks WHERE id = ?", (dep_id,)).fetchone() + if dep and dep["state"] != "DONE": + result.append(dict(t)) + break return result # ─── Agent Log Operations ─────────────────────────────────────── def log_action(agent_id, action, task_id=None, details=None, files_modified=None, duration_sec=None, status="SUCCESS"): - conn = get_conn() - conn.execute(""" - INSERT INTO agent_log (agent_id, task_id, action, details, - files_modified, duration_sec, status) - VALUES (?, ?, ?, ?, ?, ?, ?) - """, (agent_id, task_id, action, json.dumps(details) if details else None, - json.dumps(files_modified) if files_modified else None, - duration_sec, status)) - conn.commit() - conn.close() + with get_db() as conn: + conn.execute(""" + INSERT INTO agent_log (agent_id, task_id, action, details, + files_modified, duration_sec, status) + VALUES (?, ?, ?, ?, ?, ?, ?) + """, (agent_id, task_id, action, json.dumps(details) if details else None, + json.dumps(files_modified) if files_modified else None, + duration_sec, status)) # ─── Artifact Operations ──────────────────────────────────────── def register_artifact(task_id, agent_id, file_path, language, lines_of_code, version): - conn = get_conn() - conn.execute(""" - INSERT OR REPLACE INTO artifacts (task_id, agent_id, file_path, language, - lines_of_code, version, updated_at) - VALUES (?, ?, ?, ?, ?, ?, datetime('now')) - """, (task_id, agent_id, file_path, language, lines_of_code, version)) - conn.commit() - conn.close() + with get_db() as conn: + conn.execute(""" + INSERT OR REPLACE INTO artifacts (task_id, agent_id, file_path, language, + lines_of_code, version, updated_at) + VALUES (?, ?, ?, ?, ?, ?, datetime('now')) + """, (task_id, agent_id, file_path, language, lines_of_code, version)) # ─── Benchmark Operations ─────────────────────────────────────── def record_benchmark(build_version, metric, value, unit=None, site_id=None, passed=True): - conn = get_conn() - conn.execute(""" - INSERT INTO benchmarks (build_version, metric, value, unit, site_id, passed) - VALUES (?, ?, ?, ?, ?, ?) - """, (build_version, metric, value, unit, site_id, 1 if passed else 0)) - conn.commit() - conn.close() + with get_db() as conn: + conn.execute(""" + INSERT INTO benchmarks (build_version, metric, value, unit, site_id, passed) + VALUES (?, ?, ?, ?, ?, ?) + """, (build_version, metric, value, unit, site_id, 1 if passed else 0)) def get_latest_benchmarks(build_version=None): - conn = get_conn() - if build_version: - rows = conn.execute( - "SELECT * FROM benchmarks WHERE build_version = ? ORDER BY measured_at DESC", - (build_version,) - ).fetchall() - else: - rows = conn.execute(""" - SELECT * FROM benchmarks WHERE id IN ( - SELECT MAX(id) FROM benchmarks GROUP BY metric - ) ORDER BY metric - """).fetchall() - conn.close() + with get_db() as conn: + if build_version: + rows = conn.execute( + "SELECT * FROM benchmarks WHERE build_version = ? ORDER BY measured_at DESC", + (build_version,) + ).fetchall() + else: + rows = conn.execute(""" + SELECT * FROM benchmarks WHERE id IN ( + SELECT MAX(id) FROM benchmarks GROUP BY metric + ) ORDER BY metric + """).fetchall() return [dict(r) for r in rows] # ─── Deployment Operations ────────────────────────────────────── def record_deployment(site_id, image_version): - conn = get_conn() - conn.execute(""" - INSERT INTO deployments (site_id, image_version) VALUES (?, ?) - """, (site_id, image_version)) - conn.commit() - conn.close() + with get_db() as conn: + conn.execute(""" + INSERT INTO deployments (site_id, image_version) VALUES (?, ?) + """, (site_id, image_version)) def get_fleet_status(): """Current deployment version at each site.""" - conn = get_conn() - rows = conn.execute(""" - SELECT d.* FROM deployments d - INNER JOIN ( - SELECT site_id, MAX(id) as max_id FROM deployments GROUP BY site_id - ) latest ON d.id = latest.max_id - ORDER BY d.site_id - """).fetchall() - conn.close() + with get_db() as conn: + rows = conn.execute(""" + SELECT d.* FROM deployments d + INNER JOIN ( + SELECT site_id, MAX(id) as max_id FROM deployments GROUP BY site_id + ) latest ON d.id = latest.max_id + ORDER BY d.site_id + """).fetchall() return [dict(r) for r in rows] # ─── Sprint Operations ────────────────────────────────────────── def create_sprint(name, goal, start_date, end_date): - conn = get_conn() - conn.execute(""" - INSERT INTO sprints (name, goal, start_date, end_date) VALUES (?, ?, ?, ?) - """, (name, goal, start_date, end_date)) - conn.commit() - sprint_id = conn.execute("SELECT last_insert_rowid()").fetchone()[0] - conn.close() + with get_db() as conn: + conn.execute(""" + INSERT INTO sprints (name, goal, start_date, end_date) VALUES (?, ?, ?, ?) + """, (name, goal, start_date, end_date)) + sprint_id = conn.execute("SELECT last_insert_rowid()").fetchone()[0] return sprint_id def get_sprint_summary(sprint_id): - conn = get_conn() - sprint = dict(conn.execute("SELECT * FROM sprints WHERE id = ?", (sprint_id,)).fetchone()) - tasks = conn.execute("SELECT state, COUNT(*) as cnt FROM tasks WHERE sprint_id = ? GROUP BY state", - (sprint_id,)).fetchall() - sprint["task_counts"] = {r["state"]: r["cnt"] for r in tasks} - conn.close() + with get_db() as conn: + sprint = dict(conn.execute("SELECT * FROM sprints WHERE id = ?", (sprint_id,)).fetchone()) + tasks = conn.execute("SELECT state, COUNT(*) as cnt FROM tasks WHERE sprint_id = ? GROUP BY state", + (sprint_id,)).fetchall() + sprint["task_counts"] = {r["state"]: r["cnt"] for r in tasks} return sprint # ─── Reporting ────────────────────────────────────────────────── def get_agent_stats(): """Summary of each agent's activity.""" - conn = get_conn() - rows = conn.execute(""" - SELECT agent_id, - COUNT(*) as total_actions, - SUM(CASE WHEN status='SUCCESS' THEN 1 ELSE 0 END) as successes, - SUM(CASE WHEN status='FAILED' THEN 1 ELSE 0 END) as failures, - ROUND(SUM(duration_sec), 1) as total_seconds - FROM agent_log - GROUP BY agent_id - ORDER BY agent_id - """).fetchall() - conn.close() + with get_db() as conn: + rows = conn.execute(""" + SELECT agent_id, + COUNT(*) as total_actions, + SUM(CASE WHEN status='SUCCESS' THEN 1 ELSE 0 END) as successes, + SUM(CASE WHEN status='FAILED' THEN 1 ELSE 0 END) as failures, + ROUND(SUM(duration_sec), 1) as total_seconds + FROM agent_log + GROUP BY agent_id + ORDER BY agent_id + """).fetchall() return [dict(r) for r in rows] def dashboard(): """Print full system status.""" - conn = get_conn() - task_states = conn.execute( - "SELECT state, COUNT(*) as cnt FROM tasks GROUP BY state" - ).fetchall() + with get_db() as conn: + task_states = conn.execute( + "SELECT state, COUNT(*) as cnt FROM tasks GROUP BY state" + ).fetchall() total_tasks = sum(r["cnt"] for r in task_states) done_tasks = next((r["cnt"] for r in task_states if r["state"] == "DONE"), 0) agent_stats = get_agent_stats() fleet = get_fleet_status() benchmarks = get_latest_benchmarks() - conn.close() print("\n" + "="*60) print(" SENTINELFORGE DASHBOARD") diff --git a/frontend/digital_twin.html b/frontend/digital_twin.html index 33740ce..f081197 100644 --- a/frontend/digital_twin.html +++ b/frontend/digital_twin.html @@ -66,28 +66,7 @@