@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/src/pick-wakes.ts
DELETED
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WHAT WAKES A PICK THAT FOUND NOTHING IT CAN CLAIM (`steps.ts` `untilPicked`), never a timer. A wake is any event that can
|
|
3
|
-
* change what a Pick can claim: a note on one of its workflow's tasks (new input, a reopen, a release, a hold, an outcome:
|
|
4
|
-
* `tasks.ts`), an item new to its list (`recordListing`), its limits set (which also has the run planner plan
|
|
5
|
-
* again: `runPlanChanged`), or one of its runs ending, which lets that run's
|
|
6
|
-
* claim go. A claim held outside its workflow's runs ending, or a lease running out, wakes nothing: the next of these events
|
|
7
|
-
* re-checks it. Before 2026-11-11 a Pick with nothing picked again each minute, and at budget 0 it ended its run at once:
|
|
8
|
-
* the first project's main workflow then ran Pick and Finish about once a second, 17,844 times in 6 hours.
|
|
9
|
-
*
|
|
10
|
-
* EVERY WAKE NAMES ITS SOURCE, and each minute the log says, per workflow, how many wakes came from where and how many
|
|
11
|
-
* re-picks they cost (`[pick wakes]`): a waiting Pick that keeps picking again shows the event doing it (2026-11-12: the
|
|
12
|
-
* job runner spent 84-90% of its thread in re-picks, and nothing said what woke them).
|
|
13
|
-
*
|
|
14
|
-
* Wakes reach the Picks of the process that raised them: an event in another process wakes nothing here.
|
|
15
|
-
*/
|
|
16
|
-
import { Effect } from 'effect';
|
|
17
|
-
|
|
18
|
-
interface Waiter {
|
|
19
|
-
readonly workflow: string;
|
|
20
|
-
readonly wake: () => void;
|
|
21
|
-
readonly release: () => void;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/** A wake as it travels to another process: one workflow's, or every workflow's when `workflow` is none. */
|
|
25
|
-
export interface Wake {
|
|
26
|
-
readonly workflow: string | null;
|
|
27
|
-
readonly source: string;
|
|
28
|
-
/** It changed how many runs the planner keeps (`runPlanChanged`). */
|
|
29
|
-
readonly runPlan?: true;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface PickWakes {
|
|
33
|
-
/** How many wakes have reached this workflow's Picks so far: a Pick reads it before it picks, so no wake is missed. */
|
|
34
|
-
readonly pickWakesSoFar: (workflow: string) => number;
|
|
35
|
-
/** Something one workflow's Picks can claim may have changed; `source` says what, for the minute's summary. */
|
|
36
|
-
readonly wakePicksOf: (workflow: string, source: string) => void;
|
|
37
|
-
/** Something every workflow's Picks can claim may have changed (every budget was set). */
|
|
38
|
-
readonly wakeEveryPick: (source: string) => void;
|
|
39
|
-
/**
|
|
40
|
-
* A workflow's limits were set (every workflow's, when `workflow` is none): its Picks are woken, and the run
|
|
41
|
-
* planner (`keep-running.ts`), told through `onRunPlanChanged` in the process it runs in, plans its runs again at once.
|
|
42
|
-
*/
|
|
43
|
-
readonly runPlanChanged: (r: { readonly workflow: string | null; readonly source: string }) => void;
|
|
44
|
-
/** Calls `listener` on each `runPlanChanged` that reaches this process, raised here or relayed. */
|
|
45
|
-
readonly onRunPlanChanged: (listener: (workflow: string | null) => void) => void;
|
|
46
|
-
/** Calls `listener` on each wake that reaches this process, raised here or relayed: what else follows a queue's changes (`queue-counts.ts`). */
|
|
47
|
-
readonly onWake: (listener: (wake: Wake) => void) => void;
|
|
48
|
-
/**
|
|
49
|
-
* Waits until a wake reaches this workflow after `after` wakes, at once when one already has: `woken`. `released`
|
|
50
|
-
* where its process is handing its runs to another (`releaseWaiting`), which a step answers by standing down.
|
|
51
|
-
*/
|
|
52
|
-
readonly untilPickWoken: (r: { readonly workflow: string; readonly after: number }) => Effect.Effect<'woken' | 'released'>;
|
|
53
|
-
/** A waiting Pick picked again after a wake: `ms` is how long that pick took. */
|
|
54
|
-
readonly countRePick: (workflow: string, ms: number) => void;
|
|
55
|
-
/**
|
|
56
|
-
* Sends every wake raised here to `send` too: for a process that changes what Picks can claim (a route setting a budget)
|
|
57
|
-
* while the Picks wait in another, which applies it with `wake`.
|
|
58
|
-
*/
|
|
59
|
-
readonly relayTo: (send: (wake: Wake) => void) => void;
|
|
60
|
-
/** Applies a wake another process relayed, without relaying it again. */
|
|
61
|
-
readonly wake: (wake: Wake) => void;
|
|
62
|
-
/**
|
|
63
|
-
* LETS EVERY WAITING STEP GO, now and from now on, for a process handing its runs to another: each wait ends `released`,
|
|
64
|
-
* and its step stands down (`steps.ts` `HANDED_OVER`), so its run ends untried and the next process starts one. Ended
|
|
65
|
-
* interrupted, the engine tried the step again 10 times and then failed its run: a run waiting for an agent was recorded
|
|
66
|
-
* as a failed try of its task (2026-11-19). A waiting step has done no work, and kept
|
|
67
|
-
* waiting it held its process's handover open for the engine's full limit, 10 minutes (2026-11-11).
|
|
68
|
-
*/
|
|
69
|
-
readonly releaseWaiting: () => void;
|
|
70
|
-
/** Whether this process is handing its runs to another (`releaseWaiting` was called): what a step that waits on work elsewhere asks. */
|
|
71
|
-
readonly handingOver: () => boolean;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const SUMMARY_MS = 60_000;
|
|
75
|
-
|
|
76
|
-
export const makePickWakes = (): PickWakes => {
|
|
77
|
-
const waiters = new Set<Waiter>();
|
|
78
|
-
const wakesOfWorkflow = new Map<string, number>();
|
|
79
|
-
let wakesOfEvery = 0;
|
|
80
|
-
/** This minute's wakes, by workflow and source, and the re-picks they cost. */
|
|
81
|
-
const wakesBySource = new Map<string, Map<string, number>>();
|
|
82
|
-
const rePicks = new Map<string, { count: number; ms: number }>();
|
|
83
|
-
|
|
84
|
-
const countWake = (workflow: string, source: string): void => {
|
|
85
|
-
const sources = wakesBySource.get(workflow) ?? new Map<string, number>();
|
|
86
|
-
sources.set(source, (sources.get(source) ?? 0) + 1);
|
|
87
|
-
wakesBySource.set(workflow, sources);
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
setInterval(() => {
|
|
91
|
-
for (const workflow of new Set([...wakesBySource.keys(), ...rePicks.keys()])) {
|
|
92
|
-
const sources = [...(wakesBySource.get(workflow) ?? new Map<string, number>())].map(([source, n]) => `${n} ${source}`).join(', ');
|
|
93
|
-
const picked = rePicks.get(workflow);
|
|
94
|
-
console.log(`[pick wakes] pid ${process.pid}, ${workflow} in the last minute: wakes ${sources === '' ? 'none' : sources}; ${picked === undefined ? 'no re-pick' : `${picked.count} re-picks taking ${Math.round(picked.ms)} ms`}`);
|
|
95
|
-
}
|
|
96
|
-
wakesBySource.clear();
|
|
97
|
-
rePicks.clear();
|
|
98
|
-
}, SUMMARY_MS).unref();
|
|
99
|
-
|
|
100
|
-
const pickWakesSoFar = (workflow: string): number => (wakesOfWorkflow.get(workflow) ?? 0) + wakesOfEvery;
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Wakes the waiting Picks after the code that called it has finished: a note written in a transaction is committed
|
|
104
|
-
* before any Pick reads the tasks again.
|
|
105
|
-
*/
|
|
106
|
-
const wake = (reaches: (waiter: Waiter) => boolean) => {
|
|
107
|
-
queueMicrotask(() => {
|
|
108
|
-
for (const waiter of [...waiters]) if (reaches(waiter)) waiter.wake();
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
let relay: ((wake: Wake) => void) | null = null;
|
|
113
|
-
const runPlanListeners = new Set<(workflow: string | null) => void>();
|
|
114
|
-
const wakeListeners = new Set<(wake: Wake) => void>();
|
|
115
|
-
let released = false;
|
|
116
|
-
|
|
117
|
-
const wakeHere = (w: Wake): void => {
|
|
118
|
-
for (const listener of wakeListeners) queueMicrotask(() => listener(w));
|
|
119
|
-
if (w.runPlan === true) for (const listener of runPlanListeners) queueMicrotask(() => listener(w.workflow));
|
|
120
|
-
if (w.workflow === null) {
|
|
121
|
-
wakesOfEvery += 1;
|
|
122
|
-
countWake('every workflow', w.source);
|
|
123
|
-
wake(() => true);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
const workflow = w.workflow;
|
|
127
|
-
wakesOfWorkflow.set(workflow, (wakesOfWorkflow.get(workflow) ?? 0) + 1);
|
|
128
|
-
countWake(workflow, w.source);
|
|
129
|
-
wake((waiter) => waiter.workflow === workflow);
|
|
130
|
-
};
|
|
131
|
-
|
|
132
|
-
const raise = (w: Wake): void => {
|
|
133
|
-
wakeHere(w);
|
|
134
|
-
if (relay !== null) relay(w);
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
pickWakesSoFar,
|
|
139
|
-
wakePicksOf: (workflow, source) => raise({ workflow, source }),
|
|
140
|
-
wakeEveryPick: (source) => raise({ workflow: null, source }),
|
|
141
|
-
runPlanChanged: ({ workflow, source }) => raise({ workflow, source, runPlan: true }),
|
|
142
|
-
onRunPlanChanged: (listener) => {
|
|
143
|
-
runPlanListeners.add(listener);
|
|
144
|
-
},
|
|
145
|
-
onWake: (listener) => {
|
|
146
|
-
wakeListeners.add(listener);
|
|
147
|
-
},
|
|
148
|
-
relayTo: (send) => {
|
|
149
|
-
relay = send;
|
|
150
|
-
},
|
|
151
|
-
wake: wakeHere,
|
|
152
|
-
handingOver: () => released,
|
|
153
|
-
releaseWaiting: () => {
|
|
154
|
-
released = true;
|
|
155
|
-
for (const waiter of [...waiters]) waiter.release();
|
|
156
|
-
},
|
|
157
|
-
untilPickWoken: (input) =>
|
|
158
|
-
Effect.callback<'woken' | 'released'>((resume) => {
|
|
159
|
-
if (released) return resume(Effect.succeed('released'));
|
|
160
|
-
if (pickWakesSoFar(input.workflow) !== input.after) return resume(Effect.succeed('woken'));
|
|
161
|
-
const waiter: Waiter = {
|
|
162
|
-
workflow: input.workflow,
|
|
163
|
-
wake: () => {
|
|
164
|
-
waiters.delete(waiter);
|
|
165
|
-
resume(Effect.succeed('woken'));
|
|
166
|
-
},
|
|
167
|
-
release: () => {
|
|
168
|
-
waiters.delete(waiter);
|
|
169
|
-
resume(Effect.succeed('released'));
|
|
170
|
-
},
|
|
171
|
-
};
|
|
172
|
-
waiters.add(waiter);
|
|
173
|
-
return Effect.sync(() => {
|
|
174
|
-
waiters.delete(waiter);
|
|
175
|
-
});
|
|
176
|
-
}),
|
|
177
|
-
countRePick: (workflow, ms) => {
|
|
178
|
-
const was = rePicks.get(workflow) ?? { count: 0, ms: 0 };
|
|
179
|
-
rePicks.set(workflow, { count: was.count + 1, ms: was.ms + ms });
|
|
180
|
-
},
|
|
181
|
-
};
|
|
182
|
-
};
|
package/src/project.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WHAT A PROJECT TELLS THE PLATFORM ABOUT ITSELF: the facts no generic code can know. Everything else (how a step is
|
|
3
|
-
* measured, how a task is queued, when a waiting Pick wakes) is the platform's.
|
|
4
|
-
*/
|
|
5
|
-
import type { Effect } from 'effect';
|
|
6
|
-
import type { StorageError } from './storage/storage-error';
|
|
7
|
-
|
|
8
|
-
/** A process, by its pid and the second it started (as `ps` reports it), so a reused pid is never taken for it. */
|
|
9
|
-
export interface ProcessIdentity {
|
|
10
|
-
readonly pid: number;
|
|
11
|
-
readonly startedAt: number;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/** What one process a step started used, as the project's process starter measures it when the process exits. */
|
|
15
|
-
export interface ProcessUsage {
|
|
16
|
-
readonly label: string;
|
|
17
|
-
readonly cpuSeconds: number;
|
|
18
|
-
readonly peakBytes: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** Where a process's usage goes: the step it ran for. `record` also tells a child process which step record it works for. */
|
|
22
|
-
export interface UsageSink {
|
|
23
|
-
readonly record: number | null;
|
|
24
|
-
readonly add: (usage: ProcessUsage) => void;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** A short command run at once, outside any CPU limit's queue (`ps`, `diff3`). */
|
|
28
|
-
export type RunLightNow = (
|
|
29
|
-
command: ReadonlyArray<string>,
|
|
30
|
-
options?: { readonly env?: Record<string, string> },
|
|
31
|
-
) => { readonly code: number; readonly stdout: string; readonly stdoutBytes: Uint8Array; readonly stderr: string };
|
|
32
|
-
|
|
33
|
-
/** Texts stored by their content hash: a task note's change and new input name theirs. */
|
|
34
|
-
export interface TextStore {
|
|
35
|
-
/** Stores `text` and returns its hash; storing it again changes nothing. */
|
|
36
|
-
readonly write: (text: string) => string;
|
|
37
|
-
/** The text stored under `sha`, checked against it; throws when there is none. */
|
|
38
|
-
readonly read: (sha: string) => string;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/** The project's working tree, as a step's changed files are read from it (`step-files.ts`). */
|
|
42
|
-
export interface ProjectTree {
|
|
43
|
-
/** The folder every path is relative to: the checkout. */
|
|
44
|
-
readonly root: string;
|
|
45
|
-
/** Where step snapshots of the tree are kept. */
|
|
46
|
-
readonly snapshots: string;
|
|
47
|
-
/** Paths, from `root`, that are the project's own output rather than anyone's edit. */
|
|
48
|
-
readonly notTree: ReadonlyArray<string>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* THE ONE CPU LIMIT FOR EVERYTHING THE PROJECT STARTS: its workflows' processes, its own long-running processes and its
|
|
53
|
-
* agents' commands. The project runs it (a process starter that holds a slot from start to exit, admitting new work only
|
|
54
|
-
* while what the project uses, as measured, leaves room); the platform reads it, to say where the project stands.
|
|
55
|
-
*/
|
|
56
|
-
export interface CpuLimit {
|
|
57
|
-
/** The most cores the project may use at once. */
|
|
58
|
-
readonly limit: () => number;
|
|
59
|
-
/** The newest reading of what the project used, in cores, and how many slots were held and waiting; none before the first. */
|
|
60
|
-
readonly newestUse: () => { readonly at: number; readonly cores: number; readonly held: number; readonly waiting: number } | null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/** One good reading of the week the project's agents spend from, as its account reports it. */
|
|
64
|
-
export interface WeekUsageReading {
|
|
65
|
-
/** How much of the week is used, in percent. */
|
|
66
|
-
readonly usedPercent: number;
|
|
67
|
-
/** When it was read, unix seconds. */
|
|
68
|
-
readonly readAt: number;
|
|
69
|
-
/** When the week starts again, unix seconds; none where the account does not say. */
|
|
70
|
-
readonly resetsAt: number | null;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/** What the project knows of its week now: its newest good reading, and a read that failed after it. */
|
|
74
|
-
export interface WeekUsageNow {
|
|
75
|
-
/** The newest reading that succeeded, however old; none before the first. */
|
|
76
|
-
readonly good: WeekUsageReading | null;
|
|
77
|
-
/** The newest read, where it failed after that one: when, and why in words. */
|
|
78
|
-
readonly failedSince: { readonly at: number; readonly error: string } | null;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* THE WEEK THE PROJECT'S AGENTS SPEND FROM, as the project reads it: the one figure the spending limit judges
|
|
83
|
-
* (`spending-limit.ts`). The project reads it however it reads its account; the platform never asks the account itself.
|
|
84
|
-
*/
|
|
85
|
-
export interface WeekUsage {
|
|
86
|
-
readonly now: () => Effect.Effect<WeekUsageNow, StorageError>;
|
|
87
|
-
/** How old a good reading may be before a page calls it stale: the project's time between readings, and its margin. */
|
|
88
|
-
readonly staleAfterSeconds: number;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export interface WorkflowProject {
|
|
92
|
-
/** The folder a workflow's or step's source link is relative to. */
|
|
93
|
-
readonly sourceRoot: string;
|
|
94
|
-
readonly tree: ProjectTree;
|
|
95
|
-
readonly texts: TextStore;
|
|
96
|
-
readonly runLightNow: RunLightNow;
|
|
97
|
-
/** This process, and whether another is still alive. */
|
|
98
|
-
readonly processes: {
|
|
99
|
-
readonly thisProcess: () => ProcessIdentity;
|
|
100
|
-
readonly isAlive: (process: ProcessIdentity) => boolean;
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* The variable that tells a process a step started which step record it works for, so its compiles reach that record
|
|
104
|
-
* (`step-usage.ts`).
|
|
105
|
-
*/
|
|
106
|
-
readonly stepRecordVariable: string;
|
|
107
|
-
/** A failure that says nothing about the work and is waited out: a step retries on it (a locked database). */
|
|
108
|
-
readonly isTransientFailure: (failure: unknown) => boolean;
|
|
109
|
-
readonly cpu: CpuLimit;
|
|
110
|
-
/**
|
|
111
|
-
* The week its agents spend from, where it reads one: the spending limit holds its agent steps by it
|
|
112
|
-
* (`spending-limit.ts`). A project that gives none has no spending limit.
|
|
113
|
-
*/
|
|
114
|
-
readonly weekUsage?: WeekUsage;
|
|
115
|
-
}
|
package/src/queue-counts.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* EACH QUEUE'S COUNTS, RECORDED WHEN ITS QUEUE MAY HAVE CHANGED (`queue_counts`), so a page reads the newest row and its
|
|
3
|
-
* history and never counts. A workflow's tasks by status are read from its tasks' notes (`tasks.ts` `taskSummaries`), once
|
|
4
|
-
* per change, in the one process that runs the project's workflows. A count only counts: blocking a task is its Pick's
|
|
5
|
-
* step, never a page's or this (`tasks.ts` `settleQueue`).
|
|
6
|
-
*
|
|
7
|
-
* WHAT COUNTS AS A CHANGE: every wake of a workflow's Picks (`pick-wakes.ts`: a note on one of its tasks, an item new to
|
|
8
|
-
* its list, one of its runs ending, a limit set), and a Pick claiming an item (`changed`). Every change within
|
|
9
|
-
* `COUNT_AFTER_MS` of the first is one count, taken at its end, so 25 short runs ending a minute cost 12 counts and each
|
|
10
|
-
* change shows within 5 seconds. A row is written only where a count moved.
|
|
11
|
-
*/
|
|
12
|
-
import { Effect } from 'effect';
|
|
13
|
-
import type { PickWakes } from './pick-wakes';
|
|
14
|
-
import type { StorageError } from './storage/storage-error';
|
|
15
|
-
import type { TaskStore } from './storage/task-store';
|
|
16
|
-
import { statusCounts, type Tasks } from './tasks';
|
|
17
|
-
|
|
18
|
-
/** How long after a queue's first change it is counted: every change in that time is in the one count. */
|
|
19
|
-
export const COUNT_AFTER_MS = 5_000;
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* The count recorded beside the four statuses: how many queued tasks are marked as needing an agent (`tasks.ts`
|
|
23
|
-
* `TaskSummary.needsAgent`). Part of `queued`, never an addend of its own: a page shows it beside that row.
|
|
24
|
-
*/
|
|
25
|
-
export const QUEUED_NEEDING_AN_AGENT = 'queued, needing an agent';
|
|
26
|
-
|
|
27
|
-
export const makeQueueCounts = (deps: {
|
|
28
|
-
readonly store: Pick<TaskStore, 'recordQueueCounts'>;
|
|
29
|
-
readonly tasks: Pick<Tasks, 'taskSummaries'>;
|
|
30
|
-
readonly wakes: Pick<PickWakes, 'onWake'>;
|
|
31
|
-
/** The workflows that have a queue, now. */
|
|
32
|
-
readonly withQueues: () => ReadonlyArray<string>;
|
|
33
|
-
/** `COUNT_AFTER_MS` where absent; a test names a shorter one. */
|
|
34
|
-
readonly countAfterMs?: number;
|
|
35
|
-
}) => {
|
|
36
|
-
/** Counts one workflow's queue now and records it. */
|
|
37
|
-
const count = (workflow: string): Effect.Effect<void, StorageError> => Effect.gen(function* () {
|
|
38
|
-
const summaries = yield* deps.tasks.taskSummaries({ workflow });
|
|
39
|
-
const counts = { ...statusCounts(summaries), [QUEUED_NEEDING_AN_AGENT]: summaries.filter((t) => t.status === 'queued' && t.needsAgent !== null).length };
|
|
40
|
-
yield* deps.store.recordQueueCounts({ at: Date.now() / 1000, counts: Object.entries(counts).map(([bucket, tasks]) => ({ workflow, bucket, tasks })) });
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
let countSoon: (workflow: string) => void = () => undefined;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Starts recording: for the one process that runs the project's workflows, which gives `run`, how it runs an effect of
|
|
47
|
-
* theirs. Every queue is counted once at the start, then each on its changes.
|
|
48
|
-
*/
|
|
49
|
-
const record = (run: (count: Effect.Effect<void, StorageError>) => Promise<void>): void => {
|
|
50
|
-
const due = new Set<string>();
|
|
51
|
-
countSoon = (workflow) => {
|
|
52
|
-
if (due.has(workflow) || !deps.withQueues().includes(workflow)) return;
|
|
53
|
-
due.add(workflow);
|
|
54
|
-
setTimeout(() => {
|
|
55
|
-
due.delete(workflow);
|
|
56
|
-
void run(count(workflow)).catch((e: unknown) => console.error(`[queue counts] ${workflow}: not counted: ${e instanceof Error ? e.message : String(e)}`));
|
|
57
|
-
}, deps.countAfterMs ?? COUNT_AFTER_MS).unref?.();
|
|
58
|
-
};
|
|
59
|
-
deps.wakes.onWake((wake) => { for (const workflow of wake.workflow === null ? deps.withQueues() : [wake.workflow]) countSoon(workflow); });
|
|
60
|
-
for (const workflow of deps.withQueues()) countSoon(workflow);
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
return { count, record, changed: (workflow: string): void => countSoon(workflow) };
|
|
64
|
-
};
|
package/src/reasons.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* WHY A STEP ENDED AS IT DID, IN A LINE, on every step record: what outcomes are counted by, per workflow and step. A step
|
|
3
|
-
* can say how its result gives its reason (`defineStep`'s `reason`); otherwise `standardReason` reads it from the result's
|
|
4
|
-
* own words. A failure's reason is its error's first line, and an interruption's is "interrupted". Dependency-free, so a
|
|
5
|
-
* page can import it.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
/** The most a reason keeps, so reasons compare and count. */
|
|
9
|
-
export const REASON_CHARACTERS = 160;
|
|
10
|
-
|
|
11
|
-
/** A text as one comparable line: its first line, cut short. Pure. */
|
|
12
|
-
export const oneLine = (text: string): string => {
|
|
13
|
-
const first = text.trim().split('\n')[0]!.trim();
|
|
14
|
-
return first.length <= REASON_CHARACTERS ? first : `${first.slice(0, REASON_CHARACTERS - 1)}…`;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* A result's reason, read from its own words: a text is its own reason; an object gives its `reason`, `why` or `outcome`,
|
|
19
|
-
* the first that is a text; an empty `Option` or none found nothing; anything else succeeded with no words of its own. Pure.
|
|
20
|
-
*/
|
|
21
|
-
export const standardReason = (result: unknown): string => {
|
|
22
|
-
if (typeof result === 'string') return oneLine(result);
|
|
23
|
-
if (result === null || result === undefined) return 'found nothing';
|
|
24
|
-
if (typeof result === 'object') {
|
|
25
|
-
const r = result as { readonly reason?: unknown; readonly why?: unknown; readonly outcome?: unknown; readonly _tag?: unknown };
|
|
26
|
-
for (const words of [r.reason, r.why, r.outcome]) if (typeof words === 'string' && words.trim() !== '') return oneLine(words);
|
|
27
|
-
if (r._tag === 'None') return 'found nothing';
|
|
28
|
-
}
|
|
29
|
-
return 'succeeded';
|
|
30
|
-
};
|
package/src/spending-limit.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* THE SPENDING LIMIT: ONE SETTING FOR THE WHOLE ACCOUNT, "AGENTS STOP AT X% OF THE WEEK". Alex, 2026-11-20: "ideally the
|
|
3
|
-
* workflow platform offers a configurable spending limit", and, of this design, "yes to building the spending limit".
|
|
4
|
-
*
|
|
5
|
-
* While the project's newest good reading of its week (`WorkflowProject.weekUsage`) is at or past X, every agent step of
|
|
6
|
-
* every workflow waits before its agent call, as it waits at an agents' limit of 0 (`steps.ts` `untilAgentSlot`), its record
|
|
7
|
-
* saying why in words (`spendingWait`). An agent call already going is never cut off. The steps go on by themselves once a
|
|
8
|
-
* reading is below X, which the week's reset brings: the project tells the platform of each reading it takes
|
|
9
|
-
* (`readingTaken`), and a verdict that changed wakes every waiting step. None is set: X is 95 (`DEFAULT_SPENDING_LIMIT_PERCENT`).
|
|
10
|
-
*
|
|
11
|
-
* IT JUDGES THE ACCOUNT'S WEEK, NOT A COUNT OF TOKENS, because the week is the one figure that also counts spending the
|
|
12
|
-
* platform cannot see: agent threads run by hand, outside any workflow, which no step record holds. They were 73% of the
|
|
13
|
-
* spending of the week that ran out on 2026-11-01, 57 hours before its reset (the first project's `usage/pace.ts`).
|
|
14
|
-
*
|
|
15
|
-
* A READING THAT FAILS OR GROWS OLD NEVER LIFTS IT. The rule judges the newest good reading, however old, so a hold stands
|
|
16
|
-
* until a good reading below X, and a page says the reading is stale and why. That holds past the reset the reading named
|
|
17
|
-
* too: with every read failing, agents wait until one succeeds, rather than spend on a week nobody can see. A reading that
|
|
18
|
-
* cannot be read at all holds every agent step, as limits that cannot be read hold their runs. With no good reading yet
|
|
19
|
-
* there is nothing to judge, and nothing is held. A project that reads no week has no spending limit.
|
|
20
|
-
*/
|
|
21
|
-
import { Effect, pipe, Result } from 'effect';
|
|
22
|
-
import type { PickWakes } from './pick-wakes';
|
|
23
|
-
import type { WeekUsage, WeekUsageNow, WeekUsageReading } from './project';
|
|
24
|
-
import { SET_SPENDING_LIMIT_SQL, type LimitStore, type StoredSpendingLimit } from './storage/budgets';
|
|
25
|
-
import type { StorageError } from './storage/storage-error';
|
|
26
|
-
import { percentCut } from './views/format';
|
|
27
|
-
|
|
28
|
-
/** The percent of the week at which agents stop where none is set. */
|
|
29
|
-
export const DEFAULT_SPENDING_LIMIT_PERCENT = 95;
|
|
30
|
-
|
|
31
|
-
/** The spending limit against the newest reading of the week, for a page and for the rule. */
|
|
32
|
-
export interface SpendingLimitState {
|
|
33
|
-
/** The percent of the week at which agents stop. */
|
|
34
|
-
readonly limitPercent: number;
|
|
35
|
-
/** When it was set, unix seconds; none while it is the default. */
|
|
36
|
-
readonly setAt: number | null;
|
|
37
|
-
/** The newest good reading, which the verdict rests on; none before the first. */
|
|
38
|
-
readonly reading: WeekUsageReading | null;
|
|
39
|
-
/** The newest read, where it failed after that one. */
|
|
40
|
-
readonly failedSince: WeekUsageNow['failedSince'];
|
|
41
|
-
/** Whether the reading is stale: a read failed since, or it is older than the project's time between readings allows. */
|
|
42
|
-
readonly stale: boolean;
|
|
43
|
-
/** Whether every agent step waits now. */
|
|
44
|
-
readonly holds: boolean;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/** What every agent step's record says while the limit holds it. */
|
|
48
|
-
export const spendingWait = (r: { readonly usedPercent: number; readonly limitPercent: number }): string =>
|
|
49
|
-
`waits: the week's use is ${percentCut(r.usedPercent)}, at or past the spending limit of ${r.limitPercent}%`;
|
|
50
|
-
|
|
51
|
-
/** How every wait the spending limit gives starts, its unreadable one included. */
|
|
52
|
-
const SPENDING_WAIT_START = "waits: the week's use ";
|
|
53
|
-
|
|
54
|
-
/** Whether what a step waits on is the spending limit: what a page counts its waiting steps by. Pure. */
|
|
55
|
-
export const isSpendingLimitWait = (why: string): boolean => why.startsWith(SPENDING_WAIT_START);
|
|
56
|
-
|
|
57
|
-
/** The limit against the week now. Pure. */
|
|
58
|
-
export const judgeSpending = (input: {
|
|
59
|
-
readonly limit: StoredSpendingLimit | null;
|
|
60
|
-
readonly usage: WeekUsageNow;
|
|
61
|
-
readonly staleAfterSeconds: number;
|
|
62
|
-
readonly now: number;
|
|
63
|
-
}): SpendingLimitState => {
|
|
64
|
-
const limitPercent = input.limit === null ? DEFAULT_SPENDING_LIMIT_PERCENT : input.limit.weekPercent;
|
|
65
|
-
const good = input.usage.good;
|
|
66
|
-
return {
|
|
67
|
-
limitPercent,
|
|
68
|
-
setAt: input.limit === null ? null : input.limit.changedAt,
|
|
69
|
-
reading: good,
|
|
70
|
-
failedSince: input.usage.failedSince,
|
|
71
|
-
stale: good !== null && (input.usage.failedSince !== null || input.now - good.readAt > input.staleAfterSeconds),
|
|
72
|
-
holds: good !== null && good.usedPercent >= limitPercent,
|
|
73
|
-
};
|
|
74
|
-
};
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* What the limit does to an agent step now. `why` is what its record says (`spendingWait`); `unavailable`, in words, is why
|
|
78
|
-
* no agent can be had until a reading or the limit changes, the reason a run with a task ends and marks it as needing an
|
|
79
|
-
* agent (`steps.ts` `untilAgentSlot`). An unreadable week has none: the step waits, since the next read may well succeed.
|
|
80
|
-
*/
|
|
81
|
-
export interface SpendingHold {
|
|
82
|
-
readonly why: string;
|
|
83
|
-
readonly unavailable: string | null;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export interface SpendingLimit {
|
|
87
|
-
/** What the limit does to an agent step now; none while it holds nothing. */
|
|
88
|
-
readonly holdsAgents: () => Effect.Effect<SpendingHold | null>;
|
|
89
|
-
/** The limit against the newest reading, for a page; none for a project that reads no week. */
|
|
90
|
-
readonly state: (now: number) => Effect.Effect<SpendingLimitState | null, StorageError>;
|
|
91
|
-
/**
|
|
92
|
-
* The project took a reading of its week. Where the verdict changed since the last one, or a step found the week
|
|
93
|
-
* unreadable since, every waiting step is woken to judge it again: a reading below the limit lets them go.
|
|
94
|
-
*/
|
|
95
|
-
readonly readingTaken: () => Effect.Effect<void>;
|
|
96
|
-
/**
|
|
97
|
-
* THE ONE WAY TO SET IT, for the project's route: checks the percent asked, stores it with the platform's statement
|
|
98
|
-
* through the project's `write`, wakes every waiting step (relayed to the process its runs wait in), and says in the log
|
|
99
|
-
* who asked (`from`), as the routes that set a workflow's limits do. A refused one changes nothing.
|
|
100
|
-
*/
|
|
101
|
-
readonly set: <E>(r: {
|
|
102
|
-
readonly weekPercent: unknown;
|
|
103
|
-
readonly from: string;
|
|
104
|
-
readonly write: (statement: { readonly sql: string; readonly params: ReadonlyArray<number> }) => Effect.Effect<unknown, E>;
|
|
105
|
-
}) => Effect.Effect<{ readonly ok: true; readonly weekPercent: number } | { readonly ok: false; readonly error: string }, E>;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export const makeSpendingLimit = (deps: { readonly store: LimitStore; readonly usage: WeekUsage | null; readonly wakes: PickWakes }): SpendingLimit => {
|
|
109
|
-
const { usage } = deps;
|
|
110
|
-
const state = (now: number): Effect.Effect<SpendingLimitState | null, StorageError> =>
|
|
111
|
-
usage === null ? Effect.succeed(null) : Effect.map(
|
|
112
|
-
Effect.all({ limit: deps.store.spendingLimit(), week: usage.now() }),
|
|
113
|
-
({ limit, week }) => judgeSpending({ limit, usage: week, staleAfterSeconds: usage.staleAfterSeconds, now }),
|
|
114
|
-
);
|
|
115
|
-
/** What the last reading told held, in this process; none before the first. */
|
|
116
|
-
let heldAtLastReading: boolean | null = null;
|
|
117
|
-
/** A step found the week unreadable since the last reading, so it waits for a wake that a changed verdict alone may not bring. */
|
|
118
|
-
let unreadSinceLastReading = false;
|
|
119
|
-
return {
|
|
120
|
-
holdsAgents: () => pipe(
|
|
121
|
-
state(Date.now() / 1000),
|
|
122
|
-
Effect.map((s): SpendingHold | null => (s === null || !s.holds || s.reading === null ? null : {
|
|
123
|
-
why: spendingWait({ usedPercent: s.reading.usedPercent, limitPercent: s.limitPercent }),
|
|
124
|
-
unavailable: `the week's use is ${percentCut(s.reading.usedPercent)}, at or past the spending limit of ${s.limitPercent}%`,
|
|
125
|
-
})),
|
|
126
|
-
Effect.catchTag('StorageError', (e) => Effect.sync((): SpendingHold => {
|
|
127
|
-
unreadSinceLastReading = true;
|
|
128
|
-
return { why: `waits: the week's use could not be read (${e.reason}), so the spending limit holds every agent step`, unavailable: null };
|
|
129
|
-
})),
|
|
130
|
-
),
|
|
131
|
-
state,
|
|
132
|
-
readingTaken: () => Effect.gen(function* () {
|
|
133
|
-
const judged = yield* Effect.result(state(Date.now() / 1000));
|
|
134
|
-
if (Result.isFailure(judged)) return yield* Effect.logError(`[spending limit] a reading was taken and could not be judged: ${judged.failure.reason}`);
|
|
135
|
-
const holds = judged.success !== null && judged.success.holds;
|
|
136
|
-
if (holds === heldAtLastReading && !unreadSinceLastReading) return;
|
|
137
|
-
if (holds !== heldAtLastReading && judged.success !== null) {
|
|
138
|
-
yield* Effect.log(`[spending limit] ${holds ? 'holds every agent step' : 'holds nothing'}: the week's use is ${judged.success.reading === null ? 'not read yet' : percentCut(judged.success.reading.usedPercent)}, the limit ${judged.success.limitPercent}%`);
|
|
139
|
-
}
|
|
140
|
-
heldAtLastReading = holds;
|
|
141
|
-
unreadSinceLastReading = false;
|
|
142
|
-
// It changes whether any run can take work (`steps.ts` `whyNoRunCanTakeWork`): the run planner plans again too.
|
|
143
|
-
deps.wakes.runPlanChanged({ workflow: null, source: holds ? 'a reading at or past the spending limit' : 'a reading below the spending limit' });
|
|
144
|
-
}),
|
|
145
|
-
set: (r) => Effect.gen(function* () {
|
|
146
|
-
const weekPercent = r.weekPercent;
|
|
147
|
-
if (typeof weekPercent !== 'number' || !Number.isInteger(weekPercent) || weekPercent < 0 || weekPercent > 100) {
|
|
148
|
-
return { ok: false as const, error: 'weekPercent must be a whole number from 0 to 100' };
|
|
149
|
-
}
|
|
150
|
-
yield* r.write({ sql: SET_SPENDING_LIMIT_SQL, params: [weekPercent, Date.now() / 1000] });
|
|
151
|
-
deps.wakes.runPlanChanged({ workflow: null, source: 'the spending limit set' });
|
|
152
|
-
yield* Effect.log(`[spending limit] ${weekPercent}%, from ${r.from}`);
|
|
153
|
-
return { ok: true as const, weekPercent };
|
|
154
|
-
}),
|
|
155
|
-
};
|
|
156
|
-
};
|