@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
@@ -0,0 +1,188 @@
1
+ import { paginationOptsValidator } from "convex/server"
2
+ import { v } from "convex/values"
3
+ import { query } from "./_generated/server"
4
+ import type { Doc } from "./_generated/dataModel"
5
+ import { noSpend, skippedOtherValidator, type StepSpend, stepSpendValidator, taskFileValidator } from "../src/graph"
6
+
7
+ const spreadValidator = v.object({ median: v.number(), p90: v.number() })
8
+
9
+ /** The nearest-rank percentile of `values`, or 0 when there are none. */
10
+ const percentile = (values: ReadonlyArray<number>, fraction: number): number => {
11
+ if (values.length === 0) return 0
12
+ const sorted = [...values].sort((a, b) => a - b)
13
+ return sorted[Math.min(sorted.length - 1, Math.max(0, Math.ceil(fraction * sorted.length) - 1))]!
14
+ }
15
+
16
+ const spreadOf = (values: ReadonlyArray<number>) => ({ median: percentile(values, 0.5), p90: percentile(values, 0.9) })
17
+
18
+ const tokensOf = (run: Doc<"stepRuns">) => run.spend.fresh + run.spend.cacheWrite + run.spend.cacheRead + run.spend.output
19
+
20
+ /**
21
+ * One step's figures over the runs that started in `[since, until)`: how many runs, how they ended, what they spent,
22
+ * and, per task (a task's runs added up, retries included), the spread of wall time, CPU and tokens over the tasks
23
+ * whose runs have all finished.
24
+ */
25
+ export const stepStats = query({
26
+ args: { workflow: v.string(), step: v.string(), since: v.number(), until: v.optional(v.number()) },
27
+ returns: v.object({
28
+ calls: v.number(),
29
+ running: v.number(),
30
+ failed: v.number(),
31
+ endStates: v.array(v.object({ endState: v.string(), runs: v.number() })),
32
+ spend: v.object({
33
+ fresh: v.number(),
34
+ cacheWrite: v.number(),
35
+ cacheRead: v.number(),
36
+ output: v.number(),
37
+ requests: v.number(),
38
+ costUsd: v.number(),
39
+ compiles: v.number(),
40
+ }),
41
+ /** The wall time of the finished runs, added up, so it can be split across steps. */
42
+ wallSeconds: v.number(),
43
+ cpuSeconds: v.object({ own: v.number(), child: v.number() }),
44
+ peakMemoryMb: v.number(),
45
+ filesWritten: v.number(),
46
+ agents: v.array(v.object({ agent: v.string(), runs: v.number() })),
47
+ tasks: v.number(),
48
+ perTask: v.object({ wallSeconds: spreadValidator, cpuSeconds: spreadValidator, tokens: spreadValidator }),
49
+ }),
50
+ handler: async (ctx, args) => {
51
+ const until = args.until ?? Number.MAX_SAFE_INTEGER
52
+ const runs = await ctx.db
53
+ .query("stepRuns")
54
+ .withIndex("by_workflow_step_started", (q) => q.eq("workflow", args.workflow).eq("step", args.step).gte("startedAt", args.since).lt("startedAt", until))
55
+ .collect()
56
+ const count = (what: (run: Doc<"stepRuns">) => string | undefined) => {
57
+ const counts = new Map<string, number>()
58
+ for (const run of runs) {
59
+ const key = what(run)
60
+ if (key !== undefined) counts.set(key, (counts.get(key) ?? 0) + 1)
61
+ }
62
+ return [...counts.entries()]
63
+ }
64
+ const sum = (what: (run: Doc<"stepRuns">) => number) => runs.reduce((total, run) => total + what(run), 0)
65
+
66
+ const byTask = new Map<string, Array<Doc<"stepRuns">>>()
67
+ for (const run of runs) byTask.set(run.task, [...(byTask.get(run.task) ?? []), run])
68
+ const finishedTasks = [...byTask.values()].filter((taskRuns) => taskRuns.every((run) => run.endedAt !== undefined))
69
+ const perTask = (what: (run: Doc<"stepRuns">) => number) => finishedTasks.map((taskRuns) => taskRuns.reduce((total, run) => total + what(run), 0))
70
+
71
+ return {
72
+ calls: runs.length,
73
+ running: runs.filter((run) => run.status === "running").length,
74
+ failed: runs.filter((run) => run.status === "failed").length,
75
+ endStates: count((run) => run.endState).map(([endState, total]) => ({ endState, runs: total })),
76
+ spend: {
77
+ fresh: sum((run) => run.spend.fresh),
78
+ cacheWrite: sum((run) => run.spend.cacheWrite),
79
+ cacheRead: sum((run) => run.spend.cacheRead),
80
+ output: sum((run) => run.spend.output),
81
+ requests: sum((run) => run.spend.requests),
82
+ costUsd: sum((run) => run.spend.costUsd),
83
+ compiles: sum((run) => run.spend.compiles),
84
+ },
85
+ wallSeconds: sum((run) => (run.endedAt === undefined ? 0 : (run.endedAt - run.startedAt) / 1000)),
86
+ cpuSeconds: { own: sum((run) => run.used.ownCpuSeconds), child: sum((run) => run.used.childCpuSeconds) },
87
+ peakMemoryMb: runs.reduce((peak, run) => Math.max(peak, run.used.peakMemoryMb), 0),
88
+ filesWritten: sum((run) => run.used.filesWritten),
89
+ agents: count((run) => run.agent).map(([agent, total]) => ({ agent, runs: total })),
90
+ tasks: byTask.size,
91
+ perTask: {
92
+ wallSeconds: spreadOf(perTask((run) => ((run.endedAt ?? run.startedAt) - run.startedAt) / 1000)),
93
+ cpuSeconds: spreadOf(perTask((run) => run.used.ownCpuSeconds + run.used.childCpuSeconds)),
94
+ tokens: spreadOf(perTask(tokensOf)),
95
+ },
96
+ }
97
+ },
98
+ })
99
+
100
+ /**
101
+ * What the tasks a parent's sub-workflow step created here spent, over their runs that started in `[since, until)`:
102
+ * the parent's `stepStats` of a sub-workflow step reports this as its spend, since the step itself runs nothing.
103
+ * `perParentTaskTokens` is the spread over the parent's tasks, each one's sub-workflow tasks added up. Spend of a
104
+ * sub-workflow these tasks start in turn is recorded in that sub-workflow's component and not included.
105
+ */
106
+ export const subWorkflowSpend = query({
107
+ args: { parent: v.object({ workflow: v.string(), step: v.string() }), since: v.number(), until: v.optional(v.number()) },
108
+ returns: v.object({ spend: stepSpendValidator, parentTasks: v.number(), perParentTaskTokens: spreadValidator }),
109
+ handler: async (ctx, args) => {
110
+ const until = args.until ?? Number.MAX_SAFE_INTEGER
111
+ const tasks = await ctx.db
112
+ .query("tasks")
113
+ .withIndex("by_parent_step", (q) => q.eq("parent.workflow", args.parent.workflow).eq("parent.step", args.parent.step))
114
+ .collect()
115
+ let spend: StepSpend = noSpend
116
+ const tokensByParentTask = new Map<string, number>()
117
+ for (const task of tasks) {
118
+ if (task.parent === undefined) continue
119
+ const parentTask = task.parent.task
120
+ const runs = await ctx.db.query("stepRuns").withIndex("by_task", (q) => q.eq("task", task._id)).collect()
121
+ for (const run of runs.filter((candidate) => candidate.startedAt >= args.since && candidate.startedAt < until)) {
122
+ spend = {
123
+ fresh: spend.fresh + run.spend.fresh,
124
+ cacheWrite: spend.cacheWrite + run.spend.cacheWrite,
125
+ cacheRead: spend.cacheRead + run.spend.cacheRead,
126
+ output: spend.output + run.spend.output,
127
+ requests: spend.requests + run.spend.requests,
128
+ costUsd: spend.costUsd + run.spend.costUsd,
129
+ compiles: spend.compiles + run.spend.compiles,
130
+ }
131
+ tokensByParentTask.set(parentTask, (tokensByParentTask.get(parentTask) ?? 0) + tokensOf(run))
132
+ }
133
+ }
134
+ return { spend, parentTasks: tokensByParentTask.size, perParentTaskTokens: spreadOf([...tokensByParentTask.values()]) }
135
+ },
136
+ })
137
+
138
+ /**
139
+ * One step's runs, newest first, a page at a time: those that started at or after `since` when given. Each run comes
140
+ * with its task, which try it was, when it started and ended, how it ended, its agent, spend and transcripts.
141
+ */
142
+ export const stepRuns = query({
143
+ args: { workflow: v.string(), step: v.string(), since: v.optional(v.number()), paginationOpts: paginationOptsValidator },
144
+ returns: v.object({
145
+ page: v.array(
146
+ v.object({
147
+ task: v.id("tasks"),
148
+ call: v.number(),
149
+ status: v.string(),
150
+ startedAt: v.number(),
151
+ endedAt: v.optional(v.number()),
152
+ endState: v.optional(v.string()),
153
+ error: v.optional(v.string()),
154
+ agent: v.optional(v.string()),
155
+ spend: stepSpendValidator,
156
+ transcripts: v.array(taskFileValidator),
157
+ skippedOthers: v.optional(v.array(skippedOtherValidator)),
158
+ }),
159
+ ),
160
+ isDone: v.boolean(),
161
+ continueCursor: v.string(),
162
+ }),
163
+ handler: async (ctx, args) => {
164
+ const since = args.since ?? 0
165
+ const runs = await ctx.db
166
+ .query("stepRuns")
167
+ .withIndex("by_workflow_step_started", (q) => q.eq("workflow", args.workflow).eq("step", args.step).gte("startedAt", since))
168
+ .order("desc")
169
+ .paginate(args.paginationOpts)
170
+ return {
171
+ isDone: runs.isDone,
172
+ continueCursor: runs.continueCursor,
173
+ page: runs.page.map((run) => ({
174
+ task: run.task,
175
+ call: run.call,
176
+ status: run.status,
177
+ startedAt: run.startedAt,
178
+ ...(run.endedAt !== undefined ? { endedAt: run.endedAt } : {}),
179
+ ...(run.endState !== undefined ? { endState: run.endState } : {}),
180
+ ...(run.error !== undefined ? { error: run.error } : {}),
181
+ ...(run.agent !== undefined ? { agent: run.agent } : {}),
182
+ spend: run.spend,
183
+ transcripts: run.transcripts,
184
+ ...(run.skippedOthers !== undefined ? { skippedOthers: run.skippedOthers } : {}),
185
+ })),
186
+ }
187
+ },
188
+ })