Skip to content

Commit ade0fb2

Browse files
authored
fix(auto-reply): restore ambient room event silence (#126527)
Restore intentional silence for undirected room events while retaining visible-answer recovery for mentions, commands, and ordinary requests. Co-authored-by: Ayaan Zaidi <[email protected]>
1 parent 916eef4 commit ade0fb2

9 files changed

Lines changed: 59 additions & 20 deletions

‎src/auto-reply/reply/agent-runner-run-params.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ export function buildEmbeddedRunBaseParams(params: {
111111
enforceFinalTag,
112112
silentExpected: params.run.silentExpected,
113113
allowEmptyAssistantReplyAsSilent: params.run.allowEmptyAssistantReplyAsSilent,
114+
terminalReplyExpectation: params.run.terminalReplyExpectation,
114115
silentReplyPromptMode: params.run.silentReplyPromptMode,
115116
sourceReplyDeliveryMode: params.run.sourceReplyDeliveryMode,
116117
clientCaps: params.run.clientCaps,

‎src/auto-reply/reply/agent-runner-utils.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ describe("agent-runner-utils", () => {
148148
enforceFinalTag: true,
149149
cwd: "/tmp/task-repo",
150150
taskSuggestionDeliveryMode: "gateway",
151+
terminalReplyExpectation: "optional",
151152
trustedInternalHandoff: {
152153
kind: "subagent-completion",
153154
sourceSessionKey: "agent:child",
@@ -202,6 +203,7 @@ describe("agent-runner-utils", () => {
202203
expect(resolved.runId).toBe("run-1");
203204
expect(resolved.promptCacheKey).toBe("webchat-cache-key");
204205
expect(resolved.taskSuggestionDeliveryMode).toBe("gateway");
206+
expect(resolved.terminalReplyExpectation).toBe("optional");
205207
});
206208

207209
it("threads prompt cache affinity through embedded execution params", () => {

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { waitForReplyDispatcherIdle } from "./reply-dispatcher.js";
4848
import { isDuplicateRestartRecoverySource } from "./restart-recovery-claim.js";
4949
import { resolveStableMessageToolAvailability } from "./session-stable-reply-mode.js";
5050
import {
51+
isDirectedSourceReplyTurn,
5152
isExplicitSourceReplyCommand,
5253
isUnauthorizedTextSlashCommand,
5354
resolveSourceReplyVisibilityPolicy,
@@ -378,18 +379,7 @@ export async function prepareDispatchOperationContext(state: PrepareDispatchDeli
378379
const explicitCommandTurnCtx = isExplicitSourceReplyCommand(ctx, cfg);
379380
const unauthorizedTextSlashSourceReplyCtx =
380381
(chatType === "group" || chatType === "channel") && isUnauthorizedTextSlashCommand(ctx);
381-
// The no-visible-reply fallback exists for a user who asked and got nothing.
382-
// Only positively directed turns qualify: direct chats, explicit mentions
383-
// (channels fold reply-to-bot into WasMentioned), and command turns. Ambient
384-
// group chatter, room events, and turns whose classification facts were lost
385-
// upstream (queued followups, rebuilt contexts) can never draw a visible
386-
// failure notice, even when silence policy is disallow. A command turn is the
387-
// one directed room_event (mirrors the room_event source-reply suppression
388-
// bypass below); every other room_event stays undirected regardless of a
389-
// stray WasMentioned/direct classification.
390-
const noVisibleReplyFallbackDirected =
391-
explicitCommandTurnCtx ||
392-
(ctx.InboundEventKind !== "room_event" && (chatType === "direct" || ctx.WasMentioned === true));
382+
const noVisibleReplyFallbackDirected = isDirectedSourceReplyTurn(ctx, cfg, chatType === "direct");
393383
const shouldDeliverPluginBindingReply =
394384
!suppressAutomaticSourceDelivery ||
395385
explicitCommandTurnCtx ||

‎src/auto-reply/reply/get-reply-run-context.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
} from "./session-reset-prompt.js";
5454
import { resolveSessionStableReplyMode } from "./session-stable-reply-mode.js";
5555
import {
56-
isExplicitSourceReplyCommand,
56+
isDirectedSourceReplyTurn,
5757
isSyntheticSourceReplyTurn,
5858
} from "./source-reply-delivery-mode.js";
5959
import { shouldApplyStartupContext, buildSessionStartupContextPrelude } from "./startup-context.js";
@@ -223,11 +223,18 @@ export async function prepareReplyRunContext(params: RunPreparedReplyParams) {
223223
sourceConversationContextByMode[sessionPromptSourceReplyDeliveryMode ?? "automatic"];
224224
// Claude CLI fixes the system prompt at session creation; group intro must stay session-stable.
225225
const groupIntro = isGroupChat ? buildGroupIntro({ sessionEntry, defaultActivation }) : "";
226-
const isDirectedTurn =
227-
isExplicitSourceReplyCommand(ctx, cfg) ||
228-
(inboundEventKind !== "room_event" && (isDirectChat || ctx.WasMentioned === true));
226+
const isDirectedTurn = isDirectedSourceReplyTurn(ctx, cfg, isDirectChat, inboundEventKind);
227+
const isAmbientRoomEvent = inboundEventKind === "room_event" && !isDirectedTurn;
229228
const allowEmptyAssistantReplyAsSilent =
230-
isGroupChat && !isDirectedTurn && silentReplySettings.policy === "allow";
229+
isGroupChat &&
230+
!isDirectedTurn &&
231+
(isAmbientRoomEvent || silentReplySettings.policy === "allow");
232+
// Heartbeats retain the embedded runner's trigger-owned optional default.
233+
const terminalReplyExpectation = isHeartbeat
234+
? undefined
235+
: isAmbientRoomEvent
236+
? "optional"
237+
: "required";
231238
const groupSystemPrompt = normalizeOptionalString(promptSessionCtx.GroupSystemPrompt) ?? "";
232239
const inboundMetaPrompt = buildInboundMetaSystemPrompt(
233240
isNewSession ? sessionCtx : { ...sessionCtx, ThreadStarterBody: undefined },
@@ -476,6 +483,7 @@ export async function prepareReplyRunContext(params: RunPreparedReplyParams) {
476483
getInboundContext: () => ({ activeGoalContext, inboundUserContext }),
477484
refreshInboundContextAfterAdmissionWait,
478485
allowEmptyAssistantReplyAsSilent,
486+
terminalReplyExpectation,
479487
} as const;
480488
}
481489

‎src/auto-reply/reply/get-reply-run-execute.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export async function executePreparedReplyRun(state: PreparedReplyRunAdmission)
106106
shouldInjectGroupIntro,
107107
typingMode,
108108
allowEmptyAssistantReplyAsSilent,
109+
terminalReplyExpectation,
109110
} = context;
110111
const {
111112
ctx,
@@ -497,6 +498,7 @@ export async function executePreparedReplyRun(state: PreparedReplyRunAdmission)
497498
cliSessionBindingFacts,
498499
skipProviderRuntimeHints: useFastReplyRuntime,
499500
allowEmptyAssistantReplyAsSilent,
501+
terminalReplyExpectation,
500502
suppressTranscriptOnlyAssistantPersistence: isRoomEvent,
501503
...(opts?.skillWorkshopProposalRevision
502504
? {

‎src/auto-reply/reply/get-reply-run.media-only.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,15 @@ describe("runPreparedReply media-only handling", () => {
622622

623623
let call = requireLastRunReplyAgentCall();
624624
expect(call?.followupRun.run.allowEmptyAssistantReplyAsSilent).toBe(true);
625+
expect(call?.followupRun.run.terminalReplyExpectation).toBe("required");
625626

626627
await runPrepared({
627628
defaultActivation: "mention",
628629
});
629630

630631
call = requireLastRunReplyAgentCall();
631632
expect(call?.followupRun.run.allowEmptyAssistantReplyAsSilent).toBe(true);
633+
expect(call?.followupRun.run.terminalReplyExpectation).toBe("required");
632634
});
633635

634636
it.each([
@@ -658,9 +660,10 @@ describe("runPreparedReply media-only handling", () => {
658660

659661
const call = requireLastRunReplyAgentCall();
660662
expect(call?.followupRun.run.allowEmptyAssistantReplyAsSilent).toBe(false);
663+
expect(call?.followupRun.run.terminalReplyExpectation).toBe("required");
661664
});
662665

663-
it("keeps empty-assistant silence available for ambient room events", async () => {
666+
it("keeps empty-assistant silence optional for ambient room events", async () => {
664667
const defaults = baseParams();
665668
await runPrepared({
666669
ctx: {
@@ -673,10 +676,20 @@ describe("runPreparedReply media-only handling", () => {
673676
InboundEventKind: "room_event",
674677
WasMentioned: true,
675678
},
679+
cfg: {
680+
agents: {
681+
defaults: {
682+
silentReply: { group: "disallow" },
683+
},
684+
},
685+
},
676686
});
677687

678688
const call = requireLastRunReplyAgentCall();
679-
expect(call?.followupRun.run.allowEmptyAssistantReplyAsSilent).toBe(true);
689+
expect(call.followupRun.run).toMatchObject({
690+
allowEmptyAssistantReplyAsSilent: true,
691+
terminalReplyExpectation: "optional",
692+
});
680693
});
681694

682695
it("hydrates runtime thinking metadata before trusting static provider support", async () => {
@@ -2420,6 +2433,7 @@ describe("runPreparedReply media-only handling", () => {
24202433
expect(call?.shouldSteer).toBe(false);
24212434
expect(call?.shouldFollowup).toBe(true);
24222435
expect(call?.isActive).toBe(true);
2436+
expect(call?.followupRun.run.terminalReplyExpectation).toBeUndefined();
24232437
});
24242438

24252439
it.each([

‎src/auto-reply/reply/queue/drain.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,7 @@ export function resolveFollowupDeliveryContextKey(run: FollowupRun): string {
280280
execution.skipProviderRuntimeHints === true,
281281
execution.silentExpected === true,
282282
execution.allowEmptyAssistantReplyAsSilent === true,
283+
execution.terminalReplyExpectation ?? "",
283284
execution.suppressNextUserMessagePersistence === true,
284285
execution.suppressTranscriptOnlyAssistantPersistence === true,
285286
execution.blockReplyBreak,

‎src/auto-reply/reply/queue/types.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import type { QueueMode } from "../../../../packages/gateway-protocol/src/schema
44
import type { AutoFallbackPrimaryProbe } from "../../../agents/agent-scope.js";
55
import type { ExecToolDefaults } from "../../../agents/bash-tools.js";
66
import type { CliSessionBindingFacts } from "../../../agents/cli-runner/types.js";
7-
import type { CurrentInboundPromptContext } from "../../../agents/embedded-agent-runner/run/params.js";
7+
import type {
8+
CurrentInboundPromptContext,
9+
RunEmbeddedAgentParams,
10+
} from "../../../agents/embedded-agent-runner/run/params.js";
811
import type { ModelFallbackRouteResolution } from "../../../agents/model-fallback.types.js";
912
import type { ScheduledToolPolicyContext } from "../../../agents/scheduled-tool-policy.js";
1013
import type { TrustedSubagentCompletionHandoff } from "../../../agents/subagents/announce/subagent-announce-handoff.js";
@@ -230,6 +233,7 @@ export type FollowupRun = {
230233
skipProviderRuntimeHints?: boolean;
231234
silentExpected?: boolean;
232235
allowEmptyAssistantReplyAsSilent?: boolean;
236+
terminalReplyExpectation?: RunEmbeddedAgentParams["terminalReplyExpectation"];
233237
suppressNextUserMessagePersistence?: boolean;
234238
suppressTranscriptOnlyAssistantPersistence?: boolean;
235239
/** Gateway-private optimistic-concurrency constraint for an operator-requested proposal revision. */

‎src/auto-reply/reply/source-reply-delivery-mode.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export type SourceReplyDeliveryModeContext = {
2121
CommandSource?: "text" | "native";
2222
CommandTurn?: CommandTurnContext;
2323
BotUsername?: string;
24+
WasMentioned?: boolean;
2425
InputProvenance?: InputProvenance;
2526
};
2627

@@ -43,6 +44,22 @@ export function isExplicitSourceReplyCommand(
4344
return isExplicitCommandTurnContext(ctx, cfg);
4445
}
4546

47+
/**
48+
* Room events remain ambient despite stale mention/direct facts. Explicit commands stay directed
49+
* because their parsed command context is authoritative.
50+
*/
51+
export function isDirectedSourceReplyTurn(
52+
ctx: SourceReplyDeliveryModeContext,
53+
cfg: OpenClawConfig,
54+
isDirectChat: boolean,
55+
inboundEventKind = ctx.InboundEventKind,
56+
): boolean {
57+
return (
58+
isExplicitSourceReplyCommand(ctx, cfg) ||
59+
(inboundEventKind !== "room_event" && (isDirectChat || ctx.WasMentioned === true))
60+
);
61+
}
62+
4663
/** Returns true for text slash commands that lack authorization metadata. */
4764
export function isUnauthorizedTextSlashCommand(ctx: SourceReplyDeliveryModeContext): boolean {
4865
const commandTurn = resolveCommandTurnContext(ctx);

0 commit comments

Comments
 (0)