Skip to content

[Security Assistant] Fixes conversation message not appended/title not updating#233219

Merged
stephmilovic merged 7 commits intoelastic:mainfrom
stephmilovic:retries_convo
Sep 11, 2025
Merged

[Security Assistant] Fixes conversation message not appended/title not updating#233219
stephmilovic merged 7 commits intoelastic:mainfrom
stephmilovic:retries_convo

Conversation

@stephmilovic
Copy link
Copy Markdown
Contributor

@stephmilovic stephmilovic commented Aug 27, 2025

Summary

While working on assistant sharing, I discovered several related bugs tied to race conditions and async/sync mismatches in conversation handling:

  • The assistant’s first response was sometimes not saved properly.
  • Conversation titles were not reliably updated in the UI.
  • The loading comment during message send would disappear and reappear, causing a distracting flicker.

These issues were all related to conflicts in updating the data stream after changes were made to run title generation and first message handling asynchronously within the graph.


Root Causes

  1. Data Conflicts During Message Append

    • Race conditions caused the first response not to persist.
    • Retry logic was attempted but max retries were too low, so some updates failed.
  2. Title Not Updating Reliably

    • When sending a message, the client sometimes fetched the conversation before the title was ready, leaving it empty.
  3. Loading Comment Flicker

    • isFetchingResponse state only covered API call completion, not the entire message flow.
    • This created a gap between API finish and response render where the loading state disappeared.

Fixes Implemented

Backend

  • File: append_conversation_messages.ts
    • Replaced updateByQuery with DocumentsDataWriter.bulk for safer updates.
    • Updated semantic_text handling to use doc updates instead of scripts.
    • Function signature updated to accept DocumentsDataWriter.

Frontend

  • File: use_chat_send.tsx
    • Added retry logic for fetching conversation titles after receiving the first response.
    • Introduced local isLoadingChatSend state that persists for the entire flow.
    • Wrapped message handling in a try...finally block to ensure consistent state.
    • Return value now respects local loading state instead of API-only state.

Results

✅ Assistant’s first response is always saved
✅ Conversation titles now update reliably, even under async conditions
✅ Loading comment stays visible until the full response arrives (no flicker)
✅ Improved user experience with smooth, reliable message handling


Visual Examples

  • Missing first response due to race condition:
    Screenshot 2025-08-27 at 3 36 05 PM

  • Title not updating reliably in UI:
    Screenshot 2025-08-27 at 3 52 13 PM


How to Test

  1. Create multiple new conversations in the UI.
  2. Verify:
    • Each conversation always gets a title.
    • The assistant’s first response is consistently saved.
    • The loading comment stays visible until the assistant’s response fully appears.
    • No flicker or disappearing comments.

@stephmilovic stephmilovic requested a review from a team as a code owner August 27, 2025 21:53
@stephmilovic stephmilovic added Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. backport:version Backport to applied version labels v9.2.0 v9.1.3 v8.19.3 labels Aug 27, 2025
@elasticmachine
Copy link
Copy Markdown
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@stephmilovic
Copy link
Copy Markdown
Contributor Author

This change: https://github.com/elastic/kibana/pull/232288/files#r2305375978
Could also be contributing to these bugs, but I created 100 conversations in the UI and didn't see the title creation have a conflict so I think it's ok?

@e40pud
Copy link
Copy Markdown
Contributor

e40pud commented Aug 28, 2025

@KDKHD
Copy link
Copy Markdown
Member

KDKHD commented Aug 28, 2025

Client side changes look good! @e40pud's suggestion sounds reasonable for the server-side code.

@e40pud
Copy link
Copy Markdown
Contributor

e40pud commented Aug 28, 2025

Here are the changes that I proposed #233288

@stephmilovic
Copy link
Copy Markdown
Contributor Author

Here are the changes that I proposed #233288

This is great for the bulk update, but since append_conversation_message uses esClient.updateByQuery, we cannot use the same solution unless we rewrite it to use the bulk updater.

]);

const maxRetries = 3;
const maxRetries = 10;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do not think this is enough. I have seen up to 8 retries happen.

We should fix the exponential backoff below to actually be exponential. E.g:

const delay = Math.min(50 * 2 ** attempt, 1000);

@stephmilovic stephmilovic requested a review from KDKHD September 9, 2025 20:47
@stephmilovic
Copy link
Copy Markdown
Contributor Author

@elasticmachine merge upstream

Copy link
Copy Markdown
Member

@KDKHD KDKHD left a comment

Choose a reason for hiding this comment

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

Tested locally and was unable to reproduce the original issue.

@elasticmachine
Copy link
Copy Markdown
Contributor

💚 Build Succeeded

Metrics [docs]

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
elasticAssistant 301.5KB 301.7KB +133.0B

History

@stephmilovic stephmilovic merged commit b81bbd0 into elastic:main Sep 11, 2025
12 checks passed
@kibanamachine
Copy link
Copy Markdown
Contributor

Starting backport for target branches: 8.19, 9.1

https://github.com/elastic/kibana/actions/runs/17656003638

@kibanamachine
Copy link
Copy Markdown
Contributor

💔 All backports failed

Status Branch Result
8.19 Backport failed because of merge conflicts
9.1 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 233219

Questions ?

Please refer to the Backport tool documentation

@stephmilovic
Copy link
Copy Markdown
Contributor Author

💚 All backports created successfully

Status Branch Result
9.1
8.19

Note: Successful backport PRs will be merged automatically after passing CI.

Questions ?

Please refer to the Backport tool documentation

stephmilovic added a commit to stephmilovic/kibana that referenced this pull request Sep 12, 2025
stephmilovic added a commit to stephmilovic/kibana that referenced this pull request Sep 12, 2025
KodeRad pushed a commit to KodeRad/kibana that referenced this pull request Sep 15, 2025
stephmilovic added a commit that referenced this pull request Sep 15, 2025
…itle not updating (#233219) (#234967)

# Backport

This will backport the following commits from `main` to `8.19`:
- [[Security Assistant] Fixes conversation message not appended/title
not updating (#233219)](#233219)

<!--- Backport version: 10.0.2 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Steph
Milovic","email":"stephanie.milovic@elastic.co"},"sourceCommit":{"committedDate":"2025-09-11T20:06:53Z","message":"[Security
Assistant] Fixes conversation message not appended/title not updating
(#233219)","sha":"b81bbd005f94e6a333b04b06da3ac108e179fa6a","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:
SecuritySolution","backport:version","v9.2.0","v9.1.3","v8.19.3"],"title":"[Security
Assistant] Fixes conversation message not appended/title not
updating","number":233219,"url":"https://github.com/elastic/kibana/pull/233219","mergeCommit":{"message":"[Security
Assistant] Fixes conversation message not appended/title not updating
(#233219)","sha":"b81bbd005f94e6a333b04b06da3ac108e179fa6a"}},"sourceBranch":"main","suggestedTargetBranches":["9.1","8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/233219","number":233219,"mergeCommit":{"message":"[Security
Assistant] Fixes conversation message not appended/title not updating
(#233219)","sha":"b81bbd005f94e6a333b04b06da3ac108e179fa6a"}},{"branch":"9.1","label":"v9.1.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: Ievgen Sorokopud <ievgen.sorokopud@elastic.co>
stephmilovic added a commit that referenced this pull request Sep 15, 2025
…tle not updating (#233219) (#234966)

# Backport

This will backport the following commits from `main` to `9.1`:
- [[Security Assistant] Fixes conversation message not appended/title
not updating (#233219)](#233219)

<!--- Backport version: 10.0.2 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT [{"author":{"name":"Steph
Milovic","email":"stephanie.milovic@elastic.co"},"sourceCommit":{"committedDate":"2025-09-11T20:06:53Z","message":"[Security
Assistant] Fixes conversation message not appended/title not updating
(#233219)","sha":"b81bbd005f94e6a333b04b06da3ac108e179fa6a","branchLabelMapping":{"^v9.2.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","Team:
SecuritySolution","backport:version","v9.2.0","v9.1.3","v8.19.3"],"title":"[Security
Assistant] Fixes conversation message not appended/title not
updating","number":233219,"url":"https://github.com/elastic/kibana/pull/233219","mergeCommit":{"message":"[Security
Assistant] Fixes conversation message not appended/title not updating
(#233219)","sha":"b81bbd005f94e6a333b04b06da3ac108e179fa6a"}},"sourceBranch":"main","suggestedTargetBranches":["9.1","8.19"],"targetPullRequestStates":[{"branch":"main","label":"v9.2.0","branchLabelMappingKey":"^v9.2.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/233219","number":233219,"mergeCommit":{"message":"[Security
Assistant] Fixes conversation message not appended/title not updating
(#233219)","sha":"b81bbd005f94e6a333b04b06da3ac108e179fa6a"}},{"branch":"9.1","label":"v9.1.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.19","label":"v8.19.3","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: Ievgen Sorokopud <ievgen.sorokopud@elastic.co>
CAWilson94 pushed a commit to CAWilson94/kibana that referenced this pull request Sep 24, 2025
niros1 pushed a commit that referenced this pull request Sep 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport:version Backport to applied version labels release_note:fix Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. v8.19.3 v8.19.4 v9.1.3 v9.1.4 v9.2.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants