-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell-result.js
More file actions
232 lines (213 loc) · 7.98 KB
/
shell-result.js
File metadata and controls
232 lines (213 loc) · 7.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { writeFileSync } from 'fs';
import { join } from 'path';
import { getResolvedDbPath } from './db.js';
import { ensureArtifactsDir, resolveArtifactsDir } from './paths.js';
export const DEFAULT_STORE_LIMIT = 64 * 1024;
export const DEFAULT_EXCERPT_LIMIT = 64 * 1024;
export const DEFAULT_SUMMARY_LIMIT = 64 * 1024;
export const DEFAULT_OFFLOAD_THRESHOLD = 64 * 1024;
function toText(value) {
if (value == null) return '';
return typeof value === 'string' ? value : String(value);
}
function textBytes(value) {
return Buffer.byteLength(toText(value), 'utf8');
}
// Truncate to a byte limit (UTF-8). Slices at a character boundary that
// does not exceed the byte budget, so multi-byte characters are never split.
function truncateText(value, limitBytes) {
const text = toText(value).trim();
if (!text) return { text: '', truncated: false, bytes: 0 };
const bytes = textBytes(text);
if (bytes <= limitBytes) return { text, truncated: false, bytes };
// Walk characters until we exceed the byte budget minus suffix room
const suffix = '\n...[truncated]';
const suffixBytes = Buffer.byteLength(suffix, 'utf8');
const target = Math.max(0, limitBytes - suffixBytes);
let usedBytes = 0;
let cutIndex = 0;
for (const char of text) {
const charBytes = Buffer.byteLength(char, 'utf8');
if (usedBytes + charBytes > target) break;
usedBytes += charBytes;
cutIndex += char.length;
}
return {
text: text.slice(0, cutIndex) + suffix,
truncated: true,
bytes,
};
}
function deriveErrorMessage(result, timeoutMs) {
if (result.status === 'ok') return null;
if (result.timedOut) return `Shell command timed out after ${timeoutMs}ms`;
if (typeof result.exitCode === 'number') return `Shell exited with code ${result.exitCode}`;
if (result.signal) return `Shell terminated by signal ${result.signal}`;
return result.rawError?.message || 'Shell command failed';
}
function writeOutputArtifact(kind, runId, text, artifactsDir) {
if (!artifactsDir || !runId || !text.trim()) return null;
try {
const baseDir = ensureArtifactsDir(join(artifactsDir, 'runs', runId));
const filePath = join(baseDir, `${kind}.txt`);
writeFileSync(filePath, text, 'utf8');
return filePath;
} catch (err) {
process.stderr.write(`[shell-result] writeOutputArtifact failed for ${kind} (run ${runId}): ${err.message}\n`);
return null;
}
}
function formatOutputBlock(label, excerpt, artifactPath, bytes) {
const parts = [];
if (excerpt.text) {
parts.push(`${label}:`);
parts.push(excerpt.text);
}
if (artifactPath) {
parts.push(`[${label} offloaded: ${artifactPath} (${bytes} bytes)]`);
}
return parts.join('\n');
}
export function normalizeShellResult(
{
stdout = '',
stderr = '',
error = null,
},
{
runId = null,
timeoutMs = 300000,
storeLimit = DEFAULT_STORE_LIMIT,
excerptLimit = DEFAULT_EXCERPT_LIMIT,
summaryLimit = DEFAULT_SUMMARY_LIMIT,
offloadThreshold = DEFAULT_OFFLOAD_THRESHOLD,
artifactsDir = resolveArtifactsDir({ dbPath: getResolvedDbPath() }),
} = {}
) {
const rawStdout = toText(stdout);
// Extract [IMAGE:path] markers from stdout before processing.
// Scripts can signal image attachments by writing lines like:
// [IMAGE:/tmp/chart.png]
// [IMAGE:/tmp/report.pdf]
// Extracted paths are passed through to the delivery layer.
const IMAGE_MARKER_RE = /^\[IMAGE:(\/[^\]\n]+)\]$/gm;
const imageAttachments = [];
let match;
while ((match = IMAGE_MARKER_RE.exec(rawStdout)) !== null) {
imageAttachments.push(match[1]);
}
// Strip markers from stdout so they don't appear in delivery text
const stdoutText = imageAttachments.length > 0
? rawStdout.replace(IMAGE_MARKER_RE, '').replace(/\n{3,}/g, '\n\n').trim()
: rawStdout;
const stderrText = toText(stderr);
const stdoutBytes = textBytes(stdoutText);
const stderrBytes = textBytes(stderrText);
const stdoutOffloaded = stdoutBytes > offloadThreshold
? writeOutputArtifact('stdout', runId, stdoutText, artifactsDir)
: null;
const stderrOffloaded = stderrBytes > offloadThreshold
? writeOutputArtifact('stderr', runId, stderrText, artifactsDir)
: null;
const stdoutStored = truncateText(stdoutText, Math.min(storeLimit, stdoutOffloaded ? excerptLimit : storeLimit));
const stderrStored = truncateText(stderrText, Math.min(storeLimit, stderrOffloaded ? excerptLimit : storeLimit));
const stdoutExcerpt = truncateText(stdoutText, excerptLimit);
const stderrExcerpt = truncateText(stderrText, excerptLimit);
const exitCode = Number.isInteger(error?.code) ? error.code : null;
const signal = error?.signal || null;
const timedOut = Boolean(
error && (
error.code === 'ETIMEDOUT'
|| error.killed === true
|| /timed out/i.test(error?.message || '')
|| /exceeded absolute timeout/i.test(error?.message || '')
|| /idle.*timeout/i.test(error?.message || '')
)
);
const status = timedOut ? 'timeout' : error ? 'error' : 'ok';
const errorMessage = deriveErrorMessage({ status, timedOut, exitCode, signal, rawError: error }, timeoutMs);
const blocks = [
formatOutputBlock('stdout', stdoutExcerpt, stdoutOffloaded, stdoutBytes),
formatOutputBlock('stderr', stderrExcerpt, stderrOffloaded, stderrBytes),
].filter(Boolean);
if (blocks.length === 0 && errorMessage) blocks.push(errorMessage);
const previewText = blocks.join('\n\n').trim() || '(no output)';
return {
status,
exitCode,
signal,
timedOut,
stdout: stdoutStored.text,
stderr: stderrStored.text,
stdoutPath: stdoutOffloaded,
stderrPath: stderrOffloaded,
stdoutBytes,
stderrBytes,
stdoutTruncated: stdoutStored.truncated,
stderrTruncated: stderrStored.truncated,
summary: truncateText(previewText, summaryLimit).text,
deliveryText: previewText,
imageAttachments,
errorMessage,
contextSummary: {
shell_result: {
exit_code: exitCode,
signal,
timed_out: timedOut,
error_message: errorMessage,
stdout_excerpt: stdoutExcerpt.text,
stderr_excerpt: stderrExcerpt.text,
stdout_truncated: stdoutStored.truncated || stdoutExcerpt.truncated,
stderr_truncated: stderrStored.truncated || stderrExcerpt.truncated,
stdout_path: stdoutOffloaded,
stderr_path: stderrOffloaded,
stdout_bytes: stdoutBytes,
stderr_bytes: stderrBytes,
}
}
};
}
export function extractShellResultFromRun(run) {
if (!run) return null;
const hasDirectFields = run.shell_exit_code != null
|| run.shell_signal != null
|| run.shell_timed_out != null
|| (typeof run.shell_stdout === 'string' && run.shell_stdout.length > 0)
|| (typeof run.shell_stderr === 'string' && run.shell_stderr.length > 0)
|| typeof run.shell_stdout_path === 'string'
|| typeof run.shell_stderr_path === 'string';
if (hasDirectFields) {
return {
exitCode: run.shell_exit_code ?? null,
signal: run.shell_signal ?? null,
timedOut: Boolean(run.shell_timed_out),
stdout: run.shell_stdout || '',
stderr: run.shell_stderr || '',
stdoutPath: run.shell_stdout_path || null,
stderrPath: run.shell_stderr_path || null,
stdoutBytes: run.shell_stdout_bytes ?? textBytes(run.shell_stdout || ''),
stderrBytes: run.shell_stderr_bytes ?? textBytes(run.shell_stderr || ''),
errorMessage: run.error_message || null,
};
}
if (!run.context_summary) return null;
try {
const parsed = JSON.parse(run.context_summary);
const shell = parsed?.shell_result;
if (!shell) return null;
return {
exitCode: shell.exit_code ?? null,
signal: shell.signal ?? null,
timedOut: Boolean(shell.timed_out),
stdout: shell.stdout_excerpt || '',
stderr: shell.stderr_excerpt || '',
stdoutPath: shell.stdout_path || null,
stderrPath: shell.stderr_path || null,
stdoutBytes: shell.stdout_bytes ?? 0,
stderrBytes: shell.stderr_bytes ?? 0,
errorMessage: shell.error_message || null,
};
} catch {
return null;
}
}