Skip to content

fixed recursive nested tuple cycles assignations#497

Merged
yissellokta merged 1 commit intomainfrom
feat/tuple_cyle
Sep 19, 2025
Merged

fixed recursive nested tuple cycles assignations#497
yissellokta merged 1 commit intomainfrom
feat/tuple_cyle

Conversation

@yissellokta
Copy link
Copy Markdown
Contributor

@yissellokta yissellokta commented Sep 18, 2025

For the recursive nested tuples cycles model, some edges were missing the tuple cycle flag. This issue does not affect the weight of the node or the edge that are part of the tuple cycle. The weight is correctly calculated in the recursion or cycles, but the inclusion of all possible edges as part of the tuple cycle was missing some use cases.

Description

What problem is being solved?

For example for a model like this:

model
	schema 1.1
type user
type document
	relations
		define viewer: [team#member, org#employee]
type team
	relations
		define member: [user, document#viewer, org#employee]
type org
	relations
		define employee: [user, document#viewer, team#member]

All the edges were part of the tuple cycle except the last edge between document#viewer and the org#employee, and the reason was the org#employee was already declared as part of the tuple cycles and had a referential weight based on document#viewer. However, the edge between document#viewer and org@employee didn't detect a cycle because the node was already visited, and a cycle was already detected.

How is it being solved?

When fixing the referential weight in the presence of a cycle, if the node owning the reference id has the tuple cycles, all the dependent cycle edges need to be declared as a tuple cycle.

What changes are made to solve it?

References

Review Checklist

  • I have clicked on "allow edits by maintainers".
  • I have added documentation for new/changed functionality in this PR or in a PR to openfga.dev [Provide a link to any relevant PRs in the references section above]
  • The correct base branch is being used, if not main
  • I have added tests to validate that the change in functionality is working as expected

@yissellokta yissellokta requested a review from a team as a code owner September 18, 2025 19:46
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Sep 18, 2025

Walkthrough

The change propagates a node’s tupleCycle flag into dependent edges and nodes during weight-fixing in weighted_graph.go. Tests were added to validate nested and recursive tuple cycle scenarios, and one existing test expectation for a node’s tupleCycle flag was updated.

Changes

Cohort / File(s) Summary
Core graph weight propagation
pkg/go/graph/weighted_graph.go
Reads node.tupleCycle into a local variable in fixDependantEdgesWeight and fixDependantNodesWeight; after updating weights, sets edge.tupleCycle = true and fromNode.tupleCycle = true when the originating node’s tupleCycle is true, propagating tuple-cycle state downstream.
Extended cycle test coverage
pkg/go/graph/weighted_graph_builder_test.go
Adds three tests covering super-nested cycles, algebraic nested cycles, and recursion with nested cycles; asserts node/edge counts, weights (finite/Infinite), recursive relations, and tupleCycle flags on nodes/edges.
Adjusted existing test expectation
pkg/go/graph/weighted_graph_test.go
Flips expectation of graph.nodes["state-member-or-or"].tupleCycle from false to true in TestValidMixedRecursionWithTupleCycles.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant GB as GraphBuilder
  participant WG as WeightedGraph
  participant FEN as fixDependantNodesWeight
  participant FEE as fixDependantEdgesWeight
  participant N as Node
  participant E as Edge

  GB->>WG: build()
  WG->>FEN: adjust dependent node weights
  FEN->>N: read node.tupleCycle
  FEN->>N: update fromNode weights
  alt node.tupleCycle == true
    FEN->>N: set fromNode.tupleCycle = true
  end

  WG->>FEE: adjust dependent edge weights
  FEE->>N: read node.tupleCycle
  FEE->>E: update edge weights
  alt node.tupleCycle == true
    FEE->>E: set edge.tupleCycle = true
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • jpadilla
  • adriantam

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly describes the main intent of the change—fixing recursive nested tuple-cycle markings—and directly maps to the PR objectives and code edits that propagate tuple-cycle flags to dependent nodes and edges; it is concise and focused. The term "assignations" is unusual and may be a typo, but it does not make the title misleading about the core change.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/tuple_cyle

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (5)
pkg/go/graph/weighted_graph.go (2)

659-660: Tuple-cycle propagation to dependent edges — LGTM; tiny naming nit.

Behavior is correct and matches the PR intent. Consider renaming the local boolean to avoid confusion with the slice/term “tuple cycle”.

Apply:

-	tupleCycle := node.tupleCycle
+	isTupleCycle := node.tupleCycle
...
-		if tupleCycle {
+		if isTupleCycle {
 			edge.tupleCycle = true
 		}

Also applies to: 693-695


704-705: Tuple-cycle propagation to dependent from-nodes — LGTM; keep naming consistent.

Same nit as above for readability.

-	tupleCycle := node.tupleCycle
+	isTupleCycle := node.tupleCycle
...
-		if tupleCycle {
+		if isTupleCycle {
 			fromNode.tupleCycle = true
 		}

Also applies to: 726-728

pkg/go/graph/weighted_graph_builder_test.go (3)

796-853: Strong coverage for super‑nested cycles; add GetWeight ok assertions to harden tests.

Current checks ignore the boolean return; add it to fail fast on missing keys. Also, index‑based edge assertions are a bit order‑sensitive.

Example pattern to apply in this test (and similar spots):

-	weight, _ := graph.edges["document#viewer"][0].GetWeight("user")
+	weight, ok := graph.edges["document#viewer"][0].GetWeight("user")
+	require.True(t, ok)
 	require.True(t, graph.edges["document#viewer"][0].IsPartOfTupleCycle())
 	require.Equal(t, Infinite, weight)

855-929: Algebraic + nested cycles test looks good; minor robustness tweak.

Same suggestion: assert the ok flag from GetWeight; optional: prefer selecting edges by to.uniqueLabel over fixed indices to reduce brittleness.


931-1011: Typo in test name; rename for clarity.

Function name: “Recursionc” → “Recursion”. Also consider adding ok checks on GetWeight as above.

-func TestGraphConstructionRecursioncWithNestedCycles(t *testing.T) {
+func TestGraphConstructionRecursionWithNestedCycles(t *testing.T) {
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8168cf7 and 71e4cbf.

📒 Files selected for processing (3)
  • pkg/go/graph/weighted_graph.go (4 hunks)
  • pkg/go/graph/weighted_graph_builder_test.go (1 hunks)
  • pkg/go/graph/weighted_graph_test.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
pkg/go/graph/weighted_graph_builder_test.go (3)
pkg/go/transformer/dsltojson.go (1)
  • MustTransformDSLToProto (553-560)
pkg/go/graph/weighted_graph_builder.go (1)
  • NewWeightedAuthorizationModelGraphBuilder (19-21)
pkg/go/graph/weighted_graph.go (1)
  • Infinite (11-11)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
pkg/go/graph/weighted_graph_test.go (1)

1212-1212: Expectation flip to true is correct.

Matches the new tuple-cycle propagation to dependent nodes.

@yissellokta yissellokta added this pull request to the merge queue Sep 19, 2025
Merged via the queue into main with commit 5fb59a8 Sep 19, 2025
10 checks passed
@yissellokta yissellokta deleted the feat/tuple_cyle branch September 19, 2025 13:59
@coderabbitai coderabbitai bot mentioned this pull request Nov 13, 2025
4 tasks
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.

2 participants