deer-flow/frontend/tests/unit/core/threads/send-message.test.ts
Beverly621 0464502af1
fix(frontend): keep human input cards with their turn (#4892)
* fix(frontend): keep human input cards with their turn

* fix(frontend): keep human input cards with the correct turn

Multi-turn ordering in restoreLocalTurnMessageOrder could place an
`ask_clarification` (needYourHelp) card on the wrong side of a newly
submitted human message, and after an interrupt/stop it could move the
current run's own already-executed steps above the human that started them.

- Restore established messages that a live checkpoint tail wove after the
  new human (displacedBaselineMessages).
- Treat cards/messages confirmed only by the REST history page as
  established-past-turn too (confirmedHistoryIdentities), not as in-flight
  pending steps (displacedHistoryMessages).
- Never displace the CURRENT run's own steps after an interrupt/stop; they
  belong after the human even once canonical history confirms them
  (currentTurnRunIds, anchored by the pending human's run_id).

Fixes #4889

* fix(frontend): preserve ordering across displaced messages

* fix(frontend): close canonical history ordering gaps

* fix(frontend): preserve current turn anchor after compaction

* fix(frontend): anchor the local turn on the submitted human identity

Follow-up to #4892. R2 is reachable through the full hook chain: when the
checkpoint baseline covers only the latest turn, the server echo of the
submitted human confirms the optimistic copy against the unthrottled SDK
state while the ~80ms render snapshot cannot show it yet; the baseline-only
anchor scan then promoted an older history-only human into the current
turn's anchor and moved established history behind it.

- Record a LocalTurnAnchor at dispatch: one client-generated human id is
  shared by the optimistic display copy and the submitted message, so the
  server X__user echo confirms the exact identity already on screen.
- restoreLocalTurnMessageOrder repairs only when that identity is present
  in the display; a null anchor (hidden human-input reply, regenerate
  replay) or a not-yet-rendered identity keeps established history
  untouched.
- Optimistic confirmation now observes the same coalesced render snapshot
  (identity match first, rendered human-count growth as fallback for
  runtime-re-keyed first turns) instead of the per-chunk array.
- Edit replays adopt the prepare response's replacement identity; the
  render ledger excludes unconfirmed optimistic copies by identity now
  that the local input no longer uses an opt- prefix, so a failed send
  cannot pin a message the server never saw.
- Anchor lifecycle matches the previous baseline: kept across
  finish/stop/error until canonical data takes over, replaced by the next
  local submit, cleared on send failure, thread switch, and replay gaps.

* test(threads): type submit mock calls in local-turn-order dom tests

* fix(frontend): bound local turn repair to pre-submit history

* fix(frontend): preserve pre-submit bridge ordering

---------

Co-authored-by: 肘子香香 <hyh112300@163.com>
Co-authored-by: 霍英豪 <huoyinghao250707@credithc.com>
Co-authored-by: wangzeren <1004695029@qq.com>
2026-09-12 17:27:04 +08:00

115 lines
2.8 KiB
TypeScript

import type { Message } from "@langchain/langgraph-sdk";
import { expect, test } from "@rstest/core";
import { buildThreadSubmitMessages } from "@/core/threads/hooks";
test("builds thread submit messages with hidden sidecar context before the visible user message", () => {
const hiddenContext = {
type: "human",
content: "Hidden sidecar context",
additional_kwargs: {
hide_from_ui: true,
sidecar_context: true,
},
} as Message;
const messages = buildThreadSubmitMessages({
text: "What should we do next?",
additionalInputMessages: [hiddenContext],
});
expect(messages).toEqual([
hiddenContext,
{
type: "human",
content: [{ type: "text", text: "What should we do next?" }],
additional_kwargs: {},
},
]);
});
test("keeps uploaded files on the visible user message only", () => {
const messages = buildThreadSubmitMessages({
text: "Use this file",
additionalInputMessages: [
{
type: "human",
content: "Hidden sidecar context",
additional_kwargs: { hide_from_ui: true },
} as Message,
],
filesForSubmit: [
{
filename: "report.pdf",
size: 42,
path: "/uploads/report.pdf",
status: "uploaded",
},
],
});
expect(messages[0]?.additional_kwargs).toEqual({ hide_from_ui: true });
expect(messages[1]?.additional_kwargs).toEqual({
files: [
{
filename: "report.pdf",
size: 42,
path: "/uploads/report.pdf",
status: "uploaded",
},
],
});
});
test("keeps human input response metadata on the hidden user message", () => {
const response = {
version: 1,
kind: "human_input_response",
source: "ask_clarification",
request_id: "clarification:call-abc",
response_kind: "option",
option_id: "option-2",
value: "staging",
};
const messages = buildThreadSubmitMessages({
text: 'For your clarification "Which environment?", my answer is: staging',
additionalKwargs: {
hide_from_ui: true,
human_input_response: response,
},
});
expect(messages).toEqual([
{
type: "human",
content: [
{
type: "text",
text: 'For your clarification "Which environment?", my answer is: staging',
},
],
additional_kwargs: {
hide_from_ui: true,
human_input_response: response,
},
},
]);
});
test("uses the caller-provided human message id for the visible user message", () => {
const messages = buildThreadSubmitMessages({
text: "hello",
humanMessageId: "local-human-1",
});
expect(messages).toEqual([
{
type: "human",
id: "local-human-1",
content: [{ type: "text", text: "hello" }],
additional_kwargs: {},
},
]);
});