@mergeline/agent 0.8.0 → 0.8.1

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.
Files changed (3) hide show
  1. package/README.md +18 -1
  2. package/dist/worker.js +129 -9
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -207,7 +207,7 @@ added to the engine's command line. It stops when the queue is empty
207
207
  says so, and a new start goes on), or after 3 failed runs (releases) in a
208
208
  row; after a failed run it waits `--interval` before
209
209
  claiming again. Ctrl+C while the engine runs releases
210
- the ticket. A worker killed outright leaves its ticket In progress; a new
210
+ the ticket (below: its work is pushed first). A worker killed outright leaves its ticket In progress; a new
211
211
  one waits, claiming every `--interval`, until the board lets it resume (5
212
212
  silent minutes), and starts again from what origin has.
213
213
 
@@ -217,6 +217,23 @@ After upgrading the package (or rebuilding it from a checkout), stop the
217
217
  worker with Ctrl+C while it is not running an engine (it is waiting for a
218
218
  ticket), and start it again.
219
219
 
220
+ A run stopped before its result leaves its work on the ticket's branch:
221
+ the engine ran past `--timeout`, ended without a result, or you stopped the
222
+ worker (Ctrl+C, SIGTERM) while the engine ran. The worker commits what the
223
+ engine left uncommitted as `WIP: <ticket>: stopped before a result` (an
224
+ empty one when the engine had committed everything; the tests were not
225
+ run), pushes the branch `agent/<ticket>` once (no PR), and releases the
226
+ ticket. The release reason on the board says what happened (the time
227
+ limit in minutes, the exit code, or the signal), where the work is (`Its
228
+ work is pushed to agent/<ticket> ...`, or `It left no changes.`) and that
229
+ the next run goes on from it: that run starts from the branch on origin,
230
+ and its ticket file tells the engine to read the earlier work and go on.
231
+ Look at the work with `git log origin/main..origin/agent/<ticket>`. A
232
+ second Ctrl+C while the worker commits or pushes stops it at once: nothing
233
+ more is pushed, and what was committed is kept as below. If the push is
234
+ refused, the reason says so and where it is kept. A ticket that needs
235
+ longer runs: start the worker with a larger `--timeout <min>`.
236
+
220
237
  Commits of a run that origin does not have are kept, never lost: a run
221
238
  that ends without pushing (released by the engine or the worker, a question
222
239
  after a push that failed) keeps them as it deletes its local branch, and a
package/dist/worker.js CHANGED
@@ -391,12 +391,26 @@ export async function work(opts) {
391
391
  let engineChild = null;
392
392
  /** The signal that stopped the worker while an engine ran: that run is released. */
393
393
  let engineStopped = null;
394
+ /** A builder's engine runs: a run it stops has its work pushed (R6.3). */
395
+ let builderEngine = false;
396
+ /** The steps after a stopped builder run (R6.3), while they run; and their push: a second signal kills it. */
397
+ let stopSteps = false;
398
+ let stopPush = null;
394
399
  const onSignal = (signal) => {
395
400
  if (state.stopped)
396
401
  hard.stopped = true;
397
402
  state.stopped = true;
398
403
  stopSignal ??= signal;
399
- log(`${signal}: stopping${engineChild ? " the engine" : " after the current step"}`);
404
+ if (hard.stopped && stopSteps) {
405
+ log(`${signal}: stopping at once; nothing more is pushed`);
406
+ if (stopPush)
407
+ killGroup(stopPush);
408
+ return;
409
+ }
410
+ const what = engineChild
411
+ ? ` the engine${builderEngine ? "; its work will be pushed (again to stop at once)" : ""}`
412
+ : " after the current step";
413
+ log(`${signal}: stopping${what}`);
400
414
  if (engineChild) {
401
415
  engineStopped ??= signal;
402
416
  killGroup(engineChild);
@@ -507,9 +521,10 @@ export async function work(opts) {
507
521
  * as it was.
508
522
  */
509
523
  const gitEnv = withoutBoard(process.env);
510
- const git = async (cwd, ...args) => {
524
+ const git = async (cwd, ...args) => gitTracked(cwd, undefined, ...args);
525
+ const gitTracked = async (cwd, track, ...args) => {
511
526
  checkConfig();
512
- return exec("git", ["-c", "core.hooksPath=/dev/null", "-c", "core.fsmonitor=false", ...args], { cwd, env: gitEnv, group: true });
527
+ return exec("git", ["-c", "core.hooksPath=/dev/null", "-c", "core.fsmonitor=false", ...args], { cwd, env: gitEnv, group: true, track });
513
528
  };
514
529
  const removeWorktree = async (path, branch) => {
515
530
  await git(repo, "worktree", "remove", "--force", path);
@@ -690,7 +705,9 @@ export async function work(opts) {
690
705
  const runDir = join(path, RUN_DIR);
691
706
  mkdirSync(runDir, { recursive: true });
692
707
  writeFileSync(join(runDir, ".gitignore"), "*\n");
693
- writeFileSync(join(runDir, "ticket.md"), ticketFile(ticket, lbl, onOrigin, sha, spec.stdout));
708
+ // An earlier run stopped before its result left its work on the branch (R6.3): told from the branch alone.
709
+ const wipTip = onOrigin && (await git(repo, "log", "-1", "--format=%s", `origin/${ticket.branch}`)).stdout.startsWith("WIP: ");
710
+ writeFileSync(join(runDir, "ticket.md"), ticketFile(ticket, lbl, onOrigin, sha, spec.stdout, wipTip));
694
711
  // The engine.
695
712
  const originUrl = (await git(repo, "remote", "get-url", "origin")).stdout.trim();
696
713
  const deadline = Date.now() + opts.timeoutSec * 1000;
@@ -762,6 +779,7 @@ export async function work(opts) {
762
779
  track: (c) => {
763
780
  current = c;
764
781
  engineChild = c;
782
+ builderEngine = c !== null;
765
783
  },
766
784
  }).finally(() => clearInterval(poll));
767
785
  while (polling)
@@ -772,16 +790,113 @@ export async function work(opts) {
772
790
  await readFolder(true);
773
791
  held();
774
792
  usage = engine.usage(ran.stdout);
775
- if (engineStopped)
776
- return await release(`board watch was stopped (${engineStopped})`);
793
+ if (engineStopped) {
794
+ try {
795
+ checkConfig();
796
+ }
797
+ catch (err) {
798
+ // A changed git config: released as before R6.3, and nothing more of git (the finally stops the worker).
799
+ if (err instanceof ConfigChanged)
800
+ return await release(`board watch was stopped (${engineStopped})`);
801
+ throw err;
802
+ }
803
+ }
777
804
  checkConfig();
805
+ /**
806
+ * A run the worker stopped, or that ended without a result (R6.3): what the engine left is committed as WIP,
807
+ * pushed once to the ticket's branch, and the release says what happened, where the work is and what is next.
808
+ */
809
+ const stopped = async (what, why) => {
810
+ stopSteps = true;
811
+ try {
812
+ return await stoppedSteps(what, why);
813
+ }
814
+ finally {
815
+ stopSteps = false;
816
+ }
817
+ };
818
+ const stoppedSteps = async (what, why) => {
819
+ const problems = [];
820
+ let wip = 0;
821
+ const status = await git(path, "status", "--porcelain");
822
+ const dirty = status.code === 0 && status.stdout.trim() !== "";
823
+ if (status.code !== 0)
824
+ problems.push(`could not read the worktree's status (${oneLine(status.stderr)})`);
825
+ const count = async () => Number((await git(path, "rev-list", "--count", `${base}..HEAD`)).stdout.trim()) || 0;
826
+ if (!hard.stopped && (dirty || (await count()) > 0)) {
827
+ const added = dirty ? await git(path, "add", "-A") : null;
828
+ const body = [
829
+ why,
830
+ "The tests were not run.",
831
+ dirty ? `Committed by the worker: ${engine.name} left this uncommitted.` : "Committed by the worker, empty: it marks where the run stopped.",
832
+ ].join("\n\n");
833
+ const c = added && added.code !== 0 ? added : await git(path, "commit", ...(dirty ? [] : ["--allow-empty"]), "-m", `WIP: ${lbl}: stopped before a result`, "-m", body);
834
+ if (c.code === 0)
835
+ wip = 1;
836
+ else
837
+ problems.push(`could not commit what the engine left (${oneLine(c.stderr || c.stdout)})`);
838
+ }
839
+ const k = await count();
840
+ let where;
841
+ if (!k) {
842
+ where = dirty ? "Its uncommitted changes are not pushed." : "It left no changes.";
843
+ }
844
+ else {
845
+ let failure = "";
846
+ if (hard.stopped)
847
+ failure = "a second signal stopped the worker before the push";
848
+ else {
849
+ held();
850
+ await beat();
851
+ held();
852
+ const pushed = await gitTracked(path, (c) => (stopPush = c), "push", originUrl, `HEAD:refs/heads/${ticket.branch}`);
853
+ if (pushed.code === 0)
854
+ pushedOk = true;
855
+ else
856
+ failure = hard.stopped ? "a second signal stopped the push" : oneLine(pushed.stderr || pushed.stdout) || `exit ${pushed.code}`;
857
+ }
858
+ if (pushedOk) {
859
+ // The open PR of a rework run, told apart with gh pr view; a call that fails leaves the part out.
860
+ let pr = null;
861
+ if (!hard.stopped) {
862
+ checkConfig();
863
+ const view = await exec("gh", ["pr", "view", ticket.branch, "--json", "url,state"], { cwd: repo, env: withoutBoard(process.env), group: true });
864
+ if (view.code === 0) {
865
+ try {
866
+ const state = JSON.parse(view.stdout).state;
867
+ pr = state === "OPEN" ? "; its PR is still open, with unfinished work on it." : "; no PR.";
868
+ }
869
+ catch {
870
+ // not gh's JSON: left out
871
+ }
872
+ }
873
+ else if (/no (open )?pull requests? found/i.test(view.stderr)) {
874
+ pr = "; no PR.";
875
+ }
876
+ }
877
+ where = `Its work is pushed to ${ticket.branch} (${k} commit(s), ${wip} of them the worker's WIP commit)${pr ?? "."} The next run goes on from it.`;
878
+ }
879
+ else {
880
+ where = `Its work is not pushed (${failure}); it is kept on the worker's machine under refs/mergeline/kept/${ticket.id}/.`;
881
+ }
882
+ }
883
+ const problem = problems.length ? ` The worker ${problems.join("; ")}.` : "";
884
+ return await release(`${/[.!?]$/.test(what) ? what : `${what}.`}${problem} ${where}`);
885
+ };
886
+ if (engineStopped) {
887
+ const signal = engineStopped;
888
+ return await stopped(`board watch was stopped (${signal})`, `The worker was stopped (${signal}) while the engine ran, and stopped the engine.`);
889
+ }
778
890
  let result = ran.killed
779
- ? { outcome: "none", why: `the engine ran past the timeout (${opts.timeoutSec}s) and was stopped` }
891
+ ? { outcome: "none", why: `the engine ran past the time limit (${limitText(opts.timeoutSec)}) and was stopped` }
780
892
  : readResult(join(runDir, "result.json"));
781
893
  if (result.outcome === "none") {
782
894
  say(`no result from the engine: ${result.why}`);
895
+ if (ran.killed)
896
+ return await stopped(result.why, `The worker stopped the engine at the time limit (${limitText(opts.timeoutSec)}).`);
783
897
  const error = engine.error(ran.stdout);
784
- return await release(`the engine ended without a result (exit ${ran.code})${error ? `: ${error}` : ""}`);
898
+ const what = `the engine ended without a result (exit ${ran.code})${error ? `: ${error}` : ""}`;
899
+ return await stopped(what, `The engine ended without a result (exit ${ran.code}): ${result.why}.`);
785
900
  }
786
901
  // The engine's reports (R5), while the run is still open: before submit, ask or release, whatever the outcome.
787
902
  if (result.reports) {
@@ -1488,6 +1603,8 @@ function killGroup(child) {
1488
1603
  kill("SIGTERM");
1489
1604
  setTimeout(() => kill("SIGKILL"), KILL_GRACE_MS).unref();
1490
1605
  }
1606
+ /** A time limit as the worker names it (R6.3): whole minutes, else seconds. */
1607
+ export const limitText = (sec) => (sec % 60 === 0 ? `${sec / 60} min` : `${sec} s`);
1491
1608
  const oneLine = (s) => clip(s.replace(/\s+/g, " ").trim(), 500);
1492
1609
  /** A question for a run that could not push, after what the engine itself asked (if it asked). */
1493
1610
  export function withBefore(result, what, lost) {
@@ -1542,7 +1659,9 @@ async function repoTest(git, repo) {
1542
1659
  return null;
1543
1660
  }
1544
1661
  }
1545
- function ticketFile(t, lbl, onOrigin, sha, spec) {
1662
+ /** The line under Branch: when the branch's tip is an earlier run's WIP commit (R6.3). */
1663
+ export const WIP_LINE = "The branch holds an earlier run's work that was stopped before it finished: not tested, not reviewed. Read it (git log origin/main..HEAD) and go on from it; do not start over.";
1664
+ function ticketFile(t, lbl, onOrigin, sha, spec, wipTip = false) {
1546
1665
  const notes = t.notes ?? [];
1547
1666
  const round = 1 + notes.filter((n) => n.kind === "review").length;
1548
1667
  return [
@@ -1550,6 +1669,7 @@ function ticketFile(t, lbl, onOrigin, sha, spec) {
1550
1669
  "",
1551
1670
  `Ticket: ${lbl} (${t.id})`,
1552
1671
  `Branch: ${t.branch}, ${onOrigin ? `from origin/${t.branch}` : "new, from origin/main"}`,
1672
+ ...(wipTip ? [WIP_LINE] : []),
1553
1673
  `Review round: ${round}`,
1554
1674
  `Spec: ${t.spec_path}/spec.md at ${sha}`,
1555
1675
  "",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mergeline/agent",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
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",