@saltpub/agent-autoloop 0.1.3 → 0.1.5
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/CHANGELOG.md +19 -0
- package/README.en.md +9 -9
- package/README.md +9 -9
- package/SECURITY.md +1 -1
- package/dist/cli/dashboard/favicon.svg +8 -0
- package/dist/cli/dashboard/index.html +1 -0
- package/dist/cli/dashboard.js +1 -0
- package/dist/cli/main.js +9 -13
- package/dist/cli/runs.js +17 -3
- package/dist/cli/status.js +19 -8
- package/dist/host/runtime.js +13 -3
- package/dist/host/server.js +7 -1
- package/dist/host/service.js +8 -2
- package/dist/host/settle.js +10 -0
- package/dist/host/state.js +36 -20
- package/dist/kernel/decide.js +82 -4
- package/dist/kernel/due.js +2 -0
- package/dist/kernel/evolve.js +3 -0
- package/dist/kernel/http.js +11 -0
- package/dist/kernel/query.js +9 -3
- package/dist/plugins/claude/agent.js +14 -1
- package/dist/plugins/codex/agent.js +14 -1
- package/dist/plugins/delivery/delivery.js +3 -1
- package/dist/plugins/delivery/review-ledger.js +37 -22
- package/dist/plugins/delivery/review-policy.js +2 -12
- package/dist/plugins/delivery/review.js +124 -51
- package/dist/plugins/xiaojiu/channel.js +59 -9
- package/dist/protocol/client.js +6 -0
- package/dist/protocol/rules.js +1 -1
- package/dist/protocol/schema.json +6 -1
- package/dist/protocol/validate.js +19 -2
- package/dist/worker/agents.js +51 -18
- package/dist/worker/worker.js +6 -3
- package/docs/plugins.md +4 -2
- package/docs/protocol.md +12 -7
- package/docs/state-machines.md +2 -2
- package/examples/recipes/feishu-messages/README.md +60 -0
- package/examples/recipes/feishu-messages/SKILL.md +16 -0
- package/examples/recipes/feishu-messages/rules.json +21 -0
- package/examples/recipes/feishu-messages/watch.ts +157 -0
- package/examples/recipes/github-issues/rules.json +6 -1
- package/examples/recipes/github-issues/watch.ts +39 -0
- package/examples/recipes/ops-alerts/README.md +2 -2
- package/examples/recipes/ops-alerts/restart.ts +3 -0
- package/examples/recipes/ops-alerts/rules.json +1 -1
- package/examples/watch-github-issues.sh +3 -22
- package/package.json +1 -1
- package/plugins/claude/agent.ts +15 -1
- package/plugins/codex/agent.ts +14 -1
- package/plugins/delivery/delivery.ts +5 -1
- package/plugins/delivery/review-ledger.ts +46 -24
- package/plugins/delivery/review-policy.ts +2 -14
- package/plugins/delivery/review.ts +158 -56
- package/plugins/xiaojiu/channel.ts +59 -11
- package/skills/agent-autoloop/SKILL.md +23 -9
- package/examples/recipes/ops-alerts/restart.sh +0 -5
package/dist/kernel/decide.js
CHANGED
|
@@ -5,13 +5,14 @@ import { latestBefore, nextRun, parseCron } from "./cron.js";
|
|
|
5
5
|
import { deadlines, MINUTE, offlineCard, OFFLINE_AFTER, RUN_LEASE, SEEN_EVERY, WORKER_FRESH } from "./due.js";
|
|
6
6
|
import { AuthError, Conflict, InputError, NotFound } from "./errors.js";
|
|
7
7
|
import { iso, rows } from "./evolve.js";
|
|
8
|
-
import { agentsOf, askedAbout, cardView, configuration, eventsOf, jobOf, openInScope, pauseOf, routingOf, sourceOf, waitingOn, workerOf, workers, } from "./query.js";
|
|
8
|
+
import { agentsOf, askedAbout, cardView, configuration, eventsOf, jobOf, limitOf, openInScope, pauseOf, routingOf, sourceOf, waitingOn, workerOf, workers, } from "./query.js";
|
|
9
9
|
import { cronOf, eventRule, ruleOf, SCHEDULE, typeMatches } from "./rules.js";
|
|
10
10
|
import { addUsage, ANSWER_BY, ANSWER_REF, ANSWER_TEXT, DELIVERY_REF, parseEvent, parseResult, } from "../protocol/validate.js";
|
|
11
11
|
/** Minutes after the first try that a card is sent to a channel again. */
|
|
12
12
|
const RESEND_AT = [0, 1, 3, 7, 15, 25, 35, 45, 55];
|
|
13
13
|
/** The last resend and the minute it holds the card: after this a channel has given up. */
|
|
14
14
|
const RESEND_WINDOW = 57 * MINUTE;
|
|
15
|
+
const LIMIT_LONGEST = 8 * 24 * 60 * MINUTE;
|
|
15
16
|
/** How long a chain of events causing events can get before it stops being dispatched. */
|
|
16
17
|
const MAX_HOPS = 8;
|
|
17
18
|
/** Failures in a row after which a run stops being retried: each try spends the owner's real tokens. */
|
|
@@ -79,7 +80,7 @@ function assignQueued(draft, run) {
|
|
|
79
80
|
if (!rule)
|
|
80
81
|
return;
|
|
81
82
|
const session = rule.to === run.agent ? {} : nulls("session", "resume");
|
|
82
|
-
put(draft, "runs", run.id, { agent: rule.to, rules_version: config.version, ...session });
|
|
83
|
+
put(draft, "runs", run.id, { agent: rule.to, grp: rule.group, rules_version: config.version, ...session });
|
|
83
84
|
}
|
|
84
85
|
const threadOf = (run) => ({ rule: run.rule, lane: run.lane, grp: run.grp });
|
|
85
86
|
/** The session a round about to start goes on in. With resume, one that has none takes its thread's last, unless that round was
|
|
@@ -118,6 +119,12 @@ function closeOpen(draft, matches, result) {
|
|
|
118
119
|
/** Once it's stopped, nothing should still say it will try again, or that it may still be going. */
|
|
119
120
|
const closeStopped = (draft, run, result) => closeOpen(draft, (key) => key === `run:${run}:failing` || key === `run:${run}:unknown`, result);
|
|
120
121
|
const closeOffline = (draft, worker) => closeOpen(draft, (key) => key.startsWith(`worker:${worker}:offline:`), "连上了");
|
|
122
|
+
/** An agent's usage limit is over, and the card saying when it would be has nothing more to say. */
|
|
123
|
+
function lift(draft, agent, result) {
|
|
124
|
+
const { until } = draft.view.get("limits", agent);
|
|
125
|
+
set(draft, "limits", agent, null);
|
|
126
|
+
closeOpen(draft, (key) => key === `limit:${until}`, result);
|
|
127
|
+
}
|
|
121
128
|
/** Whether a new stamp would tell nothing this one doesn't. One in the future means a clock was set back: it gets redone. */
|
|
122
129
|
function justSeen({ now }, row) {
|
|
123
130
|
const age = now - Date.parse(row?.seen_at);
|
|
@@ -258,6 +265,9 @@ function claimNext(draft, worker, claim) {
|
|
|
258
265
|
// What was queued before a pause, or an answer that came in during one, waits for the resume.
|
|
259
266
|
if (!rule || !agents.includes(rule.to) || pauseOf(draft.view, rule, draft.now))
|
|
260
267
|
return false;
|
|
268
|
+
// An agent out of quota fails each round within seconds: only one the owner asked for goes, to try it.
|
|
269
|
+
if (!run.manual && limitOf(draft.view, rule.to, draft.now))
|
|
270
|
+
return false;
|
|
261
271
|
const agentLimit = draft.rules.agents[rule.to].maxActive;
|
|
262
272
|
return (Date.parse(run.not_before) <= draft.now &&
|
|
263
273
|
!claimed.some((other) => other.grp === run.grp && other.lane === run.lane) &&
|
|
@@ -325,8 +335,13 @@ function briefError(error, run) {
|
|
|
325
335
|
function retryLater(draft, run, result) {
|
|
326
336
|
const { id } = run;
|
|
327
337
|
const attempts = run.attempts + 1;
|
|
328
|
-
const notBefore = draft.now + Math.min(5 * MINUTE * 2 ** (attempts - 1), 60 * MINUTE);
|
|
329
338
|
const error = (result.error ?? "").trim().replace(/[。.]+$/, "") || "没说原因";
|
|
339
|
+
const told = result.limitedUntil === undefined ? 0 : Date.parse(result.limitedUntil);
|
|
340
|
+
// A usage limit resets within a week. A time further off is misread, and would hold the agent that long.
|
|
341
|
+
const limitedUntil = told > draft.now && told <= draft.now + LIMIT_LONGEST ? told : 0;
|
|
342
|
+
if (limitedUntil)
|
|
343
|
+
limit(draft, run, limitedUntil, error);
|
|
344
|
+
const notBefore = Math.max(draft.now + Math.min(5 * MINUTE * 2 ** (attempts - 1), 60 * MINUTE), limitedUntil);
|
|
330
345
|
const unclaimed = nulls("claim", "claimed_at", "seen_at", "cancel_at");
|
|
331
346
|
const rejected = [];
|
|
332
347
|
for (const what of ["ask", "resolve", "later", "events"]) {
|
|
@@ -369,6 +384,33 @@ function retryLater(draft, run, result) {
|
|
|
369
384
|
assignQueued(draft, draft.view.get("runs", id));
|
|
370
385
|
return rejected;
|
|
371
386
|
}
|
|
387
|
+
/** An agent out of quota gets no work until it resets, and the owner hears once when that is. */
|
|
388
|
+
function limit(draft, run, until, error) {
|
|
389
|
+
const held = limitOf(draft.view, run.agent, draft.now);
|
|
390
|
+
if (held && Date.parse(held.until) >= until)
|
|
391
|
+
return;
|
|
392
|
+
if (draft.view.get("limits", run.agent))
|
|
393
|
+
lift(draft, run.agent, held ? "恢复时间推后了" : "恢复了");
|
|
394
|
+
put(draft, "limits", run.agent, {
|
|
395
|
+
agent: run.agent,
|
|
396
|
+
until: iso(until),
|
|
397
|
+
reason: error,
|
|
398
|
+
since: held?.since ?? draft.at,
|
|
399
|
+
});
|
|
400
|
+
const agent = configuration(draft.view, run.rules_version).agents[run.agent]?.name ?? run.agent;
|
|
401
|
+
const time = new Intl.DateTimeFormat("zh-CN", {
|
|
402
|
+
timeZone: draft.rules.timezone,
|
|
403
|
+
month: "numeric",
|
|
404
|
+
day: "numeric",
|
|
405
|
+
hour: "2-digit",
|
|
406
|
+
minute: "2-digit",
|
|
407
|
+
hourCycle: "h23",
|
|
408
|
+
}).format(until);
|
|
409
|
+
const words = `${agent} 用量到上限了(${briefError(error, run.id)}),${time} 前不再派活给它。排着的和这期间新来的活都留着,` +
|
|
410
|
+
`到时自动接着做。想提前试,手动运行一轮「${run.rule}」(agent-autoloop run),跑通就接着给它派活。`;
|
|
411
|
+
// Agents on one account are told the same moment: keyed by it, the owner hears of it once.
|
|
412
|
+
notify(draft, `limit:${iso(until)}`, `${agent} 用量到上限`, words);
|
|
413
|
+
}
|
|
372
414
|
function scheduleReview(draft, run, later) {
|
|
373
415
|
const { rule, lane } = run;
|
|
374
416
|
const [existing] = rows(draft.view, "timers", { rule, lane, key: later.key, event_id: null });
|
|
@@ -470,8 +512,19 @@ function finish(draft, id, token, given) {
|
|
|
470
512
|
}
|
|
471
513
|
else if (result.outcome === "failed")
|
|
472
514
|
rejected = retryLater(draft, run, result);
|
|
473
|
-
else
|
|
515
|
+
else {
|
|
474
516
|
rejected = applyReceipt(draft, run, result);
|
|
517
|
+
// Only the owner's round is handed out while an agent is held: getting through says it works again.
|
|
518
|
+
const held = limitOf(draft.view, run.agent, draft.now);
|
|
519
|
+
if (held && run.claimed_at >= held.since) {
|
|
520
|
+
// The rounds that hit the limit were put off to the reset: they go now with the rest.
|
|
521
|
+
for (const waiting of rows(draft.view, "runs", { state: "queued", agent: run.agent })) {
|
|
522
|
+
if (waiting.not_before === held.until)
|
|
523
|
+
put(draft, "runs", waiting.id, { not_before: draft.at });
|
|
524
|
+
}
|
|
525
|
+
lift(draft, run.agent, "提前恢复了");
|
|
526
|
+
}
|
|
527
|
+
}
|
|
475
528
|
put(draft, "runs", id, { last_answer: JSON.stringify(rejected) });
|
|
476
529
|
return { rejected };
|
|
477
530
|
}
|
|
@@ -553,6 +606,10 @@ function tick(draft) {
|
|
|
553
606
|
set(draft, "pauses", pause.name, null);
|
|
554
607
|
reopen(draft, pause.name, Date.parse(pause.until));
|
|
555
608
|
}
|
|
609
|
+
for (const limit of rows(draft.view, "limits")) {
|
|
610
|
+
if (Date.parse(limit.until) <= now)
|
|
611
|
+
lift(draft, limit.agent, "恢复了");
|
|
612
|
+
}
|
|
556
613
|
for (const rule of rules) {
|
|
557
614
|
if (eventRule(rule))
|
|
558
615
|
continue;
|
|
@@ -583,6 +640,10 @@ function tick(draft) {
|
|
|
583
640
|
const rule = ruleNamed(draft, timer.rule);
|
|
584
641
|
const event = { id: `timer:${timer.id}`, type: "timer.due", subject: timer.lane, hops: timer.hops ?? 0 };
|
|
585
642
|
const eventId = recordEvent(draft, timer.source, event, rule ? [rule.name] : []);
|
|
643
|
+
// The row these were written on is removed once the round this wakes ends, and an answer can wake a round after that:
|
|
644
|
+
// they travel with the event itself, which every later reader already has (#338).
|
|
645
|
+
if (timer.events)
|
|
646
|
+
put(draft, "events", eventId, { events: timer.events });
|
|
586
647
|
if (rule) {
|
|
587
648
|
put(draft, "timers", timer.id, { event_id: eventId });
|
|
588
649
|
enqueue(draft, rule, eventId, timer.lane, timer.agent === rule.to ? timer.session : null);
|
|
@@ -701,12 +762,29 @@ const CONTROL = {
|
|
|
701
762
|
const HANDLERS = {
|
|
702
763
|
rules(draft, rules) {
|
|
703
764
|
put(draft, "config", "rules", { value: rules, version: draft.seq });
|
|
765
|
+
// A schedule's next moment was worked out on the old zone's clock. Forgetting the cron it was counted for has the next
|
|
766
|
+
// tick count again from then, as for a changed cron; this input's own clock isn't the tick's.
|
|
767
|
+
if (draft.rules && rules.timezone !== draft.rules.timezone) {
|
|
768
|
+
for (const schedule of rows(draft.view, "schedules"))
|
|
769
|
+
put(draft, "schedules", schedule.rule, { cron: null });
|
|
770
|
+
}
|
|
704
771
|
for (const run of rows(draft.view, "runs", { state: "queued" })) {
|
|
705
772
|
if (ruleOf(rules, run.rule))
|
|
706
773
|
assignQueued(draft, run);
|
|
707
774
|
else
|
|
708
775
|
stopRun(draft, run, "rule-gone", ...ruleGone(run.rule));
|
|
709
776
|
}
|
|
777
|
+
// Groups are counted, and open cards found, by the group on each row: what's going and what's left open move with
|
|
778
|
+
// their rule, or the old group and the new each let a run through.
|
|
779
|
+
const moved = (table, row) => {
|
|
780
|
+
const group = ruleOf(rules, row.rule)?.group;
|
|
781
|
+
if (group !== undefined && group !== row.grp)
|
|
782
|
+
put(draft, table, row.id, { grp: group });
|
|
783
|
+
};
|
|
784
|
+
for (const run of rows(draft.view, "runs", { state: ["claimed", "unknown"] }))
|
|
785
|
+
moved("runs", run);
|
|
786
|
+
for (const card of rows(draft.view, "cards", { state: "open" }))
|
|
787
|
+
moved("cards", card);
|
|
710
788
|
closeOpen(draft, (key) => key.startsWith("rules:"), "改好了");
|
|
711
789
|
},
|
|
712
790
|
rulesError: (draft, problem) => notify(draft, `rules:${digest(problem)}`, "rules.json 写错了,还在用旧规则", problem),
|
package/dist/kernel/due.js
CHANGED
|
@@ -40,6 +40,8 @@ export function deadlines(view, now) {
|
|
|
40
40
|
if (pause.until !== null)
|
|
41
41
|
add("pause", pause.name, Date.parse(pause.until));
|
|
42
42
|
}
|
|
43
|
+
for (const limit of rows(view, "limits"))
|
|
44
|
+
add("limit", limit.agent, Date.parse(limit.until));
|
|
43
45
|
for (const timer of rows(view, "timers", { event_id: null }))
|
|
44
46
|
add("later", String(timer.id), Date.parse(timer.at));
|
|
45
47
|
for (const run of rows(view, "runs", { state: "queued" })) {
|
package/dist/kernel/evolve.js
CHANGED
|
@@ -13,6 +13,7 @@ export const TABLES = [
|
|
|
13
13
|
"config",
|
|
14
14
|
"due",
|
|
15
15
|
"pauses",
|
|
16
|
+
"limits",
|
|
16
17
|
];
|
|
17
18
|
const NUMBERED = new Set(["events", "runs", "cards", "timers"]);
|
|
18
19
|
const indexes = {
|
|
@@ -190,6 +191,8 @@ export function keyOf(table, row) {
|
|
|
190
191
|
return `${row.kind}:${row.key}`;
|
|
191
192
|
if (table === "pauses")
|
|
192
193
|
return row.name;
|
|
194
|
+
if (table === "limits")
|
|
195
|
+
return row.agent;
|
|
193
196
|
return "rules";
|
|
194
197
|
}
|
|
195
198
|
export function empty(db = new DatabaseSync(":memory:")) {
|
package/dist/kernel/http.js
CHANGED
|
@@ -332,6 +332,17 @@ async function route(call) {
|
|
|
332
332
|
const ruleControl = /^\/v1\/rules\/([^/]+)\/(pause|resume|run|replay)$/.exec(path);
|
|
333
333
|
if (method === "POST" && ruleControl)
|
|
334
334
|
return control(call, ruleControl[1], ruleControl[2]);
|
|
335
|
+
if (method === "GET" && path === "/v1/runs") {
|
|
336
|
+
requireKey(call, "owner");
|
|
337
|
+
const rule = call.url.searchParams.get("rule");
|
|
338
|
+
if (!rule)
|
|
339
|
+
throw new InputError("缺规则名");
|
|
340
|
+
const before = call.url.searchParams.get("before");
|
|
341
|
+
if (before !== null && (!POSITIVE_ID.test(before) || !Number.isSafeInteger(Number(before))))
|
|
342
|
+
throw new InputError("编号不正确");
|
|
343
|
+
const page = services.ruleRuns(rule, before === null ? undefined : Number(before));
|
|
344
|
+
return json({ now: iso(call.now), timezone: call.rules.timezone, ...page });
|
|
345
|
+
}
|
|
335
346
|
if (method === "GET" && path === "/v1/state")
|
|
336
347
|
return state(call);
|
|
337
348
|
if (method === "GET" && path === "/v1/stream")
|
package/dist/kernel/query.js
CHANGED
|
@@ -11,6 +11,11 @@ export function pauseOf(view, rule, now) {
|
|
|
11
11
|
const row = view.get("pauses", rule.name);
|
|
12
12
|
return row && (row.until === null || Date.parse(row.until) > now) ? row : null;
|
|
13
13
|
}
|
|
14
|
+
/** The usage limit an agent has hit and that hasn't reset yet: until then none of its rounds is handed out. */
|
|
15
|
+
export function limitOf(view, agent, now) {
|
|
16
|
+
const row = view.get("limits", agent);
|
|
17
|
+
return row && Date.parse(row.until) > now ? row : null;
|
|
18
|
+
}
|
|
14
19
|
export const workerOf = (view, run) => configuration(view, run.rules_version).agents[run.agent]?.worker;
|
|
15
20
|
export function agentsOf(view, worker) {
|
|
16
21
|
const agents = Object.entries(configuration(view).agents);
|
|
@@ -36,10 +41,11 @@ export function askedAbout(view, runId, seen = new Set()) {
|
|
|
36
41
|
const ids = eventsOf(view, runId).flatMap((event) => {
|
|
37
42
|
const cards = rows(view, "cards", { answer_event: event.id });
|
|
38
43
|
if (cards.length === 0) {
|
|
39
|
-
// A check that came due:
|
|
40
|
-
//
|
|
44
|
+
// A check that came due: what it was set for was written on the event when it fired, and on the row before that for
|
|
45
|
+
// logs written then. The row is gone once the round it wakes ends, and an answer can wake a round later still.
|
|
41
46
|
const [fired] = rows(view, "timers", { event_id: event.id }).filter((timer) => timer.events);
|
|
42
|
-
|
|
47
|
+
const written = event.events ?? fired?.events;
|
|
48
|
+
return written ? JSON.parse(written) : [event.key];
|
|
43
49
|
}
|
|
44
50
|
return cards.flatMap((card) => (card.run_id === null ? [event.key] : askedAbout(view, card.run_id, seen)));
|
|
45
51
|
});
|
|
@@ -22,6 +22,19 @@ export function inTheApp() {
|
|
|
22
22
|
return builds.map((build) => path.join(root, version, build, "claude.app/Contents/MacOS/claude"));
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
|
+
const HOUR = 3_600_000;
|
|
26
|
+
/**
|
|
27
|
+
* When a subscription's usage limit lets it work again. At its 5-hour limit 2.1.318 ends the turn within two seconds with
|
|
28
|
+
* `api_error` "usage_limit_reached" and the result "You've hit your 5-hour limit · resets 1:50pm (Asia/Shanghai)", after a
|
|
29
|
+
* `rate_limit_event` whose `rate_limit_info.resetsAt` is that moment in Unix seconds. Without one, an hour.
|
|
30
|
+
*/
|
|
31
|
+
function limitedUntil(messages, result) {
|
|
32
|
+
if (result.api_error !== "usage_limit_reached")
|
|
33
|
+
return {};
|
|
34
|
+
const info = messages.findLast((message) => message?.type === "rate_limit_event")?.rate_limit_info;
|
|
35
|
+
const resets = Number(info?.resetsAt) * 1000;
|
|
36
|
+
return { limitedUntil: new Date(resets > 0 ? resets : Date.now() + HOUR).toISOString() };
|
|
37
|
+
}
|
|
25
38
|
export default {
|
|
26
39
|
command: (turn) => [
|
|
27
40
|
turn.program ?? findProgram("claude", inTheApp),
|
|
@@ -95,7 +108,7 @@ export default {
|
|
|
95
108
|
const text = carried === undefined ? said : `${said}\n\n${carried}`;
|
|
96
109
|
if (result.is_error || result.subtype !== "success") {
|
|
97
110
|
const error = said.split("\n")[0] || result.errors?.[0] || result.subtype || "没留下结果";
|
|
98
|
-
return { ...known, usage, sessionUsage, finished: true, error };
|
|
111
|
+
return { ...known, usage, sessionUsage, finished: true, error, ...limitedUntil(messages, result) };
|
|
99
112
|
}
|
|
100
113
|
return { text, ...known, usage, sessionUsage, finished: true };
|
|
101
114
|
},
|
|
@@ -90,8 +90,9 @@ export default {
|
|
|
90
90
|
},
|
|
91
91
|
link: (session) => `codex://threads/${session}`,
|
|
92
92
|
check(program) {
|
|
93
|
+
const exe = program ?? findProgram("codex", () => IN_THE_APP);
|
|
93
94
|
try {
|
|
94
|
-
execFileSync(...commandLine([
|
|
95
|
+
execFileSync(...commandLine([exe, "login", "status"]), {
|
|
95
96
|
encoding: "utf8",
|
|
96
97
|
timeout: 20_000,
|
|
97
98
|
stdio: ["ignore", "pipe", "pipe"],
|
|
@@ -99,6 +100,18 @@ export default {
|
|
|
99
100
|
return null;
|
|
100
101
|
}
|
|
101
102
|
catch (error) {
|
|
103
|
+
// `login status` only reads the stored login: a key from the environment (OPENAI_API_KEY, or the
|
|
104
|
+
// provider's env_key such as CRS_API_KEY) still runs, and `codex doctor` is what reports that.
|
|
105
|
+
try {
|
|
106
|
+
const { checks } = JSON.parse(execFileSync(...commandLine([exe, "doctor", "--json"]), {
|
|
107
|
+
encoding: "utf8",
|
|
108
|
+
timeout: 20_000,
|
|
109
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
110
|
+
}));
|
|
111
|
+
if (checks?.["auth.credentials"]?.status === "ok")
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
catch { }
|
|
102
115
|
return `Codex 没有登录或用不了:运行 codex login(${error.message.split("\n")[0]})`;
|
|
103
116
|
}
|
|
104
117
|
},
|
|
@@ -166,8 +166,10 @@ export async function deliver(repo, number, io, testLocalHealth = false) {
|
|
|
166
166
|
if (!pr.merged_at && !io.verifyReview) {
|
|
167
167
|
// The label stays on after findings, so another event lands here: a second review that misses them would merge the commit.
|
|
168
168
|
// The review status keeps the record, since a failed round writes its error over `delivery`.
|
|
169
|
+
// An owner's decision goes on to the review, which checks who wrote it.
|
|
169
170
|
const { statuses } = await io.api(`commits/${head}/status?per_page=100`);
|
|
170
|
-
|
|
171
|
+
const decided = statuses.some((item) => item.context === `delivery-owner/${number}` && item.state === "success");
|
|
172
|
+
if (!decided && statuses.some((item) => item.context === config.review.status && item.state === "failure"))
|
|
171
173
|
return { summary: "这个提交审查发现过问题,修复后推新提交才会重审" };
|
|
172
174
|
}
|
|
173
175
|
if (!io.verifyReview)
|
|
@@ -2,7 +2,6 @@ import { createHash } from "node:crypto";
|
|
|
2
2
|
const SECTION = "## Codex 审查";
|
|
3
3
|
const ROUND = /^第 (\d+) 轮(([^)\n]+),([a-z]+(?: \+ [a-z]+)*),审到 ([0-9a-f]{7,40})):/gm;
|
|
4
4
|
const ITEM = /^(\d+)\. /;
|
|
5
|
-
export const REVIEW_ROUNDS = 3;
|
|
6
5
|
function section(body) {
|
|
7
6
|
return /^## Codex 审查\n([\s\S]*?)(?=^## |$(?![\s\S]))/m.exec(body);
|
|
8
7
|
}
|
|
@@ -11,19 +10,25 @@ export function ledger(description) {
|
|
|
11
10
|
const match = section(body);
|
|
12
11
|
const record = match?.[1] ?? "";
|
|
13
12
|
const matches = [...record.matchAll(ROUND)];
|
|
14
|
-
const rounds = matches.map(([, number, signature, effort, head], index) =>
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
.
|
|
24
|
-
.
|
|
25
|
-
|
|
26
|
-
|
|
13
|
+
const rounds = matches.map(([, number, signature, effort, head], index) => {
|
|
14
|
+
const text = record.slice(matches[index].index, matches[index + 1]?.index).trim();
|
|
15
|
+
return {
|
|
16
|
+
number: Number(number),
|
|
17
|
+
signature,
|
|
18
|
+
effort,
|
|
19
|
+
head,
|
|
20
|
+
text,
|
|
21
|
+
// Only blocking findings are numbered; a suggestion is a bullet with nothing to handle.
|
|
22
|
+
blocking: text.split("\n").filter((line) => ITEM.test(line)).length,
|
|
23
|
+
// Bind all earlier rounds too. Removing an old finding must invalidate the latest proof.
|
|
24
|
+
digest: createHash("sha256")
|
|
25
|
+
.update(record
|
|
26
|
+
.slice(0, matches[index + 1]?.index)
|
|
27
|
+
.replace(/^ 处理:[^\n]*$/gm, " 处理:")
|
|
28
|
+
.trim())
|
|
29
|
+
.digest("hex"),
|
|
30
|
+
};
|
|
31
|
+
});
|
|
27
32
|
const waiting = new Map();
|
|
28
33
|
let current;
|
|
29
34
|
for (const line of record.split("\n")) {
|
|
@@ -57,26 +62,36 @@ export function ledger(description) {
|
|
|
57
62
|
},
|
|
58
63
|
};
|
|
59
64
|
}
|
|
65
|
+
/** Two rounds in a row left blocking findings: the second checked the first's fixes and still found some. */
|
|
66
|
+
export const stalled = (current) => current.rounds.length >= 2 && current.rounds.slice(-2).every((round) => round.blocking > 0);
|
|
67
|
+
export const OWNER = "已经审了两轮仍有挡上线的问题,不再自动审第三轮,交给负责人定";
|
|
68
|
+
const oneLine = (text) => text.replace(/\s+/g, " ").trim();
|
|
60
69
|
export function renderRound(current, signature, effort, head, findings) {
|
|
61
70
|
const header = `第 ${current.rounds.length + 1} 轮(${signature},${effort.join(" + ")},审到 ${head}):`;
|
|
62
71
|
if (!findings.length)
|
|
63
72
|
return `${header}没有发现问题。`;
|
|
64
|
-
const
|
|
73
|
+
const blocking = findings.filter((finding) => finding.level === "block");
|
|
74
|
+
const suggestions = findings.filter((finding) => finding.level === "suggest");
|
|
65
75
|
return [
|
|
66
|
-
`${header}
|
|
67
|
-
|
|
68
|
-
...(round >= REVIEW_ROUNDS
|
|
76
|
+
`${header}挡上线 ${blocking.length} 条,建议 ${suggestions.length} 条。`,
|
|
77
|
+
...(blocking.length && current.rounds.at(-1)?.blocking
|
|
69
78
|
? [
|
|
70
|
-
`\n
|
|
79
|
+
`\n${OWNER}:放行就在每条“处理:”后写上负责人的原话,由可信账号在提交上写 \`delivery-owner/<PR 编号>\` 状态后重交;不放行就照负责人说的改。`,
|
|
71
80
|
]
|
|
72
81
|
: []),
|
|
73
|
-
...
|
|
74
|
-
const body = text
|
|
82
|
+
...blocking.map(({ title, how, text }, index) => {
|
|
83
|
+
const body = `怎么发生:${how}\n${text}`
|
|
75
84
|
.replaceAll("\r\n", "\n")
|
|
76
85
|
.split("\n")
|
|
77
86
|
.map((line) => (line ? ` ${line}` : ""))
|
|
78
87
|
.join("\n");
|
|
79
|
-
return `\n${current.numbered + index + 1}. **${title
|
|
88
|
+
return `\n${current.numbered + index + 1}. **${oneLine(title)}**\n\n${body}\n\n 处理:`;
|
|
80
89
|
}),
|
|
90
|
+
...(suggestions.length
|
|
91
|
+
? [
|
|
92
|
+
"\n建议(不挡上线):\n",
|
|
93
|
+
...suggestions.map(({ title, how, text }) => `- **${oneLine(title)}**:${oneLine(how).replace(/[。.]$/, "")}。${oneLine(text)}`),
|
|
94
|
+
]
|
|
95
|
+
: []),
|
|
81
96
|
].join("\n");
|
|
82
97
|
}
|
|
@@ -65,9 +65,8 @@ const commandPlan = (command) => `command/${createHash("sha256").update(JSON.str
|
|
|
65
65
|
export function reviewCovers(round, reviewers, efforts) {
|
|
66
66
|
const ran = round.effort.split(" + ");
|
|
67
67
|
const done = round.signature.split(" + ").map((entry, index) => {
|
|
68
|
-
// Entries are "command/hash"
|
|
69
|
-
|
|
70
|
-
const plan = entry.startsWith("command/") || !entry.includes("/") ? entry : entry.slice(entry.indexOf("/") + 1);
|
|
68
|
+
// Entries are "command/hash" or "name/model[@configured depth]"; the depth that ran is recorded apart.
|
|
69
|
+
const plan = entry.startsWith("command/") ? entry : entry.slice(entry.indexOf("/") + 1);
|
|
71
70
|
return `${plan.replace(/@[a-z]+$/, "")}@${ran[index]}`;
|
|
72
71
|
});
|
|
73
72
|
return reviewers.every(({ command, model = DEFAULT_MODEL }, index) => {
|
|
@@ -80,15 +79,6 @@ export function reviewCovers(round, reviewers, efforts) {
|
|
|
80
79
|
}
|
|
81
80
|
export function reviewSignature(reviewers) {
|
|
82
81
|
const model = DEFAULT_MODEL;
|
|
83
|
-
if (reviewers.length === 1) {
|
|
84
|
-
const [reviewer] = reviewers;
|
|
85
|
-
// Match the name that older rounds used for the default plan.
|
|
86
|
-
if (reviewer.name === "codex" &&
|
|
87
|
-
(reviewer.model ?? model) === model &&
|
|
88
|
-
(reviewer.effort ?? "high") === "high" &&
|
|
89
|
-
!reviewer.command)
|
|
90
|
-
return model;
|
|
91
|
-
}
|
|
92
82
|
return reviewers
|
|
93
83
|
.map(({ name, model: selected = model, effort = "high", command }) => {
|
|
94
84
|
if (command)
|