Skip to content

Commit 52a7845

Browse files
committed
fix(clownfish): address review for ghcrawl-165998-agentic-merge (1)
1 parent 83dc3b3 commit 52a7845

3 files changed

Lines changed: 178 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Docs: https://docs.openclaw.ai
3030
- CLI/plugins: keep bundled plugin installs out of `plugins.load.paths` while preserving install records, so install/inspect/doctor loops no longer warn about the current bundled plugin directory. Thanks @vincentkoc.
3131
- Cron tool: infer the creating session's agentId for `cron.add` jobs when `agentId` is omitted or passed as undefined, keeping scheduled agentTurn jobs routed to the session agent; #40571 identified the guard bug and supplied the focused regression coverage. Thanks @ChanningYul.
3232
- Cron/Telegram: add `--thread-id` to `openclaw cron add` and `openclaw cron edit`, preserving Telegram forum topic delivery targets across scheduled announcements. Carries forward #51581, #60373, and #60890. Thanks @ChunHao-dev.
33+
- Cron/Telegram: preserve session-derived Telegram topic thread IDs when isolated cron delivery explicitly targets the parent chat, keeping bare chat targets in the active forum topic without leaking stale topics to other chats. Carries forward #64708. Thanks @addelh.
3334
- Memory/compaction: keep pre-compaction memory-flush prompts runtime-only so session transcripts and `chat.history` no longer expose them as normal user turns. Fixes #54408 and #58956; refs #43567. Thanks @markgong and @guoyuhang9.
3435
- Control UI/WebChat: keep large attachment payloads out of Lit state and optimistic chat messages, using object URL previews plus send-time payload serialization so PDF/image uploads no longer trigger `RangeError: Maximum call stack size exceeded`. Fixes #73360; refs #54378 and #63432. Thanks @hejunhui-73, @Ansub, and @christianhernandez3-afk.
3536
- Agents/Anthropic: cancel stalled Anthropic Messages SSE body reads when abort signals fire, so active-memory timeouts release transport resources instead of leaving hidden recall runs parked on `reader.read()`. Refs #72965 and #73120. Thanks @wdeveloper16.

‎src/cron/isolated-agent/delivery-target.test.ts‎

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest";
22
import type { ChannelOutboundAdapter } from "../../channels/plugins/types.js";
33
import type { OpenClawConfig } from "../../config/config.js";
4-
import { forumMessagingForTest } from "../../infra/outbound/targets.test-helpers.js";
4+
import {
5+
forumMessagingForTest,
6+
telegramMessagingForTest,
7+
} from "../../infra/outbound/targets.test-helpers.js";
58
import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../plugins/runtime.js";
69
import { createOutboundTestPlugin, createTestRegistry } from "../../test-utils/channel-plugins.js";
710

@@ -86,8 +89,20 @@ function createAllowlistAwareStubOutbound(label: string): ChannelOutboundAdapter
8689
};
8790
}
8891

92+
const normalizeTelegramTargetForDeliveryTest = vi.fn((raw: string): string | undefined => {
93+
const target = telegramMessagingForTest.parseExplicitTarget?.({ raw });
94+
if (!target?.to) {
95+
return undefined;
96+
}
97+
const normalizedTo = target.to.toLowerCase();
98+
return target.threadId == null
99+
? `telegram:${normalizedTo}`
100+
: `telegram:${normalizedTo}:topic:${target.threadId}`;
101+
});
102+
89103
beforeEach(() => {
90104
resetPluginRuntimeStateForTest();
105+
normalizeTelegramTargetForDeliveryTest.mockClear();
91106
vi.mocked(resolveOutboundTarget).mockReset();
92107
setActivePluginRegistry(
93108
createTestRegistry([
@@ -100,6 +115,18 @@ beforeEach(() => {
100115
}),
101116
source: "test",
102117
},
118+
{
119+
pluginId: "telegram",
120+
plugin: createOutboundTestPlugin({
121+
id: "telegram",
122+
outbound: createStubOutbound("Telegram"),
123+
messaging: {
124+
...telegramMessagingForTest,
125+
normalizeTarget: normalizeTelegramTargetForDeliveryTest,
126+
},
127+
}),
128+
source: "test",
129+
},
103130
{
104131
pluginId: "alpha",
105132
plugin: {
@@ -421,6 +448,38 @@ describe("resolveDeliveryTarget", () => {
421448
);
422449
});
423450

451+
it("returns an unresolved target when loaded target resolution throws", async () => {
452+
setMainSessionEntry(undefined);
453+
setActivePluginRegistry(
454+
createTestRegistry([
455+
{
456+
pluginId: "alpha",
457+
plugin: createOutboundTestPlugin({
458+
id: "alpha",
459+
outbound: {
460+
deliveryMode: "gateway",
461+
resolveTarget: () => {
462+
throw new Error("target normalizer exploded");
463+
},
464+
},
465+
}),
466+
source: "test",
467+
},
468+
]),
469+
);
470+
471+
const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, {
472+
channel: "alpha",
473+
to: "room:default",
474+
});
475+
476+
expect(result.ok).toBe(false);
477+
if (result.ok) {
478+
throw new Error("expected invalid delivery target");
479+
}
480+
expect(result.error.message).toContain("Invalid delivery target: target normalizer exploded");
481+
});
482+
424483
it("selects correct binding when multiple agents have bindings", async () => {
425484
setMainSessionEntry(undefined);
426485

@@ -483,6 +542,66 @@ describe("resolveDeliveryTarget", () => {
483542
expect(result.threadId).toBe("thread-2");
484543
});
485544

545+
it("keeps a session Telegram topic threadId when a bare explicit target matches the topic route", async () => {
546+
setLastSessionEntry({
547+
sessionId: "sess-telegram-topic",
548+
lastChannel: "telegram",
549+
lastTo: "-100200300:topic:77",
550+
lastThreadId: "77",
551+
});
552+
normalizeTelegramTargetForDeliveryTest.mockClear();
553+
554+
const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, {
555+
channel: "telegram",
556+
to: "-100200300",
557+
});
558+
559+
expect(result.ok).toBe(true);
560+
expect(result.to).toBe("-100200300");
561+
expect(result.threadId).toBe(77);
562+
expect(normalizeTelegramTargetForDeliveryTest).toHaveBeenCalledWith("-100200300");
563+
expect(normalizeTelegramTargetForDeliveryTest).toHaveBeenCalledWith("-100200300:topic:77");
564+
});
565+
566+
it("drops carried threadId instead of throwing when target normalization fails", async () => {
567+
setLastSessionEntry({
568+
sessionId: "sess-telegram-topic-invalid",
569+
lastChannel: "telegram",
570+
lastTo: "-100200300:topic:77",
571+
lastThreadId: "77",
572+
});
573+
normalizeTelegramTargetForDeliveryTest.mockImplementationOnce(() => {
574+
throw new Error("target normalizer exploded");
575+
});
576+
577+
const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, {
578+
channel: "telegram",
579+
to: "-100200300",
580+
});
581+
582+
expect(result.ok).toBe(true);
583+
expect(result.to).toBe("-100200300");
584+
expect(result.threadId).toBeUndefined();
585+
});
586+
587+
it("drops a session Telegram topic threadId when a bare explicit target names a different chat", async () => {
588+
setLastSessionEntry({
589+
sessionId: "sess-telegram-topic-stale",
590+
lastChannel: "telegram",
591+
lastTo: "-100200300:topic:77",
592+
lastThreadId: "77",
593+
});
594+
595+
const result = await resolveDeliveryTarget(makeCfg({ bindings: [] }), AGENT_ID, {
596+
channel: "telegram",
597+
to: "-100999999",
598+
});
599+
600+
expect(result.ok).toBe(true);
601+
expect(result.to).toBe("-100999999");
602+
expect(result.threadId).toBeUndefined();
603+
});
604+
486605
it("uses single configured channel when neither explicit nor session channel exists", async () => {
487606
setMainSessionEntry(undefined);
488607

‎src/cron/isolated-agent/delivery-target.ts‎

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { parseExplicitTargetForLoadedChannel } from "../../channels/plugins/target-parsing-loaded.js";
12
import type { ChannelId } from "../../channels/plugins/types.public.js";
23
import { resolveAgentMainSessionKey } from "../../config/sessions/main-session.js";
34
import { resolveStorePath } from "../../config/sessions/paths.js";
45
import { loadSessionStore } from "../../config/sessions/store-load.js";
56
import type { OpenClawConfig } from "../../config/types.openclaw.js";
67
import { formatErrorMessage } from "../../infra/errors.js";
78
import { maybeResolveIdLikeTarget } from "../../infra/outbound/target-id-resolution.js";
9+
import { normalizeTargetForProvider } from "../../infra/outbound/target-normalization.js";
810
import { tryResolveLoadedOutboundTarget } from "../../infra/outbound/targets-loaded.js";
911
import { resolveSessionDeliveryTarget } from "../../infra/outbound/targets-session.js";
1012
import type { OutboundChannel } from "../../infra/outbound/targets.js";
@@ -41,12 +43,56 @@ async function loadTargetsRuntime() {
4143
async function resolveOutboundTargetWithRuntime(
4244
params: Parameters<typeof tryResolveLoadedOutboundTarget>[0],
4345
) {
44-
const loaded = tryResolveLoadedOutboundTarget(params);
45-
if (loaded) {
46-
return loaded;
46+
try {
47+
const loaded = tryResolveLoadedOutboundTarget(params);
48+
if (loaded) {
49+
return loaded;
50+
}
51+
const { resolveOutboundTarget } = await loadTargetsRuntime();
52+
return resolveOutboundTarget(params);
53+
} catch (err) {
54+
return {
55+
ok: false as const,
56+
error: new Error(`Invalid delivery target: ${formatErrorMessage(err)}`),
57+
};
4758
}
48-
const { resolveOutboundTarget } = await loadTargetsRuntime();
49-
return resolveOutboundTarget(params);
59+
}
60+
61+
function normalizeTargetForThreadCarry(
62+
channel: Exclude<OutboundChannel, "none"> | undefined,
63+
to: string | undefined,
64+
): string | undefined {
65+
if (!channel || !to) {
66+
return undefined;
67+
}
68+
try {
69+
const normalized = normalizeTargetForProvider(channel, to);
70+
const comparable = normalized ?? to.trim();
71+
if (!comparable) {
72+
return undefined;
73+
}
74+
const parsed = parseExplicitTargetForLoadedChannel(channel, comparable);
75+
const base = parsed?.to ?? comparable;
76+
return normalizeTargetForProvider(channel, base) ?? base;
77+
} catch {
78+
return undefined;
79+
}
80+
}
81+
82+
function deliveryTargetsShareThreadRoute(params: {
83+
channel: Exclude<OutboundChannel, "none"> | undefined;
84+
to: string | undefined;
85+
lastTo: string | undefined;
86+
}): boolean {
87+
if (!params.to || !params.lastTo) {
88+
return false;
89+
}
90+
if (params.to === params.lastTo) {
91+
return true;
92+
}
93+
const normalizedTo = normalizeTargetForThreadCarry(params.channel, params.to);
94+
const normalizedLastTo = normalizeTargetForThreadCarry(params.channel, params.lastTo);
95+
return Boolean(normalizedTo && normalizedLastTo && normalizedTo === normalizedLastTo);
5096
}
5197

5298
let channelSelectionRuntimePromise:
@@ -160,7 +206,12 @@ export async function resolveDeliveryTarget(
160206
// stale thread IDs from leaking to a different chat.
161207
let threadId =
162208
resolved.threadId &&
163-
(resolved.threadIdExplicit || (resolved.to && resolved.to === resolved.lastTo))
209+
(resolved.threadIdExplicit ||
210+
deliveryTargetsShareThreadRoute({
211+
channel,
212+
to: resolved.to,
213+
lastTo: resolved.lastTo,
214+
}))
164215
? resolved.threadId
165216
: undefined;
166217

0 commit comments

Comments
 (0)