Skip to content

Commit 336ca03

Browse files
fix(cron): honor configured delivery.channel instead of misrouting (#46899)
Co-authored-by: Cursor <[email protected]>
1 parent 66b91d7 commit 336ca03

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎src/cron/delivery-field-schemas.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** Parses user-provided cron delivery fields into narrow runtime values. */
2+
import { isRecord } from "@openclaw/normalization-core/record-coerce";
23
import { normalizeOptionalLowercaseString } from "@openclaw/normalization-core/string-coerce";
34
import { z, type ZodType } from "zod";
45

@@ -18,6 +19,19 @@ export const LowercaseNonEmptyStringFieldSchema = z.preprocess(
1819
z.string().min(1),
1920
);
2021

22+
// Persisted/legacy cron jobs may carry `delivery.channel` as a `{ id }` channel
23+
// reference instead of a bare id. Recover the id so the explicit channel survives
24+
// normalization; otherwise it is dropped and routing degrades to implicit "last",
25+
// misrouting delivery to another configured channel (e.g. Feishu instead of Telegram).
26+
const channelReferenceToId = (value: unknown): unknown =>
27+
isRecord(value) && typeof value.id === "string" ? value.id : value;
28+
29+
/** Accepts a channel id string or a `{ id }` channel reference from persisted cron jobs. */
30+
export const DeliveryChannelFieldSchema = z.preprocess(
31+
channelReferenceToId,
32+
LowercaseNonEmptyStringFieldSchema,
33+
);
34+
2135
/** Accepts non-empty string fields after trimming delivery input without changing case. */
2236
export const TrimmedNonEmptyStringFieldSchema = z.preprocess(
2337
trimStringPreprocess,
@@ -45,7 +59,7 @@ type ParsedDeliveryInput = {
4559
export function parseDeliveryInput(input: Record<string, unknown>): ParsedDeliveryInput {
4660
return {
4761
mode: parseOptionalField(DeliveryModeFieldSchema, input.mode),
48-
channel: parseOptionalField(LowercaseNonEmptyStringFieldSchema, input.channel),
62+
channel: parseOptionalField(DeliveryChannelFieldSchema, input.channel),
4963
to: parseOptionalField(TrimmedNonEmptyStringFieldSchema, input.to),
5064
threadId: parseOptionalField(DeliveryThreadIdFieldSchema, input.threadId),
5165
accountId: parseOptionalField(TrimmedNonEmptyStringFieldSchema, input.accountId),

‎src/cron/normalize.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
validateCronAddParams,
55
validateCronUpdateParams,
66
} from "../../packages/gateway-protocol/src/index.js";
7+
import { resolveCronDeliveryPlan } from "./delivery-plan.js";
78
import { normalizeCronJobCreate, normalizeCronJobPatch } from "./normalize.js";
89
import { DEFAULT_TOP_OF_HOUR_STAGGER_MS } from "./stagger.js";
10+
import type { CronJob } from "./types.js";
911

1012
function expectNormalizedAtSchedule(scheduleInput: Record<string, unknown>) {
1113
const normalized = normalizeCronJobCreate({
@@ -142,6 +144,24 @@ describe("normalizeCronJobCreate", () => {
142144
expectAnnounceDeliveryTarget(delivery, { channel: "telegram", to: "7200373102" });
143145
});
144146

147+
it("recovers an object-shaped delivery.channel so cron honors the explicit channel (#46899)", () => {
148+
const normalized = normalizeIsolatedAgentTurnCreateJob({
149+
name: "object channel ref",
150+
delivery: {
151+
mode: "announce",
152+
channel: { id: "telegram" },
153+
to: "7200373102",
154+
},
155+
});
156+
157+
const delivery = normalized.delivery as Record<string, unknown>;
158+
// A persisted `{ id }` channel reference must normalize to the canonical id.
159+
// Dropping it degrades routing to implicit "last", which misroutes delivery to
160+
// another configured channel (the #46899 Telegram-configured/Feishu-used regression).
161+
expectAnnounceDeliveryTarget(delivery, { channel: "telegram", to: "7200373102" });
162+
expect(resolveCronDeliveryPlan(normalized as unknown as CronJob).channel).toBe("telegram");
163+
});
164+
145165
it("coerces ISO schedule.at to normalized ISO (UTC)", () => {
146166
expectNormalizedAtSchedule({ kind: "at", at: "2026-01-12T18:00:00" });
147167
});

0 commit comments

Comments
 (0)