% Safe-boundary compaction illustration
% Requires: \usepackage{tikz}
% Requires: \usetikzlibrary{positioning, decorations.pathreplacing, calc, fit, backgrounds}
\begin{tikzpicture}[
    node distance=0.12cm,
    msg/.style={
        draw=black!40,
        rounded corners=2pt,
        minimum width=3.4cm,
        minimum height=0.55cm,
        text centered,
        font=\footnotesize,
    },
    user/.style={msg, fill=blue!10, draw=blue!40},
    assistant/.style={msg, fill=gray!10, draw=gray!50},
    tooluse/.style={msg, fill=orange!12, draw=orange!40},
    toolresult/.style={msg, fill=orange!8, draw=orange!35},
    splitlabel/.style={font=\footnotesize\itshape},
]

% ============ LEFT SIDE: Unsafe Split ============
\node[font=\small\bfseries, text=red!70!black] (ulabel) at (0, 4.2) {Unsafe Split};

\node[user] (u1) at (0, 3.5) {User};
\node[assistant, below=of u1] (a1) {Assistant};
\node[tooluse, below=of a1] (tu1) {+ ToolUse};
% --- red dashed line here ---
\node[toolresult, below=0.45cm of tu1] (tr1) {ToolResult};
\node[user, below=of tr1] (u2) {User};
\node[assistant, below=of u2] (a2) {Assistant};
\node[tooluse, below=of a2] (tu2) {+ ToolUse};
\node[toolresult, below=of tu2] (tr2) {ToolResult};
\node[user, below=of tr2] (u3) {User};

% Red dashed split line
\coordinate (splitY) at ($(tu1.south)!0.5!(tr1.north)$);
\draw[red!70, dashed, very thick] (-2.2, 0 |- splitY) -- ++(4.4, 0);

% Label
\node[splitlabel, text=red!70!black, right] at (2.3, 0 |- splitY)
    {\begin{tabular}{l}Breaks\\tool pair\end{tabular}};

% Cross mark
\node[font=\large, text=red!70] at (-2.5, 0 |- splitY) {\ding{55}};

% Brace linking ToolUse and ToolResult
\draw[decorate, decoration={brace, amplitude=4pt, mirror},
      orange!60!black, thick]
    ($(tu1.south east)+(0.08,0)$) -- ($(tr1.north east)+(0.08,0)$)
    node[midway, right=5pt, font=\tiny, text=orange!60!black] {paired};

% ============ RIGHT SIDE: Safe Split ============
\def\xoff{7.5}
\node[font=\small\bfseries, text=green!50!black] at (\xoff, 4.2) {Safe Split};

\node[user] (su1) at (\xoff, 3.5) {User};
\node[assistant, below=of su1] (sa1) {Assistant};
\node[tooluse, below=of sa1] (stu1) {+ ToolUse};
\node[toolresult, below=of stu1] (str1) {ToolResult};
% --- green dashed line here ---
\node[user, below=0.45cm of str1] (su2) {User};
\node[assistant, below=of su2] (sa2) {Assistant};
\node[tooluse, below=of sa2] (stu2) {+ ToolUse};
\node[toolresult, below=of stu2] (str2) {ToolResult};
\node[user, below=of str2] (su3) {User};

% Green dashed split line
\coordinate (safesplitY) at ($(str1.south)!0.5!(su2.north)$);
\draw[green!50!black, dashed, very thick]
    (\xoff - 2.2, 0 |- safesplitY) -- ++( 4.4, 0);

% Label
\node[splitlabel, text=green!50!black, right] at (\xoff + 2.3, 0 |- safesplitY)
    {\begin{tabular}{l}Clean\\boundary\end{tabular}};

% Check mark
\node[font=\large, text=green!50!black] at (\xoff - 2.5, 0 |- safesplitY) {\ding{51}};

% Summary annotations
\node[draw=red!50, fill=red!5, rounded corners=2pt, font=\tiny,
      text width=3.2cm, align=center]
    at (0, -1.2)
    {Summarize prefix \emph{above} the line;\\ orphaned ToolResult causes API error};

\node[draw=green!40!black, fill=green!5, rounded corners=2pt, font=\tiny,
      text width=3.2cm, align=center]
    at (\xoff, -1.2)
    {Summarize prefix \emph{above} the line;\\ all tool pairs remain intact};

\end{tikzpicture}
