mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-13 22:06:38 +00:00
fix(cron): reject sessionTarget "main" for non-default agents at creation time (openclaw#30217) thanks @liaosvcaf
Verified: - pnpm install --frozen-lockfile - pnpm build - pnpm check - pnpm test:macmini Co-authored-by: liaosvcaf <51533973+liaosvcaf@users.noreply.github.com> Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import crypto from "node:crypto";
|
||||
import { normalizeAgentId } from "../../routing/session-key.js";
|
||||
import { parseAbsoluteTimeMs } from "../parse.js";
|
||||
import { computeNextRunAtMs } from "../schedule.js";
|
||||
import {
|
||||
@@ -91,6 +92,25 @@ export function assertSupportedJobSpec(job: Pick<CronJob, "sessionTarget" | "pay
|
||||
}
|
||||
}
|
||||
|
||||
function assertMainSessionAgentId(
|
||||
job: Pick<CronJob, "sessionTarget" | "agentId">,
|
||||
defaultAgentId: string | undefined,
|
||||
) {
|
||||
if (job.sessionTarget !== "main") {
|
||||
return;
|
||||
}
|
||||
if (!job.agentId) {
|
||||
return;
|
||||
}
|
||||
const normalized = normalizeAgentId(job.agentId);
|
||||
const normalizedDefault = normalizeAgentId(defaultAgentId);
|
||||
if (normalized !== normalizedDefault) {
|
||||
throw new Error(
|
||||
`cron: sessionTarget "main" is only valid for the default agent. Use sessionTarget "isolated" with payload.kind "agentTurn" for non-default agents (agentId: ${job.agentId})`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const TELEGRAM_TME_URL_REGEX = /^https?:\/\/t\.me\/|t\.me\//i;
|
||||
const TELEGRAM_SLASH_TOPIC_REGEX = /^-?\d+\/\d+$/;
|
||||
|
||||
@@ -426,12 +446,17 @@ export function createJob(state: CronServiceState, input: CronJobCreate): CronJo
|
||||
},
|
||||
};
|
||||
assertSupportedJobSpec(job);
|
||||
assertMainSessionAgentId(job, state.deps.defaultAgentId);
|
||||
assertDeliverySupport(job);
|
||||
job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
|
||||
return job;
|
||||
}
|
||||
|
||||
export function applyJobPatch(job: CronJob, patch: CronJobPatch) {
|
||||
export function applyJobPatch(
|
||||
job: CronJob,
|
||||
patch: CronJobPatch,
|
||||
opts?: { defaultAgentId?: string },
|
||||
) {
|
||||
if ("name" in patch) {
|
||||
job.name = normalizeRequiredName(patch.name);
|
||||
}
|
||||
@@ -501,6 +526,7 @@ export function applyJobPatch(job: CronJob, patch: CronJobPatch) {
|
||||
job.sessionKey = normalizeOptionalSessionKey((patch as { sessionKey?: unknown }).sessionKey);
|
||||
}
|
||||
assertSupportedJobSpec(job);
|
||||
assertMainSessionAgentId(job, opts?.defaultAgentId);
|
||||
assertDeliverySupport(job);
|
||||
}
|
||||
|
||||
|
||||
@@ -270,7 +270,7 @@ export async function update(state: CronServiceState, id: string, patch: CronJob
|
||||
await ensureLoaded(state, { skipRecompute: true });
|
||||
const job = findJobOrThrow(state, id);
|
||||
const now = state.deps.nowMs();
|
||||
applyJobPatch(job, patch);
|
||||
applyJobPatch(job, patch, { defaultAgentId: state.deps.defaultAgentId });
|
||||
if (job.schedule.kind === "every") {
|
||||
const anchor = job.schedule.anchorMs;
|
||||
if (typeof anchor !== "number" || !Number.isFinite(anchor)) {
|
||||
|
||||
Reference in New Issue
Block a user