@jive-sh/workflow-platform 0.1.0 → 0.3.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.
Files changed (40) hide show
  1. package/README.md +41 -15
  2. package/dist/cpu-limit/limiter.js +54 -11
  3. package/dist/cpu-limit/limiter.js.map +1 -1
  4. package/dist/index.js +44 -39
  5. package/dist/index.js.map +1 -1
  6. package/dist/keep-running.js +5 -5
  7. package/dist/keep-running.js.map +1 -1
  8. package/dist/pick-wakes.js +7 -1
  9. package/dist/pick-wakes.js.map +1 -1
  10. package/dist/queue-counts.js +47 -0
  11. package/dist/queue-counts.js.map +1 -0
  12. package/dist/steps.js +75 -18
  13. package/dist/steps.js.map +1 -1
  14. package/dist/storage/budgets.js +14 -0
  15. package/dist/storage/budgets.js.map +1 -1
  16. package/dist/storage/sqlite-store.js +68 -15
  17. package/dist/storage/sqlite-store.js.map +1 -1
  18. package/dist/views/workflow-flows.js +53 -0
  19. package/dist/views/workflow-flows.js.map +1 -0
  20. package/dist/views/workflows-view.js +81 -55
  21. package/dist/views/workflows-view.js.map +1 -1
  22. package/package.json +1 -1
  23. package/src/cpu-limit/limiter.ts +61 -11
  24. package/src/dashboard/dashboard.css +78 -0
  25. package/src/dashboard/index.tsx +3 -0
  26. package/src/dashboard/queue-chart.tsx +137 -0
  27. package/src/dashboard/step-graph.tsx +98 -0
  28. package/src/dashboard/workflow-dag.tsx +256 -0
  29. package/src/dashboard/workflows-dashboard.tsx +313 -0
  30. package/src/index.ts +46 -39
  31. package/src/keep-running.ts +5 -7
  32. package/src/pick-wakes.ts +9 -2
  33. package/src/queue-counts.ts +61 -0
  34. package/src/steps.ts +83 -26
  35. package/src/storage/budgets.ts +13 -0
  36. package/src/storage/sqlite-store.ts +82 -18
  37. package/src/storage/step-records.ts +6 -5
  38. package/src/storage/task-store.ts +32 -0
  39. package/src/views/workflow-flows.ts +97 -0
  40. package/src/views/workflows-view.ts +148 -82
@@ -1,8 +1,15 @@
1
1
  /**
2
- * EVERY WORKFLOW OF A PROJECT AS ITS DASHBOARD SHOWS IT: by phase, each workflow with its steps and their cost over a window,
3
- * its step graph with the window's runs on it, where its runs stopped and why (its step funnel), its queue's buckets and
4
- * its limits. Built from the declarations and the stores alone, so any project's workflows read the same; a project adds
5
- * its own panels beside it. Where its runs are now (`runs-now.ts`) is read at each request, since it changes by the second.
2
+ * A PROJECT'S WORKFLOWS AS ITS DASHBOARD SHOWS THEM, READ FROM THE RECORDS AT EACH REQUEST, in two readings:
3
+ *
4
+ * every workflow what it is, its limits, its queue's buckets and their history, and where work goes between workflows
5
+ * (`buildWorkflowsView`): what a page shows before a workflow is opened, a few small readings.
6
+ * one workflow its steps and their cost over a window, its step graph with the window's runs on it, and where its
7
+ * runs stopped and why (`buildWorkflowFigures`): read from its own records when a page opens it.
8
+ *
9
+ * Built whole and stored by a job every 5 minutes, the two were one view: 5.7 s of reading on a busy machine, 4.4 s of it
10
+ * three scans of every step record for figures only an opened workflow shows, and a page up to 5 minutes old (2026-11-17).
11
+ * Built from the declarations and the stores alone, so any project's workflows read the same; a project adds its own
12
+ * panels beside it. Where its runs are now (`runs-now.ts`) is read at each request too.
6
13
  */
7
14
  import type { SourceRef, StepDeclaration, WorkflowDeclaration } from '../steps';
8
15
 
@@ -14,9 +21,23 @@ import { Effect } from 'effect';
14
21
  import type { StorageError } from '../storage/storage-error';
15
22
  import type { EngineRecords } from '../storage/engine-records';
16
23
  import type { FunnelStepRecord, RunCost, RunOutcomeRow, StepRecords, StepRunRow } from '../storage/step-records';
17
- import { bucketCounts, type Bucket, type Tasks } from '../tasks';
24
+ import { BUCKETS, type Bucket } from '../tasks';
25
+ import type { QueueCount, TaskStore } from '../storage/task-store';
18
26
  import { stepFunnel, type StepFunnel } from './step-funnel';
19
27
  import { workflowGraphs, type WorkflowGraph } from './step-graph';
28
+ import { pipelinePlaces, workflowFlows, type PipelinePlace, type WorkflowFlow } from './workflow-flows';
29
+
30
+ /**
31
+ * WHAT STARTS ITS RUNS, from its declaration: its queue (a list of work, its runs kept going while it has some), its inputs
32
+ * changing (`inputs`, in its own words), or neither, a timer or a request.
33
+ */
34
+ export type StartsOn =
35
+ | { readonly kind: 'queue' }
36
+ | { readonly kind: 'inputs'; readonly says: string }
37
+ | { readonly kind: 'timer or request' };
38
+
39
+ /** How long the queues' history reaches back. */
40
+ export const QUEUE_HISTORY_DAYS = 7;
20
41
 
21
42
  /** What one step of a workflow cost over the window, from its records: none for a step with no record in it. */
22
43
  export interface StepCost {
@@ -123,6 +144,7 @@ export const yieldOf = (r: { readonly runs: number; readonly finished: number; r
123
144
  perCompile: r.compiles === 0 ? null : r.finished / r.compiles,
124
145
  });
125
146
 
147
+ /** A workflow as a page shows it before it is opened: what it is, its limits, its queue, and its place among the others. */
126
148
  export interface WorkflowView {
127
149
  readonly name: string;
128
150
  readonly phase: string;
@@ -133,12 +155,39 @@ export interface WorkflowView {
133
155
  readonly finishedWord: string | null;
134
156
  readonly usesAgents: boolean;
135
157
  /**
136
- * Its limits: how many of its runs may be in an agent step at once, and how many runs it keeps going, `runsSet` false
137
- * where that is the default. None for a workflow no budget rules.
158
+ * Its limits: how many runs of it may go at once (`runs`, none for no limit; `runsSet` false where that is the default),
159
+ * and, for a workflow its budget keeps going, how many of its runs may be in an agent step at once (`agents`, none for
160
+ * any other). None where no store is read.
138
161
  */
139
- readonly limits: { readonly agents: number; readonly runs: number; readonly runsSet: boolean } | null;
140
- /** Why it is paused; none while it is not. */
141
- readonly paused: string | null;
162
+ readonly limits: { readonly agents: number | null; readonly runs: number | null; readonly runsSet: boolean } | null;
163
+ /** Its tasks by bucket, as last counted (`queue-counts.ts`); none for a workflow with no item. */
164
+ readonly queue: Readonly<Record<Bucket, number>> | null;
165
+ /** What starts its runs. */
166
+ readonly startsOn: StartsOn;
167
+ /** The workflows whose results become its tasks, as declared (`fedBy`). */
168
+ readonly fedBy: ReadonlyArray<string>;
169
+ /** Its place in the pipeline: what the DAG lays it out by. */
170
+ readonly place: PipelinePlace;
171
+ }
172
+
173
+ /** Every workflow, read at the request: nothing here is stored for the page. */
174
+ export interface WorkflowsView {
175
+ /** When it was read, unix seconds. */
176
+ readonly at: number;
177
+ readonly groups: ReadonlyArray<{ readonly phase: string; readonly workflows: ReadonlyArray<WorkflowView> }>;
178
+ /** Where work goes between workflows: the DAG's edges (`workflow-flows.ts`). */
179
+ readonly flows: ReadonlyArray<WorkflowFlow>;
180
+ /** Each queue's counts over the last `QUEUE_HISTORY_DAYS`, a row where one changed, oldest first. */
181
+ readonly queueHistory: ReadonlyArray<QueueCount>;
182
+ }
183
+
184
+ /** One workflow's figures over a window, read from its own records when it is opened. */
185
+ export interface WorkflowFigures {
186
+ readonly workflow: string;
187
+ /** When it was read, unix seconds, and the window: the `hours` before it. */
188
+ readonly at: number;
189
+ readonly hours: number;
190
+ readonly since: number;
142
191
  readonly steps: ReadonlyArray<StepView>;
143
192
  /** Its steps' costs added up. */
144
193
  readonly total: StepCost;
@@ -148,16 +197,6 @@ export interface WorkflowView {
148
197
  readonly runCosts: RunCosts;
149
198
  /** What its runs gave in the window, and in the window before it: the trend. */
150
199
  readonly yields: { readonly now: Yield; readonly before: Yield };
151
- /** Its tasks by bucket; none for a workflow with no item. */
152
- readonly queue: Readonly<Record<Bucket, number>> | null;
153
- }
154
-
155
- export interface WorkflowsView {
156
- /** When it was built, unix seconds. */
157
- readonly at: number;
158
- readonly hours: number;
159
- readonly since: number;
160
- readonly groups: ReadonlyArray<{ readonly phase: string; readonly workflows: ReadonlyArray<WorkflowView> }>;
161
200
  }
162
201
 
163
202
  const NO_COST: StepCost = { calls: 0, failed: 0, open: 0, seconds: 0, inputTokens: 0, outputTokens: 0, cpuSeconds: 0, compiles: 0, items: 0 };
@@ -182,87 +221,114 @@ export const costOf = (rows: ReadonlyArray<StepRunRow>): StepCost | null =>
182
221
  items: new Set(rows.flatMap((r) => (r.subject === null ? [] : [r.subject.toLowerCase()]))).size,
183
222
  };
184
223
 
185
- type Declared = Pick<WorkflowDeclaration<string, unknown>, 'name' | 'phase' | 'description' | 'source' | 'item' | 'finishedWord' | 'graph'> & {
224
+ type Declared = Pick<WorkflowDeclaration<string, unknown>, 'name' | 'phase' | 'description' | 'source' | 'item' | 'finishedWord' | 'graph' | 'fedBy' | 'pending' | 'inputs'> & {
186
225
  readonly steps: () => ReadonlyArray<StepDeclaration>;
187
226
  };
188
227
 
189
- /** The view over the `hours` before `now`, from the stores. `limitsOf` is none for a workflow no budget rules. */
228
+ const noTasks = (): Record<Bucket, number> => Object.fromEntries(BUCKETS.map((b) => [b, 0])) as Record<Bucket, number>;
229
+
230
+ /** Each queue's newest count from its history (`queueCountsSince`, oldest first), a bucket never counted at 0. Pure. */
231
+ export const queuesNow = (history: ReadonlyArray<QueueCount>): ReadonlyMap<string, Readonly<Record<Bucket, number>>> => {
232
+ const queues = new Map<string, Record<Bucket, number>>();
233
+ for (const row of history) {
234
+ const queue = queues.get(row.workflow) ?? noTasks();
235
+ if ((BUCKETS as ReadonlyArray<string>).includes(row.bucket)) queue[row.bucket as Bucket] = row.tasks;
236
+ queues.set(row.workflow, queue);
237
+ }
238
+ return queues;
239
+ };
240
+
241
+ /**
242
+ * EVERY WORKFLOW AS A PAGE SHOWS IT BEFORE ONE IS OPENED, READ AT THE REQUEST: the declarations, the limits, each queue's
243
+ * counts as last counted and their history, and where work goes between workflows. Four small readings of the store, so a
244
+ * page load needs no stored copy of it. `limitsOf` is none where no store is read.
245
+ */
190
246
  export const buildWorkflowsView = (input: {
191
247
  readonly declared: ReadonlyArray<Declared>;
192
248
  /** The project's phases, in the order its page shows them; a workflow of another phase follows them. */
193
249
  readonly phases: ReadonlyArray<string>;
194
250
  readonly now: number;
251
+ /** The queues' history and the new input between workflows, from the task store. */
252
+ readonly taskStore: Pick<TaskStore, 'queueCountsSince' | 'newInputFlows' | 'listedWaits'>;
253
+ /** The line a definition's text is on now. */
254
+ readonly locate: (source: SourceRef) => number | null;
255
+ readonly limitsOf: (workflow: string) => WorkflowView['limits'];
256
+ }): Effect.Effect<WorkflowsView, StorageError> => Effect.gen(function* () {
257
+ const flows = workflowFlows({ declared: input.declared, newInputs: yield* input.taskStore.newInputFlows(), waits: yield* input.taskStore.listedWaits() });
258
+ const phases = [...input.phases, ...[...new Set(input.declared.map((d) => d.phase))].filter((p) => !input.phases.includes(p))];
259
+ const places = new Map(pipelinePlaces({ workflows: input.declared.map((d) => ({ name: d.name, stage: d.phase })), stages: phases, flows }).map((p) => [p.workflow, p]));
260
+ const queueHistory = yield* input.taskStore.queueCountsSince(input.now - QUEUE_HISTORY_DAYS * 86_400);
261
+ const queues = queuesNow(queueHistory);
262
+ const workflowView = (d: Declared): WorkflowView => ({
263
+ name: d.name, phase: d.phase, description: d.description, source: { ...d.source, line: input.locate(d.source) }, item: d.item, finishedWord: d.finishedWord,
264
+ usesAgents: d.steps().some((s) => s.agent),
265
+ limits: input.limitsOf(d.name),
266
+ queue: d.item === null ? null : queues.get(d.name) ?? noTasks(),
267
+ startsOn: d.pending !== undefined ? { kind: 'queue' } : d.inputs !== undefined ? { kind: 'inputs', says: d.inputs.says } : { kind: 'timer or request' },
268
+ fedBy: d.fedBy ?? [],
269
+ place: places.get(d.name)!,
270
+ });
271
+ return {
272
+ at: input.now,
273
+ groups: phases.map((phase) => ({ phase, workflows: input.declared.filter((d) => d.phase === phase).map(workflowView) })).filter((g) => g.workflows.length > 0),
274
+ flows,
275
+ queueHistory,
276
+ };
277
+ });
278
+
279
+ /**
280
+ * ONE WORKFLOW'S FIGURES OVER THE `hours` BEFORE `now`, READ FROM ITS OWN RECORDS when a page opens it: each step's cost and
281
+ * endings, its step graph with the runs on each edge, where its runs stopped, what a run costs, and what its runs gave
282
+ * against the window before.
283
+ */
284
+ export const buildWorkflowFigures = (input: {
285
+ readonly declaration: Declared;
286
+ readonly now: number;
195
287
  readonly hours: number;
196
288
  readonly records: StepRecords;
197
289
  readonly engine: EngineRecords;
198
- readonly tasks: Pick<Tasks, 'taskSummaries'>;
199
290
  /** The line a definition's text is on now. */
200
291
  readonly locate: (source: SourceRef) => number | null;
201
- readonly limitsOf: (workflow: string) => WorkflowView['limits'];
202
- readonly pausedOf: (workflow: string) => string | null;
203
- }): Effect.Effect<WorkflowsView, StorageError> => Effect.gen(function* () {
292
+ }): Effect.Effect<WorkflowFigures, StorageError> => Effect.gen(function* () {
293
+ const d = input.declaration;
204
294
  const since = input.now - input.hours * 3600;
205
295
  const before = since - input.hours * 3600;
206
- const rows = yield* input.records.stepRunsSince(since);
296
+ const own = yield* input.records.stepRunsSince(since, d.name);
207
297
  // The window before, for the trend: its records are those since its start, less the window's own.
208
- const rowsBoth = yield* input.records.stepRunsSince(before);
209
- const funnelRecords = yield* input.records.funnelRecordsSince(since);
210
- const funnelBoth = yield* input.records.funnelRecordsSince(before);
211
- const finishedNow = yield* input.records.runOutcomesSince(since);
212
- const runCosts = yield* input.records.runCostsSince(since);
213
- const finishedBoth = yield* input.records.runOutcomesSince(before);
214
- const moves = yield* input.records.stepEdgesSince(since);
298
+ const ownBoth = yield* input.records.stepRunsSince(before, d.name);
299
+ const funnelRecords = yield* input.records.funnelRecordsSince(since, d.name);
300
+ const funnelBoth = yield* input.records.funnelRecordsSince(before, d.name);
301
+ const finishedNow = yield* input.records.runOutcomesSince(since, d.name);
302
+ const finishedBoth = yield* input.records.runOutcomesSince(before, d.name);
303
+ const runCosts = yield* input.records.runCostsSince(since, d.name);
304
+ const moves = yield* input.records.stepEdgesSince(since, d.name);
215
305
  const going = new Set((yield* input.engine.unfinishedRuns()).map((u) => u.run));
216
- const queues = new Map(yield* Effect.forEach(input.declared.filter((d) => d.item !== null), (d) =>
217
- Effect.map(input.tasks.taskSummaries({ workflow: d.name }), (tasks) => [d.name, bucketCounts(tasks)] as const)));
218
- const graphs = new Map(workflowGraphs({ declared: input.declared, taken: moves.edges, lasts: moves.lasts, going }).map((g) => [g.workflow, g]));
219
-
220
306
  const link = (s: SourceRef): SourceLink => ({ ...s, line: input.locate(s) });
221
307
 
222
- const workflowView = (d: Declared): WorkflowView => {
223
- const own = rows.filter((r) => r.workflow === d.name);
224
- const ownRecords = funnelRecords.records.filter((r) => r.workflow === d.name);
225
- const declaredSteps = d.steps();
226
- const onlyInRecords = [...new Set(own.map((r) => r.step))].filter((s) => !declaredSteps.some((x) => x.name === s));
227
- const steps: ReadonlyArray<StepView> = [
228
- ...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(ownRecords.filter((r) => r.step === s.name)) })),
229
- ...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(ownRecords.filter((r) => r.step === name)) })),
230
- ];
231
- const funnel = stepFunnel({
232
- declaration: d, records: ownRecords, outcomes: funnelRecords.outcomes.filter((o) => o.workflow === d.name),
233
- unfinished: going, hours: input.hours, since, now: input.now,
234
- });
235
- const funnelBefore = stepFunnel({
236
- declaration: d, records: funnelBoth.records.filter((r) => r.workflow === d.name), outcomes: funnelBoth.outcomes.filter((o) => o.workflow === d.name),
237
- unfinished: going, hours: input.hours, since: before, now: since,
238
- });
239
- const costNow = costOf(own) ?? NO_COST;
240
- const costBoth = costOf(rowsBoth.filter((r) => r.workflow === d.name)) ?? NO_COST;
241
- const finished = (outcomes: ReadonlyArray<RunOutcomeRow>) => outcomes.filter((o) => o.workflow === d.name && o.finished === 1).length;
242
- return {
243
- steps,
244
- funnel,
245
- runCosts: runCostsOf(runCosts.filter((c) => c.workflow === d.name)),
246
- yields: {
247
- now: yieldOf({ runs: funnel.runs, finished: finished(finishedNow), tokens: costNow.inputTokens + costNow.outputTokens, compiles: costNow.compiles }),
248
- before: yieldOf({
249
- runs: funnelBefore.runs, finished: finished(finishedBoth) - finished(finishedNow),
250
- tokens: costBoth.inputTokens + costBoth.outputTokens - (costNow.inputTokens + costNow.outputTokens), compiles: costBoth.compiles - costNow.compiles,
251
- }),
252
- },
253
- name: d.name, phase: d.phase, description: d.description, source: link(d.source), item: d.item, finishedWord: d.finishedWord,
254
- usesAgents: declaredSteps.some((s) => s.agent),
255
- limits: input.limitsOf(d.name),
256
- paused: input.pausedOf(d.name),
257
- total: steps.reduce((t, s) => (s.cost === null ? t : addCosts(t, s.cost)), NO_COST),
258
- graph: graphs.get(d.name)!,
259
- queue: queues.get(d.name) ?? null,
260
- };
261
- };
262
-
263
- const phases = [...input.phases, ...[...new Set(input.declared.map((d) => d.phase))].filter((p) => !input.phases.includes(p))];
308
+ const declaredSteps = d.steps();
309
+ const onlyInRecords = [...new Set(own.map((r) => r.step))].filter((s) => !declaredSteps.some((x) => x.name === s));
310
+ const steps: ReadonlyArray<StepView> = [
311
+ ...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)) })),
312
+ ...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)) })),
313
+ ];
314
+ const funnel = stepFunnel({ declaration: d, records: funnelRecords.records, outcomes: funnelRecords.outcomes, unfinished: going, hours: input.hours, since, now: input.now });
315
+ const funnelBefore = stepFunnel({ declaration: d, records: funnelBoth.records, outcomes: funnelBoth.outcomes, unfinished: going, hours: input.hours, since: before, now: since });
316
+ const costNow = costOf(own) ?? NO_COST;
317
+ const costBoth = costOf(ownBoth) ?? NO_COST;
318
+ const finished = (outcomes: ReadonlyArray<RunOutcomeRow>) => outcomes.filter((o) => o.finished === 1).length;
264
319
  return {
265
- at: input.now, hours: input.hours, since,
266
- groups: phases.map((phase) => ({ phase, workflows: input.declared.filter((d) => d.phase === phase).map(workflowView) })).filter((g) => g.workflows.length > 0),
320
+ workflow: d.name, at: input.now, hours: input.hours, since,
321
+ steps,
322
+ total: steps.reduce((t, s) => (s.cost === null ? t : addCosts(t, s.cost)), NO_COST),
323
+ graph: workflowGraphs({ declared: [d], taken: moves.edges, lasts: moves.lasts, going })[0]!,
324
+ funnel,
325
+ runCosts: runCostsOf(runCosts),
326
+ yields: {
327
+ now: yieldOf({ runs: funnel.runs, finished: finished(finishedNow), tokens: costNow.inputTokens + costNow.outputTokens, compiles: costNow.compiles }),
328
+ before: yieldOf({
329
+ runs: funnelBefore.runs, finished: finished(finishedBoth) - finished(finishedNow),
330
+ tokens: costBoth.inputTokens + costBoth.outputTokens - (costNow.inputTokens + costNow.outputTokens), compiles: costBoth.compiles - costNow.compiles,
331
+ }),
332
+ },
267
333
  };
268
334
  });