@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.
- package/README.md +70 -435
- package/component/_generated/api.ts +72 -0
- package/component/_generated/component.ts +629 -0
- package/component/_generated/dataModel.ts +60 -0
- package/component/_generated/server.ts +169 -0
- package/component/convex.config.ts +3 -0
- package/component/engine.ts +756 -0
- package/component/errors.ts +66 -0
- package/component/events.ts +17 -0
- package/component/notes.ts +19 -0
- package/component/places.ts +136 -0
- package/component/retention.ts +205 -0
- package/component/runEngine.ts +25 -0
- package/component/schema.ts +135 -0
- package/component/steps.ts +188 -0
- package/component/store.ts +513 -0
- package/component/tasks.ts +290 -0
- package/component/worker.ts +109 -0
- package/component/workflows.ts +83 -0
- package/dist/definition/agent-runner.js +9 -0
- package/dist/definition/agent-runner.js.map +1 -0
- package/dist/definition/file-store.js +9 -0
- package/dist/definition/file-store.js.map +1 -0
- package/dist/definition/program-build.js +10 -0
- package/dist/definition/program-build.js.map +1 -0
- package/dist/definition/step.js +1 -0
- package/dist/definition/step.js.map +1 -0
- package/dist/definition/task-schema.js +5 -0
- package/dist/definition/task-schema.js.map +1 -0
- package/dist/definition/tasks.js +9 -0
- package/dist/definition/tasks.js.map +1 -0
- package/dist/definition/workflow.js +117 -0
- package/dist/definition/workflow.js.map +1 -0
- package/dist/graph.js +310 -9
- package/dist/graph.js.map +1 -1
- package/dist/index.js +7 -196
- package/dist/index.js.map +1 -1
- package/package.json +9 -4
- package/src/definition/agent-runner.ts +59 -0
- package/src/definition/file-store.ts +20 -0
- package/src/definition/program-build.ts +30 -0
- package/src/definition/step.ts +106 -0
- package/src/definition/task-schema.ts +15 -0
- package/src/definition/tasks.ts +43 -0
- package/src/definition/workflow.ts +262 -0
- package/src/graph.ts +352 -19
- package/src/index.ts +8 -238
- package/worker/directory-file-store.ts +42 -0
- package/worker/index.ts +12 -0
- package/worker/platform-client.ts +144 -0
- package/worker/step-meter.ts +80 -0
- package/worker/worker-tasks.ts +27 -0
- package/worker/worker.test.ts +200 -0
- package/worker/worker.ts +240 -0
- package/dist/budget-states.js +0 -30
- package/dist/budget-states.js.map +0 -1
- package/dist/cpu-limit/limiter.js +0 -839
- package/dist/cpu-limit/limiter.js.map +0 -1
- package/dist/cpu-limit/use.js +0 -139
- package/dist/cpu-limit/use.js.map +0 -1
- package/dist/dashboard/agent-usage.js +0 -25
- package/dist/dashboard/agent-usage.js.map +0 -1
- package/dist/dashboard/dashboard.css +0 -148
- package/dist/dashboard/drilldown.js +0 -47
- package/dist/dashboard/drilldown.js.map +0 -1
- package/dist/dashboard/queue-lines.js +0 -44
- package/dist/dashboard/queue-lines.js.map +0 -1
- package/dist/dashboard/queue-table.js +0 -16
- package/dist/dashboard/queue-table.js.map +0 -1
- package/dist/dashboard/run-doing-words.js +0 -29
- package/dist/dashboard/run-doing-words.js.map +0 -1
- package/dist/dashboard/setting-words.js +0 -18
- package/dist/dashboard/setting-words.js.map +0 -1
- package/dist/dashboard/spending-words.js +0 -24
- package/dist/dashboard/spending-words.js.map +0 -1
- package/dist/failure.js +0 -35
- package/dist/failure.js.map +0 -1
- package/dist/keep-running.js +0 -79
- package/dist/keep-running.js.map +0 -1
- package/dist/outside-failure.js +0 -31
- package/dist/outside-failure.js.map +0 -1
- package/dist/pick-wakes.js +0 -125
- package/dist/pick-wakes.js.map +0 -1
- package/dist/project.js +0 -1
- package/dist/project.js.map +0 -1
- package/dist/queue-counts.js +0 -51
- package/dist/queue-counts.js.map +0 -1
- package/dist/reasons.js +0 -33
- package/dist/reasons.js.map +0 -1
- package/dist/retention.js +0 -59
- package/dist/retention.js.map +0 -1
- package/dist/settings.js +0 -88
- package/dist/settings.js.map +0 -1
- package/dist/spending-limit.js +0 -82
- package/dist/spending-limit.js.map +0 -1
- package/dist/step-files.js +0 -200
- package/dist/step-files.js.map +0 -1
- package/dist/step-meter.js +0 -97
- package/dist/step-meter.js.map +0 -1
- package/dist/step-times.js +0 -114
- package/dist/step-times.js.map +0 -1
- package/dist/step-usage.js +0 -19
- package/dist/step-usage.js.map +0 -1
- package/dist/steps.js +0 -1205
- package/dist/steps.js.map +0 -1
- package/dist/storage/agent-claims.js +0 -3
- package/dist/storage/agent-claims.js.map +0 -1
- package/dist/storage/budgets.js +0 -121
- package/dist/storage/budgets.js.map +0 -1
- package/dist/storage/engine-records.js +0 -91
- package/dist/storage/engine-records.js.map +0 -1
- package/dist/storage/engine-storage.js +0 -67
- package/dist/storage/engine-storage.js.map +0 -1
- package/dist/storage/input-records.js +0 -12
- package/dist/storage/input-records.js.map +0 -1
- package/dist/storage/move-store.js +0 -251
- package/dist/storage/move-store.js.map +0 -1
- package/dist/storage/sqlite-store.js +0 -901
- package/dist/storage/sqlite-store.js.map +0 -1
- package/dist/storage/step-records.js +0 -11
- package/dist/storage/step-records.js.map +0 -1
- package/dist/storage/storage-error.js +0 -19
- package/dist/storage/storage-error.js.map +0 -1
- package/dist/storage/task-store.js +0 -30
- package/dist/storage/task-store.js.map +0 -1
- package/dist/system-workflows.js +0 -54
- package/dist/system-workflows.js.map +0 -1
- package/dist/tasks.js +0 -500
- package/dist/tasks.js.map +0 -1
- package/dist/views/format.js +0 -37
- package/dist/views/format.js.map +0 -1
- package/dist/views/runs-now.js +0 -117
- package/dist/views/runs-now.js.map +0 -1
- package/dist/views/step-funnel.js +0 -98
- package/dist/views/step-funnel.js.map +0 -1
- package/dist/views/step-graph.js +0 -39
- package/dist/views/step-graph.js.map +0 -1
- package/dist/views/workflows-view.js +0 -236
- package/dist/views/workflows-view.js.map +0 -1
- package/src/budget-states.ts +0 -54
- package/src/cpu-limit/limiter.ts +0 -935
- package/src/cpu-limit/use.ts +0 -166
- package/src/dashboard/agent-usage-line.tsx +0 -28
- package/src/dashboard/agent-usage.ts +0 -48
- package/src/dashboard/dashboard.css +0 -148
- package/src/dashboard/drilldown.ts +0 -70
- package/src/dashboard/index.tsx +0 -4
- package/src/dashboard/limits-and-runs.tsx +0 -191
- package/src/dashboard/machine-controls.tsx +0 -53
- package/src/dashboard/queue-bar.tsx +0 -62
- package/src/dashboard/queue-chart.tsx +0 -149
- package/src/dashboard/queue-lines.ts +0 -87
- package/src/dashboard/queue-table.ts +0 -37
- package/src/dashboard/run-doing-words.ts +0 -41
- package/src/dashboard/setting-words.ts +0 -30
- package/src/dashboard/spending-limit-control.tsx +0 -47
- package/src/dashboard/spending-words.ts +0 -32
- package/src/dashboard/time-distribution.tsx +0 -61
- package/src/dashboard/workflow-dag.tsx +0 -322
- package/src/dashboard/workflow-drilldown.tsx +0 -31
- package/src/dashboard/workflows-dashboard.tsx +0 -532
- package/src/failure.ts +0 -36
- package/src/keep-running.ts +0 -85
- package/src/outside-failure.ts +0 -39
- package/src/pick-wakes.ts +0 -182
- package/src/project.ts +0 -127
- package/src/queue-counts.ts +0 -64
- package/src/reasons.ts +0 -30
- package/src/retention.ts +0 -90
- package/src/settings.ts +0 -171
- package/src/spending-limit.ts +0 -139
- package/src/step-files.ts +0 -208
- package/src/step-meter.ts +0 -120
- package/src/step-times.ts +0 -151
- package/src/step-usage.ts +0 -23
- package/src/steps.ts +0 -1616
- package/src/storage/agent-claims.ts +0 -63
- package/src/storage/budgets.ts +0 -152
- package/src/storage/engine-records.ts +0 -119
- package/src/storage/engine-storage.ts +0 -82
- package/src/storage/input-records.ts +0 -43
- package/src/storage/move-store.ts +0 -265
- package/src/storage/sqlite-store.ts +0 -1022
- package/src/storage/step-records.ts +0 -358
- package/src/storage/storage-error.ts +0 -35
- package/src/storage/task-store.ts +0 -235
- package/src/system-workflows.ts +0 -70
- package/src/tasks.ts +0 -586
- package/src/views/format.ts +0 -40
- package/src/views/runs-now.ts +0 -188
- package/src/views/step-funnel.ts +0 -163
- package/src/views/step-graph.ts +0 -77
- package/src/views/workflows-view.ts +0 -484
- package/src/workflow-dag.test.tsx +0 -89
- package/src/workflow-page.test.tsx +0 -170
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { Cause, Effect, Exit, Option, Schema } from "effect"
|
|
2
|
+
import {
|
|
3
|
+
type Budgets,
|
|
4
|
+
everySubWorkflowTaskEnded,
|
|
5
|
+
type GraphEvent,
|
|
6
|
+
type GraphStep,
|
|
7
|
+
lineIdOf,
|
|
8
|
+
type Retention,
|
|
9
|
+
type StepAccessGraph,
|
|
10
|
+
type WorkflowGraph,
|
|
11
|
+
} from "../graph"
|
|
12
|
+
import type {
|
|
13
|
+
AnyStep,
|
|
14
|
+
Granted,
|
|
15
|
+
RunnableStep,
|
|
16
|
+
ServiceKey,
|
|
17
|
+
ServicesOfSteps,
|
|
18
|
+
StepAccess,
|
|
19
|
+
StepInput,
|
|
20
|
+
StepOutcome,
|
|
21
|
+
SubWorkflowStep,
|
|
22
|
+
TasksGrant,
|
|
23
|
+
} from "./step"
|
|
24
|
+
import type { FieldsSchema, TaskSchema } from "./task-schema"
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* One workflow: its task schema, the steps tasks go through, the lines between them, the events that start flows,
|
|
28
|
+
* the end states it names and its budget defaults. `graph` is the form the Convex component runs; `runnableSteps` is
|
|
29
|
+
* what workers run, and `Services` what its steps require of the worker running them.
|
|
30
|
+
*/
|
|
31
|
+
export interface WorkflowDefinition<Fields extends FieldsSchema = FieldsSchema, EndState extends string = string, Services = any> {
|
|
32
|
+
readonly name: string
|
|
33
|
+
readonly task: TaskSchema<Fields>
|
|
34
|
+
readonly endStates: ReadonlyArray<EndState>
|
|
35
|
+
readonly graph: WorkflowGraph
|
|
36
|
+
readonly runnableSteps: Readonly<Record<string, RunnableStep<Fields["Type"], string, Services>>>
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type EventDefinition<Fields> =
|
|
40
|
+
| { readonly kind: "task" }
|
|
41
|
+
| { readonly kind: "change"; readonly field: keyof Fields & string; readonly resumes?: string }
|
|
42
|
+
/**
|
|
43
|
+
* A turn every `everyMs`: a new task whose fields are what the task schema decodes from `{}`, so every field of a
|
|
44
|
+
* workflow a timer starts needs a default. One that has none is refused when the workflow is registered.
|
|
45
|
+
*/
|
|
46
|
+
| { readonly kind: "timer"; readonly everyMs: number }
|
|
47
|
+
| { readonly kind: "request" }
|
|
48
|
+
/**
|
|
49
|
+
* A declared item list, kept as tasks by the step that syncs it (`syncList`): each item's task arrives here, keyed by its
|
|
50
|
+
* string field `key`, and one whose item leaves the list before it ends ends in `leftAs`, one of the workflow's end states.
|
|
51
|
+
*/
|
|
52
|
+
| { readonly kind: "list"; readonly key: StringFieldOf<Fields>; readonly leftAs: string }
|
|
53
|
+
|
|
54
|
+
/** The fields of `Fields` that hold a string. */
|
|
55
|
+
type StringFieldOf<Fields> = { [K in keyof Fields]: Fields[K] extends string ? K : never }[keyof Fields] & string
|
|
56
|
+
|
|
57
|
+
/** The list fields of `Fields` whose entries are tasks of a workflow with `Entry` fields. */
|
|
58
|
+
type ListFieldOf<Fields, Entry> = { [K in keyof Fields]: Fields[K] extends ReadonlyArray<Entry> ? K : never }[keyof Fields] & string
|
|
59
|
+
|
|
60
|
+
/** A step's code: what it may use (`access`), and its `run`, which may require only that. */
|
|
61
|
+
interface RunnableStepInput<Fields, EndState extends string, Services extends ReadonlyArray<ServiceKey>, Grant extends TasksGrant | undefined> {
|
|
62
|
+
readonly endStates: ReadonlyArray<EndState>
|
|
63
|
+
readonly access?: StepAccess<Services, Grant>
|
|
64
|
+
readonly run: (input: StepInput<Fields>) => Effect.fn.Return<StepOutcome<NoInfer<EndState>, Fields>, unknown, NoInfer<Granted<Services, Grant>>>
|
|
65
|
+
readonly retries?: { readonly limit: number; readonly delayMs: number; readonly giveUp: NoInfer<EndState> }
|
|
66
|
+
/** What this step's records keep, over its workflow's. */
|
|
67
|
+
readonly retention?: Retention
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface StepBuilder<Fields> {
|
|
71
|
+
code<const EndState extends string, const Services extends ReadonlyArray<ServiceKey> = [], const Grant extends TasksGrant | undefined = undefined>(
|
|
72
|
+
input: RunnableStepInput<Fields, EndState, Services, Grant>,
|
|
73
|
+
): RunnableStep<Fields, EndState, Granted<Services, Grant>>
|
|
74
|
+
agent<const EndState extends string, const Services extends ReadonlyArray<ServiceKey> = [], const Grant extends TasksGrant | undefined = undefined>(
|
|
75
|
+
input: RunnableStepInput<Fields, EndState, Services, Grant>,
|
|
76
|
+
): RunnableStep<Fields, EndState, Granted<Services, Grant>>
|
|
77
|
+
/** One task of `workflow` with this task's fields; the step ends with the end state that task reaches. */
|
|
78
|
+
workflow<ChildFields extends FieldsSchema, ChildEnd extends string>(input: {
|
|
79
|
+
readonly workflow: WorkflowDefinition<ChildFields, ChildEnd>
|
|
80
|
+
}): SubWorkflowStep<ChildEnd>
|
|
81
|
+
/**
|
|
82
|
+
* An inputs gate: `read` fingerprints what the workflow works from (files, records), and the step ends "changed" when
|
|
83
|
+
* that differs from the fingerprint of the last turn that settled one, or when none has, and "unchanged" otherwise. A
|
|
84
|
+
* turn settles its fingerprint when its task ends in one of `settledBy`, the workflow's success end states, as
|
|
85
|
+
* decomp-service's `runIfInputsChanged` compares with its last successful run. A turn that saw a change and then
|
|
86
|
+
* failed settles nothing, so the next one does the work again. Put first in a workflow a timer starts, it lets a turn
|
|
87
|
+
* do its work only when its inputs changed.
|
|
88
|
+
*/
|
|
89
|
+
inputsChanged<const Services extends ReadonlyArray<ServiceKey> = [], const Grant extends TasksGrant | undefined = undefined>(input: {
|
|
90
|
+
readonly access?: StepAccess<Services, Grant>
|
|
91
|
+
readonly read: (input: StepInput<Fields>) => Effect.fn.Return<string, unknown, NoInfer<Granted<Services, Grant>>>
|
|
92
|
+
readonly settledBy: ReadonlyArray<string>
|
|
93
|
+
}): RunnableStep<Fields, "changed" | "unchanged", Granted<Services, Grant>>
|
|
94
|
+
/**
|
|
95
|
+
* Reads a declared item list, the `list` event `list`: `read` returns every item on it now, each as its task's fields,
|
|
96
|
+
* and the step ends "synced" once the list's tasks are in step with it (each new item a task, each item gone from it
|
|
97
|
+
* ended in the event's `leftAs`). Put in a workflow a timer starts, it keeps the list's tasks in step as it changes.
|
|
98
|
+
*/
|
|
99
|
+
syncList<const Services extends ReadonlyArray<ServiceKey> = [], const Grant extends TasksGrant | undefined = undefined>(input: {
|
|
100
|
+
readonly access?: StepAccess<Services, Grant>
|
|
101
|
+
readonly list: string
|
|
102
|
+
readonly read: (input: StepInput<Fields>) => Effect.fn.Return<ReadonlyArray<Fields>, unknown, NoInfer<Granted<Services, Grant>>>
|
|
103
|
+
}): RunnableStep<Fields, "synced", Granted<Services, Grant>>
|
|
104
|
+
/**
|
|
105
|
+
* One task of `workflow` per entry of the list field `each`; the step ends with `ended` once all of them have.
|
|
106
|
+
* With `collect`, each one that ends in `collect.endState` has its fields appended to the list field `collect.into`.
|
|
107
|
+
*/
|
|
108
|
+
each<ChildFields extends FieldsSchema, ChildEnd extends string>(input: {
|
|
109
|
+
readonly workflow: WorkflowDefinition<ChildFields, ChildEnd>
|
|
110
|
+
readonly each: ListFieldOf<Fields, ChildFields["Type"]>
|
|
111
|
+
readonly collect?: { readonly endState: NoInfer<ChildEnd>; readonly into: ListFieldOf<Fields, ChildFields["Type"]> }
|
|
112
|
+
}): SubWorkflowStep<typeof everySubWorkflowTaskEnded>
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
type LineStartOf<Steps extends Record<string, AnyStep<any>>, Events, EndState extends string> =
|
|
116
|
+
| { readonly event: keyof Events & string }
|
|
117
|
+
| { [K in keyof Steps & string]: { readonly step: K; readonly endState: Steps[K]["endStates"][number] } }[keyof Steps & string]
|
|
118
|
+
| { readonly end: EndState }
|
|
119
|
+
|
|
120
|
+
type LineEndOf<Steps, EndState extends string> = { readonly step: keyof Steps & string } | { readonly end: EndState }
|
|
121
|
+
|
|
122
|
+
export interface LineDefinition<Steps extends Record<string, AnyStep<any>>, Events, EndState extends string> {
|
|
123
|
+
readonly from: LineStartOf<Steps, Events, EndState>
|
|
124
|
+
readonly to: LineEndOf<Steps, EndState>
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A step's declared access as its runnable form keeps it: nothing declared is nothing granted. */
|
|
128
|
+
const accessOf = (access: { readonly services?: ReadonlyArray<ServiceKey>; readonly tasks?: TasksGrant | undefined } | undefined) => ({
|
|
129
|
+
services: [...(access?.services ?? [])],
|
|
130
|
+
...(access?.tasks !== undefined ? { tasks: { read: [...access.tasks.read], write: [...access.tasks.write] } } : {}),
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const stepBuilder = <Fields>(): StepBuilder<Fields> => ({
|
|
134
|
+
syncList: (input) => ({
|
|
135
|
+
kind: "code",
|
|
136
|
+
endStates: ["synced"],
|
|
137
|
+
access: accessOf(input.access),
|
|
138
|
+
syncs: input.list,
|
|
139
|
+
run: function* (task) {
|
|
140
|
+
return { endState: "synced" as const, listed: yield* input.read(task) }
|
|
141
|
+
},
|
|
142
|
+
}),
|
|
143
|
+
inputsChanged: (input) => ({
|
|
144
|
+
kind: "code",
|
|
145
|
+
endStates: ["changed", "unchanged"],
|
|
146
|
+
access: accessOf(input.access),
|
|
147
|
+
inputsSettledBy: [...input.settledBy],
|
|
148
|
+
run: function* (task) {
|
|
149
|
+
const fingerprint = yield* input.read(task)
|
|
150
|
+
const unchanged = Option.isSome(task.lastFingerprint) && task.lastFingerprint.value === fingerprint
|
|
151
|
+
return { endState: unchanged ? ("unchanged" as const) : ("changed" as const), fingerprint }
|
|
152
|
+
},
|
|
153
|
+
}),
|
|
154
|
+
code: (input) => ({
|
|
155
|
+
kind: "code",
|
|
156
|
+
endStates: input.endStates,
|
|
157
|
+
access: accessOf(input.access),
|
|
158
|
+
run: input.run,
|
|
159
|
+
...(input.retries !== undefined ? { retries: input.retries } : {}),
|
|
160
|
+
...(input.retention !== undefined ? { retention: input.retention } : {}),
|
|
161
|
+
}),
|
|
162
|
+
agent: (input) => ({
|
|
163
|
+
kind: "agent",
|
|
164
|
+
endStates: input.endStates,
|
|
165
|
+
access: accessOf(input.access),
|
|
166
|
+
run: input.run,
|
|
167
|
+
...(input.retries !== undefined ? { retries: input.retries } : {}),
|
|
168
|
+
...(input.retention !== undefined ? { retention: input.retention } : {}),
|
|
169
|
+
}),
|
|
170
|
+
workflow: (input) => ({ kind: "workflow", endStates: input.workflow.endStates, workflow: { name: input.workflow.name } }),
|
|
171
|
+
each: (input) => ({
|
|
172
|
+
kind: "workflow",
|
|
173
|
+
endStates: [everySubWorkflowTaskEnded],
|
|
174
|
+
workflow: { name: input.workflow.name, each: input.each, ...(input.collect !== undefined ? { collect: input.collect } : {}) },
|
|
175
|
+
}),
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* The step builder for tasks of `task`, outside a workflow: the same `code`, `agent`, `inputsChanged`, `syncList`,
|
|
180
|
+
* `workflow` and `each` a workflow's `steps` gets, so a package can build its steps as values of their own and export
|
|
181
|
+
* them, each step's requirements inferred from its `access` as there. `Workflow.make`'s `steps` then returns them.
|
|
182
|
+
*/
|
|
183
|
+
export const stepsOf = <Fields extends FieldsSchema>(_task: TaskSchema<Fields>): StepBuilder<Fields["Type"]> => stepBuilder<Fields["Type"]>()
|
|
184
|
+
|
|
185
|
+
/** A timer's turn task: what the task schema decodes from `{}`, encoded as Convex stores it, or why it cannot be made. */
|
|
186
|
+
const turnFieldsOf = (fields: FieldsSchema): { readonly fields: unknown } | { readonly fieldsProblem: string } => {
|
|
187
|
+
const decoded = Schema.decodeUnknownExit(fields)({})
|
|
188
|
+
if (Exit.isFailure(decoded)) return { fieldsProblem: `its task schema cannot make a task from {}, so some field has no default: ${Cause.pretty(decoded.cause)}` }
|
|
189
|
+
const encoded = Schema.encodeUnknownExit(fields)(decoded.value)
|
|
190
|
+
if (Exit.isFailure(encoded)) return { fieldsProblem: `the task its schema makes from {} cannot be encoded: ${Cause.pretty(encoded.cause)}` }
|
|
191
|
+
return { fields: encoded.value }
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export const make = <
|
|
195
|
+
Fields extends FieldsSchema,
|
|
196
|
+
const EndState extends string,
|
|
197
|
+
const Events extends Record<string, EventDefinition<Fields["Type"]>>,
|
|
198
|
+
const Steps extends Record<string, AnyStep<Fields["Type"]>>,
|
|
199
|
+
>(input: {
|
|
200
|
+
readonly name: string
|
|
201
|
+
readonly task: TaskSchema<Fields>
|
|
202
|
+
readonly endStates: ReadonlyArray<EndState>
|
|
203
|
+
readonly budgets: Budgets
|
|
204
|
+
/** How long its records are kept, unless a step sets its own: 7 days and the newest 10,000 runs by default. */
|
|
205
|
+
readonly retention?: Retention
|
|
206
|
+
/**
|
|
207
|
+
* At most `limit` tasks in progress at once, or of each value of the field `per`: a lock held from a task's first step to
|
|
208
|
+
* its end, such as one on each function that every workflow touching it goes through as a sub-workflow step.
|
|
209
|
+
*/
|
|
210
|
+
readonly inProgress?: { readonly limit: number; readonly per?: keyof Fields["Type"] & string }
|
|
211
|
+
readonly events: Events
|
|
212
|
+
readonly steps: (step: StepBuilder<Fields["Type"]>) => Steps
|
|
213
|
+
readonly lines: ReadonlyArray<LineDefinition<NoInfer<Steps>, NoInfer<Events>, NoInfer<EndState>>>
|
|
214
|
+
}): WorkflowDefinition<Fields, EndState, ServicesOfSteps<Steps>> => {
|
|
215
|
+
const steps = input.steps(stepBuilder<Fields["Type"]>())
|
|
216
|
+
const events: Array<GraphEvent> = Object.entries(input.events).map(([id, event]): GraphEvent =>
|
|
217
|
+
event.kind === "timer"
|
|
218
|
+
? { id, kind: "timer", everyMs: event.everyMs, ...turnFieldsOf(input.task.fields) }
|
|
219
|
+
: event.kind === "change"
|
|
220
|
+
? { id, kind: "change", field: event.field, ...(event.resumes !== undefined ? { resumes: event.resumes } : {}) }
|
|
221
|
+
: event.kind === "list"
|
|
222
|
+
? { id, kind: "list", key: event.key, leftAs: event.leftAs }
|
|
223
|
+
: { id, kind: event.kind },
|
|
224
|
+
)
|
|
225
|
+
const graphSteps: Array<GraphStep> = Object.entries(steps).map(([id, step]): GraphStep => {
|
|
226
|
+
if (step.kind === "workflow") return { id, kind: "workflow", endStates: [...step.endStates], workflow: step.workflow }
|
|
227
|
+
const access: StepAccessGraph = {
|
|
228
|
+
services: step.access.services.map((service) => service.key),
|
|
229
|
+
...(step.access.tasks !== undefined ? { tasks: { read: [...step.access.tasks.read], write: [...step.access.tasks.write] } } : {}),
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
id,
|
|
233
|
+
kind: step.kind,
|
|
234
|
+
endStates: [...step.endStates],
|
|
235
|
+
access,
|
|
236
|
+
...(step.retries !== undefined ? { retries: { ...step.retries } } : {}),
|
|
237
|
+
...(step.inputsSettledBy !== undefined ? { inputsSettledBy: [...step.inputsSettledBy] } : {}),
|
|
238
|
+
...(step.syncs !== undefined ? { syncs: step.syncs } : {}),
|
|
239
|
+
...(step.retention !== undefined ? { retention: { ...step.retention } } : {}),
|
|
240
|
+
}
|
|
241
|
+
})
|
|
242
|
+
const runnableSteps: Record<string, RunnableStep<Fields["Type"], string, ServicesOfSteps<Steps>>> = {}
|
|
243
|
+
for (const [id, step] of Object.entries(steps)) {
|
|
244
|
+
if (step.kind !== "workflow") runnableSteps[id] = step
|
|
245
|
+
}
|
|
246
|
+
return {
|
|
247
|
+
name: input.name,
|
|
248
|
+
task: input.task,
|
|
249
|
+
endStates: input.endStates,
|
|
250
|
+
runnableSteps,
|
|
251
|
+
graph: {
|
|
252
|
+
name: input.name,
|
|
253
|
+
endStates: [...input.endStates],
|
|
254
|
+
events,
|
|
255
|
+
steps: graphSteps,
|
|
256
|
+
lines: input.lines.map((line) => ({ id: lineIdOf(line), from: line.from, to: line.to })),
|
|
257
|
+
budgets: input.budgets,
|
|
258
|
+
...(input.retention !== undefined ? { retention: { ...input.retention } } : {}),
|
|
259
|
+
...(input.inProgress !== undefined ? { inProgress: { ...input.inProgress } } : {}),
|
|
260
|
+
},
|
|
261
|
+
}
|
|
262
|
+
}
|
package/src/graph.ts
CHANGED
|
@@ -1,25 +1,358 @@
|
|
|
1
|
+
import { Option } from "effect"
|
|
2
|
+
import { type Infer, v } from "convex/values"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The serializable form of one workflow: what the compiler emits and what the Convex component stores and runs.
|
|
6
|
+
* Every place a task can be is derived from it: a line, a step, or an end state.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const stepKindValidator = v.union(v.literal("code"), v.literal("agent"), v.literal("workflow"))
|
|
10
|
+
export type StepKind = Infer<typeof stepKindValidator>
|
|
11
|
+
|
|
12
|
+
export const eventValidator = v.union(
|
|
13
|
+
v.object({ id: v.string(), kind: v.literal("task") }),
|
|
14
|
+
/**
|
|
15
|
+
* A change of `field` starts a flow for the task. With `resumes`, it instead moves the task's flow waiting at that end
|
|
16
|
+
* state onto this event's line: an end state that names why a task stopped (it waits for a name) is left when what it
|
|
17
|
+
* waited for changes.
|
|
18
|
+
*/
|
|
19
|
+
v.object({ id: v.string(), kind: v.literal("change"), field: v.string(), resumes: v.optional(v.string()) }),
|
|
20
|
+
/**
|
|
21
|
+
* A turn every `everyMs`: a task with `fields`, what the workflow's task schema decodes from `{}`. `fieldsProblem` says
|
|
22
|
+
* why the schema cannot, and a graph with one is refused.
|
|
23
|
+
*/
|
|
24
|
+
v.object({ id: v.string(), kind: v.literal("timer"), everyMs: v.number(), fields: v.optional(v.any()), fieldsProblem: v.optional(v.string()) }),
|
|
25
|
+
v.object({ id: v.string(), kind: v.literal("request") }),
|
|
26
|
+
/**
|
|
27
|
+
* A declared item list: a sync step (`syncs` naming this event) reads the list, and each item it has no task for
|
|
28
|
+
* becomes a task arriving here, keyed by the string field `key`. A task whose item left the list before it ended ends in
|
|
29
|
+
* `leftAs`, saying so; one that left and is listed again goes back on this event's line.
|
|
30
|
+
*/
|
|
31
|
+
v.object({ id: v.string(), kind: v.literal("list"), key: v.string(), leftAs: v.string() }),
|
|
32
|
+
)
|
|
33
|
+
export type GraphEvent = Infer<typeof eventValidator>
|
|
34
|
+
|
|
35
|
+
export const subWorkflowValidator = v.object({
|
|
36
|
+
name: v.string(),
|
|
37
|
+
/** One task of the sub-workflow per entry of this list field of the task; absent means one task with the same fields. */
|
|
38
|
+
each: v.optional(v.string()),
|
|
39
|
+
/** When a sub-workflow task ends in `endState`, its fields are appended to the list field `into` of this task. */
|
|
40
|
+
collect: v.optional(v.object({ endState: v.string(), into: v.string() })),
|
|
41
|
+
})
|
|
42
|
+
export type SubWorkflow = Infer<typeof subWorkflowValidator>
|
|
43
|
+
|
|
1
44
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* Dependency-free, so a page can import it.
|
|
45
|
+
* How a code or agent step treats a failed run: the task goes back to the front of its queue, and no worker takes it
|
|
46
|
+
* again until `delayMs` has passed. After `limit` failed runs in a row it leaves the step through `giveUp`, one of the
|
|
47
|
+
* step's own end states, with why as a task note. A step with none is tried again at once, however often it fails.
|
|
6
48
|
*/
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
49
|
+
export const retriesValidator = v.object({ limit: v.number(), delayMs: v.number(), giveUp: v.string() })
|
|
50
|
+
export type Retries = Infer<typeof retriesValidator>
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* What a code or agent step's code may use, as its definition declares it: the services the worker gives its runs (by
|
|
54
|
+
* key), and the workflows whose tasks it may read through `Tasks` and write through `others`.
|
|
55
|
+
*/
|
|
56
|
+
export const stepAccessValidator = v.object({
|
|
57
|
+
services: v.array(v.string()),
|
|
58
|
+
tasks: v.optional(v.object({ read: v.array(v.string()), write: v.array(v.string()) })),
|
|
59
|
+
})
|
|
60
|
+
export type StepAccessGraph = Infer<typeof stepAccessValidator>
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* How long a workflow keeps its records, set on the workflow and overridden on a step. A step run of a task that has
|
|
64
|
+
* ended goes once it ended more than `days` ago, unless it is one of its step's newest `keepAtLeastRuns`; a turn task (one
|
|
65
|
+
* a timer made) goes the same way, its runs and notes with it, unless it is one of the newest that many at its end state.
|
|
66
|
+
* An item's task, made by a request, a list or `createTask`, is the work's state, not its history, and never goes. Alex,
|
|
67
|
+
* 2026-11-21: "time based retention is a good idea. it should be configurable at the highest level with allowed step
|
|
68
|
+
* level overrides ... i think 7 days sounds reasonable"; 2026-11-22: "10k runs".
|
|
69
|
+
*/
|
|
70
|
+
export const retentionValidator = v.object({ days: v.optional(v.number()), keepAtLeastRuns: v.optional(v.number()) })
|
|
71
|
+
export type Retention = Infer<typeof retentionValidator>
|
|
72
|
+
export const defaultRetention = { days: 7, keepAtLeastRuns: 10_000 } as const
|
|
73
|
+
|
|
74
|
+
/** What a step keeps: its own setting, else its workflow's, else the platform's. */
|
|
75
|
+
export const retentionOf = (graph: WorkflowGraph, step: string | null): { readonly days: number; readonly keepAtLeastRuns: number } => {
|
|
76
|
+
const own = step === null ? undefined : graph.steps.find((candidate) => candidate.id === step)?.retention
|
|
77
|
+
return {
|
|
78
|
+
days: own?.days ?? graph.retention?.days ?? defaultRetention.days,
|
|
79
|
+
keepAtLeastRuns: own?.keepAtLeastRuns ?? graph.retention?.keepAtLeastRuns ?? defaultRetention.keepAtLeastRuns,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export const stepValidator = v.object({
|
|
84
|
+
id: v.string(),
|
|
85
|
+
kind: stepKindValidator,
|
|
86
|
+
endStates: v.array(v.string()),
|
|
87
|
+
/** What this step's records keep, over its workflow's (`retentionOf`). */
|
|
88
|
+
retention: v.optional(retentionValidator),
|
|
89
|
+
/** Set on every code and agent step; nothing listed grants nothing. */
|
|
90
|
+
access: v.optional(stepAccessValidator),
|
|
91
|
+
workflow: v.optional(subWorkflowValidator),
|
|
92
|
+
retries: v.optional(retriesValidator),
|
|
93
|
+
/**
|
|
94
|
+
* An inputs gate (`inputsChanged`): its runs record a fingerprint, and each run is given the newest one whose turn
|
|
95
|
+
* settled it, meaning its task ended in one of these workflow end states. A turn that saw changed inputs and then failed
|
|
96
|
+
* settles nothing, so the next one sees the change again.
|
|
97
|
+
*/
|
|
98
|
+
inputsSettledBy: v.optional(v.array(v.string())),
|
|
99
|
+
/** A code step that reads a declared item list: the id of the `list` event whose tasks its runs keep in step with it. */
|
|
100
|
+
syncs: v.optional(v.string()),
|
|
101
|
+
})
|
|
102
|
+
export type GraphStep = Infer<typeof stepValidator>
|
|
103
|
+
|
|
104
|
+
export const lineStartValidator = v.union(
|
|
105
|
+
v.object({ event: v.string() }),
|
|
106
|
+
v.object({ step: v.string(), endState: v.string() }),
|
|
107
|
+
/** A step consuming one of the workflow's end states: tasks that reach that end state wait there as a queue. */
|
|
108
|
+
v.object({ end: v.string() }),
|
|
109
|
+
)
|
|
110
|
+
export type LineStart = Infer<typeof lineStartValidator>
|
|
111
|
+
|
|
112
|
+
export const lineEndValidator = v.union(v.object({ step: v.string() }), v.object({ end: v.string() }))
|
|
113
|
+
export type LineEnd = Infer<typeof lineEndValidator>
|
|
114
|
+
|
|
115
|
+
export const lineValidator = v.object({ id: v.string(), from: lineStartValidator, to: lineEndValidator })
|
|
116
|
+
export type GraphLine = Infer<typeof lineValidator>
|
|
117
|
+
|
|
118
|
+
export const budgetsValidator = v.object({ agents: v.number(), cpu: v.number() })
|
|
119
|
+
export type Budgets = Infer<typeof budgetsValidator>
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
export const graphValidator = v.object({
|
|
123
|
+
name: v.string(),
|
|
124
|
+
endStates: v.array(v.string()),
|
|
125
|
+
events: v.array(eventValidator),
|
|
126
|
+
steps: v.array(stepValidator),
|
|
127
|
+
lines: v.array(lineValidator),
|
|
128
|
+
/** Defaults only: a registered workflow keeps the budgets it was given with `setBudgets`. */
|
|
129
|
+
budgets: budgetsValidator,
|
|
130
|
+
/** What the workflow's records keep, unless a step sets its own (`retentionOf`). */
|
|
131
|
+
retention: v.optional(retentionValidator),
|
|
132
|
+
/**
|
|
133
|
+
* At most `limit` of its tasks in progress at once: past the line an event put them on, and not yet ended. With
|
|
134
|
+
* `per`, the limit holds for each value of that field instead, so a workflow with `{ limit: 1, per: "function" }` is a
|
|
135
|
+
* lock on each function, held from its first step to its end by whichever workflow's task (through a sub-workflow
|
|
136
|
+
* step) got there first. A budget counts running steps; this counts tasks, so it holds across several steps.
|
|
137
|
+
*/
|
|
138
|
+
inProgress: v.optional(v.object({ limit: v.number(), per: v.optional(v.string()) })),
|
|
139
|
+
})
|
|
140
|
+
export type WorkflowGraph = Infer<typeof graphValidator>
|
|
141
|
+
|
|
142
|
+
export const taskFileValidator = v.object({ name: v.string(), ref: v.string() })
|
|
143
|
+
export type TaskFile = Infer<typeof taskFileValidator>
|
|
144
|
+
|
|
145
|
+
/** Which budget a step takes one unit of while a task is in it. A sub-workflow step takes none: its tasks use the sub-workflow's budgets. */
|
|
146
|
+
export const budgetOfStepKind = (kind: StepKind): keyof Budgets | "none" =>
|
|
147
|
+
kind === "agent" ? "agents" : kind === "code" ? "cpu" : "none"
|
|
148
|
+
|
|
149
|
+
export const lineIdOf = (line: { readonly from: LineStart; readonly to: LineEnd }): string => {
|
|
150
|
+
const from = "event" in line.from
|
|
151
|
+
? `event.${line.from.event}`
|
|
152
|
+
: "step" in line.from
|
|
153
|
+
? `step.${line.from.step}.${line.from.endState}`
|
|
154
|
+
: `end.${line.from.end}`
|
|
155
|
+
const to = "step" in line.to ? `step.${line.to.step}` : `end.${line.to.end}`
|
|
156
|
+
return `${from}>${to}`
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The place a task waits after travelling a line: the line itself, or the end state it leads to. */
|
|
160
|
+
export const placeAtEndOf = (line: GraphLine) => ("end" in line.to ? `end:${line.to.end}` : `line:${line.id}`)
|
|
161
|
+
|
|
162
|
+
/** A line out of an end state makes that end state the queue in front of its step. */
|
|
163
|
+
export const placeAtStartOf = (line: GraphLine) => ("end" in line.from ? `end:${line.from.end}` : `line:${line.id}`)
|
|
164
|
+
|
|
165
|
+
export const endStateOfPlace = (place: string): Option.Option<string> =>
|
|
166
|
+
place.startsWith("end:") ? Option.some(place.slice("end:".length)) : Option.none()
|
|
167
|
+
|
|
168
|
+
export const stepOfPlace = (place: string): Option.Option<string> =>
|
|
169
|
+
place.startsWith("step:") ? Option.some(place.slice("step:".length)) : Option.none()
|
|
170
|
+
|
|
171
|
+
export const sameStart = (a: LineStart, b: LineStart) =>
|
|
172
|
+
("event" in a && "event" in b && a.event === b.event) ||
|
|
173
|
+
("step" in a && "step" in b && a.step === b.step && a.endState === b.endState) ||
|
|
174
|
+
("end" in a && "end" in b && a.end === b.end)
|
|
175
|
+
|
|
176
|
+
export const lineFrom = (graph: WorkflowGraph, start: LineStart): Option.Option<GraphLine> =>
|
|
177
|
+
Option.fromNullishOr(graph.lines.find((line) => sameStart(line.from, start)))
|
|
178
|
+
|
|
179
|
+
export const inputPlacesOf = (graph: WorkflowGraph, step: string): ReadonlyArray<string> =>
|
|
180
|
+
graph.lines.filter((line) => "step" in line.to && line.to.step === step).map(placeAtStartOf)
|
|
181
|
+
|
|
182
|
+
/** Events that bring a new task into the workflow; a change event starts a flow for a task already in it. */
|
|
183
|
+
export const createsTasks = (event: GraphEvent) => event.kind !== "change"
|
|
184
|
+
|
|
185
|
+
/** Every structural problem in a graph, so a broken definition is refused at registration, not mid-run. */
|
|
186
|
+
export const problemsOf = (graph: WorkflowGraph): ReadonlyArray<string> => {
|
|
187
|
+
const problems: Array<string> = []
|
|
188
|
+
const steps = new Map(graph.steps.map((step) => [step.id, step]))
|
|
189
|
+
const events = new Map(graph.events.map((event) => [event.id, event]))
|
|
190
|
+
if (steps.size !== graph.steps.length) problems.push("two steps share an id")
|
|
191
|
+
if (events.size !== graph.events.length) problems.push("two events share an id")
|
|
192
|
+
const ends = new Set(graph.endStates)
|
|
193
|
+
const starts: Array<LineStart> = []
|
|
194
|
+
for (const line of graph.lines) {
|
|
195
|
+
if (line.id !== lineIdOf(line)) problems.push(`line ${line.id} should have id ${lineIdOf(line)}`)
|
|
196
|
+
if (starts.some((start) => sameStart(start, line.from))) problems.push(`two lines leave ${lineIdOf(line).split(">")[0]}`)
|
|
197
|
+
starts.push(line.from)
|
|
198
|
+
if ("event" in line.from && !events.has(line.from.event)) problems.push(`line ${line.id} leaves unknown event ${line.from.event}`)
|
|
199
|
+
if ("step" in line.from) {
|
|
200
|
+
const step = steps.get(line.from.step)
|
|
201
|
+
if (step === undefined) problems.push(`line ${line.id} leaves unknown step ${line.from.step}`)
|
|
202
|
+
else if (!step.endStates.includes(line.from.endState)) problems.push(`line ${line.id} leaves unknown end state ${line.from.endState} of ${step.id}`)
|
|
203
|
+
}
|
|
204
|
+
if ("end" in line.from && !ends.has(line.from.end)) problems.push(`line ${line.id} leaves unknown workflow end state ${line.from.end}`)
|
|
205
|
+
if ("step" in line.to && !steps.has(line.to.step)) problems.push(`line ${line.id} enters unknown step ${line.to.step}`)
|
|
206
|
+
if ("end" in line.to && !ends.has(line.to.end)) problems.push(`line ${line.id} enters unknown workflow end state ${line.to.end}`)
|
|
207
|
+
}
|
|
208
|
+
for (const event of graph.events) {
|
|
209
|
+
if (Option.isNone(lineFrom(graph, { event: event.id }))) problems.push(`event ${event.id} has no line out of it`)
|
|
210
|
+
if (event.kind === "change" && event.resumes !== undefined && !ends.has(event.resumes)) problems.push(`event ${event.id} resumes unknown end state ${event.resumes}`)
|
|
211
|
+
if (event.kind === "timer" && event.fieldsProblem !== undefined) problems.push(`timer event ${event.id} cannot start a turn: ${event.fieldsProblem}`)
|
|
212
|
+
if (event.kind === "timer" && event.fieldsProblem === undefined && event.fields === undefined) problems.push(`timer event ${event.id} has no fields for its turn task`)
|
|
213
|
+
if (event.kind === "list" && !ends.has(event.leftAs)) problems.push(`event ${event.id} ends a task that left its list in ${event.leftAs}, which is not an end state of the workflow`)
|
|
214
|
+
if (event.kind === "list" && !graph.steps.some((step) => step.syncs === event.id)) problems.push(`list event ${event.id} has no step that syncs it`)
|
|
215
|
+
}
|
|
216
|
+
for (const step of graph.steps) {
|
|
217
|
+
if ((step.kind === "workflow") !== (step.workflow !== undefined)) problems.push(`step ${step.id} must name a sub-workflow exactly when its kind is workflow`)
|
|
218
|
+
if (step.kind === "workflow" && step.access !== undefined) problems.push(`step ${step.id} is a sub-workflow step, whose code runs in its own workflow, so it declares no access`)
|
|
219
|
+
for (const workflow of [...(step.access?.tasks?.read ?? []), ...(step.access?.tasks?.write ?? [])]) {
|
|
220
|
+
if (workflow.length === 0) problems.push(`step ${step.id} names a workflow with no name in its access`)
|
|
221
|
+
}
|
|
222
|
+
for (const end of step.inputsSettledBy ?? []) {
|
|
223
|
+
if (!ends.has(end)) problems.push(`step ${step.id} settles its inputs by ${end}, which is not an end state of the workflow`)
|
|
224
|
+
}
|
|
225
|
+
if (step.inputsSettledBy !== undefined && step.inputsSettledBy.length === 0) problems.push(`step ${step.id} names no end state that settles its inputs`)
|
|
226
|
+
if (step.syncs !== undefined) {
|
|
227
|
+
if (events.get(step.syncs)?.kind !== "list") problems.push(`step ${step.id} syncs ${step.syncs}, which is not a list event of the workflow`)
|
|
228
|
+
if (step.kind !== "code") problems.push(`step ${step.id} syncs a list, which only a code step does`)
|
|
229
|
+
}
|
|
230
|
+
if (step.retries !== undefined) {
|
|
231
|
+
if (step.kind === "workflow") problems.push(`step ${step.id} is a sub-workflow step, which has no runs to retry`)
|
|
232
|
+
if (!Number.isInteger(step.retries.limit) || step.retries.limit < 1) problems.push(`step ${step.id} retries a run at least once`)
|
|
233
|
+
if (step.retries.delayMs < 0) problems.push(`step ${step.id} waits no less than 0 ms between tries`)
|
|
234
|
+
if (!step.endStates.includes(step.retries.giveUp)) problems.push(`step ${step.id} gives up through ${step.retries.giveUp}, which is not one of its end states`)
|
|
235
|
+
}
|
|
236
|
+
for (const endState of step.endStates) {
|
|
237
|
+
if (Option.isNone(lineFrom(graph, { step: step.id, endState }))) problems.push(`end state ${endState} of step ${step.id} has nowhere to go`)
|
|
238
|
+
}
|
|
239
|
+
if (inputPlacesOf(graph, step.id).length === 0) problems.push(`step ${step.id} has no line into it`)
|
|
240
|
+
}
|
|
241
|
+
if (graph.inProgress !== undefined && !(Number.isInteger(graph.inProgress.limit) && graph.inProgress.limit >= 1)) problems.push("the workflow lets at least one task be in progress at once")
|
|
242
|
+
for (const [who, retention] of [["the workflow", graph.retention] as const, ...graph.steps.map((step) => [`step ${step.id}`, step.retention] as const)]) {
|
|
243
|
+
if (retention?.days !== undefined && !(retention.days >= 0)) problems.push(`${who} keeps its records no fewer than 0 days`)
|
|
244
|
+
// A gate reads its newest settled run, so every step keeps at least its newest one.
|
|
245
|
+
if (retention?.keepAtLeastRuns !== undefined && !(Number.isInteger(retention.keepAtLeastRuns) && retention.keepAtLeastRuns >= 1)) problems.push(`${who} keeps at least its newest run`)
|
|
246
|
+
}
|
|
247
|
+
return problems
|
|
12
248
|
}
|
|
13
249
|
|
|
14
|
-
/** The ends of a graph: before a run's first step and after its last. */
|
|
15
|
-
export const RUN_STARTS = 'run starts';
|
|
16
|
-
export const RUN_ENDS = 'run ends';
|
|
17
|
-
/** An edge from any step, taken when that step fails. */
|
|
18
|
-
export const A_STEP_FAILS = 'a step fails';
|
|
19
250
|
|
|
20
|
-
/**
|
|
21
|
-
export const
|
|
251
|
+
/** The end state of a step making one sub-workflow task per list entry, reached once every one of them has ended. */
|
|
252
|
+
export const everySubWorkflowTaskEnded = "ended"
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* One thing a step, or a call that sent the task back, said about a task, at the time it was said. `step` is absent on a
|
|
256
|
+
* note no step wrote, such as the reason a task was requeued. `workflow` names the task's workflow, which differs from
|
|
257
|
+
* the reader's for a note on a parent task.
|
|
258
|
+
*/
|
|
259
|
+
/** The run that wrote a note on another workflow's or another task's behalf (`StepOutcome.others`): its workflow, step and task. */
|
|
260
|
+
export const noteWriterValidator = v.object({ workflow: v.string(), step: v.string(), task: v.string() })
|
|
261
|
+
export type NoteWriter = Infer<typeof noteWriterValidator>
|
|
262
|
+
|
|
263
|
+
export const taskNoteValidator = v.object({
|
|
264
|
+
workflow: v.string(),
|
|
265
|
+
step: v.optional(v.string()),
|
|
266
|
+
note: v.string(),
|
|
267
|
+
at: v.number(),
|
|
268
|
+
/** Set on a note another task's run wrote through `others`: who wrote it, as fields to match on, not words. */
|
|
269
|
+
by: v.optional(noteWriterValidator),
|
|
270
|
+
})
|
|
271
|
+
export type TaskNote = Infer<typeof taskNoteValidator>
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Where a sub-workflow task came from: the parent workflow, the sub-workflow step and the parent task that started it,
|
|
275
|
+
* the parent's flow waiting in that step, and the list entry it is for.
|
|
276
|
+
*/
|
|
277
|
+
export const parentRefValidator = v.object({ workflow: v.string(), step: v.string(), task: v.string(), flow: v.string(), entryIndex: v.number() })
|
|
278
|
+
export type ParentRef = Infer<typeof parentRefValidator>
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* The workforce mutations (Convex function handles) a workflow component schedules to reach another workflow's
|
|
282
|
+
* component: one creates sub-workflow tasks, the other tells a parent that one of them ended.
|
|
283
|
+
*/
|
|
284
|
+
export const workforceHooksValidator = v.object({ startSubWorkflowTasks: v.string(), subWorkflowTaskEnded: v.string() })
|
|
285
|
+
export type WorkforceHooks = Infer<typeof workforceHooksValidator>
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* What one step run spent and used, as the worker measured it; the old platform's step records keep the same figures.
|
|
289
|
+
* Input tokens are split by how the provider prices them; `requests` counts the API calls an agent made, and `costUsd`
|
|
290
|
+
* is what those calls cost in US dollars, as the organization's agent runner reports it.
|
|
291
|
+
*/
|
|
292
|
+
export const stepSpendValidator = v.object({
|
|
293
|
+
fresh: v.number(),
|
|
294
|
+
cacheWrite: v.number(),
|
|
295
|
+
cacheRead: v.number(),
|
|
296
|
+
output: v.number(),
|
|
297
|
+
requests: v.number(),
|
|
298
|
+
costUsd: v.number(),
|
|
299
|
+
compiles: v.number(),
|
|
300
|
+
})
|
|
301
|
+
export type StepSpend = Infer<typeof stepSpendValidator>
|
|
302
|
+
export const noSpend: StepSpend = { fresh: 0, cacheWrite: 0, cacheRead: 0, output: 0, requests: 0, costUsd: 0, compiles: 0 }
|
|
303
|
+
|
|
304
|
+
export const stepUsedValidator = v.object({
|
|
305
|
+
/** CPU of the worker process's own work while the step ran. */
|
|
306
|
+
ownCpuSeconds: v.number(),
|
|
307
|
+
/** CPU of processes the step started, as the step reports it. */
|
|
308
|
+
childCpuSeconds: v.number(),
|
|
309
|
+
peakMemoryMb: v.number(),
|
|
310
|
+
filesWritten: v.number(),
|
|
311
|
+
})
|
|
312
|
+
export type StepUsed = Infer<typeof stepUsedValidator>
|
|
313
|
+
export const nothingUsed: StepUsed = { ownCpuSeconds: 0, childCpuSeconds: 0, peakMemoryMb: 0, filesWritten: 0 }
|
|
314
|
+
|
|
315
|
+
/** A worker's measurements of one step run, sent with its end state or its failure. */
|
|
316
|
+
export const stepMeasurementsValidator = v.object({
|
|
317
|
+
spend: stepSpendValidator,
|
|
318
|
+
used: stepUsedValidator,
|
|
319
|
+
/** The agent (provider and model) that ran an agent step; absent for a step that ran no agent. */
|
|
320
|
+
agent: v.optional(v.string()),
|
|
321
|
+
/** Names of the files the step wrote. */
|
|
322
|
+
files: v.array(v.string()),
|
|
323
|
+
/** The transcripts of the agent runs the step made, kept through the organization's `FileStore`. */
|
|
324
|
+
transcripts: v.array(taskFileValidator),
|
|
325
|
+
/** What the step read of its inputs, as one fingerprint, for the next run of the step to compare (`inputsChanged`). */
|
|
326
|
+
fingerprint: v.optional(v.string()),
|
|
327
|
+
})
|
|
328
|
+
export type StepMeasurements = Infer<typeof stepMeasurementsValidator>
|
|
329
|
+
|
|
330
|
+
/** A failed step's error as its record keeps it: 8,000 characters, the start and the end of a longer one. */
|
|
331
|
+
export const errorKept = (error: string): string =>
|
|
332
|
+
error.length <= 8000 ? error : `${error.slice(0, 5000)}\n… ${error.length - 8000} characters of the error left out …\n${error.slice(-3000)}`
|
|
333
|
+
|
|
334
|
+
/** A short fingerprint of a graph, recorded on every step run so a record says which version of its workflow ran. */
|
|
335
|
+
export const versionOf = (graph: WorkflowGraph): string => {
|
|
336
|
+
let hash = 0x811c9dc5
|
|
337
|
+
for (const character of JSON.stringify(graph)) {
|
|
338
|
+
hash ^= character.charCodeAt(0)
|
|
339
|
+
hash = Math.imul(hash, 0x01000193) >>> 0
|
|
340
|
+
}
|
|
341
|
+
return hash.toString(16).padStart(8, "0")
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* One place of a workflow's diagram, as the renderer names it: a line by its id, a step by its id, an end state by its
|
|
346
|
+
* name. `inFrontOf` names a step and means its whole queue: every task waiting on any line into it.
|
|
347
|
+
*/
|
|
348
|
+
export const placeRefValidator = v.union(
|
|
349
|
+
v.object({ line: v.string() }),
|
|
350
|
+
v.object({ step: v.string() }),
|
|
351
|
+
v.object({ end: v.string() }),
|
|
352
|
+
v.object({ inFrontOf: v.string() }),
|
|
353
|
+
)
|
|
354
|
+
export type PlaceRef = Infer<typeof placeRefValidator>
|
|
22
355
|
|
|
23
|
-
/**
|
|
24
|
-
export const
|
|
25
|
-
|
|
356
|
+
/** A write to another task (`others`) a run made that was not applied: the task, its workflow, and why, in words. */
|
|
357
|
+
export const skippedOtherValidator = v.object({ workflow: v.string(), task: v.string(), reason: v.string() })
|
|
358
|
+
export type SkippedOther = Infer<typeof skippedOtherValidator>
|