@operad-io/engine 0.0.0-stage → 0.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/LICENSE +21 -0
- package/README.md +11 -2
- 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 +90 -0
- package/src/index.ts +18 -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 +675 -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 +1786 -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 +1243 -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 +1221 -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 +2609 -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/src/util.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/** Small functions that several packages need; each has one definition, here. */
|
|
2
|
+
|
|
3
|
+
/** Whether the value is a plain object: not null, not an array. */
|
|
4
|
+
export function isRecord(value: unknown): value is Record<string, unknown> {
|
|
5
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** The strings of an array value; anything that is not an array gives an empty list. */
|
|
9
|
+
export function stringList(value: unknown): readonly string[] {
|
|
10
|
+
return Array.isArray(value)
|
|
11
|
+
? value.filter((item): item is string => typeof item === "string")
|
|
12
|
+
: [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The params of a step as an object; `at` names the step in the error. */
|
|
16
|
+
export function paramsObject(params: unknown, at: string): Record<string, unknown> {
|
|
17
|
+
if (!isRecord(params)) throw new Error(`${at}: params: must be an object`);
|
|
18
|
+
return params;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Throws when the value is empty or only spaces; `field` names it in the error. */
|
|
22
|
+
export function requireName(value: string, field: string): void {
|
|
23
|
+
if (value.trim().length === 0) {
|
|
24
|
+
throw new Error(`${field}: must be a non-empty string`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* One line: whitespace collapsed, and cut with an ellipsis when a limit is given.
|
|
30
|
+
* The cut keeps a space at its edge unless `trimCut` is set.
|
|
31
|
+
*/
|
|
32
|
+
export function oneLine(
|
|
33
|
+
text: string,
|
|
34
|
+
limit: number = Number.POSITIVE_INFINITY,
|
|
35
|
+
trimCut = false,
|
|
36
|
+
): string {
|
|
37
|
+
const flat = text.replace(/\s+/g, " ").trim();
|
|
38
|
+
if (flat.length <= limit) return flat;
|
|
39
|
+
const cut = flat.slice(0, limit - 1);
|
|
40
|
+
return `${trimCut ? cut.trimEnd() : cut}…`;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Calls back every `ms` until the returned function is called; it never keeps the process alive. */
|
|
44
|
+
export function repeatEvery(ms: number, callback: () => void): () => void {
|
|
45
|
+
const timer = setInterval(callback, ms);
|
|
46
|
+
(timer as { unref?: () => void }).unref?.();
|
|
47
|
+
return () => {
|
|
48
|
+
clearInterval(timer);
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,634 @@
|
|
|
1
|
+
/** The history view: one line per recorded step, in order, in words. */
|
|
2
|
+
|
|
3
|
+
import type { JsonValue } from "../log/types.ts";
|
|
4
|
+
import type { Actor } from "../plugin/types.ts";
|
|
5
|
+
|
|
6
|
+
/** The facts a history entry needs; run facts and global facts both fit. */
|
|
7
|
+
export interface HistoryFact {
|
|
8
|
+
readonly kind: string;
|
|
9
|
+
readonly payload: JsonValue;
|
|
10
|
+
readonly time: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface HistoryEntry {
|
|
14
|
+
readonly time: number;
|
|
15
|
+
readonly step: string | null;
|
|
16
|
+
readonly what: string;
|
|
17
|
+
readonly why: string;
|
|
18
|
+
/** Who answered, when a person did. */
|
|
19
|
+
readonly actor?: Actor | undefined;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function record(value: unknown): Record<string, unknown> {
|
|
23
|
+
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
24
|
+
? (value as Record<string, unknown>)
|
|
25
|
+
: {};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function text(value: unknown): string | undefined {
|
|
29
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function number(value: unknown): number | undefined {
|
|
33
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function iso(time: number): string {
|
|
37
|
+
return new Date(time).toISOString();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function reasonOf(data: Record<string, unknown>): string {
|
|
41
|
+
const message = text(data.message) ?? text(data.error);
|
|
42
|
+
const reason = text(data.reason);
|
|
43
|
+
const log = text(data.log) ?? text(data.path);
|
|
44
|
+
let why = message ?? reason ?? "";
|
|
45
|
+
if (log !== undefined) {
|
|
46
|
+
why = why.length > 0 ? `${why}, log at ${log}` : `log at ${log}`;
|
|
47
|
+
}
|
|
48
|
+
return why;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Turns facts into user words. Nothing here prints a fact kind, an effect, a
|
|
53
|
+
* node, a graph or a decision; those are internal names.
|
|
54
|
+
*/
|
|
55
|
+
export function historyEntry(fact: HistoryFact): HistoryEntry {
|
|
56
|
+
const payload = record(fact.payload);
|
|
57
|
+
const step = text(payload.step) ?? null;
|
|
58
|
+
const time = fact.time;
|
|
59
|
+
|
|
60
|
+
switch (fact.kind) {
|
|
61
|
+
case "run_started": {
|
|
62
|
+
const flow = text(payload.flow) ?? "unknown";
|
|
63
|
+
const trigger = record(payload.trigger);
|
|
64
|
+
const source = text(trigger.source);
|
|
65
|
+
const type = text(trigger.type);
|
|
66
|
+
const why =
|
|
67
|
+
source !== undefined && type !== undefined
|
|
68
|
+
? `flow "${flow}", started by ${source} ${type}`
|
|
69
|
+
: `flow "${flow}"`;
|
|
70
|
+
return {
|
|
71
|
+
time,
|
|
72
|
+
step,
|
|
73
|
+
what: "run started",
|
|
74
|
+
why,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
case "step_entered":
|
|
79
|
+
return {
|
|
80
|
+
time,
|
|
81
|
+
step,
|
|
82
|
+
what: `started ${step ?? "a step"}`,
|
|
83
|
+
why: `attempt ${number(payload.visit) ?? 1}`,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
case "effect_requested": {
|
|
87
|
+
if (payload.recovery === true) {
|
|
88
|
+
return {
|
|
89
|
+
time,
|
|
90
|
+
step,
|
|
91
|
+
what: `re-running ${step ?? "the step"} after a crash`,
|
|
92
|
+
why: "",
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
const kind = text(payload.effect);
|
|
96
|
+
if (kind === "schedule_wake") {
|
|
97
|
+
const due = number(payload.due);
|
|
98
|
+
return {
|
|
99
|
+
time,
|
|
100
|
+
step,
|
|
101
|
+
what: `waiting for ${step ?? "the step"}`,
|
|
102
|
+
why: due === undefined ? "" : `until ${iso(due)}`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (kind === "poll_check") {
|
|
106
|
+
return {
|
|
107
|
+
time,
|
|
108
|
+
step,
|
|
109
|
+
what: `checking ${step ?? "the step"}`,
|
|
110
|
+
why: "",
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
time,
|
|
115
|
+
step,
|
|
116
|
+
what: `running ${step ?? "the step"}`,
|
|
117
|
+
why: "",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
case "step_result": {
|
|
122
|
+
const result = text(payload.result) ?? "finished";
|
|
123
|
+
const data = record(payload.data);
|
|
124
|
+
return {
|
|
125
|
+
time,
|
|
126
|
+
step,
|
|
127
|
+
what: `${step ?? "the step"} ${result}`,
|
|
128
|
+
why: reasonOf(data),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
case "process_replaced":
|
|
133
|
+
return {
|
|
134
|
+
time,
|
|
135
|
+
step,
|
|
136
|
+
what: "continuing on updated code",
|
|
137
|
+
why: text(payload.reason) ?? "",
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
case "step_queued":
|
|
141
|
+
return {
|
|
142
|
+
time,
|
|
143
|
+
step,
|
|
144
|
+
what: `${step ?? "the step"} is queued`,
|
|
145
|
+
why: `waiting for a place under ${text(payload.key) ?? "its limit"}`,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
case "edge_taken": {
|
|
149
|
+
const target = text(payload.target) ?? "the next step";
|
|
150
|
+
return {
|
|
151
|
+
time,
|
|
152
|
+
step,
|
|
153
|
+
what: target === "end" ? "moving to the end" : `moving to ${target}`,
|
|
154
|
+
why: text(payload.result) === undefined ? "" : `after ${text(payload.result) ?? ""}`,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
case "waiting_for_person":
|
|
159
|
+
return {
|
|
160
|
+
time,
|
|
161
|
+
step,
|
|
162
|
+
what: "waiting for you",
|
|
163
|
+
why: text(payload.prompt) ?? "",
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
case "person_answered": {
|
|
167
|
+
const actorValue = record(payload.actor);
|
|
168
|
+
const via = text(actorValue.via);
|
|
169
|
+
const id = text(actorValue.id);
|
|
170
|
+
const whyParts: string[] = [];
|
|
171
|
+
const note = text(payload.note);
|
|
172
|
+
if (note !== undefined) whyParts.push(note);
|
|
173
|
+
if (via !== undefined) whyParts.push(`via ${via}`);
|
|
174
|
+
if (id !== undefined) whyParts.push(`as ${id}`);
|
|
175
|
+
return {
|
|
176
|
+
time,
|
|
177
|
+
step,
|
|
178
|
+
what: `you chose ${text(payload.choice) ?? "an answer"}`,
|
|
179
|
+
why: whyParts.join(", "),
|
|
180
|
+
...(via === undefined || id === undefined
|
|
181
|
+
? {}
|
|
182
|
+
: { actor: { via, id } }),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
case "ask_opened":
|
|
187
|
+
return {
|
|
188
|
+
time,
|
|
189
|
+
step: text(payload.step) ?? step,
|
|
190
|
+
what: "asked you",
|
|
191
|
+
why: text(payload.prompt) ?? "",
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
case "ask_answered": {
|
|
195
|
+
const actorValue = record(payload.actor);
|
|
196
|
+
const via = text(actorValue.via);
|
|
197
|
+
const id = text(actorValue.id);
|
|
198
|
+
const whyParts: string[] = [];
|
|
199
|
+
const note = text(payload.note);
|
|
200
|
+
if (note !== undefined) whyParts.push(note);
|
|
201
|
+
if (via !== undefined) whyParts.push(`via ${via}`);
|
|
202
|
+
if (id !== undefined) whyParts.push(`as ${id}`);
|
|
203
|
+
return {
|
|
204
|
+
time,
|
|
205
|
+
step,
|
|
206
|
+
what: `you chose ${text(payload.choice) ?? "an answer"}`,
|
|
207
|
+
why: whyParts.join(", "),
|
|
208
|
+
...(via === undefined || id === undefined
|
|
209
|
+
? {}
|
|
210
|
+
: { actor: { via, id } }),
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
case "ask_timed_out":
|
|
215
|
+
return {
|
|
216
|
+
time,
|
|
217
|
+
step,
|
|
218
|
+
what: "nobody answered in time",
|
|
219
|
+
why: "",
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
case "grant_used":
|
|
223
|
+
return {
|
|
224
|
+
time,
|
|
225
|
+
step,
|
|
226
|
+
what: "let one call through that you approved",
|
|
227
|
+
why: text(payload.value) ?? "",
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
case "ask_refused":
|
|
231
|
+
return {
|
|
232
|
+
time,
|
|
233
|
+
step,
|
|
234
|
+
what: "an answer was refused",
|
|
235
|
+
why: [text(payload.choice), text(payload.reason)]
|
|
236
|
+
.filter((part): part is string => part !== undefined)
|
|
237
|
+
.join(": "),
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
case "run_ended": {
|
|
241
|
+
const status = text(payload.status) ?? "ended";
|
|
242
|
+
const words =
|
|
243
|
+
status === "done" ? "run done" : status === "cancelled" ? "run cancelled" : "run failed";
|
|
244
|
+
return { time, step, what: words, why: text(payload.reason) ?? "" };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
case "step_interrupted": {
|
|
248
|
+
const reason = text(payload.reason);
|
|
249
|
+
const why =
|
|
250
|
+
reason === "recover_unsafe"
|
|
251
|
+
? "the run stopped and this step cannot be replayed safely"
|
|
252
|
+
: reason === "undeclared_result"
|
|
253
|
+
? `the step returned "${text(payload.result) ?? "an unknown result"}", which the flow does not expect${
|
|
254
|
+
text(payload.message) === undefined ? "" : `: ${text(payload.message)}`
|
|
255
|
+
}`
|
|
256
|
+
: "the step did not finish";
|
|
257
|
+
return { time, step, what: `${step ?? "the step"} interrupted`, why };
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
case "crash_loop_stopped":
|
|
261
|
+
return {
|
|
262
|
+
time,
|
|
263
|
+
step,
|
|
264
|
+
what: `${step ?? "the step"} keeps crashing`,
|
|
265
|
+
why: `waiting for you after ${number(payload.recoveries) ?? 0} attempts`,
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
case "trigger_received": {
|
|
269
|
+
const source = text(payload.source) ?? "an outside source";
|
|
270
|
+
const type = text(payload.type) ?? "event";
|
|
271
|
+
return {
|
|
272
|
+
time,
|
|
273
|
+
step,
|
|
274
|
+
what: `event ${type} from ${source}`,
|
|
275
|
+
why: text(payload.summary) ?? text(payload.link) ?? "recorded while the run was active",
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
case "trigger_observed": {
|
|
280
|
+
const flow = text(payload.flow) ?? "a flow";
|
|
281
|
+
const object = text(payload.object) ?? "an object";
|
|
282
|
+
return {
|
|
283
|
+
time,
|
|
284
|
+
step,
|
|
285
|
+
what: `would start ${flow} on ${object}`,
|
|
286
|
+
why: "",
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
case "trigger_skipped": {
|
|
291
|
+
const source = text(payload.source) ?? "an outside source";
|
|
292
|
+
const type = text(payload.type) ?? "event";
|
|
293
|
+
return {
|
|
294
|
+
time,
|
|
295
|
+
step,
|
|
296
|
+
what: `event ${type} from ${source} not started`,
|
|
297
|
+
why: text(payload.reason) ?? "the chain is too deep",
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
case "visit_limit_reached":
|
|
302
|
+
return {
|
|
303
|
+
time,
|
|
304
|
+
step,
|
|
305
|
+
what: `${step ?? "the step"} ran too many times`,
|
|
306
|
+
why: `limit ${number(payload.limit) ?? 0}`,
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
default:
|
|
310
|
+
return { time, step, what: "something happened", why: "" };
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Formats one entry for a terminal: time, what happened, and why. */
|
|
315
|
+
export function formatHistoryEntry(entry: HistoryEntry): string {
|
|
316
|
+
const why = entry.why.length > 0 ? `: ${entry.why}` : "";
|
|
317
|
+
return `${iso(entry.time)} ${entry.what}${why}`;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
export async function* historyEntries(
|
|
321
|
+
facts: AsyncIterable<HistoryFact>,
|
|
322
|
+
): AsyncGenerator<HistoryEntry> {
|
|
323
|
+
for await (const fact of facts) {
|
|
324
|
+
// Where a step runs is kernel bookkeeping; history shows what it did.
|
|
325
|
+
if (
|
|
326
|
+
fact.kind === "step_started" ||
|
|
327
|
+
fact.kind === "step_slot" ||
|
|
328
|
+
fact.kind === "step_released"
|
|
329
|
+
) {
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
yield historyEntry(fact);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** Results that mean "carry on with the next step"; they need no explaining. */
|
|
337
|
+
const PLAIN_RESULTS: ReadonlySet<string> = new Set([
|
|
338
|
+
"ready",
|
|
339
|
+
"done",
|
|
340
|
+
"ok",
|
|
341
|
+
"pass",
|
|
342
|
+
"passed",
|
|
343
|
+
"success",
|
|
344
|
+
"next",
|
|
345
|
+
]);
|
|
346
|
+
|
|
347
|
+
const SHORT_DETAIL = 100;
|
|
348
|
+
|
|
349
|
+
function two(value: number): string {
|
|
350
|
+
return String(value).padStart(2, "0");
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/** HH:MM:SS in the time zone of the host that serves the view. */
|
|
354
|
+
export function localTime(time: number): string {
|
|
355
|
+
const date = new Date(time);
|
|
356
|
+
return `${two(date.getHours())}:${two(date.getMinutes())}:${two(date.getSeconds())}`;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** HH:MM, for a run's heading. */
|
|
360
|
+
export function localMinute(time: number): string {
|
|
361
|
+
return localTime(time).slice(0, 5);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** YYYY-MM-DD in the host's time zone. */
|
|
365
|
+
export function localDay(time: number): string {
|
|
366
|
+
const date = new Date(time);
|
|
367
|
+
return `${date.getFullYear()}-${two(date.getMonth() + 1)}-${two(date.getDate())}`;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** 1s, 58s, 41m, 2h 5m, 3d. */
|
|
371
|
+
export function shortDuration(milliseconds: number): string {
|
|
372
|
+
const seconds = Math.max(0, Math.floor(milliseconds / 1000));
|
|
373
|
+
if (seconds < 60) return `${seconds}s`;
|
|
374
|
+
const minutes = Math.floor(seconds / 60);
|
|
375
|
+
if (minutes < 60) return `${minutes}m`;
|
|
376
|
+
const hours = Math.floor(minutes / 60);
|
|
377
|
+
if (hours < 24) return minutes % 60 === 0 ? `${hours}h` : `${hours}h ${minutes % 60}m`;
|
|
378
|
+
return `${Math.floor(hours / 24)}d`;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
function detailLine(value: string, full: boolean): string {
|
|
382
|
+
const flat = value.replace(/\s*\n\s*/g, " ").replace(/[ \t]+/g, " ").trim();
|
|
383
|
+
if (full || flat.length <= SHORT_DETAIL) return flat;
|
|
384
|
+
return `${flat.slice(0, SHORT_DETAIL - 1).trimEnd()}…`;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
interface Attempt {
|
|
388
|
+
readonly time: number;
|
|
389
|
+
readonly step: string;
|
|
390
|
+
readonly attempt: number;
|
|
391
|
+
readonly reason: string;
|
|
392
|
+
outcome: string | undefined;
|
|
393
|
+
endedAt: number | undefined;
|
|
394
|
+
detail: string;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/** Attempts of one step that repeated with the same outcome, as one line. */
|
|
398
|
+
interface Folded {
|
|
399
|
+
readonly time: number;
|
|
400
|
+
readonly last: Attempt;
|
|
401
|
+
readonly count: number;
|
|
402
|
+
readonly lastTime: number;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
type Item = Attempt | Folded | { readonly time: number; readonly text: string };
|
|
406
|
+
|
|
407
|
+
function isFolded(item: Item): item is Folded {
|
|
408
|
+
return "count" in item;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* What two attempts must share to fold: the step, how it started and how it
|
|
413
|
+
* ended. Time and attempt number do not count, and neither do the times
|
|
414
|
+
* written into the detail (a retry time); any other difference keeps them apart.
|
|
415
|
+
*/
|
|
416
|
+
const ISO_TIME = /\d{4}-\d\d-\d\dT\d\d:\d\d(?::\d\d(?:\.\d+)?)?Z?/g;
|
|
417
|
+
|
|
418
|
+
function signature(item: Item): string | undefined {
|
|
419
|
+
if (!isAttempt(item) || item.outcome === undefined || item.outcome === "waiting") {
|
|
420
|
+
return undefined;
|
|
421
|
+
}
|
|
422
|
+
return JSON.stringify([
|
|
423
|
+
item.step,
|
|
424
|
+
item.reason,
|
|
425
|
+
item.outcome,
|
|
426
|
+
item.detail.replace(ISO_TIME, "#"),
|
|
427
|
+
]);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* Folds consecutive attempts that repeat: the same attempt again and again, or
|
|
432
|
+
* a step that alternates with its retry step (the pair folds as one, shown by
|
|
433
|
+
* the first step). Anything else in between breaks the fold.
|
|
434
|
+
*/
|
|
435
|
+
function foldRepeats(items: readonly Item[]): Item[] {
|
|
436
|
+
const out: Item[] = [];
|
|
437
|
+
let index = 0;
|
|
438
|
+
while (index < items.length) {
|
|
439
|
+
const first = items[index];
|
|
440
|
+
const firstSig = first === undefined ? undefined : signature(first);
|
|
441
|
+
if (first === undefined || !isAttempt(first) || firstSig === undefined) {
|
|
442
|
+
if (first !== undefined) out.push(first);
|
|
443
|
+
index += 1;
|
|
444
|
+
continue;
|
|
445
|
+
}
|
|
446
|
+
// The same attempt repeated.
|
|
447
|
+
let end = index + 1;
|
|
448
|
+
while (end < items.length && signatureAt(items, end) === firstSig) end += 1;
|
|
449
|
+
let count = end - index;
|
|
450
|
+
let lastAt = end - 1;
|
|
451
|
+
if (count < 2) {
|
|
452
|
+
// A step that alternates with another one.
|
|
453
|
+
const secondSig = signatureAt(items, index + 1);
|
|
454
|
+
const second = items[index + 1];
|
|
455
|
+
if (
|
|
456
|
+
secondSig !== undefined &&
|
|
457
|
+
secondSig !== firstSig &&
|
|
458
|
+
second !== undefined &&
|
|
459
|
+
isAttempt(second) &&
|
|
460
|
+
second.step === `${first.step}_retry`
|
|
461
|
+
) {
|
|
462
|
+
let cursor = index;
|
|
463
|
+
count = 0;
|
|
464
|
+
while (
|
|
465
|
+
signatureAt(items, cursor) === firstSig &&
|
|
466
|
+
signatureAt(items, cursor + 1) === secondSig
|
|
467
|
+
) {
|
|
468
|
+
count += 1;
|
|
469
|
+
lastAt = cursor;
|
|
470
|
+
cursor += 2;
|
|
471
|
+
}
|
|
472
|
+
// The run may end on the first step with no retry after it.
|
|
473
|
+
if (count > 0 && signatureAt(items, cursor) === firstSig) {
|
|
474
|
+
count += 1;
|
|
475
|
+
lastAt = cursor;
|
|
476
|
+
cursor += 1;
|
|
477
|
+
}
|
|
478
|
+
end = cursor;
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
const lastItem = items[lastAt];
|
|
482
|
+
if (count >= 2 && lastItem !== undefined && isAttempt(lastItem)) {
|
|
483
|
+
out.push({ time: first.time, last: lastItem, count, lastTime: lastItem.time });
|
|
484
|
+
index = end;
|
|
485
|
+
} else {
|
|
486
|
+
out.push(first);
|
|
487
|
+
index += 1;
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
return out;
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
function signatureAt(items: readonly Item[], index: number): string | undefined {
|
|
494
|
+
const item = items[index];
|
|
495
|
+
return item === undefined ? undefined : signature(item);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function isAttempt(item: Item): item is Attempt {
|
|
499
|
+
return "attempt" in item;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export interface RunHistoryOptions {
|
|
503
|
+
/** The current time, for a step that is still running. */
|
|
504
|
+
readonly now: number;
|
|
505
|
+
/** Keep every detail whole instead of cutting it to one short line. */
|
|
506
|
+
readonly full: boolean;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* One line per step attempt, with a date line where the day changes. The
|
|
511
|
+
* lines are plain text; a caller adds the list marks it needs.
|
|
512
|
+
*/
|
|
513
|
+
export function runHistoryLines(
|
|
514
|
+
facts: readonly HistoryFact[],
|
|
515
|
+
options: RunHistoryOptions,
|
|
516
|
+
): string[] {
|
|
517
|
+
const items: Item[] = [];
|
|
518
|
+
let open: Attempt | undefined;
|
|
519
|
+
let reason = "";
|
|
520
|
+
|
|
521
|
+
const close = (outcome: string, at: number, detail: string): void => {
|
|
522
|
+
if (open === undefined) return;
|
|
523
|
+
if (open.outcome === undefined || open.outcome === "waiting") {
|
|
524
|
+
open.outcome = outcome;
|
|
525
|
+
open.endedAt = at;
|
|
526
|
+
open.detail = detail;
|
|
527
|
+
}
|
|
528
|
+
open = undefined;
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
for (const fact of facts) {
|
|
532
|
+
const payload = record(fact.payload);
|
|
533
|
+
switch (fact.kind) {
|
|
534
|
+
case "run_started": // the run's heading says it
|
|
535
|
+
case "step_started":
|
|
536
|
+
case "step_slot":
|
|
537
|
+
case "step_released":
|
|
538
|
+
case "effect_requested":
|
|
539
|
+
break;
|
|
540
|
+
case "step_entered": {
|
|
541
|
+
// An end step is not work; the run's own closing line says it.
|
|
542
|
+
if (payload.kind === "end") {
|
|
543
|
+
reason = "";
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
const step = text(payload.step) ?? "a step";
|
|
547
|
+
const attempt: Attempt = {
|
|
548
|
+
time: fact.time,
|
|
549
|
+
step,
|
|
550
|
+
attempt: number(payload.visit) ?? 1,
|
|
551
|
+
reason,
|
|
552
|
+
outcome: undefined,
|
|
553
|
+
endedAt: undefined,
|
|
554
|
+
detail: "",
|
|
555
|
+
};
|
|
556
|
+
reason = "";
|
|
557
|
+
open = attempt;
|
|
558
|
+
items.push(attempt);
|
|
559
|
+
break;
|
|
560
|
+
}
|
|
561
|
+
case "step_result":
|
|
562
|
+
close(text(payload.result) ?? "finished", fact.time, reasonOf(record(payload.data)));
|
|
563
|
+
break;
|
|
564
|
+
case "waiting_for_person":
|
|
565
|
+
if (open !== undefined && open.outcome === undefined) {
|
|
566
|
+
open.outcome = "waiting";
|
|
567
|
+
open.detail = text(payload.prompt) ?? "";
|
|
568
|
+
} else if (open === undefined) {
|
|
569
|
+
const entry = historyEntry(fact);
|
|
570
|
+
items.push({ time: fact.time, text: `${entry.what}: ${detailLine(entry.why, options.full)}` });
|
|
571
|
+
}
|
|
572
|
+
break;
|
|
573
|
+
case "edge_taken": {
|
|
574
|
+
const result = text(payload.result);
|
|
575
|
+
if (open !== undefined) close(result ?? "finished", fact.time, "");
|
|
576
|
+
reason = result === undefined || PLAIN_RESULTS.has(result) ? "" : `after ${result}`;
|
|
577
|
+
break;
|
|
578
|
+
}
|
|
579
|
+
case "step_interrupted": {
|
|
580
|
+
const entry = historyEntry(fact);
|
|
581
|
+
close("interrupted", fact.time, entry.why);
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
case "run_ended": {
|
|
585
|
+
const entry = historyEntry(fact);
|
|
586
|
+
const status = text(payload.status) ?? "ended";
|
|
587
|
+
close(status === "done" ? "stopped" : status, fact.time, "");
|
|
588
|
+
items.push({ time: fact.time, text: `${entry.what}${entry.why.length > 0 ? `: ${entry.why}` : ""}` });
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
default: {
|
|
592
|
+
const entry = historyEntry(fact);
|
|
593
|
+
items.push({
|
|
594
|
+
time: fact.time,
|
|
595
|
+
text: `${entry.what}${entry.why.length > 0 ? `: ${detailLine(entry.why, options.full)}` : ""}`,
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const lines: string[] = [];
|
|
602
|
+
let day = "";
|
|
603
|
+
for (const item of foldRepeats(items)) {
|
|
604
|
+
const today = localDay(item.time);
|
|
605
|
+
if (today !== day) {
|
|
606
|
+
day = today;
|
|
607
|
+
lines.push(`**${today}**`);
|
|
608
|
+
}
|
|
609
|
+
if (isFolded(item)) {
|
|
610
|
+
const parts: string[] = [item.last.step];
|
|
611
|
+
if (item.last.reason.length > 0) parts.push(item.last.reason);
|
|
612
|
+
parts.push(
|
|
613
|
+
`${item.last.outcome ?? ""} ×${item.count}`,
|
|
614
|
+
`${localMinute(item.time)}–${localMinute(item.lastTime)}`,
|
|
615
|
+
);
|
|
616
|
+
if (item.last.detail.length > 0) parts.push(detailLine(item.last.detail, options.full));
|
|
617
|
+
lines.push(parts.join(" "));
|
|
618
|
+
continue;
|
|
619
|
+
}
|
|
620
|
+
if (!isAttempt(item)) {
|
|
621
|
+
lines.push(`${localTime(item.time)} ${item.text}`);
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
const outcome = item.outcome ?? "running";
|
|
625
|
+
const end = item.endedAt ?? options.now;
|
|
626
|
+
const parts = [localTime(item.time), item.step];
|
|
627
|
+
if (item.attempt > 1) parts.push(`attempt ${item.attempt}`);
|
|
628
|
+
if (item.reason.length > 0) parts.push(item.reason);
|
|
629
|
+
parts.push(outcome, shortDuration(end - item.time));
|
|
630
|
+
if (item.detail.length > 0) parts.push(detailLine(item.detail, options.full));
|
|
631
|
+
lines.push(parts.join(" "));
|
|
632
|
+
}
|
|
633
|
+
return lines;
|
|
634
|
+
}
|