@@ -106,6 +106,8 @@ type PTYConversation struct {
106106 firstStableSnapshot string
107107 // userSentMessageAfterLoadState tracks if the user has sent their first message after we load the state
108108 userSentMessageAfterLoadState bool
109+ // loadStateSuccessful indicates whether conversation state was successfully restored from file.
110+ loadStateSuccessful bool
109111}
110112
111113var _ Conversation = & PTYConversation {}
@@ -123,9 +125,13 @@ func NewPTY(ctx context.Context, cfg PTYConversationConfig, initialPrompt string
123125 Time : cfg .GetTime (),
124126 },
125127 },
126- InitialPrompt : initialPrompt ,
127- InitialPromptSent : len (initialPrompt ) == 0 ,
128- toolCallMessageSet : make (map [string ]bool ),
128+ InitialPrompt : initialPrompt ,
129+ InitialPromptSent : len (initialPrompt ) == 0 ,
130+ toolCallMessageSet : make (map [string ]bool ),
131+ dirty : false ,
132+ firstStableSnapshot : "" ,
133+ userSentMessageAfterLoadState : false ,
134+ loadStateSuccessful : false ,
129135 }
130136 return c
131137}
@@ -170,7 +176,9 @@ func (c *PTYConversation) updateLastAgentMessageLocked(screen string, timestamp
170176 if c .cfg .FormatMessage != nil {
171177 agentMessage = c .cfg .FormatMessage (agentMessage , lastUserMessage .Message )
172178 }
173- agentMessage = c .skipInitialSnapshot (agentMessage )
179+ if c .loadStateSuccessful {
180+ agentMessage = c .adjustScreenAfterStateLoad (agentMessage )
181+ }
174182 if c .cfg .FormatToolCall != nil {
175183 agentMessage , toolCalls = c .cfg .FormatToolCall (agentMessage )
176184 }
@@ -473,12 +481,13 @@ func (c *PTYConversation) LoadState(stateFile string) ([]ConversationMessage, er
473481 c .firstStableSnapshot = c .cfg .FormatMessage (strings .TrimSpace (snapshots [len (snapshots )- 1 ].screen ), "" )
474482 }
475483
484+ c .loadStateSuccessful = true
476485 c .cfg .Logger .Info ("Successfully loaded state" , "path" , stateFile , "messages" , len (c .messages ))
477486 return c .messages , nil
478487}
479488
480- func (c * PTYConversation ) skipInitialSnapshot (screen string ) string {
481- newScreen := strings .ReplaceAll (screen , c .firstStableSnapshot , "" )
489+ func (c * PTYConversation ) adjustScreenAfterStateLoad (screen string ) string {
490+ newScreen := strings .Replace (screen , c .firstStableSnapshot , "" , 1 )
482491
483492 // Before the first user message after loading state, return the last message from the loaded state.
484493 // This prevents computing incorrect diffs from the restored screen, as the agent's message should
0 commit comments