Implement automatic backlinks for intra-note edges (WP-26 v1.4)

- Added functionality to automatically create inverse backlinks for intra-note edges at the chunk level, ensuring that backlinks are generated only when they do not already exist.
- Updated the documentation to outline the requirements and rules for backlink creation, including conditions for deduplication and scope.
- Introduced unit tests to validate the creation of backlinks and ensure correct behavior when existing backlinks are present.
- Incremented version to 4.4.0 to reflect the new feature addition.
This commit is contained in:
Lars committed 2026-01-26 11:16:40 +01:00
1 parent bc8bdfac3c
commit 52ed079067
3 files changed
+253 -2

No files matched your search

+63 -1
View File
@@ -24,7 +24,7 @@ DESCRIPTION: Hauptlogik zur Kanten-Aggregation und De-Duplizierung.
- Chunk-Scope gewinnt zwingend über Note-Scope (außer explicit:note_zone)
- Confidence-Werte: candidate_pool explicit:callout = 1.0, globaler Scan = 0.7
- Key-Generierung gehärtet für konsistente Deduplizierung
VERSION: 4.3.1 (WP-24c: Präzisions-Priorität)
VERSION: 4.4.0 (WP-26 v1.4: Automatische Backlinks für Intra-Note-Edges)
STATUS: Active
"""
import re
@@ -35,6 +35,11 @@ from .graph_utils import (
PROVENANCE_PRIORITY, load_types_registry, get_edge_defaults_for,
get_typical_edge_for # WP-26 v1.1: Für automatische Intra-Note-Edges
)
# WP-26 v1.4: Für automatische Backlinks bei Intra-Note-Edges
try:
from app.services.edge_registry import registry as edge_registry
except ImportError:
edge_registry = None
from .graph_extractors import (
extract_typed_relations, extract_callout_relations, extract_wikilinks
)
@@ -1052,4 +1057,61 @@ def build_edges_for_note(
final_edges.append(winner)
# WP-26 v1.4: Automatische Backlinks für Intra-Note-Edges (Chunk-Level)
# Erstelle inverse Edges für alle Intra-Note-Edges, wenn noch nicht vorhanden
if edge_registry:
# Erstelle Set aller existierenden Edge-Keys für schnelle Lookup
existing_edge_keys: Set[Tuple[str, str, str, Optional[str]]] = set()
for e in final_edges:
source = e.get("source_id", "")
target = e.get("target_id", "")
kind = e.get("kind", "")
target_section = e.get("target_section")
existing_edge_keys.add((source, target, kind, target_section))
# Durchlaufe alle Edges und erstelle Backlinks für Intra-Note-Edges
backlinks_to_add: List[dict] = []
for e in final_edges:
is_internal = e.get("is_internal", False)
scope = e.get("scope", "chunk")
source_id = e.get("source_id", "")
target_id = e.get("target_id", "")
kind = e.get("kind", "")
target_section = e.get("target_section")
# Nur Intra-Note-Edges auf Chunk-Level berücksichtigen
if not is_internal or scope != "chunk":
continue
# Prüfe, ob bereits ein inverser Edge existiert
inv_kind = edge_registry.get_inverse(kind) if edge_registry else None
if not inv_kind:
continue # Kein inverser Edge-Type verfügbar
# Prüfe, ob inverser Edge bereits existiert
inv_key = (target_id, source_id, inv_kind, None) # Backlink hat keine target_section
if inv_key in existing_edge_keys:
continue # Backlink bereits vorhanden
# Erstelle automatischen Backlink
backlink_edge = _edge(inv_kind, "chunk", target_id, source_id, note_id, {
"chunk_id": target_id, # Backlink geht vom Target-Chunk aus
"edge_id": _mk_edge_id(inv_kind, target_id, source_id, "chunk"),
"provenance": "rule",
"rule_id": "derived:intra_note_backlink",
"source_hint": "automatic_backlink",
"confidence": PROVENANCE_PRIORITY.get("derived:backlink", 0.8),
"is_internal": True,
"original_edge_kind": kind # Debug-Info: Welcher Edge-Type wurde invertiert
})
backlinks_to_add.append(backlink_edge)
existing_edge_keys.add(inv_key) # Verhindere Duplikate
logger.debug(f"WP-26 Backlink erstellt: {target_id} --[{inv_kind}]--> {source_id} (Original: {kind})")
# Füge Backlinks zu final_edges hinzu
if backlinks_to_add:
final_edges.extend(backlinks_to_add)
logger.info(f"WP-26: {len(backlinks_to_add)} automatische Backlinks für Intra-Note-Edges erstellt")
return final_edges