proofdeck 0.0.0-stage → 0.1.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.
- package/LICENSE +201 -0
- package/NOTICE +23 -0
- package/README.md +152 -2
- package/README.zh-CN.md +152 -0
- package/bin/proofdeck.js +191 -0
- package/board/LICENSE +201 -0
- package/board/README.md +473 -0
- package/board/README.zh-CN.md +424 -0
- package/board/dist/_chk.db-shm +0 -0
- package/board/dist/_chk.db-wal +0 -0
- package/board/dist/adapters.d.ts +40 -0
- package/board/dist/adapters.d.ts.map +1 -0
- package/board/dist/adapters.js +124 -0
- package/board/dist/adapters.js.map +1 -0
- package/board/dist/board.d.ts +628 -0
- package/board/dist/board.d.ts.map +1 -0
- package/board/dist/board.js +2767 -0
- package/board/dist/board.js.map +1 -0
- package/board/dist/cli.d.ts +3 -0
- package/board/dist/cli.d.ts.map +1 -0
- package/board/dist/cli.js +1663 -0
- package/board/dist/cli.js.map +1 -0
- package/board/dist/config.d.ts +57 -0
- package/board/dist/config.d.ts.map +1 -0
- package/board/dist/config.js +99 -0
- package/board/dist/config.js.map +1 -0
- package/board/dist/db.d.ts +9 -0
- package/board/dist/db.d.ts.map +1 -0
- package/board/dist/db.js +506 -0
- package/board/dist/db.js.map +1 -0
- package/board/dist/git.d.ts +35 -0
- package/board/dist/git.d.ts.map +1 -0
- package/board/dist/git.js +147 -0
- package/board/dist/git.js.map +1 -0
- package/board/dist/index.d.ts +18 -0
- package/board/dist/index.d.ts.map +1 -0
- package/board/dist/index.js +13 -0
- package/board/dist/index.js.map +1 -0
- package/board/dist/mcp-config.d.ts +26 -0
- package/board/dist/mcp-config.d.ts.map +1 -0
- package/board/dist/mcp-config.js +88 -0
- package/board/dist/mcp-config.js.map +1 -0
- package/board/dist/mcp.d.ts +3 -0
- package/board/dist/mcp.d.ts.map +1 -0
- package/board/dist/mcp.js +949 -0
- package/board/dist/mcp.js.map +1 -0
- package/board/dist/quickstart.d.ts +75 -0
- package/board/dist/quickstart.d.ts.map +1 -0
- package/board/dist/quickstart.js +156 -0
- package/board/dist/quickstart.js.map +1 -0
- package/board/dist/serve.d.ts +10 -0
- package/board/dist/serve.d.ts.map +1 -0
- package/board/dist/serve.js +445 -0
- package/board/dist/serve.js.map +1 -0
- package/board/dist/signing.d.ts +18 -0
- package/board/dist/signing.d.ts.map +1 -0
- package/board/dist/signing.js +26 -0
- package/board/dist/signing.js.map +1 -0
- package/board/dist/sync.d.ts +49 -0
- package/board/dist/sync.d.ts.map +1 -0
- package/board/dist/sync.js +158 -0
- package/board/dist/sync.js.map +1 -0
- package/board/dist/types.d.ts +359 -0
- package/board/dist/types.d.ts.map +1 -0
- package/board/dist/types.js +20 -0
- package/board/dist/types.js.map +1 -0
- package/board/docs/adr/0001-shared-worktree-collision.md +102 -0
- package/board/docs/adr/0001-shared-worktree-collision.zh-CN.md +90 -0
- package/board/docs/attribution.md +123 -0
- package/board/docs/attribution.zh-CN.md +104 -0
- package/board/docs/development-workflow.md +81 -0
- package/board/docs/development-workflow.zh-CN.md +75 -0
- package/board/docs/diagrams/banner.png +0 -0
- package/board/docs/diagrams/banner.svg +1 -0
- package/board/docs/diagrams/before-after.png +0 -0
- package/board/docs/diagrams/before-after.svg +1 -0
- package/board/docs/diagrams/governance-chain.png +0 -0
- package/board/docs/diagrams/governance-chain.svg +1 -0
- package/board/docs/diagrams/value-flow.png +0 -0
- package/board/docs/diagrams/value-flow.svg +1 -0
- package/board/docs/entry-point.md +81 -0
- package/board/docs/entry-point.zh-CN.md +65 -0
- package/board/docs/kanban.png +0 -0
- package/board/docs/octoryn-mark.png +0 -0
- package/board/docs/registry-metadata.md +34 -0
- package/board/docs/registry-metadata.zh-CN.md +30 -0
- package/board/package.json +87 -0
- package/config.example.json +72 -0
- package/daemon.js +1148 -0
- package/docs/assets/demo.gif +0 -0
- package/docs/assets/logo-512.png +0 -0
- package/docs/assets/logo.svg +31 -0
- package/docs/assets/social-preview.png +0 -0
- package/docs/demo-run.mjs +61 -0
- package/docs/render-demo.py +94 -0
- package/docs/render-logo.py +101 -0
- package/docs/task-book.md +58 -0
- package/e2e/verify-acceptance.mjs +150 -0
- package/e2e/verify-antifraud.mjs +128 -0
- package/e2e/verify-caps.mjs +46 -0
- package/e2e/verify-checkpoint.mjs +208 -0
- package/e2e/verify-firstclass.mjs +110 -0
- package/e2e/verify-judge.mjs +52 -0
- package/e2e/verify-lease.mjs +95 -0
- package/e2e/verify-replay.mjs +92 -0
- package/e2e/verify-resilience.mjs +323 -0
- package/e2e/verify-review.mjs +86 -0
- package/e2e/verify-scheduling.mjs +110 -0
- package/e2e/verify-schema-evolution.mjs +107 -0
- package/e2e/verify-sla-digest.mjs +105 -0
- package/guard.js +97 -0
- package/lib/adapters/codex.js +206 -0
- package/lib/billing-convert.js +57 -0
- package/lib/board.js +46 -0
- package/lib/caps.js +35 -0
- package/lib/checkpoint.js +130 -0
- package/lib/gating.js +218 -0
- package/lib/guard-core.mjs +108 -0
- package/lib/http.js +161 -0
- package/lib/judge.js +89 -0
- package/lib/lease.js +93 -0
- package/lib/notify.js +56 -0
- package/lib/platform.js +120 -0
- package/lib/replay.js +68 -0
- package/lib/review.js +40 -0
- package/lib/runner.js +152 -0
- package/lib/schedule.js +59 -0
- package/lib/sla.js +42 -0
- package/lib/spans.js +36 -0
- package/lib/starve.js +43 -0
- package/lib/store.js +109 -0
- package/lib/supply.js +101 -0
- package/lib/usage.js +103 -0
- package/lib/verify.js +223 -0
- package/lib/watchdog-core.mjs +68 -0
- package/lib/watchdog-worker.mjs +75 -0
- package/package.json +55 -4
- package/scripts/run-daemon.sh +18 -0
- package/scripts/run-e2e.mjs +39 -0
package/daemon.js
ADDED
|
@@ -0,0 +1,1148 @@
|
|
|
1
|
+
// agent-bridge daemon — real-time blackboard task executor.
|
|
2
|
+
// Doorbell (OS file event) -> timeline cursor catch-up -> gating -> headless
|
|
3
|
+
// agent spawn -> automatic claim/progress/done/note write-back.
|
|
4
|
+
// Board I/O exclusively via the shared octopus-blackboard Board library.
|
|
5
|
+
import { readFileSync, writeFileSync, existsSync, mkdirSync, appendFileSync } from "node:fs";
|
|
6
|
+
import { join, dirname, resolve, isAbsolute } from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { spawn } from "node:child_process";
|
|
9
|
+
import { Worker } from "node:worker_threads";
|
|
10
|
+
import { killTree, getProcessInfo } from "./lib/platform.js";
|
|
11
|
+
import * as board from "./lib/board.js";
|
|
12
|
+
import { Store } from "./lib/store.js";
|
|
13
|
+
import { createNotifier } from "./lib/notify.js";
|
|
14
|
+
import { createGating, findDispatchFiles, nonAsciiProductNames, faceReachable } from "./lib/gating.js";
|
|
15
|
+
import { runTask, getAdapter } from "./lib/runner.js";
|
|
16
|
+
import { scanRunDir, supplyReason, hold, left } from "./lib/supply.js";
|
|
17
|
+
import { convertBillingTask, billingOwnerNotice } from "./lib/billing-convert.js";
|
|
18
|
+
import { maxSegments, capture, renderPriming, shouldStopSegmenting, segmentOverrun, SEGMENT_CAP } from "./lib/checkpoint.js";
|
|
19
|
+
import { STARVE_LIMIT } from "./lib/starve.js";
|
|
20
|
+
import { normalizeUsage, missingUsage, appendUsage } from "./lib/usage.js";
|
|
21
|
+
import { leaseSweep } from "./lib/lease.js";
|
|
22
|
+
import { bestLaneIndex, retryDecision } from "./lib/schedule.js";
|
|
23
|
+
import { parseBridgeBlock, evaluateAcceptance, formatVerifyNote, applyVerifyPass, applyVerifyFail } from "./lib/verify.js";
|
|
24
|
+
import { runJudge } from "./lib/judge.js";
|
|
25
|
+
import { parseNeeds, faceHasCaps, pickCapableFace } from "./lib/caps.js";
|
|
26
|
+
import { initSpans, emitSpan } from "./lib/spans.js";
|
|
27
|
+
import { queueReview } from "./lib/review.js";
|
|
28
|
+
import { overdueDecision, buildDigest } from "./lib/sla.js";
|
|
29
|
+
import { createControlServer } from "./lib/http.js";
|
|
30
|
+
|
|
31
|
+
// BASE = runtime state root (state.json, logs/, runs/, control.token).
|
|
32
|
+
// Defaults to this file's directory (repo checkout); PROOFDECK_BASE lets an
|
|
33
|
+
// npm-installed copy run against a user project dir instead of node_modules
|
|
34
|
+
// (bin/proofdeck.js daemon sets it). Config lookup still honors argv[2].
|
|
35
|
+
const BASE = process.env.PROOFDECK_BASE ? resolve(process.env.PROOFDECK_BASE) : dirname(fileURLToPath(import.meta.url));
|
|
36
|
+
const USAGEF = join(BASE, "usage.jsonl"); // 用量台账:随 BASE 走,冒烟的第二实例天然写到自己目录
|
|
37
|
+
const cfgPath = process.argv[2] ? resolveCfg(process.argv[2]) : join(BASE, "daemon.config.json");
|
|
38
|
+
function resolveCfg(p) { return p.includes(":") || p.startsWith("/") ? p : join(BASE, p); }
|
|
39
|
+
const cfg = JSON.parse(readFileSync(cfgPath, "utf8"));
|
|
40
|
+
|
|
41
|
+
// workdir absolutization: a relative workdir gets double-resolved through the
|
|
42
|
+
// spawn chain (daemon cwd → agent cwd) and dies with os error 3 before the
|
|
43
|
+
// face can say anything (dogfood finding). Normalize against BASE at load.
|
|
44
|
+
for (const face of Object.values(cfg.agents ?? {})) {
|
|
45
|
+
if (face.workdir && !isAbsolute(face.workdir)) face.workdir = resolve(BASE, face.workdir);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const ID = cfg.board.identity;
|
|
49
|
+
const DAY = () => { const d = new Date(); return `${d.getFullYear()}${String(d.getMonth() + 1).padStart(2, "0")}${String(d.getDate()).padStart(2, "0")}`; };
|
|
50
|
+
const LOGF = join(BASE, "logs", `daemon-${DAY()}.log`);
|
|
51
|
+
mkdirSync(dirname(LOGF), { recursive: true });
|
|
52
|
+
initSpans({ dir: join(BASE, "logs"), enabled: cfg.runtime?.spans?.enabled !== false });
|
|
53
|
+
|
|
54
|
+
let t0 = process.hrtime.bigint();
|
|
55
|
+
function log(msg) {
|
|
56
|
+
const line = `[${new Date().toISOString()}] ${msg}`;
|
|
57
|
+
appendFileSync(LOGF, line + "\n");
|
|
58
|
+
console.log(line);
|
|
59
|
+
}
|
|
60
|
+
const latencyMark = () => Number(process.hrtime.bigint() - t0) / 1e6;
|
|
61
|
+
|
|
62
|
+
const store = new Store(join(BASE, "state.json"));
|
|
63
|
+
const gating = createGating(cfg.gating);
|
|
64
|
+
const STOP = join(BASE, "STOP");
|
|
65
|
+
const enabledAgents = Object.entries(cfg.agents)
|
|
66
|
+
.filter(([, a]) => a.enabled).map(([name]) => name);
|
|
67
|
+
|
|
68
|
+
// ---- 供应方熔断(#25)-------------------------------------------------------
|
|
69
|
+
// 判据是三条同时成立,缺一条都不冻:
|
|
70
|
+
// ① 这次 run 判失败;② 它一步都没走(items===0);③ run 目录里扫到供应方文案。
|
|
71
|
+
// ②不可省:拿 19 个历史 run 全量回归时,一个「成功交付 1991B 产物」的 codex run
|
|
72
|
+
// 因为 agent 自己在工具调用里引用了 "invalid api key" 字样而被 ①③ 命中——
|
|
73
|
+
// 只看文案会把活着的面冻掉,只看进度会把"0 步但没留下文案"的失败漏掉。
|
|
74
|
+
// 冻结只作用于「该面的派发」,队列不清空、卡不判死:供应方回来探针自己会放行。
|
|
75
|
+
const SUPPLY = cfg.runtime?.supply ?? {};
|
|
76
|
+
const HOLD_BASE = SUPPLY.baseMs ?? 300000; // 第 1 次冻 5 分钟
|
|
77
|
+
const HOLD_MAX = SUPPLY.maxMs ?? 7200000; // 封顶 2 小时
|
|
78
|
+
const holdMsFor = (streak) => Math.min(HOLD_BASE * 2 ** (streak - 1), HOLD_MAX);
|
|
79
|
+
// 供应方时长写法统一走 lib/supply.js 的 hold/left,别让板上有「暂停 0 分钟」这种读着像没冻的字符串。
|
|
80
|
+
|
|
81
|
+
// ---- 认领租约(claim lease)--------------------------------------------------
|
|
82
|
+
// 只自动释放【自己身份】的过期 claim,他人的只报告(最小权力,见 lib/lease.js 头注)。
|
|
83
|
+
// 豁免:本守护 runs 里正在跑的键(在跑=活着的持有)。
|
|
84
|
+
const LEASE = cfg.runtime?.lease ?? {};
|
|
85
|
+
const LEASE_TTL_MS = (LEASE.ttlHours ?? 24) * 3600000;
|
|
86
|
+
const LEASE_SESSION_MS = LEASE.sessionMs ?? 600000; // 持有者心跳窗口:10 分钟内有 heartbeat 算活着
|
|
87
|
+
const RETRY = cfg.runtime?.retry ?? {}; // 自动重派(调度层 C.3)
|
|
88
|
+
const PRIORITY = cfg.runtime?.priority ?? {}; // 优先级 aging(调度层 C.2)
|
|
89
|
+
const VERIFY = cfg.runtime?.verify ?? {}; // 验收层 V1(验证层 Part B)
|
|
90
|
+
const JUDGE = cfg.runtime?.judge ?? {}; // 验收层 V1.5 judge(2026-10-07 采纳建议①)
|
|
91
|
+
const SLA = cfg.runtime?.sla ?? {}; // 逾期标红(调度层 C.4)
|
|
92
|
+
const DIGEST = cfg.runtime?.digest ?? {}; // owner 摘要(调度层 C.6)
|
|
93
|
+
// owner 提醒开关:额度/账单类供应方事件由黑板内部消化,不再私信 owner 刷屏。
|
|
94
|
+
// cfg.runtime.notify.ownerSupply=false 时,首场供应方故障与恢复只进日志与卡级消息
|
|
95
|
+
// (创建人仍会收到),不再 ping owner。默认 true = 旧行为。
|
|
96
|
+
const NOTIFY = cfg.runtime?.notify ?? {};
|
|
97
|
+
const OWNER_SUPPLY_NOTIFY = NOTIFY.ownerSupply !== false;
|
|
98
|
+
|
|
99
|
+
// ---- 余额断供 → 转公开待认领任务 ----------------------------------------
|
|
100
|
+
// 现状(改前):billing 类失败 → 卡落 blocked → 该面熔断 → 队列保留,等熔断到期或人工 requeue。
|
|
101
|
+
// 问题是「卡僵在那」:余额耗尽不会自己恢复,等下去永远等不到,而卡面上没人知道"这是断供、可以换人做"。
|
|
102
|
+
// 转化形态:不自动改派(同一部署的面往往共用一个钱包,改派很可能撞另一个同样空的),
|
|
103
|
+
// 而是把原卡标成"断供待认领"并在板上广播一张公开可认领的任务;owner 只在该面【首次】断供时报一次。
|
|
104
|
+
const BILLING_CONVERT = cfg.runtime?.billingConvert ?? {};
|
|
105
|
+
const BILLING_CONVERT_ON = BILLING_CONVERT.enabled !== false;
|
|
106
|
+
// 已转过的 key(进程内 + 落 state):熔断会反复触发,广播只该发一次。
|
|
107
|
+
const billingConverted = store.s.billingConverted ??= {};
|
|
108
|
+
|
|
109
|
+
async function maybeLease() {
|
|
110
|
+
if (!LEASE.enabled || existsSync(STOP)) return;
|
|
111
|
+
try {
|
|
112
|
+
const busy = new Set(Object.keys(store.s.runs));
|
|
113
|
+
const r = await leaseSweep({
|
|
114
|
+
boardCfg: cfg.board, identity: ID,
|
|
115
|
+
ttlMs: LEASE_TTL_MS, sessionMs: LEASE_SESSION_MS,
|
|
116
|
+
busyKeys: busy, apply: true, log,
|
|
117
|
+
});
|
|
118
|
+
// 他人过期 claim 的报告按集合变化记日志,不按节奏刷——这块改不了,反复喊也没用。
|
|
119
|
+
const sig = r.otherStale.map((o) => o.key).sort().join(",");
|
|
120
|
+
const prevSig = store.s.lease?.otherSig ?? "";
|
|
121
|
+
if (sig !== prevSig) {
|
|
122
|
+
(store.s.lease ??= {}).otherSig = sig;
|
|
123
|
+
store.save();
|
|
124
|
+
if (sig) log(`lease report: 他人持有的过期 claim(只报告不代释):${r.otherStale.map((o) => `${o.key}(${o.holder},${(o.ageMs / 3600000).toFixed(0)}h)`).join(" ")}`);
|
|
125
|
+
}
|
|
126
|
+
if (r.released.length) {
|
|
127
|
+
log(`lease sweep: released ${r.released.join(", ")}`);
|
|
128
|
+
await board.withBoard(cfg.board, (b) => board.message(b, ID, cfg.owner,
|
|
129
|
+
`[${ID}] 租约巡检:自动释放了自己持有的过期 claim ${r.released.join(", ")}(note 已上板)。`
|
|
130
|
+
+ (r.otherStale.length ? `另有 ${r.otherStale.length} 张他人持有的过期 claim 未动(最小权力,只报告):${r.otherStale.map((o) => `${o.key}(${o.holder})`).join(" ")}。查看: node bridge-ctl.js leases` : "")));
|
|
131
|
+
}
|
|
132
|
+
} catch (e) {
|
|
133
|
+
log(`lease sweep error: ${e.message}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 自动重派到期扫描(调度层 C.3):到点把重试卡 requeue 回队。面忙就顺延一拍不丢卡;
|
|
138
|
+
// 已进人工审批的作废;面被熔断时 requeue 会成功但派发被 supply hold 挡住——排队等恢复,无害。
|
|
139
|
+
async function maybeRetry() {
|
|
140
|
+
if (existsSync(STOP)) return;
|
|
141
|
+
const now = Date.now();
|
|
142
|
+
const due = Object.entries(store.s.retries ?? {}).filter(([, r]) => Number(r.dueAt) <= now);
|
|
143
|
+
for (const [key, r] of due) {
|
|
144
|
+
if (store.s.pending?.[key]) {
|
|
145
|
+
delete store.s.retries[key];
|
|
146
|
+
store.save();
|
|
147
|
+
log(`auto-retry cancelled for ${key}: moved to human approval`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
// 台账可能比结案活得久(复核中/人工改状态/历史遗留):重派前看一眼板面。
|
|
151
|
+
// 已 done/blocked 的卡不再重派(成功或人工裁定优先于过期的重试意图);
|
|
152
|
+
// 在复核队列里的卡不重派(复核结果才是裁决,重派会跟复核打架)。
|
|
153
|
+
if (store.s.review?.[key]) {
|
|
154
|
+
delete store.s.retries[key];
|
|
155
|
+
store.save();
|
|
156
|
+
log(`auto-retry dropped for ${key}: awaiting review`);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
let settled = null;
|
|
160
|
+
try { settled = await board.withBoard(cfg.board, (b) => board.card(b, key)?.task?.status ?? null); } catch { }
|
|
161
|
+
if (settled === "done" || settled === "blocked") {
|
|
162
|
+
delete store.s.retries[key];
|
|
163
|
+
store.save();
|
|
164
|
+
log(`auto-retry dropped for ${key}: task already ${settled}`);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const res = store.requeue(key, r.agent);
|
|
168
|
+
if (res.ok) {
|
|
169
|
+
delete store.s.retries[key];
|
|
170
|
+
store.save();
|
|
171
|
+
log(`auto-requeued ${key} -> ${r.agent} (attempt ${r.n})`);
|
|
172
|
+
await board.withBoard(cfg.board, (b) =>
|
|
173
|
+
board.note(b, ID, `[${ID}→${r.agent}] ${key} 自动重派第 ${r.n} 次尝试(上次失败:${r.why})`))
|
|
174
|
+
.catch((e) => log(`retry requeue note failed for ${key}: ${e.message}`));
|
|
175
|
+
wake(`auto-retry ${key}`);
|
|
176
|
+
} else if (String(res.why ?? "").includes("正在运行")) {
|
|
177
|
+
r.dueAt = now + (RETRY.sweepMs ?? 60000) + 1000;
|
|
178
|
+
store.save();
|
|
179
|
+
} else {
|
|
180
|
+
delete store.s.retries[key];
|
|
181
|
+
store.save();
|
|
182
|
+
log(`auto-retry dropped ${key}: ${res.why}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// 逾期标红(调度层 C.4):任务书 bridge 块有 dueAt 且未终态、未提醒过 → 标 [OVERDUE] + 喊创建人。
|
|
188
|
+
// 一卡一次(v1),不强制取消——只做升级可见性。
|
|
189
|
+
async function maybeSla() {
|
|
190
|
+
if (SLA.enabled === false || existsSync(STOP)) return;
|
|
191
|
+
const now = Date.now();
|
|
192
|
+
const hits = [];
|
|
193
|
+
await board.withBoard(cfg.board, (b) => {
|
|
194
|
+
for (const t of b.listTasks()) {
|
|
195
|
+
if (t.status === "done" || t.status === "blocked") continue;
|
|
196
|
+
const bb = parseBridgeBlock(t.description);
|
|
197
|
+
const dec = overdueDecision({ dueAt: bb?.dueAt, status: t.status, now, notifiedAt: store.s.slaNotified?.[t.key] });
|
|
198
|
+
if (!dec) continue;
|
|
199
|
+
(store.s.slaNotified ??= {})[t.key] = now;
|
|
200
|
+
hits.push(t.key);
|
|
201
|
+
board.note(b, ID, `[OVERDUE] ${t.key} 已逾期 ${left(dec.overdueMs)}(状态=${t.status})—— 需要人处置:催办/改期/撤卡。`);
|
|
202
|
+
emitSpan("gen_ai.agent.sla", { task: t.key, overdueMs: dec.overdueMs, status: t.status });
|
|
203
|
+
board.message(b, ID, t.createdBy ?? cfg.owner, `[${ID}] 任务 ${t.key} 已逾期 ${left(dec.overdueMs)}(状态=${t.status}),请处置(催办/改期/撤卡)。`);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
if (hits.length) {
|
|
207
|
+
store.save();
|
|
208
|
+
log(`sla: flagged overdue ${hits.join(", ")}`);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// owner 摘要(调度层 C.6):未读/在跑/待审批/逾期/重试/近期失败的聚合,一条消息摊薄裁决成本。
|
|
213
|
+
// 首见时从当下计时(防重启轰炸);全空静默跳过且照常顺延计时。
|
|
214
|
+
async function maybeDigest() {
|
|
215
|
+
if (DIGEST.enabled === false) return;
|
|
216
|
+
const now = Date.now();
|
|
217
|
+
const d = (store.s.digest ??= { lastAt: now });
|
|
218
|
+
const everyMs = DIGEST.everyMs ?? 21600000;
|
|
219
|
+
if (now - d.lastAt < everyMs) return;
|
|
220
|
+
let digest = null;
|
|
221
|
+
await board.withBoard(cfg.board, (b) => {
|
|
222
|
+
const unreadBy = {};
|
|
223
|
+
for (const m of b.inbox(cfg.owner, false)) {
|
|
224
|
+
const from = m.from ?? m.actor ?? "?";
|
|
225
|
+
unreadBy[from] = (unreadBy[from] ?? 0) + 1;
|
|
226
|
+
}
|
|
227
|
+
const pending = Object.entries(store.s.pending ?? {}).map(([key, p]) => ({ key, reason: p.reason }));
|
|
228
|
+
const overdue = Object.keys(store.s.slaNotified ?? {});
|
|
229
|
+
const retries = Object.entries(store.s.retries ?? {}).map(([key, r]) => ({ key, n: r.n }));
|
|
230
|
+
const blockedRecent = Object.entries(store.s.finished ?? {})
|
|
231
|
+
.filter(([, f]) => Date.parse(f.at ?? "") > now - everyMs && f.result !== "done").map(([k]) => k);
|
|
232
|
+
const running = Object.keys(store.s.runs ?? {});
|
|
233
|
+
// 验收覆盖(§17 推广):本期新派卡(timeline kind=task 落在本周期内)里有多少带验收标准。
|
|
234
|
+
let coverage = null;
|
|
235
|
+
try {
|
|
236
|
+
const cutoff = new Date(now - everyMs).toISOString();
|
|
237
|
+
const rows = b.db.prepare("SELECT payload, at FROM timeline WHERE kind = 'task' AND at > ? ORDER BY seq").all(cutoff);
|
|
238
|
+
const keys = rows.map((r) => { try { return JSON.parse(r.payload ?? "{}")?.key; } catch { return null; } }).filter(Boolean);
|
|
239
|
+
const uniq = [...new Set(keys)];
|
|
240
|
+
const unverified = uniq.filter((k) => { const t = b.getTask(k); return t && !t.acceptance; });
|
|
241
|
+
coverage = { total: uniq.length, verified: uniq.length - unverified.length, unverified };
|
|
242
|
+
} catch { /* 统计失败不砸摘要 */ }
|
|
243
|
+
digest = buildDigest({ unreadBy, pending, overdue, retries, blockedRecent, running, coverage });
|
|
244
|
+
if (!digest.empty) board.message(b, ID, cfg.owner, digest.text);
|
|
245
|
+
});
|
|
246
|
+
d.lastAt = now;
|
|
247
|
+
store.save();
|
|
248
|
+
log(digest?.empty ? "digest: nothing to report" : `digest sent to ${cfg.owner}`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function tick(why) {
|
|
252
|
+
const state = store.s;
|
|
253
|
+
// 1) catch up the durable cursor (truth), doorbell is just a trigger
|
|
254
|
+
const { events, headSeq } = await board.withBoard(cfg.board, (b) => {
|
|
255
|
+
const from = state.cursor ?? b.head().seq;
|
|
256
|
+
return { events: board.since(b, from), headSeq: b.head().seq };
|
|
257
|
+
});
|
|
258
|
+
if (state.cursor == null) {
|
|
259
|
+
state.cursor = headSeq; // first boot: don't replay history
|
|
260
|
+
log(`cursor initialized at ${headSeq}`);
|
|
261
|
+
store.save();
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
if (state.cursor > headSeq) {
|
|
265
|
+
log(`cursor ${state.cursor} ahead of head ${headSeq}: re-anchored`);
|
|
266
|
+
state.cursor = headSeq; // board switched / stale cursor — re-anchor, never replay
|
|
267
|
+
store.save();
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
let queued = 0;
|
|
271
|
+
for (const e of events) {
|
|
272
|
+
state.cursor = Math.max(state.cursor, e.seq);
|
|
273
|
+
if (e.kind === "assign" && e.actor !== ID) {
|
|
274
|
+
const to = e.payload?.to, key = e.payload?.key;
|
|
275
|
+
if (to === "workbuddy") {
|
|
276
|
+
await pokeWorkbuddyWorker(`首戳 ${key}`);
|
|
277
|
+
// 登记 re-poke 看门狗:未被认领前持续补戳,抵消"撞上在跑的 tick 被重置"的坑
|
|
278
|
+
const watch = (store.s.repokes ??= {});
|
|
279
|
+
watch[key] = { addedAt: Date.now(), lastPokeAt: Date.now(), attempts: 1 };
|
|
280
|
+
store.save();
|
|
281
|
+
log(`@workbuddy ${key} 实时侦测 (seq ${e.seq}, from ${e.actor}, +${latencyMark()}ms) → 已戳小羽 worker(~1-2min 拉起;未受理会自动补戳)`);
|
|
282
|
+
} else {
|
|
283
|
+
// direct exec agent (e.g. codex/pi) OR a board alias that routes to one
|
|
284
|
+
// (e.g. bridge -> codex). @codex/@pi 仍由 daemon 用 headless codex/pi 引擎处理。
|
|
285
|
+
const route = cfg.board?.route || {};
|
|
286
|
+
const execAgent = (to && enabledAgents.includes(to)) ? to : (to && route[to]);
|
|
287
|
+
if (execAgent && key) {
|
|
288
|
+
if (store.queue(key, execAgent, { priority: e.payload?.priority })) { queued++; log(`queued ${key} -> ${execAgent} (via ${to}, seq ${e.seq}, from ${e.actor}, +${latencyMark()}ms since wake)`); }
|
|
289
|
+
} else if (key) {
|
|
290
|
+
log(`assign ${key} -> ${to ?? "?"} not for us (seq ${e.seq})`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
store.save();
|
|
296
|
+
|
|
297
|
+
// 1.5) autoAssign(v0.1.1 上手路径修复):带 ```bridge auto 标记的 open 卡
|
|
298
|
+
// 无需手动 assign 事件——扫描后直接入队到第一个启用的面(gating 照常把关)。
|
|
299
|
+
// 关闭方式:runtime.autoAssign = false。
|
|
300
|
+
if (cfg.runtime?.autoAssign !== false) {
|
|
301
|
+
let auto = 0;
|
|
302
|
+
try {
|
|
303
|
+
await board.withBoard(cfg.board, (b) => {
|
|
304
|
+
for (const c of b.listTaskCards()) {
|
|
305
|
+
const t = c.task || {};
|
|
306
|
+
if (t.status !== "open") continue;
|
|
307
|
+
const key = t.key;
|
|
308
|
+
if (!key || store.s.runs[key] || store.s.finished[key] || store.s.pending[key]) continue;
|
|
309
|
+
if (Object.values(store.s.lanes ?? {}).some((l) => l.includes(key))) continue;
|
|
310
|
+
const fence = parseBridgeBlock(t.description ?? "");
|
|
311
|
+
if (!fence || !/^(yes|true|1)$/i.test(String(fence.auto ?? ""))) continue;
|
|
312
|
+
const agent = (enabledAgents.includes(fence.agent) && fence.agent) || enabledAgents[0];
|
|
313
|
+
if (!agent) continue;
|
|
314
|
+
if (store.queue(key, agent, { priority: fence.priority })) {
|
|
315
|
+
auto++;
|
|
316
|
+
log(`autoAssign ${key} -> ${agent} (fence auto flag)`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
});
|
|
320
|
+
} catch (e) {
|
|
321
|
+
log(`autoAssign scan error: ${e.message}`);
|
|
322
|
+
}
|
|
323
|
+
if (auto) store.save();
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// 2) schedule lanes (paused entirely while STOP is present)
|
|
327
|
+
if (existsSync(STOP)) {
|
|
328
|
+
if (store.s.lanes && Object.values(store.s.lanes).some((l) => l.length)) log("STOP present — scheduling paused");
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
for (const agent of enabledAgents) {
|
|
332
|
+
if (store.supplyHeld(agent)) {
|
|
333
|
+
// 队列原样留着:熔断只掐「现在要不要起子进程」,不改卡的状态、不丢任务。
|
|
334
|
+
const held = store.supplyGet(agent);
|
|
335
|
+
const lane = store.s.lanes?.[agent] ?? [];
|
|
336
|
+
if (lane.length && Date.now() - (held.lastHoldLogAt ?? 0) > (SUPPLY.logEveryMs ?? 600000)) {
|
|
337
|
+
held.lastHoldLogAt = Date.now();
|
|
338
|
+
store.save();
|
|
339
|
+
log(`supply hold: ${agent} 熔断中(${held.category} streak=${held.streak},剩 ${left(held.holdUntil - Date.now())} 放行探针)→ 队列 ${lane.length} 张暂不派发`);
|
|
340
|
+
}
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
// 优先级出队(调度层 C.2):assign 时的 laneAt 优先级 → 卡 payload.priority /
|
|
344
|
+
// 任务书 description 里 ```bridge 块的 priority → 缺省 P2。
|
|
345
|
+
// 队列里 >1 张才开一次板查任务书;0/1 张零开销直走。
|
|
346
|
+
const laneKeys = store.s.lanes?.[agent] ?? [];
|
|
347
|
+
let cardPrio = null;
|
|
348
|
+
if (laneKeys.length > 1) {
|
|
349
|
+
cardPrio = await board.withBoard(cfg.board, (b) =>
|
|
350
|
+
Object.fromEntries(laneKeys.map((k) => {
|
|
351
|
+
const t = board.card(b, k)?.task;
|
|
352
|
+
return [k, t?.payload?.priority ?? parseBridgeBlock(t?.description)?.priority ?? null];
|
|
353
|
+
})))
|
|
354
|
+
.catch(() => null);
|
|
355
|
+
}
|
|
356
|
+
const priorityOf = (k) => store.s.laneAt?.[k]?.priority ?? cardPrio?.[k] ?? null;
|
|
357
|
+
const agingMs = PRIORITY.agingMs ?? 7200000;
|
|
358
|
+
// 面级并发(调度层 C.5):每面 maxConcurrent(缺省 1 = 与旧行为一致——同面共享
|
|
359
|
+
// workdir,并发调大前先确认该面适配器不会互相踩工作目录)。
|
|
360
|
+
const faceMax = cfg.agents[agent]?.maxConcurrent ?? 1;
|
|
361
|
+
while (store.runsFor(agent) < faceMax && store.runningCount() < (cfg.runtime.maxConcurrentTotal ?? 2)) {
|
|
362
|
+
const key = store.pickBest(agent, priorityOf, Date.now(), agingMs, bestLaneIndex);
|
|
363
|
+
if (!key) break;
|
|
364
|
+
// reserve synchronously so the lane can't pop a second task for this agent
|
|
365
|
+
store.beginRun(key, { agent, dir: null, pid: null, startedAt: Date.now(), reserving: true });
|
|
366
|
+
store.save();
|
|
367
|
+
handle(key, agent)
|
|
368
|
+
.catch((e) => log(`handle(${key}) crashed: ${e.stack || e.message}`))
|
|
369
|
+
.finally(() => {
|
|
370
|
+
if (store.s.runs[key]?.reserving) { delete store.s.runs[key]; store.save(); }
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// 实时唤醒 小羽 worker(WorkBuddy automation 80a9684a):把它的 next_run_at 改成 now,
|
|
377
|
+
// 宿主调度器扫到后约 1-2 分钟拉起一轮(实测干净空窗 ~97s 认领 / ~145s 完成)。
|
|
378
|
+
// 这样 @workbuddy 黑板任务由"小羽本体"执行,而非 headless codex。
|
|
379
|
+
// DB 锁竞争用 busy_timeout 兜底;返回是否真的命中那一行(false = 白戳或失败)。
|
|
380
|
+
//
|
|
381
|
+
// ⚠ 已知坑(2026-09-19 实测):若这一戳落在 worker 某轮运行「进行中」的窗口里,
|
|
382
|
+
// 那轮跑完时宿主会把 next_run_at 重置成「结束时间 + 1 小时」,本戳就白戳了。
|
|
383
|
+
// 由 maybeRepoke() 看门狗负责在任务仍未被认领时重复补戳,把戳"跨过"那个重置窗口。
|
|
384
|
+
async function pokeWorkbuddyWorker(label) {
|
|
385
|
+
const dbPath = cfg.board?.workbuddyDb;
|
|
386
|
+
const automationId = cfg.board?.workbuddyAutomationId;
|
|
387
|
+
if (!dbPath || !automationId) { log("poke 小羽 worker 跳过:未配置 board.workbuddyDb / workbuddyAutomationId"); return false; }
|
|
388
|
+
try {
|
|
389
|
+
const { DatabaseSync } = await import("node:sqlite");
|
|
390
|
+
const db = new DatabaseSync(dbPath);
|
|
391
|
+
try {
|
|
392
|
+
db.exec("PRAGMA busy_timeout = 5000;");
|
|
393
|
+
const now = Date.now();
|
|
394
|
+
const res = db.prepare("UPDATE automations SET next_run_at = ?, updated_at = ? WHERE id = ?").run(now, now, automationId);
|
|
395
|
+
log(`poked 小羽 worker next_run_at=${now} changes=${res.changes}${res.changes ? "" : " [automationId 未匹配]"}${label ? ` [${label}]` : ""}`);
|
|
396
|
+
return res.changes > 0;
|
|
397
|
+
} finally { db.close(); }
|
|
398
|
+
} catch (e) {
|
|
399
|
+
log(`poke 小羽 worker 失败(可能 DB 被锁): ${e.message} —— 交给 re-poke 看门狗重试`);
|
|
400
|
+
return false;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// --- @workbuddy re-poke 看门狗 ---------------------------------------------
|
|
405
|
+
// 只要派给 workbuddy 的任务还没被认领/完结,就周期性把 worker 的 next_run_at 重新
|
|
406
|
+
// 戳成 now。目的:抵消上面那个"被运行中 tick 重置"的坑,也让 DB 偶发锁失败可自愈。
|
|
407
|
+
// 任务一旦被 claim / done / cancelled 就立刻停止催。
|
|
408
|
+
const REPOKE = {
|
|
409
|
+
intervalMs: 20000, // 多频繁检查一次待催列表
|
|
410
|
+
graceMs: 40000, // 距上次补戳多久之后再戳(给 worker 启动留出时间)
|
|
411
|
+
maxAttempts: 18, // 上限,避免无限骚扰(约 12 分钟)
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
async function maybeRepoke() {
|
|
415
|
+
if (existsSync(STOP)) return; // STOP 期间不催
|
|
416
|
+
const watch = store.s.repokes;
|
|
417
|
+
if (!watch || Object.keys(watch).length === 0) return;
|
|
418
|
+
const now = Date.now();
|
|
419
|
+
let changed = false;
|
|
420
|
+
for (const [key, w] of Object.entries(watch)) {
|
|
421
|
+
try {
|
|
422
|
+
const card = await board.withBoard(cfg.board, (b) => board.card(b, key));
|
|
423
|
+
if (!card) { delete watch[key]; changed = true; continue; }
|
|
424
|
+
const t = card.task;
|
|
425
|
+
if (t.claimedBy || ["done", "closed", "cancelled"].includes(t.status)) {
|
|
426
|
+
log(`re-poke watch: ${key} 已受理(status=${t.status}, claimedBy=${t.claimedBy ?? "-"})→ 停止补戳`);
|
|
427
|
+
delete watch[key];
|
|
428
|
+
changed = true;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
if (now - w.lastPokeAt < REPOKE.graceMs) continue;
|
|
432
|
+
if (w.attempts >= REPOKE.maxAttempts) {
|
|
433
|
+
log(`re-poke watch: ${key} 已戳 ${w.attempts} 次仍未受理,放弃(等 worker 常规轮询兜底)`);
|
|
434
|
+
delete watch[key];
|
|
435
|
+
changed = true;
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
const ok = await pokeWorkbuddyWorker(`补戳 #${w.attempts + 1} ${key}`);
|
|
439
|
+
if (ok) {
|
|
440
|
+
w.attempts += 1;
|
|
441
|
+
w.lastPokeAt = now;
|
|
442
|
+
log(`re-poke watch: ${key} 仍 open/unclaimed → 第 ${w.attempts} 次补戳`);
|
|
443
|
+
changed = true;
|
|
444
|
+
}
|
|
445
|
+
} catch (err) {
|
|
446
|
+
log(`re-poke watch: 检查 ${key} 出错: ${err.stack || err.message}`);
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
if (changed) store.save();
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
async function handle(key, agent) {
|
|
453
|
+
const state = store.s;
|
|
454
|
+
if (existsSync(STOP)) {
|
|
455
|
+
(state.lanes[agent] ??= []).unshift(key);
|
|
456
|
+
store.save();
|
|
457
|
+
log(`STOP present — ${key} returned to lane`);
|
|
458
|
+
return;
|
|
459
|
+
}
|
|
460
|
+
const card = await board.withBoard(cfg.board, (b) => board.card(b, key));
|
|
461
|
+
if (!card) return log(`skip ${key}: no such task`);
|
|
462
|
+
const task = card.task;
|
|
463
|
+
if (["done", "closed", "cancelled"].includes(task.status)) return log(`skip ${key}: status=${task.status}`);
|
|
464
|
+
if (task.claimedBy && task.claimedBy !== ID) return log(`skip ${key}: claimed by ${task.claimedBy}`);
|
|
465
|
+
|
|
466
|
+
const paths = findDispatchFiles([task.title, task.description]);
|
|
467
|
+
const dispatch = gating.readDispatch(paths);
|
|
468
|
+
|
|
469
|
+
// 面特定硬门限(2026-09-25 定):trae 的 bash 是单条常驻 cmd.exe,输出只能按一个 OEM 码页回读,
|
|
470
|
+
// 产出物文件名含非 ASCII 时盘上名字是对的、模型读回却是乱码 ⇒ 误判失败空烧到 max_steps。
|
|
471
|
+
// 命中就改派兜底面,绝不让它静默空转。局限:只认带扩展名的文件名 token。
|
|
472
|
+
const faceCfg = cfg.agents[agent] ?? {};
|
|
473
|
+
// 能力匹配(采纳建议③ 第一步,2026-10-07):任务书 bridge 块声明 needs(字符串或数组),
|
|
474
|
+
// 各面 agents.<face>.capabilities 声明能力;未声明能力的面视为万能(存量配置零改动)。
|
|
475
|
+
// 派发前校验:面缺能力 → 按 fallbacks 找有能力的可用面改派(与 asciiOnly 门同一形态)。
|
|
476
|
+
const needs = parseNeeds(parseBridgeBlock(task?.description));
|
|
477
|
+
if (needs.length && !faceHasCaps(faceCfg.capabilities, needs)) {
|
|
478
|
+
const cands = (faceCfg.fallbacks ?? []).filter((f) => enabledAgents.includes(f));
|
|
479
|
+
const { face: to, skipped } = await pickCapableFace({
|
|
480
|
+
candidates: cands, needs, agentsCfg: cfg.agents,
|
|
481
|
+
isReachable: (f) => faceReachable(cfg.agents[f]),
|
|
482
|
+
});
|
|
483
|
+
if (!to) {
|
|
484
|
+
log(`refuse ${key}: 需要能力 [${needs.join(",")}],${agent} 面不具备,候选 ${cands.join(",") || "未配置"} 均不可用(探不通=${skipped.join(",") || "无"})`);
|
|
485
|
+
await board.withBoard(cfg.board, (b) => board.note(b, ID,
|
|
486
|
+
`[${ID}] ${key} 未执行:需要能力 [${needs.join(",")}] 而 ${agent} 面不具备,候选面 ${cands.join(", ") || "(未配置)"} 均不可用${skipped.length ? `(${skipped.join(", ")} 探活不通或缺能力)` : ""}。请改派有能力的面或修正任务书 needs。`));
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
await board.withBoard(cfg.board, (b) => board.note(b, ID,
|
|
490
|
+
`[${ID}] ${key} 需要能力 [${needs.join(",")}],${agent} 面不具备 ⇒ 已改派 ${to} 执行${skipped.length ? `(${skipped.join(", ")} 探活不通或缺能力,跳过)` : ""}`));
|
|
491
|
+
log(`cap-route ${key}: ${agent} -> ${to} (needs [${needs.join(",")}])${skipped.length ? ` skipped=[${skipped.join(",")}]` : ""}`);
|
|
492
|
+
agent = to;
|
|
493
|
+
}
|
|
494
|
+
if (faceCfg.asciiOnlyProducts) {
|
|
495
|
+
const bad = nonAsciiProductNames([task.title, task.description, dispatch?.text].filter(Boolean).join("\n"));
|
|
496
|
+
if (bad.length) {
|
|
497
|
+
const cands = (faceCfg.fallbacks ?? []).filter((f) => enabledAgents.includes(f));
|
|
498
|
+
const skipped = [];
|
|
499
|
+
let to = null;
|
|
500
|
+
for (const f of cands) {
|
|
501
|
+
if (await faceReachable(cfg.agents[f])) { to = f; break; }
|
|
502
|
+
skipped.push(f);
|
|
503
|
+
}
|
|
504
|
+
if (!to) {
|
|
505
|
+
log(`refuse ${key}: ${agent} 门限命中,兜底面全不可用(候选=${cands.join(",") || "未配置"},探不通=${skipped.join(",") || "无"})`);
|
|
506
|
+
await board.withBoard(cfg.board, (b) => board.note(b, ID,
|
|
507
|
+
`[${ID}] ${key} 未执行:${agent} 面不适合非 ASCII 产出文件名(${bad.join(", ")}),兜底面 ${cands.join(", ") || "(未配置)"} 均不可达${skipped.length ? `(探不通: ${skipped.join(", ")})` : ""}。请改派可执行的面。`));
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
await board.withBoard(cfg.board, (b) => board.note(b, ID,
|
|
511
|
+
`[${ID}] ${key} 产出文件名含非 ASCII(${bad.join(", ")})⇒ ${agent} 面会读回乱码并空烧步数,已改派 ${to} 执行${skipped.length ? `(${skipped.join(", ")} 探活不通,跳过)` : ""}`));
|
|
512
|
+
log(`re-route ${key}: ${agent} -> ${to} (non-ASCII product names: ${bad.join(", ")})${skipped.length ? ` skipped=[${skipped.join(",")}]` : ""}`);
|
|
513
|
+
agent = to;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
let decision;
|
|
518
|
+
if (state.approved && state.approved[key]) {
|
|
519
|
+
delete state.approved[key]; // one-shot: human already approved this exact key
|
|
520
|
+
decision = { action: "auto", reason: "人工批准放行", fm: dispatch ? gating.decide(card, dispatch.text).fm : {} };
|
|
521
|
+
} else {
|
|
522
|
+
decision = gating.decide(card, dispatch?.text ?? null);
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// §17 硬档(VERIFY.enforce,默认关):产出型卡无验收标准 → 升级人工批准。
|
|
526
|
+
// 人工 one-shot 批准(上面分支)优先于本档;开档前先用默认档(未验 note+周报)观察一段时间。
|
|
527
|
+
if (VERIFY.enforce === true && decision.action === "auto"
|
|
528
|
+
&& !(task?.acceptance ?? parseBridgeBlock(task?.description)?.acceptance)) {
|
|
529
|
+
decision = { action: "ask", reason: "未带验收标准(verify.enforce 已开):补 acceptance 或声明 mode=human 后重新派发,或人工放行走自报通道" };
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// gating v2 (2026-10-04):软命中分流。TIER2 发布/凭据类词(推送/发布/上架/部署/
|
|
533
|
+
// 安装/token/secret/credential…)不再 park,只往板面写一条 note 然后照常派发;
|
|
534
|
+
// TIER1 真险词(rm -rf/drop table/删除/密码/密钥/生产环境…)仍走下面的 ask 分支。
|
|
535
|
+
// 没有这一段的话 action:"soft" 会既不 park 也不留痕,直接静默派发 —— 降级形同虚设。
|
|
536
|
+
if (decision.action === "soft") {
|
|
537
|
+
await board.withBoard(cfg.board, (b) =>
|
|
538
|
+
board.note(b, ID, `[${ID}→${agent}] ${key}: 软命中不拦截(${(decision.softHits ?? []).join(", ")})——发布/凭据类词属 TIER2,仅留痕后照常派发`));
|
|
539
|
+
log(`soft: ${key} proceeds with note (${(decision.softHits ?? []).join(", ")})`);
|
|
540
|
+
decision = { ...decision, action: "auto" };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
if (decision.action === "ask") {
|
|
544
|
+
state.pending[key] = { agent, at: new Date().toISOString(), reason: decision.reason, dispatch: dispatch?.path ?? null };
|
|
545
|
+
store.save();
|
|
546
|
+
await board.withBoard(cfg.board, (b) => {
|
|
547
|
+
board.message(b, ID, task.createdBy ?? cfg.owner,
|
|
548
|
+
`[${ID}] 任务 ${key} 派给 ${agent},但按分级策略需人工批准(${decision.reason})。放行: node bridge-ctl.js approve ${key};拒绝: ... deny ${key}`);
|
|
549
|
+
board.note(b, ID, `[${ID}→${agent}] ${key}: awaiting approval (${decision.reason})`);
|
|
550
|
+
});
|
|
551
|
+
log(`ask: ${key} parked (${decision.reason})`);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
if (cfg.runtime.dryRun) {
|
|
556
|
+
log(`DRYRUN: would spawn ${agent} for ${key} (${decision.reason})`);
|
|
557
|
+
state.finished[key] = { at: new Date().toISOString(), result: "dryrun", agent };
|
|
558
|
+
store.save();
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
await execute(key, agent, task, dispatch, decision);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
async function execute(key, agent, task, dispatch, decision) {
|
|
566
|
+
let taskSessionId = null;
|
|
567
|
+
try {
|
|
568
|
+
const session = await board.withBoard(cfg.board, (b) => board.startSession(b, `task ${key}`, {
|
|
569
|
+
project: task.project ?? null,
|
|
570
|
+
taskKey: key,
|
|
571
|
+
detached: true,
|
|
572
|
+
}));
|
|
573
|
+
taskSessionId = session?.id ?? null;
|
|
574
|
+
if (taskSessionId) {
|
|
575
|
+
store.s.runs[key] = { ...(store.s.runs[key] ?? {}), sessionId: taskSessionId };
|
|
576
|
+
store.save();
|
|
577
|
+
log(`task session started ${key}: ${taskSessionId} project=${task.project ?? "-"}`);
|
|
578
|
+
}
|
|
579
|
+
} catch (e) {
|
|
580
|
+
log(`task session start failed for ${key}: ${e.message} — continue on daemon session`);
|
|
581
|
+
}
|
|
582
|
+
try {
|
|
583
|
+
await executeInSession(key, agent, task, dispatch, decision, taskSessionId);
|
|
584
|
+
} finally {
|
|
585
|
+
if (taskSessionId) {
|
|
586
|
+
await board.withBoard(cfg.board, (b) => board.stopSession(b, taskSessionId), taskSessionId)
|
|
587
|
+
.catch((e) => log(`task session stop failed for ${key}/${taskSessionId}: ${e.message}`));
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
async function executeInSession(key, agent, task, dispatch, decision, taskSessionId = null) {
|
|
593
|
+
const pendingSessionWrites = new Set();
|
|
594
|
+
const ts = new Date().toISOString().replace(/[:.]/g, "-");
|
|
595
|
+
const baseRunDir = join(BASE, "runs", key, ts);
|
|
596
|
+
const agentCfg = cfg.agents[agent];
|
|
597
|
+
const fm = decision.fm ?? {};
|
|
598
|
+
const workdir = fm.cwd || agentCfg.workdir;
|
|
599
|
+
mkdirSync(workdir, { recursive: true });
|
|
600
|
+
const probe = (await import(`./lib/adapters/${agent}.js`)).probeModel?.(agentCfg) ?? {};
|
|
601
|
+
// 分段续跑(#29):默认 1 段=今天的行为;任务书 front matter 写 segments: 2|3 才切段,封顶 3。
|
|
602
|
+
// 选默认关是因为多一段就是多一笔真钱,谁掏钱谁点。
|
|
603
|
+
const totalSegments = maxSegments(fm);
|
|
604
|
+
const canResume = !!getAdapter(agent).resumeSpec;
|
|
605
|
+
|
|
606
|
+
const basePrompt = buildPrompt(key, task, dispatch, agent, totalSegments);
|
|
607
|
+
|
|
608
|
+
await board.withBoard(cfg.board, (b) => {
|
|
609
|
+
const r = board.claim(b, ID, key);
|
|
610
|
+
if (r.conflict && r.conflict !== ID) throw new Error(`lost claim race for ${key} to ${r.conflict}`);
|
|
611
|
+
board.progress(b, ID, key, 3);
|
|
612
|
+
// §17 派活验收纪律(2026-10-24):无验收标准的卡走自报通道——不拦人,但每次都留痕,
|
|
613
|
+
// 让"未验"在板面可见、可统计(digest 周期点名)。mode=human 是显式豁免,不算未验。
|
|
614
|
+
const blk = parseBridgeBlock(task?.description);
|
|
615
|
+
if (VERIFY.enabled !== false && !(task?.acceptance ?? blk?.acceptance)) {
|
|
616
|
+
board.note(b, ID, `[未验通道] ${key} 无验收标准(acceptance 列/bridge 块均未声明),done 将按执行面自报放行。派活方请按宪法 §17 补标准或声明 mode=human。`);
|
|
617
|
+
}
|
|
618
|
+
}, taskSessionId);
|
|
619
|
+
log(`claim+progress(3) ok: ${key}${totalSegments > 1 ? ` segments=${totalSegments}` : ""}`);
|
|
620
|
+
store.s.runs[key] = { agent, dir: baseRunDir, pid: null, startedAt: Date.now(), model: probe.model, segment: 1, segments: totalSegments, sessionId: taskSessionId };
|
|
621
|
+
store.save();
|
|
622
|
+
|
|
623
|
+
let prev = null, seg = 1;
|
|
624
|
+
let result = null, ok = false, summary = "", why = null, stopWhy = null, killedBy = null;
|
|
625
|
+
let usage = null, lastRunDir = baseRunDir, latch = null, supplyHit = null, prevSupply = null, recoveredFrom = null, lastHit = null;
|
|
626
|
+
|
|
627
|
+
for (;;) {
|
|
628
|
+
// 调用点硬闸(2026-10-02 加,起因是 10-01 的 P0:判据把"成功"写成"可再起一段",
|
|
629
|
+
// 一段已完成的活被连烧 7 段 / 466,241 token)。判据是纯函数可以写错,段号预算与全局封顶
|
|
630
|
+
// 是两条独立算出来的界 —— 越过任何一条都不再起段,代价是钱的事不靠单点判断。
|
|
631
|
+
if (segmentOverrun({ segment: seg, total: totalSegments })) {
|
|
632
|
+
stopWhy = `段号越界(第 ${seg} 段 > 本卡预算 ${totalSegments} / 全局封顶 ${SEGMENT_CAP}),调用点硬闸拦下`;
|
|
633
|
+
log(`SEGMENT GUARD ${key}: ${stopWhy}`);
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
// 每段一个独立 run 目录(seg1 沿用原路径不破坏既有台账):
|
|
637
|
+
// 适配器按目录读用量,同目录追加两段会让第二段把第一段的 token 再记一遍。
|
|
638
|
+
const runDir = seg === 1 ? baseRunDir : join(BASE, "runs", key, `${ts}-seg${seg}`);
|
|
639
|
+
mkdirSync(runDir, { recursive: true });
|
|
640
|
+
lastRunDir = runDir;
|
|
641
|
+
const promptFile = join(runDir, "prompt.md");
|
|
642
|
+
writeFileSync(promptFile, prev ? `${basePrompt}\n\n${prev.priming}` : basePrompt, "utf8");
|
|
643
|
+
writeFileSync(join(runDir, "meta.json"), JSON.stringify({
|
|
644
|
+
key, agent, startedAt: ts, workdir, model: probe.model, provider: probe.provider,
|
|
645
|
+
dispatch: dispatch?.path ?? null, gate: decision.reason,
|
|
646
|
+
segment: seg, segments: totalSegments, resumeFrom: prev?.runDir ?? null,
|
|
647
|
+
}, null, 1));
|
|
648
|
+
store.s.runs[key] = { ...store.s.runs[key], dir: runDir, pid: null, segment: seg };
|
|
649
|
+
store.save();
|
|
650
|
+
|
|
651
|
+
let lastProgressAt = 0, items = 0;
|
|
652
|
+
result = await runTask({
|
|
653
|
+
agentCfg, adapter: agent, runDir, promptFile, workdir, fm,
|
|
654
|
+
stall: cfg.runtime?.stall,
|
|
655
|
+
resume: prev?.threadId ? { threadId: prev.threadId, segment: seg } : null,
|
|
656
|
+
runId: `${key}@${ts}${seg > 1 ? `#seg${seg}` : ""}`, log,
|
|
657
|
+
onSpawn: (pid) => { if (store.s.runs[key]) { store.s.runs[key].pid = pid; store.save(); log(`spawned pid=${pid} for ${key} seg=${seg}`); emitSpan("gen_ai.agent.dispatch", { task: key, agent, pid, segment: seg }); } },
|
|
658
|
+
onMilestone: (m, st) => {
|
|
659
|
+
if (m === "item") {
|
|
660
|
+
items = st.lastMsgCount;
|
|
661
|
+
const pct = Math.min(90, 5 + Math.floor(items / 3) * 5);
|
|
662
|
+
const now = Date.now();
|
|
663
|
+
if (now - lastProgressAt > 45000) {
|
|
664
|
+
lastProgressAt = now;
|
|
665
|
+
const write = board.withBoard(cfg.board, (b) => board.progress(b, ID, key, pct), taskSessionId)
|
|
666
|
+
.then(() => log(`progress(${pct}) ${key} items=${items}`))
|
|
667
|
+
.catch((e) => log(`progress write failed: ${e.message}`));
|
|
668
|
+
pendingSessionWrites.add(write);
|
|
669
|
+
write.then(() => pendingSessionWrites.delete(write));
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
},
|
|
673
|
+
});
|
|
674
|
+
if (store.s.runs[key]) { store.s.runs[key].pid = null; }
|
|
675
|
+
killedBy = store.s.runs[key]?.killedBy ?? null;
|
|
676
|
+
|
|
677
|
+
summary = gating.redact(trunc(result.resultText ?? "", 1500));
|
|
678
|
+
// agent 自报 success=false(例:步数耗尽)不能算完成——rc=0 只说明进程没崩。
|
|
679
|
+
// stalled 也要算失败:判停是靠 taskkill 掐的,被掐那一刻子进程常常 rc=0 且已吐过一段话。
|
|
680
|
+
ok = result.exitCode === 0 && !result.killed && !result.stalled && summary.length > 0 && result.agentSuccess !== false;
|
|
681
|
+
why = ok ? null : failureWhy(result, killedBy, agentCfg);
|
|
682
|
+
|
|
683
|
+
// 成本采集(#26):每段各记一行——失败的那段照样烧了 token,混成一行会把段数藏掉。
|
|
684
|
+
usage = await recordUsage({
|
|
685
|
+
agent, agentCfg, runDir, key, ts, result, ok, probe,
|
|
686
|
+
segment: seg, segments: totalSegments, resumeFrom: prev?.runDir ?? null,
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
// 供应方判定(#25):三条同时成立才冻面 —— 失败 + 0 步进展 + 扫到供应方文案。
|
|
690
|
+
const hit = ok ? null : scanRunDir(runDir);
|
|
691
|
+
lastHit = hit;
|
|
692
|
+
supplyHit = hit && (result.items ?? 0) === 0 ? hit : null;
|
|
693
|
+
prevSupply = store.supplyGet(agent);
|
|
694
|
+
if (supplyHit) {
|
|
695
|
+
const streak = (prevSupply?.streak ?? 0) + 1; // 与 supplyLatch 内部同式(这中间没人 release)
|
|
696
|
+
latch = store.supplyLatch(agent, {
|
|
697
|
+
category: supplyHit.category, label: supplyHit.label,
|
|
698
|
+
evidence: supplyHit.line, evidenceFile: supplyHit.file,
|
|
699
|
+
key, to: task.createdBy ?? cfg.owner, runDir, holdMs: holdMsFor(streak),
|
|
700
|
+
});
|
|
701
|
+
emitSpan("gen_ai.agent.breaker", { agent, category: latch.category, streak: latch.streak, holdMs: latch.holdMs, task: key });
|
|
702
|
+
// 熔断到点要有事件把队列叫醒:只靠 reconcile 兜底的话,恢复探针最多晚一个 reconcileMs(真配置 60s)才发。
|
|
703
|
+
setTimeout(() => wake(`supply hold ${agent} 到点`), latch.holdMs + 500).unref?.();
|
|
704
|
+
}
|
|
705
|
+
recoveredFrom = ok ? prevSupply : null;
|
|
706
|
+
if (ok && recoveredFrom) store.supplyRelease(agent);
|
|
707
|
+
|
|
708
|
+
stopWhy = shouldStopSegmenting({
|
|
709
|
+
ok, segment: seg, total: totalSegments, killedBy,
|
|
710
|
+
stopFilePresent: existsSync(STOP), supplyLatched: !!supplyHit,
|
|
711
|
+
starved: (result.starve ?? 0) >= STARVE_LIMIT,
|
|
712
|
+
});
|
|
713
|
+
if (stopWhy) break;
|
|
714
|
+
|
|
715
|
+
// 走到这里 = 这一段没成、没被人工或供应方叫停、还有段数没用 ⇒ 落检查点,续下一段
|
|
716
|
+
const cp = capture({ agent, workdir, runDir, segment: seg, result, usage, why });
|
|
717
|
+
const willResume = canResume && !!result.threadId;
|
|
718
|
+
const nextSeg = seg + 1;
|
|
719
|
+
prev = {
|
|
720
|
+
runDir, threadId: result.threadId ?? null,
|
|
721
|
+
priming: renderPriming({ segment: nextSeg, total: totalSegments, cp, why, resumed: willResume }),
|
|
722
|
+
};
|
|
723
|
+
log(`segment ${seg}->${nextSeg} for ${key}: why=${why} checkpoint=${cp.file} kind=${cp.kind} resume=${willResume ? prev.threadId.slice(0, 8) : "否(检查点重进)"}`);
|
|
724
|
+
await board.withBoard(cfg.board, (b) => {
|
|
725
|
+
board.progress(b, ID, key, Math.min(85, 5 + seg * 20));
|
|
726
|
+
board.note(b, ID, `[${ID}→${agent}] ${key} 第 ${seg}/${totalSegments} 段未完成(${why})⇒ 续跑第 ${nextSeg} 段。检查点 ${cp.file}(来源=${cp.kind === "agent" ? "模型自写" : "桥派生"},工作目录 ${cp.fileCount} 个文件);工作目录不变 ${workdir};接${willResume ? `原 thread ${String(result.threadId).slice(0, 8)}` : "新会话(这一面无会话级续跑,靠检查点题面重建状态)"}。`);
|
|
727
|
+
}, taskSessionId);
|
|
728
|
+
seg = nextSeg;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
const segTag = totalSegments > 1 ? ` 段=${seg}/${totalSegments}` : "";
|
|
732
|
+
// 验收判定(验证层 V1,毫秒级纯文件检查):优先任务卡一等 acceptance 列
|
|
733
|
+
// (2026-10-07 共享包扩展);未声明时回退任务书 bridge 围栏块,兼容存量任务。
|
|
734
|
+
// 在写板前算完,决定结案走哪条路径——执行者自报不算数。
|
|
735
|
+
const bridgeBlock = parseBridgeBlock(task?.description);
|
|
736
|
+
const acc = task?.acceptance ?? bridgeBlock?.acceptance ?? null;
|
|
737
|
+
const accMode = acc?.mode === "human" ? "human"
|
|
738
|
+
: acc?.mode === "cross" ? "cross" : "auto";
|
|
739
|
+
// human 不跑机械项(直接进 V3 复核);cross 先跑机械项(有的话),过了才进 V2 复核;
|
|
740
|
+
// auto 或未声明跑机械项,全过即关单。
|
|
741
|
+
let verifyRes = null;
|
|
742
|
+
if (ok && VERIFY.enabled !== false && acc && accMode !== "human") {
|
|
743
|
+
verifyRes = evaluateAcceptance({ acceptance: acc, baseDir: workdir });
|
|
744
|
+
}
|
|
745
|
+
const reviewMode = VERIFY.enabled !== false && (accMode === "human" || accMode === "cross");
|
|
746
|
+
let reviewQueued = false;
|
|
747
|
+
const verifyFailed = !!verifyRes && !verifyRes.pass;
|
|
748
|
+
const verifyInvalid = verifyFailed && verifyRes.checks.some((c) => c.invalid);
|
|
749
|
+
if (verifyFailed) log(`verify FAIL ${key}: ${verifyRes.checks.filter((c) => !c.pass).length}/${verifyRes.checks.length} 项未过${verifyInvalid ? "(含 spec 类错误)" : ""}`);
|
|
750
|
+
// V1.5 judge(采纳建议①):机械项验形,judge 验质。只对 auto 模式且声明了
|
|
751
|
+
// acceptance 的任务跑(acceptance-only 档控制成本);judge 只当信号不当闸门——
|
|
752
|
+
// fail 升级 V3 复核、不可用则跳过,都不自动改判。
|
|
753
|
+
let judgeRes = null;
|
|
754
|
+
if (ok && !verifyFailed && !reviewMode && JUDGE.enabled && acc) {
|
|
755
|
+
const judgeAgent = JUDGE.agent ?? "dsh";
|
|
756
|
+
judgeRes = await runJudge({
|
|
757
|
+
judgeAgent,
|
|
758
|
+
agentCfg: cfg.agents?.[judgeAgent] ?? {},
|
|
759
|
+
key, description: task?.description,
|
|
760
|
+
rubric: acc?.judge?.rubric,
|
|
761
|
+
workdir, runDirBase: baseRunDir, log,
|
|
762
|
+
});
|
|
763
|
+
log(`judge V1.5 ${key}: ${judgeRes.verdict}${judgeRes.reason ? ` — ${trunc(judgeRes.reason, 120)}` : ""}`);
|
|
764
|
+
}
|
|
765
|
+
try {
|
|
766
|
+
await board.withBoard(cfg.board, (b) => {
|
|
767
|
+
if (ok && verifyFailed) {
|
|
768
|
+
// —— 验收未通过:不接受 done(验证层 V1)——
|
|
769
|
+
if (verifyInvalid) {
|
|
770
|
+
board.setStatus(b, ID, key, "blocked");
|
|
771
|
+
board.note(b, ID, `[${ID}→${agent}] ${formatVerifyNote({ key, verdict: "FAIL", checks: verifyRes.checks, by: ID })}\n· 验收标准本身有误(spec 类:未知检查类型/缺字段)→ 不罚面、不烧重试;请任务作者修正任务书 bridge 块后 requeue。`);
|
|
772
|
+
board.message(b, ID, task.createdBy ?? cfg.owner, `[${ID}] 任务 ${key} 的验收标准写错了(见 note),任务已落 blocked。修正任务的 acceptance(一等列或任务书 bridge 块)后:node bridge-ctl.js requeue ${key} ${agent}。`);
|
|
773
|
+
log(`verify spec-invalid ${key}: parked for author fix`);
|
|
774
|
+
} else {
|
|
775
|
+
const max = Number(bridgeBlock?.retry?.max ?? task?.payload?.retry?.max ?? RETRY.max ?? 2);
|
|
776
|
+
const res = applyVerifyFail({ b, io: board, store, ID, owner: cfg.owner, key, agent, task, v: verifyRes, maxAttempts: max, baseMs: RETRY.baseMs ?? 1800000, maxDelayMs: RETRY.maxMs ?? 14400000, holdBase: HOLD_BASE, holdMax: HOLD_MAX, now: Date.now() });
|
|
777
|
+
emitSpan("gen_ai.agent.verify", { task: key, agent, level: "V1", verdict: "fail", outcome: res.outcome, attempts: res.attempts ?? null });
|
|
778
|
+
log(`verify fail handled ${key}: ${res.outcome}${res.outcome === "retry-scheduled" ? ` attempt ${res.attempts}/${max} in ${left(res.delayMs)}` : ` streak=${res.streak}`}`);
|
|
779
|
+
}
|
|
780
|
+
} else if (ok && reviewMode) {
|
|
781
|
+
// —— V2/V3:机械项已过(或未声明)但不自动关单,进复核队列(验证层 Part B)——
|
|
782
|
+
const reviewer = acc?.reviewer ?? cfg.owner;
|
|
783
|
+
const v1Note = verifyRes ? `(V1 机械项已过 ${verifyRes.checks.filter((c) => c.pass).length}/${verifyRes.checks.length})` : "";
|
|
784
|
+
const res = queueReview({ b, io: board, store, ID, key, agent, mode: accMode, reviewer, runDir: lastRunDir, v1Note });
|
|
785
|
+
emitSpan("gen_ai.agent.review", { task: key, agent, mode: accMode, level: res.level, reviewer: res.reviewer, verdict: "queued" });
|
|
786
|
+
if (verifyRes?.pass) board.verify?.(b, ID, key, "pass", { level: "V1", summary: `verify PASS ${key} (V1, pre-${res.level})`, checks: verifyRes.checks });
|
|
787
|
+
reviewQueued = true;
|
|
788
|
+
log(`review queued ${key} (${res.level}, reviewer=${res.reviewer})`);
|
|
789
|
+
} else if (ok && judgeRes?.verdict === "fail") {
|
|
790
|
+
// —— V1.5 judge 存疑:升级人工复核(V3),复核人才是裁决者(采纳建议①)——
|
|
791
|
+
const reviewer = acc?.reviewer ?? cfg.owner;
|
|
792
|
+
const res = queueReview({ b, io: board, store, ID, key, agent, mode: "human", reviewer, runDir: lastRunDir, v1Note: `(V1 机械项已过${verifyRes ? ` ${verifyRes.checks.filter((c) => c.pass).length}/${verifyRes.checks.length}` : ""};V1.5 judge 存疑:${trunc(judgeRes.reason, 160)})` });
|
|
793
|
+
board.verify?.(b, ID, key, "fail", { level: "V1.5", summary: `verify FAIL ${key} (V1.5 judge) — ${trunc(judgeRes.reason, 120)}` });
|
|
794
|
+
emitSpan("gen_ai.agent.verify", { task: key, agent, level: "V1.5", verdict: "fail", judgeModel: judgeRes.model ?? null });
|
|
795
|
+
emitSpan("gen_ai.agent.review", { task: key, agent, mode: "human", level: "V3", reviewer: res.reviewer, verdict: "queued", reason: "V1.5 judge fail" });
|
|
796
|
+
reviewQueued = true;
|
|
797
|
+
log(`judge upgraded ${key} → V3 review (${res.reviewer})`);
|
|
798
|
+
} else if (ok) {
|
|
799
|
+
if (verifyRes) {
|
|
800
|
+
applyVerifyPass({ b, io: board, ID, key, v: verifyRes });
|
|
801
|
+
emitSpan("gen_ai.agent.verify", { task: key, agent, level: "V1", verdict: "pass", checks: verifyRes.checks.length });
|
|
802
|
+
log(`verify PASS ${key} (${verifyRes.checks.length} checks)`);
|
|
803
|
+
}
|
|
804
|
+
if (judgeRes?.verdict === "pass") board.verify?.(b, ID, key, "pass", { level: "V1.5", summary: `verify PASS ${key} (V1.5 judge)` });
|
|
805
|
+
board.progress(b, ID, key, 100);
|
|
806
|
+
board.done(b, ID, key);
|
|
807
|
+
board.note(b, ID, `[${ID}→${agent}] ${key} 完成(headless ${agent} exec, model=${result.model ?? probe.model ?? "?"}${result.threadId ? ", thread=" + result.threadId.slice(0, 8) : ""}, log=${lastRunDir}${segTag}):\n${summary}`);
|
|
808
|
+
board.message(b, ID, agent, `[${ID}] 任务 ${key} 已由你无头完成,结果:\n${trunc(summary, 600)}\n日志: ${lastRunDir}`);
|
|
809
|
+
if (recoveredFrom) {
|
|
810
|
+
// 熔断期间队列里可能还压着卡,恢复要立刻说出来,免得别人以为还在冻。
|
|
811
|
+
const queued = (store.s.lanes?.[agent] ?? []).length;
|
|
812
|
+
board.note(b, ID, `[${ID}→${agent}] 供应方恢复:${key} 正常跑完(items=${result.items}),解除 ${recoveredFrom.category} 熔断(此前连续失败 ${recoveredFrom.streak} 次)${queued ? `,队列还有 ${queued} 张待派` : ""}`);
|
|
813
|
+
if (OWNER_SUPPLY_NOTIFY) board.message(b, ID, recoveredFrom.to ?? cfg.owner, `[${ID}] ${agent} 面的供应方已恢复(${recoveredFrom.category} 熔断解除),被熔断压住的队列会继续派发。`);
|
|
814
|
+
else log(`supply recovered ${agent}: ${recoveredFrom.category} 熔断解除(owner 提醒已关闭)`);
|
|
815
|
+
}
|
|
816
|
+
} else {
|
|
817
|
+
// 失败也要结案:板上没有 failed/cancelled 状态词,一律落 blocked,
|
|
818
|
+
// 四档死因(人工掐死/超时被杀/自报 success=false/带 rc)写进下面的 note。
|
|
819
|
+
board.setStatus(b, ID, key, "blocked");
|
|
820
|
+
let extra = "";
|
|
821
|
+
if (latch) {
|
|
822
|
+
// 打扰 owner 只在该面「刚出事」或「毛病换了类」时;同场故障的每次探针失败只进日志。
|
|
823
|
+
const first = latch.streak === 1 || (prevSupply?.category ?? null) !== latch.category;
|
|
824
|
+
extra = `\n· 供应方熔断:${supplyReason(supplyHit)},${agent} 面暂停派发 ${hold(latch.holdMs)}(第 ${latch.streak} 次连续失败,退避 ${hold(HOLD_BASE)}→${hold(HOLD_MAX)} 封顶;到期自动放一张探针过去)。证据 ${supplyHit.file}:「${trunc(supplyHit.line ?? "", 240)}」`;
|
|
825
|
+
if (first) {
|
|
826
|
+
if (OWNER_SUPPLY_NOTIFY) {
|
|
827
|
+
board.message(b, ID, cfg.owner, `[${ID}] ${agent} 面的供应方出问题了(不是任务本身):${supplyReason(supplyHit)}。本次任务 ${key} 已落 blocked,该面已熔断 ${hold(latch.holdMs)}、期间不再派卡(队列保留)。证据:${supplyHit.file}:「${trunc(supplyHit.line ?? "", 240)}」\n日志: ${lastRunDir}`);
|
|
828
|
+
} else {
|
|
829
|
+
log(`supply hold ${agent}: streak=${latch.streak}(首场故障,owner 提醒已关闭:${supplyReason(supplyHit)})`);
|
|
830
|
+
}
|
|
831
|
+
} else {
|
|
832
|
+
log(`supply hold ${agent}: streak=${latch.streak}(同场故障,不再打扰 owner)`);
|
|
833
|
+
}
|
|
834
|
+
// 余额/额度耗尽(billing):卡不会自己恢复,转成公开待认领任务,别让它僵在 blocked 等人。
|
|
835
|
+
// 只对 billing(rate 会自愈、auth 要换 key,都不该转派);只转一次(幂等)。
|
|
836
|
+
const conv = convertBillingTask({
|
|
837
|
+
setStatus: (k, s) => board.setStatus(b, ID, k, s),
|
|
838
|
+
defineTask: (k, o) => board.defineTask(b, ID, k, o),
|
|
839
|
+
note: (t) => board.note(b, ID, `[${ID}] ${t}`),
|
|
840
|
+
}, { key, agent, task, hit: supplyHit, runDir: lastRunDir, converted: billingConverted, enabled: BILLING_CONVERT_ON });
|
|
841
|
+
if (conv.converted) {
|
|
842
|
+
extra += `\n· 已转为公开待认领(余额断供不会自愈)—— 见卡面「供应方断供 · 公开待认领」段与广播 note。`;
|
|
843
|
+
log(`billing-convert ${key}: ${agent} 余额断供 → 公开待认领`);
|
|
844
|
+
// "只报一次"纪律:该面首次断供时私信 owner 一句人话。
|
|
845
|
+
if (first) {
|
|
846
|
+
board.message(b, ID, cfg.owner, `[${ID}] ${billingOwnerNotice({ agent, hit: supplyHit, key, runDir: lastRunDir })}`);
|
|
847
|
+
}
|
|
848
|
+
} else if (conv.reason === "already-converted") {
|
|
849
|
+
extra += `\n· (此卡早已转公开待认领,本次不重复广播)`;
|
|
850
|
+
}
|
|
851
|
+
} else if (lastHit) {
|
|
852
|
+
// 只记录不熔断:文案在、但这一步确实往前走了,说明多半是 agent 引用的内容而非 API 回的话。
|
|
853
|
+
extra = `\n· 扫到供应方文案但本次有进展(items=${result.items})⇒ 只记录不熔断。命中 ${lastHit.file}:「${trunc(lastHit.line, 240)}」`;
|
|
854
|
+
}
|
|
855
|
+
if (stopWhy && seg > 1) extra += `\n· 分段续跑:共跑 ${seg}/${totalSegments} 段仍未收敛(停手原因:${stopWhy});各段检查点在 ${dirname(lastRunDir)}\\${ts}-seg* 下,要人工拆题再派。`;
|
|
856
|
+
board.note(b, ID, `[${ID}→${agent}] ${key} FAILED ${why}${totalSegments > 1 ? `(第 ${seg}/${totalSegments} 段)` : ""} log=${lastRunDir}${extra}`);
|
|
857
|
+
if (latch) {
|
|
858
|
+
// 卡级照报(谁的卡谁知道自己这张被挡了),故障级只在开场/换类时报 owner —— 免得刷屏。
|
|
859
|
+
board.message(b, ID, task.createdBy ?? cfg.owner,
|
|
860
|
+
`[${ID}] 任务 ${key} 落 blocked:不是任务本身的问题,是 ${agent} 面的供应方${supplyReason(supplyHit)}。该面已熔断 ${hold(latch.holdMs)},到期自动放探针;要立刻重试: node bridge-ctl.js supply-clear ${agent} && node bridge-ctl.js requeue ${key} ${agent}。日志: ${lastRunDir}`);
|
|
861
|
+
} else {
|
|
862
|
+
board.message(b, ID, task.createdBy ?? cfg.owner, `[${ID}] 任务 ${key} 执行失败(${why}),日志: ${lastRunDir}${summary ? `\n它自报的收尾:\n${trunc(summary, 600)}` : ""}`);
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
}, taskSessionId);
|
|
866
|
+
} catch (e) {
|
|
867
|
+
log(`write-back failed for ${key}: ${e.message}`);
|
|
868
|
+
}
|
|
869
|
+
store.save();
|
|
870
|
+
store.endRun(key, ok ? (verifyFailed ? "verify-failed" : reviewQueued ? "review-pending" : "done") : `failed rc=${result.exitCode}`);
|
|
871
|
+
// 自动重派(调度层 C.3):瞬态类失败(自报失败/rc/stall/timeout)按退避自动 requeue;
|
|
872
|
+
// terminal 类(人工掐、供应方熔断、spawn 失败)不自动重试。任务书 payload.retry.max 可覆盖次数。
|
|
873
|
+
// 验收失败(verifyFailed 且非 spec 类):重试台账由 applyVerifyFail 在写板回调里接管,这里不再动。
|
|
874
|
+
if (!ok) {
|
|
875
|
+
const attempts = store.s.retries?.[key]?.n ?? 0;
|
|
876
|
+
const max = Number(task?.payload?.retry?.max ?? RETRY.max ?? 2);
|
|
877
|
+
const d = retryDecision({ why, latch, killedBy, result, attempts, maxAttempts: max, baseMs: RETRY.baseMs ?? 1800000, maxDelayMs: RETRY.maxMs ?? 14400000 });
|
|
878
|
+
if (d.retry) {
|
|
879
|
+
(store.s.retries ??= {})[key] = { n: d.attempt, dueAt: Date.now() + d.delayMs, agent, why, lastAt: Date.now() };
|
|
880
|
+
log(`auto-retry scheduled ${key}: attempt ${d.attempt}/${max} in ${left(d.delayMs)}`);
|
|
881
|
+
emitSpan("gen_ai.agent.retry", { task: key, agent, attempt: d.attempt, delayMs: d.delayMs, why });
|
|
882
|
+
await board.withBoard(cfg.board, (b) =>
|
|
883
|
+
board.note(b, ID, `[${ID}→${agent}] ${key} 将自动重派(第 ${d.attempt}/${max} 次,${left(d.delayMs)} 后;上次失败:${why})`), taskSessionId)
|
|
884
|
+
.catch((e) => log(`retry note failed for ${key}: ${e.message}`));
|
|
885
|
+
} else {
|
|
886
|
+
delete store.s.retries?.[key];
|
|
887
|
+
log(`no auto-retry for ${key}: ${d.reason}`);
|
|
888
|
+
}
|
|
889
|
+
} else if (!verifyFailed || verifyInvalid) {
|
|
890
|
+
// 真成功(或 spec 类验收错误打回作者)时清重试账;work 类验收失败的重试账在
|
|
891
|
+
// applyVerifyFail 里已写好,这里必须别碰。
|
|
892
|
+
delete store.s.retries?.[key];
|
|
893
|
+
}
|
|
894
|
+
store.save();
|
|
895
|
+
if (pendingSessionWrites.size) await Promise.all([...pendingSessionWrites]);
|
|
896
|
+
log(`finished ${key}: ${ok ? "done" : "FAILED " + why}${result.threadId ? " thread=" + result.threadId.slice(0, 8) : ""}${segTag}`);
|
|
897
|
+
// run 结束本身就是一条调度事件:车道刚腾出来、结案已落盘、重试台账已结算 ⇒ 立刻叫醒一次。
|
|
898
|
+
// 不接这一下,下一张卡只能等 reconcile 兜底(真配置 60s),每张卡之间白等一轮。
|
|
899
|
+
// execute 是被 tick await 着的,此时 ticking=true ⇒ wake() 走再入保护置 again,
|
|
900
|
+
// 本轮 tick 收尾后自动补一次 wake("re-run")——这正是「不丢唤醒」要的语义,不会自激。
|
|
901
|
+
log(`run-end wake ${key}`);
|
|
902
|
+
wake(`run-end ${key}`);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/** 死因分档(原来内联在结案块里,分段后循环也要用同一个判据,抽出共用)。 */
|
|
906
|
+
function failureWhy(result, killedBy, agentCfg) {
|
|
907
|
+
return result.spawnError
|
|
908
|
+
?? (killedBy === "manual" ? "人工掐死(bridge-ctl kill)"
|
|
909
|
+
: result.stalled ? `无进展被掐(stall=${agentCfg.stallMs ?? cfg.runtime?.stall?.ms ?? 600000}ms 内没有新 milestone,items=${result.items})`
|
|
910
|
+
: result.killed ? "超时被杀(timeoutMs)"
|
|
911
|
+
: (result.starve ?? 0) >= STARVE_LIMIT
|
|
912
|
+
? `输出预算饿死(连续 ${result.starve} 次 finish_reason=length 且 content 空且无 tool_calls,累计 ${result.starvedTotal ?? result.starve} 次)——面级 max_tokens 不足,抬预算或关思考,重派/续跑都只会再烧一遍`
|
|
913
|
+
: result.agentSuccess === false ? "agent 自报 success=false"
|
|
914
|
+
: "rc=" + result.exitCode);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* 成本采集(#26):把这轮的权威用量追加到 usage.jsonl。
|
|
919
|
+
* 铁律——读不到就记 missing,绝不补 0:账上的 0 会被读成「这一面没花钱」,
|
|
920
|
+
* 而预算校验恰恰是靠这个数决定还要不要再往这面派卡。
|
|
921
|
+
* 采集失败绝不能拖垮结案,所以整段自己吞错。
|
|
922
|
+
*/
|
|
923
|
+
async function recordUsage({ agent, agentCfg, runDir, key, ts, result, ok, probe, segment = 1, segments = 1, resumeFrom = null }) {
|
|
924
|
+
let entry;
|
|
925
|
+
try {
|
|
926
|
+
const mod = await import(`./lib/adapters/${agent}.js`);
|
|
927
|
+
entry = normalizeUsage(agent, mod.readUsage ? mod.readUsage({ runDir, agentCfg }) : null);
|
|
928
|
+
} catch (e) {
|
|
929
|
+
entry = missingUsage(agent, `readUsage 抛错:${e.message}`);
|
|
930
|
+
}
|
|
931
|
+
Object.assign(entry, {
|
|
932
|
+
ts: new Date().toISOString(), key, runId: `${key}@${ts}${segment > 1 ? `#seg${segment}` : ""}`, runDir,
|
|
933
|
+
outcome: ok ? "done" : "failed", exitCode: result.exitCode ?? null,
|
|
934
|
+
killed: !!result.killed, stalled: !!result.stalled,
|
|
935
|
+
// 分段续跑(#29):一行 = 一段。不带段号的话,两张 2 段的卡在账上看起来像两次独立派发,
|
|
936
|
+
// 成本对不上「这张卡实际跑了几段」。
|
|
937
|
+
segment, segments, resumeFrom,
|
|
938
|
+
model: result.model ?? probe?.model ?? null,
|
|
939
|
+
provider: result.provider ?? probe?.provider ?? null,
|
|
940
|
+
});
|
|
941
|
+
try {
|
|
942
|
+
appendUsage(USAGEF, entry);
|
|
943
|
+
} catch (e) {
|
|
944
|
+
log(`usage append failed: ${e.message}`);
|
|
945
|
+
}
|
|
946
|
+
log(entry.missing
|
|
947
|
+
? `usage MISSING ${key}/${agent}:${entry.reason}(不记 0)`
|
|
948
|
+
: `usage ${key}/${agent} steps=${entry.steps ?? "?"}${entry.overflow ? "(达上限)" : ""} prompt=${entry.prompt ?? "?"} completion=${entry.completion ?? "?"} total=${entry.total ?? "?"}${entry.costUsd != null ? ` cost=$${entry.costUsd}` : ""}`);
|
|
949
|
+
return entry;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
function buildPrompt(key, task, dispatch, agent, totalSegments = 1) {
|
|
953
|
+
return [
|
|
954
|
+
`你是 ${agent},正被 agent-bridge 无头调起,执行黑板任务 ${key}(#${task.number ?? "?"})。`,
|
|
955
|
+
`标题: ${task.title ?? key}`,
|
|
956
|
+
task.description ? `描述: ${task.description}` : null,
|
|
957
|
+
dispatch ? `任务书文件: ${dispatch.path}` : null,
|
|
958
|
+
dispatch ? ["", "===== 任务书全文 =====", trunc(dispatch.text, 200000)].join("\n") : null,
|
|
959
|
+
"",
|
|
960
|
+
"要求:",
|
|
961
|
+
"1) 完成该任务;产物留在工作目录内。",
|
|
962
|
+
"2) 不要读写黑板(回报由 bridge 统一完成),不要再分派任务给任何 agent。",
|
|
963
|
+
"3) 最后一条回复用中文简明汇报:做了什么 / 改了哪些文件 / 如何验证 / 未尽事项。",
|
|
964
|
+
// 只在会分段时加:不写 segments 的卡保持今天的题面,一处字节差异都不留。
|
|
965
|
+
totalSegments > 1 ? `4) 这一轮可能被步数或时间上限打断(任务分 ${totalSegments} 段跑)。若你预感到这一轮做不完,先在工作目录根部写 CHECKPOINT.md:已完成 / 现存产物文件 / 下一步(三步以内、可直接执行)。做完了就不用写。` : null,
|
|
966
|
+
].filter((s) => s !== null).join("\n");
|
|
967
|
+
}
|
|
968
|
+
const trunc = (s, n) => (s.length > n ? s.slice(0, n) + "\n…[截断]" : s);
|
|
969
|
+
|
|
970
|
+
// ---- wake serialization ----
|
|
971
|
+
let ticking = false, again = false;
|
|
972
|
+
async function wake(why) {
|
|
973
|
+
if (ticking) { again = true; return; }
|
|
974
|
+
ticking = true;
|
|
975
|
+
t0 = process.hrtime.bigint();
|
|
976
|
+
try {
|
|
977
|
+
await tick(why);
|
|
978
|
+
} catch (e) {
|
|
979
|
+
log(`tick(${why}) error: ${e.stack || e.message}`);
|
|
980
|
+
} finally {
|
|
981
|
+
ticking = false;
|
|
982
|
+
if (again) { again = false; wake("re-run"); }
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
// Windows 不给孤儿进程重新认父:进程表里 ParentProcessId 仍是早已死去的老守护。
|
|
987
|
+
// 收割前用它验明正身——pid 会被复用,盲杀树可能杀掉无关进程。
|
|
988
|
+
// (进程观察原语在 lib/platform.js:win32 走排水版 PowerShell CIM,POSIX 走 ps。)
|
|
989
|
+
async function isOurChild(pid, parentPid) {
|
|
990
|
+
if (!Number.isInteger(parentPid)) return false;
|
|
991
|
+
const info = await getProcessInfo(pid);
|
|
992
|
+
return info.parentPid === parentPid;
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
async function main() {
|
|
996
|
+
// 单实例锁 = control 端口本身。bind 必须排在任何板上写入与定时器之前:
|
|
997
|
+
// 旧顺序下抢输的实例已经写了一条 session-start、也 armed 了整套调度器,
|
|
998
|
+
// 10-03 16:07:48/49/50 三实例并发各自抢占 state.json,rename 撞 EPERM。
|
|
999
|
+
try {
|
|
1000
|
+
await createControlServer(cfg, { store, wake, log, BASE });
|
|
1001
|
+
} catch (e) {
|
|
1002
|
+
log(`refused to start: control port unavailable (${e.code ?? e.message}) — another daemon holds it, exiting before any board write`);
|
|
1003
|
+
// rc=3 = "我抢输了",与 rc=1(崩溃)分开:run_daemon.bat 见 3 就自杀退场,
|
|
1004
|
+
// 否则每个抢输的循环会永远 10s 一轮地重烧一次几分钟的 node 冷启动。
|
|
1005
|
+
process.exit(3);
|
|
1006
|
+
}
|
|
1007
|
+
// single-instance lock: the bat restart-loop and the guard task may both
|
|
1008
|
+
// try to (re)start us; a second daemon would double-heartbeat and fight over runs.
|
|
1009
|
+
let prevPid = null;
|
|
1010
|
+
try {
|
|
1011
|
+
const prev = JSON.parse(readFileSync(join(BASE, "state.json"), "utf8"));
|
|
1012
|
+
prevPid = Number.isInteger(prev.pid) ? prev.pid : null;
|
|
1013
|
+
const pid = prev.pid;
|
|
1014
|
+
if (Number.isInteger(pid) && pid !== process.pid) {
|
|
1015
|
+
let ours = false;
|
|
1016
|
+
try { process.kill(pid, 0); ours = true; } catch { }
|
|
1017
|
+
if (ours) {
|
|
1018
|
+
// pid alive may be a recycled pid after reboot — confirm it really is this daemon
|
|
1019
|
+
const info = await getProcessInfo(pid);
|
|
1020
|
+
ours = !!info.commandLine && info.commandLine.includes("daemon.js");
|
|
1021
|
+
}
|
|
1022
|
+
if (ours) { console.log(`another daemon alive pid=${pid} — exiting`); process.exit(0); }
|
|
1023
|
+
}
|
|
1024
|
+
} catch { }
|
|
1025
|
+
log(`daemon start pid=${process.pid} dryRun=${cfg.runtime.dryRun} agents=[${enabledAgents.join(",")}] board=${cfg.board.dir}`);
|
|
1026
|
+
await board.init(cfg.board.package);
|
|
1027
|
+
store.s.pid = process.pid;
|
|
1028
|
+
store.s.startedAt = new Date().toISOString();
|
|
1029
|
+
// 必须立刻落盘:单实例锁与 guard 都以 state.json 的 pid 为唯一判据,
|
|
1030
|
+
// 晚一拍会让重启窗口里再起来一个守护,两条 run_daemon 循环并存。
|
|
1031
|
+
store.save();
|
|
1032
|
+
|
|
1033
|
+
// reap runs left behind by a previous daemon death
|
|
1034
|
+
for (const [key, r] of Object.entries({ ...store.s.runs })) {
|
|
1035
|
+
let alive = false;
|
|
1036
|
+
try { if (r.pid) process.kill(r.pid, 0); alive = !!r.pid; } catch { alive = false; }
|
|
1037
|
+
// 老守护一死就没人再读它的 stdout、也没人结算。留着它跑=卡已判死但 agent
|
|
1038
|
+
// 还在改工作目录;重派时两个 agent 同写一处。只在能证明它确实是本守护的子进程
|
|
1039
|
+
// 时才掐(pid 重启后会被复用,盲 taskkill 可能杀掉无关系统进程)。
|
|
1040
|
+
let killed = false;
|
|
1041
|
+
if (alive && await isOurChild(r.pid, prevPid)) {
|
|
1042
|
+
killTree(r.pid, { log });
|
|
1043
|
+
killed = true;
|
|
1044
|
+
log(`killed orphan child pid=${r.pid} of ${key}`);
|
|
1045
|
+
}
|
|
1046
|
+
store.endRun(key, "interrupted-by-daemon-restart");
|
|
1047
|
+
// 必须落盘:runs 不清掉,下次重启会再收割一次——那时这个 pid 可能已被复用。
|
|
1048
|
+
store.save();
|
|
1049
|
+
// 守护重启打断不是任务的错(瞬态类):接调度层自动重派(退避+上限防崩溃循环);
|
|
1050
|
+
// 重试用尽仍落 blocked 转人工。台账格式与 maybeRetry 扫描一致。
|
|
1051
|
+
const nowMs = Date.now();
|
|
1052
|
+
const rd = retryDecision({ why: "daemon-restart", attempts: store.s.retries?.[key]?.n ?? 0, maxAttempts: RETRY.max ?? 2, baseMs: RETRY.baseMs ?? 1800000, maxDelayMs: RETRY.maxMs ?? 14400000 });
|
|
1053
|
+
await board.withBoard(cfg.board, (b) => {
|
|
1054
|
+
const t = board.card(b, key)?.task;
|
|
1055
|
+
// 上一任守护可能已经结过案(跑完/判死)却没来得及清 runs。
|
|
1056
|
+
const unsettled = t && t.status !== "done" && t.status !== "blocked";
|
|
1057
|
+
const why = unsettled ? `FAILED 守护重启,运行被中断(子进程 ${r.pid} ${killed ? "已掐" : alive ? "仍在跑,需人工 kill" : "已退出"})` : "守护重启,运行被中断";
|
|
1058
|
+
const willRetry = unsettled && rd.retry;
|
|
1059
|
+
if (willRetry) {
|
|
1060
|
+
store.s.retries ??= {};
|
|
1061
|
+
store.s.retries[key] = { n: rd.attempt, dueAt: nowMs + rd.delayMs, agent: r.agent, why: "daemon-restart", lastAt: nowMs };
|
|
1062
|
+
store.save();
|
|
1063
|
+
}
|
|
1064
|
+
if (unsettled) board.setStatus(b, ID, key, willRetry ? "open" : "blocked");
|
|
1065
|
+
board.note(b, ID, `[${ID}→${r.agent}] ${key} ${why}${willRetry ? `;${left(rd.delayMs)} 后自动重派(第 ${rd.attempt}/${RETRY.max ?? 2} 次)` : ""} log=${r.dir}`);
|
|
1066
|
+
if (unsettled) board.message(b, ID, t.createdBy ?? cfg.owner,
|
|
1067
|
+
willRetry
|
|
1068
|
+
? `[${ID}] 任务 ${key} 因守护重启中断(非任务自身问题),${left(rd.delayMs)} 后自动重派(第 ${rd.attempt}/${RETRY.max ?? 2} 次)。日志: ${r.dir}`
|
|
1069
|
+
: `[${ID}] 任务 ${key} 因守护重启中断,已落 blocked;重派: node bridge-ctl.js requeue ${key} ${r.agent ?? ""}。日志: ${r.dir}`);
|
|
1070
|
+
}).catch((e) => log(`orphan write-back failed for ${key}: ${e.message}`));
|
|
1071
|
+
if (r.sessionId) {
|
|
1072
|
+
await board.withBoard(cfg.board, (b) => board.stopSession(b, r.sessionId), r.sessionId)
|
|
1073
|
+
.catch((e) => log(`orphan session stop failed for ${key}/${r.sessionId}: ${e.message}`));
|
|
1074
|
+
}
|
|
1075
|
+
log(`reaped orphan run ${key} (child alive=${alive} killed=${killed})`);
|
|
1076
|
+
emitSpan("gen_ai.agent.reap", { task: key, killed, willRetry: !!(rd && rd.retry) });
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
await board.withBoard(cfg.board, (b) => board.startSession(b, "agent-bridge daemon")).catch((e) => log(`session start failed: ${e.message}`));
|
|
1080
|
+
setInterval(() => {
|
|
1081
|
+
board.withBoard(cfg.board, (b) => {
|
|
1082
|
+
board.heartbeat(b); // daemon session
|
|
1083
|
+
for (const r of Object.values(store.s.runs)) {
|
|
1084
|
+
if (r.sessionId) board.heartbeat(b, r.sessionId);
|
|
1085
|
+
}
|
|
1086
|
+
}).catch((e) => log(`heartbeat failed: ${e.message}`));
|
|
1087
|
+
}, cfg.runtime.heartbeatMs ?? 60000);
|
|
1088
|
+
// @workbuddy re-poke 看门狗:抵消 worker 运行窗口对本戳的覆盖
|
|
1089
|
+
setInterval(() => {
|
|
1090
|
+
maybeRepoke().catch((e) => log(`re-poke tick error: ${e.stack || e.message}`));
|
|
1091
|
+
}, REPOKE.intervalMs);
|
|
1092
|
+
// 认领租约巡检:TTL 是小时级的事,10 分钟一扫足够;漏一拍由下一拍兜住
|
|
1093
|
+
setInterval(() => {
|
|
1094
|
+
maybeLease().catch((e) => log(`lease tick error: ${e.stack || e.message}`));
|
|
1095
|
+
}, LEASE.sweepMs ?? 600000);
|
|
1096
|
+
// 自动重派到期扫描(调度层 C.3)
|
|
1097
|
+
setInterval(() => {
|
|
1098
|
+
maybeRetry().catch((e) => log(`retry sweep error: ${e.stack || e.message}`));
|
|
1099
|
+
}, RETRY.sweepMs ?? 60000);
|
|
1100
|
+
// 逾期标红(C.4)与 owner 摘要(C.6)
|
|
1101
|
+
setInterval(() => {
|
|
1102
|
+
maybeSla().catch((e) => log(`sla tick error: ${e.stack || e.message}`));
|
|
1103
|
+
}, SLA.sweepMs ?? 300000);
|
|
1104
|
+
setInterval(() => {
|
|
1105
|
+
maybeDigest().catch((e) => log(`digest tick error: ${e.stack || e.message}`));
|
|
1106
|
+
}, DIGEST.tickMs ?? 600000);
|
|
1107
|
+
log(`sla+digest armed (sla sweep ${SLA.sweepMs ?? 300000}ms; digest every ${DIGEST.everyMs ?? 21600000}ms)`);
|
|
1108
|
+
log(`scheduler armed (retry max=${RETRY.max ?? 2} base=${RETRY.baseMs ?? 1800000}ms cap=${RETRY.maxMs ?? 14400000}ms sweep=${RETRY.sweepMs ?? 60000}ms; priority aging=${PRIORITY.agingMs ?? 7200000}ms)`);
|
|
1109
|
+
if (VERIFY.enabled !== false) log("verify V1 armed (acceptance: task column first, bridge-block fallback)");
|
|
1110
|
+
if (JUDGE.enabled) log(`judge V1.5 armed (agent=${JUDGE.agent ?? "dsh"}, mode=${JUDGE.mode ?? "acceptance-only"}; fail→V3, unavailable→skip)`);
|
|
1111
|
+
// 冻结自愈看门狗(2026-10-02 事故缺口④):worker 在独立线程里盯心跳,
|
|
1112
|
+
// 主线程事件循环冻结(任何 in-process 定时器都停摆)时由 worker SIGKILL 自杀,
|
|
1113
|
+
// run_daemon.bat 循环接手重启。graceMs 必须盖过启动期(收割孤儿 run 会静默跑几分钟)。
|
|
1114
|
+
// 2026-10-06 补 10-05 误杀的两道闸(决策核 lib/watchdog-core.mjs):
|
|
1115
|
+
// 闸一 worker 自测自己那一拍,迟到即「整进程被挂起过」→ 静默读数不可解释,清零不 I/O;
|
|
1116
|
+
// 闸二 两振后先探控制口,端口能答(哪怕慢)= 主循环还活着 → 弃权。
|
|
1117
|
+
// 所以这里必须把 port/tokenPath 传下去:不传的话 probePort() 只能保守弃权,
|
|
1118
|
+
// 冻结自愈会静默退化成「永远不杀」——比误杀更糟,因为它不报警。
|
|
1119
|
+
const WATCHDOG = cfg.runtime?.watchdog ?? {};
|
|
1120
|
+
if (WATCHDOG.enabled !== false) {
|
|
1121
|
+
try {
|
|
1122
|
+
const hb = WATCHDOG.heartbeatMs ?? 5000;
|
|
1123
|
+
const w = new Worker(new URL("./lib/watchdog-worker.mjs", import.meta.url), {
|
|
1124
|
+
workerData: {
|
|
1125
|
+
pid: process.pid,
|
|
1126
|
+
staleMs: WATCHDOG.staleMs ?? 90000,
|
|
1127
|
+
graceMs: WATCHDOG.graceMs ?? 120000,
|
|
1128
|
+
checkMs: WATCHDOG.checkMs ?? 10000,
|
|
1129
|
+
logFile: LOGF,
|
|
1130
|
+
port: cfg.http?.port ?? 8790,
|
|
1131
|
+
tokenPath: join(BASE, "control.token"),
|
|
1132
|
+
probeBudgetMs: WATCHDOG.probeBudgetMs ?? 120000, // 与 watchdog-worker 的同名默认保持一致(10-05 实测饥饿守护 36.989433s 才答 /ps)
|
|
1133
|
+
probeAttemptMs: WATCHDOG.probeAttemptMs ?? 8000,
|
|
1134
|
+
},
|
|
1135
|
+
});
|
|
1136
|
+
w.unref();
|
|
1137
|
+
setInterval(() => { try { w.postMessage("hb"); } catch { /* worker gone */ } }, hb).unref();
|
|
1138
|
+
const budget = WATCHDOG.probeBudgetMs ?? 120000;
|
|
1139
|
+
log(`watchdog armed (heartbeat ${hb}ms, stale ${WATCHDOG.staleMs ?? 90000}ms, grace ${WATCHDOG.graceMs ?? 120000}ms, port ${cfg.http?.port ?? 8790} probe budget ${budget}ms)`);
|
|
1140
|
+
} catch (e) { log(`watchdog start failed (running without freeze self-heal): ${e.message}`); }
|
|
1141
|
+
}
|
|
1142
|
+
createNotifier(cfg.board.dir, cfg.runtime, wake);
|
|
1143
|
+
wake("boot");
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
process.on("uncaughtException", (e) => log(`UNCAUGHT: ${e.stack || e.message}`));
|
|
1147
|
+
process.on("unhandledRejection", (e) => log(`UNHANDLED: ${e?.stack || e}`));
|
|
1148
|
+
main().catch((e) => { log(`fatal: ${e.stack || e.message}`); process.exit(1); });
|