fix(custom-cli): affichage conversation stable au second tour — dédup du prompt et scope de progression par tour, sans reliquats du tour précédent — #251 (QA verte)

This commit is contained in:
2026-08-23 17:51:50 +02:00
parent d5fea391b2
commit 7b7ab5b95a
2 changed files with 151 additions and 35 deletions

View File

@ -1176,6 +1176,119 @@ describe("CustomAgentChatView", () => {
expect(screen.getByText("Terminé").closest(".justify-end")).toBeTruthy(); expect(screen.getByText("Terminé").closest(".justify-end")).toBeTruthy();
}); });
it("keeps successive turns isolated when backend userPrompt echoes arrive after progress (#251)", async () => {
const agent = {
launchAgentChat: vi.fn(),
reattachAgentChat: vi.fn(async (sessionId: string) => ({
sessionId,
scrollback: [],
})),
sendAgentChat: vi.fn(
async (_sessionId: string, prompt: string, onChunk: (chunk: ReplyChunk) => void) => {
onChunk({
kind: "progress",
progress: {
source: "providerNative",
kind: "tool",
stage: "started",
label: "shell",
toolName: "shell",
text: `Running ${prompt}`,
},
});
onChunk({ kind: "userPrompt", text: prompt });
onChunk({
kind: "progress",
progress: {
source: "providerNative",
kind: "tool",
stage: "completed",
label: "shell",
toolName: "shell",
text: `Done ${prompt}`,
},
});
onChunk({ kind: "final", content: `final ${prompt}` });
},
),
cancelAgentChat: vi.fn(async () => {}),
closeAgentChat: vi.fn(async () => {}),
};
render(
<DIProvider
gateways={{
agent,
system: { pickFile: vi.fn(async () => null) },
} as unknown as Gateways}
>
<CustomAgentChatView
projectId="project-1"
agentId="agent-1"
agentName="Worker"
profile={profile}
cwd="/repo"
nodeId="node-1"
sessionId="chat-session-1"
conversationId="conversation-1"
onSessionId={vi.fn()}
onConversationId={vi.fn()}
/>
</DIProvider>,
);
await waitFor(() =>
expect(agent.reattachAgentChat).toHaveBeenCalledWith(
"chat-session-1",
expect.any(Function),
),
);
fireEvent.change(screen.getByLabelText(/message CLI custom/), {
target: { value: "first request" },
});
fireEvent.click(screen.getByRole("button", { name: "Envoyer" }));
await screen.findByText("final first request");
fireEvent.change(screen.getByLabelText(/message CLI custom/), {
target: { value: "second request" },
});
fireEvent.click(screen.getByRole("button", { name: "Envoyer" }));
await screen.findByText("final second request");
expect(screen.getAllByText("first request")).toHaveLength(1);
expect(screen.getAllByText("second request")).toHaveLength(1);
expect(screen.getAllByTestId("chat-tool-progress")).toHaveLength(2);
expect(screen.queryByText("Running first request")).toBeNull();
expect(screen.queryByText("Running second request")).toBeNull();
expect(screen.getByText("Done first request")).toBeTruthy();
expect(screen.getByText("Done second request")).toBeTruthy();
expect(screen.getAllByText("Terminé")).toHaveLength(2);
const firstPrompt = screen.getByText("first request");
const firstProgress = screen.getByText("Done first request");
const firstFinal = screen.getByText("final first request");
const secondPrompt = screen.getByText("second request");
const secondProgress = screen.getByText("Done second request");
const secondFinal = screen.getByText("final second request");
expect(
Boolean(firstPrompt.compareDocumentPosition(firstProgress) & Node.DOCUMENT_POSITION_FOLLOWING),
).toBe(true);
expect(
Boolean(firstProgress.compareDocumentPosition(firstFinal) & Node.DOCUMENT_POSITION_FOLLOWING),
).toBe(true);
expect(
Boolean(firstFinal.compareDocumentPosition(secondPrompt) & Node.DOCUMENT_POSITION_FOLLOWING),
).toBe(true);
expect(
Boolean(secondPrompt.compareDocumentPosition(secondProgress) & Node.DOCUMENT_POSITION_FOLLOWING),
).toBe(true);
expect(
Boolean(secondProgress.compareDocumentPosition(secondFinal) & Node.DOCUMENT_POSITION_FOLLOWING),
).toBe(true);
});
it("renders canonical progress chunks as live agent, MCP, and delegation activity", async () => { it("renders canonical progress chunks as live agent, MCP, and delegation activity", async () => {
const agent = { const agent = {
launchAgentChat: vi.fn(), launchAgentChat: vi.fn(),
@ -2293,7 +2406,7 @@ describe("CustomAgentChatView", () => {
expect(screen.getByText("Progress").parentElement?.textContent).toContain("Terminé"); expect(screen.getByText("Progress").parentElement?.textContent).toContain("Terminé");
}); });
it("renders late progress chunks after the final answer without reopening the turn", async () => { it("ignores late progress chunks after the final answer without reopening the turn", async () => {
let emitChunk: ((chunk: unknown) => void) | null = null; let emitChunk: ((chunk: unknown) => void) | null = null;
const agent = { const agent = {
launchAgentChat: vi.fn(), launchAgentChat: vi.fn(),
@ -2368,7 +2481,7 @@ describe("CustomAgentChatView", () => {
}); });
expect(screen.getByText("done")).toBeTruthy(); expect(screen.getByText("done")).toBeTruthy();
expect(screen.getByText("idea_ticket_read")).toBeTruthy(); expect(screen.queryByText("idea_ticket_read")).toBeNull();
expect(screen.getByRole("button", { name: "Envoyer" })).toBeTruthy(); expect(screen.getByRole("button", { name: "Envoyer" })).toBeTruthy();
expect(screen.queryByRole("button", { name: "Stop" })).toBeNull(); expect(screen.queryByRole("button", { name: "Stop" })).toBeNull();
}); });

View File

@ -236,24 +236,31 @@ function appendUserPrompt(
text: string, text: string,
attachments: string[] = [], attachments: string[] = [],
receivedAtMs = Date.now(), receivedAtMs = Date.now(),
): ChatTurn[] { ): { turns: ChatTurn[]; appended: boolean } {
const last = turns[turns.length - 1]; for (let index = turns.length - 1; index >= 0; index -= 1) {
if ( const turn = turns[index];
last?.role === "user" && if (turn.role === "final" || turn.role === "error") break;
last.text === text && if (turn.role !== "user") continue;
JSON.stringify(last.attachments ?? []) === JSON.stringify(attachments) if (
) { turn.text === text &&
return turns; JSON.stringify(turn.attachments ?? []) === JSON.stringify(attachments)
) {
return { turns, appended: false };
}
break;
} }
return [ return {
...turns, turns: [
{ ...turns,
role: "user", {
text, role: "user",
receivedAtMs, text,
...(attachments.length > 0 ? { attachments } : {}), receivedAtMs,
}, ...(attachments.length > 0 ? { attachments } : {}),
]; },
],
appended: true,
};
} }
function isReplyProgressSource(value: unknown): value is ReplyProgressSource { function isReplyProgressSource(value: unknown): value is ReplyProgressSource {
@ -775,14 +782,6 @@ function foldChunk(
if (isRecoverableNoResponseTurn(last) && raw.kind !== "error") { if (isRecoverableNoResponseTurn(last) && raw.kind !== "error") {
return foldChunk({ ...state, turns: turns.slice(0, -1) }, raw, receivedAtMs); return foldChunk({ ...state, turns: turns.slice(0, -1) }, raw, receivedAtMs);
} }
if (raw.kind === "progress") {
return appendProgress(state, normalizeProgress(raw.progress) ?? {
source: "ideaLocal",
kind: "other",
stage: "info",
label: unknownChunkLabel(raw),
});
}
return state; return state;
} }
switch (raw.kind) { switch (raw.kind) {
@ -838,17 +837,21 @@ function foldChunk(
return { ...completedState, turns: next }; return { ...completedState, turns: next };
} }
case "userPrompt": case "userPrompt":
case "UserPrompt": case "UserPrompt": {
const appended = appendUserPrompt(
turns,
String(raw.text ?? raw.prompt ?? ""),
attachmentLabels(raw.attachments),
receivedAtMs,
);
return { return {
...state, ...state,
turns: appendUserPrompt( turns: appended.turns,
turns, progressScope: appended.appended
String(raw.text ?? raw.prompt ?? ""), ? state.progressScope + 1
attachmentLabels(raw.attachments), : state.progressScope,
receivedAtMs,
),
progressScope: state.progressScope + 1,
}; };
}
default: default:
return { return {
...state, ...state,