Skip to content

Commit 8eaa4d6

Browse files
obviyusclaude
andcommitted
fix(auto-reply): require a directed turn for the no-visible-reply fallback
The fallback exists for a user who asked and got nothing. Gate it and the eligibility flag on positive directedness so ambient group chatter, room events, and turns whose classification facts were lost upstream can never surface a visible failure notice - even under silentReply: disallow. A turn is directed when it is an explicit command, or a non-room_event direct chat or mention/reply-to-bot; a command turn is the one directed room_event, mirroring the room_event source-reply suppression bypass. Every other room_event stays undirected regardless of a stray WasMentioned/direct classification, so the exact production regression - an unmentioned room_event group turn drawing the fallback into the channel on every ambient message - cannot recur through any room_event path. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 9641377 commit 8eaa4d6

4 files changed

Lines changed: 148 additions & 14 deletions

‎src/auto-reply/reply/dispatch-from-config.finalize.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
4545
markInboundDedupeReplayUnsafe,
4646
maybeApplyTtsWithFinalizationLease,
4747
normalizeReplyMediaPayload,
48+
noVisibleReplyFallbackDirected,
4849
preserveProgressCallbackStartOrder,
4950
reasoningPayloadsEnabled,
5051
recordAgentDispatchCompleted,
@@ -279,8 +280,11 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
279280

280281
await waitForPendingDirectBlockReplyDelivery(getDispatchAbortSignal());
281282
// Observed delivery is plugin-attested visibility, a trust level the transport
282-
// ledger intentionally does not own.
283+
// ledger intentionally does not own. Directedness gates both the fallback and
284+
// eligibility: only a turn that positively addressed the bot may surface a
285+
// visible failure notice.
283286
const noVisibleReplyFallbackAllowed = () =>
287+
noVisibleReplyFallbackDirected &&
284288
!suppressDelivery &&
285289
!sendPolicyDenied &&
286290
sourceReplyDeliveryMode !== "message_tool_only" &&
@@ -377,7 +381,8 @@ export async function finalizeDispatchAndAudit(state: ExecuteDispatchReadyState)
377381
// eligible, while any settled visible delivery clears it. An aborted or
378382
// timed-out settle leaves delivery unresolved, and a fallback reported as
379383
// delivered must not stay recoverable — either could double-send.
380-
...(queuedSettleResult === "settled" &&
384+
...(noVisibleReplyFallbackDirected &&
385+
queuedSettleResult === "settled" &&
381386
!turnLedger.hasVisibleDelivery() &&
382387
!noVisibleReplyFallbackDelivered &&
383388
!getObservedReplyDelivery() &&

‎src/auto-reply/reply/dispatch-from-config.hooks-and-send-policy.test-utils.ts‎

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
389389
expect(result).toEqual({ queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } });
390390
});
391391

392-
it("delivers core no-visible-reply fallback for disallowed empty group turns", async () => {
392+
it("delivers core no-visible-reply fallback for disallowed empty mentioned group turns", async () => {
393393
setNoAbort();
394394
const dispatcher = createDispatcher();
395395
const replyResolver = vi.fn(async () => undefined);
@@ -398,6 +398,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
398398
Surface: "feishu",
399399
Provider: "feishu",
400400
SessionKey: "agent:main:feishu:group:oc_group",
401+
WasMentioned: true,
401402
});
402403

403404
const result = await dispatchReplyFromConfig({
@@ -426,6 +427,112 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
426427
expect(result.noVisibleReplyFallbackEligible).toBeUndefined();
427428
});
428429

430+
it("keeps ambient group turns silent even when silence policy is disallow", async () => {
431+
setNoAbort();
432+
// The fallback exists for a user who asked and got nothing. An undirected
433+
// group turn never draws a visible failure notice, regardless of silence
434+
// policy (#114799: ambient HamVerBot group chatter drew fallback spam).
435+
const dispatcher = createDispatcher();
436+
const replyResolver = vi.fn(async () => undefined);
437+
const ctx = buildTestCtx({
438+
ChatType: "group",
439+
Surface: "telegram",
440+
Provider: "telegram",
441+
SessionKey: "agent:main:telegram:group:oc_group",
442+
});
443+
444+
const result = await dispatchReplyFromConfig({
445+
ctx,
446+
cfg: {
447+
agents: {
448+
defaults: {
449+
silentReply: {
450+
group: "disallow",
451+
},
452+
},
453+
},
454+
} as OpenClawConfig,
455+
dispatcher,
456+
replyResolver,
457+
});
458+
459+
expect(dispatcher.sendFinalReply).not.toHaveBeenCalled();
460+
expect(result.noVisibleReplyFallbackDelivered).toBeUndefined();
461+
expect(result.noVisibleReplyFallbackEligible).toBeUndefined();
462+
});
463+
464+
it("keeps room_event turns silent even when silence policy is disallow", async () => {
465+
setNoAbort();
466+
const dispatcher = createDispatcher();
467+
const replyResolver = vi.fn(async () => undefined);
468+
const ctx = buildTestCtx({
469+
ChatType: "group",
470+
Surface: "telegram",
471+
Provider: "telegram",
472+
SessionKey: "agent:main:telegram:group:oc_group",
473+
InboundEventKind: "room_event",
474+
});
475+
476+
const result = await dispatchReplyFromConfig({
477+
ctx,
478+
cfg: {
479+
agents: {
480+
defaults: {
481+
silentReply: {
482+
group: "disallow",
483+
},
484+
},
485+
},
486+
} as OpenClawConfig,
487+
dispatcher,
488+
replyResolver,
489+
});
490+
491+
expect(dispatcher.sendFinalReply).not.toHaveBeenCalledWith({
492+
text: NO_VISIBLE_REPLY_FALLBACK_TEXT,
493+
});
494+
expect(result.noVisibleReplyFallbackDelivered).toBeUndefined();
495+
expect(result.noVisibleReplyFallbackEligible).toBeUndefined();
496+
});
497+
498+
it("keeps room_event turns silent even when they carry a mention", async () => {
499+
setNoAbort();
500+
// A room_event is ambient by construction; a stray WasMentioned/direct
501+
// classification must not promote it to a directed turn (only a command
502+
// turn does, matching the room_event source-reply suppression bypass).
503+
const dispatcher = createDispatcher();
504+
const replyResolver = vi.fn(async () => undefined);
505+
const ctx = buildTestCtx({
506+
ChatType: "group",
507+
Surface: "telegram",
508+
Provider: "telegram",
509+
SessionKey: "agent:main:telegram:group:oc_group",
510+
InboundEventKind: "room_event",
511+
WasMentioned: true,
512+
});
513+
514+
const result = await dispatchReplyFromConfig({
515+
ctx,
516+
cfg: {
517+
agents: {
518+
defaults: {
519+
silentReply: {
520+
group: "disallow",
521+
},
522+
},
523+
},
524+
} as OpenClawConfig,
525+
dispatcher,
526+
replyResolver,
527+
});
528+
529+
expect(dispatcher.sendFinalReply).not.toHaveBeenCalledWith({
530+
text: NO_VISIBLE_REPLY_FALLBACK_TEXT,
531+
});
532+
expect(result.noVisibleReplyFallbackDelivered).toBeUndefined();
533+
expect(result.noVisibleReplyFallbackEligible).toBeUndefined();
534+
});
535+
429536
it("delivers fallback for mentioned group turns even when group silence is allowed", async () => {
430537
setNoAbort();
431538
const dispatcher = createDispatcher();
@@ -525,6 +632,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
525632
Surface: "telegram",
526633
Provider: "telegram",
527634
SessionKey: "agent:main:telegram:group:oc_group",
635+
WasMentioned: true,
528636
});
529637

530638
const result = await dispatchReplyFromConfig({
@@ -636,6 +744,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
636744
OriginatingChannel: "telegram",
637745
OriginatingTo: "telegram:999",
638746
SessionKey: "agent:main:slack:group:oc_group",
747+
WasMentioned: true,
639748
});
640749

641750
const result = await dispatchReplyFromConfig({
@@ -677,6 +786,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
677786
OriginatingChannel: "telegram",
678787
OriginatingTo: "telegram:999",
679788
SessionKey: "agent:main:slack:group:oc_group",
789+
WasMentioned: true,
680790
});
681791

682792
const result = await dispatchReplyFromConfig({
@@ -712,6 +822,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
712822
OriginatingChannel: "telegram",
713823
OriginatingTo: "telegram:999",
714824
SessionKey: "agent:main:slack:group:oc_group",
825+
WasMentioned: true,
715826
});
716827

717828
const result = await dispatchReplyFromConfig({
@@ -749,6 +860,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
749860
OriginatingChannel: "telegram",
750861
OriginatingTo: "telegram:999",
751862
SessionKey: "agent:main:slack:group:oc_group",
863+
WasMentioned: true,
752864
});
753865

754866
const result = await dispatchReplyFromConfig({
@@ -790,6 +902,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
790902
Surface: "telegram",
791903
Provider: "telegram",
792904
SessionKey: "agent:main:telegram:group:oc_group",
905+
WasMentioned: true,
793906
});
794907

795908
const result = await dispatchReplyFromConfig({
@@ -827,6 +940,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
827940
Surface: "telegram",
828941
Provider: "telegram",
829942
SessionKey: "agent:main:telegram:group:oc_group",
943+
WasMentioned: true,
830944
});
831945

832946
const result = await dispatchReplyFromConfig({
@@ -861,6 +975,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
861975
Surface: "telegram",
862976
Provider: "telegram",
863977
SessionKey: "agent:main:telegram:group:oc_group",
978+
WasMentioned: true,
864979
});
865980

866981
const result = await dispatchReplyFromConfig({
@@ -905,6 +1020,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
9051020
OriginatingChannel: "telegram",
9061021
OriginatingTo: "telegram:999",
9071022
SessionKey: "agent:main:slack:group:oc_group",
1023+
WasMentioned: true,
9081024
});
9091025

9101026
const result = await dispatchReplyFromConfig({
@@ -951,6 +1067,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
9511067
Surface: "telegram",
9521068
Provider: "telegram",
9531069
SessionKey: "agent:main:telegram:group:oc_group",
1070+
WasMentioned: true,
9541071
});
9551072

9561073
const result = await dispatchReplyFromConfig({
@@ -991,6 +1108,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
9911108
Surface: "telegram",
9921109
Provider: "telegram",
9931110
SessionKey: "agent:main:telegram:group:oc_group",
1111+
WasMentioned: true,
9941112
});
9951113

9961114
const result = await dispatchReplyFromConfig({
@@ -1029,6 +1147,7 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
10291147
Surface: "telegram",
10301148
Provider: "telegram",
10311149
SessionKey: "agent:main:telegram:group:oc_group",
1150+
WasMentioned: true,
10321151
});
10331152

10341153
const result = await dispatchReplyFromConfig({

‎src/auto-reply/reply/dispatch-from-config.prepare-context.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,18 @@ export async function prepareDispatchOperationContext(state: PrepareDispatchDeli
351351
const explicitCommandTurnCtx = isExplicitSourceReplyCommand(ctx, cfg);
352352
const unauthorizedTextSlashSourceReplyCtx =
353353
(chatType === "group" || chatType === "channel") && isUnauthorizedTextSlashCommand(ctx);
354+
// The no-visible-reply fallback exists for a user who asked and got nothing.
355+
// Only positively directed turns qualify: direct chats, explicit mentions
356+
// (channels fold reply-to-bot into WasMentioned), and command turns. Ambient
357+
// group chatter, room events, and turns whose classification facts were lost
358+
// upstream (queued followups, rebuilt contexts) can never draw a visible
359+
// failure notice, even when silence policy is disallow. A command turn is the
360+
// one directed room_event (mirrors the room_event source-reply suppression
361+
// bypass below); every other room_event stays undirected regardless of a
362+
// stray WasMentioned/direct classification.
363+
const noVisibleReplyFallbackDirected =
364+
explicitCommandTurnCtx ||
365+
(ctx.InboundEventKind !== "room_event" && (chatType === "direct" || ctx.WasMentioned === true));
354366
const shouldDeliverPluginBindingReply =
355367
!suppressAutomaticSourceDelivery ||
356368
explicitCommandTurnCtx ||
@@ -510,6 +522,7 @@ export async function prepareDispatchOperationContext(state: PrepareDispatchDeli
510522
sendPolicy,
511523
chatType,
512524
emptyFinalAllowedAsSilent,
525+
noVisibleReplyFallbackDirected,
513526
sourceReplyPolicy,
514527
sourceReplyDeliveryMode,
515528
sessionStableSourceReplyDeliveryMode,

‎src/auto-reply/reply/dispatch-from-config.reply-dispatch.test.ts‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,12 @@ describe("dispatchReplyFromConfig reply_dispatch hook", () => {
204204
});
205205

206206
expect(hookMocks.runner.runReplyDispatch).toHaveBeenCalled();
207+
// createHookCtx's "private" chat type is undirected, so no fallback
208+
// eligibility surfaces for this turn.
207209
expect(result).toEqual({
208210
queuedFinal: false,
209211
counts: { tool: 0, block: 0, final: 0 },
210212
sendPolicyDenied: true,
211-
noVisibleReplyFallbackEligible: true,
212213
});
213214
});
214215

@@ -381,14 +382,13 @@ describe("dispatchReplyFromConfig reply_dispatch hook", () => {
381382
});
382383
await hookStarted.promise;
383384
await vi.advanceTimersByTimeAsync(15_000);
384-
// The timed-out final settles invisible, so the no-visible-reply fallback
385-
// is attempted through the same wedged hook and needs its own stage budget.
386-
await vi.advanceTimersByTimeAsync(15_000);
387385
const result = await resultPromise;
388386

389387
expect(result.queuedFinal).toBe(true);
390388
expect(deliver).not.toHaveBeenCalled();
391-
expect(dispatcher.getFailedCounts?.()).toEqual({ tool: 0, block: 0, final: 2 });
389+
// createHookCtx's "private" chat type is undirected, so no fallback
390+
// attempt follows the timed-out final.
391+
expect(dispatcher.getFailedCounts?.()).toEqual({ tool: 0, block: 0, final: 1 });
392392
expect(sessionStoreMocks.updateSessionEntry).toHaveBeenCalledOnce();
393393
expect(sessionStoreMocks.currentEntry?.pendingFinalDelivery).toBe(true);
394394
expect(sessionStoreMocks.currentEntry?.pendingFinalDeliveryText).toBe("durable reply");
@@ -675,9 +675,6 @@ describe("dispatchReplyFromConfig reply_dispatch hook", () => {
675675
pendingFinalDeliveryIntentId: "newer-intent",
676676
};
677677
await vi.advanceTimersByTimeAsync(15_000);
678-
// Second stage budget: the no-visible-reply fallback runs through the same
679-
// wedged hook after the older final times out.
680-
await vi.advanceTimersByTimeAsync(15_000);
681678
await resultPromise;
682679

683680
expect(sessionStoreMocks.currentEntry?.pendingFinalDelivery).toBe(true);
@@ -756,9 +753,9 @@ describe("dispatchReplyFromConfig reply_dispatch hook", () => {
756753

757754
expect(result.queuedFinal).toBe(true);
758755
expect(deliver).not.toHaveBeenCalled();
759-
// The cancelled final settles invisible, so the no-visible-reply fallback is
760-
// attempted and cancelled by the same hook before the turn ends.
761-
expect(dispatcher.getCancelledCounts?.()).toEqual({ tool: 0, block: 0, final: 2 });
756+
// createHookCtx's "private" chat type is undirected, so the cancelled final
757+
// does not trigger a fallback attempt.
758+
expect(dispatcher.getCancelledCounts?.()).toEqual({ tool: 0, block: 0, final: 1 });
762759
expect(dispatcher.getFailedCounts?.()).toEqual({ tool: 0, block: 0, final: 0 });
763760
expect(sessionStoreMocks.currentEntry?.pendingFinalDelivery).toBeUndefined();
764761
expect(sessionStoreMocks.currentEntry?.pendingFinalDeliveryText).toBeUndefined();

0 commit comments

Comments
 (0)