Skip to content

Commit 767e46f

Browse files
feat: add user input blocking lifecycle gates
1 parent 96348c4 commit 767e46f

4 files changed

Lines changed: 64 additions & 9 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4581,6 +4581,38 @@ describe("sendPolicy deny — suppress delivery, not processing (#53328)", () =>
45814581
);
45824582
});
45834583

4584+
it("preserves hook-blocked metadata when source delivery is message-tool-only", async () => {
4585+
setNoAbort();
4586+
sessionStoreMocks.currentEntry = {
4587+
sessionId: "s1",
4588+
updatedAt: 0,
4589+
sendPolicy: "allow",
4590+
};
4591+
const dispatcher = createDispatcher();
4592+
const blockedReply = setReplyPayloadMetadata(
4593+
{ text: "Your message could not be sent: blocked by policy-plugin", isError: true },
4594+
{ beforeAgentRunBlocked: true },
4595+
);
4596+
const replyResolver = vi.fn(async () => blockedReply satisfies ReplyPayload);
4597+
4598+
const result = await dispatchReplyFromConfig({
4599+
ctx: buildTestCtx({ SessionKey: "test:session" }),
4600+
cfg: emptyConfig,
4601+
dispatcher,
4602+
replyResolver,
4603+
replyOptions: {
4604+
sourceReplyDeliveryMode: "message_tool_only",
4605+
},
4606+
});
4607+
4608+
expect(replyResolver).toHaveBeenCalledTimes(1);
4609+
expect(result.queuedFinal).toBe(false);
4610+
expect(result.beforeAgentRunBlocked).toBe(true);
4611+
expect(result.sourceReplyDeliveryMode).toBe("message_tool_only");
4612+
expect(dispatcher.sendFinalReply).not.toHaveBeenCalled();
4613+
expect(dispatcher.sendBlockReply).not.toHaveBeenCalled();
4614+
});
4615+
45844616
it("delivers marked runtime failure notices in message-tool-only mode", async () => {
45854617
setNoAbort();
45864618
sessionStoreMocks.currentEntry = {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ import { withFullRuntimeReplyConfig } from "./get-reply-fast-path.js";
9595
import { claimInboundDedupe, commitInboundDedupe, releaseInboundDedupe } from "./inbound-dedupe.js";
9696
import { resolveOriginMessageProvider } from "./origin-routing.js";
9797
import { resolveReplyRoutingDecision } from "./routing-policy.js";
98-
import { resolveSourceReplyVisibilityPolicy } from "./source-reply-delivery-mode.js";
98+
import {
99+
isExplicitSourceReplyCommand,
100+
resolveSourceReplyVisibilityPolicy,
101+
} from "./source-reply-delivery-mode.js";
99102
import { resolveRunTypingPolicy } from "./typing-policy.js";
100103

101104
const routeReplyRuntimeLoader = createLazyImportLoader(() => import("./route-reply.runtime.js"));
@@ -711,7 +714,7 @@ export async function dispatchReplyFromConfig(
711714
const prefersMessageToolDelivery =
712715
params.replyOptions?.sourceReplyDeliveryMode === "message_tool_only" ||
713716
(params.replyOptions?.sourceReplyDeliveryMode === undefined &&
714-
ctx.CommandSource !== "native" &&
717+
!isExplicitSourceReplyCommand(ctx) &&
715718
(chatType === "group" || chatType === "channel"
716719
? effectiveVisibleReplies !== "automatic"
717720
: effectiveVisibleReplies === "message_tool"));

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,23 @@ describe("resolveSourceReplyDeliveryMode", () => {
9797
expect(
9898
resolveSourceReplyDeliveryMode({
9999
cfg: emptyConfig,
100-
ctx: { ChatType: "group", CommandSource: "text", CommandAuthorized: true },
100+
ctx: {
101+
ChatType: "group",
102+
CommandSource: "text",
103+
CommandAuthorized: true,
104+
CommandBody: "/status",
105+
},
101106
}),
102107
).toBe("automatic");
103108
expect(
104109
resolveSourceReplyDeliveryMode({
105110
cfg: emptyConfig,
106-
ctx: { ChatType: "group", CommandSource: "text" },
111+
ctx: {
112+
ChatType: "group",
113+
CommandSource: "text",
114+
CommandAuthorized: false,
115+
CommandBody: "/status",
116+
},
107117
}),
108118
).toBe("message_tool_only");
109119
});
@@ -192,7 +202,12 @@ describe("resolveSourceReplyVisibilityPolicy", () => {
192202
it("keeps native and authorized text command replies visible in groups", () => {
193203
for (const ctx of [
194204
{ ChatType: "group", CommandSource: "native" },
195-
{ ChatType: "group", CommandSource: "text", CommandAuthorized: true },
205+
{
206+
ChatType: "group",
207+
CommandSource: "text",
208+
CommandAuthorized: true,
209+
CommandBody: "/status",
210+
},
196211
] as const) {
197212
expect(
198213
resolveSourceReplyVisibilityPolicy({

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import type { SourceReplyDeliveryMode } from "../get-reply-options.types.js";
66
export type SourceReplyDeliveryModeContext = {
77
ChatType?: string;
88
CommandAuthorized?: boolean;
9+
CommandBody?: string;
910
CommandSource?: "text" | "native";
1011
};
1112

13+
export function isExplicitSourceReplyCommand(ctx: SourceReplyDeliveryModeContext): boolean {
14+
if (ctx.CommandSource === "native") {
15+
return true;
16+
}
17+
return ctx.CommandSource === "text" && ctx.CommandAuthorized === true;
18+
}
19+
1220
export function resolveSourceReplyDeliveryMode(params: {
1321
cfg: OpenClawConfig;
1422
ctx: SourceReplyDeliveryModeContext;
@@ -21,10 +29,7 @@ export function resolveSourceReplyDeliveryMode(params: {
2129
? "automatic"
2230
: params.requested;
2331
}
24-
if (
25-
params.ctx.CommandSource === "native" ||
26-
(params.ctx.CommandSource === "text" && params.ctx.CommandAuthorized === true)
27-
) {
32+
if (isExplicitSourceReplyCommand(params.ctx)) {
2833
return "automatic";
2934
}
3035
const chatType = normalizeChatType(params.ctx.ChatType);

0 commit comments

Comments
 (0)