fix(application,backend,app-tauri,frontend): réconcilie l'état idle et les Finals après Stop — #357 (QA verte)

Cause: après un cancel manuel de tour, le ticket médié busy/FIFO n'était pas
retiré (l'agent restait Busy, bouton Stop persistant) et un terminal tardif du
tour annulé pouvait bloquer le tour suivant (Final non affiché).

Backend: StructuredSessions lie chaque génération de tour à son ticket médié
(bind_turn_accounting) et request_turn_cancel restitue l'identité (agent,
ticket) exacte observée avant annulation; CancelAgentChat.with_turn_accounting
retire le ticket exact (cancel_head + mark_idle) seulement s'il est toujours
le ticket courant — jamais un successeur déjà remplacé;
StructuredHumanTurnGuard expose accounting_identity et agent_send (app-tauri)
lie la comptabilité au démarrage du tour humain.

Frontend: CustomAgentChatView réconcilie l'interaction active depuis le
work-state (agent idle sans délégation -> interaction terminée, fallback
« Tour terminé sans réponse finale. ») au lieu de laisser un agentBusyChanged
sans tour-id clore un tour plus récent; les terminaux tardifs (final/error) du
tour read-model-settled remplacent le fallback; tour suivant isolé des
callbacks tardifs de la génération précédente.

Tests: cancel_agent_chat (retrait du ticket exact avant le prompt suivant,
nettoyage tardif sans idle sur ticket successeur), CustomAgentChatView
(isolation tour suivant, réconciliation work-state + remplacement fallback par
Final tardif).

Réserve non bloquante: alignement fixture skills préexistant porté par le
commit test dédié 83659d9a.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-09-14 15:13:03 +02:00
parent 83659d9a09
commit 50e395b602
7 changed files with 414 additions and 36 deletions

View File

@ -1780,6 +1780,112 @@ describe("CustomAgentChatView", () => {
expect(screen.queryByRole("button", { name: "Stop" })).toBeNull();
});
it("keeps a following turn isolated from a late terminal callback of the previous turn (#357)", async () => {
const harness = createStreamingAgentHarness();
render(
<DIProvider
gateways={{
agent: harness.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>,
);
const composer = await screen.findByLabelText(/Message CLI personnalisée/);
fireEvent.change(composer, { target: { value: "first turn" } });
fireEvent.click(screen.getByRole("button", { name: "Envoyer" }));
await waitFor(() => expect(harness.emitterCount()).toBe(1));
act(() => harness.emit(0, { kind: "final", content: "First final" }));
await screen.findByText("First final");
fireEvent.change(composer, { target: { value: "second turn" } });
fireEvent.click(screen.getByRole("button", { name: "Envoyer" }));
await waitFor(() => expect(harness.emitterCount()).toBe(2));
act(() => harness.emit(0, { kind: "final", content: "Late first final" }));
expect(screen.queryByText("Late first final")).toBeNull();
expect(screen.getByRole("button", { name: "Stop" })).toBeTruthy();
act(() => harness.emit(1, { kind: "final", content: "Second final" }));
expect(await screen.findByText("Second final")).toBeTruthy();
expect(screen.queryByRole("button", { name: "Stop" })).toBeNull();
expect(screen.getByRole("button", { name: "Envoyer" })).toBeTruthy();
});
it("reconciles an active composer from the terminal work-state and replaces its fallback with a late Final (#357)", async () => {
const harness = createStreamingAgentHarness();
const system = new MockSystemGateway();
const workState = new MockWorkStateGateway();
const stateFor = (busy: boolean) => ({
agents: [
{
agentId: "agent-1",
name: "Worker",
profileId: "structured",
busy: busy
? ({ state: "busy", ticket: "turn-357", sinceMs: 1 } as const)
: ({ state: "idle" } as const),
tickets: [],
},
],
conversations: [],
});
workState._setProjectWorkState("project-1", stateFor(false));
render(
<DIProvider
gateways={{ agent: harness.agent, system, workState } 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>,
);
const composer = await screen.findByLabelText(/Message CLI personnalisée/);
await waitFor(() => expect(screen.getByRole("button", { name: "Envoyer" })).toBeTruthy());
workState._setProjectWorkState("project-1", stateFor(true));
fireEvent.change(composer, { target: { value: "turn without projected final" } });
fireEvent.click(screen.getByRole("button", { name: "Envoyer" }));
await waitFor(() => expect(harness.emitterCount()).toBe(1));
expect(screen.getByRole("button", { name: "Stop" })).toBeTruthy();
workState._setProjectWorkState("project-1", stateFor(false));
act(() => system.emit({ type: "agentBusyChanged", agentId: "agent-1", busy: false }));
expect(await screen.findByText("Tour terminé sans réponse finale.")).toBeTruthy();
expect(screen.queryByRole("button", { name: "Stop" })).toBeNull();
expect(screen.getByRole("button", { name: "Envoyer" })).toBeTruthy();
act(() => harness.emit(0, { kind: "final", content: "Recovered late final" }));
expect(await screen.findByText("Recovered late final")).toBeTruthy();
expect(screen.queryByText("Tour terminé sans réponse finale.")).toBeNull();
expect(screen.queryByRole("button", { name: "Stop" })).toBeNull();
});
it.each(["alreadyCancelled", "alreadyCompleted"] as const)(
"treats backend outcome %s as one already-stopped terminal result",
async (outcome) => {