@proofoftech/flowsafe 0.26.1 → 0.27.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 +86 -0
- package/README.md +11 -8
- package/dist/agent-host/router.d.ts.map +1 -1
- package/dist/agent-host/router.js +5 -11
- package/dist/agent-host/router.js.map +1 -1
- package/dist/agent-host/thread-host.d.ts.map +1 -1
- package/dist/agent-host/thread-host.js +82 -40
- package/dist/agent-host/thread-host.js.map +1 -1
- package/dist/agent-host/thread-topology.d.ts +8 -0
- package/dist/agent-host/thread-topology.d.ts.map +1 -1
- package/dist/agent-host/thread-topology.js +65 -33
- package/dist/agent-host/thread-topology.js.map +1 -1
- package/dist/agent-runner/durable-agent-runner.d.ts +10 -4
- package/dist/agent-runner/durable-agent-runner.d.ts.map +1 -1
- package/dist/agent-runner/durable-agent-runner.js +98 -20
- package/dist/agent-runner/durable-agent-runner.js.map +1 -1
- package/dist/approval-api/actor-context.d.ts +18 -3
- package/dist/approval-api/actor-context.d.ts.map +1 -1
- package/dist/approval-api/actor-context.js.map +1 -1
- package/dist/approval-api/grants.d.ts +9 -4
- package/dist/approval-api/grants.d.ts.map +1 -1
- package/dist/approval-api/grants.js +9 -4
- package/dist/approval-api/grants.js.map +1 -1
- package/dist/approval-api/principal.d.ts +12 -0
- package/dist/approval-api/principal.d.ts.map +1 -1
- package/dist/approval-api/principal.js +8 -0
- package/dist/approval-api/principal.js.map +1 -1
- package/dist/approval-api/router.d.ts.map +1 -1
- package/dist/approval-api/router.js +34 -16
- package/dist/approval-api/router.js.map +1 -1
- package/dist/approval-api/service.d.ts +7 -1
- package/dist/approval-api/service.d.ts.map +1 -1
- package/dist/approval-api/service.js +34 -13
- package/dist/approval-api/service.js.map +1 -1
- package/dist/approval-api/types.d.ts +33 -9
- package/dist/approval-api/types.d.ts.map +1 -1
- package/dist/approval-api/types.js.map +1 -1
- package/dist/audit-export/audit-proxy.d.ts.map +1 -1
- package/dist/audit-export/audit-proxy.js +4 -0
- package/dist/audit-export/audit-proxy.js.map +1 -1
- package/dist/background-tasks/d1-storage.d.ts.map +1 -1
- package/dist/background-tasks/d1-storage.js +2 -1
- package/dist/background-tasks/d1-storage.js.map +1 -1
- package/dist/do-runner/d1-storage.d.ts +6 -0
- package/dist/do-runner/d1-storage.d.ts.map +1 -1
- package/dist/do-runner/d1-storage.js +32 -10
- package/dist/do-runner/d1-storage.js.map +1 -1
- package/dist/do-runner/do-error-response.d.ts +14 -0
- package/dist/do-runner/do-error-response.d.ts.map +1 -1
- package/dist/do-runner/do-error-response.js +24 -40
- package/dist/do-runner/do-error-response.js.map +1 -1
- package/dist/do-runner/do-status-error.d.ts +29 -4
- package/dist/do-runner/do-status-error.d.ts.map +1 -1
- package/dist/do-runner/do-status-error.js +38 -3
- package/dist/do-runner/do-status-error.js.map +1 -1
- package/dist/do-runner/durable-object.d.ts.map +1 -1
- package/dist/do-runner/durable-object.js +44 -21
- package/dist/do-runner/durable-object.js.map +1 -1
- package/dist/do-runner/fenced-workflow-capability.d.ts +32 -5
- package/dist/do-runner/fenced-workflow-capability.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.d.ts +0 -8
- package/dist/do-runner/fenced-workflows-d1.d.ts.map +1 -1
- package/dist/do-runner/fenced-workflows-d1.js +71 -32
- package/dist/do-runner/fenced-workflows-d1.js.map +1 -1
- package/dist/do-runner/run-input-depth.d.ts +10 -3
- package/dist/do-runner/run-input-depth.d.ts.map +1 -1
- package/dist/do-runner/run-input-depth.js +12 -3
- package/dist/do-runner/run-input-depth.js.map +1 -1
- package/dist/do-runner/run-lifecycle.d.ts +14 -5
- package/dist/do-runner/run-lifecycle.d.ts.map +1 -1
- package/dist/do-runner/run-lifecycle.js +16 -5
- package/dist/do-runner/run-lifecycle.js.map +1 -1
- package/dist/do-runner/runtime.d.ts +45 -18
- package/dist/do-runner/runtime.d.ts.map +1 -1
- package/dist/do-runner/runtime.js +205 -113
- package/dist/do-runner/runtime.js.map +1 -1
- package/dist/do-runner/serialized-by-key.d.ts +9 -0
- package/dist/do-runner/serialized-by-key.d.ts.map +1 -0
- package/dist/do-runner/serialized-by-key.js +23 -0
- package/dist/do-runner/serialized-by-key.js.map +1 -0
- package/dist/goals/objective-routes.d.ts.map +1 -1
- package/dist/goals/objective-routes.js +2 -1
- package/dist/goals/objective-routes.js.map +1 -1
- package/dist/host-kit/approval-bridge.d.ts +6 -5
- package/dist/host-kit/approval-bridge.d.ts.map +1 -1
- package/dist/host-kit/approval-bridge.js +2 -2
- package/dist/host-kit/approval-bridge.js.map +1 -1
- package/dist/host-kit/do-response.d.ts +21 -1
- package/dist/host-kit/do-response.d.ts.map +1 -1
- package/dist/host-kit/do-response.js +56 -25
- package/dist/host-kit/do-response.js.map +1 -1
- package/dist/host-kit/flowsafe-worker.d.ts +29 -6
- package/dist/host-kit/flowsafe-worker.d.ts.map +1 -1
- package/dist/host-kit/flowsafe-worker.js +97 -105
- package/dist/host-kit/flowsafe-worker.js.map +1 -1
- package/dist/host-kit/run-route-error.d.ts +11 -0
- package/dist/host-kit/run-route-error.d.ts.map +1 -1
- package/dist/host-kit/run-route-error.js +13 -5
- package/dist/host-kit/run-route-error.js.map +1 -1
- package/dist/host-kit/run-router.d.ts.map +1 -1
- package/dist/host-kit/run-router.js +18 -41
- package/dist/host-kit/run-router.js.map +1 -1
- package/dist/host-kit/stream-router.d.ts.map +1 -1
- package/dist/host-kit/stream-router.js +11 -15
- package/dist/host-kit/stream-router.js.map +1 -1
- package/dist/internal-error-response.d.ts +7 -2
- package/dist/internal-error-response.d.ts.map +1 -1
- package/dist/internal-error-response.js +10 -3
- package/dist/internal-error-response.js.map +1 -1
- package/dist/schedules/router.d.ts.map +1 -1
- package/dist/schedules/router.js +9 -2
- package/dist/schedules/router.js.map +1 -1
- package/dist/schedules/schedules-d1.d.ts.map +1 -1
- package/dist/schedules/schedules-d1.js +4 -1
- package/dist/schedules/schedules-d1.js.map +1 -1
- package/dist/schedules/tick.d.ts +30 -16
- package/dist/schedules/tick.d.ts.map +1 -1
- package/dist/schedules/tick.js +236 -331
- package/dist/schedules/tick.js.map +1 -1
- package/dist/signal-providers/webhook-route.d.ts.map +1 -1
- package/dist/signal-providers/webhook-route.js +12 -6
- package/dist/signal-providers/webhook-route.js.map +1 -1
- package/dist/signals/router.d.ts.map +1 -1
- package/dist/signals/router.js +6 -4
- package/dist/signals/router.js.map +1 -1
- package/dist/signals/thread-do-routes.d.ts.map +1 -1
- package/dist/signals/thread-do-routes.js +7 -1
- package/dist/signals/thread-do-routes.js.map +1 -1
- package/package.json +2 -1
package/dist/schedules/tick.js
CHANGED
|
@@ -32,6 +32,7 @@ import { computeNextFireAt } from '@mastra/core/workflows';
|
|
|
32
32
|
import { admitsWorkAuthoring, isPathSafeId, isReservedExecutionContextKey, RESERVED_EXECUTION_CONTEXT_KEYS, readExecutionFence, resolveScheduleStartOwner, stripReservedExecutionContext, } from '../do-runner/index.js';
|
|
33
33
|
import { exceedsDepth, exceedsRunInputDepth, MAX_RUN_INPUT_DEPTH, runInputDepthMessage, } from '../do-runner/run-input-depth.js';
|
|
34
34
|
import { nonnegativeSafeInteger } from '../numeric-config.js';
|
|
35
|
+
import { parseScheduleAgentDispatchReceipt, } from './schedules-d1.js';
|
|
35
36
|
import { scheduleCreatorRole, } from './target-policy.js';
|
|
36
37
|
/**
|
|
37
38
|
* Compatibility alias for the former schedule-specific inventory. New code
|
|
@@ -228,7 +229,11 @@ function agentScheduleProviderOptions(scheduleId, target) {
|
|
|
228
229
|
},
|
|
229
230
|
};
|
|
230
231
|
}
|
|
231
|
-
|
|
232
|
+
/**
|
|
233
|
+
* Parse a stored dispatch reference and report whether its target nests past
|
|
234
|
+
* the run-input bound.
|
|
235
|
+
*/
|
|
236
|
+
function storedDispatchRef(value) {
|
|
232
237
|
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
233
238
|
return undefined;
|
|
234
239
|
}
|
|
@@ -243,8 +248,7 @@ function scheduleTickDispatchRef(value) {
|
|
|
243
248
|
if (stored === null ||
|
|
244
249
|
typeof stored !== 'object' ||
|
|
245
250
|
Array.isArray(stored) ||
|
|
246
|
-
stored.type !== 'workflow'
|
|
247
|
-
deepScheduleTargetField(stored) !== undefined) {
|
|
251
|
+
stored.type !== 'workflow') {
|
|
248
252
|
return undefined;
|
|
249
253
|
}
|
|
250
254
|
const workflowTarget = normalizeAndSanitizeWorkflowTarget(stored);
|
|
@@ -252,12 +256,15 @@ function scheduleTickDispatchRef(value) {
|
|
|
252
256
|
return undefined;
|
|
253
257
|
}
|
|
254
258
|
return {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
259
|
+
ref: {
|
|
260
|
+
scheduleId: candidate.scheduleId,
|
|
261
|
+
dispatchId: candidate.dispatchId,
|
|
262
|
+
target: 'workflow',
|
|
263
|
+
workflowId: candidate.workflowId,
|
|
264
|
+
runId: candidate.runId,
|
|
265
|
+
workflowTarget,
|
|
266
|
+
},
|
|
267
|
+
deep: deepScheduleTargetField(stored) !== undefined,
|
|
261
268
|
};
|
|
262
269
|
}
|
|
263
270
|
if (candidate.target !== 'agent' ||
|
|
@@ -270,8 +277,7 @@ function scheduleTickDispatchRef(value) {
|
|
|
270
277
|
if (stored === null ||
|
|
271
278
|
typeof stored !== 'object' ||
|
|
272
279
|
Array.isArray(stored) ||
|
|
273
|
-
stored.type !== 'agent'
|
|
274
|
-
deepScheduleTargetField(stored) !== undefined) {
|
|
280
|
+
stored.type !== 'agent') {
|
|
275
281
|
return undefined;
|
|
276
282
|
}
|
|
277
283
|
const agentTarget = normalizeAndSanitizeAgentTarget(candidate.scheduleId, stored);
|
|
@@ -282,16 +288,33 @@ function scheduleTickDispatchRef(value) {
|
|
|
282
288
|
return undefined;
|
|
283
289
|
}
|
|
284
290
|
return {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
291
|
+
ref: {
|
|
292
|
+
scheduleId: candidate.scheduleId,
|
|
293
|
+
dispatchId: candidate.dispatchId,
|
|
294
|
+
target: 'agent',
|
|
295
|
+
agentId: candidate.agentId,
|
|
296
|
+
threadId: candidate.threadId,
|
|
297
|
+
runId: candidate.runId,
|
|
298
|
+
mode: candidate.mode,
|
|
299
|
+
agentTarget,
|
|
300
|
+
},
|
|
301
|
+
deep: deepScheduleTargetField(stored) !== undefined,
|
|
293
302
|
};
|
|
294
303
|
}
|
|
304
|
+
/** A stored dispatch reference a dispatch path may send or execute. */
|
|
305
|
+
function scheduleTickDispatchRef(value) {
|
|
306
|
+
const stored = storedDispatchRef(value);
|
|
307
|
+
return stored?.deep ? undefined : stored?.ref;
|
|
308
|
+
}
|
|
309
|
+
/** A dispatch reference without its target snapshot. */
|
|
310
|
+
function dispatchIdentifiers(ref) {
|
|
311
|
+
if (ref.target === 'workflow') {
|
|
312
|
+
const { workflowTarget: _target, ...identifiers } = ref;
|
|
313
|
+
return identifiers;
|
|
314
|
+
}
|
|
315
|
+
const { agentTarget: _target, ...identifiers } = ref;
|
|
316
|
+
return identifiers;
|
|
317
|
+
}
|
|
295
318
|
/**
|
|
296
319
|
* Resolve the canonical target snapshot bound to one atomically claimed fire.
|
|
297
320
|
* A reusable schedule id alone never authorizes a start.
|
|
@@ -344,6 +367,22 @@ function isPermanentDispatchError(error) {
|
|
|
344
367
|
const status = statusOf(error);
|
|
345
368
|
return status === 400 || status === 403 || status === 404 || status === 422;
|
|
346
369
|
}
|
|
370
|
+
/**
|
|
371
|
+
* Age at which a lookup's 404 decides a threadless agent start the target did
|
|
372
|
+
* not refuse. The tick sends a start only at its fire, and the target commits
|
|
373
|
+
* an admitted start's run ownership, or its start recovery resolves the start,
|
|
374
|
+
* well within this age. Settling the fire also ends the target's admission of a
|
|
375
|
+
* late start.
|
|
376
|
+
*/
|
|
377
|
+
const UNRESOLVED_START_SETTLE_MS = 60 * 60_000;
|
|
378
|
+
/** The tally an outcome counts under: an outcome that ran nothing is skipped. */
|
|
379
|
+
function outcomeCounter(outcome) {
|
|
380
|
+
return outcome === 'persisted' ||
|
|
381
|
+
outcome === 'discarded' ||
|
|
382
|
+
outcome === 'skipped'
|
|
383
|
+
? 'skipped'
|
|
384
|
+
: 'fired';
|
|
385
|
+
}
|
|
347
386
|
/**
|
|
348
387
|
* Build the schedule tick: a `() => Promise<ScheduleTickResult>` a host slots
|
|
349
388
|
* into its alarm dispatch as its OWN failure-isolated duty (own try/catch, own
|
|
@@ -381,113 +420,143 @@ export function createScheduleTick(options) {
|
|
|
381
420
|
: {}),
|
|
382
421
|
...extra,
|
|
383
422
|
});
|
|
384
|
-
const
|
|
385
|
-
const published = summary !== undefined;
|
|
386
|
-
const receipt = summary?.dispatchReceipt;
|
|
387
|
-
const outcome = receipt?.outcome ?? (published ? 'published' : 'failed');
|
|
388
|
-
const resolvedRunId = receipt?.runId ?? summary?.runId ?? ref.runId;
|
|
389
|
-
const { error: priorError, ...resolvedTrigger } = trigger;
|
|
390
|
-
if (!published)
|
|
391
|
-
result.failed += 1;
|
|
392
|
-
else if (receipt &&
|
|
393
|
-
(receipt.outcome === 'persisted' ||
|
|
394
|
-
receipt.outcome === 'discarded' ||
|
|
395
|
-
receipt.outcome === 'skipped')) {
|
|
396
|
-
result.skipped += 1;
|
|
397
|
-
}
|
|
398
|
-
else {
|
|
399
|
-
result.fired += 1;
|
|
400
|
-
}
|
|
423
|
+
const auditOrLog = async (event, runId) => {
|
|
401
424
|
try {
|
|
402
|
-
await
|
|
403
|
-
...resolvedTrigger,
|
|
404
|
-
runId: published ? resolvedRunId : ref.runId,
|
|
405
|
-
outcome,
|
|
406
|
-
...(published ? {} : { error: priorError ?? 'start failed' }),
|
|
407
|
-
metadata: triggerMetadata({
|
|
408
|
-
...(trigger.metadata ?? {}),
|
|
409
|
-
dispatchRef: ref,
|
|
410
|
-
reason: published ? 'dispatch-reconciled' : 'start-error-confirmed',
|
|
411
|
-
}),
|
|
412
|
-
});
|
|
413
|
-
await audit({
|
|
414
|
-
scheduleId: ref.scheduleId,
|
|
415
|
-
target: ref.target,
|
|
416
|
-
outcome,
|
|
417
|
-
runId: resolvedRunId,
|
|
418
|
-
...(published ? {} : { reason: 'start-error-confirmed' }),
|
|
419
|
-
});
|
|
425
|
+
await audit(event);
|
|
420
426
|
}
|
|
421
427
|
catch (error) {
|
|
422
|
-
logBookkeepingError(
|
|
428
|
+
logBookkeepingError(event.scheduleId, runId, error);
|
|
423
429
|
}
|
|
424
430
|
};
|
|
425
|
-
|
|
431
|
+
/**
|
|
432
|
+
* Writes a trigger, then counts the fire under `counter` and audits it. A
|
|
433
|
+
* failed write counts the fire as deferred, because its trigger stays
|
|
434
|
+
* deferred for a later pass, and a failed audit changes no count. Resolves
|
|
435
|
+
* to whether the write changed the trigger; a write that resolves `false`
|
|
436
|
+
* changed nothing and counts nothing.
|
|
437
|
+
*/
|
|
438
|
+
const settleTrigger = async (result, write, counter, event, logRunId) => {
|
|
426
439
|
let changed;
|
|
427
440
|
try {
|
|
428
|
-
changed = await
|
|
441
|
+
changed = await write();
|
|
429
442
|
}
|
|
430
|
-
catch (
|
|
443
|
+
catch (error) {
|
|
431
444
|
result.deferred += 1;
|
|
432
|
-
logBookkeepingError(
|
|
445
|
+
logBookkeepingError(event.scheduleId, logRunId, error);
|
|
433
446
|
return false;
|
|
434
447
|
}
|
|
435
|
-
if (
|
|
448
|
+
if (changed === false)
|
|
436
449
|
return false;
|
|
437
|
-
result
|
|
450
|
+
result[counter] += 1;
|
|
451
|
+
await auditOrLog(event, logRunId);
|
|
452
|
+
return true;
|
|
453
|
+
};
|
|
454
|
+
const recordResolvedDispatch = (ref, trigger, summary, result, recordedRef = ref) => {
|
|
455
|
+
const published = summary !== undefined;
|
|
456
|
+
const receipt = summary?.dispatchReceipt;
|
|
457
|
+
const outcome = receipt?.outcome ?? (published ? 'published' : 'failed');
|
|
458
|
+
const resolvedRunId = receipt?.runId ?? summary?.runId ?? ref.runId;
|
|
459
|
+
const { error: priorError, ...resolvedTrigger } = trigger;
|
|
460
|
+
return settleTrigger(result, () => store.recordTrigger({
|
|
461
|
+
...resolvedTrigger,
|
|
462
|
+
runId: published ? resolvedRunId : ref.runId,
|
|
463
|
+
outcome,
|
|
464
|
+
...(published ? {} : { error: priorError ?? 'start failed' }),
|
|
465
|
+
metadata: triggerMetadata({
|
|
466
|
+
...(trigger.metadata ?? {}),
|
|
467
|
+
dispatchRef: recordedRef,
|
|
468
|
+
reason: published ? 'dispatch-reconciled' : 'start-error-confirmed',
|
|
469
|
+
}),
|
|
470
|
+
}), published ? outcomeCounter(outcome) : 'failed', {
|
|
471
|
+
scheduleId: ref.scheduleId,
|
|
472
|
+
target: ref.target,
|
|
473
|
+
outcome,
|
|
474
|
+
runId: resolvedRunId,
|
|
475
|
+
...(published ? {} : { reason: 'start-error-confirmed' }),
|
|
476
|
+
}, ref.runId);
|
|
477
|
+
};
|
|
478
|
+
/** Resolves to whether the write changed the deferred row. */
|
|
479
|
+
const recordFailedDispatch = (ref, trigger, error, reason, result) => settleTrigger(result, () => store.failDeferredTrigger(trigger.id ?? '', trigger.scheduleId, error instanceof Error ? error.message : String(error), triggerMetadata({ dispatchRef: ref, reason })), 'failed', {
|
|
480
|
+
scheduleId: ref.scheduleId,
|
|
481
|
+
target: ref.target,
|
|
482
|
+
outcome: 'failed',
|
|
483
|
+
reason,
|
|
484
|
+
runId: ref.runId,
|
|
485
|
+
}, ref.runId);
|
|
486
|
+
const settleMalformedDispatch = async (trigger, result, error = 'stored deferred dispatch is malformed', recorded = {}) => {
|
|
438
487
|
try {
|
|
439
|
-
await
|
|
440
|
-
|
|
441
|
-
target: ref.target,
|
|
488
|
+
await store.recordTrigger({
|
|
489
|
+
...trigger,
|
|
442
490
|
outcome: 'failed',
|
|
443
|
-
|
|
444
|
-
|
|
491
|
+
error,
|
|
492
|
+
metadata: triggerMetadata({
|
|
493
|
+
reason: 'invalid-deferred-dispatch',
|
|
494
|
+
...recorded,
|
|
495
|
+
}),
|
|
445
496
|
});
|
|
497
|
+
result.failed += 1;
|
|
498
|
+
result.reconciled += 1;
|
|
446
499
|
}
|
|
447
500
|
catch (bookkeepingError) {
|
|
448
|
-
|
|
501
|
+
result.deferred += 1;
|
|
502
|
+
logBookkeepingError(trigger.scheduleId, trigger.runId ?? undefined, bookkeepingError);
|
|
449
503
|
}
|
|
450
|
-
return true;
|
|
451
504
|
};
|
|
452
505
|
const reconcileDeferred = async (result) => {
|
|
453
506
|
const pending = await store.listDeferredTriggers(limit);
|
|
454
507
|
for (const trigger of pending) {
|
|
455
|
-
const
|
|
508
|
+
const storedRef = trigger.metadata?.dispatchRef;
|
|
509
|
+
const ref = scheduleTickDispatchRef(storedRef);
|
|
456
510
|
if (!ref) {
|
|
511
|
+
const stored = storedDispatchRef(storedRef);
|
|
512
|
+
if (!stored?.deep) {
|
|
513
|
+
await settleMalformedDispatch(trigger, result);
|
|
514
|
+
continue;
|
|
515
|
+
}
|
|
516
|
+
// A target nested past the run-input bound is never dispatched again,
|
|
517
|
+
// so the target's own state is the evidence of whether this fire ran.
|
|
518
|
+
let summary;
|
|
457
519
|
try {
|
|
458
|
-
await
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
520
|
+
summary = await options.status(stored.ref);
|
|
521
|
+
}
|
|
522
|
+
catch (lookupError) {
|
|
523
|
+
// With nothing to send again, a lookup that cannot decide the fire
|
|
524
|
+
// settles it, keeping the lookup's error.
|
|
525
|
+
await settleMalformedDispatch(trigger, result, runInputDepthMessage('stored deferred dispatch'), {
|
|
526
|
+
dispatchRef: dispatchIdentifiers(stored.ref),
|
|
527
|
+
statusError: lookupError instanceof Error
|
|
528
|
+
? lookupError.message
|
|
529
|
+
: String(lookupError),
|
|
463
530
|
});
|
|
464
|
-
|
|
465
|
-
result.reconciled += 1;
|
|
531
|
+
continue;
|
|
466
532
|
}
|
|
467
|
-
|
|
468
|
-
result.
|
|
469
|
-
logBookkeepingError(trigger.scheduleId, trigger.runId ?? undefined, bookkeepingError);
|
|
533
|
+
if (await recordResolvedDispatch(stored.ref, trigger, summary, result, dispatchIdentifiers(stored.ref))) {
|
|
534
|
+
result.reconciled += 1;
|
|
470
535
|
}
|
|
471
536
|
continue;
|
|
472
537
|
}
|
|
473
538
|
try {
|
|
474
539
|
const summary = await options.status(ref);
|
|
475
|
-
result
|
|
476
|
-
|
|
540
|
+
if (await recordResolvedDispatch(ref, trigger, summary, result)) {
|
|
541
|
+
result.reconciled += 1;
|
|
542
|
+
}
|
|
477
543
|
}
|
|
478
544
|
catch (error) {
|
|
479
|
-
// A start the target refused before it reserved a run leaves none in
|
|
480
|
-
// flight, so a lookup that states the run does not exist decides it.
|
|
481
545
|
if (ref.target === 'agent' &&
|
|
482
546
|
ref.mode === 'start' &&
|
|
483
|
-
isPermanentDispatchError({
|
|
484
|
-
status: trigger.metadata?.refusedStatus,
|
|
485
|
-
}) &&
|
|
486
547
|
statusOf(error) === 404) {
|
|
487
|
-
|
|
488
|
-
|
|
548
|
+
// A start the target refused before it reserved a run leaves none in
|
|
549
|
+
// flight, so a lookup that states the run does not exist decides it.
|
|
550
|
+
const refused = isPermanentDispatchError({
|
|
551
|
+
status: trigger.metadata?.refusedStatus,
|
|
552
|
+
});
|
|
553
|
+
if (refused ||
|
|
554
|
+
now() - trigger.actualFireAt >= UNRESOLVED_START_SETTLE_MS) {
|
|
555
|
+
if (await recordFailedDispatch(ref, trigger, trigger.error ?? 'agent start outcome unresolved', refused ? 'dispatch-refused' : 'dispatch-unresolved', result)) {
|
|
556
|
+
result.reconciled += 1;
|
|
557
|
+
}
|
|
558
|
+
continue;
|
|
489
559
|
}
|
|
490
|
-
continue;
|
|
491
560
|
}
|
|
492
561
|
let pendingError = error;
|
|
493
562
|
if (!isRunStartPendingError(error) &&
|
|
@@ -510,18 +579,27 @@ export function createScheduleTick(options) {
|
|
|
510
579
|
streamRequestContext: ref.agentTarget.ifIdle?.streamOptions?.requestContext ?? {},
|
|
511
580
|
providerOptions: agentScheduleProviderOptions(ref.scheduleId, ref.agentTarget),
|
|
512
581
|
});
|
|
513
|
-
|
|
514
|
-
await recordResolvedDispatch(ref, trigger, {
|
|
582
|
+
if (await recordResolvedDispatch(ref, trigger, {
|
|
515
583
|
...(receipt.runId !== undefined
|
|
516
584
|
? { runId: receipt.runId }
|
|
517
585
|
: {}),
|
|
518
586
|
dispatchReceipt: receipt,
|
|
519
|
-
}, result)
|
|
587
|
+
}, result)) {
|
|
588
|
+
result.reconciled += 1;
|
|
589
|
+
}
|
|
520
590
|
continue;
|
|
521
591
|
}
|
|
522
592
|
catch (retryError) {
|
|
523
593
|
if (isPermanentDispatchError(retryError)) {
|
|
524
|
-
|
|
594
|
+
// A receipt the target stored decided the fire; refusing a later
|
|
595
|
+
// redelivery does not change that decision.
|
|
596
|
+
const stored = parseScheduleAgentDispatchReceipt(trigger.metadata?.dispatchReceipt);
|
|
597
|
+
if (stored) {
|
|
598
|
+
if (await recordResolvedDispatch(ref, trigger, { dispatchReceipt: stored }, result)) {
|
|
599
|
+
result.reconciled += 1;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
else if (await recordFailedDispatch(ref, trigger, retryError, 'dispatch-refused', result)) {
|
|
525
603
|
result.reconciled += 1;
|
|
526
604
|
}
|
|
527
605
|
continue;
|
|
@@ -564,7 +642,7 @@ export function createScheduleTick(options) {
|
|
|
564
642
|
if (ref.target === 'agent' &&
|
|
565
643
|
isPermanentDispatchError(error) &&
|
|
566
644
|
(ref.mode === 'signal' || statusOf(statusError) === 404)) {
|
|
567
|
-
await
|
|
645
|
+
await recordFailedDispatch(ref, trigger, error, 'dispatch-refused', result);
|
|
568
646
|
return;
|
|
569
647
|
}
|
|
570
648
|
result.deferred += 1;
|
|
@@ -635,12 +713,12 @@ export function createScheduleTick(options) {
|
|
|
635
713
|
}
|
|
636
714
|
catch {
|
|
637
715
|
result.failed += 1;
|
|
638
|
-
await
|
|
716
|
+
await auditOrLog({
|
|
639
717
|
scheduleId: schedule.id,
|
|
640
718
|
target: targetType,
|
|
641
719
|
outcome: 'failed',
|
|
642
720
|
reason: 'invalid-cron',
|
|
643
|
-
});
|
|
721
|
+
}, undefined);
|
|
644
722
|
return;
|
|
645
723
|
}
|
|
646
724
|
// 2. Mint the provisional runId before the CAS so lastRunId identifies this
|
|
@@ -679,21 +757,44 @@ export function createScheduleTick(options) {
|
|
|
679
757
|
});
|
|
680
758
|
if (!claimed) {
|
|
681
759
|
result.lost += 1;
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
760
|
+
try {
|
|
761
|
+
const current = await store.getSchedule(schedule.id);
|
|
762
|
+
const reason = !current
|
|
763
|
+
? 'disappeared'
|
|
764
|
+
: current.status === 'paused'
|
|
765
|
+
? 'paused'
|
|
766
|
+
: 'concurrent-claim';
|
|
767
|
+
await audit({
|
|
768
|
+
scheduleId: schedule.id,
|
|
769
|
+
target: targetType,
|
|
770
|
+
outcome: 'lost',
|
|
771
|
+
reason: `lost: ${reason}`,
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
catch (error) {
|
|
775
|
+
logBookkeepingError(schedule.id, runId, error);
|
|
776
|
+
}
|
|
695
777
|
return;
|
|
696
778
|
}
|
|
779
|
+
const recordUndispatched = (auditTarget, outcome, reason, error) => settleTrigger(result, () => store.recordTrigger({
|
|
780
|
+
...claimTrigger,
|
|
781
|
+
runId: null,
|
|
782
|
+
outcome,
|
|
783
|
+
error,
|
|
784
|
+
metadata: triggerMetadata({ reason }),
|
|
785
|
+
}), outcome, { scheduleId: schedule.id, target: auditTarget, outcome, reason }, runId);
|
|
786
|
+
const recordPublished = (auditTarget, dispatchedRunId) => settleTrigger(result, () => store.recordTrigger({
|
|
787
|
+
...claimTrigger,
|
|
788
|
+
runId: dispatchedRunId,
|
|
789
|
+
outcome: 'published',
|
|
790
|
+
error: undefined,
|
|
791
|
+
metadata: triggerMetadata(),
|
|
792
|
+
}), 'fired', {
|
|
793
|
+
scheduleId: schedule.id,
|
|
794
|
+
target: auditTarget,
|
|
795
|
+
outcome: 'published',
|
|
796
|
+
runId: dispatchedRunId,
|
|
797
|
+
}, dispatchedRunId);
|
|
697
798
|
// 4. We own this fire — nextFireAt is already advanced (consumed, never
|
|
698
799
|
// hot-looped), whatever happens below.
|
|
699
800
|
let targetDecision;
|
|
@@ -705,97 +806,18 @@ export function createScheduleTick(options) {
|
|
|
705
806
|
}
|
|
706
807
|
if (!targetDecision?.allowed) {
|
|
707
808
|
const reason = targetDecision?.reason ?? 'invalid-creator-role';
|
|
708
|
-
|
|
709
|
-
await store.recordTrigger({
|
|
710
|
-
...claimTrigger,
|
|
711
|
-
runId: null,
|
|
712
|
-
outcome: 'failed',
|
|
713
|
-
error: reason,
|
|
714
|
-
metadata: triggerMetadata({ reason }),
|
|
715
|
-
});
|
|
716
|
-
await audit({
|
|
717
|
-
scheduleId: schedule.id,
|
|
718
|
-
target: targetType,
|
|
719
|
-
outcome: 'failed',
|
|
720
|
-
reason,
|
|
721
|
-
});
|
|
809
|
+
await recordUndispatched(targetType, 'failed', reason, reason);
|
|
722
810
|
return;
|
|
723
811
|
}
|
|
724
812
|
const deepField = deepScheduleTargetField(schedule.target);
|
|
725
813
|
if (deepField !== undefined) {
|
|
726
|
-
|
|
727
|
-
await store.recordTrigger({
|
|
728
|
-
...claimTrigger,
|
|
729
|
-
runId: null,
|
|
730
|
-
outcome: 'failed',
|
|
731
|
-
error: runInputDepthMessage(deepField),
|
|
732
|
-
metadata: triggerMetadata({ reason: 'input-too-deep' }),
|
|
733
|
-
});
|
|
734
|
-
await audit({
|
|
735
|
-
scheduleId: schedule.id,
|
|
736
|
-
target: targetType,
|
|
737
|
-
outcome: 'failed',
|
|
738
|
-
reason: 'input-too-deep',
|
|
739
|
-
});
|
|
814
|
+
await recordUndispatched(targetType, 'failed', 'input-too-deep', runInputDepthMessage(deepField));
|
|
740
815
|
return;
|
|
741
816
|
}
|
|
742
817
|
if (targetType === 'agent') {
|
|
743
818
|
const target = normalizeAndSanitizeAgentTarget(schedule.id, schedule.target);
|
|
744
819
|
if (!target) {
|
|
745
|
-
|
|
746
|
-
await store.recordTrigger({
|
|
747
|
-
...claimTrigger,
|
|
748
|
-
runId: null,
|
|
749
|
-
outcome: 'failed',
|
|
750
|
-
error: 'invalid agent target',
|
|
751
|
-
metadata: triggerMetadata({ reason: 'invalid-agent-target' }),
|
|
752
|
-
});
|
|
753
|
-
await audit({
|
|
754
|
-
scheduleId: schedule.id,
|
|
755
|
-
target: 'agent',
|
|
756
|
-
outcome: 'failed',
|
|
757
|
-
reason: 'invalid-agent-target',
|
|
758
|
-
});
|
|
759
|
-
return;
|
|
760
|
-
}
|
|
761
|
-
if ((target.threadId !== undefined && !isPathSafeId(target.threadId)) ||
|
|
762
|
-
(target.resourceId !== undefined && !isPathSafeId(target.resourceId)) ||
|
|
763
|
-
(target.threadId !== undefined && target.resourceId === undefined)) {
|
|
764
|
-
result.failed += 1;
|
|
765
|
-
await store.recordTrigger({
|
|
766
|
-
...claimTrigger,
|
|
767
|
-
runId: null,
|
|
768
|
-
outcome: 'failed',
|
|
769
|
-
error: 'invalid memory id',
|
|
770
|
-
metadata: triggerMetadata({ reason: 'invalid-memory-id' }),
|
|
771
|
-
});
|
|
772
|
-
await audit({
|
|
773
|
-
scheduleId: schedule.id,
|
|
774
|
-
target: 'agent',
|
|
775
|
-
outcome: 'failed',
|
|
776
|
-
reason: 'invalid-memory-id',
|
|
777
|
-
});
|
|
778
|
-
return;
|
|
779
|
-
}
|
|
780
|
-
if (target.threadId === undefined &&
|
|
781
|
-
(target.resourceId !== undefined ||
|
|
782
|
-
target.signalType !== undefined ||
|
|
783
|
-
target.ifActive !== undefined ||
|
|
784
|
-
target.ifIdle !== undefined)) {
|
|
785
|
-
result.failed += 1;
|
|
786
|
-
await store.recordTrigger({
|
|
787
|
-
...claimTrigger,
|
|
788
|
-
runId: null,
|
|
789
|
-
outcome: 'failed',
|
|
790
|
-
error: 'invalid agent target',
|
|
791
|
-
metadata: triggerMetadata({ reason: 'invalid-agent-target' }),
|
|
792
|
-
});
|
|
793
|
-
await audit({
|
|
794
|
-
scheduleId: schedule.id,
|
|
795
|
-
target: 'agent',
|
|
796
|
-
outcome: 'failed',
|
|
797
|
-
reason: 'invalid-agent-target',
|
|
798
|
-
});
|
|
820
|
+
await recordUndispatched('agent', 'failed', 'invalid-agent-target', 'invalid agent target');
|
|
799
821
|
return;
|
|
800
822
|
}
|
|
801
823
|
const firedRunId = runId;
|
|
@@ -804,20 +826,7 @@ export function createScheduleTick(options) {
|
|
|
804
826
|
const providerOptions = agentScheduleProviderOptions(schedule.id, target);
|
|
805
827
|
if (threaded) {
|
|
806
828
|
if (!options.signalAgent) {
|
|
807
|
-
|
|
808
|
-
await store.recordTrigger({
|
|
809
|
-
...claimTrigger,
|
|
810
|
-
runId: null,
|
|
811
|
-
outcome: 'skipped',
|
|
812
|
-
error: undefined,
|
|
813
|
-
metadata: triggerMetadata({ reason: 'agent-target-unsupported' }),
|
|
814
|
-
});
|
|
815
|
-
await audit({
|
|
816
|
-
scheduleId: schedule.id,
|
|
817
|
-
target: 'agent',
|
|
818
|
-
outcome: 'skipped',
|
|
819
|
-
reason: 'agent-target-unsupported',
|
|
820
|
-
});
|
|
829
|
+
await recordUndispatched('agent', 'skipped', 'agent-target-unsupported');
|
|
821
830
|
return;
|
|
822
831
|
}
|
|
823
832
|
const dispatch = {
|
|
@@ -858,72 +867,32 @@ export function createScheduleTick(options) {
|
|
|
858
867
|
}, error, result);
|
|
859
868
|
return;
|
|
860
869
|
}
|
|
861
|
-
|
|
862
|
-
receipt.outcome === 'discarded' ||
|
|
863
|
-
receipt.outcome === 'skipped') {
|
|
864
|
-
result.skipped += 1;
|
|
865
|
-
}
|
|
866
|
-
else {
|
|
867
|
-
result.fired += 1;
|
|
868
|
-
}
|
|
869
|
-
try {
|
|
870
|
-
await store.recordTrigger({
|
|
871
|
-
...claimTrigger,
|
|
872
|
-
runId: receipt.runId ?? null,
|
|
873
|
-
outcome: receipt.outcome,
|
|
874
|
-
error: undefined,
|
|
875
|
-
metadata: triggerMetadata({
|
|
876
|
-
action: receipt.action,
|
|
877
|
-
...(receipt.signalId !== undefined
|
|
878
|
-
? { signalId: receipt.signalId }
|
|
879
|
-
: {}),
|
|
880
|
-
}),
|
|
881
|
-
});
|
|
882
|
-
await audit({
|
|
883
|
-
scheduleId: schedule.id,
|
|
884
|
-
target: 'agent',
|
|
885
|
-
outcome: receipt.outcome,
|
|
886
|
-
...(receipt.runId !== undefined ? { runId: receipt.runId } : {}),
|
|
887
|
-
});
|
|
888
|
-
}
|
|
889
|
-
catch (error) {
|
|
890
|
-
logBookkeepingError(schedule.id, receipt.runId ?? firedRunId, error);
|
|
891
|
-
}
|
|
892
|
-
return;
|
|
893
|
-
}
|
|
894
|
-
if (!options.startAgent) {
|
|
895
|
-
result.skipped += 1;
|
|
896
|
-
await store.recordTrigger({
|
|
870
|
+
await settleTrigger(result, () => store.recordTrigger({
|
|
897
871
|
...claimTrigger,
|
|
898
|
-
runId: null,
|
|
899
|
-
outcome:
|
|
872
|
+
runId: receipt.runId ?? null,
|
|
873
|
+
outcome: receipt.outcome,
|
|
900
874
|
error: undefined,
|
|
901
|
-
metadata: triggerMetadata({
|
|
902
|
-
|
|
903
|
-
|
|
875
|
+
metadata: triggerMetadata({
|
|
876
|
+
action: receipt.action,
|
|
877
|
+
...(receipt.signalId !== undefined
|
|
878
|
+
? { signalId: receipt.signalId }
|
|
879
|
+
: {}),
|
|
880
|
+
}),
|
|
881
|
+
}), outcomeCounter(receipt.outcome), {
|
|
904
882
|
scheduleId: schedule.id,
|
|
905
883
|
target: 'agent',
|
|
906
|
-
outcome:
|
|
907
|
-
|
|
908
|
-
});
|
|
884
|
+
outcome: receipt.outcome,
|
|
885
|
+
...(receipt.runId !== undefined ? { runId: receipt.runId } : {}),
|
|
886
|
+
}, receipt.runId ?? firedRunId);
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
if (!options.startAgent) {
|
|
890
|
+
await recordUndispatched('agent', 'skipped', 'agent-target-unsupported');
|
|
909
891
|
return;
|
|
910
892
|
}
|
|
911
893
|
const allowed = options.runCap ? await options.runCap() : true;
|
|
912
894
|
if (!allowed) {
|
|
913
|
-
|
|
914
|
-
await store.recordTrigger({
|
|
915
|
-
...claimTrigger,
|
|
916
|
-
runId: null,
|
|
917
|
-
outcome: 'skipped',
|
|
918
|
-
error: undefined,
|
|
919
|
-
metadata: triggerMetadata({ reason: 'run-capped' }),
|
|
920
|
-
});
|
|
921
|
-
await audit({
|
|
922
|
-
scheduleId: schedule.id,
|
|
923
|
-
target: 'agent',
|
|
924
|
-
outcome: 'skipped',
|
|
925
|
-
reason: 'run-capped',
|
|
926
|
-
});
|
|
895
|
+
await recordUndispatched('agent', 'skipped', 'run-capped');
|
|
927
896
|
return;
|
|
928
897
|
}
|
|
929
898
|
const dispatch = {
|
|
@@ -964,64 +933,19 @@ export function createScheduleTick(options) {
|
|
|
964
933
|
}, error, result);
|
|
965
934
|
return;
|
|
966
935
|
}
|
|
967
|
-
|
|
968
|
-
result.fired += 1;
|
|
969
|
-
try {
|
|
970
|
-
await store.recordTrigger({
|
|
971
|
-
...claimTrigger,
|
|
972
|
-
runId: dispatchedRunId,
|
|
973
|
-
outcome: 'published',
|
|
974
|
-
error: undefined,
|
|
975
|
-
metadata: triggerMetadata(),
|
|
976
|
-
});
|
|
977
|
-
await audit({
|
|
978
|
-
scheduleId: schedule.id,
|
|
979
|
-
target: 'agent',
|
|
980
|
-
outcome: 'published',
|
|
981
|
-
runId: dispatchedRunId,
|
|
982
|
-
});
|
|
983
|
-
}
|
|
984
|
-
catch (error) {
|
|
985
|
-
logBookkeepingError(schedule.id, dispatchedRunId, error);
|
|
986
|
-
}
|
|
936
|
+
await recordPublished('agent', summary.runId ?? firedRunId);
|
|
987
937
|
return;
|
|
988
938
|
}
|
|
989
939
|
const target = normalizeAndSanitizeWorkflowTarget(schedule.target);
|
|
990
940
|
if (!target) {
|
|
991
|
-
|
|
992
|
-
await store.recordTrigger({
|
|
993
|
-
...claimTrigger,
|
|
994
|
-
runId: null,
|
|
995
|
-
outcome: 'failed',
|
|
996
|
-
error: 'invalid workflow target',
|
|
997
|
-
metadata: triggerMetadata({ reason: 'invalid-workflow-target' }),
|
|
998
|
-
});
|
|
999
|
-
await audit({
|
|
1000
|
-
scheduleId: schedule.id,
|
|
1001
|
-
target: 'workflow',
|
|
1002
|
-
outcome: 'failed',
|
|
1003
|
-
reason: 'invalid-workflow-target',
|
|
1004
|
-
});
|
|
941
|
+
await recordUndispatched('workflow', 'failed', 'invalid-workflow-target', 'invalid workflow target');
|
|
1005
942
|
return;
|
|
1006
943
|
}
|
|
1007
944
|
// 5. Workflow target: consult the run cap.
|
|
1008
945
|
const allowed = options.runCap ? await options.runCap() : true;
|
|
1009
946
|
if (!allowed) {
|
|
1010
947
|
// Capped: the schedule stays healthy (already advanced) and is audited.
|
|
1011
|
-
|
|
1012
|
-
await store.recordTrigger({
|
|
1013
|
-
...claimTrigger,
|
|
1014
|
-
runId: null,
|
|
1015
|
-
outcome: 'skipped',
|
|
1016
|
-
error: undefined,
|
|
1017
|
-
metadata: triggerMetadata({ reason: 'run-capped' }),
|
|
1018
|
-
});
|
|
1019
|
-
await audit({
|
|
1020
|
-
scheduleId: schedule.id,
|
|
1021
|
-
target: 'workflow',
|
|
1022
|
-
outcome: 'skipped',
|
|
1023
|
-
reason: 'run-capped',
|
|
1024
|
-
});
|
|
948
|
+
await recordUndispatched('workflow', 'skipped', 'run-capped');
|
|
1025
949
|
return;
|
|
1026
950
|
}
|
|
1027
951
|
// 7. Fire the workflow run (runId is defined by construction here).
|
|
@@ -1063,26 +987,7 @@ export function createScheduleTick(options) {
|
|
|
1063
987
|
// The run WAS dispatched. Record it as `published` (write-once at dispatch,
|
|
1064
988
|
// per core's ScheduleTriggerOutcome doc). Bookkeeping failures are logged
|
|
1065
989
|
// locally and do not reclassify the successful dispatch as a start failure.
|
|
1066
|
-
|
|
1067
|
-
result.fired += 1;
|
|
1068
|
-
try {
|
|
1069
|
-
await store.recordTrigger({
|
|
1070
|
-
...claimTrigger,
|
|
1071
|
-
runId: dispatchedRunId,
|
|
1072
|
-
outcome: 'published',
|
|
1073
|
-
error: undefined,
|
|
1074
|
-
metadata: triggerMetadata(),
|
|
1075
|
-
});
|
|
1076
|
-
await audit({
|
|
1077
|
-
scheduleId: schedule.id,
|
|
1078
|
-
target: 'workflow',
|
|
1079
|
-
outcome: 'published',
|
|
1080
|
-
runId: dispatchedRunId,
|
|
1081
|
-
});
|
|
1082
|
-
}
|
|
1083
|
-
catch (error) {
|
|
1084
|
-
logBookkeepingError(schedule.id, dispatchedRunId, error);
|
|
1085
|
-
}
|
|
990
|
+
await recordPublished('workflow', summary.runId ?? firedRunId);
|
|
1086
991
|
};
|
|
1087
992
|
const idlePass = () => ({
|
|
1088
993
|
due: 0,
|