From 9c38caf7484758ed6bcbeeead743718b244d591c Mon Sep 17 00:00:00 2001 From: Lan_zhijiang Date: Sat, 28 Nov 2026 22:44:32 +0800 Subject: [PATCH] =?UTF-8?q?fix(graph):=20=E5=8F=AA=E6=8A=8A=E6=9C=89?= =?UTF-8?q?=E6=95=88=E7=AB=AF=E7=82=B9=E8=AE=A1=E5=85=A5=20Block=20?= =?UTF-8?q?=E9=A2=84=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../graph-incomplete-relation-budget.fixed.md | 1 + .../graph_navigation_retrieval/main.py | 21 ++--- .../test_graph_navigation_legacy_endpoints.py | 79 +++++++++++++++++++ 3 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 .changes/graph-incomplete-relation-budget.fixed.md create mode 100644 tests/info_base/integration/test_graph_navigation_legacy_endpoints.py diff --git a/.changes/graph-incomplete-relation-budget.fixed.md b/.changes/graph-incomplete-relation-budget.fixed.md new file mode 100644 index 00000000..de66cab7 --- /dev/null +++ b/.changes/graph-incomplete-relation-budget.fixed.md @@ -0,0 +1 @@ +Graph paths and connected components ignore legacy Relations with missing endpoints when counting explored Blocks. diff --git a/inkcre_core/business/graph_navigation_retrieval/main.py b/inkcre_core/business/graph_navigation_retrieval/main.py index 1da4c943..88f0d566 100644 --- a/inkcre_core/business/graph_navigation_retrieval/main.py +++ b/inkcre_core/business/graph_navigation_retrieval/main.py @@ -193,7 +193,7 @@ async def get_connected_components( relation_id = relation.id seen_relations.add(relation_id) neighbor = relation.to_ if relation.from_ == current else relation.from_ - if neighbor in members: + if neighbor is None or neighbor in members: continue if ( neighbor not in explored_blocks @@ -371,17 +371,20 @@ def _relation_steps( direction: GraphDirection, reverse: bool, ) -> tuple[tuple[BlockID, BlockID], ...]: + source, target = relation.from_, relation.to_ + if source is None or target is None: + return () steps: list[tuple[BlockID, BlockID]] = [] if not reverse: - if direction in ("out", "both") and relation.from_ in frontier: - steps.append((relation.from_, relation.to_)) - if direction in ("in", "both") and relation.to_ in frontier: - steps.append((relation.to_, relation.from_)) + if direction in ("out", "both") and source in frontier: + steps.append((source, target)) + if direction in ("in", "both") and target in frontier: + steps.append((target, source)) else: - if direction in ("out", "both") and relation.to_ in frontier: - steps.append((relation.to_, relation.from_)) - if direction in ("in", "both") and relation.from_ in frontier: - steps.append((relation.from_, relation.to_)) + if direction in ("out", "both") and target in frontier: + steps.append((target, source)) + if direction in ("in", "both") and source in frontier: + steps.append((source, target)) return tuple(steps) @classmethod diff --git a/tests/info_base/integration/test_graph_navigation_legacy_endpoints.py b/tests/info_base/integration/test_graph_navigation_legacy_endpoints.py new file mode 100644 index 00000000..7359733f --- /dev/null +++ b/tests/info_base/integration/test_graph_navigation_legacy_endpoints.py @@ -0,0 +1,79 @@ +"""Graph traversal ignores persisted Relations with missing endpoints.""" + +import os +import uuid + +import pytest +from sqlalchemy import text + +from inkcre_core.business.graph_navigation_retrieval import GraphNavigationRetrievalManager +from inkcre_core.schemas.info_base.block import BlockForm +from tests.database import TestSession, seed_blocks, seed_relation + + +pytestmark = pytest.mark.skipif( + not os.getenv("INKCRE_TEST_DATABASE_URL"), + reason="requires an explicitly selected migrated PostgreSQL runtime", +) + + +@pytest.fixture +def graph_with_partial_relations(): + content = f"partial-relation-{uuid.uuid4()}" + with TestSession() as db, db.begin(): + start, end = seed_blocks( + ( + BlockForm(resolver="core.text.v1", content=f"{content}:start"), + BlockForm(resolver="core.text.v1", content=f"{content}:end"), + ), + db, + ) + assert start.id is not None and end.id is not None + path = seed_relation(start.id, end.id, content, db) + assert path.id is not None + db.connection().execute( + text( + "INSERT INTO inkcre.relations (from_, to_, content) VALUES " + "(:start, NULL, :content), (NULL, :end, :content)" + ), + {"start": start.id, "end": end.id, "content": content}, + ) + try: + yield start.id, end.id, path.id, content + finally: + with TestSession() as db, db.begin(): + db.connection().execute( + text("DELETE FROM inkcre.blocks WHERE id IN (:start, :end)"), + {"start": start.id, "end": end.id}, + ) + + +@pytest.mark.parametrize("direction", ["in", "out"]) +def test_path_budget_counts_real_blocks( + async_runner, graph_with_partial_relations, direction +): + start, end, relation, content = graph_with_partial_relations + if direction == "in": + start, end = end, start + result = async_runner.run( + GraphNavigationRetrievalManager.find_path( + start, end, direction=direction, contents=(content,), max_explored_blocks=2 + ) + ) + assert result.status == "found" + assert result.block_path == (start, end) + assert result.relation_path == (relation,) + + +def test_connected_components_exclude_incomplete_relations( + async_runner, graph_with_partial_relations +): + start, end, relation, content = graph_with_partial_relations + result = async_runner.run( + GraphNavigationRetrievalManager.get_connected_components( + (start, end), contents=(content,), max_explored_blocks=2 + ) + ) + assert not result.truncated + assert [item.member_block_ids for item in result.components] == [(start, end)] + assert [item.id for item in result.proof_graph.relations] == [relation]