@operad-io/engine 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/config.schema.json +188 -0
- package/keys.schema.json +12 -0
- package/package.json +22 -4
- package/src/config/config.ts +627 -0
- package/src/config/index.ts +1 -0
- package/src/flow/define-flow.ts +57 -0
- package/src/flow/index.ts +5 -0
- package/src/flow/load.ts +41 -0
- package/src/flow/types.ts +107 -0
- package/src/flow/validate.ts +296 -0
- package/src/http/address.ts +142 -0
- package/src/http/index.ts +25 -0
- package/src/http/link.ts +20 -0
- package/src/http/server.ts +920 -0
- package/src/http/token.ts +85 -0
- package/src/index.ts +16 -0
- package/src/kernel/decide.ts +689 -0
- package/src/kernel/index.ts +3 -0
- package/src/kernel/state.ts +275 -0
- package/src/kernel/types.ts +366 -0
- package/src/log/errors.ts +77 -0
- package/src/log/facts.ts +50 -0
- package/src/log/index.ts +6 -0
- package/src/log/json-value.ts +67 -0
- package/src/log/open-log.ts +17 -0
- package/src/log/schema.ts +5 -0
- package/src/log/secret.ts +30 -0
- package/src/log/sqlite.ts +1011 -0
- package/src/log/types.ts +230 -0
- package/src/loop/code-watch.ts +486 -0
- package/src/loop/index.ts +3 -0
- package/src/loop/older-code.ts +58 -0
- package/src/loop/serve.ts +483 -0
- package/src/markdown.ts +451 -0
- package/src/notify/index.ts +10 -0
- package/src/notify/notify.ts +256 -0
- package/src/platform/file-lock.ts +385 -0
- package/src/platform/index.ts +13 -0
- package/src/platform/load.ts +18 -0
- package/src/platform/process-group.ts +635 -0
- package/src/platform/process-watch.ts +152 -0
- package/src/platform/sqlite-open.ts +104 -0
- package/src/platform/step-reap.ts +668 -0
- package/src/platform/watch.ts +158 -0
- package/src/platform/worker-reports.ts +34 -0
- package/src/plugin/action-schema.ts +550 -0
- package/src/plugin/declared-flows.ts +74 -0
- package/src/plugin/define-plugin.ts +12 -0
- package/src/plugin/errors.ts +73 -0
- package/src/plugin/filters.ts +46 -0
- package/src/plugin/harness.ts +50 -0
- package/src/plugin/home.ts +13 -0
- package/src/plugin/index.ts +65 -0
- package/src/plugin/install.ts +268 -0
- package/src/plugin/keys.ts +63 -0
- package/src/plugin/load.ts +256 -0
- package/src/plugin/logger.ts +30 -0
- package/src/plugin/object-facts.ts +132 -0
- package/src/plugin/open-store.ts +32 -0
- package/src/plugin/paging.ts +133 -0
- package/src/plugin/registry.ts +1783 -0
- package/src/plugin/secrets.ts +141 -0
- package/src/plugin/semver.ts +110 -0
- package/src/plugin/settings.ts +64 -0
- package/src/plugin/sqlite-store.ts +756 -0
- package/src/plugin/store.ts +318 -0
- package/src/plugin/types.ts +1236 -0
- package/src/plugin/usage.ts +157 -0
- package/src/plugin/validate.ts +1028 -0
- package/src/runner/answer.ts +156 -0
- package/src/runner/ask.ts +483 -0
- package/src/runner/cancel.ts +51 -0
- package/src/runner/errors.ts +20 -0
- package/src/runner/grants.ts +92 -0
- package/src/runner/index.ts +33 -0
- package/src/runner/limits.ts +403 -0
- package/src/runner/lock.ts +140 -0
- package/src/runner/runner.ts +1175 -0
- package/src/runner/service.ts +184 -0
- package/src/runner/step-worker.ts +303 -0
- package/src/testing/clock.ts +67 -0
- package/src/testing/index.ts +90 -0
- package/src/testing/types.ts +116 -0
- package/src/testing/world.ts +369 -0
- package/src/trigger/cron.ts +193 -0
- package/src/trigger/index.ts +32 -0
- package/src/trigger/kernel.ts +1212 -0
- package/src/trigger/rules.ts +300 -0
- package/src/trigger/store.ts +233 -0
- package/src/trigger/types.ts +156 -0
- package/src/util.ts +50 -0
- package/src/views/history.ts +634 -0
- package/src/views/index.ts +7 -0
- package/src/views/pending.ts +209 -0
- package/src/views/presentation.ts +2551 -0
- package/src/views/running.ts +255 -0
- package/src/views/settings.ts +452 -0
- package/src/views/types.ts +72 -0
- package/src/views/uploads.ts +228 -0
- package/triggers.schema.json +48 -0
- package/README.md +0 -3
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A fast look at the processes below one process, for macOS. The system
|
|
3
|
+
* withholds the environment of Apple's own binaries, so a loop a shell left in
|
|
4
|
+
* the background cannot be found by the marker; it has to be seen while it
|
|
5
|
+
* still has a parent, and a shell that starts it may end a few milliseconds
|
|
6
|
+
* later. A kqueue watches each process below the step for `fork` and `exit`;
|
|
7
|
+
* the kernel keeps what happened until it is read, so a reader that comes every
|
|
8
|
+
* couple of milliseconds misses nothing, and each fork is followed at once by a
|
|
9
|
+
* look at the children of the process that forked.
|
|
10
|
+
*
|
|
11
|
+
* `bun:ffi` reaches the two system calls; no package is added. On a system
|
|
12
|
+
* without them `createProcessWatch` answers `undefined` and the table poll of
|
|
13
|
+
* `step-reap.ts` is all there is.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { dlopen, FFIType, ptr } from "bun:ffi";
|
|
17
|
+
|
|
18
|
+
/** What the system says about one process. */
|
|
19
|
+
export interface WatchedProcess {
|
|
20
|
+
readonly pid: number;
|
|
21
|
+
readonly ppid: number;
|
|
22
|
+
readonly pgid: number;
|
|
23
|
+
readonly uid: number;
|
|
24
|
+
/** Start time in whole seconds since the epoch. */
|
|
25
|
+
readonly startSec: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** What happened to the processes that are watched since the last `drain`. */
|
|
29
|
+
export interface WatchEvents {
|
|
30
|
+
/** Processes that forked: look at their children. */
|
|
31
|
+
readonly forked: readonly number[];
|
|
32
|
+
/** Processes that ended. */
|
|
33
|
+
readonly exited: readonly number[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ProcessWatch {
|
|
37
|
+
/** Starts watching a process for fork and exit; false when it is gone. */
|
|
38
|
+
watch(pid: number): boolean;
|
|
39
|
+
/** The live children of a process. */
|
|
40
|
+
children(pid: number): readonly number[];
|
|
41
|
+
info(pid: number): WatchedProcess | undefined;
|
|
42
|
+
drain(): WatchEvents;
|
|
43
|
+
close(): void;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const EVFILT_PROC = -5;
|
|
47
|
+
const EV_ADD = 0x1;
|
|
48
|
+
const EV_CLEAR = 0x20;
|
|
49
|
+
const NOTE_EXIT = 0x80000000;
|
|
50
|
+
const NOTE_FORK = 0x40000000;
|
|
51
|
+
const PROC_PIDTBSDINFO = 3;
|
|
52
|
+
const BSDINFO_SIZE = 136;
|
|
53
|
+
const EVENT_SIZE = 32;
|
|
54
|
+
const MAX_EVENTS = 256;
|
|
55
|
+
|
|
56
|
+
type Symbols = {
|
|
57
|
+
kqueue(): number;
|
|
58
|
+
kevent(kq: number, changes: unknown, nchanges: number, events: unknown, nevents: number, timeout: unknown): number;
|
|
59
|
+
close(fd: number): number;
|
|
60
|
+
proc_listchildpids(pid: number, buffer: unknown, size: number): number;
|
|
61
|
+
proc_pidinfo(pid: number, flavor: number, arg: bigint, buffer: unknown, size: number): number;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** A watch on this system, or `undefined` when it has none. */
|
|
65
|
+
export function createProcessWatch(): ProcessWatch | undefined {
|
|
66
|
+
if (process.platform !== "darwin") return undefined;
|
|
67
|
+
let symbols: Symbols;
|
|
68
|
+
try {
|
|
69
|
+
symbols = dlopen("libc.dylib", {
|
|
70
|
+
kqueue: { args: [], returns: FFIType.i32 },
|
|
71
|
+
kevent: {
|
|
72
|
+
args: [FFIType.i32, FFIType.ptr, FFIType.i32, FFIType.ptr, FFIType.i32, FFIType.ptr],
|
|
73
|
+
returns: FFIType.i32,
|
|
74
|
+
},
|
|
75
|
+
close: { args: [FFIType.i32], returns: FFIType.i32 },
|
|
76
|
+
proc_listchildpids: { args: [FFIType.i32, FFIType.ptr, FFIType.i32], returns: FFIType.i32 },
|
|
77
|
+
proc_pidinfo: {
|
|
78
|
+
args: [FFIType.i32, FFIType.i32, FFIType.u64, FFIType.ptr, FFIType.i32],
|
|
79
|
+
returns: FFIType.i32,
|
|
80
|
+
},
|
|
81
|
+
}).symbols as unknown as Symbols;
|
|
82
|
+
} catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
const queue = symbols.kqueue();
|
|
86
|
+
if (queue < 0) return undefined;
|
|
87
|
+
|
|
88
|
+
const change = new DataView(new ArrayBuffer(EVENT_SIZE));
|
|
89
|
+
const events = new ArrayBuffer(EVENT_SIZE * MAX_EVENTS);
|
|
90
|
+
const eventView = new DataView(events);
|
|
91
|
+
const noWait = new Uint8Array(16);
|
|
92
|
+
const nothing = new Uint8Array(EVENT_SIZE);
|
|
93
|
+
const kids = new Int32Array(1024);
|
|
94
|
+
const bsd = new Uint8Array(BSDINFO_SIZE);
|
|
95
|
+
const bsdView = new DataView(bsd.buffer);
|
|
96
|
+
let open = true;
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
watch(pid) {
|
|
100
|
+
if (!open) return false;
|
|
101
|
+
change.setBigUint64(0, BigInt(pid), true);
|
|
102
|
+
change.setInt16(8, EVFILT_PROC, true);
|
|
103
|
+
change.setUint16(10, EV_ADD | EV_CLEAR, true);
|
|
104
|
+
change.setUint32(12, (NOTE_FORK | NOTE_EXIT) >>> 0, true);
|
|
105
|
+
change.setBigInt64(16, 0n, true);
|
|
106
|
+
change.setBigUint64(24, 0n, true);
|
|
107
|
+
return (
|
|
108
|
+
symbols.kevent(queue, ptr(new Uint8Array(change.buffer)), 1, ptr(nothing), 0, ptr(noWait)) >= 0
|
|
109
|
+
);
|
|
110
|
+
},
|
|
111
|
+
children(pid) {
|
|
112
|
+
if (!open) return [];
|
|
113
|
+
const count = symbols.proc_listchildpids(pid, ptr(kids), kids.byteLength);
|
|
114
|
+
return count <= 0 ? [] : Array.from(kids.slice(0, count));
|
|
115
|
+
},
|
|
116
|
+
info(pid) {
|
|
117
|
+
if (!open) return undefined;
|
|
118
|
+
const got = symbols.proc_pidinfo(pid, PROC_PIDTBSDINFO, 0n, ptr(bsd), BSDINFO_SIZE);
|
|
119
|
+
if (got !== BSDINFO_SIZE) return undefined;
|
|
120
|
+
return {
|
|
121
|
+
pid: bsdView.getUint32(12, true),
|
|
122
|
+
ppid: bsdView.getUint32(16, true),
|
|
123
|
+
uid: bsdView.getUint32(20, true),
|
|
124
|
+
pgid: bsdView.getUint32(100, true),
|
|
125
|
+
startSec: Number(bsdView.getBigUint64(120, true)),
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
drain() {
|
|
129
|
+
const forked: number[] = [];
|
|
130
|
+
const exited: number[] = [];
|
|
131
|
+
if (!open) return { forked, exited };
|
|
132
|
+
for (;;) {
|
|
133
|
+
const count = symbols.kevent(queue, null, 0, ptr(new Uint8Array(events)), MAX_EVENTS, ptr(noWait));
|
|
134
|
+
if (count <= 0) break;
|
|
135
|
+
for (let index = 0; index < count; index += 1) {
|
|
136
|
+
const at = index * EVENT_SIZE;
|
|
137
|
+
const pid = Number(eventView.getBigUint64(at, true));
|
|
138
|
+
const flags = eventView.getUint32(at + 12, true);
|
|
139
|
+
if ((flags & NOTE_FORK) !== 0) forked.push(pid);
|
|
140
|
+
if ((flags & NOTE_EXIT) !== 0) exited.push(pid);
|
|
141
|
+
}
|
|
142
|
+
if (count < MAX_EVENTS) break;
|
|
143
|
+
}
|
|
144
|
+
return { forked, exited };
|
|
145
|
+
},
|
|
146
|
+
close() {
|
|
147
|
+
if (!open) return;
|
|
148
|
+
open = false;
|
|
149
|
+
symbols.close(queue);
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The settings every SQLite database in the repository opens with.
|
|
3
|
+
*
|
|
4
|
+
* Two processes open the same file: a long-running runner and a command that
|
|
5
|
+
* reads or writes next to it. They have to wait for each other instead of
|
|
6
|
+
* failing at once, and switching to WAL (which lets a reader keep a snapshot
|
|
7
|
+
* while another process appends) is the one step the busy handler does not
|
|
8
|
+
* cover. The rules for both live here so a new store does not rediscover them.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { Database } from "bun:sqlite";
|
|
12
|
+
|
|
13
|
+
/** How long a writer waits for another writer before SQLITE_BUSY. */
|
|
14
|
+
export const BUSY_TIMEOUT_MS = 5_000;
|
|
15
|
+
|
|
16
|
+
/** Install the busy handler, so ordinary lock acquisition waits. */
|
|
17
|
+
export function setBusyTimeout(database: Database): void {
|
|
18
|
+
database.run(`PRAGMA busy_timeout = ${BUSY_TIMEOUT_MS}`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isBusy(error: unknown): boolean {
|
|
22
|
+
return (
|
|
23
|
+
typeof error === "object" &&
|
|
24
|
+
error !== null &&
|
|
25
|
+
"code" in error &&
|
|
26
|
+
(error as { code?: unknown }).code === "SQLITE_BUSY"
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Wait until another connection releases the database. SQLite's busy handler,
|
|
32
|
+
* which `busy_timeout` installs, covers ordinary lock acquisition but not a
|
|
33
|
+
* journal mode switch; an immediate transaction goes through it and blocks
|
|
34
|
+
* until the other writer commits, so nothing has to sleep and poll.
|
|
35
|
+
*/
|
|
36
|
+
function waitForWriter(database: Database): void {
|
|
37
|
+
database.transaction(() => undefined).immediate();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Switch the store to WAL. Two processes opening a fresh store race for the
|
|
42
|
+
* exclusive lock the switch takes: the loser sees SQLITE_BUSY at once, waits
|
|
43
|
+
* for the winner through the busy handler, then sees the mode is already WAL.
|
|
44
|
+
* `onBusy` is told each time the switch met SQLITE_BUSY, so a test can act on
|
|
45
|
+
* that event instead of guessing a duration.
|
|
46
|
+
*
|
|
47
|
+
* A database that cannot be WAL (an in-memory one stays in MEMORY mode and
|
|
48
|
+
* raises nothing) is an error, not something to retry.
|
|
49
|
+
*/
|
|
50
|
+
export function enableWal(database: Database, onBusy?: () => void): void {
|
|
51
|
+
if (currentJournalMode(database) === "wal") return;
|
|
52
|
+
for (;;) {
|
|
53
|
+
try {
|
|
54
|
+
const mode = database
|
|
55
|
+
.query<{ journal_mode: string }, []>("PRAGMA journal_mode = WAL")
|
|
56
|
+
.get()?.journal_mode;
|
|
57
|
+
if (mode === "wal") return;
|
|
58
|
+
throw new Error(`the database could not switch to WAL (journal mode stays ${mode ?? "unknown"})`);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (!isBusy(error)) throw error;
|
|
61
|
+
}
|
|
62
|
+
onBusy?.();
|
|
63
|
+
waitForWriter(database);
|
|
64
|
+
if (currentJournalMode(database) === "wal") return;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function currentJournalMode(database: Database): string | undefined {
|
|
69
|
+
return database
|
|
70
|
+
.query<{ journal_mode: string }, []>("PRAGMA journal_mode")
|
|
71
|
+
.get()?.journal_mode;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The two calls `writeImmediate` makes; a `Database` has both. */
|
|
75
|
+
export interface ImmediateWriter {
|
|
76
|
+
transaction<T>(work: () => T): { immediate(): T };
|
|
77
|
+
query(sql: string): { get(): unknown };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function dataVersion(database: ImmediateWriter): number | undefined {
|
|
81
|
+
const row = database.query("PRAGMA data_version").get();
|
|
82
|
+
if (typeof row !== "object" || row === null) return undefined;
|
|
83
|
+
const version = (row as { data_version?: unknown }).data_version;
|
|
84
|
+
return typeof version === "number" ? version : undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Run `work` in an immediate transaction, waiting for other writers for as long
|
|
89
|
+
* as they make progress. The busy handler gives up after BUSY_TIMEOUT_MS, but a
|
|
90
|
+
* writer that commits again the moment it releases the lock can keep another
|
|
91
|
+
* connection waiting longer than that on a slow disk, with nothing stuck. So
|
|
92
|
+
* SQLITE_BUSY is final only when no other connection committed while this one
|
|
93
|
+
* waited: a holder that never commits is stuck, and the error says so.
|
|
94
|
+
*/
|
|
95
|
+
export function writeImmediate<T>(database: ImmediateWriter, work: () => T): T {
|
|
96
|
+
for (;;) {
|
|
97
|
+
const before = dataVersion(database);
|
|
98
|
+
try {
|
|
99
|
+
return database.transaction(work).immediate();
|
|
100
|
+
} catch (error) {
|
|
101
|
+
if (!isBusy(error) || dataVersion(database) === before) throw error;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|