






















@@ -23,7 +23,10 @@ import {
2323resolveCronRunLogPruneOptions,
2424} from "../cron/run-log.js";
2525import { CronService } from "../cron/service.js";
26-import { assertSafeCronSessionTargetId } from "../cron/session-target.js";
26+import {
27+resolveCronDeliverySessionKey,
28+resolveCronSessionTargetSessionKey,
29+} from "../cron/session-target.js";
2730import { resolveCronStorePath } from "../cron/store.js";
2831import { normalizeHttpWebhookUrl } from "../cron/webhook-url.js";
2932import { formatErrorMessage } from "../infra/errors.js";
@@ -32,6 +35,7 @@ import { requestHeartbeatNow } from "../infra/heartbeat-wake.js";
3235import { fetchWithSsrFGuard } from "../infra/net/fetch-guard.js";
3336import { SsrFBlockedError } from "../infra/net/ssrf.js";
3437import { deliverOutboundPayloads } from "../infra/outbound/deliver.js";
38+import { buildOutboundSessionContext } from "../infra/outbound/session-context.js";
3539import { enqueueSystemEvent } from "../infra/system-events.js";
3640import { getChildLogger } from "../logging.js";
3741import { normalizeAgentId, toAgentStoreSessionKey } from "../routing/session-key.js";
@@ -332,10 +336,7 @@ export function buildGatewayCronService(params: {
332336},
333337runIsolatedAgentJob: async ({ job, message, abortSignal }) => {
334338const { agentId, cfg: runtimeConfig } = resolveCronAgent(job.agentId);
335-let sessionKey = `cron:${job.id}`;
336-if (job.sessionTarget.startsWith("session:")) {
337-sessionKey = assertSafeCronSessionTargetId(job.sessionTarget.slice(8));
338-}
339+const sessionKey = resolveCronSessionTargetSessionKey(job.sessionTarget) ?? `cron:${job.id}`;
339340try {
340341return await runCronIsolatedAgentTurn({
341342cfg: runtimeConfig,
@@ -395,14 +396,21 @@ export function buildGatewayCronService(params: {
395396return;
396397}
397398399+const deliverySessionKey = resolveCronDeliverySessionKey(job);
398400const target = await resolveDeliveryTarget(runtimeConfig, agentId, {
399401 channel,
400402 to,
401403 accountId,
404+sessionKey: deliverySessionKey,
402405});
403406if (!target.ok) {
404407throw target.error;
405408}
409+const session = buildOutboundSessionContext({
410+cfg: runtimeConfig,
411+ agentId,
412+sessionKey: deliverySessionKey ?? `cron:${job.id}:failure`,
413+});
406414await deliverOutboundPayloads({
407415cfg: runtimeConfig,
408416channel: target.channel,
@@ -411,6 +419,7 @@ export function buildGatewayCronService(params: {
411419threadId: target.threadId,
412420payloads: [{ text }],
413421deps: createOutboundSendDeps(params.deps),
422+ session,
414423});
415424},
416425log: getChildLogger({ module: "cron", storePath }),
@@ -470,6 +479,7 @@ export function buildGatewayCronService(params: {
470479if (!isBestEffort) {
471480const failureMessage = `Cron job "${job.name}" failed: ${evt.error ?? "unknown error"}`;
472481const failureDest = resolveFailureDestination(job, params.cfg.cron?.failureDestination);
482+const deliverySessionKey = resolveCronDeliverySessionKey(job);
473483474484if (failureDest) {
475485// Explicit failureDestination configured — use it
@@ -518,7 +528,7 @@ export function buildGatewayCronService(params: {
518528channel: failureDest.channel,
519529to: failureDest.to,
520530accountId: failureDest.accountId,
521-sessionKey: job.sessionKey,
531+sessionKey: deliverySessionKey,
522532},
523533`⚠️ ${failureMessage}`,
524534);
@@ -537,7 +547,7 @@ export function buildGatewayCronService(params: {
537547channel: primaryPlan.channel,
538548to: primaryPlan.to,
539549accountId: primaryPlan.accountId,
540-sessionKey: job.sessionKey,
550+sessionKey: deliverySessionKey,
541551},
542552`⚠️ ${failureMessage}`,
543553);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。