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

+98 -1
View File
@@ -3,7 +3,7 @@ FILE: tests/test_wp26_section_types.py
DESCRIPTION: Unit-Tests für WP-26 Phase 1: Section-Types und Intra-Note-Edges
WP-26 v1.1: Erweitert um Tests für Section-Split und automatische Edges
WP-26 v1.3: Erweitert um Tests für rückwirkende section_type Propagation
VERSION: 1.3.0
VERSION: 1.4.0 (WP-26 v1.4: Automatische Backlinks)
"""
import pytest
from app.core.chunking.chunking_parser import parse_blocks
@@ -624,5 +624,102 @@ class TestBlockIdParsing:
assert section == "Normale Überschrift"
class TestAutomaticBacklinks:
"""UT-18: Automatische Backlinks für Intra-Note-Edges (WP-26 v1.4)"""
def test_backlink_created_for_intra_note_edge(self):
"""Backlink wird automatisch für Intra-Note-Edge erstellt"""
from app.core.graph.graph_derive_edges import build_edges_for_note
# Mock-Chunks mit Section-Types
chunks = [
{
"chunk_id": "note1#c01",
"type": "experience",
"section_type": "experience",
"window": "Situation text"
},
{
"chunk_id": "note1#c02",
"type": "insight",
"section_type": "insight",
"window": "Reflexion text"
}
]
edges = build_edges_for_note(
note_id="note1",
chunks=chunks,
note_level_references=None,
include_note_scope_refs=False,
markdown_body=""
)
# Prüfe, dass sowohl Forward-Edge als auch Backlink vorhanden sind
forward_edges = [e for e in edges if e.get("source_id") == "note1#c01" and e.get("target_id") == "note1#c02"]
backlink_edges = [e for e in edges if e.get("source_id") == "note1#c02" and e.get("target_id") == "note1#c01"]
assert len(forward_edges) > 0, "Forward-Edge sollte vorhanden sein"
assert len(backlink_edges) > 0, "Backlink sollte automatisch erstellt werden"
# Prüfe Backlink-Eigenschaften
backlink = backlink_edges[0]
assert backlink.get("is_internal") is True
assert backlink.get("scope") == "chunk"
assert backlink.get("provenance") == "rule"
assert backlink.get("rule_id") == "derived:intra_note_backlink"
def test_backlink_not_created_if_already_exists(self):
"""Backlink wird nicht erstellt, wenn bereits ein inverser Edge existiert"""
from app.core.graph.graph_derive_edges import build_edges_for_note
# Mock-Chunks
chunks = [
{
"chunk_id": "note1#c01",
"type": "experience",
"section_type": "experience",
"window": "Situation text",
"candidate_pool": [
{
"kind": "derived_from", # Inverser Edge-Type
"to": "note1#c02",
"provenance": "explicit:callout"
}
]
},
{
"chunk_id": "note1#c02",
"type": "insight",
"section_type": "insight",
"window": "Reflexion text",
"candidate_pool": [
{
"kind": "derives", # Forward-Edge
"to": "note1#c01",
"provenance": "explicit:callout"
}
]
}
]
edges = build_edges_for_note(
note_id="note1",
chunks=chunks,
note_level_references=None,
include_note_scope_refs=False,
markdown_body=""
)
# Zähle Backlinks (sollte nicht dupliziert werden)
backlink_edges = [e for e in edges
if e.get("source_id") == "note1#c01"
and e.get("target_id") == "note1#c02"
and e.get("kind") == "derived_from"]
# Sollte genau einen Backlink geben (der explizite, nicht zusätzlich automatischer)
assert len(backlink_edges) == 1, f"Erwartet genau einen Backlink, gefunden: {len(backlink_edges)}"
if __name__ == "__main__":
pytest.main([__file__, "-v"])