Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3389fa3e7a | |||
| 79abf23ea8 | |||
| d5925e57af | |||
| 1877829b1d | |||
| 866889b453 | |||
| 9760dca443 | |||
| e5e7b825af | |||
| 4818fc51c2 |
@@ -0,0 +1,73 @@
|
|||||||
|
package iace
|
||||||
|
|
||||||
|
// P3: pin accepted proposer decisions into the GT gate.
|
||||||
|
//
|
||||||
|
// When a human accepts a proposal from the offline proposer (a dedup
|
||||||
|
// supersession, a foreign-framing gate, a vocab→tag mapping, a coverage hazard),
|
||||||
|
// they record an AcceptedPin. A pin is a tiny, machine-scoped invariant — "this
|
||||||
|
// pattern MUST (or must NOT) fire for this machine" — that a test re-checks on
|
||||||
|
// every run. This is what makes the library's growth COMPOUND into the gate
|
||||||
|
// instead of silently eroding it: a future change that re-introduces a dropped
|
||||||
|
// duplicate, un-gates a foreign pattern, or removes a coverage hazard breaks the
|
||||||
|
// pin and fails CI.
|
||||||
|
//
|
||||||
|
// A single boolean covers all four proposal types:
|
||||||
|
// - dedup supersession accepted → DropPattern MustFire=false
|
||||||
|
// - foreign-framing gate accepted → foreign pattern MustFire=false
|
||||||
|
// - vocab→tag / coverage hazard accepted → the enabled pattern MustFire=true
|
||||||
|
|
||||||
|
// AcceptedPin is one regression invariant for an accepted proposal.
|
||||||
|
type AcceptedPin struct {
|
||||||
|
Pattern string `json:"pattern"`
|
||||||
|
MustFire bool `json:"must_fire"`
|
||||||
|
Reason string `json:"reason"`
|
||||||
|
FromProposal string `json:"from_proposal,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PinSet is the accepted-pin registry for one machine (testdata/accepted_pins_*.json).
|
||||||
|
type PinSet struct {
|
||||||
|
Machine string `json:"machine"`
|
||||||
|
Pins []AcceptedPin `json:"pins"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PinResult is the verdict for one pin against an engine run.
|
||||||
|
type PinResult struct {
|
||||||
|
Pin AcceptedPin
|
||||||
|
OK bool
|
||||||
|
Detail string
|
||||||
|
}
|
||||||
|
|
||||||
|
// VerifyPins checks every pin against the set of pattern IDs the engine actually
|
||||||
|
// fired for the machine. A pin holds iff the pattern's presence equals MustFire.
|
||||||
|
func VerifyPins(pins []AcceptedPin, firedPatternIDs []string) []PinResult {
|
||||||
|
fired := make(map[string]bool, len(firedPatternIDs))
|
||||||
|
for _, id := range firedPatternIDs {
|
||||||
|
fired[id] = true
|
||||||
|
}
|
||||||
|
out := make([]PinResult, 0, len(pins))
|
||||||
|
for _, p := range pins {
|
||||||
|
got := fired[p.Pattern]
|
||||||
|
ok := got == p.MustFire
|
||||||
|
detail := "ok"
|
||||||
|
if !ok {
|
||||||
|
if p.MustFire {
|
||||||
|
detail = "expected to fire but did NOT — coverage/mapping regressed"
|
||||||
|
} else {
|
||||||
|
detail = "expected to be suppressed but FIRED — gate/supersession regressed"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out = append(out, PinResult{Pin: p, OK: ok, Detail: detail})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// GenerateDedupPin turns an accepted (verdict=duplicate) dedup candidate into the
|
||||||
|
// pin that protects the supersession: the dropped pattern must no longer fire.
|
||||||
|
func GenerateDedupPin(c DedupCandidate) AcceptedPin {
|
||||||
|
return AcceptedPin{
|
||||||
|
Pattern: c.DropPattern,
|
||||||
|
MustFire: false,
|
||||||
|
Reason: "accepted duplicate of " + c.KeepPattern + " (" + c.Category + ")",
|
||||||
|
FromProposal: "dedup " + c.DropPattern + " -> " + c.KeepPattern,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package iace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVerifyPins(t *testing.T) {
|
||||||
|
pins := []AcceptedPin{
|
||||||
|
{Pattern: "HPa", MustFire: true},
|
||||||
|
{Pattern: "HPb", MustFire: false},
|
||||||
|
}
|
||||||
|
res := VerifyPins(pins, []string{"HPa", "HPb"})
|
||||||
|
if !res[0].OK {
|
||||||
|
t.Errorf("HPa must_fire=true and it fired -> should be OK")
|
||||||
|
}
|
||||||
|
if res[1].OK {
|
||||||
|
t.Errorf("HPb must_fire=false but it fired -> should be VIOLATED")
|
||||||
|
}
|
||||||
|
res2 := VerifyPins(pins, []string{})
|
||||||
|
if res2[0].OK || !res2[1].OK {
|
||||||
|
t.Errorf("expected HPa violated + HPb ok, got %+v", res2)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateDedupPin(t *testing.T) {
|
||||||
|
pin := GenerateDedupPin(DedupCandidate{KeepPattern: "HP144", DropPattern: "HP013", Category: "electrical_hazard"})
|
||||||
|
if pin.Pattern != "HP013" || pin.MustFire {
|
||||||
|
t.Fatalf("want pin {HP013, must_fire=false}, got %+v", pin)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestWarewashing_AcceptedPins re-checks every accepted P1 supersession against the
|
||||||
|
// live warewashing engine output. A future change that un-suppresses HP013/016/018
|
||||||
|
// or drops HP2201/HP144 breaks a pin here — the gate compounds, not erodes.
|
||||||
|
func TestWarewashing_AcceptedPins(t *testing.T) {
|
||||||
|
raw, err := os.ReadFile(filepath.Join("testdata", "accepted_pins_warewashing.json"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read pins: %v", err)
|
||||||
|
}
|
||||||
|
var ps PinSet
|
||||||
|
if err := json.Unmarshal(raw, &ps); err != nil {
|
||||||
|
t.Fatalf("parse pins: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, kept := warewashingEngineOutput()
|
||||||
|
firedIDs := make([]string, 0, len(kept))
|
||||||
|
for _, pm := range kept {
|
||||||
|
firedIDs = append(firedIDs, pm.PatternID)
|
||||||
|
}
|
||||||
|
|
||||||
|
ok := 0
|
||||||
|
for _, r := range VerifyPins(ps.Pins, firedIDs) {
|
||||||
|
if r.OK {
|
||||||
|
ok++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.Errorf("PIN VIOLATED: %s (must_fire=%v) — %s [%s]", r.Pin.Pattern, r.Pin.MustFire, r.Detail, r.Pin.Reason)
|
||||||
|
}
|
||||||
|
t.Logf("accepted pins for %q: %d/%d hold", ps.Machine, ok, len(ps.Pins))
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"machine": "Gewerbliche Untertisch-Geschirrspuelmaschine (vernetzt)",
|
||||||
|
"pins": [
|
||||||
|
{"pattern": "HP016", "must_fire": false, "reason": "generic hot-surface (Formwerkzeuge/Auspuffleitung framing) superseded by HP2201", "from_proposal": "P1 thermal supersession"},
|
||||||
|
{"pattern": "HP018", "must_fire": false, "reason": "actuator-burn superseded by HP2201", "from_proposal": "P1 thermal supersession"},
|
||||||
|
{"pattern": "HP013", "must_fire": false, "reason": "stored-energy Batterie/USV framing superseded by HP144", "from_proposal": "P1 stored-energy supersession"},
|
||||||
|
{"pattern": "HP2201", "must_fire": true, "reason": "warewashing hot-surface (Boiler/Tank/Spuelkammer) must remain — it is the clean equivalent that replaces HP016/HP018", "from_proposal": "P1 thermal supersession"},
|
||||||
|
{"pattern": "HP144", "must_fire": true, "reason": "residual-voltage (Frequenzumrichter/Zwischenkreis) must remain — clean equivalent that replaces HP013", "from_proposal": "P1 stored-energy supersession"}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -28,6 +28,10 @@ var guidanceIntentSignals = []string{
|
|||||||
"edpb", "europäischer datenschutzausschuss", "europaeischer datenschutzausschuss",
|
"edpb", "europäischer datenschutzausschuss", "europaeischer datenschutzausschuss",
|
||||||
"dsk", "enisa", "bsi", "leitlinie", "guideline", "orientierungshilfe",
|
"dsk", "enisa", "bsi", "leitlinie", "guideline", "orientierungshilfe",
|
||||||
"auslegung", "empfiehlt", "empfehlung", "sagt", "laut",
|
"auslegung", "empfiehlt", "empfehlung", "sagt", "laut",
|
||||||
|
// Guidance-Dokumente direkt benannt (WP29-Working-Papers WP2xx + EDPB-Guidelines "GL 0x/20xx"):
|
||||||
|
// "Welche Kriterien nennt WP248 ..." / "Was sagt GL 07/2020 ..." tragen Guidance-Intent ohne
|
||||||
|
// die Verben oben. Fix: queryWantsGuidance verfehlte rein-doc-namige Formulierungen.
|
||||||
|
"wp2", "wp 2", "wp29", "working paper", "gl 0",
|
||||||
}
|
}
|
||||||
|
|
||||||
// controlIntentSignals mark a query that asks HOW to implement / which controls or
|
// controlIntentSignals mark a query that asks HOW to implement / which controls or
|
||||||
|
|||||||
@@ -0,0 +1,105 @@
|
|||||||
|
package ucca
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestGuidanceFixE2E runs the 10 hard cases through the REAL LegalRAGClient against the
|
||||||
|
// homogeneous build collection. Guarded by RUN_E2E=1. Reports the rank of the expected
|
||||||
|
// document within the returned top-K — proving whether the guidanceIntentSignals fix lifts
|
||||||
|
// guidance (WP248/WP260) back into the prompt. Toggle RAG_HYBRID_SEARCH to compare modes.
|
||||||
|
func TestGuidanceFixE2E(t *testing.T) {
|
||||||
|
if os.Getenv("RUN_E2E") != "1" {
|
||||||
|
t.Skip("set RUN_E2E=1 + QDRANT_URL/OLLAMA_URL to run")
|
||||||
|
}
|
||||||
|
c := NewLegalRAGClient()
|
||||||
|
coll := os.Getenv("E2E_COLLECTION")
|
||||||
|
if coll == "" {
|
||||||
|
coll = "bp_compliance_kb_2026_1_build"
|
||||||
|
}
|
||||||
|
cases := []struct{ id, q, expect string }{
|
||||||
|
{"GQ-0012", "Welche neun Kriterien nennt WP248 fuer ein voraussichtlich hohes Risiko?", "WP248"},
|
||||||
|
{"GQ-0013", "Ab wie vielen der WP248-Kriterien ist in der Regel eine Datenschutz-Folgenabschaetzung erforderlich?", "WP248"},
|
||||||
|
{"GQ-0023", "Welche Anforderungen stellt WP260 an eine klare und einfache Sprache?", "WP260"},
|
||||||
|
{"GQ-0024", "Was versteht WP260 unter Layered Privacy Notices?", "WP260"},
|
||||||
|
{"GQ-0054", "Welche grundlegenden Cybersecurity-Anforderungen enthaelt Annex I Part I?", "CRA"},
|
||||||
|
{"GQ-0060", "Wann muss eine aktiv ausgenutzte Schwachstelle gemeldet werden?", "CRA"},
|
||||||
|
{"GQ-0074", "Benoetigt eine SPS ohne Netzwerkanschluss eine CRA-Bewertung?", "CRA"},
|
||||||
|
{"GQ-0079", "Welche grundlegenden Sicherheits- und Gesundheitsschutzanforderungen enthaelt Anhang III?", "MASCHVO"},
|
||||||
|
{"GQ-0091", "Welche Anforderungen gelten fuer wesentliche Veraenderungen einer Maschine?", "MASCHVO"},
|
||||||
|
{"GQ-0070", "Wie greifen CRA und Maschinenverordnung bei einer vernetzten Maschine ineinander?", "CRA"},
|
||||||
|
}
|
||||||
|
fmt.Printf("\n### hybrid=%v collection=%s\n", os.Getenv("RAG_HYBRID_SEARCH") != "false", coll)
|
||||||
|
for _, tc := range cases {
|
||||||
|
res, err := c.SearchCollection(context.Background(), coll, tc.q, nil, 8)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", tc.id, err)
|
||||||
|
}
|
||||||
|
rank := -1
|
||||||
|
for i, r := range res {
|
||||||
|
lab := strings.ToUpper(r.RegulationCode + " " + r.ArticleLabel)
|
||||||
|
if strings.Contains(lab, tc.expect) {
|
||||||
|
rank = i + 1
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
top1 := ""
|
||||||
|
if len(res) > 0 {
|
||||||
|
top1 = res[0].RegulationCode + " (" + res[0].SourceClass + ")"
|
||||||
|
}
|
||||||
|
status := "FAIL"
|
||||||
|
if rank > 0 {
|
||||||
|
status = "OK"
|
||||||
|
}
|
||||||
|
fmt.Printf("%-9s expect=%-8s rank_in_top8=%-2d %-5s top1=%s\n", tc.id, tc.expect, rank, status, top1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBenchE2E runs the FULL ComplianceBench (E2E_BENCH_FILE) through the real client and
|
||||||
|
// prints, per question, the ordered top-8 regulation codes. Diffing BEFORE vs AFTER proves
|
||||||
|
// the fix only perturbs guidance-intent queries (gated on queryWantsGuidance) and never the
|
||||||
|
// norm questions — the Knowledge-Freeze regression guard.
|
||||||
|
func TestBenchE2E(t *testing.T) {
|
||||||
|
if os.Getenv("RUN_E2E") != "1" {
|
||||||
|
t.Skip("set RUN_E2E=1 + E2E_BENCH_FILE")
|
||||||
|
}
|
||||||
|
path := os.Getenv("E2E_BENCH_FILE")
|
||||||
|
if path == "" {
|
||||||
|
t.Skip("E2E_BENCH_FILE not set")
|
||||||
|
}
|
||||||
|
raw, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var bench struct {
|
||||||
|
Questions []struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Question string `json:"question"`
|
||||||
|
} `json:"questions"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal(raw, &bench); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c := NewLegalRAGClient()
|
||||||
|
coll := os.Getenv("E2E_COLLECTION")
|
||||||
|
if coll == "" {
|
||||||
|
coll = "bp_compliance_kb_2026_1_build"
|
||||||
|
}
|
||||||
|
fmt.Printf("### BENCH n=%d hybrid=%v\n", len(bench.Questions), os.Getenv("RAG_HYBRID_SEARCH") != "false")
|
||||||
|
for _, q := range bench.Questions {
|
||||||
|
res, err := c.SearchCollection(context.Background(), coll, q.Question, nil, 8)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", q.ID, err)
|
||||||
|
}
|
||||||
|
codes := make([]string, 0, len(res))
|
||||||
|
for _, r := range res {
|
||||||
|
codes = append(codes, strings.ReplaceAll(r.RegulationCode, ";", ","))
|
||||||
|
}
|
||||||
|
fmt.Printf("BENCH|%s|%s\n", q.ID, strings.Join(codes, ";"))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -78,6 +78,19 @@ func (c *LegalRAGClient) Search(ctx context.Context, query string, regulationIDs
|
|||||||
// If hybrid search is enabled, it uses the Qdrant Query API with RRF fusion
|
// If hybrid search is enabled, it uses the Qdrant Query API with RRF fusion
|
||||||
// (dense + full-text). Falls back to dense-only /points/search on failure.
|
// (dense + full-text). Falls back to dense-only /points/search on failure.
|
||||||
func (c *LegalRAGClient) searchInternal(ctx context.Context, collection string, query string, regulationIDs []string, topK int) ([]LegalSearchResult, error) {
|
func (c *LegalRAGClient) searchInternal(ctx context.Context, collection string, query string, regulationIDs []string, topK int) ([]LegalSearchResult, error) {
|
||||||
|
// Multi-Regulation-Retrieval: nennt die Query EXPLIZIT >=2 Regelwerke (z.B. "CRA und
|
||||||
|
// Maschinenverordnung"), wird pro Regelwerk separat retrieved + gemergt, damit BEIDE
|
||||||
|
// Domaenen im Prompt landen statt nur der keyword-dominanten. Generisch (Query->Regelwerke,
|
||||||
|
// keine doc-spezifische Logik); nur wenn der Caller nicht ohnehin schon auf Regulierungen
|
||||||
|
// filtert. Best-effort: leeres/fehlerhaftes Multi-Ergebnis faellt auf die Standardsuche zurueck.
|
||||||
|
if len(regulationIDs) == 0 {
|
||||||
|
if regs := detectRegulations(query); len(regs) >= 2 {
|
||||||
|
if mr, mErr := c.searchMultiRegulation(ctx, collection, query, regs, topK); mErr == nil && len(mr) > 0 {
|
||||||
|
return mr, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
embedding, err := c.generateEmbedding(ctx, query)
|
embedding, err := c.generateEmbedding(ctx, query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate embedding: %w", err)
|
return nil, fmt.Errorf("failed to generate embedding: %w", err)
|
||||||
@@ -123,43 +136,7 @@ func (c *LegalRAGClient) searchInternal(ctx context.Context, collection string,
|
|||||||
hits = c.expandViaGraph(ctx, collection, hits)
|
hits = c.expandViaGraph(ctx, collection, hits)
|
||||||
}
|
}
|
||||||
|
|
||||||
results := make([]LegalSearchResult, len(hits))
|
results := hitsToResults(hits)
|
||||||
for i, hit := range hits {
|
|
||||||
// Legal-Metadaten nach rag_reingest_spec.md §2: bevorzugt die normalisierten Felder
|
|
||||||
// (article_label/regulation_code/article/...); Fallback auf alte Feldnamen, solange der
|
|
||||||
// Korpus noch nicht re-ingestiert ist (regulation_id, section="§ 38").
|
|
||||||
regCode := getString(hit.Payload, "regulation_code")
|
|
||||||
if regCode == "" {
|
|
||||||
regCode = getString(hit.Payload, "regulation_id")
|
|
||||||
}
|
|
||||||
article := getString(hit.Payload, "article")
|
|
||||||
if article == "" {
|
|
||||||
article = getString(hit.Payload, "section")
|
|
||||||
}
|
|
||||||
results[i] = LegalSearchResult{
|
|
||||||
Text: getString(hit.Payload, "chunk_text"),
|
|
||||||
RegulationCode: regCode,
|
|
||||||
RegulationName: getString(hit.Payload, "regulation_name_de"),
|
|
||||||
RegulationShort: getString(hit.Payload, "regulation_short"),
|
|
||||||
Category: getString(hit.Payload, "category"),
|
|
||||||
ArticleLabel: getString(hit.Payload, "article_label"),
|
|
||||||
Article: article,
|
|
||||||
Paragraph: getString(hit.Payload, "paragraph"),
|
|
||||||
Sub: getString(hit.Payload, "sub"),
|
|
||||||
IsRecital: getBool(hit.Payload, "is_recital"),
|
|
||||||
CitationStyle: getString(hit.Payload, "citation_style"),
|
|
||||||
Pages: getIntSlice(hit.Payload, "pages"),
|
|
||||||
SourceURL: getString(hit.Payload, "source"),
|
|
||||||
Score: hit.Score,
|
|
||||||
AuthorityWeight: getInt(hit.Payload, "authority_weight"),
|
|
||||||
SourceClass: getString(hit.Payload, "source_class"),
|
|
||||||
Jurisdiction: getString(hit.Payload, "jurisdiction"),
|
|
||||||
CitationUnit: getString(hit.Payload, "citation_unit"),
|
|
||||||
ReferencesOut: getStringSlice(hit.Payload, "references_out"),
|
|
||||||
ReferencesIn: getStringSlice(hit.Payload, "references_in"),
|
|
||||||
Superseded: getString(hit.Payload, "status") == "superseded",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Authority-aware Re-Ranking: bindendes Recht der passenden Jurisdiktion/Domaene nach
|
// Authority-aware Re-Ranking: bindendes Recht der passenden Jurisdiktion/Domaene nach
|
||||||
// oben, Guidance/Fremdrecht/Off-Domain runter (nichts wird geloescht). Reihenfolge only,
|
// oben, Guidance/Fremdrecht/Off-Domain runter (nichts wird geloescht). Reihenfolge only,
|
||||||
|
|||||||
@@ -122,12 +122,14 @@ func (c *LegalRAGClient) searchHybrid(ctx context.Context, collection string, em
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(regulationIDs) > 0 {
|
if len(regulationIDs) > 0 {
|
||||||
conditions := make([]qdrantCondition, len(regulationIDs))
|
// Match BOTH the legacy field (regulation_id) and the normalized field
|
||||||
for i, regID := range regulationIDs {
|
// (regulation_code) so per-regulation filtering works on the re-ingested corpus too.
|
||||||
conditions[i] = qdrantCondition{
|
conditions := make([]qdrantCondition, 0, len(regulationIDs)*2)
|
||||||
Key: "regulation_id",
|
for _, regID := range regulationIDs {
|
||||||
Match: qdrantMatch{Value: regID},
|
conditions = append(conditions,
|
||||||
}
|
qdrantCondition{Key: "regulation_id", Match: qdrantMatch{Value: regID}},
|
||||||
|
qdrantCondition{Key: "regulation_code", Match: qdrantMatch{Value: regID}},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
queryReq.Filter = &qdrantFilter{Should: conditions}
|
queryReq.Filter = &qdrantFilter{Should: conditions}
|
||||||
}
|
}
|
||||||
@@ -175,12 +177,14 @@ func (c *LegalRAGClient) searchDense(ctx context.Context, collection string, emb
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(regulationIDs) > 0 {
|
if len(regulationIDs) > 0 {
|
||||||
conditions := make([]qdrantCondition, len(regulationIDs))
|
// Match BOTH the legacy field (regulation_id) and the normalized field
|
||||||
for i, regID := range regulationIDs {
|
// (regulation_code) so per-regulation filtering works on the re-ingested corpus too.
|
||||||
conditions[i] = qdrantCondition{
|
conditions := make([]qdrantCondition, 0, len(regulationIDs)*2)
|
||||||
Key: "regulation_id",
|
for _, regID := range regulationIDs {
|
||||||
Match: qdrantMatch{Value: regID},
|
conditions = append(conditions,
|
||||||
}
|
qdrantCondition{Key: "regulation_id", Match: qdrantMatch{Value: regID}},
|
||||||
|
qdrantCondition{Key: "regulation_code", Match: qdrantMatch{Value: regID}},
|
||||||
|
)
|
||||||
}
|
}
|
||||||
searchReq.Filter = &qdrantFilter{Should: conditions}
|
searchReq.Filter = &qdrantFilter{Should: conditions}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,143 @@
|
|||||||
|
package ucca
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// multiRegMinPerRegulation is the minimum number of hits fetched per named regulation, so
|
||||||
|
// each domain is fairly represented even when topK/len(regs) would be tiny.
|
||||||
|
const multiRegMinPerRegulation = 3
|
||||||
|
|
||||||
|
// regulationCatalog maps a regulation to (a) the aliases that signal it is EXPLICITLY named
|
||||||
|
// in a query and (b) the regulation_code/regulation_id values used to filter the corpus.
|
||||||
|
// Deterministic + generic: a query naming >=2 regulations triggers per-regulation retrieval
|
||||||
|
// so a cross-regulation question returns every named domain — NOT a doc-specific rule.
|
||||||
|
var regulationCatalog = []struct {
|
||||||
|
Canonical string
|
||||||
|
Aliases []string
|
||||||
|
CodeValues []string
|
||||||
|
}{
|
||||||
|
{"CRA", []string{"cra", "cyber resilience"}, []string{"CRA"}},
|
||||||
|
{"MaschVO", []string{"maschinenverordnung", "maschvo", "machinery regulation"}, []string{"MASCHVO", "MaschVO"}},
|
||||||
|
{"NIS2", []string{"nis2", "nis-2", "nis 2"}, []string{"NIS2"}},
|
||||||
|
{"DORA", []string{"dora"}, []string{"DORA"}},
|
||||||
|
{"Data Act", []string{"data act", "datengesetz"}, []string{"DATA ACT", "DataAct"}},
|
||||||
|
{"AI Act", []string{"ai act", "ki-vo", "ki-verordnung", "ai-verordnung"}, []string{"AI ACT", "AIAct"}},
|
||||||
|
{"DSGVO", []string{"dsgvo", "gdpr"}, []string{"DSGVO"}},
|
||||||
|
{"TDDDG", []string{"tdddg"}, []string{"TDDDG"}},
|
||||||
|
{"BDSG", []string{"bdsg"}, []string{"BDSG"}},
|
||||||
|
}
|
||||||
|
|
||||||
|
type detectedRegulation struct {
|
||||||
|
Canonical string
|
||||||
|
CodeValues []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// detectRegulations returns the DISTINCT regulations explicitly named in the query. >=2 of
|
||||||
|
// them is the trigger for multi-regulation retrieval. Pure + deterministic, no LLM.
|
||||||
|
func detectRegulations(query string) []detectedRegulation {
|
||||||
|
q := strings.ToLower(query)
|
||||||
|
var out []detectedRegulation
|
||||||
|
for _, r := range regulationCatalog {
|
||||||
|
for _, a := range r.Aliases {
|
||||||
|
if strings.Contains(q, a) {
|
||||||
|
out = append(out, detectedRegulation{Canonical: r.Canonical, CodeValues: r.CodeValues})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func hitID(h qdrantSearchHit) string { return fmt.Sprintf("%v", h.ID) }
|
||||||
|
|
||||||
|
// searchMultiRegulation retrieves each explicitly-named regulation SEPARATELY (per-regulation
|
||||||
|
// filter) and merges, so a cross-regulation query ("Wie greifen CRA und MaschVO ineinander?")
|
||||||
|
// returns BOTH domains in the prompt instead of only the keyword-dominant one. Generic over any
|
||||||
|
// named pair (DSGVO+TDDDG, CRA+NIS2, DORA+NIS2, AI Act+DSGVO, ...). The merged pool is
|
||||||
|
// authority-reranked once. Pure pool-construction; topK contract preserved.
|
||||||
|
func (c *LegalRAGClient) searchMultiRegulation(ctx context.Context, collection, query string, regs []detectedRegulation, topK int) ([]LegalSearchResult, error) {
|
||||||
|
embedding, err := c.generateEmbedding(ctx, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to generate embedding: %w", err)
|
||||||
|
}
|
||||||
|
perReg := topK / len(regs)
|
||||||
|
if perReg < multiRegMinPerRegulation {
|
||||||
|
perReg = multiRegMinPerRegulation
|
||||||
|
}
|
||||||
|
var merged []qdrantSearchHit
|
||||||
|
seen := make(map[string]bool)
|
||||||
|
for _, r := range regs {
|
||||||
|
var hits []qdrantSearchHit
|
||||||
|
if c.hybridEnabled {
|
||||||
|
if h, hErr := c.searchHybrid(ctx, collection, embedding, r.CodeValues, perReg); hErr == nil {
|
||||||
|
hits = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if hits == nil {
|
||||||
|
if h, dErr := c.searchDense(ctx, collection, embedding, r.CodeValues, perReg); dErr == nil {
|
||||||
|
hits = h
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, h := range hits {
|
||||||
|
id := hitID(h)
|
||||||
|
if seen[id] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[id] = true
|
||||||
|
merged = append(merged, h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(merged) == 0 {
|
||||||
|
return nil, fmt.Errorf("multi-regulation search returned no hits")
|
||||||
|
}
|
||||||
|
results := hitsToResults(merged)
|
||||||
|
results = rerankByAuthority(query, results)
|
||||||
|
if topK > 0 && len(results) > topK {
|
||||||
|
results = results[:topK]
|
||||||
|
}
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// hitsToResults maps raw Qdrant hits to LegalSearchResult, preferring the normalized payload
|
||||||
|
// fields (regulation_code/article_label/...) with fallback to the legacy names (regulation_id,
|
||||||
|
// section) while the corpus is mid-re-ingestion. Shared by searchInternal + searchMultiRegulation.
|
||||||
|
func hitsToResults(hits []qdrantSearchHit) []LegalSearchResult {
|
||||||
|
results := make([]LegalSearchResult, len(hits))
|
||||||
|
for i, hit := range hits {
|
||||||
|
regCode := getString(hit.Payload, "regulation_code")
|
||||||
|
if regCode == "" {
|
||||||
|
regCode = getString(hit.Payload, "regulation_id")
|
||||||
|
}
|
||||||
|
article := getString(hit.Payload, "article")
|
||||||
|
if article == "" {
|
||||||
|
article = getString(hit.Payload, "section")
|
||||||
|
}
|
||||||
|
results[i] = LegalSearchResult{
|
||||||
|
Text: getString(hit.Payload, "chunk_text"),
|
||||||
|
RegulationCode: regCode,
|
||||||
|
RegulationName: getString(hit.Payload, "regulation_name_de"),
|
||||||
|
RegulationShort: getString(hit.Payload, "regulation_short"),
|
||||||
|
Category: getString(hit.Payload, "category"),
|
||||||
|
ArticleLabel: getString(hit.Payload, "article_label"),
|
||||||
|
Article: article,
|
||||||
|
Paragraph: getString(hit.Payload, "paragraph"),
|
||||||
|
Sub: getString(hit.Payload, "sub"),
|
||||||
|
IsRecital: getBool(hit.Payload, "is_recital"),
|
||||||
|
CitationStyle: getString(hit.Payload, "citation_style"),
|
||||||
|
Pages: getIntSlice(hit.Payload, "pages"),
|
||||||
|
SourceURL: getString(hit.Payload, "source"),
|
||||||
|
Score: hit.Score,
|
||||||
|
AuthorityWeight: getInt(hit.Payload, "authority_weight"),
|
||||||
|
SourceClass: getString(hit.Payload, "source_class"),
|
||||||
|
Jurisdiction: getString(hit.Payload, "jurisdiction"),
|
||||||
|
CitationUnit: getString(hit.Payload, "citation_unit"),
|
||||||
|
ReferencesOut: getStringSlice(hit.Payload, "references_out"),
|
||||||
|
ReferencesIn: getStringSlice(hit.Payload, "references_in"),
|
||||||
|
Superseded: getString(hit.Payload, "status") == "superseded",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package ucca
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestDetectRegulations is a pure unit test of the multi-regulation TRIGGER (no Qdrant):
|
||||||
|
// only an explicit naming of >=2 regulations enables multi-regulation retrieval. A single
|
||||||
|
// named regulation, or a topical question that doesn't name one, stays single-domain.
|
||||||
|
func TestDetectRegulations(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
q string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"Welche neun Kriterien nennt WP248 fuer ein voraussichtlich hohes Risiko?", 0},
|
||||||
|
{"Welche Anforderungen gelten fuer wesentliche Veraenderungen einer Maschine?", 0}, // "Maschine" != MaschVO
|
||||||
|
{"Benoetigt eine SPS ohne Netzwerkanschluss eine CRA-Bewertung?", 1}, // 1 -> single
|
||||||
|
{"Wie greifen CRA und Maschinenverordnung bei einer vernetzten Maschine ineinander?", 2},
|
||||||
|
{"Wie greifen DSGVO und TDDDG bei der Nutzung von Cookies ineinander?", 2},
|
||||||
|
{"Wie verhalten sich DORA und NIS2 fuer ein Finanzunternehmen?", 2},
|
||||||
|
{"Wie greifen AI Act und DSGVO bei einem KI-System ineinander?", 2},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
if got := len(detectRegulations(c.q)); got != c.want {
|
||||||
|
t.Errorf("detectRegulations(%q) = %d, want %d", c.q, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMultiRegE2E (RUN_E2E=1) verifies against the build collection that an explicit
|
||||||
|
// cross-regulation query returns BOTH named domains in the top-K — the core acceptance
|
||||||
|
// gate for multi-regulation retrieval.
|
||||||
|
func TestMultiRegE2E(t *testing.T) {
|
||||||
|
if os.Getenv("RUN_E2E") != "1" {
|
||||||
|
t.Skip("set RUN_E2E=1 + QDRANT_URL/OLLAMA_URL")
|
||||||
|
}
|
||||||
|
c := NewLegalRAGClient()
|
||||||
|
coll := os.Getenv("E2E_COLLECTION")
|
||||||
|
if coll == "" {
|
||||||
|
coll = "bp_compliance_kb_2026_1_build"
|
||||||
|
}
|
||||||
|
cases := []struct {
|
||||||
|
id string
|
||||||
|
q string
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{"GQ-0070 CRA+MaschVO", "Wie greifen CRA und Maschinenverordnung bei einer vernetzten Maschine ineinander?", []string{"CRA", "MASCH"}},
|
||||||
|
{"DSGVO+TDDDG", "Wie greifen DSGVO und TDDDG bei der Nutzung von Cookies und Tracking-Technologien ineinander?", []string{"DSGVO", "TDDDG"}},
|
||||||
|
{"CRA+NIS2", "Wie verhalten sich CRA und NIS2 bei einem vernetzten Produkt eines wichtigen Unternehmens zueinander?", []string{"CRA", "NIS2"}},
|
||||||
|
{"DORA+NIS2", "Wie greifen DORA und NIS2 bei einem Finanzunternehmen ineinander?", []string{"DORA", "NIS2"}},
|
||||||
|
{"AI Act+DSGVO", "Wie greifen AI Act und DSGVO bei einem KI-System ineinander, das personenbezogene Daten verarbeitet?", []string{"AI ACT", "DSGVO"}},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
res, err := c.SearchCollection(context.Background(), coll, tc.q, nil, 8)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", tc.id, err)
|
||||||
|
}
|
||||||
|
present := map[string]bool{}
|
||||||
|
for _, r := range res {
|
||||||
|
present[strings.ToUpper(r.RegulationCode)] = true
|
||||||
|
}
|
||||||
|
ok := true
|
||||||
|
for _, w := range tc.want {
|
||||||
|
found := false
|
||||||
|
for cd := range present {
|
||||||
|
if strings.Contains(cd, w) {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
codes := make([]string, 0, len(present))
|
||||||
|
for cd := range present {
|
||||||
|
codes = append(codes, cd)
|
||||||
|
}
|
||||||
|
status := "OK"
|
||||||
|
if !ok {
|
||||||
|
status = "FAIL"
|
||||||
|
}
|
||||||
|
fmt.Printf("%-22s want=%v present=%v %s\n", tc.id, tc.want, codes, status)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("%s: not all named regulations in top-8 (want %v, got %v)", tc.id, tc.want, codes)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -162,7 +162,7 @@ async def update_ai_system(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
"""Update an AI system."""
|
"""Update an AI system."""
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
system = db.query(AISystemDB).filter(AISystemDB.id == system_id).first()
|
system = db.query(AISystemDB).filter(AISystemDB.id == system_id).first()
|
||||||
if not system:
|
if not system:
|
||||||
@@ -226,7 +226,7 @@ async def assess_ai_system(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
"""Run AI Act risk assessment for an AI system."""
|
"""Run AI Act risk assessment for an AI system."""
|
||||||
from datetime import datetime
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
system = db.query(AISystemDB).filter(AISystemDB.id == system_id).first()
|
system = db.query(AISystemDB).filter(AISystemDB.id == system_id).first()
|
||||||
if not system:
|
if not system:
|
||||||
|
|||||||
@@ -47,6 +47,8 @@ from compliance.services.canonical_control_service import (
|
|||||||
_control_row, # re-exported for legacy test imports
|
_control_row, # re-exported for legacy test imports
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
router = APIRouter(prefix="/v1/canonical", tags=["canonical-controls"])
|
router = APIRouter(prefix="/v1/canonical", tags=["canonical-controls"])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ Endpoints:
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from datetime import datetime, date, timedelta
|
from datetime import datetime, date, timedelta, timezone
|
||||||
from calendar import month_abbr
|
from calendar import month_abbr
|
||||||
from typing import Optional, Dict, Any, List
|
from typing import Optional, Dict, Any, List
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|||||||
@@ -26,10 +26,11 @@ versions). Module-level helpers re-exported for legacy tests.
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any, List, Optional
|
from typing import Any, List, Optional
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Query
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from fastapi.responses import Response
|
from fastapi.responses import Response
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
from classroom_engine.database import get_db
|
from classroom_engine.database import get_db
|
||||||
from compliance.api._http_errors import translate_domain_errors
|
from compliance.api._http_errors import translate_domain_errors
|
||||||
@@ -484,6 +485,7 @@ async def list_dsfas(
|
|||||||
async def create_dsfa(
|
async def create_dsfa(
|
||||||
request: DSFACreate,
|
request: DSFACreate,
|
||||||
tenant_id: Optional[str] = Query(None),
|
tenant_id: Optional[str] = Query(None),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
service: DSFAService = Depends(get_dsfa_service),
|
service: DSFAService = Depends(get_dsfa_service),
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Neue DSFA erstellen."""
|
"""Neue DSFA erstellen."""
|
||||||
|
|||||||
@@ -16,6 +16,11 @@ from the legacy path.
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import json
|
||||||
|
import hashlib
|
||||||
|
import uuid as uuid_module
|
||||||
|
from datetime import datetime, timedelta
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
||||||
@@ -30,14 +35,15 @@ from ..db import (
|
|||||||
EvidenceConfidenceEnum,
|
EvidenceConfidenceEnum,
|
||||||
EvidenceTruthStatusEnum,
|
EvidenceTruthStatusEnum,
|
||||||
)
|
)
|
||||||
from ..db.models import EvidenceDB, ControlDB, AuditTrailDB
|
from ..db.models import EvidenceDB, AuditTrailDB
|
||||||
from ..services.auto_risk_updater import AutoRiskUpdater
|
from ..services.auto_risk_updater import AutoRiskUpdater
|
||||||
from ..services.evidence_service import EvidenceService
|
from ..services.evidence_service import EvidenceService, _update_risks as _update_risks_impl
|
||||||
from .schemas import (
|
from .schemas import (
|
||||||
EvidenceCreate, EvidenceResponse, EvidenceListResponse,
|
EvidenceCreate, EvidenceResponse, EvidenceListResponse,
|
||||||
EvidenceRejectRequest,
|
EvidenceRejectRequest,
|
||||||
)
|
)
|
||||||
from .audit_trail_utils import log_audit_trail
|
from .audit_trail_utils import log_audit_trail
|
||||||
|
from ._http_errors import translate_domain_errors
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
router = APIRouter(tags=["compliance-evidence"])
|
router = APIRouter(tags=["compliance-evidence"])
|
||||||
@@ -146,6 +152,7 @@ async def list_evidence(
|
|||||||
status: Optional[str] = None,
|
status: Optional[str] = None,
|
||||||
page: Optional[int] = Query(None, ge=1, description="Page number (1-based)"),
|
page: Optional[int] = Query(None, ge=1, description="Page number (1-based)"),
|
||||||
limit: Optional[int] = Query(None, ge=1, le=500, description="Items per page"),
|
limit: Optional[int] = Query(None, ge=1, le=500, description="Items per page"),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
service: EvidenceService = Depends(get_evidence_service),
|
service: EvidenceService = Depends(get_evidence_service),
|
||||||
) -> EvidenceListResponse:
|
) -> EvidenceListResponse:
|
||||||
"""List evidence with optional filters and pagination."""
|
"""List evidence with optional filters and pagination."""
|
||||||
@@ -186,9 +193,11 @@ async def list_evidence(
|
|||||||
@router.post("/evidence", response_model=EvidenceResponse)
|
@router.post("/evidence", response_model=EvidenceResponse)
|
||||||
async def create_evidence(
|
async def create_evidence(
|
||||||
evidence_data: EvidenceCreate,
|
evidence_data: EvidenceCreate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
service: EvidenceService = Depends(get_evidence_service),
|
service: EvidenceService = Depends(get_evidence_service),
|
||||||
) -> EvidenceResponse:
|
) -> EvidenceResponse:
|
||||||
"""Create new evidence record."""
|
"""Create new evidence record."""
|
||||||
|
dsms_cid = None
|
||||||
repo = EvidenceRepository(db)
|
repo = EvidenceRepository(db)
|
||||||
|
|
||||||
# Get control UUID
|
# Get control UUID
|
||||||
@@ -257,6 +266,7 @@ async def create_evidence(
|
|||||||
@router.delete("/evidence/{evidence_id}")
|
@router.delete("/evidence/{evidence_id}")
|
||||||
async def delete_evidence(
|
async def delete_evidence(
|
||||||
evidence_id: str,
|
evidence_id: str,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
service: EvidenceService = Depends(get_evidence_service),
|
service: EvidenceService = Depends(get_evidence_service),
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Delete an evidence record."""
|
"""Delete an evidence record."""
|
||||||
@@ -275,6 +285,7 @@ async def upload_evidence(
|
|||||||
title: str = Query(...),
|
title: str = Query(...),
|
||||||
file: UploadFile = File(...),
|
file: UploadFile = File(...),
|
||||||
description: Optional[str] = Query(None),
|
description: Optional[str] = Query(None),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
service: EvidenceService = Depends(get_evidence_service),
|
service: EvidenceService = Depends(get_evidence_service),
|
||||||
) -> EvidenceResponse:
|
) -> EvidenceResponse:
|
||||||
"""Upload evidence file."""
|
"""Upload evidence file."""
|
||||||
@@ -674,6 +685,7 @@ async def collect_ci_evidence(
|
|||||||
async def get_ci_evidence_status(
|
async def get_ci_evidence_status(
|
||||||
control_id: Optional[str] = Query(None, description="Filter by control ID"),
|
control_id: Optional[str] = Query(None, description="Filter by control ID"),
|
||||||
days: int = Query(30, description="Look back N days"),
|
days: int = Query(30, description="Look back N days"),
|
||||||
|
db: Session = Depends(get_db),
|
||||||
service: EvidenceService = Depends(get_evidence_service),
|
service: EvidenceService = Depends(get_evidence_service),
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Get CI/CD evidence collection status overview."""
|
"""Get CI/CD evidence collection status overview."""
|
||||||
@@ -681,70 +693,8 @@ async def get_ci_evidence_status(
|
|||||||
return service.ci_status(control_id, days)
|
return service.ci_status(control_id, days)
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
# (Alte CI-Status-Implementierung entfernt — unerreichbarer Code nach `return
|
||||||
# Legacy re-exports for tests that import helpers directly.
|
# service.ci_status(...)`; durch den Service ersetzt, `query` war nie initialisiert.)
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
if control_id:
|
|
||||||
ctrl_repo = ControlRepository(db)
|
|
||||||
control = ctrl_repo.get_by_control_id(control_id)
|
|
||||||
if control:
|
|
||||||
query = query.filter(EvidenceDB.control_id == control.id)
|
|
||||||
|
|
||||||
evidence_list = query.order_by(EvidenceDB.collected_at.desc()).limit(100).all()
|
|
||||||
|
|
||||||
# Group by control and calculate stats
|
|
||||||
control_stats = defaultdict(lambda: {
|
|
||||||
"total": 0,
|
|
||||||
"valid": 0,
|
|
||||||
"failed": 0,
|
|
||||||
"last_collected": None,
|
|
||||||
"evidence": [],
|
|
||||||
})
|
|
||||||
|
|
||||||
for e in evidence_list:
|
|
||||||
# Get control_id string
|
|
||||||
control = db.query(ControlDB).filter(ControlDB.id == e.control_id).first()
|
|
||||||
ctrl_id = control.control_id if control else "unknown"
|
|
||||||
|
|
||||||
stats = control_stats[ctrl_id]
|
|
||||||
stats["total"] += 1
|
|
||||||
if e.status:
|
|
||||||
if e.status.value == "valid":
|
|
||||||
stats["valid"] += 1
|
|
||||||
elif e.status.value == "failed":
|
|
||||||
stats["failed"] += 1
|
|
||||||
if not stats["last_collected"] or e.collected_at > stats["last_collected"]:
|
|
||||||
stats["last_collected"] = e.collected_at
|
|
||||||
|
|
||||||
# Add evidence summary
|
|
||||||
stats["evidence"].append({
|
|
||||||
"id": e.id,
|
|
||||||
"type": e.evidence_type,
|
|
||||||
"status": e.status.value if e.status else None,
|
|
||||||
"collected_at": e.collected_at.isoformat() if e.collected_at else None,
|
|
||||||
"ci_job_id": e.ci_job_id,
|
|
||||||
})
|
|
||||||
|
|
||||||
# Convert to list and sort
|
|
||||||
result = []
|
|
||||||
for ctrl_id, stats in control_stats.items():
|
|
||||||
result.append({
|
|
||||||
"control_id": ctrl_id,
|
|
||||||
"total_evidence": stats["total"],
|
|
||||||
"valid_count": stats["valid"],
|
|
||||||
"failed_count": stats["failed"],
|
|
||||||
"last_collected": stats["last_collected"].isoformat() if stats["last_collected"] else None,
|
|
||||||
"recent_evidence": stats["evidence"][:5],
|
|
||||||
})
|
|
||||||
|
|
||||||
result.sort(key=lambda x: x["last_collected"] or "", reverse=True)
|
|
||||||
|
|
||||||
return {
|
|
||||||
"period_days": days,
|
|
||||||
"total_evidence": len(evidence_list),
|
|
||||||
"controls": result,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
@@ -772,6 +722,7 @@ async def review_evidence(
|
|||||||
approval_status='first_approved'. A second (different) reviewer then
|
approval_status='first_approved'. A second (different) reviewer then
|
||||||
sets second_reviewer and approval_status='approved'.
|
sets second_reviewer and approval_status='approved'.
|
||||||
"""
|
"""
|
||||||
|
dsms_cid = None
|
||||||
evidence = db.query(EvidenceDB).filter(EvidenceDB.id == evidence_id).first()
|
evidence = db.query(EvidenceDB).filter(EvidenceDB.id == evidence_id).first()
|
||||||
if not evidence:
|
if not evidence:
|
||||||
raise HTTPException(status_code=404, detail=f"Evidence {evidence_id} not found")
|
raise HTTPException(status_code=404, detail=f"Evidence {evidence_id} not found")
|
||||||
@@ -851,6 +802,7 @@ async def reject_evidence(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
):
|
):
|
||||||
"""Reject evidence (sets approval_status='rejected')."""
|
"""Reject evidence (sets approval_status='rejected')."""
|
||||||
|
dsms_cid = None
|
||||||
evidence = db.query(EvidenceDB).filter(EvidenceDB.id == evidence_id).first()
|
evidence = db.query(EvidenceDB).filter(EvidenceDB.id == evidence_id).first()
|
||||||
if not evidence:
|
if not evidence:
|
||||||
raise HTTPException(status_code=404, detail=f"Evidence {evidence_id} not found")
|
raise HTTPException(status_code=404, detail=f"Evidence {evidence_id} not found")
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ from fastapi.responses import FileResponse
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from classroom_engine.database import get_db
|
from classroom_engine.database import get_db
|
||||||
|
from ..db.models import EvidenceDB
|
||||||
|
|
||||||
from .audit_trail_utils import log_audit_trail
|
from .audit_trail_utils import log_audit_trail
|
||||||
from ..db import (
|
from ..db import (
|
||||||
@@ -310,6 +311,7 @@ async def list_controls_paginated(
|
|||||||
)
|
)
|
||||||
async def get_control(
|
async def get_control(
|
||||||
control_id: str,
|
control_id: str,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
svc: ControlExportService = Depends(get_ctrl_export_service),
|
svc: ControlExportService = Depends(get_ctrl_export_service),
|
||||||
) -> ControlResponse:
|
) -> ControlResponse:
|
||||||
"""Get a specific control by control_id."""
|
"""Get a specific control by control_id."""
|
||||||
@@ -354,6 +356,7 @@ async def get_control(
|
|||||||
async def update_control(
|
async def update_control(
|
||||||
control_id: str,
|
control_id: str,
|
||||||
update: ControlUpdate,
|
update: ControlUpdate,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
svc: ControlExportService = Depends(get_ctrl_export_service),
|
svc: ControlExportService = Depends(get_ctrl_export_service),
|
||||||
) -> ControlResponse:
|
) -> ControlResponse:
|
||||||
"""Update a control."""
|
"""Update a control."""
|
||||||
@@ -443,6 +446,7 @@ async def update_control(
|
|||||||
async def review_control(
|
async def review_control(
|
||||||
control_id: str,
|
control_id: str,
|
||||||
review: ControlReviewRequest,
|
review: ControlReviewRequest,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
svc: ControlExportService = Depends(get_ctrl_export_service),
|
svc: ControlExportService = Depends(get_ctrl_export_service),
|
||||||
) -> ControlResponse:
|
) -> ControlResponse:
|
||||||
"""Mark a control as reviewed with new status."""
|
"""Mark a control as reviewed with new status."""
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ Phase 1 Step 4 refactor: handlers delegate to VVTService.
|
|||||||
import logging
|
import logging
|
||||||
from typing import Any, List, Optional
|
from typing import Any, List, Optional
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, Query, Request
|
from fastapi import APIRouter, Depends, HTTPException, Query, Request
|
||||||
from fastapi.responses import StreamingResponse
|
from fastapi.responses import StreamingResponse
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user