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/lib/schedule.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
// Scheduling core (pure, no I/O) — priority lanes with aging + auto-retry policy.
|
|
2
|
+
// Part of the 2026-10-02 调度层 design (docs/design/2026-10-02-评估-*.md Part C).
|
|
3
|
+
// Daemon wiring lives in daemon.js/store.js; this module stays testable.
|
|
4
|
+
|
|
5
|
+
export const PRIORITY_INDEX = { P0: 0, P1: 1, P2: 2, P3: 3 };
|
|
6
|
+
|
|
7
|
+
// Unknown/absent → P2 so legacy tasks keep FIFO-within-same-rank behaviour.
|
|
8
|
+
export function normPriority(p, fallback = "P2") {
|
|
9
|
+
const k = String(p ?? "").toUpperCase();
|
|
10
|
+
return Object.prototype.hasOwnProperty.call(PRIORITY_INDEX, k) ? k : fallback;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Aging: every agingMs of waiting lifts the task one priority rank toward P0.
|
|
14
|
+
// A P3 task ranks as P2 after agingMs, P1 after 2*agingMs … floor at P0.
|
|
15
|
+
export function effectiveRank(basePriority, waitMs, agingMs) {
|
|
16
|
+
const base = PRIORITY_INDEX[normPriority(basePriority)] ?? 2;
|
|
17
|
+
const lift = Math.floor(Math.max(0, waitMs) / Math.max(1, agingMs));
|
|
18
|
+
return Math.max(0, base - lift);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Index of the lane entry to dispatch next: lowest effective rank wins, ties break
|
|
22
|
+
// FIFO by enqueue time (older first), then by lane position for stability.
|
|
23
|
+
// laneAt: key -> {at, priority} enqueue metadata (missing at ⇒ treated as now).
|
|
24
|
+
export function bestLaneIndex({ lane, laneAt = {}, priorityOf, now, agingMs }) {
|
|
25
|
+
if (!Array.isArray(lane) || lane.length === 0) return -1;
|
|
26
|
+
let best = -1;
|
|
27
|
+
let bestScore = null;
|
|
28
|
+
lane.forEach((key, i) => {
|
|
29
|
+
const meta = laneAt[key] ?? {};
|
|
30
|
+
const at = Number.isFinite(meta.at) ? meta.at : now;
|
|
31
|
+
const base = priorityOf ? priorityOf(key) : meta.priority;
|
|
32
|
+
const rank = effectiveRank(base, now - at, agingMs);
|
|
33
|
+
const score = [rank, at, i];
|
|
34
|
+
if (bestScore === null || score[0] < bestScore[0]
|
|
35
|
+
|| (score[0] === bestScore[0] && (score[1] < bestScore[1]
|
|
36
|
+
|| (score[1] === bestScore[1] && score[2] < bestScore[2])))) {
|
|
37
|
+
best = i;
|
|
38
|
+
bestScore = score;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return best;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Auto-retry policy for FAILED runs (调度层 C.3). Retryable = transient task-level
|
|
45
|
+
// failures (self-reported failure, rc≠0, stall-kill, timeout-kill). NOT retryable:
|
|
46
|
+
// human kill (a person decided), supply-breaker failures (the face's own recovery
|
|
47
|
+
// path owns those), face-adapter spawn errors (provider-side, not task-side).
|
|
48
|
+
// maxAttempts caps burn; backoff doubles per attempt and clamps at maxDelayMs.
|
|
49
|
+
export function retryDecision({
|
|
50
|
+
why = "", latch = null, killedBy = null, result = {},
|
|
51
|
+
attempts = 0, maxAttempts = 2, baseMs = 30 * 60000, maxDelayMs = 4 * 3600000,
|
|
52
|
+
} = {}) {
|
|
53
|
+
if (attempts >= maxAttempts) return { retry: false, reason: `已重试 ${attempts} 次达上限,转人工` };
|
|
54
|
+
if (latch) return { retry: false, reason: "供应方熔断类失败,归熔断恢复路径管,不自动重试" };
|
|
55
|
+
if (killedBy === "manual") return { retry: false, reason: "人工掐死,不自动重试" };
|
|
56
|
+
if (result.spawnError) return { retry: false, reason: "面适配器 spawn 失败(供应方侧),不自动重试" };
|
|
57
|
+
const delayMs = Math.min(baseMs * Math.pow(2, attempts), maxDelayMs);
|
|
58
|
+
return { retry: true, attempt: attempts + 1, delayMs };
|
|
59
|
+
}
|
package/lib/sla.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// SLA 逾期判定与 owner 摘要(调度层 C.4/C.6,2026-10-02 设计稿)——纯函数,可测。
|
|
2
|
+
// 逾期只做「升级可见性」:标红 + 提醒,不强制取消(v1 不给系统杀人权限)。
|
|
3
|
+
// 摘要把未读消息/待审批/逾期卡/重试排队/近期 blocked 摊成一条消息给 owner,摊薄裁决成本。
|
|
4
|
+
|
|
5
|
+
// 任务书 bridge 块的 dueAt(ISO 字符串或 epoch ms)。返回 null 表示未声明/不可解析。
|
|
6
|
+
export function parseDueAt(dueAt) {
|
|
7
|
+
if (typeof dueAt === "number" && Number.isFinite(dueAt)) return dueAt;
|
|
8
|
+
if (typeof dueAt === "string") {
|
|
9
|
+
const t = Date.parse(dueAt);
|
|
10
|
+
return Number.isFinite(t) ? t : null;
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 逾期判定:终态(done/blocked)不管;未到期不管;已提醒过(v1 一卡一次)不再提醒。
|
|
16
|
+
export function overdueDecision({ dueAt, status, now, notifiedAt }) {
|
|
17
|
+
const due = parseDueAt(dueAt);
|
|
18
|
+
if (due == null) return null;
|
|
19
|
+
if (status === "done" || status === "blocked") return null;
|
|
20
|
+
if (!Number.isFinite(now) || now <= due) return null;
|
|
21
|
+
if (notifiedAt) return null;
|
|
22
|
+
return { due, overdueMs: now - due };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// owner 摘要文本。入参均为已聚合好的数字/短列表;全空 → {empty:true}(调用方跳过发送)。
|
|
26
|
+
// coverage = { total, verified, unverified[] }:统计周期内新派卡的验收覆盖(§17 推广可见性)。
|
|
27
|
+
export function buildDigest({ unreadBy = {}, pending = [], overdue = [], retries = [], blockedRecent = [], running = [], coverage = null } = {}) {
|
|
28
|
+
const unreadTotal = Object.values(unreadBy).reduce((a, b) => a + b, 0);
|
|
29
|
+
const lines = [];
|
|
30
|
+
if (unreadTotal) lines.push(`· 未读消息 ${unreadTotal} 条(${Object.entries(unreadBy).map(([a, n]) => `${a}×${n}`).join("、")})`);
|
|
31
|
+
if (running.length) lines.push(`· 正在跑 ${running.length} 张:${running.slice(0, 5).join(", ")}${running.length > 5 ? "…" : ""}`);
|
|
32
|
+
if (pending.length) lines.push(`· 待审批 ${pending.length} 张:${pending.slice(0, 5).map((p) => `${p.key}(${p.reason ?? "需人批"})`).join(";")}${pending.length > 5 ? "…" : ""}`);
|
|
33
|
+
if (overdue.length) lines.push(`· 逾期 ${overdue.length} 张:${overdue.slice(0, 5).join(", ")}${overdue.length > 5 ? "…" : ""}(已标 [OVERDUE])`);
|
|
34
|
+
if (retries.length) lines.push(`· 自动重派排队 ${retries.length} 张:${retries.slice(0, 5).map((r) => `${r.key}(第 ${r.n} 次)`).join(";")}${retries.length > 5 ? "…" : ""}`);
|
|
35
|
+
if (blockedRecent.length) lines.push(`· 近期失败/阻塞 ${blockedRecent.length} 张:${blockedRecent.slice(0, 5).join(", ")}${blockedRecent.length > 5 ? "…" : ""}`);
|
|
36
|
+
if (coverage && coverage.total > 0) {
|
|
37
|
+
const pct = Math.round((coverage.verified / coverage.total) * 100);
|
|
38
|
+
lines.push(`· 验收覆盖(本期新卡):${coverage.verified}/${coverage.total}(${pct}%)${coverage.unverified?.length ? `;未验:${coverage.unverified.slice(0, 5).join(", ")}${coverage.unverified.length > 5 ? "…" : ""}(宪法 §17:补标准或声明 mode=human)` : ""}`);
|
|
39
|
+
}
|
|
40
|
+
if (!lines.length) return { empty: true, text: "" };
|
|
41
|
+
return { empty: false, text: `[bridge 摘要] 需要你知道的:\n${lines.join("\n")}\n(详情见板;本摘要周期性发送,可配置关闭)` };
|
|
42
|
+
}
|
package/lib/spans.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// spans(2026-10-07 采纳建议⑤):守护的关键动作产出 OTel GenAI 语义风格的 span,
|
|
2
|
+
// 落 logs/spans-YYYYMMDD.jsonl(本地 JSONL;将来接 Langfuse/Phoenix/OTLP 只是格式转换)。
|
|
3
|
+
// 命名对齐 open-telemetry/semantic-conventions-genai 的 gen_ai.* 约定(未 Stable,取其形):
|
|
4
|
+
// gen_ai.agent.dispatch {task, agent, attempt?}
|
|
5
|
+
// gen_ai.agent.verify {task, level, verdict, checks}
|
|
6
|
+
// gen_ai.agent.review {task, mode, verdict, reviewer}
|
|
7
|
+
// gen_ai.agent.retry {task, agent, attempt, delayMs, why}
|
|
8
|
+
// gen_ai.agent.breaker {agent, category, streak, holdMs}
|
|
9
|
+
// gen_ai.agent.reap {task, killed}
|
|
10
|
+
// gen_ai.agent.sla {task}
|
|
11
|
+
// span != LLM 调用级 tracing(那在 faces 各自的轨迹里);这里是编排层动作链。
|
|
12
|
+
import { appendFileSync, mkdirSync } from "node:fs";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
|
|
15
|
+
let SPANS_DIR = null;
|
|
16
|
+
let enabled = true;
|
|
17
|
+
|
|
18
|
+
export function initSpans({ dir, enabled: en = true }) {
|
|
19
|
+
SPANS_DIR = dir;
|
|
20
|
+
enabled = !!en;
|
|
21
|
+
if (enabled) {
|
|
22
|
+
try { mkdirSync(dir, { recursive: true }); } catch { /* 已存在 */ }
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function emitSpan(name, attrs = {}) {
|
|
27
|
+
if (!enabled || !SPANS_DIR) return;
|
|
28
|
+
try {
|
|
29
|
+
const rec = {
|
|
30
|
+
ts: new Date().toISOString(),
|
|
31
|
+
name,
|
|
32
|
+
...attrs,
|
|
33
|
+
};
|
|
34
|
+
appendFileSync(join(SPANS_DIR, `spans-${new Date().toISOString().slice(0, 10)}.jsonl`), JSON.stringify(rec) + "\n", "utf8");
|
|
35
|
+
} catch { /* 遥测绝不砸主流程 */ }
|
|
36
|
+
}
|
package/lib/starve.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// 输出预算饿死(第五类死因)的识别:一次 LLM 调用 finish_reason=length,且在落笔前就被切
|
|
2
|
+
// (content 空 **且** 无 tool_calls)⇒ 模型"想写"但一个字都没写出。连续 N 次即判死环路。
|
|
3
|
+
//
|
|
4
|
+
// 为什么只能事后判:这面的轨迹在进程结束时才落盘,跑到一半读不到 ⇒ 不能在段中途掐,
|
|
5
|
+
// 只能做到"识别出来就判死,并且不再起下一段"(续跑对一个纯配置缺陷是白烧)。
|
|
6
|
+
//
|
|
7
|
+
// 实测标本(2026-10-01,面级 max_tokens: 4096 时代的一次探针轮次):14 次调用里 6 次饿死
|
|
8
|
+
// (步 7/9/11/12/13/14),最长连续 4,每次 output_tokens 恰为 4096;抬到 32768 后同一面
|
|
9
|
+
// 8 段/35 步命中 0 次。
|
|
10
|
+
// 反面对照(不能误伤):另一仍配 4096 的面 success=true 却有 2 次饿死 ⇒ 阈值取 3,
|
|
11
|
+
// 恰好放过"偶发截断但整体做成"的正常轮次。
|
|
12
|
+
//
|
|
13
|
+
// 注意 usage.reasoning_tokens 在本机该面轨迹里恒为 0(供应方未报该字段),
|
|
14
|
+
// 所以判据不许依赖它——只看可观测的三件事:finish_reason / content / tool_calls。
|
|
15
|
+
|
|
16
|
+
export const STARVE_LIMIT = 3;
|
|
17
|
+
|
|
18
|
+
export function isStarved(response) {
|
|
19
|
+
if (!response || response.finish_reason !== "length") return false;
|
|
20
|
+
if (String(response.content ?? "").trim()) return false;
|
|
21
|
+
return !(response.tool_calls ?? []).length;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** 最长连续饿死次数(环路证据)。 */
|
|
25
|
+
export function maxStarveRun(interactions) {
|
|
26
|
+
let best = 0, cur = 0;
|
|
27
|
+
for (const x of interactions ?? []) {
|
|
28
|
+
if (isStarved(x?.response)) { cur += 1; if (cur > best) best = cur; }
|
|
29
|
+
else cur = 0;
|
|
30
|
+
}
|
|
31
|
+
return best;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function starveTotal(interactions) {
|
|
35
|
+
let n = 0;
|
|
36
|
+
for (const x of interactions ?? []) if (isStarved(x?.response)) n += 1;
|
|
37
|
+
return n;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 达阈值即真。给调用点一个唯一入口,免得两处各写一遍比较。 */
|
|
41
|
+
export function isStarveLoop(interactions, limit = STARVE_LIMIT) {
|
|
42
|
+
return maxStarveRun(interactions) >= limit;
|
|
43
|
+
}
|
package/lib/store.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// Atomic JSON state store for the bridge daemon.
|
|
2
|
+
import { readFileSync, writeFileSync, renameSync, mkdirSync, existsSync } from "node:fs";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
|
|
5
|
+
export class Store {
|
|
6
|
+
constructor(path) {
|
|
7
|
+
this.path = path;
|
|
8
|
+
this.state = this.#load();
|
|
9
|
+
}
|
|
10
|
+
#load() {
|
|
11
|
+
try {
|
|
12
|
+
if (existsSync(this.path)) return JSON.parse(readFileSync(this.path, "utf8"));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
console.error("store: unreadable state.json, starting fresh:", e.message);
|
|
15
|
+
}
|
|
16
|
+
return {
|
|
17
|
+
cursor: null,
|
|
18
|
+
pid: null,
|
|
19
|
+
startedAt: null,
|
|
20
|
+
runs: {}, // key -> {agent, dir, pid, startedAt, model}
|
|
21
|
+
finished: {},// key -> {at, result}
|
|
22
|
+
pending: {}, // key -> {agent, at, reason} (awaiting approval)
|
|
23
|
+
approved: {},// key -> {at} (one-shot human approval, consumed by handle)
|
|
24
|
+
lanes: {}, // agent -> [key,...]
|
|
25
|
+
laneAt: {}, // key -> {at, priority} 入队元数据(优先级排序与 aging 用)
|
|
26
|
+
retries: {}, // key -> {n, dueAt, agent, why, lastAt} 自动重派台账
|
|
27
|
+
supply: {}, // agent -> {category, streak, latchedAt, holdUntil, ...} 供应方熔断台账
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
save() {
|
|
31
|
+
const dir = dirname(this.path);
|
|
32
|
+
mkdirSync(dir, { recursive: true });
|
|
33
|
+
const tmp = `${this.path}.${process.pid}.tmp`;
|
|
34
|
+
writeFileSync(tmp, JSON.stringify(this.state, null, 1));
|
|
35
|
+
renameSync(tmp, this.path);
|
|
36
|
+
}
|
|
37
|
+
get s() { return this.state; }
|
|
38
|
+
|
|
39
|
+
// meta: {priority} —— assign 事件或调用方给的优先级(P0..P3),随入队记进 laneAt。
|
|
40
|
+
queue(key, agent, meta = {}) {
|
|
41
|
+
const lane = (this.state.lanes[agent] ??= []);
|
|
42
|
+
if (!lane.includes(key) && !this.state.runs[key] && !this.state.finished[key] && !this.state.pending[key]) {
|
|
43
|
+
lane.push(key);
|
|
44
|
+
(this.state.laneAt ??= {})[key] = { at: Date.now(), priority: meta.priority ?? null };
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
shiftLane(agent) {
|
|
50
|
+
const lane = this.state.lanes[agent] ?? [];
|
|
51
|
+
const key = lane.shift();
|
|
52
|
+
if (key) delete this.state.laneAt?.[key];
|
|
53
|
+
return key ?? null;
|
|
54
|
+
}
|
|
55
|
+
// 优先级出队(调度层 C.2):按 schedule.bestLaneIndex 选最优一张并出队。
|
|
56
|
+
// priorityOf(key) 由调用方提供(assign 时的 laneAt 记录 → 卡 payload → 缺省 P2)。
|
|
57
|
+
pickBest(agent, priorityOf, now, agingMs, bestLaneIndex) {
|
|
58
|
+
const lane = this.state.lanes[agent] ?? [];
|
|
59
|
+
const i = bestLaneIndex({ lane, laneAt: this.state.laneAt ?? {}, priorityOf, now, agingMs });
|
|
60
|
+
if (i < 0) return null;
|
|
61
|
+
const [key] = lane.splice(i, 1);
|
|
62
|
+
delete this.state.laneAt?.[key];
|
|
63
|
+
return key ?? null;
|
|
64
|
+
}
|
|
65
|
+
// queue() 会对「已结案」的 key 静默返回 false——重派一张已完成的卡必须先清 finished。
|
|
66
|
+
// 把这条规则收在一个地方,免得调用方各写一遍删键、漏掉 pending。
|
|
67
|
+
requeue(key, agent, meta = {}) {
|
|
68
|
+
if (this.state.runs[key]) return { ok: false, why: "该卡正在运行" };
|
|
69
|
+
delete this.state.finished[key];
|
|
70
|
+
delete this.state.pending[key];
|
|
71
|
+
return this.queue(key, agent, meta)
|
|
72
|
+
? { ok: true }
|
|
73
|
+
: { ok: false, why: `已在 ${agent} 的队列里` };
|
|
74
|
+
}
|
|
75
|
+
runningCount() { return Object.keys(this.state.runs).length; }
|
|
76
|
+
runsFor(agent) { return Object.values(this.state.runs).filter((r) => r.agent === agent).length; }
|
|
77
|
+
laneIdle(agent) { return !Object.values(this.state.runs).some(r => r.agent === agent); }
|
|
78
|
+
|
|
79
|
+
// ---- 供应方熔断台账(#25)--------------------------------------------------
|
|
80
|
+
// 台账按面存,不按供应方存:同一供应商的不同面(pi 与 dsh 都走 deepseek)在本机
|
|
81
|
+
// 配置里没有可比对的供应方标识,宁可少冻一面,也不要错冻三面。
|
|
82
|
+
supplyAll() { return (this.state.supply ??= {}); }
|
|
83
|
+
supplyGet(agent) { return this.supplyAll()[agent] ?? null; }
|
|
84
|
+
// streak = 这一面上「连续」的供应方失败次数,只在跑成功一次或人工 clear 时归零
|
|
85
|
+
// (归零走 supplyRelease)。holdUntil 过期不算归零:探针又撞同样的墙就该继续加倍退避。
|
|
86
|
+
supplyLatch(agent, info) {
|
|
87
|
+
const all = this.supplyAll();
|
|
88
|
+
const streak = (all[agent]?.streak ?? 0) + 1;
|
|
89
|
+
all[agent] = { ...info, streak, latchedAt: Date.now(), holdUntil: Date.now() + info.holdMs };
|
|
90
|
+
return all[agent];
|
|
91
|
+
}
|
|
92
|
+
supplyRelease(agent) {
|
|
93
|
+
const all = this.supplyAll();
|
|
94
|
+
const prev = all[agent] ?? null;
|
|
95
|
+
delete all[agent];
|
|
96
|
+
return prev;
|
|
97
|
+
}
|
|
98
|
+
supplyHeld(agent, now = Date.now()) {
|
|
99
|
+
const rec = this.supplyGet(agent);
|
|
100
|
+
return !!rec && rec.holdUntil > now;
|
|
101
|
+
}
|
|
102
|
+
beginRun(key, info) { this.state.runs[key] = info; }
|
|
103
|
+
endRun(key, result, agent) {
|
|
104
|
+
const r = this.state.runs[key] ?? {};
|
|
105
|
+
delete this.state.runs[key];
|
|
106
|
+
// 记下 agent:requeue 默认沿用上一次的面,不必人工再指一次。
|
|
107
|
+
this.state.finished[key] = { at: new Date().toISOString(), result, agent: agent ?? r.agent ?? null };
|
|
108
|
+
}
|
|
109
|
+
}
|
package/lib/supply.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Supply-error sentinel: tell "the model/face is broken" apart from
|
|
2
|
+
// "the API key/credit behind it is gone", from the artifacts a run left behind.
|
|
3
|
+
//
|
|
4
|
+
// Why this exists (measured, 2026-09-26, one long-running task): the task burned a
|
|
5
|
+
// full dispatch and the board only said `FAILED rc=0` — trae exits 0 even when
|
|
6
|
+
// every LLM call returned 402. The real cause sat in one place:
|
|
7
|
+
// trae.traj.json -> agent_steps[0].error =
|
|
8
|
+
// "Error code: 402 - {'error': {'message': 'Insufficient Balance (request_id: …)' …"
|
|
9
|
+
// rc=0 + a text-free failure note is the worst case for a dispatcher: it looks
|
|
10
|
+
// retryable, so the same dead key gets re-fed task after task.
|
|
11
|
+
//
|
|
12
|
+
// We scan the run *directory* rather than hooking each adapter: a new face then
|
|
13
|
+
// needs no adapter change to be covered, and the wording usually reaches disk in
|
|
14
|
+
// at least one artifact (jsonl / trajectory / stderr).
|
|
15
|
+
//
|
|
16
|
+
// Deliberately NOT matched: bare digits. `402` alone fires on step counts, byte
|
|
17
|
+
// counts and port numbers all over these files, so every numeric rule needs
|
|
18
|
+
// error-shaped context. The phrase rules carry their own context.
|
|
19
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
20
|
+
import { join } from "node:path";
|
|
21
|
+
|
|
22
|
+
export const SUPPLY_RULES = [
|
|
23
|
+
// category=billing — spend what you have, get told no. Retrying is pure loss.
|
|
24
|
+
{ category: "billing", label: "Insufficient Balance", re: /insufficient\s*balance/i },
|
|
25
|
+
{ category: "billing", label: "insufficient_quota", re: /insufficient[_\s]quota/i },
|
|
26
|
+
{ category: "billing", label: "余额不足", re: /余额不足|欠费|额度不足/ },
|
|
27
|
+
{ category: "billing", label: "quota exceeded", re: /quota\s*(exceeded|exhausted|exceeded)/i },
|
|
28
|
+
{ category: "billing", label: "billing hard limit", re: /billing\s*hard\s*limit|out\s*of\s*credits/i },
|
|
29
|
+
{ category: "billing", label: "status 402", re: /\b(?:error\s*code|status(?:\s*code)?|http)\D{0,12}\b402\b/i },
|
|
30
|
+
// 订阅额度到顶:实测 codex 2026-09-29 回「You've hit your usage limit … try again at Oct 4th」,
|
|
31
|
+
// 不给 402 也不给 429,只给一句人话 ⇒ 不进表就会每张卡重试一次,一直重试到恢复日。
|
|
32
|
+
{ category: "billing", label: "usage limit(订阅额度到顶)", re: /usage\s*limit|hit\s*your\s*limit/i },
|
|
33
|
+
|
|
34
|
+
// category=auth — key missing/rotated/wrong audience. Also not retryable, but
|
|
35
|
+
// fixed by a human action rather than by buying credit.
|
|
36
|
+
{ category: "auth", label: "invalid api key", re: /invalid\s+api\s*key|incorrect\s+api\s*key|invalid_api_key/i },
|
|
37
|
+
{ category: "auth", label: "unauthorized", re: /\bunauthorized\b/i },
|
|
38
|
+
{ category: "auth", label: "authentication error", re: /authentication\s*(error|failed|problem)|missing authentication/i },
|
|
39
|
+
{ category: "auth", label: "status 401", re: /\b(?:error\s*code|status(?:\s*code)?|http)\D{0,12}\b401\b/i },
|
|
40
|
+
{ category: "auth", label: "status 403", re: /\b(?:error\s*code|status(?:\s*code)?|http)\D{0,12}\b403\b/i },
|
|
41
|
+
|
|
42
|
+
// category=rate — transient by design: hold, then retry.
|
|
43
|
+
{ category: "rate", label: "rate limit", re: /rate\s*limit|too\s*many\s*requests|throttl/i },
|
|
44
|
+
{ category: "rate", label: "status 429", re: /\b(?:error\s*code|status(?:\s*code)?|http)\D{0,12}\b429\b/i },
|
|
45
|
+
];
|
|
46
|
+
|
|
47
|
+
const TEXT_EXT = /\.(jsonl|json|txt|log|md|yaml|yml|err|out)$/i;
|
|
48
|
+
const FILE_CAP = 2 * 1024 * 1024; // per file: sentinels must not become an I/O tax
|
|
49
|
+
const SCAN_CAP = 8 * 1024 * 1024; // per run dir, in file order
|
|
50
|
+
const LINE_MAX = 400;
|
|
51
|
+
|
|
52
|
+
const oneLine = (s) => s.replace(/\s+/g, " ").trim().slice(0, LINE_MAX);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* scanRunDir(runDir) -> {category, label, file, line} | null
|
|
56
|
+
* First rule that hits wins; numeric rules carry their own context so a match is
|
|
57
|
+
* quotable evidence, not a guess.
|
|
58
|
+
*/
|
|
59
|
+
export function scanRunDir(runDir) {
|
|
60
|
+
let names;
|
|
61
|
+
try { names = readdirSync(runDir); } catch { return null; }
|
|
62
|
+
let budget = SCAN_CAP;
|
|
63
|
+
for (const name of names) {
|
|
64
|
+
if (budget <= 0) break;
|
|
65
|
+
let size = 0;
|
|
66
|
+
try {
|
|
67
|
+
const st = statSync(join(runDir, name));
|
|
68
|
+
if (!st.isFile() || !TEXT_EXT.test(name)) continue;
|
|
69
|
+
size = st.size;
|
|
70
|
+
} catch { continue; }
|
|
71
|
+
const take = Math.min(size, FILE_CAP, budget);
|
|
72
|
+
budget -= take;
|
|
73
|
+
let text;
|
|
74
|
+
try { text = readFileSync(join(runDir, name), { encoding: "utf8" }).slice(0, take); } catch { continue; }
|
|
75
|
+
for (const rule of SUPPLY_RULES) {
|
|
76
|
+
const m = rule.re.exec(text);
|
|
77
|
+
if (!m) continue;
|
|
78
|
+
// widen the hit into its surrounding line so the board text is verifiable
|
|
79
|
+
const at = m.index;
|
|
80
|
+
const from = Math.max(0, text.lastIndexOf("\n", at - 1) + 1);
|
|
81
|
+
const to = (() => { const i = text.indexOf("\n", at); return i < 0 ? text.length : i; })();
|
|
82
|
+
return { category: rule.category, label: rule.label, file: name, line: oneLine(text.slice(from, to)) || oneLine(m[0]) };
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Human sentence for the board. Keeps the quoted evidence line, no editorialising. */
|
|
89
|
+
export function supplyReason(hit) {
|
|
90
|
+
if (!hit) return null;
|
|
91
|
+
const head = hit.category === "billing" ? "供应方额度/账单耗尽"
|
|
92
|
+
: hit.category === "auth" ? "供应方鉴权失败(key 失效或未配)"
|
|
93
|
+
: hit.category === "rate" ? "供应方限流"
|
|
94
|
+
: hit.category;
|
|
95
|
+
return `${head}〔${hit.label}〕`;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// 时长写法:不足 90 秒一律用秒。板与日志里出现「暂停派发 0 分钟」会被读成「根本没冻」,
|
|
99
|
+
// 出现「剩 1min」而实为 5 秒会让人怀疑 hold 逻辑没生效。
|
|
100
|
+
export const hold = (ms) => (ms < 90000 ? `${Math.round(ms / 1000)}s` : `${Math.round(ms / 60000)}min`);
|
|
101
|
+
export const left = (ms) => (ms < 90000 ? `${Math.max(1, Math.ceil(ms / 1000))}s` : `${Math.ceil(ms / 60000)}min`);
|
package/lib/usage.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
// 用量台账 — 每个 run 追加一行到 usage.jsonl,供成本统计与派单前预算校验消费。
|
|
2
|
+
//
|
|
3
|
+
// 一条硬规则:**缺字段绝不静默记 0**。0 和「没采到」在账上是两回事——记 0 会把
|
|
4
|
+
// 「这一面根本没花钱」伪装成「这一面很省」,而预算校验正是靠这个数决定要不要再派。
|
|
5
|
+
// 所以读不到权威产物时写 {missing:true, reason:…},数值字段一律 null。
|
|
6
|
+
//
|
|
7
|
+
// 归一化口径的出处(2026-09-29 对 19 个历史 run 实测,不是猜字段名):
|
|
8
|
+
// codex codex.jsonl 里唯一一条 type="turn.completed" 的顶层 usage:
|
|
9
|
+
// input_tokens 已含 cached_input_tokens(OpenAI Responses 口径,观测 456448 ≤ 540309 一致)
|
|
10
|
+
// pi pi.jsonl 每条 assistant message_end 的 message.usage,逐轮相加:
|
|
11
|
+
// input 是**不含**缓存的那一段 ⇒ 有 totalTokens 可自证:10358+2760+25856=38974
|
|
12
|
+
// trae trae.traj.json 的 llm_interactions[*].response.usage,逐次调用相加
|
|
13
|
+
// dsh dsh-events.jsonl 每条 type="assistant/message" 的 data.usage,逐轮相加(steps 走 dsh-meta.json)
|
|
14
|
+
// prompt 一律归一成「含缓存的全部输入」,方便跨面比;各面原始字段同时留在 raw 里,归一化不覆盖事实。
|
|
15
|
+
import { appendFileSync, readFileSync, existsSync } from "node:fs";
|
|
16
|
+
|
|
17
|
+
export const LEDGER_FIELDS = ["prompt", "completion", "cached", "cacheWrite", "reasoning"];
|
|
18
|
+
|
|
19
|
+
/** 把某面读到的原始 usage 归一;u 为 null ⇒ 判红(missing),不产生 0。 */
|
|
20
|
+
export function normalizeUsage(agent, u) {
|
|
21
|
+
if (!u) return missingUsage(agent, "权威产物不存在或解析不了(跑到一半被掐、或该面这轮没落盘)");
|
|
22
|
+
const n = u.normalized ?? {};
|
|
23
|
+
if (LEDGER_FIELDS.every((k) => n[k] == null)) {
|
|
24
|
+
// 产物读得通、就是没给用量:步数等有的是知道的,别一起丢进 null,结案时能说清缺在哪。
|
|
25
|
+
return { ...missingUsage(agent, `产物在但没给用量字段(source=${u.source ?? "?"})`),
|
|
26
|
+
source: u.source ?? null, steps: u.steps ?? null, maxSteps: u.maxSteps ?? null, overflow: u.overflow ?? null };
|
|
27
|
+
}
|
|
28
|
+
// fail-closed(第二道):能走到这里,说明某面的 reader 声称「读到了用量」。但若五项加
|
|
29
|
+
// total 全为 0、且没有 knownZero 的正当理由,这就不是「真的一次计费都没发生」,
|
|
30
|
+
// 而是读取器交了一份默认零值(空对象也是 truthy,pi 面上就这么踩过——见 adapters/pi.js)。
|
|
31
|
+
// 0 在账上会被读成「这面没花钱」,而预算校验正是靠这个数决定还要不要往这面派 ⇒ 降级 missing。
|
|
32
|
+
const allZero = LEDGER_FIELDS.every((k) => (n[k] ?? 0) === 0) && (u.total ?? 0) === 0;
|
|
33
|
+
if (allZero && !u.knownZero) {
|
|
34
|
+
return { ...missingUsage(agent, `读到的用量字段全为 0(source=${u.source ?? "?"}),无法区分「真零次计费」与「读取器给了默认零值」——按铁律不静默记 0`),
|
|
35
|
+
source: u.source ?? null, steps: u.steps ?? null, maxSteps: u.maxSteps ?? null, overflow: u.overflow ?? null };
|
|
36
|
+
}
|
|
37
|
+
const prompt = n.prompt ?? null;
|
|
38
|
+
const completion = n.completion ?? null;
|
|
39
|
+
return {
|
|
40
|
+
agent,
|
|
41
|
+
source: u.source ?? null,
|
|
42
|
+
steps: u.steps ?? null,
|
|
43
|
+
maxSteps: u.maxSteps ?? null,
|
|
44
|
+
overflow: u.overflow ?? null,
|
|
45
|
+
// knownZero=true:产物可读、确实一次计费调用都没发生(区别于 missing=读不到、不知道)。
|
|
46
|
+
knownZero: u.knownZero ?? false,
|
|
47
|
+
zeroReason: u.zeroReason ?? null,
|
|
48
|
+
prompt,
|
|
49
|
+
completion,
|
|
50
|
+
cached: n.cached ?? null,
|
|
51
|
+
cacheWrite: n.cacheWrite ?? null,
|
|
52
|
+
reasoning: n.reasoning ?? null,
|
|
53
|
+
// total 是派生值(prompt+completion),面自己给了总数时以面的为准。
|
|
54
|
+
total: u.total ?? (prompt != null && completion != null ? prompt + completion : null),
|
|
55
|
+
costUsd: u.costUsd ?? null,
|
|
56
|
+
semantics: u.semantics ?? null,
|
|
57
|
+
raw: u.raw ?? null,
|
|
58
|
+
missing: false,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function missingUsage(agent, reason) {
|
|
63
|
+
return {
|
|
64
|
+
agent, missing: true, reason,
|
|
65
|
+
source: null, steps: null, maxSteps: null, overflow: null,
|
|
66
|
+
prompt: null, completion: null, cached: null, cacheWrite: null,
|
|
67
|
+
reasoning: null, total: null, costUsd: null, semantics: null, raw: null,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** 追加一行;file 由调用方给(守护用 BASE 下的 usage.jsonl,冒烟用隔离目录)。 */
|
|
72
|
+
export function appendUsage(file, entry) {
|
|
73
|
+
appendFileSync(file, JSON.stringify(entry) + "\n");
|
|
74
|
+
return entry;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readUsageLedger(file) {
|
|
78
|
+
if (!existsSync(file)) return [];
|
|
79
|
+
const rows = [];
|
|
80
|
+
for (const line of readFileSync(file, "utf8").split("\n")) {
|
|
81
|
+
if (!line.trim()) continue;
|
|
82
|
+
try { rows.push(JSON.parse(line)); } catch { /* 末行可能被写断,跳过不影响其余 */ }
|
|
83
|
+
}
|
|
84
|
+
return rows;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** 按面汇总窗口内的用量,给预算校验用。missing 行计入 nMissing,不计入任何 token 数。 */
|
|
88
|
+
export function summarizeUsage(rows, agent, sinceTs = 0) {
|
|
89
|
+
const s = { agent, runs: 0, nMissing: 0, prompt: 0, completion: 0, total: 0, costUsd: 0, maxStepsHit: 0, lastTs: null };
|
|
90
|
+
for (const r of rows) {
|
|
91
|
+
if (r.agent !== agent) continue;
|
|
92
|
+
if (sinceTs && r.ts && r.ts < sinceTs) continue;
|
|
93
|
+
s.runs++;
|
|
94
|
+
s.lastTs = r.ts ?? s.lastTs;
|
|
95
|
+
if (r.missing) { s.nMissing++; continue; }
|
|
96
|
+
s.prompt += r.prompt ?? 0;
|
|
97
|
+
s.completion += r.completion ?? 0;
|
|
98
|
+
s.total += r.total ?? 0;
|
|
99
|
+
if (r.costUsd != null) s.costUsd += r.costUsd;
|
|
100
|
+
if (r.overflow === true) s.maxStepsHit++;
|
|
101
|
+
}
|
|
102
|
+
return s;
|
|
103
|
+
}
|