@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.
Files changed (102) hide show
  1. package/LICENSE +21 -0
  2. package/config.schema.json +188 -0
  3. package/keys.schema.json +12 -0
  4. package/package.json +22 -4
  5. package/src/config/config.ts +627 -0
  6. package/src/config/index.ts +1 -0
  7. package/src/flow/define-flow.ts +57 -0
  8. package/src/flow/index.ts +5 -0
  9. package/src/flow/load.ts +41 -0
  10. package/src/flow/types.ts +107 -0
  11. package/src/flow/validate.ts +296 -0
  12. package/src/http/address.ts +142 -0
  13. package/src/http/index.ts +25 -0
  14. package/src/http/link.ts +20 -0
  15. package/src/http/server.ts +920 -0
  16. package/src/http/token.ts +85 -0
  17. package/src/index.ts +16 -0
  18. package/src/kernel/decide.ts +689 -0
  19. package/src/kernel/index.ts +3 -0
  20. package/src/kernel/state.ts +275 -0
  21. package/src/kernel/types.ts +366 -0
  22. package/src/log/errors.ts +77 -0
  23. package/src/log/facts.ts +50 -0
  24. package/src/log/index.ts +6 -0
  25. package/src/log/json-value.ts +67 -0
  26. package/src/log/open-log.ts +17 -0
  27. package/src/log/schema.ts +5 -0
  28. package/src/log/secret.ts +30 -0
  29. package/src/log/sqlite.ts +1011 -0
  30. package/src/log/types.ts +230 -0
  31. package/src/loop/code-watch.ts +486 -0
  32. package/src/loop/index.ts +3 -0
  33. package/src/loop/older-code.ts +58 -0
  34. package/src/loop/serve.ts +483 -0
  35. package/src/markdown.ts +451 -0
  36. package/src/notify/index.ts +10 -0
  37. package/src/notify/notify.ts +256 -0
  38. package/src/platform/file-lock.ts +385 -0
  39. package/src/platform/index.ts +13 -0
  40. package/src/platform/load.ts +18 -0
  41. package/src/platform/process-group.ts +635 -0
  42. package/src/platform/process-watch.ts +152 -0
  43. package/src/platform/sqlite-open.ts +104 -0
  44. package/src/platform/step-reap.ts +668 -0
  45. package/src/platform/watch.ts +158 -0
  46. package/src/platform/worker-reports.ts +34 -0
  47. package/src/plugin/action-schema.ts +550 -0
  48. package/src/plugin/declared-flows.ts +74 -0
  49. package/src/plugin/define-plugin.ts +12 -0
  50. package/src/plugin/errors.ts +73 -0
  51. package/src/plugin/filters.ts +46 -0
  52. package/src/plugin/harness.ts +50 -0
  53. package/src/plugin/home.ts +13 -0
  54. package/src/plugin/index.ts +65 -0
  55. package/src/plugin/install.ts +268 -0
  56. package/src/plugin/keys.ts +63 -0
  57. package/src/plugin/load.ts +256 -0
  58. package/src/plugin/logger.ts +30 -0
  59. package/src/plugin/object-facts.ts +132 -0
  60. package/src/plugin/open-store.ts +32 -0
  61. package/src/plugin/paging.ts +133 -0
  62. package/src/plugin/registry.ts +1783 -0
  63. package/src/plugin/secrets.ts +141 -0
  64. package/src/plugin/semver.ts +110 -0
  65. package/src/plugin/settings.ts +64 -0
  66. package/src/plugin/sqlite-store.ts +756 -0
  67. package/src/plugin/store.ts +318 -0
  68. package/src/plugin/types.ts +1236 -0
  69. package/src/plugin/usage.ts +157 -0
  70. package/src/plugin/validate.ts +1028 -0
  71. package/src/runner/answer.ts +156 -0
  72. package/src/runner/ask.ts +483 -0
  73. package/src/runner/cancel.ts +51 -0
  74. package/src/runner/errors.ts +20 -0
  75. package/src/runner/grants.ts +92 -0
  76. package/src/runner/index.ts +33 -0
  77. package/src/runner/limits.ts +403 -0
  78. package/src/runner/lock.ts +140 -0
  79. package/src/runner/runner.ts +1175 -0
  80. package/src/runner/service.ts +184 -0
  81. package/src/runner/step-worker.ts +303 -0
  82. package/src/testing/clock.ts +67 -0
  83. package/src/testing/index.ts +90 -0
  84. package/src/testing/types.ts +116 -0
  85. package/src/testing/world.ts +369 -0
  86. package/src/trigger/cron.ts +193 -0
  87. package/src/trigger/index.ts +32 -0
  88. package/src/trigger/kernel.ts +1212 -0
  89. package/src/trigger/rules.ts +300 -0
  90. package/src/trigger/store.ts +233 -0
  91. package/src/trigger/types.ts +156 -0
  92. package/src/util.ts +50 -0
  93. package/src/views/history.ts +634 -0
  94. package/src/views/index.ts +7 -0
  95. package/src/views/pending.ts +209 -0
  96. package/src/views/presentation.ts +2551 -0
  97. package/src/views/running.ts +255 -0
  98. package/src/views/settings.ts +452 -0
  99. package/src/views/types.ts +72 -0
  100. package/src/views/uploads.ts +228 -0
  101. package/triggers.schema.json +48 -0
  102. package/README.md +0 -3
@@ -0,0 +1,158 @@
1
+ /**
2
+ * Watching a directory for changes another process makes, with a fallback.
3
+ *
4
+ * A store or log writes its files in one directory; watching that directory
5
+ * reports changes another process makes, which a process-local listener set
6
+ * cannot see. A watch is the fast path, not a promise: on macOS a write that
7
+ * lands in a sqlite `-wal` file may produce no directory event at all. Every
8
+ * owner therefore pairs the watch with a check on a timer. The timer comes from
9
+ * an injected clock so a test never waits for real time.
10
+ */
11
+
12
+ import { watch } from "node:fs";
13
+ import type { FSWatcher } from "node:fs";
14
+ import { basename } from "node:path";
15
+ import { repeatEvery } from "../util.ts";
16
+
17
+ /**
18
+ * Calls `onChange` when anything in `directory` changes, with the entry name
19
+ * when the platform reports one. Returns a stop function; watching a missing
20
+ * directory does nothing and still returns a stop function.
21
+ */
22
+ export function watchDirectory(
23
+ directory: string,
24
+ onChange: (filename: string | null) => void,
25
+ ): () => void {
26
+ let watcher: FSWatcher;
27
+ try {
28
+ watcher = watch(directory, { persistent: true }, (_event, filename) => {
29
+ onChange(filename === undefined ? null : String(filename));
30
+ });
31
+ } catch {
32
+ return () => undefined;
33
+ }
34
+ watcher.on("error", () => {
35
+ // A watcher that loses its directory stops notifying. Reads still refresh
36
+ // on demand, so this is not a reason to fail the owner.
37
+ });
38
+ return () => {
39
+ watcher.close();
40
+ };
41
+ }
42
+
43
+ /** How often an open subscription re-reads storage by default. */
44
+ export const DEFAULT_POLL_MS = 500;
45
+
46
+ /**
47
+ * Whether an open subscription follows changes another writer makes. A command
48
+ * that answers once and exits passes false: it has nothing to follow.
49
+ */
50
+ export interface FollowOptions {
51
+ /** Defaults to true. False opens without a watch or a fallback timer. */
52
+ readonly follow?: boolean | undefined;
53
+ /** The same as `follow: false`: this command only reads what it opens. */
54
+ readonly readonly?: boolean | undefined;
55
+ /** The same as `follow: false`, in camel case. */
56
+ readonly readOnly?: boolean | undefined;
57
+ /** The same as `follow: false`: this process opens what it reads once. */
58
+ readonly oneShot?: boolean | undefined;
59
+ }
60
+
61
+ /** Whether a subscription starts a watch and a fallback timer. */
62
+ export function followsChanges(options: FollowOptions): boolean {
63
+ if (options.follow !== undefined) return options.follow;
64
+ if (options.readonly !== undefined) return !options.readonly;
65
+ if (options.readOnly !== undefined) return !options.readOnly;
66
+ if (options.oneShot !== undefined) return !options.oneShot;
67
+ return true;
68
+ }
69
+
70
+ /** The watch, the fallback timer and its period one subscription uses. */
71
+ export interface SubscriptionWatch {
72
+ readonly pollMs: number;
73
+ readonly repeat: (ms: number, callback: () => void) => () => void;
74
+ readonly watch: (
75
+ directory: string,
76
+ onChange: (filename: string | null) => void,
77
+ ) => () => void;
78
+ }
79
+
80
+ /** What an owner lets a test replace: the period, the timer and the watch. */
81
+ export interface SubscriptionWatchOptions extends FollowOptions {
82
+ readonly pollMs?: number | undefined;
83
+ readonly repeat?: ((ms: number, callback: () => void) => () => void) | undefined;
84
+ readonly watch?:
85
+ | ((
86
+ directory: string,
87
+ onChange: (filename: string | null) => void,
88
+ ) => () => void)
89
+ | undefined;
90
+ }
91
+
92
+ function never(): () => void {
93
+ return () => undefined;
94
+ }
95
+
96
+ export function resolveSubscriptionWatch(
97
+ options: SubscriptionWatchOptions,
98
+ ): SubscriptionWatch {
99
+ const follow = followsChanges(options);
100
+ return {
101
+ pollMs: options.pollMs ?? DEFAULT_POLL_MS,
102
+ repeat: follow ? (options.repeat ?? repeatEvery) : never,
103
+ watch: follow ? (options.watch ?? watchDirectory) : never,
104
+ };
105
+ }
106
+
107
+ /**
108
+ * Test-only: the directory watches and fallback timers `waitForChange` holds
109
+ * open. Production code never reads this; a suite asserts it is zero at the
110
+ * end, so a wait that forgets to close its handles fails.
111
+ */
112
+ export const openWaitHandles = { watchers: 0, timers: 0 };
113
+
114
+ /** Test-only: the number of wait handles still open. */
115
+ export function openWaitHandleCount(): number {
116
+ return openWaitHandles.watchers + openWaitHandles.timers;
117
+ }
118
+
119
+ /**
120
+ * Waits until `entry` (or an unnamed entry) changes in `directory`, or for one
121
+ * fallback interval of `ms`, whichever comes first. A rename replaces the file
122
+ * itself, so the directory is watched, not the file.
123
+ */
124
+ export function waitForChange(
125
+ directory: string,
126
+ entry: string,
127
+ ms: number,
128
+ ): Promise<void> {
129
+ return new Promise((done) => {
130
+ let watcher: FSWatcher | undefined;
131
+ let timer: ReturnType<typeof setTimeout> | undefined;
132
+ let finished = false;
133
+ const finish = (): void => {
134
+ if (finished) return;
135
+ finished = true;
136
+ if (watcher !== undefined) {
137
+ openWaitHandles.watchers -= 1;
138
+ watcher.close();
139
+ }
140
+ if (timer !== undefined) {
141
+ openWaitHandles.timers -= 1;
142
+ clearTimeout(timer);
143
+ }
144
+ done();
145
+ };
146
+ timer = setTimeout(finish, Math.max(1, ms));
147
+ openWaitHandles.timers += 1;
148
+ try {
149
+ watcher = watch(directory, (_event, filename) => {
150
+ if (filename === null || filename === basename(entry)) finish();
151
+ });
152
+ openWaitHandles.watchers += 1;
153
+ watcher.on("error", finish);
154
+ } catch {
155
+ // The timer is the fallback when watching is unavailable.
156
+ }
157
+ });
158
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * What a step that asked for a worker can learn about it while it runs: the
3
+ * pool says when it gave the worker a place, the harness says when the worker's
4
+ * program is running. The step that asked installs the reports around its call
5
+ * and the two steps below it send theirs; nothing is passed down through
6
+ * params, so a harness a flow names by data still reports.
7
+ */
8
+
9
+ import { AsyncLocalStorage } from "node:async_hooks";
10
+
11
+ /** The two things a worker run reports to the step that asked for it. */
12
+ export interface WorkerReports {
13
+ /** The pool gave worker `name` a place. */
14
+ readonly placed?: ((name: string) => void) | undefined;
15
+ /** The worker's program is running. */
16
+ readonly started?: (() => void) | undefined;
17
+ }
18
+
19
+ const current = new AsyncLocalStorage<WorkerReports>();
20
+
21
+ /** Runs `body` so the pool and the harness below it report to `reports`. */
22
+ export function withWorkerReports<T>(reports: WorkerReports, body: () => T): T {
23
+ return current.run(reports, body);
24
+ }
25
+
26
+ /** Said by the pool when it gave worker `name` a place. */
27
+ export function reportWorkerPlaced(name: string): void {
28
+ current.getStore()?.placed?.(name);
29
+ }
30
+
31
+ /** Said by a harness once the program that does the work is running. */
32
+ export function reportWorkerStarted(): void {
33
+ current.getStore()?.started?.();
34
+ }