bazis 0.125.0 → 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.
@@ -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.125.0" }, capabilities: { experimentalApi: true },
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
- @Column({ type: "uuid" })
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,10 @@ 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";
12
14
  import { TimerStep } from "./internal/TimerStep";
13
15
  import { ToolStep } from "./internal/ToolStep";
14
16
  import { WorkflowEngine } from "./internal/WorkflowEngine";
@@ -37,6 +39,7 @@ import { WorkflowRuntime } from "./services/WorkflowRuntime.service";
37
39
  ))),
38
40
  scoped(ToolStep, ToolStep, [RuntimeDbContext, WorkflowEngine, WorkflowTools] as const),
39
41
  scoped(TimerStep, TimerStep, [RuntimeDbContext, WorkflowEngine] as const),
42
+ scoped(Schedules, Schedules, [RuntimeDbContext, WorkflowEngine, IWorkflowDefinitions] as const),
40
43
  // The application's input validator, if it registered one anywhere; the built-in one otherwise.
41
44
  DI.scoped(DI.factoryProviderWithResolver(IWorkflowRuntime, [], (resolver) => new WorkflowRuntime(
42
45
  resolver.resolve(RuntimeDbContext),
@@ -45,7 +48,7 @@ import { WorkflowRuntime } from "./services/WorkflowRuntime.service";
45
48
  resolver.resolve(SERVICE_PROVIDER).tryResolve(WorkflowInputValidator) ?? new DefaultWorkflowInputValidator(),
46
49
  ))),
47
50
  ],
48
- controllers: [DI.bindDeps(InstancesController, IWorkflowRuntime)],
51
+ controllers: [DI.bindDeps(InstancesController, IWorkflowRuntime), DI.bindDeps(EventsController, IWorkflowRuntime)],
49
52
  background: [
50
53
  DI.bindDeps(WorkflowWorker, SERVICE_PROVIDER, workflowRuntimeConfig.token),
51
54
  DI.bindDeps(WorkflowRetention, SERVICE_PROVIDER, workflowRuntimeConfig.token),
@@ -2,6 +2,7 @@ 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";
5
6
  import { TimerStep } from "../internal/TimerStep";
6
7
  import { ToolStep } from "../internal/ToolStep";
7
8
  import { WorkflowEngine } from "../internal/WorkflowEngine";
@@ -12,6 +13,8 @@ import type { WfJob } from "../model/WfJob.model";
12
13
  const LEASE_MS = 5 * 60_000;
13
14
  /** A job that keeps failing outside its Tool (the database, a lost version) fails its step after this many tries. */
14
15
  const MAX_JOB_ATTEMPTS = 5;
16
+ /** How often the scheduled starts are matched against the published versions. */
17
+ const RECONCILE_MS = 60_000;
15
18
 
16
19
 
17
20
  /**
@@ -32,7 +35,12 @@ export class WorkflowWorker extends BackgroundService {
32
35
  if (!this.config.get("worker.enabled")) return;
33
36
  const intervalMs = this.config.get("worker.intervalMs");
34
37
  const batchSize = this.config.get("worker.batchSize");
38
+ let reconciledAt = 0;
35
39
  while (!signal.aborted) {
40
+ if (Date.now() - reconciledAt >= RECONCILE_MS) {
41
+ reconciledAt = Date.now();
42
+ await this.reconcile();
43
+ }
36
44
  let claimed = 0;
37
45
  try {
38
46
  const jobs = await this.claim(batchSize);
@@ -53,6 +61,7 @@ export class WorkflowWorker extends BackgroundService {
53
61
  const scope = this.services.createScope();
54
62
  try {
55
63
  if (job.kind === "timer") await scope.resolve(TimerStep).run(job);
64
+ else if (job.kind === "schedule") await scope.resolve(Schedules).fire(job);
56
65
  else await scope.resolve(ToolStep).run(job, signal);
57
66
  } catch (error) {
58
67
  this.reportFailure("job failed", error, { job: job.id, instance: job.instanceId, attempts: job.attempts });
@@ -62,6 +71,17 @@ export class WorkflowWorker extends BackgroundService {
62
71
  }
63
72
  }
64
73
 
74
+ private async reconcile(): Promise<void> {
75
+ const scope = this.services.createScope();
76
+ try {
77
+ await scope.resolve(Schedules).reconcile();
78
+ } catch (error) {
79
+ this.reportFailure("scheduled starts could not be planned", error);
80
+ } finally {
81
+ await scope.dispose();
82
+ }
83
+ }
84
+
65
85
  /** A job that failed outside its Tool waits a minute per try, then fails its step for a person to retry. */
66
86
  private async giveUpOrDelay(job: WfJob, error: unknown): Promise<void> {
67
87
  const message = error instanceof Error ? error.message : String(error);
@@ -90,7 +110,7 @@ export class WorkflowWorker extends BackgroundService {
90
110
  return await db.transactionScope(async () => {
91
111
  const now = new Date();
92
112
  const jobs = await db.jobs
93
- .where((j) => j.kind.in(["tool", "timer"]).and(j.dueAt.lte(now)).and(j.lockedUntil.isNull().or(j.lockedUntil.lt(now))))
113
+ .where((j) => j.kind.in(["tool", "timer", "schedule"]).and(j.dueAt.lte(now)).and(j.lockedUntil.isNull().or(j.lockedUntil.lt(now))))
94
114
  .orderBy((j) => j.id)
95
115
  .take(batchSize)
96
116
  .forUpdate({ skipLocked: true })
@@ -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);
@@ -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
+ }
@@ -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(["eventWait"]);
41
40
  const LIVE = new Set(["active", "waiting", "failed"]);
42
41
 
43
42
  /**
@@ -174,9 +173,32 @@ export class WorkflowEngine {
174
173
  this.history(step, token, token.nodeId, "timer", { firedAt });
175
174
  return await this.complete(step, tokenId, { firedAt });
176
175
  }
177
- if (node?.kind !== "boundaryTimer" || node.attachedTo === undefined) return false;
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;
178
200
  const interrupting = node.config?.interrupting !== false;
179
- this.history(step, token, token.nodeId, "timer", { firedAt, interrupting });
201
+ this.history(step, token, token.nodeId, type, { ...details, ...(type === "timer" ? output : {}), interrupting });
180
202
  if (interrupting) {
181
203
  for (const host of step.tokens.filter((t) => t.nodeId === node.attachedTo && LIVE.has(t.status))) {
182
204
  this.close(host, "cancelled");
@@ -186,7 +208,7 @@ export class WorkflowEngine {
186
208
  }
187
209
  this.disarm(step, node.attachedTo, token.id);
188
210
  }
189
- step.instance.nodes = { ...step.instance.nodes, [token.nodeId]: { firedAt } };
211
+ step.instance.nodes = { ...step.instance.nodes, [token.nodeId]: output };
190
212
  await this.advance(step, this.leave(step, token, this.single(step, token.nodeId)));
191
213
  return true;
192
214
  }
@@ -277,10 +299,6 @@ export class WorkflowEngine {
277
299
  }
278
300
  // An error boundary that caught a step's failure: it only leads on.
279
301
  if (role === "boundary") return this.leave(step, token, this.single(step, token.nodeId));
280
- if (NOT_YET.has(node.kind)) {
281
- this.fail(step, token, "not_supported", `${node.kind} steps are not supported yet`);
282
- return [];
283
- }
284
302
  switch (node.kind) {
285
303
  case "serviceTask":
286
304
  return this.arm(step, token, this.waitForTool(step, token));
@@ -288,6 +306,8 @@ export class WorkflowEngine {
288
306
  return this.arm(step, token, this.waitForTask(step, token, node));
289
307
  case "timerCatch":
290
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));
291
311
  case "exclusiveGateway":
292
312
  return this.choose(step, token);
293
313
  case "parallelGateway":
@@ -316,6 +336,17 @@ export class WorkflowEngine {
316
336
  private arm(step: Step, host: WfToken, next: WfToken[]): WfToken[] {
317
337
  if (host.status !== "waiting") return next;
318
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
+ }
319
350
  if (node.kind !== "boundaryTimer") continue;
320
351
  const dueAt = timerDue(node.config ?? {}, dataOf(step.instance));
321
352
  if (dueAt === undefined) {
@@ -338,6 +369,28 @@ export class WorkflowEngine {
338
369
  }
339
370
  }
340
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
+
341
394
  private waitForTool(step: Step, token: WfToken): WfToken[] {
342
395
  token.status = "waiting";
343
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() }));
@@ -465,6 +518,17 @@ export function timerDue(config: Readonly<Record<string, unknown>>, data: Instan
465
518
  return undefined;
466
519
  }
467
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
+
468
532
  export function dataOf(instance: WfInstance): InstanceData {
469
533
  return { input: instance.input, vars: instance.vars, nodes: instance.nodes };
470
534
  }
@@ -11,10 +11,10 @@ export class WfHistory {
11
11
  @UUID({ version: "v7" })
12
12
  id = "";
13
13
 
14
- @Column({ type: "uuid" })
14
+ @Column({ type: "text" })
15
15
  instanceId = "";
16
16
 
17
- @Column({ type: "uuid", nullable: true })
17
+ @Column({ type: "text", nullable: true })
18
18
  tokenId: string | null = null;
19
19
 
20
20
  @Column({ type: "text", nullable: true })
@@ -19,7 +19,7 @@ export class WfInstance {
19
19
  id = "";
20
20
 
21
21
  /** The version (a Definitions row: a reference without a foreign key). */
22
- @Column({ type: "uuid" })
22
+ @Column({ type: "text" })
23
23
  versionId = "";
24
24
 
25
25
  @Column({ type: "text" })
@@ -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: "uuid", nullable: true })
22
+ @Column({ type: "text", nullable: true })
23
23
  instanceId: string | null = null;
24
24
 
25
25
  @Index()
26
- @Column({ type: "uuid", nullable: true })
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: "uuid", nullable: true })
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: "uuid" })
12
+ @Column({ type: "text" })
13
13
  instanceId = "";
14
14
 
15
15
  @Index()
16
- @Column({ type: "uuid" })
16
+ @Column({ type: "text" })
17
17
  tokenId = "";
18
18
 
19
19
  @Column({ type: "text" })
@@ -15,7 +15,7 @@ export class WfToken {
15
15
  @UUID({ version: "v7" })
16
16
  id = "";
17
17
 
18
- @Column({ type: "uuid" })
18
+ @Column({ type: "text" })
19
19
  instanceId = "";
20
20
 
21
21
  @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
- const parts = value.trim().split(/\s+/);
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.125.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",