@jive-sh/workflow-platform 0.3.3 → 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.
Files changed (61) hide show
  1. package/README.md +68 -23
  2. package/dist/dashboard/agent-usage.js +25 -0
  3. package/dist/dashboard/agent-usage.js.map +1 -0
  4. package/dist/dashboard/drilldown.js +16 -0
  5. package/dist/dashboard/drilldown.js.map +1 -0
  6. package/dist/dashboard/queue-lines.js +44 -0
  7. package/dist/dashboard/queue-lines.js.map +1 -0
  8. package/dist/index.js +8 -6
  9. package/dist/index.js.map +1 -1
  10. package/dist/keep-running.js +1 -1
  11. package/dist/keep-running.js.map +1 -1
  12. package/dist/pick-wakes.js +4 -4
  13. package/dist/pick-wakes.js.map +1 -1
  14. package/dist/queue-counts.js +7 -9
  15. package/dist/queue-counts.js.map +1 -1
  16. package/dist/steps.js +157 -41
  17. package/dist/steps.js.map +1 -1
  18. package/dist/storage/sqlite-store.js +117 -38
  19. package/dist/storage/sqlite-store.js.map +1 -1
  20. package/dist/storage/task-store.js +14 -1
  21. package/dist/storage/task-store.js.map +1 -1
  22. package/dist/tasks.js +215 -267
  23. package/dist/tasks.js.map +1 -1
  24. package/dist/views/runs-now.js +3 -2
  25. package/dist/views/runs-now.js.map +1 -1
  26. package/dist/views/step-funnel.js +1 -1
  27. package/dist/views/step-funnel.js.map +1 -1
  28. package/dist/views/step-graph.js +1 -1
  29. package/dist/views/step-graph.js.map +1 -1
  30. package/dist/views/workflow-flows.js +2 -2
  31. package/dist/views/workflow-flows.js.map +1 -1
  32. package/dist/views/workflows-view.js +83 -17
  33. package/dist/views/workflows-view.js.map +1 -1
  34. package/package.json +1 -1
  35. package/src/dashboard/agent-usage-line.tsx +28 -0
  36. package/src/dashboard/agent-usage.ts +48 -0
  37. package/src/dashboard/dashboard.css +70 -29
  38. package/src/dashboard/drilldown.ts +43 -0
  39. package/src/dashboard/index.tsx +1 -0
  40. package/src/dashboard/machine-controls.tsx +53 -0
  41. package/src/dashboard/queue-bar.tsx +43 -0
  42. package/src/dashboard/queue-chart.tsx +89 -76
  43. package/src/dashboard/queue-lines.ts +87 -0
  44. package/src/dashboard/step-graph.tsx +18 -10
  45. package/src/dashboard/workflow-dag.tsx +31 -103
  46. package/src/dashboard/workflow-drilldown.tsx +89 -0
  47. package/src/dashboard/workflows-dashboard.tsx +117 -42
  48. package/src/index.ts +11 -9
  49. package/src/keep-running.ts +3 -3
  50. package/src/pick-wakes.ts +14 -9
  51. package/src/queue-counts.ts +10 -14
  52. package/src/steps.ts +213 -52
  53. package/src/storage/sqlite-store.ts +126 -39
  54. package/src/storage/step-records.ts +14 -3
  55. package/src/storage/task-store.ts +97 -22
  56. package/src/tasks.ts +235 -301
  57. package/src/views/runs-now.ts +5 -4
  58. package/src/views/step-funnel.ts +2 -2
  59. package/src/views/step-graph.ts +4 -4
  60. package/src/views/workflow-flows.ts +7 -7
  61. package/src/views/workflows-view.ts +117 -35
@@ -29,7 +29,7 @@ export interface WorkflowRunsNow {
29
29
  readonly workflow: string;
30
30
  /** Every unfinished run: its steps' `inStep` and `waitingAfter`, and `notStarted`. */
31
31
  readonly runs: number;
32
- /** Runs in an agent step now: the agent steps' `inStep`. */
32
+ /** Runs an agent is working for now: those in an agent step that wait on nothing (a run waiting there for a free agent is on none). */
33
33
  readonly onAgents: number;
34
34
  /** Runs with no step record yet. */
35
35
  readonly notStarted: number;
@@ -44,7 +44,7 @@ export interface PhaseRunsNow<Phase extends string = string> {
44
44
  readonly workflows: ReadonlyArray<WorkflowRunsNow>;
45
45
  }
46
46
 
47
- type Declared<Phase extends string> = { readonly name: string; readonly phase: Phase; readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'agent'>> };
47
+ type Declared<Phase extends string> = { readonly name: string; readonly phase: Phase; readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'runs'>> };
48
48
 
49
49
  const sum = <T>(xs: ReadonlyArray<T>, f: (x: T) => number) => xs.reduce((a, x) => a + f(x), 0);
50
50
 
@@ -53,7 +53,7 @@ const workflowRunsNow = <Phase extends string>(d: Declared<Phase>, runs: Readonl
53
53
  const r = newest.get(run);
54
54
  return r === undefined ? [] : [r];
55
55
  });
56
- const declared = d.steps().map((s) => ({ name: s.name, agent: s.agent }));
56
+ const declared = d.steps().map((s) => ({ name: s.name, agent: s.runs === 'agentic' }));
57
57
  const undeclared = [...new Set(records.map((r) => r.step))].filter((name) => !declared.some((s) => s.name === name))
58
58
  .map((name) => ({ name, agent: records.some((r) => r.step === name && r.agent === 1) }));
59
59
  const steps = [...declared, ...undeclared].map((s): StepRunsNow => {
@@ -70,7 +70,8 @@ const workflowRunsNow = <Phase extends string>(d: Declared<Phase>, runs: Readonl
70
70
  return {
71
71
  workflow: d.name,
72
72
  runs: runs.length,
73
- onAgents: sum(steps.filter((s) => s.agent), (s) => s.inStep),
73
+ // A run waiting at an agent step for a free agent (its record says what it waits on) is on no agent.
74
+ onAgents: steps.filter((s) => s.agent).reduce((n, s) => n + records.filter((r) => r.step === s.step && r.ended === null && r.waiting === null).length, 0),
74
75
  notStarted: runs.length - records.length,
75
76
  steps,
76
77
  };
@@ -62,7 +62,7 @@ export const FUNNEL_WINDOW_HOURS = [1, 24] as const;
62
62
  /** Reasons listed under the step most runs stopped at; the rest are counted together. */
63
63
  export const TOP_STOP_REASONS = 3;
64
64
 
65
- type Declared = { readonly item: string | null } & { readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'agent'>> };
65
+ type Declared = { readonly item: string | null } & { readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'runs'>> };
66
66
 
67
67
  /** The commonest of a run's item outcomes, so a batch run gives one reason. */
68
68
  const commonest = (xs: ReadonlyArray<string>): string => {
@@ -123,7 +123,7 @@ export const stepFunnel = (input: {
123
123
  return { step: last.step, finished, reason: finished ? null : stopReason(last, theirs) };
124
124
  });
125
125
 
126
- const declared = input.declaration.steps().map((s) => ({ name: s.name, agent: s.agent }));
126
+ const declared = input.declaration.steps().map((s) => ({ name: s.name, agent: s.runs === 'agentic' }));
127
127
  const undeclared = [...new Set(runs.flatMap(([, rs]) => rs.map((r) => r.step)))].filter((name) => !declared.some((s) => s.name === name))
128
128
  .map((name) => ({ name, agent: runs.some(([, rs]) => rs.some((r) => r.step === name && r.agent === 1)) }));
129
129
  const counted = [...declared, ...undeclared].map((s) => {
@@ -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
  };
@@ -3,14 +3,14 @@
3
3
  * Built from what the platform knows and nothing written for the page:
4
4
  *
5
5
  * feeds a workflow declares the other among those whose results it works on (`fedBy`);
6
- * sends back a workflow gave new input (`giveNewInput`) to a task of one that is upstream of it, through feeds;
7
- * gives input it gave new input to a task of one that is not upstream of it, and no feed is declared between them;
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
8
  * waits on its list puts tasks on a wait that names another workflow (`PendingWork.waits`), not the one that feeds it:
9
9
  * the list's own words say why (`why`).
10
10
  *
11
11
  * Its place: its stage (the project's phases, in order), then how many feeds lead into it within its stage. Pure.
12
12
  */
13
- import type { ListedWait, NewInputFlow } from '../storage/task-store';
13
+ import type { ListedWait, ReturnedBetween } from '../storage/task-store';
14
14
 
15
15
  export type FlowKind = 'feeds' | 'sends back' | 'gives input' | 'waits on';
16
16
 
@@ -21,7 +21,7 @@ export interface WorkflowFlow {
21
21
  readonly kind: FlowKind;
22
22
  /** Whether a declaration says it (`fedBy`); a recorded one only is not declared. */
23
23
  readonly declared: boolean;
24
- /** The new-input notes recorded along it, and the last one's time; 0 and none where none was recorded. */
24
+ /** The returns to the queue recorded along it, and the last one's time; 0 and none where none was recorded. */
25
25
  readonly notes: number;
26
26
  readonly last: number | null;
27
27
  /** Along a `waits on` edge: the tasks waiting, and the list's words for the wait; 0 and none on any other. */
@@ -54,17 +54,17 @@ const reaches = (feeds: ReadonlyArray<{ readonly from: string; readonly to: stri
54
54
 
55
55
  export const workflowFlows = (input: {
56
56
  readonly declared: ReadonlyArray<{ readonly name: string; readonly fedBy?: ReadonlyArray<string> }>;
57
- readonly newInputs: ReadonlyArray<NewInputFlow>;
57
+ readonly returned: ReadonlyArray<ReturnedBetween>;
58
58
  readonly waits: ReadonlyArray<ListedWait>;
59
59
  }): ReadonlyArray<WorkflowFlow> => {
60
60
  const names = new Set(input.declared.map((d) => d.name));
61
- const recorded = (from: string, to: string) => input.newInputs.find((n) => n.from === from && n.to === to);
61
+ const recorded = (from: string, to: string) => input.returned.find((n) => n.from === from && n.to === to);
62
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
63
  const declaredFlows: ReadonlyArray<WorkflowFlow> = feeds.map((f) => {
64
64
  const r = recorded(f.from, f.to);
65
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
66
  });
67
- const recordedOnly: ReadonlyArray<WorkflowFlow> = input.newInputs
67
+ const recordedOnly: ReadonlyArray<WorkflowFlow> = input.returned
68
68
  .filter((n) => names.has(n.from) && names.has(n.to) && !feeds.some((f) => f.from === n.from && f.to === n.to))
69
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
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.
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * A PROJECT'S WORKFLOWS AS ITS DASHBOARD SHOWS THEM, READ FROM THE RECORDS AT EACH REQUEST, in two readings:
3
3
  *
4
- * every workflow what it is, its limits, its queue's buckets and their history, and where work goes between workflows
4
+ * every workflow what it is, its steps with their kinds, its limits, and its tasks by status with their history
5
5
  * (`buildWorkflowsView`): what a page shows before a workflow is opened, a few small readings.
6
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
7
  * runs stopped and why (`buildWorkflowFigures`): read from its own records when a page opens it.
@@ -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 type { SourceRef, StepDeclaration, 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 {
@@ -21,11 +22,10 @@ import { Effect } from 'effect';
21
22
  import type { StorageError } from '../storage/storage-error';
22
23
  import type { EngineRecords } from '../storage/engine-records';
23
24
  import type { FunnelStepRecord, RunCost, RunOutcomeRow, StepRecords, StepRunRow } from '../storage/step-records';
24
- import { BUCKETS, type Bucket } from '../tasks';
25
+ import { TASK_STATUSES, type BlockedReason, type TaskStatus } from '../tasks';
25
26
  import type { QueueCount, TaskStore } from '../storage/task-store';
26
27
  import { stepFunnel, type StepFunnel } from './step-funnel';
27
28
  import { workflowGraphs, type WorkflowGraph } from './step-graph';
28
- import { pipelinePlaces, workflowFlows, type PipelinePlace, type WorkflowFlow } from './workflow-flows';
29
29
 
30
30
  /**
31
31
  * WHAT STARTS ITS RUNS, from its declaration: its queue (a list of work, its runs kept going while it has some), its inputs
@@ -58,7 +58,8 @@ export interface StepCost {
58
58
 
59
59
  export interface StepView {
60
60
  readonly name: string;
61
- 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;
62
63
  readonly description: string;
63
64
  readonly source: SourceLink;
64
65
  /** Whether its declaration lists it; a step only the records name is no longer declared. */
@@ -144,9 +145,35 @@ export const yieldOf = (r: { readonly runs: number; readonly finished: number; r
144
145
  perCompile: r.compiles === 0 ? null : r.finished / r.compiles,
145
146
  });
146
147
 
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. */
148
+ /**
149
+ * A step as a workflow's own view lists it: its kind in words (`steps.ts` `StepKind`), and for a sub-workflow the workflow
150
+ * it opens.
151
+ */
152
+ export interface StepSummary {
153
+ readonly name: string;
154
+ readonly kind: StepKind;
155
+ /** How it runs; none for a sub-workflow step, whose own steps say theirs. */
156
+ readonly runs: StepRuns | null;
157
+ readonly description: string;
158
+ /** For a sub-workflow: the workflow it runs, by name; none for any other kind. */
159
+ readonly workflow: string | null;
160
+ }
161
+
162
+ /**
163
+ * THE TOP OF A PROJECT IS ONE WORKFLOW (Alex, 2026-11-19: "decompiling an entire game is in itself a workflow"): what a page
164
+ * enters at. Its steps are sub-workflows, and each opens that workflow's own view.
165
+ */
166
+ export interface TopWorkflowView {
167
+ readonly name: string;
168
+ readonly description: string;
169
+ readonly steps: ReadonlyArray<StepSummary>;
170
+ }
171
+
172
+ /** A workflow as a page shows it before it is opened: what it is, its steps, its limits and its queue. */
148
173
  export interface WorkflowView {
149
174
  readonly name: string;
175
+ /** Its own steps, in order, each with its kind. */
176
+ readonly steps: ReadonlyArray<StepSummary>;
150
177
  readonly phase: string;
151
178
  readonly description: string;
152
179
  readonly source: SourceLink;
@@ -160,25 +187,24 @@ export interface WorkflowView {
160
187
  * any other). None where no store is read.
161
188
  */
162
189
  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;
190
+ /** Its tasks by status, as last counted (`queue-counts.ts`); none for a workflow with no item. */
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;
165
194
  /** What starts its runs. */
166
195
  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
196
  }
172
197
 
173
198
  /** Every workflow, read at the request: nothing here is stored for the page. */
174
199
  export interface WorkflowsView {
175
200
  /** When it was read, unix seconds. */
176
201
  readonly at: number;
202
+ readonly top: TopWorkflowView;
177
203
  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. */
204
+ /** Each queue's counts by status over the last `QUEUE_HISTORY_DAYS` (`historyByStatus`), a row where one changed, oldest first. */
181
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>;
182
208
  }
183
209
 
184
210
  /** One workflow's figures over a window, read from its own records when it is opened. */
@@ -197,6 +223,8 @@ export interface WorkflowFigures {
197
223
  readonly runCosts: RunCosts;
198
224
  /** What its runs gave in the window, and in the window before it: the trend. */
199
225
  readonly yields: { readonly now: Yield; readonly before: Yield };
226
+ /** Why its blocked tasks are blocked now: a count for each reason, most tasks first (`tasks.ts` `blockedReasons`). */
227
+ readonly blocked: ReadonlyArray<BlockedReason>;
200
228
  }
201
229
 
202
230
  const NO_COST: StepCost = { calls: 0, failed: 0, open: 0, seconds: 0, inputTokens: 0, outputTokens: 0, cpuSeconds: 0, compiles: 0, items: 0 };
@@ -221,18 +249,57 @@ export const costOf = (rows: ReadonlyArray<StepRunRow>): StepCost | null =>
221
249
  items: new Set(rows.flatMap((r) => (r.subject === null ? [] : [r.subject.toLowerCase()]))).size,
222
250
  };
223
251
 
224
- type Declared = Pick<WorkflowDeclaration<string, unknown>, 'name' | 'phase' | 'description' | 'source' | 'item' | 'finishedWord' | 'graph' | 'fedBy' | 'pending' | 'inputs'> & {
252
+ type Declared = Pick<WorkflowDeclaration<string, unknown>, 'name' | 'phase' | 'description' | 'source' | 'item' | 'finishedWord' | 'graph' | 'fedBy' | 'pending' | 'items' | 'inputs'> & {
225
253
  readonly steps: () => ReadonlyArray<StepDeclaration>;
226
254
  };
227
255
 
228
- const noTasks = (): Record<Bucket, number> => Object.fromEntries(BUCKETS.map((b) => [b, 0])) as Record<Bucket, number>;
256
+ const noTasks = (): Record<TaskStatus, number> => Object.fromEntries(TASK_STATUSES.map((s) => [s, 0])) as Record<TaskStatus, number>;
257
+
258
+ /**
259
+ * The names a count was recorded under before 2026-11-19, when a task stood in one of six buckets, and the status each is
260
+ * part of now. `queued` and `done` kept their names.
261
+ */
262
+ const RECORDED_BEFORE: Readonly<Record<string, TaskStatus>> = { running: 'in progress', waiting: 'blocked', stuck: 'blocked', held: 'blocked' };
263
+
264
+ /**
265
+ * THE RECORDED COUNTS (`queueCountsSince`, oldest first) AS THE FOUR STATUSES: a row for each status of each workflow where
266
+ * its count moved. A count recorded under an older name is part of its status until the workflow has a count under the
267
+ * four, which replaces it; one recorded under an older name after that (by code still running as the newer began) is
268
+ * not read. Pure.
269
+ */
270
+ export const historyByStatus = (recorded: ReadonlyArray<QueueCount>): ReadonlyArray<QueueCount> => {
271
+ const raw = new Map<string, Map<string, number>>();
272
+ const countedByStatus = new Set<string>();
273
+ const last = new Map<string, number>();
274
+ const out: QueueCount[] = [];
275
+ recorded.forEach((row, i) => {
276
+ const counts = raw.get(row.workflow) ?? new Map<string, number>();
277
+ raw.set(row.workflow, counts);
278
+ const olderName = RECORDED_BEFORE[row.bucket] !== undefined;
279
+ if (!olderName && (row.bucket === 'in progress' || row.bucket === 'blocked') && !countedByStatus.has(row.workflow)) {
280
+ countedByStatus.add(row.workflow);
281
+ for (const older of Object.keys(RECORDED_BEFORE)) counts.delete(older);
282
+ }
283
+ if (!(olderName && countedByStatus.has(row.workflow))) counts.set(row.bucket, row.tasks);
284
+ // Every row of one moment is one count of the workflow: its statuses are read once all of them are in.
285
+ const next = recorded[i + 1];
286
+ if (next !== undefined && next.workflow === row.workflow && next.at === row.at) return;
287
+ for (const status of TASK_STATUSES) {
288
+ const tasks = [...counts].reduce((sum, [name, n]) => sum + (name === status || RECORDED_BEFORE[name] === status ? n : 0), 0);
289
+ const key = `${row.workflow}\u0000${status}`;
290
+ if (last.get(key) !== tasks) out.push({ at: row.at, workflow: row.workflow, bucket: status, tasks });
291
+ last.set(key, tasks);
292
+ }
293
+ });
294
+ return out;
295
+ };
229
296
 
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>>();
297
+ /** Each queue's newest count from its history by status (`historyByStatus`), a status never counted at 0. Pure. */
298
+ export const queuesNow = (history: ReadonlyArray<QueueCount>): ReadonlyMap<string, Readonly<Record<TaskStatus, number>>> => {
299
+ const queues = new Map<string, Record<TaskStatus, number>>();
233
300
  for (const row of history) {
234
301
  const queue = queues.get(row.workflow) ?? noTasks();
235
- if ((BUCKETS as ReadonlyArray<string>).includes(row.bucket)) queue[row.bucket as Bucket] = row.tasks;
302
+ if ((TASK_STATUSES as ReadonlyArray<string>).includes(row.bucket)) queue[row.bucket as TaskStatus] = row.tasks;
236
303
  queues.set(row.workflow, queue);
237
304
  }
238
305
  return queues;
@@ -240,7 +307,7 @@ export const queuesNow = (history: ReadonlyArray<QueueCount>): ReadonlyMap<strin
240
307
 
241
308
  /**
242
309
  * 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
310
+ * counts as last counted and their history. A few small readings of the store, so a
244
311
  * page load needs no stored copy of it. `limitsOf` is none where no store is read.
245
312
  */
246
313
  export const buildWorkflowsView = (input: {
@@ -248,31 +315,43 @@ export const buildWorkflowsView = (input: {
248
315
  /** The project's phases, in the order its page shows them; a workflow of another phase follows them. */
249
316
  readonly phases: ReadonlyArray<string>;
250
317
  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'>;
318
+ /** The queues' history, from the task store. */
319
+ readonly taskStore: Pick<TaskStore, 'queueCountsSince' | 'returnedBetween' | 'listedWaits'>;
253
320
  /** The line a definition's text is on now. */
254
321
  readonly locate: (source: SourceRef) => number | null;
255
322
  readonly limitsOf: (workflow: string) => WorkflowView['limits'];
323
+ /** The project's top workflow, in its own words, until it declares one. */
324
+ readonly top: { readonly name: string; readonly description: string };
256
325
  }): Effect.Effect<WorkflowsView, StorageError> => Effect.gen(function* () {
257
- const flows = workflowFlows({ declared: input.declared, newInputs: yield* input.taskStore.newInputFlows(), waits: yield* input.taskStore.listedWaits() });
326
+ const stepSummary = (s: StepDeclaration): StepSummary => ({ name: s.name, kind: stepKindOf(s), runs: s.runs, description: s.description, workflow: s.workflow ?? null });
327
+ // Until the project declares its top workflow, its steps are every workflow no other workflow uses as a step.
328
+ const usedAsAStep = new Set(input.declared.flatMap((d) => d.steps().flatMap((s) => (s.workflow === undefined ? [] : [s.workflow]))));
258
329
  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);
330
+ const queueHistory = historyByStatus(yield* input.taskStore.queueCountsSince(input.now - QUEUE_HISTORY_DAYS * 86_400));
261
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]));
262
336
  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),
337
+ name: d.name, steps: d.steps().map(stepSummary),
338
+ phase: d.phase, description: d.description, source: { ...d.source, line: input.locate(d.source) }, item: d.item, finishedWord: d.finishedWord,
339
+ usesAgents: d.steps().some(stepUsesAnAgent),
265
340
  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
341
  place: places.get(d.name)!,
342
+ queue: d.item === null ? null : queues.get(d.name) ?? noTasks(),
343
+ startsOn: d.pending !== undefined || d.items !== undefined ? { kind: 'queue' } : d.inputs !== undefined ? { kind: 'inputs', says: d.inputs.says } : { kind: 'timer or request' },
270
344
  });
345
+ const inPageOrder = phases.flatMap((phase) => input.declared.filter((d) => d.phase === phase));
271
346
  return {
272
347
  at: input.now,
348
+ top: {
349
+ ...input.top,
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 })),
351
+ },
273
352
  groups: phases.map((phase) => ({ phase, workflows: input.declared.filter((d) => d.phase === phase).map(workflowView) })).filter((g) => g.workflows.length > 0),
274
- flows,
275
353
  queueHistory,
354
+ flows,
276
355
  };
277
356
  });
278
357
 
@@ -287,6 +366,8 @@ export const buildWorkflowFigures = (input: {
287
366
  readonly hours: number;
288
367
  readonly records: StepRecords;
289
368
  readonly engine: EngineRecords;
369
+ /** Why its blocked tasks are blocked now, read from its tasks. */
370
+ readonly blocked: Effect.Effect<ReadonlyArray<BlockedReason>, StorageError>;
290
371
  /** The line a definition's text is on now. */
291
372
  readonly locate: (source: SourceRef) => number | null;
292
373
  }): Effect.Effect<WorkflowFigures, StorageError> => Effect.gen(function* () {
@@ -308,8 +389,8 @@ export const buildWorkflowFigures = (input: {
308
389
  const declaredSteps = d.steps();
309
390
  const onlyInRecords = [...new Set(own.map((r) => r.step))].filter((s) => !declaredSteps.some((x) => x.name === s));
310
391
  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)) })),
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)) })),
313
394
  ];
314
395
  const funnel = stepFunnel({ declaration: d, records: funnelRecords.records, outcomes: funnelRecords.outcomes, unfinished: going, hours: input.hours, since, now: input.now });
315
396
  const funnelBefore = stepFunnel({ declaration: d, records: funnelBoth.records, outcomes: funnelBoth.outcomes, unfinished: going, hours: input.hours, since: before, now: since });
@@ -323,6 +404,7 @@ export const buildWorkflowFigures = (input: {
323
404
  graph: workflowGraphs({ declared: [d], taken: moves.edges, lasts: moves.lasts, going })[0]!,
324
405
  funnel,
325
406
  runCosts: runCostsOf(runCosts),
407
+ blocked: yield* input.blocked,
326
408
  yields: {
327
409
  now: yieldOf({ runs: funnel.runs, finished: finished(finishedNow), tokens: costNow.inputTokens + costNow.outputTokens, compiles: costNow.compiles }),
328
410
  before: yieldOf({