@operad-io/shell 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/README.md +23 -2
- package/package.json +21 -4
- package/src/index.ts +399 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yinghai Zhao
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @operad-io/shell
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The base `stage` plugin. It provides `shell/command`, which runs one shell
|
|
4
|
+
line; `shell/check`, which runs a list of lines and stops at the first failure;
|
|
5
|
+
and `shell/probe`, which a time or condition wait can call.
|
|
6
|
+
|
|
7
|
+
## `shell/check` result data
|
|
8
|
+
|
|
9
|
+
`shell/check` ends as `pass`, `fail` or `interrupted`. Its result's `data`:
|
|
10
|
+
|
|
11
|
+
| field | when | meaning |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| `log` | always | the step log's path |
|
|
14
|
+
| `logStart` | always | the byte offset in `log` where this invocation's output starts; a caller reading this invocation's output starts here, so output an earlier invocation wrote to the same run log is left out |
|
|
15
|
+
| `tail` | always | the last output lines |
|
|
16
|
+
| `lines` | on `pass` | how many lines ran |
|
|
17
|
+
| `line`, `lineNumber` | when the check stops on a line | that line and its 1-based position in `params.lines` |
|
|
18
|
+
| `exitCode`, `signal` | when the outcome has them | how the line's command ended |
|
|
19
|
+
| `reason` | on `fail` and `interrupted` | `cwd`, `spawn`, `timeout`, `signal`, `exit` or `cancelled` |
|
|
20
|
+
| `message` | when the outcome has one | the human text for the failure |
|
|
21
|
+
|
|
22
|
+
The log is the kernel logger's file for the step and run. It is appended to
|
|
23
|
+
across every visit of the step in that run, so an earlier visit's output stays
|
|
24
|
+
before `logStart`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@operad-io/shell",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"exports": {
|
|
6
|
+
".": "./src/index.ts"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@operad-io/engine": "0.0.1"
|
|
10
|
+
},
|
|
11
|
+
"description": "operad: the shell plugin's command and check steps (in development)",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/operad-io/operad.git",
|
|
15
|
+
"directory": "packages/shell"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"README.md",
|
|
21
|
+
"LICENSE"
|
|
22
|
+
]
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The base plugin installed with operad: it runs a shell line, a list of
|
|
3
|
+
* checks, and a probe the kernel's `wait` step can poll. The kernel never runs
|
|
4
|
+
* a command itself.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { stat } from "node:fs/promises";
|
|
9
|
+
import { definePlugin, paramsObject, type PluginLogger, type PluginStepResult, type StepContext } from "@operad-io/engine";
|
|
10
|
+
import {
|
|
11
|
+
runLine,
|
|
12
|
+
type Clock,
|
|
13
|
+
type LineOutcome,
|
|
14
|
+
} from "@operad-io/engine/platform";
|
|
15
|
+
|
|
16
|
+
function lineParam(params: Record<string, unknown>, at: string): string {
|
|
17
|
+
const line = params.line;
|
|
18
|
+
if (typeof line !== "string" || line.trim().length === 0) {
|
|
19
|
+
throw new Error(`${at}: params.line: must be a non-empty string`);
|
|
20
|
+
}
|
|
21
|
+
return line;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function cwdParam(ctx: StepContext, params: Record<string, unknown>, at: string): string {
|
|
25
|
+
const cwd = params.cwd;
|
|
26
|
+
if (cwd === undefined) return ctx.cwd;
|
|
27
|
+
if (typeof cwd !== "string" || cwd.length === 0) {
|
|
28
|
+
throw new Error(`${at}: params.cwd: must be a non-empty string`);
|
|
29
|
+
}
|
|
30
|
+
return cwd;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function timeoutParam(
|
|
34
|
+
params: Record<string, unknown>,
|
|
35
|
+
at: string,
|
|
36
|
+
): { readonly ms: number | undefined; readonly secs: number | undefined } {
|
|
37
|
+
const secs = params.timeoutSecs;
|
|
38
|
+
if (secs === undefined) return { ms: undefined, secs: undefined };
|
|
39
|
+
if (typeof secs !== "number" || !Number.isFinite(secs) || secs < 0) {
|
|
40
|
+
throw new Error(`${at}: params.timeoutSecs: must be zero or more seconds`);
|
|
41
|
+
}
|
|
42
|
+
return { ms: secs * 1000, secs };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function envParam(
|
|
46
|
+
params: Record<string, unknown>,
|
|
47
|
+
at: string,
|
|
48
|
+
): Readonly<Record<string, string>> | undefined {
|
|
49
|
+
const env = params.env;
|
|
50
|
+
if (env === undefined) return undefined;
|
|
51
|
+
if (typeof env !== "object" || env === null || Array.isArray(env)) {
|
|
52
|
+
throw new Error(`${at}: params.env: must be an object of strings`);
|
|
53
|
+
}
|
|
54
|
+
const entries: [string, string][] = [];
|
|
55
|
+
for (const [key, value] of Object.entries(env)) {
|
|
56
|
+
if (typeof value !== "string") {
|
|
57
|
+
throw new Error(`${at}: params.env.${key}: must be a string`);
|
|
58
|
+
}
|
|
59
|
+
entries.push([key, value]);
|
|
60
|
+
}
|
|
61
|
+
return Object.fromEntries(entries);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The reason a directory cannot be a working directory, if it cannot. */
|
|
65
|
+
async function directoryError(cwd: string): Promise<string | undefined> {
|
|
66
|
+
try {
|
|
67
|
+
const info = await stat(cwd);
|
|
68
|
+
return info.isDirectory() ? undefined : `cwd is not a directory: ${cwd}`;
|
|
69
|
+
} catch {
|
|
70
|
+
return `cwd does not exist: ${cwd}`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async function writeNote(logger: PluginLogger, note: string): Promise<string> {
|
|
75
|
+
return logger.write(note);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The log's size before this call writes, the byte where its output starts.
|
|
80
|
+
* A log that is not there yet starts at zero.
|
|
81
|
+
*/
|
|
82
|
+
async function logStart(logger: PluginLogger): Promise<number> {
|
|
83
|
+
try {
|
|
84
|
+
return (await stat(logger.path)).size;
|
|
85
|
+
} catch {
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function failureData(
|
|
91
|
+
outcome: LineOutcome,
|
|
92
|
+
extra: Readonly<Record<string, unknown>>,
|
|
93
|
+
): Record<string, unknown> {
|
|
94
|
+
return {
|
|
95
|
+
exitCode: outcome.exitCode,
|
|
96
|
+
signal: outcome.signal,
|
|
97
|
+
log: outcome.log,
|
|
98
|
+
tail: outcome.tail,
|
|
99
|
+
...extra,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function commandResult(
|
|
104
|
+
outcome: LineOutcome,
|
|
105
|
+
timeoutSecs: number | undefined,
|
|
106
|
+
): PluginStepResult {
|
|
107
|
+
if (outcome.spawnError !== undefined) {
|
|
108
|
+
return {
|
|
109
|
+
result: "fail",
|
|
110
|
+
data: failureData(outcome, {
|
|
111
|
+
reason: "spawn",
|
|
112
|
+
message: outcome.spawnError,
|
|
113
|
+
}),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (outcome.cancelled) {
|
|
117
|
+
return {
|
|
118
|
+
result: "interrupted",
|
|
119
|
+
data: failureData(outcome, { reason: "cancelled" }),
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (outcome.timedOut) {
|
|
123
|
+
const limit = timeoutSecs === undefined ? "" : ` after ${timeoutSecs}s`;
|
|
124
|
+
return {
|
|
125
|
+
result: "fail",
|
|
126
|
+
data: failureData(outcome, {
|
|
127
|
+
reason: "timeout",
|
|
128
|
+
message: `the command was stopped${limit}`,
|
|
129
|
+
}),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
if (outcome.exitCode === 0) {
|
|
133
|
+
return { result: "pass", data: failureData(outcome, {}) };
|
|
134
|
+
}
|
|
135
|
+
const killed =
|
|
136
|
+
outcome.exitCode === null && outcome.signal !== null
|
|
137
|
+
? `the command was killed by ${outcome.signal}`
|
|
138
|
+
: `the command exited ${String(outcome.exitCode)}`;
|
|
139
|
+
return {
|
|
140
|
+
result: "fail",
|
|
141
|
+
data: failureData(outcome, {
|
|
142
|
+
reason: outcome.exitCode === null ? "signal" : "exit",
|
|
143
|
+
message: killed,
|
|
144
|
+
}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function probeId(runId: string, cwd: string, line: string): string {
|
|
149
|
+
return createHash("sha256")
|
|
150
|
+
.update(JSON.stringify([runId, cwd, line]))
|
|
151
|
+
.digest("hex");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Options for the shell plugin. A test injects a clock to control timeouts. */
|
|
155
|
+
export interface ShellPluginOptions {
|
|
156
|
+
readonly clock?: Clock | undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function createShellPlugin(options: ShellPluginOptions = {}) {
|
|
160
|
+
const clock = options.clock;
|
|
161
|
+
return definePlugin({
|
|
162
|
+
name: "shell",
|
|
163
|
+
kind: "stage",
|
|
164
|
+
version: "0.0.0",
|
|
165
|
+
steps: [
|
|
166
|
+
{
|
|
167
|
+
name: "command",
|
|
168
|
+
results: ["pass", "fail", "interrupted"],
|
|
169
|
+
async run(ctx, params) {
|
|
170
|
+
const at = "shell/command";
|
|
171
|
+
const input = paramsObject(params, at);
|
|
172
|
+
const line = lineParam(input, at);
|
|
173
|
+
const cwd = cwdParam(ctx, input, at);
|
|
174
|
+
const env = envParam(input, at);
|
|
175
|
+
const timeout = timeoutParam(input, at);
|
|
176
|
+
|
|
177
|
+
const cwdProblem = await directoryError(cwd);
|
|
178
|
+
if (cwdProblem !== undefined) {
|
|
179
|
+
const log = await writeNote(ctx.logger, cwdProblem);
|
|
180
|
+
return {
|
|
181
|
+
result: "fail",
|
|
182
|
+
data: {
|
|
183
|
+
reason: "cwd",
|
|
184
|
+
message: cwdProblem,
|
|
185
|
+
exitCode: null,
|
|
186
|
+
log,
|
|
187
|
+
tail: [cwdProblem],
|
|
188
|
+
},
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const outcome = await runLine({
|
|
193
|
+
line,
|
|
194
|
+
cwd,
|
|
195
|
+
env,
|
|
196
|
+
timeoutMs: timeout.ms,
|
|
197
|
+
signal: ctx.signal,
|
|
198
|
+
logger: ctx.logger,
|
|
199
|
+
clock,
|
|
200
|
+
});
|
|
201
|
+
return commandResult(outcome, timeout.secs);
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
name: "check",
|
|
206
|
+
results: ["pass", "fail", "interrupted"],
|
|
207
|
+
async run(ctx, params) {
|
|
208
|
+
const at = "shell/check";
|
|
209
|
+
const input = paramsObject(params, at);
|
|
210
|
+
const lines = input.lines;
|
|
211
|
+
if (
|
|
212
|
+
!Array.isArray(lines) ||
|
|
213
|
+
!lines.every(
|
|
214
|
+
(line): line is string =>
|
|
215
|
+
typeof line === "string" && line.trim().length > 0,
|
|
216
|
+
)
|
|
217
|
+
) {
|
|
218
|
+
throw new Error(
|
|
219
|
+
`${at}: params.lines: must be an array of non-empty strings`,
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
const cwd = cwdParam(ctx, input, at);
|
|
223
|
+
const timeout = timeoutParam(input, at);
|
|
224
|
+
const start = await logStart(ctx.logger);
|
|
225
|
+
|
|
226
|
+
const cwdProblem = await directoryError(cwd);
|
|
227
|
+
if (cwdProblem !== undefined) {
|
|
228
|
+
const log = await writeNote(ctx.logger, cwdProblem);
|
|
229
|
+
return {
|
|
230
|
+
result: "fail",
|
|
231
|
+
data: {
|
|
232
|
+
reason: "cwd",
|
|
233
|
+
message: cwdProblem,
|
|
234
|
+
exitCode: null,
|
|
235
|
+
log,
|
|
236
|
+
logStart: start,
|
|
237
|
+
tail: [cwdProblem],
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (lines.length === 0) {
|
|
243
|
+
const log = await ctx.logger.write("");
|
|
244
|
+
return { result: "pass", data: { lines: 0, log, logStart: start, tail: [] } };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
for (const [index, line] of lines.entries()) {
|
|
248
|
+
if (ctx.signal.aborted) {
|
|
249
|
+
// No line has written output yet: create the log the fact names,
|
|
250
|
+
// so the path is a file a reader can open.
|
|
251
|
+
const log = await writeNote(ctx.logger, "");
|
|
252
|
+
return {
|
|
253
|
+
result: "interrupted",
|
|
254
|
+
data: {
|
|
255
|
+
reason: "cancelled",
|
|
256
|
+
line,
|
|
257
|
+
lineNumber: index + 1,
|
|
258
|
+
log,
|
|
259
|
+
logStart: start,
|
|
260
|
+
tail: [],
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const outcome = await runLine({
|
|
266
|
+
line,
|
|
267
|
+
cwd,
|
|
268
|
+
timeoutMs: timeout.ms,
|
|
269
|
+
signal: ctx.signal,
|
|
270
|
+
logger: ctx.logger,
|
|
271
|
+
clock,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
if (outcome.cancelled) {
|
|
275
|
+
return {
|
|
276
|
+
result: "interrupted",
|
|
277
|
+
data: failureData(outcome, {
|
|
278
|
+
reason: "cancelled",
|
|
279
|
+
line,
|
|
280
|
+
lineNumber: index + 1,
|
|
281
|
+
logStart: start,
|
|
282
|
+
}),
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (
|
|
287
|
+
outcome.spawnError !== undefined ||
|
|
288
|
+
outcome.timedOut ||
|
|
289
|
+
outcome.exitCode !== 0
|
|
290
|
+
) {
|
|
291
|
+
const reason =
|
|
292
|
+
outcome.spawnError !== undefined
|
|
293
|
+
? "spawn"
|
|
294
|
+
: outcome.timedOut
|
|
295
|
+
? "timeout"
|
|
296
|
+
: outcome.exitCode === null
|
|
297
|
+
? "signal"
|
|
298
|
+
: "exit";
|
|
299
|
+
const message =
|
|
300
|
+
outcome.spawnError ??
|
|
301
|
+
(outcome.timedOut
|
|
302
|
+
? `check line ${index + 1} was stopped after ${String(timeout.secs)}s`
|
|
303
|
+
: outcome.exitCode === null
|
|
304
|
+
? `check line ${index + 1} was killed by ${String(outcome.signal)}`
|
|
305
|
+
: `check line ${index + 1} exited ${String(outcome.exitCode)}`);
|
|
306
|
+
return {
|
|
307
|
+
result: "fail",
|
|
308
|
+
data: failureData(outcome, {
|
|
309
|
+
reason,
|
|
310
|
+
message,
|
|
311
|
+
line,
|
|
312
|
+
lineNumber: index + 1,
|
|
313
|
+
logStart: start,
|
|
314
|
+
}),
|
|
315
|
+
};
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return {
|
|
320
|
+
result: "pass",
|
|
321
|
+
data: {
|
|
322
|
+
lines: lines.length,
|
|
323
|
+
log: ctx.logger.path,
|
|
324
|
+
logStart: start,
|
|
325
|
+
tail: [],
|
|
326
|
+
},
|
|
327
|
+
};
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
name: "probe",
|
|
332
|
+
results: ["ready", "waiting", "interrupted"],
|
|
333
|
+
async run(ctx, params) {
|
|
334
|
+
const at = "shell/probe";
|
|
335
|
+
const input = paramsObject(params, at);
|
|
336
|
+
const line = lineParam(input, at);
|
|
337
|
+
const cwd = cwdParam(ctx, input, at);
|
|
338
|
+
const timeout = timeoutParam(input, at);
|
|
339
|
+
|
|
340
|
+
const cwdProblem = await directoryError(cwd);
|
|
341
|
+
if (cwdProblem !== undefined) {
|
|
342
|
+
return {
|
|
343
|
+
result: "interrupted",
|
|
344
|
+
data: { reason: "cwd", message: cwdProblem },
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const outcome = await runLine({
|
|
349
|
+
line,
|
|
350
|
+
cwd,
|
|
351
|
+
timeoutMs: timeout.ms,
|
|
352
|
+
signal: ctx.signal,
|
|
353
|
+
clock,
|
|
354
|
+
});
|
|
355
|
+
if (outcome.cancelled) {
|
|
356
|
+
return { result: "interrupted", data: { reason: "cancelled" } };
|
|
357
|
+
}
|
|
358
|
+
if (outcome.spawnError !== undefined) {
|
|
359
|
+
return {
|
|
360
|
+
result: "interrupted",
|
|
361
|
+
data: { reason: "spawn", message: outcome.spawnError },
|
|
362
|
+
};
|
|
363
|
+
}
|
|
364
|
+
if (outcome.timedOut) {
|
|
365
|
+
return {
|
|
366
|
+
result: "interrupted",
|
|
367
|
+
data: {
|
|
368
|
+
reason: "timeout",
|
|
369
|
+
message: `the probe was stopped after ${String(timeout.secs)}s`,
|
|
370
|
+
},
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
const exitCode = outcome.exitCode;
|
|
375
|
+
const id = probeId(ctx.runId, cwd, line);
|
|
376
|
+
const previous = await ctx.store.get("probe", id);
|
|
377
|
+
const data = { line, cwd, exitCode };
|
|
378
|
+
if (previous === undefined) {
|
|
379
|
+
await ctx.store.put("probe", id, data, "probe");
|
|
380
|
+
return { result: "waiting", data };
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
const before = previous.data.exitCode;
|
|
384
|
+
const previousCode = typeof before === "number" ? before : null;
|
|
385
|
+
if (previousCode !== exitCode) {
|
|
386
|
+
await ctx.store.put("probe", id, data, "probe");
|
|
387
|
+
return { result: "ready", data };
|
|
388
|
+
}
|
|
389
|
+
return { result: "waiting", data };
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
],
|
|
393
|
+
check() {
|
|
394
|
+
return "ready";
|
|
395
|
+
},
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export default createShellPlugin();
|