@mergeline/agent 0.7.1 → 0.7.3

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 CHANGED
@@ -202,8 +202,9 @@ tickets, checking every `--interval <s>` (default 30); `--max-runs <n>`
202
202
  (default 10) is the most tickets one start takes; `--timeout <min>`
203
203
  (default 90) is the most one engine run may take; what follows `--` is
204
204
  added to the engine's command line. It stops when the queue is empty
205
- (unless `--follow`), after `--max-runs` tickets, or after 3 failed runs
206
- (releases) in a row; after a failed run it waits `--interval` before
205
+ (unless `--follow`), after `--max-runs` tickets (with `--follow` too: it
206
+ says so, and a new start goes on), or after 3 failed runs (releases) in a
207
+ row; after a failed run it waits `--interval` before
207
208
  claiming again. Ctrl+C while the engine runs releases
208
209
  the ticket. A worker killed outright leaves its ticket In progress; a new
209
210
  one waits, claiming every `--interval`, until the board lets it resume (5
@@ -283,9 +284,12 @@ After the engine, the worker checks that neither the worktree's `HEAD` nor
283
284
  the branch on origin moved; if one did, it sends nothing and releases the
284
285
  run. It runs the repository's `test` command (from `origin/main`) as a
285
286
  gate, and a failing one adds a blocking finding, "The test command fails",
286
- with its output as proof. A run with no `review.json`, or a malformed one,
287
- is released, and the board queues the review again (three tries, then the
288
- PR comes to a person marked not reviewed). The worktree is always removed:
287
+ with its output as proof. The proof starts with the lines that name the
288
+ failure (`FAIL`, `not ok`, `✕`, each with the line after it; a passing
289
+ check's line never counts), then the end of the output; the gate's detail
290
+ and a developer's question do the same. A run with no `review.json`, or a
291
+ malformed one, is released, and the board queues the review again (three
292
+ tries, then the PR comes to a person marked not reviewed). The worktree is always removed:
289
293
  what the engine changed there is never kept or pushed.
290
294
 
291
295
  The review goes to the PR through `gh`, as the person who runs the worker,
package/dist/review.js CHANGED
@@ -215,6 +215,89 @@ export function tailWithin(text, max) {
215
215
  const lead = "… ";
216
216
  return lead + t.slice(-(max - lead.length));
217
217
  }
218
+ /** The last lines of a command's output (a question's "The end of its output"). */
219
+ export function tail(text, lines = 40, chars = 4000) {
220
+ const end = text.replace(/\s+$/, "").split("\n").slice(-lines).join("\n");
221
+ return end.length > chars ? end.slice(-chars) : end;
222
+ }
223
+ /** The most failure lines a test's output is shown with, and the most of each line. */
224
+ const FAILURES_MAX = 10;
225
+ const FAILURE_LINE = 300;
226
+ /**
227
+ * A line that names a failure: FAIL or FAILED in capitals (this repository's suites, Jest, Vitest, pytest, Go's
228
+ * --- FAIL), a TAP `not ok`, or a line that starts with ✕ or ✗. Lowercase "failed" does not count: a summary reads
229
+ * "139 passed, 1 failed", and a passing check can say "failed ones too". A passing check never names a failure,
230
+ * whatever it says: a line that starts with `ok` (TAP, this repository's suites), `✓`, `✔` or `PASS`. Indents and
231
+ * colour codes are ignored.
232
+ */
233
+ function namesFailure(line) {
234
+ // biome-ignore lint/suspicious/noControlCharactersInRegex: the ESC of a colour code
235
+ const l = line.replace(/\x1b\[[0-9;]*m/g, "");
236
+ if (/^\s*(?:ok\b|[✓✔]|PASS\b)/.test(l))
237
+ return false;
238
+ return /(?:^|[^A-Za-z0-9_])FAIL(?:ED)?(?![A-Za-z0-9_])/.test(l) || /^\s*not ok\b/.test(l) || /^\s*[✕✗]/.test(l);
239
+ }
240
+ /**
241
+ * The lines of a test command's output that name a failure, each with the line after it (where this repository's
242
+ * suites print `got: …`): the first 10, each line cut to 300 characters with the worker's mark.
243
+ *
244
+ * shortcut: the output is what the worker keeps of it, its last 256 KB; a failure printed before that is not found.
245
+ * `npm test` here chains its suites with &&, so the failing suite is the last one.
246
+ */
247
+ export function failureLines(output) {
248
+ // A line keeps its indent (cut trims) unless it is cut.
249
+ const fit = (l) => (l.length <= FAILURE_LINE ? l : cut(l, FAILURE_LINE));
250
+ const lines = output.replace(/\s+$/, "").split("\n");
251
+ const out = [];
252
+ let found = 0;
253
+ for (let i = 0; i < lines.length && found < FAILURES_MAX; i++) {
254
+ if (!namesFailure(lines[i]))
255
+ continue;
256
+ found++;
257
+ out.push(fit(lines[i].replace(/\s+$/, "")));
258
+ const next = lines[i + 1];
259
+ // A next line that names a failure is its own (or one past the 10).
260
+ if (next !== undefined && next.trim() && !namesFailure(next))
261
+ out.push(fit(next.replace(/\s+$/, "")));
262
+ }
263
+ return out;
264
+ }
265
+ /**
266
+ * A failed test command's output for one place, within max characters: the lines that name a failure first, under
267
+ * "Failures:", then the end of the output (tail) cut to what is left. With no such line the text is as before R9.2:
268
+ * head and the tail within max.
269
+ * - head: what goes first (`exit N\n`), counted in max.
270
+ * - first: only the first failure line, no labels (a gate's detail of 500).
271
+ * - fence: each part in a ``` block, the end under "The end of its output:" (a question to the person, head its
272
+ * sentence): head, then " " and that label and block as before, or a blank line and both parts.
273
+ */
274
+ export function testOutputWithin(output, max, opts = {}) {
275
+ const head = opts.head ?? "";
276
+ const end = tail(output);
277
+ const block = (label, body) => (opts.fence ? `${label}\n\n\`\`\`\n${body}\n\`\`\`` : `${label}\n${body}`);
278
+ const failures = failureLines(output);
279
+ // The end within room characters; nothing when too little is left to show anything.
280
+ const endWithin = (room) => (room >= 20 ? tailWithin(end, room) : "");
281
+ if (!failures.length) {
282
+ if (opts.fence)
283
+ return `${head}${head ? " " : ""}${block("The end of its output:", end)}`;
284
+ return tailWithin(head + end, max);
285
+ }
286
+ if (opts.first) {
287
+ const line = cut(failures[0], Math.max(1, max - head.length));
288
+ const rest = endWithin(max - head.length - line.length - 1);
289
+ return `${head}${line}${rest ? `\n${rest}` : ""}`;
290
+ }
291
+ const lead = opts.fence && head ? `${head}\n\n` : head;
292
+ const shell = block("Failures:", "").length + 2 + block("The end of its output:", "").length;
293
+ const room = max - lead.length - shell;
294
+ let lines = failures.join("\n");
295
+ // The failure lines come first, but not at the cost of the whole end: past two thirds of the room they are cut.
296
+ if (lines.length > room - Math.min(end.length, Math.floor(room / 3)))
297
+ lines = cut(lines, Math.max(1, room - Math.min(end.length, Math.floor(room / 3))));
298
+ const rest = endWithin(room - lines.length);
299
+ return `${lead}${block("Failures:", lines)}\n\n${block("The end of its output:", rest)}`;
300
+ }
218
301
  /**
219
302
  * The number the board will give each finding (R5's mergeFindings): an earlier finding's own, else the next F<n>
220
303
  * in the order they are sent.
package/dist/worker.js CHANGED
@@ -33,7 +33,7 @@ import { closeSync, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, rea
33
33
  import { homedir, tmpdir } from "node:os";
34
34
  import { join, resolve } from "node:path";
35
35
  import { reviewPrompt } from "./engines.js";
36
- import { badPath, boardNumbers, ciGates, diffHunks, findingMarkdown, fitFindings, fitGate, ghFailure, githubBody, githubPr, inHunks, LIMITS, newRefs, readReview, reviewTicketFile, rulesFile, tailWithin, TEST_FINDING, withTestFinding, } from "./review.js";
36
+ import { badPath, boardNumbers, ciGates, diffHunks, findingMarkdown, fitFindings, fitGate, ghFailure, githubBody, githubPr, inHunks, LIMITS, newRefs, readReview, reviewTicketFile, rulesFile, tail, tailWithin, testOutputWithin, TEST_FINDING, withTestFinding, } from "./review.js";
37
37
  import { BoardError, BoardPaused, } from "./service.js";
38
38
  import { hasWork, isUnreachable, label, sleep, takeLock, untilAnswered } from "./watch.js";
39
39
  const FAILURE_STOP = 3;
@@ -374,11 +374,6 @@ function readFrom(file, offset, max) {
374
374
  closeSync(fd);
375
375
  }
376
376
  }
377
- /** The last lines of a command's output, for a question. */
378
- export function tail(text, lines = 40, chars = 4000) {
379
- const end = text.replace(/\s+$/, "").split("\n").slice(-lines).join("\n");
380
- return end.length > chars ? end.slice(-chars) : end;
381
- }
382
377
  /** Runs the worker until a stop condition; returns the exit code for the CLI. */
383
378
  export async function work(opts) {
384
379
  const { board, engine, log, repo } = opts;
@@ -818,7 +813,7 @@ export async function work(opts) {
818
813
  if (t.code === 0 && !t.killed)
819
814
  return null;
820
815
  const how = t.killed ? "ran past the timeout" : `failed (exit ${t.code})`;
821
- return `The test command \`${testCommand}\` ${how} after the engine said done. The end of its output:\n\n\`\`\`\n${tail(t.output)}\n\`\`\``;
816
+ return testOutputWithin(t.output, LONG_MAX, { head: `The test command \`${testCommand}\` ${how} after the engine said done.`, fence: true });
822
817
  };
823
818
  if (result.outcome === "done") {
824
819
  const failed = await runTest();
@@ -1148,18 +1143,19 @@ export async function work(opts) {
1148
1143
  held();
1149
1144
  checkConfig();
1150
1145
  const passed = t.code === 0 && !t.killed;
1151
- const end = tail(t.output);
1152
1146
  gates.push(fitGate({
1153
1147
  name: testCommand,
1154
1148
  result: t.killed ? "not_run" : passed ? "passed" : "failed",
1155
- detail: t.killed ? "stopped: it ran past the worker's timeout" : tailWithin(passed ? end : `exit ${t.code}\n${end}`, LIMITS.short),
1149
+ detail: t.killed ? "stopped: it ran past the worker's timeout" : passed
1150
+ ? tailWithin(tail(t.output), LIMITS.short)
1151
+ : testOutputWithin(t.output, LIMITS.short, { head: `exit ${t.code}\n`, first: true }),
1156
1152
  source: "worker",
1157
1153
  }));
1158
1154
  if (!t.killed) {
1159
1155
  findings = withTestFinding(findings, review.findings, {
1160
1156
  command: testCommand,
1161
1157
  passed,
1162
- output: tailWithin(`exit ${t.code}\n${end}`, LIMITS.proof - testCommand.length - 3),
1158
+ output: passed ? "" : testOutputWithin(t.output, LIMITS.proof - testCommand.length - 3, { head: `exit ${t.code}\n` }),
1163
1159
  });
1164
1160
  }
1165
1161
  if (!passed)
@@ -1352,10 +1348,13 @@ export async function work(opts) {
1352
1348
  if (mine === null)
1353
1349
  break;
1354
1350
  if (hasWork(mine)) {
1355
- log(`stopping after ${runs} runs (--max-runs) with work still in the queue`);
1351
+ log(`stopping after ${runs} runs (--max-runs ${opts.maxRuns}) with work still in the queue`);
1356
1352
  return 1;
1357
1353
  }
1358
- log(`queue empty after ${runs} run(s); stopping`);
1354
+ if (opts.follow)
1355
+ log(`stopping after ${runs} runs (--max-runs ${opts.maxRuns}); start it again to go on (--max-runs sets how many)`);
1356
+ else
1357
+ log(`queue empty after ${runs} run(s); stopping`);
1359
1358
  return 0;
1360
1359
  }
1361
1360
  let claimed;
package/engine/prompt.md CHANGED
@@ -14,8 +14,14 @@ on the ticket's branch. Stay in it: do not read or write outside it.
14
14
  - On a rework run (the notes end with a review that asks for changes), the
15
15
  ticket's notes say what to change: the run is done when each point there
16
16
  is fixed or answered in the summary.
17
- - A fresh worktree has no dependencies: install them with the project's
18
- package manager (`npm ci`, `pnpm install`, ...) before you run anything.
17
+ - A fresh worktree has no dependencies: install them before you run
18
+ anything, the way the repository's lockfile says: `package-lock.json`,
19
+ `npm ci`; `pnpm-lock.yaml`, `pnpm install --frozen-lockfile`;
20
+ `yarn.lock`, `yarn install --frozen-lockfile`. With no lockfile, run
21
+ `npm install --no-package-lock` (it writes none), and only when
22
+ `package.json` lists dependencies: with none, there is nothing to install.
23
+ - A lockfile the run did not mean to add is not part of the change: do not
24
+ leave one behind.
19
25
 
20
26
  ## Steps
21
27
 
package/engine/review.md CHANGED
@@ -21,9 +21,13 @@ outside it.
21
21
 
22
22
  1. **Read** `ticket.md` and `rules.md`, then the diff
23
23
  (`git diff origin/main...HEAD`) and the code it touches.
24
- 2. **Install the dependencies** with the project's package manager (`npm
25
- ci`, `pnpm install`, ...), and run what `rules.md` says to run. Run
26
- nothing that writes outside this folder.
24
+ 2. **Install the dependencies** the way the repository's lockfile says:
25
+ `package-lock.json`, `npm ci`; `pnpm-lock.yaml`, `pnpm install
26
+ --frozen-lockfile`; `yarn.lock`, `yarn install --frozen-lockfile`. With
27
+ no lockfile, run `npm install --no-package-lock` (it writes none), and
28
+ only when `package.json` lists dependencies: with none, there is nothing
29
+ to install. Then run what `rules.md` says to run. Run nothing that
30
+ writes outside this folder.
27
31
  3. **Check the change against the spec,** requirement by requirement, and
28
32
  against `rules.md`. Then hunt for bugs in the changed code and in its
29
33
  callers.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mergeline/agent",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "The agent's side of a Mergeline board: an MCP server and the `board` CLI. Works with any agent tool that runs MCP servers or shell commands.",
5
5
  "license": "MIT",
6
6
  "author": "frontender-ua",