@molecule/api-ai-tools 1.6.5 → 1.7.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.
@@ -37,7 +37,19 @@ export interface CommandSegment {
37
37
  args: readonly string[];
38
38
  /** The program and its arguments as one line — what a runner pattern is matched against. */
39
39
  text: string;
40
+ /**
41
+ * The seconds the segment's own `timeout` wrapper allows the command it runs
42
+ * (`timeout 280 hurl --test …` → 280), or null when it carries none.
43
+ */
44
+ timeoutSeconds: number | null;
40
45
  }
46
+ /**
47
+ * The seconds a duration word stands for.
48
+ *
49
+ * @param word - A duration (`280`, `20s`, `1m`).
50
+ * @returns The seconds, or null when the word is not a duration.
51
+ */
52
+ export declare function durationSeconds(word: string): number | null;
41
53
  /**
42
54
  * The segments of a command, each with its program and arguments — the answer every classifier
43
55
  * that reads a command as "this runs the suite" is asking for.
@@ -46,6 +58,20 @@ export interface CommandSegment {
46
58
  * @returns One entry per statement or pipeline stage that runs something, in order.
47
59
  */
48
60
  export declare function commandSegments(command: string): CommandSegment[];
61
+ /**
62
+ * The bound a command puts on its own run: the largest `timeout <N>` any of its segments runs
63
+ * under (`timeout 280 hurl --test …` → 280), or null when it carries none.
64
+ *
65
+ * This is the bound `man timeout` enforces by KILLING the command — so a run that used it did not
66
+ * finish, whatever the pipeline's own exit status says. It is read here, from the same parse every
67
+ * other command reader uses, rather than matched as text: `timeout` in an argument
68
+ * (`grep -n 'timeout 280' notes.md`) bounds nothing, and `-k 5` is the grace before the kill, not
69
+ * the bound ({@link WRAPPER_VALUE_FLAGS}).
70
+ *
71
+ * @param command - The shell command.
72
+ * @returns The seconds the command allows itself, or null.
73
+ */
74
+ export declare function commandTimeoutSeconds(command: string): number | null;
49
75
  /**
50
76
  * Whether any segment of the command runs a program whose command-position text matches
51
77
  * `pattern` — the one question a runner-kind table asks.
@@ -1 +1 @@
1
- {"version":3,"file":"command-position.d.ts","sourceRoot":"","sources":["../src/command-position.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,uFAAuF;AACvF,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAA;IACf,qFAAqF;IACrF,IAAI,EAAE,SAAS,MAAM,EAAE,CAAA;IACvB,4FAA4F;IAC5F,IAAI,EAAE,MAAM,CAAA;CACb;AA8TD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc,EAAE,CAEjE;AA+BD;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAK/E"}
1
+ {"version":3,"file":"command-position.d.ts","sourceRoot":"","sources":["../src/command-position.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAEH,uFAAuF;AACvF,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAA;IACf,qFAAqF;IACrF,IAAI,EAAE,SAAS,MAAM,EAAE,CAAA;IACvB,4FAA4F;IAC5F,IAAI,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,cAAc,EAAE,MAAM,GAAG,IAAI,CAAA;CAC9B;AAmFD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAM3D;AAyPD;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,GAAG,cAAc,EAAE,CAEjE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAOpE;AAoCD;;;;;;;;;;;GAWG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAK/E"}
@@ -92,6 +92,22 @@ const WRAPPER_VALUE_FLAGS = {
92
92
  const ENV_ASSIGNMENT = /^[A-Za-z_][A-Za-z0-9_]*=/;
93
93
  /** A wrapper's own duration — `timeout 280 …`, `timeout 1m …` — which is not a flag. */
94
94
  const DURATION = /^\d+(?:\.\d+)?[smhd]?$/;
95
+ /** The seconds a `timeout` duration word names (`280`, `1m`, `1.5h`), or null when it is none. */
96
+ const DURATION_UNITS = { s: 1, m: 60, h: 3600, d: 86_400 };
97
+ /**
98
+ * The seconds a duration word stands for.
99
+ *
100
+ * @param word - A duration (`280`, `20s`, `1m`).
101
+ * @returns The seconds, or null when the word is not a duration.
102
+ */
103
+ export function durationSeconds(word) {
104
+ if (!DURATION.test(word))
105
+ return null;
106
+ const [, number = '', unit = 's'] = /^(\d+(?:\.\d+)?)([smhd]?)$/.exec(word) ?? [];
107
+ const scale = DURATION_UNITS[unit] ?? 1;
108
+ const seconds = Number(number) * scale;
109
+ return Number.isFinite(seconds) ? seconds : null;
110
+ }
95
111
  /**
96
112
  * A word without its shell quotes.
97
113
  *
@@ -155,14 +171,16 @@ function nestedCommand(words, from) {
155
171
  return null;
156
172
  }
157
173
  /**
158
- * Where the program starts in one segment's words.
174
+ * Where the program starts in one segment's words, and the bound the segment puts on itself.
159
175
  *
160
176
  * @param words - The segment's words, quotes kept.
161
177
  * @returns The index of the word in command position (one past the end when the segment runs
162
- * nothing), and the command string of a `sh -c '<command>'` this segment turns out to be.
178
+ * nothing), the command string of a `sh -c '<command>'` this segment turns out to be, and the
179
+ * seconds its own `timeout` wrapper allows that command (`timeout 280 hurl …` → 280) or null.
163
180
  */
164
181
  function commandStart(words) {
165
182
  let at = 0;
183
+ let timeoutSeconds = null;
166
184
  for (let guard = 0; guard < MAX_WRAPPERS && at < words.length; guard++) {
167
185
  const word = words[at] ?? '';
168
186
  if (ENV_ASSIGNMENT.test(word)) {
@@ -178,7 +196,7 @@ function commandStart(words) {
178
196
  if (SHELLS.has(name)) {
179
197
  const nested = nestedCommand(words, at + 1);
180
198
  if (nested !== null)
181
- return { at, nested };
199
+ return { at, nested, timeoutSeconds };
182
200
  at += 1 + flagWords(words, at + 1, name);
183
201
  continue;
184
202
  }
@@ -192,12 +210,14 @@ function commandStart(words) {
192
210
  continue;
193
211
  }
194
212
  if (!LAUNCHERS.has(name))
195
- return { at, nested: null };
213
+ return { at, nested: null, timeoutSeconds };
196
214
  at += 1 + flagWords(words, at + 1, name);
197
- if (name === 'timeout' && DURATION.test(words[at] ?? ''))
215
+ if (name === 'timeout' && DURATION.test(words[at] ?? '')) {
216
+ timeoutSeconds = durationSeconds(words[at] ?? '');
198
217
  at += 1;
218
+ }
199
219
  }
200
- return { at, nested: null };
220
+ return { at, nested: null, timeoutSeconds };
201
221
  }
202
222
  /**
203
223
  * A command's statements and pipeline stages, split where the shell splits them — inside quotes
@@ -335,6 +355,28 @@ function segmentWords(text) {
335
355
  export function commandSegments(command) {
336
356
  return segmentsOf(command, 0);
337
357
  }
358
+ /**
359
+ * The bound a command puts on its own run: the largest `timeout <N>` any of its segments runs
360
+ * under (`timeout 280 hurl --test …` → 280), or null when it carries none.
361
+ *
362
+ * This is the bound `man timeout` enforces by KILLING the command — so a run that used it did not
363
+ * finish, whatever the pipeline's own exit status says. It is read here, from the same parse every
364
+ * other command reader uses, rather than matched as text: `timeout` in an argument
365
+ * (`grep -n 'timeout 280' notes.md`) bounds nothing, and `-k 5` is the grace before the kill, not
366
+ * the bound ({@link WRAPPER_VALUE_FLAGS}).
367
+ *
368
+ * @param command - The shell command.
369
+ * @returns The seconds the command allows itself, or null.
370
+ */
371
+ export function commandTimeoutSeconds(command) {
372
+ let bound = null;
373
+ for (const segment of segmentsOf(command, 0)) {
374
+ if (segment.timeoutSeconds === null)
375
+ continue;
376
+ bound = bound === null ? segment.timeoutSeconds : Math.max(bound, segment.timeoutSeconds);
377
+ }
378
+ return bound;
379
+ }
338
380
  /**
339
381
  * {@link commandSegments}'s recursion, so a `sh -c '<command>'` is opened as far as
340
382
  * {@link MAX_NESTING} allows.
@@ -362,7 +404,12 @@ function segmentsOf(command, depth) {
362
404
  continue;
363
405
  const program = programName(word);
364
406
  const args = words.slice(start.at + 1);
365
- out.push({ program, args, text: [program, ...args].join(' ') });
407
+ out.push({
408
+ program,
409
+ args,
410
+ text: [program, ...args].join(' '),
411
+ timeoutSeconds: start.timeoutSeconds,
412
+ });
366
413
  }
367
414
  return out;
368
415
  }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * A command that a budget stopped before it finished, and the state its result carries so no
3
+ * reader can mistake it for a run that ended.
4
+ *
5
+ * X1 xr-molecule-flash-m93 (2026-10-16, +12:37) sent the supplied RealWorld suite as
6
+ * `timeout 280 hurl --test --continue-on-error --jobs 1 … hurl/*.hurl 2>&1 | tail -60` and got
7
+ * back `exitCode: 0`, four finished files' lines and nothing else. Two lies in one result: the
8
+ * `timeout 280` killed the run at 280 s, and the `| tail` made the pipeline's status the status —
9
+ * so the executor read a suite that stopped at its fifth file as a suite that ran and nearly
10
+ * passed, fixed one message, and immediately sent the same 280-second command again (the run's
11
+ * third, ~14 minutes of a 60-minute cap across three of them).
12
+ *
13
+ * So the tool never reports a stopped run as `exitCode: 0`. Such a result carries
14
+ * `status: 'cut_at_budget'` and an exit code that is `null` whenever the number the pipeline
15
+ * reported belongs to the filter rather than to the run, and every reader of a run's verdict reads
16
+ * the state through {@link commandRunState} — a cut run is NOT RUN, never a pass.
17
+ *
18
+ * Two things stop a run, and both are read from the command and the result, never inferred:
19
+ *
20
+ * 1. **Its own bound** — a `timeout <N>` the command carries ({@link commandTimeoutSeconds}). The
21
+ * run used it, so the run was killed at it; with no closing tally in the output, the run did
22
+ * not finish. This is m93's shape.
23
+ * 2. **This tool's budget** — the backend runs the command under `timeout` (see
24
+ * `backends/sandbox.ts`), which reports 124 whatever the pipeline did, and returns everything
25
+ * printed until then. The budget is the tool's own, so its number belongs to the tool.
26
+ *
27
+ * @module
28
+ */
29
+ /**
30
+ * The state a result carries when a budget stopped its command: a run that did not finish is not a
31
+ * run that passed, and no reader may treat it as one.
32
+ */
33
+ export declare const CUT_AT_BUDGET = "cut_at_budget";
34
+ /** What a command's result records: a run that ended, or one a budget stopped. */
35
+ export type CommandRunState = 'ran' | typeof CUT_AT_BUDGET;
36
+ /**
37
+ * The state a command's result records — THE reader every verdict about a run goes through.
38
+ *
39
+ * A result that carries no state is a run that ended: the state is written where the run is
40
+ * stopped ({@link commandCutRun}), so a result without it was never stopped by a budget this
41
+ * platform set or can see. Results produced before this state existed read the same way, which is
42
+ * what keeps a past turn's tally readable.
43
+ *
44
+ * @param result - The exec_command / wait_for_task result.
45
+ * @returns The state the result records.
46
+ */
47
+ export declare function commandRunState(result: unknown): CommandRunState;
48
+ /**
49
+ * Whether text carries a runner's closing summary ({@link SUITE_TALLY}).
50
+ *
51
+ * @param text - The run's stdout and stderr.
52
+ * @returns True when the runner's own end-of-run summary is there.
53
+ */
54
+ export declare function suiteTally(text: string): boolean;
55
+ /** What stopped a run. */
56
+ export type CutBy = 'tool-budget' | 'command-timeout';
57
+ /** A run a budget stopped, as its result reports it. */
58
+ export interface CutRun {
59
+ /** The bound the run was allowed, in seconds. */
60
+ seconds: number;
61
+ /** Whether this tool's budget stopped it or the `timeout` the command carried. */
62
+ stoppedBy: CutBy;
63
+ /**
64
+ * The exit code the run's own process reported, or null when the code in the result belongs to
65
+ * the pipeline's filter (`… | tail`) rather than to the run — the case that read as success.
66
+ */
67
+ exitCode: number | null;
68
+ /** How many files or tests the runner's own output shows finishing, or null when it shows none. */
69
+ ran: number | null;
70
+ /** How many the run was walking, when the runner printed it (Playwright's `[n/K]`), or null. */
71
+ total: number | null;
72
+ /** The noun the runner counts in. */
73
+ noun: 'files' | 'tests';
74
+ /** The last thing the run printed — the line a reader looks at first. */
75
+ doing: string | null;
76
+ }
77
+ /**
78
+ * The run a command's result records as stopped by a budget, or null when the result is a run that
79
+ * ended (or says nothing about a run).
80
+ *
81
+ * Evidence, in order — never a guess:
82
+ *
83
+ * - `timeout`'s own 124 (the code the backend's wrapper returns whenever the TOOL's budget fired,
84
+ * whatever the pipeline ended with) — and, when the clock says the command's own smaller bound
85
+ * ran out first and less than the budget, that bound instead.
86
+ * - the command carried its own `timeout <N>`, N is inside this tool's budget (a larger one never
87
+ * fires — the tool's does), the run used N, and no runner printed a closing summary: the run was
88
+ * killed at its own bound. Its exit code, when the pipeline handed one back, is the filter's.
89
+ * - the run used this tool's whole budget behind a filter that withheld its output, and printed no
90
+ * closing summary: the budget stopped it, and no code in the result is the run's.
91
+ *
92
+ * @param command - The command the executor sent.
93
+ * @param result - Its result.
94
+ * @param elapsedMs - How long the command ran, in ms.
95
+ * @param budgetSeconds - This tool's foreground budget, in seconds (0 when it has none).
96
+ * @returns The stopped run, or null.
97
+ */
98
+ export declare function commandCutRun(command: string, result: unknown, elapsedMs: number, budgetSeconds?: number): CutRun | null;
99
+ /**
100
+ * The facts a result records about the run a budget stopped it in — what a reader needs to say
101
+ * how far the run got WITHOUT re-reading its log or re-deriving the counts.
102
+ *
103
+ * The numbers are read from the runner's own output where it printed them (Hurl's per-file lines,
104
+ * Playwright's `[n/K]`), so a caller that can also see the suite itself — the file count of the
105
+ * `*.hurl` directory the run walked — fills in the `total` this could not know, and both then say
106
+ * the same sentence ({@link cutRunSentence}).
107
+ *
108
+ * @param result - The exec_command / wait_for_task result.
109
+ * @returns The facts the result carries, or null when it records no stopped run.
110
+ */
111
+ export declare function cutRunFacts(result: unknown): CutRun | null;
112
+ /**
113
+ * The first line of a stopped run's result: how long it was allowed, what stopped it, how far it
114
+ * got, and the last thing it printed.
115
+ *
116
+ * Written to be quotable by the model in one sentence and to leave no room for "nearly passing":
117
+ * the counts come from the runner's own output ({@link runProgress}), and a `total` the caller
118
+ * read elsewhere (the suite's own file count) fills in the "M of K" it can then state.
119
+ *
120
+ * @param run - The stopped run.
121
+ * @returns The sentence.
122
+ */
123
+ export declare function cutRunSentence(run: CutRun): string;
124
+ //# sourceMappingURL=cut-run.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":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M,CAkBlD"}
@@ -0,0 +1,267 @@
1
+ /**
2
+ * A command that a budget stopped before it finished, and the state its result carries so no
3
+ * reader can mistake it for a run that ended.
4
+ *
5
+ * X1 xr-molecule-flash-m93 (2026-10-16, +12:37) sent the supplied RealWorld suite as
6
+ * `timeout 280 hurl --test --continue-on-error --jobs 1 … hurl/*.hurl 2>&1 | tail -60` and got
7
+ * back `exitCode: 0`, four finished files' lines and nothing else. Two lies in one result: the
8
+ * `timeout 280` killed the run at 280 s, and the `| tail` made the pipeline's status the status —
9
+ * so the executor read a suite that stopped at its fifth file as a suite that ran and nearly
10
+ * passed, fixed one message, and immediately sent the same 280-second command again (the run's
11
+ * third, ~14 minutes of a 60-minute cap across three of them).
12
+ *
13
+ * So the tool never reports a stopped run as `exitCode: 0`. Such a result carries
14
+ * `status: 'cut_at_budget'` and an exit code that is `null` whenever the number the pipeline
15
+ * reported belongs to the filter rather than to the run, and every reader of a run's verdict reads
16
+ * the state through {@link commandRunState} — a cut run is NOT RUN, never a pass.
17
+ *
18
+ * Two things stop a run, and both are read from the command and the result, never inferred:
19
+ *
20
+ * 1. **Its own bound** — a `timeout <N>` the command carries ({@link commandTimeoutSeconds}). The
21
+ * run used it, so the run was killed at it; with no closing tally in the output, the run did
22
+ * not finish. This is m93's shape.
23
+ * 2. **This tool's budget** — the backend runs the command under `timeout` (see
24
+ * `backends/sandbox.ts`), which reports 124 whatever the pipeline did, and returns everything
25
+ * printed until then. The budget is the tool's own, so its number belongs to the tool.
26
+ *
27
+ * @module
28
+ */
29
+ import { commandTimeoutSeconds } from './command-position.js';
30
+ import { isSuiteRunCommand } from './utilities.js';
31
+ /**
32
+ * The state a result carries when a budget stopped its command: a run that did not finish is not a
33
+ * run that passed, and no reader may treat it as one.
34
+ */
35
+ export const CUT_AT_BUDGET = 'cut_at_budget';
36
+ /**
37
+ * The state a command's result records — THE reader every verdict about a run goes through.
38
+ *
39
+ * A result that carries no state is a run that ended: the state is written where the run is
40
+ * stopped ({@link commandCutRun}), so a result without it was never stopped by a budget this
41
+ * platform set or can see. Results produced before this state existed read the same way, which is
42
+ * what keeps a past turn's tally readable.
43
+ *
44
+ * @param result - The exec_command / wait_for_task result.
45
+ * @returns The state the result records.
46
+ */
47
+ export function commandRunState(result) {
48
+ if (result === null || typeof result !== 'object')
49
+ return 'ran';
50
+ return result.status === CUT_AT_BUDGET ? CUT_AT_BUDGET : 'ran';
51
+ }
52
+ /**
53
+ * The gap between a bound and the elapsed time a run killed at it reports. Zero, and deliberately
54
+ * not a tolerance: a command killed at its bound reports AT LEAST the bound (the shell's own
55
+ * bookkeeping runs after the kill, and this tool's round trip is on top), so any slack would only
56
+ * make a SHORT bound fire on a run that finished inside it (`timeout 5 curl …` answering in 3 s).
57
+ * A run that finished near its bound carried its runner's closing summary — {@link suiteTally}.
58
+ *
59
+ * @param boundSeconds - The bound the run was allowed.
60
+ * @returns The elapsed ms a run stopped at that bound reports at least.
61
+ */
62
+ function atBoundMs(boundSeconds) {
63
+ return boundSeconds * 1000;
64
+ }
65
+ /**
66
+ * A runner's closing summary, printed only by a run that walked its whole suite: Hurl's
67
+ * `Executed files:` block and the `N passed` / `N failed` / `N flaky` line Playwright and Vitest
68
+ * print when they finish.
69
+ *
70
+ * This is what separates "stopped at the bound" from "used the bound and finished": a suite that
71
+ * printed its summary ran to its end, and is read as the run it was.
72
+ */
73
+ const SUITE_TALLY = /\bExecuted files:\s*\d+|\b\d+\s+(?:passed|failed|flaky|skipped)\b/;
74
+ /**
75
+ * Whether text carries a runner's closing summary ({@link SUITE_TALLY}).
76
+ *
77
+ * @param text - The run's stdout and stderr.
78
+ * @returns True when the runner's own end-of-run summary is there.
79
+ */
80
+ export function suiteTally(text) {
81
+ return SUITE_TALLY.test(text);
82
+ }
83
+ /** The last thing a run printed: its last non-blank line, as one line and short enough to quote. */
84
+ function lastPrinted(text) {
85
+ const lines = text.split('\n');
86
+ for (let i = lines.length - 1; i >= 0; i -= 1) {
87
+ const line = (lines[i] ?? '').trim();
88
+ if (line)
89
+ return line.length > 180 ? `${line.slice(0, 177)}…` : line;
90
+ }
91
+ return null;
92
+ }
93
+ /** Hurl's per-file line: `Success hurl/articles.hurl (17 request(s) in 318 ms)`. */
94
+ const HURL_FILE_LINE = /^(?:Success|Failure)\s+(\S+)/gm;
95
+ /** Playwright's progress line — `[5/13] specs/auth.spec.ts:10:5 › signs up`. */
96
+ const PROGRESS_LINE = /^\s*\[(\d+)\/(\d+)\]/gm;
97
+ /**
98
+ * What the runner's own output says the run got through.
99
+ *
100
+ * Hurl prints a line per finished file and no total unless it finished; Playwright's line reporter
101
+ * prints `[n/K]` with both numbers. Neither is inferred from the command — a run that printed
102
+ * nothing says nothing, which is what `null` means here.
103
+ *
104
+ * @param text - The run's stdout and stderr.
105
+ * @param suite - Whether the command runs a suite.
106
+ * @returns How many finished, how many the run was walking, and the noun.
107
+ */
108
+ function runProgress(text, suite) {
109
+ const files = new Set();
110
+ for (const m of text.matchAll(HURL_FILE_LINE))
111
+ files.add(m[1] ?? '');
112
+ if (files.size > 0)
113
+ return { ran: files.size, total: null, noun: 'files' };
114
+ let ran = null;
115
+ let total = null;
116
+ for (const m of text.matchAll(PROGRESS_LINE)) {
117
+ ran = Number(m[1]);
118
+ total = Number(m[2]);
119
+ }
120
+ if (ran !== null)
121
+ return { ran, total, noun: 'tests' };
122
+ return { ran: null, total: null, noun: suite ? 'tests' : 'files' };
123
+ }
124
+ /**
125
+ * The run a command's result records as stopped by a budget, or null when the result is a run that
126
+ * ended (or says nothing about a run).
127
+ *
128
+ * Evidence, in order — never a guess:
129
+ *
130
+ * - `timeout`'s own 124 (the code the backend's wrapper returns whenever the TOOL's budget fired,
131
+ * whatever the pipeline ended with) — and, when the clock says the command's own smaller bound
132
+ * ran out first and less than the budget, that bound instead.
133
+ * - the command carried its own `timeout <N>`, N is inside this tool's budget (a larger one never
134
+ * fires — the tool's does), the run used N, and no runner printed a closing summary: the run was
135
+ * killed at its own bound. Its exit code, when the pipeline handed one back, is the filter's.
136
+ * - the run used this tool's whole budget behind a filter that withheld its output, and printed no
137
+ * closing summary: the budget stopped it, and no code in the result is the run's.
138
+ *
139
+ * @param command - The command the executor sent.
140
+ * @param result - Its result.
141
+ * @param elapsedMs - How long the command ran, in ms.
142
+ * @param budgetSeconds - This tool's foreground budget, in seconds (0 when it has none).
143
+ * @returns The stopped run, or null.
144
+ */
145
+ export function commandCutRun(command, result, elapsedMs, budgetSeconds = 0) {
146
+ if (typeof command !== 'string' || !command.trim())
147
+ return null;
148
+ if (result === null || typeof result !== 'object')
149
+ return null;
150
+ if (!Number.isFinite(elapsedMs))
151
+ return null;
152
+ const r = result;
153
+ const exitCode = typeof r.exitCode === 'number' ? r.exitCode : null;
154
+ const text = `${String(r.stdout ?? '')}\n${String(r.stderr ?? '')}`;
155
+ // A suite that printed its summary ran to its end, whatever it was wrapped in.
156
+ if (suiteTally(text))
157
+ return null;
158
+ const suite = isSuiteRunCommand(command);
159
+ const withheld = /\|\s*(?:tail|head|grep|egrep|fgrep|sort|uniq|wc|jq)\b/.test(command);
160
+ const progress = runProgress(text, suite);
161
+ const doing = lastPrinted(text);
162
+ const declared = commandTimeoutSeconds(command);
163
+ // A command whose own bound is inside this tool's budget is stopped by ITS bound: a larger one
164
+ // never gets the chance — the backend's `timeout` fires first (see the module doc). With no
165
+ // tool budget at all (a reader re-reading a background run) the command's own bound is the only
166
+ // one there is.
167
+ const ownBoundStops = budgetSeconds === 0 || (declared !== null && declared < budgetSeconds);
168
+ // `timeout`'s own 124 — the code the backend's wrapper returns when the TOOL's budget fires,
169
+ // whatever the pipeline ended with (`backends/sandbox.ts`). Which of the two bounds fired is the
170
+ // clock's to say: a run killed at the tool's budget reports at least the budget, and one killed
171
+ // at its own bound reports at least that bound and less than the budget.
172
+ if (budgetSeconds > 0 && exitCode === 124) {
173
+ const ownFired = declared !== null &&
174
+ ownBoundStops &&
175
+ elapsedMs >= atBoundMs(declared) &&
176
+ elapsedMs < atBoundMs(budgetSeconds);
177
+ return {
178
+ seconds: ownFired ? declared : budgetSeconds,
179
+ stoppedBy: ownFired ? 'command-timeout' : 'tool-budget',
180
+ exitCode,
181
+ ...progress,
182
+ doing,
183
+ };
184
+ }
185
+ if (declared !== null && ownBoundStops && elapsedMs >= atBoundMs(declared)) {
186
+ return {
187
+ seconds: declared,
188
+ stoppedBy: 'command-timeout',
189
+ // A code the filter produced is not the run's: `… | tail` handed back 0 forever (m93).
190
+ exitCode: withheld || exitCode === 0 ? null : exitCode,
191
+ ...progress,
192
+ doing,
193
+ };
194
+ }
195
+ // The tool's budget with its code masked: the run used the whole budget and the pipeline left
196
+ // nothing readable of it — no code of its own, and no runner's closing summary.
197
+ if (budgetSeconds > 0 && withheld && elapsedMs >= atBoundMs(budgetSeconds)) {
198
+ return {
199
+ seconds: budgetSeconds,
200
+ stoppedBy: 'tool-budget',
201
+ exitCode: exitCode === 0 ? null : exitCode,
202
+ ...progress,
203
+ doing,
204
+ };
205
+ }
206
+ return null;
207
+ }
208
+ /**
209
+ * The facts a result records about the run a budget stopped it in — what a reader needs to say
210
+ * how far the run got WITHOUT re-reading its log or re-deriving the counts.
211
+ *
212
+ * The numbers are read from the runner's own output where it printed them (Hurl's per-file lines,
213
+ * Playwright's `[n/K]`), so a caller that can also see the suite itself — the file count of the
214
+ * `*.hurl` directory the run walked — fills in the `total` this could not know, and both then say
215
+ * the same sentence ({@link cutRunSentence}).
216
+ *
217
+ * @param result - The exec_command / wait_for_task result.
218
+ * @returns The facts the result carries, or null when it records no stopped run.
219
+ */
220
+ export function cutRunFacts(result) {
221
+ if (result === null || typeof result !== 'object')
222
+ return null;
223
+ if (commandRunState(result) !== CUT_AT_BUDGET)
224
+ return null;
225
+ const cut = result.cutRun;
226
+ if (cut === null || typeof cut !== 'object')
227
+ return null;
228
+ const c = cut;
229
+ if (typeof c.seconds !== 'number' || typeof c.stoppedBy !== 'string')
230
+ return null;
231
+ return {
232
+ seconds: c.seconds,
233
+ stoppedBy: c.stoppedBy,
234
+ exitCode: typeof c.exitCode === 'number' ? c.exitCode : null,
235
+ ran: typeof c.ran === 'number' ? c.ran : null,
236
+ total: typeof c.total === 'number' ? c.total : null,
237
+ noun: c.noun === 'tests' ? 'tests' : 'files',
238
+ doing: typeof c.doing === 'string' && c.doing ? c.doing : null,
239
+ };
240
+ }
241
+ /**
242
+ * The first line of a stopped run's result: how long it was allowed, what stopped it, how far it
243
+ * got, and the last thing it printed.
244
+ *
245
+ * Written to be quotable by the model in one sentence and to leave no room for "nearly passing":
246
+ * the counts come from the runner's own output ({@link runProgress}), and a `total` the caller
247
+ * read elsewhere (the suite's own file count) fills in the "M of K" it can then state.
248
+ *
249
+ * @param run - The stopped run.
250
+ * @returns The sentence.
251
+ */
252
+ export function cutRunSentence(run) {
253
+ const by = run.stoppedBy === 'tool-budget' ? "this tool's limit" : `its own \`timeout ${run.seconds}\``;
254
+ const lead = `This run was stopped after ${run.seconds}s (${by})`;
255
+ const count = run.ran !== null && run.total !== null
256
+ ? `: ${run.ran} of ${run.total} ${run.noun} ran, the rest did not run`
257
+ : run.ran !== null
258
+ ? `: ${run.ran} ${run.noun} ran and the rest did not run`
259
+ : ' before it finished';
260
+ const doing = run.doing ? `; the last thing it was doing: ${run.doing}` : '';
261
+ // Why the result carries no code: without this the model reads the missing field as an
262
+ // omission, and the number it used to read there was the pipeline's own 0.
263
+ const code = run.exitCode === null
264
+ ? ' It has no exit code of its own to report — the one this pipeline produced belongs to the filter, not to the run.'
265
+ : '';
266
+ return `${lead}${count}${doing}.${code}`;
267
+ }
package/dist/index.d.ts CHANGED
@@ -133,12 +133,14 @@ export * from './backends/local.js';
133
133
  export * from './backends/sandbox.js';
134
134
  export * from './browser-guard.js';
135
135
  export * from './command-position.js';
136
+ export * from './cut-run.js';
136
137
  export * from './input-normalizer.js';
137
138
  export * from './no-test-run.js';
138
139
  export * from './schemas.js';
139
140
  export * from './suite-overlap.js';
140
141
  export * from './system-prompt.js';
141
142
  export * from './tools.js';
143
+ export * from './trace-artifacts.js';
142
144
  export * from './types.js';
143
145
  export * from './utilities.js';
144
146
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkIG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,oBAAoB,CAAA;AAClC,cAAc,uBAAuB,CAAA;AACrC,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAC1B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkIG;AAEH,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,oBAAoB,CAAA;AAClC,cAAc,uBAAuB,CAAA;AACrC,cAAc,cAAc,CAAA;AAC5B,cAAc,uBAAuB,CAAA;AACrC,cAAc,kBAAkB,CAAA;AAChC,cAAc,cAAc,CAAA;AAC5B,cAAc,oBAAoB,CAAA;AAClC,cAAc,oBAAoB,CAAA;AAClC,cAAc,YAAY,CAAA;AAC1B,cAAc,sBAAsB,CAAA;AACpC,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA"}
package/dist/index.js CHANGED
@@ -133,11 +133,13 @@ export * from './backends/local.js';
133
133
  export * from './backends/sandbox.js';
134
134
  export * from './browser-guard.js';
135
135
  export * from './command-position.js';
136
+ export * from './cut-run.js';
136
137
  export * from './input-normalizer.js';
137
138
  export * from './no-test-run.js';
138
139
  export * from './schemas.js';
139
140
  export * from './suite-overlap.js';
140
141
  export * from './system-prompt.js';
141
142
  export * from './tools.js';
143
+ export * from './trace-artifacts.js';
142
144
  export * from './types.js';
143
145
  export * from './utilities.js';
package/dist/tools.d.ts CHANGED
@@ -26,6 +26,26 @@ import type { AITool } from '@molecule/api-ai';
26
26
  */
27
27
  export declare function taskStopCommand(backend: ExecutionBackend, taskId: string): Promise<string>;
28
28
  import type { ExecutionBackend, ToolBuildConfig } from './types.js';
29
+ /**
30
+ * The closing advice on an over-budget message for a command that is not a test
31
+ * suite: the work it was chaining is what to split.
32
+ */
33
+ export declare const SPLIT_WORK_ADVICE: string;
34
+ /**
35
+ * The closing advice on an over-budget message for a command that IS a suite run
36
+ * (Playwright, Vitest, a `hurl` run — {@link isSuiteRunCommand}): its files are not
37
+ * a bigger unit to break up.
38
+ *
39
+ * X1 flash-m36 asked for this half and the message kept saying "run a smaller unit"
40
+ * for two months. It is the wrong direction for a suite, and measurably: a run
41
+ * stopped here reports NO tally, so nothing in the result says which test or request
42
+ * hung — the run has to be sent again in the background (off this budget, its output
43
+ * written to a log that can be read mid-run) or, when a consumer knows the runner's
44
+ * per-request limit, with each request bounded. xr-molecule-flash-m93 (+06:22,
45
+ * 2026-10-16) lost 4.8 minutes to a Hurl run whose ONE unanswered request had hurl's
46
+ * 300 s default per request holding the file, ten seconds past this tool's budget.
47
+ */
48
+ export declare const SUITE_OVER_BUDGET_ADVICE: string;
29
49
  /**
30
50
  * The steer attached to a SUCCESSFUL read of a package's built output. The ENOENT
31
51
  * redirect below fires only when the read FAILS, so a model that knows the hoisted
@@ -1 +1 @@
1
- {"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../src/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