Skip to content

Commit edbcf11

Browse files
authored
refactor(auto-reply): privatize reply helpers (#106600)
1 parent b69e047 commit edbcf11

16 files changed

Lines changed: 75 additions & 176 deletions

‎scripts/deadcode-exports.baseline.mjs‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,41 +1211,32 @@ export const KNIP_UNUSED_EXPORT_BASELINE = [
12111211
"src/auto-reply/reply/agent-runner-execution.ts: MAX_LIVE_SWITCH_RETRIES",
12121212
"src/auto-reply/reply/agent-runner-memory.ts: setAgentRunnerMemoryTestDeps",
12131213
"src/auto-reply/reply/agent-runner-session-reset.ts: setAgentRunnerSessionResetTestDeps",
1214-
"src/auto-reply/reply/block-reply-pipeline.ts: createBlockReplyPayloadKey",
12151214
"src/auto-reply/reply/commands-diagnostics.ts: createDiagnosticsCommandHandler",
12161215
"src/auto-reply/reply/commands-login.ts: testing",
12171216
"src/auto-reply/reply/commands-mcp.ts: createMcpCommandHandler",
12181217
"src/auto-reply/reply/commands-name.ts: parseNameCommand",
12191218
"src/auto-reply/reply/commands-setunset.ts: parseSetUnsetCommand",
12201219
"src/auto-reply/reply/commands-setunset.ts: parseSetUnsetCommandAction",
12211220
"src/auto-reply/reply/commands-tasks.ts: buildTasksReply",
1222-
"src/auto-reply/reply/completion-delivery-policy.ts: resolveCompletionChatType",
12231221
"src/auto-reply/reply/delivery-hints.ts: MESSAGE_TOOL_ONLY_DELIVERY_HINT",
12241222
"src/auto-reply/reply/delivery-hints.ts: ROOM_EVENT_DELIVERY_HINT",
12251223
"src/auto-reply/reply/dispatch-from-config.ts: getDispatcherFinalOutcomeCounts",
12261224
"src/auto-reply/reply/dispatch-from-config.ts: testing",
1227-
"src/auto-reply/reply/dispatcher-registry.ts: clearAllDispatchers",
12281225
"src/auto-reply/reply/get-reply-directives-apply.ts: formatModelOverrideResetEvent",
12291226
"src/auto-reply/reply/get-reply-fast-path.ts: markCompleteReplyConfig",
12301227
"src/auto-reply/reply/get-reply-fast-path.ts: withFastReplyConfig",
12311228
"src/auto-reply/reply/get-reply-run.ts: buildExecOverridePromptHint",
12321229
"src/auto-reply/reply/get-reply-run.ts: resolvePromptSessionContextForSystemEvent",
12331230
"src/auto-reply/reply/get-reply-run.ts: resolvePromptSilentReplyConversationType",
1234-
"src/auto-reply/reply/history.ts: appendHistoryEntry",
12351231
"src/auto-reply/reply/inbound-dedupe.ts: buildInboundDedupeKey",
12361232
"src/auto-reply/reply/progress-narrator.ts: createProgressNarrator",
12371233
"src/auto-reply/reply/prompt-prelude.ts: buildReplyPromptBodies",
12381234
"src/auto-reply/reply/queue/cleanup.ts: testing",
12391235
"src/auto-reply/reply/queue/drain.ts: resolveFollowupAuthorizationKey",
12401236
"src/auto-reply/reply/queue/enqueue.ts: resetRecentQueuedMessageIdDedupe",
1241-
"src/auto-reply/reply/reply-payloads-dedupe.ts: getMatchingMessagingToolReplyTargets",
12421237
"src/auto-reply/reply/reply-run-registry.ts: __testing",
12431238
"src/auto-reply/reply/reply-run-registry.ts: testing",
1244-
"src/auto-reply/reply/reply-threading.ts: createReplyToModeFilter",
1245-
"src/auto-reply/reply/reply-threading.ts: resolveConfiguredReplyToMode",
1246-
"src/auto-reply/reply/reply-threading.ts: resolveReplyToModeWithThreading",
12471239
"src/auto-reply/reply/reply-usage-state.ts: clearReplyUsageStateForTest",
1248-
"src/auto-reply/reply/session-reset-prompt.ts: buildBareSessionResetPrompt",
12491240
"src/auto-reply/reply/stage-sandbox-media.ts: appendScpStderrTail",
12501241
"src/auto-reply/reply/stage-sandbox-media.ts: SCP_STDERR_TAIL_CHARS",
12511242
"src/auto-reply/reply/stage-sandbox-media.ts: testing",

‎src/agents/embedded-agent-subscribe.tools.reconcile-thread.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { afterEach, describe, expect, it } from "vitest";
2-
import { getMatchingMessagingToolReplyTargets } from "../auto-reply/reply/reply-payloads-dedupe.js";
2+
import { resolveMessagingToolPayloadDedupe } from "../auto-reply/reply/reply-payloads-dedupe.js";
33
import { setActivePluginRegistry } from "../plugins/runtime.js";
44
import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js";
55
import {
@@ -75,13 +75,13 @@ describe("extractMessagingToolSendResult thread evidence", () => {
7575
expect(confirmed.threadImplicit).toBe(true);
7676
expect(confirmed.threadId).toBe("root-1");
7777

78-
const matches = getMatchingMessagingToolReplyTargets({
78+
const decision = resolveMessagingToolPayloadDedupe({
7979
messageProvider: PARTIAL_RESULT_PROVIDER,
8080
originatingTo: "channel:abc",
8181
originatingThreadId: "root-1",
8282
messagingToolSentTargets: [confirmed],
8383
});
84-
expect(matches).toHaveLength(1);
84+
expect(decision.matchingRoute).toBe(true);
8585
});
8686

8787
it("lets an explicit provider-reported thread override pending implicit evidence", () => {

‎src/auto-reply/reply/block-reply-pipeline.test.ts‎

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22
import { MAX_TIMER_TIMEOUT_MS } from "@openclaw/normalization-core/number-coercion";
33
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
44
import { getReplyPayloadMetadata, setReplyPayloadMetadata } from "../reply-payload.js";
5-
import {
6-
createBlockReplyContentKey,
7-
createBlockReplyPayloadKey,
8-
createBlockReplyPipeline,
9-
} from "./block-reply-pipeline.js";
5+
import { createBlockReplyContentKey, createBlockReplyPipeline } from "./block-reply-pipeline.js";
106

117
const waitForAbort = (signal: AbortSignal | undefined): Promise<void> =>
128
new Promise((resolve) => {
@@ -25,48 +21,6 @@ afterEach(() => {
2521
vi.useRealTimers();
2622
});
2723

28-
describe("createBlockReplyPayloadKey", () => {
29-
it("produces different keys for payloads differing only by replyToId", () => {
30-
const a = createBlockReplyPayloadKey({ text: "hello world", replyToId: "post-1" });
31-
const b = createBlockReplyPayloadKey({ text: "hello world", replyToId: "post-2" });
32-
const c = createBlockReplyPayloadKey({ text: "hello world" });
33-
expect(a).not.toBe(b);
34-
expect(a).not.toBe(c);
35-
});
36-
37-
it("produces different keys for payloads with different text", () => {
38-
const a = createBlockReplyPayloadKey({ text: "hello" });
39-
const b = createBlockReplyPayloadKey({ text: "world" });
40-
expect(a).not.toBe(b);
41-
});
42-
43-
it("produces different keys for payloads with different media", () => {
44-
const a = createBlockReplyPayloadKey({ text: "hello", mediaUrl: "file:///a.png" });
45-
const b = createBlockReplyPayloadKey({ text: "hello", mediaUrl: "file:///b.png" });
46-
expect(a).not.toBe(b);
47-
});
48-
49-
it("produces different keys for payloads with different presentation content", () => {
50-
const a = createBlockReplyPayloadKey({
51-
presentation: {
52-
blocks: [{ type: "buttons", buttons: [{ label: "Approve", value: "approve" }] }],
53-
},
54-
});
55-
const b = createBlockReplyPayloadKey({
56-
presentation: {
57-
blocks: [{ type: "buttons", buttons: [{ label: "Reject", value: "reject" }] }],
58-
},
59-
});
60-
expect(a).not.toBe(b);
61-
});
62-
63-
it("trims whitespace from text for key comparison", () => {
64-
const a = createBlockReplyPayloadKey({ text: " hello " });
65-
const b = createBlockReplyPayloadKey({ text: "hello" });
66-
expect(a).toBe(b);
67-
});
68-
});
69-
7024
describe("createBlockReplyContentKey", () => {
7125
it("produces the same key for payloads differing only by replyToId", () => {
7226
const a = createBlockReplyContentKey({ text: "hello world", replyToId: "post-1" });

‎src/auto-reply/reply/block-reply-pipeline.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function createAudioAsVoiceBuffer(params: {
4949
}
5050

5151
/** Creates a stable duplicate key for a complete outbound payload. */
52-
export function createBlockReplyPayloadKey(payload: ReplyPayload): string {
52+
function createBlockReplyPayloadKey(payload: ReplyPayload): string {
5353
const reply = resolveSendableOutboundReplyParts(payload);
5454
return JSON.stringify({
5555
statusNotice: isReplyPayloadStatusNotice(payload),

‎src/auto-reply/reply/completion-delivery-policy.test.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@
22
import { describe, expect, it } from "vitest";
33
import {
44
completionRequiresMessageToolDelivery,
5-
resolveCompletionChatType,
65
shouldRouteCompletionThroughRequesterSession,
76
} from "./completion-delivery-policy.js";
87

8+
const chatTypeProbeConfig = {
9+
messages: {
10+
visibleReplies: "message_tool",
11+
groupChat: { visibleReplies: "automatic" },
12+
},
13+
} as const;
14+
915
describe("completion delivery policy", () => {
1016
it.each([
1117
{
@@ -33,17 +39,23 @@ describe("completion delivery policy", () => {
3339
requesterSessionKey: "agent:main:whatsapp:[email protected]",
3440
expected: "group",
3541
},
36-
])("infers $name", ({ requesterSessionKey, expected }) => {
37-
expect(resolveCompletionChatType({ requesterSessionKey })).toBe(expected);
42+
])("applies the inferred $expected policy for $name", ({ requesterSessionKey, expected }) => {
43+
expect(
44+
completionRequiresMessageToolDelivery({
45+
cfg: chatTypeProbeConfig,
46+
requesterSessionKey,
47+
}),
48+
).toBe(expected === "direct");
3849
});
3950

4051
it("prefers explicit session chat type over key inference", () => {
4152
expect(
42-
resolveCompletionChatType({
53+
completionRequiresMessageToolDelivery({
54+
cfg: chatTypeProbeConfig,
4355
requesterSessionKey: "agent:main:slack:channel:C123",
4456
requesterEntry: { chatType: "direct" },
4557
}),
46-
).toBe("direct");
58+
).toBe(true);
4759
});
4860

4961
it.each([
@@ -55,11 +67,12 @@ describe("completion delivery policy", () => {
5567
{ to: "user:U123", expected: "direct" },
5668
] as const)("falls back to origin target prefix $to", ({ to, expected }) => {
5769
expect(
58-
resolveCompletionChatType({
70+
completionRequiresMessageToolDelivery({
71+
cfg: chatTypeProbeConfig,
5972
requesterSessionKey: "agent:main:opaque:unknown-target",
6073
directOrigin: { channel: "test", to },
6174
}),
62-
).toBe(expected);
75+
).toBe(expected === "direct");
6376
});
6477

6578
it("allows automatic delivery for group and channel completions by default", () => {

‎src/auto-reply/reply/completion-delivery-policy.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type CompletionDeliverySessionEntry = {
1212
origin?: { chatType?: string | null } | null;
1313
};
1414

15-
export function resolveCompletionChatType(params: {
15+
function resolveCompletionChatType(params: {
1616
requesterSessionKey?: string | null;
1717
targetRequesterSessionKey?: string | null;
1818
requesterEntry?: CompletionDeliverySessionEntry;

‎src/auto-reply/reply/dispatcher-registry.ts‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,3 @@ export function getTotalPendingReplies(): number {
4545
}
4646
return total;
4747
}
48-
49-
/**
50-
* Clear all registered dispatchers (for testing).
51-
* WARNING: Only use this in test cleanup!
52-
*/
53-
export function clearAllDispatchers(): void {
54-
if (!process.env.VITEST && process.env.NODE_ENV !== "test") {
55-
throw new Error("clearAllDispatchers() is only available in test environments");
56-
}
57-
activeDispatchers.clear();
58-
}

‎src/auto-reply/reply/history.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function buildHistoryContext(params: {
4848
}
4949

5050
/** Appends one history entry, enforces per-session limit, and refreshes LRU key order. */
51-
export function appendHistoryEntry<T extends HistoryEntry>(params: {
51+
function appendHistoryEntry<T extends HistoryEntry>(params: {
5252
historyMap: Map<string, T[]>;
5353
historyKey: string;
5454
entry: T;

‎src/auto-reply/reply/reply-flow.test.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
createReplyDispatcher,
88
waitForReplyDispatcherIdle,
99
} from "./reply-dispatcher.js";
10-
import { createReplyToModeFilter } from "./reply-threading.js";
10+
import { createReplyToModeFilterForChannel } from "./reply-threading.js";
1111

1212
type DeliverPayload = Parameters<Parameters<typeof createReplyDispatcher>[0]["deliver"]>[0];
1313
type DeliverMock = { mock: { calls: unknown[][] } };
@@ -539,25 +539,25 @@ describe("waitForReplyDispatcherIdle", () => {
539539
});
540540
});
541541

542-
describe("createReplyToModeFilter", () => {
542+
describe("createReplyToModeFilterForChannel", () => {
543543
it("handles off/all mode behavior for replyToId", () => {
544544
const cases: Array<{
545-
filter: ReturnType<typeof createReplyToModeFilter>;
545+
filter: ReturnType<typeof createReplyToModeFilterForChannel>;
546546
input: { text: string; replyToId?: string; replyToTag?: boolean };
547547
expectedReplyToId?: string;
548548
}> = [
549549
{
550-
filter: createReplyToModeFilter("off"),
550+
filter: createReplyToModeFilterForChannel("off"),
551551
input: { text: "hi", replyToId: "1" },
552552
expectedReplyToId: undefined,
553553
},
554554
{
555-
filter: createReplyToModeFilter("off", { allowExplicitReplyTagsWhenOff: true }),
555+
filter: createReplyToModeFilterForChannel("off", "slack"),
556556
input: { text: "hi", replyToId: "1", replyToTag: true },
557557
expectedReplyToId: "1",
558558
},
559559
{
560-
filter: createReplyToModeFilter("all"),
560+
filter: createReplyToModeFilterForChannel("all"),
561561
input: { text: "hi", replyToId: "1" },
562562
expectedReplyToId: "1",
563563
},
@@ -568,7 +568,7 @@ describe("createReplyToModeFilter", () => {
568568
});
569569

570570
it("keeps only the first replyToId when mode is first", () => {
571-
const filter = createReplyToModeFilter("first");
571+
const filter = createReplyToModeFilterForChannel("first");
572572
expect(filter({ text: "hi", replyToId: "1" }).replyToId).toBe("1");
573573
expect(filter({ text: "next", replyToId: "1" }).replyToId).toBeUndefined();
574574
});

‎src/auto-reply/reply/reply-payloads-dedupe.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ export function shouldDedupeMessagingToolRepliesForRoute(params: {
269269
}
270270

271271
/** Finds message-tool sends that target the same channel/account/thread as the source reply. */
272-
export function getMatchingMessagingToolReplyTargets(params: {
272+
function getMatchingMessagingToolReplyTargets(params: {
273273
config?: OpenClawConfig;
274274
messageProvider?: string;
275275
messagingToolSentTargets?: MessagingToolSend[];

0 commit comments

Comments
 (0)