Merged
Conversation
Reviewer's GuideThe PR enhances the run_command function in agi_cli.py by adding configurable retry logic: it updates the function signature, wraps the existing command dispatch in a retry loop, and improves error and retry messaging with delays between attempts. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Pull Request Overview
This PR adds retry logic to the AGI command execution functionality to improve reliability when commands fail due to temporary issues.
- Added retry parameters (
max_retriesandretry_delay) to therun_commandfunction - Implemented a retry loop that attempts command execution up to the specified maximum number of times
- Enhanced error handling to differentiate between retry attempts and final failures
Comments suppressed due to low confidence (4)
agi_cli.py:136
- The indentation is inconsistent. This line should be aligned with the 'except' statement above it, not indented further.
if attempt == max_retries:
agi_cli.py:137
- The indentation is inconsistent. This line should be aligned properly within the if block.
print(f"❌ Error executing command after {max_retries} attempts: {e}")
agi_cli.py:138
- The indentation is inconsistent. This line should be aligned properly within the if block.
return False
agi_cli.py:139
- The indentation is inconsistent. This line should be aligned with the 'if' statement above it.
print(f"⚠️ Attempt {attempt} failed: {e}. Retrying in {retry_delay} seconds...")
There was a problem hiding this comment.
Hey @Bryan-Roe - I've reviewed your changes and they look great!
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location> `agi_cli.py:88` </location>
<code_context>
-
- elif command == "help":
- result = """
+ for attempt in range(1, max_retries + 1):
+ try:
+ if command == "reason":
</code_context>
<issue_to_address>
Retry logic will always retry on any Exception, including KeyboardInterrupt.
Consider catching only specific exceptions or re-raising critical ones like KeyboardInterrupt and SystemExit to avoid suppressing important signals.
</issue_to_address>
### Comment 2
<location> `agi_cli.py:79` </location>
<code_context>
-async def run_command(command: str, *args):
- """Run an AGI command"""
+async def run_command(
+ command: str,
+ *args,
</code_context>
<issue_to_address>
Consider refactoring by extracting retry logic into a decorator, moving help text to a constant, and using a command handler map to simplify dispatch.
```suggestion
# 1) Move the help text to a module‐level constant
HELP_TEXT = """
🤖 AGI Command Line Interface
Available commands:
reason <query> - Perform AGI reasoning on a query
file <filename> [operation] - Process file (analyze, optimize, transform, summarize)
code <language> <description> - Generate code in specified language
plan <goal> - Create and execute a plan for a goal
help - Show this help message
Examples:
python agi_cli.py reason "How does machine learning work?"
python agi_cli.py file myfile.txt analyze
python agi_cli.py code python "Create a web scraper"
python agi_cli.py plan "Build a recommendation system"
"""
# 2) Extract retry logic into a decorator
def retry_async(max_retries: int = 3, retry_delay: float = 1.0):
def decorator(fn):
async def wrapper(*args, **kwargs):
for attempt in range(1, max_retries + 1):
try:
return await fn(*args, **kwargs)
except Exception as e:
if attempt == max_retries:
print(f"❌ Error after {max_retries} attempts: {e}")
return False
print(f"⚠️ Attempt {attempt} failed: {e}. Retrying in {retry_delay} seconds…")
await asyncio.sleep(retry_delay)
return wrapper
return decorator
# 3) Define per‐command handlers, then dispatch via a map
async def _handle_reason(agi, args):
query = " ".join(args) if args else "What is artificial general intelligence?"
fn = agi.kernel.get_function("agi_cli", "reason")
return await fn.invoke(agi.kernel, query=query)
# … similarly define _handle_file, _handle_code, _handle_plan …
COMMAND_HANDLERS = {
"reason": _handle_reason,
"file": _handle_file,
"code": _handle_code,
"plan": _handle_plan,
"help": lambda agi, args: HELP_TEXT,
}
# 4) Simplify run_command
@retry_async()
async def run_command(command: str, *args) -> bool:
agi = AGICommandLine()
handler = COMMAND_HANDLERS.get(command)
if not handler:
print(f"❌ Unknown command: {command}. Use 'help' for available commands.")
return False
result = await handler(agi, args)
print(result)
return True
```
This keeps the same retry behavior, removes the nested if/elif chain, and relocates help text to a constant for clarity.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com> Signed-off-by: Bryan <74067792+Bryan-Roe@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Testing
python -m py_compile agi_cli.pypytest -k "" -q(fails: SystemExit 1)https://chatgpt.com/codex/tasks/task_e_6886a6eb30548322a9496ade2b8f5543
Summary by Sourcery
Introduce retry mechanisms to the AGI CLI command executor to improve resilience against transient failures.
New Features: