Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

MUL-7319 fix(wecom): a completion of whitespace is not a message (#8347)

Xichang(Seacen) Zhao27d0529faf69
diff --git a/server/internal/integrations/wecom/outbound.go b/server/internal/integrations/wecom/outbound.go
index f732c7b522..7947c7266e 100644
--- a/server/internal/integrations/wecom/outbound.go
+++ b/server/internal/integrations/wecom/outbound.go
@@ -178,7 +178,7 @@ func (o *Outbound) processEvent(ctx context.Context, e events.Event) error {
// assistant message for exactly that case, and returning now would throw
// the work away.
content := deliverableContent(e)
- if content == "" && !o.mayCarryAttachments(e) {
+ if !hasVisibleChar(content) && !o.mayCarryAttachments(e) {
o.skipped(ctx, e, skipNothingToSay)
return nil
}
@@ -284,7 +284,12 @@ func (o *Outbound) processEvent(ctx context.Context, e events.Event) error {
// Words first. An empty completion reaches here only because a file is
// bound to it, and an empty markdown bubble ahead of that file would be
// noise the user has to scroll past.
- if content != "" {
+ //
+ // Empty is hasVisibleChar's sense of it, not `!= ""`. A completion of "\n"
+ // is a bubble with nothing in it on the reader's screen, and counting it
+ // as the words that answered the turn also tells the file below it that
+ // the reply has already been accounted for.
+ if hasVisibleChar(content) {
if err := sender.sendTextCtx(ctx, binding.ChannelChatID, chatType, content); err != nil {
return err
}
@@ -300,7 +305,7 @@ func (o *Outbound) processEvent(ctx context.Context, e events.Event) error {
ChatID: binding.ChannelChatID,
ChatType: chatType,
SessionID: e.ChatSessionID,
- }, content == "")
+ }, !hasVisibleChar(content))
return nil
}
diff --git a/server/internal/integrations/wecom/outbound_whitespace_test.go b/server/internal/integrations/wecom/outbound_whitespace_test.go
new file mode 100644
index 0000000000..73f0466774
--- /dev/null
+++ b/server/internal/integrations/wecom/outbound_whitespace_test.go
@@ -0,0 +1,121 @@
+package wecom
+
+// outbound_whitespace_test.go — a completion of whitespace is not a message,
+// on either of the two paths an answer can take out of this adapter.
+//
+// Which one runs is decided by where the WS lease happens to sit, so the two
+// have to read the same completion the same way: otherwise the replica holding
+// the socket decides whether the user sees a blank bubble, and whether the
+// words or the file is what the reply counter counted.
+
+import (
+ "context"
+ "testing"
+
+ "github.com/multica-ai/multica/server/internal/util"
+ db "github.com/multica-ai/multica/server/pkg/db/generated"
+)
+
+// whitespaceQueries is a turn with a bound chat, a live installation, and no
+// file bound after all. No file is what makes the reply's own outcome
+// observable: with the files carrying it, an empty turn is a skip.
+func whitespaceQueries(t *testing.T) *fakeOutboundQueries {
+ t.Helper()
+ q := &fakeOutboundQueries{
+ sessionBinding: db.ChannelChatSessionBinding{ChannelChatID: "CHAT_1", ChatType: "group"},
+ installation: db.ChannelInstallation{Status: string(InstallationActive)},
+ attachments: nil,
+ channelIngested: askedOverWecom(),
+ }
+ q.fileTask(t, testTaskID)
+ return q
+}
+
+// The local path: the words go out from the replica that produced them.
+//
+// REVERSE VERIFICATION: restore `content != ""` and `content == ""` in
+// processEvent and this reports a blank message on the socket, outbound
+// _delivered = 1, and no skip. Build and vet stay silent — both spellings
+// compile.
+func TestOutbound_WhitespaceOnlyCompletionIsNotSentAndTheFilesCarryTheReply(t *testing.T) {
+ t.Parallel()
+ q := whitespaceQueries(t)
+ mx := newCountingMetrics()
+ reg := newSendersRegistry()
+ instID := mustTestUUID(t)
+ conn := newMediaConn()
+ reg.set(instID, conn.newSender())
+ q.sessionBinding.InstallationID = instID
+ q.installation.ID = instID
+ o := NewOutbound(q, reg, testLogger(),
+ WithOutboundMetrics(mx), WithAttachments(&fakeObjectStore{key: "obj/bin", data: []byte("DATA")}))
+ o.spawn = func(f func()) { f() }
+
+ if err := o.processEvent(context.Background(), chatDoneEvent("\n")); err != nil {
+ t.Fatalf("processEvent: %v", err)
+ }
+
+ assertWhitespaceWasNotAMessage(t, conn, mx)
+}
+
+// The relayed path: the same completion, on the replica that holds the socket.
+//
+// REVERSE VERIFICATION: restore `f.Content != ""` and `f.Content == ""` in
+// deliverRelayed and this reports a blank message on the socket, outbound
+// _delivered = 1, and no skip. Build and vet stay silent — both spellings
+// compile. The _delivered half of that only holds because the settlement
+// callback is run below: this path hands its record to the dispatcher instead
+// of counting inline.
+func TestRelayedReply_WhitespaceOnlyContentIsNotSentAndTheFilesCarryTheReply(t *testing.T) {
+ t.Parallel()
+ q := whitespaceQueries(t)
+ reg := newSendersRegistry()
+ instID := mustTestUUID(t)
+ conn := newMediaConn()
+ reg.set(instID, conn.newSender())
+ mx := newCountingMetrics()
+ o := NewOutbound(q, reg, testLogger(),
+ WithOutboundMetrics(mx), WithAttachments(&fakeObjectStore{key: "obj/bin", data: []byte("DATA")}))
+ o.spawn = func(f func()) { f() }
+
+ res := o.deliverRelayed(context.Background(), relayFrame{
+ Kind: relayKindReply,
+ InstallationID: util.UUIDToString(instID),
+ ChatID: "CHAT_1",
+ ChatType: chatTypeGroupInt,
+ Content: "\n",
+ MessageID: testMessageID,
+ WorkspaceID: testWorkspaceID,
+ SessionID: testSessionID,
+ TaskID: testTaskID,
+ CarriesFiles: true,
+ })
+ if res.outcome != outcomeDone {
+ t.Fatalf("outcome = %v, want outcomeDone", res.outcome)
+ }
+ // A relayed delivery does not move the reply counters itself; it hands
+ // back the one record for the frame and the dispatcher runs it once the
+ // claim is settled. Run it here, or the outbound_delivered assertion below
+ // passes because nobody counted rather than because there was nothing to
+ // count — and it would go on passing with `f.Content != ""` restored.
+ if res.record != nil {
+ res.record()
+ }
+
+ assertWhitespaceWasNotAMessage(t, conn, mx)
+}
+
+func assertWhitespaceWasNotAMessage(t *testing.T, conn *mediaConn, mx *countingMetrics) {
+ t.Helper()
+ if got := markdownSends(t, conn); len(got) != 0 {
+ t.Errorf("the chat received %q, want nothing — a completion of whitespace is not a "+
+ "message", got)
+ }
+ if got := mx.get("outbound_delivered"); got != 0 {
+ t.Errorf("outbound_delivered = %d, want 0 — no words reached anybody", got)
+ }
+ if got := mx.get("outbound_skipped:" + string(skipNothingToSay)); got != 1 {
+ t.Errorf("outbound_skipped:%s = %d, want 1 — with no words, the files carry this "+
+ "reply's outcome, and there turned out to be none", skipNothingToSay, got)
+ }
+}
diff --git a/server/internal/integrations/wecom/relay_outbound.go b/server/internal/integrations/wecom/relay_outbound.go
index ce1eac7d95..2da5283a8b 100644
--- a/server/internal/integrations/wecom/relay_outbound.go
+++ b/server/internal/integrations/wecom/relay_outbound.go
@@ -1248,7 +1248,14 @@ func (o *Outbound) deliverRelayed(ctx context.Context, f relayFrame) relayResult
// a delivered reply, a dropped reply, or nothing at all depending on
// which replica happened to hold the socket.
var record func()
- if f.Content != "" {
+ // hasVisibleChar, not `!= ""`, and the same predicate the local path uses
+ // (outbound.go). A completion of "\n" carrying a file is words to neither
+ // of them: the local path sends nothing and lets the file carry the
+ // reply's outcome, and a frame routed here has to reach the same two
+ // conclusions or which replica held the socket decides whether the user
+ // sees a blank message and whether the text or the file is what the reply
+ // counter is counting.
+ if hasVisibleChar(f.Content) {
if err := sender.sendTextCtx(ctx, f.ChatID, f.ChatType, f.Content); err != nil {
// WHETHER THIS FRAME IS FINISHED IS SETTLED BEFORE ANY COUNTER
// MOVES. A frame that is owed another offer is still in flight,
@@ -1309,7 +1316,7 @@ func (o *Outbound) deliverRelayed(ctx context.Context, f relayFrame) relayResult
ChatID: f.ChatID,
ChatType: f.ChatType,
SessionID: f.SessionID,
- }, f.Content == "")
+ }, !hasVisibleChar(f.Content))
}
return relayResult{outcome: outcomeDone, record: record}
}
diff --git a/server/internal/integrations/wecom/ws_frame.go b/server/internal/integrations/wecom/ws_frame.go
index f5d2c5b9d7..bffcb941e3 100644
--- a/server/internal/integrations/wecom/ws_frame.go
+++ b/server/internal/integrations/wecom/ws_frame.go
@@ -720,3 +720,24 @@ func aibotChatTypeFromChannel(t channel.ChatType) int {
}
return chatTypeSingleInt
}
+
+// hasVisibleChar reports whether s contains a rune that is neither whitespace
+// nor a control character. That is the test a completion has to pass before it
+// becomes a message: a body the client renders as nothing still occupies a
+// bubble in the chat, and a completion of newlines is one.
+//
+// Not the same as "the client will render something", and deliberately not.
+// Format runes — U+200B zero width space, U+FEFF, a soft hyphen — are neither
+// space nor control, so a body made only of those passes here and still shows
+// as nothing. Nothing upstream rejects such a body either: it reaches the chat
+// as an empty bubble, and this predicate is not what stops it. The line is
+// drawn here to keep a Unicode category table out of the adapter — moving it
+// is a separate decision, and that table is its cost.
+func hasVisibleChar(s string) bool {
+ for _, r := range s {
+ if !unicode.IsSpace(r) && !unicode.IsControl(r) {
+ return true
+ }
+ }
+ return false
+}