Skip to content

Conversation

@KJ7LNW
Copy link
Contributor

@KJ7LNW KJ7LNW commented Jun 4, 2025

Context

This PR adds detailed debug logging to the readApiMessages function to help diagnose "Unexpected: No existing API conversation history" errors.

Implementation

Added logging for three scenarios:

  1. When the API conversation history file exists but contains an empty array
  2. When the old history file exists but contains an empty array
  3. When neither history file is found (already implemented)

How to Test

When encountering the "Unexpected: No existing API conversation history" error, check the Developer Tools console (Help > Toggle Developer Tools > Console tab) for debug messages with the format:
[Roo-Debug] readApiMessages: ...

These logs will show the taskId and file path, helping to diagnose the issue.

Screenshots

N/A - Console logging only

Get in Touch

Discord: KJ7LNW

does not completely fix #4311 but it may provide debug output that may help us fix it from other users.


Important

Adds detailed debug logging to readApiMessages in apiMessages.ts for diagnosing issues with API conversation history files.

  • Debug Logging:
    • Adds detailed debug logging to readApiMessages in apiMessages.ts for diagnosing missing or empty API conversation history files.
    • Logs when the history file exists but is empty, and when the old history file exists but is empty.
    • Logs when neither the new nor old history file is found.
  • Error Handling:
    • Catches JSON parsing errors and logs them with taskId and file path details.
    • Ensures old history file is not deleted if parsing fails.

This description was created by Ellipsis for 9d19c68. You can customize this summary. It will automatically update as commits are pushed.

Add detailed logging to readApiMessages function to help diagnose
"Unexpected: No existing API conversation history" errors.

The logs will show:
- When the history file exists but contains an empty array
- When the old history file exists but contains an empty array
- When neither history file is found

This will help diagnose issues like the one in #4311 where the
API conversation history file gets truncated to an empty array
during task resumption with orchestrator tasks.

Fixes: #4311
Signed-off-by: Eric Wheeler <[email protected]>
@KJ7LNW KJ7LNW requested review from cte and mrubens as code owners June 4, 2025 04:57
@dosubot dosubot bot added size:S This PR changes 10-29 lines, ignoring generated files. bug Something isn't working labels Jun 4, 2025
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jun 4, 2025
Add error handling for JSON parsing failures in readApiMessages:
- Wrap JSON.parse calls in try/catch blocks for both current and legacy history files
- Add detailed error logging with taskId and file paths
- Return empty arrays when parsing fails to maintain function contract
- Improve existing debug logging for empty history arrays
- Handle file unlinking even when parsing fails

This prevents crashes from malformed JSON in history files and provides
better debugging information when API conversation history is corrupted.
Related to issue #4311.

Signed-off-by: Eric Wheeler <[email protected]>
@KJ7LNW KJ7LNW force-pushed the debug-api-conversation-history branch from 21a8167 to 9d19c68 Compare June 4, 2025 05:22
@hannesrudolph hannesrudolph marked this pull request as draft June 4, 2025 19:12
@hannesrudolph hannesrudolph changed the title fix: add debug logging for API conversation history issues TEMP: add debug logging for API conversation history issues Jun 4, 2025
@hannesrudolph hannesrudolph moved this from Triage to PR [Draft / In Progress] in Roo Code Roadmap Jun 4, 2025
@hannesrudolph
Copy link
Collaborator

See notes on the issue as to why I moved this to draft

@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 5, 2025

I just wanted to provide the steps I took for troubleshooting and case it helps someone else along the way.

These debug prints are non-intrusive, uncommon, only trigger upon error, and only serve to help troubleshoot this problem if somebody else hits it.

IMHO, PRs like this should be merged quickly to help future troubleshooting by others. (Someday if I clean up and delete the branch, then this research could be lost.)

@KJ7LNW KJ7LNW marked this pull request as ready for review June 6, 2025 22:02
@KJ7LNW KJ7LNW requested a review from jr as a code owner June 6, 2025 22:02
@KJ7LNW KJ7LNW self-assigned this Jun 6, 2025
@KJ7LNW KJ7LNW moved this from PR [Draft / In Progress] to PR [Needs Prelim Review] in Roo Code Roadmap Jun 6, 2025
@KJ7LNW KJ7LNW changed the title TEMP: add debug logging for API conversation history issues add debug logging for API conversation history issues Jun 6, 2025
@KJ7LNW KJ7LNW changed the title add debug logging for API conversation history issues Add proper error handling for API conversation history issues Jun 6, 2025
@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 6, 2025

@cte - Windows test error is probably unrelated to this pull request - do you know what is going on there?

@daniel-lxs
Copy link
Member

Looking at this PR, I noticed the debug logging is only written to console.error. Have you considered propagating these error details up to the actual error that users see?

Currently, when readApiMessages() encounters an empty file or missing file, it logs to console but returns []. Then in Task.ts line 922, it throws a generic error: "Unexpected: No existing API conversation history".

Wouldn't it be more helpful if the error message included the specific reason? For example:

  • "No existing API conversation history: File not found at ~/.config/.../api_conversation_history.json"
  • "No existing API conversation history: File exists but is empty at ~/.config/.../api_conversation_history.json"
  • "No existing API conversation history: JSON parsing error at ~/.config/.../api_conversation_history.json"

This way users would immediately know what went wrong without needing to check developer console logs. The debug info could either be:

  1. Returned as part of a result object from readApiMessages()
  2. Thrown as specific error types that can be caught and re-thrown with context

What do you think about enhancing the error propagation in addition to the console logging?

@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Changes Requested] in Roo Code Roadmap Jun 7, 2025
@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented Jun 15, 2025

Looking at this PR, I noticed the debug logging is only written to console.error. Have you considered propagating these error details up to the actual error that users see?

It is a good idea, however I do not really want to put a lot more work into this. Is there a VS Code function that can replace console.error to provide a simple pop-up? if there is, then probably all console.{warn,error} occurrences in the code base should be standardized to use such a mechanism, and then .roo/rules/logging-rules.md needs to be created to instruct future AI debug use to use the same mechanism.

The intention of this PR was just to provide a little more information to possibly help others by simple debug; in this case the review process is introducing unnecessary complexity to what was intended as trivial.

Sometimes when troubleshooting an issue if I do not find a complete solution, I will create a pull request with whatever instrumentation led me there, in hopes that someone else can report with additional information. The error framework that you suggest as a great idea, and maybe it should be taken on by another developer in a separate pull request to streamline PRs like this in the future.

If you (or someone else) would like to implement such a thing then please do, but I think if this pull request is not acceptable in its current state than we should close it because I do not have the bandwidth to take that on at the moment.

@daniel-lxs daniel-lxs moved this from PR [Changes Requested] to PR [Needs Prelim Review] in Roo Code Roadmap Jun 16, 2025
Copy link
Member

@daniel-lxs daniel-lxs left a comment

Choose a reason for hiding this comment

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

Looks good to me, this will help us understand what might be going on when tasks disappear in some cases.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Jun 16, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Needs Review] in Roo Code Roadmap Jun 16, 2025
@mrubens mrubens merged commit 9b605c9 into RooCodeInc:main Jun 17, 2025
28 of 29 checks passed
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Jun 17, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Jun 17, 2025
valekseev pushed a commit to valekseev/Roo-Code that referenced this pull request Jun 18, 2025
cte pushed a commit that referenced this pull request Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer PR - Needs Review size:M This PR changes 30-99 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

bug: API conversation history file truncated to empty array during task resume

4 participants