@jive-sh/workflow-platform 0.6.3 → 1.0.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 +66 -295
- package/component/_generated/api.ts +72 -0
- package/component/_generated/component.ts +621 -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 +755 -0
- package/component/errors.ts +66 -0
- package/component/events.ts +17 -0
- package/component/notes.ts +13 -0
- package/component/places.ts +136 -0
- package/component/retention.ts +205 -0
- package/component/runEngine.ts +25 -0
- package/component/schema.ts +132 -0
- package/component/steps.ts +188 -0
- package/component/store.ts +512 -0
- package/component/tasks.ts +289 -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 +111 -0
- package/dist/definition/workflow.js.map +1 -0
- package/dist/graph.js +301 -9
- package/dist/graph.js.map +1 -1
- package/dist/index.js +7 -133
- 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 +255 -0
- package/src/graph.ts +341 -19
- package/src/index.ts +8 -168
- package/worker/directory-file-store.ts +42 -0
- package/worker/index.ts +12 -0
- package/worker/platform-client.ts +138 -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 -134
- package/dist/dashboard/drilldown.js +0 -16
- 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/spending-words.js +0 -32
- 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 -77
- 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/spending-limit.js +0 -88
- 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-usage.js +0 -19
- package/dist/step-usage.js.map +0 -1
- package/dist/steps.js +0 -920
- package/dist/steps.js.map +0 -1
- package/dist/storage/budgets.js +0 -85
- package/dist/storage/budgets.js.map +0 -1
- package/dist/storage/engine-records.js +0 -75
- 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 -723
- 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 -24
- package/dist/storage/task-store.js.map +0 -1
- package/dist/tasks.js +0 -475
- 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 -75
- 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 -34
- package/dist/views/step-graph.js.map +0 -1
- package/dist/views/workflow-flows.js +0 -53
- package/dist/views/workflow-flows.js.map +0 -1
- package/dist/views/workflows-view.js +0 -230
- package/dist/views/workflows-view.js.map +0 -1
- package/src/budget-states.ts +0 -55
- 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 -134
- package/src/dashboard/drilldown.ts +0 -43
- package/src/dashboard/index.tsx +0 -4
- package/src/dashboard/machine-controls.tsx +0 -53
- package/src/dashboard/queue-bar.tsx +0 -43
- package/src/dashboard/queue-chart.tsx +0 -150
- package/src/dashboard/queue-lines.ts +0 -87
- package/src/dashboard/queue-table.ts +0 -37
- package/src/dashboard/spending-limit-control.tsx +0 -50
- package/src/dashboard/spending-words.ts +0 -50
- package/src/dashboard/step-graph.tsx +0 -106
- package/src/dashboard/workflow-dag.tsx +0 -204
- package/src/dashboard/workflow-drilldown.tsx +0 -89
- package/src/dashboard/workflows-dashboard.tsx +0 -414
- package/src/failure.ts +0 -36
- package/src/keep-running.ts +0 -83
- package/src/outside-failure.ts +0 -39
- package/src/pick-wakes.ts +0 -182
- package/src/project.ts +0 -115
- package/src/queue-counts.ts +0 -64
- package/src/reasons.ts +0 -30
- package/src/spending-limit.ts +0 -156
- package/src/step-files.ts +0 -208
- package/src/step-meter.ts +0 -120
- package/src/step-usage.ts +0 -23
- package/src/steps.ts +0 -1239
- package/src/storage/budgets.ts +0 -113
- package/src/storage/engine-records.ts +0 -102
- 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 -829
- package/src/storage/step-records.ts +0 -306
- package/src/storage/storage-error.ts +0 -35
- package/src/storage/task-store.ts +0 -235
- package/src/tasks.ts +0 -561
- package/src/views/format.ts +0 -40
- package/src/views/runs-now.ts +0 -120
- package/src/views/step-funnel.ts +0 -163
- package/src/views/step-graph.ts +0 -68
- package/src/views/workflow-flows.ts +0 -97
- package/src/views/workflows-view.ts +0 -443
package/worker/worker.ts
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Cause, Context, Data, Duration, Effect, Exit, Option, pipe, Result, Schedule, Schema } from "effect"
|
|
2
|
+
import { AgentRunner } from "../src/definition/agent-runner"
|
|
3
|
+
import { FileStore } from "../src/definition/file-store"
|
|
4
|
+
import { ProgramBuild } from "../src/definition/program-build"
|
|
5
|
+
import { Tasks } from "../src/definition/tasks"
|
|
6
|
+
import type { WorkflowDefinition } from "../src/definition/workflow"
|
|
7
|
+
import { nothingUsed, noSpend, type StepMeasurements, type TaskFile } from "../src/graph"
|
|
8
|
+
import { type PlatformCallFailed, type ReportedOtherWrite, type TakenStep, WorkflowPlatformClient } from "./platform-client"
|
|
9
|
+
import { StepMeter } from "./step-meter"
|
|
10
|
+
import { RunTasks } from "./worker-tasks"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* What a worker needs around it: the platform to take tasks from, the file store it keeps steps' files in, and the
|
|
14
|
+
* organization's implementations of every service its workflows' steps declare. `Tasks` it makes itself, per run.
|
|
15
|
+
*/
|
|
16
|
+
export type WorkerServices<Services = never> = WorkflowPlatformClient | FileStore | Exclude<Services, Tasks>
|
|
17
|
+
|
|
18
|
+
/** The services the steps of `Workflows` declare, together. */
|
|
19
|
+
export type ServicesOfWorkflows<Workflows extends ReadonlyArray<WorkflowDefinition<any, any, any>>> =
|
|
20
|
+
Workflows[number] extends WorkflowDefinition<any, any, infer Services> ? Services : never
|
|
21
|
+
|
|
22
|
+
export interface WorkerOptions<Workflows extends ReadonlyArray<WorkflowDefinition<any, any, any>> = ReadonlyArray<WorkflowDefinition<any, any, any>>> {
|
|
23
|
+
/** Recorded on every step run this worker takes. */
|
|
24
|
+
readonly worker: string
|
|
25
|
+
/** Every code and agent step of these workflows is one this worker can run. */
|
|
26
|
+
readonly workflows: Workflows
|
|
27
|
+
/** How long a taken task stays this worker's without a heartbeat before Convex gives it to another. */
|
|
28
|
+
readonly leaseMs: number
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A worker refuses to start when a step it serves declares a service it was not given. */
|
|
32
|
+
export class WorkerLacksService extends Data.TaggedError("WorkerLacksService")<{ readonly workflow: string; readonly step: string; readonly service: string }> {
|
|
33
|
+
override get message() {
|
|
34
|
+
return `this worker cannot run step ${this.step} of ${this.workflow}: the step uses ${this.service}, which the worker was not given`
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** What one attempt to take a task did. */
|
|
39
|
+
export type WorkResult = { readonly _tag: "Idle" } | { readonly _tag: "Ran"; readonly taken: TakenStep; readonly outcome: "completed" | "failed" | "unreported" }
|
|
40
|
+
|
|
41
|
+
/** Fails with the first step of `workflows` whose declared services `context` lacks. */
|
|
42
|
+
const checkServices = function* (input: { readonly workflows: ReadonlyArray<WorkflowDefinition<any, any, any>>; readonly context: Context.Context<never> }): Effect.fn.Return<void, WorkerLacksService> {
|
|
43
|
+
for (const workflow of input.workflows) {
|
|
44
|
+
for (const [step, definition] of Object.entries(workflow.runnableSteps)) {
|
|
45
|
+
for (const service of definition.access.services) {
|
|
46
|
+
if (Option.isNone(Context.getOption(input.context, service))) {
|
|
47
|
+
return yield* Effect.fail(new WorkerLacksService({ workflow: workflow.name, step, service: service.key }))
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Takes one task from Convex, runs its step with only the services the step declares, and reports the end state with
|
|
56
|
+
* the task's changed fields and files and the step's writes to other tasks. A step that fails or dies is reported as
|
|
57
|
+
* failed, and Convex puts its task back at the front of its queue.
|
|
58
|
+
*/
|
|
59
|
+
export const workOnce = function* <const Workflows extends ReadonlyArray<WorkflowDefinition<any, any, any>>>(
|
|
60
|
+
options: WorkerOptions<Workflows>,
|
|
61
|
+
): Effect.fn.Return<WorkResult, unknown, WorkerServices<ServicesOfWorkflows<Workflows>>> {
|
|
62
|
+
const context = yield* Effect.context<WorkerServices<ServicesOfWorkflows<Workflows>>>()
|
|
63
|
+
yield* checkServices({ workflows: options.workflows, context })
|
|
64
|
+
return yield* runOnce({ options, context })
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const runOnce = function* <Services>(input: {
|
|
68
|
+
readonly options: WorkerOptions
|
|
69
|
+
readonly context: Context.Context<WorkerServices<Services>>
|
|
70
|
+
}): Effect.fn.Return<WorkResult, unknown, WorkerServices<Services>> {
|
|
71
|
+
const options = input.options
|
|
72
|
+
const client = yield* WorkflowPlatformClient
|
|
73
|
+
const steps = options.workflows.flatMap((workflow) => Object.keys(workflow.runnableSteps).map((step) => ({ workflow: workflow.name, step })))
|
|
74
|
+
// Workers racing for one queue head can exhaust Convex's own retries; the loser asks again after a jittered wait,
|
|
75
|
+
// for about 5 seconds in all, since a loaded machine can take a second over each take.
|
|
76
|
+
const taken = yield* pipe(
|
|
77
|
+
Effect.gen(() => client.take({ worker: options.worker, steps, leaseMs: options.leaseMs })),
|
|
78
|
+
Effect.retry({ schedule: Schedule.jittered(Schedule.exponential(Duration.millis(20))), times: 8 }),
|
|
79
|
+
)
|
|
80
|
+
if (taken === null) return { _tag: "Idle" }
|
|
81
|
+
|
|
82
|
+
const workflow = options.workflows.find((candidate) => candidate.name === taken.workflow)
|
|
83
|
+
const step = workflow?.runnableSteps[taken.step]
|
|
84
|
+
if (workflow === undefined || step === undefined) {
|
|
85
|
+
const reported = yield* reportFailure({
|
|
86
|
+
taken,
|
|
87
|
+
error: `worker ${options.worker} has no code for step ${taken.step} of ${taken.workflow}`,
|
|
88
|
+
measurements: { spend: noSpend, used: nothingUsed, files: [], transcripts: [] },
|
|
89
|
+
})
|
|
90
|
+
return { _tag: "Ran", taken, outcome: reported ? "failed" : "unreported" }
|
|
91
|
+
}
|
|
92
|
+
const fileStore = yield* FileStore
|
|
93
|
+
const meter = yield* Effect.sync(() => new StepMeter())
|
|
94
|
+
|
|
95
|
+
// The run sees only what its step declared: those services (the agent runner and program build metered), and its
|
|
96
|
+
// own `Tasks` when it declared tasks to read or write.
|
|
97
|
+
let granted = Context.pick(...step.access.services)(input.context)
|
|
98
|
+
const maybeAgentRunner = Context.getOption(granted, AgentRunner)
|
|
99
|
+
if (Option.isSome(maybeAgentRunner)) granted = Context.add(granted, AgentRunner, meter.meteredAgentRunner(maybeAgentRunner.value))
|
|
100
|
+
const maybeProgramBuild = Context.getOption(granted, ProgramBuild)
|
|
101
|
+
if (Option.isSome(maybeProgramBuild)) granted = Context.add(granted, ProgramBuild, meter.meteredProgramBuild(maybeProgramBuild.value))
|
|
102
|
+
if (step.access.tasks !== undefined) granted = Context.add(granted, Tasks, new RunTasks(client, taken))
|
|
103
|
+
const stepContext = granted
|
|
104
|
+
|
|
105
|
+
// The lease taken with the task covers the first interval, so the first heartbeat waits a third of it.
|
|
106
|
+
const heartbeatEvery = Duration.millis(Math.max(1, Math.floor(options.leaseMs / 3)))
|
|
107
|
+
const heartbeats = pipe(
|
|
108
|
+
Effect.sleep(heartbeatEvery),
|
|
109
|
+
Effect.andThen(Effect.gen(() => client.heartbeat({ workflow: taken.workflow, run: taken.run, leaseMs: options.leaseMs }))),
|
|
110
|
+
Effect.repeat(Schedule.spaced(heartbeatEvery)),
|
|
111
|
+
)
|
|
112
|
+
const exit = yield* pipe(
|
|
113
|
+
Effect.gen(function* () {
|
|
114
|
+
const fields = yield* Schema.decodeUnknownEffect(workflow.task.fields)(taken.task.fields)
|
|
115
|
+
const outcome = yield* pipe(
|
|
116
|
+
Effect.gen(() =>
|
|
117
|
+
step.run({
|
|
118
|
+
taskId: taken.task.id,
|
|
119
|
+
fields,
|
|
120
|
+
files: taken.task.files,
|
|
121
|
+
notes: taken.task.notes,
|
|
122
|
+
lastFingerprint: Option.fromNullishOr(taken.lastFingerprint),
|
|
123
|
+
}),
|
|
124
|
+
),
|
|
125
|
+
Effect.updateContext(() => stepContext),
|
|
126
|
+
)
|
|
127
|
+
const encodedFields =
|
|
128
|
+
outcome.fields === undefined ? undefined : yield* Schema.encodeUnknownEffect(workflow.task.fields)({ ...fields, ...outcome.fields })
|
|
129
|
+
const encodeItem = Schema.encodeUnknownEffect(workflow.task.fields)
|
|
130
|
+
const listed = outcome.listed === undefined ? undefined : yield* Effect.forEach(outcome.listed, (item) => encodeItem(item))
|
|
131
|
+
const written: Array<TaskFile> = []
|
|
132
|
+
for (const file of outcome.files ?? []) written.push(yield* fileStore.write(file))
|
|
133
|
+
const files =
|
|
134
|
+
written.length === 0
|
|
135
|
+
? undefined
|
|
136
|
+
: taken.task.files.filter((file) => !written.some((newFile) => newFile.name === file.name)).concat(written)
|
|
137
|
+
const others: Array<ReportedOtherWrite> = []
|
|
138
|
+
for (const other of outcome.others ?? []) {
|
|
139
|
+
const otherFiles: Array<TaskFile> = []
|
|
140
|
+
for (const file of other.files ?? []) otherFiles.push(yield* fileStore.write(file))
|
|
141
|
+
others.push({
|
|
142
|
+
workflow: other.workflow ?? taken.workflow,
|
|
143
|
+
task: other.taskId,
|
|
144
|
+
note: other.note,
|
|
145
|
+
...(other.fields !== undefined ? { fields: other.fields } : {}),
|
|
146
|
+
...(otherFiles.length > 0 ? { files: otherFiles } : {}),
|
|
147
|
+
})
|
|
148
|
+
}
|
|
149
|
+
const measurements = yield* meter.measurements({
|
|
150
|
+
files: [...written, ...others.flatMap((other) => other.files ?? [])].map((file) => file.name),
|
|
151
|
+
reported: outcome.used ?? {},
|
|
152
|
+
fingerprint: Option.fromNullishOr(outcome.fingerprint),
|
|
153
|
+
})
|
|
154
|
+
return { endState: outcome.endState, encodedFields, files, measurements, note: outcome.note, listed, others }
|
|
155
|
+
}),
|
|
156
|
+
Effect.raceFirst(Effect.andThen(heartbeats, Effect.never)),
|
|
157
|
+
Effect.exit,
|
|
158
|
+
)
|
|
159
|
+
if (Exit.isFailure(exit)) {
|
|
160
|
+
const measurements = yield* meter.measurements({ files: [], reported: {}, fingerprint: Option.none() })
|
|
161
|
+
const reported = yield* reportFailure({ taken, error: Cause.pretty(exit.cause), measurements })
|
|
162
|
+
return { _tag: "Ran", taken, outcome: reported ? "failed" : "unreported" }
|
|
163
|
+
}
|
|
164
|
+
const completed = yield* reportedWithRetries(() =>
|
|
165
|
+
client.complete({
|
|
166
|
+
run: taken.run,
|
|
167
|
+
workflow: workflow.name,
|
|
168
|
+
endState: exit.value.endState,
|
|
169
|
+
measurements: exit.value.measurements,
|
|
170
|
+
...(exit.value.encodedFields !== undefined ? { fields: exit.value.encodedFields } : {}),
|
|
171
|
+
...(exit.value.files !== undefined ? { files: exit.value.files } : {}),
|
|
172
|
+
...(exit.value.note !== undefined ? { note: exit.value.note } : {}),
|
|
173
|
+
...(exit.value.listed !== undefined ? { listed: exit.value.listed } : {}),
|
|
174
|
+
...(exit.value.others.length > 0 ? { others: exit.value.others } : {}),
|
|
175
|
+
}),
|
|
176
|
+
)
|
|
177
|
+
if (Result.isSuccess(completed)) return { _tag: "Ran", taken, outcome: "completed" }
|
|
178
|
+
// Convex refused the end state, or never answered: the run fails with why, so its task is tried again.
|
|
179
|
+
const reported = yield* reportFailure({
|
|
180
|
+
taken,
|
|
181
|
+
error: `its end state ${exit.value.endState} could not be reported: ${completed.failure.message}`,
|
|
182
|
+
measurements: exit.value.measurements,
|
|
183
|
+
})
|
|
184
|
+
return { _tag: "Ran", taken, outcome: reported ? "failed" : "unreported" }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* A report to Convex, tried again a few times: a mutation that timed out was rolled back, so trying it again is safe,
|
|
189
|
+
* and a busy machine can take over a second for one.
|
|
190
|
+
*/
|
|
191
|
+
const reportedWithRetries = <A>(report: () => Effect.fn.Return<A, PlatformCallFailed>) =>
|
|
192
|
+
pipe(
|
|
193
|
+
Effect.gen(report),
|
|
194
|
+
Effect.retry({ schedule: Schedule.jittered(Schedule.exponential(Duration.millis(100))), times: 4 }),
|
|
195
|
+
Effect.result,
|
|
196
|
+
)
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Reports a failed run. When even that cannot reach Convex, the run is left to its lease, which fails it once this
|
|
200
|
+
* worker stops heartbeating, and the worker carries on with other tasks. Says whether the report got through.
|
|
201
|
+
*/
|
|
202
|
+
const reportFailure = function* (input: {
|
|
203
|
+
readonly taken: TakenStep
|
|
204
|
+
readonly error: string
|
|
205
|
+
readonly measurements: StepMeasurements
|
|
206
|
+
}): Effect.fn.Return<boolean, never, WorkflowPlatformClient> {
|
|
207
|
+
const client = yield* WorkflowPlatformClient
|
|
208
|
+
const failed = yield* reportedWithRetries(() => client.fail({ workflow: input.taken.workflow, run: input.taken.run, error: input.error, measurements: input.measurements }))
|
|
209
|
+
if (Result.isSuccess(failed)) return true
|
|
210
|
+
yield* Effect.logWarning(`run ${input.taken.run} of ${input.taken.workflow} step ${input.taken.step} could not be reported, so it is left to its lease: ${failed.failure.message}`)
|
|
211
|
+
return false
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Works until no step this worker can run has a task it may take, and returns what it ran. */
|
|
215
|
+
export const workUntilIdle = function* <const Workflows extends ReadonlyArray<WorkflowDefinition<any, any, any>>>(
|
|
216
|
+
options: WorkerOptions<Workflows>,
|
|
217
|
+
): Effect.fn.Return<ReadonlyArray<WorkResult>, unknown, WorkerServices<ServicesOfWorkflows<Workflows>>> {
|
|
218
|
+
const context = yield* Effect.context<WorkerServices<ServicesOfWorkflows<Workflows>>>()
|
|
219
|
+
yield* checkServices({ workflows: options.workflows, context })
|
|
220
|
+
const results: Array<WorkResult> = []
|
|
221
|
+
while (true) {
|
|
222
|
+
const result = yield* runOnce({ options, context })
|
|
223
|
+
if (result._tag === "Idle") return results
|
|
224
|
+
results.push(result)
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Works forever, waiting `idleMs` whenever there is nothing to take. It refuses to start without every service its steps declare. */
|
|
229
|
+
export const workForever = function* <const Workflows extends ReadonlyArray<WorkflowDefinition<any, any, any>>>(
|
|
230
|
+
options: WorkerOptions<Workflows> & { readonly idleMs: number },
|
|
231
|
+
): Effect.fn.Return<never, unknown, WorkerServices<ServicesOfWorkflows<Workflows>>> {
|
|
232
|
+
const context = yield* Effect.context<WorkerServices<ServicesOfWorkflows<Workflows>>>()
|
|
233
|
+
yield* checkServices({ workflows: options.workflows, context })
|
|
234
|
+
while (true) {
|
|
235
|
+
// Convex unreachable even after `take`'s retries: the loop waits and asks again rather than ending.
|
|
236
|
+
const attempt = yield* Effect.result(Effect.gen(() => runOnce({ options, context })))
|
|
237
|
+
if (Result.isFailure(attempt)) yield* Effect.logWarning(`worker ${options.worker} could not take a task: ${String(attempt.failure)}`)
|
|
238
|
+
if (Result.isFailure(attempt) || attempt.success._tag === "Idle") yield* Effect.sleep(Duration.millis(options.idleMs))
|
|
239
|
+
}
|
|
240
|
+
}
|
package/dist/budget-states.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/** WHAT EACH BUDGETED WORKFLOW'S LIMITS ASK FOR AGAINST WHAT IT HAS, for a dashboard and the route that sets a limit. Pure. */
|
|
2
|
-
import { runLimit } from './storage/budgets';
|
|
3
|
-
export const workflowBudgetStates = (workflows, budgets, work, assigned) => workflows.map((w) => {
|
|
4
|
-
const row = budgets.find((b) => b.workflow === w.name);
|
|
5
|
-
const runs = assigned.filter((a) => a.workflow === w.name);
|
|
6
|
-
const limits = { agents: row?.budget ?? 0, runs: row?.runs ?? null };
|
|
7
|
-
return {
|
|
8
|
-
workflow: w.name, phase: w.phase, budget: limits.agents, runs: runLimit(limits),
|
|
9
|
-
changedAt: row?.changed_at ?? 0, running: runs.length,
|
|
10
|
-
items: work.find((x) => x.workflow === w.name)?.items ?? 0,
|
|
11
|
-
newestStart: runs.length === 0 ? null : Math.max(...runs.map((r) => r.started)),
|
|
12
|
-
};
|
|
13
|
-
});
|
|
14
|
-
/** Fewer runs going than its run limit: a run with nothing to pick waits for an item, so work or none, it is a fault. */
|
|
15
|
-
export const belowBudget = (w) => w.running < w.runs;
|
|
16
|
-
/**
|
|
17
|
-
* Whether the trigger started a run above the run limit. Every run going now had started by the newest one's start, so that
|
|
18
|
-
* many were going then; if the limit was already this one, that start went over it. Runs above a limit lowered after they
|
|
19
|
-
* started are finishing their call, and end before the next.
|
|
20
|
-
*/
|
|
21
|
-
export const startedAboveBudget = (w) => w.running > w.runs && w.newestStart !== null && w.newestStart > w.changedAt;
|
|
22
|
-
/** Why the route refuses a limit, with its status; null when it takes it. `budgeted` is every workflow with limits. */
|
|
23
|
-
export const budgetRefusal = (workflow, asked, budget, budgeted) => {
|
|
24
|
-
if (workflow === null)
|
|
25
|
-
return { status: 404, error: `no workflow ${asked}` };
|
|
26
|
-
if (!budgeted.includes(workflow))
|
|
27
|
-
return { status: 409, error: `${workflow} has no agent step and no list of work, so it has no budget` };
|
|
28
|
-
return typeof budget === 'number' && Number.isInteger(budget) && budget >= 0 ? null : { status: 400, error: 'budget must be a whole number, 0 or more' };
|
|
29
|
-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