Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(wecom): read a long answer in a bubble to the end (#9692)
Xichang(Seacen) Zhao53b3fba3d081
diff --git a/server/internal/integrations/wecom/long_reply_test.go b/server/internal/integrations/wecom/long_reply_test.goindex d8f1b561dd..379f8565b2 100644--- a/server/internal/integrations/wecom/long_reply_test.go+++ b/server/internal/integrations/wecom/long_reply_test.go@@ -19,6 +19,7 @@ package wecomimport ("context""encoding/json"+ "errors""regexp""strconv""strings"@@ -29,6 +30,7 @@ import ("unicode/utf8""github.com/multica-ai/multica/server/internal/events"+ "github.com/multica-ai/multica/server/internal/util"db "github.com/multica-ai/multica/server/pkg/db/generated""github.com/multica-ai/multica/server/pkg/protocol")@@ -825,3 +827,300 @@ func visibleOnly(s string) string {}return b.String()}++// readableInBubble is everything the person can read of a bubbled answer: what+// the sealed bubble holds, then the messages underneath it, in order.+func readableInBubble(t *testing.T, rig *bubbleRig) []string {+ t.Helper()+ var readable []string+ for _, s := range rig.conn.streamFrames(t) {+ if s["finish"] == true {+ readable = append(readable, s["content"].(string))+ }+ }+ if len(readable) == 0 {+ t.Fatal("the bubble was never sealed, so it spins forever and the answer is nowhere")+ }+ for _, push := range rig.conn.pushes(t) {+ readable = append(readable, pushText(push))+ }+ return readable+}++func assertWholeAnswer(t *testing.T, readable []string, answer string) {+ t.Helper()+ whole := reassemble(readable)+ if strings.HasSuffix(readable[0], "…") {+ t.Fatalf("the bubble was clipped to fit one frame and ends in an ellipsis; "++ "%d of %d bytes are readable", len(whole), len(answer))+ }+ if whole != answer {+ t.Fatalf("the person can read %d bytes of a %d-byte answer", len(whole), len(answer))+ }+ for i, part := range readable {+ if len(part) > sendMsgContentLimit {+ t.Fatalf("part %d is %d bytes, past the %d-byte cap", i+1, len(part), sendMsgContentLimit)+ }+ }+}++// A closing stream frame is capped at the same 20480 bytes as a message and is+// CLIPPED to fit rather than refused (truncateStreamContent): the answer looks+// complete, ends in an ellipsis, and the rest exists nowhere the reader can get+// to. The bubble holds what one frame can and the rest follows underneath.+//+// REVERSE VERIFICATION: seal the whole text in deliverAnswer instead of+// splitting it, and this reads 20478 of the answer's 67584 bytes, ending in "…".+func TestALongAnswerInABubbleIsReadableToTheEnd(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ rig.ask(t, "REQ-LONG")+ rig.queued(t, "task-1")++ answer := aLongAnswer()+ rig.answer(t, answer, "task-1")++ assertWholeAnswer(t, readableInBubble(t, rig), answer)+}++// The common answer is still one sealed bubble and nothing else: a follow-up+// message under a short reply would be a new defect, not a fix.+func TestABubbleThatStillFitsIsNotSplit(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ rig.ask(t, "REQ-SHORT")+ rig.queued(t, "task-1")+ rig.answer(t, "答案是 42", "task-1")++ if n := len(rig.conn.pushes(t)); n != 0 {+ t.Fatalf("a short answer produced %d message(s) under the bubble, want none", n)+ }+}++// The relayed path seals the same bubble with the same answer, so it has to+// make the same split: which replica produced the completion must not decide+// whether the reader gets the end of it.+//+// REVERSE VERIFICATION: seal the whole text in deliverRelayed instead of+// splitting it, and this reads one clipped frame of 20478 bytes.+func TestALongRelayedAnswerInABubbleIsReadableToTheEnd(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ rig.ran(t, "REQ-RELAY-LONG", "task-1")++ answer := aLongAnswer()+ res := rig.out.deliverRelayed(context.Background(), relayFrame{+ Kind: relayKindReply,+ InstallationID: util.UUIDToString(rig.instID),+ ChatID: "CHAT_1",+ ChatType: chatTypeGroupInt,+ Content: answer,+ TaskID: taskUUID(t, "task-1"),+ SessionID: bubbleSession,+ })+ if res.outcome != outcomeDone {+ t.Fatalf("outcome = %v, want outcomeDone", res.outcome)+ }+ assertWholeAnswer(t, readableInBubble(t, rig), answer)+}++// A piece under the bubble that cannot be sent leaves the reader with the head+// of the answer, which is what sendTextCtx's own split reports as delivered+// (errPartiallySent). The bubbled answer counts the same way: a drop tells an+// operator to resend, and a resend prints the head twice.+//+// REVERSE VERIFICATION: record a drop when a piece under the bubble fails, and+// this reports outbound_dropped = 1.+func TestALongBubbledAnswerWhoseRestFailsStillCountsDelivered(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ mx := newCountingMetrics()+ rig.out.metrics = mx+ rig.conn.failPushWrite = errors.New("broken pipe")+ rig.ask(t, "REQ-LONG-FAIL")+ rig.queued(t, "task-1")++ rig.answer(t, aLongAnswer(), "task-1")++ readable := readableInBubble(t, rig)+ if strings.HasSuffix(readable[0], "…") {+ t.Fatal("the bubble was clipped; the head is supposed to fit whole")+ }+ assertPartialCounted(t, mx, "a bubble whose second piece could not be sent")+}++// waitForChatWaiters returns once n callers hold or are queued on the chat's+// lock. A caller counts from the moment it registers, which is before it waits.+func waitForChatWaiters(t *testing.T, s *wsSender, chatID string, n int) {+ t.Helper()+ deadline := time.Now().Add(5 * time.Second)+ for {+ s.chats.mu.Lock()+ got := 0+ if l := s.chats.locks[chatID]; l != nil {+ got = l.refs+ }+ s.chats.mu.Unlock()+ if got >= n {+ return+ }+ if time.Now().After(deadline) {+ t.Fatalf("%d caller(s) on chat %s's lock, want %d", got, chatID, n)+ }+ time.Sleep(time.Millisecond)+ }+}++// The pieces under a bubble are one answer, so nothing else pushed to the same+// chat may land between them: with two long answers, or a file or an inbox card+// arriving while the rest goes out, the reader could no longer tell whose (2/4)+// is whose. sendTextCtx holds the chat for every piece of a split answer;+// sendRest has to hold it the same way.+//+// Another push is queued on the chat while the first piece under the bubble+// waits for its verdict, which is the moment a lock taken piece by piece is+// let go.+//+// REVERSE VERIFICATION: send the rest one sendTextCtx per piece, and the other+// push reads second, between (2/4) and (3/4).+func TestNothingLandsBetweenThePiecesUnderABubble(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ rig.ask(t, "REQ-LONG-LOCK")+ rig.queued(t, "task-1")++ sender := rig.senders.get(rig.instID)+ other := make(chan error, 1)+ rig.conn.onPush = func(n int) {+ if n != 1 {+ return+ }+ go func() {+ other <- rig.senders.sendTextCtx(context.Background(), rig.instID, "CHAT_1", chatTypeSingleInt, "another message")+ }()+ waitForChatWaiters(t, sender, "CHAT_1", 2)+ }++ answer := aLongAnswer()+ rig.answer(t, answer, "task-1")+ if err := <-other; err != nil {+ t.Fatalf("the other push: %v", err)+ }++ var texts []string+ for _, push := range rig.conn.pushes(t) {+ texts = append(texts, pushText(push))+ }+ if len(texts) < 3 {+ t.Fatalf("%d messages under the bubble, want the rest of the answer and the other push", len(texts))+ }+ if last := texts[len(texts)-1]; last != "another message" {+ at := -1+ for i, text := range texts {+ if text == "another message" {+ at = i+ }+ }+ t.Fatalf("the other push landed as message %d of %d, inside the answer; want it after the last piece", at+1, len(texts))+ }+ assertWholeAnswer(t, readableInBubble(t, rig)[:len(texts)], answer)+}++// The pieces under a bubble are reply frames, and every reply frame asks again+// before it is written (#9694): an installation revoked while the bubble is+// being sealed gets no more of the answer. The head is on screen, so the reply+// counts as delivered, as with a plain answer revoked after its first piece.+//+// REVERSE VERIFICATION: drop guardReply from sendRest and the three pieces+// under the bubble go out after the revoke.+func TestTheRestOfABubbledAnswerIsNotSentAfterTheRevoke(t *testing.T) {+ t.Parallel()+ r := newRevocableBubbleRig(t)+ r.ran(t, "REQ-LONG-REVOKE", "task-1")+ r.conn.onClosing = func() { r.rq.revoked.Store(true) }++ if err := r.out.processEvent(context.Background(), r.answerEvent(t, "task-1", aLongAnswer())); err != nil {+ t.Fatalf("processEvent: %v", err)+ }+ if n := r.closingFrames(t); n != 1 {+ t.Fatalf("%d closing frames, want 1: the head was sealed before the revoke", n)+ }+ if n := len(r.conn.pushes(t)); n != 0 {+ t.Fatalf("%d messages under the bubble, want 0: the rest went out after the revoke", n)+ }+ if got := r.mx.get("outbound_delivered"); got != 1 {+ t.Errorf("outbound_delivered = %d, want 1: the head is on screen", got)+ }+}++// assertRestUnderBubble checks that every piece after the head reached the+// chat as a message, in order, with nothing missing.+func assertRestUnderBubble(t *testing.T, rig *bubbleRig, answer string) {+ t.Helper()+ pieces := splitForWire(defuseThinkTags(answer))+ pushes := rig.conn.pushes(t)+ if len(pushes) != len(pieces)-1 {+ t.Fatalf("%d messages under the bubble, want the %d pieces after the head", len(pushes), len(pieces)-1)+ }+ for i, push := range pushes {+ if got := pushText(push); got != pieces[i+1] {+ t.Fatalf("message %d under the bubble is not piece %d of the answer", i+1, i+2)+ }+ }+}++// A closing frame whose verdict never comes back may or may not have put the+// head on screen. The rest was never sent, so sending it cannot print anything+// twice, and holding it back leaves a bubble that ends at (1/n) if the head did+// land. Its verdict is lost here until the turn's deadline is gone, which is+// how an unknown seal usually ends, so the pieces also need a budget of their+// own.+//+// REVERSE VERIFICATION: drop sendRest from the sealUnknown branch of+// deliverAnswer and no piece follows; drop fallbackBudget from sendRest and the+// pieces are attempted on the dead deadline and none is written.+func TestARestFollowsABubbleWhoseSealIsUnknown(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ mx := newCountingMetrics()+ rig.out.metrics = mx+ rig.conn.loseClosingAcks = 100+ rig.ask(t, "REQ-LONG-UNKNOWN")+ rig.queued(t, "task-1")++ answer := aLongAnswer()+ ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)+ defer cancel()+ rig.answerWithContext(t, ctx, answer, "task-1")++ assertRestUnderBubble(t, rig, answer)+ if got := mx.get("outbound_delivered"); got != 0 {+ t.Errorf("outbound_delivered = %d, want 0: the head's seal never answered", got)+ }+}++// The relayed path seals the same bubble and has to make the same call.+//+// REVERSE VERIFICATION: drop sendRest from the sealUnknown branch of+// deliverRelayed and no piece follows; drop fallbackBudget from sendRest and+// none is written.+func TestARestFollowsARelayedBubbleWhoseSealIsUnknown(t *testing.T) {+ t.Parallel()+ rig := newBubbleRig(t)+ rig.conn.loseClosingAcks = 100+ rig.ran(t, "REQ-RELAY-UNKNOWN", "task-1")++ answer := aLongAnswer()+ ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)+ defer cancel()+ rig.out.deliverRelayed(ctx, relayFrame{+ Kind: relayKindReply,+ InstallationID: util.UUIDToString(rig.instID),+ ChatID: "CHAT_1",+ ChatType: chatTypeGroupInt,+ Content: answer,+ TaskID: taskUUID(t, "task-1"),+ SessionID: bubbleSession,+ })+ assertRestUnderBubble(t, rig, answer)+}diff --git a/server/internal/integrations/wecom/outbound.go b/server/internal/integrations/wecom/outbound.goindex a8c843a030..e48a4bb2dd 100644--- a/server/internal/integrations/wecom/outbound.go+++ b/server/internal/integrations/wecom/outbound.go@@ -407,12 +407,20 @@ func (o *Outbound) deliverAnswer(ctx context.Context, e events.Event, taskID pgttext = c.StreamNoReply}}- sealErr := o.finishStream(ctx, t.Handle, text)+ // A closing frame is capped at the same 20480 bytes as any other body+ // and is CLIPPED to fit: the answer ends in an ellipsis and the rest of+ // it is nowhere the reader can get to. So the bubble carries what one+ // frame holds and the remainder follows as ordinary messages, split and+ // numbered the way sendTextCtx splits an answer with no bubble.+ head, rest := splitForBubble(text)+ sealErr := o.finishStream(ctx, t.Handle, head)switch classifySeal(sealErr) {case sealOnScreen:+ o.sendRest(ctx, t.Handle, rest)o.delivered()return answerOutcome{addr: t.Handle.address(), spoke: true}, nilcase sealUnknown:+ o.sendRest(ctx, t.Handle, rest)o.unconfirmed(ctx, e, unconfirmedSealReason(sealErr), sealErr)return answerOutcome{addr: t.Handle.address(), spoke: true}, nil}@@ -753,6 +761,62 @@ func (o *Outbound) finishStream(ctx context.Context, h streamHandle, text stringreturn err}+// splitForBubble divides an answer into the part one stream frame holds and+// the part that has to follow it as messages.+//+// It is splitForWire, so a bubble and a plain message break an answer at the+// same places and number the pieces the same way. Think tags are defused+// first: defusing inserts bytes, so splitting first could hand the frame a head+// that fits and is then pushed back over the cap (defusing is idempotent, so+// the frame's own pass changes nothing).+func splitForBubble(text string) (head string, rest []string) {+ pieces := splitForWire(defuseThinkTags(text))+ if len(pieces) <= 1 {+ return text, nil+ }+ return pieces[0], pieces[1:]+}++// sendRest delivers the pieces of an answer that did not fit in its sealed+// bubble, as messages directly underneath it, in order.+//+// All of them go out under one hold of the chat's lock (sendPiecesCtx), the+// way sendTextCtx sends a split answer: taken piece by piece, a second long+// answer, an inbox card or a file pushed to the same chat can land between+// them, and the reader can no longer tell whose (2/3) is whose.+//+// They are reply frames like any other, so each one re-reads the installation+// first (guardReply): a revocation that lands while they wait for the lock+// stops them.+//+// It runs when the seal is on screen and also when its outcome is unknown. The+// rest has never been sent, so sending it cannot print anything twice; if the+// head did land, the reader gets the whole answer, and if it did not, (2/n)+// onwards at least shows that the start is missing. Holding it back would leave+// a bubble that ends at (1/n) with nothing after it.+//+// The pieces get the budget the bubble cannot spend (fallbackBudget): a seal+// whose verdict never came back has usually used the turn's deadline up, and+// the rest is still the answer.+//+// A piece that fails cannot un-send anything before it; it stops the rest,+// because what follows only reads after it. The reply is counted by what the+// seal says, which is how sendTextCtx's own split reports a later piece+// failing (errPartiallySent), so a long answer is counted one way with or+// without a bubble.+func (o *Outbound) sendRest(ctx context.Context, h streamHandle, rest []string) {+ if len(rest) == 0 {+ return+ }+ ctx, cancel := fallbackBudget(ctx)+ defer cancel()+ if err := o.senders.sendPiecesCtx(guardReply(ctx, o.q, h.InstallationID), h.InstallationID, h.ChatID, h.ChatType, rest); err != nil {+ o.logger.WarnContext(ctx, "wecom outbound: only part of a long answer reached the chat",+ "installation_id", uuidStringPub(h.InstallationID),+ "pieces", len(rest)+1, "error", err)+ }+}+// chatDoneContent extracts the reply text from an EventChatDone payload// (the typed payload, or its map form after a serialization round trip).func chatDoneContent(payload any) string {diff --git a/server/internal/integrations/wecom/relay_outbound.go b/server/internal/integrations/wecom/relay_outbound.goindex 84cae65c44..8cd758867b 100644--- a/server/internal/integrations/wecom/relay_outbound.go+++ b/server/internal/integrations/wecom/relay_outbound.go@@ -1529,11 +1529,17 @@ func (o *Outbound) deliverRelayed(ctx context.Context, f relayFrame) relayResultif !hasVisibleChar(text) {text = wordlessSealCopy(t.Handle.Locale, f.CarriesFiles)}- sealErr := o.finishStream(ctx, t.Handle, text)+ // The same split the local path makes (outbound.go): a closing+ // frame is clipped to 20480 bytes, so the rest follows as+ // messages under the bubble.+ head, rest := splitForBubble(text)+ sealErr := o.finishStream(ctx, t.Handle, head)switch classifySeal(sealErr) {case sealOnScreen:+ o.sendRest(ctx, t.Handle, rest)record, spoke = o.delivered, truecase sealUnknown:+ o.sendRest(ctx, t.Handle, rest)se := sealErrrecord = func() {o.unconfirmedFor(ctx, f.SessionID, f.Kind, unconfirmedSealReason(se), se)diff --git a/server/internal/integrations/wecom/senders_registry.go b/server/internal/integrations/wecom/senders_registry.goindex 8c5c851436..894b51d798 100644--- a/server/internal/integrations/wecom/senders_registry.go+++ b/server/internal/integrations/wecom/senders_registry.go@@ -202,3 +202,14 @@ func (r *sendersRegistry) sendTextCtx(ctx context.Context, id pgtype.UUID, chatI}return sender.sendTextCtx(ctx, chatID, chatType, content)}++// sendPiecesCtx is sendTextCtx for an answer that is already split: every piece+// goes out under one hold of the chat's lock, so nothing another caller pushes+// to the same chat can land between them.+func (r *sendersRegistry) sendPiecesCtx(ctx context.Context, id pgtype.UUID, chatID string, chatType int, pieces []string) error {+ sender := r.get(id)+ if sender == nil {+ return errNoLiveConnection+ }+ return sender.sendPiecesCtx(ctx, chatID, chatType, pieces)+}diff --git a/server/internal/integrations/wecom/stream_bubble_test.go b/server/internal/integrations/wecom/stream_bubble_test.goindex 6502e72071..d90931c229 100644--- a/server/internal/integrations/wecom/stream_bubble_test.go+++ b/server/internal/integrations/wecom/stream_bubble_test.go@@ -58,6 +58,16 @@ type bubbleConn struct {// stream is dead and no bubble exists.failOpeningWrite error+ // failPushWrite makes the socket refuse every aibot_send_msg — the "as a+ // new message" path — while stream frames still go through.+ failPushWrite error++ // onPush runs after the nth aibot_send_msg (counted from 1) has been+ // recorded and before its verdict is routed: the sender is still inside+ // that piece, holding whatever it holds while it waits for an ack.+ onPush func(n int)+ pushWrites int+// onClosing runs after a closing frame has been recorded and before its// verdict is routed. It is how a test says "the socket dropped right after// this frame went out" — by swapping the installation's sender from@@ -75,7 +85,9 @@ func (c *bubbleConn) WriteMessage(_ int, data []byte) error {s := c.sendercode := 0lost := false- var onClosing func()+ // afterRecord runs once the frame is recorded and before its verdict is+ // routed: onClosing for a closing frame, onPush for a push.+ var afterRecord func()if isClosingFrame(env) {c.closingWrites++if c.failClosingWrite != nil {@@ -86,7 +98,14 @@ func (c *bubbleConn) WriteMessage(_ int, data []byte) error {code = c.refuseClosingCode}lost = c.closingWrites <= c.loseClosingAcks- onClosing = c.onClosing+ afterRecord = c.onClosing+ } else if env.Cmd == cmdSendMsg && c.failPushWrite != nil {+ c.mu.Unlock()+ return c.failPushWrite+ } else if env.Cmd == cmdSendMsg && c.onPush != nil {+ c.pushWrites+++ n, hook := c.pushWrites, c.onPush+ afterRecord = func() { hook(n) }} else if env.Cmd == cmdRespondMsg {if c.failOpeningWrite != nil {c.mu.Unlock()@@ -97,8 +116,8 @@ func (c *bubbleConn) WriteMessage(_ int, data []byte) error {}}c.mu.Unlock()- if onClosing != nil {- onClosing()+ if afterRecord != nil {+ afterRecord()}if s != nil && !lost {s.routeResponse(frameEnvelope{diff --git a/server/internal/integrations/wecom/ws_sender.go b/server/internal/integrations/wecom/ws_sender.goindex cee7bced98..ae092c5e9a 100644--- a/server/internal/integrations/wecom/ws_sender.go+++ b/server/internal/integrations/wecom/ws_sender.go@@ -1146,7 +1146,15 @@ func (s *wsSender) sendText(chatID string, chatTypeInt int, content string) erro// caller's question — may this send be tried again? — has a different answer// once part of the answer is in the chat.func (s *wsSender) sendTextCtx(ctx context.Context, chatID string, chatTypeInt int, content string) error {- pieces := splitForWire(content)+ return s.sendPiecesCtx(ctx, chatID, chatTypeInt, splitForWire(content))+}++// sendPiecesCtx sends pieces that are already cut to fit, in order, under one+// hold of the chat's lock. sendTextCtx is the caller for a whole answer; the+// other is the remainder of an answer whose head went into its bubble, which+// is split before the bubble is sealed and so arrives here already in pieces.+// Each piece must fit in one frame.+func (s *wsSender) sendPiecesCtx(ctx context.Context, chatID string, chatTypeInt int, pieces []string) error {// Held for every send, not only a split one: a single-frame push from// another caller — an inbox card, the file this same answer produced// (sendMedia takes the same lock), the unsupported-type notice — is