Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62b94b7

Browse files
committed
fix: handle new user messages correctly
Two things: 1. Message IDs can be zero-based. 2. We were not actually updating the last user message ID.
1 parent 1006b98 commit 62b94b7

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

cli/exp_mcp.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ func (*RootCmd) mcpConfigureCursor() *serpent.Command {
363363

364364
type taskReport struct {
365365
link string
366-
messageID int64
366+
messageID *int64
367367
selfReported bool
368368
state codersdk.WorkspaceAppStatusState
369369
summary string
@@ -391,7 +391,7 @@ func (r *RootCmd) mcpServer() *serpent.Command {
391391
// lastUserMessageID is the ID of the last *user* message that we saw. A
392392
// user message only happens when interacting via the AI AgentAPI (as
393393
// opposed to interacting with the terminal directly).
394-
var lastUserMessageID int64
394+
lastUserMessageID := int64(-1)
395395
var lastReport taskReport
396396
// Create a queue that skips duplicates and preserves summaries.
397397
queue := cliutil.NewQueue[taskReport](512).WithPredicate(func(report taskReport) (taskReport, bool) {
@@ -415,8 +415,9 @@ func (r *RootCmd) mcpServer() *serpent.Command {
415415
// user manually submits a new prompt and the AI agent becomes active
416416
// (and does not update itself), but it avoids spamming useless status
417417
// updates as the user is typing, so the tradeoff is worth it.
418-
if report.messageID > lastUserMessageID {
418+
if report.messageID != nil && *report.messageID > lastUserMessageID {
419419
report.state = codersdk.WorkspaceAppStatusStateWorking
420+
lastUserMessageID = *report.messageID
420421
} else if report.state == codersdk.WorkspaceAppStatusStateWorking && !report.selfReported && lastReport.state != "" {
421422
return report, false
422423
}
@@ -607,7 +608,7 @@ func (s *mcpServer) startWatcher(ctx context.Context, inv *serpent.Invocation) {
607608
case agentapi.EventMessageUpdate:
608609
if ev.Role == agentapi.RoleUser {
609610
err := s.queue.Push(taskReport{
610-
messageID: ev.Id,
611+
messageID: &ev.Id,
611612
})
612613
if err != nil {
613614
cliui.Warnf(inv.Stderr, "Failed to queue update: %s", err)

cli/exp_mcp_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ func TestExpMcpReporter(t *testing.T) {
835835
},
836836
// Agent messages are ignored.
837837
{
838-
event: makeMessageEvent(1, agentapi.RoleAgent),
838+
event: makeMessageEvent(0, agentapi.RoleAgent),
839839
},
840840
// AI agent reports that it failed and URI is blank.
841841
{
@@ -854,7 +854,7 @@ func TestExpMcpReporter(t *testing.T) {
854854
// ... but this time we have a new user message so we know there is AI
855855
// agent activity. This time the "working" update will not be skipped.
856856
{
857-
event: makeMessageEvent(2, agentapi.RoleUser),
857+
event: makeMessageEvent(1, agentapi.RoleUser),
858858
expected: &codersdk.WorkspaceAppStatus{
859859
State: codersdk.WorkspaceAppStatusStateWorking,
860860
Message: "oops",
@@ -895,6 +895,15 @@ func TestExpMcpReporter(t *testing.T) {
895895
URI: "",
896896
},
897897
},
898+
// Zero ID should be accepted.
899+
{
900+
event: makeMessageEvent(0, agentapi.RoleUser),
901+
expected: &codersdk.WorkspaceAppStatus{
902+
State: codersdk.WorkspaceAppStatusStateWorking,
903+
Message: "",
904+
URI: "",
905+
},
906+
},
898907
},
899908
},
900909
}
@@ -954,6 +963,7 @@ func TestExpMcpReporter(t *testing.T) {
954963
case w, ok := <-watcher:
955964
require.True(t, ok, "watch channel closed")
956965
if w.LatestAppStatus != nil && w.LatestAppStatus.ID != lastAppStatus.ID {
966+
t.Logf("Got status update: %s > %s", lastAppStatus.State, w.LatestAppStatus.State)
957967
lastAppStatus = *w.LatestAppStatus
958968
return lastAppStatus
959969
}

0 commit comments

Comments
 (0)