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): require green CI before done, guard publish against a moved branch, add daemon mode

Zhuowei Wangbcff4eada710
diff --git a/tools/linear-autofix/README.md b/tools/linear-autofix/README.md
index da8c8f5..008635c 100644
--- a/tools/linear-autofix/README.md
+++ b/tools/linear-autofix/README.md
@@ -108,7 +108,21 @@ AUTOFIX_ISSUE=CITRO-2908 node src/cli.mjs tick # one real ticket end
```
Watch that PR through a full review loop (`node src/cli.mjs status`, the tick
-log comments on the log ticket), then schedule:
+log comments on the log ticket), then keep it running. Two options.
+
+**Option A, a `screen` session** (no launchd; the process paces itself):
+
+```sh
+screen -dmS linear-autofix /bin/zsh -lc 'source ~/.config/linear-autofix/env && cd ~/code/ego-lite/tools/linear-autofix && exec node src/cli.mjs daemon 2>&1 | tee -a /tmp/linear-autofix.log'
+screen -r linear-autofix # attach; Ctrl-A D detaches again
+```
+
+`daemon` runs a tick, sleeps `AUTOFIX_INTERVAL_MS` (default 30 min), repeats.
+Ctrl-C or SIGTERM lets the current tick finish, then exits. The STOP file
+skips ticks without killing the loop. A `screen` session does not survive a
+reboot or a logout, so after either one start it again (or keep Option B).
+
+**Option B, launchd** (survives reboots once the GUI session is back):
```sh
cp launchd/com.citrolabs.linear-autofix.plist ~/Library/LaunchAgents/
diff --git a/tools/linear-autofix/src/cli.mjs b/tools/linear-autofix/src/cli.mjs
index d0caffb..62705bb 100755
--- a/tools/linear-autofix/src/cli.mjs
+++ b/tools/linear-autofix/src/cli.mjs
@@ -9,7 +9,7 @@ import {
transition,
upsertFromIssue,
} from "./state.mjs";
-import { reconcile, tick } from "./tick.mjs";
+import { daemon, reconcile, tick } from "./tick.mjs";
import { triage } from "./triage.mjs";
import { implement, publish } from "./implement.mjs";
import { review } from "./review.mjs";
@@ -19,6 +19,7 @@ const USAGE = `linear-autofix — Linear ticket → PR → review loop, unattend
Usage:
linear-autofix tick one scheduler pass over all assigned tickets
+ linear-autofix daemon run ticks forever, AUTOFIX_INTERVAL_MS apart (for screen/tmux)
linear-autofix triage <CITRO-123> triage one ticket now
linear-autofix implement <ID> implement a ticket that was triaged as fix
linear-autofix review <ID> run one review-loop check for a ticket's PR
@@ -77,6 +78,8 @@ async function main([command, arg]) {
switch (command) {
case "tick":
return tick();
+ case "daemon":
+ return daemon();
case "triage": {
const { issue, record } = await one(arg);
return triage(issue, record);
diff --git a/tools/linear-autofix/src/config.mjs b/tools/linear-autofix/src/config.mjs
index 5f8ddf3..46ada65 100644
--- a/tools/linear-autofix/src/config.mjs
+++ b/tools/linear-autofix/src/config.mjs
@@ -50,6 +50,8 @@ export const config = Object.freeze({
// without them and pushes with the pre-push hook excluded; the PR body says so.
e2eKnownFailures: list("AUTOFIX_E2E_KNOWN_FAILURES", []),
+ // `daemon` mode: pause between ticks.
+ intervalMs: num("AUTOFIX_INTERVAL_MS", 30 * 60_000),
maxImplementing: num("AUTOFIX_MAX_IMPLEMENTING", 2),
maxReviewRounds: num("AUTOFIX_MAX_REVIEW_ROUNDS", 5),
diff --git a/tools/linear-autofix/src/implement.mjs b/tools/linear-autofix/src/implement.mjs
index aba9669..48514be 100644
--- a/tools/linear-autofix/src/implement.mjs
+++ b/tools/linear-autofix/src/implement.mjs
@@ -168,6 +168,15 @@ export async function publish(record) {
return escalate(record, `分支上没有领先 ${base} 的提交,无法 publish。`);
}
+ const changed = await git.changedFiles(dir, base);
+ const expected = record.triage?.affected_paths ?? [];
+ if (!sharesScope(changed, expected)) {
+ return escalate(
+ record,
+ `分支上的改动(${changed.slice(0, 8).join(", ")}${changed.length > 8 ? ", …" : ""})与分诊预期的路径(${expected.join(", ")})完全不相交,怀疑分支指向了错误的提交,已停止 publish。worktree: \`${dir}\``,
+ );
+ }
+
let gate;
try {
gate = await gatedPush(dir, record.branch);
@@ -226,6 +235,19 @@ export async function publish(record) {
transition(record, "in_review", prInfo.url);
}
+/**
+ * Whether the branch's changed files land in the same top-level area as the
+ * triage's expected paths (first two path segments). Guards against pushing a
+ * branch that was moved onto unrelated commits. Empty expectations pass.
+ */
+export function sharesScope(changedFiles, expectedPaths) {
+ if (!expectedPaths.length || !changedFiles.length)
+ return changedFiles.length > 0;
+ const area = (p) => p.split("/").slice(0, 2).join("/");
+ const expected = new Set(expectedPaths.map(area));
+ return changedFiles.some((f) => expected.has(area(f)));
+}
+
/**
* PR title/body for a ticket: saved on the record, or read from the implement
* run's pr.json (records written before the field existed). Pure apart from
diff --git a/tools/linear-autofix/src/review.mjs b/tools/linear-autofix/src/review.mjs
index edf3e76..f44cc32 100644
--- a/tools/linear-autofix/src/review.mjs
+++ b/tools/linear-autofix/src/review.mjs
@@ -22,7 +22,11 @@ const CODEX_WAIT_MS = 2 * 60 * 60_000;
/**
* Decide what the review loop should do for the PR's current head. Pure.
- * Returns { action: 'wait' | 'done' | 'draft_ready' | 'fix', problems }.
+ * Returns { action: 'wait' | 'done' | 'draft_ready' | 'fix' | 'escalate', problems, why }.
+ *
+ * `done` requires CI to have reported success. A head with no checks at all
+ * (`none`) is treated as not reported yet; after the wait window it escalates
+ * instead of finishing, since this repository runs CI on every PR.
*/
export function assessHead({
pr,
@@ -36,13 +40,22 @@ export function assessHead({
if (ci === "failure") problems.push("ci");
if (codex && !isCleanReview(codex.body)) problems.push("codex");
if (threads.length) problems.push("threads");
+ if (problems.length) return { action: "fix", problems };
- if (ci === "pending" && !problems.length)
+ const waited = headSeenAt ? now - new Date(headSeenAt).getTime() : 0;
+ if (ci === "pending")
return { action: "wait", problems, why: "CI still running" };
- if (problems.length) return { action: "fix", problems };
+ if (ci !== "success") {
+ if (waited < CODEX_WAIT_MS)
+ return { action: "wait", problems, why: "no CI result reported yet" };
+ return {
+ action: "escalate",
+ problems,
+ why: "no CI result was reported for this head within the wait window",
+ };
+ }
if (pr.isDraft) return { action: "draft_ready", problems };
if (!codex) {
- const waited = headSeenAt ? now - new Date(headSeenAt).getTime() : 0;
if (waited < CODEX_WAIT_MS)
return { action: "wait", problems, why: "waiting for the Codex review" };
return {
@@ -92,6 +105,12 @@ export async function review(issue, record) {
);
if (verdict.action === "wait") return;
+ if (verdict.action === "escalate") {
+ return escalate(
+ record,
+ `review 循环无法继续:${verdict.why}。PR: ${pr.url}`,
+ );
+ }
if (verdict.action === "draft_ready") {
if (!record.review.draftNotified) {
record.review.draftNotified = true;
diff --git a/tools/linear-autofix/src/review.test.mjs b/tools/linear-autofix/src/review.test.mjs
index 4225f92..0dfdad4 100644
--- a/tools/linear-autofix/src/review.test.mjs
+++ b/tools/linear-autofix/src/review.test.mjs
@@ -10,49 +10,66 @@ const dirty = {
};
const ready = { isDraft: false };
const now = Date.parse("2026-11-02T12:00:00Z");
+const recent = "2026-11-02T11:30:00Z";
+const old = "2026-11-02T09:00:00Z";
-test("waits while CI runs or while the Codex review is pending", () => {
+test("waits while CI runs, has not reported, or while the Codex review is pending", () => {
assert.equal(
assessHead({ pr: ready, codex: null, ci: "pending", threads: [], now })
.action,
"wait",
);
+ assert.equal(
+ assessHead({
+ pr: ready,
+ codex: clean,
+ ci: "none",
+ threads: [],
+ headSeenAt: recent,
+ now,
+ }).action,
+ "wait",
+ );
assert.equal(
assessHead({
pr: ready,
codex: null,
ci: "success",
threads: [],
- headSeenAt: "2026-11-02T11:30:00Z",
+ headSeenAt: recent,
now,
}).action,
"wait",
);
});
-test("finishes when Codex is clean, CI green and no threads are open", () => {
+test("finishes only when CI reported success", () => {
assert.equal(
assessHead({ pr: ready, codex: clean, ci: "success", threads: [], now })
.action,
"done",
);
assert.equal(
- assessHead({ pr: ready, codex: clean, ci: "none", threads: [], now })
- .action,
+ assessHead({
+ pr: ready,
+ codex: null,
+ ci: "success",
+ threads: [],
+ headSeenAt: old,
+ now,
+ }).action,
"done",
);
-});
-
-test("finishes without Codex once the wait window has passed", () => {
- const r = assessHead({
+ const noCi = assessHead({
pr: ready,
- codex: null,
- ci: "success",
+ codex: clean,
+ ci: "none",
threads: [],
- headSeenAt: "2026-11-02T09:00:00Z",
+ headSeenAt: old,
now,
});
- assert.equal(r.action, "done");
+ assert.equal(noCi.action, "escalate");
+ assert.match(noCi.why, /no CI result/);
});
test("fixes on CI failure, Codex findings or open human threads, even while CI is pending", () => {
@@ -104,4 +121,15 @@ test("a draft PR with nothing to fix is reported as ready for manual verificatio
}).action,
"fix",
);
+ assert.equal(
+ assessHead({
+ pr: { isDraft: true },
+ codex: null,
+ ci: "none",
+ threads: [],
+ headSeenAt: recent,
+ now,
+ }).action,
+ "wait",
+ );
});
diff --git a/tools/linear-autofix/src/tick.mjs b/tools/linear-autofix/src/tick.mjs
index b68006c..8a06f37 100644
--- a/tools/linear-autofix/src/tick.mjs
+++ b/tools/linear-autofix/src/tick.mjs
@@ -83,6 +83,7 @@ export async function tick() {
if (config.onlyIssue)
issues = issues.filter((i) => i.identifier === config.onlyIssue);
issues.sort(byPriority);
+ issueCount = issues.length;
console.log(
`${issues.length} assigned issue(s) in ${config.linearStates.join("/")}`,
);
@@ -154,3 +155,35 @@ export async function tick() {
releaseLock();
}
}
+
+/**
+ * Run ticks forever with `config.intervalMs` between them. For hosts without
+ * launchd: `screen -dmS linear-autofix … node src/cli.mjs daemon`. SIGINT and
+ * SIGTERM finish the current tick, then exit. The STOP file skips ticks but
+ * keeps the loop alive, so `resume` takes effect without a restart.
+ */
+export async function daemon() {
+ let stopping = false;
+ const onSignal = (signal) => {
+ console.log(`${signal} received; finishing the current tick, then exiting`);
+ stopping = true;
+ };
+ process.on("SIGINT", onSignal);
+ process.on("SIGTERM", onSignal);
+ console.log(
+ `daemon: tick every ${Math.round(config.intervalMs / 60_000)} min, state in ${config.stateDir}`,
+ );
+ while (!stopping) {
+ const started = Date.now();
+ try {
+ await tick();
+ } catch (err) {
+ console.error(`tick failed: ${err?.stack ?? err}`);
+ }
+ const wait = Math.max(60_000, config.intervalMs - (Date.now() - started));
+ console.log(`next tick at ${new Date(Date.now() + wait).toISOString()}`);
+ for (let waited = 0; waited < wait && !stopping; waited += 1000) {
+ await new Promise((r) => setTimeout(r, Math.min(1000, wait - waited)));
+ }
+ }
+}
diff --git a/tools/linear-autofix/src/triage.test.mjs b/tools/linear-autofix/src/triage.test.mjs
index 6862e84..988389a 100644
--- a/tools/linear-autofix/src/triage.test.mjs
+++ b/tools/linear-autofix/src/triage.test.mjs
@@ -1,7 +1,12 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { renderTriageComment, validateTriage } from "./triage.mjs";
-import { composePrBody, PR_FOOTER, prMetadata } from "./implement.mjs";
+import {
+ composePrBody,
+ PR_FOOTER,
+ prMetadata,
+ sharesScope,
+} from "./implement.mjs";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -134,3 +139,22 @@ test("prMetadata prefers the saved PR fields and falls back to the run's pr.json
);
assert.equal(prMetadata({}), null);
});
+
+test("sharesScope compares the first two path segments and refuses empty branches", () => {
+ assert.equal(
+ sharesScope(
+ ["package/ego-browser/src/a.ts"],
+ ["package/ego-browser/src/b.ts"],
+ ),
+ true,
+ );
+ assert.equal(
+ sharesScope(
+ ["tools/linear-autofix/src/cli.mjs"],
+ ["package/ego-browser/src/b.ts"],
+ ),
+ false,
+ );
+ assert.equal(sharesScope(["docs/x.md"], []), true);
+ assert.equal(sharesScope([], ["package/ego-browser/src/b.ts"]), false);
+});