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

Skip to content

Commit 7800b33

Browse files
committed
fix: handle zero-based message id
1 parent 1006b98 commit 7800b33

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

cli/exp_mcp.go

Lines changed: 3 additions & 7 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
@@ -388,10 +388,6 @@ func (r *RootCmd) mcpServer() *serpent.Command {
388388
return &serpent.Command{
389389
Use: "server",
390390
Handler: func(inv *serpent.Invocation) error {
391-
// lastUserMessageID is the ID of the last *user* message that we saw. A
392-
// user message only happens when interacting via the AI AgentAPI (as
393-
// opposed to interacting with the terminal directly).
394-
var lastUserMessageID int64
395391
var lastReport taskReport
396392
// Create a queue that skips duplicates and preserves summaries.
397393
queue := cliutil.NewQueue[taskReport](512).WithPredicate(func(report taskReport) (taskReport, bool) {
@@ -415,7 +411,7 @@ func (r *RootCmd) mcpServer() *serpent.Command {
415411
// user manually submits a new prompt and the AI agent becomes active
416412
// (and does not update itself), but it avoids spamming useless status
417413
// updates as the user is typing, so the tradeoff is worth it.
418-
if report.messageID > lastUserMessageID {
414+
if report.messageID != nil {
419415
report.state = codersdk.WorkspaceAppStatusStateWorking
420416
} else if report.state == codersdk.WorkspaceAppStatusStateWorking && !report.selfReported && lastReport.state != "" {
421417
return report, false
@@ -607,7 +603,7 @@ func (s *mcpServer) startWatcher(ctx context.Context, inv *serpent.Invocation) {
607603
case agentapi.EventMessageUpdate:
608604
if ev.Role == agentapi.RoleUser {
609605
err := s.queue.Push(taskReport{
610-
messageID: ev.Id,
606+
messageID: &ev.Id,
611607
})
612608
if err != nil {
613609
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)