Draft: Check the last Duo Chat message before loading the full history
What does this MR do and why?
With the duo_agentic_chat_lazy_history flag on, the Duo Agentic Chat panel reuses its cached conversation on a page load when the newest message is unchanged, and skips the history request. Rebuilding that history is the slow part of the getWorkflowLatestCheckpoint resolver, and the issue estimates this removes 31-41% of the slow requests in its Kibana sample. The check also needs duo_agentic_chat_paged_history, because the "Show earlier messages" button from !265826 (closed) is how a reused snapshot reaches older messages.
Design notes
The check:
- On a page load, the panel asks for
lastDuoMessagefirst (@include(if: $withLastMessage)in the checkpoint query). It compares it with the newest message of the sessionStorage snapshot, which holds the newest 20 messages. - If they match, the panel shows the snapshot and skips the history request. If they don't match, the workflow has no message, or there is no reusable snapshot, the panel loads the newest page as in !265826 (closed).
- Flow lock release skips the check, because another client held the lock and the history has almost always changed.
When the snapshot is reused:
- The snapshot now stores
fromServer. It is true when the snapshot's newest message equalslastLoadedMessage, the newest message of the last server load. - Messages streamed over the websocket lack checkpoint metadata (
thread_ts,parent_ts,alternative_count), and a snapshot saved after a retry can hold the retried attempts. Reusing such a snapshot would show a different conversation than a full load. - So after the user chats, the next page load does one full load, and later loads reuse the snapshot.
How messages compare:
- Two messages are equal when
message_id,message_type,status, andcontentmatch. Missing fields read as null, because the JSON round trip of the snapshot drops undefined keys. - A message without
message_idnever matches. Comparingcontentcatches a snapshot saved mid-stream.
Paging from a reused snapshot:
- A reused snapshot has no page cursor. The first "Show earlier messages" click loads the newest page and reconciles it with the snapshot, and later clicks page back from its cursor. A send or a retry from a reused snapshot does the same before it pages back.
- A full snapshot (20 messages) shows the button, because older messages can exist. A chat with exactly 20 messages shows the button once, and the click loads nothing new.
Other notes:
- If the check fails and the load retries, the retry clears the messages on screen. A later match shows the snapshot again.
- An aborted load sends no history request after the check.
lastDuoMessagedecodes only the newest blob, but the query still reads every checkpoint header of the workflow, so this MR does not remove that part of the cost.duo_agentic_chat_state_manager_light.vuedoes not change. Nothing in GitLab mounts it, only its spec.- This is the last of three MRs. It builds on !265826 (closed), which builds on !265824 (closed).
Tests:
- The state manager spec covers a match, a mismatch, a missing message, a snapshot that is not a copy of the server's history, each flag off, the retry after a failed check, the
fromServerflag before and after a streamed message, paging from a reused snapshot, and flow lock release. - The service spec covers the check, its retry, and an abort between the two requests.
isSameMessageand the snapshot have unit specs.
References
- Issue: #631642
- Parent issue: #631606
- Builds on !265826 (closed) and !265824 (closed)
- Rollout issue for this flag: #632717
- Rollout issue for the paging flag: #636695 (closed)
- Related merged MRs: !264039 (merged) (cap and
messagesconnection), !258940 (merged) (chat history cache), !258974 (merged) (parse only the new part of each snapshot)
Screenshots or screen recordings
No UI change in this MR. The button comes from !265826 (closed).
How to set up and validate locally
- In a Rails console, enable both flags:
Feature.enable(:duo_agentic_chat_paged_history)andFeature.enable(:duo_agentic_chat_lazy_history). - Open any project on GDK (http://gdk.test:3000/...), open the GitLab Duo panel, and open a chat with a few messages.
- After the last response finishes, reload the page once, then reload again. The first reload loads the newest page, because the snapshot holds streamed messages.
- In the browser dev tools Network tab, filter by
graphqland inspectgetWorkflowLatestCheckpointfor the second reload. Expect: one request withwithLastMessage: trueandwithMessagesPage: false, and a response withoutduoMessagesormessages. - Send a message, wait for the reply, and reload.
Expect: a request with
withMessagesPage: true, because the snapshot now holds streamed messages.
Edge cases
- With either flag off, every reload loads the history (the newest page with only the paging flag on, the full history with both off).
- If the chat changed in another tab or the IDE, the reload loads the newest page.
- A snapshot saved mid-stream does not match, because its content differs.
MR acceptance checklist
Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.