@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
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { ConvexHttpClient } from "convex/browser"
|
|
2
|
+
import type { FunctionReference } from "convex/server"
|
|
3
|
+
import { Context, Data, Effect, Schema } from "effect"
|
|
4
|
+
import type { FoundTask, TasksAt } from "../src/definition/tasks"
|
|
5
|
+
import type { StepMeasurements, TaskFile } from "../src/graph"
|
|
6
|
+
|
|
7
|
+
export class PlatformCallFailed extends Data.TaggedError("PlatformCallFailed")<{ readonly call: string; readonly cause: unknown }> {
|
|
8
|
+
override get message() {
|
|
9
|
+
return `${this.call} failed: ${String(this.cause)}`
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const TaskNote = Schema.Struct({ workflow: Schema.String, step: Schema.optionalKey(Schema.String), note: Schema.String, at: Schema.Number })
|
|
14
|
+
const File = Schema.Struct({ name: Schema.String, ref: Schema.String })
|
|
15
|
+
|
|
16
|
+
const TakenStep = Schema.Struct({
|
|
17
|
+
run: Schema.String,
|
|
18
|
+
workflow: Schema.String,
|
|
19
|
+
step: Schema.String,
|
|
20
|
+
task: Schema.Struct({ id: Schema.String, fields: Schema.Unknown, files: Schema.Array(File), notes: Schema.Array(TaskNote) }),
|
|
21
|
+
lastFingerprint: Schema.optionalKey(Schema.String),
|
|
22
|
+
})
|
|
23
|
+
export type TakenStep = typeof TakenStep.Type
|
|
24
|
+
const decodeTaken = Schema.decodeUnknownEffect(Schema.NullOr(TakenStep))
|
|
25
|
+
|
|
26
|
+
const Found = Schema.Array(
|
|
27
|
+
Schema.Struct({
|
|
28
|
+
taskId: Schema.String,
|
|
29
|
+
workflow: Schema.String,
|
|
30
|
+
fields: Schema.Unknown,
|
|
31
|
+
files: Schema.Array(File),
|
|
32
|
+
notes: Schema.Array(TaskNote),
|
|
33
|
+
at: Schema.Union([Schema.Struct({ line: Schema.String }), Schema.Struct({ step: Schema.String }), Schema.Struct({ end: Schema.String })]),
|
|
34
|
+
}),
|
|
35
|
+
)
|
|
36
|
+
const decodeFound = Schema.decodeUnknownEffect(Found)
|
|
37
|
+
|
|
38
|
+
/** A write to another task as a worker reports it: its files already kept in the organization's file store. */
|
|
39
|
+
export interface ReportedOtherWrite {
|
|
40
|
+
readonly workflow: string
|
|
41
|
+
readonly task: string
|
|
42
|
+
readonly fields?: Readonly<Record<string, unknown>>
|
|
43
|
+
readonly files?: ReadonlyArray<TaskFile>
|
|
44
|
+
readonly note: string
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CompleteInput {
|
|
48
|
+
readonly run: string
|
|
49
|
+
readonly workflow: string
|
|
50
|
+
readonly endState: string
|
|
51
|
+
readonly fields?: Record<string, unknown>
|
|
52
|
+
readonly files?: ReadonlyArray<TaskFile>
|
|
53
|
+
readonly measurements: StepMeasurements
|
|
54
|
+
readonly note?: string
|
|
55
|
+
/** From a step that syncs a declared item list: every item on it, as encoded task fields. */
|
|
56
|
+
readonly listed?: ReadonlyArray<unknown>
|
|
57
|
+
/** Writes to other tasks, applied with the run's own result. */
|
|
58
|
+
readonly others?: ReadonlyArray<ReportedOtherWrite>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** What a worker asks of the workflow platform in Convex: take a task, report on it, and read other tasks for its run. */
|
|
62
|
+
export interface IWorkflowPlatformClient {
|
|
63
|
+
take(input: { readonly worker: string; readonly steps: ReadonlyArray<{ readonly workflow: string; readonly step: string }>; readonly leaseMs: number }): Effect.fn.Return<TakenStep | null, PlatformCallFailed>
|
|
64
|
+
complete(input: CompleteInput): Effect.fn.Return<void, PlatformCallFailed>
|
|
65
|
+
fail(input: { readonly workflow: string; readonly run: string; readonly error: string; readonly measurements: StepMeasurements }): Effect.fn.Return<void, PlatformCallFailed>
|
|
66
|
+
heartbeat(input: { readonly workflow: string; readonly run: string; readonly leaseMs: number }): Effect.fn.Return<void, PlatformCallFailed>
|
|
67
|
+
/** Tasks of `in` that the run `run` of `workflow` asks for; Convex refuses a workflow its step may not read. */
|
|
68
|
+
findTasks(input: {
|
|
69
|
+
readonly workflow: string
|
|
70
|
+
readonly run: string
|
|
71
|
+
readonly in: string
|
|
72
|
+
readonly where?: Readonly<Record<string, unknown>>
|
|
73
|
+
readonly at?: TasksAt
|
|
74
|
+
readonly limit: number
|
|
75
|
+
}): Effect.fn.Return<ReadonlyArray<FoundTask>, PlatformCallFailed>
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export class WorkflowPlatformClient extends Context.Service<WorkflowPlatformClient, IWorkflowPlatformClient>()("WorkflowPlatformClient") {}
|
|
79
|
+
|
|
80
|
+
/** The workforce functions `workforceFunctions` defines, as the workforce app's generated `api` names them. */
|
|
81
|
+
export interface WorkerFunctions {
|
|
82
|
+
readonly take: FunctionReference<"mutation">
|
|
83
|
+
readonly complete: FunctionReference<"mutation">
|
|
84
|
+
readonly fail: FunctionReference<"mutation">
|
|
85
|
+
readonly heartbeat: FunctionReference<"mutation">
|
|
86
|
+
readonly findTasks: FunctionReference<"query">
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class ConvexWorkflowPlatformClient implements IWorkflowPlatformClient {
|
|
90
|
+
constructor(
|
|
91
|
+
private readonly client: ConvexHttpClient,
|
|
92
|
+
private readonly functions: WorkerFunctions,
|
|
93
|
+
) {}
|
|
94
|
+
|
|
95
|
+
private call(name: Exclude<keyof WorkerFunctions, "findTasks">, args: Record<string, unknown>) {
|
|
96
|
+
return Effect.tryPromise({
|
|
97
|
+
try: (): Promise<unknown> => this.client.mutation(this.functions[name], args),
|
|
98
|
+
catch: (cause) => new PlatformCallFailed({ call: name, cause }),
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
*take(input: { readonly worker: string; readonly steps: ReadonlyArray<{ readonly workflow: string; readonly step: string }>; readonly leaseMs: number }) {
|
|
103
|
+
const taken = yield* this.call("take", { ...input, steps: [...input.steps] })
|
|
104
|
+
return yield* Effect.mapError(decodeTaken(taken), (cause) => new PlatformCallFailed({ call: "take", cause }))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
*complete(input: CompleteInput) {
|
|
108
|
+
yield* this.call("complete", {
|
|
109
|
+
...input,
|
|
110
|
+
...(input.files !== undefined ? { files: [...input.files] } : {}),
|
|
111
|
+
...(input.listed !== undefined ? { listed: [...input.listed] } : {}),
|
|
112
|
+
...(input.others !== undefined ? { others: input.others.map((other) => ({ ...other, ...(other.files !== undefined ? { files: [...other.files] } : {}) })) } : {}),
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
*fail(input: { readonly workflow: string; readonly run: string; readonly error: string; readonly measurements: StepMeasurements }) {
|
|
117
|
+
yield* this.call("fail", input)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
*heartbeat(input: { readonly workflow: string; readonly run: string; readonly leaseMs: number }) {
|
|
121
|
+
yield* this.call("heartbeat", input)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
*findTasks(input: {
|
|
125
|
+
readonly workflow: string
|
|
126
|
+
readonly run: string
|
|
127
|
+
readonly in: string
|
|
128
|
+
readonly where?: Readonly<Record<string, unknown>>
|
|
129
|
+
readonly at?: TasksAt
|
|
130
|
+
readonly limit: number
|
|
131
|
+
}) {
|
|
132
|
+
const found = yield* Effect.tryPromise({
|
|
133
|
+
try: (): Promise<unknown> => this.client.query(this.functions.findTasks, { ...input }),
|
|
134
|
+
catch: (cause) => new PlatformCallFailed({ call: "findTasks", cause }),
|
|
135
|
+
})
|
|
136
|
+
return yield* Effect.mapError(decodeFound(found), (cause) => new PlatformCallFailed({ call: "findTasks", cause }))
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { Effect, Option } from "effect"
|
|
2
|
+
import type { AgentRunInput, AgentRunResult, IAgentRunner } from "../src/definition/agent-runner"
|
|
3
|
+
import type { IProgramBuild, ProgramBuildResult } from "../src/definition/program-build"
|
|
4
|
+
import type { TaskFile } from "../src/graph"
|
|
5
|
+
import { noSpend, type StepMeasurements, type StepSpend } from "../src/graph"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Measures one step run on this worker: the worker process's CPU and memory while it ran, and the spend of every agent
|
|
9
|
+
* run it made through the metered `AgentRunner`. Steps that start processes or compile report those themselves.
|
|
10
|
+
*/
|
|
11
|
+
export class StepMeter {
|
|
12
|
+
private spend: StepSpend = noSpend
|
|
13
|
+
private agent: string | undefined = undefined
|
|
14
|
+
private transcripts: Array<TaskFile> = []
|
|
15
|
+
private readonly cpuAtStart = process.cpuUsage()
|
|
16
|
+
private peakRssBytes = process.memoryUsage().rss
|
|
17
|
+
|
|
18
|
+
/** The organization's agent runner, with every run's spend and agent added to this meter. */
|
|
19
|
+
meteredAgentRunner(inner: IAgentRunner): IAgentRunner {
|
|
20
|
+
const meter = this
|
|
21
|
+
return {
|
|
22
|
+
*run(input: AgentRunInput) {
|
|
23
|
+
const result: AgentRunResult = yield* inner.run(input)
|
|
24
|
+
meter.addAgentRun(result)
|
|
25
|
+
return result
|
|
26
|
+
},
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The organization's program build, with every build's compiled functions added to this meter's compiles. */
|
|
31
|
+
meteredProgramBuild(inner: IProgramBuild): IProgramBuild {
|
|
32
|
+
const meter = this
|
|
33
|
+
return {
|
|
34
|
+
*build(input: { readonly changedFunctions: ReadonlyArray<string> }) {
|
|
35
|
+
const result: ProgramBuildResult = yield* inner.build(input)
|
|
36
|
+
meter.spend = { ...meter.spend, compiles: meter.spend.compiles + result.functions.compiled }
|
|
37
|
+
return result
|
|
38
|
+
},
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private addAgentRun(result: AgentRunResult) {
|
|
43
|
+
this.spend = {
|
|
44
|
+
fresh: this.spend.fresh + result.spend.fresh,
|
|
45
|
+
cacheWrite: this.spend.cacheWrite + result.spend.cacheWrite,
|
|
46
|
+
cacheRead: this.spend.cacheRead + result.spend.cacheRead,
|
|
47
|
+
output: this.spend.output + result.spend.output,
|
|
48
|
+
requests: this.spend.requests + result.spend.requests,
|
|
49
|
+
costUsd: this.spend.costUsd + result.spend.costUsd,
|
|
50
|
+
compiles: this.spend.compiles,
|
|
51
|
+
}
|
|
52
|
+
this.agent = result.agent
|
|
53
|
+
this.transcripts = [...this.transcripts, result.transcript]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The step's figures so far, with what the step itself reported about processes it started. */
|
|
57
|
+
measurements(input: {
|
|
58
|
+
readonly files: ReadonlyArray<string>
|
|
59
|
+
readonly reported: { readonly childCpuSeconds?: number; readonly compiles?: number }
|
|
60
|
+
readonly fingerprint: Option.Option<string>
|
|
61
|
+
}) {
|
|
62
|
+
return Effect.sync((): StepMeasurements => {
|
|
63
|
+
const cpu = process.cpuUsage(this.cpuAtStart)
|
|
64
|
+
this.peakRssBytes = Math.max(this.peakRssBytes, process.memoryUsage().rss)
|
|
65
|
+
return {
|
|
66
|
+
spend: { ...this.spend, compiles: this.spend.compiles + (input.reported.compiles ?? 0) },
|
|
67
|
+
used: {
|
|
68
|
+
ownCpuSeconds: (cpu.user + cpu.system) / 1_000_000,
|
|
69
|
+
childCpuSeconds: input.reported.childCpuSeconds ?? 0,
|
|
70
|
+
peakMemoryMb: this.peakRssBytes / (1024 * 1024),
|
|
71
|
+
filesWritten: input.files.length,
|
|
72
|
+
},
|
|
73
|
+
files: [...input.files],
|
|
74
|
+
transcripts: [...this.transcripts],
|
|
75
|
+
...(Option.isSome(input.fingerprint) ? { fingerprint: input.fingerprint.value } : {}),
|
|
76
|
+
...(this.agent !== undefined ? { agent: this.agent } : {}),
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Effect } from "effect"
|
|
2
|
+
import { type ITasks, type TasksAt, TasksCallFailed } from "../src/definition/tasks"
|
|
3
|
+
import type { IWorkflowPlatformClient, TakenStep } from "./platform-client"
|
|
4
|
+
|
|
5
|
+
/** The `Tasks` a worker gives one run: every read goes to Convex as that run's, which checks the step may make it. */
|
|
6
|
+
export class RunTasks implements ITasks {
|
|
7
|
+
constructor(
|
|
8
|
+
private readonly client: IWorkflowPlatformClient,
|
|
9
|
+
private readonly taken: TakenStep,
|
|
10
|
+
) {}
|
|
11
|
+
|
|
12
|
+
*find(input: { readonly workflow?: string; readonly where?: Readonly<Record<string, unknown>>; readonly at?: TasksAt; readonly limit: number }) {
|
|
13
|
+
return yield* Effect.mapError(
|
|
14
|
+
Effect.gen(() =>
|
|
15
|
+
this.client.findTasks({
|
|
16
|
+
workflow: this.taken.workflow,
|
|
17
|
+
run: this.taken.run,
|
|
18
|
+
in: input.workflow ?? this.taken.workflow,
|
|
19
|
+
limit: input.limit,
|
|
20
|
+
...(input.where !== undefined ? { where: input.where } : {}),
|
|
21
|
+
...(input.at !== undefined ? { at: input.at } : {}),
|
|
22
|
+
}),
|
|
23
|
+
),
|
|
24
|
+
(cause) => new TasksCallFailed({ cause }),
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test"
|
|
2
|
+
import { Effect, pipe, Schema } from "effect"
|
|
3
|
+
import { AgentRunFailed, AgentRunner } from "../src/definition/agent-runner"
|
|
4
|
+
import { FileStore, FileStoreError } from "../src/definition/file-store"
|
|
5
|
+
import * as TaskSchema from "../src/definition/task-schema"
|
|
6
|
+
import * as Workflow from "../src/definition/workflow"
|
|
7
|
+
import { problemsOf, type StepMeasurements, type TaskFile } from "../src/graph"
|
|
8
|
+
import { type IWorkflowPlatformClient, PlatformCallFailed, type TakenStep, WorkflowPlatformClient } from "./platform-client"
|
|
9
|
+
import { workOnce } from "./worker"
|
|
10
|
+
|
|
11
|
+
const counted = Workflow.make({
|
|
12
|
+
name: "counted",
|
|
13
|
+
task: TaskSchema.make({ fields: Schema.Struct({ n: Schema.Number }) }),
|
|
14
|
+
endStates: ["done"],
|
|
15
|
+
budgets: { agents: 0, cpu: 1 },
|
|
16
|
+
events: { arrive: { kind: "task" } },
|
|
17
|
+
steps: (step) => ({
|
|
18
|
+
count: step.code({
|
|
19
|
+
endStates: ["done"],
|
|
20
|
+
run: function* () {
|
|
21
|
+
return { endState: "done" as const }
|
|
22
|
+
},
|
|
23
|
+
}),
|
|
24
|
+
}),
|
|
25
|
+
lines: [
|
|
26
|
+
{ from: { event: "arrive" }, to: { step: "count" } },
|
|
27
|
+
{ from: { step: "count", endState: "done" }, to: { end: "done" } },
|
|
28
|
+
],
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
const taken: TakenStep = { run: "run-1", workflow: "counted", step: "count", task: { id: "task-1", fields: { n: 1 }, files: [], notes: [] } }
|
|
32
|
+
|
|
33
|
+
const timedOut = (call: string) => new PlatformCallFailed({ call, cause: "Function execution timed out" })
|
|
34
|
+
|
|
35
|
+
/** A platform that hands out one task and answers each report as `answers` says, recording every call. */
|
|
36
|
+
class FakePlatform implements IWorkflowPlatformClient {
|
|
37
|
+
readonly calls: Array<string> = []
|
|
38
|
+
readonly failures: Array<string> = []
|
|
39
|
+
constructor(private readonly answers: { readonly complete: boolean; readonly fail: boolean }) {}
|
|
40
|
+
*take() {
|
|
41
|
+
return taken
|
|
42
|
+
}
|
|
43
|
+
*complete() {
|
|
44
|
+
this.calls.push("complete")
|
|
45
|
+
if (!this.answers.complete) return yield* Effect.fail(timedOut("complete"))
|
|
46
|
+
}
|
|
47
|
+
*fail(input: { readonly error: string; readonly measurements: StepMeasurements }) {
|
|
48
|
+
this.calls.push("fail")
|
|
49
|
+
this.failures.push(input.error)
|
|
50
|
+
if (!this.answers.fail) return yield* Effect.fail(timedOut("fail"))
|
|
51
|
+
}
|
|
52
|
+
*heartbeat() {}
|
|
53
|
+
*findTasks() {
|
|
54
|
+
return []
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
class UnusedFileStore {
|
|
59
|
+
*write(input: { readonly name: string }): Effect.fn.Return<TaskFile, FileStoreError> {
|
|
60
|
+
return yield* Effect.fail(new FileStoreError({ file: input.name, cause: "unused" }))
|
|
61
|
+
}
|
|
62
|
+
*read(file: TaskFile): Effect.fn.Return<Uint8Array, FileStoreError> {
|
|
63
|
+
return yield* Effect.fail(new FileStoreError({ file: file.name, cause: "unused" }))
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const workWith = (platform: FakePlatform) =>
|
|
68
|
+
Effect.runPromise(
|
|
69
|
+
pipe(
|
|
70
|
+
Effect.gen(() => workOnce({ worker: "w", workflows: [counted], leaseMs: 60_000 })),
|
|
71
|
+
Effect.provideService(WorkflowPlatformClient, platform),
|
|
72
|
+
Effect.provideService(FileStore, new UnusedFileStore()),
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
describe("a worker reporting to a platform that times out", () => {
|
|
77
|
+
test("a complete that keeps timing out is tried 5 times, then reported as the run's failure", async () => {
|
|
78
|
+
const platform = new FakePlatform({ complete: false, fail: true })
|
|
79
|
+
const result = await workWith(platform)
|
|
80
|
+
expect(result).toEqual({ _tag: "Ran", taken, outcome: "failed" })
|
|
81
|
+
expect(platform.calls).toEqual(["complete", "complete", "complete", "complete", "complete", "fail"])
|
|
82
|
+
expect(platform.failures).toEqual(["its end state done could not be reported: complete failed: Function execution timed out"])
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test("when the failure cannot be reported either, the run is left to its lease and the worker carries on", async () => {
|
|
86
|
+
const platform = new FakePlatform({ complete: false, fail: false })
|
|
87
|
+
const result = await workWith(platform)
|
|
88
|
+
expect(result).toEqual({ _tag: "Ran", taken, outcome: "unreported" })
|
|
89
|
+
expect(platform.calls.filter((call) => call === "fail")).toHaveLength(5)
|
|
90
|
+
})
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
describe("what a step may use", () => {
|
|
94
|
+
/** A step that asks for the agent runner; whether it declared it decides whether it compiles and whether it gets it. */
|
|
95
|
+
const asksForAgents = function* () {
|
|
96
|
+
const agents = yield* AgentRunner
|
|
97
|
+
yield* agents.run({ prompt: "hello", files: [] })
|
|
98
|
+
return { endState: "done" as const }
|
|
99
|
+
}
|
|
100
|
+
const agentRunner = { *run() { return yield* Effect.fail(new AgentRunFailed({ cause: "no agents in this test" })) } }
|
|
101
|
+
|
|
102
|
+
test("a step's run may require only the services its access lists", () => {
|
|
103
|
+
const declared = Workflow.make({
|
|
104
|
+
name: "declared",
|
|
105
|
+
task: TaskSchema.make({ fields: Schema.Struct({}) }),
|
|
106
|
+
endStates: ["done"],
|
|
107
|
+
budgets: { agents: 1, cpu: 1 },
|
|
108
|
+
events: { arrive: { kind: "task" } },
|
|
109
|
+
steps: (step) => ({
|
|
110
|
+
think: step.agent({ endStates: ["done"], access: { services: [AgentRunner] }, run: asksForAgents }),
|
|
111
|
+
// @ts-expect-error the step lists no services, so its run may not use the agent runner
|
|
112
|
+
guess: step.agent({ endStates: ["done"], run: asksForAgents }),
|
|
113
|
+
// @ts-expect-error the step lists the file store, not the agent runner
|
|
114
|
+
write: step.code({ endStates: ["done"], access: { services: [FileStore] }, run: asksForAgents }),
|
|
115
|
+
}),
|
|
116
|
+
lines: [
|
|
117
|
+
{ from: { event: "arrive" }, to: { step: "think" } },
|
|
118
|
+
{ from: { step: "think", endState: "done" }, to: { step: "guess" } },
|
|
119
|
+
{ from: { step: "guess", endState: "done" }, to: { step: "write" } },
|
|
120
|
+
{ from: { step: "write", endState: "done" }, to: { end: "done" } },
|
|
121
|
+
],
|
|
122
|
+
})
|
|
123
|
+
expect(declared.graph.steps.map((step) => ({ step: step.id, access: step.access }))).toEqual([
|
|
124
|
+
{ step: "think", access: { services: ["AgentRunner"] } },
|
|
125
|
+
{ step: "guess", access: { services: [] } },
|
|
126
|
+
{ step: "write", access: { services: ["FileStore"] } },
|
|
127
|
+
])
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test("a worker given a step whose service it lacks refuses to start, naming the step and the service", async () => {
|
|
131
|
+
const thinking = Workflow.make({
|
|
132
|
+
name: "thinking",
|
|
133
|
+
task: TaskSchema.make({ fields: Schema.Struct({}) }),
|
|
134
|
+
endStates: ["done"],
|
|
135
|
+
budgets: { agents: 1, cpu: 0 },
|
|
136
|
+
events: { arrive: { kind: "task" } },
|
|
137
|
+
steps: (step) => ({ think: step.agent({ endStates: ["done"], access: { services: [AgentRunner] }, run: asksForAgents }) }),
|
|
138
|
+
lines: [
|
|
139
|
+
{ from: { event: "arrive" }, to: { step: "think" } },
|
|
140
|
+
{ from: { step: "think", endState: "done" }, to: { end: "done" } },
|
|
141
|
+
],
|
|
142
|
+
})
|
|
143
|
+
const platform = new FakePlatform({ complete: true, fail: true })
|
|
144
|
+
const exit = await Effect.runPromiseExit(
|
|
145
|
+
// @ts-expect-error the worker is not given the agent runner its workflow's step lists, which is what this test checks
|
|
146
|
+
pipe(
|
|
147
|
+
Effect.gen(() => workOnce({ worker: "w", workflows: [thinking], leaseMs: 60_000 })),
|
|
148
|
+
Effect.provideService(WorkflowPlatformClient, platform),
|
|
149
|
+
Effect.provideService(FileStore, new UnusedFileStore()),
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
expect(String(exit)).toContain("this worker cannot run step think of thinking: the step uses AgentRunner, which the worker was not given")
|
|
153
|
+
expect(platform.calls).toEqual([])
|
|
154
|
+
})
|
|
155
|
+
|
|
156
|
+
test("a run gets only the services its step lists, though the worker holds more", async () => {
|
|
157
|
+
// Written by hand, without the builder that checks a run against its access: the step lists nothing but asks for
|
|
158
|
+
// the agent runner, which the worker holds.
|
|
159
|
+
const handWritten = { ...counted, runnableSteps: { count: { kind: "code" as const, endStates: ["done"], access: { services: [] }, run: asksForAgents } } }
|
|
160
|
+
const platform = new FakePlatform({ complete: true, fail: true })
|
|
161
|
+
await Effect.runPromise(
|
|
162
|
+
pipe(
|
|
163
|
+
Effect.gen(() => workOnce({ worker: "w", workflows: [handWritten], leaseMs: 60_000 })),
|
|
164
|
+
Effect.provideService(WorkflowPlatformClient, platform),
|
|
165
|
+
Effect.provideService(FileStore, new UnusedFileStore()),
|
|
166
|
+
Effect.provideService(AgentRunner, agentRunner),
|
|
167
|
+
),
|
|
168
|
+
)
|
|
169
|
+
expect(platform.calls).toEqual(["fail"])
|
|
170
|
+
expect(platform.failures[0]).toContain("AgentRunner")
|
|
171
|
+
expect(platform.failures[0]).not.toContain("no agents in this test")
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
describe("timer turns", () => {
|
|
176
|
+
const ticking = (fields: TaskSchema.TaskSchema<any>) =>
|
|
177
|
+
Workflow.make({
|
|
178
|
+
name: "ticking",
|
|
179
|
+
task: fields,
|
|
180
|
+
endStates: ["done"],
|
|
181
|
+
budgets: { agents: 0, cpu: 1 },
|
|
182
|
+
events: { tick: { kind: "timer", everyMs: 1000 } },
|
|
183
|
+
steps: (step) => ({ count: step.code({ endStates: ["done"], run: function* () { return { endState: "done" as const } } }) }),
|
|
184
|
+
lines: [
|
|
185
|
+
{ from: { event: "tick" }, to: { step: "count" } },
|
|
186
|
+
{ from: { step: "count", endState: "done" }, to: { end: "done" } },
|
|
187
|
+
],
|
|
188
|
+
})
|
|
189
|
+
|
|
190
|
+
test("a timer's turn task is what the task schema decodes from {}", () => {
|
|
191
|
+
const withDefaults = ticking(TaskSchema.make({ fields: Schema.Struct({ n: Schema.Number.pipe(Schema.withDecodingDefaultKey(Effect.succeed(0))) }) }))
|
|
192
|
+
expect(withDefaults.graph.events).toEqual([{ id: "tick", kind: "timer", everyMs: 1000, fields: { n: 0 } }])
|
|
193
|
+
expect(problemsOf(withDefaults.graph)).toEqual([])
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test("a timer workflow whose task schema has a field with no default is refused, saying so", () => {
|
|
197
|
+
const withoutDefaults = ticking(TaskSchema.make({ fields: Schema.Struct({ n: Schema.Number }) }))
|
|
198
|
+
expect(problemsOf(withoutDefaults.graph).join("\n")).toContain("timer event tick cannot start a turn: its task schema cannot make a task from {}, so some field has no default")
|
|
199
|
+
})
|
|
200
|
+
})
|