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 12 commits into
base: main
Choose a base branch
from
Draft

detect cycle in cli custom commands #765

wants to merge 12 commits into from

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
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 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)

cmd/cmd_utils.go Outdated Show resolved Hide resolved
cmd/cmd_utils.go Outdated Show resolved Hide resolved
cmd/cmd_utils.go Outdated Show resolved Hide resolved
@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
@pkbhowmick
Copy link
Collaborator Author

pkbhowmick commented Nov 23, 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.

Hi @osterman
if there is a cycle then the recursion will never end. i.e. do you want to allow cycle for some iteration?

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

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.

Hi @osterman if there is a cycle then the recursion will never end. i.e. do you want to allow cycle for some iteration?

Only if we know conclusively, without a doubt, that it will result in infinite recursion. The problem I foresee is it we cannot easily determine anything conclusively.

That is my way of saying, we probably need to allow for some recursion before triggering the circuit breaker.

Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
coderabbitai[bot]
coderabbitai bot previously approved these changes Nov 24, 2024
Signed-off-by: Pulak Kanti Bhowmick <[email protected]>
@pkbhowmick
Copy link
Collaborator Author

image

@pkbhowmick
Copy link
Collaborator Author

pkbhowmick commented Nov 24, 2024

I might be wrong but I have found that from code the command is run once. When there is a cycle and the command reaches to shell. Shell automatically runs that cyclic command. I have found no way to control it from code.

That's why I am proposing a way to let the user know about the cycle and run/exit command based on user input.

cc: @osterman

@osterman
Copy link
Member

I might be wrong but I have found that from code the command is run once. When there is a cycle and the command reaches to shell. Shell automatically runs that cyclic command. I have found no way to control it from code.

Please provide more technical details. This is conventional behavior of task runner functionality, and not something novel we are attempting to implement.

@osterman
Copy link
Member

As far as I can tell, this implementation never sets an environment variable, and that environment variable, therefore never passed to the recursive shell. Therefore, there is no circuit breaker. This is attempting strictly to implement a circuit breaker using the YAML definition, which is going be very complicated please implement the circuit breaker using an environment variable

@osterman
Copy link
Member

osterman commented Nov 24, 2024

It is similar in principle to what we have going on in this other Pool request to send the Shell level. The difference is that at a certain depth we should fatally exit.

@pkbhowmick
Copy link
Collaborator Author

I might be wrong but I have found that from code the command is run once. When there is a cycle and the command reaches to shell. Shell automatically runs that cyclic command. I have found no way to control it from code.

Please provide more technical details. This is conventional behavior of task runner functionality, and not something novel we are attempting to implement.

Hi @osterman
In my finding, when the command runs for the first time, the shell has the full control not the code. But with that env it seems we might have some control. Let me take a look again

@pkbhowmick
Copy link
Collaborator Author

It is similar in principle to what we have going on in this other Pool request to send the Shell level. The difference is that at a certain depth we should fatally exit.

While trying this way, I have found we are submitting all the steps to a shell runner. https://github.com/cloudposse/atmos/blob/main/internal/exec/shell_utils.go#L77-L93

Now if we want to control the way of execution of cyclic custom command, we need to do something here. So far I don't find a way

@osterman
Copy link
Member

Simply check the ATMOS_SHLVL is not greater than some maximum (e.g. 10). If it is greater log a fatal error. Otherwise, increment the ATMOS_SHLVL envar before execution, and ensure the envar is passed to the command.

If this doesn't work, then submit an example of why not, or there's no feedback that can be given.

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