Improve error handling for Ctrl+C/Exit/Error in devika.py and Bug fixes #475
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.
Description
Changes in devika.py
A new import statement has been added: from contextlib import ExitStack. This suggests that the code is using the ExitStack context manager to manage resources, such as signal handlers. A new function handle_error has been added. This function appears to handle errors and Ctrl+C interruptions by setting an agent's state to inactive and completed. It also exits the program with a non-zero code to indicate an error. The handle_error function is registered as a signal handler for SIGINT (Ctrl+C) using the signal module and ExitStack.
Changes in runner.py
The validate_response and validate_rerunner_response methods have been modified to extract JSON data from the response string. The old implementation used strip() and replace() to remove unwanted characters, whereas the new implementation uses index() and rindex() to find the start and end of the JSON data. The JSON data is now extracted using slicing (response[start:end]) and loaded into a Python dictionary using json.loads().
The subprocess module is now used with Popen instead of run. This change allows for more flexibility in handling the subprocess output. The universal_newlines=True parameter has been added to the Popen constructor. This allows the output to be captured as strings instead of bytes. The communicate() method is now used to capture the output of the subprocess, instead of stdout.decode('utf-8'). Impact of changes
The changes in devika.py appear to improve the handling of errors and Ctrl+C interruptions, making the program more robust.
The changes in runner.py seem to improve the parsing of JSON data from responses and the handling of subprocess output.
The use of Popen instead of run allows for more flexibility in handling the output, and the universal_newlines=True parameter makes it easier to work with string output.
Issues Fixed
Allows the user to view the Error in UI Terminal before devika starts prompting for fixes.
Updates Agent State to inactive after error or before exit (Bug: Espically when Terminal runs a webapp, there is no way to terminate the process, terminating devika causes the agent to freeze in "active" state.
No more Invalid Json from Llama 3 due to text outside Json Blocks. (ex. Here is the formatted Json Block { ... })