Skip to content

Add cycle detection on transitive relationship types#40

Open
anormang1992 wants to merge 1 commit intomainfrom
fix/adds-cycle-detection-in-graph-repo
Open

Add cycle detection on transitive relationship types#40
anormang1992 wants to merge 1 commit intomainfrom
fix/adds-cycle-detection-in-graph-repo

Conversation

@anormang1992
Copy link
Owner

Summary

  • Adds cycle detection to save_primitive() that prevents edges on transitive relation types (REQUIRES, DEPENDS_ON, CONSTRAINED_BY) from forming cycles, which would cause unbounded traversal in resolve_subgraph
  • Learning engine catches CyclicRelationshipError and returns SKIPPED so the learning loop continues to the next gap
  • Centralizes the transitive type set as TRANSITIVE_RELATION_TYPES in models.py, replacing the private _TRANSITIVE_RELS string list in graph.py
  • Removes dead target refetch in _persist_reachability

Closes #33

Test plan

  • 194 tests pass, zero regressions
  • 9 new tests covering: self-referential edges, direct cycles (A→B→A), indirect cycles (A→B→C→A), mixed transitive types, non-transitive cycles allowed (APPLIES_TO, INCLUDES), valid transitive edges accepted
  • StubRepository BFS cycle detection mirrors Neo4j Cypher behavior
  • Manual verification against live Neo4j instance (Cypher reachability query)

🤖 Generated with Claude Code

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds cycle prevention for transitive relationship edges so resolve_subgraph’s unbounded traversal can’t be corrupted by cycles, and updates the learning loop to skip candidates that would introduce such cycles.

Changes:

  • Introduces TRANSITIVE_RELATION_TYPES and CyclicRelationshipError in core.models, exporting them via package __init__ modules.
  • Adds transitive-cycle detection to Neo4j PrimitiveRepository.save_primitive() and updates the learning engine to treat cycle attempts as SKIPPED.
  • Adds/updates learning tests and enhances the test StubRepository with BFS-based cycle checks.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/vre/test_learning.py Adds cycle-detection tests and implements matching cycle checks in the in-memory test repository.
src/vre/learning/engine.py Catches CyclicRelationshipError during reachability persistence and returns SKIPPED; removes dead target refetch.
src/vre/core/models.py Centralizes transitive relation types and defines a dedicated cycle error type.
src/vre/core/graph.py Adds Neo4j-side cycle detection in save_primitive() using a reachability query; derives transitive type list from the centralized set.
src/vre/core/init.py Re-exports CyclicRelationshipError and TRANSITIVE_RELATION_TYPES.
src/vre/init.py Exposes CyclicRelationshipError at the top-level public API and updates parameter docs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Prevent cyclical edges on transitive relation types (REQUIRES, DEPENDS_ON,
CONSTRAINED_BY) which would cause unbounded traversal in resolve_subgraph.

Cycle check runs inside the save_primitive transaction after edge deletion
but before creation — Neo4j rolls back atomically if a cycle is detected.
The learning engine catches CyclicRelationshipError and returns SKIPPED so
the loop continues to the next gap.

Also removes dead target refetch in _persist_reachability and centralizes
the transitive type set as TRANSITIVE_RELATION_TYPES in models.py.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@anormang1992 anormang1992 force-pushed the fix/adds-cycle-detection-in-graph-repo branch from c25a2e7 to 42965d6 Compare March 24, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation on edge placement to detect possible cyclical relationships

2 participants