bazis 0.124.1 → 0.126.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/core/agent/AgentToolExecutor.ts +1 -1
- package/core/agent/errors.ts +14 -3
- package/core/infra/connectors/codex/CodexAppServerClient.ts +1 -1
- package/core/workflow/definitions_module/model/WfVersion.model.ts +2 -1
- package/core/workflow/definitions_module/services/IWorkflowDefinitions.service.ts +2 -0
- package/core/workflow/definitions_module/services/WorkflowDefinitions.service.ts +10 -0
- package/core/workflow/runtime_module/WorkflowRuntime.module.ts +6 -1
- package/core/workflow/runtime_module/background/WorkflowWorker.ts +50 -3
- package/core/workflow/runtime_module/events/Runtime.events.ts +2 -1
- package/core/workflow/runtime_module/http/Events.controller.ts +26 -0
- package/core/workflow/runtime_module/internal/GraphCache.ts +8 -2
- package/core/workflow/runtime_module/internal/Schedules.ts +82 -0
- package/core/workflow/runtime_module/internal/TimerStep.ts +21 -0
- package/core/workflow/runtime_module/internal/ToolStep.ts +40 -4
- package/core/workflow/runtime_module/internal/WorkflowEngine.ts +187 -9
- package/core/workflow/runtime_module/model/WfHistory.model.ts +2 -2
- package/core/workflow/runtime_module/model/WfInstance.model.ts +1 -1
- package/core/workflow/runtime_module/model/WfJob.model.ts +3 -3
- package/core/workflow/runtime_module/model/WfSubscription.model.ts +2 -2
- package/core/workflow/runtime_module/model/WfToken.model.ts +1 -1
- package/core/workflow/runtime_module/services/IWorkflowRuntime.service.ts +24 -0
- package/core/workflow/runtime_module/services/WorkflowRuntime.service.ts +43 -1
- package/library/workflow/cron.ts +89 -0
- package/library/workflow/index.ts +1 -0
- package/library/workflow/nodeKinds.ts +3 -3
- package/package.json +1 -1
|
@@ -2146,7 +2146,7 @@ export class AgentToolExecutor {
|
|
|
2146
2146
|
details?: JsonObject,
|
|
2147
2147
|
): AgentToolResult {
|
|
2148
2148
|
if (error instanceof ToolError) {
|
|
2149
|
-
return this.errorResult(call, code, error.message, metadata, startedAt, details);
|
|
2149
|
+
return this.errorResult(call, code, error.message, metadata, startedAt, error.reason === undefined ? details : { ...details, reason: error.reason });
|
|
2150
2150
|
}
|
|
2151
2151
|
this.logger?.error("Agent tool failed", { agent: agentName ?? null, tool: call.name, callId: call.id, code, error: redactSensitive(error) });
|
|
2152
2152
|
return this.errorResult(call, code, fallback, metadata, startedAt, details);
|
package/core/agent/errors.ts
CHANGED
|
@@ -29,9 +29,20 @@ export class AgentToolExecutionError extends Error {
|
|
|
29
29
|
* goes to the application log with its stack.
|
|
30
30
|
*/
|
|
31
31
|
export class ToolError extends Error {
|
|
32
|
-
|
|
32
|
+
/**
|
|
33
|
+
* A business reason the caller can act on, such as `card_declined`: it comes
|
|
34
|
+
* back as `error.details.reason`. A process routes it to an error boundary
|
|
35
|
+
* and does not retry it (WORKFLOW-001).
|
|
36
|
+
*/
|
|
37
|
+
readonly reason?: string;
|
|
38
|
+
|
|
39
|
+
constructor(message: string, options: { readonly reason?: string } = {}) {
|
|
33
40
|
super(message);
|
|
34
41
|
this.name = "ToolError";
|
|
42
|
+
if (options.reason !== undefined) {
|
|
43
|
+
if (!/^[A-Za-z][A-Za-z0-9_.-]{0,63}$/.test(options.reason)) throw new TypeError("ToolError reason must match ^[A-Za-z][A-Za-z0-9_.-]{0,63}$");
|
|
44
|
+
this.reason = options.reason;
|
|
45
|
+
}
|
|
35
46
|
}
|
|
36
47
|
}
|
|
37
48
|
|
|
@@ -41,8 +52,8 @@ export class ToolError extends Error {
|
|
|
41
52
|
* for an idempotency-gated retry. Its message reaches the model like a ToolError.
|
|
42
53
|
*/
|
|
43
54
|
export class AgentToolPreCommitError extends ToolError {
|
|
44
|
-
constructor(message: string) {
|
|
45
|
-
super(message);
|
|
55
|
+
constructor(message: string, options: { readonly reason?: string } = {}) {
|
|
56
|
+
super(message, options);
|
|
46
57
|
this.name = "AgentToolPreCommitError";
|
|
47
58
|
}
|
|
48
59
|
}
|
|
@@ -232,7 +232,7 @@ export class CodexAppServerClient implements CodexClient {
|
|
|
232
232
|
if (method === "account/updated") this.modelCache = undefined;
|
|
233
233
|
}, () => { this.clearLogin(); this.modelCache = undefined; });
|
|
234
234
|
const initialized = await client.request("initialize", {
|
|
235
|
-
clientInfo: { name: "bazis", title: "Bazis", version: "0.
|
|
235
|
+
clientInfo: { name: "bazis", title: "Bazis", version: "0.126.0" }, capabilities: { experimentalApi: true },
|
|
236
236
|
}, 15_000, signal);
|
|
237
237
|
if (resolve(text(initialized.codexHome, 4096)) !== this.paths.home) throw new CodexError("PROTOCOL_ERROR");
|
|
238
238
|
client.notify("initialized");
|
|
@@ -9,7 +9,8 @@ export class WfVersion {
|
|
|
9
9
|
@UUID({ version: "v7" })
|
|
10
10
|
id = "";
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
/** A reference: the foreign key makes the column a native uuid. A non-key `type: "uuid"` would generate a value. */
|
|
13
|
+
@Column({ type: "text" })
|
|
13
14
|
processId = "";
|
|
14
15
|
|
|
15
16
|
@Column({ type: "integer" })
|
|
@@ -70,6 +70,8 @@ export abstract class IWorkflowDefinitions {
|
|
|
70
70
|
abstract version(key: string, version: number): Promise<VersionDetails>;
|
|
71
71
|
/** The latest published version of a process that is not archived. */
|
|
72
72
|
abstract latest(key: string): Promise<VersionDetails>;
|
|
73
|
+
/** The latest version of every process that is not archived: where event and scheduled starts come from. */
|
|
74
|
+
abstract published(): Promise<readonly VersionDetails[]>;
|
|
73
75
|
/** A version by id, for the engine; `undefined` if there is none. */
|
|
74
76
|
abstract versionById(id: string): Promise<VersionDetails | undefined>;
|
|
75
77
|
/** Processes with a retention period: what the retention service deletes. */
|
|
@@ -141,6 +141,16 @@ export class WorkflowDefinitions extends IWorkflowDefinitions {
|
|
|
141
141
|
return await this.version(key, row.latestVersion);
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
async published(): Promise<readonly VersionDetails[]> {
|
|
145
|
+
const processes = await this.db.processes.asNoTracking().where((p) => p.archivedAt.isNull().and(p.latestVersion.isNotNull())).toList();
|
|
146
|
+
if (processes.length === 0) return [];
|
|
147
|
+
const byId = new Map(processes.map((process) => [process.id, process]));
|
|
148
|
+
const versions = await this.db.versions.asNoTracking().where((v) => v.processId.in([...byId.keys()])).toList();
|
|
149
|
+
return versions
|
|
150
|
+
.filter((version) => byId.get(version.processId)?.latestVersion === version.version)
|
|
151
|
+
.map((version) => versionOf(byId.get(version.processId)!.key, version));
|
|
152
|
+
}
|
|
153
|
+
|
|
144
154
|
async versionById(id: string): Promise<VersionDetails | undefined> {
|
|
145
155
|
if (!isUuid(id)) return undefined;
|
|
146
156
|
const version = await this.db.versions.asNoTracking().firstOrDefault((v) => v.id.eq(id));
|
|
@@ -7,8 +7,11 @@ import { WorkflowTools } from "../internal/WorkflowTools";
|
|
|
7
7
|
import { WorkflowRetention } from "./background/WorkflowRetention";
|
|
8
8
|
import { WorkflowWorker } from "./background/WorkflowWorker";
|
|
9
9
|
import { workflowRuntimeConfig } from "./config/Runtime.config";
|
|
10
|
+
import { EventsController } from "./http/Events.controller";
|
|
10
11
|
import { InstancesController } from "./http/Instances.controller";
|
|
11
12
|
import { GraphCache } from "./internal/GraphCache";
|
|
13
|
+
import { Schedules } from "./internal/Schedules";
|
|
14
|
+
import { TimerStep } from "./internal/TimerStep";
|
|
12
15
|
import { ToolStep } from "./internal/ToolStep";
|
|
13
16
|
import { WorkflowEngine } from "./internal/WorkflowEngine";
|
|
14
17
|
import { RUNTIME_ENTITIES, RuntimeDbContext, runtimeStore } from "./model/Runtime.dbContext";
|
|
@@ -35,6 +38,8 @@ import { WorkflowRuntime } from "./services/WorkflowRuntime.service";
|
|
|
35
38
|
resolver.tryResolve(LOGGER),
|
|
36
39
|
))),
|
|
37
40
|
scoped(ToolStep, ToolStep, [RuntimeDbContext, WorkflowEngine, WorkflowTools] as const),
|
|
41
|
+
scoped(TimerStep, TimerStep, [RuntimeDbContext, WorkflowEngine] as const),
|
|
42
|
+
scoped(Schedules, Schedules, [RuntimeDbContext, WorkflowEngine, IWorkflowDefinitions] as const),
|
|
38
43
|
// The application's input validator, if it registered one anywhere; the built-in one otherwise.
|
|
39
44
|
DI.scoped(DI.factoryProviderWithResolver(IWorkflowRuntime, [], (resolver) => new WorkflowRuntime(
|
|
40
45
|
resolver.resolve(RuntimeDbContext),
|
|
@@ -43,7 +48,7 @@ import { WorkflowRuntime } from "./services/WorkflowRuntime.service";
|
|
|
43
48
|
resolver.resolve(SERVICE_PROVIDER).tryResolve(WorkflowInputValidator) ?? new DefaultWorkflowInputValidator(),
|
|
44
49
|
))),
|
|
45
50
|
],
|
|
46
|
-
controllers: [DI.bindDeps(InstancesController, IWorkflowRuntime)],
|
|
51
|
+
controllers: [DI.bindDeps(InstancesController, IWorkflowRuntime), DI.bindDeps(EventsController, IWorkflowRuntime)],
|
|
47
52
|
background: [
|
|
48
53
|
DI.bindDeps(WorkflowWorker, SERVICE_PROVIDER, workflowRuntimeConfig.token),
|
|
49
54
|
DI.bindDeps(WorkflowRetention, SERVICE_PROVIDER, workflowRuntimeConfig.token),
|
|
@@ -2,12 +2,19 @@ import { BackgroundService, delay } from "../../../background";
|
|
|
2
2
|
import type { ServiceProvider } from "../../../di";
|
|
3
3
|
import type { ConfigView } from "../../../kernel";
|
|
4
4
|
import type { workflowRuntimeConfig } from "../config/Runtime.config";
|
|
5
|
+
import { Schedules } from "../internal/Schedules";
|
|
6
|
+
import { TimerStep } from "../internal/TimerStep";
|
|
5
7
|
import { ToolStep } from "../internal/ToolStep";
|
|
8
|
+
import { WorkflowEngine } from "../internal/WorkflowEngine";
|
|
6
9
|
import { RuntimeDbContext } from "../model/Runtime.dbContext";
|
|
7
10
|
import type { WfJob } from "../model/WfJob.model";
|
|
8
11
|
|
|
9
12
|
/** How long a claimed job belongs to this worker; if it dies, another one takes the job after that. */
|
|
10
13
|
const LEASE_MS = 5 * 60_000;
|
|
14
|
+
/** A job that keeps failing outside its Tool (the database, a lost version) fails its step after this many tries. */
|
|
15
|
+
const MAX_JOB_ATTEMPTS = 5;
|
|
16
|
+
/** How often the scheduled starts are matched against the published versions. */
|
|
17
|
+
const RECONCILE_MS = 60_000;
|
|
11
18
|
|
|
12
19
|
|
|
13
20
|
/**
|
|
@@ -28,7 +35,12 @@ export class WorkflowWorker extends BackgroundService {
|
|
|
28
35
|
if (!this.config.get("worker.enabled")) return;
|
|
29
36
|
const intervalMs = this.config.get("worker.intervalMs");
|
|
30
37
|
const batchSize = this.config.get("worker.batchSize");
|
|
38
|
+
let reconciledAt = 0;
|
|
31
39
|
while (!signal.aborted) {
|
|
40
|
+
if (Date.now() - reconciledAt >= RECONCILE_MS) {
|
|
41
|
+
reconciledAt = Date.now();
|
|
42
|
+
await this.reconcile();
|
|
43
|
+
}
|
|
32
44
|
let claimed = 0;
|
|
33
45
|
try {
|
|
34
46
|
const jobs = await this.claim(batchSize);
|
|
@@ -48,9 +60,44 @@ export class WorkflowWorker extends BackgroundService {
|
|
|
48
60
|
private async run(job: WfJob, signal: AbortSignal): Promise<void> {
|
|
49
61
|
const scope = this.services.createScope();
|
|
50
62
|
try {
|
|
51
|
-
await scope.resolve(
|
|
63
|
+
if (job.kind === "timer") await scope.resolve(TimerStep).run(job);
|
|
64
|
+
else if (job.kind === "schedule") await scope.resolve(Schedules).fire(job);
|
|
65
|
+
else await scope.resolve(ToolStep).run(job, signal);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
this.reportFailure("job failed", error, { job: job.id, instance: job.instanceId, attempts: job.attempts });
|
|
68
|
+
await this.giveUpOrDelay(job, error).catch((failure: unknown) => this.reportFailure("job failure handling failed", failure, { job: job.id }));
|
|
69
|
+
} finally {
|
|
70
|
+
await scope.dispose();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private async reconcile(): Promise<void> {
|
|
75
|
+
const scope = this.services.createScope();
|
|
76
|
+
try {
|
|
77
|
+
await scope.resolve(Schedules).reconcile();
|
|
52
78
|
} catch (error) {
|
|
53
|
-
this.reportFailure("
|
|
79
|
+
this.reportFailure("scheduled starts could not be planned", error);
|
|
80
|
+
} finally {
|
|
81
|
+
await scope.dispose();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** A job that failed outside its Tool waits a minute per try, then fails its step for a person to retry. */
|
|
86
|
+
private async giveUpOrDelay(job: WfJob, error: unknown): Promise<void> {
|
|
87
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
88
|
+
const scope = this.services.createScope();
|
|
89
|
+
try {
|
|
90
|
+
const db = scope.resolve(RuntimeDbContext);
|
|
91
|
+
if (job.attempts < MAX_JOB_ATTEMPTS || job.instanceId === null || job.tokenId === null) {
|
|
92
|
+
await db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeUpdate({ dueAt: new Date(Date.now() + job.attempts * 60_000), lockedUntil: null, lastError: message.slice(0, 1000) });
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const engine = scope.resolve(WorkflowEngine);
|
|
96
|
+
await engine.inStep(job.instanceId, async (step) => {
|
|
97
|
+
await db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
98
|
+
const token = step.tokens.find((t) => t.id === job.tokenId);
|
|
99
|
+
if (token && token.status === "waiting" && step.instance.status === "running") engine.fail(step, token, "job_failed", message.slice(0, 1000));
|
|
100
|
+
});
|
|
54
101
|
} finally {
|
|
55
102
|
await scope.dispose();
|
|
56
103
|
}
|
|
@@ -63,7 +110,7 @@ export class WorkflowWorker extends BackgroundService {
|
|
|
63
110
|
return await db.transactionScope(async () => {
|
|
64
111
|
const now = new Date();
|
|
65
112
|
const jobs = await db.jobs
|
|
66
|
-
.where((j) => j.kind.
|
|
113
|
+
.where((j) => j.kind.in(["tool", "timer", "schedule"]).and(j.dueAt.lte(now)).and(j.lockedUntil.isNull().or(j.lockedUntil.lt(now))))
|
|
67
114
|
.orderBy((j) => j.id)
|
|
68
115
|
.take(batchSize)
|
|
69
116
|
.forUpdate({ skipLocked: true })
|
|
@@ -20,7 +20,8 @@ export interface WorkflowTaskRequested {
|
|
|
20
20
|
export interface WorkflowTokenClosed {
|
|
21
21
|
readonly instanceId: string;
|
|
22
22
|
readonly tokenId: string;
|
|
23
|
-
|
|
23
|
+
/** `cancelled` — the instance was cancelled; `interrupted` — a boundary timer took over the step. */
|
|
24
|
+
readonly reason: "cancelled" | "interrupted";
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
/** An instance finished: completed or cancelled. */
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { contextOnlyController } from "../../../http/Routing/contextOnlyControllers";
|
|
2
|
+
import { Controller, Post, type HttpContext } from "../../../http";
|
|
3
|
+
import { authorize, body, optionalString, param } from "../../internal/http";
|
|
4
|
+
import { IWorkflowRuntime } from "../services/IWorkflowRuntime.service";
|
|
5
|
+
|
|
6
|
+
/** Events from outside — a webhook, another service — over HTTP (WORKFLOW-001 §4). */
|
|
7
|
+
@Controller("workflow/events")
|
|
8
|
+
export class EventsController {
|
|
9
|
+
constructor(private readonly runtime: IWorkflowRuntime) {}
|
|
10
|
+
|
|
11
|
+
@Post(":eventKey")
|
|
12
|
+
async emit(ctx: HttpContext) {
|
|
13
|
+
const eventKey = param(ctx, "eventKey");
|
|
14
|
+
const request = await body(ctx);
|
|
15
|
+
const partition = optionalString(request.partition, "partition") ?? null;
|
|
16
|
+
const actor = await authorize(ctx, "events.emit", { partition });
|
|
17
|
+
const result = await this.runtime.emit(eventKey, request.payload ?? {}, {
|
|
18
|
+
correlation: optionalString(request.correlation, "correlation") ?? null,
|
|
19
|
+
partition,
|
|
20
|
+
actor,
|
|
21
|
+
});
|
|
22
|
+
ctx.res.status(202);
|
|
23
|
+
return result;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
contextOnlyController(EventsController);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ProcessDocument, ProcessFlow } from "../../../../library/workflow";
|
|
1
|
+
import type { ProcessDocument, ProcessFlow, ProcessNode } from "../../../../library/workflow";
|
|
2
2
|
|
|
3
3
|
/** A version's graph with flows indexed by node. Built once per version. */
|
|
4
4
|
export interface ProcessGraph {
|
|
@@ -8,6 +8,8 @@ export interface ProcessGraph {
|
|
|
8
8
|
readonly document: ProcessDocument;
|
|
9
9
|
readonly outgoing: ReadonlyMap<string, readonly (readonly [string, ProcessFlow])[]>;
|
|
10
10
|
readonly incoming: ReadonlyMap<string, readonly (readonly [string, ProcessFlow])[]>;
|
|
11
|
+
/** Boundary nodes by the step they are attached to. */
|
|
12
|
+
readonly boundaries: ReadonlyMap<string, readonly (readonly [string, ProcessNode])[]>;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
15
|
export function buildGraph(versionId: string, processKey: string, version: number, document: ProcessDocument): ProcessGraph {
|
|
@@ -17,7 +19,11 @@ export function buildGraph(versionId: string, processKey: string, version: numbe
|
|
|
17
19
|
outgoing.set(flow.source, [...outgoing.get(flow.source) ?? [], [id, flow]]);
|
|
18
20
|
incoming.set(flow.target, [...incoming.get(flow.target) ?? [], [id, flow]]);
|
|
19
21
|
}
|
|
20
|
-
|
|
22
|
+
const boundaries = new Map<string, (readonly [string, ProcessNode])[]>();
|
|
23
|
+
for (const [id, node] of Object.entries(document.nodes)) {
|
|
24
|
+
if (node.attachedTo !== undefined) boundaries.set(node.attachedTo, [...boundaries.get(node.attachedTo) ?? [], [id, node]]);
|
|
25
|
+
}
|
|
26
|
+
return { versionId, processKey, version, document, outgoing, incoming, boundaries };
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
/** Graphs of versions in memory. Versions are immutable, so entries never go stale; the oldest go first past the limit. */
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { parseCron } from "../../../../library/workflow";
|
|
2
|
+
import type { IWorkflowDefinitions, VersionDetails } from "../../definitions_module/services/IWorkflowDefinitions.service";
|
|
3
|
+
import type { RuntimeDbContext } from "../model/Runtime.dbContext";
|
|
4
|
+
import { WfJob } from "../model/WfJob.model";
|
|
5
|
+
import type { WorkflowEngine } from "./WorkflowEngine";
|
|
6
|
+
|
|
7
|
+
const SCHEDULE_ACTOR = { kind: "system", id: "schedule" } as const;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Scheduled starts (`scheduleStart`, cron in UTC). Every latest published
|
|
11
|
+
* version with such nodes has one `schedule` job, due at the nearest firing of
|
|
12
|
+
* any of them. `reconcile` keeps the jobs in line with the catalog; `fire`
|
|
13
|
+
* starts the instances of a due job and plans the next firing in the same
|
|
14
|
+
* transaction, so a crash never starts one firing twice. Firings missed while
|
|
15
|
+
* no worker ran are not caught up: the next one is planned from now.
|
|
16
|
+
*/
|
|
17
|
+
export class Schedules {
|
|
18
|
+
constructor(
|
|
19
|
+
private readonly db: RuntimeDbContext,
|
|
20
|
+
private readonly engine: WorkflowEngine,
|
|
21
|
+
private readonly definitions: IWorkflowDefinitions,
|
|
22
|
+
) {}
|
|
23
|
+
|
|
24
|
+
/** Adds jobs for new scheduled versions and drops those of replaced or archived ones. */
|
|
25
|
+
async reconcile(now = new Date()): Promise<void> {
|
|
26
|
+
const versions = (await this.definitions.published()).filter((version) => scheduleNodes(version).length > 0);
|
|
27
|
+
const wanted = new Map(versions.map((version) => [version.id, version]));
|
|
28
|
+
const added: WfJob[] = [];
|
|
29
|
+
await this.db.transactionScope(async () => {
|
|
30
|
+
// One reconciler at a time across application instances.
|
|
31
|
+
await this.db.database.querySqlRaw("SELECT pg_advisory_xact_lock(hashtext('bazis.workflow.schedules'))");
|
|
32
|
+
const jobs = await this.db.jobs.asNoTracking().where((j) => j.kind.eq("schedule")).toList();
|
|
33
|
+
const stale = jobs.filter((job) => job.versionId === null || !wanted.has(job.versionId)).map((job) => job.id);
|
|
34
|
+
if (stale.length > 0) await this.db.jobs.asNoTracking().where((j) => j.id.in(stale)).executeDelete();
|
|
35
|
+
const planned = new Set(jobs.map((job) => job.versionId));
|
|
36
|
+
for (const version of versions) {
|
|
37
|
+
if (planned.has(version.id)) continue;
|
|
38
|
+
const dueAt = nextFiring(version, now);
|
|
39
|
+
if (dueAt === undefined) continue;
|
|
40
|
+
const job = Object.assign(new WfJob(), { id: Bun.randomUUIDv7(), kind: "schedule" as const, versionId: version.id, dueAt });
|
|
41
|
+
this.db.jobs.add(job);
|
|
42
|
+
added.push(job);
|
|
43
|
+
}
|
|
44
|
+
await this.db.saveChanges();
|
|
45
|
+
});
|
|
46
|
+
// The jobs belong to the worker from now on: this context must not hold them.
|
|
47
|
+
for (const job of added) this.db.detach(job);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A `schedule` job is due: start an instance from every node firing at this minute and plan the next firing. */
|
|
51
|
+
async fire(job: WfJob): Promise<void> {
|
|
52
|
+
const version = job.versionId === null ? undefined : await this.definitions.versionById(job.versionId);
|
|
53
|
+
const latest = version === undefined ? undefined : (await this.definitions.published()).find((item) => item.id === version.id);
|
|
54
|
+
if (!latest) {
|
|
55
|
+
await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const due = new Date(Math.floor(job.dueAt.getTime() / 60_000) * 60_000);
|
|
59
|
+
const graph = this.engine.cache(latest);
|
|
60
|
+
await this.db.transactionScope(async () => {
|
|
61
|
+
const next = nextFiring(latest, due);
|
|
62
|
+
if (next === undefined) await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
63
|
+
else await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeUpdate({ dueAt: next, lockedUntil: null, attempts: 0, lastError: null });
|
|
64
|
+
for (const [nodeId, cron] of scheduleNodes(latest)) {
|
|
65
|
+
if (!cron.matches(due)) continue;
|
|
66
|
+
await this.engine.begin(graph, nodeId, { input: {}, partition: null, businessKey: null, startedVia: "schedule", actor: SCHEDULE_ACTOR });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function scheduleNodes(version: VersionDetails) {
|
|
73
|
+
return Object.entries(version.document.nodes).flatMap(([id, node]) => {
|
|
74
|
+
const cron = node.kind === "scheduleStart" && typeof node.config?.cron === "string" ? parseCron(node.config.cron) : undefined;
|
|
75
|
+
return cron === undefined ? [] : [[id, cron] as const];
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function nextFiring(version: VersionDetails, after: Date): Date | undefined {
|
|
80
|
+
const times = scheduleNodes(version).map(([, cron]) => cron.next(after)?.getTime()).filter((time): time is number => time !== undefined);
|
|
81
|
+
return times.length === 0 ? undefined : new Date(Math.min(...times));
|
|
82
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { RuntimeDbContext } from "../model/Runtime.dbContext";
|
|
2
|
+
import type { WfJob } from "../model/WfJob.model";
|
|
3
|
+
import type { WorkflowEngine } from "./WorkflowEngine";
|
|
4
|
+
|
|
5
|
+
/** One `timer` job: a `timerCatch` step or a boundary timer is due. A closed token drops it. */
|
|
6
|
+
export class TimerStep {
|
|
7
|
+
constructor(
|
|
8
|
+
private readonly db: RuntimeDbContext,
|
|
9
|
+
private readonly engine: WorkflowEngine,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
async run(job: WfJob): Promise<void> {
|
|
13
|
+
const fired = job.instanceId === null || job.tokenId === null
|
|
14
|
+
? null
|
|
15
|
+
: await this.engine.inStep(job.instanceId, async (step) => {
|
|
16
|
+
await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
17
|
+
return await this.engine.timerFired(step, job.tokenId!);
|
|
18
|
+
});
|
|
19
|
+
if (fired === null) await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { AgentToolResult } from "../../../agent";
|
|
2
|
-
import { resolveInput } from "../../../../library/workflow";
|
|
2
|
+
import { durationMs, resolveInput } from "../../../../library/workflow";
|
|
3
3
|
import type { WorkflowTools } from "../../internal/WorkflowTools";
|
|
4
4
|
import type { RuntimeDbContext } from "../model/Runtime.dbContext";
|
|
5
5
|
import type { WfJob } from "../model/WfJob.model";
|
|
@@ -40,21 +40,57 @@ export class ToolStep {
|
|
|
40
40
|
signal,
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
+
const reason = reasonOf(result);
|
|
43
44
|
await this.engine.inStep(instance.id, async (step) => {
|
|
44
|
-
await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
45
45
|
const current = step.tokens.find((t) => t.id === token.id);
|
|
46
|
+
const retryAt = current?.status === "waiting" && step.instance.status === "running" && result.status === "error" && reason === undefined
|
|
47
|
+
? nextAttempt(node?.config?.retry, job.attempts)
|
|
48
|
+
: undefined;
|
|
49
|
+
if (retryAt !== undefined) {
|
|
50
|
+
// The same job runs again later: its attempts keep counting.
|
|
51
|
+
await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeUpdate({ dueAt: retryAt, lockedUntil: null, lastError: result.error?.message ?? null });
|
|
52
|
+
} else {
|
|
53
|
+
await this.db.jobs.asNoTracking().where((j) => j.id.eq(job.id)).executeDelete();
|
|
54
|
+
}
|
|
46
55
|
if (!current || current.status !== "waiting" || step.instance.status !== "running") return;
|
|
47
56
|
this.engine.history(step, current, current.nodeId, "tool", {
|
|
48
57
|
tool: name,
|
|
49
58
|
status: result.status,
|
|
59
|
+
attempt: job.attempts,
|
|
50
60
|
durationMs: result.durationMs ?? null,
|
|
51
|
-
...(result.error ? { error: { code: result.error.code ?? null, message: result.error.message } } : {}),
|
|
61
|
+
...(result.error ? { error: { code: result.error.code ?? null, message: result.error.message, ...(reason === undefined ? {} : { reason }) } } : {}),
|
|
62
|
+
...(retryAt === undefined ? {} : { retryAt: retryAt.toISOString() }),
|
|
52
63
|
});
|
|
64
|
+
if (retryAt !== undefined) return;
|
|
53
65
|
if (result.status === "success") {
|
|
54
66
|
await this.engine.complete(step, current.id, result.output ?? null);
|
|
55
67
|
} else {
|
|
56
|
-
this.engine.
|
|
68
|
+
await this.engine.failStep(step, current, {
|
|
69
|
+
code: result.error?.code ?? "TOOL_FAILED",
|
|
70
|
+
message: result.error?.message ?? `Tool ${name} failed`,
|
|
71
|
+
...(reason === undefined ? {} : { reason }),
|
|
72
|
+
});
|
|
57
73
|
}
|
|
58
74
|
});
|
|
59
75
|
}
|
|
60
76
|
}
|
|
77
|
+
|
|
78
|
+
/** The business reason of a `ToolError`, if the Tool gave one. */
|
|
79
|
+
function reasonOf(result: AgentToolResult): string | undefined {
|
|
80
|
+
const details = result.error?.details;
|
|
81
|
+
const reason = typeof details === "object" && details !== null && !Array.isArray(details) ? (details as Record<string, unknown>).reason : undefined;
|
|
82
|
+
return typeof reason === "string" ? reason : undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* When to try a failed call again under the node's `retry` policy, or
|
|
87
|
+
* `undefined` when the attempts are used up. The pause doubles from `backoff`
|
|
88
|
+
* (10 seconds by default) and stops growing at a day.
|
|
89
|
+
*/
|
|
90
|
+
export function nextAttempt(policy: unknown, attempts: number): Date | undefined {
|
|
91
|
+
if (typeof policy !== "object" || policy === null) return undefined;
|
|
92
|
+
const { maxAttempts, backoff } = policy as { maxAttempts?: unknown; backoff?: unknown };
|
|
93
|
+
if (typeof maxAttempts !== "number" || attempts >= maxAttempts) return undefined;
|
|
94
|
+
const base = typeof backoff === "string" ? durationMs(backoff) ?? 10_000 : 10_000;
|
|
95
|
+
return new Date(Date.now() + Math.min(base * 2 ** Math.max(attempts - 1, 0), 86_400_000));
|
|
96
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ServiceResolver } from "../../../di";
|
|
2
2
|
import { EventBus, type EventToken, type Logger } from "../../../kernel";
|
|
3
|
-
import { durationMs, evaluateCondition, nodeKindOf, type FormSpec, type InstanceData, type ProcessFlow, type ProcessNode } from "../../../../library/workflow";
|
|
3
|
+
import { durationMs, evaluateCondition, nodeKindOf, readPath, type FormSpec, type InstanceData, type ProcessFlow, type ProcessNode } from "../../../../library/workflow";
|
|
4
4
|
import type { WorkflowActor } from "../../contracts/types";
|
|
5
5
|
import type { IWorkflowDefinitions, VersionDetails } from "../../definitions_module/services/IWorkflowDefinitions.service";
|
|
6
6
|
import { WORKFLOW_INSTANCE_FINISHED, WORKFLOW_TASK_REQUESTED, WORKFLOW_TOKEN_CLOSED } from "../events/Runtime.events";
|
|
@@ -8,6 +8,7 @@ import type { RuntimeDbContext } from "../model/Runtime.dbContext";
|
|
|
8
8
|
import { WfHistory, type HistoryType } from "../model/WfHistory.model";
|
|
9
9
|
import { WfInstance, type StartedVia } from "../model/WfInstance.model";
|
|
10
10
|
import { WfJob } from "../model/WfJob.model";
|
|
11
|
+
import { WfSubscription } from "../model/WfSubscription.model";
|
|
11
12
|
import { WfToken } from "../model/WfToken.model";
|
|
12
13
|
import { buildGraph, type GraphCache, type ProcessGraph } from "./GraphCache";
|
|
13
14
|
|
|
@@ -36,8 +37,6 @@ export interface BeginOptions {
|
|
|
36
37
|
readonly actor: WorkflowActor;
|
|
37
38
|
}
|
|
38
39
|
|
|
39
|
-
/** Node kinds that come with later stages: an instance reaching one fails with a clear reason. */
|
|
40
|
-
const NOT_YET = new Set(["timerCatch", "eventWait"]);
|
|
41
40
|
const LIVE = new Set(["active", "waiting", "failed"]);
|
|
42
41
|
|
|
43
42
|
/**
|
|
@@ -132,12 +131,91 @@ export class WorkflowEngine {
|
|
|
132
131
|
const saveTo = node.config?.saveTo;
|
|
133
132
|
if (typeof saveTo === "string") step.instance.vars = { ...step.instance.vars, [saveTo]: output ?? null };
|
|
134
133
|
if (actor !== undefined) this.history(step, token, token.nodeId, "completed", null, actor);
|
|
134
|
+
this.disarm(step, token.nodeId);
|
|
135
|
+
await this.advance(step, this.leave(step, token, this.single(step, token.nodeId)));
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* A step's Tool failed for good. An error boundary attached to the step takes
|
|
141
|
+
* the token (first one whose `errorCode` equals the Tool's `reason` or error
|
|
142
|
+
* code, else one without `errorCode`); otherwise the instance fails.
|
|
143
|
+
*/
|
|
144
|
+
async failStep(step: Step, token: WfToken, error: { readonly code: string; readonly message: string; readonly reason?: string }): Promise<void> {
|
|
145
|
+
const boundaries = (step.graph.boundaries.get(token.nodeId) ?? []).filter(([, node]) => node.kind === "boundaryError");
|
|
146
|
+
const matching = boundaries.find(([, node]) => node.config?.errorCode !== undefined && (node.config.errorCode === error.reason || node.config.errorCode === error.code))
|
|
147
|
+
?? boundaries.find(([, node]) => node.config?.errorCode === undefined);
|
|
148
|
+
if (!matching) {
|
|
149
|
+
this.fail(step, token, error.code, error.message);
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
const [boundaryId] = matching;
|
|
153
|
+
const caught = { code: error.code, message: error.message, ...(error.reason === undefined ? {} : { reason: error.reason }) };
|
|
154
|
+
token.output = { error: caught };
|
|
155
|
+
this.close(token, "done");
|
|
156
|
+
this.disarm(step, token.nodeId);
|
|
157
|
+
this.history(step, token, token.nodeId, "error", { ...caught, boundary: boundaryId });
|
|
158
|
+
step.instance.nodes = { ...step.instance.nodes, [boundaryId]: caught };
|
|
159
|
+
await this.advance(step, [this.spawn(step, boundaryId, null)]);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* A timer job is due. A `timerCatch` step moves on. A `boundaryTimer` takes
|
|
164
|
+
* its own flow; an interrupting one (the default) first closes the step it
|
|
165
|
+
* watches. `false` when the token no longer waits.
|
|
166
|
+
*/
|
|
167
|
+
async timerFired(step: Step, tokenId: string): Promise<boolean> {
|
|
168
|
+
const token = step.tokens.find((t) => t.id === tokenId);
|
|
169
|
+
if (!token || token.status !== "waiting" || step.instance.status !== "running") return false;
|
|
170
|
+
const node = step.graph.document.nodes[token.nodeId];
|
|
171
|
+
const firedAt = new Date().toISOString();
|
|
172
|
+
if (node?.kind === "timerCatch") {
|
|
173
|
+
this.history(step, token, token.nodeId, "timer", { firedAt });
|
|
174
|
+
return await this.complete(step, tokenId, { firedAt });
|
|
175
|
+
}
|
|
176
|
+
if (node?.kind !== "boundaryTimer") return false;
|
|
177
|
+
return await this.fireBoundary(step, token, node, { firedAt }, "timer");
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* An event the token waits for arrived. An `eventWait` step moves on with
|
|
182
|
+
* the payload (and `saveTo`); a `boundaryEvent` takes its flow like a
|
|
183
|
+
* boundary timer. `false` when the token no longer waits.
|
|
184
|
+
*/
|
|
185
|
+
async eventReceived(step: Step, tokenId: string, eventKey: string, payload: Record<string, unknown>): Promise<boolean> {
|
|
186
|
+
const token = step.tokens.find((t) => t.id === tokenId);
|
|
187
|
+
if (!token || token.status !== "waiting" || step.instance.status !== "running") return false;
|
|
188
|
+
const node = step.graph.document.nodes[token.nodeId];
|
|
189
|
+
if (node?.kind === "eventWait") {
|
|
190
|
+
this.history(step, token, token.nodeId, "event", { event: eventKey });
|
|
191
|
+
return await this.complete(step, tokenId, payload);
|
|
192
|
+
}
|
|
193
|
+
if (node?.kind !== "boundaryEvent") return false;
|
|
194
|
+
return await this.fireBoundary(step, token, node, payload, "event", { event: eventKey });
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A boundary timer or event fired: an interrupting one closes the watched step first, then the boundary takes its flow. */
|
|
198
|
+
private async fireBoundary(step: Step, token: WfToken, node: ProcessNode, output: Record<string, unknown>, type: "timer" | "event", details: Record<string, unknown> = {}): Promise<boolean> {
|
|
199
|
+
if (node.attachedTo === undefined) return false;
|
|
200
|
+
const interrupting = node.config?.interrupting !== false;
|
|
201
|
+
this.history(step, token, token.nodeId, type, { ...details, ...(type === "timer" ? output : {}), interrupting });
|
|
202
|
+
if (interrupting) {
|
|
203
|
+
for (const host of step.tokens.filter((t) => t.nodeId === node.attachedTo && LIVE.has(t.status))) {
|
|
204
|
+
this.close(host, "cancelled");
|
|
205
|
+
if (step.graph.document.nodes[host.nodeId]?.kind === "userTask") {
|
|
206
|
+
step.events.push({ event: WORKFLOW_TOKEN_CLOSED as EventToken<unknown>, payload: { instanceId: step.instance.id, tokenId: host.id, reason: "interrupted" } });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
this.disarm(step, node.attachedTo, token.id);
|
|
210
|
+
}
|
|
211
|
+
step.instance.nodes = { ...step.instance.nodes, [token.nodeId]: output };
|
|
135
212
|
await this.advance(step, this.leave(step, token, this.single(step, token.nodeId)));
|
|
136
213
|
return true;
|
|
137
214
|
}
|
|
138
215
|
|
|
139
216
|
/** A step failed: the token waits for a retry, the instance is `failed` with the reason. */
|
|
140
217
|
fail(step: Step, token: WfToken, code: string, message: string): void {
|
|
218
|
+
this.disarm(step, token.nodeId);
|
|
141
219
|
token.status = "failed";
|
|
142
220
|
step.instance.status = "failed";
|
|
143
221
|
step.instance.error = { node: token.nodeId, code, message };
|
|
@@ -219,15 +297,17 @@ export class WorkflowEngine {
|
|
|
219
297
|
this.close(token, "done");
|
|
220
298
|
return [];
|
|
221
299
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return [];
|
|
225
|
-
}
|
|
300
|
+
// An error boundary that caught a step's failure: it only leads on.
|
|
301
|
+
if (role === "boundary") return this.leave(step, token, this.single(step, token.nodeId));
|
|
226
302
|
switch (node.kind) {
|
|
227
303
|
case "serviceTask":
|
|
228
|
-
return this.waitForTool(step, token);
|
|
304
|
+
return this.arm(step, token, this.waitForTool(step, token));
|
|
229
305
|
case "userTask":
|
|
230
|
-
return this.waitForTask(step, token, node);
|
|
306
|
+
return this.arm(step, token, this.waitForTask(step, token, node));
|
|
307
|
+
case "timerCatch":
|
|
308
|
+
return this.arm(step, token, this.waitForTimer(step, token, node));
|
|
309
|
+
case "eventWait":
|
|
310
|
+
return this.arm(step, token, this.waitForEvent(step, token, node));
|
|
231
311
|
case "exclusiveGateway":
|
|
232
312
|
return this.choose(step, token);
|
|
233
313
|
case "parallelGateway":
|
|
@@ -238,6 +318,79 @@ export class WorkflowEngine {
|
|
|
238
318
|
}
|
|
239
319
|
}
|
|
240
320
|
|
|
321
|
+
private waitForTimer(step: Step, token: WfToken, node: ProcessNode): WfToken[] {
|
|
322
|
+
const dueAt = timerDue(node.config ?? {}, dataOf(step.instance));
|
|
323
|
+
if (dueAt === undefined) {
|
|
324
|
+
this.fail(step, token, "bad_timer", `the timer of ${token.nodeId} has no valid date`);
|
|
325
|
+
return [];
|
|
326
|
+
}
|
|
327
|
+
token.status = "waiting";
|
|
328
|
+
this.db.jobs.add(Object.assign(new WfJob(), { id: Bun.randomUUIDv7(), kind: "timer" as const, instanceId: step.instance.id, tokenId: token.id, dueAt }));
|
|
329
|
+
return [];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* A step started waiting: its boundary timers start too, each as a waiting
|
|
334
|
+
* token on the boundary node with a timer job.
|
|
335
|
+
*/
|
|
336
|
+
private arm(step: Step, host: WfToken, next: WfToken[]): WfToken[] {
|
|
337
|
+
if (host.status !== "waiting") return next;
|
|
338
|
+
for (const [id, node] of step.graph.boundaries.get(host.nodeId) ?? []) {
|
|
339
|
+
if (node.kind === "boundaryEvent") {
|
|
340
|
+
const correlation = correlationOf(node.config ?? {}, dataOf(step.instance));
|
|
341
|
+
if (correlation === undefined) {
|
|
342
|
+
this.fail(step, host, "bad_correlation", `the correlation of ${id} has no value`);
|
|
343
|
+
return [];
|
|
344
|
+
}
|
|
345
|
+
const watcher = this.spawn(step, id, null);
|
|
346
|
+
watcher.status = "waiting";
|
|
347
|
+
this.subscribe(step, watcher, String(node.config?.event), correlation);
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (node.kind !== "boundaryTimer") continue;
|
|
351
|
+
const dueAt = timerDue(node.config ?? {}, dataOf(step.instance));
|
|
352
|
+
if (dueAt === undefined) {
|
|
353
|
+
this.fail(step, host, "bad_timer", `the timer of ${id} has no valid date`);
|
|
354
|
+
return [];
|
|
355
|
+
}
|
|
356
|
+
const watcher = this.spawn(step, id, null);
|
|
357
|
+
watcher.status = "waiting";
|
|
358
|
+
this.db.jobs.add(Object.assign(new WfJob(), { id: Bun.randomUUIDv7(), kind: "timer" as const, instanceId: step.instance.id, tokenId: watcher.id, dueAt }));
|
|
359
|
+
}
|
|
360
|
+
return next;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** The step left or failed: its boundary tokens close; their jobs find the tokens closed and drop. */
|
|
364
|
+
private disarm(step: Step, hostNodeId: string, except?: string): void {
|
|
365
|
+
const boundaries = new Set((step.graph.boundaries.get(hostNodeId) ?? []).map(([id]) => id));
|
|
366
|
+
if (boundaries.size === 0) return;
|
|
367
|
+
for (const token of step.tokens) {
|
|
368
|
+
if (token.id !== except && boundaries.has(token.nodeId) && LIVE.has(token.status)) this.close(token, "cancelled");
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
private waitForEvent(step: Step, token: WfToken, node: ProcessNode): WfToken[] {
|
|
373
|
+
const correlation = correlationOf(node.config ?? {}, dataOf(step.instance));
|
|
374
|
+
if (correlation === undefined) {
|
|
375
|
+
this.fail(step, token, "bad_correlation", `the correlation of ${token.nodeId} has no value`);
|
|
376
|
+
return [];
|
|
377
|
+
}
|
|
378
|
+
token.status = "waiting";
|
|
379
|
+
this.subscribe(step, token, String(node.config?.event), correlation);
|
|
380
|
+
return [];
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
private subscribe(step: Step, token: WfToken, eventKey: string, correlation: string | null): void {
|
|
384
|
+
this.db.subscriptions.add(Object.assign(new WfSubscription(), {
|
|
385
|
+
id: Bun.randomUUIDv7(),
|
|
386
|
+
instanceId: step.instance.id,
|
|
387
|
+
tokenId: token.id,
|
|
388
|
+
eventKey,
|
|
389
|
+
correlation,
|
|
390
|
+
partition: step.instance.partition,
|
|
391
|
+
}));
|
|
392
|
+
}
|
|
393
|
+
|
|
241
394
|
private waitForTool(step: Step, token: WfToken): WfToken[] {
|
|
242
395
|
token.status = "waiting";
|
|
243
396
|
this.db.jobs.add(Object.assign(new WfJob(), { id: Bun.randomUUIDv7(), kind: "tool" as const, instanceId: step.instance.id, tokenId: token.id, dueAt: new Date() }));
|
|
@@ -351,6 +504,31 @@ export class WorkflowEngine {
|
|
|
351
504
|
}
|
|
352
505
|
}
|
|
353
506
|
|
|
507
|
+
/** When a timer fires: `duration` from now, or the date at the `at` path; `undefined` if neither is valid. */
|
|
508
|
+
export function timerDue(config: Readonly<Record<string, unknown>>, data: InstanceData): Date | undefined {
|
|
509
|
+
if (typeof config.duration === "string") {
|
|
510
|
+
const ms = durationMs(config.duration);
|
|
511
|
+
return ms === undefined ? undefined : new Date(Date.now() + ms);
|
|
512
|
+
}
|
|
513
|
+
if (typeof config.at === "string") {
|
|
514
|
+
const value = readPath(data, config.at);
|
|
515
|
+
const date = value instanceof Date ? value : typeof value === "string" || typeof value === "number" ? new Date(value) : undefined;
|
|
516
|
+
return date === undefined || Number.isNaN(date.getTime()) ? undefined : date;
|
|
517
|
+
}
|
|
518
|
+
return undefined;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* The correlation a wait matches: the value at `config.correlation` as a
|
|
523
|
+
* string, `null` without a path (any event of the key), `undefined` when the
|
|
524
|
+
* path has no value.
|
|
525
|
+
*/
|
|
526
|
+
export function correlationOf(config: Readonly<Record<string, unknown>>, data: InstanceData): string | null | undefined {
|
|
527
|
+
if (typeof config.correlation !== "string") return null;
|
|
528
|
+
const value = readPath(data, config.correlation);
|
|
529
|
+
return value === undefined || value === null || value === "" || typeof value === "object" ? undefined : String(value);
|
|
530
|
+
}
|
|
531
|
+
|
|
354
532
|
export function dataOf(instance: WfInstance): InstanceData {
|
|
355
533
|
return { input: instance.input, vars: instance.vars, nodes: instance.nodes };
|
|
356
534
|
}
|
|
@@ -11,10 +11,10 @@ export class WfHistory {
|
|
|
11
11
|
@UUID({ version: "v7" })
|
|
12
12
|
id = "";
|
|
13
13
|
|
|
14
|
-
@Column({ type: "
|
|
14
|
+
@Column({ type: "text" })
|
|
15
15
|
instanceId = "";
|
|
16
16
|
|
|
17
|
-
@Column({ type: "
|
|
17
|
+
@Column({ type: "text", nullable: true })
|
|
18
18
|
tokenId: string | null = null;
|
|
19
19
|
|
|
20
20
|
@Column({ type: "text", nullable: true })
|
|
@@ -19,15 +19,15 @@ export class WfJob {
|
|
|
19
19
|
|
|
20
20
|
/** `null` for a `schedule` job: its instance does not exist yet. */
|
|
21
21
|
@Index()
|
|
22
|
-
@Column({ type: "
|
|
22
|
+
@Column({ type: "text", nullable: true })
|
|
23
23
|
instanceId: string | null = null;
|
|
24
24
|
|
|
25
25
|
@Index()
|
|
26
|
-
@Column({ type: "
|
|
26
|
+
@Column({ type: "text", nullable: true })
|
|
27
27
|
tokenId: string | null = null;
|
|
28
28
|
|
|
29
29
|
/** The version a `schedule` job starts. */
|
|
30
|
-
@Column({ type: "
|
|
30
|
+
@Column({ type: "text", nullable: true })
|
|
31
31
|
versionId: string | null = null;
|
|
32
32
|
|
|
33
33
|
@Index()
|
|
@@ -9,11 +9,11 @@ export class WfSubscription {
|
|
|
9
9
|
@UUID({ version: "v7" })
|
|
10
10
|
id = "";
|
|
11
11
|
|
|
12
|
-
@Column({ type: "
|
|
12
|
+
@Column({ type: "text" })
|
|
13
13
|
instanceId = "";
|
|
14
14
|
|
|
15
15
|
@Index()
|
|
16
|
-
@Column({ type: "
|
|
16
|
+
@Column({ type: "text" })
|
|
17
17
|
tokenId = "";
|
|
18
18
|
|
|
19
19
|
@Column({ type: "text" })
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { FormIssue } from "../../../../library/workflow";
|
|
1
2
|
import type { WorkflowActor, WorkflowPage, WorkflowPaging } from "../../contracts/types";
|
|
2
3
|
import type { HistoryType } from "../model/WfHistory.model";
|
|
3
4
|
import type { InstanceStatus, StartedVia } from "../model/WfInstance.model";
|
|
@@ -55,6 +56,24 @@ export interface HistoryEntry {
|
|
|
55
56
|
readonly at: Date;
|
|
56
57
|
}
|
|
57
58
|
|
|
59
|
+
export interface EmitOptions {
|
|
60
|
+
/** Which waits it is for: `order:42`. Waits without a correlation take any event of the key. */
|
|
61
|
+
readonly correlation?: string | null;
|
|
62
|
+
/** Only the waits of this partition; it is also the partition of instances the event starts. */
|
|
63
|
+
readonly partition?: string | null;
|
|
64
|
+
/** On whose behalf started instances run; `{ kind: "system", id: "event:<key>" }` by default. */
|
|
65
|
+
readonly actor?: WorkflowActor;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface EmitResult {
|
|
69
|
+
/** Instances started by an `eventStart`. */
|
|
70
|
+
readonly started: readonly string[];
|
|
71
|
+
/** Instances whose `eventWait` or `boundaryEvent` took the event. */
|
|
72
|
+
readonly continued: readonly string[];
|
|
73
|
+
/** Processes with an `eventStart` whose input the payload did not pass. */
|
|
74
|
+
readonly rejected: readonly { readonly processKey: string; readonly issues: readonly FormIssue[] }[];
|
|
75
|
+
}
|
|
76
|
+
|
|
58
77
|
export interface InstanceListQuery extends WorkflowPaging {
|
|
59
78
|
readonly partition?: string;
|
|
60
79
|
readonly status?: InstanceStatus;
|
|
@@ -75,6 +94,11 @@ export abstract class IWorkflowRuntime {
|
|
|
75
94
|
abstract retry(instanceId: string, options: { readonly actor: WorkflowActor }): Promise<InstanceDetails>;
|
|
76
95
|
/** Finishes a user task's wait with the form output and moves on; `wrong_status` when the token no longer waits. */
|
|
77
96
|
abstract completeToken(tokenId: string, output: unknown, actor: WorkflowActor): Promise<InstanceDetails>;
|
|
97
|
+
/**
|
|
98
|
+
* Delivers an event: starts processes whose latest version has an
|
|
99
|
+
* `eventStart` for the key, and moves the instances waiting for it.
|
|
100
|
+
*/
|
|
101
|
+
abstract emit(eventKey: string, payload?: unknown, options?: EmitOptions): Promise<EmitResult>;
|
|
78
102
|
/** Instances, newest first. */
|
|
79
103
|
abstract instances(query?: InstanceListQuery): Promise<WorkflowPage<InstanceSummary>>;
|
|
80
104
|
abstract instance(instanceId: string): Promise<InstanceDetails>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FormSpec } from "../../../../library/workflow";
|
|
1
|
+
import type { FormIssue, FormSpec } from "../../../../library/workflow";
|
|
2
2
|
import { isUuid, pagingOf, type WorkflowActor, type WorkflowPage } from "../../contracts/types";
|
|
3
3
|
import { WorkflowError } from "../../contracts/WorkflowError";
|
|
4
4
|
import type { WorkflowInputValidator } from "../../contracts/WorkflowInputValidator";
|
|
@@ -10,6 +10,8 @@ import type { WfInstance } from "../model/WfInstance.model";
|
|
|
10
10
|
import type { WfToken } from "../model/WfToken.model";
|
|
11
11
|
import {
|
|
12
12
|
IWorkflowRuntime,
|
|
13
|
+
type EmitOptions,
|
|
14
|
+
type EmitResult,
|
|
13
15
|
type HistoryEntry,
|
|
14
16
|
type InstanceDetails,
|
|
15
17
|
type InstanceListQuery,
|
|
@@ -18,6 +20,7 @@ import {
|
|
|
18
20
|
} from "./IWorkflowRuntime.service";
|
|
19
21
|
|
|
20
22
|
const STATUSES = new Set(["running", "completed", "cancelled", "failed"]);
|
|
23
|
+
const EVENT_KEY = /^[a-z][a-z0-9_-]*(\.[a-z][a-z0-9_-]*)+$/;
|
|
21
24
|
|
|
22
25
|
export class WorkflowRuntime extends IWorkflowRuntime {
|
|
23
26
|
constructor(
|
|
@@ -87,6 +90,45 @@ export class WorkflowRuntime extends IWorkflowRuntime {
|
|
|
87
90
|
return await this.instance(token.instanceId);
|
|
88
91
|
}
|
|
89
92
|
|
|
93
|
+
async emit(eventKey: string, payload: unknown = {}, options: EmitOptions = {}): Promise<EmitResult> {
|
|
94
|
+
if (typeof eventKey !== "string" || !EVENT_KEY.test(eventKey)) throw new WorkflowError("invalid_input", "eventKey must look like payments.received");
|
|
95
|
+
if (typeof payload !== "object" || payload === null || Array.isArray(payload)) throw new WorkflowError("invalid_input", "payload must be a JSON object");
|
|
96
|
+
const data = payload as Record<string, unknown>;
|
|
97
|
+
const correlation = optionalText(options.correlation, "correlation");
|
|
98
|
+
const partition = optionalText(options.partition, "partition");
|
|
99
|
+
const actor = options.actor ?? { kind: "system" as const, id: `event:${eventKey}` };
|
|
100
|
+
actorOf(actor);
|
|
101
|
+
|
|
102
|
+
let waits = this.db.subscriptions.asNoTracking().where((s) => s.eventKey.eq(eventKey));
|
|
103
|
+
waits = correlation === null ? waits.where((s) => s.correlation.isNull()) : waits.where((s) => s.correlation.isNull().or(s.correlation.eq(correlation)));
|
|
104
|
+
if (partition !== null) waits = waits.where((s) => s.partition.eq(partition));
|
|
105
|
+
const continued = new Set<string>();
|
|
106
|
+
for (const wait of await waits.orderBy((s) => s.id).toList()) {
|
|
107
|
+
const moved = await this.engine.inStep(wait.instanceId, async (step) => {
|
|
108
|
+
await this.db.subscriptions.asNoTracking().where((s) => s.id.eq(wait.id)).executeDelete();
|
|
109
|
+
return await this.engine.eventReceived(step, wait.tokenId, eventKey, data);
|
|
110
|
+
});
|
|
111
|
+
if (moved === true) continued.add(wait.instanceId);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const started: string[] = [];
|
|
115
|
+
const rejected: { processKey: string; issues: readonly FormIssue[] }[] = [];
|
|
116
|
+
for (const version of await this.definitions.published()) {
|
|
117
|
+
for (const [nodeId, node] of Object.entries(version.document.nodes)) {
|
|
118
|
+
if (node.kind !== "eventStart" || node.config?.event !== eventKey) continue;
|
|
119
|
+
// An event carries more than a process needs: fields the input does not declare are dropped, not refused.
|
|
120
|
+
const { value, issues: all } = await this.validator.validate(version.document.input, data, { purpose: "input", processKey: version.key, partition });
|
|
121
|
+
const issues = all.filter((issue) => issue.code !== "unknown");
|
|
122
|
+
if (issues.length > 0) {
|
|
123
|
+
rejected.push({ processKey: version.key, issues });
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
started.push(await this.engine.begin(this.engine.cache(version), nodeId, { input: value, partition, businessKey: correlation, startedVia: "event", actor }));
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return { started, continued: [...continued], rejected };
|
|
130
|
+
}
|
|
131
|
+
|
|
90
132
|
async instances(query: InstanceListQuery = {}): Promise<WorkflowPage<InstanceSummary>> {
|
|
91
133
|
const { limit, offset } = pagingOf(query);
|
|
92
134
|
if (query.status !== undefined && !STATUSES.has(query.status)) throw new WorkflowError("invalid_input", "status must be running, completed, cancelled or failed");
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/** A parsed five-field cron expression, evaluated in UTC. */
|
|
2
|
+
export interface CronSchedule {
|
|
3
|
+
/** Whether the expression fires at this minute. */
|
|
4
|
+
matches(date: Date): boolean;
|
|
5
|
+
/** The first minute after `after` when it fires; `undefined` if none within five years. */
|
|
6
|
+
next(after: Date): Date | undefined;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const FIELDS = [
|
|
10
|
+
{ min: 0, max: 59 },
|
|
11
|
+
{ min: 0, max: 23 },
|
|
12
|
+
{ min: 1, max: 31 },
|
|
13
|
+
{ min: 1, max: 12 },
|
|
14
|
+
{ min: 0, max: 7 },
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
const MINUTE = 60_000;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Parses `minute hour day-of-month month day-of-week` with `*`, numbers,
|
|
21
|
+
* ranges `a-b`, steps `/n` and lists `a,b`. Day of week 0 and 7 are Sunday.
|
|
22
|
+
* When both day fields are restricted, either one matching is enough, as in
|
|
23
|
+
* classic cron. Returns `undefined` for an invalid expression.
|
|
24
|
+
*/
|
|
25
|
+
export function parseCron(expression: string): CronSchedule | undefined {
|
|
26
|
+
const parts = expression.trim().split(/\s+/);
|
|
27
|
+
if (parts.length !== 5) return undefined;
|
|
28
|
+
const sets: Set<number>[] = [];
|
|
29
|
+
for (const [index, part] of parts.entries()) {
|
|
30
|
+
const set = field(part, FIELDS[index]!.min, FIELDS[index]!.max);
|
|
31
|
+
if (set === undefined) return undefined;
|
|
32
|
+
sets.push(set);
|
|
33
|
+
}
|
|
34
|
+
const [minutes, hours, days, months, weekdays] = sets as [Set<number>, Set<number>, Set<number>, Set<number>, Set<number>];
|
|
35
|
+
if (weekdays.has(7)) weekdays.add(0);
|
|
36
|
+
const daysRestricted = parts[2] !== "*";
|
|
37
|
+
const weekdaysRestricted = parts[4] !== "*";
|
|
38
|
+
|
|
39
|
+
const dayMatches = (date: Date): boolean => {
|
|
40
|
+
const day = days.has(date.getUTCDate());
|
|
41
|
+
const weekday = weekdays.has(date.getUTCDay());
|
|
42
|
+
return daysRestricted && weekdaysRestricted ? day || weekday : day && weekday;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const matches = (date: Date): boolean =>
|
|
46
|
+
minutes.has(date.getUTCMinutes()) && hours.has(date.getUTCHours()) && months.has(date.getUTCMonth() + 1) && dayMatches(date);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
matches,
|
|
50
|
+
next(after: Date): Date | undefined {
|
|
51
|
+
const limit = after.getTime() + 5 * 366 * 24 * 60 * MINUTE;
|
|
52
|
+
const date = new Date(Math.floor(after.getTime() / MINUTE) * MINUTE + MINUTE);
|
|
53
|
+
while (date.getTime() <= limit) {
|
|
54
|
+
if (!months.has(date.getUTCMonth() + 1)) {
|
|
55
|
+
date.setUTCMonth(date.getUTCMonth() + 1, 1);
|
|
56
|
+
date.setUTCHours(0, 0, 0, 0);
|
|
57
|
+
} else if (!dayMatches(date)) {
|
|
58
|
+
date.setUTCDate(date.getUTCDate() + 1);
|
|
59
|
+
date.setUTCHours(0, 0, 0, 0);
|
|
60
|
+
} else if (!hours.has(date.getUTCHours())) {
|
|
61
|
+
date.setUTCHours(date.getUTCHours() + 1, 0, 0, 0);
|
|
62
|
+
} else if (!minutes.has(date.getUTCMinutes())) {
|
|
63
|
+
date.setTime(date.getTime() + MINUTE);
|
|
64
|
+
} else {
|
|
65
|
+
return date;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return undefined;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function field(text: string, min: number, max: number): Set<number> | undefined {
|
|
74
|
+
const out = new Set<number>();
|
|
75
|
+
for (const item of text.split(",")) {
|
|
76
|
+
const match = /^(\*|\d+(?:-\d+)?)(?:\/(\d+))?$/.exec(item);
|
|
77
|
+
if (!match) return undefined;
|
|
78
|
+
const step = match[2] === undefined ? 1 : Number(match[2]);
|
|
79
|
+
let from = min, to = max;
|
|
80
|
+
if (match[1] !== "*") {
|
|
81
|
+
const [a, b] = match[1]!.split("-").map(Number) as [number, number | undefined];
|
|
82
|
+
from = a;
|
|
83
|
+
to = b ?? (match[2] === undefined ? a : max);
|
|
84
|
+
}
|
|
85
|
+
if (step < 1 || from < min || to > max || from > to) return undefined;
|
|
86
|
+
for (let value = from; value <= to; value += step) out.add(value);
|
|
87
|
+
}
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
@@ -21,4 +21,5 @@ export { PROCESS_LIMITS, validateProcess, hasErrors, type ProcessIssue, type Pro
|
|
|
21
21
|
export { DATA_PATH, isPath, readPath, resolveInput, type InstanceData } from "./paths";
|
|
22
22
|
export { evaluateCondition } from "./conditions";
|
|
23
23
|
export { durationMs } from "./duration";
|
|
24
|
+
export { parseCron, type CronSchedule } from "./cron";
|
|
24
25
|
export { checkFormSpec, formFieldCodes, validateForm, type FormField, type FormFieldType, type FormIssue, type FormSpec } from "./formFields";
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parseCron } from "./cron";
|
|
1
2
|
import { checkFormSpec } from "./formFields";
|
|
2
3
|
import { DATA_PATH } from "./paths";
|
|
3
4
|
import type { NodeKind } from "./ProcessDocument";
|
|
@@ -42,8 +43,7 @@ function first(...checks: (string | null)[]): string | null {
|
|
|
42
43
|
function cron(config: Record<string, unknown>): string | null {
|
|
43
44
|
const value = config.cron;
|
|
44
45
|
if (typeof value !== "string") return "config.cron is required";
|
|
45
|
-
|
|
46
|
-
return parts.length === 5 && parts.every((part) => /^[\d*/,\-]+$/.test(part)) ? null : "config.cron must have five fields";
|
|
46
|
+
return parseCron(value) === undefined ? "config.cron must be five valid fields: minute hour day month weekday (UTC)" : null;
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/** A timer: either `duration` (ISO 8601) or `at`, a path to a date in instance data. */
|
|
@@ -99,7 +99,7 @@ export const NODE_KINDS: readonly NodeKindDefinition[] = Object.freeze([
|
|
|
99
99
|
{ kind: "parallelGateway", role: "gateway", check: none },
|
|
100
100
|
{ kind: "boundaryError", role: "boundary", check: (c) => string(c, "errorCode", /^[A-Za-z][A-Za-z0-9_.-]{0,63}$/, false) },
|
|
101
101
|
{ kind: "boundaryTimer", role: "boundary", check: (c) => first(timer(c), boolean(c, "interrupting")) },
|
|
102
|
-
{ kind: "boundaryEvent", role: "boundary", check: (c) => first(string(c, "event", DOTTED_KEY), boolean(c, "interrupting")) },
|
|
102
|
+
{ kind: "boundaryEvent", role: "boundary", check: (c) => first(string(c, "event", DOTTED_KEY), string(c, "correlation", DATA_PATH, false), boolean(c, "interrupting")) },
|
|
103
103
|
{ kind: "end", role: "end", check: none },
|
|
104
104
|
] satisfies NodeKindDefinition[]);
|
|
105
105
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bazis",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.126.0",
|
|
4
4
|
"description": "Modular backend framework for Bun in TypeScript: DI wired by codegen, HTTP, a PostgreSQL ORM, JWT, WebSocket, agents and a CLI.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Alexander Alenin",
|