@jive-sh/workflow-platform 0.7.0 → 1.1.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 (195) hide show
  1. package/README.md +70 -435
  2. package/component/_generated/api.ts +72 -0
  3. package/component/_generated/component.ts +629 -0
  4. package/component/_generated/dataModel.ts +60 -0
  5. package/component/_generated/server.ts +169 -0
  6. package/component/convex.config.ts +3 -0
  7. package/component/engine.ts +756 -0
  8. package/component/errors.ts +66 -0
  9. package/component/events.ts +17 -0
  10. package/component/notes.ts +19 -0
  11. package/component/places.ts +136 -0
  12. package/component/retention.ts +205 -0
  13. package/component/runEngine.ts +25 -0
  14. package/component/schema.ts +135 -0
  15. package/component/steps.ts +188 -0
  16. package/component/store.ts +513 -0
  17. package/component/tasks.ts +290 -0
  18. package/component/worker.ts +109 -0
  19. package/component/workflows.ts +83 -0
  20. package/dist/definition/agent-runner.js +9 -0
  21. package/dist/definition/agent-runner.js.map +1 -0
  22. package/dist/definition/file-store.js +9 -0
  23. package/dist/definition/file-store.js.map +1 -0
  24. package/dist/definition/program-build.js +10 -0
  25. package/dist/definition/program-build.js.map +1 -0
  26. package/dist/definition/step.js +1 -0
  27. package/dist/definition/step.js.map +1 -0
  28. package/dist/definition/task-schema.js +5 -0
  29. package/dist/definition/task-schema.js.map +1 -0
  30. package/dist/definition/tasks.js +9 -0
  31. package/dist/definition/tasks.js.map +1 -0
  32. package/dist/definition/workflow.js +117 -0
  33. package/dist/definition/workflow.js.map +1 -0
  34. package/dist/graph.js +310 -9
  35. package/dist/graph.js.map +1 -1
  36. package/dist/index.js +7 -196
  37. package/dist/index.js.map +1 -1
  38. package/package.json +9 -4
  39. package/src/definition/agent-runner.ts +59 -0
  40. package/src/definition/file-store.ts +20 -0
  41. package/src/definition/program-build.ts +30 -0
  42. package/src/definition/step.ts +106 -0
  43. package/src/definition/task-schema.ts +15 -0
  44. package/src/definition/tasks.ts +43 -0
  45. package/src/definition/workflow.ts +262 -0
  46. package/src/graph.ts +352 -19
  47. package/src/index.ts +8 -238
  48. package/worker/directory-file-store.ts +42 -0
  49. package/worker/index.ts +12 -0
  50. package/worker/platform-client.ts +144 -0
  51. package/worker/step-meter.ts +80 -0
  52. package/worker/worker-tasks.ts +27 -0
  53. package/worker/worker.test.ts +200 -0
  54. package/worker/worker.ts +240 -0
  55. package/dist/budget-states.js +0 -30
  56. package/dist/budget-states.js.map +0 -1
  57. package/dist/cpu-limit/limiter.js +0 -839
  58. package/dist/cpu-limit/limiter.js.map +0 -1
  59. package/dist/cpu-limit/use.js +0 -139
  60. package/dist/cpu-limit/use.js.map +0 -1
  61. package/dist/dashboard/agent-usage.js +0 -25
  62. package/dist/dashboard/agent-usage.js.map +0 -1
  63. package/dist/dashboard/dashboard.css +0 -148
  64. package/dist/dashboard/drilldown.js +0 -47
  65. package/dist/dashboard/drilldown.js.map +0 -1
  66. package/dist/dashboard/queue-lines.js +0 -44
  67. package/dist/dashboard/queue-lines.js.map +0 -1
  68. package/dist/dashboard/queue-table.js +0 -16
  69. package/dist/dashboard/queue-table.js.map +0 -1
  70. package/dist/dashboard/run-doing-words.js +0 -29
  71. package/dist/dashboard/run-doing-words.js.map +0 -1
  72. package/dist/dashboard/setting-words.js +0 -18
  73. package/dist/dashboard/setting-words.js.map +0 -1
  74. package/dist/dashboard/spending-words.js +0 -24
  75. package/dist/dashboard/spending-words.js.map +0 -1
  76. package/dist/failure.js +0 -35
  77. package/dist/failure.js.map +0 -1
  78. package/dist/keep-running.js +0 -79
  79. package/dist/keep-running.js.map +0 -1
  80. package/dist/outside-failure.js +0 -31
  81. package/dist/outside-failure.js.map +0 -1
  82. package/dist/pick-wakes.js +0 -125
  83. package/dist/pick-wakes.js.map +0 -1
  84. package/dist/project.js +0 -1
  85. package/dist/project.js.map +0 -1
  86. package/dist/queue-counts.js +0 -51
  87. package/dist/queue-counts.js.map +0 -1
  88. package/dist/reasons.js +0 -33
  89. package/dist/reasons.js.map +0 -1
  90. package/dist/retention.js +0 -59
  91. package/dist/retention.js.map +0 -1
  92. package/dist/settings.js +0 -88
  93. package/dist/settings.js.map +0 -1
  94. package/dist/spending-limit.js +0 -82
  95. package/dist/spending-limit.js.map +0 -1
  96. package/dist/step-files.js +0 -200
  97. package/dist/step-files.js.map +0 -1
  98. package/dist/step-meter.js +0 -97
  99. package/dist/step-meter.js.map +0 -1
  100. package/dist/step-times.js +0 -114
  101. package/dist/step-times.js.map +0 -1
  102. package/dist/step-usage.js +0 -19
  103. package/dist/step-usage.js.map +0 -1
  104. package/dist/steps.js +0 -1205
  105. package/dist/steps.js.map +0 -1
  106. package/dist/storage/agent-claims.js +0 -3
  107. package/dist/storage/agent-claims.js.map +0 -1
  108. package/dist/storage/budgets.js +0 -121
  109. package/dist/storage/budgets.js.map +0 -1
  110. package/dist/storage/engine-records.js +0 -91
  111. package/dist/storage/engine-records.js.map +0 -1
  112. package/dist/storage/engine-storage.js +0 -67
  113. package/dist/storage/engine-storage.js.map +0 -1
  114. package/dist/storage/input-records.js +0 -12
  115. package/dist/storage/input-records.js.map +0 -1
  116. package/dist/storage/move-store.js +0 -251
  117. package/dist/storage/move-store.js.map +0 -1
  118. package/dist/storage/sqlite-store.js +0 -901
  119. package/dist/storage/sqlite-store.js.map +0 -1
  120. package/dist/storage/step-records.js +0 -11
  121. package/dist/storage/step-records.js.map +0 -1
  122. package/dist/storage/storage-error.js +0 -19
  123. package/dist/storage/storage-error.js.map +0 -1
  124. package/dist/storage/task-store.js +0 -30
  125. package/dist/storage/task-store.js.map +0 -1
  126. package/dist/system-workflows.js +0 -54
  127. package/dist/system-workflows.js.map +0 -1
  128. package/dist/tasks.js +0 -500
  129. package/dist/tasks.js.map +0 -1
  130. package/dist/views/format.js +0 -37
  131. package/dist/views/format.js.map +0 -1
  132. package/dist/views/runs-now.js +0 -117
  133. package/dist/views/runs-now.js.map +0 -1
  134. package/dist/views/step-funnel.js +0 -98
  135. package/dist/views/step-funnel.js.map +0 -1
  136. package/dist/views/step-graph.js +0 -39
  137. package/dist/views/step-graph.js.map +0 -1
  138. package/dist/views/workflows-view.js +0 -236
  139. package/dist/views/workflows-view.js.map +0 -1
  140. package/src/budget-states.ts +0 -54
  141. package/src/cpu-limit/limiter.ts +0 -935
  142. package/src/cpu-limit/use.ts +0 -166
  143. package/src/dashboard/agent-usage-line.tsx +0 -28
  144. package/src/dashboard/agent-usage.ts +0 -48
  145. package/src/dashboard/dashboard.css +0 -148
  146. package/src/dashboard/drilldown.ts +0 -70
  147. package/src/dashboard/index.tsx +0 -4
  148. package/src/dashboard/limits-and-runs.tsx +0 -191
  149. package/src/dashboard/machine-controls.tsx +0 -53
  150. package/src/dashboard/queue-bar.tsx +0 -62
  151. package/src/dashboard/queue-chart.tsx +0 -149
  152. package/src/dashboard/queue-lines.ts +0 -87
  153. package/src/dashboard/queue-table.ts +0 -37
  154. package/src/dashboard/run-doing-words.ts +0 -41
  155. package/src/dashboard/setting-words.ts +0 -30
  156. package/src/dashboard/spending-limit-control.tsx +0 -47
  157. package/src/dashboard/spending-words.ts +0 -32
  158. package/src/dashboard/time-distribution.tsx +0 -61
  159. package/src/dashboard/workflow-dag.tsx +0 -322
  160. package/src/dashboard/workflow-drilldown.tsx +0 -31
  161. package/src/dashboard/workflows-dashboard.tsx +0 -532
  162. package/src/failure.ts +0 -36
  163. package/src/keep-running.ts +0 -85
  164. package/src/outside-failure.ts +0 -39
  165. package/src/pick-wakes.ts +0 -182
  166. package/src/project.ts +0 -127
  167. package/src/queue-counts.ts +0 -64
  168. package/src/reasons.ts +0 -30
  169. package/src/retention.ts +0 -90
  170. package/src/settings.ts +0 -171
  171. package/src/spending-limit.ts +0 -139
  172. package/src/step-files.ts +0 -208
  173. package/src/step-meter.ts +0 -120
  174. package/src/step-times.ts +0 -151
  175. package/src/step-usage.ts +0 -23
  176. package/src/steps.ts +0 -1616
  177. package/src/storage/agent-claims.ts +0 -63
  178. package/src/storage/budgets.ts +0 -152
  179. package/src/storage/engine-records.ts +0 -119
  180. package/src/storage/engine-storage.ts +0 -82
  181. package/src/storage/input-records.ts +0 -43
  182. package/src/storage/move-store.ts +0 -265
  183. package/src/storage/sqlite-store.ts +0 -1022
  184. package/src/storage/step-records.ts +0 -358
  185. package/src/storage/storage-error.ts +0 -35
  186. package/src/storage/task-store.ts +0 -235
  187. package/src/system-workflows.ts +0 -70
  188. package/src/tasks.ts +0 -586
  189. package/src/views/format.ts +0 -40
  190. package/src/views/runs-now.ts +0 -188
  191. package/src/views/step-funnel.ts +0 -163
  192. package/src/views/step-graph.ts +0 -77
  193. package/src/views/workflows-view.ts +0 -484
  194. package/src/workflow-dag.test.tsx +0 -89
  195. package/src/workflow-page.test.tsx +0 -170
@@ -1,188 +0,0 @@
1
- /**
2
- * THE RUNS GOING NOW, by workflow and step: each unfinished run counted once, on the step its newest record names.
3
- * Pure, so it is tested alone; its inputs are the engine's unfinished runs (`EngineRecords.unfinishedRuns`), their newest
4
- * step records (`StepRecords.newestStepRecords`) and the agent claims that count now (`steps.ts` `agentClaimsNow`).
5
- *
6
- * Alex, 2026-11-06: "showing current run counts for each phase and workflow and step, and then this implies a certain
7
- * number of agents running". A run holds its agents from its first agent step to its end (`steps.ts` `claimAgentsOrEnd`; Alex, 2026-11-22:
8
- * "decompile is down one agent"), so a run holding a claim, or in a sub-workflow step whose run holds one, is holding an
9
- * agent whatever step it is in. What was a phase is a workflow made of workflows, counted like any other.
10
- */
11
- import type { AgentClaim } from '../storage/agent-claims';
12
- import type { UnfinishedRun } from '../storage/engine-records';
13
- import type { NewestStepRecord } from '../storage/step-records';
14
- import { isMadeOfWorkflows, type StepDeclaration } from '../steps';
15
-
16
- export interface StepRunsNow {
17
- readonly step: string;
18
- readonly agent: boolean;
19
- /** Runs whose newest record is this step, still open. */
20
- readonly inStep: number;
21
- /** Runs whose newest record is this step, ended, with no next step started. */
22
- readonly waitingAfter: number;
23
- /** When the longest of those ended this step, unix seconds; none with none waiting. */
24
- readonly waitingSince: number | null;
25
- /** What a run in it waits on, in the step's own words (a Pick with nothing it can claim); none while each works. */
26
- readonly waitingOn: string | null;
27
- }
28
-
29
- /**
30
- * What one going run is doing now: holding an agent where it, or the run its open sub-workflow step started, at any depth,
31
- * holds a claim; otherwise read from its newest record, and where that is an open sub-workflow step, from the newest record
32
- * of the run it started. Alex, 2026-11-21, on Decompile's "4 runs going · 1 on agents", its 4 runs all inside its
33
- * sub-workflow step: "how can it be possible that we've been at 4 concurrent non agentic runs going on for hours".
34
- */
35
- export type RunDoing =
36
- /** It, or the run under it, holds an agent claim: in an agent step or not. */
37
- | { readonly kind: 'holding an agent' }
38
- /** Any other open record that waits on nothing, by its step's name. */
39
- | { readonly kind: 'in a step'; readonly step: string }
40
- /** Any other open record that says what it waits on (a Pick with nothing it can claim), by its step's name. */
41
- | { readonly kind: 'waiting at a step'; readonly step: string }
42
- /** Its newest record ended, and no next step started. */
43
- | { readonly kind: 'between steps' }
44
- /** No record yet. */
45
- | { readonly kind: 'not started' };
46
-
47
- /** How many going runs do one thing now. */
48
- export interface RunsDoing {
49
- readonly doing: RunDoing;
50
- readonly runs: number;
51
- }
52
-
53
- export interface WorkflowRunsNow {
54
- readonly workflow: string;
55
- /** Every unfinished run: its steps' `inStep` and `waitingAfter`, and `notStarted`. */
56
- readonly runs: number;
57
- /** What its going runs are doing, each counted once, adding up to `runs`, most engaged first (`RUN_DOING_ORDER`). */
58
- readonly doing: ReadonlyArray<RunsDoing>;
59
- /** The agents its runs hold now: what the claims of `doing`'s runs holding an agent claim. */
60
- readonly onAgents: number;
61
- /** Runs with no step record yet. */
62
- readonly notStarted: number;
63
- /** Its declared steps in order, then any step its records name that it no longer declares. */
64
- readonly steps: ReadonlyArray<StepRunsNow>;
65
- }
66
-
67
- type Declared = { readonly name: string; readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'runs' | 'workflow' | 'runsOnItsOwnTrigger'>> };
68
-
69
- const sum = <T>(xs: ReadonlyArray<T>, f: (x: T) => number) => xs.reduce((a, x) => a + f(x), 0);
70
-
71
- /** The order `doing` lists its kinds in: runs holding an agent first, then other steps, then no step. */
72
- export const RUN_DOING_ORDER: ReadonlyArray<RunDoing['kind']> = ['holding an agent', 'in a step', 'waiting at a step', 'between steps', 'not started'];
73
-
74
- /**
75
- * The agents a run holds: its own claim, the claims of the runs under it (each names the run at its top, which reaches a run
76
- * whose sub-workflow step's record a restart closed while it waited), or the run its open sub-workflow step started, at any
77
- * depth; 0 for none.
78
- */
79
- const agentsHeld = (run: string, newest: ReadonlyMap<string, NewestStepRecord>, claimed: ReadonlyMap<string, number>, depth = 0): number => {
80
- const own = claimed.get(run);
81
- if (own !== undefined) return own;
82
- const r = newest.get(run);
83
- return r === undefined || r.ended !== null || r.childRun === undefined || r.childRun === null || depth > 8 ? 0 : agentsHeld(r.childRun, newest, claimed, depth + 1);
84
- };
85
-
86
- /**
87
- * What a run whose newest record is `r` is doing (`RunDoing`), once it holds no agent. A sub-workflow step's record is read
88
- * through to the run it started: a run inside a sub-workflow's step read as in that step, on the page of the workflow above.
89
- */
90
- const doingOf = (r: NewestStepRecord, newest: ReadonlyMap<string, NewestStepRecord>, depth = 0): RunDoing => {
91
- if (r.ended !== null) return { kind: 'between steps' };
92
- const under = r.childRun === undefined || r.childRun === null || depth > 8 ? undefined : newest.get(r.childRun);
93
- if (under !== undefined) return doingOf(under, newest, depth + 1);
94
- return r.waiting === null ? { kind: 'in a step', step: r.step } : { kind: 'waiting at a step', step: r.step };
95
- };
96
-
97
- /** Counts that do the same thing added together, in `RUN_DOING_ORDER`, a kind's steps in the order first counted. */
98
- const addUpDoing = (counts: ReadonlyArray<RunsDoing>): ReadonlyArray<RunsDoing> => {
99
- const keyOf = (d: RunDoing) => ('step' in d ? `${d.kind}\u0000${d.step}` : d.kind);
100
- const added = new Map<string, RunsDoing>();
101
- for (const c of counts) added.set(keyOf(c.doing), { doing: c.doing, runs: (added.get(keyOf(c.doing))?.runs ?? 0) + c.runs });
102
- return [...added.values()].filter((c) => c.runs > 0).sort((a, b) => RUN_DOING_ORDER.indexOf(a.doing.kind) - RUN_DOING_ORDER.indexOf(b.doing.kind));
103
- };
104
-
105
- const workflowRunsNow = (d: Declared, runs: ReadonlyArray<string>, newest: ReadonlyMap<string, NewestStepRecord>, claimed: ReadonlyMap<string, number>): WorkflowRunsNow => {
106
- const records = runs.flatMap((run) => {
107
- const r = newest.get(run);
108
- return r === undefined ? [] : [r];
109
- });
110
- const held = runs.map((run) => agentsHeld(run, newest, claimed));
111
- const doing = addUpDoing([
112
- ...runs.map((run, i): RunsDoing => {
113
- const r = newest.get(run);
114
- return { doing: held[i]! > 0 ? { kind: 'holding an agent' } : r === undefined ? { kind: 'not started' } : doingOf(r, newest), runs: 1 };
115
- }),
116
- ]);
117
- const declared = d.steps().map((s) => ({ name: s.name, agent: s.runs === 'agentic' }));
118
- const undeclared = [...new Set(records.map((r) => r.step))].filter((name) => !declared.some((s) => s.name === name))
119
- .map((name) => ({ name, agent: records.some((r) => r.step === name && r.agent === 1) }));
120
- const steps = [...declared, ...undeclared].map((s): StepRunsNow => {
121
- const on = records.filter((r) => r.step === s.name);
122
- const waiting = on.flatMap((r) => (r.ended === null ? [] : [r.ended]));
123
- return {
124
- step: s.name, agent: s.agent,
125
- inStep: on.length - waiting.length,
126
- waitingAfter: waiting.length,
127
- waitingSince: waiting.length === 0 ? null : Math.min(...waiting),
128
- waitingOn: on.find((r) => r.ended === null && r.waiting !== null)?.waiting ?? null,
129
- };
130
- });
131
- return {
132
- workflow: d.name,
133
- runs: runs.length,
134
- doing,
135
- onAgents: sum(held, (n) => n),
136
- notStarted: runs.length - records.length,
137
- steps,
138
- };
139
- };
140
-
141
- /**
142
- * Every workflow, in the order declared, running or not, then each workflow no file defines now that a run is of: each run
143
- * counted once, on its own workflow. One made of workflows has no runs of its own (`steps.ts` `isMadeOfWorkflows`): its runs
144
- * and agents are its steps', added up, at every depth, and each of its step's rows is that workflow's runs.
145
- */
146
- export const runsNow = (input: {
147
- readonly declared: ReadonlyArray<Declared>;
148
- readonly unfinished: ReadonlyArray<UnfinishedRun>;
149
- readonly newest: ReadonlyArray<NewestStepRecord>;
150
- readonly claims: ReadonlyArray<Pick<AgentClaim, 'run' | 'agents'> & { readonly top?: { readonly run: string } }>;
151
- }): ReadonlyArray<WorkflowRunsNow> => {
152
- const newest = new Map(input.newest.map((r) => [r.run, r]));
153
- // Each claim counts for its own run and for the run at its top, which holds it for the work below it.
154
- const claimed = new Map<string, number>();
155
- for (const c of input.claims) {
156
- claimed.set(c.run, (claimed.get(c.run) ?? 0) + c.agents);
157
- if (c.top !== undefined && c.top.run !== c.run) claimed.set(c.top.run, (claimed.get(c.top.run) ?? 0) + c.agents);
158
- }
159
- const names = new Set(input.declared.map((d) => d.name));
160
- const all: ReadonlyArray<Declared> = [
161
- ...input.declared,
162
- ...[...new Set(input.unfinished.map((u) => u.workflow))].filter((w) => !names.has(w)).map((name): Declared => ({ name, steps: () => [] })),
163
- ];
164
- const byName = new Map(all.map((d) => [d.name, d]));
165
- const counted = new Map<string, WorkflowRunsNow>();
166
- const runsOf = (d: Declared, depth = 0): WorkflowRunsNow => {
167
- const known = counted.get(d.name);
168
- if (known !== undefined) return known;
169
- if (!isMadeOfWorkflows(d) || depth > 8) {
170
- const own = workflowRunsNow(d, input.unfinished.filter((u) => u.workflow === d.name).map((u) => u.run), newest, claimed);
171
- counted.set(d.name, own);
172
- return own;
173
- }
174
- const under = d.steps().map((s) => ({ step: s.name, runs: s.workflow === undefined || byName.get(s.workflow) === undefined ? null : runsOf(byName.get(s.workflow)!, depth + 1) }));
175
- const doing = addUpDoing(under.flatMap((u) => u.runs?.doing ?? []));
176
- const made: WorkflowRunsNow = {
177
- workflow: d.name,
178
- runs: sum(under, (u) => u.runs?.runs ?? 0),
179
- doing,
180
- onAgents: sum(under, (u) => u.runs?.onAgents ?? 0),
181
- notStarted: 0,
182
- steps: under.map((u): StepRunsNow => ({ step: u.step, agent: false, inStep: u.runs?.runs ?? 0, waitingAfter: 0, waitingSince: null, waitingOn: null })),
183
- };
184
- counted.set(d.name, made);
185
- return made;
186
- };
187
- return all.map((d) => runsOf(d));
188
- };
@@ -1,163 +0,0 @@
1
- /**
2
- * WHERE A WORKFLOW'S RUNS STOP: for each step in declared order, the runs of a window that reached it, the runs that
3
- * ended there without finishing, the runs that ended there finished, and the typical (median) time a run spent in it.
4
- * Under the step most runs stopped at, why they stopped. Pure, so it is tested alone; its inputs are the step records
5
- * and item outcomes of the window's runs (`StepRecords.funnelRecordsSince`) and the engine's unfinished runs.
6
- *
7
- * Alex, 2026-11-06: "per workflow to have a distribution on which step is reached in a workflow before getting stuck. Or
8
- * perhaps time spent on each step? … we want visibility into where current work isn't able to get past".
9
- *
10
- * A run is in the window when its first step started in it, and only its records started by `now` count. It reached a step when it has a record of that step. It
11
- * ended at the step of its newest record, once the engine has finished it. It finished when one of its items' recorded
12
- * outcomes says finished; a workflow with no item finishes a run whose last step succeeded. Every other ended run
13
- * stopped, for the first of: its last step's error, its item's outcome, its Pick finding nothing, an interruption.
14
- */
15
- import type { RunOutcomeRow, FunnelStepRecord } from '../storage/step-records';
16
- import type { StepDeclaration } from '../steps';
17
- import { oneLine } from '../reasons';
18
-
19
- export interface FunnelStep {
20
- readonly step: string;
21
- readonly agent: boolean;
22
- /** Runs with a record of this step. */
23
- readonly reached: number;
24
- /** Ended runs whose last step was this one, not finished. */
25
- readonly stopped: number;
26
- /** Ended runs whose last step was this one, finished. */
27
- readonly finished: number;
28
- /** The median, over runs with an ended record of it, of a run's time in it; none with none. */
29
- readonly medianSeconds: number | null;
30
- /** Whether every run that reached it stopped here and none reached the step declared after it. */
31
- readonly noneGotPast: boolean;
32
- }
33
-
34
- export interface StopReason {
35
- readonly reason: string;
36
- readonly runs: number;
37
- }
38
-
39
- export interface StepFunnel {
40
- readonly hours: number;
41
- /** Runs whose first step started at or after this, unix seconds, up to `now`. */
42
- readonly since: number;
43
- readonly now: number;
44
- readonly runs: number;
45
- /** Of those, not finished by the engine now. */
46
- readonly going: number;
47
- /** Its declared steps in order, then any step its records name that it no longer declares. */
48
- readonly steps: ReadonlyArray<FunnelStep>;
49
- /** The step most runs stopped at, earliest on a tie, with its commonest reasons; none when no run stopped. */
50
- readonly mostStopped: { readonly step: string; readonly stopped: number; readonly reasons: ReadonlyArray<StopReason>; readonly otherRuns: number } | null;
51
- }
52
-
53
- export interface WorkflowFunnels {
54
- readonly workflow: string;
55
- /** One per window, shortest first. */
56
- readonly windows: ReadonlyArray<StepFunnel>;
57
- }
58
-
59
- /** The windows the page offers: the last hour, and the last day. */
60
- export const FUNNEL_WINDOW_HOURS = [1, 24] as const;
61
-
62
- /** Reasons listed under the step most runs stopped at; the rest are counted together. */
63
- export const TOP_STOP_REASONS = 3;
64
-
65
- type Declared = { readonly item: string | null } & { readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'runs'>> };
66
-
67
- /** The commonest of a run's item outcomes, so a batch run gives one reason. */
68
- const commonest = (xs: ReadonlyArray<string>): string => {
69
- const counts = new Map<string, number>();
70
- for (const x of xs) counts.set(x, (counts.get(x) ?? 0) + 1);
71
- return [...counts.entries()].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0]))[0]![0];
72
- };
73
-
74
- /** Why a run stopped where it did: its item's outcome first, then the reason its last step recorded (`reasons.ts`). */
75
- const stopReason = (last: FunnelStepRecord, outcomes: ReadonlyArray<RunOutcomeRow>): string => {
76
- if (last.outcome === 'failure') return last.error === null ? last.reason ?? 'its step failed with no error recorded' : oneLine(last.error);
77
- if (outcomes.length > 0) return oneLine(commonest(outcomes.map((o) => o.outcome)));
78
- if (last.reason !== null && last.reason !== 'succeeded') return last.reason;
79
- if (last.scope === 'nothing') return 'found nothing to work on';
80
- if (last.outcome === 'interrupted') return 'interrupted';
81
- if (last.ended === null) return 'ended during the step, with no end recorded';
82
- return 'ended with no outcome recorded';
83
- };
84
-
85
- /** Rows by their run, each run's in the order given. */
86
- const groupBy = <T extends { readonly run: string }>(rows: ReadonlyArray<T>): Map<string, T[]> => {
87
- const out = new Map<string, T[]>();
88
- for (const r of rows) {
89
- const list = out.get(r.run);
90
- if (list === undefined) out.set(r.run, [r]);
91
- else list.push(r);
92
- }
93
- return out;
94
- };
95
-
96
- const median = (xs: ReadonlyArray<number>): number | null => {
97
- if (xs.length === 0) return null;
98
- const sorted = [...xs].sort((a, b) => a - b);
99
- const mid = Math.floor(sorted.length / 2);
100
- return sorted.length % 2 === 1 ? sorted[mid]! : (sorted[mid - 1]! + sorted[mid]!) / 2;
101
- };
102
-
103
- /** One workflow's funnel over the `hours` before `now`, from its records in that window or a wider one. */
104
- export const stepFunnel = (input: {
105
- readonly declaration: Declared;
106
- /** This workflow's records, oldest first; runs that began before `since` are left out here. */
107
- readonly records: ReadonlyArray<FunnelStepRecord>;
108
- readonly outcomes: ReadonlyArray<RunOutcomeRow>;
109
- readonly unfinished: ReadonlySet<string>;
110
- readonly hours: number;
111
- readonly since: number;
112
- readonly now: number;
113
- }): StepFunnel => {
114
- // Records are read after `now` was fixed: what started later is outside the window the page states.
115
- const byRun = groupBy(input.records.filter((r) => r.started <= input.now));
116
- const runs = [...byRun.entries()].filter(([, rs]) => Math.min(...rs.map((r) => r.started)) >= input.since);
117
- const outcomesOf = groupBy(input.outcomes);
118
-
119
- const ends = runs.filter(([run]) => !input.unfinished.has(run)).map(([run, rs]) => {
120
- const last = rs[rs.length - 1]!;
121
- const theirs = outcomesOf.get(run) ?? [];
122
- const finished = input.declaration.item === null ? last.outcome === 'success' : theirs.some((o) => o.finished === 1);
123
- return { step: last.step, finished, reason: finished ? null : stopReason(last, theirs) };
124
- });
125
-
126
- const declared = input.declaration.steps().map((s) => ({ name: s.name, agent: s.runs === 'agentic' }));
127
- const undeclared = [...new Set(runs.flatMap(([, rs]) => rs.map((r) => r.step)))].filter((name) => !declared.some((s) => s.name === name))
128
- .map((name) => ({ name, agent: runs.some(([, rs]) => rs.some((r) => r.step === name && r.agent === 1)) }));
129
- const counted = [...declared, ...undeclared].map((s) => {
130
- const timed = runs.flatMap(([, rs]) => {
131
- const ended = rs.filter((r) => r.step === s.name && r.ended !== null);
132
- return ended.length === 0 ? [] : [ended.reduce((a, r) => a + (r.ended! - r.started), 0)];
133
- });
134
- return {
135
- step: s.name, agent: s.agent,
136
- reached: runs.filter(([, rs]) => rs.some((r) => r.step === s.name)).length,
137
- stopped: ends.filter((e) => e.step === s.name && !e.finished).length,
138
- finished: ends.filter((e) => e.step === s.name && e.finished).length,
139
- medianSeconds: median(timed),
140
- };
141
- });
142
- const steps = counted.map((s, i): FunnelStep => ({
143
- ...s,
144
- noneGotPast: i < declared.length - 1 && s.reached > 0 && s.stopped === s.reached && counted[i + 1]!.reached === 0,
145
- }));
146
-
147
- const most = steps.reduce<FunnelStep | null>((best, s) => (s.stopped > 0 && (best === null || s.stopped > best.stopped) ? s : best), null);
148
- const mostStopped = most === null ? null : (() => {
149
- const counts = new Map<string, number>();
150
- for (const e of ends) if (e.step === most.step && e.reason !== null) counts.set(e.reason, (counts.get(e.reason) ?? 0) + 1);
151
- const reasons = [...counts.entries()].map(([reason, n]) => ({ reason, runs: n })).sort((a, b) => b.runs - a.runs || a.reason.localeCompare(b.reason));
152
- const top = reasons.slice(0, TOP_STOP_REASONS);
153
- return { step: most.step, stopped: most.stopped, reasons: top, otherRuns: most.stopped - top.reduce((a, r) => a + r.runs, 0) };
154
- })();
155
-
156
- return {
157
- hours: input.hours, since: input.since, now: input.now,
158
- runs: runs.length,
159
- going: runs.filter(([run]) => input.unfinished.has(run)).length,
160
- steps,
161
- mostStopped,
162
- };
163
- };
@@ -1,77 +0,0 @@
1
- /**
2
- * EACH WORKFLOW'S STEP GRAPH WITH THE WINDOW'S RUNS ON IT. The graph is the workflow's own declaration (`StepEdge` in
3
- * `steps.ts`); the runs are its step records' moves (`StepRecords.stepEdgesSince`). A move the graph does not
4
- * declare is listed as undeclared, never added to it. Pure, so it is tested alone.
5
- */
6
- import type { LastStepNow, StepEdgeTaken } from '../storage/step-records';
7
- import { A_STEP_FAILS, isRetry, RUN_ENDS, RUN_STARTS, type StepEdge } from '../graph';
8
- import type { StepDeclaration, StepRuns } from '../steps';
9
-
10
- /** A step-to-step edge of a workflow's graph, and how many runs took it in the window. */
11
- export interface GraphEdge {
12
- readonly from: string;
13
- readonly to: string;
14
- readonly when: string;
15
- readonly runs: number;
16
- }
17
-
18
- /** A workflow's declared graph with the window's runs on it, and the moves it does not declare. */
19
- export interface WorkflowGraph {
20
- readonly workflow: string;
21
- /**
22
- * Whether the workflow declares its graph. One that declares none still has one: its steps in the order it declares
23
- * them, from the run's start to its end (`inOrder`), each such edge with no condition of its own.
24
- */
25
- readonly declared: boolean;
26
- readonly steps: ReadonlyArray<{ readonly name: string; readonly runs: StepRuns | null; readonly description: string; readonly workflow: string | null }>;
27
- readonly edges: ReadonlyArray<GraphEdge>;
28
- readonly undeclared: ReadonlyArray<{ readonly from: string; readonly to: string; readonly runs: number }>;
29
- }
30
-
31
- /** What of a declaration the graph reads. */
32
- interface Declared {
33
- readonly name: string;
34
- readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'runs' | 'description' | 'workflow'>>;
35
- readonly graph?: (() => ReadonlyArray<StepEdge>) | undefined;
36
- }
37
-
38
- /** The graph of a workflow that declares none: the run's start, each step in the order declared, the run's end. Pure. */
39
- export const inOrder = (steps: ReadonlyArray<{ readonly name: string }>): ReadonlyArray<StepEdge> => {
40
- const names = [RUN_STARTS, ...steps.map((s) => s.name), RUN_ENDS];
41
- return names.slice(1).map((to, i) => ({ from: names[i]!, to, when: '' }));
42
- };
43
-
44
- export const workflowGraphs = (input: {
45
- readonly declared: ReadonlyArray<Declared>;
46
- readonly taken: ReadonlyArray<StepEdgeTaken>;
47
- readonly lasts: ReadonlyArray<LastStepNow>;
48
- /** Runs not ended: their last step has no edge to the end yet. */
49
- readonly going: ReadonlySet<string>;
50
- }): ReadonlyArray<WorkflowGraph> =>
51
- input.declared.map((d) => {
52
- const graph = d.graph?.() ?? inOrder(d.steps());
53
- const runs = new Map(graph.map((e) => [`${e.from}\u0000${e.to}`, 0]));
54
- const undeclared = new Map<string, { from: string; to: string; runs: number }>();
55
- const count = (from: string, failed: boolean, to: string, n: number) => {
56
- if (isRetry({ step: from, failed }, to)) return;
57
- const exact = `${from}\u0000${to}`;
58
- const onFailure = `${A_STEP_FAILS}\u0000${to}`;
59
- const key = runs.has(exact) ? exact : failed && runs.has(onFailure) ? onFailure : null;
60
- if (key !== null) {
61
- runs.set(key, (runs.get(key) ?? 0) + n);
62
- return;
63
- }
64
- const shown = { from: failed ? `${from} (failed)` : from, to };
65
- const k = `${shown.from}\u0000${to}`;
66
- undeclared.set(k, { ...shown, runs: (undeclared.get(k)?.runs ?? 0) + n });
67
- };
68
- for (const e of input.taken) if (e.workflow === d.name) count(e.from ?? RUN_STARTS, e.fromFailed, e.to, e.runs);
69
- for (const l of input.lasts) if (l.workflow === d.name && !input.going.has(l.run)) count(l.step, l.failed, RUN_ENDS, 1);
70
- return {
71
- workflow: d.name,
72
- declared: d.graph !== undefined,
73
- steps: d.steps().map((s) => ({ name: s.name, runs: s.runs, description: s.description, workflow: s.workflow ?? null })),
74
- edges: graph.map((e) => ({ ...e, runs: runs.get(`${e.from}\u0000${e.to}`) ?? 0 })),
75
- undeclared: [...undeclared.values()].sort((a, b) => b.runs - a.runs),
76
- };
77
- });