sweepai / sweep

Sweep: open-source AI-powered Software Developer for small features and bug fixes.
https://sweep.dev
Other
7.45k stars 427 forks source link

Sweep: Add traceback to error logs in sweepai/utils/search_and_replace.py #1859

Open sweep-nightly[bot] opened 1 year ago

sweep-nightly[bot] commented 1 year ago

In the file sweepai/utils/search_and_replace.py, the logger is used to log exceptions, but traceback is not used. This makes it harder to debug the code because the stack trace is not available in the logs.

For example, in the get_files_to_change function, exceptions are caught and logged without traceback:

except RegexMatchError as e:
    logger.print(e)
    logger.warning("Failed to parse! Retrying...")
    self.delete_messages_from_chat("files_to_change")
    self.delete_messages_from_chat("pr_diffs")

To fix this issue, import the traceback module and use traceback.format_exc() to get the stack trace. Then, include the stack trace in the log message:

import traceback

# ...

except RegexMatchError:
    logger.warning(f"Failed to parse! Retrying...\n{traceback.format_exc()}")
    self.delete_messages_from_chat("files_to_change")
    self.delete_messages_from_chat("pr_diffs")

Please apply this change to all places in the code where exceptions are logged.

Checklist - [X] `sweepai/utils/search_and_replace.py` βœ… Commit [`2f08043`](https://github.com/sweepai/sweep/commit/2f08043a0697bdad7fed2f8c6aab260f132335c3) - [X] `sweepai/core/sweep_bot.py` βœ… Commit [`455d4c0`](https://github.com/sweepai/sweep/commit/455d4c0e4e1f8339fecfd4a5ef1f1cbd4bf45d55) - [X] `sweepai/core/lexical_search.py` βœ… Commit [`f407ea5`](https://github.com/sweepai/sweep/commit/f407ea57b107f297b9e690cb0ba394b6d1943174)
sweep-nightly[bot] commented 1 year ago

Here's the PR! https://github.com/sweepai/sweep/pull/1861.

πŸ’Ž Sweep Pro: I'm creating this ticket using GPT-4. You have unlimited GPT-4 tickets.

Actions (click)


Step 1: πŸ”Ž Searching

I found the following snippets in your repository. I will now analyze these snippets and come up with a plan.

Some code snippets I looked at (click to expand). If some file is missing from here, you can mention the path in the ticket description. https://github.com/sweepai/sweep/blob/945ea441eb2681a8fa55f697de647a7b3eedb34e/sweepai/utils/search_and_replace.py#L191-L662

Step 2: ⌨️ Coding

In the file sweepai/utils/search_and_replace.py, the get_files_to_change function needs to be modified to include traceback in the log messages. This can be done by importing the traceback module at the beginning of the file and then using traceback.format_exc() to get the stack trace. The stack trace should then be included in the log message.

The relevant snippet from file_path in the format file_path:start_idx-end_idx is sweepai/utils/search_and_replace.py:610-615. This is the part of the code where the exception is caught and logged without traceback.

The code should be modified as follows:

import traceback

# ...

except RegexMatchError as e:
    logger.warning(f"Failed to parse! Retrying...\n{traceback.format_exc()}")
    self.delete_messages_from_chat("files_to_change")
    self.delete_messages_from_chat("pr_diffs")
```</blockquote>

- [X] `sweepai/core/sweep_bot.py` βœ… Commit [`455d4c0`](https://github.com/sweepai/sweep/commit/455d4c0e4e1f8339fecfd4a5ef1f1cbd4bf45d55)
<blockquote>The changes are constrained to the file_path and code mentioned in sweepai/core/sweep_bot.py. 

In the function `generate_subissues`, the exception handling code needs to be modified to include traceback. The relevant snippet is sweepai/core/sweep_bot.py:213-216. The code should be changed to:

```python
except RegexMatchError:
    logger.warning(f"Failed to parse! Retrying...\n{traceback.format_exc()}")
    self.delete_messages_from_chat("files_to_change")

Similarly, in the function get_files_to_change, the exception handling code needs to be modified to include traceback. The relevant snippet is sweepai/core/sweep_bot.py:392-395. The code should be changed to:

except RegexMatchError:
    logger.warning(f"Failed to parse! Retrying...\n{traceback.format_exc()}")
    self.delete_messages_from_chat("files_to_change")
    self.delete_messages_from_chat("pr_diffs")

In both cases, the traceback module needs to be imported at the beginning of the file.

In the file sweepai/core/lexical_search.py, there are two places where exceptions are caught and logged without traceback. These are in the functions prepare_index_from_docs and search_index.

In the prepare_index_from_docs function, the relevant code is:

try:
    writer.commit()
except SystemExit:
    raise SystemExit
except Exception as e:
    logger.error(e)

This should be changed to:

import traceback

try:
    writer.commit()
except SystemExit:
    raise SystemExit
except Exception:
    logger.error(f"Error occurred:\n{traceback.format_exc()}")

In the search_index function, the relevant code is:

except SystemExit:
    raise SystemExit
except Exception as e:
    logger.print(e)
    traceback.print_exc()

This should be changed to:


import traceback

except SystemExit:
    raise SystemExit
except Exception:
    logger.print(f"Error occurred:\n{traceback.format_exc()}")
```</blockquote>

---
## Step 3: πŸ” Code Review
I have finished reviewing the code for completeness. I did not find errors for  [`sweep/add-traceback-to-error-logs_10`](https://github.com/sweepai/sweep/commits/sweep/add-traceback-to-error-logs_10).

.

---

πŸŽ‰ Latest improvements to Sweep:

* Getting Sweep to run linters before committing! Check out [Sweep Sandbox Configs](https://docs.sweep.dev/usage/config) to set it up.
* Added support for self-hosting! Check out [Self-hosting Sweep](https://docs.sweep.dev/deployment) to get started.
* [Self Hosting] Multiple options to compute vector embeddings, configure your .env file using [VECTOR_EMBEDDING_SOURCE](https://github.com/sweepai/sweep/blob/main/sweepai/config/server.py#L144)

---
 πŸ’‘ To recreate the pull request edit the issue title or description. To tweak the pull request, leave a comment on the pull request.
<sup>[Join Our Discord](https://discord.com/invite/sweep)