@tagma/sdk 0.1.8 → 0.1.9
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/README.md +3 -3
- package/package.json +1 -1
- package/src/adapters/stdin-approval.ts +117 -117
- package/src/adapters/websocket-approval.ts +175 -144
- package/src/approval.ts +4 -1
- package/src/completions/exit-code.ts +19 -19
- package/src/completions/file-exists.ts +39 -39
- package/src/completions/output-check.ts +57 -57
- package/src/config-ops.ts +239 -220
- package/src/dag.ts +222 -222
- package/src/drivers/claude-code.ts +207 -207
- package/src/engine.ts +743 -714
- package/src/hooks.ts +147 -138
- package/src/logger.ts +112 -107
- package/src/middlewares/static-context.ts +29 -29
- package/src/pipeline-runner.ts +126 -125
- package/src/runner.ts +213 -195
- package/src/schema.ts +386 -358
- package/src/triggers/file.ts +105 -94
- package/src/triggers/manual.ts +61 -61
- package/src/utils.ts +154 -147
- package/src/validate-raw.ts +223 -203
package/src/triggers/file.ts
CHANGED
|
@@ -1,94 +1,105 @@
|
|
|
1
|
-
import { watch } from 'chokidar';
|
|
2
|
-
import { resolve, dirname } from 'path';
|
|
3
|
-
import type { TriggerPlugin, TriggerContext } from '../types';
|
|
4
|
-
import { parseDuration, validatePath } from '../utils';
|
|
5
|
-
|
|
6
|
-
const IS_WINDOWS = process.platform === 'win32';
|
|
7
|
-
|
|
8
|
-
function pathsEqual(a: string, b: string): boolean {
|
|
9
|
-
return IS_WINDOWS ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export const FileTrigger: TriggerPlugin = {
|
|
13
|
-
name: 'file',
|
|
14
|
-
|
|
15
|
-
watch(config: Record<string, unknown>, ctx: TriggerContext): Promise<unknown> {
|
|
16
|
-
const filePath = config.path as string;
|
|
17
|
-
if (!filePath) throw new Error(`file trigger: "path" is required`);
|
|
18
|
-
|
|
19
|
-
const safePath = validatePath(filePath, ctx.workDir);
|
|
20
|
-
const timeoutMs = config.timeout != null ? parseDuration(String(config.timeout)) : 0;
|
|
21
|
-
|
|
22
|
-
return new Promise((resolve_p, reject) => {
|
|
23
|
-
if (ctx.signal.aborted) {
|
|
24
|
-
reject(new Error('Pipeline aborted'));
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
let settled = false;
|
|
29
|
-
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
30
|
-
|
|
31
|
-
const dir = dirname(safePath);
|
|
32
|
-
const watcher = watch(dir, {
|
|
33
|
-
ignoreInitial: true,
|
|
34
|
-
depth: 0,
|
|
35
|
-
awaitWriteFinish: { stabilityThreshold: 100, pollInterval: 50 },
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
const cleanup = () => {
|
|
39
|
-
if (settled) return;
|
|
40
|
-
settled = true;
|
|
41
|
-
watcher.close().catch(() => { /* ignore */ });
|
|
42
|
-
if (timer) clearTimeout(timer);
|
|
43
|
-
ctx.signal.removeEventListener('abort', onAbort);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const onAbort = () => {
|
|
47
|
-
cleanup();
|
|
48
|
-
reject(new Error('Pipeline aborted'));
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
watcher.on('add', (addedPath: string) => {
|
|
52
|
-
if (settled) return;
|
|
53
|
-
if (pathsEqual(resolve(addedPath), safePath)) {
|
|
54
|
-
cleanup();
|
|
55
|
-
resolve_p({ path: safePath });
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
1
|
+
import { watch } from 'chokidar';
|
|
2
|
+
import { resolve, dirname } from 'path';
|
|
3
|
+
import type { TriggerPlugin, TriggerContext } from '../types';
|
|
4
|
+
import { parseDuration, validatePath } from '../utils';
|
|
5
|
+
|
|
6
|
+
const IS_WINDOWS = process.platform === 'win32';
|
|
7
|
+
|
|
8
|
+
function pathsEqual(a: string, b: string): boolean {
|
|
9
|
+
return IS_WINDOWS ? a.toLowerCase() === b.toLowerCase() : a === b;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const FileTrigger: TriggerPlugin = {
|
|
13
|
+
name: 'file',
|
|
14
|
+
|
|
15
|
+
watch(config: Record<string, unknown>, ctx: TriggerContext): Promise<unknown> {
|
|
16
|
+
const filePath = config.path as string;
|
|
17
|
+
if (!filePath) throw new Error(`file trigger: "path" is required`);
|
|
18
|
+
|
|
19
|
+
const safePath = validatePath(filePath, ctx.workDir);
|
|
20
|
+
const timeoutMs = config.timeout != null ? parseDuration(String(config.timeout)) : 0;
|
|
21
|
+
|
|
22
|
+
return new Promise((resolve_p, reject) => {
|
|
23
|
+
if (ctx.signal.aborted) {
|
|
24
|
+
reject(new Error('Pipeline aborted'));
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let settled = false;
|
|
29
|
+
let timer: ReturnType<typeof setTimeout> | null = null;
|
|
30
|
+
|
|
31
|
+
const dir = dirname(safePath);
|
|
32
|
+
const watcher = watch(dir, {
|
|
33
|
+
ignoreInitial: true,
|
|
34
|
+
depth: 0,
|
|
35
|
+
awaitWriteFinish: { stabilityThreshold: 100, pollInterval: 50 },
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const cleanup = () => {
|
|
39
|
+
if (settled) return;
|
|
40
|
+
settled = true;
|
|
41
|
+
watcher.close().catch(() => { /* ignore */ });
|
|
42
|
+
if (timer) clearTimeout(timer);
|
|
43
|
+
ctx.signal.removeEventListener('abort', onAbort);
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const onAbort = () => {
|
|
47
|
+
cleanup();
|
|
48
|
+
reject(new Error('Pipeline aborted'));
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
watcher.on('add', (addedPath: string) => {
|
|
52
|
+
if (settled) return;
|
|
53
|
+
if (pathsEqual(resolve(addedPath), safePath)) {
|
|
54
|
+
cleanup();
|
|
55
|
+
resolve_p({ path: safePath });
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Also fire on 'change' so that overwriting an existing file is detected.
|
|
60
|
+
// Without this, upstream tasks that truncate-and-rewrite a file emit only
|
|
61
|
+
// a 'change' event and the downstream trigger would never resolve.
|
|
62
|
+
watcher.on('change', (changedPath: string) => {
|
|
63
|
+
if (settled) return;
|
|
64
|
+
if (pathsEqual(resolve(changedPath), safePath)) {
|
|
65
|
+
cleanup();
|
|
66
|
+
resolve_p({ path: safePath });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
watcher.on('error', (err: unknown) => {
|
|
71
|
+
if (settled) return;
|
|
72
|
+
cleanup();
|
|
73
|
+
reject(new Error(`file trigger watch error: ${err instanceof Error ? err.message : String(err)}`));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// After the watcher finishes its initial scan, check if the file already exists.
|
|
77
|
+
// Doing this inside 'ready' eliminates the race window between existence check
|
|
78
|
+
// and watcher startup, so we neither miss events nor double-resolve.
|
|
79
|
+
watcher.on('ready', () => {
|
|
80
|
+
if (settled) return;
|
|
81
|
+
Bun.file(safePath).exists().then((exists) => {
|
|
82
|
+
if (settled) return;
|
|
83
|
+
if (exists) {
|
|
84
|
+
cleanup();
|
|
85
|
+
resolve_p({ path: safePath });
|
|
86
|
+
}
|
|
87
|
+
}).catch((err: unknown) => {
|
|
88
|
+
if (settled) return;
|
|
89
|
+
cleanup();
|
|
90
|
+
reject(new Error(`file trigger existence check failed: ${err instanceof Error ? err.message : String(err)}`));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
if (timeoutMs > 0) {
|
|
95
|
+
timer = setTimeout(() => {
|
|
96
|
+
if (settled) return;
|
|
97
|
+
cleanup();
|
|
98
|
+
reject(new Error(`file trigger timeout: ${filePath} did not appear within ${config.timeout}`));
|
|
99
|
+
}, timeoutMs);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
ctx.signal.addEventListener('abort', onAbort);
|
|
103
|
+
});
|
|
104
|
+
},
|
|
105
|
+
};
|
package/src/triggers/manual.ts
CHANGED
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
import type { TriggerPlugin, TriggerContext } from '../types';
|
|
2
|
-
import { parseDuration } from '../utils';
|
|
3
|
-
|
|
4
|
-
export const ManualTrigger: TriggerPlugin = {
|
|
5
|
-
name: 'manual',
|
|
6
|
-
|
|
7
|
-
async watch(config: Record<string, unknown>, ctx: TriggerContext): Promise<unknown> {
|
|
8
|
-
const message =
|
|
9
|
-
(config.message as string | undefined) ?? `Manual confirmation required for task "${ctx.taskId}"`;
|
|
10
|
-
const timeoutMs = config.timeout ? parseDuration(config.timeout as string) : 0;
|
|
11
|
-
const options = Array.isArray(config.options)
|
|
12
|
-
? (config.options as unknown[]).map(String)
|
|
13
|
-
: undefined;
|
|
14
|
-
const metadata =
|
|
15
|
-
config.metadata && typeof config.metadata === 'object'
|
|
16
|
-
? (config.metadata as Record<string, unknown>)
|
|
17
|
-
: undefined;
|
|
18
|
-
|
|
19
|
-
const decisionPromise = ctx.approvalGateway.request({
|
|
20
|
-
taskId: ctx.taskId,
|
|
21
|
-
trackId: ctx.trackId,
|
|
22
|
-
message,
|
|
23
|
-
options,
|
|
24
|
-
timeoutMs,
|
|
25
|
-
metadata,
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
// Wire AbortSignal → try to resolve this specific request as aborted.
|
|
29
|
-
// We can't directly cancel via the gateway (no id yet at .request() call site),
|
|
30
|
-
// so instead we race against an abort promise and let engine status logic
|
|
31
|
-
// fall back to pipelineAborted → skipped. abortAll() on gateway still runs
|
|
32
|
-
// from engine shutdown path to clean up any truly-pending entries.
|
|
33
|
-
const abortPromise = new Promise<never>((_, reject) => {
|
|
34
|
-
if (ctx.signal.aborted) {
|
|
35
|
-
reject(new Error('Pipeline aborted'));
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
ctx.signal.addEventListener(
|
|
39
|
-
'abort',
|
|
40
|
-
() => reject(new Error('Pipeline aborted')),
|
|
41
|
-
{ once: true },
|
|
42
|
-
);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const decision = await Promise.race([decisionPromise, abortPromise]);
|
|
46
|
-
|
|
47
|
-
switch (decision.outcome) {
|
|
48
|
-
case 'approved':
|
|
49
|
-
return { confirmed: true, approvalId: decision.approvalId, choice: decision.choice, actor: decision.actor };
|
|
50
|
-
case 'rejected':
|
|
51
|
-
throw new Error(
|
|
52
|
-
`Manual trigger rejected by ${decision.actor ?? 'user'}` +
|
|
53
|
-
(decision.reason ? `: ${decision.reason}` : ''),
|
|
54
|
-
);
|
|
55
|
-
case 'timeout':
|
|
56
|
-
throw new Error(`Manual trigger timeout: ${decision.reason ?? 'no decision made'}`);
|
|
57
|
-
case 'aborted':
|
|
58
|
-
throw new Error(`Manual trigger aborted: ${decision.reason ?? 'pipeline aborted'}`);
|
|
59
|
-
}
|
|
60
|
-
},
|
|
61
|
-
};
|
|
1
|
+
import type { TriggerPlugin, TriggerContext } from '../types';
|
|
2
|
+
import { parseDuration } from '../utils';
|
|
3
|
+
|
|
4
|
+
export const ManualTrigger: TriggerPlugin = {
|
|
5
|
+
name: 'manual',
|
|
6
|
+
|
|
7
|
+
async watch(config: Record<string, unknown>, ctx: TriggerContext): Promise<unknown> {
|
|
8
|
+
const message =
|
|
9
|
+
(config.message as string | undefined) ?? `Manual confirmation required for task "${ctx.taskId}"`;
|
|
10
|
+
const timeoutMs = config.timeout ? parseDuration(config.timeout as string) : 0;
|
|
11
|
+
const options = Array.isArray(config.options)
|
|
12
|
+
? (config.options as unknown[]).map(String)
|
|
13
|
+
: undefined;
|
|
14
|
+
const metadata =
|
|
15
|
+
config.metadata && typeof config.metadata === 'object'
|
|
16
|
+
? (config.metadata as Record<string, unknown>)
|
|
17
|
+
: undefined;
|
|
18
|
+
|
|
19
|
+
const decisionPromise = ctx.approvalGateway.request({
|
|
20
|
+
taskId: ctx.taskId,
|
|
21
|
+
trackId: ctx.trackId,
|
|
22
|
+
message,
|
|
23
|
+
options,
|
|
24
|
+
timeoutMs,
|
|
25
|
+
metadata,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Wire AbortSignal → try to resolve this specific request as aborted.
|
|
29
|
+
// We can't directly cancel via the gateway (no id yet at .request() call site),
|
|
30
|
+
// so instead we race against an abort promise and let engine status logic
|
|
31
|
+
// fall back to pipelineAborted → skipped. abortAll() on gateway still runs
|
|
32
|
+
// from engine shutdown path to clean up any truly-pending entries.
|
|
33
|
+
const abortPromise = new Promise<never>((_, reject) => {
|
|
34
|
+
if (ctx.signal.aborted) {
|
|
35
|
+
reject(new Error('Pipeline aborted'));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
ctx.signal.addEventListener(
|
|
39
|
+
'abort',
|
|
40
|
+
() => reject(new Error('Pipeline aborted')),
|
|
41
|
+
{ once: true },
|
|
42
|
+
);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const decision = await Promise.race([decisionPromise, abortPromise]);
|
|
46
|
+
|
|
47
|
+
switch (decision.outcome) {
|
|
48
|
+
case 'approved':
|
|
49
|
+
return { confirmed: true, approvalId: decision.approvalId, choice: decision.choice, actor: decision.actor };
|
|
50
|
+
case 'rejected':
|
|
51
|
+
throw new Error(
|
|
52
|
+
`Manual trigger rejected by ${decision.actor ?? 'user'}` +
|
|
53
|
+
(decision.reason ? `: ${decision.reason}` : ''),
|
|
54
|
+
);
|
|
55
|
+
case 'timeout':
|
|
56
|
+
throw new Error(`Manual trigger timeout: ${decision.reason ?? 'no decision made'}`);
|
|
57
|
+
case 'aborted':
|
|
58
|
+
throw new Error(`Manual trigger aborted: ${decision.reason ?? 'pipeline aborted'}`);
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
package/src/utils.ts
CHANGED
|
@@ -1,147 +1,154 @@
|
|
|
1
|
-
import { resolve, relative } from 'path';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
case '
|
|
15
|
-
case '
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
`
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
//
|
|
133
|
-
return
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
1
|
+
import { resolve, relative } from 'path';
|
|
2
|
+
import { randomBytes } from 'crypto';
|
|
3
|
+
|
|
4
|
+
const DURATION_RE = /^(\d+(?:\.\d+)?)\s*(s|m|h)$/;
|
|
5
|
+
|
|
6
|
+
export function parseDuration(input: string): number {
|
|
7
|
+
const match = DURATION_RE.exec(input.trim());
|
|
8
|
+
if (!match) {
|
|
9
|
+
throw new Error(`Invalid duration format: "${input}". Expected format: <number>(s|m|h)`);
|
|
10
|
+
}
|
|
11
|
+
const value = parseFloat(match[1]);
|
|
12
|
+
const unit = match[2];
|
|
13
|
+
switch (unit) {
|
|
14
|
+
case 's': return value * 1000;
|
|
15
|
+
case 'm': return value * 60_000;
|
|
16
|
+
case 'h': return value * 3_600_000;
|
|
17
|
+
default: throw new Error(`Unknown duration unit: "${unit}"`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function validatePath(filePath: string, projectRoot: string): string {
|
|
22
|
+
const resolved = resolve(projectRoot, filePath);
|
|
23
|
+
const rel = relative(projectRoot, resolved);
|
|
24
|
+
|
|
25
|
+
if (rel.startsWith('..') || rel.startsWith('/') || /^[a-zA-Z]:/.test(rel)) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Security: path "${filePath}" escapes project root. ` +
|
|
28
|
+
`All file references must be within "${projectRoot}".`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return resolved;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const SHELL_META_CHARS = /[;&|$`\\!><()\[\]{}*?#~]/;
|
|
36
|
+
|
|
37
|
+
export function validatePathParam(filePath: string): void {
|
|
38
|
+
if (filePath.includes('..')) {
|
|
39
|
+
throw new Error(`Template param type=path: ".." traversal not allowed in "${filePath}"`);
|
|
40
|
+
}
|
|
41
|
+
if (resolve(filePath) === filePath) {
|
|
42
|
+
throw new Error(`Template param type=path: absolute path not allowed: "${filePath}"`);
|
|
43
|
+
}
|
|
44
|
+
if (SHELL_META_CHARS.test(filePath)) {
|
|
45
|
+
throw new Error(`Template param type=path: shell metacharacters not allowed in "${filePath}"`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
let runCounter = 0;
|
|
50
|
+
|
|
51
|
+
export function generateRunId(): string {
|
|
52
|
+
const ts = Date.now().toString(36);
|
|
53
|
+
const seq = (runCounter++).toString(36).padStart(2, '0');
|
|
54
|
+
// Random suffix prevents ID collisions when two pipelines start within
|
|
55
|
+
// the same millisecond or after a process restart resets the counter.
|
|
56
|
+
const rand = randomBytes(3).toString('hex');
|
|
57
|
+
return `run_${ts}_${seq}_${rand}`;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function truncateForName(text: string, maxLen = 40): string {
|
|
61
|
+
const first = text.split('\n')[0]!.trim();
|
|
62
|
+
// Guard: if the first line is empty (e.g. prompt is all whitespace/newlines),
|
|
63
|
+
// fall back to the raw text trimmed rather than silently producing an empty name.
|
|
64
|
+
if (!first) return text.trim().slice(0, maxLen) || '...';
|
|
65
|
+
return first.length > maxLen ? first.slice(0, maxLen) + '...' : first;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function nowISO(): string {
|
|
69
|
+
return new Date().toISOString();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// ═══ Platform-aware shell ═══
|
|
73
|
+
//
|
|
74
|
+
// Resolution order:
|
|
75
|
+
// 1. Env override: PIPELINE_SHELL="bash" or PIPELINE_SHELL="cmd" etc.
|
|
76
|
+
// 2. Windows: prefer sh (Git Bash / MSYS2) if on PATH, fall back to cmd.exe
|
|
77
|
+
// 3. Unix: sh
|
|
78
|
+
//
|
|
79
|
+
// Resolution is cached once on first call to avoid repeated PATH lookups.
|
|
80
|
+
|
|
81
|
+
const IS_WINDOWS = process.platform === 'win32';
|
|
82
|
+
|
|
83
|
+
type ShellKind = 'sh' | 'bash' | 'cmd' | 'powershell';
|
|
84
|
+
let resolvedShell: { kind: ShellKind; path: string } | null = null;
|
|
85
|
+
|
|
86
|
+
function detectShell(): { kind: ShellKind; path: string } {
|
|
87
|
+
// Env override takes precedence
|
|
88
|
+
const override = process.env.PIPELINE_SHELL;
|
|
89
|
+
if (override) {
|
|
90
|
+
const kind = override as ShellKind;
|
|
91
|
+
return { kind, path: override };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!IS_WINDOWS) {
|
|
95
|
+
return { kind: 'sh', path: 'sh' };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Windows: probe PATH for sh (bundled with Git for Windows / MSYS2)
|
|
99
|
+
const pathEnv = process.env.PATH ?? '';
|
|
100
|
+
const pathExt = (process.env.PATHEXT ?? '.EXE;.CMD;.BAT').split(';');
|
|
101
|
+
const dirs = pathEnv.split(';').filter(Boolean);
|
|
102
|
+
|
|
103
|
+
for (const dir of dirs) {
|
|
104
|
+
for (const ext of ['', ...pathExt]) {
|
|
105
|
+
const candidate = `${dir}\\sh${ext}`;
|
|
106
|
+
try {
|
|
107
|
+
if (Bun.file(candidate).size > 0) {
|
|
108
|
+
return { kind: 'sh', path: candidate };
|
|
109
|
+
}
|
|
110
|
+
} catch { /* ignore */ }
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Fallback: cmd.exe (always present on Windows)
|
|
115
|
+
const systemRoot = process.env.SystemRoot ?? 'C:\\Windows';
|
|
116
|
+
return { kind: 'cmd', path: `${systemRoot}\\System32\\cmd.exe` };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function getShell(): { kind: ShellKind; path: string } {
|
|
120
|
+
if (!resolvedShell) resolvedShell = detectShell();
|
|
121
|
+
return resolvedShell;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function shellArgs(command: string): readonly string[] {
|
|
125
|
+
const sh = getShell();
|
|
126
|
+
if (sh.kind === 'cmd') {
|
|
127
|
+
return [sh.path, '/c', command];
|
|
128
|
+
}
|
|
129
|
+
if (sh.kind === 'powershell') {
|
|
130
|
+
return [sh.path, '-Command', command];
|
|
131
|
+
}
|
|
132
|
+
// sh or bash
|
|
133
|
+
return [sh.path, '-c', command];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Quote a single argument for inclusion in a shell command string. */
|
|
137
|
+
function quoteArg(arg: string): string {
|
|
138
|
+
if (!/[\s"'\\<>|&;`$!^%]/.test(arg)) return arg;
|
|
139
|
+
// Double-quote and escape embedded double quotes + backslashes
|
|
140
|
+
return '"' + arg.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Convert an args array to shell-wrapped args suitable for Bun.spawn.
|
|
145
|
+
* Each arg is quoted as needed, then joined and passed through shellArgs.
|
|
146
|
+
*/
|
|
147
|
+
export function shellArgsFromArray(args: readonly string[]): readonly string[] {
|
|
148
|
+
return shellArgs(args.map(quoteArg).join(' '));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// For tests: allow resetting the cached shell detection
|
|
152
|
+
export function _resetShellCache(): void {
|
|
153
|
+
resolvedShell = null;
|
|
154
|
+
}
|