@jive-sh/workflow-platform 0.3.3 → 0.5.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 +68 -23
- package/dist/dashboard/agent-usage.js +25 -0
- package/dist/dashboard/agent-usage.js.map +1 -0
- package/dist/dashboard/drilldown.js +16 -0
- package/dist/dashboard/drilldown.js.map +1 -0
- package/dist/dashboard/queue-lines.js +44 -0
- package/dist/dashboard/queue-lines.js.map +1 -0
- package/dist/index.js +8 -6
- package/dist/index.js.map +1 -1
- package/dist/keep-running.js +1 -1
- package/dist/keep-running.js.map +1 -1
- package/dist/pick-wakes.js +4 -4
- package/dist/pick-wakes.js.map +1 -1
- package/dist/queue-counts.js +7 -9
- package/dist/queue-counts.js.map +1 -1
- package/dist/steps.js +157 -41
- package/dist/steps.js.map +1 -1
- package/dist/storage/sqlite-store.js +117 -38
- package/dist/storage/sqlite-store.js.map +1 -1
- package/dist/storage/task-store.js +14 -1
- package/dist/storage/task-store.js.map +1 -1
- package/dist/tasks.js +215 -267
- package/dist/tasks.js.map +1 -1
- package/dist/views/runs-now.js +3 -2
- package/dist/views/runs-now.js.map +1 -1
- package/dist/views/step-funnel.js +1 -1
- package/dist/views/step-funnel.js.map +1 -1
- package/dist/views/step-graph.js +1 -1
- package/dist/views/step-graph.js.map +1 -1
- package/dist/views/workflow-flows.js +2 -2
- package/dist/views/workflow-flows.js.map +1 -1
- package/dist/views/workflows-view.js +83 -17
- package/dist/views/workflows-view.js.map +1 -1
- package/package.json +1 -1
- package/src/dashboard/agent-usage-line.tsx +28 -0
- package/src/dashboard/agent-usage.ts +48 -0
- package/src/dashboard/dashboard.css +70 -29
- package/src/dashboard/drilldown.ts +43 -0
- package/src/dashboard/index.tsx +1 -0
- package/src/dashboard/machine-controls.tsx +53 -0
- package/src/dashboard/queue-bar.tsx +43 -0
- package/src/dashboard/queue-chart.tsx +89 -76
- package/src/dashboard/queue-lines.ts +87 -0
- package/src/dashboard/step-graph.tsx +18 -10
- package/src/dashboard/workflow-dag.tsx +31 -103
- package/src/dashboard/workflow-drilldown.tsx +89 -0
- package/src/dashboard/workflows-dashboard.tsx +117 -42
- package/src/index.ts +11 -9
- package/src/keep-running.ts +3 -3
- package/src/pick-wakes.ts +14 -9
- package/src/queue-counts.ts +10 -14
- package/src/steps.ts +213 -52
- package/src/storage/sqlite-store.ts +126 -39
- package/src/storage/step-records.ts +14 -3
- package/src/storage/task-store.ts +97 -22
- package/src/tasks.ts +235 -301
- package/src/views/runs-now.ts +5 -4
- package/src/views/step-funnel.ts +2 -2
- package/src/views/step-graph.ts +4 -4
- package/src/views/workflow-flows.ts +7 -7
- package/src/views/workflows-view.ts +117 -35
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
stepErrorKept, type FunnelStepRecord, type ItemCost, type ItemHistoryRow, type LastStepNow, type NewestStepRecord, type RunCost, type RunOutcomeRow, type StepEdgeTaken,
|
|
12
12
|
type StepFileCount, type StepRecords, type StepRunRow, type UndeclaredWrites, type WorkStep, AGENT_LIVE_SECONDS,
|
|
13
13
|
} from './step-records';
|
|
14
|
-
import type
|
|
14
|
+
import { LISTED_AGAIN, MOVED_TO_FRONT, STATUS_KINDS, STOOD_DOWN_OUTCOME, TRIES_START_AGAIN_KINDS, type Listed, type ListedWait, type NewNote, type QueueCount, type ReturnedBetween, type TaskNote, type TaskRef, type TaskState, type TaskStore } from './task-store';
|
|
15
15
|
import type { InputRecords, InputRun } from './input-records';
|
|
16
16
|
import { storeCall, storeCalls } from './storage-error';
|
|
17
17
|
|
|
@@ -165,7 +165,7 @@ CREATE INDEX IF NOT EXISTS queue_counts_by_at ON queue_counts (at);`;
|
|
|
165
165
|
* that applies the schema only when it must (a write takes the lock) asks this first.
|
|
166
166
|
*/
|
|
167
167
|
export const stepRecordsSchemaApplied = (db: Database): boolean => {
|
|
168
|
-
const newest = ['step_runs', 'queue_counts', 'step_runs_by_workflow'];
|
|
168
|
+
const newest = ['step_runs', 'queue_counts', 'step_runs_by_workflow', 'step_runs_by_child'];
|
|
169
169
|
const present = new Set((db.query('SELECT name FROM sqlite_master').all() as Array<{ name: string }>).map((r) => r.name));
|
|
170
170
|
return newest.every((name) => present.has(name));
|
|
171
171
|
};
|
|
@@ -193,6 +193,9 @@ export const applyStepRecordsSchema = (db: Database): void => {
|
|
|
193
193
|
if (!columns.includes('owner_started_at')) db.exec('ALTER TABLE step_runs ADD COLUMN owner_started_at REAL');
|
|
194
194
|
// Why it ended as it did, in a line (`steps.ts` `reasonOf`): what the platform counts outcomes by.
|
|
195
195
|
if (!columns.includes('reason')) db.exec('ALTER TABLE step_runs ADD COLUMN reason TEXT');
|
|
196
|
+
// The run a sub-workflow step started (`steps.ts` `defineSubWorkflow`): what ties that run to the run above it.
|
|
197
|
+
if (!columns.includes('child_run')) db.exec('ALTER TABLE step_runs ADD COLUMN child_run TEXT');
|
|
198
|
+
db.exec('CREATE INDEX IF NOT EXISTS step_runs_by_child ON step_runs (child_run) WHERE child_run IS NOT NULL');
|
|
196
199
|
db.exec(STEP_ITEMS_SCHEMA);
|
|
197
200
|
db.exec(UNDECLARED_WRITES_SCHEMA);
|
|
198
201
|
db.exec(INPUT_RUNS_SCHEMA);
|
|
@@ -283,6 +286,56 @@ const noteOf = (r: NoteRow): TaskNote => ({
|
|
|
283
286
|
change: r.before_sha === null || r.after_sha === null ? null : { beforeSha: r.before_sha, afterSha: r.after_sha, flags: r.flags ?? '', diff: r.diff ?? '' },
|
|
284
287
|
});
|
|
285
288
|
|
|
289
|
+
const quoted = (kinds: ReadonlyArray<string>): string => kinds.map((k) => `'${k.replace(/'/g, "''")}'`).join(', ');
|
|
290
|
+
/** A note a task's status is read from (`task-store.ts` `STATUS_KINDS`): an outcome of a run that did not stand down, or one of the kinds. */
|
|
291
|
+
const IS_STATUS_NOTE = `((n.kind = 'outcome' AND n.why <> '${STOOD_DOWN_OUTCOME}') OR n.kind IN (${quoted(Object.keys(STATUS_KINDS))}))`;
|
|
292
|
+
/** A note after which a task's failed tries are counted again: its final check passing, or a return to its queue. */
|
|
293
|
+
const STARTS_TRIES_AGAIN = `((n.kind = 'outcome' AND n.finished = 1) OR n.kind IN (${quoted(TRIES_START_AGAIN_KINDS)}))`;
|
|
294
|
+
|
|
295
|
+
interface StateRow {
|
|
296
|
+
readonly id: number; readonly workflow: string; readonly item: string; readonly listed: string | null; readonly created_at: number;
|
|
297
|
+
readonly note: number | null; readonly front: number | null; readonly kind: string | null; readonly why: string | null; readonly at: number | null;
|
|
298
|
+
readonly finished: number | null; readonly inputs: string | null; readonly about_workflow: string | null; readonly about_item: string | null;
|
|
299
|
+
readonly tries: number; readonly changes: number; readonly start_kind: string | null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* EVERY TASK MATCHING `where` AS ITS NOTES LEAVE IT, in one query: its newest status note, its failed tries since they last
|
|
304
|
+
* started again, and whether the reorder call moved it since. Nothing stores a status; it is read from the notes each time
|
|
305
|
+
* (0.3 s for a project's 20,000 tasks and 55,000 notes, 2026-11-19).
|
|
306
|
+
*/
|
|
307
|
+
const statesWhere = (db: Database, where: string, params: ReadonlyArray<string | number>): ReadonlyArray<TaskState> =>
|
|
308
|
+
(db.query(`
|
|
309
|
+
WITH s AS (
|
|
310
|
+
SELECT t.id AS id,
|
|
311
|
+
(SELECT MAX(n.id) FROM task_notes n WHERE n.task = t.id AND ${IS_STATUS_NOTE}) AS note,
|
|
312
|
+
(SELECT MAX(n.id) FROM task_notes n WHERE n.task = t.id AND ${STARTS_TRIES_AGAIN}) AS start,
|
|
313
|
+
(SELECT MAX(n.id) FROM task_notes n WHERE n.task = t.id AND n.kind = '${MOVED_TO_FRONT}') AS front
|
|
314
|
+
FROM tasks t WHERE ${where})
|
|
315
|
+
SELECT t.id, t.workflow, t.item, t.listed, t.created_at, s.note, s.front, n.kind, n.why, n.at, n.finished, n.inputs,
|
|
316
|
+
a.workflow AS about_workflow, a.item AS about_item,
|
|
317
|
+
(SELECT COUNT(*) FROM task_notes o WHERE o.task = t.id AND o.kind = 'outcome' AND o.finished = 0 AND o.why <> '${STOOD_DOWN_OUTCOME}' AND o.id > COALESCE(s.start, 0)) AS tries,
|
|
318
|
+
(SELECT COUNT(*) FROM task_notes c WHERE c.task = t.id AND c.before_sha IS NOT NULL) AS changes,
|
|
319
|
+
(SELECT k.kind FROM task_notes k WHERE k.id = s.start) AS start_kind
|
|
320
|
+
FROM s JOIN tasks t ON t.id = s.id LEFT JOIN task_notes n ON n.id = s.note LEFT JOIN tasks a ON a.id = n.about
|
|
321
|
+
ORDER BY t.id`).all(...params) as ReadonlyArray<StateRow>).map((r): TaskState => ({
|
|
322
|
+
id: r.id, ref: { workflow: r.workflow, item: r.item }, listed: r.listed === null ? null : (JSON.parse(r.listed) as Listed),
|
|
323
|
+
status: r.kind === null ? 'queued' : r.kind === 'outcome' ? (r.finished === 1 ? 'done' : 'queued') : STATUS_KINDS[r.kind]!,
|
|
324
|
+
why: r.why, kind: r.kind, since: r.at ?? r.created_at, note: r.note ?? 0,
|
|
325
|
+
front: r.front !== null && r.front > (r.note ?? 0) ? r.front : null,
|
|
326
|
+
tries: r.tries, inputs: r.inputs,
|
|
327
|
+
about: r.about_workflow === null || r.about_item === null ? null : { workflow: r.about_workflow, item: r.about_item },
|
|
328
|
+
changes: r.changes,
|
|
329
|
+
returned: r.start_kind !== null && TRIES_START_AGAIN_KINDS.includes(r.start_kind),
|
|
330
|
+
}));
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* WHETHER A TASK IS ITS WORKFLOW'S NOW: on its list; or done; or put back in its queue by a step or a person and not
|
|
334
|
+
* finished since (a task another needs redone is work whatever its list says). Never one `removed`.
|
|
335
|
+
*/
|
|
336
|
+
const isItsWorkflowsNow = (s: TaskState): boolean =>
|
|
337
|
+
s.status !== 'removed' && (s.listed !== null || s.status === 'done' || s.returned);
|
|
338
|
+
|
|
286
339
|
/** A task's id, made the first time it is named. */
|
|
287
340
|
const taskIdIn = (db: Database, task: TaskRef, now: number): number => {
|
|
288
341
|
db.query('INSERT OR IGNORE INTO tasks (workflow, item, item_key, created_at) VALUES (?, ?, ?, ?)').run(task.workflow, task.item, keyOf(task.item), now);
|
|
@@ -417,7 +470,15 @@ export const sqliteStore = (options: {
|
|
|
417
470
|
const stepRecords: StepRecords = storeCalls<StepRecords>('step records', {
|
|
418
471
|
openStepRun: (r) => tryWrite((db) => {
|
|
419
472
|
const now = Date.now() / 1000;
|
|
420
|
-
|
|
473
|
+
if (r.resume === true) {
|
|
474
|
+
const open = db.query('SELECT id FROM step_runs WHERE run = ? AND step = ? AND call IS ? AND ended IS NULL ORDER BY id DESC LIMIT 1').get(r.run, r.step, r.call) as { id: number } | null;
|
|
475
|
+
if (open !== null) return open.id;
|
|
476
|
+
}
|
|
477
|
+
// What it works on: named, or what its run last worked on, or what the step that started its run as a sub-workflow works on.
|
|
478
|
+
const inherited = r.subject
|
|
479
|
+
?? (db.query('SELECT subject FROM step_runs WHERE run = ? AND subject IS NOT NULL ORDER BY id DESC LIMIT 1').get(r.run) as { subject: string } | null)?.subject
|
|
480
|
+
?? (db.query('SELECT subject FROM step_runs WHERE child_run = ? AND subject IS NOT NULL ORDER BY id DESC LIMIT 1').get(r.run) as { subject: string } | null)?.subject
|
|
481
|
+
?? null;
|
|
421
482
|
const owner = ownerOfNewRecords();
|
|
422
483
|
return Number(db.query(
|
|
423
484
|
'INSERT INTO step_runs (workflow, run, step, call, agent, started, beat, subject, owner_pid, owner_started_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
|
|
@@ -485,8 +546,25 @@ export const sqliteStore = (options: {
|
|
|
485
546
|
.get(run) as { step: string; outcome: string | null; owner_pid: number | null; owner_started_at: number | null } | null;
|
|
486
547
|
return r === null ? null : { step: r.step, failed: r.outcome === 'failure', owner: r.owner_pid === null || r.owner_started_at === null ? null : { pid: r.owner_pid, startedAt: r.owner_started_at } };
|
|
487
548
|
}),
|
|
549
|
+
setStepChild: (r) => {
|
|
550
|
+
tryWrite((db) => db.query('UPDATE step_runs SET child_run = ? WHERE id = ?').run(r.childRun, r.id));
|
|
551
|
+
},
|
|
552
|
+
rootOfRun: (run) =>
|
|
553
|
+
read((db) => db.query(
|
|
554
|
+
`WITH RECURSIVE up(run, workflow, depth) AS (
|
|
555
|
+
SELECT p.run, p.workflow, 1 FROM step_runs p WHERE p.child_run = ?1
|
|
556
|
+
UNION SELECT p.run, p.workflow, u.depth + 1 FROM up u JOIN step_runs p ON p.child_run = u.run WHERE u.depth < 32)
|
|
557
|
+
SELECT run, workflow FROM up ORDER BY depth DESC LIMIT 1`,
|
|
558
|
+
).get(run) as { run: string; workflow: string } | null, null),
|
|
488
559
|
runsInAgentStep: (workflow) =>
|
|
489
|
-
|
|
560
|
+
// Each open agent step, then the run that started its run as a sub-workflow, and so on up: every one of them that is a
|
|
561
|
+
// run of this workflow has an agent working for it.
|
|
562
|
+
read((db) => (db.query(
|
|
563
|
+
`WITH RECURSIVE up(run, depth) AS (
|
|
564
|
+
SELECT s.run, 0 FROM step_runs s WHERE s.ended IS NULL AND s.agent = 1 AND s.waiting IS NULL
|
|
565
|
+
UNION SELECT p.run, u.depth + 1 FROM up u JOIN step_runs p ON p.child_run = u.run WHERE u.depth < 32)
|
|
566
|
+
SELECT DISTINCT u.run AS run FROM up u WHERE EXISTS (SELECT 1 FROM step_runs r WHERE r.run = u.run AND r.workflow = ?)`,
|
|
567
|
+
).all(workflow) as ReadonlyArray<{ run: string }>).map((r) => r.run), []),
|
|
490
568
|
stepOfRun: (run) =>
|
|
491
569
|
read((db) => db.query('SELECT step, subject, (SELECT MIN(started) FROM step_runs s WHERE s.run = ?1) AS started FROM step_runs WHERE run = ?1 ORDER BY id DESC LIMIT 1').get(run) as { step: string; subject: string | null; started: number } | null, null),
|
|
492
570
|
subjectOfRun: (run) =>
|
|
@@ -619,19 +697,6 @@ export const sqliteStore = (options: {
|
|
|
619
697
|
}, { edges: [], lasts: [] }),
|
|
620
698
|
});
|
|
621
699
|
|
|
622
|
-
/** The tasks matching `where`, each with its notes. */
|
|
623
|
-
const factsWhere = (where: string, params: ReadonlyArray<string | number>): ReadonlyArray<TaskFacts> =>
|
|
624
|
-
read((db) => {
|
|
625
|
-
const tasks = db.query(`SELECT id, workflow, item, listed FROM tasks t WHERE ${where}`).all(...params) as ReadonlyArray<{ id: number; workflow: string; item: string; listed: string | null }>;
|
|
626
|
-
if (tasks.length === 0) return [];
|
|
627
|
-
const notes = new Map<number, TaskNote[]>();
|
|
628
|
-
for (const r of db.query(`${NOTE_SELECT} WHERE n.task IN (SELECT id FROM tasks t WHERE ${where}) ORDER BY n.id`).all(...params) as ReadonlyArray<NoteRow>) {
|
|
629
|
-
const list = notes.get(r.task_id);
|
|
630
|
-
if (list === undefined) notes.set(r.task_id, [noteOf(r)]); else list.push(noteOf(r));
|
|
631
|
-
}
|
|
632
|
-
return tasks.map((t) => ({ id: t.id, ref: { workflow: t.workflow, item: t.item }, listed: t.listed === null ? null : (JSON.parse(t.listed) as Listed), notes: notes.get(t.id) ?? [] }));
|
|
633
|
-
}, []);
|
|
634
|
-
|
|
635
700
|
/** A write in one transaction; it throws, for the caller to say what it could not record. */
|
|
636
701
|
const inTransaction = <A>(write: (db: Database) => A): A => {
|
|
637
702
|
const db = writer();
|
|
@@ -647,31 +712,53 @@ export const sqliteStore = (options: {
|
|
|
647
712
|
appendOutcome: (r) => storeCall('tasks: appendOutcome', () => inTransaction((db) => {
|
|
648
713
|
const now = Date.now() / 1000;
|
|
649
714
|
if (appendIn(db, { task: r.task, by: r.by, kind: 'outcome', why: r.why, finished: r.finished, ...(r.inputs === null ? {} : { inputs: r.inputs }) }, now) === null) return { appended: false, resumed: [] };
|
|
715
|
+
if (r.blocked !== null) appendIn(db, { task: r.task, by: r.by, kind: 'blocked', why: r.blocked, ...(r.inputs === null ? {} : { inputs: r.inputs }) }, now);
|
|
650
716
|
if (!r.finished) return { appended: true, resumed: [] };
|
|
717
|
+
// Every task whose block, still in force, waits on this one goes back to its queue.
|
|
651
718
|
const id = taskIdIn(db, r.task, now);
|
|
652
|
-
const waiters = db.
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
).all(id) as ReadonlyArray<TaskRef>;
|
|
657
|
-
for (const w of waiters) {
|
|
658
|
-
const why = r.resumedWhy(w);
|
|
659
|
-
appendIn(db, { task: w, by: r.by, kind: 'resumed', why: why.resumed, about: r.task }, now);
|
|
660
|
-
appendIn(db, { task: r.task, by: r.by, kind: 'waiter resumed', why: why.waiterResumed, about: w }, now);
|
|
661
|
-
}
|
|
719
|
+
const waiters = statesWhere(db, `t.id IN (SELECT n.task FROM task_notes n WHERE n.about = ?)`, [id])
|
|
720
|
+
.filter((w) => w.status === 'blocked' && w.about !== null && w.about.workflow === r.task.workflow && keyOf(w.about.item) === keyOf(r.task.item))
|
|
721
|
+
.map((w) => w.ref);
|
|
722
|
+
for (const w of waiters) appendIn(db, { task: w, by: r.by, kind: 'queued', why: r.resumedWhy(w), about: r.task }, now);
|
|
662
723
|
return { appended: true, resumed: waiters };
|
|
663
724
|
})),
|
|
664
|
-
recordListing: ({ workflow, listed }) => storeCall('tasks: recordListing', () => inTransaction((db) => {
|
|
725
|
+
recordListing: ({ workflow, listed, everyItem, readAt, by }) => storeCall('tasks: recordListing', () => inTransaction((db) => {
|
|
665
726
|
const now = Date.now() / 1000;
|
|
666
|
-
const
|
|
667
|
-
const
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
for (const l of listed)
|
|
671
|
-
|
|
727
|
+
const before = new Map(statesWhere(db, 't.workflow = ?', [workflow]).map((s) => [keyOf(s.ref.item), s]));
|
|
728
|
+
const setListed = db.query('UPDATE tasks SET listed = ?, listed_at = ? WHERE id = ?');
|
|
729
|
+
const listedNow = new Set<string>();
|
|
730
|
+
let fresh = false;
|
|
731
|
+
for (const l of listed) {
|
|
732
|
+
const key = keyOf(l.item);
|
|
733
|
+
if (listedNow.has(key)) continue;
|
|
734
|
+
listedNow.add(key);
|
|
735
|
+
const was = before.get(key);
|
|
736
|
+
const text = JSON.stringify(l.listed);
|
|
737
|
+
if (was === undefined) {
|
|
738
|
+
setListed.run(text, now, taskIdIn(db, { workflow, item: l.item }, now));
|
|
739
|
+
fresh = true;
|
|
740
|
+
continue;
|
|
741
|
+
}
|
|
742
|
+
const wasGone = !isItsWorkflowsNow(was);
|
|
743
|
+
// A list read before the task finished still held it: only one read since says it is work again.
|
|
744
|
+
const workAgain = !everyItem && was.status === 'done' && l.listed.kind === 'line' && was.since < readAt;
|
|
745
|
+
if (wasGone || workAgain) {
|
|
746
|
+
appendIn(db, { task: was.ref, by, kind: LISTED_AGAIN, why: wasGone ? 'its workflow lists it again' : 'its workflow lists it as work again' }, now);
|
|
747
|
+
fresh = true;
|
|
748
|
+
}
|
|
749
|
+
if (was.listed === null || JSON.stringify(was.listed) !== text) setListed.run(text, now, was.id);
|
|
750
|
+
}
|
|
751
|
+
const clearListed = db.query('UPDATE tasks SET listed = NULL WHERE id = ?');
|
|
752
|
+
for (const [key, was] of before) {
|
|
753
|
+
if (listedNow.has(key)) continue;
|
|
754
|
+
if (was.listed !== null) clearListed.run(was.id);
|
|
755
|
+
if (everyItem && was.status === 'done') appendIn(db, { task: was.ref, by, kind: 'removed', why: 'its workflow no longer has this item' }, now);
|
|
756
|
+
}
|
|
757
|
+
return { fresh };
|
|
672
758
|
})),
|
|
673
|
-
|
|
674
|
-
|
|
759
|
+
statesOf: (workflow) => storeCall('tasks: statesOf', () =>
|
|
760
|
+
read((db) => statesWhere(db, 't.workflow = ?', [workflow]).filter(isItsWorkflowsNow), [])),
|
|
761
|
+
state: (task) => storeCall('tasks: state', () => read((db) => statesWhere(db, 't.workflow = ? AND t.item_key = ?', [task.workflow, keyOf(task.item)])[0] ?? null, null)),
|
|
675
762
|
notes: (task) => storeCall('tasks: notes', () =>
|
|
676
763
|
read((db) => (db.query(`${NOTE_SELECT} WHERE t.workflow = ? AND t.item_key = ? ORDER BY n.id`).all(task.workflow, keyOf(task.item)) as ReadonlyArray<NoteRow>).map(noteOf), [])),
|
|
677
764
|
taskId: (task) => storeCall('tasks: taskId', () =>
|
|
@@ -692,11 +779,11 @@ export const sqliteStore = (options: {
|
|
|
692
779
|
WHERE at >= ?1 OR at = (SELECT MAX(p.at) FROM queue_counts p WHERE p.workflow = q.workflow AND p.bucket = q.bucket AND p.at < ?1)
|
|
693
780
|
ORDER BY at`,
|
|
694
781
|
).all(since) as ReadonlyArray<QueueCount>, [])),
|
|
695
|
-
|
|
782
|
+
returnedBetween: () => storeCall('tasks: returnedBetween', () =>
|
|
696
783
|
read((db) => db.query(
|
|
697
784
|
`SELECT n.by_workflow AS "from", t.workflow AS "to", COUNT(*) AS notes, MAX(n.at) AS last FROM task_notes n JOIN tasks t ON t.id = n.task
|
|
698
|
-
WHERE n.kind
|
|
699
|
-
).all() as ReadonlyArray<
|
|
785
|
+
WHERE n.kind IN (${TRIES_START_AGAIN_KINDS.map((k) => `'${k}'`).join(', ')}) AND n.by_workflow IS NOT NULL AND n.by_workflow <> t.workflow GROUP BY 1, 2 ORDER BY 1, 2`,
|
|
786
|
+
).all() as ReadonlyArray<ReturnedBetween>, [])),
|
|
700
787
|
listedWaits: () => storeCall('tasks: listedWaits', () =>
|
|
701
788
|
read((db) => db.query(
|
|
702
789
|
`SELECT workflow, json_extract(listed, '$.on') AS "on", json_extract(listed, '$.label') AS label, COUNT(*) AS tasks FROM tasks
|
|
@@ -217,8 +217,16 @@ export interface LastStepNow {
|
|
|
217
217
|
|
|
218
218
|
export interface StepRecords {
|
|
219
219
|
// ------------------------------------------------------------------ written by the step definer
|
|
220
|
-
/**
|
|
221
|
-
|
|
220
|
+
/**
|
|
221
|
+
* Opens a step's record. It works on what `subject` names, else on what the run last worked on, else on what the run
|
|
222
|
+
* that started it as a sub-workflow works on. `resume`: the run's open record of this step and call is taken up again
|
|
223
|
+
* where there is one (a sub-workflow step, run again when its workflow has ended). Null where nothing is written.
|
|
224
|
+
*/
|
|
225
|
+
readonly openStepRun: (r: { readonly workflow: string; readonly run: string; readonly step: string; readonly call: number | null; readonly agent: boolean; readonly subject: string | null; readonly resume?: true }) => Effect.Effect<number | null, StorageError>;
|
|
226
|
+
/** The run a sub-workflow step started, on that step's open record: what ties a run to the run above it. */
|
|
227
|
+
readonly setStepChild: (r: { readonly id: number; readonly childRun: string }) => Effect.Effect<void, StorageError>;
|
|
228
|
+
/** The run at the top of the runs that started this one as a sub-workflow, with its workflow; none for a run nothing started so. */
|
|
229
|
+
readonly rootOfRun: (run: string) => Effect.Effect<{ readonly run: string; readonly workflow: string } | null, StorageError>;
|
|
222
230
|
readonly beatStepRun: (id: number) => Effect.Effect<void, StorageError>;
|
|
223
231
|
/** What an agent step has spent so far, on its open record, as each usage arrives. */
|
|
224
232
|
readonly updateStepSpend: (r: { readonly id: number; readonly spend: StepSpend; readonly model: string | null }) => Effect.Effect<void, StorageError>;
|
|
@@ -249,7 +257,10 @@ export interface StepRecords {
|
|
|
249
257
|
readonly stepFilesOf: (r: { readonly run: string; readonly step: string }) => Effect.Effect<string | null, StorageError>;
|
|
250
258
|
/** The step a run did last, and whether it failed; none before its first. A step a restart interrupted is not one. */
|
|
251
259
|
readonly lastStepOf: (run: string) => Effect.Effect<{ readonly step: string; readonly failed: boolean; readonly owner: ProcessIdentity | null } | null, StorageError>;
|
|
252
|
-
/**
|
|
260
|
+
/**
|
|
261
|
+
* The runs of a workflow an agent works for now: each with an agent step's record open and not waiting, its own or that
|
|
262
|
+
* of a run under it (one a sub-workflow step of it started, at any depth). The agents it has in use.
|
|
263
|
+
*/
|
|
253
264
|
readonly runsInAgentStep: (workflow: string) => Effect.Effect<ReadonlyArray<string>, StorageError>;
|
|
254
265
|
/** The step a run is on now, and what it works on. */
|
|
255
266
|
readonly stepOfRun: (run: string) => Effect.Effect<{ readonly step: string; readonly subject: string | null; readonly started: number } | null, StorageError>;
|
|
@@ -62,21 +62,64 @@ export interface NewNote {
|
|
|
62
62
|
readonly change?: TextChange;
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
/**
|
|
65
|
+
/**
|
|
66
|
+
* Where a workflow's list last put a task: on one of its lines, or among the items it says no run should start on, with
|
|
67
|
+
* the reason (`label`) and what it waits for (`on`).
|
|
68
|
+
*/
|
|
66
69
|
export type Listed =
|
|
67
|
-
|
|
68
|
-
| { readonly kind: 'line'; readonly label: string; readonly position?: number; readonly last?: true }
|
|
70
|
+
| { readonly kind: 'line'; readonly label: string }
|
|
69
71
|
| { readonly kind: 'wait'; readonly label: string; readonly on: string };
|
|
70
72
|
|
|
71
|
-
/**
|
|
72
|
-
|
|
73
|
+
/**
|
|
74
|
+
* WHERE A TASK'S NOTES LEAVE IT (`tasks.ts`, where the rule is written down): the newest of its outcomes, its blocks and
|
|
75
|
+
* its returns to the queue decides. `removed`: its workflow no longer has the item.
|
|
76
|
+
*/
|
|
77
|
+
export type NotedStatus = 'queued' | 'blocked' | 'done' | 'removed';
|
|
78
|
+
|
|
79
|
+
/** The note kinds a status is read from, and what each leaves a task as. A run that stood down wrote no such note. */
|
|
80
|
+
export const STATUS_KINDS: Readonly<Record<string, NotedStatus>> = {
|
|
81
|
+
queued: 'queued', unblocked: 'queued', 'listed again': 'queued', blocked: 'blocked', removed: 'removed',
|
|
82
|
+
// Kinds written before 2026-11-19, read as what they did: each put its task back in its queue, or held it out.
|
|
83
|
+
reopened: 'queued', 'new input': 'queued', resumed: 'queued', released: 'queued', held: 'blocked', 'waits on': 'blocked',
|
|
84
|
+
};
|
|
85
|
+
/** The kinds after which a task's count of failed tries starts again: every return to the queue but a lapsed block's. */
|
|
86
|
+
export const TRIES_START_AGAIN_KINDS: ReadonlyArray<string> = ['queued', 'reopened', 'new input', 'resumed', 'released'];
|
|
87
|
+
/** The outcome of a run that stood down for its workflow's limits: no agent was called, so it is no try and no status. */
|
|
88
|
+
export const STOOD_DOWN_OUTCOME = 'stood down';
|
|
89
|
+
/** The kind a reading of its workflow's list writes on a task it holds again: back in its queue, its failed tries still counted. */
|
|
90
|
+
export const LISTED_AGAIN = 'listed again';
|
|
91
|
+
/** The kind the reorder call writes: its task goes ahead of the queue until a run takes it. */
|
|
92
|
+
export const MOVED_TO_FRONT = 'moved to front';
|
|
93
|
+
|
|
94
|
+
/** A task as its queue reads it, from one query: no note is read in code. */
|
|
95
|
+
export interface TaskState {
|
|
73
96
|
readonly id: number;
|
|
74
97
|
readonly ref: TaskRef;
|
|
75
98
|
readonly listed: Listed | null;
|
|
76
|
-
readonly
|
|
99
|
+
readonly status: NotedStatus;
|
|
100
|
+
/** Why, in the words of the note that left it there; none for a task no note has moved. */
|
|
101
|
+
readonly why: string | null;
|
|
102
|
+
/** The kind of that note; none for a task no note has moved. */
|
|
103
|
+
readonly kind: string | null;
|
|
104
|
+
/** When it came to that status, unix seconds: its note's time, or when it was made. */
|
|
105
|
+
readonly since: number;
|
|
106
|
+
/** That note's id: what orders two tasks of one moment. 0 with none. */
|
|
107
|
+
readonly note: number;
|
|
108
|
+
/** The id of its `moved to front` note written since it came to that status; none otherwise. */
|
|
109
|
+
readonly front: number | null;
|
|
110
|
+
/** Failed tries since its count last started again. */
|
|
111
|
+
readonly tries: number;
|
|
112
|
+
/** The fingerprint of its inputs that note recorded (an outcome's, a block's); none where it recorded none. */
|
|
113
|
+
readonly inputs: string | null;
|
|
114
|
+
/** The other task that note is about: the one a blocked task waits on. */
|
|
115
|
+
readonly about: TaskRef | null;
|
|
116
|
+
/** Whether a step or a person put it back in its queue (`TRIES_START_AGAIN_KINDS`), and its final check has not passed since. */
|
|
117
|
+
readonly returned: boolean;
|
|
118
|
+
/** Notes on it that changed its text: what the flipping rule reads only where there are enough to flip. */
|
|
119
|
+
readonly changes: number;
|
|
77
120
|
}
|
|
78
121
|
|
|
79
|
-
/** How many of a workflow's tasks stood in one
|
|
122
|
+
/** How many of a workflow's tasks stood in one status from `at` on, unix seconds. */
|
|
80
123
|
export interface QueueCount {
|
|
81
124
|
readonly at: number;
|
|
82
125
|
readonly workflow: string;
|
|
@@ -84,8 +127,11 @@ export interface QueueCount {
|
|
|
84
127
|
readonly tasks: number;
|
|
85
128
|
}
|
|
86
129
|
|
|
87
|
-
/**
|
|
88
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Tasks of one workflow that a step of another returned to their queue (`tasks.ts` `requeue`, `reopen`): how many notes,
|
|
132
|
+
* the last when.
|
|
133
|
+
*/
|
|
134
|
+
export interface ReturnedBetween {
|
|
89
135
|
readonly from: string;
|
|
90
136
|
readonly to: string;
|
|
91
137
|
readonly notes: number;
|
|
@@ -100,6 +146,12 @@ export interface ListedWait {
|
|
|
100
146
|
readonly tasks: number;
|
|
101
147
|
}
|
|
102
148
|
|
|
149
|
+
/** What a reading of a workflow's list changed. */
|
|
150
|
+
export interface ListingRecorded {
|
|
151
|
+
/** Whether it put any task in the queue: one new to the list, or one it lists again. */
|
|
152
|
+
readonly fresh: boolean;
|
|
153
|
+
}
|
|
154
|
+
|
|
103
155
|
export interface TaskStore {
|
|
104
156
|
/**
|
|
105
157
|
* Appends notes in one transaction, in order; each one's id, or null for an outcome a run already has (a resumed run ends
|
|
@@ -107,29 +159,52 @@ export interface TaskStore {
|
|
|
107
159
|
*/
|
|
108
160
|
readonly append: (notes: ReadonlyArray<NewNote>) => Effect.Effect<ReadonlyArray<number | null>, StorageError>;
|
|
109
161
|
/**
|
|
110
|
-
* In one transaction: appends a run's outcome on its task
|
|
111
|
-
*
|
|
162
|
+
* In one transaction: appends a run's outcome on its task; where the run's step blocked it, a `blocked` note after it;
|
|
163
|
+
* and, when the outcome is new and finished, a `queued` note on every task blocked waiting on it. Returns whether the
|
|
164
|
+
* outcome was new and the tasks returned to their queues.
|
|
165
|
+
*/
|
|
166
|
+
readonly appendOutcome: (r: {
|
|
167
|
+
readonly task: TaskRef; readonly by: NoteBy; readonly why: string; readonly finished: boolean; readonly inputs: string | null;
|
|
168
|
+
/** Why the run's step blocked the task, where it did. */
|
|
169
|
+
readonly blocked: string | null;
|
|
170
|
+
readonly resumedWhy: (waiter: TaskRef) => string;
|
|
171
|
+
}) => Effect.Effect<{ readonly appended: boolean; readonly resumed: ReadonlyArray<TaskRef> }, StorageError>;
|
|
172
|
+
/**
|
|
173
|
+
* WHERE A WORKFLOW'S LIST PUTS EACH OF ITS ITEMS NOW, replacing the last listing in one transaction. `everyItem` says
|
|
174
|
+
* which kind of list it is (`steps.ts` `WorkflowDeclaration`):
|
|
175
|
+
*
|
|
176
|
+
* every item (`items`) a task is made for each; one whose item left the list gets a `removed` note, and one listed
|
|
177
|
+
* again a `listed again` note, which puts it in the queue. No status moves otherwise.
|
|
178
|
+
* work left (`pending`) an item off the list is its workflow's task only while it is done, or once a step or a
|
|
179
|
+
* person has put it back in the queue, until it finishes. One listed again after
|
|
180
|
+
* it left, or listed as work while done (by a list read after it finished), gets a
|
|
181
|
+
* `listed again` note, which puts it in the queue.
|
|
112
182
|
*/
|
|
113
|
-
readonly
|
|
183
|
+
readonly recordListing: (r: {
|
|
184
|
+
readonly workflow: string; readonly listed: ReadonlyArray<{ readonly item: string; readonly listed: Listed }>;
|
|
185
|
+
readonly everyItem: boolean;
|
|
186
|
+
/** When the list was read, unix seconds. */
|
|
187
|
+
readonly readAt: number;
|
|
188
|
+
readonly by: NoteBy;
|
|
189
|
+
}) => Effect.Effect<ListingRecorded, StorageError>;
|
|
114
190
|
/**
|
|
115
|
-
*
|
|
116
|
-
*
|
|
191
|
+
* A workflow's tasks as their notes leave them. Only those that are its own now: on its list; or done; or put back in its
|
|
192
|
+
* queue by a step or a person and not finished since, whatever its list says; never one
|
|
193
|
+
* `removed`.
|
|
117
194
|
*/
|
|
118
|
-
readonly
|
|
119
|
-
/**
|
|
120
|
-
readonly
|
|
121
|
-
/** One task with its notes; none for a task no workflow has named. */
|
|
122
|
-
readonly task: (task: TaskRef) => Effect.Effect<TaskFacts | null, StorageError>;
|
|
195
|
+
readonly statesOf: (workflow: string) => Effect.Effect<ReadonlyArray<TaskState>, StorageError>;
|
|
196
|
+
/** One task as its notes leave it, its workflow's own now or not; none for a task no workflow has named. */
|
|
197
|
+
readonly state: (task: TaskRef) => Effect.Effect<TaskState | null, StorageError>;
|
|
123
198
|
/** A task's notes, oldest first. */
|
|
124
199
|
readonly notes: (task: TaskRef) => Effect.Effect<ReadonlyArray<TaskNote>, StorageError>;
|
|
125
200
|
/** A task's stable id; null for one no workflow has named yet. */
|
|
126
201
|
readonly taskId: (task: TaskRef) => Effect.Effect<number | null, StorageError>;
|
|
127
202
|
/** Records each queue's counts as they are `at`, a count only where it differs from the last recorded for it. */
|
|
128
203
|
readonly recordQueueCounts: (r: { readonly at: number; readonly counts: ReadonlyArray<Omit<QueueCount, 'at'>> }) => Effect.Effect<void, StorageError>;
|
|
129
|
-
/** Each count recorded since `since`, and for each queue and
|
|
204
|
+
/** Each count recorded since `since`, and for each queue and status the last recorded before it, oldest first. */
|
|
130
205
|
readonly queueCountsSince: (since: number) => Effect.Effect<ReadonlyArray<QueueCount>, StorageError>;
|
|
131
|
-
/** Every workflow
|
|
132
|
-
readonly
|
|
206
|
+
/** Every workflow a step of which returned another's tasks to their queue, by the two. */
|
|
207
|
+
readonly returnedBetween: () => Effect.Effect<ReadonlyArray<ReturnedBetween>, StorageError>;
|
|
133
208
|
/** Every wait a workflow's list puts its tasks on now, with how many. */
|
|
134
209
|
readonly listedWaits: () => Effect.Effect<ReadonlyArray<ListedWait>, StorageError>;
|
|
135
210
|
/** Whether the store can be written: false in a test that named no store. */
|