Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
fix(wecom): honor inbox deadlines before cancellation fires (#9334)
Bohan Jiangdcc8f4937fe0
diff --git a/server/internal/integrations/wecom/outbound_inbox_wait_test.go b/server/internal/integrations/wecom/outbound_inbox_wait_test.goindex 91375b2226..7218244eb1 100644--- a/server/internal/integrations/wecom/outbound_inbox_wait_test.go+++ b/server/internal/integrations/wecom/outbound_inbox_wait_test.go@@ -70,38 +70,78 @@ func (r inboxWaitRig) deliver(ctx context.Context, item map[string]any) (bool, t// measured on the request that created the comment.func TestTryDeliverInbox_MemberAuthoredPushDoesNotWaitForTheAcceptanceWindow(t *testing.T) {t.Parallel()- // The production default: cmd/server does not set AcceptanceWindow.- rig := newInboxWaitRig(t, 2*time.Second)- item := agentInboxItem("new_comment", inboxWaitAgentID)- item["actor_type"] = util.TextToPtr(pgtype.Text{String: "member", Valid: true})-- got, elapsed := rig.deliver(context.Background(), item)- if elapsed > 300*time.Millisecond {- t.Fatalf("a member-authored push blocked %v; a verdict with no fallback behind it must not be waited for", elapsed)- }- if !got {- t.Error("tryDeliverInbox = false; the bus took the frame and there is no second route to report a miss to")- }- if n := rig.dedupe.claimStateCount(); n != 0 {- t.Errorf("the claim was polled %d times; a member-authored push must not enter the acceptance window", n)- }+ synctest.Test(t, func(t *testing.T) {+ // The production default: cmd/server does not set AcceptanceWindow.+ rig := newInboxWaitRig(t, 2*time.Second)+ item := agentInboxItem("new_comment", inboxWaitAgentID)+ item["actor_type"] = util.TextToPtr(pgtype.Text{String: "member", Valid: true})++ got, elapsed := rig.deliver(context.Background(), item)+ if elapsed != 0 {+ t.Fatalf("a member-authored push blocked %v; a verdict with no fallback behind it must not be waited for", elapsed)+ }+ if !got {+ t.Error("tryDeliverInbox = false; the bus took the frame and there is no second route to report a miss to")+ }+ if n := rig.dedupe.claimStateCount(); n != 0 {+ t.Errorf("the claim was polled %d times; a member-authored push must not enter the acceptance window", n)+ }+ })}+// pendingInboxDeadline models a deadline whose cancellation callback has not run.+type pendingInboxDeadline struct {+ context.Context+ deadline time.Time+}++func (c pendingInboxDeadline) Deadline() (time.Time, bool) { return c.deadline, true }+// TestTryDeliverInbox_WaitIsBoundedByTheCallersDeadline: the wait runs on the// request's goroutine, so the caller's budget has to cap it. A 3s window with a// 150ms caller must come back in the caller's time, not the window's.func TestTryDeliverInbox_WaitIsBoundedByTheCallersDeadline(t *testing.T) {t.Parallel()- rig := newInboxWaitRig(t, 3*time.Second)- ctx, cancel := context.WithTimeout(context.Background(), 150*time.Millisecond)- defer cancel()+ for _, tc := range []struct {+ name string+ cancellationPending bool+ }{+ {name: "canceled"},+ {name: "cancellation_pending", cancellationPending: true},+ } {+ t.Run(tc.name, func(t *testing.T) {+ synctest.Test(t, func(t *testing.T) {+ const budget = 150 * time.Millisecond+ rig := newInboxWaitRig(t, 3*time.Second)+ var ctx context.Context+ if tc.cancellationPending {+ ctx = pendingInboxDeadline{Context: context.Background(), deadline: time.Now().Add(budget)}+ } else {+ var cancel context.CancelFunc+ ctx, cancel = context.WithTimeout(context.Background(), budget)+ defer cancel()+ }- got, elapsed := rig.deliver(ctx, agentInboxItem("new_comment", inboxWaitAgentID))- if elapsed > 900*time.Millisecond {- t.Fatalf("the push blocked %v with a 150ms caller budget; the caller's ctx must cap the window", elapsed)- }- if got {- t.Error("tryDeliverInbox = true; a caller that ran out of time confirmed nothing")+ item := agentInboxItem("new_comment", inboxWaitAgentID)+ got, elapsed := rig.deliver(ctx, item)+ if elapsed != budget {+ t.Fatalf("the push blocked %v, want exactly the caller's %v budget", elapsed, budget)+ }+ if got {+ t.Error("tryDeliverInbox = true; a caller that ran out of time confirmed nothing")+ }+ if tc.cancellationPending && ctx.Err() != nil {+ t.Fatal("the deadline's cancellation must remain pending to exercise the timer ordering")+ }+ if n := rig.relay.publishedTo(inboxWaitFallbackInst); n != 0 {+ t.Errorf("the fallback was offered %d frames after the caller's deadline, want 0", n)+ }+ eventID := relayInboxEventID(itemIDOf(item), "33333333-3333-3333-3333-333333333333", inboxWaitAgentInst)+ if rig.dedupe.holds(dedupeKey(eventID)) {+ t.Error("the unconfirmed delivery was fenced after the caller's deadline")+ }+ })+ })}}diff --git a/server/internal/integrations/wecom/relay_outbound.go b/server/internal/integrations/wecom/relay_outbound.goindex 6e1fbcd776..b8d0e07f77 100644--- a/server/internal/integrations/wecom/relay_outbound.go+++ b/server/internal/integrations/wecom/relay_outbound.go@@ -905,8 +905,9 @@ func (r *RelayOutbound) confirmInbox(ctx context.Context, eventID string) relayP// a window it configured elsewhere. A tighter caller deadline shortens the// window; with no deadline the window stands.deadline := time.Now().Add(window)- if d, ok := ctx.Deadline(); ok && d.Before(deadline) {- deadline = d+ callerDeadline, hasCallerDeadline := ctx.Deadline()+ if hasCallerDeadline && callerDeadline.Before(deadline) {+ deadline = callerDeadline}// The poll's OWN deadline ends the wait; the context is the backstop that// keeps a claim store which ignores its deadline from outlasting it, so it@@ -948,14 +949,15 @@ func (r *RelayOutbound) confirmInbox(ctx context.Context, eventID string) relayPreturn relayUncertain}}- // A caller that ran out of time has nothing left to re-route WITH: its own- // local send would see the same expired context, so the honest answer is- // that the delivery is unknown -- nothing sent, nothing re-routed. In a- // production request the 5s budget leaves the 2s window plenty of room, so- // this is the backstop rather than the common exit.- if err := ctx.Err(); err != nil {+ // A deadline can pass before its cancellation callback updates Err. An+ // expired caller must not fence the unconfirmed delivery or start a fallback.+ callerErr := ctx.Err()+ if callerErr == nil && hasCallerDeadline && !time.Now().Before(callerDeadline) {+ callerErr = context.DeadlineExceeded+ }+ if callerErr != nil {r.logger.WarnContext(ctx, "wecom relay: the caller's budget ran out before an inbox push's bot could be confirmed; not re-routing",- "error", err, "event_id", eventID)+ "error", callerErr, "event_id", eventID)return relayUncertain}won, err := r.dedupe.Claim(pollCtx, key, r.fenceTokenFor(eventID), r.dedupeTTL)