@jive-sh/workflow-platform 0.4.0 → 0.5.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.
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import type { LastStepNow, StepEdgeTaken } from '../storage/step-records';
7
7
  import { A_STEP_FAILS, isRetry, RUN_ENDS, RUN_STARTS, type StepEdge } from '../graph';
8
- import type { StepDeclaration } from '../steps';
8
+ import type { StepDeclaration, StepRuns } from '../steps';
9
9
 
10
10
  /** A step-to-step edge of a workflow's graph, and how many runs took it in the window. */
11
11
  export interface GraphEdge {
@@ -20,7 +20,7 @@ export interface WorkflowGraph {
20
20
  readonly workflow: string;
21
21
  /** False until the workflow declares its graph: then `edges` is empty and a page says so. */
22
22
  readonly declared: boolean;
23
- readonly steps: ReadonlyArray<{ readonly name: string; readonly agent: boolean; readonly description: string }>;
23
+ readonly steps: ReadonlyArray<{ readonly name: string; readonly runs: StepRuns | null; readonly description: string; readonly workflow: string | null }>;
24
24
  readonly edges: ReadonlyArray<GraphEdge>;
25
25
  readonly undeclared: ReadonlyArray<{ readonly from: string; readonly to: string; readonly runs: number }>;
26
26
  }
@@ -28,7 +28,7 @@ export interface WorkflowGraph {
28
28
  /** What of a declaration the graph reads. */
29
29
  interface Declared {
30
30
  readonly name: string;
31
- readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'agent' | 'description'>>;
31
+ readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'runs' | 'description' | 'workflow'>>;
32
32
  readonly graph?: (() => ReadonlyArray<StepEdge>) | undefined;
33
33
  }
34
34
 
@@ -61,7 +61,7 @@ export const workflowGraphs = (input: {
61
61
  return {
62
62
  workflow: d.name,
63
63
  declared: d.graph !== undefined,
64
- steps: d.steps().map((s) => ({ name: s.name, agent: s.agent, description: s.description })),
64
+ steps: d.steps().map((s) => ({ name: s.name, runs: s.runs, description: s.description, workflow: s.workflow ?? null })),
65
65
  edges: graph.map((e) => ({ ...e, runs: runs.get(`${e.from}\u0000${e.to}`) ?? 0 })),
66
66
  undeclared: [...undeclared.values()].sort((a, b) => b.runs - a.runs),
67
67
  };
@@ -0,0 +1,97 @@
1
+ /**
2
+ * WHERE WORK FLOWS BETWEEN WORKFLOWS: the edges of a project's DAG of workflows, and each workflow's place in its pipeline.
3
+ * Built from what the platform knows and nothing written for the page:
4
+ *
5
+ * feeds a workflow declares the other among those whose results it works on (`fedBy`);
6
+ * sends back a step of a workflow returned a task of one upstream of it, through feeds, to that one's queue (`requeue`);
7
+ * gives input it returned to its queue a task of one that is not upstream of it, with no feed declared between them;
8
+ * waits on its list puts tasks on a wait that names another workflow (`PendingWork.waits`), not the one that feeds it:
9
+ * the list's own words say why (`why`).
10
+ *
11
+ * Its place: its stage (the project's phases, in order), then how many feeds lead into it within its stage. Pure.
12
+ */
13
+ import type { ListedWait, ReturnedBetween } from '../storage/task-store';
14
+
15
+ export type FlowKind = 'feeds' | 'sends back' | 'gives input' | 'waits on';
16
+
17
+ /** One edge: work going from one workflow to another. */
18
+ export interface WorkflowFlow {
19
+ readonly from: string;
20
+ readonly to: string;
21
+ readonly kind: FlowKind;
22
+ /** Whether a declaration says it (`fedBy`); a recorded one only is not declared. */
23
+ readonly declared: boolean;
24
+ /** The returns to the queue recorded along it, and the last one's time; 0 and none where none was recorded. */
25
+ readonly notes: number;
26
+ readonly last: number | null;
27
+ /** Along a `waits on` edge: the tasks waiting, and the list's words for the wait; 0 and none on any other. */
28
+ readonly waiting: number;
29
+ readonly why: string | null;
30
+ }
31
+
32
+ /** A workflow's place in its pipeline: its stage's index, then its depth within the stage through feeds. */
33
+ export interface PipelinePlace {
34
+ readonly workflow: string;
35
+ readonly stage: number;
36
+ readonly depth: number;
37
+ }
38
+
39
+ /** Whether `to` reaches `from` through `feeds`: work goes from `to`, by way of others, to `from`. */
40
+ const reaches = (feeds: ReadonlyArray<{ readonly from: string; readonly to: string }>, start: string, target: string): boolean => {
41
+ const seen = new Set<string>([start]);
42
+ const next = [start];
43
+ while (next.length > 0) {
44
+ const at = next.pop()!;
45
+ for (const f of feeds) {
46
+ if (f.from !== at || seen.has(f.to)) continue;
47
+ if (f.to === target) return true;
48
+ seen.add(f.to);
49
+ next.push(f.to);
50
+ }
51
+ }
52
+ return false;
53
+ };
54
+
55
+ export const workflowFlows = (input: {
56
+ readonly declared: ReadonlyArray<{ readonly name: string; readonly fedBy?: ReadonlyArray<string> }>;
57
+ readonly returned: ReadonlyArray<ReturnedBetween>;
58
+ readonly waits: ReadonlyArray<ListedWait>;
59
+ }): ReadonlyArray<WorkflowFlow> => {
60
+ const names = new Set(input.declared.map((d) => d.name));
61
+ const recorded = (from: string, to: string) => input.returned.find((n) => n.from === from && n.to === to);
62
+ const feeds = input.declared.flatMap((d) => (d.fedBy ?? []).filter((f) => names.has(f) && f !== d.name).map((f) => ({ from: f, to: d.name })));
63
+ const declaredFlows: ReadonlyArray<WorkflowFlow> = feeds.map((f) => {
64
+ const r = recorded(f.from, f.to);
65
+ return { from: f.from, to: f.to, kind: 'feeds', declared: true, notes: r?.notes ?? 0, last: r?.last ?? null, waiting: 0, why: null };
66
+ });
67
+ const recordedOnly: ReadonlyArray<WorkflowFlow> = input.returned
68
+ .filter((n) => names.has(n.from) && names.has(n.to) && !feeds.some((f) => f.from === n.from && f.to === n.to))
69
+ .map((n) => ({ from: n.from, to: n.to, kind: reaches(feeds, n.to, n.from) ? 'sends back' : 'gives input', declared: false, notes: n.notes, last: n.last, waiting: 0, why: null }));
70
+ // A wait on the workflow that feeds it is that feed's work still to come, drawn as the feed; one on another is an edge.
71
+ const waitedOn: ReadonlyArray<WorkflowFlow> = input.waits
72
+ .filter((w) => names.has(w.on) && w.on !== w.workflow && !feeds.some((f) => f.from === w.on && f.to === w.workflow))
73
+ .map((w) => ({ from: w.workflow, to: w.on, kind: 'waits on', declared: false, notes: 0, last: null, waiting: w.tasks, why: w.label }));
74
+ return [...declaredFlows, ...recordedOnly, ...waitedOn];
75
+ };
76
+
77
+ /** Each workflow's place: its stage by `stageOf` in `stages`' order (one not among them after them), then its depth there. */
78
+ export const pipelinePlaces = (input: {
79
+ readonly workflows: ReadonlyArray<{ readonly name: string; readonly stage: string }>;
80
+ readonly stages: ReadonlyArray<string>;
81
+ readonly flows: ReadonlyArray<WorkflowFlow>;
82
+ }): ReadonlyArray<PipelinePlace> => {
83
+ const stageIndex = (s: string) => (input.stages.includes(s) ? input.stages.indexOf(s) : input.stages.length);
84
+ const stageOf = new Map(input.workflows.map((w) => [w.name, stageIndex(w.stage)]));
85
+ // Forward work only: feeds, and input given to one not upstream. A send-back points against the pipeline.
86
+ const forward = input.flows.filter((f) => f.kind !== 'sends back');
87
+ const depth = new Map<string, number>();
88
+ const depthOf = (name: string, path: ReadonlySet<string>): number => {
89
+ const known = depth.get(name);
90
+ if (known !== undefined) return known;
91
+ const into = forward.filter((f) => f.to === name && !path.has(f.from) && stageOf.get(f.from) === stageOf.get(name));
92
+ const d = into.length === 0 ? 0 : Math.max(...into.map((f) => depthOf(f.from, new Set([...path, name])) + 1));
93
+ depth.set(name, d);
94
+ return d;
95
+ };
96
+ return input.workflows.map((w) => ({ workflow: w.name, stage: stageIndex(w.stage), depth: depthOf(w.name, new Set()) }));
97
+ };
@@ -11,7 +11,8 @@
11
11
  * Built from the declarations and the stores alone, so any project's workflows read the same; a project adds its own
12
12
  * panels beside it. Where its runs are now (`runs-now.ts`) is read at each request too.
13
13
  */
14
- import { stepKindOf, stepUsesAnAgent, type SourceRef, type StepDeclaration, type StepKind, type WorkflowDeclaration } from '../steps';
14
+ import { stepKindOf, stepUsesAnAgent, type SourceRef, type StepDeclaration, type StepKind, type StepRuns, type WorkflowDeclaration } from '../steps';
15
+ import { pipelinePlaces, workflowFlows, type PipelinePlace, type WorkflowFlow } from './workflow-flows';
15
16
 
16
17
  /** A definition's link: its file and text, and the line that text is on now; none when the text is no longer in the file. */
17
18
  export interface SourceLink extends SourceRef {
@@ -57,7 +58,8 @@ export interface StepCost {
57
58
 
58
59
  export interface StepView {
59
60
  readonly name: string;
60
- readonly agent: boolean;
61
+ /** How it runs (`StepRuns`); none for a sub-workflow step, and for one no longer declared that no agent ran. */
62
+ readonly runs: StepRuns | null;
61
63
  readonly description: string;
62
64
  readonly source: SourceLink;
63
65
  /** Whether its declaration lists it; a step only the records name is no longer declared. */
@@ -150,8 +152,8 @@ export const yieldOf = (r: { readonly runs: number; readonly finished: number; r
150
152
  export interface StepSummary {
151
153
  readonly name: string;
152
154
  readonly kind: StepKind;
153
- /** Whether it uses an agent: an agent step, or a sub-workflow with one anywhere under it. */
154
- readonly usesAgent: boolean;
155
+ /** How it runs; none for a sub-workflow step, whose own steps say theirs. */
156
+ readonly runs: StepRuns | null;
155
157
  readonly description: string;
156
158
  /** For a sub-workflow: the workflow it runs, by name; none for any other kind. */
157
159
  readonly workflow: string | null;
@@ -187,6 +189,8 @@ export interface WorkflowView {
187
189
  readonly limits: { readonly agents: number | null; readonly runs: number | null; readonly runsSet: boolean } | null;
188
190
  /** Its tasks by status, as last counted (`queue-counts.ts`); none for a workflow with no item. */
189
191
  readonly queue: Readonly<Record<TaskStatus, number>> | null;
192
+ /** Its place among the project's workflows: what the map lays it out by (`workflow-flows.ts`). */
193
+ readonly place: PipelinePlace;
190
194
  /** What starts its runs. */
191
195
  readonly startsOn: StartsOn;
192
196
  }
@@ -199,6 +203,8 @@ export interface WorkflowsView {
199
203
  readonly groups: ReadonlyArray<{ readonly phase: string; readonly workflows: ReadonlyArray<WorkflowView> }>;
200
204
  /** Each queue's counts by status over the last `QUEUE_HISTORY_DAYS` (`historyByStatus`), a row where one changed, oldest first. */
201
205
  readonly queueHistory: ReadonlyArray<QueueCount>;
206
+ /** Where work goes between the top workflow's steps: the map's arrows (`workflow-flows.ts`). */
207
+ readonly flows: ReadonlyArray<WorkflowFlow>;
202
208
  }
203
209
 
204
210
  /** One workflow's figures over a window, read from its own records when it is opened. */
@@ -243,7 +249,7 @@ export const costOf = (rows: ReadonlyArray<StepRunRow>): StepCost | null =>
243
249
  items: new Set(rows.flatMap((r) => (r.subject === null ? [] : [r.subject.toLowerCase()]))).size,
244
250
  };
245
251
 
246
- type Declared = Pick<WorkflowDeclaration<string, unknown>, 'name' | 'phase' | 'description' | 'source' | 'item' | 'finishedWord' | 'graph' | 'pending' | 'items' | 'inputs'> & {
252
+ type Declared = Pick<WorkflowDeclaration<string, unknown>, 'name' | 'phase' | 'description' | 'source' | 'item' | 'finishedWord' | 'graph' | 'fedBy' | 'pending' | 'items' | 'inputs'> & {
247
253
  readonly steps: () => ReadonlyArray<StepDeclaration>;
248
254
  };
249
255
 
@@ -310,24 +316,29 @@ export const buildWorkflowsView = (input: {
310
316
  readonly phases: ReadonlyArray<string>;
311
317
  readonly now: number;
312
318
  /** The queues' history, from the task store. */
313
- readonly taskStore: Pick<TaskStore, 'queueCountsSince'>;
319
+ readonly taskStore: Pick<TaskStore, 'queueCountsSince' | 'returnedBetween' | 'listedWaits'>;
314
320
  /** The line a definition's text is on now. */
315
321
  readonly locate: (source: SourceRef) => number | null;
316
322
  readonly limitsOf: (workflow: string) => WorkflowView['limits'];
317
323
  /** The project's top workflow, in its own words, until it declares one. */
318
324
  readonly top: { readonly name: string; readonly description: string };
319
325
  }): Effect.Effect<WorkflowsView, StorageError> => Effect.gen(function* () {
320
- const stepSummary = (s: StepDeclaration): StepSummary => ({ name: s.name, kind: stepKindOf(s), usesAgent: stepUsesAnAgent(s), description: s.description, workflow: s.workflow ?? null });
326
+ const stepSummary = (s: StepDeclaration): StepSummary => ({ name: s.name, kind: stepKindOf(s), runs: s.runs, description: s.description, workflow: s.workflow ?? null });
321
327
  // Until the project declares its top workflow, its steps are every workflow no other workflow uses as a step.
322
328
  const usedAsAStep = new Set(input.declared.flatMap((d) => d.steps().flatMap((s) => (s.workflow === undefined ? [] : [s.workflow]))));
323
329
  const phases = [...input.phases, ...[...new Set(input.declared.map((d) => d.phase))].filter((p) => !input.phases.includes(p))];
324
330
  const queueHistory = historyByStatus(yield* input.taskStore.queueCountsSince(input.now - QUEUE_HISTORY_DAYS * 86_400));
325
331
  const queues = queuesNow(queueHistory);
332
+ // The map is of the top workflow's steps: a workflow used as a step of another is drawn inside that one, not here.
333
+ const onTheMap = input.declared.filter((d) => !usedAsAStep.has(d.name));
334
+ const flows = workflowFlows({ declared: onTheMap, returned: yield* input.taskStore.returnedBetween(), waits: yield* input.taskStore.listedWaits() });
335
+ const places = new Map(pipelinePlaces({ workflows: input.declared.map((d) => ({ name: d.name, stage: d.phase })), stages: phases, flows }).map((p) => [p.workflow, p]));
326
336
  const workflowView = (d: Declared): WorkflowView => ({
327
337
  name: d.name, steps: d.steps().map(stepSummary),
328
338
  phase: d.phase, description: d.description, source: { ...d.source, line: input.locate(d.source) }, item: d.item, finishedWord: d.finishedWord,
329
339
  usesAgents: d.steps().some(stepUsesAnAgent),
330
340
  limits: input.limitsOf(d.name),
341
+ place: places.get(d.name)!,
331
342
  queue: d.item === null ? null : queues.get(d.name) ?? noTasks(),
332
343
  startsOn: d.pending !== undefined || d.items !== undefined ? { kind: 'queue' } : d.inputs !== undefined ? { kind: 'inputs', says: d.inputs.says } : { kind: 'timer or request' },
333
344
  });
@@ -336,10 +347,11 @@ export const buildWorkflowsView = (input: {
336
347
  at: input.now,
337
348
  top: {
338
349
  ...input.top,
339
- steps: inPageOrder.filter((d) => !usedAsAStep.has(d.name)).map((d): StepSummary => ({ name: d.name, kind: 'sub-workflow', usesAgent: d.steps().some(stepUsesAnAgent), description: d.description, workflow: d.name })),
350
+ steps: inPageOrder.filter((d) => !usedAsAStep.has(d.name)).map((d): StepSummary => ({ name: d.name, kind: 'sub-workflow', runs: null, description: d.description, workflow: d.name })),
340
351
  },
341
352
  groups: phases.map((phase) => ({ phase, workflows: input.declared.filter((d) => d.phase === phase).map(workflowView) })).filter((g) => g.workflows.length > 0),
342
353
  queueHistory,
354
+ flows,
343
355
  };
344
356
  });
345
357
 
@@ -377,8 +389,8 @@ export const buildWorkflowFigures = (input: {
377
389
  const declaredSteps = d.steps();
378
390
  const onlyInRecords = [...new Set(own.map((r) => r.step))].filter((s) => !declaredSteps.some((x) => x.name === s));
379
391
  const steps: ReadonlyArray<StepView> = [
380
- ...declaredSteps.map((s) => ({ name: s.name, agent: s.agent, description: s.description, source: link(s.source), declared: true, cost: costOf(own.filter((r) => r.step === s.name)), endings: endingsOf(funnelRecords.records.filter((r) => r.step === s.name)) })),
381
- ...onlyInRecords.map((name) => ({ name, agent: own.some((r) => r.step === name && r.agent === 1), description: 'no longer declared', source: link(d.source), declared: false, cost: costOf(own.filter((r) => r.step === name)), endings: endingsOf(funnelRecords.records.filter((r) => r.step === name)) })),
392
+ ...declaredSteps.map((s) => ({ name: s.name, runs: s.runs, description: s.description, source: link(s.source), declared: true, cost: costOf(own.filter((r) => r.step === s.name)), endings: endingsOf(funnelRecords.records.filter((r) => r.step === s.name)) })),
393
+ ...onlyInRecords.map((name) => ({ name, runs: own.some((r) => r.step === name && r.agent === 1) ? 'agentic' as const : null, description: 'no longer declared', source: link(d.source), declared: false, cost: costOf(own.filter((r) => r.step === name)), endings: endingsOf(funnelRecords.records.filter((r) => r.step === name)) })),
382
394
  ];
383
395
  const funnel = stepFunnel({ declaration: d, records: funnelRecords.records, outcomes: funnelRecords.outcomes, unfinished: going, hours: input.hours, since, now: input.now });
384
396
  const funnelBefore = stepFunnel({ declaration: d, records: funnelBoth.records, outcomes: funnelBoth.outcomes, unfinished: going, hours: input.hours, since: before, now: since });