@@ -99,6 +99,10 @@ type PTYConversation struct {
9999 // layer holds s.mu, and Send blocks until the message is processed),
100100 // so ordering is preserved.
101101 outboundQueue chan outboundMessage
102+ // sendingMessage is true while the send loop is processing a message.
103+ // Set under lock in the snapshot loop when signaling, cleared under
104+ // lock in the send loop after sendMessage returns.
105+ sendingMessage bool
102106 // stableSignal is used by the snapshot loop to signal the send loop
103107 // when the agent is stable and there are items in the outbound queue.
104108 stableSignal chan struct {}
@@ -162,6 +166,7 @@ func (c *PTYConversation) Start(ctx context.Context) {
162166 if c .initialPromptReady && len (c .outboundQueue ) > 0 && c .isScreenStableLocked () {
163167 select {
164168 case c .stableSignal <- struct {}{}:
169+ c .sendingMessage = true
165170 default :
166171 // Signal already pending
167172 }
@@ -181,7 +186,7 @@ func (c *PTYConversation) Start(ctx context.Context) {
181186 case msg := <- c .outboundQueue :
182187 if msg .errCh != nil {
183188 msg .errCh <- ctx .Err ()
184- close (msg .errCh ) // help GC
189+ close (msg .errCh )
185190 }
186191 default :
187192 return
@@ -198,9 +203,14 @@ func (c *PTYConversation) Start(ctx context.Context) {
198203 return
199204 case msg := <- c .outboundQueue :
200205 err := c .sendMessage (ctx , msg .parts ... )
206+ c .lock .Lock ()
207+ c .sendingMessage = false
208+ c .lock .Unlock ()
201209 if msg .errCh != nil {
202210 msg .errCh <- err
203- close (msg .errCh ) // help GC
211+ // Close so the Send() caller's <-errCh never blocks
212+ // if it has already consumed the error value.
213+ close (msg .errCh )
204214 }
205215 default :
206216 c .cfg .Logger .Error ("received stable signal but outbound queue is empty" )
@@ -448,7 +458,7 @@ func (c *PTYConversation) statusLocked() ConversationStatus {
448458
449459 // Handle initial prompt readiness: report "changing" until the queue is drained
450460 // to avoid the status flipping "changing" -> "stable" -> "changing"
451- if len (c .outboundQueue ) > 0 {
461+ if len (c .outboundQueue ) > 0 || c . sendingMessage {
452462 return ConversationStatusChanging
453463 }
454464
0 commit comments