Skip to content

keep assistant errors in the active response slot - #9

Open
HrachShah wants to merge 1 commit into
mainfrom
patch/invocation-error-placeholder
Open

keep assistant errors in the active response slot#9
HrachShah wants to merge 1 commit into
mainfrom
patch/invocation-error-placeholder

Conversation

@HrachShah

@HrachShah HrachShah commented Jul 24, 2026

Copy link
Copy Markdown
Owner

When the Ollama IPC invocation rejects, replace the existing assistant placeholder instead of appending a second assistant message. This keeps the failed turn grouped with its user prompt and adds an assertion for the resulting message count.

Summary by Sourcery

Handle Ollama IPC failures by updating the existing assistant message instead of adding a new one, and ensure message count and content are validated in tests.

Enhancements:

  • Log Ollama invocation failures to the console for easier debugging.

Tests:

  • Update useOllama tests to assert the specific assistant error message content and that the failed turn keeps the expected two-message conversation length.

@sourcery-ai

sourcery-ai Bot commented Jul 24, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

This PR changes how Ollama IPC failures are surfaced: instead of adding a new assistant error message, it updates the existing assistant placeholder in the active response slot and tightens the corresponding test expectations.

Sequence diagram for handling Ollama IPC failure in useOllama

sequenceDiagram
  actor User
  participant useOllama
  participant ask_ollama
  participant MessagesState

  User->>useOllama: invoke ask
  useOllama->>MessagesState: setMessages add assistant placeholder
  useOllama->>ask_ollama: ask_ollama
  ask_ollama-->>useOllama: [reject]
  useOllama->>useOllama: console.error
  useOllama->>MessagesState: setMessages map update assistantId
  useOllama->>MessagesState: setIsGenerating false
Loading

File-Level Changes

Change Details Files
On Ollama invocation failure, update the existing assistant placeholder message instead of appending a new assistant error message, and log the error.
  • Wrap the Ollama ask_ollama call in a try/catch that captures the thrown error as an unknown and logs it via console.error
  • Replace the previous logic that appended a new assistant error message with logic that maps over existing messages and updates the one matching assistantId with error content and errorKind
  • Ensure isGenerating is set to false after handling the error
src/hooks/useOllama.ts
Tighten unit test expectations to verify the specific error content and that no extra assistant messages are created.
  • Update the test to assert the exact error message content string used for Ollama failures
  • Add an assertion that the messages array length remains 2, confirming the error reuses the existing assistant slot
src/hooks/__tests__/useOllama.test.tsx

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Jul 24, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@HrachShah, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 2bd8cf10-7689-4803-b5d7-e748c0712784

📥 Commits

Reviewing files that changed from the base of the PR and between f6bceab and e74f159.

📒 Files selected for processing (2)
  • src/hooks/__tests__/useOllama.test.tsx
  • src/hooks/useOllama.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch patch/invocation-error-placeholder

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In the catch handler, if assistantId is not found in the current messages array, setMessages will be a no-op and no error message will be visible; consider adding a fallback that appends an error message when the placeholder message cannot be located.
  • The error message string 'Something went wrong Could not reach Ollama.' is duplicated between the hook and the test; consider extracting it to a shared constant to keep the test aligned with the implementation and make future changes safer.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the catch handler, if `assistantId` is not found in the current messages array, `setMessages` will be a no-op and no error message will be visible; consider adding a fallback that appends an error message when the placeholder message cannot be located.
- The error message string `'Something went wrong
Could not reach Ollama.'` is duplicated between the hook and the test; consider extracting it to a shared constant to keep the test aligned with the implementation and make future changes safer.

## Individual Comments

### Comment 1
<location path="src/hooks/__tests__/useOllama.test.tsx" line_range="492-495" />
<code_context>
       );
       expect(errorMsg?.errorKind).toBe('Other');
-      expect(errorMsg?.content).toBeTruthy();
+      expect(errorMsg?.content).toBe(
+        'Something went wrong\nCould not reach Ollama.',
+      );
+      expect(result.current.messages).toHaveLength(2);
     });
   });
</code_context>
<issue_to_address>
**suggestion (testing):** Add assertions that the assistant message is replaced in-place (same id, single assistant message) rather than a new one being appended.

The added `toHaveLength(2)` helps ensure we don’t append an extra assistant message, but the test should also verify that the existing assistant placeholder is updated in-place. Consider capturing the assistant message id before triggering the error, then after the rejection assert that there is still exactly one assistant message and that its id is unchanged. This will more directly validate the new `map`-based replacement behavior and protect against regressions that reintroduce multiple assistant messages.

Suggested implementation:

```typescript
        (m) => m.errorKind === 'Other',
      );
      expect(errorMsg?.errorKind).toBe('Other');
      expect(errorMsg?.content).toBe(
        'Something went wrong\nCould not reach Ollama.',
      );
      expect(result.current.messages).toHaveLength(2);

      const assistantMessages = result.current.messages.filter(
        (m) => m.role === 'assistant',
      );
      expect(assistantMessages).toHaveLength(1);
      expect(assistantMessages[0].id).toBe(assistantMessageId);
    });
  });

```

To fully implement the suggestion, you also need to:
1. Capture the assistant placeholder message id *before* triggering the error in this test, e.g.:
   `const assistantMessageId = result.current.messages.find((m) => m.role === 'assistant')!.id;`
2. Ensure `assistantMessageId` is in scope where the new expectations are added (likely defined near the start of the test case, before the action that causes the rejection).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +492 to +495
expect(errorMsg?.content).toBe(
'Something went wrong\nCould not reach Ollama.',
);
expect(result.current.messages).toHaveLength(2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion (testing): Add assertions that the assistant message is replaced in-place (same id, single assistant message) rather than a new one being appended.

The added toHaveLength(2) helps ensure we don’t append an extra assistant message, but the test should also verify that the existing assistant placeholder is updated in-place. Consider capturing the assistant message id before triggering the error, then after the rejection assert that there is still exactly one assistant message and that its id is unchanged. This will more directly validate the new map-based replacement behavior and protect against regressions that reintroduce multiple assistant messages.

Suggested implementation:

        (m) => m.errorKind === 'Other',
      );
      expect(errorMsg?.errorKind).toBe('Other');
      expect(errorMsg?.content).toBe(
        'Something went wrong\nCould not reach Ollama.',
      );
      expect(result.current.messages).toHaveLength(2);

      const assistantMessages = result.current.messages.filter(
        (m) => m.role === 'assistant',
      );
      expect(assistantMessages).toHaveLength(1);
      expect(assistantMessages[0].id).toBe(assistantMessageId);
    });
  });

To fully implement the suggestion, you also need to:

  1. Capture the assistant placeholder message id before triggering the error in this test, e.g.:
    const assistantMessageId = result.current.messages.find((m) => m.role === 'assistant')!.id;
  2. Ensure assistantMessageId is in scope where the new expectations are added (likely defined near the start of the test case, before the action that causes the rejection).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant