Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect cycle in cli custom commands #765

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft

Conversation

pkbhowmick
Copy link
Collaborator

@pkbhowmick pkbhowmick commented Nov 5, 2024

what

why

references

Summary by CodeRabbit

  • New Features

    • Introduced a cycle detection mechanism for custom CLI commands to prevent command dependency cycles.
  • Bug Fixes

    • Enhanced error handling for command processing by validating command dependencies before execution.
  • Documentation

    • Updated comments to clarify the purpose and functionality of the new cycle detection logic.

Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
@osterman
Copy link
Member

osterman commented Nov 5, 2024

This is not wrong, but we still need to catch when atmos is executing atmos in a free form command. Walking the tree won't help with that.

The only practical way is to set an environment variable for the sub command and increment it. We should allow some level of recursion, just not infinite.

Copy link
Contributor

coderabbitai bot commented Nov 5, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes introduce a cycle detection mechanism for custom CLI commands in the cmd/cmd_utils.go file. A new function, detectCycle, is implemented to construct a command dependency graph and identify cycles using depth-first search (DFS). The processCustomCommands function is updated to call detectCycle, ensuring that command processing halts if a cycle is detected. Additionally, comments have been updated for clarity regarding the new functionality.

Changes

File Change Summary
cmd/cmd_utils.go - Added detectCycle(commands []schema.Command) bool for cycle detection.
- Added detectCycleUtil(command string, graph map[string][]string, visited, recStack map[string]bool) bool for DFS cycle detection.
- Added parseCommandName(step string) string to extract command names.
- Modified processCustomCommands to incorporate cycle detection and return an error if a cycle is found.
- Updated comments for clarity on new cycle detection logic.

Assessment against linked issues

Objective Addressed Explanation
Detect cycles in Atmos commands to prevent recursive calls (DEV-2688)

Possibly related issues

  • DEV-2688: The changes directly address the need to detect cycles in Atmos commands, aligning with the objectives outlined in the issue.

Possibly related PRs

Suggested labels

minor

Suggested reviewers

  • osterman
  • johncblandii
  • gberenice
  • aknysh

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@pkbhowmick pkbhowmick marked this pull request as draft November 5, 2024 19:23
Copy link
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: 3

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4098092 and 91ad5e6.

📒 Files selected for processing (1)
  • cmd/cmd_utils.go (2 hunks)

Comment on lines +36 to +57
func detectCycle(commands []schema.Command) bool {
// Build a command graph
graph := make(map[string][]string)
for _, cmd := range commands {
for _, step := range cmd.Steps {
// Add an edge from command to each command it depends on
graph[cmd.Name] = append(graph[cmd.Name], parseCommandName(step))
}
}

// To track visited nodes and detect cycles
visited := make(map[string]bool)
recStack := make(map[string]bool)

// Run DFS for each command to detect cycles
for cmd := range graph {
if detectCycleUtil(cmd, graph, visited, recStack) {
return true // Cycle detected
}
}
return false // No cycle detected
}
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 5, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider adding validation for command existence and depth limiting

The cycle detection implementation is solid, but could be enhanced in a few ways:

  1. Validate that all commands referenced in steps exist in the command list
  2. Add a maximum depth limit as mentioned in the requirements

Here's a suggested enhancement:

 func detectCycle(commands []schema.Command) bool {
+    // Create a map of valid commands for quick lookup
+    validCommands := make(map[string]bool)
+    for _, cmd := range commands {
+        validCommands[cmd.Name] = true
+    }
+
     // Build a command graph
     graph := make(map[string][]string)
     for _, cmd := range commands {
         for _, step := range cmd.Steps {
-            graph[cmd.Name] = append(graph[cmd.Name], parseCommandName(step))
+            cmdName := parseCommandName(step)
+            if cmdName != "" && !validCommands[cmdName] {
+                return true  // Invalid command reference creates a broken chain
+            }
+            graph[cmd.Name] = append(graph[cmd.Name], cmdName)
         }
     }

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member

Choose a reason for hiding this comment

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

Seems like a good recommendation

Copy link
Contributor

Choose a reason for hiding this comment

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

@osterman, here's an updated detectCycle function incorporating command existence validation and depth limiting:

func detectCycle(commands []schema.Command, maxDepth int) bool {
    // Create a map of valid commands for quick lookup
    validCommands := make(map[string]bool)
    for _, cmd := range commands {
        validCommands[cmd.Name] = true
    }

    // Build a command graph
    graph := make(map[string][]string)
    for _, cmd := range commands {
        for _, step := range cmd.Steps {
            cmdName := parseCommandName(step)
            if cmdName != "" {
                if !validCommands[cmdName] {
                    return true // Invalid command reference creates a broken chain
                }
                graph[cmd.Name] = append(graph[cmd.Name], cmdName)
            }
        }
    }

    // To track visited nodes and detect cycles
    visited := make(map[string]bool)
    recStack := make(map[string]bool)

    // Run DFS for each command to detect cycles with depth limit
    for cmd := range graph {
        if detectCycleUtil(cmd, graph, visited, recStack, 0, maxDepth) {
            return true // Cycle detected or depth limit exceeded
        }
    }
    return false // No cycle detected
}

func detectCycleUtil(command string, graph map[string][]string, visited, recStack map[string]bool, depth, maxDepth int) bool {
    if depth > maxDepth {
        return true // Depth limit exceeded
    }

    // If the current command is in the recursion stack, there's a cycle
    if recStack[command] {
        return true
    }

    // If already visited, no need to explore again
    if visited[command] {
        return false
    }

    // Mark as visited and add to recursion stack
    visited[command] = true
    recStack[command] = true

    // Recurse for all dependencies
    for _, dep := range graph[command] {
        if detectCycleUtil(dep, graph, visited, recStack, depth+1, maxDepth) {
            return true
        }
    }

    // Remove from recursion stack before backtracking
    recStack[command] = false
    return false
}

You'll need to define an appropriate maxDepth when calling detectCycle to enforce the recursion limit.

Comment on lines +86 to +97
// Helper function to parse command name from the step
func parseCommandName(step string) string {
// Split the step into parts
parts := strings.Split(step, " ")

// Check if the command starts with "atmos" and has additional parts
if len(parts) > 1 && parts[0] == "atmos" {
// Return everything after "atmos" as a single string
return strings.Join(parts[1:], " ")
}
return ""
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance command parsing robustness

The current parsing logic might miss cycles in complex command formats and could mask issues by returning empty strings.

Consider this enhanced implementation:

 func parseCommandName(step string) string {
-    // Split the step into parts
     parts := strings.Split(step, " ")
 
-    // Check if the command starts with "atmos" and has additional parts
     if len(parts) > 1 && parts[0] == "atmos" {
-        // Return everything after "atmos" as a single string
-        return strings.Join(parts[1:], " ")
+        // Extract the actual command name, handling flags and arguments
+        cmdParts := []string{}
+        for _, part := range parts[1:] {
+            if strings.HasPrefix(part, "-") {
+                break  // Stop at first flag
+            }
+            cmdParts = append(cmdParts, part)
+        }
+        return strings.Join(cmdParts, " ")
     }
     return ""
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Helper function to parse command name from the step
func parseCommandName(step string) string {
// Split the step into parts
parts := strings.Split(step, " ")
// Check if the command starts with "atmos" and has additional parts
if len(parts) > 1 && parts[0] == "atmos" {
// Return everything after "atmos" as a single string
return strings.Join(parts[1:], " ")
}
return ""
}
// Helper function to parse command name from the step
func parseCommandName(step string) string {
parts := strings.Split(step, " ")
if len(parts) > 1 && parts[0] == "atmos" {
// Extract the actual command name, handling flags and arguments
cmdParts := []string{}
for _, part := range parts[1:] {
if strings.HasPrefix(part, "-") {
break // Stop at first flag
}
cmdParts = append(cmdParts, part)
}
return strings.Join(cmdParts, " ")
}
return ""
}

Comment on lines +113 to +115
if detectCycle(commands) {
return errors.New("cycle detected in custom cli commands")
}
Copy link
Contributor

@coderabbitai coderabbitai bot Nov 5, 2024

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance error reporting and implement environment variable tracking

The current implementation could provide more helpful error messages and implement the environment variable tracking suggested in the PR comments.

Consider this enhancement:

+const AtmosCommandDepthEnv = "ATMOS_COMMAND_DEPTH"
+
+func getCommandDepth() int {
+    depth := os.Getenv(AtmosCommandDepthEnv)
+    if depth == "" {
+        return 0
+    }
+    val, _ := strconv.Atoi(depth)
+    return val
+}
+
 if detectCycle(commands) {
-    return errors.New("cycle detected in custom cli commands")
+    return fmt.Errorf("cycle detected in custom CLI commands - this could lead to infinite recursion")
+}
+
+// Implement depth tracking as suggested in PR comments
+depth := getCommandDepth()
+if depth > 10 {  // Configure max depth as needed
+    return fmt.Errorf("maximum command depth exceeded (%d) - possible recursive loop", depth)
 }
+os.Setenv(AtmosCommandDepthEnv, strconv.Itoa(depth+1))

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member

Choose a reason for hiding this comment

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

@pkbhowmick something like this 👆

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@pkbhowmick something like this 👆

got it. Thank you

@pkbhowmick
Copy link
Collaborator Author

This is not wrong, but we still need to catch when atmos is executing atmos in a free form command. Walking the tree won't help with that.

The only practical way is to set an environment variable for the sub command and increment it. We should allow some level of recursion, just not infinite.

Thanks for the info @osterman . I'll change accordingly

@osterman osterman added the minor New features that do not break anything label Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor New features that do not break anything
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants