File size: 13,514 Bytes
f778c12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Shared schedule option resolver for cron create/edit commands.
import { expectDefined } from "@openclaw/normalization-core/expect";
import { normalizeOptionalString } from "@openclaw/normalization-core/string-coerce";
import type { CronSchedule } from "../../cron/types.js";
import { CronCliError } from "./cron-cli-error.js";
import {
  parseAt,
  parseCronStaggerMs,
  parseCronStreamCommandArgv,
  parsePositiveCronDurationMs,
} from "./shared.js";

type ScheduleOptionInput = {
  at?: unknown;
  cron?: unknown;
  every?: unknown;
  onExit?: unknown;
  onExitCwd?: unknown;
  streamCommand?: unknown;
  streamCwd?: unknown;
  streamMode?: unknown;
  streamMatch?: unknown;
  streamBatchMs?: unknown;
  streamMaxBatchBytes?: unknown;
  exact?: unknown;
  stagger?: unknown;
  tz?: unknown;
};

type PositionalScheduleInput = {
  positionalSchedule?: unknown;
};

type NormalizedScheduleOptions = {
  at: string;
  cronExpr: string;
  every: string;
  onExitCommand: string;
  onExitCwd: string | undefined;
  streamCommand: string[] | undefined;
  streamCwd: string | undefined;
  streamCwdSupplied: boolean;
  streamMode: "line" | "match";
  streamModeSupplied: boolean;
  streamMatch: string | undefined;
  streamMatchSupplied: boolean;
  streamBatchMs: number | undefined;
  streamMaxBatchBytes: number | undefined;
  requestedStaggerMs: number | undefined;
  tz: string | undefined;
};

/** Normalized schedule edit request, including patch-only updates for cron metadata. */
type CronEditScheduleRequest =
  | { kind: "direct"; schedule: CronSchedule }
  | { kind: "patch-existing-cron"; staggerMs: number | undefined; tz: string | undefined }
  | {
      kind: "patch-existing-stream";
      cwd: string | null | undefined;
      mode: "line" | "match" | undefined;
      match: string | null | undefined;
      batchMs: number | undefined;
      maxBatchBytes: number | undefined;
    }
  | { kind: "none" };

/** A single normalized creation selector resolves or throws during validation. */
function resolveCronCreateSchedule(options: ScheduleOptionInput): CronSchedule {
  const normalized = normalizeScheduleOptions(options);
  if (normalized.onExitCwd && !normalized.onExitCommand) {
    throw new CronCliError("--on-exit-cwd requires --on-exit.");
  }
  const chosen = countChosenSchedules(normalized);
  if (chosen !== 1) {
    throw new CronCliError(
      "Choose exactly one schedule: --at, --every, --cron, --on-exit, or --stream-command",
    );
  }
  return expectDefined(resolveDirectSchedule(normalized), "created cron schedule");
}

/** Resolve cron creation schedule from either a positional shorthand or explicit flags. */
export function resolveCronCreateScheduleFromArgs(
  options: ScheduleOptionInput & PositionalScheduleInput,
): CronSchedule {
  const positionalSchedule = normalizeOptionalString(options.positionalSchedule);
  if (!positionalSchedule) {
    return resolveCronCreateSchedule(options);
  }
  const normalized = normalizeScheduleOptions(options);
  if (countChosenSchedules(normalized) > 0) {
    throw new CronCliError(
      "Choose a positional schedule or one of --at, --every, --cron, --on-exit, or --stream-command.",
    );
  }
  const every = parseEverySchedule(positionalSchedule);
  return resolveCronCreateSchedule({
    ...options,
    at: every
      ? undefined
      : looksLikeCronExpression(positionalSchedule)
        ? undefined
        : positionalSchedule,
    cron: looksLikeCronExpression(positionalSchedule) ? positionalSchedule : undefined,
    every,
  });
}

/** Resolve a cron edit request, allowing at most one direct schedule replacement. */
export function resolveCronEditScheduleRequest(
  options: ScheduleOptionInput,
): CronEditScheduleRequest {
  const normalized = normalizeScheduleOptions(options);
  const chosen = countChosenSchedules(normalized);
  const streamPatchRequested = hasStreamSchedulePatch(normalized);
  if (streamPatchRequested && !normalized.streamCommand) {
    if (normalized.tz !== undefined || normalized.requestedStaggerMs !== undefined) {
      throw new CronCliError("--tz/--stagger/--exact are not valid with stream schedule edits");
    }
    if (chosen > 0) {
      throw new CronCliError("Choose at most one schedule change");
    }
    return {
      kind: "patch-existing-stream",
      cwd: normalized.streamCwdSupplied ? (normalized.streamCwd ?? null) : undefined,
      mode: normalized.streamModeSupplied ? normalized.streamMode : undefined,
      match: normalized.streamMatchSupplied ? (normalized.streamMatch ?? null) : undefined,
      batchMs: normalized.streamBatchMs,
      maxBatchBytes: normalized.streamMaxBatchBytes,
    };
  }
  if (chosen > 1) {
    throw new CronCliError("Choose at most one schedule change");
  }
  const schedule = resolveDirectSchedule(normalized, { deferStreamMetadataValidation: true });
  if (schedule) {
    return { kind: "direct", schedule };
  }
  if (normalized.requestedStaggerMs !== undefined || normalized.tz !== undefined) {
    return {
      kind: "patch-existing-cron",
      tz: normalized.tz,
      staggerMs: normalized.requestedStaggerMs,
    };
  }
  return { kind: "none" };
}

/** Apply stream metadata edits without requiring callers to restate the source argv. */
export function applyExistingStreamSchedulePatch(
  existingSchedule: CronSchedule,
  request: Extract<CronEditScheduleRequest, { kind: "patch-existing-stream" }>,
): CronSchedule {
  if (existingSchedule.kind !== "stream") {
    throw new CronCliError(
      "Current job is not a stream schedule; use --stream-command to convert first",
    );
  }
  const mode = request.mode ?? existingSchedule.mode ?? "line";
  const requestedMatch =
    request.match === undefined ? existingSchedule.match : (request.match ?? undefined);
  if (mode === "match" && !requestedMatch) {
    throw new CronCliError("--stream-match is required when --stream-mode=match");
  }
  if (mode === "line" && request.match) {
    throw new CronCliError("--stream-match requires --stream-mode=match");
  }
  return {
    ...existingSchedule,
    cwd: request.cwd === undefined ? existingSchedule.cwd : (request.cwd ?? undefined),
    mode,
    match: mode === "match" ? requestedMatch : undefined,
    batchMs: request.batchMs ?? existingSchedule.batchMs,
    maxBatchBytes: request.maxBatchBytes ?? existingSchedule.maxBatchBytes,
  };
}

/** Validate a newly-created stream schedule after edit metadata has been merged. */
export function validateStreamScheduleMetadata(
  schedule: Extract<CronSchedule, { kind: "stream" }>,
): void {
  const mode = schedule.mode ?? "line";
  if (mode === "match" && !schedule.match) {
    throw new CronCliError("--stream-match is required when --stream-mode=match");
  }
  if (mode === "line" && schedule.match) {
    throw new CronCliError("--stream-match requires --stream-mode=match");
  }
}

/** Apply `--tz`, `--stagger`, or `--exact` metadata changes to an existing cron schedule. */
export function applyExistingCronSchedulePatch(
  existingSchedule: CronSchedule,
  request: Extract<CronEditScheduleRequest, { kind: "patch-existing-cron" }>,
): CronSchedule {
  if (existingSchedule.kind !== "cron") {
    throw new CronCliError("Current job is not a cron schedule; use --cron to convert first");
  }
  return {
    kind: "cron",
    expr: existingSchedule.expr,
    tz: request.tz ?? existingSchedule.tz,
    staggerMs: request.staggerMs !== undefined ? request.staggerMs : existingSchedule.staggerMs,
  };
}

function normalizeScheduleOptions(options: ScheduleOptionInput): NormalizedScheduleOptions {
  for (const value of [options.at, options.every, options.cron, options.onExit]) {
    if (typeof value === "string" && !value.trim()) {
      throw new CronCliError("Schedule values must not be blank");
    }
  }
  const staggerRaw = normalizeOptionalString(options.stagger) ?? "";
  const useExact = Boolean(options.exact);
  if (staggerRaw && useExact) {
    throw new CronCliError("Choose either --stagger or --exact, not both");
  }
  const streamModeSupplied = options.streamMode !== undefined;
  const suppliedStreamMode = normalizeOptionalString(options.streamMode);
  if (streamModeSupplied && !suppliedStreamMode) {
    throw new CronCliError("--stream-mode must be line or match");
  }
  const streamModeRaw = suppliedStreamMode ?? "line";
  if (streamModeRaw !== "line" && streamModeRaw !== "match") {
    throw new CronCliError("--stream-mode must be line or match");
  }
  const parsePositiveInteger = (value: unknown, flag: string): number | undefined => {
    if (value === undefined) {
      return undefined;
    }
    if (typeof value !== "string" && typeof value !== "number") {
      throw new CronCliError(`${flag} must be a positive integer`);
    }
    const text = String(value).trim();
    if (!/^\d+$/u.test(text)) {
      throw new CronCliError(`${flag} must be a positive integer`);
    }
    const parsed = Number(text);
    if (!Number.isSafeInteger(parsed) || parsed <= 0) {
      throw new CronCliError(`${flag} must be a positive integer`);
    }
    return parsed;
  };
  return {
    at: normalizeOptionalString(options.at) ?? "",
    every: normalizeOptionalString(options.every) ?? "",
    cronExpr: normalizeOptionalString(options.cron) ?? "",
    onExitCommand: normalizeOptionalString(options.onExit) ?? "",
    onExitCwd: normalizeOptionalString(options.onExitCwd),
    streamCommand: parseCronStreamCommandArgv(options.streamCommand),
    streamCwd: normalizeOptionalString(options.streamCwd),
    streamCwdSupplied: options.streamCwd !== undefined,
    streamMode: streamModeRaw,
    streamModeSupplied,
    streamMatch: normalizeOptionalString(options.streamMatch),
    streamMatchSupplied: options.streamMatch !== undefined,
    streamBatchMs: parsePositiveInteger(options.streamBatchMs, "--stream-batch-ms"),
    streamMaxBatchBytes: parsePositiveInteger(
      options.streamMaxBatchBytes,
      "--stream-max-batch-bytes",
    ),
    tz: normalizeOptionalString(options.tz),
    requestedStaggerMs: parseCronStaggerMs({ staggerRaw, useExact }),
  };
}

function hasStreamSchedulePatch(options: NormalizedScheduleOptions): boolean {
  return (
    options.streamCwdSupplied ||
    options.streamModeSupplied ||
    options.streamMatchSupplied ||
    options.streamBatchMs !== undefined ||
    options.streamMaxBatchBytes !== undefined
  );
}

function countChosenSchedules(options: NormalizedScheduleOptions): number {
  return [
    Boolean(options.at),
    Boolean(options.every),
    Boolean(options.cronExpr),
    Boolean(options.onExitCommand),
    Boolean(options.streamCommand),
  ].filter(Boolean).length;
}

function parseEverySchedule(value: string): string | undefined {
  const match = /^every\s+(.+)$/iu.exec(value.trim());
  return match?.[1]?.trim() || undefined;
}

function looksLikeCronExpression(value: string): boolean {
  const parts = value.trim().split(/\s+/u);
  return parts.length === 5 || parts.length === 6;
}

function resolveDirectSchedule(
  options: NormalizedScheduleOptions,
  behavior: { deferStreamMetadataValidation?: boolean } = {},
): CronSchedule | undefined {
  if (options.onExitCwd && !options.onExitCommand) {
    throw new CronCliError("--on-exit-cwd requires --on-exit.");
  }
  if (hasStreamSchedulePatch(options) && !options.streamCommand) {
    throw new CronCliError("Stream options require --stream-command.");
  }
  if (options.tz && options.every) {
    throw new CronCliError("--tz is only valid with --cron or offset-less --at");
  }
  if (options.requestedStaggerMs !== undefined && (options.at || options.every)) {
    throw new CronCliError("--stagger/--exact are only valid for cron schedules");
  }
  if (options.at) {
    const atIso = parseAt(options.at, options.tz);
    if (!atIso) {
      throw new CronCliError("Invalid --at. Use an ISO timestamp or a duration like 20m.");
    }
    return { kind: "at", at: atIso };
  }
  if (options.every) {
    const everyMs = parsePositiveCronDurationMs(options.every);
    if (!everyMs) {
      throw new CronCliError("Invalid --every. Use a duration like 10m, 1h, or 1d.");
    }
    return { kind: "every", everyMs };
  }
  if (options.cronExpr) {
    return {
      kind: "cron",
      expr: options.cronExpr,
      tz: options.tz,
      staggerMs: options.requestedStaggerMs,
    };
  }
  if (options.onExitCommand) {
    if (options.tz || options.requestedStaggerMs !== undefined) {
      throw new CronCliError("--tz/--stagger/--exact are not valid with --on-exit");
    }
    return {
      kind: "on-exit",
      command: options.onExitCommand,
      ...(options.onExitCwd ? { cwd: options.onExitCwd } : {}),
    };
  }
  if (options.streamCommand) {
    if (options.tz || options.requestedStaggerMs !== undefined) {
      throw new CronCliError("--tz/--stagger/--exact are not valid with --stream-command");
    }
    const schedule: Extract<CronSchedule, { kind: "stream" }> = {
      kind: "stream",
      command: options.streamCommand,
      ...(options.streamCwd ? { cwd: options.streamCwd } : {}),
      mode: options.streamMode,
      ...(options.streamMatch ? { match: options.streamMatch } : {}),
      ...(options.streamBatchMs !== undefined ? { batchMs: options.streamBatchMs } : {}),
      ...(options.streamMaxBatchBytes !== undefined
        ? { maxBatchBytes: options.streamMaxBatchBytes }
        : {}),
    };
    if (!behavior.deferStreamMetadataValidation) {
      validateStreamScheduleMetadata(schedule);
    }
    return schedule;
  }
  return undefined;
}