Skip to content

Commit b51050a

Browse files
committed
Only report user messages
1 parent 6cb03be commit b51050a

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

cli/exp_mcp.go

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ func (r *RootCmd) mcpServer() *serpent.Command {
493493
}
494494

495495
func (s *mcpServer) startReporter(ctx context.Context, inv *serpent.Invocation) {
496+
// lastMessageID is the ID of the last *user* message that we saw. A user
497+
// message only happens when interacting via the API (as opposed to
498+
// interacting with the terminal directly).
496499
var lastMessageID int64
497500
shouldUpdate := func(item reportTask) codersdk.WorkspaceAppStatusState {
498501
// Always send self-reported updates.
@@ -505,18 +508,17 @@ func (s *mcpServer) startReporter(ctx context.Context, inv *serpent.Invocation)
505508
codersdk.WorkspaceAppStatusStateFailure:
506509
return item.state
507510
}
508-
// Always send "working" when there is a new message, since this means the
509-
// user submitted a message through the API and we know the LLM will begin
510-
// work soon if it has not already.
511+
// Always send "working" when there is a new user message, since we know the
512+
// LLM will begin work soon if it has not already.
511513
if item.messageID > lastMessageID {
512514
return codersdk.WorkspaceAppStatusStateWorking
513515
}
514-
// Otherwise, if the state is "working" and there have been no new messages,
515-
// it means either that the LLM is still working or it means the user has
516-
// interacted with the terminal directly. For now, we are ignoring these
517-
// updates. This risks missing cases where the user manually submits a new
518-
// prompt and the LLM becomes active and does not update itself, but it
519-
// avoids spamming useless status updates.
516+
// Otherwise, if the state is "working" and there have been no new user
517+
// messages, it means either that the LLM is still working or it means the
518+
// user has interacted with the terminal directly. For now, we are ignoring
519+
// these updates. This risks missing cases where the user manually submits
520+
// a new prompt and the LLM becomes active and does not update itself, but
521+
// it avoids spamming useless status updates.
520522
return ""
521523
}
522524
var lastPayload agentsdk.PatchAppStatus
@@ -599,12 +601,14 @@ func (s *mcpServer) startWatcher(ctx context.Context, inv *serpent.Invocation) {
599601
return
600602
}
601603
case agentapi.EventMessageUpdate:
602-
err := s.queue.Push(reportTask{
603-
messageID: ev.Id,
604-
})
605-
if err != nil {
606-
cliui.Warnf(inv.Stderr, "Failed to queue update: %s", err)
607-
return
604+
if ev.Role == agentapi.RoleUser {
605+
err := s.queue.Push(reportTask{
606+
messageID: ev.Id,
607+
})
608+
if err != nil {
609+
cliui.Warnf(inv.Stderr, "Failed to queue update: %s", err)
610+
return
611+
}
608612
}
609613
}
610614
case err := <-errCh:

cli/exp_mcp_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -790,11 +790,12 @@ func TestExpMcpReporter(t *testing.T) {
790790
}
791791
}
792792

793-
makeMessageEvent := func(id int64) *codersdk.ServerSentEvent {
793+
makeMessageEvent := func(id int64, role agentapi.ConversationRole) *codersdk.ServerSentEvent {
794794
return &codersdk.ServerSentEvent{
795795
Type: ServerSentEventTypeMessageUpdate,
796796
Data: agentapi.EventMessageUpdate{
797-
Id: id,
797+
Id: id,
798+
Role: role,
798799
},
799800
}
800801
}
@@ -813,7 +814,7 @@ func TestExpMcpReporter(t *testing.T) {
813814
return
814815
}
815816
// Send initial message.
816-
send(*makeMessageEvent(0))
817+
send(*makeMessageEvent(0, agentapi.RoleAgent))
817818
listening <- send
818819
<-closed
819820
}))
@@ -902,13 +903,17 @@ func TestExpMcpReporter(t *testing.T) {
902903
URI: "https://dev.coder.com",
903904
},
904905
},
905-
// Terminal becomes active again according to the screen watcher, but
906-
// message length is the same. This could be the LLM being active again,
907-
// but it could also be the user messing around. We will prefer not
908-
// updating the status so the "working" update here should be skipped.
906+
// Terminal becomes active again according to the screen watcher, but no
907+
// new user message. This could be the LLM being active again, but it
908+
// could also be the user messing around. We will prefer not updating the
909+
// status so the "working" update here should be skipped.
909910
{
910911
event: makeStatusEvent(agentapi.StatusRunning),
911912
},
913+
// Agent messages are ignored.
914+
{
915+
event: makeMessageEvent(1, agentapi.RoleAgent),
916+
},
912917
// LLM reports that it failed and URI is blank.
913918
{
914919
state: codersdk.WorkspaceAppStatusStateFailure,
@@ -923,10 +928,10 @@ func TestExpMcpReporter(t *testing.T) {
923928
{
924929
event: makeStatusEvent(agentapi.StatusRunning),
925930
},
926-
// ... but this time the message length has increased so we know there is
927-
// LLM activity. This time the "working" update will not be skipped.
931+
// ... but this time we have a new user message so we know there is LLM
932+
// activity. This time the "working" update will not be skipped.
928933
{
929-
event: makeMessageEvent(1),
934+
event: makeMessageEvent(2, agentapi.RoleUser),
930935
expected: &codersdk.WorkspaceAppStatus{
931936
State: codersdk.WorkspaceAppStatusStateWorking,
932937
Message: "oops",

0 commit comments

Comments
 (0)