@proofoftech/flowsafe 0.33.0 → 0.35.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/README.md +1 -1
- package/dist/approval-api/actor-context.d.ts.map +1 -1
- package/dist/approval-api/actor-context.js +2 -0
- package/dist/approval-api/actor-context.js.map +1 -1
- package/dist/approval-api/d1-store.d.ts.map +1 -1
- package/dist/approval-api/d1-store.js +37 -23
- package/dist/approval-api/d1-store.js.map +1 -1
- package/dist/approval-api/retention.d.ts.map +1 -1
- package/dist/approval-api/retention.js +6 -8
- package/dist/approval-api/retention.js.map +1 -1
- package/dist/approval-api/router.d.ts +1 -1
- package/dist/approval-api/router.d.ts.map +1 -1
- package/dist/approval-api/router.js +1 -0
- package/dist/approval-api/router.js.map +1 -1
- package/dist/approval-api/service.d.ts +4 -5
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +28 -51
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/store.d.ts +2 -18
- package/dist/approval-api/store.d.ts.map +1 -1
- package/dist/approval-api/store.js +11 -24
- package/dist/approval-api/store.js.map +1 -1
- package/dist/approval-api/types.d.ts +6 -0
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +10 -0
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.js +5 -1
- package/dist/do-runner/fenced-workflows-d1.js.map +1 -1
- package/dist/do-runner/index.d.ts +1 -0
- package/dist/do-runner/index.d.ts.map +1 -1
- package/dist/do-runner/index.js +1 -0
- package/dist/do-runner/index.js.map +1 -1
- package/dist/do-runner/run-provenance.d.ts +3 -0
- package/dist/do-runner/run-provenance.d.ts.map +1 -1
- package/dist/do-runner/run-provenance.js +15 -2
- package/dist/do-runner/run-provenance.js.map +1 -1
- package/dist/do-runner/run-requester.d.ts +11 -0
- package/dist/do-runner/run-requester.d.ts.map +1 -0
- package/dist/do-runner/run-requester.js +29 -0
- package/dist/do-runner/run-requester.js.map +1 -0
- package/dist/do-runner/runtime.d.ts +5 -0
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +23 -12
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/schedule-source.d.ts +4 -3
- package/dist/do-runner/schedule-source.d.ts.map +1 -1
- package/dist/do-runner/schedule-source.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +1 -0
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/do-response.d.ts.map +1 -1
- package/dist/host-kit/do-response.js +9 -0
- package/dist/host-kit/do-response.js.map +1 -1
- package/dist/host-kit/do-run-topology.d.ts.map +1 -1
- package/dist/host-kit/do-run-topology.js +4 -1
- package/dist/host-kit/do-run-topology.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +8 -1
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +5 -0
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/index.d.ts +2 -1
- package/dist/host-kit/index.d.ts.map +1 -1
- package/dist/host-kit/index.js +1 -0
- package/dist/host-kit/index.js.map +1 -1
- package/dist/host-kit/run-router.d.ts +16 -2
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +46 -4
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/schedules/fire-plan.d.ts +30 -0
- package/dist/schedules/fire-plan.d.ts.map +1 -0
- package/dist/schedules/fire-plan.js +111 -0
- package/dist/schedules/fire-plan.js.map +1 -0
- package/dist/schedules/index.d.ts +4 -2
- package/dist/schedules/index.d.ts.map +1 -1
- package/dist/schedules/index.js +2 -0
- package/dist/schedules/index.js.map +1 -1
- package/dist/schedules/mutation-contract.d.ts +8 -6
- package/dist/schedules/mutation-contract.d.ts.map +1 -1
- package/dist/schedules/mutation-contract.js.map +1 -1
- package/dist/schedules/router.d.ts +9 -6
- package/dist/schedules/router.d.ts.map +1 -1
- package/dist/schedules/router.js +362 -94
- package/dist/schedules/router.js.map +1 -1
- package/dist/schedules/schedule-extensions.d.ts +46 -0
- package/dist/schedules/schedule-extensions.d.ts.map +1 -0
- package/dist/schedules/schedule-extensions.js +7 -0
- package/dist/schedules/schedule-extensions.js.map +1 -0
- package/dist/schedules/schedules-d1.d.ts +44 -10
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +437 -68
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/tick.d.ts +81 -18
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +701 -168
- package/dist/schedules/tick.js.map +1 -1
- package/package.json +1 -1
package/dist/schedules/router.js
CHANGED
|
@@ -48,7 +48,9 @@ import { safeDecodeSegment } from '../host-kit/route-path.js';
|
|
|
48
48
|
import { readBoundedBody } from '../http-body.js';
|
|
49
49
|
import { internalErrorResponse } from '../internal-error-response.js';
|
|
50
50
|
import { nonnegativeSafeInteger, positiveSafeInteger, } from '../numeric-config.js';
|
|
51
|
+
import { previewScheduleFires, SCHEDULE_PREVIEW_MAX_COUNT, } from './fire-plan.js';
|
|
51
52
|
import { FENCED_SCHEDULE_STORAGE, ScheduleMutationConflictError, ScheduleMutationOutcomeUnknownError, } from './mutation-contract.js';
|
|
53
|
+
import { SCHEDULE_AT_MAX_HORIZON_MS, SCHEDULE_CATCH_UP_MAX_MISSED, SCHEDULE_CATCH_UP_MAX_WINDOW_MS, SCHEDULE_JITTER_MAX_MS, } from './schedule-extensions.js';
|
|
52
54
|
import { scheduleCreatorRole, scheduleWithCreatorRole, } from './target-policy.js';
|
|
53
55
|
import { deepScheduleTargetField, isReservedScheduleContextKey, } from './tick.js';
|
|
54
56
|
function json(payload, status = 200) {
|
|
@@ -95,6 +97,15 @@ async function requireTargetOwner(context, target, owner, validateThreadTarget)
|
|
|
95
97
|
function reject(reason, message, status = 400) {
|
|
96
98
|
return { reason, message, status };
|
|
97
99
|
}
|
|
100
|
+
function timingReject(reason, message, status = 400) {
|
|
101
|
+
return { ...reject(reason, message, status), publicReason: true };
|
|
102
|
+
}
|
|
103
|
+
function rejectionResponse(error) {
|
|
104
|
+
return json({
|
|
105
|
+
error: error.message,
|
|
106
|
+
...(error.publicReason ? { reason: error.reason } : {}),
|
|
107
|
+
}, error.status);
|
|
108
|
+
}
|
|
98
109
|
/** The refusal of a target with a value nested past the run-input bound. */
|
|
99
110
|
function targetDepthRejection(target) {
|
|
100
111
|
const field = deepScheduleTargetField(target);
|
|
@@ -160,27 +171,127 @@ function nextFireOrReject(cron, timezone, after) {
|
|
|
160
171
|
};
|
|
161
172
|
}
|
|
162
173
|
}
|
|
163
|
-
|
|
164
|
-
function checkFireRate(cron, timezone, minFireIntervalMs, now) {
|
|
174
|
+
function sampleFireInterval(cron, timezone, now) {
|
|
165
175
|
const first = nextFireOrReject(cron, timezone, now);
|
|
166
176
|
if (!first.ok)
|
|
167
|
-
return first
|
|
177
|
+
return first;
|
|
168
178
|
const second = nextFireOrReject(cron, timezone, first.value);
|
|
169
179
|
if (!second.ok)
|
|
170
|
-
return second
|
|
180
|
+
return second;
|
|
181
|
+
return {
|
|
182
|
+
ok: true,
|
|
183
|
+
value: { nextFireAt: first.value, intervalMs: second.value - first.value },
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
/** Reject a cron whose sampled fire interval is under the floor. */
|
|
187
|
+
function checkFireRate(intervalMs, minFireIntervalMs) {
|
|
171
188
|
// NOTE: this samples only the NEXT two fires at request
|
|
172
189
|
// time. A non-uniform cron (e.g. a dense minute cluster + a long gap) can pass
|
|
173
190
|
// when sampled during the gap yet still fire the dense cluster later; the
|
|
174
191
|
// deployment COUNT cap and the tick's run-cap seam are the real aggregate
|
|
175
192
|
// backstops. A precise cycle-wide bound is deliberately not attempted here.
|
|
176
|
-
if (
|
|
193
|
+
if (intervalMs < minFireIntervalMs) {
|
|
177
194
|
return reject('fire-rate-too-high', `cron fires more often than the ${minFireIntervalMs}ms floor`);
|
|
178
195
|
}
|
|
179
196
|
return undefined;
|
|
180
197
|
}
|
|
198
|
+
function ambiguousTiming() {
|
|
199
|
+
return timingReject('schedule-timing-ambiguous', 'specify exactly one of cron or at');
|
|
200
|
+
}
|
|
201
|
+
function completedSchedule() {
|
|
202
|
+
return timingReject('schedule-completed', 'give the completed schedule new timing to reactivate it', 409);
|
|
203
|
+
}
|
|
204
|
+
function atInPast() {
|
|
205
|
+
return timingReject('at-in-past', 'at must be in the future');
|
|
206
|
+
}
|
|
207
|
+
function oneOffOptionsRejection(body) {
|
|
208
|
+
if (body.timezone !== undefined) {
|
|
209
|
+
return timingReject('timezone-not-applicable', 'timezone requires a cron schedule');
|
|
210
|
+
}
|
|
211
|
+
if (body.overlapPolicy !== undefined && body.overlapPolicy !== 'allow') {
|
|
212
|
+
return timingReject('overlap-policy-not-applicable', 'one-off schedules only support the allow overlap policy');
|
|
213
|
+
}
|
|
214
|
+
if (body.catchUp !== undefined) {
|
|
215
|
+
return timingReject('catch-up-not-applicable', 'catchUp requires a cron schedule');
|
|
216
|
+
}
|
|
217
|
+
if (body.jitterMs !== undefined) {
|
|
218
|
+
return timingReject('jitter-not-applicable', 'jitterMs requires a cron schedule');
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
const ABSOLUTE_ISO_DATETIME = /^(?:\d{4}|[+-]\d{6})-\d{2}-\d{2}T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})$/;
|
|
222
|
+
function oneOffFireOrReject(body, now, minFireIntervalMs) {
|
|
223
|
+
const value = body.at;
|
|
224
|
+
const at = typeof value === 'string' && ABSOLUTE_ISO_DATETIME.test(value)
|
|
225
|
+
? Date.parse(value)
|
|
226
|
+
: value;
|
|
227
|
+
if (typeof at !== 'number' || !Number.isSafeInteger(at)) {
|
|
228
|
+
return {
|
|
229
|
+
ok: false,
|
|
230
|
+
error: timingReject('at-offset-required', 'at must be epoch milliseconds or an ISO date-time with Z or a numeric offset'),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
const options = oneOffOptionsRejection(body);
|
|
234
|
+
if (options)
|
|
235
|
+
return { ok: false, error: options };
|
|
236
|
+
if (at <= now)
|
|
237
|
+
return { ok: false, error: atInPast() };
|
|
238
|
+
if (at - now < minFireIntervalMs) {
|
|
239
|
+
return {
|
|
240
|
+
ok: false,
|
|
241
|
+
error: timingReject('at-too-soon', 'at is closer than the minimum fire interval'),
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
if (at - now > SCHEDULE_AT_MAX_HORIZON_MS) {
|
|
245
|
+
return {
|
|
246
|
+
ok: false,
|
|
247
|
+
error: timingReject('at-beyond-horizon', 'at must be within 366 days'),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
return { ok: true, value: { at, cron: '', nextFireAt: at } };
|
|
251
|
+
}
|
|
252
|
+
function cronFireOrReject(cron, timezone, now, minFireIntervalMs) {
|
|
253
|
+
if (!nonEmptyString(cron)) {
|
|
254
|
+
return {
|
|
255
|
+
ok: false,
|
|
256
|
+
error: reject('cron-invalid', 'cron must be a string'),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
if (timezone !== undefined && typeof timezone !== 'string') {
|
|
260
|
+
return {
|
|
261
|
+
ok: false,
|
|
262
|
+
error: reject('timezone-invalid', 'timezone must be a string'),
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
validateCron(cron, timezone);
|
|
267
|
+
}
|
|
268
|
+
catch (error) {
|
|
269
|
+
return {
|
|
270
|
+
ok: false,
|
|
271
|
+
error: reject('cron-invalid', error instanceof Error ? error.message : 'invalid cron'),
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
const sample = sampleFireInterval(cron, timezone, now);
|
|
275
|
+
if (!sample.ok)
|
|
276
|
+
return sample;
|
|
277
|
+
const fireRate = checkFireRate(sample.value.intervalMs, minFireIntervalMs);
|
|
278
|
+
if (fireRate)
|
|
279
|
+
return { ok: false, error: fireRate };
|
|
280
|
+
return {
|
|
281
|
+
ok: true,
|
|
282
|
+
value: {
|
|
283
|
+
cron,
|
|
284
|
+
...(timezone !== undefined ? { timezone } : {}),
|
|
285
|
+
...sample.value,
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
const POLICY_FIELDS = ['overlapPolicy', 'catchUp', 'jitterMs'];
|
|
181
290
|
const WORKFLOW_CREATE_FIELDS = new Set([
|
|
182
291
|
'workflowId',
|
|
183
292
|
'cron',
|
|
293
|
+
'at',
|
|
294
|
+
...POLICY_FIELDS,
|
|
184
295
|
'timezone',
|
|
185
296
|
'inputData',
|
|
186
297
|
'initialState',
|
|
@@ -191,6 +302,8 @@ const WORKFLOW_CREATE_FIELDS = new Set([
|
|
|
191
302
|
const AGENT_CREATE_FIELDS = new Set([
|
|
192
303
|
'agentId',
|
|
193
304
|
'cron',
|
|
305
|
+
'at',
|
|
306
|
+
...POLICY_FIELDS,
|
|
194
307
|
'prompt',
|
|
195
308
|
'name',
|
|
196
309
|
'timezone',
|
|
@@ -207,6 +320,8 @@ const AGENT_CREATE_FIELDS = new Set([
|
|
|
207
320
|
]);
|
|
208
321
|
const WORKFLOW_UPDATE_FIELDS = new Set([
|
|
209
322
|
'cron',
|
|
323
|
+
'at',
|
|
324
|
+
...POLICY_FIELDS,
|
|
210
325
|
'timezone',
|
|
211
326
|
'status',
|
|
212
327
|
'inputData',
|
|
@@ -216,6 +331,8 @@ const WORKFLOW_UPDATE_FIELDS = new Set([
|
|
|
216
331
|
]);
|
|
217
332
|
const AGENT_UPDATE_FIELDS = new Set([
|
|
218
333
|
'cron',
|
|
334
|
+
'at',
|
|
335
|
+
...POLICY_FIELDS,
|
|
219
336
|
'timezone',
|
|
220
337
|
'status',
|
|
221
338
|
'prompt',
|
|
@@ -231,6 +348,67 @@ const AGENT_UPDATE_FIELDS = new Set([
|
|
|
231
348
|
function firstUnknownField(body, allowed) {
|
|
232
349
|
return Object.keys(body).find((key) => !allowed.has(key));
|
|
233
350
|
}
|
|
351
|
+
function parseFirePolicy(body, threaded) {
|
|
352
|
+
const { overlapPolicy, catchUp, jitterMs } = body;
|
|
353
|
+
if (overlapPolicy !== undefined &&
|
|
354
|
+
overlapPolicy !== 'allow' &&
|
|
355
|
+
overlapPolicy !== 'skip' &&
|
|
356
|
+
overlapPolicy !== 'queue_one') {
|
|
357
|
+
return {
|
|
358
|
+
ok: false,
|
|
359
|
+
error: timingReject('overlap-policy-invalid', "overlapPolicy must be 'allow', 'skip', or 'queue_one'"),
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
if (threaded && overlapPolicy !== undefined && overlapPolicy !== 'allow') {
|
|
363
|
+
return {
|
|
364
|
+
ok: false,
|
|
365
|
+
error: timingReject('overlap-policy-not-applicable', 'threaded agent schedules only support the allow overlap policy'),
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
let parsedCatchUp;
|
|
369
|
+
if (catchUp !== undefined) {
|
|
370
|
+
const { maxMissed, windowMs } = catchUp && typeof catchUp === 'object' && !Array.isArray(catchUp)
|
|
371
|
+
? catchUp
|
|
372
|
+
: {};
|
|
373
|
+
if (typeof maxMissed !== 'number' ||
|
|
374
|
+
!Number.isInteger(maxMissed) ||
|
|
375
|
+
maxMissed < 0 ||
|
|
376
|
+
maxMissed > SCHEDULE_CATCH_UP_MAX_MISSED ||
|
|
377
|
+
typeof windowMs !== 'number' ||
|
|
378
|
+
!Number.isInteger(windowMs) ||
|
|
379
|
+
windowMs < 60_000 ||
|
|
380
|
+
windowMs > SCHEDULE_CATCH_UP_MAX_WINDOW_MS) {
|
|
381
|
+
return {
|
|
382
|
+
ok: false,
|
|
383
|
+
error: timingReject('catch-up-out-of-range', `catchUp requires an integer maxMissed from 0 to ${SCHEDULE_CATCH_UP_MAX_MISSED} and an integer windowMs from 60000 to ${SCHEDULE_CATCH_UP_MAX_WINDOW_MS}`),
|
|
384
|
+
};
|
|
385
|
+
}
|
|
386
|
+
parsedCatchUp = { maxMissed, windowMs };
|
|
387
|
+
}
|
|
388
|
+
if (jitterMs !== undefined &&
|
|
389
|
+
(typeof jitterMs !== 'number' ||
|
|
390
|
+
!Number.isInteger(jitterMs) ||
|
|
391
|
+
jitterMs < 0 ||
|
|
392
|
+
jitterMs > SCHEDULE_JITTER_MAX_MS)) {
|
|
393
|
+
return {
|
|
394
|
+
ok: false,
|
|
395
|
+
error: timingReject('jitter-out-of-range', `jitterMs must be an integer from 0 to ${SCHEDULE_JITTER_MAX_MS}`),
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
return {
|
|
399
|
+
ok: true,
|
|
400
|
+
value: {
|
|
401
|
+
...(overlapPolicy !== undefined ? { overlapPolicy } : {}),
|
|
402
|
+
...(parsedCatchUp !== undefined ? { catchUp: parsedCatchUp } : {}),
|
|
403
|
+
...(jitterMs !== undefined ? { jitterMs } : {}),
|
|
404
|
+
},
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
function jitterIntervalRejection(jitterMs, intervalMs) {
|
|
408
|
+
if (jitterMs !== undefined && jitterMs >= intervalMs) {
|
|
409
|
+
return timingReject('jitter-out-of-range', 'jitterMs must be below the sampled cron interval');
|
|
410
|
+
}
|
|
411
|
+
}
|
|
234
412
|
function nonEmptyString(value) {
|
|
235
413
|
return typeof value === 'string' && value.length > 0;
|
|
236
414
|
}
|
|
@@ -317,32 +495,26 @@ function buildCreateRow(body, now, minFireIntervalMs) {
|
|
|
317
495
|
error: reject('target-ambiguous', 'exactly one of workflowId or agentId is required'),
|
|
318
496
|
};
|
|
319
497
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
error: reject('cron-invalid', 'cron must be a string'),
|
|
324
|
-
};
|
|
498
|
+
const oneOff = body.at !== undefined;
|
|
499
|
+
if (oneOff === (body.cron !== undefined)) {
|
|
500
|
+
return { ok: false, error: ambiguousTiming() };
|
|
325
501
|
}
|
|
326
|
-
const
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
502
|
+
const timing = oneOff
|
|
503
|
+
? oneOffFireOrReject(body, now, minFireIntervalMs)
|
|
504
|
+
: cronFireOrReject(body.cron, body.timezone, now, minFireIntervalMs);
|
|
505
|
+
if (!timing.ok)
|
|
506
|
+
return timing;
|
|
507
|
+
const { intervalMs, ...timingFields } = 'intervalMs' in timing.value
|
|
508
|
+
? timing.value
|
|
509
|
+
: { ...timing.value, intervalMs: undefined };
|
|
510
|
+
const policy = parseFirePolicy(body, hasAgent && body.threadId !== undefined);
|
|
511
|
+
if (!policy.ok)
|
|
512
|
+
return policy;
|
|
513
|
+
if (intervalMs !== undefined) {
|
|
514
|
+
const jitter = jitterIntervalRejection(policy.value.jitterMs, intervalMs);
|
|
515
|
+
if (jitter)
|
|
516
|
+
return { ok: false, error: jitter };
|
|
336
517
|
}
|
|
337
|
-
catch (error) {
|
|
338
|
-
return {
|
|
339
|
-
ok: false,
|
|
340
|
-
error: reject('cron-invalid', error instanceof Error ? error.message : 'invalid cron'),
|
|
341
|
-
};
|
|
342
|
-
}
|
|
343
|
-
const fireRate = checkFireRate(cron, timezone, minFireIntervalMs, now);
|
|
344
|
-
if (fireRate)
|
|
345
|
-
return { ok: false, error: fireRate };
|
|
346
518
|
const status = body.status === undefined ? 'active' : body.status;
|
|
347
519
|
if (status !== 'active' && status !== 'paused') {
|
|
348
520
|
return {
|
|
@@ -357,17 +529,10 @@ function buildCreateRow(body, now, minFireIntervalMs) {
|
|
|
357
529
|
return parsedMetadata;
|
|
358
530
|
clientMetadata = parsedMetadata.value;
|
|
359
531
|
}
|
|
360
|
-
// checkFireRate above already proved the cron has a future occurrence, so this
|
|
361
|
-
// cannot throw; nextFireOrReject keeps it defensive without a bare computeNextFireAt.
|
|
362
|
-
const nextFire = nextFireOrReject(cron, timezone, now);
|
|
363
|
-
if (!nextFire.ok)
|
|
364
|
-
return { ok: false, error: nextFire.error };
|
|
365
|
-
const nextFireAt = nextFire.value;
|
|
366
532
|
const common = {
|
|
367
|
-
|
|
368
|
-
...
|
|
533
|
+
...timingFields,
|
|
534
|
+
...policy.value,
|
|
369
535
|
status,
|
|
370
|
-
nextFireAt,
|
|
371
536
|
createdAt: now,
|
|
372
537
|
updatedAt: now,
|
|
373
538
|
...(body.metadata !== undefined ? { metadata: clientMetadata } : {}),
|
|
@@ -507,7 +672,7 @@ function captureScheduleMutations(store, executionFence) {
|
|
|
507
672
|
};
|
|
508
673
|
}
|
|
509
674
|
export function createScheduleRouter(options) {
|
|
510
|
-
const { executionFence, resolve, store, targetPolicy, audit: auditSink, } = options;
|
|
675
|
+
const { executionFence, resolve, store, targetPolicy, defaultOverlapPolicy = 'allow', audit: auditSink, } = options;
|
|
511
676
|
const mutations = captureScheduleMutations(store, executionFence);
|
|
512
677
|
const roles = options.roles ?? RUN_START_ROLES;
|
|
513
678
|
const maxSchedules = nonnegativeSafeInteger(options.maxSchedules ?? 100, 'maxSchedules');
|
|
@@ -515,6 +680,7 @@ export function createScheduleRouter(options) {
|
|
|
515
680
|
const maxContentBytes = nonnegativeSafeInteger(options.maxContentBytes ?? 16_384, 'schedule maxContentBytes');
|
|
516
681
|
const base = options.basePath ?? '/api/schedules';
|
|
517
682
|
const baseSegments = base.split('/').filter(Boolean);
|
|
683
|
+
const view = (schedule) => toView(schedule, defaultOverlapPolicy);
|
|
518
684
|
return async (request) => {
|
|
519
685
|
const url = new URL(request.url);
|
|
520
686
|
const segments = url.pathname.split('/').filter(Boolean);
|
|
@@ -541,7 +707,10 @@ export function createScheduleRouter(options) {
|
|
|
541
707
|
if (operation === undefined) {
|
|
542
708
|
return json({ error: 'method not allowed' }, 405);
|
|
543
709
|
}
|
|
544
|
-
const isMutation = operation !== 'get' &&
|
|
710
|
+
const isMutation = operation !== 'get' &&
|
|
711
|
+
operation !== 'list' &&
|
|
712
|
+
operation !== 'preview' &&
|
|
713
|
+
operation !== 'triggers';
|
|
545
714
|
// `context` is hoisted so the audit closure can read it. It stays undefined
|
|
546
715
|
// until resolve succeeds, and the closure
|
|
547
716
|
// no-ops while it is — so a pre-auth failure never writes the log (an
|
|
@@ -623,7 +792,7 @@ export function createScheduleRouter(options) {
|
|
|
623
792
|
return json({
|
|
624
793
|
schedules: accessible
|
|
625
794
|
.filter((entry) => entry.allowed)
|
|
626
|
-
.map((entry) =>
|
|
795
|
+
.map((entry) => view(entry.schedule)),
|
|
627
796
|
});
|
|
628
797
|
}
|
|
629
798
|
// CREATE — count cap, field/cron/fire-rate/reserved-key checks, persist.
|
|
@@ -636,7 +805,7 @@ export function createScheduleRouter(options) {
|
|
|
636
805
|
const built = buildCreateRow(parsed, Date.now(), minFireIntervalMs);
|
|
637
806
|
if (!built.ok) {
|
|
638
807
|
await audit('rejected', built.error.reason);
|
|
639
|
-
return
|
|
808
|
+
return rejectionResponse(built.error);
|
|
640
809
|
}
|
|
641
810
|
const targetDecision = targetPolicy.authorize(built.value.target, context.actor.role);
|
|
642
811
|
if (!targetDecision.allowed) {
|
|
@@ -653,7 +822,7 @@ export function createScheduleRouter(options) {
|
|
|
653
822
|
return json({ error: `deployment schedule cap of ${maxSchedules} reached` }, 400);
|
|
654
823
|
}
|
|
655
824
|
await audit('accepted');
|
|
656
|
-
return json({ schedule:
|
|
825
|
+
return json({ schedule: view(created) }, 201);
|
|
657
826
|
}
|
|
658
827
|
// Everything below addresses ONE schedule by id — 404 (no oracle) on a
|
|
659
828
|
// missing or foreign one, before role checks or storage access.
|
|
@@ -672,7 +841,7 @@ export function createScheduleRouter(options) {
|
|
|
672
841
|
// RETENTION_DAYS is opt-in/unset by default).
|
|
673
842
|
const limit = clampLimit(url.searchParams.get('limit')) ?? 100;
|
|
674
843
|
const triggers = await store.listTriggers(scheduleId, { limit });
|
|
675
|
-
return json({ triggers });
|
|
844
|
+
return json({ triggers: triggers.map(toTriggerView) });
|
|
676
845
|
}
|
|
677
846
|
if (operation === 'delete') {
|
|
678
847
|
const outcome = mutations
|
|
@@ -695,29 +864,68 @@ export function createScheduleRouter(options) {
|
|
|
695
864
|
return json({ error: 'not found' }, 404);
|
|
696
865
|
}
|
|
697
866
|
if (operation === 'get') {
|
|
698
|
-
return json({ schedule:
|
|
867
|
+
return json({ schedule: view(existing) });
|
|
868
|
+
}
|
|
869
|
+
if (operation === 'preview') {
|
|
870
|
+
const rawCount = url.searchParams.get('count');
|
|
871
|
+
const count = rawCount === null ? 10 : Number(rawCount);
|
|
872
|
+
if ((rawCount !== null && !/^\d+$/.test(rawCount)) ||
|
|
873
|
+
!Number.isInteger(count) ||
|
|
874
|
+
count < 1 ||
|
|
875
|
+
count > SCHEDULE_PREVIEW_MAX_COUNT) {
|
|
876
|
+
const error = timingReject('count-invalid', `count must be an integer from 1 to ${SCHEDULE_PREVIEW_MAX_COUNT}`);
|
|
877
|
+
await audit('rejected', error.reason);
|
|
878
|
+
return rejectionResponse(error);
|
|
879
|
+
}
|
|
880
|
+
let fires;
|
|
881
|
+
try {
|
|
882
|
+
fires = previewScheduleFires(existing, { after: Date.now(), count });
|
|
883
|
+
}
|
|
884
|
+
catch (error) {
|
|
885
|
+
await audit('rejected', 'cron-invalid');
|
|
886
|
+
return json({ error: error instanceof Error ? error.message : 'invalid cron' }, 400);
|
|
887
|
+
}
|
|
888
|
+
return json({
|
|
889
|
+
scheduleId,
|
|
890
|
+
status: existing.status,
|
|
891
|
+
completed: existing.completedAt !== undefined,
|
|
892
|
+
overlapPolicy: effectiveOverlapPolicy(existing, defaultOverlapPolicy),
|
|
893
|
+
fires,
|
|
894
|
+
});
|
|
699
895
|
}
|
|
700
896
|
if (operation === 'pause') {
|
|
701
897
|
if (existing.status === 'paused') {
|
|
702
898
|
await audit('accepted');
|
|
703
|
-
return json({ schedule:
|
|
899
|
+
return json({ schedule: view(existing) });
|
|
704
900
|
}
|
|
705
901
|
const updated = mutations
|
|
706
902
|
? await mutations.pauseSchedule(scheduleId, mutation)
|
|
707
903
|
: await store.updateSchedule(scheduleId, { status: 'paused' });
|
|
708
904
|
await audit('accepted');
|
|
709
|
-
return json({ schedule:
|
|
905
|
+
return json({ schedule: view(updated) });
|
|
710
906
|
}
|
|
711
907
|
if (operation === 'resume') {
|
|
908
|
+
const now = Date.now();
|
|
909
|
+
const refusal = existing.completedAt !== undefined
|
|
910
|
+
? completedSchedule()
|
|
911
|
+
: existing.at !== undefined && existing.at <= now
|
|
912
|
+
? atInPast()
|
|
913
|
+
: undefined;
|
|
914
|
+
if (refusal) {
|
|
915
|
+
await audit('rejected', refusal.reason);
|
|
916
|
+
return rejectionResponse(refusal);
|
|
917
|
+
}
|
|
712
918
|
if (existing.status === 'active') {
|
|
713
919
|
await audit('accepted');
|
|
714
|
-
return json({ schedule:
|
|
920
|
+
return json({ schedule: view(existing) });
|
|
715
921
|
}
|
|
716
922
|
// Re-activating recomputes nextFireAt from now (core's resume semantics).
|
|
717
923
|
// Guarded: a legacy row with a calendrically-impossible cron would else
|
|
718
924
|
// throw an unaudited 500 here (create now rejects such crons, so this is
|
|
719
925
|
// defense-in-depth for pre-existing rows).
|
|
720
|
-
const nextFire =
|
|
926
|
+
const nextFire = existing.at === undefined
|
|
927
|
+
? nextFireOrReject(existing.cron, existing.timezone, now)
|
|
928
|
+
: { ok: true, value: existing.at };
|
|
721
929
|
if (!nextFire.ok) {
|
|
722
930
|
await audit('rejected', nextFire.error.reason);
|
|
723
931
|
return json({ error: nextFire.error.message }, nextFire.error.status);
|
|
@@ -727,6 +935,9 @@ export function createScheduleRouter(options) {
|
|
|
727
935
|
? await mutations.resumeSchedule(scheduleId, {
|
|
728
936
|
expectedCron: existing.cron,
|
|
729
937
|
expectedTimezone: existing.timezone,
|
|
938
|
+
...(existing.at === undefined
|
|
939
|
+
? {}
|
|
940
|
+
: { expectedAt: existing.at }),
|
|
730
941
|
nextFireAt,
|
|
731
942
|
}, mutation)
|
|
732
943
|
: await store.updateSchedule(scheduleId, {
|
|
@@ -734,7 +945,7 @@ export function createScheduleRouter(options) {
|
|
|
734
945
|
nextFireAt,
|
|
735
946
|
});
|
|
736
947
|
await audit('accepted');
|
|
737
|
-
return json({ schedule:
|
|
948
|
+
return json({ schedule: view(updated) });
|
|
738
949
|
}
|
|
739
950
|
// UPDATE (PATCH).
|
|
740
951
|
const parsed = await readBody(request, maxContentBytes);
|
|
@@ -745,7 +956,7 @@ export function createScheduleRouter(options) {
|
|
|
745
956
|
const patch = buildUpdatePatch(existing, parsed, minFireIntervalMs, Date.now());
|
|
746
957
|
if (!patch.ok) {
|
|
747
958
|
await audit('rejected', patch.error.reason);
|
|
748
|
-
return
|
|
959
|
+
return rejectionResponse(patch.error);
|
|
749
960
|
}
|
|
750
961
|
if (patch.value.target !== undefined) {
|
|
751
962
|
const targetDecision = targetPolicy.authorize(patch.value.target, scheduleCreatorRole(existing));
|
|
@@ -759,11 +970,18 @@ export function createScheduleRouter(options) {
|
|
|
759
970
|
}
|
|
760
971
|
await requireTargetOwner(context, patch.value.target, scheduleOwner, options.validateThreadTarget);
|
|
761
972
|
}
|
|
973
|
+
const checksTiming = patch.value.nextFireAt !== undefined ||
|
|
974
|
+
patch.value.status === 'active' ||
|
|
975
|
+
POLICY_FIELDS.some((field) => patch.value[field] !== undefined);
|
|
762
976
|
const updated = mutations
|
|
763
|
-
? await mutations.updateSchedule(scheduleId, patch.value, mutation
|
|
977
|
+
? await mutations.updateSchedule(scheduleId, patch.value, mutation, ...(checksTiming
|
|
978
|
+
? [
|
|
979
|
+
{ at: existing.at, completedAt: existing.completedAt },
|
|
980
|
+
]
|
|
981
|
+
: []))
|
|
764
982
|
: await store.updateSchedule(scheduleId, patch.value);
|
|
765
983
|
await audit('accepted');
|
|
766
|
-
return json({ schedule:
|
|
984
|
+
return json({ schedule: view(updated) });
|
|
767
985
|
}
|
|
768
986
|
catch (error) {
|
|
769
987
|
// A fence that could not be READ is not evidence the deployment is open,
|
|
@@ -827,11 +1045,28 @@ function resolveOperation(method, id, sub) {
|
|
|
827
1045
|
return method === 'POST' ? 'pause' : undefined;
|
|
828
1046
|
if (sub === 'resume')
|
|
829
1047
|
return method === 'POST' ? 'resume' : undefined;
|
|
1048
|
+
if (sub === 'preview')
|
|
1049
|
+
return method === 'GET' ? 'preview' : undefined;
|
|
830
1050
|
if (sub === 'triggers')
|
|
831
1051
|
return method === 'GET' ? 'triggers' : undefined;
|
|
832
1052
|
return null;
|
|
833
1053
|
}
|
|
834
1054
|
function buildUpdatePatch(existing, body, minFireIntervalMs, now) {
|
|
1055
|
+
const hasAt = body.at !== undefined;
|
|
1056
|
+
const hasCron = body.cron !== undefined;
|
|
1057
|
+
if (hasAt && hasCron)
|
|
1058
|
+
return { ok: false, error: ambiguousTiming() };
|
|
1059
|
+
const at = hasAt
|
|
1060
|
+
? oneOffFireOrReject(body, now, minFireIntervalMs)
|
|
1061
|
+
: undefined;
|
|
1062
|
+
if (at && !at.ok)
|
|
1063
|
+
return at;
|
|
1064
|
+
const oneOff = hasAt || (!hasCron && existing.at !== undefined);
|
|
1065
|
+
if (oneOff && !hasAt) {
|
|
1066
|
+
const options = oneOffOptionsRejection(body);
|
|
1067
|
+
if (options)
|
|
1068
|
+
return { ok: false, error: options };
|
|
1069
|
+
}
|
|
835
1070
|
const allowed = existing.target.type === 'workflow'
|
|
836
1071
|
? WORKFLOW_UPDATE_FIELDS
|
|
837
1072
|
: AGENT_UPDATE_FIELDS;
|
|
@@ -842,46 +1077,41 @@ function buildUpdatePatch(existing, body, minFireIntervalMs, now) {
|
|
|
842
1077
|
error: reject(`field-not-allowed:${unknown}`, `field '${unknown}' is not updatable`),
|
|
843
1078
|
};
|
|
844
1079
|
}
|
|
845
|
-
const
|
|
846
|
-
const
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
// persists into the timezone TEXT column.
|
|
858
|
-
if (body.timezone !== undefined && typeof body.timezone !== 'string') {
|
|
859
|
-
return {
|
|
860
|
-
ok: false,
|
|
861
|
-
error: reject('timezone-invalid', 'timezone must be a string'),
|
|
862
|
-
};
|
|
863
|
-
}
|
|
864
|
-
try {
|
|
865
|
-
validateCron(nextCron, nextTimezone);
|
|
1080
|
+
const threaded = existing.target.type === 'agent' && existing.target.threadId !== undefined;
|
|
1081
|
+
const policy = parseFirePolicy(body, threaded);
|
|
1082
|
+
if (!policy.ok)
|
|
1083
|
+
return policy;
|
|
1084
|
+
const patch = at
|
|
1085
|
+
? { ...at.value, timezone: undefined, ...policy.value }
|
|
1086
|
+
: { ...policy.value };
|
|
1087
|
+
if (at) {
|
|
1088
|
+
if (existing.at === undefined) {
|
|
1089
|
+
patch.overlapPolicy = policy.value.overlapPolicy ?? null;
|
|
1090
|
+
patch.catchUp = null;
|
|
1091
|
+
patch.jitterMs = null;
|
|
866
1092
|
}
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
patch.cron = nextCron;
|
|
1093
|
+
}
|
|
1094
|
+
else if (hasCron || body.timezone !== undefined) {
|
|
1095
|
+
const timing = cronFireOrReject(hasCron ? body.cron : existing.cron, body.timezone !== undefined ? body.timezone : existing.timezone, now, minFireIntervalMs);
|
|
1096
|
+
if (!timing.ok)
|
|
1097
|
+
return timing;
|
|
1098
|
+
const jitter = jitterIntervalRejection(policy.value.jitterMs ?? existing.jitterMs, timing.value.intervalMs);
|
|
1099
|
+
if (jitter)
|
|
1100
|
+
return { ok: false, error: jitter };
|
|
1101
|
+
patch.cron = timing.value.cron;
|
|
877
1102
|
if (body.timezone !== undefined)
|
|
878
|
-
patch.timezone =
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
1103
|
+
patch.timezone = timing.value.timezone;
|
|
1104
|
+
if (hasCron)
|
|
1105
|
+
patch.at = null;
|
|
1106
|
+
patch.nextFireAt = timing.value.nextFireAt;
|
|
1107
|
+
}
|
|
1108
|
+
else if (policy.value.jitterMs !== undefined) {
|
|
1109
|
+
const sample = sampleFireInterval(existing.cron, existing.timezone, now);
|
|
1110
|
+
if (!sample.ok)
|
|
1111
|
+
return sample;
|
|
1112
|
+
const jitter = jitterIntervalRejection(policy.value.jitterMs, sample.value.intervalMs);
|
|
1113
|
+
if (jitter)
|
|
1114
|
+
return { ok: false, error: jitter };
|
|
885
1115
|
}
|
|
886
1116
|
if (body.status !== undefined) {
|
|
887
1117
|
if (body.status !== 'active' && body.status !== 'paused') {
|
|
@@ -892,6 +1122,23 @@ function buildUpdatePatch(existing, body, minFireIntervalMs, now) {
|
|
|
892
1122
|
}
|
|
893
1123
|
patch.status = body.status;
|
|
894
1124
|
}
|
|
1125
|
+
if (existing.completedAt !== undefined) {
|
|
1126
|
+
if (hasAt || hasCron) {
|
|
1127
|
+
patch.completedAt = null;
|
|
1128
|
+
patch.status = 'active';
|
|
1129
|
+
}
|
|
1130
|
+
else if (patch.status === 'active') {
|
|
1131
|
+
return { ok: false, error: completedSchedule() };
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
else if (!hasAt &&
|
|
1135
|
+
!hasCron &&
|
|
1136
|
+
patch.status === 'active' &&
|
|
1137
|
+
existing.status === 'paused' &&
|
|
1138
|
+
existing.at !== undefined &&
|
|
1139
|
+
existing.at <= now) {
|
|
1140
|
+
return { ok: false, error: atInPast() };
|
|
1141
|
+
}
|
|
895
1142
|
const reserved = reservedContextRejection(body, existing.target.type);
|
|
896
1143
|
if (reserved)
|
|
897
1144
|
return { ok: false, error: reserved };
|
|
@@ -983,15 +1230,36 @@ function patchTarget(existing, body) {
|
|
|
983
1230
|
const normalized = normalizeAgentTarget(existing.id, merged);
|
|
984
1231
|
return normalized.ok ? { ok: true, value: normalized.value } : normalized;
|
|
985
1232
|
}
|
|
986
|
-
function
|
|
987
|
-
|
|
1233
|
+
function effectiveOverlapPolicy(schedule, defaultOverlapPolicy) {
|
|
1234
|
+
if (schedule.at !== undefined ||
|
|
1235
|
+
(schedule.target.type === 'agent' && schedule.target.threadId !== undefined)) {
|
|
1236
|
+
return 'allow';
|
|
1237
|
+
}
|
|
1238
|
+
return schedule.overlapPolicy ?? defaultOverlapPolicy;
|
|
1239
|
+
}
|
|
1240
|
+
function toView(schedule, defaultOverlapPolicy) {
|
|
988
1241
|
const view = toScheduleView(schedule);
|
|
989
1242
|
if (!view) {
|
|
990
1243
|
// Unreachable for a well-formed row; return a minimal fallback rather than
|
|
991
1244
|
// throw (a corrupt row must not 500 a whole list).
|
|
992
1245
|
return { id: schedule.id };
|
|
993
1246
|
}
|
|
994
|
-
return
|
|
1247
|
+
return {
|
|
1248
|
+
...view,
|
|
1249
|
+
...(schedule.at === undefined ? {} : { at: schedule.at }),
|
|
1250
|
+
...(schedule.completedAt === undefined
|
|
1251
|
+
? {}
|
|
1252
|
+
: { completedAt: schedule.completedAt }),
|
|
1253
|
+
overlapPolicy: effectiveOverlapPolicy(schedule, defaultOverlapPolicy),
|
|
1254
|
+
...(schedule.catchUp === undefined ? {} : { catchUp: schedule.catchUp }),
|
|
1255
|
+
...(schedule.jitterMs === undefined ? {} : { jitterMs: schedule.jitterMs }),
|
|
1256
|
+
};
|
|
1257
|
+
}
|
|
1258
|
+
function toTriggerView(trigger) {
|
|
1259
|
+
if (trigger.metadata?.scheduleOrigin === undefined)
|
|
1260
|
+
return trigger;
|
|
1261
|
+
const { scheduleOrigin: _origin, ...metadata } = trigger.metadata;
|
|
1262
|
+
return { ...trigger, metadata };
|
|
995
1263
|
}
|
|
996
1264
|
function clampLimit(raw) {
|
|
997
1265
|
if (raw === null)
|