@mergeline/agent 0.5.3 → 0.6.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.
- package/README.md +117 -3
- package/dist/cli.js +64 -19
- package/dist/engines.js +126 -0
- package/dist/mcp.js +3 -2
- package/dist/repo.js +6 -3
- package/dist/service.js +29 -4
- package/dist/terminal.js +30 -0
- package/dist/watch.js +115 -27
- package/dist/worker.js +1098 -0
- package/engine/prompt.md +58 -0
- package/package.json +2 -1
package/dist/watch.js
CHANGED
|
@@ -12,12 +12,14 @@
|
|
|
12
12
|
// non-zero the stop says they failed instead), or
|
|
13
13
|
// - the command failed three times in a row.
|
|
14
14
|
// A paused board (asleep until a person opens it) is not a failure: the
|
|
15
|
-
// watcher waits, checking every --interval, and goes on when it is back.
|
|
15
|
+
// watcher waits, checking every --interval, and goes on when it is back. So
|
|
16
|
+
// is a board it cannot reach (no answer, 502, 504, a 503 that is not a pause,
|
|
17
|
+
// 429), after the first contact: a wrong address is reported at once.
|
|
16
18
|
// One watcher per agent and machine: a lock file in the state directory.
|
|
17
19
|
import { spawn } from "node:child_process";
|
|
18
20
|
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
19
21
|
import { dirname } from "node:path";
|
|
20
|
-
import { BoardError, BoardPaused } from "./service.js";
|
|
22
|
+
import { BoardError, BoardPaused, BoardUnreachable } from "./service.js";
|
|
21
23
|
/** Usage from Claude Code's final JSON (`claude -p --output-format json`), or null. */
|
|
22
24
|
export function usageFromClaudeJson(output) {
|
|
23
25
|
const candidates = [output.trim(), ...output.trim().split("\n").reverse()];
|
|
@@ -87,6 +89,69 @@ export function errorFromClaudeJson(output) {
|
|
|
87
89
|
return null;
|
|
88
90
|
}
|
|
89
91
|
const ERROR_CHARS = 300;
|
|
92
|
+
/** Codex's `--json` events (JSONL), the ones that parse as objects. */
|
|
93
|
+
function codexEvents(output) {
|
|
94
|
+
const events = [];
|
|
95
|
+
for (const line of output.split("\n")) {
|
|
96
|
+
const text = line.trim();
|
|
97
|
+
if (!text.startsWith("{"))
|
|
98
|
+
continue;
|
|
99
|
+
try {
|
|
100
|
+
const e = JSON.parse(text);
|
|
101
|
+
if (e && typeof e === "object" && typeof e.type === "string")
|
|
102
|
+
events.push(e);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// not an event
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return events;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Usage from `codex exec --json`: the tokens of every `turn.completed`, summed.
|
|
112
|
+
* OpenAI counts cached input inside `input_tokens`; here `input_tokens` is
|
|
113
|
+
* without it and `cache_read_tokens` is it, as for Claude Code. Codex prints
|
|
114
|
+
* no cost. Null when no turn completed.
|
|
115
|
+
*/
|
|
116
|
+
export function usageFromCodexJson(output) {
|
|
117
|
+
const turns = codexEvents(output).filter((e) => e.type === "turn.completed" && e.usage && typeof e.usage === "object");
|
|
118
|
+
if (!turns.length)
|
|
119
|
+
return null;
|
|
120
|
+
const sum = (field) => {
|
|
121
|
+
const counts = turns.map((t) => t.usage[field]).filter((n) => Number.isInteger(n) && n >= 0);
|
|
122
|
+
return counts.length ? counts.reduce((a, b) => a + b, 0) : undefined;
|
|
123
|
+
};
|
|
124
|
+
const input = sum("input_tokens");
|
|
125
|
+
const cached = sum("cached_input_tokens");
|
|
126
|
+
const output_tokens = sum("output_tokens");
|
|
127
|
+
const usage = { source: "codex" };
|
|
128
|
+
if (input !== undefined)
|
|
129
|
+
usage.input_tokens = Math.max(0, input - (cached ?? 0));
|
|
130
|
+
if (output_tokens !== undefined)
|
|
131
|
+
usage.output_tokens = output_tokens;
|
|
132
|
+
if (cached !== undefined)
|
|
133
|
+
usage.cache_read_tokens = cached;
|
|
134
|
+
return usage;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The error a `codex exec --json` run ended with, one line, at most 300
|
|
138
|
+
* characters: the last `turn.failed`, else the last `error` event that is not
|
|
139
|
+
* a retry. Null when it reports none.
|
|
140
|
+
*/
|
|
141
|
+
export function errorFromCodexJson(output) {
|
|
142
|
+
const events = codexEvents(output);
|
|
143
|
+
const message = (e) => {
|
|
144
|
+
const m = e.type === "turn.failed" ? e.error?.message : e.message;
|
|
145
|
+
return typeof m === "string" && m.trim() ? m : null;
|
|
146
|
+
};
|
|
147
|
+
const failed = events.filter((e) => e.type === "turn.failed").map(message).filter((m) => m !== null);
|
|
148
|
+
const errors = events.filter((e) => e.type === "error").map(message).filter((m) => m !== null && !/^Reconnecting\b/.test(m));
|
|
149
|
+
const said = failed.at(-1) ?? errors.at(-1);
|
|
150
|
+
if (!said)
|
|
151
|
+
return null;
|
|
152
|
+
const line = said.replace(/\s+/g, " ").trim();
|
|
153
|
+
return line.length > ERROR_CHARS ? `${line.slice(0, ERROR_CHARS)}...` : line;
|
|
154
|
+
}
|
|
90
155
|
/**
|
|
91
156
|
* The ticket a run worked on, for its usage. Only a ticket the agent could
|
|
92
157
|
* take when the run started counts (ready or in progress then): what a human
|
|
@@ -103,7 +168,7 @@ export function workedOn(before, after) {
|
|
|
103
168
|
return before.find((t) => t.status === "in_progress") ?? null;
|
|
104
169
|
return null;
|
|
105
170
|
}
|
|
106
|
-
const label = (t) => /^T-\d+/.exec(t.title)?.[0] ?? t.id;
|
|
171
|
+
export const label = (t) => /^T-\d+/.exec(t.title)?.[0] ?? t.id;
|
|
107
172
|
const NO_PROGRESS_STOP = 2;
|
|
108
173
|
const FAILURE_STOP = 3;
|
|
109
174
|
export function hasWork(mine) {
|
|
@@ -125,7 +190,7 @@ function alive(pid) {
|
|
|
125
190
|
return false;
|
|
126
191
|
}
|
|
127
192
|
}
|
|
128
|
-
function takeLock(file) {
|
|
193
|
+
export function takeLock(file) {
|
|
129
194
|
try {
|
|
130
195
|
const pid = Number(JSON.parse(readFileSync(file, "utf8")).pid);
|
|
131
196
|
if (pid && pid !== process.pid && alive(pid)) {
|
|
@@ -139,7 +204,7 @@ function takeLock(file) {
|
|
|
139
204
|
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
140
205
|
writeFileSync(file, JSON.stringify({ pid: process.pid, at: new Date().toISOString() }) + "\n", { mode: 0o600 });
|
|
141
206
|
}
|
|
142
|
-
function sleep(ms, stop) {
|
|
207
|
+
export function sleep(ms, stop) {
|
|
143
208
|
return new Promise((resolve) => {
|
|
144
209
|
const step = 250;
|
|
145
210
|
let left = ms;
|
|
@@ -152,6 +217,48 @@ function sleep(ms, stop) {
|
|
|
152
217
|
tick();
|
|
153
218
|
});
|
|
154
219
|
}
|
|
220
|
+
/**
|
|
221
|
+
* A board that cannot be reached for now: no answer at all, a proxy's 502,
|
|
222
|
+
* 503 (not a pause) or 504, or the board's rate limit (429). Every other
|
|
223
|
+
* error means what it says.
|
|
224
|
+
*/
|
|
225
|
+
export function isUnreachable(err) {
|
|
226
|
+
if (err instanceof BoardPaused)
|
|
227
|
+
return false;
|
|
228
|
+
if (err instanceof BoardUnreachable)
|
|
229
|
+
return true;
|
|
230
|
+
return err instanceof BoardError && [429, 502, 503, 504].includes(err.status);
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* fn's result once the board answers: while it is paused or (unless strict)
|
|
234
|
+
* cannot be reached, waits intervalSec and tries again, with no limit. Logs
|
|
235
|
+
* once per outage and once when the board is back. Null when stopped while
|
|
236
|
+
* waiting; any other error is thrown.
|
|
237
|
+
*/
|
|
238
|
+
export async function untilAnswered(fn, o) {
|
|
239
|
+
let waiting = null;
|
|
240
|
+
while (!o.stop.stopped) {
|
|
241
|
+
try {
|
|
242
|
+
const result = await fn();
|
|
243
|
+
if (waiting)
|
|
244
|
+
o.log("the board is back");
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
catch (err) {
|
|
248
|
+
const kind = err instanceof BoardPaused ? "paused" : !o.strict && isUnreachable(err) ? "unreachable" : null;
|
|
249
|
+
if (!kind)
|
|
250
|
+
throw err;
|
|
251
|
+
if (kind !== waiting) {
|
|
252
|
+
o.log(kind === "paused"
|
|
253
|
+
? `${err.message}; waiting, checking every ${o.intervalSec}s (Ctrl+C to stop)`
|
|
254
|
+
: `board not reachable (${err.message}); retrying every ${o.intervalSec}s (Ctrl+C to stop)`);
|
|
255
|
+
}
|
|
256
|
+
waiting = kind;
|
|
257
|
+
await sleep(o.intervalSec * 1000, o.stop);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return null;
|
|
261
|
+
}
|
|
155
262
|
/** Runs the watcher until a stop condition; returns the exit code for the CLI. */
|
|
156
263
|
export async function watch(opts) {
|
|
157
264
|
const { board, command, log } = opts;
|
|
@@ -166,27 +273,8 @@ export async function watch(opts) {
|
|
|
166
273
|
};
|
|
167
274
|
process.on("SIGINT", onSignal);
|
|
168
275
|
process.on("SIGTERM", onSignal);
|
|
169
|
-
/** fn's result once the board answers; null when stopped while
|
|
170
|
-
const whenAwake =
|
|
171
|
-
let paused = false;
|
|
172
|
-
while (!state.stopped) {
|
|
173
|
-
try {
|
|
174
|
-
const result = await fn();
|
|
175
|
-
if (paused)
|
|
176
|
-
log("the board is back");
|
|
177
|
-
return result;
|
|
178
|
-
}
|
|
179
|
-
catch (err) {
|
|
180
|
-
if (!(err instanceof BoardPaused))
|
|
181
|
-
throw err;
|
|
182
|
-
if (!paused)
|
|
183
|
-
log(`${err.message}; waiting, checking every ${opts.intervalSec}s (Ctrl+C to stop)`);
|
|
184
|
-
paused = true;
|
|
185
|
-
await sleep(opts.intervalSec * 1000, state);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return null;
|
|
189
|
-
};
|
|
276
|
+
/** fn's result once the board answers; null when stopped while waiting for it. */
|
|
277
|
+
const whenAwake = (fn, strict = false) => untilAnswered(fn, { intervalSec: opts.intervalSec, log, stop: state, strict });
|
|
190
278
|
let runs = 0;
|
|
191
279
|
let noProgress = 0;
|
|
192
280
|
/** Exit codes of the runs counted in noProgress. */
|
|
@@ -194,7 +282,7 @@ export async function watch(opts) {
|
|
|
194
282
|
let failures = 0;
|
|
195
283
|
let waitingLogged = false;
|
|
196
284
|
try {
|
|
197
|
-
if ((await whenAwake(() => board.login())) === null)
|
|
285
|
+
if ((await whenAwake(() => board.login(), true)) === null)
|
|
198
286
|
return 0;
|
|
199
287
|
log(`watching ${board.agentName}'s queue; command: ${command.join(" ")}`);
|
|
200
288
|
while (!state.stopped) {
|