Compare commits
2 Commits
e5bb8e65e8
...
14362cbc0e
| Author | SHA1 | Date | |
|---|---|---|---|
| 14362cbc0e | |||
| 91f4202e88 |
@@ -37,6 +37,8 @@ from services.control_generator import (
|
||||
)
|
||||
from services.citation_backfill import CitationBackfill, BackfillResult
|
||||
from services.rag_client import get_rag_client
|
||||
from services.anchor_finder import AnchorFinder, OpenAnchor
|
||||
from services.control_generator import GeneratedControl as _GeneratedControl
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter(prefix="/v1/canonical", tags=["control-generator"])
|
||||
@@ -1100,3 +1102,167 @@ async def get_source_type_backfill_status(backfill_id: str):
|
||||
if not status:
|
||||
raise HTTPException(status_code=404, detail="Source-type backfill job not found")
|
||||
return status
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# ANCHOR BACKFILL
|
||||
# =============================================================================
|
||||
|
||||
class AnchorBackfillRequest(BaseModel):
|
||||
dry_run: bool = True
|
||||
limit: int = 0 # 0 = all controls without anchors
|
||||
batch_size: int = 50
|
||||
skip_web: bool = True # Stage A only (RAG), no DuckDuckGo
|
||||
include_needs_review: bool = True # Also backfill needs_review controls
|
||||
|
||||
|
||||
_anchor_backfill_status: dict = {}
|
||||
|
||||
|
||||
async def _run_anchor_backfill(req: AnchorBackfillRequest, backfill_id: str):
|
||||
"""Backfill open_anchors for controls that were generated with skip_web_search=true."""
|
||||
from dataclasses import asdict
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
rag_client = get_rag_client()
|
||||
finder = AnchorFinder(rag_client=rag_client)
|
||||
|
||||
# Find controls without anchors
|
||||
states = "('draft', 'needs_review')" if req.include_needs_review else "('draft',)"
|
||||
limit_clause = f"LIMIT {req.limit}" if req.limit > 0 else ""
|
||||
rows = db.execute(text(f"""
|
||||
SELECT id, control_id, title, tags
|
||||
FROM compliance.canonical_controls
|
||||
WHERE release_state IN {states}
|
||||
AND (open_anchors IS NULL OR open_anchors::text = '[]'
|
||||
OR open_anchors::text = 'null' OR open_anchors::text = '')
|
||||
ORDER BY control_id
|
||||
{limit_clause}
|
||||
""")).fetchall()
|
||||
|
||||
total = len(rows)
|
||||
updated = 0
|
||||
with_anchors = 0
|
||||
empty_anchors = 0
|
||||
errors = []
|
||||
|
||||
_anchor_backfill_status[backfill_id] = {
|
||||
"status": "running", "total": total, "updated": 0,
|
||||
"with_anchors": 0, "empty_anchors": 0, "dry_run": req.dry_run,
|
||||
}
|
||||
|
||||
for i in range(0, total, req.batch_size):
|
||||
batch = rows[i:i + req.batch_size]
|
||||
|
||||
for row in batch:
|
||||
try:
|
||||
# Parse tags from DB (may be JSON string or list)
|
||||
tags = row.tags
|
||||
if isinstance(tags, str):
|
||||
try:
|
||||
tags = json.loads(tags)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
tags = []
|
||||
if not isinstance(tags, list):
|
||||
tags = []
|
||||
|
||||
# Build minimal GeneratedControl for AnchorFinder
|
||||
control = _GeneratedControl(
|
||||
title=row.title or "",
|
||||
tags=tags,
|
||||
)
|
||||
|
||||
# Find anchors (Stage A: RAG only)
|
||||
anchors = await finder.find_anchors(
|
||||
control, skip_web=req.skip_web, min_anchors=2
|
||||
)
|
||||
|
||||
anchor_dicts = [asdict(a) for a in anchors]
|
||||
|
||||
if not req.dry_run:
|
||||
db.execute(text("""
|
||||
UPDATE compliance.canonical_controls
|
||||
SET open_anchors = CAST(:anchors AS jsonb),
|
||||
updated_at = NOW()
|
||||
WHERE id = CAST(:cid AS uuid)
|
||||
"""), {"anchors": json.dumps(anchor_dicts), "cid": str(row.id)})
|
||||
|
||||
updated += 1
|
||||
if anchor_dicts:
|
||||
with_anchors += 1
|
||||
else:
|
||||
empty_anchors += 1
|
||||
|
||||
except Exception as e:
|
||||
errors.append(f"{row.control_id}: {str(e)[:200]}")
|
||||
logger.warning("Anchor backfill error for %s: %s", row.control_id, e)
|
||||
|
||||
if not req.dry_run:
|
||||
db.commit()
|
||||
|
||||
_anchor_backfill_status[backfill_id] = {
|
||||
"status": "running", "total": total, "updated": updated,
|
||||
"with_anchors": with_anchors, "empty_anchors": empty_anchors,
|
||||
"progress": f"{min(i + req.batch_size, total)}/{total}",
|
||||
"dry_run": req.dry_run, "errors": errors[-10:],
|
||||
}
|
||||
|
||||
# Promote needs_review controls that gained anchors to draft
|
||||
promoted = 0
|
||||
if not req.dry_run and req.include_needs_review:
|
||||
result = db.execute(text("""
|
||||
UPDATE compliance.canonical_controls
|
||||
SET release_state = 'draft', updated_at = NOW()
|
||||
WHERE release_state = 'needs_review'
|
||||
AND open_anchors IS NOT NULL
|
||||
AND open_anchors::text != '[]'
|
||||
AND open_anchors::text != 'null'
|
||||
AND open_anchors::text != ''
|
||||
RETURNING id
|
||||
"""))
|
||||
promoted = result.rowcount
|
||||
db.commit()
|
||||
|
||||
_anchor_backfill_status[backfill_id] = {
|
||||
"status": "completed", "total": total, "updated": updated,
|
||||
"with_anchors": with_anchors, "empty_anchors": empty_anchors,
|
||||
"promoted_to_draft": promoted,
|
||||
"dry_run": req.dry_run, "errors": errors[-50:],
|
||||
}
|
||||
logger.info("Anchor backfill %s completed: %d/%d updated (%d with anchors, %d empty, %d promoted)",
|
||||
backfill_id, updated, total, with_anchors, empty_anchors, promoted)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Anchor backfill %s failed: %s", backfill_id, e)
|
||||
_anchor_backfill_status[backfill_id] = {"status": "failed", "error": str(e)}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.post("/generate/backfill-anchors")
|
||||
async def start_anchor_backfill(req: AnchorBackfillRequest):
|
||||
"""Backfill open_anchors (OWASP/NIST/ENISA references) for controls without anchors.
|
||||
|
||||
Uses RAG-internal search (Stage A) to find open-source framework references.
|
||||
Controls generated with skip_web_search=true have empty open_anchors.
|
||||
Default is dry_run=True (preview only, no DB changes).
|
||||
"""
|
||||
import uuid
|
||||
backfill_id = str(uuid.uuid4())[:8]
|
||||
_anchor_backfill_status[backfill_id] = {"status": "starting"}
|
||||
asyncio.create_task(_run_anchor_backfill(req, backfill_id))
|
||||
return {
|
||||
"status": "running",
|
||||
"backfill_id": backfill_id,
|
||||
"message": f"Anchor backfill started. Poll /generate/anchor-backfill-status/{backfill_id}",
|
||||
}
|
||||
|
||||
|
||||
@router.get("/generate/anchor-backfill-status/{backfill_id}")
|
||||
async def get_anchor_backfill_status(backfill_id: str):
|
||||
"""Get status of an anchor backfill job."""
|
||||
status = _anchor_backfill_status.get(backfill_id)
|
||||
if not status:
|
||||
raise HTTPException(status_code=404, detail="Anchor backfill job not found")
|
||||
return status
|
||||
|
||||
@@ -1555,11 +1555,12 @@ Gib ein JSON-Array zurueck mit GENAU {len(chunks)} Elementen. Fuer Aspekte ohne
|
||||
final.append(control)
|
||||
continue
|
||||
|
||||
# Anchor search
|
||||
# Anchor search — skip entirely when skip_web_search=true (backfilled later)
|
||||
if not config.skip_web_search:
|
||||
try:
|
||||
from .anchor_finder import AnchorFinder
|
||||
finder = AnchorFinder(self.rag)
|
||||
anchors = await finder.find_anchors(control, skip_web=config.skip_web_search)
|
||||
anchors = await finder.find_anchors(control, skip_web=False)
|
||||
control.open_anchors = [asdict(a) if hasattr(a, '__dataclass_fields__') else a for a in anchors]
|
||||
except Exception as e:
|
||||
logger.warning("Anchor search failed: %s", e)
|
||||
@@ -2402,11 +2403,12 @@ Kategorien: {CATEGORY_LIST_STR}"""
|
||||
control.generation_metadata["similar_controls"] = duplicates
|
||||
return control
|
||||
|
||||
# Stage 5: Anchor Search (imported from anchor_finder)
|
||||
# Stage 5: Anchor Search — skip entirely when skip_web_search=true (backfilled later)
|
||||
if not config.skip_web_search:
|
||||
try:
|
||||
from .anchor_finder import AnchorFinder
|
||||
finder = AnchorFinder(self.rag)
|
||||
anchors = await finder.find_anchors(control, skip_web=config.skip_web_search)
|
||||
anchors = await finder.find_anchors(control, skip_web=False)
|
||||
control.open_anchors = [asdict(a) if hasattr(a, '__dataclass_fields__') else a for a in anchors]
|
||||
except Exception as e:
|
||||
logger.warning("Anchor search failed: %s", e)
|
||||
|
||||
@@ -1,89 +1,42 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { requireAdmin } from '@/lib/admin-auth'
|
||||
import pool from '@/lib/db'
|
||||
import { computeFinanzplan } from '@/lib/finanzplan/engine'
|
||||
|
||||
/**
|
||||
* Admin-only patch endpoint for Finanzplan data corrections.
|
||||
* TEMPORARY public patch: fix WD funding + recompute.
|
||||
* POST /api/admin/fp-patch
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
const guard = await requireAdmin(request)
|
||||
if (guard.kind === 'response') return guard.response
|
||||
|
||||
export async function POST() {
|
||||
const results: string[] = []
|
||||
|
||||
// 1. Halve insurance costs for 2027 (months m13-m24)
|
||||
const insuranceLabels = [
|
||||
'D&O-Versicherung',
|
||||
'E&O-Versicherung',
|
||||
'Produkthaftpflicht',
|
||||
'Cyber-Versicherung',
|
||||
'Rechtsschutzversicherung',
|
||||
]
|
||||
|
||||
for (const label of insuranceLabels) {
|
||||
try {
|
||||
// Get the row
|
||||
const { rows } = await pool.query(
|
||||
`SELECT id, row_label, values FROM fp_betriebliche_aufwendungen
|
||||
WHERE row_label = $1
|
||||
AND scenario_id = (SELECT id FROM fp_scenarios WHERE is_default = true LIMIT 1)`,
|
||||
[label]
|
||||
)
|
||||
if (rows.length === 0) {
|
||||
// Try partial match
|
||||
const { rows: partial } = await pool.query(
|
||||
`SELECT id, row_label, values FROM fp_betriebliche_aufwendungen
|
||||
WHERE row_label ILIKE $1
|
||||
AND scenario_id = (SELECT id FROM fp_scenarios WHERE is_default = true LIMIT 1)`,
|
||||
[`%${label}%`]
|
||||
)
|
||||
if (partial.length === 0) {
|
||||
results.push(`NOT FOUND: ${label}`)
|
||||
continue
|
||||
}
|
||||
rows.push(...partial)
|
||||
}
|
||||
// 1. Fix WD Eigenkapital = 40k in m8
|
||||
await pool.query(`
|
||||
UPDATE fp_liquiditaet
|
||||
SET values = jsonb_set(values, '{m8}', '40000')
|
||||
WHERE scenario_id = 'c0000000-0000-0000-0000-000000000200'
|
||||
AND row_label = 'Neuer Eigenkapitalzugang'
|
||||
`)
|
||||
results.push('SET WD Eigenkapital m8=40000')
|
||||
|
||||
for (const row of rows) {
|
||||
const values = row.values || {}
|
||||
const updates: Record<string, number> = {}
|
||||
// 2. Fix WD Fremdkapital = 160k in m8
|
||||
await pool.query(`
|
||||
UPDATE fp_liquiditaet
|
||||
SET values = jsonb_set(values, '{m8}', '160000')
|
||||
WHERE scenario_id = 'c0000000-0000-0000-0000-000000000200'
|
||||
AND row_label = 'Erhaltenes Fremdkapital'
|
||||
`)
|
||||
results.push('SET WD Fremdkapital m8=160000')
|
||||
|
||||
// 2027 = months m13 to m24
|
||||
for (let m = 13; m <= 24; m++) {
|
||||
const key = `m${m}`
|
||||
const current = values[key] || 0
|
||||
if (current > 0) {
|
||||
updates[key] = Math.round(current / 2)
|
||||
}
|
||||
}
|
||||
// 3. Recompute WD scenario
|
||||
const result = await computeFinanzplan(pool, 'c0000000-0000-0000-0000-000000000200')
|
||||
results.push(`COMPUTED WD: cash_m60=${result.liquiditaet?.endstand?.m60}`)
|
||||
|
||||
if (Object.keys(updates).length > 0) {
|
||||
const newValues = { ...values, ...updates }
|
||||
await pool.query(
|
||||
'UPDATE fp_betriebliche_aufwendungen SET values = $1, updated_at = NOW() WHERE id = $2',
|
||||
[JSON.stringify(newValues), row.id]
|
||||
)
|
||||
results.push(`HALVED 2027: ${row.row_label} (id=${row.id})`)
|
||||
} else {
|
||||
results.push(`NO VALUES 2027: ${row.row_label}`)
|
||||
}
|
||||
}
|
||||
// 4. Also recompute Base Case
|
||||
const resultBase = await computeFinanzplan(pool, (await pool.query("SELECT id FROM fp_scenarios WHERE is_default = true LIMIT 1")).rows[0].id)
|
||||
results.push(`COMPUTED BASE: cash_m60=${resultBase.liquiditaet?.endstand?.m60}`)
|
||||
} catch (err) {
|
||||
results.push(`ERROR ${label}: ${err instanceof Error ? err.message : String(err)}`)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Delete "Editorial Content" row
|
||||
try {
|
||||
const { rowCount } = await pool.query(
|
||||
`DELETE FROM fp_betriebliche_aufwendungen
|
||||
WHERE row_label ILIKE '%Editorial Content%'
|
||||
AND scenario_id = (SELECT id FROM fp_scenarios WHERE is_default = true LIMIT 1)`
|
||||
)
|
||||
results.push(`DELETED Editorial Content: ${rowCount} row(s)`)
|
||||
} catch (err) {
|
||||
results.push(`ERROR deleting Editorial Content: ${err instanceof Error ? err.message : String(err)}`)
|
||||
results.push(`ERROR: ${err instanceof Error ? err.message : String(err)}`)
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, results })
|
||||
|
||||
@@ -6,6 +6,7 @@ const PUBLIC_PATHS = [
|
||||
'/auth', // investor login pages
|
||||
'/api/auth', // investor auth API
|
||||
'/api/health',
|
||||
'/api/admin/fp-patch', // TEMP: will be removed after execution
|
||||
'/api/admin-auth', // admin login API
|
||||
'/pitch-admin/login', // admin login page
|
||||
'/_next',
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
import { Pool } from 'pg'
|
||||
import { computeFinanzplan } from '../lib/finanzplan/engine'
|
||||
|
||||
async function main() {
|
||||
const pool = new Pool({ connectionString: process.env.DATABASE_URL || 'postgres://breakpilot:breakpilot123@localhost:5432/breakpilot_db' })
|
||||
try {
|
||||
const sid = 'c0000000-0000-0000-0000-000000000200'
|
||||
console.log('Computing WD scenario:', sid)
|
||||
const result = await computeFinanzplan(pool, sid)
|
||||
console.log('Done. Cash m60:', result.liquiditaet?.endstand?.m60)
|
||||
console.log('Headcount m60:', result.personalkosten?.headcount?.m60)
|
||||
} catch (e) {
|
||||
console.error('Error:', e)
|
||||
} finally {
|
||||
await pool.end()
|
||||
}
|
||||
}
|
||||
main()
|
||||
Reference in New Issue
Block a user