@agentsmarket/pipeline-runtime 0.3.0 → 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/dist/file-output.d.ts +38 -0
- package/dist/file-output.js +141 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/model-resolver.d.ts +14 -0
- package/dist/shell-stage.d.ts +82 -0
- package/dist/shell-stage.js +169 -0
- package/dist/stages.d.ts +9 -0
- package/dist/stages.js +52 -2
- package/package.json +1 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P5 — Server-side file output collection.
|
|
3
|
+
*
|
|
4
|
+
* Scans the outputs of every executed stage for a structured
|
|
5
|
+
* `produced_files` array, extracts each file's content, and validates
|
|
6
|
+
* against the pipeline's declared `output.files` glob patterns.
|
|
7
|
+
*
|
|
8
|
+
* Stage authors opt-in by emitting structured JSON output of the shape:
|
|
9
|
+
* {
|
|
10
|
+
* "...": "...",
|
|
11
|
+
* "produced_files": [
|
|
12
|
+
* { "name": "report.md", "content_type": "text/markdown", "content_b64": "IyBUaXRsZQo..." }
|
|
13
|
+
* ]
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* Pure functions only — no I/O, no Cloudflare bindings, no Node.js
|
|
17
|
+
* primitives beyond `atob` (available in both V8 isolates and Node ≥16).
|
|
18
|
+
* The caller (server invoke.ts) is responsible for uploading the decoded
|
|
19
|
+
* bytes to R2 and generating signed URLs.
|
|
20
|
+
*/
|
|
21
|
+
import type { PipelineOutputSpec } from './output-spec.js';
|
|
22
|
+
export interface ProducedFile {
|
|
23
|
+
stage_id: string;
|
|
24
|
+
name: string;
|
|
25
|
+
size: number;
|
|
26
|
+
content_type: string;
|
|
27
|
+
content_b64: string;
|
|
28
|
+
}
|
|
29
|
+
export interface ExtractResult {
|
|
30
|
+
files: ProducedFile[];
|
|
31
|
+
errors: string[];
|
|
32
|
+
warnings: string[];
|
|
33
|
+
}
|
|
34
|
+
export declare const MAX_FILE_BYTES: number;
|
|
35
|
+
export declare const MAX_TOTAL_BYTES: number;
|
|
36
|
+
export declare const MAX_FILES = 100;
|
|
37
|
+
export declare function extractProducedFiles(stageOutputs: Record<string, string>, spec: PipelineOutputSpec): ExtractResult;
|
|
38
|
+
export declare function globToRegex(pattern: string): RegExp;
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P5 — Server-side file output collection.
|
|
3
|
+
*
|
|
4
|
+
* Scans the outputs of every executed stage for a structured
|
|
5
|
+
* `produced_files` array, extracts each file's content, and validates
|
|
6
|
+
* against the pipeline's declared `output.files` glob patterns.
|
|
7
|
+
*
|
|
8
|
+
* Stage authors opt-in by emitting structured JSON output of the shape:
|
|
9
|
+
* {
|
|
10
|
+
* "...": "...",
|
|
11
|
+
* "produced_files": [
|
|
12
|
+
* { "name": "report.md", "content_type": "text/markdown", "content_b64": "IyBUaXRsZQo..." }
|
|
13
|
+
* ]
|
|
14
|
+
* }
|
|
15
|
+
*
|
|
16
|
+
* Pure functions only — no I/O, no Cloudflare bindings, no Node.js
|
|
17
|
+
* primitives beyond `atob` (available in both V8 isolates and Node ≥16).
|
|
18
|
+
* The caller (server invoke.ts) is responsible for uploading the decoded
|
|
19
|
+
* bytes to R2 and generating signed URLs.
|
|
20
|
+
*/
|
|
21
|
+
export const MAX_FILE_BYTES = 10 * 1024 * 1024;
|
|
22
|
+
export const MAX_TOTAL_BYTES = 50 * 1024 * 1024;
|
|
23
|
+
export const MAX_FILES = 100;
|
|
24
|
+
export function extractProducedFiles(stageOutputs, spec) {
|
|
25
|
+
const files = [];
|
|
26
|
+
const errors = [];
|
|
27
|
+
const warnings = [];
|
|
28
|
+
let totalBytes = 0;
|
|
29
|
+
for (const [stageId, text] of Object.entries(stageOutputs)) {
|
|
30
|
+
let parsed;
|
|
31
|
+
try {
|
|
32
|
+
parsed = JSON.parse(text);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
if (!parsed || typeof parsed !== 'object')
|
|
38
|
+
continue;
|
|
39
|
+
const obj = parsed;
|
|
40
|
+
const produced = obj.produced_files;
|
|
41
|
+
if (!Array.isArray(produced))
|
|
42
|
+
continue;
|
|
43
|
+
for (const entry of produced) {
|
|
44
|
+
if (!entry || typeof entry !== 'object') {
|
|
45
|
+
warnings.push(`stage '${stageId}' produced_files entry is not an object`);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const rec = entry;
|
|
49
|
+
const name = rec.name;
|
|
50
|
+
const content_b64 = rec.content_b64;
|
|
51
|
+
const content_type = rec.content_type ?? 'application/octet-stream';
|
|
52
|
+
if (typeof name !== 'string' || name.length === 0) {
|
|
53
|
+
errors.push(`stage '${stageId}' produced_files entry missing 'name'`);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (name.includes('/') || name.includes('\\') || name.includes('..')) {
|
|
57
|
+
errors.push(`stage '${stageId}' produced_files name '${name}' contains path separators or traversal`);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (typeof content_b64 !== 'string' || content_b64.length === 0) {
|
|
61
|
+
errors.push(`stage '${stageId}' produced_files entry for '${name}' missing or empty 'content_b64'`);
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
if (typeof content_type !== 'string') {
|
|
65
|
+
errors.push(`stage '${stageId}' produced_files entry for '${name}' has non-string 'content_type'`);
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
let decodedLen;
|
|
69
|
+
try {
|
|
70
|
+
decodedLen = atob(content_b64).length;
|
|
71
|
+
}
|
|
72
|
+
catch (e) {
|
|
73
|
+
errors.push(`stage '${stageId}' produced_files entry for '${name}' has invalid base64: ${e.message}`);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (decodedLen > MAX_FILE_BYTES) {
|
|
77
|
+
errors.push(`stage '${stageId}' produced file '${name}' is ${decodedLen} bytes (max ${MAX_FILE_BYTES})`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (totalBytes + decodedLen > MAX_TOTAL_BYTES) {
|
|
81
|
+
errors.push(`total file size exceeds ${MAX_TOTAL_BYTES} bytes; aborted at stage '${stageId}'`);
|
|
82
|
+
return { files, errors, warnings };
|
|
83
|
+
}
|
|
84
|
+
if (files.length >= MAX_FILES) {
|
|
85
|
+
errors.push(`produced_files count exceeds ${MAX_FILES}; aborted at stage '${stageId}'`);
|
|
86
|
+
return { files, errors, warnings };
|
|
87
|
+
}
|
|
88
|
+
totalBytes += decodedLen;
|
|
89
|
+
files.push({
|
|
90
|
+
stage_id: stageId,
|
|
91
|
+
name,
|
|
92
|
+
size: decodedLen,
|
|
93
|
+
content_type,
|
|
94
|
+
content_b64,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (spec.files && spec.files.length > 0 && files.length > 0) {
|
|
99
|
+
for (const pattern of spec.files) {
|
|
100
|
+
const regex = globToRegex(pattern);
|
|
101
|
+
const matches = files.some((f) => regex.test(f.name));
|
|
102
|
+
if (!matches) {
|
|
103
|
+
warnings.push(`output.files pattern '${pattern}' did not match any produced file`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { files, errors, warnings };
|
|
108
|
+
}
|
|
109
|
+
export function globToRegex(pattern) {
|
|
110
|
+
let regex = '';
|
|
111
|
+
let i = 0;
|
|
112
|
+
while (i < pattern.length) {
|
|
113
|
+
const c = pattern[i];
|
|
114
|
+
if (c === '*' && pattern[i + 1] === '*') {
|
|
115
|
+
regex += '.*';
|
|
116
|
+
i += 2;
|
|
117
|
+
if (pattern[i] === '/')
|
|
118
|
+
i += 1;
|
|
119
|
+
}
|
|
120
|
+
else if (c === '*') {
|
|
121
|
+
regex += '[^/]*';
|
|
122
|
+
i += 1;
|
|
123
|
+
}
|
|
124
|
+
else if (c === '?') {
|
|
125
|
+
regex += '[^/]';
|
|
126
|
+
i += 1;
|
|
127
|
+
}
|
|
128
|
+
else if (c && '.+^$()|{}[]\\'.includes(c)) {
|
|
129
|
+
regex += '\\' + c;
|
|
130
|
+
i += 1;
|
|
131
|
+
}
|
|
132
|
+
else if (c) {
|
|
133
|
+
regex += c;
|
|
134
|
+
i += 1;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
i += 1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return new RegExp('^' + regex + '$');
|
|
141
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -40,6 +40,8 @@ export { resolveInputRefs, resolveSpecialKeys, type PipelineRefContext, } from '
|
|
|
40
40
|
export { isToolSkill, invokeToolSkill, type ToolSkillContext, type ToolSkillResult, type ToolSkillHandler, } from './tools/tool-skills.js';
|
|
41
41
|
export { validateOutputSpec, DEFAULT_OUTPUT_FORMAT, DEFAULT_OUTPUT_DELIVERY, DEFAULT_OUTPUT_EXPIRES_IN, MIN_OUTPUT_EXPIRES_IN, MAX_OUTPUT_EXPIRES_IN, type PipelineOutputSpec, type PipelineOutputDelivery, type PipelineOutputFormat, type PipelineOutputSpecError, } from './output-spec.js';
|
|
42
42
|
export { analyzeCapabilities, type PipelineCapabilities, type NetworkCapability, type PipelineSpecLikeForCaps, type PipelineStageLikeForCaps, } from './capabilities.js';
|
|
43
|
+
export { extractProducedFiles, globToRegex, MAX_FILE_BYTES, MAX_TOTAL_BYTES, MAX_FILES, type ProducedFile, type ExtractResult, } from './file-output.js';
|
|
44
|
+
export { normalizeShellStage, ThrowingShellExecutor, NodeShellExecutor, ShellStageUnsupportedError, ShellStageExecError, ShellStageTimeoutError, SHELL_DEFAULT_TIMEOUT_MS, SHELL_MAX_TIMEOUT_MS, SHELL_MAX_OUTPUT_BYTES, type ShellStageSpec, type ShellExecutor, type ShellExecOptions, type ShellExecResult, } from './shell-stage.js';
|
|
43
45
|
export interface PipelineStageSpec {
|
|
44
46
|
id: string;
|
|
45
47
|
model: string;
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,8 @@ export { resolveInputRefs, resolveSpecialKeys, } from './pipeline-refs.js';
|
|
|
27
27
|
export { isToolSkill, invokeToolSkill, } from './tools/tool-skills.js';
|
|
28
28
|
export { validateOutputSpec, DEFAULT_OUTPUT_FORMAT, DEFAULT_OUTPUT_DELIVERY, DEFAULT_OUTPUT_EXPIRES_IN, MIN_OUTPUT_EXPIRES_IN, MAX_OUTPUT_EXPIRES_IN, } from './output-spec.js';
|
|
29
29
|
export { analyzeCapabilities, } from './capabilities.js';
|
|
30
|
+
export { extractProducedFiles, globToRegex, MAX_FILE_BYTES, MAX_TOTAL_BYTES, MAX_FILES, } from './file-output.js';
|
|
31
|
+
export { normalizeShellStage, ThrowingShellExecutor, NodeShellExecutor, ShellStageUnsupportedError, ShellStageExecError, ShellStageTimeoutError, SHELL_DEFAULT_TIMEOUT_MS, SHELL_MAX_TIMEOUT_MS, SHELL_MAX_OUTPUT_BYTES, } from './shell-stage.js';
|
|
30
32
|
export async function runStage(stage, ctx) {
|
|
31
33
|
const vars = [];
|
|
32
34
|
for (const [key, val] of Object.entries(ctx.outputs)) {
|
package/dist/model-resolver.d.ts
CHANGED
|
@@ -59,6 +59,20 @@ export interface PipelineStageLike {
|
|
|
59
59
|
* file helpers in stage context.
|
|
60
60
|
*/
|
|
61
61
|
working_dir?: string;
|
|
62
|
+
/**
|
|
63
|
+
* P4 NEW — shell stage. Either a command string (`"npm test"`) or an
|
|
64
|
+
* object `{ cmd, args?, cwd?, timeout_ms? }`. Executed only when the
|
|
65
|
+
* pipeline runs through `pipeline call --local`; server-side invokes
|
|
66
|
+
* throw `ShellStageUnsupportedError` with HTTP 400 SHELL_UNSUPPORTED_ON_SERVER.
|
|
67
|
+
*
|
|
68
|
+
* Mutually exclusive with `uses` / `model`+`prompt`.
|
|
69
|
+
*/
|
|
70
|
+
shell?: string | {
|
|
71
|
+
cmd: string;
|
|
72
|
+
args?: string[];
|
|
73
|
+
cwd?: string;
|
|
74
|
+
timeout_ms?: number;
|
|
75
|
+
};
|
|
62
76
|
/**
|
|
63
77
|
* Provider name (e.g. "minimax", "openrouter"). v0.3 NEW (optional in
|
|
64
78
|
* v0.3 transition; required once pickProvider auto-dispatch is removed).
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P4 — Shell stages (CLI-only).
|
|
3
|
+
*
|
|
4
|
+
* A `shell` stage lets a pipeline run a local command as part of its DAG.
|
|
5
|
+
* Useful for: running tests, linting, formatting, refactoring tools that
|
|
6
|
+
* live on the local machine.
|
|
7
|
+
*
|
|
8
|
+
* **Server-side is REJECTED.** Cloudflare Workers cannot spawn child
|
|
9
|
+
* processes. A pipeline with shell stages works only via `pipeline call
|
|
10
|
+
* --local`. The server explicitly throws `ShellStageUnsupportedError` so
|
|
11
|
+
* the user gets a clear 400 instead of a confusing 500.
|
|
12
|
+
*
|
|
13
|
+
* **Execution model:**
|
|
14
|
+
* - `spawn(cmd, args, { shell: false })` — no shell interpretation, args
|
|
15
|
+
* are passed verbatim to the OS as argv. Prevents shell injection.
|
|
16
|
+
* - `timeout` option sends SIGTERM after `timeout_ms`; the executor
|
|
17
|
+
* maps that to `ShellStageTimeoutError`.
|
|
18
|
+
* - Non-zero exit code → `ShellStageExecError` with stderr in message.
|
|
19
|
+
*
|
|
20
|
+
* **Why dynamic import of `node:child_process`:** the runtime package is
|
|
21
|
+
* also loaded by Cloudflare Workers (which have no Node bindings). Importing
|
|
22
|
+
* `child_process` at module top-level would crash Workers startup. The
|
|
23
|
+
* `NodeShellExecutor` lazily imports it inside `.exec()` so Workers
|
|
24
|
+
* never touch the binding (the server uses a throwing stub).
|
|
25
|
+
*
|
|
26
|
+
* Stage schema (two forms):
|
|
27
|
+
* shell: "npm test" # shorthand: auto-split
|
|
28
|
+
* shell: # explicit form
|
|
29
|
+
* cmd: "npm"
|
|
30
|
+
* args: ["test"]
|
|
31
|
+
* cwd: "./my-project"
|
|
32
|
+
* timeout_ms: 60000
|
|
33
|
+
*/
|
|
34
|
+
export interface ShellStageSpec {
|
|
35
|
+
cmd: string;
|
|
36
|
+
args?: string[];
|
|
37
|
+
cwd?: string;
|
|
38
|
+
timeout_ms?: number;
|
|
39
|
+
}
|
|
40
|
+
export declare const SHELL_DEFAULT_TIMEOUT_MS = 30000;
|
|
41
|
+
export declare const SHELL_MAX_OUTPUT_BYTES: number;
|
|
42
|
+
export declare const SHELL_MAX_TIMEOUT_MS = 600000;
|
|
43
|
+
export declare function normalizeShellStage(raw: unknown): ShellStageSpec | null;
|
|
44
|
+
export interface ShellExecOptions {
|
|
45
|
+
cwd: string;
|
|
46
|
+
timeoutMs: number;
|
|
47
|
+
}
|
|
48
|
+
export interface ShellExecResult {
|
|
49
|
+
stdout: string;
|
|
50
|
+
stderr: string;
|
|
51
|
+
exitCode: number;
|
|
52
|
+
durationMs: number;
|
|
53
|
+
cmd: string;
|
|
54
|
+
args: string[];
|
|
55
|
+
}
|
|
56
|
+
export interface ShellExecutor {
|
|
57
|
+
exec(spec: ShellStageSpec, opts: ShellExecOptions): Promise<ShellExecResult>;
|
|
58
|
+
}
|
|
59
|
+
export declare class ShellStageUnsupportedError extends Error {
|
|
60
|
+
readonly stageId: string;
|
|
61
|
+
readonly reason: string;
|
|
62
|
+
constructor(stageId: string, reason: string);
|
|
63
|
+
}
|
|
64
|
+
export declare class ShellStageExecError extends Error {
|
|
65
|
+
readonly stageId: string;
|
|
66
|
+
readonly exitCode: number;
|
|
67
|
+
readonly stderrSnippet: string;
|
|
68
|
+
constructor(stageId: string, exitCode: number, stderrSnippet: string);
|
|
69
|
+
}
|
|
70
|
+
export declare class ShellStageTimeoutError extends Error {
|
|
71
|
+
readonly stageId: string;
|
|
72
|
+
readonly timeoutMs: number;
|
|
73
|
+
constructor(stageId: string, timeoutMs: number);
|
|
74
|
+
}
|
|
75
|
+
export declare class ThrowingShellExecutor implements ShellExecutor {
|
|
76
|
+
private readonly reason;
|
|
77
|
+
constructor(reason: string);
|
|
78
|
+
exec(): Promise<ShellExecResult>;
|
|
79
|
+
}
|
|
80
|
+
export declare class NodeShellExecutor implements ShellExecutor {
|
|
81
|
+
exec(spec: ShellStageSpec, opts: ShellExecOptions): Promise<ShellExecResult>;
|
|
82
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P4 — Shell stages (CLI-only).
|
|
3
|
+
*
|
|
4
|
+
* A `shell` stage lets a pipeline run a local command as part of its DAG.
|
|
5
|
+
* Useful for: running tests, linting, formatting, refactoring tools that
|
|
6
|
+
* live on the local machine.
|
|
7
|
+
*
|
|
8
|
+
* **Server-side is REJECTED.** Cloudflare Workers cannot spawn child
|
|
9
|
+
* processes. A pipeline with shell stages works only via `pipeline call
|
|
10
|
+
* --local`. The server explicitly throws `ShellStageUnsupportedError` so
|
|
11
|
+
* the user gets a clear 400 instead of a confusing 500.
|
|
12
|
+
*
|
|
13
|
+
* **Execution model:**
|
|
14
|
+
* - `spawn(cmd, args, { shell: false })` — no shell interpretation, args
|
|
15
|
+
* are passed verbatim to the OS as argv. Prevents shell injection.
|
|
16
|
+
* - `timeout` option sends SIGTERM after `timeout_ms`; the executor
|
|
17
|
+
* maps that to `ShellStageTimeoutError`.
|
|
18
|
+
* - Non-zero exit code → `ShellStageExecError` with stderr in message.
|
|
19
|
+
*
|
|
20
|
+
* **Why dynamic import of `node:child_process`:** the runtime package is
|
|
21
|
+
* also loaded by Cloudflare Workers (which have no Node bindings). Importing
|
|
22
|
+
* `child_process` at module top-level would crash Workers startup. The
|
|
23
|
+
* `NodeShellExecutor` lazily imports it inside `.exec()` so Workers
|
|
24
|
+
* never touch the binding (the server uses a throwing stub).
|
|
25
|
+
*
|
|
26
|
+
* Stage schema (two forms):
|
|
27
|
+
* shell: "npm test" # shorthand: auto-split
|
|
28
|
+
* shell: # explicit form
|
|
29
|
+
* cmd: "npm"
|
|
30
|
+
* args: ["test"]
|
|
31
|
+
* cwd: "./my-project"
|
|
32
|
+
* timeout_ms: 60000
|
|
33
|
+
*/
|
|
34
|
+
export const SHELL_DEFAULT_TIMEOUT_MS = 30_000;
|
|
35
|
+
export const SHELL_MAX_OUTPUT_BYTES = 1024 * 1024;
|
|
36
|
+
export const SHELL_MAX_TIMEOUT_MS = 600_000;
|
|
37
|
+
export function normalizeShellStage(raw) {
|
|
38
|
+
if (typeof raw === 'string') {
|
|
39
|
+
const trimmed = raw.trim();
|
|
40
|
+
if (trimmed.length === 0)
|
|
41
|
+
return null;
|
|
42
|
+
const parts = trimmed.split(/\s+/);
|
|
43
|
+
return { cmd: parts[0], args: parts.slice(1) };
|
|
44
|
+
}
|
|
45
|
+
if (!raw || typeof raw !== 'object')
|
|
46
|
+
return null;
|
|
47
|
+
const obj = raw;
|
|
48
|
+
if (typeof obj.cmd !== 'string' || obj.cmd.trim().length === 0)
|
|
49
|
+
return null;
|
|
50
|
+
const spec = { cmd: obj.cmd.trim() };
|
|
51
|
+
if (Array.isArray(obj.args)) {
|
|
52
|
+
const args = obj.args.filter((a) => typeof a === 'string');
|
|
53
|
+
if (args.length > 0)
|
|
54
|
+
spec.args = args;
|
|
55
|
+
}
|
|
56
|
+
if (typeof obj.cwd === 'string' && obj.cwd.length > 0)
|
|
57
|
+
spec.cwd = obj.cwd;
|
|
58
|
+
if (typeof obj.timeout_ms === 'number' && Number.isFinite(obj.timeout_ms) && obj.timeout_ms > 0) {
|
|
59
|
+
spec.timeout_ms = Math.min(obj.timeout_ms, SHELL_MAX_TIMEOUT_MS);
|
|
60
|
+
}
|
|
61
|
+
return spec;
|
|
62
|
+
}
|
|
63
|
+
export class ShellStageUnsupportedError extends Error {
|
|
64
|
+
stageId;
|
|
65
|
+
reason;
|
|
66
|
+
constructor(stageId, reason) {
|
|
67
|
+
super(`Stage '${stageId}' uses a shell command but ${reason}`);
|
|
68
|
+
this.stageId = stageId;
|
|
69
|
+
this.reason = reason;
|
|
70
|
+
this.name = 'SHELL_STAGE_UNSUPPORTED';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
export class ShellStageExecError extends Error {
|
|
74
|
+
stageId;
|
|
75
|
+
exitCode;
|
|
76
|
+
stderrSnippet;
|
|
77
|
+
constructor(stageId, exitCode, stderrSnippet) {
|
|
78
|
+
super(`Shell stage '${stageId}' exited with code ${exitCode}: ${stderrSnippet.slice(0, 500)}`);
|
|
79
|
+
this.stageId = stageId;
|
|
80
|
+
this.exitCode = exitCode;
|
|
81
|
+
this.stderrSnippet = stderrSnippet;
|
|
82
|
+
this.name = 'SHELL_STAGE_EXEC_FAILED';
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export class ShellStageTimeoutError extends Error {
|
|
86
|
+
stageId;
|
|
87
|
+
timeoutMs;
|
|
88
|
+
constructor(stageId, timeoutMs) {
|
|
89
|
+
super(`Shell stage '${stageId}' exceeded timeout of ${timeoutMs}ms`);
|
|
90
|
+
this.stageId = stageId;
|
|
91
|
+
this.timeoutMs = timeoutMs;
|
|
92
|
+
this.name = 'SHELL_STAGE_TIMEOUT';
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export class ThrowingShellExecutor {
|
|
96
|
+
reason;
|
|
97
|
+
constructor(reason) {
|
|
98
|
+
this.reason = reason;
|
|
99
|
+
}
|
|
100
|
+
async exec() {
|
|
101
|
+
throw new ShellStageUnsupportedError('<unknown>', this.reason);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
export class NodeShellExecutor {
|
|
105
|
+
async exec(spec, opts) {
|
|
106
|
+
const { spawn } = await import('node:child_process');
|
|
107
|
+
const args = spec.args ?? [];
|
|
108
|
+
const t0 = Date.now();
|
|
109
|
+
return new Promise((resolve, reject) => {
|
|
110
|
+
const child = spawn(spec.cmd, args, {
|
|
111
|
+
cwd: opts.cwd,
|
|
112
|
+
timeout: opts.timeoutMs,
|
|
113
|
+
shell: false,
|
|
114
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
115
|
+
});
|
|
116
|
+
let stdout = '';
|
|
117
|
+
let stderr = '';
|
|
118
|
+
let stdoutTruncated = false;
|
|
119
|
+
let stderrTruncated = false;
|
|
120
|
+
const onChunk = (target) => (chunk) => {
|
|
121
|
+
const text = typeof chunk === 'string' ? chunk : chunk.toString('utf-8');
|
|
122
|
+
if (target === 'stdout') {
|
|
123
|
+
if (stdout.length + text.length > SHELL_MAX_OUTPUT_BYTES) {
|
|
124
|
+
if (!stdoutTruncated) {
|
|
125
|
+
stdout = stdout.slice(0, SHELL_MAX_OUTPUT_BYTES) + '\n[stdout truncated at 1MB]';
|
|
126
|
+
stdoutTruncated = true;
|
|
127
|
+
}
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
stdout += text;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
if (stderr.length + text.length > SHELL_MAX_OUTPUT_BYTES) {
|
|
134
|
+
if (!stderrTruncated) {
|
|
135
|
+
stderr = stderr.slice(0, SHELL_MAX_OUTPUT_BYTES) + '\n[stderr truncated at 1MB]';
|
|
136
|
+
stderrTruncated = true;
|
|
137
|
+
}
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
stderr += text;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
child.stdout.on('data', onChunk('stdout'));
|
|
144
|
+
child.stderr.on('data', onChunk('stderr'));
|
|
145
|
+
let settled = false;
|
|
146
|
+
const settle = (fn) => {
|
|
147
|
+
if (settled)
|
|
148
|
+
return;
|
|
149
|
+
settled = true;
|
|
150
|
+
fn();
|
|
151
|
+
};
|
|
152
|
+
child.on('error', (e) => {
|
|
153
|
+
settle(() => reject(e));
|
|
154
|
+
});
|
|
155
|
+
child.on('close', (code, signal) => {
|
|
156
|
+
const durationMs = Date.now() - t0;
|
|
157
|
+
if (signal === 'SIGTERM' || signal === 'SIGKILL') {
|
|
158
|
+
settle(() => reject(new ShellStageTimeoutError('<shell>', opts.timeoutMs)));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (code === 0) {
|
|
162
|
+
settle(() => resolve({ stdout, stderr, exitCode: 0, durationMs, cmd: spec.cmd, args }));
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
settle(() => reject(new ShellStageExecError('<shell>', code ?? -1, stderr || stdout)));
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
}
|
package/dist/stages.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ import type { PricingResolver } from './pricing.js';
|
|
|
18
18
|
import { createModelResolver, type ModelResolver, type PipelineLike, type PipelineStageLike, type SkillLike } from './model-resolver.js';
|
|
19
19
|
import { type PipelineEventSink } from './streaming-events.js';
|
|
20
20
|
import { type RuntimeConstraints } from './runtime-constraints.js';
|
|
21
|
+
import { type ShellExecutor } from './shell-stage.js';
|
|
21
22
|
import { type PipelineFetcher, type CompositionConfig } from './composition.js';
|
|
22
23
|
export interface ResolvedUse {
|
|
23
24
|
id: string;
|
|
@@ -115,6 +116,14 @@ export interface StageExecutorDeps {
|
|
|
115
116
|
* from `RunPipelineOptions.eventSink` by `runPipelineV2`.
|
|
116
117
|
*/
|
|
117
118
|
eventSink?: PipelineEventSink;
|
|
119
|
+
/**
|
|
120
|
+
* P4 — shell stage executor. Server passes a `ThrowingShellExecutor`
|
|
121
|
+
* that rejects every call (server cannot spawn processes). CLI passes
|
|
122
|
+
* a `NodeShellExecutor` that uses `child_process.spawn`.
|
|
123
|
+
*
|
|
124
|
+
* When absent, stages declaring `shell:` throw `ShellStageUnsupportedError`.
|
|
125
|
+
*/
|
|
126
|
+
shellExecutor?: ShellExecutor;
|
|
118
127
|
}
|
|
119
128
|
export interface RetryContext {
|
|
120
129
|
stageId: string;
|
package/dist/stages.js
CHANGED
|
@@ -22,6 +22,7 @@ import { isRetryableStatus, computeBackoffMs, RetryExhaustedError, DEFAULT_RETRY
|
|
|
22
22
|
import { NoopEventSink, makeStartedEvent as makePipelineStartedEvent, makeCompletedEvent as makePipelineCompletedEvent, makeFailedEvent as makePipelineFailedEvent, makeStageStartedEvent, makeStageCompletedEvent, makeStageSkippedEvent, makeStageFailedEvent, makeStageRetriedEvent, } from './streaming-events.js';
|
|
23
23
|
import { validateRuntimeConstraints, collectRuntimeWarnings, defaultRuntimeConstraints, } from './runtime-constraints.js';
|
|
24
24
|
import { resolveStageOutputRefs } from './stage-output-refs.js';
|
|
25
|
+
import { normalizeShellStage, ShellStageUnsupportedError, ShellStageExecError, ShellStageTimeoutError, SHELL_DEFAULT_TIMEOUT_MS, } from './shell-stage.js';
|
|
25
26
|
import { expandComposition, } from './composition.js';
|
|
26
27
|
import { groupIndependentStages, } from './parallel.js';
|
|
27
28
|
import { randomUUID } from 'node:crypto';
|
|
@@ -315,8 +316,57 @@ export async function executeStage(stage, pipeline, ctx, deps) {
|
|
|
315
316
|
const hasModel = typeof stage.model === 'string' && stage.model.length > 0;
|
|
316
317
|
const hasPrompt = typeof stage.prompt === 'string' && stage.prompt.length > 0;
|
|
317
318
|
const hasSystem = typeof stage.system === 'string' && stage.system.length > 0;
|
|
318
|
-
|
|
319
|
-
|
|
319
|
+
const hasShell = stage.shell !== undefined && stage.shell !== null;
|
|
320
|
+
if (!hasShell && !hasUses && !hasModel && !hasPrompt) {
|
|
321
|
+
throw new StageBadConfigError(`Stage '${stage.id}' requires one of: 'shell' (shell stage, CLI --local only), 'uses' (skill stage), 'model'+'prompt' (LLM stage), or just 'prompt' with default model`);
|
|
322
|
+
}
|
|
323
|
+
if (hasShell) {
|
|
324
|
+
if (!deps.shellExecutor) {
|
|
325
|
+
throw new ShellStageUnsupportedError(stage.id, 'no shell executor is configured for this run');
|
|
326
|
+
}
|
|
327
|
+
if (!deps.isLocal) {
|
|
328
|
+
throw new ShellStageUnsupportedError(stage.id, 'shell stages require CLI invocation with --local; the server cannot spawn child processes');
|
|
329
|
+
}
|
|
330
|
+
const spec = normalizeShellStage(stage.shell);
|
|
331
|
+
if (!spec) {
|
|
332
|
+
throw new StageBadConfigError(`Stage '${stage.id}' has a malformed 'shell' spec (expected string or { cmd, args?, cwd?, timeout_ms? })`);
|
|
333
|
+
}
|
|
334
|
+
const shellT0 = Date.now();
|
|
335
|
+
try {
|
|
336
|
+
const result = await deps.shellExecutor.exec(spec, {
|
|
337
|
+
cwd: spec.cwd ?? process.cwd(),
|
|
338
|
+
timeoutMs: spec.timeout_ms ?? SHELL_DEFAULT_TIMEOUT_MS,
|
|
339
|
+
});
|
|
340
|
+
const output = JSON.stringify({
|
|
341
|
+
cmd: spec.cmd,
|
|
342
|
+
args: spec.args ?? [],
|
|
343
|
+
cwd: spec.cwd ?? null,
|
|
344
|
+
stdout: result.stdout,
|
|
345
|
+
stderr: result.stderr,
|
|
346
|
+
exit_code: result.exitCode,
|
|
347
|
+
duration_ms: result.durationMs,
|
|
348
|
+
});
|
|
349
|
+
return {
|
|
350
|
+
output,
|
|
351
|
+
costMicroUsdc: 0,
|
|
352
|
+
usage: { inputTokens: 0, outputTokens: 0 },
|
|
353
|
+
nestedCalls: [],
|
|
354
|
+
modelUsed: 'shell',
|
|
355
|
+
...(spec.cwd ? { working_dir: spec.cwd, working_dir_ignored_on_server: false } : {}),
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
catch (e) {
|
|
359
|
+
if (e instanceof ShellStageExecError) {
|
|
360
|
+
throw new StageBadConfigError(`Stage '${stage.id}' ${e.message}`);
|
|
361
|
+
}
|
|
362
|
+
if (e instanceof ShellStageTimeoutError) {
|
|
363
|
+
throw new StageBadConfigError(`Stage '${stage.id}' ${e.message}`);
|
|
364
|
+
}
|
|
365
|
+
throw e;
|
|
366
|
+
}
|
|
367
|
+
finally {
|
|
368
|
+
void shellT0;
|
|
369
|
+
}
|
|
320
370
|
}
|
|
321
371
|
if (hasUses) {
|
|
322
372
|
let chainedInput = stage.input ?? ctx.inputs;
|
package/package.json
CHANGED