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

@ -17,6 +17,7 @@ import { Channel, invoke } from "@tauri-apps/api/core";
import type { Agent } from "@/domain";
import type {
AgentGateway,
ConversationDetails,
CreateAgentInput,
OpenTerminalOptions,
ReattachResult,
@ -30,6 +31,8 @@ interface LaunchAgentResponse {
cwd: string;
rows: number;
cols: number;
/** Conversation id minted by this launch (omitted when nothing was assigned). */
assignedConversationId?: string;
}
export class TauriAgentGateway implements AgentGateway {
@ -85,11 +88,19 @@ export class TauriAgentGateway implements AgentGateway {
agentId,
rows: options.rows,
cols: options.cols,
// Resume id: the leaf's persisted conversation id, when any (T4b). The
// backend resumes it; absent ⇒ a fresh cell that may get a new id.
conversationId: options.conversationId ?? null,
},
onOutput: channel,
});
return makeTerminalHandle(res.sessionId, channel);
const handle = makeTerminalHandle(res.sessionId, channel);
// Surface the id assigned by this launch so the caller persists it on the
// leaf (`setCellConversation`) and resumes next time.
return res.assignedConversationId
? { ...handle, assignedConversationId: res.assignedConversationId }
: handle;
}
async reattach(
@ -111,4 +122,16 @@ export class TauriAgentGateway implements AgentGateway {
scrollback: Uint8Array.from(res.scrollback),
};
}
inspectConversation(
projectId: string,
agentId: string,
conversationId: string,
): Promise<ConversationDetails> {
// `inspect_conversation` takes a single `request` DTO; absent fields are
// omitted from the response (best-effort), so an empty object is valid.
return invoke<ConversationDetails>("inspect_conversation", {
request: { projectId, agentId, conversationId },
});
}
}

View File

@ -30,6 +30,7 @@ import type {
} from "@/domain";
import type {
AgentGateway,
ConversationDetails,
CreateAgentInput,
CreateSkillInput,
CreateTemplateInput,
@ -323,7 +324,17 @@ export class MockAgentGateway implements AgentGateway {
queueMicrotask(() =>
session.emit(enc.encode(`agent ${agentId} @ ${cwd}\r\n`)),
);
return makeMockHandle(session, () => this.sessions.delete(sessionId));
const handle = makeMockHandle(session, () => this.sessions.delete(sessionId));
// Simulate session assignment (T4b): a fresh cell (no conversation id) gets a
// newly-minted id surfaced on the handle so the caller persists it; a cell
// that already carries an id resumes it and nothing new is assigned.
if (!options.conversationId) {
return {
...handle,
assignedConversationId: `mock-conversation-${sessionId}`,
};
}
return handle;
}
async reattach(
@ -344,6 +355,31 @@ export class MockAgentGateway implements AgentGateway {
scrollback,
};
}
/**
* Best-effort conversation details, keyed by conversation id (T7). Empty by
* default (degraded mode); a test seeds enriched details via
* {@link _setConversationDetails}.
*/
private conversationDetails = new Map<string, ConversationDetails>();
/** Seeds the (best-effort) details returned for a given conversation id. */
_setConversationDetails(
conversationId: string,
details: ConversationDetails,
): void {
this.conversationDetails.set(conversationId, details);
}
async inspectConversation(
_projectId: string,
_agentId: string,
conversationId: string,
): Promise<ConversationDetails> {
// Best-effort: an unknown conversation yields empty details (degraded mode),
// never an error — mirroring the backend contract.
return structuredClone(this.conversationDetails.get(conversationId) ?? {});
}
}
/**
@ -421,6 +457,16 @@ export class MockTerminalGateway implements TerminalGateway {
scrollback,
};
}
async closeTerminal(sessionId: string): Promise<void> {
// Best-effort / idempotent: kill the PTY by id (mirrors the handle's close)
// so a later reattach to it fails. No-op if the session is already gone.
const session = this.sessions.get(sessionId);
if (session) {
session.closed = true;
this.sessions.delete(sessionId);
}
}
}
export class MockProjectGateway implements ProjectGateway {

View File

@ -168,4 +168,21 @@ describe("MockTerminalGateway", () => {
code: "NOT_FOUND",
});
});
it("closeTerminal(sessionId) kills the PTY so a later reattach fails (Bug #3)", async () => {
const gw = new MockTerminalGateway();
const handle = await gw.openTerminal(
{ cwd: "/c", rows: 24, cols: 80 },
() => {},
);
await gw.closeTerminal(handle.sessionId);
await expect(gw.reattach(handle.sessionId, () => {})).rejects.toMatchObject({
code: "NOT_FOUND",
});
});
it("closeTerminal on an unknown session is a no-op (best-effort)", async () => {
const gw = new MockTerminalGateway();
await expect(gw.closeTerminal("does-not-exist")).resolves.toBeUndefined();
});
});

View File

@ -0,0 +1,109 @@
/**
* Contract / regression tests for the Tauri terminal adapter.
*
* The headline test guards the keystroke-ordering bug: `write` must serialise
* per handle so bytes reach the backend in call order even when the underlying
* `invoke`s resolve out of order (Tauri's IPC gives no ordering guarantee for
* concurrent calls). Without serialisation, fast typing/pasting garbles the CLI
* input.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
const invoke = vi.fn();
vi.mock("@tauri-apps/api/core", () => ({
invoke: (...args: unknown[]) => invoke(...args),
Channel: class {
onmessage: ((c: number[]) => void) | null = null;
},
}));
import { Channel } from "@tauri-apps/api/core";
import { makeTerminalHandle } from "./terminal";
const dec = new TextDecoder();
function handle() {
return makeTerminalHandle("sess-1", new Channel<number[]>());
}
describe("TauriTerminalGateway write ordering (regression)", () => {
beforeEach(() => invoke.mockReset());
it("delivers bytes to the backend in call order despite out-of-order resolution", async () => {
// Each `write_terminal` invoke resolves after a delay that is the INVERSE of
// its arrival order, so the *first* call resolves last. If writes were
// fire-and-forget (no chaining), the backend would observe them reversed.
const arrivals: string[] = [];
let order = 0;
invoke.mockImplementation((cmd: string, payload: { request: { data: number[] } }) => {
if (cmd !== "write_terminal") return Promise.resolve();
const callIndex = order++;
const delay = (4 - callIndex) * 10; // 1st call → longest delay
return new Promise<void>((resolve) => {
setTimeout(() => {
arrivals.push(dec.decode(Uint8Array.from(payload.request.data)));
resolve();
}, delay);
});
});
const h = handle();
const enc = new TextEncoder();
// Fire five writes back-to-back, as fast typing would.
const writes = ["a", "b", "c", "d", "e"].map((c) => h.write(enc.encode(c)));
await Promise.all(writes);
// Backend stdin order MUST equal the order `write` was called.
expect(arrivals).toEqual(["a", "b", "c", "d", "e"]);
});
it("a rejected write does not block subsequent writes (chain survives errors)", async () => {
const arrivals: string[] = [];
let call = 0;
invoke.mockImplementation(
(_cmd: string, payload?: { request: { data: number[] } }) => {
// Only `write_terminal` calls carry a payload; ignore any bare probe.
if (!payload) return Promise.resolve();
call++;
if (call === 1) return Promise.reject(new Error("boom"));
arrivals.push(dec.decode(Uint8Array.from(payload.request.data)));
return Promise.resolve();
},
);
const h = handle();
const enc = new TextEncoder();
const first = h.write(enc.encode("x")); // rejects
const second = h.write(enc.encode("y")); // must still run, in order
// The rejected write surfaces its error to its own caller…
let firstErr: unknown;
await first.catch((e) => {
firstErr = e;
});
expect(firstErr).toBeInstanceOf(Error);
// …but the chain survives, so the next write still reaches the backend.
await second;
expect(arrivals).toEqual(["y"]);
});
it("write nests sessionId + data array inside the request DTO", async () => {
invoke.mockResolvedValue(undefined);
const h = handle();
await h.write(Uint8Array.from([104, 105])); // "hi"
expect(invoke).toHaveBeenCalledWith("write_terminal", {
request: { sessionId: "sess-1", data: [104, 105] },
});
});
it("resize forwards rows/cols inside the request DTO", async () => {
invoke.mockResolvedValue(undefined);
const h = handle();
await h.resize(40, 120);
expect(invoke).toHaveBeenCalledWith("resize_terminal", {
request: { sessionId: "sess-1", rows: 40, cols: 120 },
});
});
});

View File

@ -48,12 +48,32 @@ export function makeTerminalHandle(
sessionId: string,
channel: Channel<number[]>,
): TerminalHandle {
// Serialise writes per handle. Each `write` chains its `invoke` after the
// previous one resolves, so the order in which `write` is *called* is the
// order the bytes reach the backend stdin — regardless of how Tauri's IPC
// schedules concurrent `invoke`s. Without this, typing/pasting fast puts
// several `invoke`s in flight at once and they can land out of order, garbling
// the CLI input (e.g. "le même" → "le mO é J é IDEIDE…").
//
// The chain only sequences *ordering*; a failed write is swallowed for the
// purpose of the chain (logged, then the chain continues) so one rejected
// promise never blocks every subsequent keystroke. The error is still
// surfaced to the caller of that specific `write` via its own promise.
let chain: Promise<void> = Promise.resolve();
return {
sessionId,
async write(data: Uint8Array): Promise<void> {
await invoke("write_terminal", {
request: { sessionId, data: Array.from(data) },
});
write(data: Uint8Array): Promise<void> {
const run = chain.then(() =>
invoke<void>("write_terminal", {
request: { sessionId, data: Array.from(data) },
}),
);
// Keep the chain alive even if this write rejects: the next write must
// still run. Swallow the error on the *chain* copy only — `run` keeps the
// rejection so the caller can observe it.
chain = run.catch(() => {});
return run;
},
async resize(rows: number, cols: number): Promise<void> {
await invoke("resize_terminal", {
@ -105,4 +125,11 @@ export class TauriTerminalGateway implements TerminalGateway {
scrollback: Uint8Array.from(res.scrollback),
};
}
async closeTerminal(sessionId: string): Promise<void> {
// Kills the PTY by id (the backend `close_terminal` command). Used when a
// cell's agent changes and the old PTY must be torn down even though its
// owning view only ever detaches.
await invoke("close_terminal", { sessionId });
}
}

View File

@ -62,11 +62,17 @@ export interface GatewayError {
export type Direction = "row" | "column";
/** A terminal-hosting leaf cell. `session` is the hosted SessionId, if any.
* `agent` is the agent id if an agent is pinned to this cell (absent = plain terminal). */
* `agent` is the agent id if an agent is pinned to this cell (absent = plain terminal).
* `conversationId` is the persistent CLI conversation id (survives PTY close/reopen,
* lets the agent resume); omitted when absent (mirrors the backend `skip_serializing_if`).
* `agentWasRunning` records whether the cell's agent process was running at close time;
* omitted when `false`. */
export interface LeafCell {
id: string;
session?: string | null;
agent?: string;
conversationId?: string;
agentWasRunning?: boolean;
}
/** A weighted child of a split. `weight` is a relative (`> 0`) share. */
@ -129,7 +135,9 @@ export type LayoutOperation =
| { type: "resize"; container: string; weights: number[] }
| { type: "move"; from: string; to: string }
| { type: "setSession"; target: string; session?: string | null }
| { type: "setCellAgent"; target: string; agent: string | null };
| { type: "setCellAgent"; target: string; agent: string | null }
| { type: "setCellConversation"; target: string; conversationId: string | null }
| { type: "setAgentRunning"; target: string; running: boolean };
/** The kind of a named layout. */
export type LayoutKind = "terminal" | "gitGraph";
@ -197,6 +205,9 @@ export interface AgentProfile {
contextInjection: ContextInjection;
detect: string | null;
cwdTemplate: string;
/** Optional CLI flags for agent-session continuity: `assignFlag` to bind a
* conversation id at launch, `resumeFlag` to resume an existing conversation. */
session?: { assignFlag?: string; resumeFlag: string };
}
/** Availability of a candidate profile after detection (mirror of the DTO). */

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,
};
}

View File

@ -0,0 +1,120 @@
/**
* Resume-conversation popup (T7). Shown for an agent cell whose PTY session is
* dead but which carries a persisted `conversationId`, **before** the automatic
* Resume launch fires. It lets the user choose between resuming the previous CLI
* conversation or starting a fresh one.
*
* Pure presentation: it receives the status + best-effort details + the two
* action callbacks. It never calls a gateway / `invoke()` itself — the parent
* (`LayoutGrid`'s `LeafView`) wires it to the {@link AgentGateway} ports. The
* status ("en cours" / "clot") is derived **only** from `agentWasRunning` (the
* universal close-time flag), never from the inspector — a missing inspector
* just hides the enriched topic/token lines (degraded mode).
*/
import type { ConversationDetails } from "@/ports";
interface ResumeConversationPopupProps {
/**
* Whether the agent process was running when the cell was last closed
* (`agentWasRunning`). `true` ⇒ "en cours"; `false`/absent ⇒ "clot". This is
* the universal status; it is NOT derived from the inspector.
*/
agentWasRunning: boolean;
/**
* Best-effort enriched details (last topic + token indicator). `null` while
* loading; an empty object once loaded with no details available (degraded
* mode: only the status + the resume affordance are shown).
*/
details: ConversationDetails | null;
/** Resume the previous conversation (keeps the conversation id ⇒ Resume). */
onResume: () => void;
/** Start a fresh conversation (clears the conversation id ⇒ Assign). */
onNewConversation: () => void;
}
export function ResumeConversationPopup({
agentWasRunning,
details,
onResume,
onNewConversation,
}: ResumeConversationPopupProps) {
const statusLabel = agentWasRunning ? "en cours" : "clot";
return (
<div
data-testid="resume-conversation-popup"
role="dialog"
aria-label="Reprise de conversation"
style={{
position: "absolute",
inset: 0,
zIndex: 5,
display: "flex",
alignItems: "center",
justifyContent: "center",
background: "rgba(0, 0, 0, 0.55)",
}}
>
<div
style={{
minWidth: 260,
maxWidth: 360,
padding: "16px 18px",
background: "var(--color-surface, #1e1e1e)",
color: "var(--color-content, #e0e0e0)",
border: "1px solid var(--color-border, #3a3a3a)",
borderRadius: 6,
boxShadow: "0 8px 24px rgba(0, 0, 0, 0.5)",
fontSize: 13,
}}
>
<p style={{ margin: "0 0 8px", fontWeight: 600 }}>
Reprise de conversation
</p>
<p style={{ margin: "0 0 6px" }}>
Statut :{" "}
<span data-testid="resume-status">{statusLabel}</span>
</p>
{details?.lastTopic && (
<p style={{ margin: "0 0 6px" }} data-testid="resume-last-topic">
Dernier sujet : {details.lastTopic}
</p>
)}
{details?.tokenCount !== undefined && (
<p style={{ margin: "0 0 10px" }} data-testid="resume-token-count">
Tokens : {details.tokenCount}
</p>
)}
<div style={{ display: "flex", gap: 8, marginTop: 12 }}>
<button
type="button"
onClick={onResume}
style={{
flex: 1,
padding: "6px 10px",
cursor: "pointer",
}}
>
Reprendre la dernière conversation
</button>
<button
type="button"
onClick={onNewConversation}
style={{
flex: 1,
padding: "6px 10px",
cursor: "pointer",
}}
>
Nouvelle conversation
</button>
</div>
</div>
</div>
);
}

View File

@ -80,6 +80,7 @@ describe("TerminalView (with MockTerminalGateway)", () => {
return handle;
}),
reattach: vi.fn(),
closeTerminal: vi.fn(),
};
expect(() => renderView(terminal)).not.toThrow();
@ -93,7 +94,7 @@ describe("TerminalView (with MockTerminalGateway)", () => {
const close = vi.fn().mockResolvedValue(undefined);
const handle = makeHandle({ detach, close });
const openTerminal = vi.fn(async () => handle);
const terminal: TerminalGateway = { openTerminal, reattach: vi.fn() };
const terminal: TerminalGateway = { openTerminal, reattach: vi.fn(), closeTerminal: vi.fn() };
const { unmount } = renderView(terminal);
@ -129,7 +130,7 @@ describe("TerminalView (with MockTerminalGateway)", () => {
},
);
const openTerminal = vi.fn(async () => handle);
const terminal: TerminalGateway = { openTerminal, reattach };
const terminal: TerminalGateway = { openTerminal, reattach, closeTerminal: vi.fn() };
renderView(terminal, "/cwd", { sessionId: "live-1" });
@ -147,7 +148,7 @@ describe("TerminalView (with MockTerminalGateway)", () => {
it("persists a newly opened session id via onSessionId", async () => {
const handle = makeHandle({ sessionId: "new-99" });
const openTerminal = vi.fn(async () => handle);
const terminal: TerminalGateway = { openTerminal, reattach: vi.fn() };
const terminal: TerminalGateway = { openTerminal, reattach: vi.fn(), closeTerminal: vi.fn() };
const onSessionId = vi.fn();
renderView(terminal, "/cwd", { onSessionId });

View File

@ -202,19 +202,41 @@ export function TerminalView({
.catch(onOpenError);
}
// Refit + propagate size to the PTY on container resize.
const ro = new ResizeObserver(() => {
// Refit + propagate size to the PTY on container resize. ResizeObserver can
// fire many times per frame, and during layout/tab transitions the container
// momentarily reports a zero or transient size — fitting then would size
// xterm's grid (and the PTY) to a stale value, leaving the repainted content
// shifted/misaligned once the real size settles. So we: (1) coalesce bursts
// into a single `requestAnimationFrame` that runs after layout settles,
// (2) skip fitting while the container has no real size, and (3) push a PTY
// resize only when rows/cols actually change (avoids redundant reflows).
let rafId = 0;
let lastRows = term.rows;
let lastCols = term.cols;
const refit = () => {
rafId = 0;
if (disposed) return;
if (container.clientWidth === 0 || container.clientHeight === 0) return;
try {
fit.fit();
} catch {
return;
}
if (handle) void handle.resize(term.rows, term.cols);
if (handle && (term.rows !== lastRows || term.cols !== lastCols)) {
lastRows = term.rows;
lastCols = term.cols;
void handle.resize(term.rows, term.cols);
}
};
const ro = new ResizeObserver(() => {
if (rafId) cancelAnimationFrame(rafId);
rafId = requestAnimationFrame(refit);
});
ro.observe(container);
return () => {
disposed = true;
if (rafId) cancelAnimationFrame(rafId);
ro.disconnect();
onKey.dispose();
// DETACH, never close: tearing the view down (navigation / layout change)

View File

@ -1,3 +1,4 @@
/** Terminals feature (L3): xterm.js wrapper bound to the `TerminalGateway`. */
export { TerminalView } from "./TerminalView";
export { ResumeConversationPopup } from "./ResumeConversationPopup";

View File

@ -52,6 +52,18 @@ export interface CreateAgentInput {
initialContent?: string;
}
/**
* Best-effort enriched details about a CLI conversation (T7), used to enrich the
* resume popup. Both fields are optional: a missing inspector or a missing
* transcript yields an empty object — the popup degrades to the status alone.
*/
export interface ConversationDetails {
/** A short, best-effort label for the conversation (last topic). */
lastTopic?: string;
/** A best-effort cumulative token count. */
tokenCount?: number;
}
/** Agents: create, list, read/update context, delete, launch (L6). */
export interface AgentGateway {
/** Lists all agents belonging to the given project. */
@ -67,6 +79,13 @@ export interface AgentGateway {
/**
* Launches the agent: opens a PTY, spawns the CLI, and wires the output stream.
* Mirrors {@link TerminalGateway.openTerminal} but invokes `launch_agent`.
*
* The returned handle carries an optional {@link TerminalHandle.assignedConversationId}:
* when the agent's profile assigns a fresh CLI conversation id on this first
* launch (the cell had none yet), it is surfaced here so the caller can persist
* it on the hosting leaf (`setCellConversation`) and resume on the next open.
* Pass the leaf's current `conversationId` via {@link OpenTerminalOptions} to
* resume an existing conversation instead.
*/
launchAgent(
projectId: string,
@ -84,6 +103,17 @@ export interface AgentGateway {
sessionId: string,
onData: (bytes: Uint8Array) => void,
): Promise<ReattachResult>;
/**
* Reads best-effort {@link ConversationDetails} for an agent's conversation
* (T7), to enrich the resume popup with the last topic + a token indicator.
* Best-effort by contract: a missing/unsupported inspector or a missing
* transcript resolves to an empty object (never rejects for "no details").
*/
inspectConversation(
projectId: string,
agentId: string,
conversationId: string,
): Promise<ConversationDetails>;
}
/** Options for opening a terminal. */
@ -94,6 +124,13 @@ export interface OpenTerminalOptions {
rows: number;
/** Initial terminal width in columns. */
cols: number;
/**
* Persistent CLI conversation id recorded on the hosting cell, if any. When
* present, an agent launch **resumes** that conversation; when absent the
* launch may *assign* a fresh id (surfaced on the returned handle). Only
* meaningful for {@link AgentGateway.launchAgent}; ignored for plain terminals.
*/
conversationId?: string;
}
/**
@ -106,6 +143,14 @@ export interface OpenTerminalOptions {
export interface TerminalHandle {
/** Stable session id (UUID) used by the backend for this PTY. */
readonly sessionId: string;
/**
* Conversation id **assigned** by an agent launch when the profile minted a
* fresh one (the cell had none yet). Present only on handles returned by
* {@link AgentGateway.launchAgent}; `undefined` for plain terminals, resumes,
* or profiles without a session block. The caller persists it on the hosting
* leaf so the next open resumes instead of re-assigning.
*/
readonly assignedConversationId?: string;
/** Sends bytes (xterm keystrokes) to the PTY. */
write(data: Uint8Array): Promise<void>;
/** Resizes the PTY. */
@ -152,6 +197,17 @@ export interface TerminalGateway {
sessionId: string,
onData: (bytes: Uint8Array) => void,
): Promise<ReattachResult>;
/**
* Kills a live PTY by its session id, independently of any view-held handle.
* Used when a cell's agent changes: the old PTY must be torn down even though
* the owning {@link TerminalHandle} is private to its (unmounting) view, whose
* cleanup only ever {@link TerminalHandle.detach}es. Best-effort: resolves even
* if the session is already gone.
*
* This is the only sanctioned way to kill a PTY outside
* {@link TerminalHandle.close}.
*/
closeTerminal(sessionId: string): Promise<void>;
}
/** The outcome of {@link TerminalGateway.reattach}. */