Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix(tools): reclaim stale locks atomically, pick the newest thread comment by time, stop publishing errored runs

Zhuowei Wangc8a435bfd58b
diff --git a/tools/linear-autofix/src/cli.mjs b/tools/linear-autofix/src/cli.mjs
index ef636bc..5ffde4f 100755
--- a/tools/linear-autofix/src/cli.mjs
+++ b/tools/linear-autofix/src/cli.mjs
@@ -166,6 +166,17 @@ async function main([command, arg]) {
const record = load(arg);
if (!record) throw new Error(`no record for ${arg}`);
record.consecutiveErrors = 0;
+ const open = await github.findPr(record.branch);
+ if (open?.state === "OPEN") {
+ record.pr = { number: open.number, url: open.url, draft: open.isDraft };
+ record.review ??= { rounds: 0 };
+ transition(
+ record,
+ "in_review",
+ `manual retry; ${open.url} is open, resuming the review loop`,
+ );
+ return;
+ }
transition(record, "new", "manual retry");
return;
}
diff --git a/tools/linear-autofix/src/implement.mjs b/tools/linear-autofix/src/implement.mjs
index c1d270f..50cda5a 100644
--- a/tools/linear-autofix/src/implement.mjs
+++ b/tools/linear-autofix/src/implement.mjs
@@ -104,8 +104,14 @@ export async function implement(issue, record) {
note(
record,
"implement run finished",
- `cost $${res.costUsd ?? "?"}, ${res.turns ?? "?"} turns`,
+ `cost $${res.costUsd ?? "?"}, ${res.turns ?? "?"} turns${res.isError ? ", ended in error" : ""}`,
);
+ if (res.isError) {
+ return escalate(
+ record,
+ `实现阶段的 Claude 运行以错误结束(预算或超时用尽、或内部错误),不发布它留下的提交:\n\n${res.text.slice(0, 1500)}\n\nworktree: \`${dir}\``,
+ );
+ }
const result = readJson(path.join(out, "result.json"));
if (result?.status === "blocked") {
diff --git a/tools/linear-autofix/src/review.mjs b/tools/linear-autofix/src/review.mjs
index 45bf00e..d35a42c 100644
--- a/tools/linear-autofix/src/review.mjs
+++ b/tools/linear-autofix/src/review.mjs
@@ -213,8 +213,14 @@ export async function review(issue, record) {
note(
record,
`review round ${round} run finished`,
- `cost $${res.costUsd ?? "?"}`,
+ `cost $${res.costUsd ?? "?"}${res.isError ? ", ended in error" : ""}`,
);
+ if (res.isError) {
+ return escalate(
+ record,
+ `review 修复的 Claude 运行以错误结束,不发布它留下的提交:\n\n${res.text.slice(0, 1500)}\n\nworktree: \`${dir}\``,
+ );
+ }
const result = readJson(path.join(out, "result.json")) ?? {
status: "fixed",
diff --git a/tools/linear-autofix/src/state.mjs b/tools/linear-autofix/src/state.mjs
index 87de7df..0f24cb6 100644
--- a/tools/linear-autofix/src/state.mjs
+++ b/tools/linear-autofix/src/state.mjs
@@ -126,18 +126,35 @@ export function latestCommentAt(issue) {
return stamps.length ? stamps.sort().at(-1) : null;
}
+/**
+ * Take the tick lock. The lock file is created exclusively; a file left by a
+ * dead process is removed and creation retried, so two starters racing over
+ * the same stale file cannot both win (only one unlink+create succeeds).
+ */
export function acquireLock() {
ensureStateDir();
- try {
- fs.writeFileSync(lockPath(), String(process.pid), { flag: "wx" });
- return true;
- } catch (err) {
- if (err.code !== "EEXIST") throw err;
- const pid = Number(fs.readFileSync(lockPath(), "utf8"));
+ for (let attempt = 0; attempt < 2; attempt++) {
+ try {
+ fs.writeFileSync(lockPath(), String(process.pid), { flag: "wx" });
+ return true;
+ } catch (err) {
+ if (err.code !== "EEXIST") throw err;
+ }
+ let pid = NaN;
+ try {
+ pid = Number(fs.readFileSync(lockPath(), "utf8"));
+ } catch (err) {
+ if (err.code !== "ENOENT") throw err;
+ continue; // the other starter removed it; retry the exclusive create
+ }
if (pid && isAlive(pid)) return false;
- fs.writeFileSync(lockPath(), String(process.pid));
- return true;
+ try {
+ fs.unlinkSync(lockPath());
+ } catch (err) {
+ if (err.code !== "ENOENT") throw err;
+ }
}
+ return false;
}
export function releaseLock() {
diff --git a/tools/linear-autofix/src/threads.mjs b/tools/linear-autofix/src/threads.mjs
index 6924204..1a9623b 100644
--- a/tools/linear-autofix/src/threads.mjs
+++ b/tools/linear-autofix/src/threads.mjs
@@ -15,12 +15,19 @@ export function splitThreads(threads, marker = config.botMarker) {
if (t.isResolved) continue;
const comments = t.comments?.nodes ?? [];
if (!comments.length) continue;
- const last = comments[comments.length - 1];
+ const last = newestComment(comments);
(String(last.body ?? "").includes(marker) ? awaitingHuman : fresh).push(t);
}
return { fresh, awaitingHuman };
}
+/** The comment with the latest createdAt; falls back to array order when timestamps are missing. */
+function newestComment(comments) {
+ return comments.reduce((best, c) =>
+ String(c.createdAt ?? "") >= String(best.createdAt ?? "") ? c : best,
+ );
+}
+
/** Unresolved threads that need the bot's attention. */
export function actionableThreads(threads, marker = config.botMarker) {
return splitThreads(threads, marker).fresh;
diff --git a/tools/linear-autofix/src/threads.test.mjs b/tools/linear-autofix/src/threads.test.mjs
index 5889cae..785de74 100644
--- a/tools/linear-autofix/src/threads.test.mjs
+++ b/tools/linear-autofix/src/threads.test.mjs
@@ -63,3 +63,33 @@ test("renderThreads includes the thread id, location and quoted comments", () =>
assert.match(text, /run\.ts:4/);
assert.match(text, /> \*\*reviewer\*\*: please add a test/);
});
+
+test("splitThreads picks the newest comment by createdAt, whatever the array order", () => {
+ const t = {
+ id: "t9",
+ isResolved: false,
+ comments: {
+ nodes: [
+ {
+ author: { login: "joway" },
+ body: "<!-- linear-autofix -->\nfixed",
+ createdAt: "2026-11-02T02:00:00Z",
+ },
+ {
+ author: { login: "reviewer" },
+ body: "still wrong",
+ createdAt: "2026-11-02T01:00:00Z",
+ },
+ ],
+ },
+ };
+ assert.deepEqual(
+ splitThreads([t]).awaitingHuman.map((x) => x.id),
+ ["t9"],
+ );
+ t.comments.nodes[1].createdAt = "2026-11-02T03:00:00Z";
+ assert.deepEqual(
+ splitThreads([t]).fresh.map((x) => x.id),
+ ["t9"],
+ );
+});
diff --git a/tools/linear-autofix/src/tick.test.mjs b/tools/linear-autofix/src/tick.test.mjs
index c369cc0..eac9877 100644
--- a/tools/linear-autofix/src/tick.test.mjs
+++ b/tools/linear-autofix/src/tick.test.mjs
@@ -96,6 +96,13 @@ test("the tick lock is exclusive, stale locks are reclaimed and the STOP file is
"a lock left by a dead process is reclaimed",
);
state.releaseLock();
+ assert.equal(
+ state.acquireLock(),
+ true,
+ "the reclaimed lock was recreated exclusively",
+ );
+ assert.equal(state.acquireLock(), false);
+ state.releaseLock();
assert.equal(state.stopRequested(), false);
state.setStop(true);
assert.equal(state.stopRequested(), true);