@jive-sh/workflow-platform 0.6.2 → 0.7.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.
Files changed (89) hide show
  1. package/README.md +189 -42
  2. package/dist/budget-states.js +1 -1
  3. package/dist/budget-states.js.map +1 -1
  4. package/dist/dashboard/dashboard.css +42 -28
  5. package/dist/dashboard/drilldown.js +35 -4
  6. package/dist/dashboard/drilldown.js.map +1 -1
  7. package/dist/dashboard/run-doing-words.js +29 -0
  8. package/dist/dashboard/run-doing-words.js.map +1 -0
  9. package/dist/dashboard/setting-words.js +18 -0
  10. package/dist/dashboard/setting-words.js.map +1 -0
  11. package/dist/dashboard/spending-words.js +2 -10
  12. package/dist/dashboard/spending-words.js.map +1 -1
  13. package/dist/index.js +76 -13
  14. package/dist/index.js.map +1 -1
  15. package/dist/keep-running.js +4 -2
  16. package/dist/keep-running.js.map +1 -1
  17. package/dist/pick-wakes.js +2 -2
  18. package/dist/retention.js +59 -0
  19. package/dist/retention.js.map +1 -0
  20. package/dist/settings.js +88 -0
  21. package/dist/settings.js.map +1 -0
  22. package/dist/spending-limit.js +15 -21
  23. package/dist/spending-limit.js.map +1 -1
  24. package/dist/step-times.js +114 -0
  25. package/dist/step-times.js.map +1 -0
  26. package/dist/steps.js +399 -114
  27. package/dist/steps.js.map +1 -1
  28. package/dist/storage/agent-claims.js +3 -0
  29. package/dist/storage/agent-claims.js.map +1 -0
  30. package/dist/storage/budgets.js +40 -4
  31. package/dist/storage/budgets.js.map +1 -1
  32. package/dist/storage/engine-records.js +26 -10
  33. package/dist/storage/engine-records.js.map +1 -1
  34. package/dist/storage/sqlite-store.js +198 -20
  35. package/dist/storage/sqlite-store.js.map +1 -1
  36. package/dist/storage/task-store.js +11 -5
  37. package/dist/storage/task-store.js.map +1 -1
  38. package/dist/system-workflows.js +54 -0
  39. package/dist/system-workflows.js.map +1 -0
  40. package/dist/tasks.js +72 -29
  41. package/dist/tasks.js.map +1 -1
  42. package/dist/views/runs-now.js +80 -38
  43. package/dist/views/runs-now.js.map +1 -1
  44. package/dist/views/step-graph.js +6 -1
  45. package/dist/views/step-graph.js.map +1 -1
  46. package/dist/views/workflows-view.js +29 -23
  47. package/dist/views/workflows-view.js.map +1 -1
  48. package/package.json +1 -1
  49. package/src/budget-states.ts +2 -3
  50. package/src/dashboard/dashboard.css +42 -28
  51. package/src/dashboard/drilldown.ts +32 -5
  52. package/src/dashboard/index.tsx +1 -1
  53. package/src/dashboard/limits-and-runs.tsx +191 -0
  54. package/src/dashboard/queue-bar.tsx +30 -11
  55. package/src/dashboard/queue-chart.tsx +1 -2
  56. package/src/dashboard/run-doing-words.ts +41 -0
  57. package/src/dashboard/setting-words.ts +30 -0
  58. package/src/dashboard/spending-limit-control.tsx +5 -8
  59. package/src/dashboard/spending-words.ts +5 -23
  60. package/src/dashboard/time-distribution.tsx +61 -0
  61. package/src/dashboard/workflow-dag.tsx +247 -129
  62. package/src/dashboard/workflow-drilldown.tsx +13 -71
  63. package/src/dashboard/workflows-dashboard.tsx +345 -227
  64. package/src/index.ts +86 -16
  65. package/src/keep-running.ts +6 -4
  66. package/src/pick-wakes.ts +2 -2
  67. package/src/project.ts +12 -0
  68. package/src/retention.ts +90 -0
  69. package/src/settings.ts +171 -0
  70. package/src/spending-limit.ts +25 -42
  71. package/src/step-times.ts +151 -0
  72. package/src/steps.ts +512 -135
  73. package/src/storage/agent-claims.ts +63 -0
  74. package/src/storage/budgets.ts +43 -4
  75. package/src/storage/engine-records.ts +28 -11
  76. package/src/storage/sqlite-store.ts +225 -32
  77. package/src/storage/step-records.ts +59 -7
  78. package/src/storage/task-store.ts +29 -29
  79. package/src/system-workflows.ts +70 -0
  80. package/src/tasks.ts +69 -30
  81. package/src/views/runs-now.ts +119 -51
  82. package/src/views/step-graph.ts +11 -2
  83. package/src/views/workflows-view.ts +80 -39
  84. package/src/workflow-dag.test.tsx +89 -0
  85. package/src/workflow-page.test.tsx +170 -0
  86. package/dist/views/workflow-flows.js +0 -53
  87. package/dist/views/workflow-flows.js.map +0 -1
  88. package/src/dashboard/step-graph.tsx +0 -106
  89. package/src/views/workflow-flows.ts +0 -97
@@ -2,11 +2,13 @@
2
2
  * THE SPENDING LIMIT: ONE SETTING FOR THE WHOLE ACCOUNT, "AGENTS STOP AT X% OF THE WEEK". Alex, 2026-11-20: "ideally the
3
3
  * workflow platform offers a configurable spending limit", and, of this design, "yes to building the spending limit".
4
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`).
5
+ * While the project's newest good reading of its week (`WorkflowProject.weekUsage`) is at or past X, no run of any workflow
6
+ * is given an agent claim (`steps.ts` `claimAgentsOrEnd`), and a run already holding one ends before its next agent call
7
+ * (`beforeAgentCall`), as at an agents' limit of 0: its task goes to the back of its queue, marked as needing an agent,
8
+ * with why in words. Nothing waits, and an agent call already going is never cut off. Runs take claims again once a reading
9
+ * is below X, which the week's reset brings: the project tells the platform of each reading it takes (`readingTaken`), and
10
+ * a verdict that changed has the run planner plan again and wakes every Pick. None is set: X is 95
11
+ * (`DEFAULT_SPENDING_LIMIT_PERCENT`).
10
12
  *
11
13
  * IT JUDGES THE ACCOUNT'S WEEK, NOT A COUNT OF TOKENS, because the week is the one figure that also counts spending the
12
14
  * platform cannot see: agent threads run by hand, outside any workflow, which no step record holds. They were 73% of the
@@ -14,9 +16,9 @@
14
16
  *
15
17
  * A READING THAT FAILS OR GROWS OLD NEVER LIFTS IT. The rule judges the newest good reading, however old, so a hold stands
16
18
  * 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.
19
+ * too: with every read failing, no agent is given until one succeeds, rather than spend on a week nobody can see. A week that
20
+ * cannot be read at all holds the same way. With no good reading yet there is nothing to judge, and nothing is held. A
21
+ * project that reads no week has no spending limit.
20
22
  */
21
23
  import { Effect, pipe, Result } from 'effect';
22
24
  import type { PickWakes } from './pick-wakes';
@@ -40,20 +42,10 @@ export interface SpendingLimitState {
40
42
  readonly failedSince: WeekUsageNow['failedSince'];
41
43
  /** Whether the reading is stale: a read failed since, or it is older than the project's time between readings allows. */
42
44
  readonly stale: boolean;
43
- /** Whether every agent step waits now. */
45
+ /** Whether it withholds every agent now. */
44
46
  readonly holds: boolean;
45
47
  }
46
48
 
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
49
  /** The limit against the week now. Pure. */
58
50
  export const judgeSpending = (input: {
59
51
  readonly limit: StoredSpendingLimit | null;
@@ -73,29 +65,22 @@ export const judgeSpending = (input: {
73
65
  };
74
66
  };
75
67
 
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
68
  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>;
69
+ /**
70
+ * Why no agent can be had now, in words, until a reading or the limit changes; none while it holds nothing. The reason a
71
+ * run ends and marks its task as needing an agent (`steps.ts` `claimAgentsOrEnd`).
72
+ */
73
+ readonly holdsAgents: () => Effect.Effect<string | null>;
89
74
  /** The limit against the newest reading, for a page; none for a project that reads no week. */
90
75
  readonly state: (now: number) => Effect.Effect<SpendingLimitState | null, StorageError>;
91
76
  /**
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.
77
+ * The project took a reading of its week. Where the verdict changed since the last one, or a run found the week
78
+ * unreadable since, the run planner plans again and every Pick is woken: a reading below the limit lets runs claim again.
94
79
  */
95
80
  readonly readingTaken: () => Effect.Effect<void>;
96
81
  /**
97
82
  * 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
83
+ * through the project's `write`, wakes every Pick (relayed to the process its runs go in), and says in the log
99
84
  * who asked (`from`), as the routes that set a workflow's limits do. A refused one changes nothing.
100
85
  */
101
86
  readonly set: <E>(r: {
@@ -114,18 +99,16 @@ export const makeSpendingLimit = (deps: { readonly store: LimitStore; readonly u
114
99
  );
115
100
  /** What the last reading told held, in this process; none before the first. */
116
101
  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. */
102
+ /** A run found the week unreadable since the last reading, so the next reading wakes every Pick, whatever its verdict. */
118
103
  let unreadSinceLastReading = false;
119
104
  return {
120
105
  holdsAgents: () => pipe(
121
106
  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 => {
107
+ Effect.map((s): string | null => (s === null || !s.holds || s.reading === null ? null
108
+ : `the week's use is ${percentCut(s.reading.usedPercent)}, at or past the spending limit of ${s.limitPercent}%`)),
109
+ Effect.catchTag('StorageError', (e) => Effect.sync((): string => {
127
110
  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 };
111
+ return `the week's use could not be read (${e.reason}), so the spending limit holds`;
129
112
  })),
130
113
  ),
131
114
  state,
@@ -135,7 +118,7 @@ export const makeSpendingLimit = (deps: { readonly store: LimitStore; readonly u
135
118
  const holds = judged.success !== null && judged.success.holds;
136
119
  if (holds === heldAtLastReading && !unreadSinceLastReading) return;
137
120
  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}%`);
121
+ yield* Effect.log(`[spending limit] ${holds ? 'withholds every agent' : '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
122
  }
140
123
  heldAtLastReading = holds;
141
124
  unreadSinceLastReading = false;
@@ -0,0 +1,151 @@
1
+ /**
2
+ * EACH STEP'S TIME DISTRIBUTION OVER THE CURRENT VERSION OF ITS WORKFLOW, FROM EVERY TASK'S DATA. Alex, 2026-11-21: "it must
3
+ * be drawn via every task's data"; "one point is (time taken, number of tasks with that time taken)".
4
+ *
5
+ * ONE VALUE PER TASK PER RUN: the task's whole time in the step within that run, every call of it added up (Write C's 1 to 3
6
+ * variants for one function are one value). A record's task is its item (`subject`), or its run where it names none; a batch
7
+ * record counts each item by its own part (`step_functions`). A value counts once the run has left the step (a record of
8
+ * another step is newer, or the run has ended) and none of its calls is open; a run that comes back to the step shows the
9
+ * value again, larger, once it leaves again.
10
+ *
11
+ * HELD IN MEMORY, NEVER SCANNED FOR A PAGE: a workflow's records of its current version (`steps.ts` `versionOf`) are read
12
+ * once through their index (`step_runs_by_version`), then each refresh reads only the records written since and the ones
13
+ * still open, by id. Where the current version has no value yet, the previous version's are read once and shown as that.
14
+ */
15
+ import { Effect } from 'effect';
16
+ import type { EngineRecords } from './storage/engine-records';
17
+ import type { StepRecords, TimedPart, TimedRecord } from './storage/step-records';
18
+ import type { StorageError } from './storage/storage-error';
19
+ import type { WorkflowVersion } from './steps';
20
+
21
+ /** One step's values: each task's time in it, seconds to 0.1, fastest first. */
22
+ export interface StepValues {
23
+ readonly step: string;
24
+ readonly values: ReadonlyArray<number>;
25
+ }
26
+
27
+ /** Every step's values of one version of a workflow. */
28
+ export interface VersionTimes {
29
+ readonly version: string;
30
+ readonly steps: ReadonlyArray<StepValues>;
31
+ }
32
+
33
+ /** What a page draws each step's distribution from (`WorkflowsDashboard`). */
34
+ export interface StepTimes extends VersionTimes {
35
+ readonly workflow: string;
36
+ /** When the newest file of the current version last changed, unix seconds. */
37
+ readonly since: number;
38
+ /** Where the current version has no value yet: the previous version's, and when its newest record started. */
39
+ readonly previous: (VersionTimes & { readonly at: number }) | null;
40
+ }
41
+
42
+ /**
43
+ * EACH STEP'S VALUES FROM ITS RECORDS: one per task per run, its calls added up, counted once the run has left the step and
44
+ * none of its calls is open. `unfinished` is the runs still going. Pure, so it is tested alone.
45
+ */
46
+ export const taskTimes = (input: {
47
+ readonly records: ReadonlyArray<TimedRecord>;
48
+ readonly parts: ReadonlyArray<TimedPart>;
49
+ readonly unfinished: ReadonlySet<string>;
50
+ }): ReadonlyMap<string, ReadonlyArray<number>> => {
51
+ const newest = new Map<string, TimedRecord>();
52
+ for (const r of input.records) if ((newest.get(r.run)?.id ?? -1) < r.id) newest.set(r.run, r);
53
+ const partsOf = new Map<number, TimedPart[]>();
54
+ for (const p of input.parts) partsOf.set(p.stepRun, [...(partsOf.get(p.stepRun) ?? []), p]);
55
+ const groups = new Map<string, { readonly run: string; readonly step: string; seconds: number; open: boolean }>();
56
+ const add = (r: TimedRecord, task: string, started: number, ended: number | null) => {
57
+ const key = `${r.run}\u0000${r.step}\u0000${task.toLowerCase()}`;
58
+ const g = groups.get(key) ?? { run: r.run, step: r.step, seconds: 0, open: false };
59
+ if (ended === null) g.open = true;
60
+ else g.seconds += Math.max(0, ended - started);
61
+ groups.set(key, g);
62
+ };
63
+ for (const r of input.records) {
64
+ const parts = r.scope === 'functions' ? partsOf.get(r.id) : undefined;
65
+ if (parts !== undefined && parts.length > 0) for (const p of parts) add(r, p.subject, p.started, p.ended);
66
+ else add(r, r.subject ?? r.run, r.started, r.ended);
67
+ }
68
+ const values = new Map<string, number[]>();
69
+ for (const g of groups.values()) {
70
+ const left = !input.unfinished.has(g.run) || newest.get(g.run)?.step !== g.step;
71
+ if (g.open || !left) continue;
72
+ values.set(g.step, [...(values.get(g.step) ?? []), Math.round(g.seconds * 10) / 10]);
73
+ }
74
+ for (const v of values.values()) v.sort((a, b) => a - b);
75
+ return values;
76
+ };
77
+
78
+ /** A version's records held: by id, with each batch record's parts, and the newest id read of every workflow's. */
79
+ interface Held {
80
+ readonly version: string;
81
+ readonly records: Map<number, TimedRecord>;
82
+ readonly parts: Map<number, ReadonlyArray<TimedPart>>;
83
+ newestId: number;
84
+ }
85
+
86
+ export const makeStepTimes = (deps: {
87
+ readonly records: Pick<StepRecords, 'versionRecords' | 'recordsSince' | 'previousVersion'>;
88
+ readonly engine: Pick<EngineRecords, 'unfinishedRuns'>;
89
+ readonly versionOf: (workflow: string) => WorkflowVersion;
90
+ /** A workflow's steps, in their declared order. */
91
+ readonly stepsOf: (workflow: string) => ReadonlyArray<string>;
92
+ }) => {
93
+ const held = new Map<string, Held>();
94
+ const previous = new Map<string, { readonly version: string; readonly at: number; readonly held: Held } | null>();
95
+
96
+ const hold = (r: { readonly workflow: string; readonly version: string }): Effect.Effect<Held, StorageError> =>
97
+ Effect.map(deps.records.versionRecords(r), (read) => ({
98
+ version: r.version,
99
+ records: new Map(read.records.map((x) => [x.id, x])),
100
+ parts: groupParts(read.parts),
101
+ newestId: read.newestId,
102
+ }));
103
+
104
+ /** Reads the records written since, and those still open again, into what is held. */
105
+ const refresh = (workflow: string, h: Held): Effect.Effect<void, StorageError> => Effect.gen(function* () {
106
+ const open = [...h.records.values()].filter((x) => x.ended === null).map((x) => x.id);
107
+ const read = yield* deps.records.recordsSince({ after: h.newestId, again: open });
108
+ for (const x of read.records) {
109
+ h.newestId = Math.max(h.newestId, x.id);
110
+ if (x.workflow === workflow && x.version === h.version) h.records.set(x.id, x);
111
+ }
112
+ for (const [id, parts] of groupParts(read.parts)) h.parts.set(id, parts);
113
+ });
114
+
115
+ const valuesOf = (workflow: string, h: Held, unfinished: ReadonlySet<string>): ReadonlyArray<StepValues> => {
116
+ const byStep = taskTimes({ records: [...h.records.values()], parts: [...h.parts.values()].flat(), unfinished });
117
+ const declared = deps.stepsOf(workflow);
118
+ return [...declared, ...[...byStep.keys()].filter((s) => !declared.includes(s))].map((step) => ({ step, values: byStep.get(step) ?? [] }));
119
+ };
120
+
121
+ /** A workflow's step times now: its current version's values, and the previous version's where the current has none. */
122
+ const timesOf = (workflow: string): Effect.Effect<StepTimes, StorageError> => Effect.gen(function* () {
123
+ const current = deps.versionOf(workflow);
124
+ const known = held.get(workflow);
125
+ const h = known !== undefined && known.version === current.version ? known : yield* hold({ workflow, version: current.version });
126
+ if (h !== known) held.set(workflow, h);
127
+ else yield* refresh(workflow, h);
128
+ const unfinished = new Set((yield* deps.engine.unfinishedRuns()).map((u) => u.run));
129
+ const steps = valuesOf(workflow, h, unfinished);
130
+ if (steps.some((s) => s.values.length > 0)) return { workflow, version: current.version, since: current.since, steps, previous: null };
131
+ if (!previous.has(workflow)) {
132
+ const before = yield* deps.records.previousVersion({ workflow, version: current.version });
133
+ previous.set(workflow, before === null ? null : { ...before, held: yield* hold({ workflow, version: before.version }) });
134
+ }
135
+ const before = previous.get(workflow) ?? null;
136
+ return {
137
+ workflow, version: current.version, since: current.since, steps,
138
+ previous: before === null ? null : { version: before.version, at: before.at, steps: valuesOf(workflow, before.held, unfinished) },
139
+ };
140
+ });
141
+
142
+ return { timesOf };
143
+ };
144
+
145
+ const groupParts = (parts: ReadonlyArray<TimedPart>): Map<number, ReadonlyArray<TimedPart>> => {
146
+ const out = new Map<number, TimedPart[]>();
147
+ for (const p of parts) out.set(p.stepRun, [...(out.get(p.stepRun) ?? []), p]);
148
+ return out;
149
+ };
150
+
151
+ export type StepTimesReader = ReturnType<typeof makeStepTimes>;