@@ -75,6 +75,197 @@ describe("ChatLog", () => {
7575 expect ( chatLog . children . length ) . toBe ( 1 ) ;
7676 } ) ;
7777
78+ it ( "keeps cumulative assistant text in chronological order around a tool call" , ( ) => {
79+ const chatLog = new ChatLog ( 40 ) ;
80+
81+ chatLog . updateAssistant ( "Before the tool." , "run-1" ) ;
82+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
83+ chatLog . updateAssistant ( "Before the tool.\n\nAfter the tool." , "run-1" ) ;
84+
85+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
86+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
87+ "AssistantMessageComponent" ,
88+ "ToolExecutionComponent" ,
89+ "AssistantMessageComponent" ,
90+ ] ) ;
91+ expect ( rendered . indexOf ( "Before the tool." ) ) . toBeLessThan ( rendered . indexOf ( "Read File" ) ) ;
92+ expect ( rendered . indexOf ( "Read File" ) ) . toBeLessThan ( rendered . indexOf ( "After the tool." ) ) ;
93+ } ) ;
94+
95+ it ( "does not repeat cumulative text across multiple tool calls" , ( ) => {
96+ const chatLog = new ChatLog ( 40 ) ;
97+
98+ chatLog . updateAssistant ( "First segment." , "run-1" ) ;
99+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
100+ chatLog . updateAssistant ( "First segment.\n\nSecond segment." , "run-1" ) ;
101+ chatLog . startTool ( "tool-2" , "read_file" , { path : "b.txt" } ) ;
102+ chatLog . updateAssistant ( "First segment.\n\nSecond segment.\n\nThird segment." , "run-1" ) ;
103+
104+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
105+ for ( const text of [ "First segment." , "Second segment." , "Third segment." ] ) {
106+ expect ( rendered . split ( text ) ) . toHaveLength ( 2 ) ;
107+ }
108+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
109+ "AssistantMessageComponent" ,
110+ "ToolExecutionComponent" ,
111+ "AssistantMessageComponent" ,
112+ "ToolExecutionComponent" ,
113+ "AssistantMessageComponent" ,
114+ ] ) ;
115+ } ) ;
116+
117+ it ( "reconciles revised assistant snapshots without repeating stale frozen text" , ( ) => {
118+ const chatLog = new ChatLog ( 40 ) ;
119+
120+ chatLog . updateAssistant ( "Hello before the tool." , "run-1" ) ;
121+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
122+ chatLog . updateAssistant ( "Hallo before the tool.\n\nRevised answer." , "run-1" ) ;
123+
124+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
125+ expect ( rendered ) . not . toContain ( "Hello before the tool." ) ;
126+ expect ( rendered . split ( "Hallo before the tool." ) ) . toHaveLength ( 2 ) ;
127+ expect ( rendered . split ( "Revised answer." ) ) . toHaveLength ( 2 ) ;
128+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
129+ "ToolExecutionComponent" ,
130+ "AssistantMessageComponent" ,
131+ ] ) ;
132+ expect ( rendered . indexOf ( "Read File" ) ) . toBeLessThan ( rendered . indexOf ( "Revised answer." ) ) ;
133+
134+ chatLog . updateAssistant ( "Hallo before the tool.\n\nRevised answer.\n\nNext segment." , "run-1" ) ;
135+
136+ const continued = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
137+ expect ( continued . indexOf ( "Read File" ) ) . toBeLessThan ( continued . indexOf ( "Next segment." ) ) ;
138+ expect ( continued . split ( "Revised answer." ) ) . toHaveLength ( 2 ) ;
139+ } ) ;
140+
141+ it ( "reconciles a revised final snapshot after multiple frozen tool calls" , ( ) => {
142+ const chatLog = new ChatLog ( 40 ) ;
143+
144+ chatLog . updateAssistant ( "First segment." , "run-1" ) ;
145+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
146+ chatLog . updateAssistant ( "First segment.\n\nSecond segment." , "run-1" ) ;
147+ chatLog . startTool ( "tool-2" , "read_file" , { path : "b.txt" } ) ;
148+ chatLog . finalizeAssistant ( "Revised first segment.\n\nFinal answer." , "run-1" ) ;
149+
150+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
151+ expect ( rendered . split ( "Revised first segment." ) ) . toHaveLength ( 2 ) ;
152+ expect ( rendered . split ( "Final answer." ) ) . toHaveLength ( 2 ) ;
153+ expect ( rendered ) . not . toContain ( "Second segment." ) ;
154+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
155+ "ToolExecutionComponent" ,
156+ "ToolExecutionComponent" ,
157+ "AssistantMessageComponent" ,
158+ ] ) ;
159+ expect ( rendered . lastIndexOf ( "Read File" ) ) . toBeLessThan ( rendered . indexOf ( "Final answer." ) ) ;
160+ } ) ;
161+
162+ it ( "removes frozen provisional text when the final snapshot retracts it" , ( ) => {
163+ const chatLog = new ChatLog ( 40 ) ;
164+
165+ chatLog . updateAssistant ( "Retracted provisional answer." , "run-1" ) ;
166+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
167+ chatLog . finalizeAssistant ( "" , "run-1" ) ;
168+
169+ expect ( normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ) . not . toContain (
170+ "Retracted provisional answer." ,
171+ ) ;
172+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
173+ "ToolExecutionComponent" ,
174+ ] ) ;
175+ } ) ;
176+
177+ it ( "removes an empty post-tool component when its final snapshot is retracted" , ( ) => {
178+ const chatLog = new ChatLog ( 40 ) ;
179+
180+ chatLog . updateAssistant ( "Before the tool." , "run-1" ) ;
181+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
182+ chatLog . updateAssistant ( "Before the tool.\n\nRetracted answer." , "run-1" ) ;
183+ chatLog . finalizeAssistant ( "Before the tool." , "run-1" ) ;
184+
185+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
186+ "AssistantMessageComponent" ,
187+ "ToolExecutionComponent" ,
188+ ] ) ;
189+ expect ( normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ) . not . toContain ( "Retracted answer." ) ;
190+ } ) ;
191+
192+ it ( "preserves indentation in assistant text that follows a tool call" , ( ) => {
193+ const chatLog = new ChatLog ( 40 ) ;
194+
195+ chatLog . updateAssistant ( "I ran it:" , "run-1" ) ;
196+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
197+ chatLog . updateAssistant ( "I ran it:\n\n command output" , "run-1" ) ;
198+
199+ const segment = chatLog . children . at ( - 1 ) ;
200+ expect ( segment ?. constructor . name ) . toBe ( "AssistantMessageComponent" ) ;
201+ const rendered = normalizeTestText ( segment ?. render ( 120 ) . join ( "\n" ) ?? "" ) ;
202+ expect ( rendered ) . toContain ( "```" ) ;
203+ expect ( rendered ) . toMatch ( / \n { 2 } c o m m a n d o u t p u t / ) ;
204+ } ) ;
205+
206+ it ( "finalizes only assistant text that follows an intervening tool call" , ( ) => {
207+ const chatLog = new ChatLog ( 40 ) ;
208+
209+ chatLog . updateAssistant ( "Before the tool." , "run-1" ) ;
210+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
211+ chatLog . finalizeAssistant ( "Before the tool.\n\nFinal answer." , "run-1" ) ;
212+
213+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
214+ expect ( rendered . split ( "Before the tool." ) ) . toHaveLength ( 2 ) ;
215+ expect ( rendered . indexOf ( "Read File" ) ) . toBeLessThan ( rendered . indexOf ( "Final answer." ) ) ;
216+ } ) ;
217+
218+ it ( "does not add an empty final assistant segment after a tool call" , ( ) => {
219+ const chatLog = new ChatLog ( 40 ) ;
220+
221+ chatLog . updateAssistant ( "Complete before the tool." , "run-1" ) ;
222+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
223+ chatLog . finalizeAssistant ( "Complete before the tool." , "run-1" ) ;
224+
225+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
226+ "AssistantMessageComponent" ,
227+ "ToolExecutionComponent" ,
228+ ] ) ;
229+ } ) ;
230+
231+ it ( "clears frozen assistant segments when the chat history is rebuilt" , ( ) => {
232+ const chatLog = new ChatLog ( 40 ) ;
233+
234+ chatLog . updateAssistant ( "Before the tool." , "run-1" ) ;
235+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
236+ chatLog . clearAll ( ) ;
237+ chatLog . updateAssistant ( "Before the tool.\n\nNew history." , "run-1" ) ;
238+
239+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
240+ expect ( rendered ) . toContain ( "Before the tool." ) ;
241+ expect ( rendered ) . toContain ( "New history." ) ;
242+ expect ( chatLog . children ) . toHaveLength ( 1 ) ;
243+ } ) ;
244+
245+ it ( "removes every frozen assistant segment when a tool-using run is dropped" , ( ) => {
246+ const chatLog = new ChatLog ( 40 ) ;
247+
248+ chatLog . updateAssistant ( "First segment." , "run-1" ) ;
249+ chatLog . startTool ( "tool-1" , "read_file" , { path : "a.txt" } ) ;
250+ chatLog . updateAssistant ( "First segment.\n\nSecond segment." , "run-1" ) ;
251+ chatLog . startTool ( "tool-2" , "read_file" , { path : "b.txt" } ) ;
252+ chatLog . updateAssistant ( "First segment.\n\nSecond segment.\n\nThird segment." , "run-1" ) ;
253+
254+ chatLog . dropAssistant ( "run-1" ) ;
255+
256+ expect ( chatLog . children . map ( ( component ) => component . constructor . name ) ) . toEqual ( [
257+ "ToolExecutionComponent" ,
258+ "ToolExecutionComponent" ,
259+ ] ) ;
260+ const rendered = normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ;
261+ expect ( rendered ) . not . toContain ( "First segment." ) ;
262+ expect ( rendered ) . not . toContain ( "Second segment." ) ;
263+ expect ( rendered ) . not . toContain ( "Third segment." ) ;
264+
265+ chatLog . updateAssistant ( "Fresh run." , "run-1" ) ;
266+ expect ( normalizeTestText ( chatLog . render ( 120 ) . join ( "\n" ) ) ) . toContain ( "Fresh run." ) ;
267+ } ) ;
268+
78269 it ( "reserves assistant position without clearing existing streamed text" , ( ) => {
79270 const chatLog = new ChatLog ( 40 ) ;
80271 chatLog . startAssistant ( "partial" , "run-active" ) ;
0 commit comments