feat(terminals): reprise de conversation par cellule + fix ordre d'écriture

Permet de recharger la conversation CLI précédente de chaque cellule à la
réouverture du projet, de façon universelle (indépendant du modèle/CLI).

- profil AgentRuntime: bloc déclaratif optionnel `session { assignFlag, resumeFlag }`
- LeafCell: `conversationId` (persistant, distinct du SessionId PTY) + `agentWasRunning`
- runtime: SessionPlan (None/Assign/Resume) + composition pure des args
- LaunchAgent: décide Assign vs Resume, génère l'UUID, remonte l'id assigné
  (persistance par l'appelant via setCellConversation — découplage SRP)
- close: SnapshotRunningAgents fige `agentWasRunning` avant le kill-all
  (statut clot/en cours universel, sans parsing CLI)
- SessionInspector: port optionnel best-effort + adapter ClaudeTranscriptInspector
- popup de reprise par cellule (statut + sujet/tokens si dispo), intercalée
  avant le Resume auto, jamais sur le chemin reattach

fix(terminals): sérialise les écritures PTY (file FIFO par handle) — corrige
les caractères mélangés/accents dus au réordonnancement des invoke Tauri concurrents

fix(layout): l'opération `move` préservait mal les champs du leaf (perdait `agent`)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-07 22:27:08 +02:00
parent d11eaaa8c0
commit 3ed0f6b45f
61 changed files with 5098 additions and 98 deletions

View File

@ -20,7 +20,12 @@ import { useEffect, useRef, useState } from "react";
import type { Agent } from "@/domain";
import type { LayoutNode } from "@/domain";
import { TerminalView } from "@/features/terminals";
import type {
ConversationDetails,
OpenTerminalOptions,
TerminalHandle,
} from "@/ports";
import { ResumeConversationPopup, TerminalView } from "@/features/terminals";
import { useGateways } from "@/app/di";
import { normalizeWeights, resizeAdjacent } from "./layout";
import { useLayout, type LayoutViewModel } from "./useLayout";
@ -89,6 +94,8 @@ function NodeView({ node, cwd, vm, parentSplit, projectId }: NodeViewProps) {
id={node.node.id}
session={node.node.session ?? null}
agent={node.node.agent ?? null}
conversationId={node.node.conversationId ?? null}
agentWasRunning={node.node.agentWasRunning ?? false}
cwd={cwd}
vm={vm}
parentSplit={parentSplit}
@ -106,13 +113,28 @@ interface LeafViewProps {
id: string;
session: string | null;
agent: string | null;
conversationId: string | null;
agentWasRunning: boolean;
cwd: string;
vm: LayoutViewModel;
parentSplit: { container: string; index: number; siblings: number } | null;
projectId: string;
}
function LeafView({ id, session, agent, cwd, vm, parentSplit, projectId }: LeafViewProps) {
/**
* A launch deferred by the resume popup (T7): TerminalView asked the opener to
* launch (fresh open or reattach-failed fallback) for a cell that carries a
* `conversationId`. We hold the open request here until the user picks
* Reprendre / Nouvelle conversation, then resolve the promise with the handle.
*/
interface PendingResume {
opts: OpenTerminalOptions;
onData: (bytes: Uint8Array) => void;
resolve: (handle: TerminalHandle) => void;
reject: (e: unknown) => void;
}
function LeafView({ id, session, agent, conversationId, agentWasRunning, cwd, vm, parentSplit, projectId }: LeafViewProps) {
// A cell can be closed only when it lives inside a (binary) split: closing it
// collapses the parent split, keeping the *sibling*. Splits are always binary
// in this model (a split wraps a leaf into a 2-child container), so the kept
@ -136,10 +158,77 @@ function LeafView({ id, session, agent, cwd, vm, parentSplit, projectId }: LeafV
// Build the terminal opener based on whether an agent is pinned.
const agentId = agent ?? null;
// ── Resume popup state (T7) ───────────────────────────────────────────────
// When an agent cell carries a persisted conversation id and its PTY session
// is dead, the opener is about to relaunch in Resume mode. We intercept that
// launch with a popup so the user can choose Resume vs a fresh conversation.
// The reattach path (live PTY) never goes through this opener, so the popup is
// naturally skipped there.
const [pendingResume, setPendingResume] = useState<PendingResume | null>(null);
const [resumeDetails, setResumeDetails] = useState<ConversationDetails | null>(null);
/** Performs the actual launch and persists any assigned id (T4b loop). */
const doLaunch = async (
opts: OpenTerminalOptions,
onData: (bytes: Uint8Array) => void,
convId: string | undefined,
): Promise<TerminalHandle> => {
const handle = await agentGateway!.launchAgent(
projectId,
agentId!,
{ ...opts, conversationId: convId },
onData,
);
// First launch on a fresh cell mints a conversation id: persist it on the
// leaf so the next open resumes (T4b — closes the persistence loop).
if (handle.assignedConversationId) {
void vm.setCellConversation(id, handle.assignedConversationId);
}
return handle;
};
const terminalOpener = agentGateway && agentId
? (opts: Parameters<typeof agentGateway.launchAgent>[2], onData: (bytes: Uint8Array) => void) =>
agentGateway.launchAgent(projectId, agentId, opts, onData)
? (opts: OpenTerminalOptions, onData: (bytes: Uint8Array) => void): Promise<TerminalHandle> => {
// No persisted conversation ⇒ fresh cell: open straight away (the launch
// may assign a new id). No popup on this path.
if (!conversationId) {
return doLaunch(opts, onData, undefined);
}
// Resume case: defer the launch behind the popup. Fetch the best-effort
// enriched details (last topic + tokens) to enrich it; failure or empty
// ⇒ degraded mode (status only). Inspection never blocks the resume.
Promise.resolve(
agentGateway.inspectConversation?.(projectId, agentId, conversationId),
)
.then((details) => setResumeDetails(details ?? {}))
.catch(() => setResumeDetails({}));
return new Promise<TerminalHandle>((resolve, reject) => {
setPendingResume({ opts, onData, resolve, reject });
});
}
: undefined;
/** "Reprendre" → launch resuming the existing conversation id. */
const onResume = () => {
const p = pendingResume;
if (!p) return;
setPendingResume(null);
setResumeDetails(null);
doLaunch(p.opts, p.onData, conversationId ?? undefined).then(p.resolve, p.reject);
};
/** "Nouvelle conversation" → clear the id first (Assign), then launch fresh. */
const onNewConversation = () => {
const p = pendingResume;
if (!p) return;
setPendingResume(null);
setResumeDetails(null);
// Clear the persisted conversation id BEFORE launching so the backend treats
// it as a fresh cell and assigns a new conversation.
void vm.setCellConversation(id, null);
doLaunch(p.opts, p.onData, undefined).then(p.resolve, p.reject);
};
// Agent cells re-attach through the agent gateway; plain cells fall back to
// the terminal gateway's reattach (handled by TerminalView's default).
const reattachOpener = agentGateway && agentId
@ -240,6 +329,14 @@ function LeafView({ id, session, agent, cwd, vm, parentSplit, projectId }: LeafV
sessionId={session}
onSessionId={(sid) => void vm.setSession(id, sid)}
/>
{pendingResume && (
<ResumeConversationPopup
agentWasRunning={agentWasRunning}
details={resumeDetails}
onResume={onResume}
onNewConversation={onNewConversation}
/>
)}
</div>
);
}

View File

@ -10,6 +10,7 @@ import { describe, it, expect } from "vitest";
import type { LayoutTree } from "@/domain";
import {
applyOperation,
droppedSessions,
leaves,
normalizeWeights,
resizeAdjacent,
@ -175,6 +176,222 @@ describe("applyOperation", () => {
});
});
describe("applyOperation — conversationId / agentWasRunning persistence", () => {
const base = (): LayoutTree => singleLeafTree("a");
// A single leaf "a" carrying both new fields (the T8 invariant under test).
const seeded = (): LayoutTree => ({
root: {
type: "leaf",
node: { id: "a", session: null, conversationId: "conv-1", agentWasRunning: true },
},
});
// --- Non-regression of the propagation trap (#1) ---------------------------
it("setSession preserves conversationId and agentWasRunning", () => {
const after = applyOperation(seeded(), {
type: "setSession",
target: "a",
session: "sess-1",
});
const leaf = leaves(after)[0];
expect(leaf.session).toBe("sess-1");
expect(leaf.conversationId).toBe("conv-1");
expect(leaf.agentWasRunning).toBe(true);
});
it("setCellAgent preserves conversationId and agentWasRunning", () => {
const after = applyOperation(seeded(), {
type: "setCellAgent",
target: "a",
agent: "agent-7",
});
const leaf = leaves(after)[0];
expect(leaf.agent).toBe("agent-7");
expect(leaf.conversationId).toBe("conv-1");
expect(leaf.agentWasRunning).toBe(true);
});
it("move preserves conversationId and agentWasRunning on both ends", () => {
// from = "a" (seeded, with a session to move); to = "b" (empty), both carrying
// the new fields. Build a split so both leaves coexist.
let tree: LayoutTree = {
root: {
type: "split",
node: {
id: "c",
direction: "row",
children: [
{
node: {
type: "leaf",
node: {
id: "a",
session: "sess-a",
conversationId: "conv-a",
agentWasRunning: true,
},
},
weight: 1,
},
{
node: {
type: "leaf",
node: {
id: "b",
session: null,
conversationId: "conv-b",
agentWasRunning: true,
},
},
weight: 1,
},
],
},
},
};
tree = applyOperation(tree, { type: "move", from: "a", to: "b" });
const byId = Object.fromEntries(leaves(tree).map((l) => [l.id, l]));
// Session moved a → b…
expect(byId.a.session ?? null).toBeNull();
expect(byId.b.session).toBe("sess-a");
// …but the new fields stay put on both leaves.
expect(byId.a.conversationId).toBe("conv-a");
expect(byId.a.agentWasRunning).toBe(true);
expect(byId.b.conversationId).toBe("conv-b");
expect(byId.b.agentWasRunning).toBe(true);
});
// --- setCellConversation ---------------------------------------------------
it("setCellConversation sets the conversationId on the target leaf", () => {
const after = applyOperation(base(), {
type: "setCellConversation",
target: "a",
conversationId: "conv-9",
});
expect(leaves(after)[0].conversationId).toBe("conv-9");
});
it("setCellConversation with null clears (omits) the conversationId", () => {
const set = applyOperation(base(), {
type: "setCellConversation",
target: "a",
conversationId: "conv-9",
});
const cleared = applyOperation(set, {
type: "setCellConversation",
target: "a",
conversationId: null,
});
expect(leaves(cleared)[0].conversationId).toBeUndefined();
expect("conversationId" in leaves(cleared)[0]).toBe(false);
});
it("setCellConversation throws NOT_FOUND on an unknown target", () => {
expect(() =>
applyOperation(base(), {
type: "setCellConversation",
target: "missing",
conversationId: "x",
}),
).toThrowError(/not found/);
});
// --- setAgentRunning -------------------------------------------------------
it("setAgentRunning true sets the flag", () => {
const after = applyOperation(base(), {
type: "setAgentRunning",
target: "a",
running: true,
});
expect(leaves(after)[0].agentWasRunning).toBe(true);
});
it("setAgentRunning false clears (omits) the flag", () => {
const set = applyOperation(base(), {
type: "setAgentRunning",
target: "a",
running: true,
});
const cleared = applyOperation(set, {
type: "setAgentRunning",
target: "a",
running: false,
});
expect(leaves(cleared)[0].agentWasRunning).toBeUndefined();
expect("agentWasRunning" in leaves(cleared)[0]).toBe(false);
});
it("setAgentRunning throws NOT_FOUND on an unknown target", () => {
expect(() =>
applyOperation(base(), {
type: "setAgentRunning",
target: "missing",
running: true,
}),
).toThrowError(/not found/);
});
// --- independence of conversationId and session ----------------------------
it("a leaf can carry conversationId without a session, and vice versa", () => {
// conversationId without session.
const convOnly = applyOperation(base(), {
type: "setCellConversation",
target: "a",
conversationId: "conv-only",
});
expect(leaves(convOnly)[0].conversationId).toBe("conv-only");
expect(leaves(convOnly)[0].session ?? null).toBeNull();
// session without conversationId.
const sessOnly = applyOperation(base(), {
type: "setSession",
target: "a",
session: "sess-only",
});
expect(leaves(sessOnly)[0].session).toBe("sess-only");
expect(leaves(sessOnly)[0].conversationId).toBeUndefined();
});
});
describe("droppedSessions", () => {
// A split "c" with children a (kept) and b (dropped), each holding a session.
const split = (): LayoutTree => {
let tree = applyOperation(singleLeafTree("a"), {
type: "split",
target: "a",
direction: "row",
newLeaf: "b",
container: "c",
});
tree = applyOperation(tree, { type: "setSession", target: "a", session: "sess-a" });
tree = applyOperation(tree, { type: "setSession", target: "b", session: "sess-b" });
return tree;
};
it("returns the sessions of the children other than keepIndex", () => {
// Keep child 0 (a) → b is dropped.
expect(droppedSessions(split(), "c", 0)).toEqual(["sess-b"]);
// Keep child 1 (b) → a is dropped.
expect(droppedSessions(split(), "c", 1)).toEqual(["sess-a"]);
});
it("ignores leaves with no session", () => {
const tree = applyOperation(singleLeafTree("a"), {
type: "split",
target: "a",
direction: "row",
newLeaf: "b",
container: "c",
});
// Neither leaf has a session yet.
expect(droppedSessions(tree, "c", 0)).toEqual([]);
});
it("returns [] for an unknown container", () => {
expect(droppedSessions(split(), "missing", 0)).toEqual([]);
});
});
describe("splitOp", () => {
it("builds a split op with fresh ids", () => {
const op = splitOp("a", "column");

View File

@ -201,11 +201,13 @@ export function applyOperation(
if (target === undefined) throw notFound(op.to);
if (target !== null) throw invalid("target cell is occupied");
const root = mapNode(tree.root, (n) => {
// Preserve agent / conversationId / agentWasRunning on both ends — only
// the ephemeral `session` moves (mirror of the Rust `move_session`).
if (n.type === "leaf" && n.node.id === op.from) {
return { type: "leaf", node: { id: n.node.id, session: null } };
return { type: "leaf", node: { ...n.node, session: null } };
}
if (n.type === "leaf" && n.node.id === op.to) {
return { type: "leaf", node: { id: n.node.id, session } };
return { type: "leaf", node: { ...n.node, session } };
}
return n;
});
@ -242,6 +244,40 @@ export function applyOperation(
if (!found) throw notFound(op.target);
return { root };
}
case "setCellConversation": {
let found = false;
const root = mapNode(tree.root, (n) => {
if (n.type === "leaf" && n.node.id === op.target) {
found = true;
const { conversationId: _c, ...rest } = n.node;
// Omit when null (mirrors the backend `skip_serializing_if`).
const updated = op.conversationId !== null
? { ...rest, conversationId: op.conversationId }
: rest;
return { type: "leaf", node: updated };
}
return n;
});
if (!found) throw notFound(op.target);
return { root };
}
case "setAgentRunning": {
let found = false;
const root = mapNode(tree.root, (n) => {
if (n.type === "leaf" && n.node.id === op.target) {
found = true;
const { agentWasRunning: _r, ...rest } = n.node;
// Omit when false (mirrors the backend `skip_serializing_if`).
const updated = op.running
? { ...rest, agentWasRunning: true }
: rest;
return { type: "leaf", node: updated };
}
return n;
});
if (!found) throw notFound(op.target);
return { root };
}
}
}
@ -255,6 +291,34 @@ export function leaves(tree: LayoutTree): LeafCell[] {
return out;
}
/**
* Sessions hosted by the children a `merge` would **drop** — every child of
* `container` except the kept one, walked recursively (a dropped child may be a
* nested split with several terminals). Used by the close button to tear down
* the discarded PTYs instead of orphaning them, mirroring `setCellAgent`. Returns
* `[]` if the container isn't found or holds no sessions.
*/
export function droppedSessions(
tree: LayoutTree,
container: string,
keepIndex: number,
): string[] {
const out: string[] = [];
mapNode(tree.root, (n) => {
if (n.type === "split" && n.node.id === container) {
n.node.children.forEach((c, i) => {
if (i === keepIndex) return;
mapNode(c.node, (m) => {
if (m.type === "leaf" && m.node.session) out.push(m.node.session);
return m;
});
});
}
return n;
});
return out;
}
/** Convenience: builds a `split` operation splitting `target` in `direction`. */
export function splitOp(target: string, direction: Direction): LayoutOperation {
return {

View File

@ -0,0 +1,316 @@
/**
* T7 — the resume-conversation popup and its wiring in the layout grid.
*
* Two layers:
* 1. The presentational {@link ResumeConversationPopup} in isolation: status
* derived from `agentWasRunning` (never the inspector); enriched mode (topic
* + tokens) vs degraded mode (status only); the two action callbacks.
* 2. The grid wiring (`LayoutGrid` → `LeafView`): an agent cell that carries a
* persisted `conversationId` shows the popup **before** the Resume launch;
* "Reprendre" launches with the id; "Nouvelle conversation" clears the id
* first then launches without it. The reattach path (live PTY) never shows
* the popup.
*
* Because xterm bails under jsdom, `TerminalView`'s opener may not run in this
* environment — so the grid-level assertions render the popup by driving the
* opener directly is not possible. Instead we drive the popup through the
* component-level tests (deterministic) and assert the *opener wiring* by
* calling the exported component with stub gateways where the opener is invoked
* manually. To stay robust we focus the integration assertions on the popup
* presence + the two action effects, guarded on the opener having run.
*/
import { useEffect } from "react";
import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import type { ConversationDetails } from "@/ports";
import { ResumeConversationPopup } from "@/features/terminals";
// ---------------------------------------------------------------------------
// 1. Presentational popup in isolation
// ---------------------------------------------------------------------------
describe("ResumeConversationPopup — presentation", () => {
it("derives status 'en cours' from agentWasRunning=true (not the inspector)", () => {
render(
<ResumeConversationPopup
agentWasRunning
details={{ lastTopic: "x", tokenCount: 5 }}
onResume={() => {}}
onNewConversation={() => {}}
/>,
);
expect(screen.getByTestId("resume-status").textContent).toBe("en cours");
});
it("derives status 'clot' from agentWasRunning=false", () => {
render(
<ResumeConversationPopup
agentWasRunning={false}
details={{}}
onResume={() => {}}
onNewConversation={() => {}}
/>,
);
expect(screen.getByTestId("resume-status").textContent).toBe("clot");
});
it("enriched mode shows last topic + token count when present", () => {
render(
<ResumeConversationPopup
agentWasRunning
details={{ lastTopic: "refactor parser", tokenCount: 4242 }}
onResume={() => {}}
onNewConversation={() => {}}
/>,
);
expect(screen.getByTestId("resume-last-topic").textContent).toContain(
"refactor parser",
);
expect(screen.getByTestId("resume-token-count").textContent).toContain("4242");
});
it("degraded mode (empty details) shows status only, no topic/token lines", () => {
render(
<ResumeConversationPopup
agentWasRunning={false}
details={{}}
onResume={() => {}}
onNewConversation={() => {}}
/>,
);
expect(screen.getByTestId("resume-status").textContent).toBe("clot");
expect(screen.queryByTestId("resume-last-topic")).toBeNull();
expect(screen.queryByTestId("resume-token-count")).toBeNull();
});
it("details=null (loading) shows status only", () => {
render(
<ResumeConversationPopup
agentWasRunning
details={null}
onResume={() => {}}
onNewConversation={() => {}}
/>,
);
expect(screen.getByTestId("resume-status").textContent).toBe("en cours");
expect(screen.queryByTestId("resume-last-topic")).toBeNull();
});
it("token count of 0 still renders (uses !== undefined, not truthiness)", () => {
render(
<ResumeConversationPopup
agentWasRunning
details={{ tokenCount: 0 }}
onResume={() => {}}
onNewConversation={() => {}}
/>,
);
expect(screen.getByTestId("resume-token-count").textContent).toContain("0");
});
it("fires onResume / onNewConversation on the matching buttons", () => {
const onResume = vi.fn();
const onNewConversation = vi.fn();
render(
<ResumeConversationPopup
agentWasRunning
details={{}}
onResume={onResume}
onNewConversation={onNewConversation}
/>,
);
fireEvent.click(screen.getByText(/Reprendre la dernière conversation/));
expect(onResume).toHaveBeenCalledTimes(1);
fireEvent.click(screen.getByText(/Nouvelle conversation/));
expect(onNewConversation).toHaveBeenCalledTimes(1);
});
});
// ---------------------------------------------------------------------------
// 2. Grid wiring
// ---------------------------------------------------------------------------
import { render as renderGridDom } from "@testing-library/react";
import type { Gateways, OpenTerminalOptions, TerminalHandle } from "@/ports";
import { MockLayoutGateway, MockAgentGateway, MockTerminalGateway } from "@/adapters/mock";
import { DIProvider } from "@/app/di";
import { leaves } from "./layout";
// xterm bails under jsdom, so the real `TerminalView` never runs its opener
// (and thus never reaches our deferral/popup wiring). Replace it with a stub
// that immediately invokes the `open` opener — exactly what the real view does
// once the PTY mount succeeds — so the deferral → popup → action → launch path
// is deterministically exercised. `ResumeConversationPopup` is NOT mocked (we
// assert its rendered output). Importing `LayoutGrid` after the mock is set up.
vi.mock("@/features/terminals", async () => {
const actual = await vi.importActual<typeof import("@/features/terminals")>(
"@/features/terminals",
);
return {
...actual,
TerminalView: ({
open,
}: {
open?: (
o: OpenTerminalOptions,
onData: (b: Uint8Array) => void,
) => Promise<TerminalHandle>;
}) => {
// Mirror the real view: call the opener once on mount (fresh-open path).
// In an effect (not during render) so the opener's setState is safe.
useEffect(() => {
if (open) void open({ cwd: "/x", rows: 24, cols: 80 }, () => {});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
return <div data-testid="terminal-view" />;
},
};
});
// eslint-disable-next-line import/first
import { LayoutGrid } from "./LayoutGrid";
function makeHandle(sessionId: string): TerminalHandle {
return {
sessionId,
write: vi.fn().mockResolvedValue(undefined),
resize: vi.fn().mockResolvedValue(undefined),
detach: vi.fn(),
close: vi.fn().mockResolvedValue(undefined),
};
}
function renderGrid(layout: MockLayoutGateway, agent: unknown, projectId = "p1") {
const gateways = {
layout,
terminal: new MockTerminalGateway(),
agent,
} as unknown as Gateways;
return renderGridDom(
<DIProvider gateways={gateways}>
<LayoutGrid projectId={projectId} cwd="/home/me/proj" />
</DIProvider>,
);
}
/** Seeds an agent on the root leaf carrying a persisted conversation id but no
* live session — the Resume case that must trigger the popup. */
async function seedResumeLeaf(layout: MockLayoutGateway, agentId: string) {
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
await layout.mutateLayout("p1", { type: "setCellAgent", target: leafId, agent: agentId });
await layout.mutateLayout("p1", {
type: "setCellConversation",
target: leafId,
conversationId: "prior-conv",
});
return leafId;
}
describe("LayoutGrid — resume popup wiring (T7)", () => {
it("shows the popup for a resume cell and 'Reprendre' launches with the conversation id", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
await seedResumeLeaf(layout, a.id);
const launchAgent = vi.fn(
async (
_p: string,
_ag: string,
_opts: OpenTerminalOptions,
_onData: (b: Uint8Array) => void,
) => makeHandle("sess-resume"),
);
const inspectConversation = vi.fn(
async (): Promise<ConversationDetails> => ({ lastTopic: "topic-x", tokenCount: 7 }),
);
const stubAgent = {
listAgents: () => Promise.resolve([a]),
launchAgent,
reattach: vi.fn(),
inspectConversation,
};
renderGrid(layout, stubAgent);
// The popup appears (opener deferred behind it) and the launch is gated.
await waitFor(() =>
expect(screen.getByTestId("resume-conversation-popup")).toBeTruthy(),
);
expect(launchAgent).not.toHaveBeenCalled();
expect(inspectConversation).toHaveBeenCalledWith("p1", a.id, "prior-conv");
fireEvent.click(screen.getByText(/Reprendre la dernière conversation/));
await waitFor(() => expect(launchAgent).toHaveBeenCalledTimes(1));
const opts = launchAgent.mock.calls[0][2] as OpenTerminalOptions;
expect(opts.conversationId).toBe("prior-conv");
// The id stays on the leaf (resume keeps it).
const fresh = await layout.loadLayout("p1");
expect(leaves(fresh)[0].conversationId).toBe("prior-conv");
});
it("'Nouvelle conversation' clears the id first, then launches without it", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
await seedResumeLeaf(layout, a.id);
const launchAgent = vi.fn(
async (
_p: string,
_ag: string,
_opts: OpenTerminalOptions,
_onData: (b: Uint8Array) => void,
) => makeHandle("sess-new"),
);
const stubAgent = {
listAgents: () => Promise.resolve([a]),
launchAgent,
reattach: vi.fn(),
inspectConversation: vi.fn(async (): Promise<ConversationDetails> => ({})),
};
renderGrid(layout, stubAgent);
await waitFor(() =>
expect(screen.getByTestId("resume-conversation-popup")).toBeTruthy(),
);
expect(launchAgent).not.toHaveBeenCalled();
fireEvent.click(screen.getByText(/Nouvelle conversation/));
await waitFor(() => expect(launchAgent).toHaveBeenCalledTimes(1));
const opts = launchAgent.mock.calls[0][2] as OpenTerminalOptions;
expect(opts.conversationId).toBeUndefined();
// The id was cleared on the leaf before launching (Assign on next launch).
const fresh = await layout.loadLayout("p1");
expect(leaves(fresh)[0].conversationId).toBeUndefined();
});
it("a fresh cell (no conversation id) does NOT show the popup", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
await layout.mutateLayout("p1", { type: "setCellAgent", target: leafId, agent: a.id });
const inspectConversation = vi.fn();
const stubAgent = {
listAgents: () => Promise.resolve([a]),
launchAgent: vi.fn(async () => makeHandle("sess-fresh")),
reattach: vi.fn(),
inspectConversation,
};
renderGrid(layout, stubAgent);
await waitFor(() => expect(screen.getByRole("combobox")).toBeTruthy());
// No popup, and inspection is never triggered for a fresh cell.
expect(screen.queryByTestId("resume-conversation-popup")).toBeNull();
expect(inspectConversation).not.toHaveBeenCalled();
});
});

View File

@ -2,7 +2,7 @@
* #3 — Agent dropdown per cell: setCellAgent persists in the layout, and the
* terminal re-mounts with the agent opener; "Plain" reverts to a plain terminal.
*/
import { describe, it, expect } from "vitest";
import { describe, it, expect, vi } from "vitest";
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
import type { Gateways } from "@/ports";
@ -19,10 +19,11 @@ function renderGrid(
layout: MockLayoutGateway,
agent: MockAgentGateway,
projectId = "p1",
terminal: MockTerminalGateway = new MockTerminalGateway(),
) {
const gateways = {
layout,
terminal: new MockTerminalGateway(),
terminal,
agent,
} as unknown as Gateways;
return render(
@ -134,6 +135,114 @@ describe("setCellAgent (agent dropdown per cell)", () => {
});
});
it("changing the agent resets the cell session to null (Bug #3)", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
// Pre-set a running session on the leaf (as if a plain terminal had opened).
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
await layout.mutateLayout("p1", {
type: "setSession",
target: leafId,
session: "old-session",
});
renderGrid(layout, agent);
await waitFor(() => expect(screen.getByRole("combobox")).toBeTruthy());
fireEvent.change(screen.getByRole("combobox"), { target: { value: a.id } });
await waitFor(async () => {
const fresh = await layout.loadLayout("p1");
const l = leaves(fresh)[0];
expect(l.agent).toBe(a.id);
// The stale session must be cleared so the new agent opens fresh.
expect(l.session === null || l.session === undefined).toBe(true);
});
});
it("changing the agent kills the previous PTY (Bug #3)", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const terminal = new MockTerminalGateway();
const closeSpy = vi.spyOn(terminal, "closeTerminal");
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
await layout.mutateLayout("p1", {
type: "setSession",
target: leafId,
session: "old-session",
});
renderGrid(layout, agent, "p1", terminal);
await waitFor(() => expect(screen.getByRole("combobox")).toBeTruthy());
fireEvent.change(screen.getByRole("combobox"), { target: { value: a.id } });
await waitFor(() => {
expect(closeSpy).toHaveBeenCalledWith("old-session");
});
});
it("does not kill any PTY when the cell had no session (Bug #3)", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const terminal = new MockTerminalGateway();
const closeSpy = vi.spyOn(terminal, "closeTerminal");
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
renderGrid(layout, agent, "p1", terminal);
await waitFor(() => expect(screen.getByRole("combobox")).toBeTruthy());
fireEvent.change(screen.getByRole("combobox"), { target: { value: a.id } });
await waitFor(async () => {
const fresh = await layout.loadLayout("p1");
expect(leaves(fresh)[0].agent).toBe(a.id);
});
expect(closeSpy).not.toHaveBeenCalled();
});
it("closing a cell kills the dropped cell's PTY (no orphan)", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const terminal = new MockTerminalGateway();
const closeSpy = vi.spyOn(terminal, "closeTerminal");
// Split the root into two cells, then give the second a running session.
const tree = await layout.loadLayout("p1");
const rootId = leaves(tree)[0].id;
const afterSplit = await layout.mutateLayout("p1", {
type: "split",
target: rootId,
direction: "row",
newLeaf: "leaf-b",
container: "split-c",
});
const droppedId = leaves(afterSplit)[1].id;
await layout.mutateLayout("p1", {
type: "setSession",
target: droppedId,
session: "dropped-session",
});
renderGrid(layout, agent, "p1", terminal);
// Close the SECOND cell: keep the first, drop the second → its PTY dies.
await waitFor(() => {
expect(screen.getByLabelText(`close ${droppedId}`)).toBeTruthy();
});
fireEvent.click(screen.getByLabelText(`close ${droppedId}`));
await waitFor(() => {
expect(closeSpy).toHaveBeenCalledWith("dropped-session");
});
});
it("agent selector shows project agents as options", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();

View File

@ -0,0 +1,199 @@
/**
* T4b — closing the conversation-id persistence loop.
*
* Three layers, mirroring the SetCellAgent coverage:
* 1. The mock agent gateway assigns a fresh conversation id on a first launch
* (cell had none) and resumes (no new id) when one is passed in.
* 2. `MockLayoutGateway` applies the `setCellConversation` op, persisting/clearing
* the id on the leaf.
* 3. The grid wiring: when an agent cell launches and the backend assigns an id,
* `setCellConversation` is emitted so the next open resumes; and a leaf that
* already carries an id passes it back into `launchAgent`.
*
* As elsewhere (TerminalView.test), xterm may bail under jsdom, so the render-level
* assertions only fire when the opener actually ran.
*/
import { describe, it, expect, vi } from "vitest";
import { render, screen, waitFor } from "@testing-library/react";
import type { Gateways, OpenTerminalOptions, TerminalHandle } from "@/ports";
import {
MockLayoutGateway,
MockAgentGateway,
MockTerminalGateway,
} from "@/adapters/mock";
import { DIProvider } from "@/app/di";
import { LayoutGrid } from "./LayoutGrid";
import { leaves } from "./layout";
function renderGrid(
layout: MockLayoutGateway,
agent: { launchAgent: unknown; listAgents: unknown; reattach: unknown },
projectId = "p1",
terminal: MockTerminalGateway = new MockTerminalGateway(),
) {
const gateways = { layout, terminal, agent } as unknown as Gateways;
return render(
<DIProvider gateways={gateways}>
<LayoutGrid projectId={projectId} cwd="/home/me/proj" />
</DIProvider>,
);
}
describe("setCellConversation — mock layout gateway", () => {
it("persists then clears the conversation id on the leaf", async () => {
const layout = new MockLayoutGateway();
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
const after = await layout.mutateLayout("p1", {
type: "setCellConversation",
target: leafId,
conversationId: "conv-42",
});
expect(leaves(after)[0].conversationId).toBe("conv-42");
const cleared = await layout.mutateLayout("p1", {
type: "setCellConversation",
target: leafId,
conversationId: null,
});
expect(leaves(cleared)[0].conversationId).toBeUndefined();
});
});
describe("MockAgentGateway.launchAgent — session assignment", () => {
it("assigns a fresh conversation id when the cell has none", async () => {
const agent = new MockAgentGateway();
await agent.createAgent("p1", { name: "A", profileId: "p" });
const list = await agent.listAgents("p1");
const handle = await agent.launchAgent(
"p1",
list[0].id,
{ cwd: "/x", rows: 24, cols: 80 } as OpenTerminalOptions,
() => {},
);
expect(handle.assignedConversationId).toBeTruthy();
});
it("does NOT assign a new id when resuming an existing conversation", async () => {
const agent = new MockAgentGateway();
await agent.createAgent("p1", { name: "A", profileId: "p" });
const list = await agent.listAgents("p1");
const handle = await agent.launchAgent(
"p1",
list[0].id,
{ cwd: "/x", rows: 24, cols: 80, conversationId: "existing-conv" },
() => {},
);
expect(handle.assignedConversationId).toBeUndefined();
});
});
describe("LayoutGrid — conversation-id loop wiring", () => {
it("persists the assigned id on the leaf after an agent launch", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
// Pin the agent on the root leaf so the grid uses the agent opener.
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
await layout.mutateLayout("p1", {
type: "setCellAgent",
target: leafId,
agent: a.id,
});
// A launch handle that always reports an assigned id (the "first launch").
const launchAgent = vi.fn(
async (
_p: string,
_ag: string,
_opts: OpenTerminalOptions,
_onData: (b: Uint8Array) => void,
): Promise<TerminalHandle> => ({
sessionId: "sess-1",
assignedConversationId: "assigned-conv-1",
write: vi.fn().mockResolvedValue(undefined),
resize: vi.fn().mockResolvedValue(undefined),
detach: vi.fn(),
close: vi.fn().mockResolvedValue(undefined),
}),
);
const stubAgent = {
listAgents: () => Promise.resolve([a]),
launchAgent,
reattach: vi.fn(),
};
renderGrid(layout, stubAgent, "p1");
await waitFor(() => expect(screen.getByRole("combobox")).toBeTruthy());
// Only assert the loop when the opener actually ran (xterm may bail in jsdom).
await waitFor(async () => {
if (launchAgent.mock.calls.length > 0) {
const fresh = await layout.loadLayout("p1");
expect(leaves(fresh)[0].conversationId).toBe("assigned-conv-1");
} else {
expect(true).toBe(true);
}
});
});
it("passes the leaf's existing conversation id into launchAgent (resume)", async () => {
const layout = new MockLayoutGateway();
const agent = new MockAgentGateway();
const a = await agent.createAgent("p1", { name: "MyAgent", profileId: "p" });
const tree = await layout.loadLayout("p1");
const leafId = leaves(tree)[0].id;
await layout.mutateLayout("p1", {
type: "setCellAgent",
target: leafId,
agent: a.id,
});
// The leaf already carries a conversation id (a prior assignment).
await layout.mutateLayout("p1", {
type: "setCellConversation",
target: leafId,
conversationId: "prior-conv",
});
const launchAgent = vi.fn(
async (
_p: string,
_ag: string,
_opts: OpenTerminalOptions,
_onData: (b: Uint8Array) => void,
): Promise<TerminalHandle> => ({
sessionId: "sess-2",
write: vi.fn().mockResolvedValue(undefined),
resize: vi.fn().mockResolvedValue(undefined),
detach: vi.fn(),
close: vi.fn().mockResolvedValue(undefined),
}),
);
const stubAgent = {
listAgents: () => Promise.resolve([a]),
launchAgent,
reattach: vi.fn(),
};
renderGrid(layout, stubAgent, "p1");
await waitFor(() => expect(screen.getByRole("combobox")).toBeTruthy());
await waitFor(() => {
if (launchAgent.mock.calls.length > 0) {
const opts = launchAgent.mock.calls[0][2] as OpenTerminalOptions;
expect(opts.conversationId).toBe("prior-conv");
} else {
expect(true).toBe(true);
}
});
});
});

View File

@ -17,7 +17,7 @@ import type {
LayoutTree,
} from "@/domain";
import { useGateways } from "@/app/di";
import { splitOp } from "./layout";
import { droppedSessions, leaves, splitOp } from "./layout";
/** What the layout grid UI needs from this hook. */
export interface LayoutViewModel {
@ -39,6 +39,11 @@ export interface LayoutViewModel {
setSession: (target: string, session: string | null) => Promise<void>;
/** Pins or clears an agent on a cell (persisted in layout). */
setCellAgent: (target: string, agent: string | null) => Promise<void>;
/**
* Records (or clears) the persistent CLI conversation id on a cell (T4b). Used
* to persist the id assigned at first launch so the next open resumes it.
*/
setCellConversation: (target: string, conversationId: string | null) => Promise<void>;
}
function describe(e: unknown): string {
@ -52,7 +57,7 @@ export function useLayout(
projectId: string | null,
layoutId?: string,
): LayoutViewModel {
const { layout: gateway } = useGateways();
const { layout: gateway, terminal } = useGateways();
const [layout, setLayout] = useState<LayoutTree | null>(null);
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
@ -98,14 +103,54 @@ export function useLayout(
[gateway, projectId, layoutId],
);
// Applies several operations in series, persisting each, but commits a single
// `setLayout` with the FINAL tree. This avoids an intermediate render between
// two related mutations — critical for `setCellAgent`, where a render showing
// the new agent while the old session is still set would remount TerminalView
// and make it reattach to the wrong (old) PTY instead of launching the agent.
const mutateChain = useCallback(
async (operations: LayoutOperation[]) => {
if (!projectId || !gateway || operations.length === 0) return;
setBusy(true);
setError(null);
try {
let tree: LayoutTree | null = null;
for (const op of operations) {
tree = await gateway.mutateLayout(projectId, op, layoutId);
}
if (tree) setLayout(tree);
} catch (e) {
setError(describe(e));
} finally {
setBusy(false);
}
},
[gateway, projectId, layoutId],
);
const split = useCallback(
(target: string, direction: Direction) => mutate(splitOp(target, direction)),
[mutate],
);
const merge = useCallback(
(container: string, keepIndex: number) =>
mutate({ type: "merge", container, keepIndex }),
[mutate],
async (container: string, keepIndex: number) => {
// Closing a cell collapses its split onto the kept child; the dropped
// child's PTYs must be killed (not just detached) or they'd linger as
// orphan agent processes. Snapshot the sessions to drop BEFORE mutating
// (the tree no longer holds them afterwards), then tear them down.
const dropped = layout ? droppedSessions(layout, container, keepIndex) : [];
await mutate({ type: "merge", container, keepIndex });
if (terminal) {
for (const session of dropped) {
try {
await terminal.closeTerminal(session);
} catch {
/* already gone — ignore */
}
}
}
},
[layout, mutate, terminal],
);
const resize = useCallback(
(container: string, weights: number[]) =>
@ -122,10 +167,51 @@ export function useLayout(
[mutate],
);
const setCellAgent = useCallback(
(target: string, agent: string | null) =>
mutate({ type: "setCellAgent", target, agent }),
async (target: string, agent: string | null) => {
// Find the cell's current agent + session. Changing the agent must reset
// the session (so the remounted TerminalView opens fresh for the new agent
// instead of reattaching to the old PTY) and kill the old PTY.
const cell = layout ? leaves(layout).find((l) => l.id === target) : undefined;
const oldAgent = cell?.agent ?? null;
const oldSession = cell?.session ?? null;
if (agent === oldAgent) return; // no-op: same agent, keep the session/PTY.
// Changing the agent also drops the previous conversation id: it belongs to
// the old agent's CLI and must never be passed to the new one (T4b).
await mutateChain([
{ type: "setCellAgent", target, agent },
{ type: "setSession", target, session: null },
{ type: "setCellConversation", target, conversationId: null },
]);
// Best-effort: tear down the previous PTY so no orphan process lingers.
if (oldSession && terminal) {
try {
await terminal.closeTerminal(oldSession);
} catch {
/* already gone — ignore */
}
}
},
[layout, mutateChain, terminal],
);
const setCellConversation = useCallback(
(target: string, conversationId: string | null) =>
mutate({ type: "setCellConversation", target, conversationId }),
[mutate],
);
return { layout, error, busy, split, merge, resize, move, setSession, setCellAgent };
return {
layout,
error,
busy,
split,
merge,
resize,
move,
setSession,
setCellAgent,
setCellConversation,
};
}