Update graph_derive_edges.py and graph_utils.py to version 4.1.0: Enhance edge ID generation by incorporating target_section into the ID calculation, allowing for distinct edges across different sections. Update documentation to reflect changes in ID structure and improve clarity on edge handling during de-duplication.

This commit is contained in:
Lars committed 2026-01-10 15:45:26 +01:00
1 parent a852975811
commit 2da98e8e37
2 files changed
+20 -20

No files matched your search

+14 -15
View File
@@ -3,7 +3,7 @@ FILE: app/core/graph/graph_derive_edges.py
DESCRIPTION: Hauptlogik zur Kanten-Aggregation und De-Duplizierung.
WP-15b/c Audit:
- Präzises Sektions-Splitting via parse_link_target.
- Eindeutige ID-Generierung pro Sektions-Variante (Multigraph).
- v4.1.0: Eindeutige ID-Generierung pro Sektions-Variante (Multigraph).
- Ermöglicht dem Retriever die Super-Edge-Aggregation.
"""
from typing import List, Optional, Dict, Tuple
@@ -56,7 +56,6 @@ def build_edges_for_note(
"provenance": "structure", "rule_id": "structure:order", "confidence": PROVENANCE_PRIORITY["structure:order"]
}))
edges.append(_edge("prev", "chunk", next_id, cid, note_id, {
"chunk_id": next_id,
"edge_id": _mk_edge_id("prev", next_id, cid, "chunk"),
"provenance": "structure", "rule_id": "structure:order", "confidence": PROVENANCE_PRIORITY["structure:order"]
}))
@@ -79,8 +78,8 @@ def build_edges_for_note(
payload = {
"chunk_id": cid,
# WP-24c v4.0.0: variant wird nur im Payload gespeichert (target_section), fließt nicht in die ID ein
"edge_id": _mk_edge_id(k, cid, t, "chunk"),
# WP-24c v4.1.0: target_section fließt nun fest in die ID-Generierung ein
"edge_id": _mk_edge_id(k, cid, t, "chunk", target_section=sec),
"provenance": "explicit", "rule_id": "inline:rel", "confidence": PROVENANCE_PRIORITY["inline:rel"]
}
if sec: payload["target_section"] = sec
@@ -92,10 +91,10 @@ def build_edges_for_note(
raw_t, k, p = cand.get("to"), cand.get("kind", "related_to"), cand.get("provenance", "semantic_ai")
t, sec = parse_link_target(raw_t, note_id)
if t:
# WP-24c v4.0.0: rule_id und variant werden nur im Payload gespeichert, fließen nicht in die ID ein
# WP-24c v4.1.0: target_section fließt nun fest in die ID-Generierung ein
payload = {
"chunk_id": cid,
"edge_id": _mk_edge_id(k, cid, t, "chunk"),
"edge_id": _mk_edge_id(k, cid, t, "chunk", target_section=sec),
"provenance": p, "rule_id": f"candidate:{p}", "confidence": PROVENANCE_PRIORITY.get(p, 0.90)
}
if sec: payload["target_section"] = sec
@@ -107,10 +106,10 @@ def build_edges_for_note(
t, sec = parse_link_target(raw_t, note_id)
if not t: continue
# WP-24c v4.0.0: rule_id und variant werden nur im Payload gespeichert, fließen nicht in die ID ein
# WP-24c v4.1.0: target_section fließt nun fest in die ID-Generierung ein
payload = {
"chunk_id": cid,
"edge_id": _mk_edge_id(k, cid, t, "chunk"),
"edge_id": _mk_edge_id(k, cid, t, "chunk", target_section=sec),
"provenance": "explicit", "rule_id": "callout:edge", "confidence": PROVENANCE_PRIORITY["callout:edge"]
}
if sec: payload["target_section"] = sec
@@ -122,10 +121,10 @@ def build_edges_for_note(
r, sec = parse_link_target(raw_r, note_id)
if not r: continue
# WP-24c v4.0.0: rule_id und variant werden nur im Payload gespeichert, fließen nicht in die ID ein
# WP-24c v4.1.0: target_section fließt nun fest in die ID-Generierung ein
payload = {
"chunk_id": cid, "ref_text": raw_r,
"edge_id": _mk_edge_id("references", cid, r, "chunk"),
"edge_id": _mk_edge_id("references", cid, r, "chunk", target_section=sec),
"provenance": "explicit", "rule_id": "explicit:wikilink", "confidence": PROVENANCE_PRIORITY["explicit:wikilink"]
}
if sec: payload["target_section"] = sec
@@ -134,10 +133,10 @@ def build_edges_for_note(
# Automatische Kanten-Vererbung aus types.yaml
for rel in defaults:
if rel != "references":
# WP-24c v4.0.0: rule_id und variant werden nur im Payload gespeichert, fließen nicht in die ID ein
# WP-24c v4.1.0: target_section fließt nun fest in die ID-Generierung ein
def_payload = {
"chunk_id": cid,
"edge_id": _mk_edge_id(rel, cid, r, "chunk"),
"edge_id": _mk_edge_id(rel, cid, r, "chunk", target_section=sec),
"provenance": "rule", "rule_id": f"edge_defaults:{rel}", "confidence": PROVENANCE_PRIORITY["edge_defaults"]
}
if sec: def_payload["target_section"] = sec
@@ -164,9 +163,9 @@ def build_edges_for_note(
}))
# 4) De-Duplizierung (In-Place)
# WP-24c v4.0.0: Da die EDGE-ID nur auf 4 Parametern basiert (kind, source, target, scope),
# werden Links auf unterschiedliche Abschnitte derselben Note durch die De-Duplizierung
# konsolidiert. Die Sektion-Information bleibt im Payload (target_section) erhalten.
# WP-24c v4.1.0: Da die EDGE-ID nun auf 5 Parametern basiert (inkl. target_section),
# bleiben Links auf unterschiedliche Abschnitte derselben Note als eigenständige
# Kanten erhalten. Nur identische Sektions-Links werden nach Confidence konsolidiert.
unique_map: Dict[str, dict] = {}
for e in edges:
eid = e["edge_id"]