@jive-sh/workflow-platform 0.7.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +70 -435
- package/component/_generated/api.ts +72 -0
- package/component/_generated/component.ts +629 -0
- package/component/_generated/dataModel.ts +60 -0
- package/component/_generated/server.ts +169 -0
- package/component/convex.config.ts +3 -0
- package/component/engine.ts +756 -0
- package/component/errors.ts +66 -0
- package/component/events.ts +17 -0
- package/component/notes.ts +19 -0
- package/component/places.ts +136 -0
- package/component/retention.ts +205 -0
- package/component/runEngine.ts +25 -0
- package/component/schema.ts +135 -0
- package/component/steps.ts +188 -0
- package/component/store.ts +513 -0
- package/component/tasks.ts +290 -0
- package/component/worker.ts +109 -0
- package/component/workflows.ts +83 -0
- package/dist/definition/agent-runner.js +9 -0
- package/dist/definition/agent-runner.js.map +1 -0
- package/dist/definition/file-store.js +9 -0
- package/dist/definition/file-store.js.map +1 -0
- package/dist/definition/program-build.js +10 -0
- package/dist/definition/program-build.js.map +1 -0
- package/dist/definition/step.js +1 -0
- package/dist/definition/step.js.map +1 -0
- package/dist/definition/task-schema.js +5 -0
- package/dist/definition/task-schema.js.map +1 -0
- package/dist/definition/tasks.js +9 -0
- package/dist/definition/tasks.js.map +1 -0
- package/dist/definition/workflow.js +117 -0
- package/dist/definition/workflow.js.map +1 -0
- package/dist/graph.js +310 -9
- package/dist/graph.js.map +1 -1
- package/dist/index.js +7 -196
- package/dist/index.js.map +1 -1
- package/package.json +9 -4
- package/src/definition/agent-runner.ts +59 -0
- package/src/definition/file-store.ts +20 -0
- package/src/definition/program-build.ts +30 -0
- package/src/definition/step.ts +106 -0
- package/src/definition/task-schema.ts +15 -0
- package/src/definition/tasks.ts +43 -0
- package/src/definition/workflow.ts +262 -0
- package/src/graph.ts +352 -19
- package/src/index.ts +8 -238
- package/worker/directory-file-store.ts +42 -0
- package/worker/index.ts +12 -0
- package/worker/platform-client.ts +144 -0
- package/worker/step-meter.ts +80 -0
- package/worker/worker-tasks.ts +27 -0
- package/worker/worker.test.ts +200 -0
- package/worker/worker.ts +240 -0
- package/dist/budget-states.js +0 -30
- package/dist/budget-states.js.map +0 -1
- package/dist/cpu-limit/limiter.js +0 -839
- package/dist/cpu-limit/limiter.js.map +0 -1
- package/dist/cpu-limit/use.js +0 -139
- package/dist/cpu-limit/use.js.map +0 -1
- package/dist/dashboard/agent-usage.js +0 -25
- package/dist/dashboard/agent-usage.js.map +0 -1
- package/dist/dashboard/dashboard.css +0 -148
- package/dist/dashboard/drilldown.js +0 -47
- package/dist/dashboard/drilldown.js.map +0 -1
- package/dist/dashboard/queue-lines.js +0 -44
- package/dist/dashboard/queue-lines.js.map +0 -1
- package/dist/dashboard/queue-table.js +0 -16
- package/dist/dashboard/queue-table.js.map +0 -1
- package/dist/dashboard/run-doing-words.js +0 -29
- package/dist/dashboard/run-doing-words.js.map +0 -1
- package/dist/dashboard/setting-words.js +0 -18
- package/dist/dashboard/setting-words.js.map +0 -1
- package/dist/dashboard/spending-words.js +0 -24
- package/dist/dashboard/spending-words.js.map +0 -1
- package/dist/failure.js +0 -35
- package/dist/failure.js.map +0 -1
- package/dist/keep-running.js +0 -79
- package/dist/keep-running.js.map +0 -1
- package/dist/outside-failure.js +0 -31
- package/dist/outside-failure.js.map +0 -1
- package/dist/pick-wakes.js +0 -125
- package/dist/pick-wakes.js.map +0 -1
- package/dist/project.js +0 -1
- package/dist/project.js.map +0 -1
- package/dist/queue-counts.js +0 -51
- package/dist/queue-counts.js.map +0 -1
- package/dist/reasons.js +0 -33
- package/dist/reasons.js.map +0 -1
- package/dist/retention.js +0 -59
- package/dist/retention.js.map +0 -1
- package/dist/settings.js +0 -88
- package/dist/settings.js.map +0 -1
- package/dist/spending-limit.js +0 -82
- package/dist/spending-limit.js.map +0 -1
- package/dist/step-files.js +0 -200
- package/dist/step-files.js.map +0 -1
- package/dist/step-meter.js +0 -97
- package/dist/step-meter.js.map +0 -1
- package/dist/step-times.js +0 -114
- package/dist/step-times.js.map +0 -1
- package/dist/step-usage.js +0 -19
- package/dist/step-usage.js.map +0 -1
- package/dist/steps.js +0 -1205
- package/dist/steps.js.map +0 -1
- package/dist/storage/agent-claims.js +0 -3
- package/dist/storage/agent-claims.js.map +0 -1
- package/dist/storage/budgets.js +0 -121
- package/dist/storage/budgets.js.map +0 -1
- package/dist/storage/engine-records.js +0 -91
- package/dist/storage/engine-records.js.map +0 -1
- package/dist/storage/engine-storage.js +0 -67
- package/dist/storage/engine-storage.js.map +0 -1
- package/dist/storage/input-records.js +0 -12
- package/dist/storage/input-records.js.map +0 -1
- package/dist/storage/move-store.js +0 -251
- package/dist/storage/move-store.js.map +0 -1
- package/dist/storage/sqlite-store.js +0 -901
- package/dist/storage/sqlite-store.js.map +0 -1
- package/dist/storage/step-records.js +0 -11
- package/dist/storage/step-records.js.map +0 -1
- package/dist/storage/storage-error.js +0 -19
- package/dist/storage/storage-error.js.map +0 -1
- package/dist/storage/task-store.js +0 -30
- package/dist/storage/task-store.js.map +0 -1
- package/dist/system-workflows.js +0 -54
- package/dist/system-workflows.js.map +0 -1
- package/dist/tasks.js +0 -500
- package/dist/tasks.js.map +0 -1
- package/dist/views/format.js +0 -37
- package/dist/views/format.js.map +0 -1
- package/dist/views/runs-now.js +0 -117
- package/dist/views/runs-now.js.map +0 -1
- package/dist/views/step-funnel.js +0 -98
- package/dist/views/step-funnel.js.map +0 -1
- package/dist/views/step-graph.js +0 -39
- package/dist/views/step-graph.js.map +0 -1
- package/dist/views/workflows-view.js +0 -236
- package/dist/views/workflows-view.js.map +0 -1
- package/src/budget-states.ts +0 -54
- package/src/cpu-limit/limiter.ts +0 -935
- package/src/cpu-limit/use.ts +0 -166
- package/src/dashboard/agent-usage-line.tsx +0 -28
- package/src/dashboard/agent-usage.ts +0 -48
- package/src/dashboard/dashboard.css +0 -148
- package/src/dashboard/drilldown.ts +0 -70
- package/src/dashboard/index.tsx +0 -4
- package/src/dashboard/limits-and-runs.tsx +0 -191
- package/src/dashboard/machine-controls.tsx +0 -53
- package/src/dashboard/queue-bar.tsx +0 -62
- package/src/dashboard/queue-chart.tsx +0 -149
- package/src/dashboard/queue-lines.ts +0 -87
- package/src/dashboard/queue-table.ts +0 -37
- package/src/dashboard/run-doing-words.ts +0 -41
- package/src/dashboard/setting-words.ts +0 -30
- package/src/dashboard/spending-limit-control.tsx +0 -47
- package/src/dashboard/spending-words.ts +0 -32
- package/src/dashboard/time-distribution.tsx +0 -61
- package/src/dashboard/workflow-dag.tsx +0 -322
- package/src/dashboard/workflow-drilldown.tsx +0 -31
- package/src/dashboard/workflows-dashboard.tsx +0 -532
- package/src/failure.ts +0 -36
- package/src/keep-running.ts +0 -85
- package/src/outside-failure.ts +0 -39
- package/src/pick-wakes.ts +0 -182
- package/src/project.ts +0 -127
- package/src/queue-counts.ts +0 -64
- package/src/reasons.ts +0 -30
- package/src/retention.ts +0 -90
- package/src/settings.ts +0 -171
- package/src/spending-limit.ts +0 -139
- package/src/step-files.ts +0 -208
- package/src/step-meter.ts +0 -120
- package/src/step-times.ts +0 -151
- package/src/step-usage.ts +0 -23
- package/src/steps.ts +0 -1616
- package/src/storage/agent-claims.ts +0 -63
- package/src/storage/budgets.ts +0 -152
- package/src/storage/engine-records.ts +0 -119
- package/src/storage/engine-storage.ts +0 -82
- package/src/storage/input-records.ts +0 -43
- package/src/storage/move-store.ts +0 -265
- package/src/storage/sqlite-store.ts +0 -1022
- package/src/storage/step-records.ts +0 -358
- package/src/storage/storage-error.ts +0 -35
- package/src/storage/task-store.ts +0 -235
- package/src/system-workflows.ts +0 -70
- package/src/tasks.ts +0 -586
- package/src/views/format.ts +0 -40
- package/src/views/runs-now.ts +0 -188
- package/src/views/step-funnel.ts +0 -163
- package/src/views/step-graph.ts +0 -77
- package/src/views/workflows-view.ts +0 -484
- package/src/workflow-dag.test.tsx +0 -89
- package/src/workflow-page.test.tsx +0 -170
package/src/keep-running.ts
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* EACH WORKFLOW KEEPS THE RUNS GOING THAT ITS BUDGET SAYS, AND NOTHING ELSE DECIDES IT.
|
|
3
|
-
*
|
|
4
|
-
* Alex, 2026-11-06: "We simply have desire concurrent run rate per phase", then "move the concurrent run controls on the
|
|
5
|
-
* workflow level rather than on the phase level, then you can eliminate any logic that goes into decideing which workflows
|
|
6
|
-
* get the available runs". A workflow with an agent step and a list of work (`keptRunningByItsBudget`) is kept running by
|
|
7
|
-
* one trigger, with no code of its own: each turn it starts as many runs as the workflow's run limit keeps (`runLimit`) above
|
|
8
|
-
* the runs going, as soon as a run ends and otherwise on a fallback timer. There is no sharing between workflows, no order,
|
|
9
|
-
* no cap per item and no check of usage: a run with nothing to pick waits for an item (`steps.ts` `untilPicked`), and the
|
|
10
|
-
* blocking a Pick runs and the queue's order keep a run off what it cannot finish. A run limit of 0 holds one workflow.
|
|
11
|
-
*
|
|
12
|
-
* NO RUN STARTS THAT COULD TAKE NO WORK: while no agent can be had for a workflow and its Pick would pass over every task
|
|
13
|
-
* in its queue (`steps.ts` `whyNoRunCanTakeWork`), it starts none, and a run waiting in its Pick ends. Alex, relayed on 2026-11-20:
|
|
14
|
-
* "if the number of queued tasks that require agents is all of them, and if there is no remaining agentic budget or all
|
|
15
|
-
* agents are in use, then the workflow runner doesn't just continually cycle picking up tasks and moving them to the back
|
|
16
|
-
* of the queue again". The next turn after an agent can be had, a mark clears or new input lands starts runs again. While
|
|
17
|
-
* its agents are only all in use, runs start: a Pick passes over the marked tasks and waits, with nothing it can take,
|
|
18
|
-
* until an agent step ends (`steps.ts` `whyNoAgentIsFree`), so no run cycles through them either.
|
|
19
|
-
*/
|
|
20
|
-
import { keptRunningByItsBudget, type WorkflowDeclaration } from './steps';
|
|
21
|
-
import { runLimit, type WorkflowLimits } from './storage/budgets';
|
|
22
|
-
|
|
23
|
-
/** With no run ending to wake it, how often the trigger looks. */
|
|
24
|
-
export const KEEP_RUNNING_EVERY_SECONDS = 60;
|
|
25
|
-
/** How long one turn of the trigger may take, less than its minute; a turn past it is abandoned and the next one runs. */
|
|
26
|
-
export const TURN_LIMIT_MS = 50_000;
|
|
27
|
-
/** How long one run's start may take. */
|
|
28
|
-
export const START_LIMIT_SECONDS = 20;
|
|
29
|
-
|
|
30
|
-
/** The workflows this trigger keeps running, each with a budget. Pure. */
|
|
31
|
-
export const budgetedWorkflows = <D extends Pick<WorkflowDeclaration<unknown>, 'pending' | 'items' | 'steps'>>(declared: ReadonlyArray<D>): ReadonlyArray<D> =>
|
|
32
|
-
declared.filter(keptRunningByItsBudget);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* How many runs of one workflow to start now: the runs its run limit keeps above the runs going. Its agents' limit plays
|
|
36
|
-
* no part: a run with a task that finds no agent free at its first agent step gives its task back and ends. Pure.
|
|
37
|
-
*/
|
|
38
|
-
export const runsToStart = (w: { readonly kept: number; readonly running: number }): number => Math.max(w.kept - w.running, 0);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* One turn's plan: for each budgeted workflow, the runs going and how many to start; none where no run could take work
|
|
42
|
-
* (`noWork`, from `steps.ts` `whyNoRunCanTakeWork`), which says why. Pure but for the reads it is given.
|
|
43
|
-
*/
|
|
44
|
-
export const planRunStarts = <D extends Pick<WorkflowDeclaration<unknown>, 'name' | 'pending' | 'items' | 'steps'>>(input: {
|
|
45
|
-
readonly declared: ReadonlyArray<D>;
|
|
46
|
-
readonly limitsOf: (workflow: string) => WorkflowLimits;
|
|
47
|
-
readonly runningRuns: (workflow: string) => number;
|
|
48
|
-
readonly noWork: (workflow: string) => string | null;
|
|
49
|
-
}): ReadonlyArray<{ readonly declaration: D; readonly running: number; readonly start: number; readonly noWork: string | null }> =>
|
|
50
|
-
budgetedWorkflows(input.declared).map((d) => {
|
|
51
|
-
const running = input.runningRuns(d.name);
|
|
52
|
-
const noWork = input.noWork(d.name);
|
|
53
|
-
return { declaration: d, running, noWork, start: noWork !== null ? 0 : runsToStart({ kept: runLimit(input.limitsOf(d.name)), running }) };
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* ONE TURN AT A TIME, AND NO TURN HOLDS THE NEXT UP FOR LONG. A call during a turn shares its result and asks for one more
|
|
58
|
-
* after it. A turn past `limitMs` fails, so nothing it hung on can leave the trigger shut, and `stillCurrent` turns false
|
|
59
|
-
* for it, so if it wakes later it starts nothing on figures that have gone old.
|
|
60
|
-
*/
|
|
61
|
-
export const oneTurnAtATime = (turn: (stillCurrent: () => boolean) => Promise<void>, limitMs: number): (() => Promise<void>) => {
|
|
62
|
-
let going: Promise<void> | null = null;
|
|
63
|
-
let again = false;
|
|
64
|
-
let current = 0;
|
|
65
|
-
const trigger = (): Promise<void> => {
|
|
66
|
-
if (going !== null) { again = true; return going; }
|
|
67
|
-
current += 1;
|
|
68
|
-
const mine = current;
|
|
69
|
-
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
70
|
-
const limit = new Promise<never>((_, reject) => {
|
|
71
|
-
timer = setTimeout(() => reject(new Error(`a turn of the trigger was abandoned after ${limitMs / 1000} s`)), limitMs);
|
|
72
|
-
});
|
|
73
|
-
going = Promise.race([turn(() => current === mine), limit]).finally(() => {
|
|
74
|
-
clearTimeout(timer);
|
|
75
|
-
current += 1;
|
|
76
|
-
going = null;
|
|
77
|
-
if (again) { again = false; void trigger().catch(() => undefined); }
|
|
78
|
-
});
|
|
79
|
-
return going;
|
|
80
|
-
};
|
|
81
|
-
return trigger;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
/** A run's id: the time it was made, and a random tail so two made in one millisecond differ. */
|
|
85
|
-
export const newRunId = () => `${Date.now().toString(36)}${Math.floor(Math.random() * 1296).toString(36).padStart(2, '0')}`;
|
package/src/outside-failure.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A RUN AN OUTSIDE SERVICE FAILED IS NO TRY. A task is blocked after `BLOCK_AFTER_TRIES` failed tries (`tasks.ts`), and
|
|
3
|
-
* only a run that did its work and ended without success is one. A run that failed because a service outside it could
|
|
4
|
-
* not do the work (a Claude usage limit, a model server that does not answer) says nothing about its item: counted, two
|
|
5
|
-
* weekly-limit failures took an item of skill-sync's skill refinement workflow from 9 tries to 11, and it was blocked
|
|
6
|
-
* for good once the limit reset (2026-11-25).
|
|
7
|
-
*
|
|
8
|
-
* A step says so by failing with `OutsideServiceUnavailable`. Claude's own usage-limit and rate-limit words are read the
|
|
9
|
-
* same way whatever error carries them, so outcomes recorded before this error existed count right too.
|
|
10
|
-
*
|
|
11
|
-
* The task store counts tries in SQL, from each outcome's text (`storage/sqlite-store.ts`): `OUTSIDE_FAILURE_PATTERNS`
|
|
12
|
-
* are its `LIKE` patterns, which SQLite matches without regard to ASCII case, and `isOutsideFailure` reads them the same
|
|
13
|
-
* way in code.
|
|
14
|
-
*/
|
|
15
|
-
import { Data } from 'effect';
|
|
16
|
-
|
|
17
|
-
/** What a step fails with when a service outside it could not do its work: its run's outcome is no try. */
|
|
18
|
-
export class OutsideServiceUnavailable extends Data.TaggedError('OutsideServiceUnavailable')<{
|
|
19
|
-
/** The service, by its own name: `claude`, `jev`. */
|
|
20
|
-
readonly service: string;
|
|
21
|
-
/** Its own words on why, such as "You've hit your weekly limit · resets 6am (UTC)". */
|
|
22
|
-
readonly reason: string;
|
|
23
|
-
}> {}
|
|
24
|
-
|
|
25
|
-
/** The outcomes of runs an outside service failed, as SQL `LIKE` patterns on an outcome's text. */
|
|
26
|
-
export const OUTSIDE_FAILURE_PATTERNS: ReadonlyArray<string> = [
|
|
27
|
-
'failed: OutsideServiceUnavailable:%',
|
|
28
|
-
// Claude's own words: "You've hit your weekly limit · resets 6am (UTC)", "Claude usage limit reached", a 429's "rate limit".
|
|
29
|
-
'failed: %hit your % limit%',
|
|
30
|
-
'failed: %usage limit%',
|
|
31
|
-
'failed: %rate limit%',
|
|
32
|
-
];
|
|
33
|
-
|
|
34
|
-
const asRegExp = (pattern: string): RegExp =>
|
|
35
|
-
new RegExp(`^${pattern.split('%').map((part) => part.replace(/[.*+?^$()|[\]\\{}_]/g, (c) => (c === '_' ? '.' : `\\${c}`))).join('.*')}$`, 'is');
|
|
36
|
-
const OUTSIDE_FAILURES = OUTSIDE_FAILURE_PATTERNS.map(asRegExp);
|
|
37
|
-
|
|
38
|
-
/** Whether a run's outcome says an outside service failed it, as the task store reads it. */
|
|
39
|
-
export const isOutsideFailure = (outcome: string): boolean => OUTSIDE_FAILURES.some((p) => p.test(outcome));
|
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`), an agent step of its runs ending, which frees an agent for a task marked as needing one
|
|
6
|
-
* (`steps.ts` `whyNoAgentIsFree`), or one of its runs ending, which lets that run's 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,127 +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
|
-
import type { InheritedSettings } from './settings';
|
|
8
|
-
import type { SystemSchedule, SystemWorkflowName } from './system-workflows';
|
|
9
|
-
|
|
10
|
-
/** A process, by its pid and the second it started (as `ps` reports it), so a reused pid is never taken for it. */
|
|
11
|
-
export interface ProcessIdentity {
|
|
12
|
-
readonly pid: number;
|
|
13
|
-
readonly startedAt: number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** What one process a step started used, as the project's process starter measures it when the process exits. */
|
|
17
|
-
export interface ProcessUsage {
|
|
18
|
-
readonly label: string;
|
|
19
|
-
readonly cpuSeconds: number;
|
|
20
|
-
readonly peakBytes: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/** Where a process's usage goes: the step it ran for. `record` also tells a child process which step record it works for. */
|
|
24
|
-
export interface UsageSink {
|
|
25
|
-
readonly record: number | null;
|
|
26
|
-
readonly add: (usage: ProcessUsage) => void;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/** A short command run at once, outside any CPU limit's queue (`ps`, `diff3`). */
|
|
30
|
-
export type RunLightNow = (
|
|
31
|
-
command: ReadonlyArray<string>,
|
|
32
|
-
options?: { readonly env?: Record<string, string> },
|
|
33
|
-
) => { readonly code: number; readonly stdout: string; readonly stdoutBytes: Uint8Array; readonly stderr: string };
|
|
34
|
-
|
|
35
|
-
/** Texts stored by their content hash: a task note's change and new input name theirs. */
|
|
36
|
-
export interface TextStore {
|
|
37
|
-
/** Stores `text` and returns its hash; storing it again changes nothing. */
|
|
38
|
-
readonly write: (text: string) => string;
|
|
39
|
-
/** The text stored under `sha`, checked against it; throws when there is none. */
|
|
40
|
-
readonly read: (sha: string) => string;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/** The project's working tree, as a step's changed files are read from it (`step-files.ts`). */
|
|
44
|
-
export interface ProjectTree {
|
|
45
|
-
/** The folder every path is relative to: the checkout. */
|
|
46
|
-
readonly root: string;
|
|
47
|
-
/** Where step snapshots of the tree are kept. */
|
|
48
|
-
readonly snapshots: string;
|
|
49
|
-
/** Paths, from `root`, that are the project's own output rather than anyone's edit. */
|
|
50
|
-
readonly notTree: ReadonlyArray<string>;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* THE ONE CPU LIMIT FOR EVERYTHING THE PROJECT STARTS: its workflows' processes, its own long-running processes and its
|
|
55
|
-
* agents' commands. The project runs it (a process starter that holds a slot from start to exit, admitting new work only
|
|
56
|
-
* while what the project uses, as measured, leaves room); the platform reads it, to say where the project stands.
|
|
57
|
-
*/
|
|
58
|
-
export interface CpuLimit {
|
|
59
|
-
/** The most cores the project may use at once. */
|
|
60
|
-
readonly limit: () => number;
|
|
61
|
-
/** The newest reading of what the project used, in cores, and how many slots were held and waiting; none before the first. */
|
|
62
|
-
readonly newestUse: () => { readonly at: number; readonly cores: number; readonly held: number; readonly waiting: number } | null;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** One good reading of the week the project's agents spend from, as its account reports it. */
|
|
66
|
-
export interface WeekUsageReading {
|
|
67
|
-
/** How much of the week is used, in percent. */
|
|
68
|
-
readonly usedPercent: number;
|
|
69
|
-
/** When it was read, unix seconds. */
|
|
70
|
-
readonly readAt: number;
|
|
71
|
-
/** When the week starts again, unix seconds; none where the account does not say. */
|
|
72
|
-
readonly resetsAt: number | null;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** What the project knows of its week now: its newest good reading, and a read that failed after it. */
|
|
76
|
-
export interface WeekUsageNow {
|
|
77
|
-
/** The newest reading that succeeded, however old; none before the first. */
|
|
78
|
-
readonly good: WeekUsageReading | null;
|
|
79
|
-
/** The newest read, where it failed after that one: when, and why in words. */
|
|
80
|
-
readonly failedSince: { readonly at: number; readonly error: string } | null;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* THE WEEK THE PROJECT'S AGENTS SPEND FROM, as the project reads it: the one figure the spending limit judges
|
|
85
|
-
* (`spending-limit.ts`). The project reads it however it reads its account; the platform never asks the account itself.
|
|
86
|
-
*/
|
|
87
|
-
export interface WeekUsage {
|
|
88
|
-
readonly now: () => Effect.Effect<WeekUsageNow, StorageError>;
|
|
89
|
-
/** How old a good reading may be before a page calls it stale: the project's time between readings, and its margin. */
|
|
90
|
-
readonly staleAfterSeconds: number;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export interface WorkflowProject {
|
|
94
|
-
/** The folder a workflow's or step's source link is relative to. */
|
|
95
|
-
readonly sourceRoot: string;
|
|
96
|
-
readonly tree: ProjectTree;
|
|
97
|
-
readonly texts: TextStore;
|
|
98
|
-
readonly runLightNow: RunLightNow;
|
|
99
|
-
/** This process, and whether another is still alive. */
|
|
100
|
-
readonly processes: {
|
|
101
|
-
readonly thisProcess: () => ProcessIdentity;
|
|
102
|
-
readonly isAlive: (process: ProcessIdentity) => boolean;
|
|
103
|
-
};
|
|
104
|
-
/**
|
|
105
|
-
* The variable that tells a process a step started which step record it works for, so its compiles reach that record
|
|
106
|
-
* (`step-usage.ts`).
|
|
107
|
-
*/
|
|
108
|
-
readonly stepRecordVariable: string;
|
|
109
|
-
/** A failure that says nothing about the work and is waited out: a step retries on it (a locked database). */
|
|
110
|
-
readonly isTransientFailure: (failure: unknown) => boolean;
|
|
111
|
-
readonly cpu: CpuLimit;
|
|
112
|
-
/**
|
|
113
|
-
* The week its agents spend from, where it reads one: the spending limit holds its agent steps by it
|
|
114
|
-
* (`spending-limit.ts`). A project that gives none has no spending limit.
|
|
115
|
-
*/
|
|
116
|
-
readonly weekUsage?: WeekUsage;
|
|
117
|
-
/**
|
|
118
|
-
* THE APP'S PLATFORM-WIDE SETTINGS: what every workflow and step inherits, the platform's own included, where nothing
|
|
119
|
-
* below sets its own (`settings.ts`), over the platform's defaults. Given once, here.
|
|
120
|
-
*/
|
|
121
|
-
readonly settings?: InheritedSettings;
|
|
122
|
-
/**
|
|
123
|
-
* How often each of the platform's own workflows runs, where not its default (`system-workflows.ts`). Its steps are the
|
|
124
|
-
* platform's; its schedule here and its run limit in the limits table are the app's.
|
|
125
|
-
*/
|
|
126
|
-
readonly systemSchedules?: Readonly<Partial<Record<SystemWorkflowName, SystemSchedule>>>;
|
|
127
|
-
}
|
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/retention.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* RETENTION: A RUN'S RECORDS GO ONCE THE RUN HAS BEEN OVER LONGER THAN ITS STEPS KEEP THEM, AND IS NOT ONE OF EACH STEP'S
|
|
3
|
-
* NEWEST. Alex, 2026-11-21: "time based retention is a good idea. it should be configurable at the highest level with
|
|
4
|
-
* allowed step level overrides ... i think 7 days sounds reasonable." Then, 2026-11-22: "a project might complete or stall
|
|
5
|
-
* but we shouldn't lose run data in that case", and "10k runs". How long, and how many runs at least, is each step's own
|
|
6
|
-
* setting, else its workflow's, from the top down, else the app's (`settings.ts`), stored while the app runs or in code; 7
|
|
7
|
-
* days and 10,000 runs where nothing sets them. There is no most a step keeps.
|
|
8
|
-
*
|
|
9
|
-
* What goes: each step record and its per-item parts (`step_runs`, `step_functions`), once every record of its run has
|
|
10
|
-
* ended that long ago, the engine has the run finished, and the run is none of that step's newest kept at least; and, for a
|
|
11
|
-
* workflow whose tasks are its runs (`steps.ts` `tasksAreRuns`), each run's task with its notes, as its workflow keeps them,
|
|
12
|
-
* its newest kept at least the same way. An item's task (a function Decompile works on: queued, in progress, blocked or done)
|
|
13
|
-
* is the work's state, not its history, and never goes. Records of a step no longer declared go as its workflow keeps them,
|
|
14
|
-
* and those of a workflow no longer declared as the app keeps them.
|
|
15
|
-
*
|
|
16
|
-
* No app runs it: the platform's own workflow Retention does, on a schedule (`system-workflows.ts`), so each pass is
|
|
17
|
-
* measured and recorded like any other step's work.
|
|
18
|
-
*/
|
|
19
|
-
import { Effect } from 'effect';
|
|
20
|
-
import { appSetting, stepSetting, workflowSetting, type InheritedSettings, type SettingName, type SettingsTree, type StoredSetting } from './settings';
|
|
21
|
-
import { tasksAreRuns, type StepDeclaration, type WorkflowDeclaration } from './steps';
|
|
22
|
-
import type { EngineRecords } from './storage/engine-records';
|
|
23
|
-
import type { StepRecords } from './storage/step-records';
|
|
24
|
-
import type { StorageError } from './storage/storage-error';
|
|
25
|
-
import type { TaskStore } from './storage/task-store';
|
|
26
|
-
|
|
27
|
-
/** What one pass deleted: step records, their per-item parts, and the tasks that were runs, with their notes. */
|
|
28
|
-
export interface RetentionPass {
|
|
29
|
-
readonly records: number;
|
|
30
|
-
readonly parts: number;
|
|
31
|
-
readonly runTasks: number;
|
|
32
|
-
readonly runTaskNotes: number;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const SECONDS_A_DAY = 86_400;
|
|
36
|
-
|
|
37
|
-
/** What retention reads of each declared workflow: its name, its item, its settings and its steps'. */
|
|
38
|
-
export type RetentionDeclared = Pick<WorkflowDeclaration<unknown>, 'name' | 'item' | SettingName> & {
|
|
39
|
-
readonly steps: () => ReadonlyArray<Pick<StepDeclaration, 'name' | 'workflow' | 'runsOnItsOwnTrigger' | SettingName>>;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export const makeRetention = (deps: {
|
|
43
|
-
/** Every workflow declared now. */
|
|
44
|
-
readonly declared: () => ReadonlyArray<RetentionDeclared>;
|
|
45
|
-
/** What the app sets above every workflow (`WorkflowProject.settings`). */
|
|
46
|
-
readonly app: InheritedSettings;
|
|
47
|
-
/** The retention settings set while the app runs, read at each pass (`LimitStore.storedSettings`). */
|
|
48
|
-
readonly stored: () => Effect.Effect<ReadonlyArray<StoredSetting>, StorageError>;
|
|
49
|
-
readonly records: Pick<StepRecords, 'deleteRecordsOfRunsEndedBefore'>;
|
|
50
|
-
readonly tasks: Pick<TaskStore, 'deleteRunTasksEndedBefore'>;
|
|
51
|
-
readonly engine: Pick<EngineRecords, 'unfinishedRuns'>;
|
|
52
|
-
/** A workflow's queue changed: its tasks that were runs went, so its counts are taken again (`queue-counts.ts`). */
|
|
53
|
-
readonly queueChanged: (workflow: string) => void;
|
|
54
|
-
}) => {
|
|
55
|
-
/** One pass at `now`, unix seconds: what each record's step keeps, applied to every declared workflow and the rest. */
|
|
56
|
-
const prune = (now: number): Effect.Effect<RetentionPass, StorageError> => Effect.gen(function* () {
|
|
57
|
-
const declared = deps.declared();
|
|
58
|
-
const tree: SettingsTree = { declared, app: deps.app, stored: yield* deps.stored() };
|
|
59
|
-
// A run the engine has not finished keeps every record and its task, however old: its next step reads them.
|
|
60
|
-
const keepRuns = (yield* deps.engine.unfinishedRuns()).map((u) => u.run);
|
|
61
|
-
const before = (days: number) => now - days * SECONDS_A_DAY;
|
|
62
|
-
let pass: RetentionPass = { records: 0, parts: 0, runTasks: 0, runTaskNotes: 0 };
|
|
63
|
-
const add = (more: Partial<RetentionPass>) => {
|
|
64
|
-
pass = { records: pass.records + (more.records ?? 0), parts: pass.parts + (more.parts ?? 0), runTasks: pass.runTasks + (more.runTasks ?? 0), runTaskNotes: pass.runTaskNotes + (more.runTaskNotes ?? 0) };
|
|
65
|
-
};
|
|
66
|
-
for (const d of declared) {
|
|
67
|
-
const steps = d.steps();
|
|
68
|
-
for (const s of steps) {
|
|
69
|
-
const at = { workflow: d.name, step: s.name };
|
|
70
|
-
add(yield* deps.records.deleteRecordsOfRunsEndedBefore({
|
|
71
|
-
select: at, before: before(stepSetting(tree, { ...at, name: 'retentionDays' }).value), keepRuns, keepNewest: stepSetting(tree, { ...at, name: 'keepAtLeastRuns' }).value,
|
|
72
|
-
}));
|
|
73
|
-
}
|
|
74
|
-
const workflowKeeps = { days: workflowSetting(tree, { workflow: d.name, name: 'retentionDays' }).value, atLeastRuns: workflowSetting(tree, { workflow: d.name, name: 'keepAtLeastRuns' }).value };
|
|
75
|
-
add(yield* deps.records.deleteRecordsOfRunsEndedBefore({ select: { workflow: d.name, stepsOtherThan: steps.map((s) => s.name) }, before: before(workflowKeeps.days), keepRuns, keepNewest: workflowKeeps.atLeastRuns }));
|
|
76
|
-
if (tasksAreRuns(d, declared)) {
|
|
77
|
-
const gone = yield* deps.tasks.deleteRunTasksEndedBefore({ workflow: d.name, before: before(workflowKeeps.days), keepItems: keepRuns, keepNewest: workflowKeeps.atLeastRuns });
|
|
78
|
-
add({ runTasks: gone.tasks, runTaskNotes: gone.notes });
|
|
79
|
-
if (gone.tasks > 0) deps.queueChanged(d.name);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
add(yield* deps.records.deleteRecordsOfRunsEndedBefore({
|
|
83
|
-
select: { workflowsOtherThan: declared.map((d) => d.name) }, before: before(appSetting(tree, 'retentionDays').value), keepRuns, keepNewest: appSetting(tree, 'keepAtLeastRuns').value,
|
|
84
|
-
}));
|
|
85
|
-
return pass;
|
|
86
|
-
});
|
|
87
|
-
return { prune };
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export type Retention = ReturnType<typeof makeRetention>;
|