11import fs from "node:fs" ;
2- import {
3- buildDeliveryFromLegacyPayload ,
4- hasLegacyDeliveryHints ,
5- stripLegacyDeliveryFields ,
6- } from "../legacy-delivery.js" ;
2+ import { normalizeLegacyDeliveryInput } from "../legacy-delivery.js" ;
73import { parseAbsoluteTimeMs } from "../parse.js" ;
84import { migrateLegacyCronPayload } from "../payload-migration.js" ;
95import { coerceFiniteScheduleNumber } from "../schedule.js" ;
@@ -14,69 +10,6 @@ import { recomputeNextRuns } from "./jobs.js";
1410import { inferLegacyName , normalizeOptionalText } from "./normalize.js" ;
1511import type { CronServiceState } from "./state.js" ;
1612
17- function buildDeliveryPatchFromLegacyPayload ( payload : Record < string , unknown > ) {
18- const deliver = payload . deliver ;
19- const channelRaw =
20- typeof payload . channel === "string" ? payload . channel . trim ( ) . toLowerCase ( ) : "" ;
21- const toRaw = typeof payload . to === "string" ? payload . to . trim ( ) : "" ;
22- const next : Record < string , unknown > = { } ;
23- let hasPatch = false ;
24-
25- if ( deliver === false ) {
26- next . mode = "none" ;
27- hasPatch = true ;
28- } else if ( deliver === true || toRaw ) {
29- next . mode = "announce" ;
30- hasPatch = true ;
31- }
32- if ( channelRaw ) {
33- next . channel = channelRaw ;
34- hasPatch = true ;
35- }
36- if ( toRaw ) {
37- next . to = toRaw ;
38- hasPatch = true ;
39- }
40- if ( typeof payload . bestEffortDeliver === "boolean" ) {
41- next . bestEffort = payload . bestEffortDeliver ;
42- hasPatch = true ;
43- }
44-
45- return hasPatch ? next : null ;
46- }
47-
48- function mergeLegacyDeliveryInto (
49- delivery : Record < string , unknown > ,
50- payload : Record < string , unknown > ,
51- ) {
52- const patch = buildDeliveryPatchFromLegacyPayload ( payload ) ;
53- if ( ! patch ) {
54- return { delivery, mutated : false } ;
55- }
56-
57- const next = { ...delivery } ;
58- let mutated = false ;
59-
60- if ( "mode" in patch && patch . mode !== next . mode ) {
61- next . mode = patch . mode ;
62- mutated = true ;
63- }
64- if ( "channel" in patch && patch . channel !== next . channel ) {
65- next . channel = patch . channel ;
66- mutated = true ;
67- }
68- if ( "to" in patch && patch . to !== next . to ) {
69- next . to = patch . to ;
70- mutated = true ;
71- }
72- if ( "bestEffort" in patch && patch . bestEffort !== next . bestEffort ) {
73- next . bestEffort = patch . bestEffort ;
74- mutated = true ;
75- }
76-
77- return { delivery : next , mutated } ;
78- }
79-
8013function normalizePayloadKind ( payload : Record < string , unknown > ) {
8114 const raw = typeof payload . kind === "string" ? payload . kind . trim ( ) . toLowerCase ( ) : "" ;
8215 if ( raw === "agentturn" ) {
@@ -512,30 +445,25 @@ export async function ensureLoaded(
512445 const isIsolatedAgentTurn =
513446 sessionTarget === "isolated" || ( sessionTarget === "" && payloadKind === "agentTurn" ) ;
514447 const hasDelivery = delivery && typeof delivery === "object" && ! Array . isArray ( delivery ) ;
515- const hasLegacyDelivery = payloadRecord ? hasLegacyDeliveryHints ( payloadRecord ) : false ;
448+ const normalizedLegacy = normalizeLegacyDeliveryInput ( {
449+ delivery : hasDelivery ? ( delivery as Record < string , unknown > ) : null ,
450+ payload : payloadRecord ,
451+ } ) ;
516452
517453 if ( isIsolatedAgentTurn && payloadKind === "agentTurn" ) {
518- if ( ! hasDelivery ) {
519- raw . delivery =
520- payloadRecord && hasLegacyDelivery
521- ? buildDeliveryFromLegacyPayload ( payloadRecord )
522- : { mode : "announce" } ;
454+ if ( ! hasDelivery && normalizedLegacy . delivery ) {
455+ raw . delivery = normalizedLegacy . delivery ;
523456 mutated = true ;
524- }
525- if ( payloadRecord && hasLegacyDelivery ) {
526- if ( hasDelivery ) {
527- const merged = mergeLegacyDeliveryInto (
528- delivery as Record < string , unknown > ,
529- payloadRecord ,
530- ) ;
531- if ( merged . mutated ) {
532- raw . delivery = merged . delivery ;
533- mutated = true ;
534- }
535- }
536- stripLegacyDeliveryFields ( payloadRecord ) ;
457+ } else if ( ! hasDelivery ) {
458+ raw . delivery = { mode : "announce" } ;
459+ mutated = true ;
460+ } else if ( normalizedLegacy . mutated && normalizedLegacy . delivery ) {
461+ raw . delivery = normalizedLegacy . delivery ;
537462 mutated = true ;
538463 }
464+ } else if ( normalizedLegacy . mutated && normalizedLegacy . delivery ) {
465+ raw . delivery = normalizedLegacy . delivery ;
466+ mutated = true ;
539467 }
540468 }
541469 state . store = { version : 1 , jobs : jobs as unknown as CronJob [ ] } ;
0 commit comments