Skip to content

[SecuritySolution] Make last conversation local storage keys space aware#214794

Merged
angorayc merged 23 commits intoelastic:mainfrom
angorayc:issues-214114
Mar 26, 2025
Merged

[SecuritySolution] Make last conversation local storage keys space aware#214794
angorayc merged 23 commits intoelastic:mainfrom
angorayc:issues-214114

Conversation

@angorayc
Copy link
Contributor

@angorayc angorayc commented Mar 17, 2025

Summary

Issue and steps to reproduce:
#214114

Screen.Recording.2025-03-18.at.14.52.12.mov

The best approach to fix this is to make local storage keys space aware. In this use case, the current key is elasticAssistantDefault.lastSelectedConversation.

Ideally it should be e.g.: elasticAssistantDefault.lastSelectedConversation.{spaceId}

To retrieve spaceId properly, we have to make sure spaceId has been available when reading the local storage. In other words, the spaceId cannot be null, undefined, or a fallback value when accessing it.

To achieve that, we have to only render the assistant overlay after spaceId is available and remove existing spaceId from the AssistantContext. The reason I removed spaceId from AssistantContext is because it is at the top of the rendering tree and can block the entire app if waiting for the spaceId becomes available there.

useAssistantSpaceId comes from SpaceIdContext which should be render only when spaceId is available, should be safe to access the exact spaceId inside Assistant overly.

Checklist

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

inferenceEnabled={inferenceEnabled}
navigateToApp={navigateToApp}
productDocBase={productDocBase}
spaceId={spaceId ?? 'default'}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might be going to use for reading the local storage keys. In other words, the spaceId cannot be null, undefined, or a fallback value when accessing it. So I moved it here x-pack/solutions/security/plugins/security_solution/public/assistant/overlay.tsx
to make sure we get space id in ElasticAssistantOverlay correctly.

async ({
cId,
isStreamRefetch = false,
silent,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should only silence the error when the id comes from the lastConversation, so down in the useEffect on L256

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if you're doing the space aware local storage keys, maybe we don't need to silence the error at all?

Copy link
Contributor Author

@angorayc angorayc Mar 18, 2025

Choose a reason for hiding this comment

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

Yeah, you are right.
I wasn't sure if I wanted to make local storage keys space aware in this PR, as wasn't sure how big the scope were 😅
After having another look, I move the last conversation relevant keys to a separate hook, looks alright so far.
To make the scope smaller, I am only making the last conversation local storage keys space aware.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stephmilovic I still snooze the error when conversation not found after make the local storage space aware. As I found if I created some conversations, and restart my ES. The not found error would show up.

@angorayc angorayc changed the title [SecuritySolution] Do not show error if the last conversation was not found [SecuritySolution] Make last conversation local storage keys space aware Mar 19, 2025
@angorayc angorayc added v8.18.0 Team:Security Generative AI Security Generative AI bug Fixes for quality problems that affect the customer experience and removed v8.19.0 labels Mar 19, 2025
@angorayc angorayc marked this pull request as ready for review March 20, 2025 15:08
@angorayc angorayc requested review from a team as code owners March 20, 2025 15:08
@angorayc angorayc added the release_note:skip Skip the PR/issue when compiling release notes label Mar 20, 2025
@elasticmachine
Copy link
Contributor

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #75 / EQL execution logic API @ess @serverless @serverlessQA EQL type rules parses shard failures for EQL event query

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
automaticImport 723 726 +3
securitySolution 7088 7091 +3
total +6

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/elastic-assistant 135 141 +6

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
securitySolution 8.9MB 8.9MB +1.6KB

Public APIs missing exports

Total count of every type that is part of your API that should be exported but is not. This will cause broken links in the API documentation system. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats exports for more detailed information.

id before after diff
@kbn/elastic-assistant 9 11 +2

Page load bundle

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

id before after diff
securitySolution 88.3KB 88.3KB -1.0B
Unknown metric groups

API count

id before after diff
@kbn/elastic-assistant 164 170 +6

History

Copy link
Contributor

@stephmilovic stephmilovic left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the improvement! It might be too late to backport to 8.18. Make sure you have the 8.19 label on here too!

@stephmilovic stephmilovic added the backport:version Backport to applied version labels label Mar 26, 2025
@angorayc angorayc added v8.18.1 backport:prev-minor and removed backport:version Backport to applied version labels v8.18.0 labels Mar 26, 2025
@angorayc angorayc merged commit 169abec into elastic:main Mar 26, 2025
22 checks passed
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.18, 8.x, 9.0

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

@angorayc angorayc added backport:version Backport to applied version labels and removed backport:prev-minor labels Mar 26, 2025
@kibanamachine
Copy link
Contributor

Starting backport for target branches: 8.18, 8.x, 9.0

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

kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Mar 26, 2025
…are (elastic#214794)

## Summary

Issue and steps to reproduce:
elastic#214114

https://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661

The best approach to fix this is to make local storage keys space aware.
In this use case, the current key is
`elasticAssistantDefault.lastSelectedConversation`.

Ideally it should be e.g.:
`elasticAssistantDefault.lastSelectedConversation.{spaceId}`

To retrieve spaceId properly, we have to make sure spaceId has been
available when reading the local storage. In other words, the spaceId
cannot be null, undefined, or a fallback value when accessing it.

To achieve that, we have to only render the assistant overlay after
spaceId is available and remove existing spaceId from the
AssistantContext. The reason I removed spaceId from AssistantContext is
because it is at the top of the rendering tree and can block the entire
app if waiting for the spaceId becomes available there.

`useAssistantSpaceId` comes from `SpaceIdContext` which should be render
only when spaceId is available, should be safe to access the exact
spaceId inside Assistant overly.

### Checklist

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 169abec)
@kibanamachine
Copy link
Contributor

💔 Some backports could not be created

Status Branch Result
8.18 Backport failed because of merge conflicts
8.x
9.0 Backport failed because of merge conflicts

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

Manual backport

To create the backport manually run:

node scripts/backport --pr 214794

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit to kibanamachine/kibana that referenced this pull request Mar 26, 2025
…are (elastic#214794)

## Summary

Issue and steps to reproduce:
elastic#214114

https://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661

The best approach to fix this is to make local storage keys space aware.
In this use case, the current key is
`elasticAssistantDefault.lastSelectedConversation`.

Ideally it should be e.g.:
`elasticAssistantDefault.lastSelectedConversation.{spaceId}`

To retrieve spaceId properly, we have to make sure spaceId has been
available when reading the local storage. In other words, the spaceId
cannot be null, undefined, or a fallback value when accessing it.

To achieve that, we have to only render the assistant overlay after
spaceId is available and remove existing spaceId from the
AssistantContext. The reason I removed spaceId from AssistantContext is
because it is at the top of the rendering tree and can block the entire
app if waiting for the spaceId becomes available there.

`useAssistantSpaceId` comes from `SpaceIdContext` which should be render
only when spaceId is available, should be safe to access the exact
spaceId inside Assistant overly.

### Checklist

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 169abec)
@kibanamachine
Copy link
Contributor

💔 Some backports could not be created

Status Branch Result
8.18 Backport failed because of merge conflicts
8.x
9.0 Backport failed because of merge conflicts

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

Manual backport

To create the backport manually run:

node scripts/backport --pr 214794

Questions ?

Please refer to the Backport tool documentation

kibanamachine added a commit that referenced this pull request Mar 27, 2025
…ace aware (#214794) (#216085)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[SecuritySolution] Make last conversation local storage keys space
aware (#214794)](#214794)

<!--- Backport version: 9.6.6 -->

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

<!--BACKPORT [{"author":{"name":"Angela
Chuang","email":"6295984+angorayc@users.noreply.github.com"},"sourceCommit":{"committedDate":"2025-03-26T16:34:48Z","message":"[SecuritySolution]
Make last conversation local storage keys space aware (#214794)\n\n##
Summary\n\nIssue and steps to
reproduce:\nhttps://github.com//issues/214114\n\n\n\n\nhttps://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661\n\n\n\nThe
best approach to fix this is to make local storage keys space aware.\nIn
this use case, the current key
is\n`elasticAssistantDefault.lastSelectedConversation`.\n\nIdeally it
should be
e.g.:\n`elasticAssistantDefault.lastSelectedConversation.{spaceId}`\n\nTo
retrieve spaceId properly, we have to make sure spaceId has
been\navailable when reading the local storage. In other words, the
spaceId\ncannot be null, undefined, or a fallback value when accessing
it.\n\nTo achieve that, we have to only render the assistant overlay
after\nspaceId is available and remove existing spaceId from
the\nAssistantContext. The reason I removed spaceId from
AssistantContext is\nbecause it is at the top of the rendering tree and
can block the entire\napp if waiting for the spaceId becomes available
there.\n\n`useAssistantSpaceId` comes from `SpaceIdContext` which should
be render\nonly when spaceId is available, should be safe to access the
exact\nspaceId inside Assistant overly.\n\n\n### Checklist\n\nCheck the
PR satisfies following conditions. \n\nReviewers should verify this PR
satisfies this list as well.\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common
scenarios\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"169abecdda27d834dfd417218e3dbf0102ff65a8","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["bug","release_note:skip","v9.0.0","Team:Security
Generative
AI","backport:version","v9.1.0","v8.19.0","v8.18.1"],"title":"[SecuritySolution]
Make last conversation local storage keys space
aware","number":214794,"url":"https://github.com/elastic/kibana/pull/214794","mergeCommit":{"message":"[SecuritySolution]
Make last conversation local storage keys space aware (#214794)\n\n##
Summary\n\nIssue and steps to
reproduce:\nhttps://github.com//issues/214114\n\n\n\n\nhttps://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661\n\n\n\nThe
best approach to fix this is to make local storage keys space aware.\nIn
this use case, the current key
is\n`elasticAssistantDefault.lastSelectedConversation`.\n\nIdeally it
should be
e.g.:\n`elasticAssistantDefault.lastSelectedConversation.{spaceId}`\n\nTo
retrieve spaceId properly, we have to make sure spaceId has
been\navailable when reading the local storage. In other words, the
spaceId\ncannot be null, undefined, or a fallback value when accessing
it.\n\nTo achieve that, we have to only render the assistant overlay
after\nspaceId is available and remove existing spaceId from
the\nAssistantContext. The reason I removed spaceId from
AssistantContext is\nbecause it is at the top of the rendering tree and
can block the entire\napp if waiting for the spaceId becomes available
there.\n\n`useAssistantSpaceId` comes from `SpaceIdContext` which should
be render\nonly when spaceId is available, should be safe to access the
exact\nspaceId inside Assistant overly.\n\n\n### Checklist\n\nCheck the
PR satisfies following conditions. \n\nReviewers should verify this PR
satisfies this list as well.\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common
scenarios\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"169abecdda27d834dfd417218e3dbf0102ff65a8"}},"sourceBranch":"main","suggestedTargetBranches":["9.0","8.x","8.18"],"targetPullRequestStates":[{"branch":"9.0","label":"v9.0.0","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/214794","number":214794,"mergeCommit":{"message":"[SecuritySolution]
Make last conversation local storage keys space aware (#214794)\n\n##
Summary\n\nIssue and steps to
reproduce:\nhttps://github.com//issues/214114\n\n\n\n\nhttps://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661\n\n\n\nThe
best approach to fix this is to make local storage keys space aware.\nIn
this use case, the current key
is\n`elasticAssistantDefault.lastSelectedConversation`.\n\nIdeally it
should be
e.g.:\n`elasticAssistantDefault.lastSelectedConversation.{spaceId}`\n\nTo
retrieve spaceId properly, we have to make sure spaceId has
been\navailable when reading the local storage. In other words, the
spaceId\ncannot be null, undefined, or a fallback value when accessing
it.\n\nTo achieve that, we have to only render the assistant overlay
after\nspaceId is available and remove existing spaceId from
the\nAssistantContext. The reason I removed spaceId from
AssistantContext is\nbecause it is at the top of the rendering tree and
can block the entire\napp if waiting for the spaceId becomes available
there.\n\n`useAssistantSpaceId` comes from `SpaceIdContext` which should
be render\nonly when spaceId is available, should be safe to access the
exact\nspaceId inside Assistant overly.\n\n\n### Checklist\n\nCheck the
PR satisfies following conditions. \n\nReviewers should verify this PR
satisfies this list as well.\n\n- [x] [Unit or
functional\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\nwere
updated or added to match the most common
scenarios\n\n---------\n\nCo-authored-by: kibanamachine
<42973632+kibanamachine@users.noreply.github.com>","sha":"169abecdda27d834dfd417218e3dbf0102ff65a8"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"8.18","label":"v8.18.1","branchLabelMappingKey":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Angela Chuang <6295984+angorayc@users.noreply.github.com>
angorayc added a commit to angorayc/kibana that referenced this pull request Mar 27, 2025
…are (elastic#214794)

Issue and steps to reproduce:
elastic#214114

https://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661

The best approach to fix this is to make local storage keys space aware.
In this use case, the current key is
`elasticAssistantDefault.lastSelectedConversation`.

Ideally it should be e.g.:
`elasticAssistantDefault.lastSelectedConversation.{spaceId}`

To retrieve spaceId properly, we have to make sure spaceId has been
available when reading the local storage. In other words, the spaceId
cannot be null, undefined, or a fallback value when accessing it.

To achieve that, we have to only render the assistant overlay after
spaceId is available and remove existing spaceId from the
AssistantContext. The reason I removed spaceId from AssistantContext is
because it is at the top of the rendering tree and can block the entire
app if waiting for the spaceId becomes available there.

`useAssistantSpaceId` comes from `SpaceIdContext` which should be render
only when spaceId is available, should be safe to access the exact
spaceId inside Assistant overly.

Check the PR satisfies following conditions.

Reviewers should verify this PR satisfies this list as well.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
(cherry picked from commit 169abec)
angorayc added a commit that referenced this pull request Mar 31, 2025
…ace aware (#214794) (#216214)

# Backport

This will backport the following commits from `main` to `9.0`:
- [[SecuritySolution] Make last conversation local storage keys space
aware (#214794)](#214794)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
cqliu1 pushed a commit to cqliu1/kibana that referenced this pull request Mar 31, 2025
…are (elastic#214794)

## Summary

Issue and steps to reproduce:
elastic#214114




https://github.com/user-attachments/assets/881fb082-a879-4816-b251-da3f2af77661



The best approach to fix this is to make local storage keys space aware.
In this use case, the current key is
`elasticAssistantDefault.lastSelectedConversation`.

Ideally it should be e.g.:
`elasticAssistantDefault.lastSelectedConversation.{spaceId}`

To retrieve spaceId properly, we have to make sure spaceId has been
available when reading the local storage. In other words, the spaceId
cannot be null, undefined, or a fallback value when accessing it.

To achieve that, we have to only render the assistant overlay after
spaceId is available and remove existing spaceId from the
AssistantContext. The reason I removed spaceId from AssistantContext is
because it is at the top of the rendering tree and can block the entire
app if waiting for the spaceId becomes available there.

`useAssistantSpaceId` comes from `SpaceIdContext` which should be render
only when spaceId is available, should be safe to access the exact
spaceId inside Assistant overly.


### Checklist

Check the PR satisfies following conditions. 

Reviewers should verify this PR satisfies this list as well.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
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 bug Fixes for quality problems that affect the customer experience release_note:skip Skip the PR/issue when compiling release notes Team:Security Generative AI Security Generative AI v8.18.1 v8.19.0 v9.0.0 v9.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants