@kb-labs/workflow-engine 2.94.0 → 2.98.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/dist/index.d.ts +67 -143
- package/dist/index.js +336 -313
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { WorkflowSpec, RunTrigger, IdempotencyKey, ConcurrencyGroup, WorkflowRun, JobRun, StepRun, RetryPolicy, ArtifactMergeConfig } from '@kb-labs/workflow-contracts';
|
|
2
|
-
import { ILogger, ICache, IEventBus, IAnalytics,
|
|
2
|
+
import { ILogger, ICache, IEventBus, IAnalytics, ISnapshotManager, Unsubscribe, IJobScheduler, JobDefinition, JobHandle, CronExpression, JobFilter } from '@kb-labs/core-platform';
|
|
3
3
|
import { JobPriority, WorkflowEventName } from '@kb-labs/workflow-constants';
|
|
4
4
|
import { ArtifactClient } from '@kb-labs/workflow-artifacts';
|
|
5
5
|
import { IEntityRegistry } from '@kb-labs/core-registry';
|
|
6
|
-
import { PlatformServices, JobHandlerDecl, PluginContextDescriptor } from '@kb-labs/plugin-contracts';
|
|
6
|
+
import { PlatformServices, JobHandlerDecl, WorkflowHandlerDecl, CronDecl, PluginContextDescriptor } from '@kb-labs/plugin-contracts';
|
|
7
7
|
import { IExecutionBackend } from '@kb-labs/core-contracts';
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -182,16 +182,6 @@ declare class RunSnapshotStorage {
|
|
|
182
182
|
deleteSnapshot(runId: string): Promise<void>;
|
|
183
183
|
}
|
|
184
184
|
|
|
185
|
-
interface SnapshotManagerClient {
|
|
186
|
-
restoreSnapshot(request: {
|
|
187
|
-
snapshotId: string;
|
|
188
|
-
workspaceId?: string;
|
|
189
|
-
environmentId?: string;
|
|
190
|
-
targetPath?: string;
|
|
191
|
-
overwrite?: boolean;
|
|
192
|
-
metadata?: Record<string, unknown>;
|
|
193
|
-
}): Promise<unknown>;
|
|
194
|
-
}
|
|
195
185
|
interface WorkflowEngineOptions {
|
|
196
186
|
scheduler?: SchedulerOptions;
|
|
197
187
|
concurrency?: AcquireOptions;
|
|
@@ -206,7 +196,7 @@ interface WorkflowEngineOptions {
|
|
|
206
196
|
/** Platform analytics adapter (OPTIONAL) */
|
|
207
197
|
analytics?: IAnalytics;
|
|
208
198
|
/** Platform snapshot manager (OPTIONAL - for infra snapshot restore in replay) */
|
|
209
|
-
snapshotManager?:
|
|
199
|
+
snapshotManager?: ISnapshotManager;
|
|
210
200
|
/** Workspace root (monorepo root) - used for plugin execution context */
|
|
211
201
|
workspaceRoot?: string;
|
|
212
202
|
}
|
|
@@ -240,6 +230,12 @@ declare class WorkflowEngine {
|
|
|
240
230
|
* Implements exponential/linear backoff retry logic.
|
|
241
231
|
*/
|
|
242
232
|
markJobFailed(runId: string, jobId: string, error: Error, shouldRetry?: boolean): Promise<void>;
|
|
233
|
+
/**
|
|
234
|
+
* Recursively cancel jobs blocked on a failed/cancelled upstream job, so the
|
|
235
|
+
* DAG does not leave dependents stuck in 'queued'. Cancellation cascades:
|
|
236
|
+
* a cancelled job also cancels its own dependents.
|
|
237
|
+
*/
|
|
238
|
+
private cancelDownstreamJobs;
|
|
243
239
|
/**
|
|
244
240
|
* Mark job as interrupted (e.g., during graceful shutdown).
|
|
245
241
|
* Interrupted jobs will be retried on next daemon startup.
|
|
@@ -253,6 +249,12 @@ declare class WorkflowEngine {
|
|
|
253
249
|
* Mark job as completed successfully.
|
|
254
250
|
*/
|
|
255
251
|
markJobCompleted(runId: string, jobId: string): Promise<void>;
|
|
252
|
+
/** Build a minimal ExpressionContext from run state (for job-level if evaluation). */
|
|
253
|
+
private buildExpressionContext;
|
|
254
|
+
/** Evaluate a job-level `if:` expression. Strips ${{ }} wrapper if present. */
|
|
255
|
+
private evaluateJobIf;
|
|
256
|
+
/** Mark a job as skipped (success) and release any jobs blocked on it. */
|
|
257
|
+
private skipJob;
|
|
256
258
|
/**
|
|
257
259
|
* Check if all jobs in a run are completed and update run status accordingly.
|
|
258
260
|
*/
|
|
@@ -309,9 +311,27 @@ declare class WorkflowEngine {
|
|
|
309
311
|
/**
|
|
310
312
|
* Move permanently failed job to Dead Letter Queue.
|
|
311
313
|
*/
|
|
312
|
-
|
|
314
|
+
/**
|
|
315
|
+
* Record a dead-letter entry for a job that exhausted its retries. This is an
|
|
316
|
+
* observability artifact only — it does NOT change the run status. The run
|
|
317
|
+
* outcome is decided by checkRunCompletion (→ 'failed'). 'dlq' as a run status
|
|
318
|
+
* is reserved for infrastructure failures and is no longer set here (B-029).
|
|
319
|
+
*/
|
|
320
|
+
private deadLetterJob;
|
|
313
321
|
updateRun(runId: string, mutator: (run: WorkflowRun) => WorkflowRun | void): Promise<WorkflowRun | null>;
|
|
314
322
|
finalizeRun(runId: string, status: WorkflowRun['status'], context?: Partial<RunContext>): Promise<WorkflowRun | null>;
|
|
323
|
+
/**
|
|
324
|
+
* Persist a snapshot for a run that just reached a terminal state, so it
|
|
325
|
+
* can be replayed from any step later (`replayRun`, used by both
|
|
326
|
+
* `workflow runs restart --from-step` and `rerun --failed-only`). Only
|
|
327
|
+
* 'success'/'failed' carry meaningful step output data worth snapshotting.
|
|
328
|
+
*
|
|
329
|
+
* Shared by finalizeRun() and checkRunCompletion() — checkRunCompletion is
|
|
330
|
+
* the run-completion path actually driven by the worker in production, so
|
|
331
|
+
* without this call here, no snapshot is ever created outside of tests
|
|
332
|
+
* that call finalizeRun() directly (issue #263).
|
|
333
|
+
*/
|
|
334
|
+
private snapshotTerminalRun;
|
|
315
335
|
nextJob(): Promise<JobQueueEntry | null>;
|
|
316
336
|
rescheduleJob(entry: JobQueueEntry, delayMs: number): Promise<void>;
|
|
317
337
|
publishRunEvent(type: WorkflowEventName, run: WorkflowRun): Promise<void>;
|
|
@@ -325,7 +345,7 @@ declare class WorkflowEngine {
|
|
|
325
345
|
lineNo: number;
|
|
326
346
|
timestamp: string;
|
|
327
347
|
meta?: Record<string, unknown>;
|
|
328
|
-
}): Promise<void>;
|
|
348
|
+
}, stepName?: string): Promise<void>;
|
|
329
349
|
/**
|
|
330
350
|
* Create a snapshot of the current run state
|
|
331
351
|
*/
|
|
@@ -464,6 +484,8 @@ interface WorkflowStats {
|
|
|
464
484
|
*/
|
|
465
485
|
interface WorkflowRuntime {
|
|
466
486
|
id: string;
|
|
487
|
+
/** Discriminator for workflow kind. Enables type-safe routing in consumers. */
|
|
488
|
+
kind?: 'workflow' | 'job' | 'cron';
|
|
467
489
|
source: 'manifest' | 'standalone' | 'plugin';
|
|
468
490
|
pluginId?: string;
|
|
469
491
|
manifestPath?: string;
|
|
@@ -484,6 +506,10 @@ interface WorkflowRuntime {
|
|
|
484
506
|
required?: boolean;
|
|
485
507
|
default?: unknown;
|
|
486
508
|
}>;
|
|
509
|
+
/** Semantic version from workflow spec (e.g. "6.6.0") */
|
|
510
|
+
version?: string;
|
|
511
|
+
/** ISO timestamp of last modification */
|
|
512
|
+
updatedAt?: string;
|
|
487
513
|
}
|
|
488
514
|
/**
|
|
489
515
|
* Options for ManifestScanner
|
|
@@ -505,25 +531,16 @@ declare class ManifestScanner {
|
|
|
505
531
|
private readonly cliApi;
|
|
506
532
|
private readonly platform;
|
|
507
533
|
private readonly cacheTtlMs;
|
|
534
|
+
private readonly converter;
|
|
508
535
|
constructor(options: ManifestScannerOptions);
|
|
509
536
|
/**
|
|
510
537
|
* Scan all installed plugins for workflows and jobs.
|
|
538
|
+
* Purpose: listing — populates the REST API GET /api/v1/workflows catalog.
|
|
539
|
+
* (CronDiscovery in daemon handles scheduling — different output, different consumer.)
|
|
511
540
|
*
|
|
512
541
|
* Returns unified WorkflowRuntime representations.
|
|
513
542
|
*/
|
|
514
543
|
scanPlugins(): Promise<WorkflowRuntime[]>;
|
|
515
|
-
/**
|
|
516
|
-
* Convert workflow handler declaration to WorkflowRuntime.
|
|
517
|
-
*/
|
|
518
|
-
private convertWorkflowHandler;
|
|
519
|
-
/**
|
|
520
|
-
* Convert job handler declaration to WorkflowRuntime.
|
|
521
|
-
*/
|
|
522
|
-
private convertJobHandler;
|
|
523
|
-
/**
|
|
524
|
-
* Convert cron schedule declaration to WorkflowRuntime.
|
|
525
|
-
*/
|
|
526
|
-
private convertCronSchedule;
|
|
527
544
|
/**
|
|
528
545
|
* Scan all installed plugins for job handlers only.
|
|
529
546
|
*
|
|
@@ -548,6 +565,21 @@ declare class ManifestScanner {
|
|
|
548
565
|
watchPlugins(callback?: (workflows: WorkflowRuntime[]) => void): () => void;
|
|
549
566
|
}
|
|
550
567
|
|
|
568
|
+
/**
|
|
569
|
+
* @module @kb-labs/workflow-engine/manifest-converter
|
|
570
|
+
*
|
|
571
|
+
* Pure conversion utilities for plugin manifest declarations → WorkflowRuntime.
|
|
572
|
+
* No I/O, no caching, no platform dependency — testable in isolation.
|
|
573
|
+
*
|
|
574
|
+
* Used by ManifestScanner which handles fetch + cache responsibilities.
|
|
575
|
+
*/
|
|
576
|
+
|
|
577
|
+
declare class ManifestConverter {
|
|
578
|
+
convertWorkflowHandler(pluginId: string, handler: WorkflowHandlerDecl, pluginRoot: string): WorkflowRuntime;
|
|
579
|
+
convertJobHandler(pluginId: string, handler: JobHandlerDecl, pluginRoot: string): WorkflowRuntime;
|
|
580
|
+
convertCronSchedule(pluginId: string, cronDecl: CronDecl, pluginRoot: string): WorkflowRuntime;
|
|
581
|
+
}
|
|
582
|
+
|
|
551
583
|
/**
|
|
552
584
|
* @module @kb-labs/workflow-engine/workflow-repository
|
|
553
585
|
*
|
|
@@ -733,6 +765,13 @@ declare class WorkflowService {
|
|
|
733
765
|
listAll(options?: WorkflowServiceListOptions): Promise<WorkflowRuntime[]>;
|
|
734
766
|
/**
|
|
735
767
|
* Get workflow by ID (from either source).
|
|
768
|
+
*
|
|
769
|
+
* Lookup order:
|
|
770
|
+
* 1. Standalone by filename-derived id (canonical, fast path)
|
|
771
|
+
* 2. Standalone by `name:` field — allows `--workflow-id=dev-cycle` even
|
|
772
|
+
* when the file is named `03-dev-cycle.yml` (F8 ergonomics fix)
|
|
773
|
+
* 3. Manifest-based by id
|
|
774
|
+
* 4. Manifest-based by name field
|
|
736
775
|
*/
|
|
737
776
|
get(id: string): Promise<WorkflowRuntime | null>;
|
|
738
777
|
/**
|
|
@@ -780,121 +819,6 @@ declare class WorkflowService {
|
|
|
780
819
|
refreshManifests(): Promise<void>;
|
|
781
820
|
}
|
|
782
821
|
|
|
783
|
-
/**
|
|
784
|
-
* @module @kb-labs/workflow-engine/workflow-schedule-manager
|
|
785
|
-
*
|
|
786
|
-
* Manages scheduled workflow execution via CronManager.
|
|
787
|
-
*
|
|
788
|
-
* ## Features
|
|
789
|
-
* - Registers scheduled workflows with CronManager
|
|
790
|
-
* - Executes workflows on cron triggers
|
|
791
|
-
* - Tracks next/last run times
|
|
792
|
-
* - Supports both manifest-based jobs and standalone workflows
|
|
793
|
-
*
|
|
794
|
-
* ## Usage
|
|
795
|
-
* ```typescript
|
|
796
|
-
* const scheduleManager = new WorkflowScheduleManager({
|
|
797
|
-
* cronManager,
|
|
798
|
-
* workflowService,
|
|
799
|
-
* executor,
|
|
800
|
-
* platform,
|
|
801
|
-
* });
|
|
802
|
-
*
|
|
803
|
-
* await scheduleManager.registerAll();
|
|
804
|
-
* ```
|
|
805
|
-
*/
|
|
806
|
-
|
|
807
|
-
/**
|
|
808
|
-
* Workflow executor interface.
|
|
809
|
-
* Executes workflows (will be implemented by workflow engine).
|
|
810
|
-
*/
|
|
811
|
-
interface WorkflowExecutor {
|
|
812
|
-
/**
|
|
813
|
-
* Execute workflow by ID.
|
|
814
|
-
*/
|
|
815
|
-
execute(request: {
|
|
816
|
-
workflowId: string;
|
|
817
|
-
trigger: 'manual' | 'schedule' | 'webhook' | 'push';
|
|
818
|
-
input?: Record<string, unknown>;
|
|
819
|
-
}): Promise<{
|
|
820
|
-
runId: string;
|
|
821
|
-
}>;
|
|
822
|
-
}
|
|
823
|
-
/**
|
|
824
|
-
* Options for WorkflowScheduleManager
|
|
825
|
-
*/
|
|
826
|
-
interface WorkflowScheduleManagerOptions {
|
|
827
|
-
/** CronManager instance */
|
|
828
|
-
cronManager: ICronManager;
|
|
829
|
-
/** WorkflowService for discovering workflows */
|
|
830
|
-
workflowService: WorkflowService;
|
|
831
|
-
/** Workflow executor */
|
|
832
|
-
executor: WorkflowExecutor;
|
|
833
|
-
/** Platform services */
|
|
834
|
-
platform: PlatformServices;
|
|
835
|
-
}
|
|
836
|
-
/**
|
|
837
|
-
* Workflow Schedule Manager
|
|
838
|
-
*
|
|
839
|
-
* Integrates workflows with CronManager for scheduled execution.
|
|
840
|
-
*/
|
|
841
|
-
declare class WorkflowScheduleManager {
|
|
842
|
-
private readonly cronManager;
|
|
843
|
-
private readonly workflowService;
|
|
844
|
-
private readonly executor;
|
|
845
|
-
private readonly platform;
|
|
846
|
-
constructor(options: WorkflowScheduleManagerOptions);
|
|
847
|
-
/**
|
|
848
|
-
* Register all scheduled workflows with CronManager.
|
|
849
|
-
*
|
|
850
|
-
* Scans both manifest-based jobs and standalone workflows with schedules.
|
|
851
|
-
*/
|
|
852
|
-
registerAll(): Promise<void>;
|
|
853
|
-
/**
|
|
854
|
-
* Register single workflow schedule.
|
|
855
|
-
*/
|
|
856
|
-
register(workflow: WorkflowRuntime): Promise<void>;
|
|
857
|
-
/**
|
|
858
|
-
* Unregister workflow schedule.
|
|
859
|
-
*/
|
|
860
|
-
unregister(workflowId: string): Promise<void>;
|
|
861
|
-
/**
|
|
862
|
-
* Re-register all schedules (refresh).
|
|
863
|
-
*
|
|
864
|
-
* Useful after workflow changes or service restart.
|
|
865
|
-
*/
|
|
866
|
-
refresh(): Promise<void>;
|
|
867
|
-
/**
|
|
868
|
-
* Get next run time for scheduled workflow.
|
|
869
|
-
*/
|
|
870
|
-
getNextRun(workflowId: string): Date | null;
|
|
871
|
-
/**
|
|
872
|
-
* Get last run time for scheduled workflow.
|
|
873
|
-
*/
|
|
874
|
-
getLastRun(workflowId: string): Date | null;
|
|
875
|
-
/**
|
|
876
|
-
* Pause scheduled workflow.
|
|
877
|
-
*/
|
|
878
|
-
pause(workflowId: string): void;
|
|
879
|
-
/**
|
|
880
|
-
* Resume paused workflow schedule.
|
|
881
|
-
*/
|
|
882
|
-
resume(workflowId: string): void;
|
|
883
|
-
/**
|
|
884
|
-
* List all scheduled workflows.
|
|
885
|
-
*/
|
|
886
|
-
listScheduled(): Array<{
|
|
887
|
-
workflowId: string;
|
|
888
|
-
schedule: string;
|
|
889
|
-
status: 'active' | 'paused';
|
|
890
|
-
lastRun?: Date;
|
|
891
|
-
nextRun?: Date;
|
|
892
|
-
runCount: number;
|
|
893
|
-
}>;
|
|
894
|
-
private getCronId;
|
|
895
|
-
private getWorkflowId;
|
|
896
|
-
}
|
|
897
|
-
|
|
898
822
|
interface WorkflowRegistryEntry {
|
|
899
823
|
id: string;
|
|
900
824
|
name: string;
|
|
@@ -1056,4 +980,4 @@ declare class JobManager implements IJobScheduler {
|
|
|
1056
980
|
private jobRecordToHandle;
|
|
1057
981
|
}
|
|
1058
982
|
|
|
1059
|
-
export { type AcquireOptions, ArtifactMerger, type ArtifactMergerOptions, ConcurrencyManager, type CreateRunInput, type EngineLogger, EnvSecretProvider, type EnvSecretProviderOptions, EventBusBridge, JobManager, type JobManagerConfig, type JobQueueEntry, ManifestScanner, type ManifestScannerOptions, type RetryDecision, type RunContext, RunCoordinator, type RunCoordinatorOptions, type RunSnapshot, RunSnapshotStorage, Scheduler, type SchedulerOptions, type SecretProvider, StateStore, type ValidationResult, WorkflowEngine, type WorkflowEngineOptions, type WorkflowEvent, type
|
|
983
|
+
export { type AcquireOptions, ArtifactMerger, type ArtifactMergerOptions, ConcurrencyManager, type CreateRunInput, type EngineLogger, EnvSecretProvider, type EnvSecretProviderOptions, EventBusBridge, JobManager, type JobManagerConfig, type JobQueueEntry, ManifestConverter, ManifestScanner, type ManifestScannerOptions, type RetryDecision, type RunContext, RunCoordinator, type RunCoordinatorOptions, type RunSnapshot, RunSnapshotStorage, Scheduler, type SchedulerOptions, type SecretProvider, StateStore, type ValidationResult, WorkflowEngine, type WorkflowEngineOptions, type WorkflowEvent, type WorkflowHandlerInfo, type WorkflowListOptions, WorkflowLoader, type WorkflowLoaderOptions, type WorkflowLoaderResult, WorkflowRegistry, type WorkflowRegistryEntry, type WorkflowRegistryOptions, WorkflowRepository, type WorkflowRepositoryOptions, type WorkflowRuntime, type WorkflowSchedule, WorkflowService, type WorkflowServiceListOptions, type WorkflowServiceOptions, type WorkflowStats, type WorkflowTrigger, type WorkflowTriggerType, calculateBackoff, createDefaultSecretProvider, shouldRetry };
|