Initial import of streamline
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* The full `<category>:<kind>` → card map, built once from the shared fixtures.
|
||||
* Used by both preview scripts so the name set can't drift between "render" and
|
||||
* "send".
|
||||
*
|
||||
* Samples are STAGE-ACCURATE, not just status-relabelled: a real row has no
|
||||
* mrUrl / fixedRisk / branchName until the fix run has actually produced them,
|
||||
* and the card's field list is `.filter(value)`-driven, so previewing a
|
||||
* `pending_confirmation` card with a populated MR would show a layout that can
|
||||
* never occur in production — and hide the sparse one that will.
|
||||
*/
|
||||
import {
|
||||
buildCard, buildFeatureCard, buildSecurityCard,
|
||||
meta, featureMeta, securityMeta,
|
||||
type NotifyKind, type FeatureNotifyKind, type SecurityNotifyKind,
|
||||
} from "../control-server/src/notify_cards";
|
||||
import { sampleTask, sampleFeature, sampleSecurity, FAILURE_REASON } from "./_notify-preview-fixtures";
|
||||
|
||||
/** How far along the lifecycle a status is, and therefore which fields exist. */
|
||||
type Stage = "pre-eval" | "planned" | "running" | "done" | "failed";
|
||||
|
||||
const BUG_STAGE: Record<NotifyKind, Stage> = {
|
||||
info_required: "pre-eval",
|
||||
pending_confirmation: "planned",
|
||||
dispatched: "running",
|
||||
manual_review: "done",
|
||||
fix_succeeded: "done",
|
||||
awaiting_merge: "done",
|
||||
fix_failed: "failed",
|
||||
unreproducible: "failed",
|
||||
};
|
||||
const FEATURE_STAGE: Record<FeatureNotifyKind, Stage> = {
|
||||
info_required: "pre-eval",
|
||||
pending_confirmation: "planned",
|
||||
dispatched: "running",
|
||||
manual_review: "done",
|
||||
completed: "done",
|
||||
implementation_failed: "failed",
|
||||
};
|
||||
const SECURITY_STAGE: Record<SecurityNotifyKind, Stage> = {
|
||||
info_required: "pre-eval",
|
||||
dispatched: "running",
|
||||
manual_review: "done",
|
||||
completed: "done",
|
||||
unreproducible: "failed",
|
||||
security_failed: "failed",
|
||||
};
|
||||
|
||||
/** Blank out everything a row of this stage would not yet have. */
|
||||
function atStage<T extends Record<string, unknown>>(row: T, status: string, stage: Stage, reason: string): T {
|
||||
const base = { ...row, status } as Record<string, unknown>;
|
||||
if (stage === "pre-eval" || stage === "planned") {
|
||||
Object.assign(base, {
|
||||
mrUrl: null, branchName: null, fixedRisk: null, failureReason: null,
|
||||
attempts: 0, durationMs: null, totalDurationMs: null,
|
||||
// The eval claim stamps executorNodeId, so a planned row does have a node;
|
||||
// an info_required row was bounced by that same eval, so it has one too.
|
||||
executionPlan: stage === "planned" ? row.executionPlan : null,
|
||||
});
|
||||
} else if (stage === "running") {
|
||||
Object.assign(base, { mrUrl: null, branchName: null, fixedRisk: null, failureReason: null, attempts: 1 });
|
||||
} else if (stage === "failed") {
|
||||
Object.assign(base, { fixedRisk: null, failureReason: reason, attempts: 6 });
|
||||
}
|
||||
return base as T;
|
||||
}
|
||||
|
||||
export const CARDS: Record<string, () => unknown> = {};
|
||||
|
||||
for (const k of Object.keys(BUG_STAGE) as NotifyKind[]) {
|
||||
const row = atStage(sampleTask, k, BUG_STAGE[k], FAILURE_REASON.bug);
|
||||
CARDS[`bug:${k}`] = () => buildCard(row, meta(k));
|
||||
}
|
||||
for (const k of Object.keys(FEATURE_STAGE) as FeatureNotifyKind[]) {
|
||||
const row = atStage(sampleFeature, k, FEATURE_STAGE[k], FAILURE_REASON.feature);
|
||||
CARDS[`feature:${k}`] = () => buildFeatureCard(row, featureMeta(k));
|
||||
}
|
||||
for (const k of Object.keys(SECURITY_STAGE) as SecurityNotifyKind[]) {
|
||||
const row = atStage(sampleSecurity, k, SECURITY_STAGE[k], FAILURE_REASON.security);
|
||||
CARDS[`security:${k}`] = () => buildSecurityCard(row, securityMeta(k));
|
||||
}
|
||||
|
||||
/**
|
||||
* The ENRICHED review card — what a manual_review card becomes once the readable
|
||||
* report lands and `refreshReviewCard` patches it: 大白话 up top, one button
|
||||
* at the bottom.
|
||||
*
|
||||
* Worth its own fixture because it is the state nobody sees while developing:
|
||||
* the plain version is what a fresh run produces, and the enriched one only
|
||||
* appears minutes later on a task that happens to have mirrored evidence. It is
|
||||
* also the layout most likely to break — the summary block is the only element
|
||||
* that sits ABOVE the metadata grid.
|
||||
*
|
||||
*/
|
||||
const PLAIN_SUMMARY =
|
||||
"点赞按钮在深色模式下几乎看不见,是因为它一直用固定的浅色透明度,没有跟随主题切换。"
|
||||
+ "改成读取当前主题的前景色后,深浅两种模式下都能看清,其它页面的按钮样式不受影响。";
|
||||
|
||||
CARDS["bug:manual_review:enriched"] = () => buildCard(
|
||||
atStage(sampleTask, "manual_review", "done", FAILURE_REASON.bug),
|
||||
meta("manual_review"),
|
||||
{
|
||||
plain: PLAIN_SUMMARY,
|
||||
},
|
||||
);
|
||||
|
||||
/** 开放平台建的单:顶部多一格「开放平台 · 申请人」,字段区多一行外部单号。 */
|
||||
CARDS["bug:dispatched:openplatform"] = () => buildCard(
|
||||
{
|
||||
...atStage(sampleTask, "dispatched", BUG_STAGE.dispatched, FAILURE_REASON.bug),
|
||||
meegoId: null, meegoUrl: null,
|
||||
createdViaKeyId: "opk_preview", createdViaSource: "jira-crm", createdViaLabel: "CRM 工单同步",
|
||||
createdViaOwner: "Yifan Jiang", externalId: "CRM-10293",
|
||||
},
|
||||
meta("dispatched"),
|
||||
);
|
||||
|
||||
CARDS["feature:manual_review:enriched"] = () => buildFeatureCard(
|
||||
atStage(sampleFeature, "manual_review", "done", FAILURE_REASON.feature),
|
||||
featureMeta("manual_review"),
|
||||
{
|
||||
plain: "访客进入他人主页时,现在能直接看到「预览」入口并进入预览页;自己的主页不受影响,原有入口保持不变。",
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,89 @@
|
||||
/**
|
||||
* Sample Task / Feature / Security rows for previewing notification cards.
|
||||
* Shared by `preview-notify-cards.ts` (render to stdout) and
|
||||
* `send-notify-preview.ts` (send through the real bot) so the two can't drift.
|
||||
*
|
||||
* Deliberately realistic rather than minimal: long CJK titles, a plan long
|
||||
* enough to exercise the excerpt truncation, and a populated MR / node / risk /
|
||||
* rounds set — those are the layout's actual stress cases.
|
||||
*/
|
||||
import type { Task, Feature, Security } from "../control-server/src/types";
|
||||
|
||||
const now = Date.now();
|
||||
|
||||
const TEST_SPEC = `## 复现条件 // Reproduce condition
|
||||
- 账号/环境:
|
||||
- abid: 23166598(用 ab-clone-skill 应用到当前设备)
|
||||
- 需登录一个金币余额 > 0 的账号,且该账号未走过预领奖流程
|
||||
- 入口路径:首页 → 侧边栏 → UG 活动页 → 任选一个「余额送礼」任务
|
||||
|
||||
## 预期 // Expected
|
||||
完成任务后,仅展示「任务完成」toast;不得出现「兑换完成且礼物已送出」。
|
||||
|
||||
## 实际 // Actual
|
||||
完成任务后误弹「兑换完成且礼物已送出」toast,但礼物实际并未送出。
|
||||
|
||||
## 验证要点 // Verification
|
||||
1. 余额 > 0 且未预领奖 → 不应出现兑换 toast
|
||||
2. 余额 = 0 → 行为保持不变(回归项)
|
||||
3. 已预领奖账号 → 兑换 toast 应正常出现`;
|
||||
|
||||
export const sampleTask = {
|
||||
id: "preview-bug", meegoId: "7357099854",
|
||||
meegoUrl: "https://meego.feishu.cn/story/detail/7357099854",
|
||||
title: "[直播打赏][strategy-ai 自动提单][UG任务] 有金币余额时,UG活动页用余额送礼(未走预领奖)完成任务后误弹\"兑换完成且礼物已送出\"toast",
|
||||
bugType: "ui", platform: "android", difficulty: "high", stepsClarity: "clear",
|
||||
executionPlan: TEST_SPEC,
|
||||
mrUrl: "https://bits.bytedance.net/devops/1499114242/code/detail/8216797",
|
||||
branchName: "bugfix/ug-balance-gift-toast",
|
||||
executorNodeId: "fei-android-01.shiro0.icu",
|
||||
fixedRisk: 50, attempts: 4, totalDurationMs: 1994758, durationMs: 1994758,
|
||||
failureReason: null,
|
||||
// The `oc_` entry is the shared report group — present on every real row, and
|
||||
// deliberately filtered out of the watcher chips.
|
||||
watchers: ["qifan.zhang@bytedance.com", "jiaopengwen@bytedance.com", "oc_91dcbdb498fd9e70ab23f8f7727ac44e"],
|
||||
status: "manual_review", createdAt: now,
|
||||
} as unknown as Task;
|
||||
|
||||
export const sampleFeature = {
|
||||
id: "preview-feature", platform: "ios",
|
||||
title: "[评论区] 支持长按评论快捷翻译,并在气泡内展示原文/译文切换入口",
|
||||
executionPlan: `## 实现方案
|
||||
1. 在 \`CommentCell\` 的长按菜单中新增「翻译」项,命中后调用 \`TranslationService\`
|
||||
2. 译文以第二行形式插入气泡内部,保留原文并提供「显示原文 / 显示译文」切换
|
||||
3. 翻译结果按 commentId 做内存缓存,避免重复请求
|
||||
|
||||
## 验收
|
||||
- 长按任意评论可见「翻译」入口
|
||||
- 切换原文/译文不引起 cell 高度跳变
|
||||
- 无网络时降级为 toast 提示,不阻塞菜单`,
|
||||
mrUrl: "https://bits.bytedance.net/devops/1499114242/code/detail/8300112",
|
||||
branchName: "feature/comment-quick-translate",
|
||||
executorNodeId: "fei-ios-02.shiro0.icu",
|
||||
attempts: 2, totalDurationMs: 903000, durationMs: 903000,
|
||||
watchers: ["zeyu.zyzhang@bytedance.com", "oc_91dcbdb498fd9e70ab23f8f7727ac44e"],
|
||||
failureReason: null,
|
||||
status: "manual_review", createdAt: now,
|
||||
} as unknown as Feature;
|
||||
|
||||
export const sampleSecurity = {
|
||||
id: "preview-security", platform: "android",
|
||||
title: "[隐私合规] 冷启动阶段在用户授权前读取 IMEI / MAC 的调用链搜证",
|
||||
executionPlan: `## 搜证方案
|
||||
1. 使用 frida hook \`TelephonyManager#getDeviceId\` 与 \`NetworkInterface#getHardwareAddress\`
|
||||
2. 冷启动至隐私弹窗出现为止,全程记录调用栈
|
||||
3. 对每一条命中记录调用方包名、线程与时间戳,产出 EvidenceReport`,
|
||||
targetRef: "release/32.5.0",
|
||||
executorNodeId: "pns-android-01.shiro0.icu",
|
||||
attempts: 1, totalDurationMs: 651000, durationMs: 651000,
|
||||
watchers: ["qifan.zhang@bytedance.com", "oc_91dcbdb498fd9e70ab23f8f7727ac44e"],
|
||||
failureReason: null,
|
||||
status: "manual_review", createdAt: now,
|
||||
} as unknown as Security;
|
||||
|
||||
/** Failure copy per category, for previewing the 失败原因 panel. */
|
||||
export const FAILURE_REASON = {
|
||||
bug: "连续 6 轮均未通过 Fix-Critic:第 3-6 轮的改动都只在 UI 层拦截 toast,未触达 `GiftExchangeViewModel` 中真正的状态判定,Critic 判定为治标不治本。最后一轮构建成功但真机回归时原问题仍可复现。",
|
||||
feature: "构建连续 3 轮失败:`CommentCell` 所在模块开启了 strict-concurrency,翻译回调跨 actor 访问 UI 状态未通过编译。",
|
||||
security: "frida 注入后 App 在冷启动阶段被反调试逻辑主动 kill,3 次重试均未采集到完整调用栈。",
|
||||
};
|
||||
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* Apply one `drizzle/*.sql` migration to the database in DATABASE_URL.
|
||||
*
|
||||
* bun scripts/apply-migration.ts drizzle/0007_node_metrics.sql # 预演,只读
|
||||
* bun scripts/apply-migration.ts drizzle/0007_node_metrics.sql --commit # 真正执行
|
||||
*
|
||||
* Why this exists: there is no migration runner in this repo — `deploy.sh` never
|
||||
* touches the schema, and the devbox has no `psql`, so the seven migrations
|
||||
* before this one were applied by hand. That is fine until it isn't: a hand-run
|
||||
* DDL has no dry run, no transaction, and no record of whether it already ran.
|
||||
*
|
||||
* Dry run is the DEFAULT and is strictly read-only: it prints the statements and
|
||||
* queries information_schema to report which of the tables/indexes involved
|
||||
* already exist. `--commit` is the only path that writes, and it wraps the whole
|
||||
* file in a single transaction so a half-applied migration is not a state this
|
||||
* can leave behind.
|
||||
*
|
||||
* Applying it is a production write. Per CLAUDE.md that is the operator's call,
|
||||
* not something an agent runs on its own initiative.
|
||||
*/
|
||||
|
||||
import { readFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import pg from "pg";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const COMMIT = args.includes("--commit");
|
||||
const file = args.find((a) => !a.startsWith("--"));
|
||||
|
||||
if (!file) {
|
||||
console.error("用法: bun scripts/apply-migration.ts <drizzle/xxxx.sql> [--commit]");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const raw = process.env.DATABASE_URL;
|
||||
if (!raw) {
|
||||
console.error("DATABASE_URL 未设置(见 .env)");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const sql = readFileSync(path.resolve(file), "utf8");
|
||||
|
||||
/** Statement-ish split, for the summary only — the file is executed as one blob. */
|
||||
const statements = sql
|
||||
.split(/;\s*$/m)
|
||||
.map((s) => s.replace(/^\s*--.*$/gm, "").trim())
|
||||
.filter(Boolean);
|
||||
|
||||
// Same URL parsing as db_client.ts: an IPv6 literal must reach pg as a plain
|
||||
// `host`, or it tries to resolve the bracketed string as a hostname.
|
||||
const u = new URL(raw);
|
||||
const pool = new pg.Pool({
|
||||
host: u.hostname.replace(/^\[|\]$/g, ""),
|
||||
port: u.port ? Number(u.port) : 5432,
|
||||
user: decodeURIComponent(u.username),
|
||||
password: decodeURIComponent(u.password),
|
||||
database: u.pathname.replace(/^\//, ""),
|
||||
connectionTimeoutMillis: 30_000,
|
||||
});
|
||||
|
||||
// Never echo the URL itself — it carries the production password.
|
||||
console.log(`[migrate] ${file}`);
|
||||
console.log(`[migrate] 目标库 ${u.pathname.replace(/^\//, "")} @ ${u.hostname}`);
|
||||
console.log(`[migrate] ${statements.length} 条语句\n`);
|
||||
for (const s of statements) {
|
||||
console.log(` ${s.split("\n").map((l) => l.trim()).filter(Boolean).join(" ").slice(0, 110)}`);
|
||||
}
|
||||
|
||||
const tables = [...sql.matchAll(/CREATE TABLE (?:IF NOT EXISTS )?"?([\w.]+)"?/gi)].map((m) => m[1]);
|
||||
const indexes = [...sql.matchAll(/CREATE (?:UNIQUE )?INDEX (?:IF NOT EXISTS )?"?([\w.]+)"?/gi)].map((m) => m[1]);
|
||||
|
||||
try {
|
||||
console.log(`\n[migrate] 当前库状态:`);
|
||||
for (const t of tables) {
|
||||
const r = await pool.query("SELECT to_regclass($1) AS oid", [t]);
|
||||
console.log(` 表 ${t.padEnd(24)} ${r.rows[0].oid ? "已存在" : "不存在"}`);
|
||||
}
|
||||
for (const i of indexes) {
|
||||
const r = await pool.query("SELECT to_regclass($1) AS oid", [i]);
|
||||
console.log(` 索引 ${i.padEnd(24)} ${r.rows[0].oid ? "已存在" : "不存在"}`);
|
||||
}
|
||||
|
||||
if (!COMMIT) {
|
||||
console.log(`\n[migrate] \x1b[33m预演模式,未写入任何东西\x1b[0m`);
|
||||
console.log(`[migrate] 确认无误后加 --commit 执行`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
console.log(`\n[migrate] \x1b[31m执行中(单个事务)\x1b[0m …`);
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
await client.query(sql);
|
||||
await client.query("COMMIT");
|
||||
console.log(`[migrate] \x1b[32m已提交\x1b[0m`);
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK");
|
||||
throw err;
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
|
||||
console.log(`\n[migrate] 复核:`);
|
||||
for (const t of [...tables, ...indexes]) {
|
||||
const r = await pool.query("SELECT to_regclass($1) AS oid", [t]);
|
||||
console.log(` ${t.padEnd(24)} ${r.rows[0].oid ? "✓ 存在" : "✗ 仍然缺失"}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`\n[migrate] 失败: ${(err as Error).message}`);
|
||||
process.exitCode = 1;
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* 合并重建:把 UI 走查以前一条一单转出、还没开跑的修复单,按面合并重建(口径见 shared/src/audit_fix_plan.ts)。
|
||||
*
|
||||
* 只动「还没开始」的单:待确认执行、评估中,以及进了修复队列但一次都没跑过的(attempts = 0)。
|
||||
* 修复中、待复核、已修好的不动;P0 / P1 和功能类本来就该一条一单,也不动。
|
||||
*
|
||||
* 合并后只剩一条的组不动(重建了还是一张单)。
|
||||
*
|
||||
* 默认只读:列出每份走查有几张单会被归档、会重建成几张。加 --apply 才写:
|
||||
* 1. 逐张归档老单(系统原因「已并入合并单」,备注写明;archive_reasons.ts::SYSTEM_ARCHIVE_REASONS);
|
||||
* 2. 调 POST /api/verifications/:id/fix-batch 按面重建 —— 服务端把指向已归档单的问题当成没转过。
|
||||
* 建单接口要 SSO,--apply 时把自己的会话放进环境变量 STREAMLINE_SSO_SESSION(x-tt-session-v2 的值),
|
||||
* 建单人就是跑脚本的人。
|
||||
*
|
||||
* 这是生产写操作,--apply 之前先看一遍预览、跟负责人确认。
|
||||
*
|
||||
* bun scripts/audit_refile.ts # 自动找出所有带待重建单的走查,预览
|
||||
* bun scripts/audit_refile.ts <验证 id>… # 只看这几份
|
||||
* bun scripts/audit_refile.ts <验证 id>… --apply # 执行
|
||||
* --api <base> # 默认 https://streamline-internal-api.bytedance.net
|
||||
*/
|
||||
import { parseAuditData, type AuditData } from "../shared/src/audit_report";
|
||||
import { auditPickKey, planAuditFixGroups, standsAlone, type AuditFixPick } from "../shared/src/audit_fix_plan";
|
||||
|
||||
const args = Bun.argv.slice(2);
|
||||
const apply = args.includes("--apply");
|
||||
const apiAt = args.indexOf("--api");
|
||||
const API = (apiAt >= 0 ? args[apiAt + 1] : "https://streamline-internal-api.bytedance.net").replace(/\/$/, "");
|
||||
const ids = args.filter((a, i) => !a.startsWith("--") && args[i - 1] !== "--api");
|
||||
const SESSION = process.env.STREAMLINE_SSO_SESSION || "";
|
||||
|
||||
interface Note { pageId: string; issueN: number; lang: string; filedTaskId: string | null; author?: string | null }
|
||||
interface TaskRow { id: string; title: string; status: string; attempts: number; startedAt: number | null; description?: string }
|
||||
|
||||
async function get<T>(path: string): Promise<T> {
|
||||
const res = await fetch(`${API}${path}`);
|
||||
if (!res.ok) throw new Error(`GET ${path} → ${res.status} ${await res.text().catch(() => "")}`);
|
||||
return res.json() as Promise<T>;
|
||||
}
|
||||
async function post<T>(path: string, body: unknown): Promise<T> {
|
||||
const res = await fetch(`${API}${path}`, { method: "POST", headers: { "content-type": "application/json", ...(SESSION ? { "x-tt-session-v2": SESSION } : {}) }, body: JSON.stringify(body) });
|
||||
const text = await res.text();
|
||||
if (!res.ok) throw new Error(`POST ${path} → ${res.status} ${text}`);
|
||||
return JSON.parse(text) as T;
|
||||
}
|
||||
|
||||
/** 还没开始修的单。 */
|
||||
const unstarted = (t: TaskRow) => t.status === "pending_confirmation" || t.status === "initializing"
|
||||
|| (t.status === "awaiting_fix" && !t.attempts && !t.startedAt);
|
||||
|
||||
async function discover(): Promise<string[]> {
|
||||
const { tasks } = await get<{ tasks: TaskRow[] }>("/api/tasks");
|
||||
const out = new Set<string>();
|
||||
for (const t of tasks.filter((x) => x.title.startsWith("[UI]") && unstarted(x))) {
|
||||
const full = await get<TaskRow>(`/api/tasks/${t.id}`).catch(() => null);
|
||||
const m = /来源:验证任务「.*?」\(([0-9a-f-]{36})\)/.exec(full?.description ?? "");
|
||||
if (m) out.add(m[1]);
|
||||
}
|
||||
return [...out];
|
||||
}
|
||||
|
||||
async function one(vid: string) {
|
||||
const data = parseAuditData(JSON.stringify(await get<AuditData>(`/api/verifications/${vid}/audit-report`)));
|
||||
if (!data) { console.log(`${vid}: 没有走查报告,跳过`); return; }
|
||||
const { notes } = await get<{ notes: Note[] }>(`/api/verifications/${vid}/audit-notes`);
|
||||
const perTask = new Map<string, Note[]>();
|
||||
for (const n of notes) if (n.filedTaskId) perTask.set(n.filedTaskId, [...(perTask.get(n.filedTaskId) ?? []), n]);
|
||||
const issueOf = (n: Note) => data.report.pages.find((p) => p.id === n.pageId)?.issues.find((i) => i.n === n.issueN);
|
||||
|
||||
const old: { task: TaskRow; note: Note }[] = [];
|
||||
for (const [tid, rows] of perTask) {
|
||||
if (rows.length !== 1) continue; // 已经是合并单
|
||||
const iss = issueOf(rows[0]);
|
||||
if (!iss || standsAlone(iss)) continue;
|
||||
const task = await get<TaskRow>(`/api/tasks/${tid}`).catch(() => null);
|
||||
if (task && unstarted(task)) old.push({ task, note: rows[0] });
|
||||
}
|
||||
// 只剩一条的组重建了也还是一张单,白折腾一次:留着原单不动。
|
||||
const all = planAuditFixGroups(old.map(({ note }) => ({ pageId: note.pageId, n: note.issueN, lang: note.lang })), data.report);
|
||||
const groups = all.filter((g) => g.picks.length > 1);
|
||||
const keep = new Set(groups.flatMap((g) => g.picks.map(auditPickKey)));
|
||||
const moving = old.filter(({ note }) => keep.has(auditPickKey({ pageId: note.pageId, n: note.issueN })));
|
||||
console.log(`\n== ${data.report.title}(${vid})`);
|
||||
console.log(` ${moving.length} 张一条一单的老单 → ${groups.length} 张合并单(另有 ${old.length - moving.length} 张单独成组,不动)`);
|
||||
for (const g of groups) console.log(` - [${g.picks.length}] ${g.title}`);
|
||||
if (!apply || !moving.length) return;
|
||||
// 先拦住:归档老单之后才发现建不了新单,就成了只归档没重建。
|
||||
if (!SESSION) throw new Error("--apply 需要 STREAMLINE_SSO_SESSION:建单接口要 SSO");
|
||||
|
||||
const archived: AuditFixPick[] = [];
|
||||
for (const { task, note } of moving) {
|
||||
try {
|
||||
await post(`/api/tasks/${task.id}/archive`, { reason: "merged", note: "合并重建:并入同一个面的走查合并单" });
|
||||
archived.push({ pageId: note.pageId, n: note.issueN, lang: note.lang });
|
||||
} catch (err) {
|
||||
console.log(` ! 归档 ${task.id} 失败,这条不重建:${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
if (!archived.length) return;
|
||||
const res = await post<{ batchTag: string; created: { taskId: string; title: string; picks: AuditFixPick[] }[]; skipped: { pick: AuditFixPick; reason: string }[]; failed: { title: string; error: string }[] }>(
|
||||
`/api/verifications/${vid}/fix-batch`, { picks: archived },
|
||||
);
|
||||
console.log(` ✓ 批次 ${res.batchTag}:建了 ${res.created.length} 张`);
|
||||
for (const c of res.created) console.log(` ${c.taskId} [${c.picks.length}] ${c.title}`);
|
||||
for (const s of res.skipped) console.log(` - 跳过 ${auditPickKey(s.pick)}:${s.reason}`);
|
||||
for (const f of res.failed) console.log(` ! 没建成「${f.title}」:${f.error}`);
|
||||
}
|
||||
|
||||
const targets = ids.length ? ids : await discover();
|
||||
if (!targets.length) { console.log("没有带待重建单的走查。"); process.exit(0); }
|
||||
console.log(`${apply ? "执行" : "预览(加 --apply 才会写)"} · ${API}`);
|
||||
for (const vid of targets) await one(vid);
|
||||
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* One-off 存量 backfill for the /lifemon ledger (`task_transitions`).
|
||||
*
|
||||
* The live chokepoint (db.ts:notifyStatusTransition → recordTransition) only
|
||||
* started capturing `source='forward'` rows on deploy day, so every task that
|
||||
* predates it is invisible in /lifemon. This reconstructs a COARSE skeleton for
|
||||
* each historical bug/feature/security from the sparse timestamps the row still
|
||||
* carries, and writes it with `source='backfill'`.
|
||||
*
|
||||
* FIDELITY (read before trusting the numbers):
|
||||
* Reliable → EXEC start (startedAt), rework-round boundaries (each HUMAN
|
||||
* reviewHistory note = a manual_review bounce → next EXEC round),
|
||||
* terminal (finishedAt / status).
|
||||
* Dropped → the createdAt→startedAt span. Its real EVAL compute is only
|
||||
* minutes, but it is dominated by pending_confirmation + queue
|
||||
* wait whose entry timestamps were never stored — labelling the
|
||||
* whole span EVAL inflated it to week-long tails (workShare→1), so
|
||||
* we emit NO pre-exec segment. Historical EVAL is therefore blank
|
||||
* in backfill; only forward capture yields trustworthy EVAL.
|
||||
* NOT stored → manual_review / info_required dwell has no entry timestamp and
|
||||
* is folded into the adjacent EXEC. So backfill still under-counts
|
||||
* human WAIT — exactly why lifecycle_db keeps it out of headline
|
||||
* percentiles and the Portal shows it only behind `含回填`.
|
||||
* `critic` notes fire inside `fixing` (not a status change) and
|
||||
* are ignored here.
|
||||
*
|
||||
* Idempotent (INSERT OR IGNORE on the derived id; re-runs never duplicate and
|
||||
* never clobber a colliding forward row). DRY-RUN by default — prints the
|
||||
* reconstructed shape + the exact matrix /lifemon will render. Pass --commit to
|
||||
* actually insert:
|
||||
* bun scripts/backfill_lifecycle.ts # preview only
|
||||
* bun scripts/backfill_lifecycle.ts --commit # write source='backfill'
|
||||
*/
|
||||
import { listTasks, listFeatures, listSecurity } from "../control-server/src/db";
|
||||
import {
|
||||
insertBackfillTransitions,
|
||||
computeLifecycleStats,
|
||||
segClassOf,
|
||||
type BackfillTx,
|
||||
type TxKind,
|
||||
type TransitionRow,
|
||||
} from "../control-server/src/lifecycle_db";
|
||||
|
||||
const COMMIT = process.argv.includes("--commit");
|
||||
|
||||
/** The EXEC status name per task kind (initializing/EVAL is shared). */
|
||||
const EXEC_STATUS: Record<TxKind, string> = {
|
||||
bug: "fixing",
|
||||
feature: "implementing",
|
||||
security: "security_running",
|
||||
};
|
||||
|
||||
/** A status is terminal iff it holds no dwell we care about (segClassOf). */
|
||||
const isTerminal = (s: string) => segClassOf(s) === "TERMINAL";
|
||||
|
||||
/** Minimal shape shared by all three kinds' rows for reconstruction. */
|
||||
interface Hist {
|
||||
id: string;
|
||||
status: string;
|
||||
createdAt: number | null;
|
||||
startedAt: number | null;
|
||||
finishedAt: number | null;
|
||||
statusChangedAt: number | null;
|
||||
attempts: number | null;
|
||||
executorNodeId: string | null;
|
||||
reviewHistory: { at: number; source: "human" | "critic"; round: number }[] | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstruct one target's ordered transitions. Emits strictly-increasing-ts
|
||||
* events only (out-of-order timestamps dropped, matching segmentsFor's skew
|
||||
* guard); a non-terminal current status is left OPEN (no trailing transition)
|
||||
* so its dwell isn't fabricated — forward capture owns it from now on.
|
||||
*/
|
||||
function reconstruct(kind: TxKind, h: Hist): BackfillTx[] {
|
||||
if (h.startedAt == null) return []; // no EXEC anchor → nothing defensible to emit
|
||||
const exec = EXEC_STATUS[kind];
|
||||
const node = h.executorNodeId ?? null;
|
||||
|
||||
// 1) collect candidate (status, ts) points, then enforce monotonic ts.
|
||||
// The createdAt→startedAt span (EVAL + confirm/queue wait) is deliberately
|
||||
// NOT emitted — its wait phases have no stored entry ts (see header).
|
||||
const pts: { status: string; ts: number; round: number | null }[] = [];
|
||||
pts.push({ status: exec, ts: h.startedAt, round: 1 });
|
||||
// each HUMAN review note = a manual_review bounce → a fresh EXEC round starts.
|
||||
for (const n of (h.reviewHistory ?? []).filter((n) => n.source === "human")) {
|
||||
if (n.at > 0) pts.push({ status: exec, ts: n.at, round: (n.round ?? 0) + 1 });
|
||||
}
|
||||
// terminal cap — only if the row actually reached a terminal status.
|
||||
if (isTerminal(h.status)) {
|
||||
const termTs = h.finishedAt ?? h.statusChangedAt;
|
||||
if (termTs != null) pts.push({ status: h.status, ts: termTs, round: h.attempts ?? null });
|
||||
}
|
||||
|
||||
pts.sort((a, b) => a.ts - b.ts);
|
||||
// drop non-increasing ts (dedupe / skew), keeping the earliest at each ts.
|
||||
const clean: typeof pts = [];
|
||||
for (const p of pts) if (!clean.length || p.ts > clean[clean.length - 1].ts) clean.push(p);
|
||||
if (clean.length < 2) return []; // need ≥1 closed segment
|
||||
|
||||
const out: BackfillTx[] = [];
|
||||
let prev: string | null = null;
|
||||
for (const p of clean) {
|
||||
out.push({ kind, targetId: h.id, fromStatus: prev, toStatus: p.status, round: p.round, nodeId: node, ts: p.ts });
|
||||
prev = p.status;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function collect(): BackfillTx[] {
|
||||
const rows: BackfillTx[] = [];
|
||||
const push = (kind: TxKind, items: Hist[]) => {
|
||||
let contributed = 0;
|
||||
for (const h of items) {
|
||||
const r = reconstruct(kind, h);
|
||||
if (r.length) { rows.push(...r); contributed++; }
|
||||
}
|
||||
console.log(` ${kind.padEnd(9)} ${items.length} targets → ${contributed} reconstructed, ${rows.filter((x) => x.kind === kind).length} transitions`);
|
||||
};
|
||||
push("bug", listTasks() as unknown as Hist[]);
|
||||
push("feature", listFeatures() as unknown as Hist[]);
|
||||
push("security", listSecurity() as unknown as Hist[]);
|
||||
return rows;
|
||||
}
|
||||
|
||||
console.log(`[backfill_lifecycle] mode=${COMMIT ? "COMMIT" : "DRY-RUN"}`);
|
||||
const backfill = collect();
|
||||
|
||||
// Preview the exact matrix /lifemon will render for the backfill bucket.
|
||||
const rowsForStats: TransitionRow[] = backfill.map((r) => ({
|
||||
id: `${r.kind}:${r.targetId}:${r.toStatus}:${r.ts}`,
|
||||
kind: r.kind, targetId: r.targetId, fromStatus: r.fromStatus, toStatus: r.toStatus,
|
||||
round: r.round, nodeId: r.nodeId, source: "backfill", ts: r.ts,
|
||||
}));
|
||||
const minTs = rowsForStats.reduce((m, r) => Math.min(m, r.ts), Number.MAX_SAFE_INTEGER);
|
||||
const stats = computeLifecycleStats(rowsForStats, Number.isFinite(minTs) ? minTs : 0, true);
|
||||
|
||||
console.log(`\n[backfill_lifecycle] reconstructed ${backfill.length} transitions over ${stats.totals.tasks} targets`);
|
||||
console.log(" per-kind work/wait (backfill is WORK-biased by construction):");
|
||||
for (const k of stats.byKind) {
|
||||
console.log(` ${k.kind.padEnd(9)} tasks=${k.tasks} work=${Math.round(k.workMs / 3600000)}h wait=${Math.round(k.waitMs / 3600000)}h workShare=${k.workShare} rework=${k.reworkRate}`);
|
||||
}
|
||||
console.log(" segment matrix (kind | seg | count | p50 min | p90 min | max min):");
|
||||
for (const s of stats.segMatrix) {
|
||||
console.log(` ${s.kind.padEnd(9)} ${s.segClass.padEnd(12)} n=${String(s.count).padEnd(4)} p50=${(s.p50Ms / 60000).toFixed(1)} p90=${(s.p90Ms / 60000).toFixed(1)} max=${(s.maxMs / 60000).toFixed(1)}`);
|
||||
}
|
||||
|
||||
if (!COMMIT) {
|
||||
console.log("\n[backfill_lifecycle] DRY-RUN — nothing written. Re-run with --commit to insert source='backfill'.");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
const inserted = insertBackfillTransitions(backfill);
|
||||
console.log(`\n[backfill_lifecycle] COMMITTED — ${inserted} new backfill rows (${backfill.length - inserted} already present, idempotent skip).`);
|
||||
@@ -0,0 +1,161 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* Backfill: `manual_review` bugs whose Meego ticket is already Close / Done.
|
||||
*
|
||||
* Marks them `fix_succeeded` (看板「修复成功 / Fixed」), writes the live Meego
|
||||
* state, and scans the bound Bits MR the same way mr_sync does. No Lark card,
|
||||
* no Meego comment, no Meego transition — the ticket is already closed.
|
||||
*
|
||||
* bun scripts/backfill_meego_closed_review.ts # dry-run
|
||||
* bun scripts/backfill_meego_closed_review.ts --commit # write
|
||||
*
|
||||
* Routing after the write (comboTaskStateOf):
|
||||
* Meego resolved/open → awaiting_qa
|
||||
* Meego close/done + MR closed → merged (DONE)
|
||||
* Meego close/done (else) → awaiting_merge
|
||||
* Bits merged → merged (DONE)
|
||||
* Merged MRs stamp `mrMergedAt` from Bits merged_time, then create_time —
|
||||
* never Date.now(), so the dashboard doesn't pile every merge on today.
|
||||
*
|
||||
* Pass `--resolved` to also accept Meego RESOLVED (→ 待 QA), not just close/done.
|
||||
*/
|
||||
import { readFileSync } from "node:fs";
|
||||
import { getTask, updateTask, listTasks } from "../control-server/src/db";
|
||||
import { BITS_FIXED_OPERATOR, getMrInfo, extractMrIdFromUrl, mrStatePatchFromInfo } from "../control-server/src/bits";
|
||||
import { broadcast } from "../control-server/src/bus";
|
||||
import { enrichTask } from "../control-server/src/enrich";
|
||||
import { comboTaskStateOf, meegoIsClosed } from "../control-server/src/types";
|
||||
import type { Task } from "../control-server/src/types";
|
||||
|
||||
/**
|
||||
* meegoId → live Meego state_key. Default file is written by the investigation
|
||||
* (CLOSE/DONE only). Plugin OpenAPI from a laptop often can't mint a token
|
||||
* against meego.larkoffice.com, so we don't re-fetch here — the file is the
|
||||
* live snapshot. Override with --states PATH (JSON object).
|
||||
*/
|
||||
function loadStates(): Map<string, string> {
|
||||
const idx = process.argv.indexOf("--states");
|
||||
const path = idx >= 0 ? process.argv[idx + 1] : "/tmp/sl-meego-closed-states.json";
|
||||
const allowResolved = process.argv.includes("--resolved");
|
||||
const raw = JSON.parse(readFileSync(path, "utf8")) as Record<string, string>;
|
||||
const m = new Map<string, string>();
|
||||
for (const [id, state] of Object.entries(raw)) {
|
||||
const s = String(state).toLowerCase();
|
||||
if (meegoIsClosed(state) || (allowResolved && s === "resolved")) m.set(String(id), state);
|
||||
}
|
||||
return m;
|
||||
}
|
||||
|
||||
const COMMIT = process.argv.includes("--commit");
|
||||
|
||||
interface Plan {
|
||||
id: string;
|
||||
title: string;
|
||||
meegoId: string;
|
||||
meegoLive: string;
|
||||
mrUrl: string | null;
|
||||
patch: Partial<Task>;
|
||||
combo: ReturnType<typeof comboTaskStateOf>;
|
||||
bitsError?: string;
|
||||
}
|
||||
|
||||
function comboOf(t: Task, patch: Partial<Task>) {
|
||||
return comboTaskStateOf({
|
||||
mrState: patch.mrState !== undefined ? (patch.mrState as string | null) : t.mrState,
|
||||
meegoState: patch.meegoState !== undefined ? (patch.meegoState as string | null) : t.meegoState,
|
||||
});
|
||||
}
|
||||
|
||||
async function planOne(t: Task, meegoLive: string): Promise<Plan> {
|
||||
const patch: Partial<Task> = {
|
||||
status: "fix_succeeded",
|
||||
meegoState: meegoLive,
|
||||
mrSyncedAt: Date.now(),
|
||||
failureReason: null,
|
||||
};
|
||||
let bitsError: string | undefined;
|
||||
const mrId = t.mrUrl ? extractMrIdFromUrl(t.mrUrl) : null;
|
||||
if (mrId) {
|
||||
const info = await getMrInfo(mrId, null, BITS_FIXED_OPERATOR);
|
||||
if (!info.ok) bitsError = info.error || "getMrInfo failed";
|
||||
else Object.assign(patch, mrStatePatchFromInfo(t, info));
|
||||
}
|
||||
return {
|
||||
id: t.id,
|
||||
title: t.title,
|
||||
meegoId: t.meegoId!,
|
||||
meegoLive,
|
||||
mrUrl: t.mrUrl,
|
||||
patch,
|
||||
combo: comboOf(t, patch),
|
||||
bitsError,
|
||||
};
|
||||
}
|
||||
|
||||
const states = loadStates();
|
||||
console.log(`[backfill] loaded ${states.size} close/done Meego id(s) from snapshot`);
|
||||
|
||||
const all = await listTasks();
|
||||
const review = all.filter((t) => t.status === "manual_review" && t.meegoId);
|
||||
console.log(`[backfill] ${review.length} manual_review bug(s) with a Meego id (commit=${COMMIT})`);
|
||||
|
||||
const plans: Plan[] = [];
|
||||
let notInSnapshot = 0;
|
||||
for (const t of review) {
|
||||
const live = states.get(String(t.meegoId));
|
||||
if (!live) { notInSnapshot++; continue; }
|
||||
plans.push(await planOne(t, live));
|
||||
}
|
||||
|
||||
const byCombo: Record<string, number> = {};
|
||||
for (const p of plans) byCombo[p.combo ?? "null"] = (byCombo[p.combo ?? "null"] ?? 0) + 1;
|
||||
const withMr = plans.filter((p) => p.mrUrl).length;
|
||||
const mergedAtSet = plans.filter((p) => p.patch.mrMergedAt != null).length;
|
||||
const bitsFail = plans.filter((p) => p.bitsError).length;
|
||||
|
||||
console.log(`[backfill] eligible close/done: ${plans.length} not in snapshot: ${notInSnapshot}`);
|
||||
console.log(`[backfill] with MR: ${withMr} no MR: ${plans.length - withMr} mrMergedAt stamped: ${mergedAtSet} bits errors: ${bitsFail}`);
|
||||
console.log(`[backfill] combo after write:`, byCombo);
|
||||
|
||||
const sample = (rows: Plan[], n = 8) => {
|
||||
for (const p of rows.slice(0, n)) {
|
||||
const mr = p.patch.mrState ?? "(none)";
|
||||
const at = p.patch.mrMergedAt ? new Date(p.patch.mrMergedAt as number).toISOString() : "-";
|
||||
console.log(` ${p.combo ?? "?"} meego=${p.meegoLive} mr=${mr} mergedAt=${at} ${p.meegoId} ${p.id.slice(0, 8)} ${p.title.slice(0, 60)}`);
|
||||
if (p.bitsError) console.log(` bits: ${p.bitsError}`);
|
||||
}
|
||||
if (rows.length > n) console.log(` … +${rows.length - n} more`);
|
||||
};
|
||||
|
||||
console.log("\n— awaiting_merge (MR still open) —");
|
||||
sample(plans.filter((p) => p.combo === "awaiting_merge"));
|
||||
console.log("\n— awaiting_qa (Meego resolved / open) —");
|
||||
sample(plans.filter((p) => p.combo === "awaiting_qa"), 12);
|
||||
console.log("\n— merged / DONE (no open MR) —");
|
||||
sample(plans.filter((p) => p.combo === "merged"), 12);
|
||||
|
||||
if (!COMMIT) {
|
||||
console.log("\n[backfill] dry-run. pass --commit to write.");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
let ok = 0;
|
||||
let fail = 0;
|
||||
for (const p of plans) {
|
||||
try {
|
||||
const fresh = await getTask(p.id);
|
||||
if (!fresh || fresh.status !== "manual_review") {
|
||||
console.warn(`[backfill] skip ${p.id}: status is now ${fresh?.status ?? "gone"}`);
|
||||
continue;
|
||||
}
|
||||
await updateTask(p.id, p.patch);
|
||||
const next = await getTask(p.id);
|
||||
if (next) broadcast({ type: "task:updated", payload: await enrichTask(next) });
|
||||
ok++;
|
||||
} catch (err) {
|
||||
fail++;
|
||||
console.warn(`[backfill] ${p.id}: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
console.log(`[backfill] wrote ${ok}, failed ${fail}`);
|
||||
process.exit(fail ? 1 : 0);
|
||||
@@ -0,0 +1,115 @@
|
||||
#!/usr/bin/env bun
|
||||
interface Options {
|
||||
taskId: string | null;
|
||||
batch: boolean;
|
||||
force: boolean;
|
||||
limit: number | null;
|
||||
}
|
||||
|
||||
type ReadableModule = typeof import("../control-server/src/readable_report");
|
||||
|
||||
function parseArgs(argv: string[]): Options {
|
||||
const opts: Options = {
|
||||
taskId: process.env.task_id || process.env.TASK_ID || null,
|
||||
batch: false,
|
||||
force: false,
|
||||
limit: null,
|
||||
};
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const arg = argv[i];
|
||||
if (arg === "--batch") opts.batch = true;
|
||||
else if (arg === "--force") opts.force = true;
|
||||
else if (arg === "--help" || arg === "-h") usage(0);
|
||||
else if (arg === "--task-id" || arg === "--task_id") opts.taskId = argv[++i] || null;
|
||||
else if (arg.startsWith("--task-id=")) opts.taskId = arg.slice("--task-id=".length);
|
||||
else if (arg.startsWith("--task_id=")) opts.taskId = arg.slice("--task_id=".length);
|
||||
else if (arg.startsWith("task_id=")) opts.taskId = arg.slice("task_id=".length);
|
||||
else if (arg === "--limit") opts.limit = Number(argv[++i] || "0") || null;
|
||||
else if (arg.startsWith("--limit=")) opts.limit = Number(arg.slice("--limit=".length)) || null;
|
||||
else {
|
||||
console.error(`Unknown argument: ${arg}`);
|
||||
usage(1);
|
||||
}
|
||||
}
|
||||
return opts;
|
||||
}
|
||||
|
||||
function usage(code: number): never {
|
||||
const text = `
|
||||
Usage:
|
||||
task_id=b3ff7fbe-b80d-4659-a600-52c4a4a87f62 bun scripts/backfill_readable_reports.ts
|
||||
bun scripts/backfill_readable_reports.ts --task-id b3ff7fbe-b80d-4659-a600-52c4a4a87f62
|
||||
bun scripts/backfill_readable_reports.ts --batch
|
||||
|
||||
Options:
|
||||
--batch Convert every historical bug task that has fix artifacts and no readable report.
|
||||
--force Regenerate even when readableReportHtml already exists.
|
||||
--limit N Process at most N tasks in batch mode.
|
||||
|
||||
Notes:
|
||||
- The script calls the task's executor satellite, so the executor node must be online.
|
||||
- Use READABLE_REPORT_DRY_RUN=1 to test the pipeline without invoking Claude.
|
||||
`;
|
||||
console.log(text.trim());
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
async function convertOne(taskId: string, opts: { force: boolean }): Promise<boolean> {
|
||||
const { getTask, getTaskReadableReport } = await import("../control-server/src/db");
|
||||
const { generateReadableReportForTask } = await import("../control-server/src/readable_report");
|
||||
// db.ts went async (Postgres); these were left as sync calls, so `task` was a
|
||||
// Promise — always truthy, and `task.meegoId` logged as undefined.
|
||||
const task = await getTask(taskId);
|
||||
if (!task) {
|
||||
console.error(`[missing] task not found: ${taskId}`);
|
||||
return false;
|
||||
}
|
||||
const existing = await getTaskReadableReport(taskId);
|
||||
if (!opts.force && existing?.html) {
|
||||
console.log(`[skip] ${taskId} already has readable report`);
|
||||
return true;
|
||||
}
|
||||
console.log(`[start] ${taskId} ${task.meegoId} ${task.title}`);
|
||||
const result = await generateReadableReportForTask(taskId, {
|
||||
force: opts.force,
|
||||
broadcastUpdate: false,
|
||||
});
|
||||
if (result.ok) {
|
||||
console.log(result.skipped ? `[skip] ${taskId}` : `[ok] ${taskId}`);
|
||||
return true;
|
||||
}
|
||||
console.error(`[fail] ${taskId}: ${result.error || "unknown error"}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const opts = parseArgs(Bun.argv.slice(2));
|
||||
if (!opts.taskId && !opts.batch) usage(1);
|
||||
if (opts.taskId && opts.batch) {
|
||||
console.error("Choose either --task-id/task_id or --batch, not both.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (opts.taskId) {
|
||||
const ok = await convertOne(opts.taskId, { force: opts.force });
|
||||
process.exit(ok ? 0 : 1);
|
||||
}
|
||||
|
||||
const { readableBackfillTaskCandidates } = await import("../control-server/src/readable_report") as ReadableModule;
|
||||
let tasks = await readableBackfillTaskCandidates({ force: opts.force });
|
||||
if (opts.limit != null) tasks = tasks.slice(0, opts.limit);
|
||||
console.log(`[batch] ${tasks.length} task(s) to convert${opts.force ? " (force)" : ""}`);
|
||||
let ok = 0;
|
||||
let fail = 0;
|
||||
for (const task of tasks) {
|
||||
if (await convertOne(task.id, { force: opts.force })) ok++;
|
||||
else fail++;
|
||||
}
|
||||
console.log(`[done] ok=${ok} fail=${fail}`);
|
||||
process.exit(fail ? 1 : 0);
|
||||
}
|
||||
|
||||
main().catch((err) => {
|
||||
console.error((err as Error).stack || (err as Error).message);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* 给存量的可读性报告生成记录补上「这是第几批」——`readable_report_runs.round`。
|
||||
*
|
||||
* bun scripts/backfill_readable_run_round.ts # 预演,只读,默认
|
||||
* bun scripts/backfill_readable_run_round.ts --commit # 真正写入
|
||||
* bun scripts/backfill_readable_run_round.ts --max-gap-h 3 # 放宽认领窗口
|
||||
*
|
||||
* ## 为什么需要它,以及为什么它是一个脚本而不是一条 UPDATE
|
||||
*
|
||||
* 0018 之前的 run 行没有批次号,所以批次切换器对存量任务只能给出「原始 markdown」
|
||||
* 那一半:那一批的大白话正文明明还在库里,却认不出它属于哪一批。这条脚本把它认回来。
|
||||
*
|
||||
* 认的依据是**时间上的因果**,不是猜:一次生成总是先把当前批次的原始报告归档下来
|
||||
* (archiveFixReportRound / archiveImplReportRound),紧接着才拿去生成正文。所以
|
||||
* 一份正文对应的批次,就是「归档时间早于这次生成、且离它最近」的那一批。生产上实测
|
||||
* 这个间隔是分钟级(第 9 批 13:48 归档、13:50 出正文)。
|
||||
*
|
||||
* 因果链断掉的地方就不认:
|
||||
* - 找不到任何早于它的归档批次 → 跳过;
|
||||
* - 最近的那一批离它超过 `--max-gap-h`(默认 1 小时)→ 跳过。中间大概率隔着一次
|
||||
* 实验室重跑或批量刷新,认过去就等于给一份正文安一个它没描述过的批次号,而这
|
||||
* 正是整套东西要防的那种假事实。
|
||||
*
|
||||
* 阈值不是拍的。2026-08-20 在生产上按同一规则只读预演过 1332 条 done+html 的记录:
|
||||
* 认得回 705 条,归档→生成的间隔中位 1.7 分钟、p90 3.9 分钟,而 p99 是 160 分钟 ——
|
||||
* 分布本身就把「正常的一次生成」和「隔了很久的重跑」分成了两坨,1 小时落在中间的
|
||||
* 空档里。剩下认不回的:474 条的目标压根没有按批归档(归档机制之前的老任务),
|
||||
* 128 条早于该目标的第一份归档。
|
||||
*
|
||||
* 预演是默认且严格只读:它把每一条的推断结果和跳过原因打出来,人看过再决定要不要
|
||||
* `--commit`。写生产是操作者的决定,不是 agent 顺手做的事(见 CLAUDE.md)。
|
||||
*/
|
||||
|
||||
import pg from "pg";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const COMMIT = args.includes("--commit");
|
||||
const gapArgIdx = args.findIndex((a) => a === "--max-gap-h");
|
||||
const MAX_GAP_H = gapArgIdx >= 0 ? Number(args[gapArgIdx + 1]) : 1;
|
||||
if (!Number.isFinite(MAX_GAP_H) || MAX_GAP_H <= 0) {
|
||||
console.error("--max-gap-h 需要一个正数小时数");
|
||||
process.exit(2);
|
||||
}
|
||||
const MAX_GAP_MS = MAX_GAP_H * 3600_000;
|
||||
|
||||
const raw = process.env.DATABASE_URL;
|
||||
if (!raw) {
|
||||
console.error("DATABASE_URL 没设置(见 .env)");
|
||||
process.exit(2);
|
||||
}
|
||||
const u = new URL(raw);
|
||||
const pool = new pg.Pool({
|
||||
host: u.hostname.replace(/^\[|\]$/g, ""),
|
||||
port: u.port ? Number(u.port) : 5432,
|
||||
user: decodeURIComponent(u.username),
|
||||
password: decodeURIComponent(u.password),
|
||||
database: u.pathname.replace(/^\//, ""),
|
||||
connectionTimeoutMillis: 30_000,
|
||||
});
|
||||
|
||||
interface RunRow { id: string; targetType: string; targetId: string; at: number; lang: string | null }
|
||||
interface ArchiveRow { targetId: string; round: number; createdAt: number }
|
||||
|
||||
const runs: RunRow[] = (await pool.query(
|
||||
`select id, target_type, target_id, coalesce(finished_at, created_at) as at, lang
|
||||
from readable_report_runs
|
||||
where round is null and status = 'done' and html is not null
|
||||
order by target_type, target_id, at`,
|
||||
)).rows.map((r) => ({
|
||||
id: r.id, targetType: r.target_type, targetId: r.target_id, at: Number(r.at), lang: r.lang,
|
||||
}));
|
||||
|
||||
const archives = new Map<string, ArchiveRow[]>();
|
||||
const loadArchives = async (kind: "task" | "feature") => {
|
||||
const table = kind === "task" ? "fix_report_versions" : "impl_report_versions";
|
||||
const idCol = kind === "task" ? "task_id" : "feature_id";
|
||||
const rows = (await pool.query(
|
||||
`select ${idCol} as target_id, round, created_at from ${table} order by ${idCol}, round`,
|
||||
)).rows;
|
||||
for (const r of rows) {
|
||||
const key = `${kind}:${r.target_id}`;
|
||||
const list = archives.get(key) ?? [];
|
||||
list.push({ targetId: r.target_id, round: Number(r.round), createdAt: Number(r.created_at) });
|
||||
archives.set(key, list);
|
||||
}
|
||||
};
|
||||
await loadArchives("task");
|
||||
await loadArchives("feature");
|
||||
|
||||
/** 归档早于这次生成、且离它最近的那一批。 */
|
||||
function inferRound(run: RunRow): { round: number; gapMs: number } | { skip: string } {
|
||||
const list = archives.get(`${run.targetType}:${run.targetId}`);
|
||||
if (!list?.length) return { skip: "该目标没有任何按批归档的原始报告" };
|
||||
let best: ArchiveRow | null = null;
|
||||
for (const a of list) {
|
||||
if (a.createdAt > run.at) continue;
|
||||
if (!best || a.round > best.round) best = a;
|
||||
}
|
||||
if (!best) return { skip: "这次生成早于该目标的第一份归档" };
|
||||
const gapMs = run.at - best.createdAt;
|
||||
if (gapMs > MAX_GAP_MS) return { skip: `离最近一批归档 ${(gapMs / 3600_000).toFixed(1)}h,超过 ${MAX_GAP_H}h` };
|
||||
return { round: best.round, gapMs };
|
||||
}
|
||||
|
||||
const decided: { id: string; round: number; gapMs: number; targetType: string; targetId: string; lang: string | null }[] = [];
|
||||
const skipped = new Map<string, number>();
|
||||
for (const run of runs) {
|
||||
const got = inferRound(run);
|
||||
if ("skip" in got) {
|
||||
skipped.set(got.skip.replace(/[\d.]+h/g, "Nh"), (skipped.get(got.skip.replace(/[\d.]+h/g, "Nh")) ?? 0) + 1);
|
||||
continue;
|
||||
}
|
||||
decided.push({ id: run.id, round: got.round, gapMs: got.gapMs, targetType: run.targetType, targetId: run.targetId, lang: run.lang });
|
||||
}
|
||||
|
||||
console.log(`存量待补的生成记录:${runs.length} 条`);
|
||||
console.log(`可推断:${decided.length} 条`);
|
||||
for (const [why, n] of skipped) console.log(`跳过:${why} —— ${n} 条`);
|
||||
|
||||
const gaps = decided.map((d) => d.gapMs).sort((a, b) => a - b);
|
||||
if (gaps.length) {
|
||||
const pct = (p: number) => gaps[Math.min(gaps.length - 1, Math.floor(gaps.length * p))];
|
||||
console.log(
|
||||
`归档→生成的间隔:中位 ${(pct(0.5) / 60000).toFixed(1)} 分钟,`
|
||||
+ `p90 ${(pct(0.9) / 60000).toFixed(1)} 分钟,最大 ${(gaps[gaps.length - 1] / 60000).toFixed(1)} 分钟`,
|
||||
);
|
||||
}
|
||||
console.log("\n前 15 条推断结果:");
|
||||
console.table(decided.slice(0, 15).map((d) => ({
|
||||
target: `${d.targetType}:${d.targetId.slice(0, 8)}`,
|
||||
lang: d.lang ?? "(旧)",
|
||||
round: d.round,
|
||||
gapMin: Number((d.gapMs / 60000).toFixed(1)),
|
||||
})));
|
||||
|
||||
if (!COMMIT) {
|
||||
console.log(`\n预演结束,一个字都没写。确认无误后加 --commit 执行。`);
|
||||
await pool.end();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// 写入:一条事务,按 id 逐条 UPDATE。条件里再带一次 `round is null`,这样即便脚本
|
||||
// 与一次正常生成撞车,也绝不会把新写进去的真实批次号覆盖成推断值。
|
||||
const client = await pool.connect();
|
||||
let written = 0;
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
for (const d of decided) {
|
||||
const res = await client.query(
|
||||
`update readable_report_runs set round = $1 where id = $2 and round is null`,
|
||||
[d.round, d.id],
|
||||
);
|
||||
written += res.rowCount ?? 0;
|
||||
}
|
||||
await client.query("COMMIT");
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK");
|
||||
console.error("回填失败,已回滚:", (err as Error).message);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
console.log(`\n已写入 ${written} 条。`);
|
||||
await pool.end();
|
||||
@@ -0,0 +1,105 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* One-off: resend 待复核 cards that were lost and are too old for the periodic
|
||||
* sweep to reach.
|
||||
*
|
||||
* `notify_reconcile.ts` runs on a 2-hour window on purpose — a card announcing a
|
||||
* day-old review reads as a bug. But when the reconciliation logic itself was
|
||||
* wrong (it compared key EXISTENCE, so every re-fix round was invisible to it),
|
||||
* the backlog it should have caught is by definition already outside that window.
|
||||
* This is the deliberate tool for that backlog, the way `readable:backfill` is
|
||||
* for reports the retry sweep can no longer see.
|
||||
*
|
||||
* Dry-run by default. It reuses `reconcileReviewCards` rather than
|
||||
* re-implementing the decision, so what it sends is exactly what the sweep would
|
||||
* have sent — including the re-read that skips anything a human already resolved.
|
||||
*
|
||||
* bun scripts/backfill_review_cards.ts --hours 48 # list only
|
||||
* bun scripts/backfill_review_cards.ts --hours 48 --send # actually send
|
||||
*/
|
||||
interface Options { hours: number; send: boolean }
|
||||
|
||||
function usage(code: number): never {
|
||||
console.log(`
|
||||
Usage:
|
||||
bun scripts/backfill_review_cards.ts [--hours N] [--send]
|
||||
|
||||
Options:
|
||||
--hours N How far back to look for review entries whose card never landed.
|
||||
Default 48. The live sweep uses 2 and is unaffected by this.
|
||||
--send Actually send. Without it, this only prints what WOULD be sent.
|
||||
`.trim());
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
function parseArgs(argv: string[]): Options {
|
||||
const opts: Options = { hours: 48, send: false };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--send") opts.send = true;
|
||||
else if (a === "--hours") opts.hours = Number(argv[++i] || "0") || opts.hours;
|
||||
else if (a.startsWith("--hours=")) opts.hours = Number(a.slice("--hours=".length)) || opts.hours;
|
||||
else if (a === "--help" || a === "-h") usage(0);
|
||||
else { console.error(`Unknown argument: ${a}`); usage(1); }
|
||||
}
|
||||
return opts;
|
||||
}
|
||||
|
||||
const KINDS = ["bug", "feature", "security"] as const;
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const opts = parseArgs(process.argv.slice(2));
|
||||
const windowMs = opts.hours * 60 * 60 * 1000;
|
||||
const now = Date.now();
|
||||
|
||||
const { listRecentManualReviewIds, settingKeyTimes } = await import("../control-server/src/db");
|
||||
const { lastEntryTimes } = await import("../control-server/src/lifecycle_db");
|
||||
const { sentCardKey } = await import("../control-server/src/lark_card_media");
|
||||
const { reviewCardMissing, reconcileReviewCards } = await import("../control-server/src/notify_reconcile");
|
||||
const { larkEnabled } = await import("../control-server/src/lark");
|
||||
|
||||
if (!larkEnabled()) {
|
||||
console.error("LARK_APP_ID / LARK_APP_SECRET not set — nothing could be sent. Aborting.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Same query the sweep runs, printed rather than acted on, so the operator sees
|
||||
// the exact set before authorising any send.
|
||||
console.log(`扫描最近 ${opts.hours} 小时进入 manual_review、卡片没跟上的条目:\n`);
|
||||
let found = 0;
|
||||
for (const kind of KINDS) {
|
||||
const rows = await listRecentManualReviewIds(kind, now - windowMs, 500);
|
||||
if (!rows.length) continue;
|
||||
const ids = rows.map((r) => r.id);
|
||||
const [cardAt, entryAt] = await Promise.all([
|
||||
settingKeyTimes(ids.map((id) => sentCardKey(kind, id))),
|
||||
lastEntryTimes(kind, "manual_review", ids),
|
||||
]);
|
||||
for (const r of rows) {
|
||||
const at = entryAt.get(r.id) ?? r.statusChangedAt ?? 0;
|
||||
const card = cardAt.get(sentCardKey(kind, r.id));
|
||||
if (!reviewCardMissing(card, at)) continue;
|
||||
found++;
|
||||
const fmt = (n: number | undefined) => (n ? new Date(n).toLocaleString("sv") : "从未发过");
|
||||
console.log(` ${kind.padEnd(8)} ${r.id} 进入复核 ${fmt(at)} 上次卡片 ${fmt(card)}`);
|
||||
}
|
||||
}
|
||||
if (!found) { console.log(" (无)"); return; }
|
||||
|
||||
if (!opts.send) {
|
||||
console.log(`\n共 ${found} 条。加 --send 才会真的发。`);
|
||||
return;
|
||||
}
|
||||
// RESEND_BATCH caps each kind per pass, so loop until a pass sends nothing.
|
||||
console.log(`\n开始补发…`);
|
||||
let total = 0;
|
||||
for (let pass = 0; pass < 10; pass++) {
|
||||
const n = await reconcileReviewCards(Date.now(), windowMs);
|
||||
if (!n) break;
|
||||
total += n;
|
||||
}
|
||||
console.log(`补发完成: ${total} 条(仍在 manual_review 的才会发;已被人复核掉的自动跳过)。`);
|
||||
}
|
||||
|
||||
await main();
|
||||
process.exit(0);
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* One-off 存量 backfill: mirror ALL existing evidence media (screenshots +
|
||||
* demo videos) for terminal-state bug/feature/security items to TOS. Mirrors
|
||||
* the logic of `POST /api/internal/evidence/backfill-tos` (routes.ts:2378) so it
|
||||
* can run out-of-process — the running control-server predates that route
|
||||
* (stale --hot reload) and 404s it.
|
||||
*
|
||||
* Idempotent (files already mirrored at matching size are skipped), best-effort
|
||||
* (unreachable nodes contribute zero). Run from repo root so bun auto-loads .env
|
||||
* (needs TOS_ACCESS_KEY): bun scripts/backfill_tos_oneoff.ts
|
||||
*/
|
||||
import { listTasks, listFeatures, listSecurity, type EvidenceOwnerKind } from "../control-server/src/db";
|
||||
import { mirrorEvidenceToTos } from "../control-server/src/evidence_tos";
|
||||
import { isTosConfigured } from "../control-server/src/tos";
|
||||
|
||||
const TERMINAL = new Set(["manual_review", "fix_succeeded", "fix_failed", "unreproducible", "completed"]);
|
||||
|
||||
const targets: { kind: EvidenceOwnerKind; id: string }[] = [
|
||||
...listTasks().filter((t) => TERMINAL.has(t.status)).map((t) => ({ kind: "bug" as const, id: t.id })),
|
||||
...listFeatures().filter((f) => TERMINAL.has(f.status)).map((f) => ({ kind: "feature" as const, id: f.id })),
|
||||
...listSecurity().filter((s) => TERMINAL.has(s.status)).map((s) => ({ kind: "security" as const, id: s.id })),
|
||||
];
|
||||
|
||||
if (!isTosConfigured()) {
|
||||
console.error("TOS not configured (TOS_ACCESS_KEY empty). Run from repo root so .env loads. Aborting.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log(`[backfill] ${targets.length} terminal candidates; POOL=3`);
|
||||
const t0 = Date.now();
|
||||
let uploaded = 0, skipped = 0, failed = 0, items = 0, done = 0;
|
||||
const POOL = 3;
|
||||
for (let i = 0; i < targets.length; i += POOL) {
|
||||
const batch = targets.slice(i, i + POOL);
|
||||
const results = await Promise.all(
|
||||
batch.map(async (t) => {
|
||||
const r = await mirrorEvidenceToTos(t.kind, t.id);
|
||||
done++;
|
||||
if (r && (r.uploaded || r.failed)) {
|
||||
console.log(` [${done}/${targets.length}] ${t.kind} ${t.id.slice(0, 8)} up=${r.uploaded} skip=${r.skipped} fail=${r.failed}`);
|
||||
}
|
||||
return r;
|
||||
}),
|
||||
);
|
||||
for (const r of results) {
|
||||
if (!r) continue;
|
||||
items++; uploaded += r.uploaded; skipped += r.skipped; failed += r.failed;
|
||||
}
|
||||
}
|
||||
const secs = ((Date.now() - t0) / 1000).toFixed(1);
|
||||
console.log(`\n[backfill] DONE in ${secs}s`);
|
||||
console.log(JSON.stringify({ candidates: targets.length, itemsMirrored: items, uploaded, skipped, failed }, null, 2));
|
||||
@@ -0,0 +1,26 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Build the self-contained satellite bundle and stamp the git SHA it was built
|
||||
# from into dist/VERSION. The satellite reads that file at boot and reports it
|
||||
# as its version (see resolveVersion() in satellite/src/index.ts), so the deploy
|
||||
# panel can show running-vs-latest drift and confirm a deploy landed.
|
||||
#
|
||||
set -euo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
SHA="$(git rev-parse --short HEAD 2>/dev/null || echo unknown)"
|
||||
DIRTY=""
|
||||
if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then
|
||||
DIRTY="-dirty"
|
||||
fi
|
||||
VERSION="${SHA}${DIRTY}"
|
||||
|
||||
# Build from entry.ts, not index.ts: the entrypoint dispatches between booting
|
||||
# the server and running the evidence-capture wrapper as a subcommand, so the
|
||||
# single artifact hot-deploy ships now carries BOTH. Previously the wrapper lived
|
||||
# only at <repo>/bin/ on each node's own checkout, and deploying a satellite left
|
||||
# it untouched.
|
||||
bun build satellite/src/entry.ts --target=bun --outfile satellite/dist/satellite.js
|
||||
printf '%s' "$VERSION" > satellite/dist/VERSION
|
||||
|
||||
echo "[build:satellite] built version ${VERSION} -> satellite/dist/satellite.js"
|
||||
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 抓取执行期提示词的「重构前」快照,作为模板化改造的逐字节基准。
|
||||
*
|
||||
* bun scripts/capture_prompt_fixtures.ts
|
||||
*
|
||||
* ## 为什么需要它
|
||||
*
|
||||
* P3 把 7 份硬编码提示词的正文抽成 `{{placeholder}}` 模板。这类改造唯一真正的风险
|
||||
* 是**悄悄改了措辞** —— 少一个空格、把某个条件分支的边界挪了一格,提示词照样能跑,
|
||||
* 只是行为变了,而且要几十次真机执行之后才可能被人察觉。
|
||||
*
|
||||
* 所以按 `shared/src/eval_prompt.ts` 的先例:先把现有构造函数在一组输入矩阵下的输出
|
||||
* 原样存成夹具,改造完再断言新的渲染结果与夹具**逐字节相同**。夹具是在重构之前从
|
||||
* 真实代码里抓的,不是照着新实现反推的 —— 这是它能当基准的唯一理由。
|
||||
*
|
||||
* 之后这些夹具继续充当回归基线:任何一次提示词措辞改动都会让测试变红,必须显式重新
|
||||
* 抓取,于是「这次改了哪句话」在 code review 里是看得见的,而不是埋在一个 800 行的
|
||||
* 模板字符串的 diff 里。
|
||||
*
|
||||
* ## 确定性
|
||||
*
|
||||
* 构造函数读若干环境变量(AI_WORKSPACE / BASE_BRANCH / MAX_ROUNDS / 工具命令行)。
|
||||
* 这里把它们全部钉死,测试端用同一套值,否则夹具在另一台机器上就对不上。
|
||||
*/
|
||||
import { mkdirSync, writeFileSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import {
|
||||
PROMPT_FIXTURE_ENV, PROMPT_FIXTURE_EXEC_PATH, promptFixtureCases, FIXTURE_DIR,
|
||||
} from "../satellite/test/prompt_fixture_cases";
|
||||
|
||||
for (const [k, v] of Object.entries(PROMPT_FIXTURE_ENV)) process.env[k] = v;
|
||||
Object.defineProperty(process, "execPath", { value: PROMPT_FIXTURE_EXEC_PATH, configurable: true });
|
||||
|
||||
const outDir = path.resolve(import.meta.dir, "..", FIXTURE_DIR);
|
||||
mkdirSync(outDir, { recursive: true });
|
||||
|
||||
const cases = await promptFixtureCases();
|
||||
let n = 0;
|
||||
for (const c of cases) {
|
||||
writeFileSync(path.join(outDir, `${c.name}.txt`), c.render(), "utf8");
|
||||
n++;
|
||||
}
|
||||
console.log(`[fixtures] 已写入 ${n} 份到 ${outDir}`);
|
||||
console.log(`[fixtures] 现在跑 \`bun test satellite/test/run_prompt_parity.test.ts\` 应当全绿(此刻是自己比自己)。`);
|
||||
console.log(`[fixtures] 重构之后再跑一次 —— 那一次才有意义。`);
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* 部署前的迁移体检:`drizzle/*.sql` 声明过的东西,生产库里是不是真的有。
|
||||
*
|
||||
* bun scripts/check-migrations.ts # 只读;有缺失时退出码 1
|
||||
*
|
||||
* ## 为什么需要它
|
||||
*
|
||||
* 这个仓库没有迁移执行器:`deploy.sh` 从不碰 schema,迁移靠人手工跑
|
||||
* `apply-migration.ts`。于是存在一个谁都没注意的耦合 —— **谁部署,谁替所有人的
|
||||
* 未落库迁移买单**。你合并的分支不需要迁移,但 `git pull` 会把别人**已经合进 main、
|
||||
* 迁移却还没跑**的提交一起带上生产,重启之后所有相关查询直接 500。
|
||||
*
|
||||
* 2026-08-22 就这么炸过一次:一次知识库改动的部署顺带把 `tasks.method_coverage`
|
||||
* 的代码拉了上去,那一列的迁移从没执行过,`/api/tasks` 全线 500,面板打不开四分钟。
|
||||
* 部署脚本的验证步骤拦住了(这是它做对的地方),但那时服务已经重启完了 —— 拦得太晚。
|
||||
*
|
||||
* 所以这一步放在 **pull 之后、restart 之前**:此时新代码已经在盘上,老进程还在跑,
|
||||
* 缺列还只是一个可以从容处理的告警,而不是一次线上事故。
|
||||
*
|
||||
* ## 为什么连列也要查
|
||||
*
|
||||
* `apply-migration.ts` 原来只复核表和索引。而生产上真正炸掉的是 **ADD COLUMN** ——
|
||||
* 加一列不建表也不建索引,原来的复核完全看不见它。这里三种都查。
|
||||
*
|
||||
* ## 它不做什么
|
||||
*
|
||||
* 不自动执行迁移。DDL 是对生产的写操作,按 CLAUDE.md 归操作者决定;这个脚本只负责
|
||||
* 让人在**正确的时刻**知道该跑哪一条。
|
||||
*/
|
||||
import { readFileSync, readdirSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import pg from "pg";
|
||||
|
||||
const DIR = path.resolve(import.meta.dir, "../drizzle");
|
||||
|
||||
const raw = process.env.DATABASE_URL;
|
||||
if (!raw) {
|
||||
console.error("[migrate-check] DATABASE_URL 未设置(见 .env)");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 明知故犯的清单:声明过、但**故意没有**落到生产的对象。
|
||||
*
|
||||
* 一个会因为历史包袱天天变红的检查,等于没有检查 —— 人会学会忽略它,然后连真的
|
||||
* 那次也一起忽略。所以豁免必须存在,但每一条都要写清楚为什么,并且这个文件在
|
||||
* code review 里是显眼的。
|
||||
*
|
||||
* 加一条之前先问:这是"当年设计废弃了"还是"该跑没跑"?后者请去跑迁移。
|
||||
*/
|
||||
const ACKNOWLEDGED: Record<string, string> = {
|
||||
// 0002 设计的 SpecDelta 事件表。这条路后来被 run_conditions 取代(执行器直接写
|
||||
// 结构化条件,不再另存一份 delta 事件),全仓库没有任何代码引用它 —— 建出来只会
|
||||
// 是一张永远空着的表。
|
||||
spec_delta_events: "设计已被 run_conditions 取代,全仓库无引用(0002)",
|
||||
// 「验证」曾是 features.deliverable='report' 的 feature,0036 把它搬进 verifications,
|
||||
// 0037 删掉这三列和 audit_notes(已并入 verification_notes)。0037 跑之前它们都还在,
|
||||
// 跑之后不在 —— 两种状态都是对的,所以豁免。
|
||||
audit_notes: "已并入 verification_notes,0037 删除(0034)",
|
||||
"features.deliverable": "验证线已拆成 verifications 表,0037 删除(0032)",
|
||||
"features.audit_report": "移到 verifications.audit_report,0037 删除(0033)",
|
||||
"features.verify_spec": "移到 verifications.verify_spec,0037 删除(0035)",
|
||||
};
|
||||
|
||||
interface Want {
|
||||
file: string;
|
||||
kind: "表" | "索引" | "列";
|
||||
/** 表名/索引名,或 "表.列"。 */
|
||||
name: string;
|
||||
table?: string;
|
||||
column?: string;
|
||||
}
|
||||
|
||||
const wants: Want[] = [];
|
||||
for (const file of readdirSync(DIR).filter((f) => f.endsWith(".sql")).sort()) {
|
||||
// 注释里出现的 SQL 片段不算数 —— 这个仓库的迁移写了大量解释性注释,里面常有
|
||||
// 示例语句,把它们当成"声明过的对象"会造出永远修不好的假缺失。
|
||||
const sql = readFileSync(path.join(DIR, file), "utf8").replace(/^\s*--.*$/gm, "");
|
||||
for (const m of sql.matchAll(/CREATE TABLE\s+(?:IF NOT EXISTS\s+)?"?([\w.]+)"?/gi))
|
||||
wants.push({ file, kind: "表", name: m[1] });
|
||||
for (const m of sql.matchAll(/CREATE (?:UNIQUE )?INDEX\s+(?:IF NOT EXISTS\s+)?"?([\w.]+)"?/gi))
|
||||
wants.push({ file, kind: "索引", name: m[1] });
|
||||
for (const m of sql.matchAll(
|
||||
/ALTER TABLE\s+"?([\w.]+)"?\s+ADD COLUMN\s+(?:IF NOT EXISTS\s+)?"?([\w.]+)"?/gi,
|
||||
)) wants.push({ file, kind: "列", name: `${m[1]}.${m[2]}`, table: m[1], column: m[2] });
|
||||
}
|
||||
|
||||
// URL 解析与 db_client.ts 一致:IPv6 字面量必须以裸 host 传给 pg。
|
||||
const u = new URL(raw);
|
||||
const pool = new pg.Pool({
|
||||
host: u.hostname.replace(/^\[|\]$/g, ""),
|
||||
port: u.port ? Number(u.port) : 5432,
|
||||
user: decodeURIComponent(u.username),
|
||||
password: decodeURIComponent(u.password),
|
||||
database: u.pathname.replace(/^\//, ""),
|
||||
connectionTimeoutMillis: 30_000,
|
||||
});
|
||||
|
||||
// 永远不要把 URL 本身打出来 —— 它带着生产口令。
|
||||
console.log(`[migrate-check] 目标库 ${u.pathname.replace(/^\//, "")} @ ${u.hostname}`);
|
||||
console.log(`[migrate-check] ${wants.length} 个对象,来自 ${new Set(wants.map((w) => w.file)).size} 份迁移`);
|
||||
|
||||
try {
|
||||
const cols = new Set<string>();
|
||||
for (const r of (await pool.query<{ t: string; c: string }>(
|
||||
`select table_name as t, column_name as c from information_schema.columns where table_schema = 'public'`,
|
||||
)).rows) cols.add(`${r.t}.${r.c}`);
|
||||
|
||||
const missing: Want[] = [];
|
||||
for (const w of wants) {
|
||||
if (w.kind === "列") {
|
||||
if (!cols.has(w.name)) missing.push(w);
|
||||
continue;
|
||||
}
|
||||
const r = await pool.query<{ oid: string | null }>("SELECT to_regclass($1) AS oid", [w.name]);
|
||||
if (!r.rows[0]?.oid) missing.push(w);
|
||||
}
|
||||
|
||||
const excused = missing.filter((m) => ACKNOWLEDGED[m.name]);
|
||||
const blocking = missing.filter((m) => !ACKNOWLEDGED[m.name]);
|
||||
for (const e of excused) console.log(`[migrate-check] 豁免 ${e.kind} ${e.name} —— ${ACKNOWLEDGED[e.name]}`);
|
||||
|
||||
if (!blocking.length) {
|
||||
console.log(`[migrate-check] \x1b[32m全部已落库\x1b[0m`);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
console.error(`\n[migrate-check] \x1b[31m${blocking.length} 个对象在生产库里不存在:\x1b[0m`);
|
||||
const byFile = new Map<string, Want[]>();
|
||||
for (const m of blocking) byFile.set(m.file, [...(byFile.get(m.file) ?? []), m]);
|
||||
for (const [file, items] of byFile) {
|
||||
console.error(`\n drizzle/${file}`);
|
||||
for (const i of items) console.error(` ${i.kind} ${i.name}`);
|
||||
console.error(` → bun scripts/apply-migration.ts drizzle/${file} # 预演`);
|
||||
console.error(` → bun scripts/apply-migration.ts drizzle/${file} --commit # 执行`);
|
||||
}
|
||||
console.error(`\n[migrate-check] 迁移是对生产的写操作,由人决定要不要跑;跑完再继续部署。`);
|
||||
process.exit(1);
|
||||
} catch (err) {
|
||||
console.error(`[migrate-check] 检查失败: ${(err as Error).message}`);
|
||||
process.exit(2);
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
/**
|
||||
* End-to-end smoke test for 节点健康, runnable on a laptop with nothing else set up.
|
||||
*
|
||||
* bun scripts/check-node-health.ts
|
||||
*
|
||||
* What it actually exercises:
|
||||
* - the REAL satellite sampler (satellite/src/metrics.ts) against this host
|
||||
* - the REAL control-plane rule engine (control-server/src/node_health.ts)
|
||||
* - the wire shape between them, over real HTTP
|
||||
*
|
||||
* What it fakes: only the transport ends. A stub control plane serves the two
|
||||
* internal endpoints the satellite calls, and the satellite side is driven
|
||||
* directly rather than by booting the whole process (which would want a
|
||||
* workspace, adb/xcrun, and a live claim loop).
|
||||
*
|
||||
* It touches NOTHING in production: DATABASE_URL is forced at a closed local
|
||||
* port, and no Lark chat is configured so the alert path is a no-op. Safe to run
|
||||
* on a developer machine at any time.
|
||||
*/
|
||||
|
||||
// Must precede any import that pulls in the DAL — imports are hoisted, so this
|
||||
// has to be its own statement before the rest (see control-server/test/setup_env.ts).
|
||||
process.env.DATABASE_URL ||= "postgres://test:test@127.0.0.1:1/streamline_test";
|
||||
delete process.env.LARK_NOTIFY_CHAT_ID;
|
||||
// Short windows so the run demonstrates firing in seconds instead of minutes.
|
||||
process.env.HEALTH_SUSTAIN_MS ||= "1000";
|
||||
process.env.HEALTH_METRICS_STALE_MS ||= "180000";
|
||||
// Fast sampling so cpuPct has a baseline to diff against within this run — at
|
||||
// the production 15s cadence a 2-second script would only ever see one tick.
|
||||
process.env.METRICS_SAMPLE_MS ||= "700";
|
||||
|
||||
const { startMetricsSampler, readMetrics } = await import("../satellite/src/metrics");
|
||||
const { noteNodeMetrics, judgeNode, __resetHealthState } = await import("../control-server/src/node_health");
|
||||
// Relative, not "@bbbug/shared/*": that alias only resolves from inside a
|
||||
// workspace package, not from scripts/ (same reason push-satellites.ts does it).
|
||||
import type { HeartbeatPayload, NodeMetrics } from "../shared/src/protocol";
|
||||
import type { ClusterNode } from "../shared/src/types";
|
||||
|
||||
const ok = (s: string) => console.log(` \x1b[32m✓\x1b[0m ${s}`);
|
||||
const info = (s: string) => console.log(` \x1b[2m${s}\x1b[0m`);
|
||||
const head = (s: string) => console.log(`\n\x1b[1m${s}\x1b[0m`);
|
||||
let failures = 0;
|
||||
const check = (cond: boolean, s: string) => {
|
||||
if (cond) ok(s);
|
||||
else { failures++; console.log(` \x1b[31m✗ ${s}\x1b[0m`); }
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
head("1. 采集器:真实采样这台机器");
|
||||
|
||||
startMetricsSampler();
|
||||
// Give the slow tier (vm_stat / sysctl / claude auth status) a moment to land.
|
||||
await Bun.sleep(2000);
|
||||
const m = readMetrics();
|
||||
if (!m) { console.error("采样器没有产出快照"); process.exit(1); }
|
||||
|
||||
info(`CPU ${m.cpuPct ?? "—"}% 负载 ${m.loadPct}% ${m.cores} 核`);
|
||||
info(`内存 ${m.memUsedPct ?? "—"}% 共 ${(m.memTotalBytes / 2 ** 30).toFixed(0)}GB swap ${((m.swapUsedBytes ?? 0) / 2 ** 30).toFixed(1)}GB`);
|
||||
info(`磁盘 ${m.diskUsedPct ?? "—"}% 剩余 ${((m.diskFreeBytes ?? 0) / 2 ** 30).toFixed(0)}GB`);
|
||||
info(`Agent ${m.agent.mode} · loggedIn=${m.agent.loggedIn} · ${m.agent.authMethod ?? "—"}`);
|
||||
info(`慢档耗时 ${m.slowMs}ms${m.slowError ? ` · 错误 ${m.slowError}` : ""}`);
|
||||
|
||||
check(m.diskUsedPct !== null, "磁盘已采集");
|
||||
check(m.memUsedPct !== null, "内存已采集(macOS 走 vm_stat,非 os.freemem)");
|
||||
check(m.memUsedPct === null || m.memUsedPct < 97, "内存读数不是 os.freemem 的假高值(应远低于 97%)");
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
head("2. 心跳路径:必须零 I/O");
|
||||
|
||||
const t0 = performance.now();
|
||||
for (let i = 0; i < 200_000; i++) readMetrics();
|
||||
const perCall = ((performance.now() - t0) * 1000) / 200_000;
|
||||
info(`readMetrics() × 200k → ${perCall.toFixed(4)} µs/次`);
|
||||
check(perCall < 1, "心跳读快照是纯属性读取(< 1µs)");
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
head("3. 传输:真实 HTTP,校验 wire shape");
|
||||
|
||||
let received: HeartbeatPayload | null = null;
|
||||
const server = Bun.serve({
|
||||
port: 0,
|
||||
async fetch(req) {
|
||||
const url = new URL(req.url);
|
||||
if (url.pathname === "/api/internal/nodes/heartbeat") {
|
||||
received = (await req.json()) as HeartbeatPayload;
|
||||
return Response.json({ ok: true });
|
||||
}
|
||||
return Response.json({ ok: true, nodeId: "test" });
|
||||
},
|
||||
});
|
||||
|
||||
const hb: HeartbeatPayload = {
|
||||
nodeId: "test-node", evalInFlight: 0, evalCapacity: 3, fixInFlight: 0, fixCapacity: 1,
|
||||
metrics: readMetrics(), pendingVersion: null, queuedDeploy: true,
|
||||
};
|
||||
const res = await fetch(`http://127.0.0.1:${server.port}/api/internal/nodes/heartbeat`, {
|
||||
method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(hb),
|
||||
});
|
||||
check(res.ok, "心跳 POST 成功");
|
||||
check(received !== null && received!.metrics !== null, "metrics 字段完整穿过 JSON 序列化");
|
||||
check(
|
||||
received?.metrics?.agent !== undefined && typeof received?.metrics?.agent.authGateMs === "number",
|
||||
"AgentHealth 嵌套结构完整",
|
||||
);
|
||||
const wireBytes = JSON.stringify(hb).length;
|
||||
info(`心跳报文 ${wireBytes} 字节(加指标前约 ${JSON.stringify({ ...hb, metrics: null }).length} 字节)`);
|
||||
check(wireBytes < 2000, "报文仍然很小(< 2KB)");
|
||||
server.stop();
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
head("4. 规则引擎:阈值、迟滞、冷却");
|
||||
|
||||
__resetHealthState();
|
||||
const NOW = Date.now();
|
||||
const node = (over: Partial<ClusterNode> = {}): ClusterNode => ({
|
||||
id: "test-node", baseUrl: "http://x", platform: "ios", status: "online",
|
||||
lastSeenAt: NOW, evalInFlight: 0, evalCapacity: 3, fixInFlight: 0, fixCapacity: 1,
|
||||
version: "v", createdAt: 0, live: true, ...over,
|
||||
} as ClusterNode);
|
||||
const withDisk = (pct: number, over: Partial<NodeMetrics> = {}): NodeMetrics =>
|
||||
({ ...readMetrics()!, at: Date.now(), diskUsedPct: pct, ...over });
|
||||
|
||||
noteNodeMetrics("test-node", withDisk(50));
|
||||
check(judgeNode(node(), NOW).health.level === "ok", "50% 磁盘 → 正常");
|
||||
|
||||
noteNodeMetrics("test-node", withDisk(93));
|
||||
const spike = judgeNode(node(), NOW);
|
||||
check(spike.health.level === "critical", "93% 磁盘 → 面板立即标红");
|
||||
check(spike.toNotify.length === 0, "但未达持续时长,不推送飞书(避免为瞬时尖峰打扰)");
|
||||
|
||||
noteNodeMetrics("test-node", withDisk(93));
|
||||
const sustained = judgeNode(node(), NOW + 5000);
|
||||
check(sustained.toNotify.length === 1, "持续超过窗口 → 推送一次");
|
||||
info(`告警文案:${sustained.toNotify[0]?.message}`);
|
||||
|
||||
noteNodeMetrics("test-node", withDisk(93));
|
||||
check(judgeNode(node(), NOW + 6000).toNotify.length === 0, "冷却期内不重复推送");
|
||||
|
||||
noteNodeMetrics("test-node", withDisk(87));
|
||||
check(judgeNode(node(), NOW + 7000).health.level === "critical", "回落到 87%(迟滞带内)仍保持告警");
|
||||
|
||||
noteNodeMetrics("test-node", withDisk(84));
|
||||
check(judgeNode(node(), NOW + 8000).health.level === "ok", "回落到 84%(低于清除线)→ 恢复");
|
||||
|
||||
__resetHealthState();
|
||||
noteNodeMetrics("test-node", withDisk(50, { agent: { ...m.agent, loggedIn: false } }));
|
||||
judgeNode(node(), NOW);
|
||||
const agentAlert = judgeNode(node(), NOW + 5000);
|
||||
check(agentAlert.toNotify.some((a) => a.rule === "agent"), "claude 未登录 → 告警");
|
||||
info(`告警文案:${agentAlert.toNotify.find((a) => a.rule === "agent")?.message}`);
|
||||
|
||||
__resetHealthState();
|
||||
noteNodeMetrics("test-node", withDisk(50, { agent: { ...m.agent, mode: "cursor", loggedIn: null } }));
|
||||
judgeNode(node(), NOW);
|
||||
check(
|
||||
judgeNode(node(), NOW + 5000).health.alerts.every((a) => a.rule !== "agent"),
|
||||
"cursor 引擎节点(loggedIn=null)默认通过,不误报",
|
||||
);
|
||||
|
||||
__resetHealthState();
|
||||
judgeNode(node({ live: false, lastSeenAt: NOW - 90_000 }), NOW);
|
||||
const off = judgeNode(node({ live: false, lastSeenAt: NOW - 90_000 }), NOW + 5000);
|
||||
check(off.toNotify.some((a) => a.rule === "offline"), "节点离线 → 告警");
|
||||
check(off.health.alerts.length === 1, "离线时短路其余规则(不用陈旧数据判断磁盘)");
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
head("5. 容错:采集失败不能变成「一切正常」");
|
||||
|
||||
__resetHealthState();
|
||||
noteNodeMetrics("test-node", withDisk(50, { diskUsedPct: null, diskFreeBytes: null }));
|
||||
const unknown = judgeNode(node(), NOW);
|
||||
check(unknown.health.alerts.every((a) => a.rule !== "disk"), "磁盘未知 → 不误报磁盘告警");
|
||||
info("(面板此时显示「未知」而非 0%,不会被读成「空间充足」)");
|
||||
|
||||
__resetHealthState();
|
||||
noteNodeMetrics("test-node", withDisk(50, { at: Date.now() - 600_000 }));
|
||||
judgeNode(node(), NOW);
|
||||
const stale = judgeNode(node(), NOW + 5000);
|
||||
check(stale.health.alerts.some((a) => a.rule === "metrics_stale"), "心跳正常但采集器停摆 → 指标停更告警");
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
console.log(
|
||||
failures === 0
|
||||
? `\n\x1b[32m全部通过\x1b[0m — 采集、传输、判定、容错四段链路都验证过了。`
|
||||
: `\n\x1b[31m${failures} 项失败\x1b[0m`,
|
||||
);
|
||||
process.exit(failures === 0 ? 0 : 1);
|
||||
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* One-off: close a Bits MR via POST /openapi/merge_request/close.
|
||||
*
|
||||
* Usage: bun scripts/close_mr.ts <mrId> [bitsUser]
|
||||
* bitsUser (email local-part) is sent as x-bits-user so the call acts on
|
||||
* behalf of someone with permission to close the MR. Omit to try as the
|
||||
* service account.
|
||||
*/
|
||||
const API_BASE = (process.env.BITS_API_BASE || "https://bits.bytedance.net").replace(/\/+$/, "");
|
||||
const SERVICE_TOKEN = process.env.BITS_TOKEN || "08bd601b7da6b14dfe7a0f076f4493f5";
|
||||
|
||||
const mrId = Bun.argv[2];
|
||||
const bitsUser = Bun.argv[3] || null;
|
||||
if (!mrId) { console.error("usage: bun scripts/close_mr.ts <mrId> [bitsUser]"); process.exit(2); }
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${SERVICE_TOKEN}`,
|
||||
};
|
||||
if (bitsUser) headers["x-bits-user"] = bitsUser;
|
||||
|
||||
const GROUP = process.env.BITS_GROUP_NAME_ANDROID || "TikTok_Android";
|
||||
const PROJECT = Number(process.env.BITS_PROJECT_GITLAB_ID_ANDROID || 113818);
|
||||
const body = {
|
||||
mr_id: Number(mrId),
|
||||
group_name: GROUP,
|
||||
host_group_name: GROUP,
|
||||
project_gitlab_id: PROJECT,
|
||||
};
|
||||
|
||||
console.log("=== close MR ===");
|
||||
console.log(JSON.stringify({ mrId, bitsUser, body }, null, 2));
|
||||
|
||||
const res = await fetch(`${API_BASE}/openapi/merge_request/close`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
});
|
||||
const raw = await res.text();
|
||||
let json: any = null;
|
||||
try { json = JSON.parse(raw); } catch { /* keep raw */ }
|
||||
|
||||
console.log("\n=== response ===");
|
||||
console.log("HTTP", res.status);
|
||||
console.log(json ? JSON.stringify(json, null, 2) : raw);
|
||||
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env bash
|
||||
# Local DEBUG session: control-plane backend (hot reload) + control-web frontend
|
||||
# (Vite dev) in ONE terminal. The Vite dev server proxies /api + /ws to the LOCAL
|
||||
# backend, so the frontend loads against THIS backend — not the prod FaaS one.
|
||||
# (import.meta.env.DEV → API_BASE="" → relative → proxy → local.) Ctrl-C stops both.
|
||||
#
|
||||
# Note: the local backend still uses DATABASE_URL from .env — set it to a debug DB
|
||||
# if you don't want to touch prod data.
|
||||
set -uo pipefail
|
||||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||||
cd "$ROOT"
|
||||
|
||||
echo "[debug] starting control-plane backend (hot) + control-web (vite dev)…"
|
||||
|
||||
# backend — hot reload; STREAMLINE_DEBUG marks the session as debug.
|
||||
STREAMLINE_DEBUG=1 bun run --hot control-server/src/index.ts &
|
||||
BACK=$!
|
||||
|
||||
# frontend — Vite dev server (proxies /api + /ws to the local backend).
|
||||
( cd control-web && bun run dev ) &
|
||||
FRONT=$!
|
||||
|
||||
# stop both on Ctrl-C / exit / if either child dies. (Portable to macOS bash 3.2,
|
||||
# which lacks `wait -n` — poll liveness instead.)
|
||||
trap 'kill "$BACK" "$FRONT" 2>/dev/null' INT TERM EXIT
|
||||
while kill -0 "$BACK" 2>/dev/null && kill -0 "$FRONT" 2>/dev/null; do
|
||||
sleep 1
|
||||
done
|
||||
@@ -0,0 +1,241 @@
|
||||
/**
|
||||
* Local preview proxy for UI work — real fleet data, zero production risk.
|
||||
*
|
||||
* bun scripts/dev-preview.ts # then: API_TARGET=http://127.0.0.1:5999 bun run dev:web
|
||||
*
|
||||
* Why this exists rather than just running the control server locally: the
|
||||
* control plane is not a read-only process. Its node sweep marks satellites
|
||||
* offline and rolls their in-flight work back, and the health sweep sends Lark
|
||||
* alerts to the real group. Pointing a laptop copy at the production DATABASE_URL
|
||||
* to look at some CSS would do both. So this proxies the read-only internal API
|
||||
* instead and REFUSES anything that isn't a GET — the guarantee is structural,
|
||||
* not a promise to be careful.
|
||||
*
|
||||
* It also synthesises node metrics. The deployed control plane predates 节点健康,
|
||||
* so /api/nodes carries no `metrics` or `health` yet and the panel would render
|
||||
* its (correct, but not very informative) 「卫星版本过旧」 empty state. With
|
||||
* --synth the proxy fills those two fields in so the panel can be reviewed
|
||||
* before anything ships. The numbers are FABRICATED and labelled as such in the
|
||||
* console; everything else on screen is the real fleet.
|
||||
*/
|
||||
|
||||
import { defaultProductProfiles } from "../shared/src/product_profiles";
|
||||
|
||||
const UPSTREAM = (process.env.PREVIEW_UPSTREAM || "https://streamline-internal-api.bytedance.net").replace(/\/$/, "");
|
||||
const PORT = Number(process.env.PREVIEW_PORT || 5999);
|
||||
const SYNTH = process.argv.includes("--synth");
|
||||
/** Identity /api/me reports locally, so the 管理 overlay is reachable. */
|
||||
const PREVIEW_ADMIN = process.env.PREVIEW_ADMIN || "jiaopengwen";
|
||||
/** In-memory Bits token flag for this preview process only. */
|
||||
let previewHasBitsToken = false;
|
||||
|
||||
interface Node { id: string; clusters?: string[]; live?: boolean; lastSeenAt: number; [k: string]: unknown }
|
||||
|
||||
/**
|
||||
* Deterministic per-node pseudo-metrics: same node, same numbers on every
|
||||
* reload, so a review isn't chasing values that move under it. Seeded off the
|
||||
* node id, with a couple of deliberately unhealthy nodes so the alert states
|
||||
* (and the ordering that floats them up) are actually visible.
|
||||
*/
|
||||
function synthMetrics(node: Node, index: number) {
|
||||
let h = 2166136261;
|
||||
for (const ch of node.id) { h ^= ch.charCodeAt(0); h = Math.imul(h, 16777619); }
|
||||
const r = (n: number, lo: number, hi: number) => lo + (Math.abs(h >> n) % (hi - lo));
|
||||
|
||||
// Make exactly one node disk-critical and one logged-out, chosen by POSITION
|
||||
// rather than by a hash of the name: a hash left it to chance whether any node
|
||||
// landed in an unhealthy branch at all, and the first run produced seven
|
||||
// healthy nodes — which is precisely the state a review of the alert UI cannot
|
||||
// use. Position guarantees both states are on screen for any fleet of ≥ 5.
|
||||
const sickDisk = index === 1;
|
||||
const loggedOut = index === 4;
|
||||
|
||||
const now = Date.now();
|
||||
const diskUsedPct = sickDisk ? 93.4 : r(3, 38, 82);
|
||||
return {
|
||||
at: now, cores: r(5, 8, 16),
|
||||
cpuPct: r(7, 4, 78), loadPct: r(9, 10, 90),
|
||||
uptimeSec: r(11, 3600, 3_000_000),
|
||||
diskUsedPct, diskFreeBytes: Math.round((100 - diskUsedPct) / 100 * 500e9),
|
||||
memTotalBytes: 32 * 2 ** 30, memUsedPct: r(13, 35, 88),
|
||||
swapUsedBytes: r(15, 0, 9) * 2 ** 30,
|
||||
slowAt: now - 12_000, slowError: null, slowMs: r(17, 120, 400),
|
||||
agent: {
|
||||
mode: "pty",
|
||||
loggedIn: loggedOut ? false : true,
|
||||
authMethod: "claude.ai",
|
||||
checkedAt: now - 12_000,
|
||||
error: null,
|
||||
authGateMs: 0,
|
||||
},
|
||||
claudeUsage: {
|
||||
at: now - 40_000, ms: 280 + (index * 17) % 90, error: null,
|
||||
email: `sat-${index}@bytedance.com`, plan: "max",
|
||||
// Every other synthetic node shares a machine with its predecessor, so
|
||||
// the dedup path is what the preview actually exercises.
|
||||
machineId: `mach-${Math.floor(index / 2)}`,
|
||||
fiveHour: { usedPct: loggedOut ? 0 : r(19, 8, 92), resetsAt: now + 3 * 3600_000 },
|
||||
sevenDay: { usedPct: loggedOut ? 12 : r(21, 20, 80), resetsAt: now + 2 * 86400_000 },
|
||||
sevenDaySonnet: null, sevenDayOpus: null,
|
||||
quotaAt: now - 40_000,
|
||||
tokens: {
|
||||
input: 2_000_000 + index * 750_000,
|
||||
output: 180_000 + index * 40_000,
|
||||
cacheRead: 8_000_000 + index * 1_200_000,
|
||||
cacheCreation: 400_000 + index * 80_000,
|
||||
},
|
||||
tokensAsOf: new Date(now).toISOString().slice(0, 10),
|
||||
startedAt: now - (60 + index * 20) * 86_400_000,
|
||||
apiCostUsd: null,
|
||||
daily: synthDaily(now, index),
|
||||
dailyFromAt: now - Math.min(60 + index * 20, 24) * 86_400_000,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A ~24-day series with a couple of idle days, so 覆盖/活跃 differ in the preview. */
|
||||
function synthDaily(now: number, index: number) {
|
||||
const out = [];
|
||||
for (let back = 23; back >= 0; back--) {
|
||||
const idle = (back + index) % 7 === 0;
|
||||
const scale = idle ? 0 : 1 + ((back * 7 + index * 13) % 9) / 4;
|
||||
out.push({
|
||||
date: new Date(now - back * 86_400_000).toISOString().slice(0, 10),
|
||||
tokens: {
|
||||
input: Math.round(60_000 * scale),
|
||||
output: Math.round(9_000 * scale),
|
||||
cacheRead: Math.round(1_400_000 * scale),
|
||||
cacheCreation: Math.round(70_000 * scale),
|
||||
},
|
||||
apiCostUsd: 1.1 * scale,
|
||||
});
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function synthHealth(node: Node, m: ReturnType<typeof synthMetrics>) {
|
||||
const alerts: unknown[] = [];
|
||||
if (!node.live) {
|
||||
alerts.push({ rule: "offline", severity: "critical", message: `节点离线,最后心跳 ${Math.round((Date.now() - node.lastSeenAt) / 60000)} 分钟前`, since: Date.now() - 600_000 });
|
||||
} else {
|
||||
if (m.diskUsedPct >= 90) {
|
||||
alerts.push({ rule: "disk", severity: "critical", message: `磁盘占用 ${m.diskUsedPct.toFixed(1)}%(阈值 90%),剩余 ${(m.diskFreeBytes / 2 ** 30).toFixed(0)}GB`, since: Date.now() - 900_000 });
|
||||
}
|
||||
if (m.agent.loggedIn === false) {
|
||||
alerts.push({ rule: "agent", severity: "critical", message: "claude 未登录(本地凭据检查未通过),需在该节点执行 claude /login", since: Date.now() - 300_000 });
|
||||
}
|
||||
}
|
||||
const level = alerts.length ? "critical" : "ok";
|
||||
return { level, alerts };
|
||||
}
|
||||
|
||||
const server = Bun.serve({
|
||||
port: PORT,
|
||||
hostname: "127.0.0.1",
|
||||
async fetch(req) {
|
||||
const url = new URL(req.url);
|
||||
|
||||
// Structural read-only guarantee: a mutating request never reaches upstream.
|
||||
if (req.method !== "GET" && req.method !== "HEAD") {
|
||||
// Local-only: remember a pasted Bits token so the MR-help card can hide
|
||||
// step 3 after save. Never forwarded upstream.
|
||||
if (req.method === "PUT" && url.pathname === "/api/me/bits-token") {
|
||||
const body = await req.json().catch(() => ({})) as { token?: unknown };
|
||||
const raw = typeof body.token === "string" ? body.token.trim().replace(/^Bearer\s+/i, "") : "";
|
||||
if (!raw || raw.length < 8) {
|
||||
return Response.json({ error: "token 格式不对:粘贴 Bits「My Token」里 Bearer 后面那一串即可" }, { status: 400 });
|
||||
}
|
||||
previewHasBitsToken = true;
|
||||
console.log("[preview] saved Bits token in memory (not sent upstream)");
|
||||
return Response.json({ ok: true, hasToken: true });
|
||||
}
|
||||
console.warn(`[preview] blocked ${req.method} ${url.pathname}`);
|
||||
return Response.json({ error: "preview proxy is read-only" }, { status: 405 });
|
||||
}
|
||||
|
||||
const target = UPSTREAM + url.pathname + url.search;
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch(target, { headers: { accept: "application/json" } });
|
||||
} catch (err) {
|
||||
console.error(`[preview] upstream failed: ${(err as Error).message}`);
|
||||
return Response.json({ error: "upstream unreachable" }, { status: 502 });
|
||||
}
|
||||
|
||||
// Grant admin locally.
|
||||
//
|
||||
// The internal read-only API resolves no identity (it is token-free by
|
||||
// design), so /api/me answers isAdmin:false and the app hides 管理 entirely —
|
||||
// which makes every admin-only panel, including 节点健康, impossible to review
|
||||
// locally. This is a fabricated identity for a localhost-bound preview
|
||||
// proxy; it grants nothing anywhere else, because the real enforcement is
|
||||
// server-side on each mutation route and every mutation is 405'd above.
|
||||
if (url.pathname === "/api/me") {
|
||||
return Response.json({
|
||||
email: `${PREVIEW_ADMIN}@bytedance.com`,
|
||||
isAdmin: true, isSuper: true, isLab: true,
|
||||
hasBitsToken: previewHasBitsToken,
|
||||
});
|
||||
}
|
||||
if (url.pathname === "/api/me/bits-token") {
|
||||
return Response.json({ hasToken: previewHasBitsToken });
|
||||
}
|
||||
// Admin-gated upstream (403 without a session) — serve the list the panel
|
||||
// expects so 通用设置 renders instead of erroring.
|
||||
if (url.pathname === "/api/admins") {
|
||||
return Response.json({ supers: ["jiaopengwen", "daizheng.l"], admins: [] });
|
||||
}
|
||||
|
||||
// Product profiles are not on production yet; the SPA fallback is HTML.
|
||||
if (url.pathname === "/api/product-profiles" || url.pathname === "/api/admin/product-profiles") {
|
||||
const ct = res.headers.get("content-type") || "";
|
||||
if (!res.ok || !ct.includes("json")) {
|
||||
const config = defaultProductProfiles();
|
||||
return Response.json(url.pathname === "/api/product-profiles"
|
||||
? { ...config, profiles: config.profiles.filter((p) => p.enabled) }
|
||||
: config);
|
||||
}
|
||||
}
|
||||
|
||||
if (SYNTH && url.pathname === "/api/nodes" && res.ok) {
|
||||
const snap = await res.json() as { nodes: Node[] };
|
||||
snap.nodes = snap.nodes.map((n, i) => {
|
||||
const metrics = synthMetrics(n, i);
|
||||
return { ...n, metrics, health: synthHealth(n, metrics) };
|
||||
});
|
||||
return Response.json(snap);
|
||||
}
|
||||
|
||||
if (SYNTH && /^\/api\/nodes\/[^/]+\/metrics$/.test(url.pathname)) {
|
||||
// Per-minute history for the trend lines: a plausible walk, not real data.
|
||||
const now = Date.now();
|
||||
const rows = Array.from({ length: 360 }, (_, i) => {
|
||||
const t = now - (360 - i) * 60_000;
|
||||
const wobble = Math.sin(i / 24) * 8;
|
||||
return {
|
||||
nodeId: url.pathname.split("/")[3], at: Math.floor(t / 60_000) * 60_000,
|
||||
cpuAvg: 40 + wobble, cpuMax: 52 + wobble,
|
||||
memAvg: 60 + wobble / 2, memMax: 66 + wobble / 2,
|
||||
diskAvg: 70 + i / 60, diskMax: 71 + i / 60,
|
||||
loadMax: 55 + wobble, swapMaxBytes: 2 * 2 ** 30, samples: 4,
|
||||
};
|
||||
});
|
||||
return Response.json({ rows });
|
||||
}
|
||||
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
headers: { "content-type": res.headers.get("content-type") || "application/json" },
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
console.log(`[preview] 只读代理 http://127.0.0.1:${server.port} → ${UPSTREAM}`);
|
||||
console.log(`[preview] 非 GET 请求一律 405,不会对生产产生任何写入`);
|
||||
console.log(`[preview] /api/me 伪装成管理员 ${PREVIEW_ADMIN}(否则内网 API 无身份,管理入口不显示)`);
|
||||
if (SYNTH) {
|
||||
console.log(`[preview] \x1b[33m--synth 已开启:节点指标与健康状态是伪造的\x1b[0m(集群标签、节点名、在线状态仍是真实的)`);
|
||||
} else {
|
||||
console.log(`[preview] 未开 --synth:节点健康面板会显示「未上报指标」(线上控制面还没这个功能,属正常)`);
|
||||
}
|
||||
console.log(`\n下一步,另开一个终端:\n API_TARGET=http://127.0.0.1:${server.port} bun run dev:web\n`);
|
||||
@@ -0,0 +1,240 @@
|
||||
#!/usr/bin/env bash
|
||||
# ---------------------------------------------------------------------------
|
||||
# download-task.sh — download one Streamline task's whole execution workspace
|
||||
# (reports, every round's logs, evidence incl. videos, TestSpec/EffectiveSpec,
|
||||
# Conditions.jsonl, images, manifests) as <id>.zip.
|
||||
#
|
||||
# bash scripts/download-task.sh <id> [--out DIR] [--direct] [--node HOST] [--fallback]
|
||||
#
|
||||
# The zip is produced ON THE SATELLITE by its workspace-archive endpoint, so
|
||||
# the content matches tarring the per-task execution dir directly — minus the
|
||||
# TikTok source checkout, built APK/AAB/IPA and artifacts/ (hook/XP modules +
|
||||
# post-run instrumentation: jacoco, tracking/AppLog, open-audit), excluded
|
||||
# server-side.
|
||||
#
|
||||
# Two ways to reach that endpoint:
|
||||
# default via the control plane proxy /api/<kind>/<id>/workspace-archive
|
||||
# (requires a deployed control plane)
|
||||
# --direct straight to the home satellite's /sat/.../workspace-archive
|
||||
# (only the satellite needs the new code; the node base URL is
|
||||
# resolved from read-only /api/nodes, or pinned with --node;
|
||||
# set CLUSTER_SECRET if the cluster requires the shared header)
|
||||
#
|
||||
# Requires company intranet access to the read API
|
||||
# (STREAMLINE_API, default https://streamline-internal-api.bytedance.net).
|
||||
# Read-only: every request is a GET.
|
||||
#
|
||||
# --fallback also enables a client-side reconstruction (report + logs list +
|
||||
# evidence one by one + DB-side readable report) for nodes still running an
|
||||
# older satellite without the workspace-archive endpoint. It cannot include
|
||||
# the workspace's images/ or be byte-identical to a dir tar.
|
||||
# ---------------------------------------------------------------------------
|
||||
set -uo pipefail
|
||||
|
||||
BASE="${STREAMLINE_API:-https://streamline-internal-api.bytedance.net}"
|
||||
ID=""
|
||||
OUT_DIR="."
|
||||
FALLBACK=0
|
||||
DIRECT=0
|
||||
NODE_PIN=""
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--out) OUT_DIR="$2"; shift 2 ;;
|
||||
--fallback) FALLBACK=1; shift ;;
|
||||
--direct) DIRECT=1; shift ;;
|
||||
--node) NODE_PIN="$2"; shift 2 ;;
|
||||
-*) echo "unknown flag: $1" >&2; exit 2 ;;
|
||||
*) ID="$1"; shift ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ -n "$ID" ] || { echo "usage: download-task.sh <task-id> [--out DIR] [--fallback]" >&2; exit 2; }
|
||||
|
||||
# Accept pasted panel URLs too — pull the uuid out.
|
||||
if [[ "$ID" =~ ([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}) ]]; then
|
||||
ID="${BASH_REMATCH[1]}"
|
||||
else
|
||||
echo "not a uuid: $ID" >&2; exit 2
|
||||
fi
|
||||
|
||||
mkdir -p "$OUT_DIR"
|
||||
OUT_ZIP="$OUT_DIR/$ID.zip"
|
||||
TMP_ZIP="$OUT_ZIP.tmp"
|
||||
trap 'rm -f "$TMP_ZIP"' EXIT
|
||||
|
||||
# --- 1. Which track does this id belong to? --------------------------------
|
||||
KIND=""
|
||||
for k in tasks features security verifications prototypes; do
|
||||
code=$(curl -sS -m 20 -o /dev/null -w '%{http_code}' "$BASE/api/$k/$ID" 2>/dev/null)
|
||||
if [ "$code" = "200" ]; then KIND="$k"; break; fi
|
||||
done
|
||||
|
||||
if [ -z "$KIND" ]; then
|
||||
echo "id not found as task/feature/security/verification/prototype on $BASE" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "id=$ID kind=$KIND"
|
||||
|
||||
# --- 2. Fast path: the satellite zips its real workspace dir ---------------
|
||||
# Workspace key differs for bugs: satellites key bug dirs by meegoId||id.
|
||||
META_URL="$BASE/api/$KIND/$ID"
|
||||
ARCHIVE_URL="$BASE/api/$KIND/$ID/workspace-archive"
|
||||
EXTRA_HEADER=()
|
||||
NODE_DESC="control-plane proxy"
|
||||
|
||||
if [ "$DIRECT" = "1" ]; then
|
||||
META_BODY="$(curl -sS -m 20 "$META_URL" 2>/dev/null || true)"
|
||||
NODE_ID_RESOLVED="$(printf '%s' "$META_BODY" | jq -r '.executorNodeId // empty' 2>/dev/null)"
|
||||
[ -n "$NODE_PIN" ] && NODE_ID_RESOLVED="$NODE_PIN"
|
||||
if [ -z "$NODE_ID_RESOLVED" ]; then
|
||||
echo "task has no executorNodeId (pre-cluster legacy record) — direct mode needs a home node; try without --direct" >&2
|
||||
exit 1
|
||||
fi
|
||||
# --node accepts either a registry node id (resolved via /api/nodes) or a
|
||||
# direct base URL (http://host:port).
|
||||
if [[ "$NODE_ID_RESOLVED" == http://* || "$NODE_ID_RESOLVED" == https://* ]]; then
|
||||
NODE_BASE="${NODE_ID_RESOLVED%/}"
|
||||
else
|
||||
NODE_BASE="$(curl -sS -m 20 "$BASE/api/nodes" 2>/dev/null \
|
||||
| jq -r --arg n "$NODE_ID_RESOLVED" '(.nodes // . // [])[] | select(.id==$n) | .baseUrl' 2>/dev/null | head -1)"
|
||||
if [ -z "$NODE_BASE" ]; then
|
||||
echo "node $NODE_ID_RESOLVED not found in /api/nodes (offline/pruned?)" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
case "$KIND" in
|
||||
tasks)
|
||||
KEY="$(printf '%s' "$META_BODY" | jq -r '.meegoId // .id' 2>/dev/null)"
|
||||
ARCHIVE_URL="$NODE_BASE/sat/ws/$KEY/workspace-archive" ;;
|
||||
features) ARCHIVE_URL="$NODE_BASE/sat/features/$ID/workspace-archive" ;;
|
||||
security) ARCHIVE_URL="$NODE_BASE/sat/security/$ID/workspace-archive" ;;
|
||||
verifications) ARCHIVE_URL="$NODE_BASE/sat/verifications/$ID/workspace-archive" ;;
|
||||
prototypes) ARCHIVE_URL="$NODE_BASE/sat/prototypes/$ID/workspace-archive" ;;
|
||||
esac
|
||||
NODE_DESC="$NODE_ID_RESOLVED ($NODE_BASE)"
|
||||
[ -n "${CLUSTER_SECRET:-}" ] && EXTRA_HEADER=(-H "x-cluster-secret: $CLUSTER_SECRET")
|
||||
fi
|
||||
|
||||
echo "archive endpoint: $NODE_DESC"
|
||||
code=$(curl -sS -m 600 ${EXTRA_HEADER[@]+"${EXTRA_HEADER[@]}"} -o "$TMP_ZIP" -w '%{http_code}' \
|
||||
"$ARCHIVE_URL" 2>/dev/null || true)
|
||||
|
||||
is_zip() { head -c2 "$1" 2>/dev/null | grep -q $'PK'; }
|
||||
|
||||
if [ "$code" = "200" ] && is_zip "$TMP_ZIP"; then
|
||||
# The readable report + full task record live in the control-plane DB, not in
|
||||
# the satellite workspace — append them so the zip is self-contained for
|
||||
# review too. Best-effort; needs local zip/unzip.
|
||||
if command -v python3 >/dev/null 2>&1; then
|
||||
STAGE="$(mktemp -d)"
|
||||
META="$STAGE/meta.json"; HTML="$STAGE/readable-report.html"
|
||||
curl -sS -m 30 -o "$META" "$BASE/api/$KIND/$ID" 2>/dev/null || true
|
||||
curl -sS -m 60 "$BASE/api/$KIND/$ID/readable-report" 2>/dev/null \
|
||||
| jq -r '.html // empty' > "$HTML" 2>/dev/null || true
|
||||
[ -s "$META" ] || META=""
|
||||
[ -s "$HTML" ] || HTML=""
|
||||
if [ -n "$META" ] || [ -n "$HTML" ]; then
|
||||
python3 - "$TMP_ZIP" "$META" "$HTML" <<'PY'
|
||||
import os, sys, zipfile
|
||||
zp, meta, html = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||
extras = {}
|
||||
if meta: extras["meta.json"] = meta
|
||||
if html: extras["readable-report.html"] = html
|
||||
with zipfile.ZipFile(zp) as zin:
|
||||
names = zin.namelist()
|
||||
prefix = names[0].split("/", 1)[0] if names else ""
|
||||
existing = set(names)
|
||||
with zipfile.ZipFile(zp + ".new", "w", zipfile.ZIP_DEFLATED, compresslevel=6) as zout:
|
||||
for item in zin.infolist():
|
||||
zout.writestr(item, zin.read(item.filename))
|
||||
for arc, src in extras.items():
|
||||
dest = f"{prefix}/{arc}" if prefix else arc
|
||||
if dest not in existing:
|
||||
zout.write(src, dest)
|
||||
os.replace(zp + ".new", zp)
|
||||
PY
|
||||
fi
|
||||
rm -rf "$STAGE"
|
||||
fi
|
||||
mv "$TMP_ZIP" "$OUT_ZIP"
|
||||
trap - EXIT
|
||||
echo "saved $(du -h "$OUT_ZIP" | awk '{print $1}') -> $OUT_ZIP"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "workspace-archive unavailable (http=$code) — satellite likely on old code"
|
||||
[ "$FALLBACK" = "1" ] || { echo "re-run with --fallback for piecewise download, or deploy the new satellite" >&2; exit 1; }
|
||||
|
||||
# --- 3. Fallback: reconstruct from the per-file endpoints ------------------
|
||||
echo "fallback: downloading pieces …"
|
||||
STAGE="$(mktemp -d)"
|
||||
trap 'rm -rf "$STAGE"; rm -f "$TMP_ZIP"' EXIT
|
||||
ROOT="$STAGE/$ID"
|
||||
mkdir -p "$ROOT/logs" "$ROOT/evidence"
|
||||
|
||||
fetch() { # <url> <outfile>
|
||||
curl -sS -m 300 -o "$2.tmp" "$1" 2>/dev/null || { rm -f "$2.tmp"; return 1; }
|
||||
# Proxied 404s come back as a small {"error":...} JSON body — treat as absent.
|
||||
if head -c 256 "$2.tmp" 2>/dev/null | grep -q '^{"error"'; then rm -f "$2.tmp"; return 1; fi
|
||||
[ -s "$2.tmp" ] && mv "$2.tmp" "$2" || { rm -f "$2.tmp"; return 1; }
|
||||
}
|
||||
|
||||
case "$KIND" in
|
||||
tasks) REPORT_PATH="fix-report"; REPORT_FILE="FixReport.md" ;;
|
||||
features) REPORT_PATH="fix-report"; REPORT_FILE="ImplReport.md" ;;
|
||||
security) REPORT_PATH="security-report"; REPORT_FILE="EvidenceReport.md" ;;
|
||||
verifications) REPORT_PATH="report"; REPORT_FILE="Verify-Report.md" ;;
|
||||
prototypes) REPORT_PATH="report"; REPORT_FILE="ProtoReport.md" ;;
|
||||
esac
|
||||
|
||||
fetch "$BASE/api/$KIND/$ID" "$ROOT/meta.json"
|
||||
fetch "$BASE/api/$KIND/$ID/$REPORT_PATH" "$ROOT/$REPORT_FILE"
|
||||
[ "$KIND" = "prototypes" ] && fetch "$BASE/api/$KIND/$ID/script" "$ROOT/prototype.js"
|
||||
[ "$KIND" = "verifications" ] && fetch "$BASE/api/$KIND/$ID/audit-report" "$ROOT/audit-report.json"
|
||||
curl -sS -m 60 "$BASE/api/$KIND/$ID/readable-report" 2>/dev/null \
|
||||
| jq -r '.html // empty' > "$ROOT/readable-report.html" 2>/dev/null || true
|
||||
[ -s "$ROOT/readable-report.html" ] || rm -f "$ROOT/readable-report.html"
|
||||
fetch "$BASE/api/$KIND/$ID/asking/history" "$ROOT/asking.json"
|
||||
fetch "$BASE/api/$KIND/$ID/conditions" "$ROOT/conditions.json"
|
||||
|
||||
LOGS=0
|
||||
if fetch "$BASE/api/$KIND/$ID/logs" "$ROOT/logs.json"; then
|
||||
while IFS=$'\t' read -r name; do
|
||||
[ -n "$name" ] || continue
|
||||
fetch "$BASE/api/$KIND/$ID/logs/$(python3 -c 'import urllib.parse,sys;print(urllib.parse.quote(sys.argv[1],safe=""))' "$name")?lines=200000" "$ROOT/logs/$name"
|
||||
LOGS=$((LOGS + 1))
|
||||
done < <(jq -r '.files[]?.name' "$ROOT/logs.json" 2>/dev/null)
|
||||
fi
|
||||
# Old nodes (pre round-split logs) only expose the latest session/eval file.
|
||||
[ -f "$ROOT/logs/session.log" ] || fetch "$BASE/api/$KIND/$ID/log-file?lines=200000" "$ROOT/logs/session.log"
|
||||
[ -f "$ROOT/logs/eval.log" ] || fetch "$BASE/api/$KIND/$ID/eval-log-file?lines=200000" "$ROOT/logs/eval.log"
|
||||
|
||||
EVID=0
|
||||
if fetch "$BASE/api/$KIND/$ID/evidence" "$ROOT/evidence.json"; then
|
||||
while IFS=$'\t' read -r name url; do
|
||||
[ -n "$name" ] || continue
|
||||
if [ -n "$url" ]; then fetch "$url" "$ROOT/evidence/$name"
|
||||
else fetch "$BASE/api/$KIND/$ID/evidence-file/$(python3 -c 'import urllib.parse,sys;print(urllib.parse.quote(sys.argv[1],safe=""))' "$name")" "$ROOT/evidence/$name"; fi
|
||||
[ -s "$ROOT/evidence/$name" ] && EVID=$((EVID + 1))
|
||||
done < <(jq -r '.files[]? | [.name, (.url // "")] | @tsv' "$ROOT/evidence.json" 2>/dev/null)
|
||||
fi
|
||||
[ "$EVID" -gt 0 ] || rmdir "$ROOT/evidence" 2>/dev/null || true
|
||||
|
||||
# Python zipfile instead of the zip CLI: the latter aborts ("Zip file
|
||||
# structure invalid") on evidence names containing unusual characters, which
|
||||
# record-evidence occasionally produces.
|
||||
python3 - "$STAGE" "$ID" "$TMP_ZIP" <<'PY'
|
||||
import os, sys, zipfile
|
||||
stage, root, out = sys.argv[1], sys.argv[2], sys.argv[3]
|
||||
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED, compresslevel=6) as z:
|
||||
for base, _, files in os.walk(os.path.join(stage, root)):
|
||||
for f in files:
|
||||
full = os.path.join(base, f)
|
||||
z.write(full, os.path.relpath(full, stage))
|
||||
PY
|
||||
[ -s "$TMP_ZIP" ] || { echo "packing failed; pieces left in $ROOT" >&2; exit 1; }
|
||||
mv "$TMP_ZIP" "$OUT_ZIP"
|
||||
trap 'rm -rf "$STAGE"' EXIT
|
||||
rm -rf "$STAGE"
|
||||
echo "saved fallback zip (logs=$LOGS evidence=$EVID) $(du -h "$OUT_ZIP" | awk '{print $1}') -> $OUT_ZIP"
|
||||
@@ -0,0 +1,62 @@
|
||||
#!/bin/zsh
|
||||
# Daily evidence-quality analyst pass — reads the metric snapshot the machine job
|
||||
# just wrote, and appends a research-log entry (findings + proposed strategies)
|
||||
# via POST /api/evidmon/findings.
|
||||
#
|
||||
# Runs HERE, on the control-plane host, rather than as a cloud agent: the analysis
|
||||
# needs the control-plane API and the Postgres, both of which are internal-only.
|
||||
#
|
||||
# Same gating idiom as weekly-report.sh — cron invokes this hourly, the script
|
||||
# self-gates on Asia/Shanghai time. Fires at 09:30 北京时间, half an hour after
|
||||
# evidmon-daily.sh has written the day's snapshot.
|
||||
#
|
||||
# Args:
|
||||
# (none) cron mode — gate on Beijing 09:30.
|
||||
# --force bypass the gate and analyse now.
|
||||
|
||||
set -e
|
||||
REPO=${STREAMLINE_REPO:-/Users/fei/BBBBBBug}
|
||||
CLAUDE=${CLAUDE_BIN:-claude}
|
||||
LOG=$HOME/.streamline/evidmon_analyst.log
|
||||
|
||||
cd "$REPO"
|
||||
mkdir -p "$(dirname "$LOG")"
|
||||
|
||||
if [[ "${1:-}" != "--force" ]]; then
|
||||
read HOUR MIN <<< "$(TZ=Asia/Shanghai date +'%H %M')"
|
||||
# cron fires this at :30; the hour gate is what actually selects the run.
|
||||
if [[ "$HOUR" != "09" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
DAY=$(TZ=Asia/Shanghai date +'%Y-%m-%d')
|
||||
|
||||
echo "===== $(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %Z') =====" >> "$LOG"
|
||||
|
||||
# The brief lives in docs/ so the procedure is reviewable and versioned rather
|
||||
# than buried in a cron line.
|
||||
"$CLAUDE" -p "你是 Streamline 证据采集监控的每日分析任务。
|
||||
|
||||
严格按 docs/evidmon-daily-analyst.md 的作业书执行,今天是 ${DAY}(北京时间)。
|
||||
|
||||
流程:
|
||||
1. 读 docs/evidmon-daily-analyst.md 了解完整规程
|
||||
2. 若当天快照尚未生成,先跑 bun run control-server/src/evidmon_daily.ts
|
||||
3. 通过控制面 API 取指标树、既有研究日志、近两日事件(用 x-cluster-secret 认证,
|
||||
密钥在 .env 的 CLUSTER_SECRET,控制面地址在 CONTROL_PLANE_URL)
|
||||
4. 按作业书的标准做分析:先看 L1-E 判定精度,再看变化项、新增 fail/warn 样本、
|
||||
skip 理由、已生效策略的实测效果
|
||||
5. 判据类策略必须先调 POST /api/evidmon/criteria/replay 拿到回放结果再提
|
||||
6. POST /api/evidmon/findings 写入,id 用 ${DAY}
|
||||
|
||||
纪律:
|
||||
- 样本不足就说样本不足,不要把噪声讲成趋势
|
||||
- evidence 必须引用真实数字和具体产物名
|
||||
- 没有新发现的日子写一条短条目即可,不要为了填日志而虚构发现
|
||||
- 你只提出,不批准任何策略;也不要改代码" \
|
||||
--model claude-opus-5 \
|
||||
--allowedTools "Bash,Read,Grep,Glob,WebFetch" \
|
||||
>> "$LOG" 2>&1
|
||||
|
||||
echo "----- done $(TZ=Asia/Shanghai date '+%H:%M:%S') -----" >> "$LOG"
|
||||
@@ -0,0 +1,29 @@
|
||||
#!/bin/zsh
|
||||
# Daily evidence-quality snapshot → Postgres (evidmon_metrics_daily).
|
||||
#
|
||||
# Same shape as weekly-report.sh: the machine clock is NOT Beijing, so cron
|
||||
# invokes this hourly and the script self-gates on Asia/Shanghai time. It fires
|
||||
# once a day at 09:00 北京时间 — early enough that the snapshot is waiting when
|
||||
# the analyst pass runs, and late enough to include the previous night's runs.
|
||||
#
|
||||
# Args:
|
||||
# (none) cron mode — gate on Beijing 09:00.
|
||||
# --force bypass the gate and snapshot now.
|
||||
|
||||
set -e
|
||||
REPO=${STREAMLINE_REPO:-/Users/fei/BBBBBBug}
|
||||
BUN=${BUN_BIN:-/opt/homebrew/bin/bun}
|
||||
LOG=$HOME/.streamline/evidmon_daily.log
|
||||
|
||||
cd "$REPO"
|
||||
mkdir -p "$(dirname "$LOG")"
|
||||
|
||||
if [[ "${1:-}" != "--force" ]]; then
|
||||
HOUR=$(TZ=Asia/Shanghai date +'%H')
|
||||
if [[ "$HOUR" != "09" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "===== $(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %Z') =====" >> "$LOG"
|
||||
"$BUN" run control-server/src/evidmon_daily.ts >> "$LOG" 2>&1
|
||||
@@ -0,0 +1,262 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# executor-restore.sh — restore a snapshot produced by executor-snapshot.sh onto
|
||||
# a remote host, killing any running executor first and starting a fresh one.
|
||||
#
|
||||
# What it does, in order:
|
||||
# 1. sshpass + scp the local tar.gz to the remote host (/tmp).
|
||||
# 2. Read the embedded snapshot/MANIFEST.txt to learn the original REPO path
|
||||
# and which HOME entries were captured.
|
||||
# 3. Kill any running satellite process on the remote (`pgrep -f satellite/src/index.ts`,
|
||||
# then SIGTERM → SIGKILL if still alive after 5s).
|
||||
# 4. Back up the existing repo dir and $HOME/<captured entries> to
|
||||
# $HOME/.streamline-restore-backup/<stamp>/ (in case a rollback is needed).
|
||||
# 5. Extract the archive:
|
||||
# snapshot/streamline/* -> <REPO> (default) or --repo <path>
|
||||
# snapshot/home/* -> $HOME/*
|
||||
# 6. Run `bun install` if node_modules is missing.
|
||||
# 7. Start the satellite in the background via nohup, logging to
|
||||
# $HOME/streamline-satellite.log.
|
||||
#
|
||||
# Usage:
|
||||
# scripts/executor-restore.sh <user@host> <password> <snapshot.tar.gz> \
|
||||
# [--repo <remote_repo_path>] [--port 22] [--no-install] [--no-start] \
|
||||
# [--env KEY=VALUE ...]
|
||||
#
|
||||
# Examples:
|
||||
# scripts/executor-restore.sh fei@fei-ios-01.shiro0.icu 'hunter2' \
|
||||
# ~/streamline-snapshots/executor-fei-ios-01_shiro0_icu-20260723-140102.tar.gz
|
||||
#
|
||||
# # Override the location and inject env before start:
|
||||
# scripts/executor-restore.sh fei@10.0.0.5 'hunter2' snap.tar.gz \
|
||||
# --repo /home/fei/streamline \
|
||||
# --env AGENT_HOST=fei-ios-02.shiro0.icu --env AGENT_PLATFORM=ios
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
# --- pre-scan for --local so we can accept a shorter argv in that mode ------
|
||||
LOCAL_MODE=0
|
||||
for a in "$@"; do
|
||||
[ "$a" = "--local" ] && LOCAL_MODE=1
|
||||
done
|
||||
|
||||
SSH_TARGET=""; SSH_PASS=""; SNAPSHOT=""
|
||||
if [ "$LOCAL_MODE" = "1" ]; then
|
||||
# In local mode only the snapshot path is required (first non-flag arg).
|
||||
# Peel it off; user@host / password are ignored.
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--*) ;;
|
||||
*) if [ -z "$SNAPSHOT" ] && [ -e "$arg" ]; then SNAPSHOT="$arg"; fi ;;
|
||||
esac
|
||||
done
|
||||
[ -n "$SNAPSHOT" ] || { echo "usage: $0 --local <snapshot.tar.gz> [--repo <path>] [--skip-home] [--no-install] [--no-start] [--env K=V ...]" >&2; exit 2; }
|
||||
# Drop the snapshot from argv so the option parser below only sees flags.
|
||||
new=(); for arg in "$@"; do
|
||||
[ "$arg" = "$SNAPSHOT" ] && continue
|
||||
new+=("$arg")
|
||||
done
|
||||
set -- "${new[@]:-}"
|
||||
else
|
||||
if [ $# -lt 3 ]; then
|
||||
sed -n '3,34p' "$0"; exit 2
|
||||
fi
|
||||
SSH_TARGET="$1"; SSH_PASS="$2"; SNAPSHOT="$3"; shift 3
|
||||
fi
|
||||
|
||||
REPO_OVERRIDE=""
|
||||
SSH_PORT=22
|
||||
DO_INSTALL=1
|
||||
DO_START=1
|
||||
SKIP_HOME=0
|
||||
EXTRA_ENV=()
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--repo) REPO_OVERRIDE="$2"; shift 2 ;;
|
||||
--port) SSH_PORT="$2"; shift 2 ;;
|
||||
--no-install) DO_INSTALL=0; shift ;;
|
||||
--no-start) DO_START=0; shift ;;
|
||||
--skip-home) SKIP_HOME=1; shift ;;
|
||||
--local) shift ;; # already handled in pre-scan
|
||||
--env) EXTRA_ENV+=("$2"); shift 2 ;;
|
||||
-h|--help) sed -n '3,34p' "$0"; exit 0 ;;
|
||||
*) echo "unknown arg: $1" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ -s "$SNAPSHOT" ] || { echo "snapshot not found or empty: $SNAPSHOT" >&2; exit 2; }
|
||||
if [ "$LOCAL_MODE" = "0" ]; then
|
||||
command -v sshpass >/dev/null 2>&1 || { echo "sshpass not found (brew install hudochenkov/sshpass/sshpass)" >&2; exit 1; }
|
||||
fi
|
||||
|
||||
SSH_OPTS=(-p "$SSH_PORT" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=15 -o ServerAliveInterval=30 -o LogLevel=ERROR)
|
||||
if [ "$LOCAL_MODE" = "1" ]; then
|
||||
run_ssh() {
|
||||
# Split KEY=VALUE prefix pairs (used before bash -s) from the rest,
|
||||
# and re-exec via env so they land in the child's environment.
|
||||
local envs=()
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
[A-Za-z_][A-Za-z0-9_]*=*) envs+=("$1"); shift ;;
|
||||
*) break ;;
|
||||
esac
|
||||
done
|
||||
env "${envs[@]}" "$@"
|
||||
}
|
||||
run_scp() { cp "$@"; }
|
||||
else
|
||||
run_ssh() { sshpass -p "$SSH_PASS" ssh "${SSH_OPTS[@]}" "$SSH_TARGET" "$@"; }
|
||||
run_scp() { sshpass -p "$SSH_PASS" scp -P "$SSH_PORT" -o StrictHostKeyChecking=accept-new -o LogLevel=ERROR "$@"; }
|
||||
fi
|
||||
|
||||
# ---------- read manifest locally to learn original REPO ----------
|
||||
MANIFEST=$(tar -xzf "$SNAPSHOT" -O snapshot/MANIFEST.txt 2>/dev/null || true)
|
||||
[ -n "$MANIFEST" ] || { echo "snapshot has no snapshot/MANIFEST.txt (wrong archive?)" >&2; exit 3; }
|
||||
ORIG_REPO=$(echo "$MANIFEST" | awk -F= '/^REPO=/{print $2; exit}')
|
||||
[ -n "$ORIG_REPO" ] || { echo "manifest missing REPO=" >&2; exit 3; }
|
||||
REMOTE_REPO="${REPO_OVERRIDE:-$ORIG_REPO}"
|
||||
echo "[restore] target host: $SSH_TARGET"
|
||||
echo "[restore] target repo: $REMOTE_REPO (snapshot REPO=$ORIG_REPO)"
|
||||
|
||||
# ---------- ship archive ----------
|
||||
STAMP="$(date +%Y%m%d-%H%M%S)"
|
||||
if [ "$LOCAL_MODE" = "1" ]; then
|
||||
REMOTE_TGZ="/tmp/streamline-snapshot-$STAMP.tar.gz"
|
||||
echo "[restore] LOCAL mode — copying archive → $REMOTE_TGZ"
|
||||
cp "$SNAPSHOT" "$REMOTE_TGZ"
|
||||
else
|
||||
REMOTE_TGZ="/tmp/streamline-snapshot-$STAMP.tar.gz"
|
||||
echo "[restore] uploading archive → $REMOTE_TGZ"
|
||||
run_scp "$SNAPSHOT" "$SSH_TARGET:$REMOTE_TGZ"
|
||||
fi
|
||||
|
||||
# ---------- serialize extra env for injection ----------
|
||||
EXTRA_ENV_STR=""
|
||||
for kv in "${EXTRA_ENV[@]:-}"; do
|
||||
# very light escaping — we forbid single-quotes in the value.
|
||||
case "$kv" in *\'*) echo "reject env with single quote: $kv" >&2; exit 2 ;; esac
|
||||
EXTRA_ENV_STR+="$kv"$'\n'
|
||||
done
|
||||
|
||||
# ---------- remote plan ----------
|
||||
run_ssh REMOTE_TGZ="$REMOTE_TGZ" REMOTE_REPO="$REMOTE_REPO" DO_INSTALL="$DO_INSTALL" DO_START="$DO_START" \
|
||||
SKIP_HOME="$SKIP_HOME" EXTRA_ENV_STR="$EXTRA_ENV_STR" STAMP="$STAMP" bash -s <<'REMOTE'
|
||||
set -euo pipefail
|
||||
: "${REMOTE_TGZ:?}"
|
||||
: "${REMOTE_REPO:?}"
|
||||
: "${STAMP:?}"
|
||||
|
||||
echo "[restore] pids before: $(pgrep -f 'satellite/src/index.ts' | tr '\n' ' ' || true)"
|
||||
|
||||
# 1. Kill running satellite(s).
|
||||
if pgrep -f 'satellite/src/index.ts' >/dev/null 2>&1; then
|
||||
echo "[restore] stopping running satellite(s) with SIGTERM"
|
||||
pkill -TERM -f 'satellite/src/index.ts' || true
|
||||
for _ in 1 2 3 4 5; do
|
||||
pgrep -f 'satellite/src/index.ts' >/dev/null 2>&1 || break
|
||||
sleep 1
|
||||
done
|
||||
if pgrep -f 'satellite/src/index.ts' >/dev/null 2>&1; then
|
||||
echo "[restore] still alive after 5s — SIGKILL"
|
||||
pkill -KILL -f 'satellite/src/index.ts' || true
|
||||
fi
|
||||
fi
|
||||
|
||||
# 2. Back up existing repo + captured $HOME entries.
|
||||
BACKUP="$HOME/.streamline-restore-backup/$STAMP"
|
||||
mkdir -p "$BACKUP"
|
||||
if [ -d "$REMOTE_REPO" ]; then
|
||||
echo "[restore] backing up existing repo → $BACKUP/streamline-prev"
|
||||
mv "$REMOTE_REPO" "$BACKUP/streamline-prev"
|
||||
fi
|
||||
|
||||
# 3. Extract snapshot into a staging dir first so we can inspect HOME_ENTRIES.
|
||||
STAGE=$(mktemp -d)
|
||||
tar -xzf "$REMOTE_TGZ" -C "$STAGE"
|
||||
[ -d "$STAGE/snapshot/streamline" ] || { echo "archive missing snapshot/streamline"; exit 3; }
|
||||
[ -f "$STAGE/snapshot/MANIFEST.txt" ] || { echo "archive missing MANIFEST"; exit 3; }
|
||||
|
||||
# Move repo into place.
|
||||
mkdir -p "$(dirname "$REMOTE_REPO")"
|
||||
mv "$STAGE/snapshot/streamline" "$REMOTE_REPO"
|
||||
|
||||
# Restore captured HOME entries — but back the old copy up first.
|
||||
if [ "${SKIP_HOME:-0}" = "1" ]; then
|
||||
echo "[restore] --skip-home: not touching \$HOME/*"
|
||||
elif [ -f "$STAGE/snapshot/HOME_ENTRIES.txt" ]; then
|
||||
while IFS= read -r rel; do
|
||||
[ -z "$rel" ] && continue
|
||||
src="$STAGE/snapshot/home/$rel"
|
||||
dst="$HOME/$rel"
|
||||
[ -e "$src" ] || continue
|
||||
if [ -e "$dst" ] || [ -L "$dst" ]; then
|
||||
mkdir -p "$BACKUP/home/$(dirname "$rel")"
|
||||
mv "$dst" "$BACKUP/home/$rel" 2>/dev/null || cp -R "$dst" "$BACKUP/home/$rel"
|
||||
rm -rf "$dst" 2>/dev/null || true
|
||||
fi
|
||||
mkdir -p "$(dirname "$dst")"
|
||||
mv "$src" "$dst"
|
||||
echo "[restore] restored ~/$rel"
|
||||
done < "$STAGE/snapshot/HOME_ENTRIES.txt"
|
||||
fi
|
||||
rm -rf "$STAGE"
|
||||
rm -f "$REMOTE_TGZ"
|
||||
|
||||
# 4. Inject extra env into .env (append; overrides existing definitions on read).
|
||||
if [ -n "${EXTRA_ENV_STR:-}" ]; then
|
||||
ENV_FILE="$REMOTE_REPO/.env"
|
||||
touch "$ENV_FILE"
|
||||
{
|
||||
echo ""
|
||||
echo "# --- injected by executor-restore.sh @ $STAMP ---"
|
||||
printf '%s' "$EXTRA_ENV_STR"
|
||||
} >> "$ENV_FILE"
|
||||
echo "[restore] appended $(printf '%s' "$EXTRA_ENV_STR" | grep -c '^') env overrides to $ENV_FILE"
|
||||
fi
|
||||
|
||||
# 5. Ensure bun is available.
|
||||
if ! command -v bun >/dev/null 2>&1; then
|
||||
if [ -x "$HOME/.bun/bin/bun" ]; then
|
||||
export PATH="$HOME/.bun/bin:$PATH"
|
||||
else
|
||||
echo "[restore] WARNING: bun not on PATH. Install it: curl -fsSL https://bun.sh/install | bash"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 6. bun install if requested and needed.
|
||||
if [ "$DO_INSTALL" = "1" ]; then
|
||||
if [ ! -d "$REMOTE_REPO/node_modules" ] || [ ! -d "$REMOTE_REPO/satellite/node_modules" ]; then
|
||||
echo "[restore] running bun install …"
|
||||
(cd "$REMOTE_REPO" && bun install --frozen-lockfile 2>&1 | tail -n 30) || \
|
||||
(cd "$REMOTE_REPO" && bun install 2>&1 | tail -n 30)
|
||||
else
|
||||
echo "[restore] node_modules present, skipping bun install"
|
||||
fi
|
||||
fi
|
||||
|
||||
# 7. Start satellite in the background.
|
||||
if [ "$DO_START" = "1" ]; then
|
||||
LOG="$HOME/streamline-satellite.log"
|
||||
echo "[restore] starting satellite → $LOG"
|
||||
(
|
||||
cd "$REMOTE_REPO"
|
||||
# Detach cleanly so ssh can exit.
|
||||
nohup bash -lc 'bun run start:satellite' >>"$LOG" 2>&1 </dev/null &
|
||||
disown || true
|
||||
)
|
||||
sleep 2
|
||||
pid=$(pgrep -f 'satellite/src/index.ts' | head -n1 || true)
|
||||
if [ -n "$pid" ]; then
|
||||
echo "[restore] satellite running as PID $pid (tail -f $LOG)"
|
||||
else
|
||||
echo "[restore] WARNING: satellite did not appear in pgrep after 2s — check $LOG"
|
||||
tail -n 40 "$LOG" || true
|
||||
exit 5
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "[restore] backup kept at: $BACKUP"
|
||||
echo "[restore] done."
|
||||
REMOTE
|
||||
|
||||
echo "[restore] complete."
|
||||
@@ -0,0 +1,236 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# executor-snapshot.sh — take a self-contained snapshot of a remote executor
|
||||
# (satellite) node so it can be restored on any other box with executor-restore.sh.
|
||||
#
|
||||
# Captured on the remote:
|
||||
# 1. Streamline repo (executor source + shared/ + bin/ + package.json + bun.lock)
|
||||
# Optionally node_modules (default: included) so restore doesn't need `bun install`.
|
||||
# Excludes .git/objects/pack (usually large) unless SNAPSHOT_KEEP_GIT=1.
|
||||
# Excludes runtime data (bug_workspace/ ai_workspace/ taskLog/ satellite/dist/)
|
||||
# unless SNAPSHOT_INCLUDE_WORKSPACES=1.
|
||||
# 2. Repo-level config: .env, .env.example, .mcp.json (if present), CLAUDE.md (if present).
|
||||
# 3. Agent CLI global configs & skills & MCP:
|
||||
# ~/.claude.json (global claude config incl. mcpServers, env, model)
|
||||
# ~/.claude/ (skills, settings.json, plugins/, sessions/… WITHOUT projects/, history.jsonl)
|
||||
# ~/.cursor/ (skills, skills-cursor, cli-config.json, agents/, hooks.json, mcp*)
|
||||
# ~/.trae/ (cli/*, plugins/, skills/, memories/)
|
||||
# ~/.agents/ (skills/, auth.json)
|
||||
# Large caches, sessions histories and per-project state that are not
|
||||
# needed to boot an executor are excluded — see EXCLUDE_HOME below.
|
||||
#
|
||||
# Output: a single local .tar.gz containing everything, plus a MANIFEST.txt
|
||||
# with the remote paths so the restore script knows where to put things.
|
||||
#
|
||||
# Auth: SSH with password, using sshpass. Install with:
|
||||
# brew install hudochenkov/sshpass/sshpass (macOS)
|
||||
# apt-get install sshpass (Debian/Ubuntu)
|
||||
#
|
||||
# Usage:
|
||||
# scripts/executor-snapshot.sh <user@host> <password> [--repo <remote_repo_path>] \
|
||||
# [--out <local_output.tar.gz>] \
|
||||
# [--no-node-modules] [--include-workspaces] [--port 22]
|
||||
#
|
||||
# Examples:
|
||||
# scripts/executor-snapshot.sh fei@fei-ios-01.shiro0.icu 'hunter2'
|
||||
# scripts/executor-snapshot.sh fei@10.0.0.5 'hunter2' --repo /home/fei/streamline --out ~/snapshots/ios01.tar.gz
|
||||
#
|
||||
set -euo pipefail
|
||||
|
||||
# ---------- argv ----------
|
||||
if [ $# -lt 2 ]; then
|
||||
sed -n '3,42p' "$0"
|
||||
exit 2
|
||||
fi
|
||||
SSH_TARGET="$1"; SSH_PASS="$2"; shift 2
|
||||
REMOTE_REPO="" # auto-detect if not provided
|
||||
OUT_PATH=""
|
||||
INCLUDE_NM=1
|
||||
INCLUDE_WORKSPACES=0
|
||||
SSH_PORT=22
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--repo) REMOTE_REPO="$2"; shift 2 ;;
|
||||
--out) OUT_PATH="$2"; shift 2 ;;
|
||||
--no-node-modules) INCLUDE_NM=0; shift ;;
|
||||
--include-node-modules) INCLUDE_NM=1; shift ;;
|
||||
--include-workspaces) INCLUDE_WORKSPACES=1; shift ;;
|
||||
--port) SSH_PORT="$2"; shift 2 ;;
|
||||
-h|--help) sed -n '3,42p' "$0"; exit 0 ;;
|
||||
*) echo "unknown arg: $1" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
REMOTE_HOST="${SSH_TARGET#*@}"
|
||||
if ! command -v sshpass >/dev/null 2>&1; then
|
||||
echo "sshpass not found — install it (brew install hudochenkov/sshpass/sshpass)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SSH_OPTS=(-p "$SSH_PORT" -o StrictHostKeyChecking=accept-new -o ConnectTimeout=15 -o ServerAliveInterval=30 -o LogLevel=ERROR)
|
||||
run_ssh() { sshpass -p "$SSH_PASS" ssh "${SSH_OPTS[@]}" "$SSH_TARGET" "$@"; }
|
||||
run_scp() { sshpass -p "$SSH_PASS" scp -P "$SSH_PORT" -o StrictHostKeyChecking=accept-new -o LogLevel=ERROR "$@"; }
|
||||
|
||||
# ---------- discover remote repo ----------
|
||||
if [ -z "$REMOTE_REPO" ]; then
|
||||
echo "[snapshot] auto-detecting remote streamline repo…"
|
||||
REMOTE_REPO=$(run_ssh 'set -e
|
||||
# look for a running satellite process first
|
||||
pid=$(pgrep -f "satellite/src/index.ts" 2>/dev/null | head -n1 || true)
|
||||
if [ -n "$pid" ]; then
|
||||
# /proc/<pid>/cwd on linux, lsof fallback for macOS
|
||||
if [ -r "/proc/$pid/cwd" ]; then
|
||||
readlink -f "/proc/$pid/cwd" && exit 0
|
||||
fi
|
||||
cwd=$(lsof -p "$pid" 2>/dev/null | awk "\$4==\"cwd\"{print \$NF; exit}")
|
||||
[ -n "$cwd" ] && { echo "$cwd"; exit 0; }
|
||||
fi
|
||||
# fallbacks
|
||||
for cand in "$HOME/streamline" "$HOME/workspace/streamline" "/opt/streamline"; do
|
||||
[ -f "$cand/package.json" ] && [ -d "$cand/satellite" ] && { echo "$cand"; exit 0; }
|
||||
done
|
||||
exit 3
|
||||
') || {
|
||||
echo "[snapshot] could not auto-detect; pass --repo <path>" >&2
|
||||
exit 3
|
||||
}
|
||||
fi
|
||||
echo "[snapshot] remote repo: $REMOTE_REPO"
|
||||
|
||||
# ---------- default output path ----------
|
||||
STAMP="$(date +%Y%m%d-%H%M%S)"
|
||||
if [ -z "$OUT_PATH" ]; then
|
||||
OUT_DIR="$HOME/streamline-snapshots"
|
||||
mkdir -p "$OUT_DIR"
|
||||
OUT_PATH="$OUT_DIR/executor-${REMOTE_HOST//[^A-Za-z0-9]/_}-$STAMP.tar.gz"
|
||||
fi
|
||||
mkdir -p "$(dirname "$OUT_PATH")"
|
||||
|
||||
# ---------- exclusion patterns ----------
|
||||
REPO_EXCLUDES=(
|
||||
"--exclude=.git/objects/pack"
|
||||
"--exclude=satellite/dist"
|
||||
"--exclude=control-web/dist"
|
||||
"--exclude=control-web/node_modules/.cache"
|
||||
"--exclude=.DS_Store"
|
||||
"--exclude=log"
|
||||
"--exclude=logs"
|
||||
)
|
||||
[ "$INCLUDE_NM" -eq 0 ] && REPO_EXCLUDES+=("--exclude=node_modules")
|
||||
[ "$INCLUDE_WORKSPACES" -eq 0 ] && REPO_EXCLUDES+=("--exclude=bug_workspace" "--exclude=ai_workspace" "--exclude=taskLog")
|
||||
[ "${SNAPSHOT_KEEP_GIT:-0}" != "1" ] && REPO_EXCLUDES+=("--exclude=.git")
|
||||
|
||||
# What we want from $HOME/* (agent CLI configs, skills, MCP). Anything else
|
||||
# under those dirs (sessions, project caches, huge blob dumps) is excluded.
|
||||
HOME_INCLUDES=(
|
||||
".claude.json"
|
||||
".claude/settings.json"
|
||||
".claude/skills"
|
||||
".claude/plugins"
|
||||
".claude/CLAUDE.md"
|
||||
".cursor/cli-config.json"
|
||||
".cursor/skills"
|
||||
".cursor/skills-cursor"
|
||||
".cursor/agents"
|
||||
".cursor/hooks.json"
|
||||
".cursor/mcp.json"
|
||||
".cursor/argv.json"
|
||||
".trae/cli/traecli.toml"
|
||||
".trae/cli/traecli.yaml"
|
||||
".trae/cli/skill-config.json"
|
||||
".trae/cli/skills"
|
||||
".trae/cli/rules"
|
||||
".trae/skills"
|
||||
".trae/plugins"
|
||||
".agents/skills"
|
||||
".agents/auth.json"
|
||||
)
|
||||
|
||||
# ---------- build tar remotely, stream to local file ----------
|
||||
REMOTE_TMP=$(run_ssh 'mktemp -d "$HOME/.streamline-snap.XXXXXX"')
|
||||
echo "[snapshot] remote staging: $REMOTE_TMP"
|
||||
|
||||
# Write manifest on the remote, then tar the repo + selected $HOME entries and pipe here.
|
||||
run_ssh bash -s -- "$REMOTE_TMP" "$REMOTE_REPO" "$INCLUDE_NM" "$INCLUDE_WORKSPACES" <<'REMOTE_BOOTSTRAP'
|
||||
set -euo pipefail
|
||||
tmp="$1"; repo="$2"; inc_nm="$3"; inc_ws="$4"
|
||||
mkdir -p "$tmp"
|
||||
{
|
||||
echo "SCHEMA=1"
|
||||
echo "STAMP=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
echo "HOST=$(hostname)"
|
||||
echo "USER=$(id -un)"
|
||||
echo "REPO=$repo"
|
||||
echo "HOME=$HOME"
|
||||
echo "INCLUDE_NODE_MODULES=$inc_nm"
|
||||
echo "INCLUDE_WORKSPACES=$inc_ws"
|
||||
echo "SATELLITE_PIDS=$(pgrep -f 'satellite/src/index.ts' 2>/dev/null | tr '\n' ',' || true)"
|
||||
echo "BUN=$(command -v bun || true)"
|
||||
echo "CLAUDE=$(command -v claude || true)"
|
||||
echo "CURSOR=$(command -v cursor-agent || true)"
|
||||
echo "CODEX=$(command -v codex || true)"
|
||||
echo "UNAME=$(uname -a)"
|
||||
} > "$tmp/MANIFEST.txt"
|
||||
|
||||
# also record which HOME entries exist (so restore only touches captured paths)
|
||||
: > "$tmp/HOME_ENTRIES.txt"
|
||||
REMOTE_BOOTSTRAP
|
||||
|
||||
# Ship the HOME include list to the remote so it selects only what exists.
|
||||
printf '%s\n' "${HOME_INCLUDES[@]}" | \
|
||||
sshpass -p "$SSH_PASS" ssh "${SSH_OPTS[@]}" "$SSH_TARGET" "cat > '$REMOTE_TMP/HOME_LIST.txt'"
|
||||
|
||||
echo "[snapshot] packing on remote and streaming to: $OUT_PATH"
|
||||
# Build a single tar with two top-level dirs: repo/ and home/, and a MANIFEST.txt.
|
||||
# We use `tar --transform` for GNU tar; on macOS `bsdtar` uses `-s` instead.
|
||||
# Detect once on the remote.
|
||||
run_ssh bash -s -- "$REMOTE_TMP" "$REMOTE_REPO" "${REPO_EXCLUDES[@]}" <<'REMOTE_TAR' > "$OUT_PATH"
|
||||
set -euo pipefail
|
||||
tmp="$1"; repo="$2"; shift 2
|
||||
excludes=("$@")
|
||||
|
||||
# Filter HOME entries to what actually exists; record for the manifest.
|
||||
# Portable across bash 3.2 (macOS) — no mapfile.
|
||||
present=()
|
||||
while IFS= read -r rel || [ -n "$rel" ]; do
|
||||
[ -z "$rel" ] && continue
|
||||
if [ -e "$HOME/$rel" ]; then present+=("$rel"); fi
|
||||
done < "$tmp/HOME_LIST.txt"
|
||||
: > "$tmp/HOME_ENTRIES.txt"
|
||||
for rel in "${present[@]:-}"; do
|
||||
[ -n "$rel" ] && echo "$rel" >> "$tmp/HOME_ENTRIES.txt"
|
||||
done
|
||||
|
||||
# Build a symlink tree under $tmp/snapshot/ that mirrors the archive layout.
|
||||
# Then tar the tree with -h (follow symlinks) — works with BSD and GNU tar
|
||||
# without any path-transform gymnastics.
|
||||
snap="$tmp/snapshot"
|
||||
rm -rf "$snap"
|
||||
mkdir -p "$snap/home"
|
||||
ln -s "$repo" "$snap/streamline"
|
||||
for rel in "${present[@]:-}"; do
|
||||
[ -z "$rel" ] && continue
|
||||
mkdir -p "$snap/home/$(dirname "$rel")"
|
||||
ln -s "$HOME/$rel" "$snap/home/$rel"
|
||||
done
|
||||
cp "$tmp/MANIFEST.txt" "$snap/MANIFEST.txt"
|
||||
cp "$tmp/HOME_ENTRIES.txt" "$snap/HOME_ENTRIES.txt"
|
||||
|
||||
# -h follows symlinks so contents are packed under snapshot/*.
|
||||
tar "${excludes[@]}" -czhf - -C "$tmp" snapshot 2>/dev/null
|
||||
REMOTE_TAR
|
||||
|
||||
# ---------- clean remote staging ----------
|
||||
run_ssh "rm -rf '$REMOTE_TMP'" || true
|
||||
|
||||
# ---------- verify locally ----------
|
||||
if ! [ -s "$OUT_PATH" ]; then
|
||||
echo "[snapshot] failed: output is empty ($OUT_PATH)" >&2
|
||||
exit 4
|
||||
fi
|
||||
size=$(du -h "$OUT_PATH" | awk '{print $1}')
|
||||
echo "[snapshot] archive: $OUT_PATH ($size)"
|
||||
|
||||
# Show the manifest for the user.
|
||||
tar -xzf "$OUT_PATH" -O snapshot/MANIFEST.txt 2>/dev/null | sed 's/^/ /'
|
||||
echo "[snapshot] done."
|
||||
@@ -0,0 +1,104 @@
|
||||
// One-shot generator: introspect the live SQLite and emit a Drizzle pg schema
|
||||
// (control-server/src/schema.ts). Fields are camelCase (matching the app's TS
|
||||
// domain); with casing:"snake_case" on the client, Drizzle maps them to
|
||||
// snake_case columns. INTEGER→bigint(mode:number), REAL→doublePrecision, else
|
||||
// text. Run once; the emitted schema.ts is then the hand-maintained source.
|
||||
//
|
||||
// STREAMLINE_DB_PATH=~/.streamline/data.sqlite bun scripts/gen_drizzle_schema.ts
|
||||
import { Database } from "bun:sqlite";
|
||||
import { homedir } from "node:os";
|
||||
import path from "node:path";
|
||||
import { writeFileSync } from "node:fs";
|
||||
|
||||
const SRC = process.env.STREAMLINE_DB_PATH || path.join(homedir(), ".streamline", "data.sqlite");
|
||||
const sq = new Database(SRC, { readonly: true });
|
||||
|
||||
type Col = { name: string; type: string; notnull: number; dflt_value: string | null; pk: number };
|
||||
|
||||
const tables = sq.query(
|
||||
"SELECT name, sql FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name",
|
||||
).all() as { name: string; sql: string }[];
|
||||
|
||||
// field name (camelCase) → snake_case column name, matching Drizzle's casing.
|
||||
const snake = (s: string) => s.replace(/([a-z0-9])([A-Z])/g, "$1_$2").toLowerCase();
|
||||
|
||||
// map a sqlite decl type → a drizzle column constructor call with an EXPLICIT
|
||||
// snake_case column name (self-documenting; independent of the casing config).
|
||||
function drizzleType(t: string, col: string): string {
|
||||
const u = (t || "").toUpperCase();
|
||||
const c = JSON.stringify(col);
|
||||
if (u.includes("INT")) return `bigint(${c}, { mode: "number" })`;
|
||||
if (u.includes("REAL") || u.includes("FLOA") || u.includes("DOUB")) return `doublePrecision(${c})`;
|
||||
return `text(${c})`;
|
||||
}
|
||||
function defaultLiteral(dflt: string, isText: boolean): string {
|
||||
// sqlite stores text defaults quoted ('x'); numbers bare.
|
||||
if (/^'.*'$/.test(dflt)) return JSON.stringify(dflt.slice(1, -1));
|
||||
if (/^-?\d+(\.\d+)?$/.test(dflt)) return dflt;
|
||||
return JSON.stringify(dflt.replace(/^'|'$/g, "")); // fallback
|
||||
}
|
||||
|
||||
const importedTypes = new Set<string>(["pgTable"]);
|
||||
const tableBlocks: string[] = [];
|
||||
|
||||
for (const { name, sql } of tables) {
|
||||
const cols = sq.query(`PRAGMA table_info("${name}")`).all() as Col[];
|
||||
const fks = sq.query(`PRAGMA foreign_key_list("${name}")`).all() as { table: string; from: string; to: string; on_delete: string }[];
|
||||
const idxList = sq.query(`PRAGMA index_list("${name}")`).all() as { name: string; unique: number; origin: string }[];
|
||||
const uniqueCols = new Set<string>();
|
||||
for (const ix of idxList) if (ix.origin === "u" && ix.unique) {
|
||||
const uc = sq.query(`PRAGMA index_info("${ix.name}")`).all() as { name: string }[];
|
||||
if (uc.length === 1) uniqueCols.add(uc[0].name); // single-col unique → inline .unique()
|
||||
}
|
||||
const autoinc = /AUTOINCREMENT/i.test(sql || "");
|
||||
const pkCols = cols.filter((c) => c.pk > 0).sort((a, b) => a.pk - b.pk).map((c) => c.name);
|
||||
const singleAiPk = autoinc && pkCols.length === 1;
|
||||
const fkByCol = new Map(fks.map((f) => [f.from, f]));
|
||||
|
||||
const lines: string[] = [];
|
||||
for (const c of cols) {
|
||||
const dt = drizzleType(c.type, snake(c.name));
|
||||
importedTypes.add(dt.split("(")[0]);
|
||||
let chain = ` ${c.name}: ${dt}`;
|
||||
if (singleAiPk && pkCols[0] === c.name) {
|
||||
chain = ` ${c.name}: bigint(${JSON.stringify(snake(c.name))}, { mode: "number" }).generatedByDefaultAsIdentity().primaryKey()`;
|
||||
lines.push(chain + ",");
|
||||
continue;
|
||||
}
|
||||
if (pkCols.length === 1 && pkCols[0] === c.name) chain += ".primaryKey()";
|
||||
if (c.notnull) chain += ".notNull()";
|
||||
if (uniqueCols.has(c.name)) chain += ".unique()";
|
||||
if (c.dflt_value !== null) chain += `.default(${defaultLiteral(c.dflt_value, dt.startsWith("text"))})`;
|
||||
const fk = fkByCol.get(c.name);
|
||||
if (fk) {
|
||||
const od = fk.on_delete && fk.on_delete.toUpperCase() !== "NO ACTION" ? `, { onDelete: "${fk.on_delete.toLowerCase()}" }` : "";
|
||||
chain += `.references(() => ${fk.table}.${fk.to}${od})`;
|
||||
}
|
||||
lines.push(chain + ",");
|
||||
}
|
||||
|
||||
// table-level: composite PK
|
||||
const extras: string[] = [];
|
||||
if (pkCols.length > 1) {
|
||||
importedTypes.add("primaryKey");
|
||||
extras.push(` primaryKey({ columns: [${pkCols.map((c) => `t.${c}`).join(", ")}] })`);
|
||||
}
|
||||
const cb = extras.length ? `, (t) => [\n${extras.join(",\n")},\n]` : "";
|
||||
tableBlocks.push(`export const ${name} = pgTable("${name}", {\n${lines.join("\n")}\n}${cb});`);
|
||||
}
|
||||
|
||||
const pgImports = [...importedTypes].filter((t) => t !== "pgTable" && t !== "primaryKey").sort();
|
||||
const coreImports = ["pgTable", ...(importedTypes.has("primaryKey") ? ["primaryKey"] : []), ...pgImports];
|
||||
|
||||
const out =
|
||||
`// AUTO-GENERATED once by scripts/gen_drizzle_schema.ts from the live SQLite, then\n` +
|
||||
`// hand-maintained. Drizzle entities for the Postgres (snake_case) database.\n` +
|
||||
`// Fields are camelCase (app domain); casing:"snake_case" on the client maps\n` +
|
||||
`// them to snake_case columns. int8 columns use mode:"number" so timestamps/\n` +
|
||||
`// counts come back as JS numbers (values are all < 2^53).\n` +
|
||||
`import { ${coreImports.join(", ")} } from "drizzle-orm/pg-core";\n\n` +
|
||||
tableBlocks.join("\n\n") + "\n";
|
||||
|
||||
const dest = path.join(import.meta.dir, "..", "control-server", "src", "schema.ts");
|
||||
writeFileSync(dest, out);
|
||||
console.log(`[gen] wrote ${dest} (${tables.length} tables, ${[...importedTypes].join("/")})`);
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* 把新的检索排序放回历史注入上重跑一遍,回答一个问题:
|
||||
*
|
||||
* 同样这些任务、同样这些候选知识,新排序**少塞多少条**,又**漏掉多少条真的被采纳过的**?
|
||||
*
|
||||
* 实现在 `control-server/src/kb_effectiveness.ts::replayRanking` —— 与面板「效果」页
|
||||
* 用的是同一段代码。命令行和面板算出两套数字,是这类度量最常见的烂尾方式。
|
||||
*
|
||||
* ## 为什么自己算 tier 而不是读那一列
|
||||
*
|
||||
* 这个脚本要能在**迁移落库之前**跑 —— 它正是用来决定那次迁移值不值得做的。所以它
|
||||
* 按 `earnsSemanticTier` 的同一套规则,从证据表和引用记录里现算一遍分层,等价于
|
||||
* `0019` 回填之后的状态。
|
||||
*
|
||||
* ## 怎么读结果
|
||||
*
|
||||
* - `保留的引用` 越高越好:闸门不能把起草者真的用过的东西挡在外面。
|
||||
* - `注入量` 越低越好:提示词里每多一段"参考资料",模型对铁律与 NEEDS_INPUT 哨兵的
|
||||
* 遵守度就掉一点 —— 这个 tradeoff 是真实的,不是洁癖。
|
||||
* - 排序那一节是**前序式**打分(先验只看更早的注入),所以「有用度」不会自证。
|
||||
*
|
||||
* 只读。用法:bun scripts/kb_rank_replay.ts
|
||||
*/
|
||||
import { db } from "../control-server/src/db_client";
|
||||
import { kb_injections, kb_claims, kb_evidence } from "../control-server/src/schema";
|
||||
import { replayRanking } from "../control-server/src/kb_effectiveness";
|
||||
import { earnsSemanticTier } from "../control-server/src/kb_db";
|
||||
import { sql } from "drizzle-orm";
|
||||
|
||||
const idList = (raw: string | null): string[] => {
|
||||
if (!raw) return [];
|
||||
try {
|
||||
const v = JSON.parse(raw) as unknown;
|
||||
return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
|
||||
} catch { return []; }
|
||||
};
|
||||
|
||||
async function main() {
|
||||
// Rebuild what 0019 would have written, from the same sources it reads.
|
||||
const witnesses = new Map<string, number>();
|
||||
for (const r of await db.select({
|
||||
claimId: kb_evidence.claimId,
|
||||
n: sql<number>`count(distinct ${kb_evidence.targetId})::int`,
|
||||
}).from(kb_evidence).groupBy(kb_evidence.claimId)) witnesses.set(r.claimId, r.n);
|
||||
|
||||
const cited = new Map<string, number>();
|
||||
for (const inj of await db.select({ usedClaimIds: kb_injections.usedClaimIds }).from(kb_injections))
|
||||
for (const id of idList(inj.usedClaimIds)) cited.set(id, (cited.get(id) ?? 0) + 1);
|
||||
|
||||
const tierOf = (c: { id: string; kind: string; source: string }) =>
|
||||
earnsSemanticTier({
|
||||
kind: c.kind, source: c.source as "distilled" | "human",
|
||||
witnessedTargets: witnesses.get(c.id) ?? 0, citedCount: cited.get(c.id) ?? 0,
|
||||
}) ? "semantic" : "episodic";
|
||||
|
||||
const r = await replayRanking({ tierOf });
|
||||
const pct = (a: number, b: number) => `${(a / Math.max(1, b) * 100).toFixed(0)}%`;
|
||||
|
||||
console.log(`\n样本:${r.injections} 次有回报的注入,${r.slots} 个条目位,其中被引用 ${r.cited} 个 (${pct(r.cited, r.slots)})\n`);
|
||||
console.log("① 分层闸(tier)");
|
||||
console.log(` 判为 episodic、今后不再注入:${r.tierDropped}/${r.slots} (${pct(r.tierDropped, r.slots)})`);
|
||||
console.log(` 其中曾被引用过的:${r.tierDroppedCited} ← 设计上应为 0(引用过即晋升 semantic)\n`);
|
||||
|
||||
console.log("② 端到端(分层 + 相关性 + 分类配额)");
|
||||
console.log(` 注入量:${r.slots} → ${r.keptSlots} 个条目位 (${pct(r.keptSlots, r.slots)}),`
|
||||
+ ` 每次注入 ${(r.slots / Math.max(1, r.injections)).toFixed(1)} → ${(r.keptSlots / Math.max(1, r.injections)).toFixed(1)} 条`);
|
||||
console.log(` 被引用过的知识:${r.keptCited}/${r.cited} 仍在注入里 (${pct(r.keptCited, r.cited)})`);
|
||||
console.log(" 注:重放只能在「当时检索出来的候选」里筛;生产上常驻知识不再要求命中工单词,");
|
||||
console.log(" 候选集只会更大,所以这一行是下限。\n");
|
||||
|
||||
console.log("③ 排序质量(同一批候选,只换排序;有用度先验前序计算)");
|
||||
console.log(" 老排序(置信度) 新排序(常驻优先)");
|
||||
console.log(` 被引用知识平均名次 ${r.meanRankOld.toFixed(2)}`.padEnd(45) + r.meanRankNew.toFixed(2));
|
||||
console.log(` MRR ${r.mrrOld.toFixed(3)}`.padEnd(45) + r.mrrNew.toFixed(3));
|
||||
console.log(` 前 3 条内命中 ${r.top3Old}/${r.cited} (${pct(r.top3Old, r.cited)})`.padEnd(45)
|
||||
+ `${r.top3New}/${r.cited} (${pct(r.top3New, r.cited)})`);
|
||||
console.log("");
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
void main();
|
||||
@@ -0,0 +1,106 @@
|
||||
/** Real control API + PostgreSQL + full web app, with local-only authentication.
|
||||
* No production entrypoint/auth changes. No external integrations or satellites.
|
||||
* DATABASE_URL must name a dedicated local streamline_profiles_dev database.
|
||||
*/
|
||||
import path from "node:path";
|
||||
|
||||
const root = path.resolve(import.meta.dir, "..");
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
if (!databaseUrl) throw new Error("Set DATABASE_URL to the isolated local development database");
|
||||
const dbUrl = new URL(databaseUrl);
|
||||
if (dbUrl.hostname !== "127.0.0.1" || dbUrl.pathname !== "/streamline_profiles_dev") {
|
||||
throw new Error("Local runner only accepts 127.0.0.1/streamline_profiles_dev; refusing other databases");
|
||||
}
|
||||
const webPort = 63868;
|
||||
const apiPort = 63869;
|
||||
const webOrigin = `http://127.0.0.1:${webPort}`;
|
||||
|
||||
if (!process.argv.includes("--server")) {
|
||||
// Child processes never inherit production .env credentials, and Bun/Vite
|
||||
// automatic env-file loading is disabled as well.
|
||||
const env = {
|
||||
PATH: process.env.PATH || "/opt/homebrew/bin:/usr/bin:/bin",
|
||||
TMPDIR: process.env.TMPDIR || "/tmp",
|
||||
NODE_ENV: "development",
|
||||
DATABASE_URL: databaseUrl,
|
||||
LOCAL_DEV_SESSION_TOKEN: crypto.randomUUID(),
|
||||
APP_BASE_URL: webOrigin,
|
||||
LARK_CLI_BIN: "/usr/bin/false",
|
||||
CLUSTER_SECRET: crypto.randomUUID(),
|
||||
BITS_SKIP_USER_TOKEN: "1",
|
||||
};
|
||||
const children = [
|
||||
Bun.spawn([process.execPath, "--no-env-file", import.meta.path, "--server"], { cwd: root, env, stdout: "inherit", stderr: "inherit" }),
|
||||
Bun.spawn([process.execPath, "--no-env-file", path.join(root, "control-web/node_modules/vite/bin/vite.js"), "--config", "vite.local.config.ts"], { cwd: path.join(root, "control-web"), env, stdout: "inherit", stderr: "inherit" }),
|
||||
];
|
||||
let stopping = false;
|
||||
const stop = () => {
|
||||
if (stopping) return;
|
||||
stopping = true;
|
||||
for (const child of children) try { child.kill("SIGTERM"); } catch { /* exited */ }
|
||||
};
|
||||
process.on("SIGINT", stop);
|
||||
process.on("SIGTERM", stop);
|
||||
const code = await Promise.race(children.map((child) => child.exited));
|
||||
stop();
|
||||
await Promise.all(children.map((child) => child.exited));
|
||||
process.exit(code);
|
||||
} else {
|
||||
const token = process.env.LOCAL_DEV_SESSION_TOKEN;
|
||||
if (!token || process.env.NODE_ENV !== "development") throw new Error("Start through the local launcher");
|
||||
// Only identity is substituted; the real SSO resolver, admin guard and every
|
||||
// task/configuration handler still run. All other outbound fetches are denied.
|
||||
globalThis.fetch = (async (input: string | Request | URL, init?: RequestInit) => {
|
||||
const url = new URL(input instanceof Request ? input.url : String(input));
|
||||
const headers = new Headers(init?.headers || (input instanceof Request ? input.headers : undefined));
|
||||
if (url.origin === "https://cloudapi.bytedance.net" && url.pathname === "/faas/services/ttuae2/invoke/getUserInfo" && headers.get("x-tt-session-v2") === token) {
|
||||
return Response.json({ data: { email: "streamline-local@bytedance.com", name: "本地测试管理员" } });
|
||||
}
|
||||
throw new Error(`External integrations are disabled in local development (${url.hostname})`);
|
||||
}) as typeof fetch;
|
||||
|
||||
const { Hono } = await import(Bun.resolveSync("hono", path.join(root, "control-server"))) as typeof import("../control-server/node_modules/hono");
|
||||
const { api } = await import("../control-server/src/routes");
|
||||
const { attach, detach } = await import("../control-server/src/bus");
|
||||
const { readAdminPrefixes, setAdminPrefixes } = await import("../control-server/src/admin_db");
|
||||
const { closePool } = await import("../control-server/src/db_client");
|
||||
await setAdminPrefixes([...await readAdminPrefixes(), "streamline-local"]);
|
||||
const app = new Hono();
|
||||
app.use("*", async (c, next) => {
|
||||
// Bind only loopback and reject DNS-rebinding/foreign-site write requests.
|
||||
if (c.req.header("host") !== `127.0.0.1:${apiPort}`) return c.json({ error: "Local host only" }, 403);
|
||||
const origin = c.req.header("origin");
|
||||
if (origin && origin !== webOrigin) return c.json({ error: "Local origin only" }, 403);
|
||||
const pathname = c.req.path;
|
||||
if (/^\/api\/(internal|deploy|rsi|lab|lark|openapi)(\/|$)/.test(pathname)) return c.json({ error: "External integrations and execution are disabled locally" }, 403);
|
||||
const read = c.req.method === "GET" || c.req.method === "HEAD";
|
||||
const allowedWrite = ["/api/import", "/api/features", "/api/admin/product-profiles", "/api/me/prefs", "/api/me/groups"].includes(pathname);
|
||||
if (!read && !allowedWrite) return c.json({ error: "Local testing supports product settings, preferences and task creation only" }, 403);
|
||||
return next();
|
||||
});
|
||||
app.route("/api", api);
|
||||
const server = Bun.serve({
|
||||
hostname: "127.0.0.1", port: apiPort,
|
||||
fetch(req, srv) {
|
||||
if (new URL(req.url).pathname === "/ws") {
|
||||
if (req.headers.get("origin") !== webOrigin) return new Response("Local origin only", { status: 403 });
|
||||
if (srv.upgrade(req)) return;
|
||||
return new Response("Upgrade failed", { status: 400 });
|
||||
}
|
||||
return app.fetch(req);
|
||||
},
|
||||
websocket: { open(ws) { attach(ws); }, close(ws) { detach(ws); }, message() {} },
|
||||
});
|
||||
console.log(`[local] Real API: http://127.0.0.1:${server.port}; UI: ${webOrigin}`);
|
||||
console.log("[local] PostgreSQL: streamline_profiles_dev. Local test admin. No production data, external calls, job runners or MR submission.");
|
||||
let stopping = false;
|
||||
const stop = async () => {
|
||||
if (stopping) return;
|
||||
stopping = true;
|
||||
server.stop(true);
|
||||
await closePool();
|
||||
process.exit(0);
|
||||
};
|
||||
process.on("SIGINT", stop);
|
||||
process.on("SIGTERM", stop);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
/** Used ONLY by vite.local.config.ts (serve-only). Never bundled for production. */
|
||||
export interface SsoUser { email: string; name: string; avatarUrl: string }
|
||||
const user: SsoUser = { email: "streamline-local@bytedance.com", name: "本地测试管理员", avatarUrl: "" };
|
||||
let current: SsoUser | null = user;
|
||||
export const getCurrentSsoUser = () => current;
|
||||
export const getCurrentSsoEmail = () => current?.email || null;
|
||||
export const setCurrentSsoUser = (value: SsoUser | null) => { current = value; };
|
||||
export const getSsoSessionToken = () => import.meta.env.VITE_LOCAL_SESSION_TOKEN as string;
|
||||
export const ssoAuthHeaders = () => ({ "x-tt-session-v2": getSsoSessionToken() });
|
||||
export const fetchUserInfo = async () => user;
|
||||
export const isLogin = async () => true;
|
||||
export const loadSsoSdk = async () => true;
|
||||
export const ssoAvailable = () => true;
|
||||
export const logout = () => { current = null; };
|
||||
export const login = () => { location.reload(); return true; };
|
||||
@@ -0,0 +1,110 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# meego_check.sh — probe Meego (飞书项目) Open API plugin permissions.
|
||||
#
|
||||
# Default run is SAFE / non-mutating: it only reads, and probes the comment /
|
||||
# state-change endpoints with bodies that get rejected before any write.
|
||||
# Add flags to perform a REAL write test on a ticket you're OK mutating.
|
||||
#
|
||||
# Usage:
|
||||
# ./scripts/meego_check.sh [WORK_ITEM_ID]
|
||||
# ./scripts/meego_check.sh 6931196005 --comment "权限测试,可删除" # real comment
|
||||
# ./scripts/meego_check.sh 6931196005 --resolve # real OPEN→…→RESOLVED transition
|
||||
#
|
||||
# Config via env (falls back to the known values / repo .env):
|
||||
# MEEGO_PLUGIN_ID, MEEGO_PLUGIN_SECRET, MEEGO_HOST, MEEGO_PROJECT,
|
||||
# MEEGO_TYPE, MEEGO_USER_KEY
|
||||
set -uo pipefail
|
||||
|
||||
# Pull MEEGO_USER_KEY (and any overrides) from repo .env if present.
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
[ -f "$SCRIPT_DIR/../.env" ] && { set -a; . "$SCRIPT_DIR/../.env"; set +a; }
|
||||
|
||||
PLUGIN_ID="${MEEGO_PLUGIN_ID:-MII_630C308A7F02410D}"
|
||||
PLUGIN_SECRET="${MEEGO_PLUGIN_SECRET:-07CD349464C75BCEEA0612152E2786DB}"
|
||||
HOST="${MEEGO_HOST_OVERRIDE:-meego.larkoffice.com}"
|
||||
PROJECT="${MEEGO_PROJECT:-tiktok}"
|
||||
TYPE_KEY="${MEEGO_TYPE:-issue}"
|
||||
USER_KEY="${MEEGO_USER_KEY:-}"
|
||||
|
||||
WORK_ITEM_ID="6931196005"
|
||||
DO_COMMENT=""; COMMENT_TEXT=""; DO_RESOLVE=""; RESOLUTION="1" # 1=本人已修复
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--comment) DO_COMMENT=1; COMMENT_TEXT="${2:-Meego 权限测试}"; shift 2;;
|
||||
--resolve) DO_RESOLVE=1; shift;;
|
||||
--resolution) RESOLUTION="$2"; shift 2;; # bug_resolution 选项值: 1本人已修复 2第三方 3无法复现 ...
|
||||
*) WORK_ITEM_ID="$1"; shift;;
|
||||
esac
|
||||
done
|
||||
|
||||
jget() { python3 -c "import sys,json
|
||||
try: d=json.load(sys.stdin)
|
||||
except: print(''); sys.exit()
|
||||
for k in '$1'.split('.'):
|
||||
d=d.get(k) if isinstance(d,dict) else None
|
||||
print('' if d is None else (json.dumps(d,ensure_ascii=False) if isinstance(d,(dict,list)) else d))"; }
|
||||
|
||||
echo "host=$HOST project=$PROJECT type=$TYPE_KEY work_item=$WORK_ITEM_ID user_key_set=$([ -n "$USER_KEY" ] && echo yes || echo NO)"
|
||||
[ -z "$USER_KEY" ] && { echo "!! MEEGO_USER_KEY 未设置(读写都需要 X-USER-KEY)"; exit 1; }
|
||||
|
||||
# 1) plugin token --------------------------------------------------------------
|
||||
TOK_RESP=$(curl -s -m 25 -X POST "https://$HOST/open_api/authen/plugin_token" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"plugin_id\":\"$PLUGIN_ID\",\"plugin_secret\":\"$PLUGIN_SECRET\",\"type\":0}")
|
||||
PT=$(echo "$TOK_RESP" | jget data.token)
|
||||
if [ -z "$PT" ]; then echo "1) plugin_token ❌ $(echo "$TOK_RESP" | head -c 200)"; exit 1; fi
|
||||
echo "1) plugin_token ✅ (${PT:0:3}…, expire=$(echo "$TOK_RESP" | jget data.expire_time)s)"
|
||||
|
||||
H=(-H "Content-Type: application/json" -H "X-PLUGIN-TOKEN: $PT" -H "X-USER-KEY: $USER_KEY")
|
||||
BASE="https://$HOST/open_api/$PROJECT"
|
||||
|
||||
# helper: POST, echo "HTTP <code> err=<err_code> <err_msg>"
|
||||
post() { # url body
|
||||
local body resp code
|
||||
resp=$(curl -s -m 25 -o /tmp/_mc.txt -w "%{http_code}" -X POST "${H[@]}" "$1" -d "$2")
|
||||
code="$resp"; body=$(cat /tmp/_mc.txt)
|
||||
echo "HTTP $code err=$(echo "$body" | jget err_code) $(echo "$body" | jget err_msg)"
|
||||
}
|
||||
|
||||
# 2) read work item ------------------------------------------------------------
|
||||
RD=$(curl -s -m 25 -X POST "${H[@]}" "$BASE/work_item/$TYPE_KEY/query" -d "{\"work_item_ids\":[$WORK_ITEM_ID]}")
|
||||
RDE=$(echo "$RD" | jget err_code)
|
||||
if [ "$RDE" = "0" ]; then
|
||||
NAME=$(echo "$RD" | python3 -c "import sys,json;d=json.load(sys.stdin)['data'][0];print(d.get('name',''))")
|
||||
STATE=$(echo "$RD" | python3 -c "import sys,json;d=json.load(sys.stdin)['data'][0];print((d.get('work_item_status') or {}).get('state_key',''))")
|
||||
echo "2) read item ✅ state=$STATE name=${NAME:0:40}"
|
||||
else
|
||||
echo "2) read item ❌ err=$RDE $(echo "$RD" | jget err_msg)"; STATE=""
|
||||
fi
|
||||
|
||||
# 3) read workflow + find current→RESOLVED transition --------------------------
|
||||
WF=$(curl -s -m 25 -X POST "${H[@]}" "$BASE/work_item/$TYPE_KEY/$WORK_ITEM_ID/workflow/query" -d '{"flow_type":1}')
|
||||
TRID=$(echo "$WF" | STATE="$STATE" python3 -c "
|
||||
import sys,json,os
|
||||
d=json.load(sys.stdin); st=os.environ.get('STATE','')
|
||||
conns=(d.get('data') or {}).get('connections',[]) if isinstance(d.get('data'),dict) else []
|
||||
cand=[c for c in conns if c.get('target_state_key')=='RESOLVED']
|
||||
exact=[c for c in cand if c.get('source_state_key')==st]
|
||||
print((exact or cand or [{}])[0].get('transition_id',''))" 2>/dev/null)
|
||||
echo "3) read workflow $([ -n "$TRID" ] && echo "✅ current($STATE)→RESOLVED transition_id=$TRID" || echo "⚠️ no RESOLVED transition found (err=$(echo "$WF" | jget err_code))")"
|
||||
|
||||
# 4) comment permission (non-mutating: empty body) -----------------------------
|
||||
# 10301 = 插件无评论权限 ; 20005/缺参 = 有权限(只是没给内容)
|
||||
echo -n "4) comment perm "; post "$BASE/work_item/$TYPE_KEY/$WORK_ITEM_ID/comment/create" '{}'
|
||||
|
||||
# 5) state-change permission (non-mutating: empty body) ------------------------
|
||||
# 20005 缺 transition_id = 接口可达 ; 10001/403 = 无权限
|
||||
echo -n "5) state perm "; post "https://$HOST/open_api/$PROJECT/workflow/$TYPE_KEY/$WORK_ITEM_ID/node/state_change" '{}'
|
||||
|
||||
# --- optional REAL writes -----------------------------------------------------
|
||||
if [ -n "$DO_COMMENT" ]; then
|
||||
echo -n ">> REAL comment "; post "$BASE/work_item/$TYPE_KEY/$WORK_ITEM_ID/comment/create" "{\"content\":\"$COMMENT_TEXT\"}"
|
||||
fi
|
||||
if [ -n "$DO_RESOLVE" ]; then
|
||||
if [ -z "$TRID" ]; then echo ">> REAL resolve ❌ 找不到 →RESOLVED 的 transition_id"; else
|
||||
echo -n ">> REAL resolve (transition $TRID, bug_resolution=$RESOLUTION, 不可逆!) "
|
||||
post "https://$HOST/open_api/$PROJECT/workflow/$TYPE_KEY/$WORK_ITEM_ID/node/state_change" \
|
||||
"{\"transition_id\":$TRID,\"field_value_pairs\":[{\"field_key\":\"bug_resolution\",\"field_value\":\"$RESOLUTION\"}]}"
|
||||
fi
|
||||
fi
|
||||
@@ -0,0 +1,149 @@
|
||||
// Drive a batch of work items to CLOSED as their current operator.
|
||||
// Reason: 历史bug无法复现 (bug_resolution = 3 无法复现,无需修复)
|
||||
// Modes: --probe read-only (state + operator + outgoing transitions)
|
||||
// (default) execute the close flow
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
const env = Object.fromEntries(
|
||||
readFileSync(new URL("../.env", import.meta.url), "utf8")
|
||||
.split("\n").map((l) => l.trim())
|
||||
.filter((l) => l && !l.startsWith("#") && l.includes("="))
|
||||
.map((l) => { const i = l.indexOf("="); return [l.slice(0, i), l.slice(i + 1)]; }),
|
||||
);
|
||||
|
||||
// .env stores a bare host; normalize to a full origin. project/type aren't in
|
||||
// .env — default to the known tiktok/issue (same as scripts/meego_check.sh).
|
||||
const rawHost = env.MEEGO_HOST || "meego.larkoffice.com";
|
||||
const ORIGIN = /^https?:\/\//.test(rawHost) ? rawHost : `https://${rawHost}`;
|
||||
const PROJECT = env.MEEGO_PROJECT || "tiktok";
|
||||
const TYPE = env.MEEGO_WORK_ITEM_TYPE || "issue";
|
||||
const DEFAULT_USER = env.MEEGO_USER_KEY || "jiaopengwen";
|
||||
|
||||
const REASON = "历史bug无法复现";
|
||||
const PROBE = process.argv.includes("--probe");
|
||||
const NO_COMMENT = process.argv.includes("--no-comment");
|
||||
const IDS = process.argv.slice(2).filter((a) => !a.startsWith("--"));
|
||||
|
||||
const RESOLUTION_VALUE = "3"; // 无法复现,无需修复
|
||||
// known select-option seeds for adaptive required-field fill on transitions
|
||||
const SELECT_SEEDS = {
|
||||
bug_resolution: "3",
|
||||
field_b45e9c: "t758tdlJp", // 线下逃逸分析: 非漏测-线下阶段无法覆盖
|
||||
};
|
||||
const NUMBER_FIELDS = new Set(["field_11605e", "field_239c4a"]); // 修复/解决耗时
|
||||
|
||||
let token = null;
|
||||
async function getToken() {
|
||||
if (token) return token;
|
||||
const r = await fetch(`${ORIGIN}/open_api/authen/plugin_token`, {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
plugin_id: env.MEEGO_PLUGIN_ID, plugin_secret: env.MEEGO_PLUGIN_SECRET,
|
||||
type: Number(env.MEEGO_TOKEN_TYPE ?? "0"),
|
||||
}),
|
||||
});
|
||||
const d = await r.json();
|
||||
token = d.data?.token ?? d.token;
|
||||
if (!token) throw new Error("token failed " + JSON.stringify(d));
|
||||
return token;
|
||||
}
|
||||
async function api(path, body, userKey = DEFAULT_USER) {
|
||||
const t = await getToken();
|
||||
const r = await fetch(`${ORIGIN}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-PLUGIN-TOKEN": t, "X-USER-KEY": userKey },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return r.json();
|
||||
}
|
||||
const codeOf = (r) => r?.err_code ?? r?.error?.code ?? r?.err?.code ?? 0;
|
||||
const msgOf = (r) => r?.err?.msg || r?.err_msg || r?.error?.msg || r?.err?.message || "";
|
||||
|
||||
function firstUserKey(v) {
|
||||
if (Array.isArray(v) && v.length) {
|
||||
const f = v[0];
|
||||
if (typeof f === "string" || typeof f === "number") return String(f);
|
||||
if (f && typeof f === "object") return String(f.user_key ?? f.value ?? f.key ?? "");
|
||||
}
|
||||
if (typeof v === "string" || typeof v === "number") return String(v);
|
||||
if (v && typeof v === "object") return String(v.value ?? "");
|
||||
return "";
|
||||
}
|
||||
async function queryItem(id) {
|
||||
const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, { work_item_ids: [Number(id)] });
|
||||
const item = q.data?.[0];
|
||||
if (!item) throw new Error("not found / read failed: " + JSON.stringify(msgOf(q) || q));
|
||||
const field = (k) => item.fields?.find?.((f) => f.field_key === k)?.field_value;
|
||||
const operator = firstUserKey(field("current_status_operator")) || firstUserKey(field("issue_operator"));
|
||||
// state_key comes back UPPERCASE (RESOLVED/CLOSED/...); normalize to lowercase
|
||||
// so the comparisons below (and target_state_key matching) line up.
|
||||
return { item, state: String(item.work_item_status?.state_key || "").toLowerCase(), operator, name: item.name };
|
||||
}
|
||||
async function outgoing(id, userKey, state) {
|
||||
const wf = await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 1 }, userKey);
|
||||
const conns = wf.data?.connections || wf.data?.workflow_confs || [];
|
||||
const want = String(state || "").toLowerCase();
|
||||
return conns.filter((c) => String(c.source_state_key || "").toLowerCase() === want);
|
||||
}
|
||||
|
||||
// adaptive state_change reacting to required/illegal field errors
|
||||
async function stateChange(id, transitionId, userKey) {
|
||||
const fields = new Map([["bug_resolution", { value: RESOLUTION_VALUE }]]);
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const body = { transition_id: Number(transitionId), fields: [...fields].map(([field_key, field_value]) => ({ field_key, field_value })) };
|
||||
const res = await api(`/open_api/${PROJECT}/workflow/${TYPE}/${id}/node/state_change`, body, userKey);
|
||||
if (codeOf(res) === 0) return { ok: true, used: body.fields };
|
||||
const msg = msgOf(res);
|
||||
const m = msg.match(/\[?([a-zA-Z0-9_]+)\]?[^]*?\b(required|illegal|empty)/i);
|
||||
if (!m) return { ok: false, msg: msg || JSON.stringify(res) };
|
||||
const key = m[1], kind = m[2].toLowerCase();
|
||||
if (kind === "illegal") {
|
||||
if (NUMBER_FIELDS.has(key) && typeof fields.get(key) !== "number") fields.set(key, 0);
|
||||
else if (fields.has(key)) fields.delete(key);
|
||||
else return { ok: false, msg: `illegal field ${key} not in payload: ${msg}` };
|
||||
} else {
|
||||
if (SELECT_SEEDS[key]) fields.set(key, { value: SELECT_SEEDS[key] });
|
||||
else if (NUMBER_FIELDS.has(key)) fields.set(key, 0);
|
||||
else return { ok: false, msg: `unknown required field ${key}: ${msg}` };
|
||||
}
|
||||
}
|
||||
return { ok: false, msg: "exceeded adaptive iterations" };
|
||||
}
|
||||
|
||||
for (const id of IDS) {
|
||||
try {
|
||||
let { state, operator, name } = await queryItem(id);
|
||||
const actor = operator || DEFAULT_USER;
|
||||
if (PROBE) {
|
||||
const outs = await outgoing(id, actor, state);
|
||||
console.log(`${id}\tstate=${state}\toperator=${operator || "(none)"}\tname="${name}"`);
|
||||
console.log(` out: ${outs.map((c) => `${c.target_state_key}#${c.transition_id}`).join(" ") || "(none)"}`);
|
||||
continue;
|
||||
}
|
||||
console.log(`\n### ${id} "${name}" start=${state} operator=${operator || "(none)"} acting=${actor}`);
|
||||
if (state === "closed") { console.log(" already CLOSED — skip"); continue; }
|
||||
|
||||
if (!NO_COMMENT) {
|
||||
const c = await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/comment/create`, { content: REASON }, actor);
|
||||
console.log(` comment: ${codeOf(c) === 0 ? "ok" : msgOf(c)}`);
|
||||
}
|
||||
|
||||
// walk toward closed: open→resolved→closed (find transition ids live)
|
||||
for (let hop = 0; hop < 3 && state !== "closed"; hop++) {
|
||||
const outs = await outgoing(id, operator || DEFAULT_USER, state);
|
||||
const tgt = (c) => String(c.target_state_key || "").toLowerCase();
|
||||
const toClosed = outs.find((c) => tgt(c) === "closed");
|
||||
const toResolved = outs.find((c) => tgt(c) === "resolved");
|
||||
const next = toClosed || toResolved;
|
||||
if (!next) { console.log(` no path forward from ${state} (out: ${outs.map((o) => o.target_state_key).join(",") || "none"})`); break; }
|
||||
const r = await stateChange(id, next.transition_id, operator || DEFAULT_USER);
|
||||
console.log(` ${state}→${next.target_state_key} (#${next.transition_id}): ${r.ok ? "ok " + JSON.stringify(r.used) : "FAIL " + r.msg}`);
|
||||
if (!r.ok) break;
|
||||
({ state, operator } = await queryItem(id)); // operator can change after a transition
|
||||
}
|
||||
const fin = await queryItem(id);
|
||||
console.log(` FINAL state=${fin.state} ${fin.state === "closed" ? "✅" : "⚠️"}`);
|
||||
} catch (e) {
|
||||
console.log(`### ${id} ERROR ${e.message}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
const env = Object.fromEntries(
|
||||
readFileSync(new URL("../.env", import.meta.url), "utf8")
|
||||
.split("\n").map((l) => l.trim()).filter((l) => l && !l.startsWith("#") && l.includes("="))
|
||||
.map((l) => { const i = l.indexOf("="); return [l.slice(0, i), l.slice(i + 1)]; }),
|
||||
);
|
||||
const rawHost = env.MEEGO_HOST || "meego.larkoffice.com";
|
||||
const ORIGIN = /^https?:\/\//.test(rawHost) ? rawHost : `https://${rawHost}`;
|
||||
const PROJECT = env.MEEGO_PROJECT || "tiktok", TYPE = env.MEEGO_WORK_ITEM_TYPE || "issue";
|
||||
let token = null;
|
||||
async function getToken() {
|
||||
if (token) return token;
|
||||
const r = await fetch(`${ORIGIN}/open_api/authen/plugin_token`, { method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ plugin_id: env.MEEGO_PLUGIN_ID, plugin_secret: env.MEEGO_PLUGIN_SECRET, type: Number(env.MEEGO_TOKEN_TYPE ?? "0") }) });
|
||||
const d = await r.json(); token = d.data?.token ?? d.token; return token;
|
||||
}
|
||||
async function api(path, body, userKey) {
|
||||
const t = await getToken();
|
||||
const r = await fetch(`${ORIGIN}${path}`, { method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-PLUGIN-TOKEN": t, "X-USER-KEY": userKey }, body: JSON.stringify(body) });
|
||||
return r.json();
|
||||
}
|
||||
const id = process.argv[2];
|
||||
const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, { work_item_ids: [Number(id)] }, env.MEEGO_USER_KEY);
|
||||
const item = q.data?.[0];
|
||||
const op = item?.fields?.find((f) => f.field_key === "current_status_operator")?.field_value;
|
||||
console.log("state:", item?.work_item_status?.state_key, "| template_id:", item?.template_id, "| work_item_type:", item?.work_item_type_key);
|
||||
console.log("current_status_operator raw:", JSON.stringify(op));
|
||||
for (const ft of [1, 2]) {
|
||||
for (const uk of [String(Array.isArray(op) ? (op[0]?.value ?? op[0]) : op), env.MEEGO_USER_KEY]) {
|
||||
const wf = await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: ft }, uk);
|
||||
const conns = wf.data?.connections || wf.data?.workflow_confs;
|
||||
console.log(`flow_type=${ft} user=${uk}: code=${wf.err_code ?? 0} msg=${wf.err?.msg || ""} conns=${conns ? JSON.stringify(conns) : JSON.stringify(wf.data)?.slice(0,200)}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// read-only probe: current status + workflow graph for a batch of work items
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
const env = Object.fromEntries(
|
||||
readFileSync(new URL("../.env", import.meta.url), "utf8")
|
||||
.split("\n")
|
||||
.map((l) => l.trim())
|
||||
.filter((l) => l && !l.startsWith("#") && l.includes("="))
|
||||
.map((l) => {
|
||||
const i = l.indexOf("=");
|
||||
return [l.slice(0, i), l.slice(i + 1)];
|
||||
}),
|
||||
);
|
||||
|
||||
const HOST = env.MEEGO_HOST;
|
||||
const PROJECT = env.MEEGO_PROJECT;
|
||||
const TYPE = env.MEEGO_WORK_ITEM_TYPE;
|
||||
|
||||
let token = null;
|
||||
async function getToken() {
|
||||
if (token) return token;
|
||||
const res = await fetch(`${HOST}/open_api/authen/plugin_token`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
plugin_id: env.MEEGO_PLUGIN_ID,
|
||||
plugin_secret: env.MEEGO_PLUGIN_SECRET,
|
||||
type: Number(env.MEEGO_TOKEN_TYPE ?? "0"),
|
||||
}),
|
||||
});
|
||||
const d = await res.json();
|
||||
token = d.data?.token ?? d.token;
|
||||
if (!token) throw new Error("token failed: " + JSON.stringify(d));
|
||||
return token;
|
||||
}
|
||||
|
||||
async function api(path, body, userKey = env.MEEGO_USER_KEY) {
|
||||
const t = await getToken();
|
||||
const res = await fetch(`${HOST}${path}`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-PLUGIN-TOKEN": t,
|
||||
"X-USER-KEY": userKey,
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
const IDS = process.argv.slice(2);
|
||||
|
||||
for (const id of IDS) {
|
||||
try {
|
||||
const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, {
|
||||
work_item_ids: [Number(id)],
|
||||
});
|
||||
const item = q.data?.[0];
|
||||
if (!item) {
|
||||
console.log(`${id}\tNOT_FOUND\t${JSON.stringify(q.err ?? q.error ?? q)}`);
|
||||
continue;
|
||||
}
|
||||
const wf = await api(
|
||||
`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`,
|
||||
{ flow_type: 1 },
|
||||
);
|
||||
const conns = wf.data?.workflow_confs ?? wf.data?.connections ?? wf.data;
|
||||
const stateOperator =
|
||||
item.fields?.find?.((f) => f.field_key === "current_status_operator")
|
||||
?.field_value ?? "";
|
||||
console.log(
|
||||
`\n=== ${id} | name="${item.name}" | state=${item.work_item_status?.state_key} | operator=${JSON.stringify(stateOperator)}`,
|
||||
);
|
||||
console.log(" workflow:", JSON.stringify(conns));
|
||||
} catch (e) {
|
||||
console.log(`${id}\tERROR\t${e.message}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
const env = Object.fromEntries(
|
||||
readFileSync(new URL("../.env", import.meta.url), "utf8")
|
||||
.split("\n").map((l) => l.trim())
|
||||
.filter((l) => l && !l.startsWith("#") && l.includes("="))
|
||||
.map((l) => { const i = l.indexOf("="); return [l.slice(0, i), l.slice(i + 1)]; }),
|
||||
);
|
||||
const HOST = env.MEEGO_HOST, PROJECT = env.MEEGO_PROJECT, TYPE = env.MEEGO_WORK_ITEM_TYPE;
|
||||
let token = null;
|
||||
async function getToken() {
|
||||
if (token) return token;
|
||||
const r = await fetch(`${HOST}/open_api/authen/plugin_token`, {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ plugin_id: env.MEEGO_PLUGIN_ID, plugin_secret: env.MEEGO_PLUGIN_SECRET, type: Number(env.MEEGO_TOKEN_TYPE ?? "0") }),
|
||||
});
|
||||
const d = await r.json(); token = d.data?.token ?? d.token;
|
||||
if (!token) throw new Error("token failed " + JSON.stringify(d));
|
||||
return token;
|
||||
}
|
||||
async function api(path, body, userKey = env.MEEGO_USER_KEY) {
|
||||
const t = await getToken();
|
||||
const r = await fetch(`${HOST}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-PLUGIN-TOKEN": t, "X-USER-KEY": userKey },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return r.json();
|
||||
}
|
||||
const id = process.argv[2];
|
||||
console.log("--- query ---");
|
||||
const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, { work_item_ids: [Number(id)] });
|
||||
console.log(JSON.stringify(q, null, 2).slice(0, 800));
|
||||
console.log("--- workflow flow_type:1 ---");
|
||||
console.log(JSON.stringify(await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 1 }), null, 2));
|
||||
console.log("--- workflow flow_type:2 ---");
|
||||
console.log(JSON.stringify(await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 2 }), null, 2));
|
||||
@@ -0,0 +1,5 @@
|
||||
// Explicit invocation after 0031. Uses configured DATABASE_URL; never run from an HTTP GET.
|
||||
import { migrateLegacyKnowledge } from "../control-server/src/eddy_knowledge_migration";
|
||||
import { closePool } from "../control-server/src/db_client";
|
||||
try { console.log(JSON.stringify(await migrateLegacyKnowledge(),null,2)); }
|
||||
finally { await closePool(); }
|
||||
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* 推合码(control-server/src/mr_merge_nudge.ts)的效果指标。只读生产库(只发 SELECT)。
|
||||
*
|
||||
* 用法(在 devbox 上,控制面目录):
|
||||
* bun --env-file=../.env run ../scripts/mr-nudge-metrics.ts [--weeks 10]
|
||||
* 本机没有库串时照 memory「生产 PG 只读查询配方」经 stdin 送过去:... bun --env-file=../.env run - --weeks 10 < scripts/mr-nudge-metrics.ts
|
||||
*
|
||||
* 口径:从「触发时刻」t0 起算——
|
||||
* - 前后对比:t0 = RD 在面板上点「复核通过」那次流转(manual_review → fix_succeeded,actor 是人;
|
||||
* system:* 和没记 actor 的排除,因为「MR 合入后自动确认」的 t0 就是合入时刻,会把数字做假)。
|
||||
* 按 t0 所在周分组,只统计 t0 时 MR 还没合的单。
|
||||
* - 同期对照:上线后看 settings `mrNudge:<taskId>`,只比 eligible 的单,推的一组 vs 对照组。
|
||||
* - 合入率 @Nd:t0 之后 N 天内合入的比例。只算已经满 N 天的单(没满的不进分母),所以最近几周的长窗口会是「-」。
|
||||
* - 合入耗时中位:14 天内合入的单里,t0 → 合入的中位数(小时)。
|
||||
*
|
||||
* 局限:tasks.mr_merged_at 由 mr_sync 写,没合的 MR 不知道何时被关;周与周之间的差异还受版本节奏、
|
||||
* reviewer 忙闲影响,所以结论以同期对照为准,前后对比只看趋势。
|
||||
*/
|
||||
import pg from "pg";
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const weeks = Number(args[args.indexOf("--weeks") + 1]) || 10;
|
||||
const DAY = 86_400_000;
|
||||
const WINDOWS = [1, 3, 7, 14];
|
||||
|
||||
const u = new URL(process.env.DATABASE_URL!);
|
||||
const pool = new pg.Pool({
|
||||
host: u.hostname.replace(/^\[|\]$/g, ""), port: Number(u.port || 5432),
|
||||
user: decodeURIComponent(u.username), password: decodeURIComponent(u.password),
|
||||
database: u.pathname.slice(1), connectionTimeoutMillis: 30_000,
|
||||
});
|
||||
async function select<T = any>(sql: string, params: unknown[] = []): Promise<T[]> {
|
||||
if (!/^\s*(select|with)\b/i.test(sql)) throw new Error("只允许 SELECT");
|
||||
return (await pool.query(sql, params)).rows as T[];
|
||||
}
|
||||
|
||||
interface Row { taskId: string; t0: number; mergedAt: number | null; mrState: string | null }
|
||||
|
||||
function summarize(rows: Row[], now: number) {
|
||||
const out: Record<string, string> = { n: String(rows.length) };
|
||||
for (const d of WINDOWS) {
|
||||
const ripe = rows.filter((r) => now - r.t0 >= d * DAY);
|
||||
const hit = ripe.filter((r) => r.mergedAt != null && r.mergedAt - r.t0 <= d * DAY);
|
||||
out[`@${d}d`] = ripe.length >= 5 ? `${Math.round((100 * hit.length) / ripe.length)}% (${hit.length}/${ripe.length})` : "-";
|
||||
}
|
||||
const hrs = rows.filter((r) => r.mergedAt != null && r.mergedAt - r.t0 <= 14 * DAY).map((r) => (r.mergedAt! - r.t0) / 3_600_000).sort((a, b) => a - b);
|
||||
out["中位h"] = hrs.length ? hrs[Math.floor(hrs.length / 2)].toFixed(1) : "-";
|
||||
return out;
|
||||
}
|
||||
|
||||
function print(title: string, groups: [string, Row[]][], now: number) {
|
||||
console.log(`\n## ${title}`);
|
||||
const cols = ["n", ...WINDOWS.map((d) => `@${d}d`), "中位h"];
|
||||
console.log(["分组", ...cols].join("\t"));
|
||||
for (const [k, rows] of groups) {
|
||||
const s = summarize(rows, now);
|
||||
console.log([k, ...cols.map((c) => s[c])].join("\t"));
|
||||
}
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
const since = now - weeks * 7 * DAY;
|
||||
|
||||
// ── 前后对比:RD 人工复核通过 ─────────────────────────────────────────────
|
||||
const approvals = await select<{ task_id: string; t0: string; merged_at: string | null; mr_state: string | null }>(`
|
||||
with first_approve as (
|
||||
select target_id, min(ts) as t0 from task_transitions
|
||||
where kind = 'bug' and from_status = 'manual_review' and to_status = 'fix_succeeded'
|
||||
and actor is not null and actor not like 'system:%' and ts >= $1
|
||||
group by target_id
|
||||
)
|
||||
select fa.target_id as task_id, fa.t0, t.mr_merged_at as merged_at, t.mr_state
|
||||
from first_approve fa join tasks t on t.id = fa.target_id
|
||||
where t.mr_url is not null and (t.mr_merged_at is null or t.mr_merged_at > fa.t0)`, [since]);
|
||||
const byWeek = new Map<string, Row[]>();
|
||||
for (const a of approvals) {
|
||||
const t0 = Number(a.t0);
|
||||
const wk = new Date(t0 - ((new Date(t0).getUTCDay() + 6) % 7) * DAY).toISOString().slice(0, 10);
|
||||
const list = byWeek.get(wk) ?? [];
|
||||
list.push({ taskId: a.task_id, t0, mergedAt: a.merged_at == null ? null : Number(a.merged_at), mrState: a.mr_state });
|
||||
byWeek.set(wk, list);
|
||||
}
|
||||
print(`前后对比 · RD 复核通过后(按周,近 ${weeks} 周)`, [...byWeek.entries()].sort(([a], [b]) => a.localeCompare(b)), now);
|
||||
|
||||
// ── 同期对照:上线后的推合码记录 ────────────────────────────────────────────
|
||||
const recs = await select<{ key: string; value: string; merged_at: string | null; mr_state: string | null }>(`
|
||||
select s.key, s.value, t.mr_merged_at as merged_at, t.mr_state
|
||||
from settings s join tasks t on t.id = substring(s.key from 9)
|
||||
where s.key like 'mrNudge:%'`);
|
||||
const arms = new Map<string, Row[]>();
|
||||
const skipped = new Map<string, number>();
|
||||
let delivered = 0, groupsCreated = 0;
|
||||
for (const r of recs) {
|
||||
let v: any;
|
||||
try { v = JSON.parse(r.value); } catch { continue; }
|
||||
if ((v?.v ?? 1) < 2) continue; // v1 记录(reviewer 读错)不进指标
|
||||
if (!v?.eligible) { skipped.set(v?.skipped ?? "?", (skipped.get(v?.skipped ?? "?") ?? 0) + 1); continue; }
|
||||
if (v.arm === "nudge") { if (v.reminded) delivered++; if (v.chatId && !v.groupExisted) groupsCreated++; }
|
||||
for (const key of [`${v.arm}`, `${v.arm} · ${v.trigger}`]) {
|
||||
const list = arms.get(key) ?? [];
|
||||
list.push({ taskId: r.key.slice(8), t0: Number(v.at), mergedAt: r.merged_at == null ? null : Number(r.merged_at), mrState: r.mr_state });
|
||||
arms.set(key, list);
|
||||
}
|
||||
}
|
||||
if (recs.length) {
|
||||
print("同期对照 · 推 vs 不推(只含 eligible)", [...arms.entries()].sort(([a], [b]) => a.localeCompare(b)), now);
|
||||
console.log(`\n推的一组:新建群 ${groupsCreated},催审送达 ${delivered}`);
|
||||
console.log(`没推的原因:${[...skipped.entries()].map(([k, n]) => `${k} ${n}`).join(",") || "无"}`);
|
||||
} else {
|
||||
console.log("\n## 同期对照\n还没有推合码记录(功能未上线或尚无触发)。");
|
||||
}
|
||||
await pool.end();
|
||||
@@ -0,0 +1,56 @@
|
||||
// P2 data copy: SQLite → Postgres THROUGH the Drizzle client, so the camelCase
|
||||
// entity ↔ snake_case column mapping is exercised on every real row. Parents
|
||||
// (tasks) first for the FK. Idempotent-ish: assumes tables freshly created by
|
||||
// the drizzle-kit DDL (run pg schema apply first).
|
||||
//
|
||||
// DATABASE_URL=… STREAMLINE_DB_PATH=~/.streamline/data.sqlite bun scripts/pg_copy_drizzle.ts
|
||||
import { drizzle } from "drizzle-orm/bun-sql";
|
||||
import { SQL } from "bun";
|
||||
import { sql } from "drizzle-orm";
|
||||
import { Database } from "bun:sqlite";
|
||||
import { homedir } from "node:os";
|
||||
import path from "node:path";
|
||||
import * as schema from "../control-server/src/schema";
|
||||
|
||||
const SRC = process.env.STREAMLINE_DB_PATH || path.join(homedir(), ".streamline", "data.sqlite");
|
||||
const client = new SQL(process.env.DATABASE_URL!);
|
||||
const db = drizzle({ client, schema, casing: "snake_case" });
|
||||
const sq = new Database(SRC, { readonly: true });
|
||||
|
||||
// tasks first (parent of logs + fix_report_versions); rest in any order.
|
||||
const order = [
|
||||
"tasks", "features", "security", "nodes", "settings", "node_clusters",
|
||||
"lark_users", "watcher_names", "readable_prompts", "readable_report_runs",
|
||||
"readable_batch_jobs", "readable_sources", "fix_report_versions",
|
||||
"evidence_media", "evidence_capture_events", "task_transitions", "logs",
|
||||
] as const;
|
||||
|
||||
let failures = 0;
|
||||
for (const name of order) {
|
||||
const table = (schema as Record<string, unknown>)[name] as never;
|
||||
const rows = sq.query(`SELECT * FROM "${name}"`).all() as Record<string, unknown>[];
|
||||
if (!rows.length) { console.log(` ${name}: 0 rows`); continue; }
|
||||
const batch = 400;
|
||||
let copied = 0;
|
||||
for (let i = 0; i < rows.length; i += batch) {
|
||||
await db.insert(table).values(rows.slice(i, i + batch) as never);
|
||||
copied += Math.min(batch, rows.length - i);
|
||||
}
|
||||
console.log(` ${name}: copied ${copied}`);
|
||||
}
|
||||
|
||||
// advance logs identity sequence past the copied max id
|
||||
await client.unsafe(
|
||||
"select setval(pg_get_serial_sequence('logs','id'), (select coalesce(max(id),1) from logs), true)");
|
||||
|
||||
// verify counts
|
||||
console.log("[copy] verifying counts…");
|
||||
for (const name of order) {
|
||||
const s = (sq.query(`SELECT COUNT(*) c FROM "${name}"`).get() as { c: number }).c;
|
||||
const p = Number((await client.unsafe(`select count(*)::int c from "${name}"`))[0].c);
|
||||
const ok = s === p; if (!ok) failures++;
|
||||
console.log(` ${ok ? "✓" : "✗"} ${name}: sqlite=${s} pg=${p}`);
|
||||
}
|
||||
await client.end();
|
||||
console.log(failures === 0 ? "\n[copy] DONE — counts match ✅" : `\n[copy] ${failures} MISMATCH ❌`);
|
||||
process.exit(failures === 0 ? 0 : 1);
|
||||
@@ -0,0 +1,198 @@
|
||||
// ---------------------------------------------------------------------------
|
||||
// pg_parity — prove the SQLite and Postgres engines return IDENTICAL data for
|
||||
// every read/query DAL method. The PG data was migrated from this SQLite, so a
|
||||
// correct adapter must make each query indistinguishable across engines.
|
||||
//
|
||||
// # dump one engine's answers, then the other, then diff:
|
||||
// DB_ENGINE=sqlite bun scripts/pg_parity.ts dump /tmp/parity_sqlite.json
|
||||
// DB_ENGINE=pg bun scripts/pg_parity.ts dump /tmp/parity_pg.json
|
||||
// bun scripts/pg_parity.ts compare /tmp/parity_sqlite.json /tmp/parity_pg.json
|
||||
//
|
||||
// Args for per-id methods are drawn from the live DB via deterministic
|
||||
// `ORDER BY <pk>` queries, so BOTH runs probe the exact same rows. Comparison is
|
||||
// data-parity (order-insensitive set equality) with a separate order-diff note,
|
||||
// since query ORDER BY ties may legitimately differ but the DATA must not.
|
||||
// ---------------------------------------------------------------------------
|
||||
import { sql } from "drizzle-orm";
|
||||
import { db as drizzleDb } from "../control-server/src/db_client";
|
||||
import * as db from "../control-server/src/db";
|
||||
import * as lab from "../control-server/src/lab_db";
|
||||
import * as life from "../control-server/src/lifecycle_db";
|
||||
import * as evid from "../control-server/src/evidmon_db";
|
||||
import * as watch from "../control-server/src/watchers";
|
||||
|
||||
// ── canonical form ─────────────────────────────────────────────────────────
|
||||
// undefined→null (JS artifact, not a data difference); Map→sorted entries;
|
||||
// object keys sorted; arrays sorted by element canonical-string for SET equality.
|
||||
function canonOrdered(v: unknown): unknown {
|
||||
if (v === undefined) return null;
|
||||
if (v === null || typeof v !== "object") return v;
|
||||
if (v instanceof Map) {
|
||||
return { __map: [...v.entries()].map(([k, val]) => [k, canonOrdered(val)]).sort((a, b) => String(a[0]).localeCompare(String(b[0]))) };
|
||||
}
|
||||
if (Array.isArray(v)) return v.map(canonOrdered);
|
||||
const o = v as Record<string, unknown>;
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const k of Object.keys(o).sort()) out[k] = canonOrdered(o[k]);
|
||||
return out;
|
||||
}
|
||||
function canonSorted(v: unknown): unknown {
|
||||
if (v === undefined) return null;
|
||||
if (v === null || typeof v !== "object") return v;
|
||||
if (v instanceof Map) return canonOrdered(v);
|
||||
if (Array.isArray(v)) {
|
||||
const mapped = v.map(canonSorted);
|
||||
return [...mapped].sort((a, b) => JSON.stringify(a).localeCompare(JSON.stringify(b)));
|
||||
}
|
||||
const o = v as Record<string, unknown>;
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const k of Object.keys(o).sort()) out[k] = canonSorted(o[k]);
|
||||
return out;
|
||||
}
|
||||
|
||||
async function ids(query: string): Promise<Record<string, unknown>[]> {
|
||||
const res = await drizzleDb.execute(sql.raw(query));
|
||||
return (Array.isArray(res) ? res : ((res as { rows?: unknown[] }).rows ?? [])) as Record<string, unknown>[];
|
||||
}
|
||||
|
||||
async function buildCases() {
|
||||
// deterministic argument samples (same rows on both engines)
|
||||
// PG is snake_case; alias back to the camelCase keys buildCases reads (quoted
|
||||
// aliases preserve case). SQLite golden was dumped with camelCase columns.
|
||||
const taskRows = await ids(`SELECT id, meego_id AS "meegoId" FROM tasks ORDER BY id LIMIT 8`);
|
||||
const featRows = await ids("SELECT id FROM features ORDER BY id LIMIT 8");
|
||||
const secRows = await ids("SELECT id FROM security ORDER BY id LIMIT 8");
|
||||
const nodeRows = await ids("SELECT id FROM nodes ORDER BY id");
|
||||
const promptRows = await ids("SELECT id FROM readable_prompts ORDER BY id LIMIT 8");
|
||||
const runRows = await ids(`SELECT id, target_type AS "targetType", target_id AS "targetId", batch_id AS "batchId" FROM readable_report_runs ORDER BY id LIMIT 8`);
|
||||
const batchRows = await ids("SELECT id FROM readable_batch_jobs ORDER BY id");
|
||||
const srcRows = await ids(`SELECT target_type AS "targetType", target_id AS "targetId" FROM readable_sources ORDER BY target_type, target_id LIMIT 8`);
|
||||
const emRows = await ids(`SELECT DISTINCT owner_kind AS "ownerKind", owner_id AS "ownerId" FROM evidence_media ORDER BY owner_kind, owner_id LIMIT 8`);
|
||||
const evRows = await ids(`SELECT DISTINCT target_type AS "targetType", target_id AS "targetId" FROM evidence_capture_events ORDER BY target_type, target_id LIMIT 8`);
|
||||
const txRows = await ids(`SELECT DISTINCT kind, target_id AS "targetId" FROM task_transitions ORDER BY kind, target_id LIMIT 8`);
|
||||
const wkeys = (await ids("SELECT wkey FROM watcher_names ORDER BY wkey LIMIT 12")).map((r) => r.wkey as string);
|
||||
const larkIds = (await ids(`SELECT lark_user_id AS "larkUserId" FROM lark_users ORDER BY lark_user_id`)).map((r) => r.larkUserId as string);
|
||||
const setKeys = (await ids("SELECT key FROM settings ORDER BY key")).map((r) => r.key as string);
|
||||
const frRows = await ids(`SELECT task_id AS "taskId", round FROM fix_report_versions ORDER BY task_id, round LIMIT 8`);
|
||||
|
||||
const cases: { name: string; run: () => Promise<unknown> }[] = [];
|
||||
const add = (name: string, run: () => Promise<unknown>) => cases.push({ name, run });
|
||||
const each = <T>(rows: T[], name: string, run: (r: T) => Promise<unknown>) =>
|
||||
rows.forEach((r, i) => add(`${name}[${i}]`, () => run(r)));
|
||||
|
||||
// ── db.ts collections ──
|
||||
add("listTasks", () => db.listTasks());
|
||||
add("listFeatures", () => db.listFeatures());
|
||||
add("listSecurity", () => db.listSecurity());
|
||||
add("listNodes", () => db.listNodes());
|
||||
add("listAllClusters", () => db.listAllClusters());
|
||||
add("getWebhookUrls", () => db.getWebhookUrls());
|
||||
add("nextAwaitingFixTask", () => db.nextAwaitingFixTask());
|
||||
// ── db.ts per-id ──
|
||||
each(taskRows, "getTask", (r) => db.getTask(r.id as string));
|
||||
each(taskRows, "getTaskByMeegoId", (r) => db.getTaskByMeegoId(r.meegoId as string));
|
||||
each(taskRows, "getTaskReadableReport", (r) => db.getTaskReadableReport(r.id as string));
|
||||
each(taskRows, "getTaskCreatorEmail", (r) => db.getTaskCreatorEmail(r.id as string));
|
||||
each(taskRows, "getFixReportVersions", (r) => db.getFixReportVersions(r.id as string));
|
||||
each(frRows, "hasFixReportVersion", (r) => db.hasFixReportVersion(r.taskId as string, Number(r.round)));
|
||||
each(featRows, "getFeature", (r) => db.getFeature(r.id as string));
|
||||
each(featRows, "getFeatureReadableReport", (r) => db.getFeatureReadableReport(r.id as string));
|
||||
each(featRows, "getFeatureCreatorEmail", (r) => db.getFeatureCreatorEmail(r.id as string));
|
||||
each(secRows, "getSecurity", (r) => db.getSecurity(r.id as string));
|
||||
each(secRows, "getSecurityReadableReport", (r) => db.getSecurityReadableReport(r.id as string));
|
||||
each(secRows, "getSecurityCreatorEmail", (r) => db.getSecurityCreatorEmail(r.id as string));
|
||||
each(nodeRows, "getNode", (r) => db.getNode(r.id as string));
|
||||
each(nodeRows, "getNodeClusters", (r) => db.getNodeClusters(r.id as string));
|
||||
each(nodeRows, "countEvalOnNode", (r) => db.countEvalOnNode(r.id as string));
|
||||
each(nodeRows, "countFixOnNode", (r) => db.countFixOnNode(r.id as string));
|
||||
each(emRows, "getEvidenceMedia", (r) => db.getEvidenceMedia(r.ownerKind as never, r.ownerId as string));
|
||||
each(emRows, "getEvidenceMediaMap", (r) => db.getEvidenceMediaMap(r.ownerKind as never, r.ownerId as string));
|
||||
each(larkIds.map((x) => ({ x })), "getLarkUser", (r) => db.getLarkUser(r.x));
|
||||
each(setKeys.map((x) => ({ x })), "getSetting", (r) => db.getSetting(r.x, null));
|
||||
each(wkeys.map((x) => ({ x })), "getWatcherName", (r) => db.getWatcherName(r.x));
|
||||
add("getWatcherNames(all)", () => db.getWatcherNames(wkeys));
|
||||
|
||||
// ── lab_db ──
|
||||
add("listPrompts", () => lab.listPrompts(false));
|
||||
add("listPrompts(incl archived)", () => lab.listPrompts(true));
|
||||
add("currentReadablePrompt", () => lab.currentReadablePrompt());
|
||||
add("listBatchJobs", () => lab.listBatchJobs(50));
|
||||
add("hasRunningBatchJob", () => lab.hasRunningBatchJob());
|
||||
add("readLabMembers", () => lab.readLabMembers());
|
||||
each(promptRows, "getPrompt", (r) => lab.getPrompt(r.id as string));
|
||||
each(runRows, "getRun", (r) => lab.getRun(r.id as string));
|
||||
each(runRows.filter((r) => r.batchId), "listRunsByBatch", (r) => lab.listRunsByBatch(r.batchId as string));
|
||||
each(batchRows, "getBatchJob", (r) => lab.getBatchJob(r.id as string));
|
||||
each(runRows, "listRunsForTarget", (r) => lab.listRunsForTarget(r.targetType as never, r.targetId as string));
|
||||
each(srcRows, "getSource", (r) => lab.getSource(r.targetType as never, r.targetId as string));
|
||||
add("isLabMember(sample)", async () => ({ a: await lab.isLabMember("jingyuan.yi"), b: await lab.isLabMember("nope-xyz") }));
|
||||
|
||||
// ── lifecycle_db ──
|
||||
add("selectTransitions()", () => life.selectTransitions({}));
|
||||
add("selectTransitions(bug)", () => life.selectTransitions({ kind: "bug" }));
|
||||
add("listTargetSummaries()", () => life.listTargetSummaries({}));
|
||||
each(txRows, "transitionsForTarget", (r) => life.transitionsForTarget(r.kind as never, r.targetId as string));
|
||||
each(txRows, "titleOf", (r) => life.titleOf(r.kind as never, r.targetId as string));
|
||||
|
||||
// ── evidmon_db ──
|
||||
add("selectEvents()", () => evid.selectEvents({}));
|
||||
add("listTargetsWithEvents", () => evid.listTargetsWithEvents(0, 200));
|
||||
each(evRows, "eventsForTarget", (r) => evid.eventsForTarget(r.targetType as never, r.targetId as string));
|
||||
|
||||
// ── watchers (DB-backed resolution) ──
|
||||
add("resolveWatcherLabels", () => watch.resolveWatcherLabels(wkeys));
|
||||
add("dedupeWatchers", () => watch.dedupeWatchers(wkeys));
|
||||
|
||||
return cases;
|
||||
}
|
||||
|
||||
async function dump(outPath: string) {
|
||||
const cases = await buildCases();
|
||||
const out: Record<string, unknown> = {};
|
||||
for (const c of cases) {
|
||||
try { out[c.name] = await c.run(); }
|
||||
catch (e) { out[c.name] = { __error: String((e as Error).message || e) }; }
|
||||
}
|
||||
await Bun.write(outPath, JSON.stringify({ engine: "pg", count: cases.length, results: out }, null, 2));
|
||||
console.log(`[parity] engine=pg (drizzle): dumped ${cases.length} methods → ${outPath}`);
|
||||
}
|
||||
|
||||
async function compare(aPath: string, bPath: string) {
|
||||
const A = JSON.parse(await Bun.file(aPath).text());
|
||||
const B = JSON.parse(await Bun.file(bPath).text());
|
||||
const names = [...new Set([...Object.keys(A.results), ...Object.keys(B.results)])].sort();
|
||||
let dataDiff = 0, orderDiff = 0, ok = 0, errs = 0;
|
||||
console.log(`[parity] comparing ${A.engine} vs ${B.engine} over ${names.length} methods\n`);
|
||||
for (const n of names) {
|
||||
const a = A.results[n], b = B.results[n];
|
||||
const aErr = a && typeof a === "object" && "__error" in a;
|
||||
const bErr = b && typeof b === "object" && "__error" in b;
|
||||
if (aErr || bErr) {
|
||||
if (JSON.stringify(a) === JSON.stringify(b)) { errs++; continue; } // both errored same → ignore
|
||||
console.log(` ✗ ${n} ERROR mismatch\n ${A.engine}: ${JSON.stringify(a)}\n ${B.engine}: ${JSON.stringify(b)}`);
|
||||
dataDiff++; continue;
|
||||
}
|
||||
const sortedA = JSON.stringify(canonSorted(a)), sortedB = JSON.stringify(canonSorted(b));
|
||||
if (sortedA !== sortedB) {
|
||||
dataDiff++;
|
||||
// find a compact hint of where they differ
|
||||
const sa = sortedA.length, sb = sortedB.length;
|
||||
let i = 0; while (i < Math.min(sa, sb) && sortedA[i] === sortedB[i]) i++;
|
||||
console.log(` ✗ ${n} DATA differs (lenA=${sa} lenB=${sb}) near:\n ${A.engine}: …${sortedA.slice(Math.max(0, i - 20), i + 80)}…\n ${B.engine}: …${sortedB.slice(Math.max(0, i - 20), i + 80)}…`);
|
||||
continue;
|
||||
}
|
||||
if (JSON.stringify(canonOrdered(a)) !== JSON.stringify(canonOrdered(b))) {
|
||||
orderDiff++;
|
||||
console.log(` ⚠ ${n} same data, DIFFERENT ORDER`);
|
||||
continue;
|
||||
}
|
||||
ok++;
|
||||
}
|
||||
console.log(`\n[parity] ${ok} identical | ${orderDiff} order-only | ${dataDiff} DATA-DIFF | ${errs} (both-errored)`);
|
||||
process.exit(dataDiff === 0 ? 0 : 1);
|
||||
}
|
||||
|
||||
const [mode, p1, p2] = process.argv.slice(2);
|
||||
if (mode === "dump" && p1) await dump(p1);
|
||||
else if (mode === "compare" && p1 && p2) await compare(p1, p2);
|
||||
else { console.error("usage: pg_parity.ts dump <out> | compare <a> <b>"); process.exit(2); }
|
||||
@@ -0,0 +1,381 @@
|
||||
/**
|
||||
* 轮询浪费度量 —— `wait-for` 上线前后的同口径回测工具。
|
||||
*
|
||||
* bun scripts/poll_waste_report.ts --since 2026-07-16 --until 2026-09-14 [--limit 50]
|
||||
* bun scripts/poll_waste_report.ts --since 2026-09-15 --json > after.json
|
||||
*
|
||||
* ## 为什么要有这个文件,而不是「到时候再查一遍」
|
||||
*
|
||||
* 2026-09-14 的 50 任务抽样得出四个数(见下方 BASELINE),`wait-for` 和放宽后的
|
||||
* Bash 超时就是冲着这四个数去的。如果上线之后的复查是临时手搓的,口径一定会飘:
|
||||
* 换一批样本、换一种归类、把评估轮算进去或不算进去,每一样都能凭空造出或抹掉
|
||||
* 一成的"改善"。那样的对比说明不了任何事情,还会被当成结论。
|
||||
*
|
||||
* 所以度量本身必须是**同一段代码**跑两次。基线数字钉死在这个文件里,跟它们一起
|
||||
* 钉死的是产生它们的那套过滤和分类规则。上线后重跑,差值才有意义。
|
||||
*
|
||||
* ## 口径(改动它就等于作废基线,要改请连 BASELINE 一起重算)
|
||||
*
|
||||
* - 样本:`finishedAt` 落在窗口内、已结束的 bug 与 feature,**剔除 `evaluate-` 前缀**
|
||||
* (那是 evaluator 基准 lane,跑在专用节点上,节奏不代表生产)。
|
||||
* - 只统计**执行轮**(session-r*.log),不含评估轮 —— 评估轮不碰设备也不构建,
|
||||
* 本来就没有轮询,混进来只会稀释分母。
|
||||
* - `--rounds-since` 只保留**在该时刻之后开跑**的轮次。样本按任务的 finishedAt 选,
|
||||
* 但一个今天结束的任务,它前几批可能跑在几天前 —— 上线当天做回测时,这批旧轮次
|
||||
* 会占到多数。实测 2026-09-15 首次回测:窗口内 54 个执行轮里 **35 轮跑在换装之前**,
|
||||
* 不筛掉的话对比的是新旧混合体,而混合比例每天都在变,数字看着动其实什么也没说明。
|
||||
* 轮次的开跑时刻按 `archivedAt - durationMs` 反推(与 runner 日志实测相差 9 秒)。
|
||||
* - 「重复调用」= 同一任务内 **字节完全相同** 的 Bash 命令的多余次数。
|
||||
* 不做归一化(不抹路径、不抹数字):归一化会把"每次改了轮次号的轮询"也算进来,
|
||||
* 那是另一回事,分开看更诚实。
|
||||
* - 「显式 sleep」只累加 **≤600s** 的字面量。更大的是守护/占位写法
|
||||
* (`nohup … sleep 100000`),算进来会凭空多出上百小时。
|
||||
*
|
||||
* ## 想做归因,必须提前留对照组(2026-09-17 的教训)
|
||||
*
|
||||
* 这个脚本只能回答「改动之后这些数字是多少」,回答不了「是不是这次改动造成的」。
|
||||
*
|
||||
* 上线三天后想查单轮时长为什么涨了 47%,我以为其它节点池还跑着旧 bundle、可以当
|
||||
* 对照 —— 查下来它们那几天已经升到同一版了(那组里有 229 次 wait-for 调用,就是
|
||||
* 证据)。**根本没有对照组**,于是「变慢」既不能归因、也不能排除,而节点间的中位数
|
||||
* 从 48min 到 135min,方差远大于要找的效应。
|
||||
*
|
||||
* 下面的 `--by-experiment` 就是为此做的。
|
||||
*
|
||||
* 做法**不是**把某台节点扣着不升级 —— 那台机器会同时错过其它所有改动,而且对照组
|
||||
* 撑得越久,它和生产的差别就越不只是被测的那一项。改用按节点关单项:在对照节点的
|
||||
* `.env` 里写 `STREAMLINE_EXPERIMENTS=waitfor=off`,它照常收所有新 bundle,只是这一项
|
||||
* 退回改动前的行为。节点把被关掉的项随心跳上报,`/api/nodes` 的 `experiments` 能读到,
|
||||
* 于是回测能按行为分组。
|
||||
*
|
||||
* 注意 `experiments` 只反映**当下**,不是历史 —— 开关一旦设好就别中途改,否则更早的
|
||||
* 轮次无从回溯,又会回到 2026-09-17 那个说不清归因的局面。
|
||||
*
|
||||
* ## 先问「这个差值分得开吗」,再问「变好了没有」—— `--noise`
|
||||
*
|
||||
* 2026-09-17 量到单轮时长涨了 47%,差点当成效应。补做 A/A 才发现噪声底有多高:
|
||||
*
|
||||
* - 同平台、跑同一版代码的不同节点,中位数跨度 iOS 31% / Android 45%;
|
||||
* - **同一台节点**、同一版代码,把轮次按时间对半切,自己跟自己比,差 −30% ~ +30%
|
||||
* (每半 ~90 轮)。也就是说「前后对比」这种做法本身就带着 ±30% 的时间漂移。
|
||||
*
|
||||
* 自举出的可判别下限(同一总体抽两组,两组中位数之差的 p90):
|
||||
*
|
||||
* 每组 20 轮 → 46% 每组 100 轮 → 21% 每组 400 轮 → 11%
|
||||
* 每组 50 轮 → 29% 每组 200 轮 → 15% 每组 800 轮 → 8%
|
||||
*
|
||||
* 换装后只有 66 轮、观测 +47% —— 刚出头,判不了。
|
||||
*
|
||||
* 结论不是「再等等」,而是**别拿单轮时长当这次改动的成败判据**:全量每天只产出
|
||||
* ~20 轮,要把下限压到 15% 得攒 200 轮/组。真正该看的是那些近乎确定性的计数 ——
|
||||
* 重复调用占比(14.3% → 0.6%,二十倍的变化,远在任何噪声之上)、wait-for 超时次数与
|
||||
* 烧掉的时长、无哨兵轮占比。它们是改动的直接后果,不是墙钟的随机游走。
|
||||
*
|
||||
* `--noise` 把上面那张表按当前窗口重算一遍,回答「我这次看到的差值分得开吗」。
|
||||
*
|
||||
* ## 只读
|
||||
*
|
||||
* 全程 GET 内网只读 API,不连 DB,不写任何东西。会话日志只流式读取、就地统计,
|
||||
* 不落盘 —— 单次全量约 600 MB。
|
||||
*/
|
||||
import { parseArgs } from "node:util";
|
||||
|
||||
const API = process.env.STREAMLINE_API ?? "https://streamline-internal-api.bytedance.net";
|
||||
|
||||
/**
|
||||
* 2026-09-14 抽样得到的基线(窗口 2026-07-16 ~ 2026-09-14,50 任务 / 86 个执行轮
|
||||
* 会话日志 / 12,487 次 Bash 调用)。上线后的复查跟它比。
|
||||
*/
|
||||
export const BASELINE = {
|
||||
window: "2026-07-16 ~ 2026-09-14",
|
||||
tasks: 50,
|
||||
execRoundsWithLog: 86,
|
||||
bashCalls: 12_487,
|
||||
dupCalls: 1_790, // 13.9% —— 首要目标
|
||||
dupRatio: 0.143,
|
||||
sleepSeconds: 82_440, // 22.9 h
|
||||
sleepPerRoundSec: 958,
|
||||
callsPerRound: 161,
|
||||
roundsNoSentinel: 52, // 占全部 140 个有 digest 的轮次的 37%
|
||||
roundsTotal: 140,
|
||||
} as const;
|
||||
|
||||
interface Row { id: string; kind: "bug" | "feature"; finishedAt: number; status: string }
|
||||
|
||||
/** nodeId → 该节点当前关掉的改动(来自 /api/nodes 的 experiments)。 */
|
||||
async function nodeExperiments(): Promise<Map<string, string>> {
|
||||
const out = new Map<string, string>();
|
||||
const j = await getJson<{ nodes: { id: string; experiments?: string[] }[] }>("/api/nodes");
|
||||
for (const n of j?.nodes ?? []) {
|
||||
out.set(n.id, n.experiments == null ? "(未上报)" : (n.experiments.length ? n.experiments.join(",") : "全开"));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
async function getJson<T>(path: string): Promise<T | null> {
|
||||
try {
|
||||
const r = await fetch(API + path, { signal: AbortSignal.timeout(120_000) });
|
||||
if (!r.ok) return null;
|
||||
return (await r.json()) as T;
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
async function listTargets(sinceMs: number, untilMs: number): Promise<Row[]> {
|
||||
const out: Row[] = [];
|
||||
const bugs = await getJson<{ tasks: any[] }>("/api/tasks");
|
||||
for (const t of bugs?.tasks ?? []) {
|
||||
if (String(t.id).startsWith("evaluate-")) continue;
|
||||
if (t.finishedAt == null || t.finishedAt < sinceMs || t.finishedAt > untilMs) continue;
|
||||
out.push({ id: t.id, kind: "bug", finishedAt: t.finishedAt, status: t.status });
|
||||
}
|
||||
const feats = await getJson<{ features: any[] }>("/api/features");
|
||||
for (const f of feats?.features ?? []) {
|
||||
if (f.finishedAt == null || f.finishedAt < sinceMs || f.finishedAt > untilMs) continue;
|
||||
out.push({ id: f.id, kind: "feature", finishedAt: f.finishedAt, status: f.status });
|
||||
}
|
||||
return out.sort((a, b) => b.finishedAt - a.finishedAt);
|
||||
}
|
||||
|
||||
interface TaskStat {
|
||||
id: string; kind: string; rounds: number;
|
||||
bash: number; dup: number; sleepSec: number;
|
||||
waitForCalls: number; // 上线后应当从 0 涨起来 —— 没涨就是提示词没生效,不是工具没用
|
||||
/** wait-for 等到上限就放弃的次数(退出码 124)。 */
|
||||
waitForTimeouts: number;
|
||||
/** 那些超时按各自 --timeout 累计烧掉的秒数 —— 纯等待,什么也没换来。 */
|
||||
waitForTimeoutSec: number;
|
||||
digestRounds: number; noSentinel: number;
|
||||
/** 因为开跑于 --rounds-since 之前而被跳过的执行轮。 */
|
||||
skippedOldRounds: number;
|
||||
/** 跑这些轮次的节点(取计入的第一轮的 nodeId)。用于按实验开关分组。 */
|
||||
node: string | null;
|
||||
}
|
||||
|
||||
const SLEEP_RE = /\bsleep\s+(\d+(?:\.\d+)?)/g;
|
||||
|
||||
/** `45s` / `10m` / `2h` / 裸数字(按秒) → 秒。认不出返回 0。 */
|
||||
function durSec(raw: string): number {
|
||||
const m = /^(\d+(?:\.\d+)?)(ms|s|m|h)?$/i.exec(raw.trim());
|
||||
if (!m) return 0;
|
||||
const n = Number(m[1]), u = (m[2] ?? "s").toLowerCase();
|
||||
return u === "ms" ? n / 1000 : u === "s" ? n : u === "m" ? n * 60 : n * 3600;
|
||||
}
|
||||
|
||||
async function measure(t: Row, roundsSinceMs: number | null): Promise<TaskStat | null> {
|
||||
const p = t.kind === "bug" ? "tasks" : "features";
|
||||
const st: TaskStat = { id: t.id, kind: t.kind, rounds: 0, bash: 0, dup: 0, sleepSec: 0, waitForCalls: 0, waitForTimeouts: 0, waitForTimeoutSec: 0, digestRounds: 0, noSentinel: 0, skippedOldRounds: 0, node: null };
|
||||
|
||||
// 轮次开跑时刻:archivedAt(控制面归档这批的时刻)减去这批的自报时长。
|
||||
const roundStart = new Map<number, number>();
|
||||
const dig = await getJson<{ rounds: any[] }>(`/api/${p}/${t.id}/round-digests`);
|
||||
for (const r of dig?.rounds ?? []) {
|
||||
const start = r.archivedAt && r.digest?.durationMs ? r.archivedAt - r.digest.durationMs : r.archivedAt;
|
||||
if (start) roundStart.set(r.round, start);
|
||||
if (roundsSinceMs && start && start < roundsSinceMs) continue; // 换装前跑的,不计
|
||||
st.node ??= r.nodeId ?? null;
|
||||
st.digestRounds++;
|
||||
if (!r.digest?.status) st.noSentinel++;
|
||||
}
|
||||
|
||||
const logs = await getJson<{ files: { name: string; kind: string }[] }>(`/api/${p}/${t.id}/logs`);
|
||||
const sessions = (logs?.files ?? []).filter((f) => f.kind === "session");
|
||||
for (const f of sessions) {
|
||||
if (roundsSinceMs) {
|
||||
const n = Number(f.name.match(/-r(\d+)\.log$/)?.[1] ?? 0);
|
||||
const start = roundStart.get(n);
|
||||
// 拿不到开跑时刻的轮次一律跳过 —— 宁可少算,也不要把旧轮次混进来冒充新数据。
|
||||
if (!start || start < roundsSinceMs) { st.skippedOldRounds++; continue; }
|
||||
}
|
||||
const res = await fetch(`${API}/api/${p}/${t.id}/logs/${f.name}?lines=400000`, { signal: AbortSignal.timeout(300_000) }).catch(() => null);
|
||||
if (!res?.ok || !res.body) continue;
|
||||
st.rounds++;
|
||||
const seen = new Set<string>();
|
||||
let lastWaitFor: string | null = null;
|
||||
// 一行一个 stream-json 事件;整份日志可能几 MB,按行流式处理,不整份读进内存。
|
||||
let buf = "";
|
||||
for await (const chunk of res.body as any) {
|
||||
buf += Buffer.from(chunk).toString("utf8");
|
||||
let nl: number;
|
||||
while ((nl = buf.indexOf("\n")) >= 0) {
|
||||
const line = buf.slice(0, nl); buf = buf.slice(nl + 1);
|
||||
if (!line.startsWith("{")) continue;
|
||||
let ev: any; try { ev = JSON.parse(line); } catch { continue; }
|
||||
if (ev.type === "user") {
|
||||
// wait-for 的超时只能从它自己的输出里认:「⌛ … 等到上限 …」。
|
||||
// 与紧邻的上一次 wait-for 调用配对,才拿得到它当时设的 --timeout。
|
||||
for (const c of ev.message?.content ?? []) {
|
||||
if (c.type !== "tool_result" || !lastWaitFor) continue;
|
||||
const out = typeof c.content === "string" ? c.content : JSON.stringify(c.content ?? "");
|
||||
if (/等到上限/.test(out)) {
|
||||
st.waitForTimeouts++;
|
||||
st.waitForTimeoutSec += durSec(lastWaitFor.match(/--timeout\s+(\S+)/)?.[1] ?? "");
|
||||
lastWaitFor = null;
|
||||
} else if (/命令已结束|条件达成/.test(out)) {
|
||||
lastWaitFor = null;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (ev.type !== "assistant") continue;
|
||||
for (const c of ev.message?.content ?? []) {
|
||||
if (c.type !== "tool_use" || c.name !== "Bash") continue;
|
||||
const cmd = String(c.input?.command ?? "").replace(/\s+/g, " ").trim();
|
||||
if (!cmd) continue;
|
||||
st.bash++;
|
||||
if (/\bwait-for\b/.test(cmd)) { st.waitForCalls++; lastWaitFor = cmd; }
|
||||
if (seen.has(cmd)) st.dup++; else seen.add(cmd);
|
||||
for (const m of cmd.matchAll(SLEEP_RE)) {
|
||||
const v = Number(m[1]);
|
||||
if (v <= 600) st.sleepSec += v; // >600s 是守护/占位写法,见文件头口径
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return st.bash || st.digestRounds || st.skippedOldRounds ? st : null;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const { values } = parseArgs({
|
||||
options: {
|
||||
since: { type: "string" }, until: { type: "string" },
|
||||
limit: { type: "string", default: "50" },
|
||||
"rounds-since": { type: "string" },
|
||||
"by-experiment": { type: "boolean", default: false },
|
||||
noise: { type: "boolean", default: false },
|
||||
json: { type: "boolean", default: false },
|
||||
concurrency: { type: "string", default: "6" },
|
||||
},
|
||||
});
|
||||
const sinceMs = values.since ? Date.parse(values.since + "T00:00:00Z") : Date.now() - 60 * 86400_000;
|
||||
const untilMs = values.until ? Date.parse(values.until + "T23:59:59Z") : Date.now();
|
||||
const limit = Number(values.limit) || 50;
|
||||
const conc = Math.max(1, Number(values.concurrency) || 6);
|
||||
// 上线回测必须传它:样本按 finishedAt 选,但轮次是按开跑时刻算新旧的。
|
||||
const roundsSinceMs = values["rounds-since"] ? Date.parse(values["rounds-since"] as string) : null;
|
||||
if (values["rounds-since"] && Number.isNaN(roundsSinceMs)) { console.error("--rounds-since 解析不了,写成 2026-09-14T17:30:00-07:00"); process.exit(2); }
|
||||
|
||||
if (values.noise) { await noiseFloor(sinceMs, untilMs); return; }
|
||||
|
||||
const all = await listTargets(sinceMs, untilMs);
|
||||
const picked = all.slice(0, limit);
|
||||
if (!picked.length) { console.error("窗口内没有已结束的任务。"); process.exit(1); }
|
||||
console.error(`[poll-waste] 窗口 ${values.since ?? "(60天前)"} ~ ${values.until ?? "(今天)"},命中 ${all.length},取最近 ${picked.length}`);
|
||||
|
||||
const stats: TaskStat[] = [];
|
||||
for (let i = 0; i < picked.length; i += conc) {
|
||||
const batch = await Promise.all(picked.slice(i, i + conc).map((t) => measure(t, roundsSinceMs)));
|
||||
for (const s of batch) if (s) stats.push(s);
|
||||
console.error(`[poll-waste] ${Math.min(i + conc, picked.length)}/${picked.length}`);
|
||||
}
|
||||
|
||||
const sum = (f: (s: TaskStat) => number) => stats.reduce((a, s) => a + f(s), 0);
|
||||
const rounds = sum((s) => s.rounds);
|
||||
const out = {
|
||||
window: `${new Date(sinceMs).toISOString().slice(0, 10)} ~ ${new Date(untilMs).toISOString().slice(0, 10)}`,
|
||||
tasks: stats.length,
|
||||
execRoundsWithLog: rounds,
|
||||
bashCalls: sum((s) => s.bash),
|
||||
dupCalls: sum((s) => s.dup),
|
||||
dupRatio: sum((s) => s.bash) ? sum((s) => s.dup) / sum((s) => s.bash) : 0,
|
||||
sleepSeconds: sum((s) => s.sleepSec),
|
||||
sleepPerRoundSec: rounds ? Math.round(sum((s) => s.sleepSec) / rounds) : 0,
|
||||
callsPerRound: rounds ? Math.round(sum((s) => s.bash) / rounds) : 0,
|
||||
waitForCalls: sum((s) => s.waitForCalls),
|
||||
waitForTimeouts: sum((s) => s.waitForTimeouts),
|
||||
waitForTimeoutHours: +(sum((s) => s.waitForTimeoutSec) / 3600).toFixed(1),
|
||||
skippedOldRounds: sum((s) => s.skippedOldRounds),
|
||||
roundsSince: values["rounds-since"] ?? null,
|
||||
waitForAdoptionRounds: stats.filter((s) => s.waitForCalls > 0).length,
|
||||
roundsTotal: sum((s) => s.digestRounds),
|
||||
roundsNoSentinel: sum((s) => s.noSentinel),
|
||||
};
|
||||
|
||||
if (values.json) { console.log(JSON.stringify({ baseline: BASELINE, current: out, perTask: stats }, null, 2)); return; }
|
||||
|
||||
const pctDelta = (now: number, base: number) => base ? `${((now - base) / base * 100).toFixed(1).padStart(6)}%` : " —";
|
||||
const line = (label: string, now: string | number, base: string | number, delta = "") =>
|
||||
console.log(` ${label.padEnd(26)} ${String(now).padStart(10)} 基线 ${String(base).padStart(10)} ${delta}`);
|
||||
console.log(`\n=== 轮询浪费回测 · ${out.window} ===\n`);
|
||||
line("任务数", out.tasks, BASELINE.tasks);
|
||||
line("执行轮(有会话日志)", out.execRoundsWithLog, BASELINE.execRoundsWithLog);
|
||||
line("Bash 调用", out.bashCalls, BASELINE.bashCalls);
|
||||
if (roundsSinceMs) console.log(` ${"(已跳过换装前开跑的轮次)".padEnd(26)} ${String(out.skippedOldRounds).padStart(10)}`);
|
||||
console.log("");
|
||||
line("重复调用占比 ★", `${(out.dupRatio * 100).toFixed(1)}%`, `${(BASELINE.dupRatio * 100).toFixed(1)}%`, pctDelta(out.dupRatio, BASELINE.dupRatio));
|
||||
line("每轮 sleep 秒数 ★", out.sleepPerRoundSec, BASELINE.sleepPerRoundSec, pctDelta(out.sleepPerRoundSec, BASELINE.sleepPerRoundSec));
|
||||
line("每轮 Bash 调用数 ★", out.callsPerRound, BASELINE.callsPerRound, pctDelta(out.callsPerRound, BASELINE.callsPerRound));
|
||||
line("无哨兵轮占比", `${out.roundsTotal ? (out.roundsNoSentinel / out.roundsTotal * 100).toFixed(0) : 0}%`,
|
||||
`${(BASELINE.roundsNoSentinel / BASELINE.roundsTotal * 100).toFixed(0)}%`);
|
||||
console.log("");
|
||||
line("wait-for 调用次数", out.waitForCalls, 0);
|
||||
line("★ 其中超时放弃(124)", out.waitForTimeouts, 0);
|
||||
line("★ 超时烧掉(小时)", out.waitForTimeoutHours, 0);
|
||||
line("用上 wait-for 的任务", out.waitForAdoptionRounds, 0);
|
||||
console.log(`\n★ = 这次改动直接冲着去的指标。wait-for 调用次数若仍是 0,先查节点跑的是不是这次部署的提示词(控制面与卫星版本),再谈效果。\n`);
|
||||
|
||||
if (values["by-experiment"]) {
|
||||
const exps = await nodeExperiments();
|
||||
const groups = new Map<string, TaskStat[]>();
|
||||
for (const st of stats) {
|
||||
const key = st.node ? (exps.get(st.node) ?? "(节点已不在册)") : "(不知道跑在哪台)";
|
||||
groups.set(key, [...(groups.get(key) ?? []), st]);
|
||||
}
|
||||
console.log("按节点的实验开关分组(对照组比较用):\n");
|
||||
console.log(" 开关状态 任务 轮次 重复调用 每轮 sleep(s) 每轮 Bash wait-for");
|
||||
for (const [key, g] of [...groups].sort((a, b) => b[1].length - a[1].length)) {
|
||||
const r = g.reduce((s, x) => s + x.rounds, 0);
|
||||
const bash = g.reduce((s, x) => s + x.bash, 0);
|
||||
const dup = g.reduce((s, x) => s + x.dup, 0);
|
||||
const slp = g.reduce((s, x) => s + x.sleepSec, 0);
|
||||
const wf = g.reduce((s, x) => s + x.waitForCalls, 0);
|
||||
console.log(` ${key.padEnd(16)} ${String(g.length).padStart(4)} ${String(r).padStart(5)}` +
|
||||
` ${(bash ? (dup / bash * 100).toFixed(1) : "—").padStart(6)}%` +
|
||||
` ${String(r ? Math.round(slp / r) : 0).padStart(11)}` +
|
||||
` ${String(r ? Math.round(bash / r) : 0).padStart(8)}` +
|
||||
` ${String(wf).padStart(7)}`);
|
||||
}
|
||||
console.log("\n 注:开关状态取自 /api/nodes 的**当下**值,不是历史 —— 对照组设好后别中途改。");
|
||||
console.log(" 「(未上报)」= 该节点还跑着不认识这个字段的旧 bundle。\n");
|
||||
}
|
||||
}
|
||||
|
||||
if (import.meta.main) await main();
|
||||
|
||||
/**
|
||||
* A/A 噪声底 —— 「我看到的这个差值,和纯随机分得开吗」。
|
||||
*
|
||||
* 做法:从**同一个窗口**的真实轮次里反复随机抽两组各 n 轮。两组来自同一总体,所以
|
||||
* 真实效应是 0;它们中位数之差的分布,就是这个样本量下「什么都没变」时的正常波动。
|
||||
* 观测到的差值没超过 p90,就不能当成效应。
|
||||
*
|
||||
* 用真实轮次自举而不是假设正态:轮时长是重尾的(一轮能从 2 分钟到 8 小时),
|
||||
* 套正态会把下限估得过于乐观。
|
||||
*/
|
||||
async function noiseFloor(sinceMs: number, untilMs: number): Promise<void> {
|
||||
const rows = await listTargets(sinceMs, untilMs);
|
||||
const durs: number[] = [];
|
||||
for (let i = 0; i < rows.length; i += 8) {
|
||||
const batch = await Promise.all(rows.slice(i, i + 8).map(async (t) => {
|
||||
const p = t.kind === "bug" ? "tasks" : "features";
|
||||
const d = await getJson<{ rounds: any[] }>(`/api/${p}/${t.id}/round-digests`);
|
||||
return (d?.rounds ?? []).map((r) => r.digest?.durationMs ?? 0).filter((x: number) => x > 0);
|
||||
}));
|
||||
for (const b of batch) durs.push(...b);
|
||||
}
|
||||
if (durs.length < 60) { console.error(`窗口内只有 ${durs.length} 轮,不足以估噪声底`); return; }
|
||||
const med = (a: number[]) => { const s = [...a].sort((x, y) => x - y); return s[Math.floor(s.length / 2)]; };
|
||||
const pick = (n: number) => { const o: number[] = []; for (let i = 0; i < n; i++) o.push(durs[(Math.random() * durs.length) | 0]); return o; };
|
||||
console.log(`\n=== A/A 噪声底 · 窗口内 ${durs.length} 轮,中位 ${(med(durs) / 60000).toFixed(0)}min ===\n`);
|
||||
console.log(" 每组轮次 两组中位数之差(真实效应=0) 可判别下限");
|
||||
console.log(" p50 p90 p95 (取 p90)");
|
||||
for (const n of [20, 50, 100, 200, 400, 800]) {
|
||||
const d: number[] = [];
|
||||
for (let k = 0; k < 4000; k++) { const a = med(pick(n)), b = med(pick(n)); d.push(Math.abs(b - a) / a); }
|
||||
d.sort((x, y) => x - y);
|
||||
const q = (p: number) => (d[Math.floor(d.length * p)] * 100).toFixed(0) + "%";
|
||||
console.log(` ${String(n).padStart(6)} ${q(0.5).padStart(5)} ${q(0.9).padStart(6)} ${q(0.95).padStart(6)} > ${q(0.9)}`);
|
||||
}
|
||||
console.log("\n 读法:同一总体抽两组,有一成概率飘出 p90 那一列。观测差值不超过它就判不了。");
|
||||
console.log(" 提醒:单轮时长是重尾的。近乎确定性的计数(重复调用占比、超时次数)信噪比高得多。\n");
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Render the Lark status-change cards WITHOUT sending them, so a design change
|
||||
* can be eyeballed (or piped into `lark-cli im +messages-send`) without pinging
|
||||
* every watcher on a real task.
|
||||
*
|
||||
* Run from repo root:
|
||||
* bun scripts/preview-notify-cards.ts # every card, as JSON lines
|
||||
* bun scripts/preview-notify-cards.ts bug:manual_review # just one
|
||||
*
|
||||
* Output is one `{name, card}` JSON object per line, so you can pick one with
|
||||
* jq and hand it straight to lark-cli:
|
||||
* bun scripts/preview-notify-cards.ts bug:manual_review | jq -c .card
|
||||
*/
|
||||
/**
|
||||
* Previews must render the PRODUCTION link, whatever the local `.env` says.
|
||||
*
|
||||
* A developer's `.env` points APP_BASE_URL at their own host, so a preview built
|
||||
* with it carries a domain no recipient can open — and the reviewer of the
|
||||
* preview has no way to tell that apart from a genuine bug in the card. Set
|
||||
* before any card module loads. Override with PREVIEW_BASE_URL when you really
|
||||
* do want to preview against a local build.
|
||||
*/
|
||||
const PREVIEW_BASE_URL = process.env.PREVIEW_BASE_URL || "https://streamline.bytedance.net";
|
||||
process.env.APP_BASE_URL = PREVIEW_BASE_URL;
|
||||
|
||||
// Dynamic import: the card modules capture APP_BASE_URL through appBaseUrl() at
|
||||
// BUILD time, so the override above has to be in place before they load.
|
||||
const { CARDS } = await import("./_notify-preview-cards");
|
||||
|
||||
const want = new Set(process.argv.slice(2));
|
||||
for (const [name, build] of Object.entries(CARDS)) {
|
||||
if (want.size && !want.has(name)) continue;
|
||||
console.log(JSON.stringify({ name, card: build() }));
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Dev probe: walk the Bits MR → Codebase comment chain for one MR and print
|
||||
* every hop, so field-name drift on the Codebase side is diagnosable without
|
||||
* redeploying the control plane.
|
||||
*
|
||||
* bun scripts/probe_mr_comments.ts <bitsMrId> [--raw]
|
||||
*
|
||||
* `--raw` dumps the ListThreads / MGetThreads payloads (truncated) — that's the
|
||||
* one thing the normalizer in codebase.ts can't tell you when it comes back
|
||||
* empty. Reads CODEBASE_PAT + BITS_TOKEN from .env.
|
||||
*/
|
||||
import { getMrCodebaseRef } from "../control-server/src/bits";
|
||||
import { getMergeRequestId, listMrComments, renderMrCommentsBlock, codebaseConfigured } from "../control-server/src/codebase";
|
||||
|
||||
const mrId = Bun.argv[2];
|
||||
const raw = Bun.argv.includes("--raw");
|
||||
if (!mrId) {
|
||||
console.error("usage: bun scripts/probe_mr_comments.ts <bitsMrId> [--raw]");
|
||||
process.exit(2);
|
||||
}
|
||||
if (!codebaseConfigured()) {
|
||||
console.error("CODEBASE_PAT is not set (see .env.example)");
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const bridge = await getMrCodebaseRef(mrId);
|
||||
console.log("① bits mr info →", JSON.stringify(bridge));
|
||||
if (!bridge.ok || !bridge.ref) process.exit(1);
|
||||
|
||||
const resolved = await getMergeRequestId(bridge.ref.repoId, bridge.ref.changeId);
|
||||
console.log("② codebase GetMergeRequest →", JSON.stringify(resolved));
|
||||
|
||||
if (raw) {
|
||||
const res = await fetch(
|
||||
`${process.env.CODEBASE_API_BASE || "https://codebase-api.byted.org"}/v2/?Action=ListThreads`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", Authorization: `Bearer ${process.env.CODEBASE_PAT}` },
|
||||
body: JSON.stringify({ CommentableType: "merge_request", CommentableId: resolved.mrId, RepoId: bridge.ref.repoId }),
|
||||
},
|
||||
);
|
||||
console.log("③ raw ListThreads →", (await res.text()).slice(0, 4000));
|
||||
}
|
||||
|
||||
const comments = await listMrComments({
|
||||
repoId: bridge.ref.repoId,
|
||||
mrId: resolved.mrId,
|
||||
headCommitId: bridge.ref.headCommitId,
|
||||
});
|
||||
console.log("④ normalized →", JSON.stringify({
|
||||
ok: comments.ok,
|
||||
error: comments.error,
|
||||
totalLive: comments.totalLive,
|
||||
dropped: comments.dropped,
|
||||
kept: comments.comments.length,
|
||||
}, null, 1));
|
||||
console.log("⑤ rendered block:\n");
|
||||
console.log(renderMrCommentsBlock(comments, null) || "(empty)");
|
||||
@@ -0,0 +1,127 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* One-off: drop selected notes out of a task/feature/security `reviewHistory`.
|
||||
*
|
||||
* Why this exists: a human note is the ONLY thing that increments 复核轮 R# /
|
||||
* humanRejects (see shared/src/types.ts — roundBreakdown). Notes that merely
|
||||
* asked for an E2E re-run because the device farm was flaky therefore inflate
|
||||
* the re-fix rate without any fix having been rejected on its merits. Removing
|
||||
* those notes is how the count gets corrected — there is no separate counter to
|
||||
* patch.
|
||||
*
|
||||
* Scope discipline: this script does NOT search for anything. It takes an
|
||||
* explicit selection file so the set being destroyed is reviewed by a human
|
||||
* first, and it matches on the note's `at` timestamp (stable, unlike array
|
||||
* index, and unique per note in practice).
|
||||
*
|
||||
* selection.json = [{ "kind": "feature", "id": "<uuid>", "at": 1770000000000 }, ...]
|
||||
*
|
||||
* Usage (run on the control plane host, where DATABASE_URL is set):
|
||||
* bun scripts/purge_review_notes.ts --file selection.json # dry run
|
||||
* bun scripts/purge_review_notes.ts --file selection.json --apply # write
|
||||
*
|
||||
* Every run writes a backup of the affected rows' PRE-state next to the
|
||||
* selection file (`<file>.backup-<epoch>.json`) — including on dry runs, so the
|
||||
* restore payload exists before anything is touched. Restore = feed those rows'
|
||||
* `reviewHistory` back through the same update helpers.
|
||||
*/
|
||||
import { getTask, updateTask, getFeature, updateFeature, getSecurity, updateSecurity } from "../control-server/src/db";
|
||||
// Relative, not "@bbbug/shared/types": that alias only resolves from inside a
|
||||
// workspace package (control-server/node_modules), not from scripts/.
|
||||
import { parseReviewHistory, type ReviewNote } from "../shared/src/types";
|
||||
|
||||
type Kind = "bug" | "feature" | "security";
|
||||
interface Selection { kind: Kind; id: string; at: number }
|
||||
|
||||
function usage(code: number): never {
|
||||
console.log(`
|
||||
purge_review_notes — remove specific review notes from the feedback lineage
|
||||
|
||||
--file <path> JSON array of { kind: "bug"|"feature"|"security", id, at }
|
||||
--apply actually write (default is a dry run)
|
||||
-h, --help
|
||||
`);
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
const argv = process.argv.slice(2);
|
||||
let file: string | null = null;
|
||||
let apply = false;
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--apply") apply = true;
|
||||
else if (a === "--file") file = argv[++i] || null;
|
||||
else if (a.startsWith("--file=")) file = a.slice("--file=".length);
|
||||
else if (a === "-h" || a === "--help") usage(0);
|
||||
else { console.error(`unknown arg: ${a}`); usage(1); }
|
||||
}
|
||||
if (!file) usage(1);
|
||||
|
||||
const selections: Selection[] = JSON.parse(await Bun.file(file).text());
|
||||
if (!Array.isArray(selections) || selections.length === 0) {
|
||||
console.error("selection file is empty — nothing to do");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
/** Group by row: one UPDATE per item even when several of its notes go. */
|
||||
const byRow = new Map<string, { kind: Kind; id: string; ats: Set<number> }>();
|
||||
for (const s of selections) {
|
||||
const key = `${s.kind}:${s.id}`;
|
||||
const row = byRow.get(key) ?? { kind: s.kind, id: s.id, ats: new Set<number>() };
|
||||
row.ats.add(s.at);
|
||||
byRow.set(key, row);
|
||||
}
|
||||
|
||||
const readers = {
|
||||
bug: getTask, feature: getFeature, security: getSecurity,
|
||||
} as const;
|
||||
const writers = {
|
||||
bug: updateTask, feature: updateFeature, security: updateSecurity,
|
||||
} as const;
|
||||
|
||||
const backup: unknown[] = [];
|
||||
let removed = 0, skipped = 0, wrote = 0;
|
||||
|
||||
for (const row of byRow.values()) {
|
||||
const item = await readers[row.kind](row.id) as { reviewHistory?: unknown; title?: string } | null;
|
||||
if (!item) { console.warn(` ! ${row.kind}/${row.id} — not found, skipped`); skipped++; continue; }
|
||||
|
||||
const before: ReviewNote[] = parseReviewHistory(item.reviewHistory);
|
||||
const after = before.filter((n) => !row.ats.has(n.at));
|
||||
const gone = before.length - after.length;
|
||||
if (gone !== row.ats.size) {
|
||||
// A timestamp that matched nothing means the selection is stale (someone
|
||||
// edited the note since the scan). Refuse the whole row rather than
|
||||
// half-applying it.
|
||||
console.warn(` ! ${row.kind}/${row.id} — expected ${row.ats.size} notes, matched ${gone}; skipped`);
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
|
||||
backup.push({ kind: row.kind, id: row.id, title: item.title, reviewHistory: before });
|
||||
|
||||
// `reviewFeedback` is the mirror of the LATEST note (see Task.reviewFeedback);
|
||||
// it has to follow, or the editable field would still show a purged note.
|
||||
const humanBefore = before.filter((n) => n.source === "human").length;
|
||||
const humanAfter = after.filter((n) => n.source === "human").length;
|
||||
// Pass the ARRAY, not JSON: db.toSetObject already stringifies this column,
|
||||
// so a pre-encoded string would land double-escaped.
|
||||
const patch = {
|
||||
reviewHistory: after.length ? after : null,
|
||||
reviewFeedback: after.length ? after[after.length - 1].text : null,
|
||||
};
|
||||
|
||||
console.log(` ${apply ? "→" : "·"} ${row.kind}/${row.id.slice(0, 8)} 「${(item.title ?? "").slice(0, 28)}」`
|
||||
+ ` notes ${before.length}→${after.length}, 复核轮 R${humanBefore + 1}→R${humanAfter + 1}`);
|
||||
removed += gone;
|
||||
if (apply) { await writers[row.kind](row.id, patch as never); wrote++; }
|
||||
}
|
||||
|
||||
const backupPath = `${file}.backup-${backup.length ? (backup[0] as { id: string }).id.slice(0, 4) : "empty"}.json`;
|
||||
await Bun.write(backupPath, JSON.stringify(backup, null, 1));
|
||||
|
||||
console.log(`\n${apply ? "APPLIED" : "DRY RUN"} — ${removed} note(s) across ${byRow.size - skipped} item(s)`
|
||||
+ `${skipped ? `, ${skipped} skipped` : ""}${apply ? `, ${wrote} row(s) written` : ""}`);
|
||||
console.log(`pre-state backup: ${backupPath}`);
|
||||
if (!apply) console.log("re-run with --apply to write.");
|
||||
process.exit(0);
|
||||
@@ -0,0 +1,306 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* 卫星热部署 CLI — the non-interactive twin of 管理 → 卫星热部署.
|
||||
*
|
||||
* Exists so a deploy can finish the job: `deploy.sh` builds the bundle, then
|
||||
* calls this to actually get it onto the nodes, instead of ending with a note
|
||||
* telling a human to go click three buttons (which is how nodes ended up
|
||||
* running week-old code while the panel cheerfully said 「最新」).
|
||||
*
|
||||
* Why it is safe to automate, when the panel deliberately kept it manual:
|
||||
* the panel's danger is 强制部署 — swapping the bundle exits the satellite
|
||||
* process and kills whatever it was running. This script NEVER does that by
|
||||
* default. A busy node gets 「空档部署」: the node persists the bundle, stops
|
||||
* claiming new work, finishes its current task, and swaps in the gap. Nothing
|
||||
* is interrupted, so there is no judgement call left for a human to make.
|
||||
* `--force` restores the destructive behaviour and is opt-in only.
|
||||
*
|
||||
* Why it talks HTTP instead of importing the control plane's own route:
|
||||
* - `POST /api/deploy` is SSO+admin gated (browser credentials) — a script
|
||||
* has none, and weakening that gate to let scripts in would be a far worse
|
||||
* trade than this file.
|
||||
* - `pendingDeploy` / `supportsQueuedDeploy` live in the control-server's
|
||||
* MEMORY (learned from heartbeats, see cluster.ts), not the DB, so a
|
||||
* separate process must ask the running server for them — `GET /api/nodes`
|
||||
* is read-only and unauthenticated.
|
||||
* So: read node state from the control plane, push bytes straight to each node
|
||||
* over the same cluster-secret channel the control plane itself uses.
|
||||
*
|
||||
* Run it ON the control-plane box (it needs CLUSTER_SECRET from .env, the built
|
||||
* bundle, and network to the nodes):
|
||||
*
|
||||
* bun scripts/push-satellites.ts # push to the `default` pool
|
||||
* bun scripts/push-satellites.ts --dry-run # print the plan, touch nothing
|
||||
* bun scripts/push-satellites.ts --all-clusters # every pool, incl. other teams'
|
||||
* bun scripts/push-satellites.ts --cluster pns
|
||||
* bun scripts/push-satellites.ts --node fei-ios-01.shiro0.icu
|
||||
* bun scripts/push-satellites.ts --force # swap NOW, aborting running tasks
|
||||
*
|
||||
* Scope defaults to the `default` cluster on purpose. The fleet also carries
|
||||
* other teams' pools (`im-sg`, `pns`) whose nodes sit many commits back on their
|
||||
* own cadence — sweeping them along with a routine deploy would be a far bigger
|
||||
* change than the one being shipped, and nobody asked for it. `--all-clusters`
|
||||
* is there when you do mean the whole fleet.
|
||||
*
|
||||
* Exit codes: 0 = every intended node is on (or armed for) the bundle;
|
||||
* 1 = at least one push failed; 2 = bad usage / no bundle.
|
||||
*/
|
||||
import { existsSync, readFileSync } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import path from "node:path";
|
||||
// Relative, not "@bbbug/shared/protocol": that alias only resolves from inside a
|
||||
// workspace package (control-server/node_modules), not from scripts/. These are
|
||||
// runtime VALUES, so unlike a type-only import it really has to resolve.
|
||||
import {
|
||||
SAT_API, BUNDLE_SHA256_HEADER, BUNDLE_VERSION_HEADER, BUNDLE_MODE_HEADER, CLUSTER_SECRET_HEADER,
|
||||
} from "../shared/src/protocol";
|
||||
import { DEFAULT_CLUSTER } from "../shared/src/types";
|
||||
|
||||
const ROOT = path.resolve(import.meta.dir, "..");
|
||||
const BUNDLE_PATH = path.join(ROOT, "satellite/dist/satellite.js");
|
||||
const VERSION_PATH = path.join(ROOT, "satellite/dist/VERSION");
|
||||
const CONTROL_URL = (process.env.PUSH_SAT_CONTROL_URL || "http://127.0.0.1:5173").replace(/\/+$/, "");
|
||||
const CLUSTER_SECRET = process.env.CLUSTER_SECRET || "";
|
||||
|
||||
interface Node {
|
||||
id: string;
|
||||
platform: string;
|
||||
baseUrl: string;
|
||||
version?: string | null;
|
||||
live?: boolean;
|
||||
lastSeenAt: number;
|
||||
evalInFlight: number;
|
||||
fixInFlight: number;
|
||||
pendingDeploy?: string | null;
|
||||
supportsQueuedDeploy?: boolean;
|
||||
clusters?: string[] | null;
|
||||
}
|
||||
|
||||
interface Opts {
|
||||
force: boolean; dryRun: boolean; only: string[]; clusters: string[]; allClusters: boolean;
|
||||
waitSec: number;
|
||||
/** Versions whose bundle is byte-identical to this one — see --equivalent-version. */
|
||||
equivalent: string[];
|
||||
}
|
||||
|
||||
function usage(code: number): never {
|
||||
console.log(`用法: bun scripts/push-satellites.ts [--force] [--dry-run] [--node <id>]... [--cluster <tag>]... [--all-clusters] [--wait-live <秒>]
|
||||
|
||||
默认: 只推 #${DEFAULT_CLUSTER} 池里落后的在线节点;忙节点走「空档部署」(不打断任务),
|
||||
空闲节点立即换装。别的池(im-sg / pns 等)是别人按自己节奏在跑的,不顺手带上。
|
||||
--force 立即换装,会中断正在跑的评估/修复。
|
||||
--dry-run 只打印计划。
|
||||
--node <id> 限定到具体节点(可重复),会绕过集群过滤。
|
||||
--cluster <tag> 换一个集群(可重复)。
|
||||
--all-clusters 整个 fleet,包括别的团队的池子。
|
||||
--wait-live N 推之前等各节点心跳刷新的秒数(默认 45)。控制面刚重启时它的
|
||||
节点能力位(supportsQueuedDeploy)要等下一次心跳才重新学到,
|
||||
不等就会把忙节点误判成「不支持空档部署」。
|
||||
--equivalent-version <sha>
|
||||
跑着这些版本的节点视为「已是最新」(可重复)。bundle 的 VERSION
|
||||
是 git SHA,所以只改了控制面/脚本的部署会产出一个逐字节相同、
|
||||
只有版本号不同的 bundle;不加这个,每次部署都会让节点白白排空
|
||||
队列重启一轮。deploy.sh 在新旧 bundle 的 sha256 相同时自动传。`);
|
||||
process.exit(code);
|
||||
}
|
||||
|
||||
function parseArgs(argv: string[]): Opts {
|
||||
const o: Opts = { force: false, dryRun: false, only: [], clusters: [], allClusters: false, waitSec: 45, equivalent: [] };
|
||||
for (let i = 0; i < argv.length; i++) {
|
||||
const a = argv[i];
|
||||
if (a === "--force") o.force = true;
|
||||
else if (a === "--dry-run") o.dryRun = true;
|
||||
else if (a === "--all-clusters") o.allClusters = true;
|
||||
else if (a === "--node") { const v = argv[++i]; if (!v) usage(2); o.only.push(v); }
|
||||
else if (a.startsWith("--node=")) o.only.push(a.slice("--node=".length));
|
||||
else if (a === "--cluster") { const v = argv[++i]; if (!v) usage(2); o.clusters.push(v); }
|
||||
else if (a.startsWith("--cluster=")) o.clusters.push(a.slice("--cluster=".length));
|
||||
else if (a === "--wait-live") o.waitSec = Number(argv[++i] ?? "45");
|
||||
else if (a.startsWith("--wait-live=")) o.waitSec = Number(a.slice("--wait-live=".length));
|
||||
else if (a === "--equivalent-version") { const v = argv[++i]; if (!v) usage(2); o.equivalent.push(v); }
|
||||
else if (a.startsWith("--equivalent-version=")) o.equivalent.push(a.slice("--equivalent-version=".length));
|
||||
else if (a === "-h" || a === "--help") usage(0);
|
||||
else { console.error(`未知参数: ${a}`); usage(2); }
|
||||
}
|
||||
if (!Number.isFinite(o.waitSec) || o.waitSec < 0) usage(2);
|
||||
if (o.clusters.length === 0) o.clusters.push(DEFAULT_CLUSTER);
|
||||
return o;
|
||||
}
|
||||
|
||||
/** A node with no tags implicitly belongs to `default` — same rule the claim SQL applies. */
|
||||
function clustersOf(n: Node): string[] {
|
||||
const own = (n.clusters ?? []).filter(Boolean);
|
||||
return own.length ? own : [DEFAULT_CLUSTER];
|
||||
}
|
||||
|
||||
/** Is this node in scope? An explicit --node list wins over the cluster filter. */
|
||||
function inScope(n: Node, o: Opts): boolean {
|
||||
if (o.only.length) return o.only.includes(n.id);
|
||||
if (o.allClusters) return true;
|
||||
return clustersOf(n).some((c) => o.clusters.includes(c));
|
||||
}
|
||||
|
||||
async function fetchNodes(): Promise<Node[]> {
|
||||
const res = await fetch(`${CONTROL_URL}/api/nodes`, { signal: AbortSignal.timeout(15_000) });
|
||||
if (!res.ok) throw new Error(`GET ${CONTROL_URL}/api/nodes → HTTP ${res.status}`);
|
||||
const j = (await res.json()) as { nodes?: Node[] };
|
||||
return j.nodes ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait until every live node has heartbeated since `since`.
|
||||
*
|
||||
* Not politeness — correctness. `supportsQueuedDeploy` and `pendingDeploy` are
|
||||
* rebuilt from heartbeats in the control-server's memory, so in the seconds
|
||||
* after a control-plane restart EVERY node looks like it can't do a queued
|
||||
* deploy. Pushing then would refuse every busy node (or, with --force, kill its
|
||||
* task). One heartbeat cycle (~10s) restores the truth.
|
||||
*/
|
||||
async function waitForFreshHeartbeats(since: number, opts: Opts): Promise<Node[]> {
|
||||
const deadline = Date.now() + opts.waitSec * 1000;
|
||||
let nodes = await fetchNodes();
|
||||
if (opts.waitSec === 0) return nodes;
|
||||
for (;;) {
|
||||
const candidates = nodes.filter((n) => n.live && inScope(n, opts));
|
||||
const stale = candidates.filter((n) => n.lastSeenAt < since);
|
||||
if (candidates.length > 0 && stale.length === 0) return nodes;
|
||||
if (Date.now() >= deadline) {
|
||||
if (stale.length > 0) {
|
||||
console.log(` ! 等心跳超时:${stale.map((n) => n.id).join(", ")} 仍是重启前的心跳,忙节点可能被判为不支持空档部署`);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 3000));
|
||||
nodes = await fetchNodes();
|
||||
}
|
||||
}
|
||||
|
||||
interface Outcome { node: string; state: "queued" | "swapped" | "current" | "armed" | "offline" | "failed"; note: string }
|
||||
|
||||
async function main(): Promise<void> {
|
||||
const opts = parseArgs(process.argv.slice(2));
|
||||
|
||||
if (!existsSync(BUNDLE_PATH)) {
|
||||
console.error(`没有 bundle:${BUNDLE_PATH}(先跑 bun run build:satellite)`);
|
||||
process.exit(2);
|
||||
}
|
||||
const bytes = readFileSync(BUNDLE_PATH);
|
||||
const sha256 = createHash("sha256").update(bytes).digest("hex");
|
||||
const version = existsSync(VERSION_PATH) ? readFileSync(VERSION_PATH, "utf8").trim() : "unknown";
|
||||
// Same guard the deploy script applies: a truncated bundle passes the node's
|
||||
// sha256 check (that only proves the bytes survived the wire) and then fails
|
||||
// to boot, taking the node down until someone notices.
|
||||
if (bytes.length < 100_000) {
|
||||
console.error(`bundle 只有 ${bytes.length} 字节,像是被截断了,拒绝推送`);
|
||||
process.exit(2);
|
||||
}
|
||||
if (!CLUSTER_SECRET) console.log("! CLUSTER_SECRET 为空 — 只有在节点也没配 secret 时才推得动");
|
||||
|
||||
console.log(`bundle ${version} · ${(bytes.length / 1024 / 1024).toFixed(2)} MB · sha256 ${sha256.slice(0, 12)}`);
|
||||
|
||||
const started = Date.now();
|
||||
const nodes = await waitForFreshHeartbeats(started, opts);
|
||||
const targets = nodes.filter((n) => inScope(n, opts));
|
||||
for (const id of opts.only) {
|
||||
if (!nodes.some((n) => n.id === id)) console.log(` ! 节点 ${id} 不在集群里`);
|
||||
}
|
||||
// Say what is being left alone, so a partial rollout never reads as a full one.
|
||||
const skipped = nodes.filter((n) => !inScope(n, opts));
|
||||
const scope = opts.only.length ? `节点 ${opts.only.join(", ")}`
|
||||
: opts.allClusters ? "全部集群" : opts.clusters.map((c) => `#${c}`).join(" ");
|
||||
console.log(`范围: ${scope} — ${targets.length} 台在列`
|
||||
+ (skipped.length ? `,${skipped.length} 台不在范围内(${skipped.map((n) => n.id).join(", ")})` : ""));
|
||||
if (targets.length === 0) { console.log("范围内没有节点"); return; }
|
||||
|
||||
const out: Outcome[] = [];
|
||||
for (const n of targets) {
|
||||
const tag = `${n.id} (${n.platform})`;
|
||||
if (!n.live) { out.push({ node: n.id, state: "offline", note: "离线,本次跳过" }); console.log(` ⤬ ${tag} 离线`); continue; }
|
||||
if (n.version === version) { out.push({ node: n.id, state: "current", note: "已是最新" }); console.log(` = ${tag} 已在 ${version}`); continue; }
|
||||
// Same bytes, different stamp: pushing would drain and restart the node to
|
||||
// change a version string. See --equivalent-version.
|
||||
if (n.version && opts.equivalent.includes(n.version)) {
|
||||
out.push({ node: n.id, state: "current", note: "字节相同" });
|
||||
console.log(` = ${tag} 跑的是 ${n.version},与本 bundle 逐字节相同 — 不推`);
|
||||
continue;
|
||||
}
|
||||
// Already armed with these bytes (under this stamp or a byte-identical one):
|
||||
// re-arming would only restamp a rollout that is already on its way.
|
||||
if (n.pendingDeploy && (n.pendingDeploy === version || opts.equivalent.includes(n.pendingDeploy))) {
|
||||
out.push({ node: n.id, state: "armed", note: "已排队" });
|
||||
console.log(` = ${tag} 已排队 ${n.pendingDeploy},等它自己换装`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const busy = n.evalInFlight > 0 || n.fixInFlight > 0;
|
||||
// queued wins over force, exactly as the route decides it: asking to wait
|
||||
// and to interrupt at once is contradictory, and waiting is the safe read.
|
||||
const wantQueued = busy && !opts.force;
|
||||
if (wantQueued && !n.supportsQueuedDeploy) {
|
||||
const note = "节点忙,且其卫星版本不支持空档部署 — 等它空闲后重跑,或用 --force(会中断任务)";
|
||||
out.push({ node: n.id, state: "failed", note });
|
||||
console.log(` ✗ ${tag} ${note}`);
|
||||
continue;
|
||||
}
|
||||
const mode = wantQueued ? "queued" : "now";
|
||||
const what = `${n.version ?? "unknown"} → ${version}${busy ? (wantQueued ? " [空档部署]" : " [强制,中断任务]") : ""}`;
|
||||
if (opts.dryRun) { console.log(` · ${tag} 将推送 ${what}`); continue; }
|
||||
|
||||
try {
|
||||
const res = await fetch(n.baseUrl.replace(/\/+$/, "") + SAT_API.deploy, {
|
||||
method: "POST",
|
||||
body: bytes,
|
||||
headers: {
|
||||
"content-type": "application/octet-stream",
|
||||
[BUNDLE_SHA256_HEADER]: sha256,
|
||||
[BUNDLE_VERSION_HEADER]: version,
|
||||
// The node re-decides now-vs-arm on arrival: it knows whether it is
|
||||
// busy at the instant the bytes land, which beats our heartbeat view.
|
||||
[BUNDLE_MODE_HEADER]: mode,
|
||||
"x-deploy-by": process.env.PUSH_SAT_BY || "deploy.sh",
|
||||
...(CLUSTER_SECRET ? { [CLUSTER_SECRET_HEADER]: CLUSTER_SECRET } : {}),
|
||||
},
|
||||
signal: AbortSignal.timeout(120_000),
|
||||
});
|
||||
const j = (await res.json().catch(() => ({}))) as
|
||||
{ ok?: boolean; error?: string; skipped?: boolean; queued?: boolean; busy?: string };
|
||||
if (!res.ok || !j.ok) {
|
||||
const note = j.error || `HTTP ${res.status}`;
|
||||
out.push({ node: n.id, state: "failed", note });
|
||||
console.log(` ✗ ${tag} ${note}`);
|
||||
} else if (j.queued) {
|
||||
out.push({ node: n.id, state: "queued", note: j.busy || "忙" });
|
||||
console.log(` ⏸ ${tag} 已排队(${j.busy || "忙"}) — 当前任务结束后自动换装重启`);
|
||||
} else if (j.skipped) {
|
||||
out.push({ node: n.id, state: "current", note: "已是最新" });
|
||||
console.log(` = ${tag} 已是最新`);
|
||||
} else {
|
||||
out.push({ node: n.id, state: "swapped", note: what });
|
||||
console.log(` ✓ ${tag} 已换装 ${what},重启中`);
|
||||
}
|
||||
} catch (e) {
|
||||
const note = (e as Error).message;
|
||||
out.push({ node: n.id, state: "failed", note });
|
||||
console.log(` ✗ ${tag} ${note}`);
|
||||
}
|
||||
}
|
||||
|
||||
if (opts.dryRun) { console.log("(--dry-run,什么都没改)"); return; }
|
||||
|
||||
const n = (s: Outcome["state"]) => out.filter((o) => o.state === s).length;
|
||||
const parts = [
|
||||
n("swapped") ? `${n("swapped")} 台已换装重启` : "",
|
||||
n("queued") ? `${n("queued")} 台已排队(空档换装)` : "",
|
||||
n("current") + n("armed") ? `${n("current") + n("armed")} 台无需动作` : "",
|
||||
n("offline") ? `${n("offline")} 台离线` : "",
|
||||
n("failed") ? `${n("failed")} 台失败` : "",
|
||||
].filter(Boolean);
|
||||
console.log(`卫星: ${parts.join(" · ") || "无节点"}`);
|
||||
// An armed node is NOT yet running the new code — say so, or a green deploy
|
||||
// reads as "rolled out" while a busy node still runs the old bundle for hours.
|
||||
if (n("queued")) console.log(" 排队的节点仍在跑旧代码,直到它当前的任务结束;面板上显示「已排队」。");
|
||||
if (n("offline")) console.log(" 离线节点回来后不会自动补装 — 重跑本脚本或在面板上推。");
|
||||
if (n("failed")) process.exit(1);
|
||||
}
|
||||
|
||||
main().catch((e) => { console.error(`卫星推送失败: ${(e as Error).message}`); process.exit(1); });
|
||||
@@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Reap orphaned readable-report claude processes on a satellite node.
|
||||
#
|
||||
# ## Why this exists
|
||||
#
|
||||
# `bin/claude-pty.ts::killTree()` used to send SIGTERM and call process.exit() on
|
||||
# the next line, never checking whether the tree died. A claude under `script`
|
||||
# that was slow to handle the signal simply outlived its wrapper. On a node the
|
||||
# control plane was retrying every few minutes (see the storm documented on
|
||||
# readable_retry.ts::MAX_CONSECUTIVE_FAILURES) the leftovers accumulated until the
|
||||
# machine could no longer start claude inside the wrapper's 120s transcript wait —
|
||||
# at which point EVERY generation on it failed, producing more retries. Measured
|
||||
# on temp-ios-01, 2026-08-22: 100+ claude processes.
|
||||
#
|
||||
# The wrapper is fixed, but a node that already has a pile needs the pile removed,
|
||||
# and a node running an OLD bundle still produces them.
|
||||
#
|
||||
# ## Why it is safe to run on a live node
|
||||
#
|
||||
# Two independent guards, both necessary:
|
||||
#
|
||||
# 1. It matches only the first line of the two report prompts, which is in
|
||||
# claude's own argv. A fix run, an evaluator run and the asking session carry
|
||||
# completely different prompts and can never match. This is the difference
|
||||
# between this script and `pkill -f claude`, which on a node with
|
||||
# fixInFlight=1 kills a production task mid-implementation.
|
||||
# 2. It ignores anything younger than MIN_AGE_MIN (default 15 min). A real
|
||||
# generation is bounded by the satellite's own 5-minute claude timeout, and
|
||||
# the control plane gives up at 5.5 minutes, so nothing older than that can
|
||||
# still have a caller waiting for it. Below the threshold we might be looking
|
||||
# at live work; above it, by construction, we are not.
|
||||
#
|
||||
# ## Usage
|
||||
#
|
||||
# # from a workstation — inspect first, it is read-only without --kill
|
||||
# ssh <user>@<node> 'bash -s' < scripts/reap-orphan-report-agents.sh
|
||||
# ssh <user>@<node> 'bash -s -- --kill' < scripts/reap-orphan-report-agents.sh
|
||||
#
|
||||
# MIN_AGE_MIN=30 ... raise the age floor (more conservative)
|
||||
#
|
||||
set -uo pipefail
|
||||
|
||||
DO_KILL=0
|
||||
[ "${1:-}" = "--kill" ] && DO_KILL=1
|
||||
MIN_AGE_MIN="${MIN_AGE_MIN:-15}"
|
||||
MIN_AGE_S=$(( MIN_AGE_MIN * 60 ))
|
||||
|
||||
# First line of DEFAULT_READABLE_PROMPT_TEMPLATE (READABLE_PROMPT_TEMPLATE_MEDIA_HERO
|
||||
# is derived from it by .replace() and keeps this line) and of the translate prompt.
|
||||
# See shared/src/readable_prompt.ts.
|
||||
PATTERNS=(
|
||||
"You transform verbose engineering Markdown"
|
||||
"You are translating an already-published HTML report fragment"
|
||||
)
|
||||
|
||||
# ps etime is [[dd-]hh:]mm:ss — turn it into seconds.
|
||||
etime_seconds() {
|
||||
echo "$1" | awk -F: '
|
||||
{ d=0; h=0;
|
||||
if ($1 ~ /-/) { split($1, a, "-"); d=a[1]; $1=a[2] }
|
||||
if (NF == 3) { h=$1; m=$2; s=$3 } else { m=$1; s=$2 }
|
||||
print d*86400 + h*3600 + m*60 + s }'
|
||||
}
|
||||
|
||||
echo "== node: $(hostname -s) | $(date '+%F %T')"
|
||||
echo "== load: $(uptime | sed 's/.*load averages*: //')"
|
||||
echo "== 年龄闸: 只处理运行超过 ${MIN_AGE_MIN} 分钟的报告渲染进程"
|
||||
echo
|
||||
|
||||
victims=()
|
||||
sessions=()
|
||||
total=0
|
||||
|
||||
for pat in "${PATTERNS[@]}"; do
|
||||
while read -r pid; do
|
||||
[ -z "$pid" ] && continue
|
||||
total=$(( total + 1 ))
|
||||
et=$(ps -o etime= -p "$pid" 2>/dev/null | tr -d ' ')
|
||||
[ -z "$et" ] && continue
|
||||
secs=$(etime_seconds "$et")
|
||||
cmd=$(ps -o command= -p "$pid" 2>/dev/null)
|
||||
sid=$(echo "$cmd" | sed -n 's/.*--session-id \([0-9a-fA-F-]\{36\}\).*/\1/p')
|
||||
if [ "$secs" -ge "$MIN_AGE_S" ]; then
|
||||
printf ' ORPHAN pid=%-7s age=%-12s session=%s\n' "$pid" "$et" "${sid:-?}"
|
||||
victims+=("$pid")
|
||||
[ -n "$sid" ] && sessions+=("$sid")
|
||||
else
|
||||
printf ' live? pid=%-7s age=%-12s session=%s (未达年龄闸,跳过)\n' "$pid" "$et" "${sid:-?}"
|
||||
fi
|
||||
done < <(pgrep -f "$pat" 2>/dev/null)
|
||||
done
|
||||
|
||||
echo
|
||||
echo "== 报告渲染进程合计: ${total} 其中判定为残留: ${#victims[@]}"
|
||||
|
||||
if [ "${#victims[@]}" -eq 0 ]; then
|
||||
echo "== 无需清理。"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
if [ "$DO_KILL" -eq 0 ]; then
|
||||
echo "== 这是只读预检。确认无误后加 --kill 再跑一次。"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo
|
||||
echo "== SIGTERM …"
|
||||
for pid in "${victims[@]}"; do kill -TERM "$pid" 2>/dev/null; done
|
||||
# The wrapper's tree (sh -c / tail -f / script) shares the session id in argv.
|
||||
for sid in "${sessions[@]}"; do pkill -TERM -f "$sid" 2>/dev/null; done
|
||||
sleep 5
|
||||
|
||||
echo "== 复查,对仍活着的升级到 SIGKILL …"
|
||||
left=0
|
||||
for pid in "${victims[@]}"; do
|
||||
if kill -0 "$pid" 2>/dev/null; then kill -KILL "$pid" 2>/dev/null; left=$(( left + 1 )); fi
|
||||
done
|
||||
for sid in "${sessions[@]}"; do pkill -KILL -f "$sid" 2>/dev/null; done
|
||||
sleep 2
|
||||
echo "== 需要 SIGKILL 的: ${left}"
|
||||
|
||||
# Only the fifos belonging to sessions we just reaped — a live fix run has its own.
|
||||
for sid in "${sessions[@]}"; do rm -f "/tmp/claude-pty-${sid}.in" 2>/dev/null; done
|
||||
|
||||
echo
|
||||
remaining=0
|
||||
for pat in "${PATTERNS[@]}"; do
|
||||
n=$(pgrep -f "$pat" 2>/dev/null | wc -l | tr -d ' ')
|
||||
remaining=$(( remaining + n ))
|
||||
done
|
||||
echo "== 清理后剩余报告渲染进程: ${remaining}"
|
||||
echo "== load: $(uptime | sed 's/.*load averages*: //')"
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# reap-stuck-claude.sh — watchdog for the Streamline bug + feature runners.
|
||||
#
|
||||
# Symptom it fixes: a `claude -p` session finishes (prints its terminal
|
||||
# `STATUS:` line and delivers the MR) but the OS process never exits because the
|
||||
# agent left non-terminating background shells (e.g. `until pgrep …; do sleep; done`
|
||||
# whose pattern self-matches, or `until adb …; do sleep; done` waiting on state
|
||||
# that never arrives, or a stray `adb logcat` left running after verification).
|
||||
# The runner blocks on `await proc.exited`, so the task is pinned in
|
||||
# `fixing` / `implementing` forever.
|
||||
#
|
||||
# This reaper finds runner-spawned claude sessions whose log shows a terminal
|
||||
# STATUS line AND has been idle for a while, then kills the whole process tree.
|
||||
# Killing claude lets the server's runner resolve `proc.exited`, read the STATUS,
|
||||
# and move the task forward (-> manual_review / fix_failed / unreproducible /
|
||||
# implementation_failed). It then writes the agent's report — FixReport.md for
|
||||
# bug runs, ImplReport.md for feature runs — to the workspace.
|
||||
#
|
||||
# Safe by design:
|
||||
# - Only touches `claude -p` processes carrying `--remote-control streamline-`.
|
||||
# - Requires the agent's own terminal STATUS line to be present (it declared
|
||||
# completion) — never kills a session that hasn't finished.
|
||||
# - Requires the log to be idle (no writes) for >= IDLE_MIN minutes, so an
|
||||
# actively-working session (or one still streaming a build) is left alone.
|
||||
#
|
||||
# Env:
|
||||
# IDLE_MIN minutes the session.log must be idle before reaping (default 15)
|
||||
# REAP_DRY_RUN set to 1 to only log what WOULD be killed
|
||||
#
|
||||
# Schedule hourly via cron: 0 * * * * /Users/fei/BBBBBBug/scripts/reap-stuck-claude.sh
|
||||
|
||||
set -uo pipefail
|
||||
|
||||
IDLE_MIN="${IDLE_MIN:-15}"
|
||||
DRY="${REAP_DRY_RUN:-0}"
|
||||
LOG="${REAP_LOG:-$HOME/.streamline/reaper.log}"
|
||||
mkdir -p "$(dirname "$LOG")"
|
||||
|
||||
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG"; }
|
||||
|
||||
# Recursively kill a process and all its descendants (children first).
|
||||
kill_tree() {
|
||||
local pid=$1 child
|
||||
for child in $(pgrep -P "$pid" 2>/dev/null); do
|
||||
kill_tree "$child"
|
||||
done
|
||||
kill "$pid" 2>/dev/null
|
||||
}
|
||||
|
||||
reaped=0
|
||||
|
||||
# All runner-spawned fix sessions.
|
||||
for pid in $(pgrep -f -- '--remote-control streamline-' 2>/dev/null); do
|
||||
cmd=$(ps -p "$pid" -o command= 2>/dev/null)
|
||||
[ -z "$cmd" ] && continue
|
||||
case "$cmd" in *"claude -p"*) ;; *) continue ;; esac
|
||||
|
||||
# Workspace dir from the prompt (…工作区在 /…/bug_workspace/<id>…).
|
||||
# Bug runs are bug_workspace/<numericMeegoId>; feature runs are
|
||||
# bug_workspace/feature-<uuid>. Charset is tight (digits or
|
||||
# `feature-[a-z0-9-]+`) because the prompt has a Chinese 。 right after
|
||||
# the path with no space — a permissive [^/ ]+ would swallow trailing
|
||||
# literals like `。\012TikTok` and break the workspace path.
|
||||
ws=$(printf '%s' "$cmd" | grep -oE '/[^ ]*bug_workspace/(feature-[a-z0-9-]+|[0-9]+)' | head -1)
|
||||
[ -z "$ws" ] && { log "pid=$pid: no workspace in cmdline, skip"; continue; }
|
||||
logfile="$ws/logs/session.log"
|
||||
[ -f "$logfile" ] || { log "pid=$pid ws=$ws: no session.log yet, skip"; continue; }
|
||||
|
||||
# Has the agent declared completion? (its contract: final line is STATUS: …)
|
||||
# Bug runner emits FIXED/UNREPRODUCIBLE/FIX_FAILED; feature runner emits
|
||||
# IMPLEMENTED/IMPLEMENTATION_FAILED. Reap on any terminal token.
|
||||
if ! grep -qE 'STATUS: (FIXED|UNREPRODUCIBLE|FIX_FAILED|IMPLEMENTED|IMPLEMENTATION_FAILED)' "$logfile"; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# Is the log idle? (no writes for >= IDLE_MIN minutes => not actively working)
|
||||
if [ -z "$(find "$logfile" -mmin +"$IDLE_MIN" 2>/dev/null)" ]; then
|
||||
log "pid=$pid ws=$ws: STATUS present but log still fresh (<${IDLE_MIN}m), leaving"
|
||||
continue
|
||||
fi
|
||||
|
||||
status=$(grep -oE 'STATUS: (FIXED|UNREPRODUCIBLE|FIX_FAILED|IMPLEMENTED|IMPLEMENTATION_FAILED)' "$logfile" | tail -1)
|
||||
kids=$(pgrep -P "$pid" 2>/dev/null | tr '\n' ' ')
|
||||
if [ "$DRY" = "1" ]; then
|
||||
log "[dry-run] WOULD reap pid=$pid ws=$ws ($status); descendants: ${kids:-none}"
|
||||
else
|
||||
log "reaping pid=$pid ws=$ws ($status); descendants: ${kids:-none}"
|
||||
kill_tree "$pid"
|
||||
reaped=$((reaped + 1))
|
||||
fi
|
||||
done
|
||||
|
||||
[ "$reaped" -gt 0 ] && log "reaped $reaped stuck session(s)"
|
||||
exit 0
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* One-off op: rebalance a slice of ios queue onto the freshly-added
|
||||
* `fei-ios-02.shiro0.icu` by RE-EVALUATING (not just re-homing).
|
||||
*
|
||||
* Context: existing queued features were `evaluate`d on ios-01, which pinned
|
||||
* their `executorNodeId` to ios-01 (claim-time binding, see db.claimEvalForNode),
|
||||
* so only ios-01 would ever schedule them. To move real work to ios-02 the owner
|
||||
* asked to send a portion BACK to eval on ios-02 — a true re-eval, ending at
|
||||
* `pending_confirmation` (fresh plan, needs re-confirm), NOT a plan-preserving
|
||||
* re-home.
|
||||
*
|
||||
* Scope (owner-approved): 5 FEATURES from `awaiting_implementation` that were
|
||||
* NEVER executed (attempts=0, no branch/MR/log/review/startedAt), oldest-first.
|
||||
* For each we:
|
||||
* 1. DELETE the existing workspace dir ON ios-01 (its current executor node),
|
||||
* via the satellite's removeWorkspace endpoint — done BEFORE re-pointing so
|
||||
* forwardFeatureToNode still resolves to ios-01.
|
||||
* 2. Reset the row to `initializing` + executorNodeId=ios-02 + cleared plan /
|
||||
* workspace / timing, so ios-02's eval loop claims and re-evals it. Goes
|
||||
* through updateFeature → notifyStatusTransition fires (/lifemon ledger +
|
||||
* status webhooks), unlike a raw SQL UPDATE.
|
||||
*
|
||||
* DRY-RUN by default (prints the plan); pass --commit to actually mutate:
|
||||
* bun scripts/reeval_migrate_ios02.ts # preview only
|
||||
* bun scripts/reeval_migrate_ios02.ts --commit # execute
|
||||
*
|
||||
* Note: this runs as a SEPARATE process from the live control-server, so it
|
||||
* cannot push WS `feature:updated` events — refresh the board/console to see
|
||||
* the moved items. The DB is the SSOT, so ios-02's next eval poll picks them up.
|
||||
*/
|
||||
import { getFeature, updateFeature } from "../control-server/src/db";
|
||||
import { forwardFeatureToNode, satPath } from "../control-server/src/cluster";
|
||||
|
||||
const COMMIT = process.argv.includes("--commit");
|
||||
|
||||
const HOME_NODE = "fei-ios-01.shiro0.icu";
|
||||
const TARGET_NODE = "fei-ios-02.shiro0.icu";
|
||||
const MOVABLE_STATUS = "awaiting_implementation";
|
||||
|
||||
// The 5 oldest never-executed features on ios-01 (owner-approved slice).
|
||||
// Hardcoded so the migration acts on EXACTLY the reviewed set, not a live re-query
|
||||
// that could drift; each is re-validated against the eligibility rule below.
|
||||
const TARGET_IDS = [
|
||||
"8613f045-7ed7-4f35-bb1a-182870491cf9", // 视频内流 DM 输入框优化
|
||||
"db878256-4398-4aac-9f02-fe3812005a29", // 聊天页灰阶优化
|
||||
"cfea926a-38f2-4bb5-b71d-3b42d28ce5d7", // 视频内流点击输入框后拉起的 DM 回复面板输入框优化
|
||||
"ffff3280-4dea-48bf-bb7f-25354b3a0bce", // 线上:同个 Story 短时间内多个回复带 story quote 卡
|
||||
"35908902-4f1b-4d68-95c4-2881a54d01ec", // 聊天页新增震感反馈
|
||||
];
|
||||
|
||||
/** Never-executed + still queued on the home node = safe to re-eval elsewhere. */
|
||||
function eligibility(f: NonNullable<ReturnType<typeof getFeature>>): string | null {
|
||||
if (f.status !== MOVABLE_STATUS) return `status=${f.status} (expected ${MOVABLE_STATUS})`;
|
||||
if (f.executorNodeId !== HOME_NODE) return `executorNodeId=${f.executorNodeId} (expected ${HOME_NODE})`;
|
||||
if ((f.attempts ?? 0) !== 0) return `attempts=${f.attempts} (expected 0 — already executed)`;
|
||||
if (f.startedAt || f.branchName || f.mrUrl || f.logPath || (f.reviewHistory?.length ?? 0) > 0)
|
||||
return "carries execution history (branch/MR/log/review/startedAt)";
|
||||
return null;
|
||||
}
|
||||
|
||||
/** The re-eval reset patch: back to eval, pinned to ios-02, plan+workspace+clock cleared. */
|
||||
const resetPatch = {
|
||||
status: "initializing" as const,
|
||||
executorNodeId: TARGET_NODE,
|
||||
executionPlan: null,
|
||||
workspacePath: null,
|
||||
regenContext: null,
|
||||
evalLogPath: null,
|
||||
failureReason: null,
|
||||
startedAt: null,
|
||||
finishedAt: null,
|
||||
durationMs: null,
|
||||
totalDurationMs: null,
|
||||
};
|
||||
|
||||
console.log(`[reeval_migrate_ios02] mode=${COMMIT ? "COMMIT" : "DRY-RUN"} ${HOME_NODE} → ${TARGET_NODE}\n`);
|
||||
|
||||
let done = 0, skipped = 0;
|
||||
for (const id of TARGET_IDS) {
|
||||
const f = getFeature(id);
|
||||
if (!f) { console.log(` SKIP ${id} — not found`); skipped++; continue; }
|
||||
const bad = eligibility(f);
|
||||
if (bad) { console.log(` SKIP ${id.slice(0, 8)} ${f.title}\n reason: ${bad}`); skipped++; continue; }
|
||||
|
||||
console.log(` MOVE ${id.slice(0, 8)} ${f.title}`);
|
||||
console.log(` rm ws on ${HOME_NODE}: ${f.workspacePath ?? "(none)"}`);
|
||||
console.log(` → ${MOVABLE_STATUS}/@${HOME_NODE} ⇒ initializing/@${TARGET_NODE} (re-eval)`);
|
||||
|
||||
if (COMMIT) {
|
||||
// 1) Remove the ios-01 workspace FIRST — resolves via current executorNodeId.
|
||||
const removed = await forwardFeatureToNode(f, satPath.feature.removeWorkspace, { method: "DELETE" });
|
||||
console.log(` workspace delete on ${HOME_NODE}: ${removed ? "ok" : "FAILED (node said no / unreachable)"}`);
|
||||
// 2) Re-point + reset to eval (fires notifyStatusTransition → ledger + webhooks).
|
||||
updateFeature(id, resetPatch);
|
||||
console.log(` db reset: done`);
|
||||
}
|
||||
done++;
|
||||
}
|
||||
|
||||
console.log(`\n[reeval_migrate_ios02] ${COMMIT ? "committed" : "planned"} ${done} move(s), ${skipped} skipped.`);
|
||||
if (!COMMIT) console.log("[reeval_migrate_ios02] DRY-RUN — nothing changed. Re-run with --commit to execute.");
|
||||
@@ -0,0 +1,173 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
|
||||
const db = new Database("/Users/fei/.streamline/data.sqlite", { readonly: true });
|
||||
|
||||
type Row = Record<string, any>;
|
||||
const bugs = db.query("SELECT * FROM tasks").all() as Row[];
|
||||
const feats = db.query("SELECT * FROM features").all() as Row[];
|
||||
|
||||
// ---- replicate shared/src/types.ts logic ----
|
||||
function meegoIsClosed(m: string | null) {
|
||||
const s = (m || "").toLowerCase();
|
||||
return s === "close" || s === "closed" || s === "done";
|
||||
}
|
||||
function comboTaskStateOf(mrState: string | null, meegoState: string | null): "merged" | "awaiting_merge" | "awaiting_qa" | null {
|
||||
const mr = (mrState || "").toLowerCase();
|
||||
const meego = (meegoState || "").toLowerCase();
|
||||
if (mr.startsWith("merg")) return "merged";
|
||||
if (meego === "resolved" || meego === "open") return "awaiting_qa";
|
||||
if (meego === "close" || meego === "closed" || meego === "done") {
|
||||
if (mr === "closed" || mr === "close") return "merged";
|
||||
return "awaiting_merge";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function isConfirmedUnrepro(status: string, meegoState: string | null) {
|
||||
return status === "unreproducible" && meegoIsClosed(meegoState);
|
||||
}
|
||||
function bucketOfBug(t: Row): string {
|
||||
const s = t.status;
|
||||
if (s === "fixing") return "running";
|
||||
if (s === "initializing" || s === "info_required" || s === "pending_confirmation") return "confirm";
|
||||
if (s === "awaiting_fix") return "queued";
|
||||
if (s === "manual_review") return "review";
|
||||
if (s === "unreproducible") return isConfirmedUnrepro(s, t.meegoState) ? "confirmed_unrepro" : "unreproducible";
|
||||
if (s === "fix_failed" || s === "cancelled") return "failed";
|
||||
if (s === "fix_succeeded") {
|
||||
const combo = comboTaskStateOf(t.mrState, t.meegoState);
|
||||
if (combo === "merged") return "merged";
|
||||
if (combo === "awaiting_merge") return "awaiting_merge";
|
||||
return "awaiting_qa";
|
||||
}
|
||||
return "failed";
|
||||
}
|
||||
function bucketOfFeat(f: Row): string {
|
||||
const s = f.status;
|
||||
if (s === "implementing") return "running";
|
||||
if (s === "initializing" || s === "info_required" || s === "pending_confirmation") return "confirm";
|
||||
if (s === "awaiting_implementation") return "queued";
|
||||
if (s === "manual_review") return "review";
|
||||
if (s === "implementation_failed" || s === "cancelled") return "failed";
|
||||
if (s === "completed") return "merged";
|
||||
return "failed";
|
||||
}
|
||||
|
||||
const pct = (n: number, d: number) => (d ? ((n / d) * 100).toFixed(1) : "0.0");
|
||||
function median(arr: number[]) {
|
||||
if (!arr.length) return 0;
|
||||
const a = [...arr].sort((x, y) => x - y);
|
||||
const m = Math.floor(a.length / 2);
|
||||
return a.length % 2 ? a[m] : (a[m - 1] + a[m]) / 2;
|
||||
}
|
||||
function mean(arr: number[]) { return arr.length ? arr.reduce((s, x) => s + x, 0) / arr.length : 0; }
|
||||
const fmtH = (ms: number) => (ms / 3600000).toFixed(2) + "h";
|
||||
const fmtM = (ms: number) => (ms / 60000).toFixed(1) + "m";
|
||||
|
||||
// ===== 1. Totals by status =====
|
||||
console.log("\n========== RAW STATUS COUNTS ==========");
|
||||
const bugStatus: Record<string, number> = {};
|
||||
for (const b of bugs) bugStatus[b.status] = (bugStatus[b.status] || 0) + 1;
|
||||
const featStatus: Record<string, number> = {};
|
||||
for (const f of feats) featStatus[f.status] = (featStatus[f.status] || 0) + 1;
|
||||
console.log("BUGS total:", bugs.length, bugStatus);
|
||||
console.log("FEATURES total:", feats.length, featStatus);
|
||||
|
||||
// ===== bucket counts =====
|
||||
const bugBucket: Record<string, number> = {};
|
||||
for (const b of bugs) { const k = bucketOfBug(b); bugBucket[k] = (bugBucket[k] || 0) + 1; }
|
||||
const featBucket: Record<string, number> = {};
|
||||
for (const f of feats) { const k = bucketOfFeat(f); featBucket[k] = (featBucket[k] || 0) + 1; }
|
||||
console.log("\n========== BUCKETS ==========");
|
||||
console.log("BUG buckets:", bugBucket);
|
||||
console.log("FEATURE buckets:", featBucket);
|
||||
|
||||
// ===== 2. Completed = awaiting_merge + awaiting_qa + merged =====
|
||||
const completedBuckets = new Set(["awaiting_merge", "awaiting_qa", "merged"]);
|
||||
const bugCompleted = bugs.filter((b) => completedBuckets.has(bucketOfBug(b)));
|
||||
const featCompleted = feats.filter((f) => completedBuckets.has(bucketOfFeat(f)));
|
||||
const allCompleted = [...bugCompleted, ...featCompleted];
|
||||
|
||||
const bugMerged = bugs.filter((b) => bucketOfBug(b) === "merged");
|
||||
const featMerged = feats.filter((f) => bucketOfFeat(f) === "merged");
|
||||
|
||||
console.log("\n========== COMPLETED (await-merge + await-qa + merged) ==========");
|
||||
console.log("Total tasks (bugs+features):", bugs.length + feats.length);
|
||||
console.log("Completed bugs:", bugCompleted.length, "/ bugs", bugs.length, `(${pct(bugCompleted.length, bugs.length)}%)`);
|
||||
console.log("Completed features:", featCompleted.length, "/ features", feats.length, `(${pct(featCompleted.length, feats.length)}%)`);
|
||||
console.log("Completed ALL:", allCompleted.length, `(${pct(allCompleted.length, bugs.length + feats.length)}% of all tasks)`);
|
||||
console.log(" of which awaiting_merge:", (bugBucket.awaiting_merge||0)+(featBucket.awaiting_merge||0));
|
||||
console.log(" of which awaiting_qa:", (bugBucket.awaiting_qa||0)+(featBucket.awaiting_qa||0));
|
||||
console.log(" of which MERGED:", bugMerged.length + featMerged.length, `(bug ${bugMerged.length}, feat ${featMerged.length})`);
|
||||
|
||||
// ===== 3. Execution rounds (attempts) for completed =====
|
||||
function roundDist(rows: Row[]) {
|
||||
const d: Record<number, number> = {};
|
||||
for (const r of rows) { const a = r.attempts ?? 0; d[a] = (d[a] || 0) + 1; }
|
||||
return d;
|
||||
}
|
||||
// NOTE: rounds = attempts (runner sets attempts = prev+1 each fix round). first-pass = attempts==1
|
||||
console.log("\n========== EXECUTION ROUNDS (rounds = attempts; 1 = first-pass) ==========");
|
||||
function roundPct(rows: Row[]) {
|
||||
const d = roundDist(rows); const tot = rows.length;
|
||||
return Object.entries(d).sort((a,b)=>+a[0]-+b[0]).map(([k,v])=>`R${k}:${v}(${pct(v,tot)}%)`).join(" ");
|
||||
}
|
||||
console.log("Completed ALL:", roundPct(allCompleted));
|
||||
console.log("Completed BUGS:", roundPct(bugCompleted));
|
||||
console.log("Completed FEATURES:", roundPct(featCompleted));
|
||||
const firstPassAll = allCompleted.filter((r) => (r.attempts ?? 0) === 1).length;
|
||||
console.log(`First-pass (R1) ALL: ${firstPassAll}/${allCompleted.length} = ${pct(firstPassAll, allCompleted.length)}%`);
|
||||
const le2 = allCompleted.filter((r)=>(r.attempts??0)<=2).length;
|
||||
console.log(`Within 2 rounds ALL: ${le2}/${allCompleted.length} = ${pct(le2, allCompleted.length)}%`);
|
||||
|
||||
for (const plat of ["android", "ios"]) {
|
||||
const rows = allCompleted.filter((r) => r.platform === plat);
|
||||
const fp = rows.filter((r) => (r.attempts ?? 0) === 1).length;
|
||||
console.log(` ${plat} (all): completed=${rows.length}, first-pass=${fp} (${pct(fp, rows.length)}%) ${roundPct(rows)}`);
|
||||
}
|
||||
for (const plat of ["android", "ios"]) {
|
||||
const rows = bugCompleted.filter((r) => r.platform === plat);
|
||||
const fp = rows.filter((r) => (r.attempts ?? 0) === 1).length;
|
||||
console.log(` ${plat} (bugs): completed=${rows.length}, first-pass=${fp} (${pct(fp, rows.length)}%)`);
|
||||
}
|
||||
|
||||
// ===== 4. Unreproducible =====
|
||||
const unrepro = bugs.filter((b) => b.status === "unreproducible");
|
||||
const unreproConfirmed = unrepro.filter((b) => meegoIsClosed(b.meegoState));
|
||||
console.log("\n========== AUTO-JUDGED UNREPRODUCIBLE ==========");
|
||||
console.log("Total unreproducible:", unrepro.length, `(${pct(unrepro.length, bugs.length)}% of bugs)`);
|
||||
console.log(" accepted/confirmed (meego closed):", unreproConfirmed.length, `(${pct(unreproConfirmed.length, unrepro.length)}%)`);
|
||||
console.log(" unconfirmed/pending:", unrepro.length - unreproConfirmed.length, `(${pct(unrepro.length - unreproConfirmed.length, unrepro.length)}%)`);
|
||||
console.log(" by platform:", "android", unrepro.filter(b=>b.platform==="android").length, "ios", unrepro.filter(b=>b.platform==="ios").length);
|
||||
|
||||
// ===== 5. Execution time for completed (totalDurationMs) =====
|
||||
console.log("\n========== EXECUTION TIME (completed tasks) ==========");
|
||||
function durStats(rows: Row[], field: string) {
|
||||
const vals = rows.map((r) => r[field]).filter((v) => typeof v === "number" && v > 0) as number[];
|
||||
return { n: vals.length, mean: mean(vals), median: median(vals), vals };
|
||||
}
|
||||
for (const field of ["totalDurationMs", "durationMs"]) {
|
||||
console.log(`\n-- field=${field} --`);
|
||||
const all = durStats(allCompleted, field);
|
||||
console.log(`ALL completed: n=${all.n} mean=${fmtH(all.mean)} (${fmtM(all.mean)}) median=${fmtH(all.median)} (${fmtM(all.median)})`);
|
||||
for (const plat of ["android", "ios"]) {
|
||||
const s = durStats(allCompleted.filter((r) => r.platform === plat), field);
|
||||
console.log(` ${plat}: n=${s.n} mean=${fmtH(s.mean)} (${fmtM(s.mean)}) median=${fmtH(s.median)} (${fmtM(s.median)})`);
|
||||
}
|
||||
// bugs only
|
||||
const sb = durStats(bugCompleted, field);
|
||||
console.log(` bugs-only: n=${sb.n} mean=${fmtH(sb.mean)} median=${fmtH(sb.median)}`);
|
||||
}
|
||||
|
||||
// ===== platform split of all tasks =====
|
||||
console.log("\n========== PLATFORM SPLIT (all tasks) ==========");
|
||||
for (const plat of ["android", "ios"]) {
|
||||
console.log(`${plat}: bugs=${bugs.filter(b=>b.platform===plat).length} features=${feats.filter(f=>f.platform===plat).length}`);
|
||||
}
|
||||
|
||||
// first-pass per platform bugs-only too
|
||||
console.log("\n========== FIRST-PASS by platform (bugs only, completed) ==========");
|
||||
for (const plat of ["android", "ios"]) {
|
||||
const rows = bugCompleted.filter((r) => r.platform === plat);
|
||||
const fp = rows.filter((r) => (r.attempts ?? 0) === 0).length;
|
||||
console.log(` ${plat}: ${fp}/${rows.length} = ${pct(fp, rows.length)}%`);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* One-off: re-submit a single bug's Bits MR through the NEW flow (service-account
|
||||
* token + x-bits-user owner), KEEPING the existing branch, then persist the new
|
||||
* mrUrl and reset the mr-sync fields so the poller picks up the fresh MR.
|
||||
*
|
||||
* Usage: bun scripts/resubmit_mr.ts <meegoId> [--write]
|
||||
* Without --write it does a dry run of the bits-user resolution only.
|
||||
*/
|
||||
import { Database } from "bun:sqlite";
|
||||
import { homedir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { fetchMeegoUserContacts } from "../control-server/src/meego";
|
||||
import { submitMrForFix, composeMrTitle, pickBitsUser, firstEmailWatcher, emailPrefix } from "../control-server/src/bits";
|
||||
import type { Platform } from "../control-server/src/types";
|
||||
|
||||
const meegoId = Bun.argv[2];
|
||||
const doWrite = Bun.argv.includes("--write");
|
||||
if (!meegoId) { console.error("usage: bun scripts/resubmit_mr.ts <meegoId> [--write]"); process.exit(2); }
|
||||
|
||||
const dbPath = path.join(homedir(), ".streamline", "data.sqlite");
|
||||
const db = new Database(dbPath); // read-write — we persist below on success
|
||||
|
||||
const row = db
|
||||
.query("SELECT id, meegoUrl, title, branchName, platform, creatorEmail, watchers, mrUrl FROM tasks WHERE meegoId = $m")
|
||||
.get({ $m: meegoId }) as
|
||||
| { id: string; meegoUrl: string; title: string; branchName: string | null; platform: Platform; creatorEmail: string | null; watchers: string | null; mrUrl: string | null }
|
||||
| null;
|
||||
|
||||
if (!row) { console.error(`task meegoId=${meegoId} not found in ${dbPath}`); process.exit(2); }
|
||||
if (!row.branchName) { console.error(`task ${row.id} has no branchName — cannot submit`); process.exit(2); }
|
||||
|
||||
let watchers: string[] = [];
|
||||
try { watchers = JSON.parse(row.watchers || "[]"); } catch { watchers = []; }
|
||||
|
||||
const contacts = await fetchMeegoUserContacts(meegoId, row.meegoUrl);
|
||||
const bitsUser = pickBitsUser([contacts.assignee, row.creatorEmail, firstEmailWatcher(watchers)]);
|
||||
|
||||
console.log("=== Re-submit (new flow) ===");
|
||||
console.log(JSON.stringify({
|
||||
taskId: row.id, meegoId, platform: row.platform, branch: row.branchName,
|
||||
oldMrUrl: row.mrUrl,
|
||||
assignee: contacts.assignee, assigneePrefix: emailPrefix(contacts.assignee),
|
||||
creatorEmail: row.creatorEmail, firstWatcher: firstEmailWatcher(watchers),
|
||||
resolvedBitsUser: bitsUser,
|
||||
}, null, 2));
|
||||
|
||||
if (!doWrite) { console.log("\n(dry run — pass --write to actually create the MR + update db)"); process.exit(0); }
|
||||
|
||||
const result = await submitMrForFix({
|
||||
platform: row.platform,
|
||||
sourceBranch: row.branchName,
|
||||
targetBranch: process.env.BASE_BRANCH || "develop",
|
||||
title: composeMrTitle(row.title),
|
||||
mrType: "bug",
|
||||
meegoTaskId: meegoId,
|
||||
assigneeEmail: contacts.assignee,
|
||||
reporterEmail: contacts.reporter,
|
||||
bitsUser,
|
||||
});
|
||||
|
||||
console.log("\n=== submitMrForFix result ===");
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
|
||||
if (result.ok && result.mrUrl) {
|
||||
db.query(
|
||||
"UPDATE tasks SET mrUrl = $u, mrState = NULL, mrMergedAt = NULL, mrSyncedAt = NULL WHERE id = $id",
|
||||
).run({ $u: result.mrUrl, $id: row.id });
|
||||
console.log(`\n✅ db updated: tasks.${row.id}.mrUrl = ${result.mrUrl} (mr-sync fields reset)`);
|
||||
} else {
|
||||
console.error("\n❌ create failed — db NOT updated");
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env bash
|
||||
# Daily fix-quality snapshot + tool-gap mining + KB attribution sweep
|
||||
# → Postgres (rsi_metrics_daily / rsi_findings / rsi_tool_gaps / kb_injections).
|
||||
#
|
||||
# Same shape as evidmon-daily.sh: the machine clock is NOT Beijing, so cron
|
||||
# invokes this hourly and the script self-gates on Asia/Shanghai time. It fires
|
||||
# once a day at 09:10 北京时间 — ten minutes after the evidence snapshot, so the
|
||||
# two never contend for the same nodes, and still well before any analyst pass.
|
||||
#
|
||||
# Args:
|
||||
# (none) cron mode — gate on Beijing 09:10.
|
||||
# --force bypass the gate and run now.
|
||||
# --no-fleet skip the fan-out to satellites (local runs).
|
||||
#
|
||||
# bash, not zsh (the sibling cron scripts here are zsh): this one's home is the
|
||||
# Linux control plane — /data00/home/jiaopengwen/streamline on the devbox, the
|
||||
# only box guaranteed to hold a current checkout because every deploy pulls it —
|
||||
# and that box has no zsh. A zsh shebang there fails as
|
||||
# `env: '…/rsi-daily.sh': No such file or directory`, which reads like the file
|
||||
# is missing rather than the interpreter. Nothing below is zsh-specific.
|
||||
|
||||
set -e
|
||||
REPO=${STREAMLINE_REPO:-/Users/fei/BBBBBBug}
|
||||
BUN=${BUN_BIN:-/opt/homebrew/bin/bun}
|
||||
LOG=$HOME/.streamline/rsi_daily.log
|
||||
|
||||
cd "$REPO"
|
||||
mkdir -p "$(dirname "$LOG")"
|
||||
|
||||
if [[ "${1:-}" != "--force" ]]; then
|
||||
HOUR=$(TZ=Asia/Shanghai date +'%H')
|
||||
if [[ "$HOUR" != "09" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "===== $(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %Z') =====" >> "$LOG"
|
||||
"$BUN" run control-server/src/rsi_daily.ts "${@:2}" >> "$LOG" 2>&1
|
||||
@@ -0,0 +1,120 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* 把一条已批准的 RSI 实验写进代码常量 —— 「批准上线」之后的落代码一步。
|
||||
*
|
||||
* bun scripts/rsi-ship.ts <实验 id> 改本地文件,打印建议的提交信息
|
||||
* bun scripts/rsi-ship.ts <实验 id> --dry-run 只算、只校验,不写文件
|
||||
*
|
||||
* 做的事:
|
||||
* 1. 从控制面只读接口 /api/rsi-ship/experiments/<id> 取上线材料(上线范围、各端基线快照与候选 / 旋钮)。
|
||||
* 2. 在**本地**代码上重算(本地可能比线上新):prompt 实验按锚点变基,单端上线把新增行包进平台段,
|
||||
* 并校验另一端展开后的正文逐字节不变(shared/src/rsi_ship.ts::planPromptShip);运行时实验改
|
||||
* shared/src/executor_profile.ts 的 PRODUCTION_PROFILE。
|
||||
* 3. 改源码后另起一个 bun 进程重新 import,核对常量的值就是算出来的那一份;不一致就还原文件。
|
||||
*
|
||||
* 不提交、不推送、不部署:改完照常 review → 提交(提交信息带 RSI-Experiment 行)→ cpd / streamline-deploy。
|
||||
* 部署后控制面判定任务按运行中的代码核对,把实验记为「已上线」。
|
||||
*
|
||||
* 控制面地址:STREAMLINE_API,默认内网只读 API。
|
||||
*/
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { parseArgs } from "node:util";
|
||||
import { PROMPT_NODES } from "../shared/src/prompt_registry";
|
||||
import { DEFAULT_RUN_TEMPLATES, type RunPromptStage } from "../shared/src/run_prompt";
|
||||
import { PRODUCTION_PROFILE, type ExecutorProfile } from "../shared/src/executor_profile";
|
||||
import {
|
||||
isShipScope, planProfileShip, planPromptShip, promptShipped, replaceLiteralConst, profileShipped,
|
||||
type ShipMember, type ShipScope,
|
||||
} from "../shared/src/rsi_ship";
|
||||
import { isPromptPlatform, type PromptPlatform } from "../shared/src/prompt_platform";
|
||||
|
||||
const ROOT = join(import.meta.dir, "..");
|
||||
const API = (process.env.STREAMLINE_API ?? "https://streamline-internal-api.bytedance.net").replace(/\/+$/, "");
|
||||
|
||||
interface Bundle {
|
||||
id: string; title: string; stage: string; kind: "prompt" | "runtime"; shipScope: string;
|
||||
members: { id: string; platform: string; status: string; base?: string; candidate?: string; profile?: ExecutorProfile }[];
|
||||
}
|
||||
|
||||
const die = (msg: string): never => { console.error(`✗ ${msg}`); process.exit(1); };
|
||||
const label = (p: string) => ({ ios: "iOS", android: "Android", both: "两端" } as Record<string, string>)[p] ?? p;
|
||||
|
||||
const { values, positionals } = parseArgs({ args: Bun.argv.slice(2), options: { "dry-run": { type: "boolean" } }, allowPositionals: true });
|
||||
const id = positionals[0] || die("用法:bun scripts/rsi-ship.ts <实验 id> [--dry-run]");
|
||||
const dry = !!values["dry-run"];
|
||||
|
||||
const res = await fetch(`${API}/api/rsi-ship/experiments/${encodeURIComponent(id)}`);
|
||||
const bundle = (await res.json().catch(() => ({}))) as Bundle & { error?: string };
|
||||
if (!res.ok) die(`取上线材料失败(${res.status}):${bundle.error ?? "?"}`);
|
||||
const scope = bundle.shipScope;
|
||||
if (!isShipScope(scope)) die(`上线范围不对:${scope}`);
|
||||
console.log(`实验「${bundle.title}」 · ${bundle.stage} · ${bundle.kind === "runtime" ? "运行时旋钮" : "prompt"} · 上线范围:${label(scope)}`);
|
||||
|
||||
/** 改一个源文件,另起进程 import 核对 —— 不一致就还原。 */
|
||||
async function writeVerified(file: string, next: string, check: string) {
|
||||
const path = join(ROOT, file);
|
||||
const prev = readFileSync(path, "utf-8");
|
||||
if (prev === next) return console.log(` ${file}:没有变化`);
|
||||
if (dry) return console.log(` ${file}:会改(--dry-run,未写)`);
|
||||
writeFileSync(path, next);
|
||||
const proc = Bun.spawnSync(["bun", "-e", check], { cwd: ROOT, stdout: "pipe", stderr: "pipe" });
|
||||
if (proc.exitCode !== 0) {
|
||||
writeFileSync(path, prev);
|
||||
die(`改完 import 回来核对不一致,已还原 ${file}:${proc.stderr.toString().slice(0, 400)}`);
|
||||
}
|
||||
console.log(` ${file}:已改,import 核对一致`);
|
||||
}
|
||||
|
||||
if (bundle.kind === "runtime") {
|
||||
const members = bundle.members.filter((m) => isPromptPlatform(m.platform))
|
||||
.map((m) => ({ platform: m.platform as PromptPlatform, profile: m.profile ?? {} }));
|
||||
if (members.every((m) => profileShipped(PRODUCTION_PROFILE, m.platform, m.profile))) {
|
||||
console.log("本地代码的 PRODUCTION_PROFILE 已经带上了,不用改。"); process.exit(0);
|
||||
}
|
||||
const plan = planProfileShip(PRODUCTION_PROFILE, members, scope as ShipScope);
|
||||
if (!plan.ok) die(plan.error);
|
||||
const file = "shared/src/executor_profile.ts";
|
||||
const src = readFileSync(join(ROOT, file), "utf-8");
|
||||
const body = Object.entries(plan.profiles).filter(([, v]) => Object.keys(v).length)
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([p, v]) => ` ${p}: { ${Object.entries(v).map(([k, x]) => `${k}: ${JSON.stringify(x)}`).join(", ")} },`).join("\n");
|
||||
const re = /(export const PRODUCTION_PROFILE: Readonly<Record<string, ExecutorProfile>> = \{)[\s\S]*?(\n\};)/;
|
||||
if (!re.test(src)) die(`${file} 里找不到 PRODUCTION_PROFILE 的定义(格式被改过?)`);
|
||||
const next = src.replace(re, (_, a, b) => `${a}${body ? `\n${body}` : ""}${b}`);
|
||||
const want = JSON.stringify(plan.profiles);
|
||||
await writeVerified(file, next, `import { PRODUCTION_PROFILE as P } from "./${file}";
|
||||
const norm = (o) => JSON.stringify(Object.fromEntries(Object.entries(o).filter(([, v]) => Object.keys(v).length).sort()));
|
||||
if (norm(P) !== norm(${want})) { console.error(JSON.stringify(P)); process.exit(1); }`);
|
||||
for (const m of members) console.log(` ${label(m.platform)}:${JSON.stringify(plan.profiles[m.platform])}`);
|
||||
} else {
|
||||
const stage = bundle.stage as RunPromptStage;
|
||||
const local = DEFAULT_RUN_TEMPLATES[stage] ?? die(`不认识的 stage ${stage}`);
|
||||
const node = PROMPT_NODES.find((n) => n.id === stage) ?? die(`提示词注册表里没有 ${stage}`);
|
||||
const members = bundle.members.filter((m) => isPromptPlatform(m.platform) && m.base && m.candidate)
|
||||
.map((m) => ({ platform: m.platform, base: m.base!, candidate: m.candidate! }) as ShipMember);
|
||||
if (members.length && members.every((m) => promptShipped(local, m))) {
|
||||
console.log("本地代码已经带上这条改动了,不用改。"); process.exit(0);
|
||||
}
|
||||
const plan = planPromptShip(local, members, scope as ShipScope);
|
||||
if (!plan.ok) die(`补丁不可用:${plan.error}`);
|
||||
for (const c of plan.checks) console.log(` ${c.ok ? "✓" : "✗"} ${label(c.platform)}:${c.note}`);
|
||||
for (const d of plan.drift) console.log(` · ${label(d.platform)}:实验之后模板改过(差 ${d.lines} 行),已按锚点变基`);
|
||||
const src = readFileSync(join(ROOT, node.source), "utf-8");
|
||||
const r = replaceLiteralConst(src, node.symbol, plan.template);
|
||||
if ("error" in r) die(r.error);
|
||||
const tmp = join(ROOT, ".rsi-ship-expected.json");
|
||||
if (!dry) writeFileSync(tmp, JSON.stringify(plan.template));
|
||||
try {
|
||||
await writeVerified(node.source, r.source, `import { ${node.symbol} as T } from "./${node.source}";
|
||||
const want = JSON.parse(require("node:fs").readFileSync(${JSON.stringify(tmp)}, "utf-8"));
|
||||
if (T !== want) process.exit(1);`);
|
||||
} finally {
|
||||
if (!dry) Bun.spawnSync(["rm", "-f", tmp]);
|
||||
}
|
||||
console.log(`\n新增(${plan.added.length} 行${scope === "both" ? ",两端共用" : `,包在 {{#${scope}}} 段里`}):`);
|
||||
for (const l of plan.added) console.log(` + ${l}`);
|
||||
}
|
||||
|
||||
console.log(`\n建议的提交信息:\n\n[RSI] ${bundle.stage} 上线「${bundle.title}」(${label(scope)})\n\n复跑通过、管理员批准的 RSI 实验。\n\nRSI-Experiment: ${bundle.id}\n`);
|
||||
console.log("提交后照常部署(cpd / streamline-deploy);判定任务核对到运行中的代码带上了它,面板记为「已上线」。");
|
||||
@@ -0,0 +1,26 @@
|
||||
# 失败片段审计规约
|
||||
|
||||
分类只回答「当时为什么浪费」,不评价最终代码,也不因为输出里出现 `error` 就判失败。
|
||||
|
||||
- `dead_end`:这条路在当前环境客观走不通,后续仍重复撞同一堵墙。典型是权限、设备、
|
||||
版本或工具能力不满足;换措辞不能让这条路变通。
|
||||
- `decision_error`:在已有信息足够的情况下选错了下一步,或没有在失败后切换策略。
|
||||
反复手写 sleep / 轮询等待 App、原样重复同族命令、Frida 报错后直接再次注入都属于此类。
|
||||
- `context_missing`:做对下一步所需的信息在当时上下文中还没有出现;不是 agent 忽略了
|
||||
已知信息。必须指出缺什么,不能把「没有想到」改写成「缺上下文」。
|
||||
- `not_failure`:切片误报。读取源码、日志或历史记录时看到的 `error/failed` 字样;
|
||||
探测命令预期返回非零;后台任务启动回执;或者片段没有形成真实的重复失败。
|
||||
|
||||
`correctInfoStep` 是时间线中的调用编号:从该步结果开始,做对下一步所需的信息已经可见。
|
||||
确实从未出现时填 `null`。证据必须逐字引用时间线,至少一条,并足以支持分类。
|
||||
|
||||
`replayType` 只按可封闭判分的动作形态选择:
|
||||
|
||||
- `T2_wait`:问题核心是等待 App / UI / 进程就绪;下一步可判为 wait-for / 后台监听,
|
||||
或手写长 sleep / 轮询。
|
||||
- `T1_repeat`:第一次失败后是否再次执行同族命令。
|
||||
- `T3_frida`:Frida 首次失败后是否直接再次注入,而不是先诊断 server、版本、进程或代码。
|
||||
- `none`:不属于以上三种,不能进入自动离线闸。
|
||||
|
||||
`cluster` 要描述可重复出现的机制,不写工单名、账号、节点名或具体 id,例如
|
||||
`app_ready_blind_wait`、`repeat_same_failed_command`、`frida_retry_without_diagnosis`。
|
||||
@@ -0,0 +1,99 @@
|
||||
# 自动 RSI 回路进程
|
||||
|
||||
每天执行一次:
|
||||
|
||||
1. 从 `/api/rsi-loop/nodes/:id/transcripts` 同步近 14 天原生执行会话;
|
||||
2. 切出同一堵墙 / 同族命令重复失败至少两次的片段(读取类命令不算失败),以及所有「要开始等界面 / App 就绪」
|
||||
的时刻(Android 认 dumpsys / uiautomator / pidof / actl,iOS 认 catuse);
|
||||
3. 最多审计 60 段,分为 `dead_end / decision_error / context_missing / not_failure`;
|
||||
4. 按浪费的失败调用数选一个问题模式,在现行 prompt 上只插入一段新纪律;
|
||||
5. 目标点和其它模式保留集分别做配对重放,每点 base / candidate 各跑三遍;
|
||||
6. 提交实验(离线未过也提交),并给已结束实验补 AI 结论和每日运行摘要;
|
||||
7. 还空着的实验机(该平台没有进行中的 bug.run 实验),各提一个**旋钮实验**:从 `/state` 的
|
||||
`runtimeKnobs` 目录里挑该平台还没试过的取值(每个取值只试一次,跳过实验机现行的取值,
|
||||
见 `config.LAB_BASELINE`),不走离线、直接真机复跑(`rsi_loop/runtime.py`)。
|
||||
8. **复核意见挖掘**(`rsi_loop/review_mining.py`,与实验互不相干,回路暂停时照跑):给新的人工复核意见打标签;
|
||||
每周一(北京时间)把近 90 天的编码习惯归纳成候选规范交给控制面过门槛,管理员在 RSI 页「复核意见挖掘」批准。
|
||||
提示词由控制面下发,这里不改一个字;首次上线先补历史:`./run.sh review-mining --days 180`(每次最多 400 条,
|
||||
多跑几次直到待标为 0),再 `./run.sh review-mining --cluster --no-label` 立刻归纳一次。见 `docs/rsi.md` 例子 14。
|
||||
|
||||
离线通过的实验由控制面转入**真机复跑**:评估集里跑过的历史任务在实验机上现行 / 候选各跑一遍
|
||||
(见 `docs/rsi.md` 例子 13),判定由控制面的判定任务执行,复跑结论就是实验终点,不自动改生产
|
||||
prompt。线上灰度已于 2026-10-05 下线(新任务之间差别太大,比不出改动的效果)。所以本进程只提
|
||||
`bug.run`、且该平台配了实验机的问题模式(从 `/state` 的 `labNodes` 读)。
|
||||
|
||||
## temp-ios-01
|
||||
|
||||
代码只依赖系统 Python 3.9 标准库和 Node 24(actl MCP 用)。目录默认是
|
||||
`~/rsi_loop`,令牌默认读 `~/rsi_loop/token`:
|
||||
|
||||
```bash
|
||||
mkdir -p ~/rsi_loop
|
||||
chmod 700 ~/rsi_loop
|
||||
printf '%s' '<与控制面 RSI_LOOP_TOKEN 相同的值>' > ~/rsi_loop/token
|
||||
chmod 600 ~/rsi_loop/token
|
||||
|
||||
export PYTHONPATH=/path/to/streamline/scripts/rsi_loop
|
||||
export RSI_LOOP_HOME="$HOME/rsi_loop"
|
||||
# 不设 RSI_LOOP_NODES = 自动发现控制面登记的全部节点;没开放会话接口的节点(没配令牌 /
|
||||
# 卫星版本旧)返回 404,记进 sync.notEnabled 后跳过
|
||||
export RSI_LOOP_ACTL="$HOME/rsi_loop/actl/lib/index.js"
|
||||
```
|
||||
|
||||
先在图形桌面终端确认 `~/.local/bin/claude -p '只回答 OK'` 能用,然后**先试运行**:
|
||||
|
||||
```bash
|
||||
cd ~/rsi_loop/app
|
||||
./run.sh dry-run
|
||||
```
|
||||
|
||||
试运行对控制面只读:同步、切片、审计、提案、重放都真实执行,但提交实验和写时间线不发出去,
|
||||
改记到 `~/rsi_loop/data/dry_posts.jsonl`。看过要提交的实验(标题、四段记录、候选改动、离线结果)
|
||||
没问题,再正式跑一轮 —— 正式跑一旦离线闸通过,控制面就在**该问题所属平台**的实验机上建复跑任务:
|
||||
|
||||
```bash
|
||||
./run.sh once
|
||||
```
|
||||
|
||||
确认 RSI 页面出现运行日志后,在同一个图形桌面终端常驻:
|
||||
|
||||
```bash
|
||||
./run.sh daemon
|
||||
```
|
||||
|
||||
默认每天本机时间 03:00 运行,可用 `RSI_LOOP_DAILY_HOUR` 调整。不要从普通 SSH 会话
|
||||
启动:该机器的 Claude OAuth 在 SSH 环境里会过期。
|
||||
|
||||
常用参数:
|
||||
|
||||
```bash
|
||||
RSI_LOOP_MAX_AUDITS=60 # 每轮最多审计片段
|
||||
RSI_LOOP_TARGET_POINTS=36 # 目标类重放点
|
||||
RSI_LOOP_HOLDOUT_POINTS=15 # 其它模式保留集
|
||||
RSI_LOOP_SEEDS=3 # 每个点、每个版本跑三遍(两遍时区间太宽,打平的候选一半被误拦)
|
||||
RSI_LOOP_WORKERS=4 # 并发 Claude 续跑数
|
||||
RSI_LOOP_MAX_REPLAYS=320 # 单轮续跑硬上限;要 ≥ (目标点+保留集点)×2×遍数,否则先挤掉保留集
|
||||
```
|
||||
|
||||
## 安全与可重算性
|
||||
|
||||
- 原生会话、审计证据和重放原始输出只落在 `~/rsi_loop/data`,目录权限 `0700`、
|
||||
文件权限 `0600`,默认 14 天删除;发给控制面的日志只有聚合数字和模式名。
|
||||
- 重放只取模型第一步动作。所有工具由 `PreToolUse` 钩子拒绝,不执行设备、文件或网络工具。
|
||||
- 失败点截在工具结果之后;有未结束后台任务、缺环境附件或上下文过大的点不参与重放。
|
||||
- 候选新增行按提案给的锚点行插进历史会话第一条用户 prompt 的同一位置(锚点里的占位符按
|
||||
通配匹配,不加任何标题),与生产渲染的位置一致;锚点在某个点上定位不到,这个点两组都不跑,
|
||||
剔除数记在 `offline.meta`。base/candidate 使用同一历史前缀,判分只看封闭的动作形态,不用 LLM judge。
|
||||
- 保留集只取同一 stage、审计过且不是误报的失败点,加上同 stage 的等待点。
|
||||
- 不提交的情况:目标点不足 15 个(选题时先数,锚点剔除后再数一次),或重放报错率超过 20%
|
||||
(基础设施故障不能记成「候选无效」)。只写一条运行日志,并在本地记一次冷却。
|
||||
- 每次重放的逐条结果保存在 `data/runs/<时间>/results.jsonl`;95% 区间以固定种子对
|
||||
决策点做 paired bootstrap,可离线重算。
|
||||
- 暂停时仍可同步、审计和补结论,但不会提交新实验;控制面也会以 409 再挡一次。
|
||||
|
||||
本地测试(不访问控制面、不调用 Claude):
|
||||
|
||||
```bash
|
||||
cd scripts/rsi_loop
|
||||
python3 -m unittest discover -s tests -v
|
||||
```
|
||||
@@ -0,0 +1,3 @@
|
||||
"""Streamline 自动 RSI 回路进程(Python 3.9 标准库)。"""
|
||||
|
||||
__version__ = "0.1.0"
|
||||
@@ -0,0 +1,27 @@
|
||||
import argparse
|
||||
import json
|
||||
|
||||
from . import runner
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Streamline 自动 RSI 回路")
|
||||
parser.add_argument("command", choices=("once", "dry-run", "daemon", "review-mining"), nargs="?", default="once")
|
||||
args, rest = parser.parse_known_args()
|
||||
if args.command == "dry-run":
|
||||
from . import config
|
||||
config.DRY_RUN = True
|
||||
print(json.dumps(runner.locked_run(), ensure_ascii=False, indent=2))
|
||||
elif args.command == "daemon":
|
||||
runner.daemon()
|
||||
elif args.command == "review-mining":
|
||||
# 单独跑挖掘(补标历史 / 立刻归纳一次),参数见 review_mining.py 头注释
|
||||
from . import config, review_mining
|
||||
config.ensure_dirs()
|
||||
print(json.dumps(review_mining.main(rest), ensure_ascii=False, indent=2))
|
||||
else:
|
||||
print(json.dumps(runner.locked_run(), ensure_ascii=False, indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,78 @@
|
||||
"""控制面 /api/rsi-loop/* 的客户端(回路令牌鉴权)。
|
||||
|
||||
回路进程只能读状态、取会话记录、提交实验、写时间线;上线 / 回滚 / revert 由控制面
|
||||
的判定任务决定,这里没有、也调不到那些动作。
|
||||
"""
|
||||
import json
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from typing import Any, Optional
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
def _req(method: str, path: str, body: Optional[dict] = None, timeout: int = 60):
|
||||
data = json.dumps(body, ensure_ascii=False).encode() if body is not None else None
|
||||
req = urllib.request.Request(config.API + path, data=data, method=method)
|
||||
req.add_header("x-rsi-loop-token", config.token())
|
||||
if data is not None:
|
||||
req.add_header("content-type", "application/json")
|
||||
return urllib.request.urlopen(req, timeout=timeout)
|
||||
|
||||
|
||||
def get(path: str, timeout: int = 60) -> Any:
|
||||
with _req("GET", path, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode())
|
||||
|
||||
|
||||
def post(path: str, body: dict, timeout: int = 120) -> Any:
|
||||
if config.DRY_RUN:
|
||||
# 试运行:不写生产,把本来要发的内容留在本地供检查
|
||||
out = config.DATA / "dry_posts.jsonl"
|
||||
with open(out, "a") as f:
|
||||
f.write(json.dumps({"path": path, "body": body}, ensure_ascii=False) + "\n")
|
||||
out.chmod(0o600)
|
||||
print(f"[rsi-loop] dry-run: POST {path} 未发送,已记到 {out}", flush=True)
|
||||
if path == "/experiments":
|
||||
return {"experiment": {"id": None, "title": body.get("title"), "status": "dry_run"}, "gate": None}
|
||||
return {}
|
||||
try:
|
||||
with _req("POST", path, body, timeout=timeout) as r:
|
||||
return json.loads(r.read().decode())
|
||||
except urllib.error.HTTPError as e:
|
||||
detail = e.read().decode(errors="replace")[:500]
|
||||
raise RuntimeError(f"POST {path} -> HTTP {e.code}: {detail}") from None
|
||||
|
||||
|
||||
def download(path: str, dest: Path, timeout: int = 900) -> int:
|
||||
tmp = dest.with_suffix(dest.suffix + ".part")
|
||||
with _req("GET", path, timeout=timeout) as r, open(tmp, "wb") as f:
|
||||
n = 0
|
||||
while True:
|
||||
chunk = r.read(1 << 20)
|
||||
if not chunk:
|
||||
break
|
||||
f.write(chunk)
|
||||
n += len(chunk)
|
||||
tmp.replace(dest)
|
||||
return n
|
||||
|
||||
|
||||
def event(title: str, body: str = "", type_: str = "run") -> bool:
|
||||
"""回路层面的时间线事件(RSI 页面「回路日志」)。失败不影响主流程。"""
|
||||
try:
|
||||
post("/events", {"type": type_, "title": title[:280], "body": body[:20000]})
|
||||
return True
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"[rsi-loop] event post failed: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def experiment_event(exp_id: str, title: str, body: str = "", type_: str = "note") -> bool:
|
||||
try:
|
||||
post(f"/experiments/{exp_id}/events", {"type": type_, "title": title[:280], "body": body[:12000]})
|
||||
return True
|
||||
except Exception as e: # noqa: BLE001
|
||||
print(f"[rsi-loop] experiment event failed: {e}")
|
||||
return False
|
||||
@@ -0,0 +1,91 @@
|
||||
"""回路进程的配置 —— 全部来自环境变量,缺省值对应 temp-ios-01 上的布局。
|
||||
|
||||
只依赖 Python 3.9 标准库:temp-ios-01 上只有系统自带的 python3,不装任何东西。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
HOME = Path.home()
|
||||
ROOT = Path(os.environ.get("RSI_LOOP_HOME", str(HOME / "rsi_loop")))
|
||||
DATA = ROOT / "data"
|
||||
TX_DIR = DATA / "tx" # 原生会话记录(按节点分目录),保留 RETAIN_DAYS 天
|
||||
STATE = DATA / "state.json" # 每个节点上次同步到的时刻等
|
||||
EPISODES = DATA / "episodes.jsonl" # 切出来的多轮失败片段(按会话文件缓存)
|
||||
POINTS = DATA / "points.jsonl" # 可重放的决策点(失败片段 + 等待起点)
|
||||
AUDITS = DATA / "audits.jsonl" # 审计结论
|
||||
RUNS = DATA / "runs" # 每次重放的原始结果,留档可重算
|
||||
LOCK = DATA / "runner.lock"
|
||||
|
||||
API = os.environ.get("RSI_LOOP_API", "https://streamline-internal-api.bytedance.net/api/rsi-loop")
|
||||
TOKEN_FILE = Path(os.environ.get("RSI_LOOP_TOKEN_FILE", str(ROOT / "token")))
|
||||
|
||||
# 数据源节点(逗号分隔)。不设 = 自动发现控制面登记的全部节点,逐个尝试;只有 .env 里配了
|
||||
# RSI_LOOP_TOKEN、且跑到带会话记录接口的卫星版本的节点才会开放,其余返回 404,静默跳过。
|
||||
NODES = [n.strip() for n in os.environ.get("RSI_LOOP_NODES", "").split(",") if n.strip()]
|
||||
|
||||
CLAUDE_BIN = os.environ.get("CLAUDE_BIN", str(HOME / ".local" / "bin" / "claude"))
|
||||
# 重放必须用会话当时的模型(否则 thinking 签名对不上);审计 / 提案 / 写结论用同一个即可。
|
||||
REPLAY_MODEL = os.environ.get("RSI_LOOP_REPLAY_MODEL", "claude-opus-5[1m]")
|
||||
THINK_MODEL = os.environ.get("RSI_LOOP_MODEL", "claude-opus-5")
|
||||
# actl 设备工具的 MCP 服务(只为让重放时的工具定义与节点一致;调用全部被钩子拦下)
|
||||
ACTL_INDEX = os.environ.get("RSI_LOOP_ACTL", str(ROOT / "actl" / "lib" / "index.js"))
|
||||
|
||||
RETAIN_DAYS = int(os.environ.get("RSI_LOOP_RETAIN_DAYS", "14"))
|
||||
SYNC_DAYS = int(os.environ.get("RSI_LOOP_SYNC_DAYS", "14"))
|
||||
# 2026-10-08 由 40 调到 60:每天新片段约 40 段,40 的上限让积压一直消不掉(当时积压 41 段)。
|
||||
MAX_AUDITS_PER_RUN = int(os.environ.get("RSI_LOOP_MAX_AUDITS", "60"))
|
||||
# 一次重放的总调用数上限:(目标 36 + 保留集 15) × 两组 × 3 遍 = 306,留一点余量。
|
||||
# 上限不够时先挤掉保留集 —— 上限和点数、遍数要一起改。
|
||||
MAX_REPLAYS_PER_RUN = int(os.environ.get("RSI_LOOP_MAX_REPLAYS", "320"))
|
||||
WORKERS = int(os.environ.get("RSI_LOOP_WORKERS", "4"))
|
||||
# 每个点每组跑几遍。2026-10-03 用 24 点 × 2 遍时差值区间半宽约 ±0.11,与现行打平的候选
|
||||
# 有一半会被「不能明显变差」(下界 ≥ −0.10)误拦;36 点 × 3 遍把半宽收到约 ±0.08。
|
||||
SEEDS = int(os.environ.get("RSI_LOOP_SEEDS", "3"))
|
||||
# 重放成本控制:截断处上下文超过这个 token 数的决策点不用
|
||||
MAX_POINT_CTX = int(os.environ.get("RSI_LOOP_MAX_POINT_CTX", "250000"))
|
||||
TARGET_POINTS = int(os.environ.get("RSI_LOOP_TARGET_POINTS", "36"))
|
||||
HOLDOUT_POINTS = int(os.environ.get("RSI_LOOP_HOLDOUT_POINTS", "15"))
|
||||
# 离线闸(与 shared/src/rsi_loop.ts 的 LOOP 一致;控制面会再按它的规则复核一遍)
|
||||
OFFLINE_MIN_POINTS = 15
|
||||
OFFLINE_HOLDOUT_TOLERANCE = 0.10
|
||||
# 同一类问题被试过之后的冷却期(天):避免对同一个模式反复提几乎一样的改法
|
||||
CLUSTER_COOLDOWN_DAYS = int(os.environ.get("RSI_LOOP_COOLDOWN_DAYS", "7"))
|
||||
# 旋钮实验:实验机上现行组已经是这个取值的旋钮,不能再当候选(两组一样,白跑一轮)。
|
||||
# 实验机不传 --effort 时用的是各自 ~/.claude/settings.json 的 modelSettings.<模型>.effortLevel:
|
||||
# evaluator-android-01 是 xhigh(2026-10-08 查),temp-ios-01 没设(模型默认)。实验机配置改了要同步这里。
|
||||
LAB_BASELINE = json.loads(os.environ.get("RSI_LOOP_LAB_BASELINE", '{"android": {"effort": "xhigh"}}'))
|
||||
# 思考强度的候选顺序:先试离现状近、省时间的方向。「不设」对应的模型默认档未知,放在最后。
|
||||
EFFORT_ORDER = ["medium", "high", "low", "max", "xhigh"]
|
||||
# 试运行:对控制面只读,所有写操作(提交实验、写时间线)改记到 data/dry_posts.jsonl
|
||||
DRY_RUN = os.environ.get("RSI_LOOP_DRY_RUN") == "1"
|
||||
# 每天几点(本机时间)跑一次
|
||||
DAILY_HOUR = int(os.environ.get("RSI_LOOP_DAILY_HOUR", "3"))
|
||||
|
||||
|
||||
def log(msg: str) -> None:
|
||||
"""阶段进度。整轮要一两个小时,不出声的话没法分辨「在干活」还是「卡死了」。"""
|
||||
import time as _t
|
||||
print(f"[rsi-loop {_t.strftime('%H:%M:%S')}] {msg}", flush=True)
|
||||
|
||||
|
||||
def child_env(cwd: str) -> dict:
|
||||
"""给 claude 子进程的环境:PWD 指向它自己的工作目录。
|
||||
|
||||
Claude Code 会参考 PWD 确定工作目录;继承回路进程的 PWD(…/scripts/rsi_loop)会让模型在
|
||||
重放里看到「rsi_loop」,第一步就去查它。回路自己的变量、PYTHONPATH 也一并去掉。"""
|
||||
drop = ("PWD", "OLDPWD", "PYTHONPATH")
|
||||
env = {k: v for k, v in os.environ.items()
|
||||
if not k.startswith(("CLAUDE", "RSI_LOOP")) and k not in drop}
|
||||
env["PWD"] = cwd
|
||||
return env
|
||||
|
||||
|
||||
def token() -> str:
|
||||
return TOKEN_FILE.read_text().strip()
|
||||
|
||||
|
||||
def ensure_dirs() -> None:
|
||||
for d in (DATA, TX_DIR, RUNS):
|
||||
d.mkdir(parents=True, exist_ok=True)
|
||||
d.chmod(0o700)
|
||||
@@ -0,0 +1,92 @@
|
||||
"""把一份执行会话切成「多轮失败片段」:同一堵墙、或同一条路连续失败 ≥2 次的一段。
|
||||
每个片段标出能用来做决策点重放的截断位置(片段内第一次失败的工具结果之后)。
|
||||
|
||||
片段同时产出一个候选决策点:
|
||||
- 撞墙涉及 frida → T3_frida
|
||||
- 下一步是等待类命令 → T2_wait(改成挑「所有要开始等待的时刻」更干净,见 harvest)
|
||||
- 其余同族连续失败 → T1_repeat
|
||||
合格的决策点要求:截断时没有在跑的后台任务、会话里有环境附件(能中和)、上下文不超限。
|
||||
"""
|
||||
import re
|
||||
from typing import List, Optional
|
||||
|
||||
from . import transcript as T
|
||||
|
||||
# UI 相关的等待(dumpsys/uiautomator/pidof/actl 等),T2 的判定基准
|
||||
# 「等 App / 界面就绪」的探查命令。Android:dumpsys / uiautomator / pidof / actl;iOS:catuse(界面大纲、截屏、
|
||||
# 进程列表都走它)。2026-10-08 之前只认 Android,iOS 的 104 段 bug.run 会话一个等待点都抽不出来。
|
||||
UI_LIKE = re.compile(r"dumpsys\s+(window|activity)|uiautomator|mCurrentFocus|ResumedActivity|\bpidof\b|\bactl\b|\bcatuse\b")
|
||||
|
||||
|
||||
def find_episodes(rows: List[Optional[dict]]) -> List[dict]:
|
||||
cs = T.calls(rows)
|
||||
n = len(cs)
|
||||
eps: List[dict] = []
|
||||
|
||||
# 按「墙」聚合:同一堵墙撞 ≥2 次
|
||||
by_wall: dict = {}
|
||||
for c in cs:
|
||||
if c["wall"]:
|
||||
by_wall.setdefault(c["wall"], []).append(c)
|
||||
for w, hits in by_wall.items():
|
||||
if len(hits) < 2:
|
||||
continue
|
||||
fams = sorted({h["fam"] for h in hits})
|
||||
ptype = "T3_frida" if any("frida" in f for f in fams) else "T1_repeat"
|
||||
eps.append(_episode(cs, hits, label=f"wall:{w}", ptype=ptype, total=n))
|
||||
|
||||
# 按命令族聚合:同族连续失败 ≥2 次(排除只读命令的常态失败)
|
||||
open_ep: dict = {}
|
||||
for c in cs:
|
||||
if c["bg"] or c["read_only"]:
|
||||
continue
|
||||
f = c["fam"]
|
||||
if not c["ok"]:
|
||||
open_ep.setdefault(f, []).append(c)
|
||||
elif f in open_ep:
|
||||
hits = open_ep.pop(f)
|
||||
if len(hits) >= 2:
|
||||
ptype = "T3_frida" if "frida" in f else "T1_repeat"
|
||||
eps.append(_episode(cs, hits, label=f"path:{f}", ptype=ptype, total=n, resolved_at=c["i"]))
|
||||
for f, hits in open_ep.items():
|
||||
if len(hits) >= 2:
|
||||
ptype = "T3_frida" if "frida" in f else "T1_repeat"
|
||||
eps.append(_episode(cs, hits, label=f"path:{f}", ptype=ptype, total=n))
|
||||
# 同一组失败常会同时命中 wall 与 path;只审计一次,否则浪费会被双算。
|
||||
unique = {}
|
||||
for ep in eps:
|
||||
unique.setdefault((ep["first_call"], ep["last_call"], ep["ptype"]), ep)
|
||||
return list(unique.values())
|
||||
|
||||
|
||||
def _episode(cs: List[dict], hits: List[dict], label: str, ptype: str, total: int, resolved_at: Optional[int] = None) -> dict:
|
||||
first, last = hits[0], hits[-1]
|
||||
return dict(label=label, ptype=ptype, n_fail=len(hits), first_call=first["i"], last_call=last["i"],
|
||||
total_calls=total, fam=first["fam"], err=first["err"], err_last=last["err"],
|
||||
fail_cmds=[h["cmd"][:240] for h in hits[:4]], resolved=resolved_at is not None,
|
||||
# 决策点:截在第一次失败的工具结果之后(含),让模型面对「刚撞了一次墙」。
|
||||
# 截在前一个结果会让模型尚未见到失败,无法判定下一步是否在重复撞墙。
|
||||
point_call=first["i"], point_row=first["row"], point_ctx=first["ctx"])
|
||||
|
||||
|
||||
def harvest_wait_points(rows: List[Optional[dict]]) -> List[dict]:
|
||||
"""T2_wait 不按结果挑,而是挑所有「要开始等待」的时刻 —— 下一步该用 wait-for 还是手写 sleep,
|
||||
由重放决定,这样历史比例才能拿来和重放对照。"""
|
||||
cs = T.calls(rows)
|
||||
out: List[dict] = []
|
||||
last_result_call: Optional[dict] = None
|
||||
for c in cs:
|
||||
cmd = c["cmd"]
|
||||
if c["tool"] == "Bash" and (("wait-for" in cmd) or _is_wait(cmd)) and UI_LIKE.search(cmd) and last_result_call is not None:
|
||||
out.append(dict(label="wait", ptype="T2_wait", first_call=c["i"], last_call=c["i"], total_calls=len(cs),
|
||||
fam=c["fam"], err="", fail_cmds=[cmd[:240]], resolved=True,
|
||||
point_call=last_result_call["i"], point_row=last_result_call["row"], point_ctx=last_result_call["ctx"]))
|
||||
last_result_call = c
|
||||
return out
|
||||
|
||||
|
||||
def _is_wait(cmd: str) -> bool:
|
||||
if re.search(r"\b(while|until|for)\b.*\bsleep\b", cmd, re.S):
|
||||
return True
|
||||
return any(int(x) >= 15 for x in re.findall(r"\bsleep\s+(\d+)", cmd))
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
"""调 claude 做「读材料、写判断」的活:审计分类、写提案、写结论。
|
||||
|
||||
全部是纯文本进、纯文本出:`--tools ""` 关掉所有工具,`--setting-sources ""` 不加载本机
|
||||
任何设置,材料从标准输入喂进去。它不碰任何系统,只产出要被下游(控制面的规则)检验的东西。
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
from typing import Any, Optional
|
||||
|
||||
from . import config
|
||||
|
||||
|
||||
def ask(prompt: str, timeout: int = 900, model: Optional[str] = None) -> str:
|
||||
env = config.child_env("/tmp")
|
||||
cmd = [config.CLAUDE_BIN, "-p", "--output-format", "json", "--model", model or config.THINK_MODEL,
|
||||
"--tools", "", "--setting-sources", "", "--strict-mcp-config"]
|
||||
p = subprocess.run(cmd, input=prompt, capture_output=True, text=True, timeout=timeout, env=env, cwd="/tmp")
|
||||
try:
|
||||
d = json.loads(p.stdout)
|
||||
except json.JSONDecodeError:
|
||||
raise RuntimeError(f"claude 输出不是 JSON: {p.stdout[:300]} {p.stderr[:300]}") from None
|
||||
if d.get("is_error"):
|
||||
raise RuntimeError(f"claude 报错: {str(d.get('result'))[:300]}")
|
||||
return str(d.get("result") or "")
|
||||
|
||||
|
||||
def ask_json(prompt: str, timeout: int = 900) -> Any:
|
||||
"""要求模型只回一个 JSON 对象;用 JSONDecoder 取第一个完整对象(允许嵌套)。"""
|
||||
text = ask(prompt, timeout=timeout)
|
||||
fenced = re.search(r"```(?:json)?\s*([\s\S]*?)```", text)
|
||||
candidates = [fenced.group(1)] if fenced else []
|
||||
candidates.append(text)
|
||||
decoder = json.JSONDecoder()
|
||||
for candidate in candidates:
|
||||
for match in re.finditer(r"\{", candidate):
|
||||
try:
|
||||
value, _ = decoder.raw_decode(candidate[match.start():])
|
||||
return value
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
raise RuntimeError(f"回复里没有合法 JSON: {text[:400]}") from None
|
||||
@@ -0,0 +1,511 @@
|
||||
"""发现:同步会话记录 → 切出多轮失败片段 → 审计分类 → 按问题模式聚类。
|
||||
|
||||
切片只是候选:自动从输出文本判断「失败」有误报(读到的源码里的报错字样等),所以每个
|
||||
片段都要过一遍审计。审计用模型,但它只产出「假设」—— 问题属于哪一类、哪一步之后本该
|
||||
做对 —— 真正决定改动去留的是离线重放与真机复跑的配对结果,不是这里的判断。
|
||||
"""
|
||||
import json
|
||||
import hashlib
|
||||
import urllib.error
|
||||
import os
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Iterable, List, Optional
|
||||
from urllib.parse import quote
|
||||
|
||||
from . import api, config, episodes, llm
|
||||
from .transcript import (
|
||||
classify_stage, first_prompt, has_env_attachment, load_rows, pending_bg,
|
||||
render_timeline, text_of,
|
||||
)
|
||||
|
||||
RUBRIC = (Path(__file__).resolve().parent.parent / "AUDIT_RUBRIC.md")
|
||||
|
||||
|
||||
# ── 同步 ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
def _state() -> dict:
|
||||
try:
|
||||
value = json.loads(config.STATE.read_text())
|
||||
return value if isinstance(value, dict) else {}
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
|
||||
def _write_json(path: Path, value: Any) -> None:
|
||||
tmp = path.with_suffix(path.suffix + ".tmp")
|
||||
tmp.write_text(json.dumps(value, ensure_ascii=False, indent=2))
|
||||
os.chmod(tmp, 0o600)
|
||||
tmp.replace(path)
|
||||
|
||||
|
||||
def _write_jsonl(path: Path, rows: Iterable[dict]) -> None:
|
||||
tmp = path.with_suffix(path.suffix + ".tmp")
|
||||
with open(tmp, "w") as f:
|
||||
for row in rows:
|
||||
f.write(json.dumps(row, ensure_ascii=False, separators=(",", ":")) + "\n")
|
||||
os.chmod(tmp, 0o600)
|
||||
tmp.replace(path)
|
||||
|
||||
|
||||
def _read_jsonl(path: Path) -> List[dict]:
|
||||
out: List[dict] = []
|
||||
try:
|
||||
with open(path, errors="replace") as f:
|
||||
for line in f:
|
||||
try:
|
||||
row = json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
if isinstance(row, dict):
|
||||
out.append(row)
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
return out
|
||||
|
||||
|
||||
def _id(*parts: object) -> str:
|
||||
raw = "\0".join(str(p) for p in parts).encode()
|
||||
return hashlib.sha256(raw).hexdigest()[:24]
|
||||
|
||||
|
||||
def _local_file(node: str, name: str) -> Path:
|
||||
return config.TX_DIR / quote(node, safe="._-") / name
|
||||
|
||||
|
||||
def _purge_expired(now_ms: int) -> int:
|
||||
cutoff = now_ms - config.RETAIN_DAYS * 86_400_000
|
||||
removed = 0
|
||||
for node_dir in config.TX_DIR.glob("*"):
|
||||
if not node_dir.is_dir():
|
||||
continue
|
||||
for meta_path in node_dir.glob("*.meta.json"):
|
||||
try:
|
||||
meta = json.loads(meta_path.read_text())
|
||||
expired = int(meta.get("mtime") or 0) < cutoff
|
||||
except (OSError, ValueError, json.JSONDecodeError):
|
||||
expired = True
|
||||
if not expired:
|
||||
continue
|
||||
raw = meta_path.with_name(meta_path.name.removesuffix(".meta.json") + ".jsonl")
|
||||
raw.unlink(missing_ok=True)
|
||||
meta_path.unlink(missing_ok=True)
|
||||
removed += 1
|
||||
try:
|
||||
node_dir.rmdir()
|
||||
except OSError:
|
||||
pass
|
||||
return removed
|
||||
|
||||
|
||||
def sync_transcripts(now_ms: Optional[int] = None) -> dict:
|
||||
"""只同步最近 14 天的执行会话;同 size/mtime 的文件不重复下载。"""
|
||||
config.ensure_dirs()
|
||||
now_ms = now_ms or int(time.time() * 1000)
|
||||
since = now_ms - config.SYNC_DAYS * 86_400_000
|
||||
stats = {"nodes": 0, "notEnabled": [], "listed": 0, "downloaded": 0, "bytes": 0, "errors": [], "purged": 0}
|
||||
state = _state()
|
||||
platforms = node_platforms()
|
||||
nodes = config.NODES or sorted(platforms)
|
||||
for node in nodes:
|
||||
try:
|
||||
try:
|
||||
listing = api.get(f"/nodes/{quote(node, safe='')}/transcripts?sinceMs={since}", timeout=900)
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code == 404: # 没配令牌 / 卫星版本旧:不是故障,记下来提示人去开
|
||||
stats["notEnabled"].append(node)
|
||||
continue
|
||||
raise
|
||||
files = listing.get("files") or []
|
||||
config.log(f"同步 {node}({platforms.get(node, '?')}):列出 {len(files)} 份会话")
|
||||
stats["nodes"] += 1
|
||||
stats["listed"] += len(files)
|
||||
node_dir = _local_file(node, "_").parent
|
||||
node_dir.mkdir(parents=True, exist_ok=True)
|
||||
node_dir.chmod(0o700)
|
||||
for info in files:
|
||||
name = str(info.get("file") or "")
|
||||
if not name.endswith(".jsonl"):
|
||||
continue
|
||||
dest = _local_file(node, name)
|
||||
meta_path = dest.with_name(dest.stem + ".meta.json")
|
||||
old = {}
|
||||
try:
|
||||
old = json.loads(meta_path.read_text())
|
||||
except (OSError, json.JSONDecodeError):
|
||||
pass
|
||||
if (dest.exists() and int(old.get("size") or -1) == int(info.get("size") or -2)
|
||||
and int(old.get("mtime") or -1) == int(info.get("mtime") or -2)):
|
||||
if not old.get("platform") and platforms.get(node):
|
||||
_write_json(meta_path, {**old, "platform": platforms[node]})
|
||||
continue
|
||||
# 大文件经控制面代理偶尔中途断开(IncompleteRead);逐个文件重试,
|
||||
# 一份失败不能连累这个节点后面所有文件。
|
||||
n = None
|
||||
for attempt in range(3):
|
||||
try:
|
||||
n = api.download(f"/nodes/{quote(node, safe='')}/transcripts/{quote(name, safe='')}", dest)
|
||||
break
|
||||
except Exception as exc: # noqa: BLE001
|
||||
last = exc
|
||||
time.sleep(5 * (attempt + 1))
|
||||
if n is None:
|
||||
stats["errors"].append(f"{node}/{name}: {last}")
|
||||
continue
|
||||
os.chmod(dest, 0o600)
|
||||
meta = {
|
||||
"node": node, "file": name, "size": n,
|
||||
"mtime": int(info.get("mtime") or now_ms),
|
||||
"stage": info.get("stage"), "workspaceKey": info.get("workspaceKey"),
|
||||
"platform": platforms.get(node) or platform_of(node),
|
||||
}
|
||||
_write_json(meta_path, meta)
|
||||
stats["downloaded"] += 1
|
||||
stats["bytes"] += n
|
||||
config.log(f" 下载 {stats['downloaded']}:{name[:8]} {n / 1e6:.1f}MB")
|
||||
state.setdefault("nodes", {})[node] = {"syncedAt": now_ms, "sinceMs": since}
|
||||
except Exception as exc: # noqa: BLE001
|
||||
stats["errors"].append(f"{node}: {exc}")
|
||||
stats["purged"] = _purge_expired(now_ms)
|
||||
state["lastSyncAt"] = now_ms
|
||||
_write_json(config.STATE, state)
|
||||
return stats
|
||||
|
||||
|
||||
def platform_of(node: str) -> str:
|
||||
"""节点名里带 ios 的是 iOS,其余按 Android(只在控制面没返回平台时兜底)。"""
|
||||
return "ios" if "ios" in (node or "").lower() else "android"
|
||||
|
||||
|
||||
def node_platforms() -> Dict[str, str]:
|
||||
"""控制面登记的节点 → 平台。"""
|
||||
try:
|
||||
return {n["id"]: n.get("platform") or platform_of(n["id"]) for n in api.get("/nodes").get("nodes") or []}
|
||||
except Exception as exc: # noqa: BLE001
|
||||
config.log(f"取节点列表失败,只用 RSI_LOOP_NODES:{exc}")
|
||||
return {n: platform_of(n) for n in config.NODES}
|
||||
|
||||
|
||||
def transcript_files() -> List[tuple]:
|
||||
out = []
|
||||
for meta_path in config.TX_DIR.glob("*/*.meta.json"):
|
||||
try:
|
||||
meta = json.loads(meta_path.read_text())
|
||||
except (OSError, json.JSONDecodeError):
|
||||
continue
|
||||
raw = meta_path.with_name(meta_path.name.removesuffix(".meta.json") + ".jsonl")
|
||||
if raw.exists():
|
||||
out.append((raw, meta))
|
||||
return out
|
||||
|
||||
|
||||
def mine_local() -> dict:
|
||||
"""重建最近会话的片段和决策点缓存。缓存不含完整工单正文。"""
|
||||
eps: List[dict] = []
|
||||
points: List[dict] = []
|
||||
bad_files = 0
|
||||
for path, meta in transcript_files():
|
||||
# 真机复跑任务(控制面 rsi_lab.ts,工作区名带 rsi-lab- 前缀)跑在实验机上,也会出现在数据源
|
||||
# 节点的会话列表里;它们是回路自己的实验,不能再被当成生产样本挖回来
|
||||
if str(meta.get("workspaceKey") or "").startswith("rsi-lab-"):
|
||||
continue
|
||||
rows = load_rows(path)
|
||||
prompt = first_prompt(rows)
|
||||
stage = classify_stage(text_of((prompt or {}).get("message", {}).get("content"))) if prompt else None
|
||||
if not stage:
|
||||
bad_files += 1
|
||||
continue
|
||||
common = {
|
||||
"node": meta.get("node"), "file": meta.get("file"), "mtime": meta.get("mtime"),
|
||||
"platform": meta.get("platform") or platform_of(str(meta.get("node"))),
|
||||
"stage": stage, "has_env": has_env_attachment(rows),
|
||||
}
|
||||
for ep in episodes.find_episodes(rows):
|
||||
item = {**common, **ep}
|
||||
item["id"] = _id(item["node"], item["file"], item["label"], item["first_call"], item["last_call"])
|
||||
eps.append(item)
|
||||
point = _point_from_episode(item, rows)
|
||||
if point:
|
||||
points.append(point)
|
||||
for wp in episodes.harvest_wait_points(rows):
|
||||
item = {**common, **wp}
|
||||
item["id"] = _id(item["node"], item["file"], "wait", item["point_row"])
|
||||
point = _point_from_episode(item, rows)
|
||||
if point:
|
||||
points.append(point)
|
||||
# 一份会话里的 wall/path 切片可能指向同一个截断点;重放只保留一个。
|
||||
point_map = {(p["node"], p["file"], p["point_row"], p["ptype"]): p for p in points}
|
||||
eps.sort(key=lambda x: (int(x.get("mtime") or 0), x["id"]), reverse=True)
|
||||
points = sorted(point_map.values(), key=lambda x: (int(x.get("mtime") or 0), x["id"]), reverse=True)
|
||||
_write_jsonl(config.EPISODES, eps)
|
||||
_write_jsonl(config.POINTS, points)
|
||||
config.log(f"切片完成:{len(eps)} 段失败片段,{len(points)} 个可重放的决策点")
|
||||
return {"episodes": len(eps), "points": len(points), "badFiles": bad_files}
|
||||
|
||||
|
||||
def _point_from_episode(item: dict, rows: List[Optional[dict]]) -> Optional[dict]:
|
||||
row = int(item.get("point_row") or 0)
|
||||
if not item.get("has_env") or int(item.get("point_ctx") or 0) > config.MAX_POINT_CTX:
|
||||
return None
|
||||
if pending_bg(rows, row):
|
||||
return None
|
||||
return {
|
||||
"id": _id(item["node"], item["file"], item["ptype"], row),
|
||||
"episodeId": item["id"], "node": item["node"], "file": item["file"],
|
||||
"mtime": item["mtime"], "stage": item["stage"], "platform": item.get("platform"), "ptype": item["ptype"],
|
||||
"fam": item.get("fam") or "", "point_row": row, "point_call": item.get("point_call"),
|
||||
"point_ctx": item.get("point_ctx") or 0,
|
||||
}
|
||||
|
||||
|
||||
# ── 问题模式的规范名 ─────────────────────────────────────────────────────────
|
||||
#
|
||||
# 审计给模式起名是自由发挥的:同一机制被起成不同名字,2026-10-02 那轮 40 段审计聚出 30 个模式,
|
||||
# 几乎每个只有一段 —— 「重复撞墙」「Frida」这几类永远凑不够离线闸要的 15 个决策点。两道补救:
|
||||
# 审计时把已有模式交给模型、要求复用;每轮审计后再做一次归并,写进别名表(旧名 → 规范名)。
|
||||
|
||||
ALIASES = "cluster_aliases.json"
|
||||
|
||||
|
||||
def _aliases() -> Dict[str, str]:
|
||||
try:
|
||||
value = json.loads((config.DATA / ALIASES).read_text())
|
||||
return value if isinstance(value, dict) else {}
|
||||
except (OSError, json.JSONDecodeError):
|
||||
return {}
|
||||
|
||||
|
||||
def canon(name: Optional[str], aliases: Optional[Dict[str, str]] = None) -> str:
|
||||
"""沿别名表走到规范名;有环就停在环上第一次重复之前。"""
|
||||
table = _aliases() if aliases is None else aliases
|
||||
cur, seen = str(name or ""), set()
|
||||
while cur in table and cur not in seen:
|
||||
seen.add(cur)
|
||||
cur = str(table[cur])
|
||||
return cur
|
||||
|
||||
|
||||
def known_clusters(limit: int = 60) -> List[dict]:
|
||||
"""已有的问题模式(按规范名合并),按出现次数排序 —— 审计时交给模型复用。"""
|
||||
aliases = _aliases()
|
||||
agg: Dict[str, dict] = {}
|
||||
for a in _read_jsonl(config.AUDITS):
|
||||
if a.get("classification") == "not_failure" or not a.get("cluster"):
|
||||
continue
|
||||
name = canon(a["cluster"], aliases)
|
||||
slot = agg.setdefault(name, {"cluster": name, "titles": {}, "count": 0,
|
||||
"classification": a.get("classification"), "replayType": a.get("replayType")})
|
||||
slot["count"] += 1
|
||||
t = str(a.get("clusterTitle") or "")
|
||||
slot["titles"][t] = slot["titles"].get(t, 0) + 1
|
||||
out = []
|
||||
for slot in agg.values():
|
||||
slot["clusterTitle"] = max(slot.pop("titles").items(), key=lambda kv: kv[1])[0]
|
||||
out.append(slot)
|
||||
return sorted(out, key=lambda x: -x["count"])[:limit]
|
||||
|
||||
|
||||
def _known_block(known: List[dict]) -> str:
|
||||
if not known:
|
||||
return "(还没有已知模式,按机制新建一个 snake_case 名字)"
|
||||
lines = ["| cluster | 中文名 | 类别 | 重放型 | 已出现 |", "|---|---|---|---|---|"]
|
||||
lines += [f"| {k['cluster']} | {k['clusterTitle']} | {k['classification']} | {k['replayType']} | {k['count']} |" for k in known]
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def consolidate_clusters() -> dict:
|
||||
"""把同一机制的不同名字归并到一个规范名,写进别名表。只合并重放型相同的;规范名必须是已有名字。"""
|
||||
known = known_clusters(limit=200)
|
||||
if len(known) < 2:
|
||||
return {"clusters": len(known), "merged": 0}
|
||||
prompt = f"""下面是 coding agent 执行失败审计出来的「问题模式」,名字是逐段审计时各自起的,同一机制常被起成不同名字。
|
||||
请把**同一机制**的名字归并到其中一个**已有**名字(规范名)上:只合并重放型相同的;拿不准就不合并。
|
||||
|
||||
{_known_block(known)}
|
||||
|
||||
只返回 JSON:{{"merge": {{"<要并掉的名字>": "<规范名>", ...}}}}。没有要合并的就返回 {{"merge": {{}}}}。"""
|
||||
try:
|
||||
value = llm.ask_json(prompt, timeout=900)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
config.log(f"归并问题模式失败(本轮跳过):{exc}")
|
||||
return {"clusters": len(known), "merged": 0, "error": str(exc)[:200]}
|
||||
merge = value.get("merge") if isinstance(value, dict) else None
|
||||
by_name = {k["cluster"]: k for k in known}
|
||||
aliases = _aliases()
|
||||
merged = 0
|
||||
for old, new in (merge or {}).items():
|
||||
old, new = str(old), str(new)
|
||||
if old == new or old not in by_name or new not in by_name:
|
||||
continue # 规范名必须是已有名字;编出来的不收
|
||||
if by_name[old].get("replayType") != by_name[new].get("replayType"):
|
||||
continue # 重放型不同,判分口径不同,不能并
|
||||
if canon(new, {**aliases, old: new}) == old:
|
||||
continue # 会成环
|
||||
aliases[old] = new
|
||||
merged += 1
|
||||
if merged:
|
||||
path = config.DATA / ALIASES
|
||||
path.write_text(json.dumps(aliases, ensure_ascii=False, indent=2))
|
||||
os.chmod(path, 0o600)
|
||||
config.log(f"归并问题模式:{len(known)} 个里并掉 {merged} 个")
|
||||
return {"clusters": len(known), "merged": merged}
|
||||
|
||||
|
||||
# ── 审计 ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
ALLOWED_CLASSES = {"dead_end", "decision_error", "context_missing", "not_failure"}
|
||||
ALLOWED_REPLAY_TYPES = {"T1_repeat", "T2_wait", "T3_frida", "none"}
|
||||
|
||||
|
||||
def _audit_prompt(ep: dict, timeline: str, known: Optional[List[dict]] = None) -> str:
|
||||
rubric = RUBRIC.read_text()
|
||||
return f"""你在审计 coding agent 的一段历史执行记录。只做分类,不给修复建议。
|
||||
|
||||
{rubric}
|
||||
|
||||
已有的问题模式(**同一机制必须复用这里的 cluster 名字**;只有确实是新机制才新建):
|
||||
{_known_block(known or [])}
|
||||
|
||||
片段元数据:
|
||||
- stage: {ep['stage']}
|
||||
- 自动切片标签: {ep['label']}
|
||||
- 重复失败次数: {ep['n_fail']}
|
||||
- 调用范围: #{ep['first_call']}..#{ep['last_call']}
|
||||
|
||||
时间线:
|
||||
{timeline}
|
||||
|
||||
只返回一个 JSON 对象:
|
||||
{{
|
||||
"classification": "dead_end|decision_error|context_missing|not_failure",
|
||||
"cluster": "稳定、简短的 snake_case 问题模式",
|
||||
"clusterTitle": "中文模式名",
|
||||
"replayType": "T1_repeat|T2_wait|T3_frida|none",
|
||||
"correctInfoStep": 数字或 null,
|
||||
"evidence": ["逐字引用时间线中的原文,至少一条"],
|
||||
"rationale": "两三句,说明分类和正确的信息在何时已出现"
|
||||
}}
|
||||
不得把账号、token、工单正文复述进 cluster/rationale。evidence 必须逐字来自上面的时间线。"""
|
||||
|
||||
|
||||
def _valid_audit(value: Any, timeline: str) -> Optional[dict]:
|
||||
if not isinstance(value, dict):
|
||||
return None
|
||||
cls = value.get("classification")
|
||||
replay_type = value.get("replayType")
|
||||
cluster = str(value.get("cluster") or "")[:120]
|
||||
title = str(value.get("clusterTitle") or "")[:160]
|
||||
evidence = [str(x)[:500] for x in value.get("evidence") or [] if isinstance(x, str) and str(x).strip()]
|
||||
if cls not in ALLOWED_CLASSES or replay_type not in ALLOWED_REPLAY_TYPES or not cluster or not title:
|
||||
return None
|
||||
# 结论必须能回指原文;不接受模型编出来的“引文”。
|
||||
if not evidence or not any(q in timeline for q in evidence):
|
||||
return None
|
||||
step = value.get("correctInfoStep")
|
||||
if step is not None and not isinstance(step, int):
|
||||
step = None
|
||||
return {
|
||||
"classification": cls, "cluster": cluster, "clusterTitle": title,
|
||||
"replayType": replay_type, "correctInfoStep": step,
|
||||
"evidence": evidence[:3], "rationale": str(value.get("rationale") or "")[:1200],
|
||||
}
|
||||
|
||||
|
||||
def audit_new(max_items: Optional[int] = None) -> dict:
|
||||
episodes_by_id = {e["id"]: e for e in _read_jsonl(config.EPISODES)}
|
||||
existing = {a["episodeId"]: a for a in _read_jsonl(config.AUDITS) if a.get("episodeId")}
|
||||
todo = [e for e in episodes_by_id.values() if e["id"] not in existing]
|
||||
todo.sort(key=lambda e: (int(e.get("n_fail") or 0), int(e.get("mtime") or 0)), reverse=True)
|
||||
todo = todo[: max_items if max_items is not None else config.MAX_AUDITS_PER_RUN]
|
||||
ok = 0
|
||||
errors: List[str] = []
|
||||
config.log(f"审计:本轮 {len(todo)} 段(每段调一次 claude,约 1~2 分钟)")
|
||||
known = known_clusters()
|
||||
for k, ep in enumerate(todo, 1):
|
||||
config.log(f" 审计 {k}/{len(todo)}:{ep['label'][:50]}")
|
||||
path = _local_file(str(ep["node"]), str(ep["file"]))
|
||||
rows = load_rows(path)
|
||||
timeline = render_timeline(rows, int(ep["first_call"]), int(ep["last_call"]))
|
||||
try:
|
||||
value = _valid_audit(llm.ask_json(_audit_prompt(ep, timeline, known)), timeline)
|
||||
if not value:
|
||||
raise RuntimeError("审计结果缺字段、类别越界或证据不是原文")
|
||||
existing[ep["id"]] = {
|
||||
"episodeId": ep["id"], "auditedAt": int(time.time() * 1000),
|
||||
"stage": ep["stage"], "node": ep["node"], "file": ep["file"],
|
||||
"nFail": ep["n_fail"], **value,
|
||||
}
|
||||
ok += 1
|
||||
if not any(k["cluster"] == canon(value["cluster"]) for k in known): # 新模式,后面的审计也要看得到
|
||||
known.append({"cluster": canon(value["cluster"]), "clusterTitle": value["clusterTitle"], "count": 1,
|
||||
"classification": value["classification"], "replayType": value["replayType"]})
|
||||
# 每一条成功后就落盘;中途 OAuth/网络失败不会丢掉前面的昂贵结果。
|
||||
_write_jsonl(config.AUDITS, sorted(existing.values(), key=lambda x: int(x.get("auditedAt") or 0)))
|
||||
except Exception as exc: # noqa: BLE001
|
||||
errors.append(f"{ep['id']}: {exc}")
|
||||
return {"requested": len(todo), "audited": ok, "errors": errors}
|
||||
|
||||
|
||||
# ── 聚类与重放点选择 ─────────────────────────────────────────────────────────
|
||||
|
||||
def clusters() -> List[dict]:
|
||||
episode_map = {e["id"]: e for e in _read_jsonl(config.EPISODES)}
|
||||
grouped: Dict[tuple, dict] = {}
|
||||
aliases = _aliases()
|
||||
titles = {k["cluster"]: k["clusterTitle"] for k in known_clusters(limit=1000)}
|
||||
for audit in _read_jsonl(config.AUDITS):
|
||||
ep = episode_map.get(audit.get("episodeId"))
|
||||
if not ep or audit.get("classification") == "not_failure":
|
||||
continue
|
||||
platform = ep.get("platform") or platform_of(str(ep.get("node")))
|
||||
name = canon(audit.get("cluster"), aliases)
|
||||
key = (audit.get("stage"), platform, audit.get("classification"), name, audit.get("replayType"))
|
||||
slot = grouped.setdefault(key, {
|
||||
"stage": key[0], "platform": platform, "problemClass": key[2], "cluster": key[3],
|
||||
"clusterTitle": titles.get(name) or audit.get("clusterTitle"), "replayType": key[4],
|
||||
"episodes": 0, "wasteCalls": 0, "transcripts": set(), "samples": [],
|
||||
})
|
||||
slot["episodes"] += 1
|
||||
slot["wasteCalls"] += int(ep.get("n_fail") or 0)
|
||||
slot["transcripts"].add((ep.get("node"), ep.get("file")))
|
||||
if len(slot["samples"]) < 4:
|
||||
slot["samples"].append({
|
||||
"episodeId": ep["id"], "nFail": ep.get("n_fail"),
|
||||
"evidence": audit.get("evidence") or [], "rationale": audit.get("rationale") or "",
|
||||
})
|
||||
out = []
|
||||
for slot in grouped.values():
|
||||
slot["transcripts"] = len(slot["transcripts"])
|
||||
out.append(slot)
|
||||
return sorted(out, key=lambda x: (x["wasteCalls"], x["episodes"]), reverse=True)
|
||||
|
||||
|
||||
def replay_points(cluster: Optional[dict] = None) -> List[dict]:
|
||||
points = _read_jsonl(config.POINTS)
|
||||
if not cluster:
|
||||
return points
|
||||
stage = cluster["stage"]
|
||||
platform = cluster.get("platform")
|
||||
ptype = cluster["replayType"]
|
||||
points = [p for p in points if p.get("stage") == stage and (not platform or p.get("platform") == platform)]
|
||||
if ptype == "T2_wait":
|
||||
return [p for p in points if p.get("ptype") == "T2_wait"]
|
||||
aliases = _aliases()
|
||||
audited_eps = {
|
||||
a["episodeId"] for a in _read_jsonl(config.AUDITS)
|
||||
if a.get("stage") == stage and canon(a.get("cluster"), aliases) == cluster.get("cluster")
|
||||
and a.get("classification") == cluster.get("problemClass")
|
||||
}
|
||||
return [p for p in points if p.get("episodeId") in audited_eps and p.get("ptype") == ptype]
|
||||
|
||||
|
||||
def real_episode_ids(stage: str) -> set:
|
||||
"""这个 stage 下审计过、且不是切片误报的片段 —— 保留集只用它们(自动切片约 25% 误报)。"""
|
||||
return {
|
||||
a["episodeId"] for a in _read_jsonl(config.AUDITS)
|
||||
if a.get("stage") == stage and a.get("episodeId") and a.get("classification") != "not_failure"
|
||||
}
|
||||
|
||||
|
||||
def load_rows_for_point(point: dict) -> List[Optional[dict]]:
|
||||
return load_rows(_local_file(str(point["node"]), str(point["file"])))
|
||||
@@ -0,0 +1,209 @@
|
||||
"""把排名最高的问题模式变成一个「只插入新行」的 prompt 候选。
|
||||
|
||||
模型只决定写什么;插入、占位符一致和长度限制都由本模块确定性复核。控制面提交时还会
|
||||
用 shared/src/rsi_loop.ts 再复核一遍。
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from . import llm
|
||||
|
||||
STAGES = {"bug.run", "feature.run.first", "feature.run.second"}
|
||||
# 业务指标(真值来自证据 manifest / 终态)优先;过程指标是机制层面的浪费。
|
||||
# 口径与生产基线见 shared/src/rsi_loop.ts::LOOP_METRICS。
|
||||
BUSINESS_METRICS = {"reach_s", "reach_miss", "evidence_fail", "dead_end_s"}
|
||||
TARGET_METRICS = BUSINESS_METRICS | {
|
||||
"ui_wait_s", "blind_sleep_s", "failed_calls",
|
||||
"repeat_fail_calls", "frida_fail_calls", "tool_calls",
|
||||
}
|
||||
# 每种重放型能登记哪些目标:离线判的是动作机制,真机复跑判登记的指标,两者要是同一条因果链。
|
||||
# 等 App 就绪 / 撞墙 / Frida 乱注入都发生在走到复现现场之前或认死路之前,所以都能挂到达和走不通时长;
|
||||
# 证据校验失败只跟「撞同一堵墙」(重拍)一类有关。
|
||||
METRICS_BY_REPLAY = {
|
||||
"T1_repeat": {"repeat_fail_calls", "reach_s", "reach_miss", "dead_end_s", "evidence_fail"},
|
||||
"T2_wait": {"ui_wait_s", "blind_sleep_s", "reach_s", "reach_miss"},
|
||||
"T3_frida": {"frida_fail_calls", "reach_s", "dead_end_s"},
|
||||
}
|
||||
EVIDENCE_TERMS = re.compile(r"record-evidence|manifest|evidence/|\bbefore-|\bafter-|证据|截图|录屏|拍照|shot\b", re.I)
|
||||
|
||||
|
||||
def gaming_error(metric: str, base: str, candidate: str) -> Optional[str]:
|
||||
"""与 shared/src/rsi_loop.ts::lintMetricGaming 同一条规则:到达类目标不许改证据采集。"""
|
||||
if metric not in ("reach_s", "reach_miss"):
|
||||
return None
|
||||
added = inserted_text(base, candidate).split("\n")
|
||||
hit = next((line for line in added if EVIDENCE_TERMS.search(line)), None)
|
||||
return f"目标是到达复现现场,新增内容却在改证据采集:{hit.strip()[:60]}" if hit else None
|
||||
|
||||
|
||||
def placeholders(template: str) -> List[str]:
|
||||
return sorted(set(re.findall(r"\{\{\s*([\w.]+)\s*\}\}", template)))
|
||||
|
||||
|
||||
def candidate_error(base: str, candidate: str) -> Optional[str]:
|
||||
if placeholders(base) != placeholders(candidate):
|
||||
return "占位符不一致"
|
||||
if len(candidate) < len(base):
|
||||
return "候选比现行模板短"
|
||||
if len(candidate) - len(base) > 4000:
|
||||
return "新增超过 4000 字"
|
||||
base_lines = base.split("\n")
|
||||
j = 0
|
||||
for line in candidate.split("\n"):
|
||||
if j < len(base_lines) and line == base_lines[j]:
|
||||
j += 1
|
||||
return None if j == len(base_lines) else "候选改动了已有行"
|
||||
|
||||
|
||||
def apply_insertion(base: str, content: str, after_line: Optional[str]) -> str:
|
||||
addition = content.strip("\n")
|
||||
if not addition:
|
||||
raise ValueError("插入内容为空")
|
||||
lines = base.split("\n")
|
||||
if after_line:
|
||||
matches = [i for i, line in enumerate(lines) if line == after_line]
|
||||
if len(matches) != 1:
|
||||
raise ValueError("afterLine 必须逐字匹配现行模板中唯一的一行")
|
||||
at = matches[0] + 1
|
||||
lines[at:at] = addition.split("\n")
|
||||
else:
|
||||
lines.extend(addition.split("\n"))
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def inserted_text(base: str, candidate: str) -> str:
|
||||
"""提取候选新增的行;base 不是 candidate 子序列时拒绝。"""
|
||||
base_lines = base.split("\n")
|
||||
added: List[str] = []
|
||||
j = 0
|
||||
for line in candidate.split("\n"):
|
||||
if j < len(base_lines) and line == base_lines[j]:
|
||||
j += 1
|
||||
else:
|
||||
added.append(line)
|
||||
if j != len(base_lines):
|
||||
raise ValueError("候选不是纯插入")
|
||||
return "\n".join(added).strip()
|
||||
|
||||
|
||||
def _prompt(cluster: dict, base: str) -> str:
|
||||
# 提案只需要审计结论,不需要把含工单正文/账号的逐字证据再喂一遍,更不能让它进入提交字段。
|
||||
safe_samples = [
|
||||
{"nFail": s.get("nFail"), "rationale": _redact(str(s.get("rationale") or ""))}
|
||||
for s in (cluster.get("samples") or [])
|
||||
]
|
||||
samples = json.dumps(safe_samples, ensure_ascii=False, indent=2)
|
||||
return f"""你负责给 coding agent 的执行 prompt 提一个小而可归因的改动。
|
||||
只能针对下面这个已审计的问题模式;一次只插入一段新纪律,不得改写、删除现有行,
|
||||
不得增加或删除 `{{{{占位符}}}}`。新增不超过 4000 字。
|
||||
|
||||
stage: {cluster['stage']}
|
||||
平台: {cluster.get('platform')}(问题来自这个平台的执行记录。下面的现行模板是按这个平台展开后的版本)
|
||||
策略范围(scope)二选一:
|
||||
- "platform":单端策略。规则只对这个平台成立(涉及该平台专有的工具、页面形态、系统行为)。
|
||||
只在这个平台的实验机复跑,通过后也只上这个平台,另一个平台的模板不变。
|
||||
- "both":双端策略。规则对两端都成立(与平台无关的执行纪律)。两端实验机各复跑一遍,
|
||||
两端都通过才两端一起上;只有一端通过就只上那一端。新增内容里出现单端专有的工具词
|
||||
(adb、catuse、xcrun 等)会被控制面按单端处理。
|
||||
拿不准就选 "platform" —— 单端通过后,人可以再决定补另一端复跑。
|
||||
问题类别: {cluster['problemClass']}
|
||||
模式: {cluster['cluster']} / {cluster.get('clusterTitle')}
|
||||
浪费: {cluster['wasteCalls']} 次失败调用,{cluster['episodes']} 个片段,
|
||||
{cluster['transcripts']} 份会话
|
||||
可引用的审计样本:
|
||||
{samples}
|
||||
|
||||
可事先登记的目标指标(越低越好)。优先登记业务指标 —— 它们是用户真正关心的结果,
|
||||
真值来自证据清单和任务终态,不来自 agent 自述;过程指标只在该模式与任何业务结果都
|
||||
没有可信因果链时才登记。
|
||||
业务指标(2026-10 生产基线,160 批):
|
||||
- reach_s: 到达复现现场用时 —— 从这批开始到第一张校验通过的修复前证据。中位 Android 30 分、
|
||||
iOS 44 分,约占整批一半。
|
||||
- reach_miss: 未到达复现现场 —— 一批结束仍没有合格的修复前证据。修好的批次里 Android 17%、
|
||||
iOS 14%;复现不了的 12 批里 10 批没到达。
|
||||
- evidence_fail: 证据校验失败次数(录空 / 太短 / 分辨率不够,需重拍)。每批 Android 0.6、iOS 1.0 次。
|
||||
- dead_end_s: 走不通时耗掉的时长 —— 没以 FIXED 收尾的批次的整批时长(早期失败识别)。
|
||||
复跑会混入历史上走不通的单,「修好的个数不少于现行」是底线,所以只能靠更早认出死路,不能靠早放弃。
|
||||
过程指标:
|
||||
- ui_wait_s: 等界面状态时长
|
||||
- blind_sleep_s: 盲等时长
|
||||
- failed_calls: 失败调用数
|
||||
- repeat_fail_calls: 同类命令连续失败
|
||||
- frida_fail_calls: Frida 失败调用
|
||||
- tool_calls: 工具调用总数
|
||||
本模式的重放型是 {cluster.get('replayType')},targetMetric 必须从
|
||||
{sorted(METRICS_BY_REPLAY.get(cluster.get('replayType'), TARGET_METRICS))} 中选,
|
||||
并在 hypothesis 里写清这个模式怎样因果地影响所选指标(离线重放判的是动作机制,
|
||||
真机复跑判的是所选指标,两者要是同一条因果链)。
|
||||
若选 reach_s / reach_miss:新增内容不得涉及证据采集(截图、录屏、record-evidence、
|
||||
before/after 命名)—— 到达是按第一张合格修复前证据量的,改快门时机会让指标变好而现场没变快,
|
||||
这种候选会被直接拒收。
|
||||
|
||||
现行模板(只能选其中唯一一行作为 afterLine;若放结尾则 afterLine=null):
|
||||
---BASE---
|
||||
{base}
|
||||
---END BASE---
|
||||
|
||||
只返回 JSON:
|
||||
{{
|
||||
"title": "实验标题",
|
||||
"summary": "问题总结,引用聚合数字,不泄露账号或工单正文",
|
||||
"hypothesis": "为什么这一个改动会降低目标指标",
|
||||
"method": "插入什么纪律、放在哪里、真机复跑时看什么",
|
||||
"targetMetric": "上面列表中允许的一个",
|
||||
"afterLine": "现行模板中逐字且唯一的一整行,或 null",
|
||||
"content": "要插入的新行",
|
||||
"scope": "platform 或 both",
|
||||
"scopeReason": "一句话:为什么这条规则只对这个平台 / 对两端都成立"
|
||||
}}
|
||||
content 要写可执行决策规则(何时、先做什么、失败后怎么换路),不要写空泛口号,
|
||||
不要修改评分规则、保留集、指标定义或要求模型自评成功。"""
|
||||
|
||||
|
||||
def _redact(text: str) -> str:
|
||||
text = re.sub(r"[\w.+-]+@[\w.-]+\.[A-Za-z]{2,}", "<email>", text)
|
||||
text = re.sub(r"(?i)\b(password|passwd|token|cookie|authorization)\s*[:=]\s*\S+", r"\1=<redacted>", text)
|
||||
text = re.sub(r"\b[0-9a-f]{24,}\b", "<id>", text, flags=re.I)
|
||||
return text[:1200]
|
||||
|
||||
|
||||
def create(cluster: dict, base: str) -> dict:
|
||||
if cluster.get("stage") not in STAGES:
|
||||
raise ValueError("不支持的 stage")
|
||||
value: Any = llm.ask_json(_prompt(cluster, base), timeout=1200)
|
||||
if not isinstance(value, dict):
|
||||
raise ValueError("提案不是 JSON 对象")
|
||||
required = ("title", "summary", "hypothesis", "method", "targetMetric", "content")
|
||||
if any(not str(value.get(k) or "").strip() for k in required):
|
||||
raise ValueError("提案缺少必填字段")
|
||||
metric = str(value["targetMetric"])
|
||||
if metric not in TARGET_METRICS:
|
||||
raise ValueError("targetMetric 不在目录里")
|
||||
allowed = METRICS_BY_REPLAY.get(cluster.get("replayType"), TARGET_METRICS)
|
||||
if metric not in allowed:
|
||||
raise ValueError("targetMetric 与该模式的离线动作判分不对应")
|
||||
after = value.get("afterLine")
|
||||
if after is not None and not isinstance(after, str):
|
||||
raise ValueError("afterLine 格式错误")
|
||||
candidate = apply_insertion(base, str(value["content"]), after)
|
||||
error = candidate_error(base, candidate) or gaming_error(metric, base, candidate)
|
||||
if error:
|
||||
raise ValueError(error)
|
||||
return {
|
||||
"stage": cluster["stage"],
|
||||
"platform": cluster.get("platform"),
|
||||
"title": str(value["title"]).strip()[:200],
|
||||
"summary": str(value["summary"]).strip()[:6000],
|
||||
"hypothesis": str(value["hypothesis"]).strip()[:4000],
|
||||
"method": str(value["method"]).strip()[:4000],
|
||||
"targetMetric": metric,
|
||||
"problemClass": cluster["problemClass"],
|
||||
"cluster": cluster["cluster"],
|
||||
"candidateTemplate": candidate,
|
||||
# 策略范围:both = 两端各复跑;其余一律单端(控制面按 platform 处理)
|
||||
"scope": "both" if str(value.get("scope") or "").strip() == "both" else cluster.get("platform"),
|
||||
"scopeReason": str(value.get("scopeReason") or "").strip()[:1000],
|
||||
# 重放按同一个锚点把同样的几行插进历史 prompt,与生产渲染的位置一致(不提交给控制面)
|
||||
"_insertion": {"afterLine": after, "content": inserted_text(base, candidate)},
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
"""决策点重放:把一段历史会话截到某个工具结果之后,用本机 claude 续跑一步,
|
||||
只记录模型「想做什么」(第一条 assistant 消息里的工具调用 / 文字),工具一律被
|
||||
PreToolUse 钩子拦下,不在本机执行。
|
||||
|
||||
保真度的三件事(2026-09 实验里踩出来的):
|
||||
- 不能截在工具调用处,否则本机会补一条「调用被中断」的假结果;只截在工具结果之后;
|
||||
- 续跑会追加「环境 / 日期 / 工具列表都变了」的附件,模型第一反应是去查环境 —— 所以把
|
||||
这些附件改写成本机值,节点的 CLAUDE.md / MEMORY.md 落到本机对应路径;
|
||||
- 本机原样起节点的 actl 设备工具 MCP,只为让工具定义一致(调用全被钩子拦)。
|
||||
"""
|
||||
import copy
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from . import config
|
||||
|
||||
HOME = Path.home()
|
||||
# 续跑的工作目录:<临时目录>/sl-ws/<会话id>/ai_workspace。模型看得见这个路径 —— 带着
|
||||
# rsi_loop 之类的字样,它第一反应就是去查「这是什么」;叫 ai_workspace 才和节点上一致。
|
||||
_REPLAY_BASE = Path(tempfile.gettempdir()).resolve() / "sl-ws"
|
||||
|
||||
|
||||
def _run_dirs(sid: str):
|
||||
cwd = _REPLAY_BASE / sid / "ai_workspace"
|
||||
proj = HOME / ".claude" / "projects" / re.sub(r"[^A-Za-z0-9]", "-", str(cwd))
|
||||
return cwd, proj
|
||||
|
||||
|
||||
def _claude_cmd(sid: str, model: str) -> List[str]:
|
||||
cmd = [config.CLAUDE_BIN, "-p", "继续", "--resume", sid, "--output-format", "stream-json", "--verbose",
|
||||
"--model", model, "--setting-sources", "project"]
|
||||
mcp = _mcp_config()
|
||||
if mcp:
|
||||
cmd += ["--mcp-config", mcp]
|
||||
return cmd + ["--strict-mcp-config", "--settings", BLOCK_ALL, "--append-system-prompt", AUTOMODE, "--max-turns", "1"]
|
||||
|
||||
AUTOMODE = ("本会话运行在无人值守的自动化流水线中,没有任何人类在观看或会作出回应。"
|
||||
"绝对禁止使用 AskUserQuestion 工具或以提问等待用户回复的方式中断任务;"
|
||||
"如必须在多个方案中取舍,直接采用你推荐的(或第一个可行的)方案并继续执行,事后在输出中说明即可。"
|
||||
"如需计划确认(ExitPlanMode),直接按你的计划执行。任务必须自主推进到完成。")
|
||||
BLOCK_ALL = json.dumps({"hooks": {"PreToolUse": [{"matcher": "*", "hooks": [
|
||||
{"type": "command", "command": "echo 'replay: tool execution disabled' >&2; exit 2"}]}]}})
|
||||
|
||||
|
||||
def _mcp_config() -> Optional[str]:
|
||||
return json.dumps({"mcpServers": {"actl": {"type": "stdio", "command": "node", "args": [config.ACTL_INDEX]}}}) \
|
||||
if Path(config.ACTL_INDEX).exists() else None
|
||||
|
||||
|
||||
_LOCAL: Optional[dict] = None
|
||||
|
||||
|
||||
def _probe(sample_rows: List[Optional[dict]]) -> dict:
|
||||
"""拿一段真实执行会话的开头(截到第一个工具结果)在本机续跑一步,收集本机追加的
|
||||
「和节点不一样」的附件 —— 它们就是中和时要换上的本机值。空会话探不出任何东西:
|
||||
Claude Code 不认残缺的会话记录,也就不会追加附件。"""
|
||||
cut = next((i for i, r in enumerate(sample_rows) if r and r.get("type") == "user"
|
||||
and isinstance((r.get("message") or {}).get("content"), list)
|
||||
and any(isinstance(b, dict) and b.get("type") == "tool_result" for b in r["message"]["content"])), None)
|
||||
if cut is None:
|
||||
raise RuntimeError("探测用的会话里没有工具结果,换一份会话")
|
||||
sid = str(uuid.uuid4())
|
||||
cwd, proj = _run_dirs(sid)
|
||||
cwd.mkdir(parents=True, exist_ok=True)
|
||||
proj.mkdir(parents=True, exist_ok=True)
|
||||
prefix = []
|
||||
for r in sample_rows[: cut + 1]:
|
||||
if not r:
|
||||
continue
|
||||
r = copy.deepcopy(r)
|
||||
r["sessionId"] = sid
|
||||
if "cwd" in r:
|
||||
r["cwd"] = str(cwd)
|
||||
prefix.append(r)
|
||||
path = proj / f"{sid}.jsonl"
|
||||
path.write_text("\n".join(json.dumps(r, ensure_ascii=False) for r in prefix) + "\n")
|
||||
env = config.child_env(str(cwd))
|
||||
loc: dict = {}
|
||||
try:
|
||||
subprocess.run(_claude_cmd(sid, config.REPLAY_MODEL), cwd=str(cwd), env=env,
|
||||
capture_output=True, text=True, timeout=600)
|
||||
for line in path.read_text(errors="replace").splitlines()[len(prefix):]:
|
||||
try:
|
||||
r = json.loads(line)
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
a = r.get("attachment")
|
||||
if r.get("type") != "attachment" or not a:
|
||||
continue
|
||||
t = a.get("type")
|
||||
if t in ("environment", "session_context", "date", "credential_org", "remote_session_change", "auto_mode"):
|
||||
loc[t] = {k: v for k, v in a.items() if k != "changed"}
|
||||
elif t == "deferred_tools_delta":
|
||||
loc["deferred_removed"] = a.get("removedNames", [])
|
||||
elif t == "agent_listing_delta":
|
||||
loc["agent_removed"] = a.get("removedTypes", [])
|
||||
finally:
|
||||
shutil.rmtree(proj, ignore_errors=True)
|
||||
shutil.rmtree(cwd.parent, ignore_errors=True)
|
||||
if "environment" not in loc:
|
||||
raise RuntimeError("探测失败:本机续跑没有追加环境附件(claude 鉴权、模型名或会话格式有问题),不能做重放")
|
||||
return loc
|
||||
|
||||
|
||||
def _local_values(sample_rows: Optional[List[Optional[dict]]] = None) -> dict:
|
||||
"""本机该用的附件值。每轮运行先用一份真实会话探测一次(prepare),之后并发重放复用。"""
|
||||
global _LOCAL
|
||||
if _LOCAL is not None:
|
||||
return _LOCAL
|
||||
if sample_rows is None:
|
||||
raise RuntimeError("重放前必须先 prepare(sample_rows) 探测本机附件")
|
||||
_LOCAL = _probe(sample_rows)
|
||||
cache = config.DATA / "local_attachments.json"
|
||||
cache.write_text(json.dumps(_LOCAL, ensure_ascii=False))
|
||||
os.chmod(cache, 0o600)
|
||||
return _LOCAL
|
||||
|
||||
|
||||
class AnchorMissing(ValueError):
|
||||
"""候选的插入位置在这份历史 prompt 里找不到(或不唯一):这个点不能公平地比较两个版本。"""
|
||||
|
||||
|
||||
def _anchor_regex(line: str) -> "re.Pattern[str]":
|
||||
# 锚点行可能带占位符;渲染后的历史 prompt 里那部分是具体值,按通配匹配。
|
||||
parts = re.split(r"\{\{\s*[\w.]+\s*\}\}", line)
|
||||
return re.compile("^" + ".*?".join(re.escape(p) for p in parts) + "$")
|
||||
|
||||
|
||||
def insert_into_text(text: str, insertion: dict) -> str:
|
||||
"""把候选新增的行插到与生产相同的位置:锚点行之后;锚点为 null 表示模板末尾。
|
||||
不加任何标题 —— 生产上这段内容就是模板里普通的几行,离线时更醒目会高估候选。"""
|
||||
content = str(insertion.get("content") or "").strip("\n")
|
||||
if not content:
|
||||
return text
|
||||
lines = text.split("\n")
|
||||
after = insertion.get("afterLine")
|
||||
if after is None:
|
||||
return "\n".join(lines + content.split("\n"))
|
||||
rx = _anchor_regex(str(after))
|
||||
hits = [i for i, line in enumerate(lines) if rx.match(line)]
|
||||
if len(hits) != 1:
|
||||
raise AnchorMissing(f"锚点在历史 prompt 里匹配到 {len(hits)} 处")
|
||||
at = hits[0] + 1
|
||||
lines[at:at] = content.split("\n")
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def _insert_into_content(content: Any, insertion: Optional[dict]) -> Any:
|
||||
if not insertion:
|
||||
return content
|
||||
if isinstance(content, str):
|
||||
return insert_into_text(content, insertion)
|
||||
if isinstance(content, list):
|
||||
out = copy.deepcopy(content)
|
||||
for block in out:
|
||||
if isinstance(block, dict) and block.get("type") == "text":
|
||||
block["text"] = insert_into_text(str(block.get("text") or ""), insertion)
|
||||
return out
|
||||
raise AnchorMissing("历史会话第一条用户消息没有文本")
|
||||
|
||||
|
||||
def candidate_applies(rows: List[Optional[dict]], insertion: Optional[dict]) -> bool:
|
||||
"""这个决策点能否施加候选改动(锚点唯一可定位)。不能的点两组都不跑。"""
|
||||
if not insertion:
|
||||
return True
|
||||
for r in rows:
|
||||
if r and r.get("type") == "user" and not r.get("isMeta"):
|
||||
try:
|
||||
_insert_into_content((r.get("message") or {}).get("content"), insertion)
|
||||
return True
|
||||
except AnchorMissing:
|
||||
return False
|
||||
return False
|
||||
|
||||
|
||||
def _neutralize(rows: List[Optional[dict]], sid: str, loc: dict, cwd: Path, proj: Path,
|
||||
insertion: Optional[dict] = None):
|
||||
files: Dict[str, str] = {}
|
||||
out: List[dict] = []
|
||||
prompt_patched = False
|
||||
for r in rows:
|
||||
if not r:
|
||||
continue
|
||||
r = copy.deepcopy(r)
|
||||
r["sessionId"] = sid
|
||||
if "cwd" in r:
|
||||
r["cwd"] = str(cwd)
|
||||
if r.get("type") == "user" and not r.get("isMeta") and not prompt_patched:
|
||||
message = r.get("message") or {}
|
||||
if "content" in message:
|
||||
message["content"] = _insert_into_content(message["content"], insertion)
|
||||
r["message"] = message
|
||||
prompt_patched = True
|
||||
a = r.get("attachment") if r.get("type") == "attachment" else None
|
||||
if a:
|
||||
t = a.get("type")
|
||||
if t == "environment" and "environment" in loc:
|
||||
# 探测时记下的是探测目录;每次重放用独立目录,不换掉的话续跑会追加一条
|
||||
# 「工作目录变了」,模型第一反应就是去查环境。
|
||||
a["snapshot"] = {**loc["environment"]["snapshot"], "workingDirectory": str(cwd)}
|
||||
a.pop("changes", None)
|
||||
elif t in ("session_context", "date", "remote_session_change", "auto_mode") and t in loc:
|
||||
r["attachment"] = copy.deepcopy(loc[t])
|
||||
elif t == "deferred_tools_delta":
|
||||
drop = set(loc.get("deferred_removed") or [])
|
||||
for k in ("addedNames", "readdedNames"):
|
||||
if isinstance(a.get(k), list):
|
||||
a[k] = [n for n in a[k] if n not in drop]
|
||||
if isinstance(a.get("addedLines"), list):
|
||||
a["addedLines"] = [l for l in a["addedLines"] if not any(l.strip().startswith(n) for n in drop)]
|
||||
elif t == "agent_listing_delta":
|
||||
drop = set(loc.get("agent_removed") or [])
|
||||
if isinstance(a.get("addedTypes"), list):
|
||||
a["addedTypes"] = [n for n in a["addedTypes"] if n not in drop]
|
||||
if isinstance(a.get("addedLines"), list):
|
||||
a["addedLines"] = [l for l in a["addedLines"] if not any(l.strip().lstrip("- ").startswith(n + ":") for n in drop)]
|
||||
elif t == "instructions":
|
||||
for fobj in a.get("files") or []:
|
||||
p = fobj.get("path", "")
|
||||
if p.endswith("/ai_workspace/CLAUDE.md") or re.search(r"ai_workspace(_\w+)?/CLAUDE\.md$", p):
|
||||
np = str(cwd / "CLAUDE.md")
|
||||
elif p.endswith("/memory/MEMORY.md"):
|
||||
np = str(proj / "memory" / "MEMORY.md")
|
||||
else:
|
||||
continue
|
||||
files[np] = fobj.get("content", "")
|
||||
fobj["path"] = np
|
||||
out.append(r)
|
||||
have = {r["attachment"].get("type") for r in out if r.get("type") == "attachment" and r.get("attachment")}
|
||||
for t in ("credential_org", "session_context", "remote_session_change"):
|
||||
if t in loc and t not in have:
|
||||
idx = next((k for k, r in enumerate(out) if r.get("type") == "attachment" and r["attachment"].get("type") == "date"), None)
|
||||
if idx is not None:
|
||||
tmpl = copy.deepcopy(out[idx])
|
||||
tmpl["attachment"] = copy.deepcopy(loc[t])
|
||||
tmpl["uuid"] = str(uuid.uuid4())
|
||||
out.insert(idx + 1, tmpl)
|
||||
return out, files
|
||||
|
||||
|
||||
def prepare(sample_rows: List[Optional[dict]]) -> None:
|
||||
"""串行探测一次本机附件(每轮重新探,不跨轮缓存:日期、工具列表会变),之后可安全并发重放。"""
|
||||
global _LOCAL
|
||||
_LOCAL = None
|
||||
_local_values(sample_rows)
|
||||
|
||||
|
||||
def run_point(rows: List[Optional[dict]], row_idx: int, insertion: Optional[dict] = None, timeout: int = 900) -> dict:
|
||||
"""截到 rows[row_idx](含)之后续跑一步。返回 {first:[...], secs, error}。first 是模型想做的事。"""
|
||||
sid = str(uuid.uuid4())
|
||||
cwd, proj = _run_dirs(sid)
|
||||
cwd.mkdir(parents=True, exist_ok=True)
|
||||
prefix, files = _neutralize(rows[: row_idx + 1], sid, _local_values(), cwd, proj, insertion)
|
||||
proj.mkdir(parents=True, exist_ok=True)
|
||||
for p, c in files.items():
|
||||
Path(p).parent.mkdir(parents=True, exist_ok=True)
|
||||
Path(p).write_text(c)
|
||||
(proj / f"{sid}.jsonl").write_text("\n".join(json.dumps(r, ensure_ascii=False) for r in prefix) + "\n")
|
||||
env = config.child_env(str(cwd))
|
||||
cmd = _claude_cmd(sid, config.REPLAY_MODEL)
|
||||
t0 = time.time()
|
||||
first: Optional[List[dict]] = None
|
||||
error: Optional[str] = None
|
||||
try:
|
||||
p = subprocess.run(cmd, cwd=str(cwd), env=env, capture_output=True, text=True, timeout=timeout)
|
||||
first, error = first_turn(p.stdout.splitlines())
|
||||
if not first and not error and p.returncode:
|
||||
error = (p.stderr or f"exit {p.returncode}")[-500:]
|
||||
except subprocess.TimeoutExpired:
|
||||
error = "timeout"
|
||||
except Exception as e: # noqa: BLE001
|
||||
error = str(e)
|
||||
finally:
|
||||
shutil.rmtree(proj, ignore_errors=True)
|
||||
shutil.rmtree(cwd.parent, ignore_errors=True)
|
||||
return {"first": first, "secs": round(time.time() - t0, 1), "error": None if first else (error or "no output")}
|
||||
|
||||
|
||||
def first_turn(lines: List[str]):
|
||||
"""从 stream-json 输出里取第一轮的内容块(文字 + 工具调用),到第一个工具调用为止。
|
||||
|
||||
流式输出会把同一轮的「说明文字」和「工具调用」拆成几条消息(常见:先一句话、再调工具)。
|
||||
读到文字就停,会把这一轮记成「没有动作」—— 判分里那等于坏选择,两组都被冤枉。"""
|
||||
first: Optional[List[dict]] = None
|
||||
for line in lines:
|
||||
try:
|
||||
r = json.loads(line)
|
||||
except Exception: # noqa: BLE001
|
||||
continue
|
||||
if r.get("type") == "assistant":
|
||||
acts = [b for b in (r.get("message") or {}).get("content") or []
|
||||
if isinstance(b, dict) and b.get("type") in ("tool_use", "text")]
|
||||
if acts:
|
||||
first = (first or []) + acts
|
||||
if any(b.get("type") == "tool_use" for b in acts):
|
||||
return first, None
|
||||
if r.get("type") == "result":
|
||||
return first, (str(r.get("result") or r.get("subtype")) if r.get("is_error") else None)
|
||||
return first, None
|
||||
|
||||
|
||||
def next_action(rows: List[Optional[dict]], row_idx: int) -> Optional[dict]:
|
||||
"""这个点之后历史上真实的下一步工具调用(用于对照)。"""
|
||||
for r in rows[row_idx + 1:]:
|
||||
if r and r.get("type") == "assistant":
|
||||
for b in (r["message"].get("content") or []):
|
||||
if isinstance(b, dict) and b.get("type") == "tool_use":
|
||||
return {"name": b["name"], "input": b.get("input") or {}}
|
||||
return None
|
||||
@@ -0,0 +1,135 @@
|
||||
"""复核意见挖掘:每晚给新的人工复核意见打标签,每周把编码习惯归纳成候选规范。
|
||||
|
||||
口径、门槛、为什么这么做见 shared/src/review_mining.ts。这里只做「调模型」那一半:
|
||||
- 提示词正文由控制面拼好下发(/review-mining/pending、/review-mining/cluster-input),这里不改一个字;
|
||||
- 模型的回答原样交回,key 校验、归属、统计、门槛都由控制面重算 —— 不信这里报的任何数;
|
||||
- 能写的只有标签和 pending 候选,批准 / 驳回要管理员在 RSI 页上点。
|
||||
|
||||
单独跑(补标历史、或者想立刻归纳一次):
|
||||
./run.sh review-mining --days 180 # 补标近 180 天,每次最多 400 条,多跑几次
|
||||
./run.sh review-mining --cluster # 不管是不是周一,立刻归纳一次
|
||||
"""
|
||||
import json
|
||||
import time
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Optional
|
||||
|
||||
from . import api, config, llm
|
||||
|
||||
LABELER = "rsi-loop/review-label@1"
|
||||
LAST_CLUSTER = config.DATA / "review_mining_last_cluster"
|
||||
# 与 shared/src/review_mining.ts 的 MINING.clusterWeekday 一致(北京时间周一)
|
||||
CLUSTER_WEEKDAY = 0 # Python 的 weekday():周一 = 0
|
||||
BEIJING = timezone(timedelta(hours=8))
|
||||
|
||||
|
||||
def _beijing_day(now_ms: int) -> datetime:
|
||||
return datetime.fromtimestamp(now_ms / 1000, BEIJING)
|
||||
|
||||
|
||||
def label_pending(days: Optional[int] = None, max_batches: Optional[int] = None) -> dict:
|
||||
"""拉没标过的意见,按批调模型,逐批交回。一批失败不影响下一批(下次运行还会再拉到它)。"""
|
||||
q = "/review-mining/pending" + (f"?days={int(days)}" if days else "")
|
||||
data = api.get(q, timeout=300)
|
||||
batches = data.get("batches") or []
|
||||
if max_batches is not None:
|
||||
batches = batches[:max_batches]
|
||||
saved = dropped = failed = 0
|
||||
for i, b in enumerate(batches):
|
||||
try:
|
||||
out = llm.ask_json(b["prompt"], timeout=900)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
failed += 1
|
||||
config.log(f"复核意见打标签 第 {i + 1}/{len(batches)} 批失败:{str(exc)[:200]}")
|
||||
continue
|
||||
labels = out.get("labels") if isinstance(out, dict) else None
|
||||
if not isinstance(labels, list):
|
||||
failed += 1
|
||||
continue
|
||||
res = api.post("/review-mining/labels", {"labels": labels, "labeler": LABELER}) or {}
|
||||
saved += int(res.get("saved") or 0)
|
||||
dropped += int(res.get("dropped") or 0)
|
||||
return {"pending": int(data.get("total") or 0), "batches": len(batches), "saved": saved, "dropped": dropped, "failedBatches": failed}
|
||||
|
||||
|
||||
def _cluster_due(now_ms: int) -> bool:
|
||||
day = _beijing_day(now_ms)
|
||||
if day.weekday() != CLUSTER_WEEKDAY:
|
||||
return False
|
||||
try:
|
||||
return LAST_CLUSTER.read_text().strip() != day.strftime("%Y-%m-%d")
|
||||
except FileNotFoundError:
|
||||
return True
|
||||
|
||||
|
||||
def cluster(now_ms: int, force: bool = False) -> Optional[dict]:
|
||||
"""每周一次:拉近 90 天的编码习惯标签和拼好的提示词,交给模型归纳,候选交回控制面过门槛。"""
|
||||
if not force and not _cluster_due(now_ms):
|
||||
return None
|
||||
inp = api.get("/review-mining/cluster-input", timeout=300)
|
||||
result = {"rows": int(inp.get("rows") or 0), "proposed": 0, "saved": 0, "rejected": []}
|
||||
if result["rows"]:
|
||||
out = llm.ask_json(inp["prompt"], timeout=1800)
|
||||
cands = out.get("candidates") if isinstance(out, dict) else None
|
||||
if not isinstance(cands, list):
|
||||
raise RuntimeError("归纳的回答里没有 candidates 数组")
|
||||
result["proposed"] = len(cands)
|
||||
res = api.post("/review-mining/candidates", {
|
||||
"runId": f"loop-{_beijing_day(now_ms).strftime('%Y%m%d')}",
|
||||
"window": inp.get("window"),
|
||||
"candidates": cands,
|
||||
}) or {}
|
||||
result["saved"] = int(res.get("saved") or 0)
|
||||
result["rejected"] = res.get("rejected") or []
|
||||
if not config.DRY_RUN:
|
||||
LAST_CLUSTER.write_text(_beijing_day(now_ms).strftime("%Y-%m-%d"))
|
||||
return result
|
||||
|
||||
|
||||
def run(now_ms: Optional[int] = None) -> dict:
|
||||
"""每日回路里的一步。两半各自兜底:挖掘失败不能拖垮回路的实验部分。"""
|
||||
now_ms = now_ms or int(time.time() * 1000)
|
||||
out: dict = {}
|
||||
try:
|
||||
out["label"] = label_pending()
|
||||
except Exception as exc: # noqa: BLE001
|
||||
out["label"] = {"error": str(exc)[:300]}
|
||||
try:
|
||||
out["cluster"] = cluster(now_ms)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
out["cluster"] = {"error": str(exc)[:300]}
|
||||
return out
|
||||
|
||||
|
||||
def summary_line(out: dict) -> str:
|
||||
"""给回路时间线的一句话。"""
|
||||
lab = out.get("label") or {}
|
||||
parts = []
|
||||
if lab.get("error"):
|
||||
parts.append(f"打标签失败:{lab['error'][:80]}")
|
||||
else:
|
||||
parts.append(f"新标 {lab.get('saved', 0)} 条意见(待标 {lab.get('pending', 0)})")
|
||||
cl = out.get("cluster")
|
||||
if cl:
|
||||
if cl.get("error"):
|
||||
parts.append(f"归纳失败:{cl['error'][:80]}")
|
||||
else:
|
||||
parts.append(f"归纳出 {cl.get('saved', 0)} 条候选规范(模型提了 {cl.get('proposed', 0)},门槛拦下 {len(cl.get('rejected') or [])})")
|
||||
return ";".join(parts)
|
||||
|
||||
|
||||
def main(argv: Optional[list] = None) -> dict:
|
||||
import argparse
|
||||
p = argparse.ArgumentParser(prog="rsi_loop review-mining")
|
||||
p.add_argument("--days", type=int, default=None, help="待标意见的回看天数(补历史时用)")
|
||||
p.add_argument("--cluster", action="store_true", help="不管是不是周一,立刻归纳一次")
|
||||
p.add_argument("--no-label", action="store_true", help="跳过打标签")
|
||||
a = p.parse_args(argv)
|
||||
now_ms = int(time.time() * 1000)
|
||||
out: dict = {}
|
||||
if not a.no_label:
|
||||
out["label"] = label_pending(days=a.days)
|
||||
out["cluster"] = cluster(now_ms, force=a.cluster)
|
||||
if any(out.values()):
|
||||
api.event("复核意见挖掘(手动)", json.dumps(out, ensure_ascii=False, indent=2), "note")
|
||||
return out
|
||||
@@ -0,0 +1,359 @@
|
||||
"""RSI 每日回路:同步 → 切片 → 审计 → 提案 → 配对重放 → 提交 → 补结论。"""
|
||||
import concurrent.futures
|
||||
import fcntl
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional
|
||||
|
||||
from . import api, config, llm, mine, proposal, replay, review_mining, runtime, scoring
|
||||
|
||||
TERMINAL = {"offline_failed", "lab_passed", "lab_failed", "declined", "stopped"}
|
||||
|
||||
|
||||
def _write_jsonl(path: Path, rows: List[dict]) -> None:
|
||||
with open(path, "w") as f:
|
||||
for row in rows:
|
||||
f.write(json.dumps(row, ensure_ascii=False, separators=(",", ":")) + "\n")
|
||||
os.chmod(path, 0o600)
|
||||
|
||||
|
||||
def _local_skips() -> dict:
|
||||
try:
|
||||
return json.loads((config.DATA / "skips.json").read_text())
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
|
||||
|
||||
def _skip_key(cluster: dict) -> str:
|
||||
return f"{cluster.get('stage')}|{cluster.get('platform')}|{cluster.get('cluster')}"
|
||||
|
||||
|
||||
def _record_skip(cluster: dict, reason: str, now_ms: int) -> None:
|
||||
if config.DRY_RUN:
|
||||
return # 试运行不留下任何会影响正式运行的状态(否则正式跑时这个模式会被冷却 7 天)
|
||||
skips = _local_skips()
|
||||
skips[_skip_key(cluster)] = {"at": now_ms, "reason": reason}
|
||||
path = config.DATA / "skips.json"
|
||||
path.write_text(json.dumps(skips, ensure_ascii=False, indent=2))
|
||||
os.chmod(path, 0o600)
|
||||
|
||||
|
||||
def _submittable(cluster: dict, state: dict) -> bool:
|
||||
"""实验只在实验机上复跑历史任务:只有 bug.run、且该平台配了实验机才收(控制面同样会拒)。"""
|
||||
return cluster.get("stage") == "bug.run" and cluster.get("platform") in (state.get("labNodes") or {})
|
||||
|
||||
|
||||
def _eligible_cluster(all_clusters: List[dict], state: dict, now_ms: int,
|
||||
point_count=lambda c: len(mine.replay_points(c))) -> Optional[dict]:
|
||||
# 同一 stage 在同一平台上同时只能有一个进行中的实验;platform 为空的旧实验对所有平台生效
|
||||
live = {(e.get("stage"), e.get("platform")) for e in state.get("live") or []}
|
||||
cutoff = now_ms - config.CLUSTER_COOLDOWN_DAYS * 86_400_000
|
||||
recent = {
|
||||
(e.get("stage"), e.get("platform"), e.get("cluster"))
|
||||
for e in state.get("recent") or []
|
||||
if int(e.get("createdAt") or 0) >= cutoff
|
||||
}
|
||||
skips = _local_skips()
|
||||
for cluster in all_clusters:
|
||||
if cluster.get("replayType") == "none" or not _submittable(cluster, state):
|
||||
continue
|
||||
stage, platform = cluster.get("stage"), cluster.get("platform")
|
||||
if (stage, platform) in live or (stage, None) in live:
|
||||
continue
|
||||
if (stage, platform, cluster.get("cluster")) in recent:
|
||||
continue
|
||||
skipped = skips.get(_skip_key(cluster)) or {}
|
||||
if int(skipped.get("at") or 0) >= cutoff:
|
||||
continue
|
||||
# 决策点不够就不提案:注定过不了离线闸,只会白花额度、再占一次冷却期
|
||||
if point_count(cluster) < config.OFFLINE_MIN_POINTS:
|
||||
continue
|
||||
return cluster
|
||||
return None
|
||||
|
||||
|
||||
def _sample_points(cluster: dict, insertion: Optional[dict] = None) -> tuple:
|
||||
applies = lambda p: replay.candidate_applies(mine.load_rows_for_point(p), insertion) # noqa: E731
|
||||
all_target = mine.replay_points(cluster)
|
||||
target = [p for p in all_target if applies(p)]
|
||||
target_ids = {p["id"] for p in all_target}
|
||||
real_eps = mine.real_episode_ids(cluster["stage"])
|
||||
holdout_all = [
|
||||
p for p in mine.replay_points()
|
||||
if p["id"] not in target_ids and p.get("stage") == cluster["stage"]
|
||||
and (not cluster.get("platform") or p.get("platform") == cluster.get("platform"))
|
||||
and (p.get("ptype") == "T2_wait" or (p.get("ptype") in ("T1_repeat", "T3_frida") and p.get("episodeId") in real_eps))
|
||||
]
|
||||
holdout = [p for p in holdout_all if applies(p)]
|
||||
seed = int.from_bytes(str(cluster["cluster"]).encode()[:8].ljust(8, b"\0"), "little")
|
||||
rng = random.Random(seed)
|
||||
rng.shuffle(target)
|
||||
rng.shuffle(holdout)
|
||||
seeds = max(2, config.SEEDS)
|
||||
capacity = max(0, config.MAX_REPLAYS_PER_RUN // (2 * seeds))
|
||||
n_target = min(config.TARGET_POINTS, len(target), capacity)
|
||||
remaining = max(0, capacity - n_target)
|
||||
n_holdout = min(config.HOLDOUT_POINTS, len(holdout), remaining)
|
||||
skipped = {"targetAnchorMissing": len(all_target) - len(target), "holdoutAnchorMissing": len(holdout_all) - len(holdout)}
|
||||
return target[:n_target], holdout[:n_holdout], seeds, skipped
|
||||
|
||||
|
||||
def _one_replay(point: dict, arm: str, seed: int, insertion: Optional[dict], rows: list) -> dict:
|
||||
result = replay.run_point(
|
||||
rows, int(point["point_row"]),
|
||||
insertion=insertion if arm == "candidate" else None,
|
||||
)
|
||||
action = scoring.action_of(result)
|
||||
cls = "no_action" if action is None else scoring.classify_action(point["ptype"], point, action)
|
||||
return {
|
||||
"pointId": point["id"], "episodeId": point.get("episodeId"),
|
||||
"stage": point.get("stage"), "ptype": point.get("ptype"),
|
||||
"arm": arm, "seed": seed, "classification": cls,
|
||||
"action": action, "secs": result.get("secs"), "error": result.get("error"),
|
||||
}
|
||||
|
||||
|
||||
class ReplayAborted(RuntimeError):
|
||||
"""重放没法给出可信结论(点不够 / 基础设施报错太多):不提交实验,只记运行日志。"""
|
||||
|
||||
|
||||
MAX_ERROR_RATE = 0.2
|
||||
|
||||
|
||||
def run_replays(cluster: dict, insertion: Optional[dict]) -> dict:
|
||||
target, holdout, seeds, skipped = _sample_points(cluster, insertion)
|
||||
if len(target) < config.OFFLINE_MIN_POINTS:
|
||||
raise ReplayAborted(
|
||||
f"候选的插入位置只在 {len(target)} 个目标决策点上能定位(需要 {config.OFFLINE_MIN_POINTS}),"
|
||||
f"{skipped['targetAnchorMissing']} 个点锚点缺失")
|
||||
tagged = [("target", p) for p in target] + [("holdout", p) for p in holdout]
|
||||
rows_by_id: Dict[str, list] = {p["id"]: mine.load_rows_for_point(p) for _, p in tagged}
|
||||
jobs = [
|
||||
(group, point, arm, seed)
|
||||
for group, point in tagged
|
||||
for arm in ("base", "candidate")
|
||||
for seed in range(seeds)
|
||||
]
|
||||
records: List[dict] = []
|
||||
if jobs:
|
||||
replay.prepare(rows_by_id[jobs[0][1]["id"]])
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=max(1, config.WORKERS)) as pool:
|
||||
future_map = {
|
||||
pool.submit(_one_replay, point, arm, seed, insertion, rows_by_id[point["id"]]):
|
||||
(group, point["id"])
|
||||
for group, point, arm, seed in jobs
|
||||
}
|
||||
config.log(f"重放:{len(target)} 个目标点 + {len(holdout)} 个保留集点,每点 2 组 × {seeds} 遍,共 {len(jobs)} 次")
|
||||
for future in concurrent.futures.as_completed(future_map):
|
||||
group, point_id = future_map[future]
|
||||
try:
|
||||
records.append({"group": group, **future.result()})
|
||||
except Exception as exc: # noqa: BLE001
|
||||
records.append({
|
||||
"group": group, "pointId": point_id, "arm": "unknown", "seed": None,
|
||||
"classification": "error", "error": str(exc)[:1000],
|
||||
})
|
||||
config.log(f" 重放 {len(records)}/{len(jobs)}")
|
||||
run_id = datetime.now().strftime("%Y%m%d-%H%M%S")
|
||||
run_dir = config.RUNS / run_id
|
||||
run_dir.mkdir(parents=True, exist_ok=False)
|
||||
run_dir.chmod(0o700)
|
||||
_write_jsonl(run_dir / "results.jsonl", records)
|
||||
errors = sum(1 for r in records if r.get("error"))
|
||||
if records and errors / len(records) > MAX_ERROR_RATE:
|
||||
raise ReplayAborted(f"重放报错 {errors}/{len(records)},超过 {int(MAX_ERROR_RATE * 100)}%,"
|
||||
f"不能把基础设施故障当成候选无效;原始结果见 {run_dir}")
|
||||
target_result = scoring.offline_summary(r for r in records if r.get("group") == "target")
|
||||
holdout_result = scoring.offline_summary(r for r in records if r.get("group") == "holdout")
|
||||
offline = {
|
||||
"target": target_result,
|
||||
"holdout": holdout_result,
|
||||
"meta": {
|
||||
"runId": run_id, "model": config.REPLAY_MODEL,
|
||||
"machine": os.uname().nodename, "calls": len(records),
|
||||
"errors": errors, **skipped,
|
||||
"scoring": "first-action-shape; paired bootstrap over decision points",
|
||||
"candidateApplication": "inserted at the same anchor line as in the template; points where the anchor is missing are excluded from both arms",
|
||||
},
|
||||
}
|
||||
(run_dir / "offline.json").write_text(json.dumps(offline, ensure_ascii=False, indent=2))
|
||||
os.chmod(run_dir / "offline.json", 0o600)
|
||||
return offline
|
||||
|
||||
|
||||
def _conclusion_prompt(exp: dict) -> str:
|
||||
payload = {
|
||||
"status": exp.get("status"), "title": exp.get("title"), "stage": exp.get("stage"),
|
||||
"summary": exp.get("summary"), "hypothesis": exp.get("hypothesis"),
|
||||
"method": exp.get("method"), "targetMetric": exp.get("targetMetric"),
|
||||
"offlineResult": exp.get("offlineResult"), "onlineResult": exp.get("onlineResult"),
|
||||
"decisionReason": exp.get("decisionReason"),
|
||||
}
|
||||
return f"""根据下面这条自动实验的确定性记录写实验结论。不得引入记录外的数字,
|
||||
不得把模型意见当真值;明确说假设是否得到支持、离线与真机复跑各发生了什么、下一轮该保留
|
||||
什么或避免什么。状态如果是离线未通过,就只下离线结论;lab_passed / lab_failed 是
|
||||
真机复跑的结论(评估集历史任务新旧 prompt 各跑一遍,onlineResult 里是配对结果;
|
||||
「自报修好」是 agent 自己说的,不能当成功证据)。两到四段中文纯文本。
|
||||
|
||||
{json.dumps(payload, ensure_ascii=False, indent=2)}"""
|
||||
|
||||
|
||||
def write_missing_conclusions(experiments: List[dict]) -> int:
|
||||
written = 0
|
||||
for exp in experiments:
|
||||
if exp.get("status") not in TERMINAL or exp.get("conclusion"):
|
||||
continue
|
||||
try:
|
||||
body = llm.ask(_conclusion_prompt(exp), timeout=900)
|
||||
posted = api.experiment_event(
|
||||
str(exp["id"]), f"实验结论:{exp.get('title')}", body[:12000], "conclusion",
|
||||
)
|
||||
written += int(posted)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(f"[rsi-loop] conclusion {exp.get('id')} failed: {exc}")
|
||||
return written
|
||||
|
||||
|
||||
def _finding_body(all_clusters: List[dict]) -> str:
|
||||
if not all_clusters:
|
||||
return "近 14 天没有审计出可聚合的问题模式。"
|
||||
lines = ["近 14 天问题模式(按重复失败调用数排序):"]
|
||||
for c in all_clusters[:10]:
|
||||
lines.append(
|
||||
f"- {c.get('clusterTitle')} (`{c.get('cluster')}`): {c.get('wasteCalls')} 次失败调用,"
|
||||
f"{c.get('episodes')} 个片段 / {c.get('transcripts')} 份会话;"
|
||||
f"{c.get('problemClass')},{c.get('stage')},重放型 {c.get('replayType')}"
|
||||
)
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
def run_once() -> dict:
|
||||
config.ensure_dirs()
|
||||
now_ms = int(time.time() * 1000)
|
||||
config.log("开始:读取控制面状态" + ("(试运行,不写生产)" if config.DRY_RUN else ""))
|
||||
state = api.get("/state")
|
||||
experiments = (api.get("/experiments?limit=500").get("experiments") or [])
|
||||
sync = mine.sync_transcripts(now_ms)
|
||||
mined = mine.mine_local()
|
||||
audited = mine.audit_new()
|
||||
consolidated = mine.consolidate_clusters()
|
||||
all_clusters = mine.clusters()
|
||||
config.log(f"聚类:{len(all_clusters)} 个问题模式")
|
||||
conclusions = write_missing_conclusions(experiments)
|
||||
# 复核意见挖掘与实验互不相干:暂停回路只停实验,不停打标签 / 归纳;失败也只记一笔
|
||||
mining = review_mining.run(now_ms)
|
||||
api.event("复核意见挖掘", review_mining.summary_line(mining) + "\n\n" + json.dumps(mining, ensure_ascii=False, indent=2), "note")
|
||||
api.event(
|
||||
f"发现 {len(all_clusters)} 个问题模式",
|
||||
_finding_body(all_clusters),
|
||||
"finding",
|
||||
)
|
||||
result = {
|
||||
"sync": sync, "mined": mined, "audited": audited, "consolidated": consolidated,
|
||||
"clusters": len(all_clusters), "conclusions": conclusions,
|
||||
"review_mining": mining,
|
||||
"submitted": None,
|
||||
}
|
||||
if (state.get("pause") or {}).get("paused"):
|
||||
api.event(
|
||||
"每日回路完成(已暂停,未提交实验)",
|
||||
json.dumps(result, ensure_ascii=False, indent=2),
|
||||
)
|
||||
return result
|
||||
cluster = _eligible_cluster(all_clusters, state, now_ms)
|
||||
busy = {e.get("platform") for e in state.get("live") or [] if e.get("stage") == "bug.run"}
|
||||
if not cluster:
|
||||
note = "没有可提交的 prompt 选题"
|
||||
else:
|
||||
note = _submit_prompt_experiment(cluster, now_ms, result)
|
||||
sub = result.get("submitted") or {}
|
||||
if sub.get("status") == "lab":
|
||||
busy.add(cluster.get("platform"))
|
||||
result["runtime"] = _submit_runtime_experiments(state, experiments, busy)
|
||||
if result["runtime"]:
|
||||
note += ";旋钮实验:" + "、".join(r.get("title") or "?" for r in result["runtime"])
|
||||
api.event(f"每日回路完成({note})", json.dumps(result, ensure_ascii=False, indent=2))
|
||||
return result
|
||||
|
||||
|
||||
def _submit_prompt_experiment(cluster: dict, now_ms: int, result: dict) -> str:
|
||||
"""写提案 → 离线重放 → 提交。返回给时间线的一句话。"""
|
||||
title = cluster.get("clusterTitle") or cluster.get("cluster")
|
||||
config.log(f"选题:{title}({cluster.get('platform')} · {cluster['stage']}),写提案中")
|
||||
# 按平台展开的现行版:模板里可能有只给某一端的段({{#ios}}…{{/ios}}),候选要插在展开后的版本上
|
||||
active = api.get(f"/prompt/{cluster['stage']}?platform={cluster.get('platform')}")
|
||||
draft = proposal.create(cluster, str(active["template"]))
|
||||
try:
|
||||
offline = run_replays(cluster, draft.pop("_insertion"))
|
||||
except ReplayAborted as exc:
|
||||
_record_skip(cluster, str(exc), now_ms)
|
||||
result["aborted"] = str(exc)
|
||||
return f"未提交:{title}:{exc}"
|
||||
draft["offline"] = offline
|
||||
response = api.post("/experiments", draft, timeout=180)
|
||||
exp = response.get("experiment") or {}
|
||||
result["submitted"] = {
|
||||
"id": exp.get("id"), "title": exp.get("title"),
|
||||
"status": exp.get("status"), "gate": response.get("gate"),
|
||||
}
|
||||
if exp.get("status") in TERMINAL:
|
||||
result["conclusions"] += write_missing_conclusions([exp])
|
||||
return f"提交 {draft['title']}"
|
||||
|
||||
|
||||
def _submit_runtime_experiments(state: dict, experiments: List[dict], busy: set) -> List[dict]:
|
||||
"""空着的实验机(没有进行中的 bug.run 实验)各提一个还没试过的旋钮取值。"""
|
||||
knobs = state.get("runtimeKnobs") or {}
|
||||
if not knobs or (None in busy): # 不带平台的旧实验占着所有平台
|
||||
return []
|
||||
out: List[dict] = []
|
||||
for platform in sorted(state.get("labNodes") or {}):
|
||||
if platform in busy:
|
||||
continue
|
||||
pick = runtime.next_candidate(platform, knobs, experiments)
|
||||
if not pick:
|
||||
continue
|
||||
draft = runtime.draft(platform, pick[0], pick[1], knobs)
|
||||
config.log(f"旋钮实验:{draft['title']}")
|
||||
try:
|
||||
exp = (api.post("/experiments", draft, timeout=180).get("experiment") or {})
|
||||
out.append({"id": exp.get("id"), "title": exp.get("title") or draft["title"], "status": exp.get("status")})
|
||||
except Exception as exc: # noqa: BLE001 一个平台提交失败不影响另一个
|
||||
out.append({"title": draft["title"], "error": str(exc)[:300]})
|
||||
return out
|
||||
|
||||
|
||||
def locked_run() -> dict:
|
||||
config.ensure_dirs()
|
||||
with open(config.LOCK, "w") as lock:
|
||||
try:
|
||||
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
||||
except BlockingIOError:
|
||||
raise RuntimeError("已有一个 RSI 回路进程在运行") from None
|
||||
lock.write(f"{os.getpid()}\n")
|
||||
lock.flush()
|
||||
try:
|
||||
return run_once()
|
||||
except Exception as exc:
|
||||
api.event("每日回路失败", str(exc)[:20000])
|
||||
raise
|
||||
|
||||
|
||||
def daemon() -> None:
|
||||
"""在图形桌面终端常驻;这样 claude 复用该登录会话的 OAuth。"""
|
||||
while True:
|
||||
now = datetime.now()
|
||||
target = now.replace(hour=config.DAILY_HOUR, minute=0, second=0, microsecond=0)
|
||||
if target <= now:
|
||||
target += timedelta(days=1)
|
||||
wait = max(1, (target - now).total_seconds())
|
||||
print(f"[rsi-loop] next run: {target.isoformat(timespec='seconds')}", flush=True)
|
||||
time.sleep(wait)
|
||||
try:
|
||||
print(json.dumps(locked_run(), ensure_ascii=False, indent=2), flush=True)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
print(f"[rsi-loop] run failed: {exc}", flush=True)
|
||||
@@ -0,0 +1,76 @@
|
||||
"""旋钮实验的选题:当天没有够格的 prompt 选题时,让空着的实验机去试执行配置包里还没试过的旋钮取值。
|
||||
|
||||
旋钮目录由控制面 /state 的 runtimeKnobs 下发(shared/src/executor_profile.ts),这里不另抄一份。
|
||||
一个(平台, 旋钮=取值)只试一次,不论结果:复跑通过的等人决定,没通过的换别的取值再说。
|
||||
实验机现行组已经是这个取值的不提(config.LAB_BASELINE),两组一样等于白跑。
|
||||
提案文字是确定性的模板,不调模型 —— 旋钮实验的「为什么值得试」就是目录里那句说明。
|
||||
"""
|
||||
import json
|
||||
from typing import Dict, List, Optional, Tuple
|
||||
|
||||
from . import config
|
||||
|
||||
TARGET_METRIC = "reach_s"
|
||||
|
||||
|
||||
def _profile_of(exp: dict) -> Dict[str, str]:
|
||||
raw = exp.get("candidateProfile")
|
||||
if not raw:
|
||||
return {}
|
||||
try:
|
||||
obj = json.loads(raw) if isinstance(raw, str) else raw
|
||||
except ValueError:
|
||||
return {}
|
||||
return {str(k): str(v) for k, v in obj.items()} if isinstance(obj, dict) else {}
|
||||
|
||||
|
||||
def tried(experiments: List[dict]) -> set:
|
||||
"""已经试过的 (平台, 旋钮, 取值)。只认单旋钮的配置包 —— 回路自己只提单旋钮。"""
|
||||
out = set()
|
||||
for e in experiments:
|
||||
if e.get("kind") != "runtime":
|
||||
continue
|
||||
prof = _profile_of(e)
|
||||
if len(prof) == 1:
|
||||
(k, v), = prof.items()
|
||||
out.add((e.get("platform"), k, v))
|
||||
return out
|
||||
|
||||
|
||||
def _ordered_values(knob: str, values: List[str]) -> List[str]:
|
||||
if knob == "effort":
|
||||
return [v for v in config.EFFORT_ORDER if v in values] + [v for v in values if v not in config.EFFORT_ORDER]
|
||||
return list(values)
|
||||
|
||||
|
||||
def next_candidate(platform: str, knobs: Dict[str, dict], experiments: List[dict]) -> Optional[Tuple[str, str]]:
|
||||
done = tried(experiments)
|
||||
base = config.LAB_BASELINE.get(platform) or {}
|
||||
for knob, d in knobs.items():
|
||||
only = d.get("platforms")
|
||||
if only and platform not in only:
|
||||
continue
|
||||
for v in _ordered_values(knob, list(d.get("values") or [])):
|
||||
if base.get(knob) == v or (platform, knob, v) in done:
|
||||
continue
|
||||
return knob, v
|
||||
return None
|
||||
|
||||
|
||||
def draft(platform: str, knob: str, value: str, knobs: Dict[str, dict]) -> dict:
|
||||
d = knobs.get(knob) or {}
|
||||
label = d.get("label") or knob
|
||||
plat = "iOS" if platform == "ios" else "Android"
|
||||
base = (config.LAB_BASELINE.get(platform) or {}).get(knob)
|
||||
base_txt = f"实验机现行为 {base}" if base else "实验机现行为生产默认(不设)"
|
||||
return {
|
||||
"stage": "bug.run", "platform": platform, "targetMetric": TARGET_METRIC,
|
||||
"title": f"{plat}:{label}调到 {value}",
|
||||
"summary": f"{d.get('doc') or ''}\n\n当天没有够格的 prompt 选题,实验机空着;这一项是旋钮目录里 {plat} 上还没试过的取值。",
|
||||
"hypothesis": f"把{label}从现状({base_txt})改为 {value},能缩短到达复现现场的用时;"
|
||||
"风险由复跑的两条底线兜住:自报修好、到达复现现场的个数不少于现行。",
|
||||
"method": f"运行时实验:两组同一版 prompt;候选组的执行配置包为 {knob}={value},其余不变。"
|
||||
"在实验机上拿评估集的历史任务配对复跑,按到达复现现场用时判定。",
|
||||
"candidateProfile": {knob: value},
|
||||
"problemClass": None, "cluster": None,
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
"""三类决策点的「坏选择」判定 —— 和 shared/src/rsi_loop.ts 的指标口径同源,但这里判的是
|
||||
重放出来的「下一步动作」,不是整批的聚合指标。
|
||||
|
||||
三类(2026-09 审计确认的真失败形态):
|
||||
T2_wait 等 App 就绪:下一步该用 wait-for / 后台 / Monitor,不该手写 sleep / 轮询循环;
|
||||
T1_repeat 同一堵墙已经失败过:下一步不该用同族命令原样再撞;
|
||||
T3_frida Frida 首次报错后:下一步不该直接再注入,应先诊断(frida-server / 版本 / 进程 / 读代码)。
|
||||
|
||||
判定只看动作形态,封闭、可审计 —— 它是离线闸的真值,不能是「另一个模型觉得好不好」。
|
||||
"""
|
||||
import re
|
||||
import random
|
||||
from collections import defaultdict
|
||||
from typing import Iterable, Optional
|
||||
|
||||
UI_PROBE = re.compile(r"dumpsys\s+(window|activity)|uiautomator|mCurrentFocus|ResumedActivity|\bpidof\b|\bactl\b")
|
||||
|
||||
|
||||
def _is_long_poll(cmd: str) -> bool:
|
||||
if re.search(r"\b(while|until|for)\b.*\bsleep\b", cmd, re.S):
|
||||
return True
|
||||
return any(int(x) >= 15 for x in re.findall(r"\bsleep\s+(\d+)", cmd))
|
||||
|
||||
|
||||
def classify_action(ptype: str, point: dict, action: Optional[dict]) -> str:
|
||||
"""返回 bad / good / other。point 至少含 'fam'(截断处那条失败命令的命令族)。"""
|
||||
if action is None:
|
||||
return "other"
|
||||
name = action.get("name", "")
|
||||
inp = action.get("input") or {}
|
||||
cmd = str(inp.get("command", "")) if name == "Bash" else ""
|
||||
if ptype == "T2_wait":
|
||||
if "wait-for" in cmd:
|
||||
return "good"
|
||||
if name in ("Monitor",) or inp.get("run_in_background"):
|
||||
return "good"
|
||||
if name == "Bash" and _is_long_poll(cmd):
|
||||
return "bad"
|
||||
return "other"
|
||||
if ptype == "T3_frida":
|
||||
if name == "Bash" and re.search(r"\bfrida\b", cmd) and not re.search(r"frida-ps|frida-ls|--version|pgrep|pidof|getprop", cmd):
|
||||
return "bad"
|
||||
return "good"
|
||||
if ptype == "T1_repeat":
|
||||
from .transcript import family
|
||||
now = family(name, cmd) if name == "Bash" else name
|
||||
previous = str(point.get("fam") or "")
|
||||
return "bad" if now and now == previous else "good"
|
||||
return "other"
|
||||
|
||||
|
||||
def action_of(replay_result: dict) -> Optional[dict]:
|
||||
for b in replay_result.get("first") or []:
|
||||
if b.get("type") == "tool_use":
|
||||
return {"name": b["name"], "input": b.get("input") or {}}
|
||||
return None
|
||||
|
||||
|
||||
def _mean(values):
|
||||
return sum(values) / len(values) if values else 0.0
|
||||
|
||||
|
||||
def _quantile(values, q: float) -> float:
|
||||
if not values:
|
||||
return 0.0
|
||||
ordered = sorted(values)
|
||||
pos = (len(ordered) - 1) * q
|
||||
lo = int(pos)
|
||||
hi = min(len(ordered) - 1, lo + 1)
|
||||
frac = pos - lo
|
||||
return ordered[lo] * (1 - frac) + ordered[hi] * frac
|
||||
|
||||
|
||||
def offline_summary(records: Iterable[dict], bootstrap_iters: int = 2000, seed: int = 7) -> dict:
|
||||
"""按决策点配对计算 baseBad-candBad,并对「点」而不是单次 seed 做 bootstrap。"""
|
||||
arms = defaultdict(lambda: {"base": [], "candidate": []})
|
||||
seed_values = set()
|
||||
for row in records:
|
||||
arm = row.get("arm")
|
||||
if arm not in ("base", "candidate") or not row.get("pointId"):
|
||||
continue
|
||||
# 续跑失败和没有动作都不算候选的成功;按坏选择处理,避免靠沉默刷过离线闸。
|
||||
bad = 0.0 if row.get("classification") in ("good", "other") and not row.get("error") else 1.0
|
||||
arms[row["pointId"]][arm].append(bad)
|
||||
if row.get("seed") is not None:
|
||||
seed_values.add(row["seed"])
|
||||
paired = [v for v in arms.values() if v["base"] and v["candidate"]]
|
||||
diffs = [_mean(v["base"]) - _mean(v["candidate"]) for v in paired]
|
||||
base_values = [x for v in paired for x in v["base"]]
|
||||
cand_values = [x for v in paired for x in v["candidate"]]
|
||||
boot = []
|
||||
if diffs:
|
||||
rng = random.Random(seed)
|
||||
for _ in range(bootstrap_iters):
|
||||
boot.append(_mean([diffs[rng.randrange(len(diffs))] for _ in diffs]))
|
||||
return {
|
||||
"points": len(paired),
|
||||
"seeds": len(seed_values),
|
||||
"baseBad": _mean(base_values),
|
||||
"candBad": _mean(cand_values),
|
||||
"diffMean": _mean(diffs),
|
||||
"ciLow": _quantile(boot, 0.025),
|
||||
"ciHigh": _quantile(boot, 0.975),
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
"""原生会话记录的解析:工具调用序列、命令族、报错「墙」、后台任务、时间线渲染。
|
||||
|
||||
这些规则都是在 2026-09 的实验里一条条踩出来的(见 docs/rsi.md「自动实验回路」):
|
||||
- 读取类命令(cat / grep / sed / Read)输出里的报错字样是源码或历史记录,不是失败 ——
|
||||
不排除它们,切片误报率是 25%;
|
||||
- 命令超时被自动转到后台(moved to the background)和主动 run_in_background 一样,
|
||||
截断时还在跑的话,续跑会收到「任务没跑完」的通知,处境就变了 —— 这种点不能用;
|
||||
- Monitor 的启动回执(you will be notified on each event)不是报错。
|
||||
"""
|
||||
import json
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
ERR = re.compile(r"(^|\n)\s*(error|fatal|exception|traceback|failed)\b|command not found|No such file|Exit code [1-9]|"
|
||||
r"exit status [1-9]|BUILD FAILED|Failed to (spawn|attach)|unable to", re.I)
|
||||
WALLS = re.compile(r"Failed to (?:spawn|attach|enumerate|inject|load)[^\n]{0,80}|unable to (?:connect|find|access)[^\n]{0,60}|"
|
||||
r"INSTALL_FAILED_\w+|device (?:offline|not found|unauthorized)|no devices/emulators found|Permission denied|"
|
||||
r"Process (?:terminated|crashed)|java\.lang\.\w+(?:Exception|Error)|ReferenceError[^\n]{0,60}|TypeError[^\n]{0,60}|"
|
||||
r"Connection refused|timed out|BUILD FAILED|Compilation error|error: [^\n]{0,80}", re.I)
|
||||
READ_ONLY = re.compile(r"^(grep|rg|ls|cat|head|tail|find|test|\[|echo|which|command|pgrep|ps|wc|stat|file|du|diff|sed|awk|jq|"
|
||||
r"sort|true|false|printf|date|tree|readlink|strings|nl|less|more)\b")
|
||||
LAUNCH = re.compile(r"(?:running in background|moved to the background)[^\"]{0,40}?\(?ID[:\s]+([a-z0-9]{6,})", re.I)
|
||||
DONE = [re.compile(r"<task-id>([a-z0-9]{6,})</task-id>[\s\S]{0,600}?<status>(?:completed|failed|stopped|killed)"),
|
||||
re.compile(r"Successfully stopped task:\s*([a-z0-9]{6,})"),
|
||||
re.compile(r'\\"task_id\\":\\"([a-z0-9]{6,})\\"[^}]{0,200}?\\"status\\":\\"(?:completed|failed|killed|stopped)')]
|
||||
|
||||
|
||||
def load_rows(path: Path) -> List[Optional[dict]]:
|
||||
rows: List[Optional[dict]] = []
|
||||
with open(path, errors="replace") as f:
|
||||
for line in f:
|
||||
try:
|
||||
rows.append(json.loads(line))
|
||||
except Exception: # noqa: BLE001
|
||||
rows.append(None)
|
||||
return rows
|
||||
|
||||
|
||||
def text_of(content: Any) -> str:
|
||||
if isinstance(content, str):
|
||||
return content
|
||||
return "\n".join(b.get("text", "") for b in content or [] if isinstance(b, dict) and b.get("type") == "text")
|
||||
|
||||
|
||||
def first_prompt(rows: List[Optional[dict]]) -> Optional[dict]:
|
||||
"""第一条用户消息所在的行(执行 prompt 本身)。"""
|
||||
for r in rows:
|
||||
if r and r.get("type") == "user" and not r.get("isMeta"):
|
||||
c = (r.get("message") or {}).get("content")
|
||||
if isinstance(c, str) or (isinstance(c, list) and any(isinstance(b, dict) and b.get("type") == "text" for b in c)):
|
||||
return r
|
||||
return None
|
||||
|
||||
|
||||
def classify_stage(prompt_text: str) -> Optional[str]:
|
||||
head = prompt_text[:600]
|
||||
if re.search(r"自主\s*Bug\s*修复|autonomous bug[- ]?fix", head, re.I):
|
||||
return "bug.run"
|
||||
if re.search(r"自主\s*Feature\s*实现|autonomous feature", head, re.I):
|
||||
return "feature.run.second" if re.search(r"再实现|re-?implement", prompt_text[:1500], re.I) else "feature.run.first"
|
||||
return None
|
||||
|
||||
|
||||
def family(tool: str, cmd: str) -> str:
|
||||
if tool != "Bash":
|
||||
return tool
|
||||
s = re.sub(r"^\s*(nohup|exec)\s+", "", cmd)
|
||||
if re.search(r"\bfrida\b(?!-server)", s) and not re.search(r"frida-(ps|ls)", s):
|
||||
return "frida"
|
||||
if re.search(r"\bwait-for\b", s):
|
||||
return "wait-for"
|
||||
if re.search(r"python3?\s*(-c|<<)", s):
|
||||
return "python-inline"
|
||||
s = re.sub(r'"?\$\{?\w+\}?"?(?=\s+(shell|install|push|pull|logcat|-s)\b)', "adb", s)
|
||||
s = re.sub(r"/[\w./@~+-]+/", "", s)
|
||||
parts = [p.strip() for p in re.split(r"&&|;|\|\||\n", s) if p.strip()]
|
||||
first = next((p for p in parts if not re.match(r"^(cd|export|set|source|sleep|echo)\b|^[A-Za-z_]+=", p)), parts[0] if parts else "")
|
||||
toks = [t for t in first.split() if not re.match(r"^\w+=", t) and t not in ("timeout", "gtimeout", "sudo", "env")]
|
||||
if toks and toks[0] == "adb":
|
||||
rest, i = [], 1
|
||||
while i < len(toks):
|
||||
if re.match(r"^-[stHP]$", toks[i]):
|
||||
i += 2
|
||||
continue
|
||||
if not toks[i].startswith("-"):
|
||||
rest.append(toks[i])
|
||||
i += 1
|
||||
return " ".join(["adb"] + rest[:2])
|
||||
if toks and toks[0] in ("bun", "node", "python3", "python", "bash", "sh") and len(toks) >= 2:
|
||||
return toks[0] + " " + Path(toks[1].strip("\"'")).name
|
||||
return " ".join(toks[:2])
|
||||
|
||||
|
||||
def wall(txt: str) -> Optional[str]:
|
||||
if re.search(r"you will be notified on each", txt, re.I):
|
||||
return None
|
||||
m = WALLS.search(txt[-6000:])
|
||||
if not m:
|
||||
return None
|
||||
s = m.group(0)[:100]
|
||||
s = re.sub(r"/[\w./@-]+", "<p>", s)
|
||||
s = re.sub(r"\b[0-9a-f]{6,}\b", "<h>", s)
|
||||
return re.sub(r"\d+", "#", s).strip()
|
||||
|
||||
|
||||
def is_read_only(tool: str, cmd: str) -> bool:
|
||||
if tool in ("Read", "Grep", "Glob", "LSP", "ToolSearch"):
|
||||
return True
|
||||
if tool != "Bash":
|
||||
return False
|
||||
first = re.split(r"&&|;|\|", re.sub(r"^\s*cd\s+\S+\s*&&\s*", "", cmd).strip())[0].strip()
|
||||
return bool(READ_ONLY.match(first))
|
||||
|
||||
|
||||
def calls(rows: List[Optional[dict]]) -> List[dict]:
|
||||
"""按工具结果的顺序展开调用:序号 i、所在行、工具、命令、成败、墙、当时的上下文 token、时刻。"""
|
||||
uses: Dict[str, tuple] = {}
|
||||
out: List[dict] = []
|
||||
ctx = 0
|
||||
for ri, r in enumerate(rows):
|
||||
if not r:
|
||||
continue
|
||||
m = r.get("message") or {}
|
||||
c = m.get("content")
|
||||
if r.get("type") == "assistant":
|
||||
u = m.get("usage") or {}
|
||||
if u:
|
||||
ctx = u.get("input_tokens", 0) + u.get("cache_read_input_tokens", 0) + u.get("cache_creation_input_tokens", 0)
|
||||
for b in c or []:
|
||||
if isinstance(b, dict) and b.get("type") == "tool_use":
|
||||
uses[b["id"]] = (b.get("name") or "?", b.get("input") or {}, r.get("timestamp"))
|
||||
if r.get("type") == "user" and isinstance(c, list):
|
||||
for b in c:
|
||||
if not (isinstance(b, dict) and b.get("type") == "tool_result"):
|
||||
continue
|
||||
name, inp, t0 = uses.get(b.get("tool_use_id"), ("?", {}, None))
|
||||
txt = text_of(b.get("content"))
|
||||
cmd = str(inp.get("command", "")) if name == "Bash" else json.dumps(inp, ensure_ascii=False)
|
||||
ro = is_read_only(name, cmd)
|
||||
failed = bool(b.get("is_error")) or (name == "Bash" and not ro and bool(ERR.search(txt[-3000:])))
|
||||
out.append(dict(i=len(out), row=ri, tool=name, cmd=cmd, fam=family(name, cmd), ok=not failed,
|
||||
read_only=ro, wall=None if ro else wall(txt), ctx=ctx, t=r.get("timestamp"), t0=t0,
|
||||
bg=bool(re.search(r"moved to the background|running in background", txt[:400])),
|
||||
err=([l for l in txt.strip().splitlines() if l.strip()] or [""])[-1][:160] if failed else ""))
|
||||
return out
|
||||
|
||||
|
||||
def pending_bg(rows: List[Optional[dict]], upto_row: int) -> List[str]:
|
||||
launched: List[str] = []
|
||||
done = set()
|
||||
for r in rows[: upto_row + 1]:
|
||||
if not r or r.get("type") != "user":
|
||||
continue
|
||||
s = json.dumps((r.get("message") or {}).get("content"), ensure_ascii=False)
|
||||
launched += [m.group(1) for m in LAUNCH.finditer(s)]
|
||||
for pat in DONE:
|
||||
done |= {m.group(1) for m in pat.finditer(s)}
|
||||
return sorted({k for k in launched if k not in done})
|
||||
|
||||
|
||||
def has_env_attachment(rows: List[Optional[dict]]) -> bool:
|
||||
return any(r and r.get("type") == "attachment" and (r.get("attachment") or {}).get("type") == "environment" for r in rows[:300])
|
||||
|
||||
|
||||
def clip(s: str, a: int, b: int) -> str:
|
||||
s = s or ""
|
||||
return s if len(s) <= a + b + 20 else s[:a] + f"\n…[省略 {len(s) - a - b} 字]…\n" + s[-b:]
|
||||
|
||||
|
||||
def render_timeline(rows: List[Optional[dict]], start_call: int, end_call: int, before: int = 12, after: int = 6,
|
||||
max_chars: int = 140_000) -> str:
|
||||
"""审计用的时间线:片段前后各带几步上下文,调用编号 #N 与 calls() 的序号一致。"""
|
||||
uses: Dict[str, tuple] = {}
|
||||
out: List[str] = []
|
||||
i = -1
|
||||
lo, hi = start_call - before, end_call + after
|
||||
pending_text: List[str] = []
|
||||
for r in rows:
|
||||
if not r:
|
||||
continue
|
||||
m = r.get("message") or {}
|
||||
c = m.get("content")
|
||||
if r.get("type") == "assistant" and isinstance(c, list):
|
||||
for b in c:
|
||||
if not isinstance(b, dict):
|
||||
continue
|
||||
if b.get("type") == "tool_use":
|
||||
uses[b["id"]] = (b.get("name"), b.get("input") or {})
|
||||
elif b.get("type") == "text" and b.get("text", "").strip():
|
||||
pending_text.append(b["text"].strip())
|
||||
if r.get("type") == "user" and isinstance(c, list):
|
||||
for b in c:
|
||||
if not (isinstance(b, dict) and b.get("type") == "tool_result"):
|
||||
continue
|
||||
i += 1
|
||||
if lo <= i <= hi:
|
||||
name, inp = uses.get(b.get("tool_use_id"), ("?", {}))
|
||||
cmd = inp.get("command") if name == "Bash" else json.dumps(inp, ensure_ascii=False)
|
||||
out += [f"💬 {clip(t, 400, 200)}" for t in pending_text]
|
||||
mark = " ⟵片段内" if start_call <= i <= end_call else ""
|
||||
out.append(f"### [#{i}] {name}{mark} {str(r.get('timestamp', ''))[11:19]}\n```\n{clip(str(cmd), 900, 300)}\n```\n"
|
||||
f"↩︎ {'(is_error) ' if b.get('is_error') else ''}\n```\n{clip(text_of(b.get('content')), 700, 700)}\n```")
|
||||
pending_text = []
|
||||
s = "\n\n".join(out)
|
||||
return s if len(s) <= max_chars else s[:max_chars] + "\n…[时间线过长,已截断]"
|
||||
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
cd "$(dirname "$0")"
|
||||
cmd="${1:-once}"
|
||||
[ $# -gt 0 ] && shift
|
||||
exec /usr/bin/python3 -m rsi_loop "$cmd" "$@"
|
||||
@@ -0,0 +1 @@
|
||||
"""Unit tests for the standalone RSI loop."""
|
||||
@@ -0,0 +1,481 @@
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from unittest import mock
|
||||
|
||||
from rsi_loop import config, episodes, llm, mine, proposal, replay, runner, runtime, scoring, transcript
|
||||
from rsi_loop.runner import _eligible_cluster
|
||||
|
||||
LAB_NODES = {"android": "evaluator-android-01.shiro0.icu", "ios": "temp-ios-01"}
|
||||
|
||||
|
||||
def assistant(tool_id, name, tool_input, tokens=1000):
|
||||
return {
|
||||
"type": "assistant",
|
||||
"message": {
|
||||
"usage": {"input_tokens": tokens},
|
||||
"content": [{"type": "tool_use", "id": tool_id, "name": name, "input": tool_input}],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def result(tool_id, text, is_error=False, timestamp="2026-09-01T00:00:00Z"):
|
||||
return {
|
||||
"type": "user", "timestamp": timestamp,
|
||||
"message": {"content": [{
|
||||
"type": "tool_result", "tool_use_id": tool_id,
|
||||
"content": text, "is_error": is_error,
|
||||
}]},
|
||||
}
|
||||
|
||||
|
||||
class TranscriptTests(unittest.TestCase):
|
||||
def test_read_only_output_does_not_become_failure(self):
|
||||
rows = [
|
||||
assistant("a", "Bash", {"command": "grep -R error src"}),
|
||||
result("a", "error: source fixture\nExit code 1"),
|
||||
]
|
||||
call = transcript.calls(rows)[0]
|
||||
self.assertTrue(call["read_only"])
|
||||
self.assertTrue(call["ok"])
|
||||
self.assertIsNone(call["wall"])
|
||||
|
||||
def test_repeated_failure_point_is_after_first_failure(self):
|
||||
rows = [
|
||||
assistant("a", "Bash", {"command": "adb install app.apk"}),
|
||||
result("a", "INSTALL_FAILED_VERSION_DOWNGRADE", True),
|
||||
assistant("b", "Bash", {"command": "adb install app.apk"}),
|
||||
result("b", "INSTALL_FAILED_VERSION_DOWNGRADE", True),
|
||||
]
|
||||
found = episodes.find_episodes(rows)
|
||||
self.assertEqual(len(found), 1)
|
||||
episode = found[0]
|
||||
self.assertEqual(episode["n_fail"], 2)
|
||||
self.assertEqual(episode["point_call"], 0)
|
||||
self.assertEqual(episode["point_row"], 1)
|
||||
|
||||
def test_wait_point_is_before_wait_command(self):
|
||||
rows = [
|
||||
assistant("a", "Bash", {"command": "adb shell input tap 1 2"}),
|
||||
result("a", "ok"),
|
||||
assistant("b", "Bash", {"command": "until adb shell dumpsys window; do sleep 5; done"}),
|
||||
result("b", "done"),
|
||||
]
|
||||
point = episodes.harvest_wait_points(rows)[0]
|
||||
self.assertEqual(point["point_call"], 0)
|
||||
self.assertEqual(point["point_row"], 1)
|
||||
|
||||
|
||||
class ProposalTests(unittest.TestCase):
|
||||
def test_only_inserts_and_preserves_placeholders(self):
|
||||
base = "A {{task}}\nB\nC {{round}}"
|
||||
candidate = proposal.apply_insertion(base, "新增一\n新增二", "B")
|
||||
self.assertEqual(candidate, "A {{task}}\nB\n新增一\n新增二\nC {{round}}")
|
||||
self.assertIsNone(proposal.candidate_error(base, candidate))
|
||||
self.assertEqual(proposal.inserted_text(base, candidate), "新增一\n新增二")
|
||||
|
||||
def test_rejects_changed_line_and_ambiguous_anchor(self):
|
||||
base = "A\nB\nB"
|
||||
with self.assertRaises(ValueError):
|
||||
proposal.apply_insertion(base, "X", "B")
|
||||
self.assertIsNotNone(proposal.candidate_error("A\nB", "A\nB changed"))
|
||||
|
||||
def test_reach_target_cannot_touch_evidence_capture(self):
|
||||
base = "步骤 3\n复现问题。\n步骤 4"
|
||||
shoot = proposal.apply_insertion(base, "- 启动 App 后立刻 record-evidence 拍一张 before-1-x.png", "复现问题。")
|
||||
route = proposal.apply_insertion(base, "- 先按 TestSpec 的入口用 deeplink 直达目标页", "复现问题。")
|
||||
self.assertIsNotNone(proposal.gaming_error("reach_s", base, shoot))
|
||||
self.assertIsNotNone(proposal.gaming_error("reach_miss", base, shoot))
|
||||
self.assertIsNone(proposal.gaming_error("reach_s", base, route))
|
||||
self.assertIsNone(proposal.gaming_error("evidence_fail", base, shoot))
|
||||
|
||||
def test_business_metrics_are_registrable_for_every_replay_type(self):
|
||||
for ptype, allowed in proposal.METRICS_BY_REPLAY.items():
|
||||
self.assertTrue(allowed & proposal.BUSINESS_METRICS, ptype)
|
||||
self.assertTrue(allowed <= proposal.TARGET_METRICS, ptype)
|
||||
|
||||
def test_nested_json_reply_is_parsed(self):
|
||||
with mock.patch.object(llm, "ask", return_value='```json\n{"a":{"b":1}}\n```'):
|
||||
self.assertEqual(llm.ask_json("x"), {"a": {"b": 1}})
|
||||
|
||||
def test_online_metric_must_match_offline_action_shape(self):
|
||||
value = {
|
||||
"title": "x", "summary": "x", "hypothesis": "x", "method": "x",
|
||||
"targetMetric": "tool_calls", "afterLine": None, "content": "新增纪律",
|
||||
}
|
||||
cluster = {
|
||||
"stage": "bug.run", "problemClass": "decision_error",
|
||||
"cluster": "wait", "clusterTitle": "等待", "replayType": "T2_wait",
|
||||
"wasteCalls": 3, "episodes": 1, "transcripts": 1, "samples": [],
|
||||
}
|
||||
with mock.patch.object(llm, "ask_json", return_value=value):
|
||||
with self.assertRaisesRegex(ValueError, "不对应"):
|
||||
proposal.create(cluster, "base")
|
||||
|
||||
|
||||
class ScoringTests(unittest.TestCase):
|
||||
def test_action_shape_rules(self):
|
||||
self.assertEqual(scoring.classify_action(
|
||||
"T2_wait", {}, {"name": "Bash", "input": {"command": "sleep 30"}},
|
||||
), "bad")
|
||||
self.assertEqual(scoring.classify_action(
|
||||
"T2_wait", {}, {"name": "Bash", "input": {"command": "node wait-for.js"}},
|
||||
), "good")
|
||||
self.assertEqual(scoring.classify_action(
|
||||
"T3_frida", {}, {"name": "Bash", "input": {"command": "frida -U -n app -l hook.js"}},
|
||||
), "bad")
|
||||
self.assertEqual(scoring.classify_action(
|
||||
"T3_frida", {}, {"name": "Bash", "input": {"command": "frida --version"}},
|
||||
), "good")
|
||||
point = {"fam": "adb install app.apk"}
|
||||
self.assertEqual(scoring.classify_action(
|
||||
"T1_repeat", point, {"name": "Bash", "input": {"command": "adb install app.apk"}},
|
||||
), "bad")
|
||||
self.assertEqual(scoring.classify_action(
|
||||
"T1_repeat", point, {"name": "Bash", "input": {"command": "adb shell getprop"}},
|
||||
), "good")
|
||||
|
||||
def test_paired_bootstrap_is_deterministic_and_positive(self):
|
||||
records = []
|
||||
for point in range(20):
|
||||
for seed in range(2):
|
||||
records.append({"pointId": str(point), "arm": "base", "seed": seed, "classification": "bad"})
|
||||
records.append({"pointId": str(point), "arm": "candidate", "seed": seed, "classification": "good"})
|
||||
a = scoring.offline_summary(records)
|
||||
b = scoring.offline_summary(records)
|
||||
self.assertEqual(a, b)
|
||||
self.assertEqual(a["points"], 20)
|
||||
self.assertEqual(a["seeds"], 2)
|
||||
self.assertGreater(a["ciLow"], 0)
|
||||
|
||||
|
||||
class AuditAndSelectionTests(unittest.TestCase):
|
||||
def test_audit_requires_verbatim_evidence(self):
|
||||
raw = {
|
||||
"classification": "decision_error", "cluster": "repeat_same_command",
|
||||
"clusterTitle": "重复撞墙", "replayType": "T1_repeat",
|
||||
"correctInfoStep": 3, "evidence": ["Exit code 1"], "rationale": "已出现错误",
|
||||
}
|
||||
self.assertIsNotNone(mine._valid_audit(raw, "tool result: Exit code 1"))
|
||||
raw["evidence"] = ["模型编造的引文"]
|
||||
self.assertIsNone(mine._valid_audit(raw, "tool result: Exit code 1"))
|
||||
|
||||
def test_live_stage_and_cooldown_are_skipped(self):
|
||||
now = 2_000_000_000_000
|
||||
clusters = [
|
||||
{"stage": "bug.run", "platform": "android", "cluster": "a", "replayType": "T1_repeat"},
|
||||
{"stage": "bug.run", "platform": "ios", "cluster": "b", "replayType": "T2_wait"},
|
||||
{"stage": "bug.run", "platform": "ios", "cluster": "c", "replayType": "T3_frida"},
|
||||
]
|
||||
state = {
|
||||
"labNodes": LAB_NODES,
|
||||
"live": [{"stage": "bug.run", "platform": "android"}],
|
||||
"recent": [{"stage": "bug.run", "platform": "ios", "cluster": "b", "createdAt": now}],
|
||||
}
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)):
|
||||
self.assertEqual(_eligible_cluster(clusters, state, now, point_count=lambda c: 30)["cluster"], "c")
|
||||
|
||||
def test_cluster_without_enough_points_is_skipped(self):
|
||||
now = 2_000_000_000_000
|
||||
clusters = [
|
||||
{"stage": "bug.run", "platform": "android", "cluster": "few", "replayType": "T1_repeat"},
|
||||
{"stage": "bug.run", "platform": "android", "cluster": "many", "replayType": "T1_repeat"},
|
||||
]
|
||||
counts = {"few": 5, "many": 40}
|
||||
state = {"labNodes": LAB_NODES}
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)):
|
||||
got = _eligible_cluster(clusters, state, now, point_count=lambda c: counts[c["cluster"]])
|
||||
self.assertEqual(got["cluster"], "many")
|
||||
runner._record_skip(clusters[1], "anchor missing", now)
|
||||
self.assertIsNone(_eligible_cluster(clusters, state, now, point_count=lambda c: counts[c["cluster"]]))
|
||||
|
||||
|
||||
class ReplayFidelityTests(unittest.TestCase):
|
||||
PROMPT = "你是自主 Bug 修复 Agent。\n工作区在 /Users/fei/bug_workspace/123。\n## 等待\n用 wait-for。\n## 结尾"
|
||||
|
||||
def test_insert_at_anchor_with_placeholder_and_no_heading(self):
|
||||
ins = {"afterLine": "工作区在 {{workspace}}。", "content": "新纪律一\n新纪律二"}
|
||||
out = replay.insert_into_text(self.PROMPT, ins)
|
||||
lines = out.split("\n")
|
||||
self.assertEqual(lines[1], "工作区在 /Users/fei/bug_workspace/123。")
|
||||
self.assertEqual(lines[2:4], ["新纪律一", "新纪律二"])
|
||||
self.assertNotIn("候选", out)
|
||||
|
||||
def test_null_anchor_appends_at_end_and_missing_anchor_raises(self):
|
||||
out = replay.insert_into_text(self.PROMPT, {"afterLine": None, "content": "末尾纪律"})
|
||||
self.assertTrue(out.endswith("## 结尾\n末尾纪律"))
|
||||
with self.assertRaises(replay.AnchorMissing):
|
||||
replay.insert_into_text(self.PROMPT, {"afterLine": "不存在的一行", "content": "x"})
|
||||
|
||||
def test_candidate_applies_checks_first_prompt(self):
|
||||
rows = [{"type": "user", "message": {"role": "user", "content": self.PROMPT}}]
|
||||
self.assertTrue(replay.candidate_applies(rows, {"afterLine": "## 等待", "content": "x"}))
|
||||
self.assertFalse(replay.candidate_applies(rows, {"afterLine": "## 不存在", "content": "x"}))
|
||||
self.assertTrue(replay.candidate_applies(rows, None))
|
||||
|
||||
def test_environment_snapshot_uses_this_runs_cwd(self):
|
||||
rows = [
|
||||
{"type": "user", "message": {"role": "user", "content": self.PROMPT}},
|
||||
{"type": "attachment", "attachment": {"type": "environment",
|
||||
"snapshot": {"workingDirectory": "/Users/fei/ai_workspace"},
|
||||
"changes": [{"field": "workingDirectory"}]}},
|
||||
]
|
||||
loc = {"environment": {"type": "environment", "snapshot": {"workingDirectory": "/probe/dir", "platform": "darwin"}}}
|
||||
out, _ = replay._neutralize(rows, "sid", loc, Path("/runs/abc"), Path("/proj/abc"),
|
||||
{"afterLine": "## 等待", "content": "新纪律"})
|
||||
env = out[1]["attachment"]
|
||||
self.assertEqual(env["snapshot"]["workingDirectory"], "/runs/abc")
|
||||
self.assertNotIn("changes", env)
|
||||
self.assertIn("## 等待\n新纪律\n用 wait-for。", out[0]["message"]["content"])
|
||||
|
||||
|
||||
class ClusterConsolidationTests(unittest.TestCase):
|
||||
def _audits(self, d):
|
||||
rows = [
|
||||
{"episodeId": "e1", "stage": "bug.run", "classification": "decision_error", "cluster": "app_ready_blind_wait", "clusterTitle": "盲等就绪", "replayType": "T2_wait"},
|
||||
{"episodeId": "e2", "stage": "bug.run", "classification": "decision_error", "cluster": "fixed_sleep_before_ready", "clusterTitle": "定长 sleep 等就绪", "replayType": "T2_wait"},
|
||||
{"episodeId": "e3", "stage": "bug.run", "classification": "decision_error", "cluster": "frida_retry", "clusterTitle": "Frida 原样重试", "replayType": "T3_frida"},
|
||||
]
|
||||
(Path(d) / "a.jsonl").write_text("\n".join(json.dumps(r, ensure_ascii=False) for r in rows))
|
||||
|
||||
def test_canon_follows_chain_and_stops_on_cycle(self):
|
||||
self.assertEqual(mine.canon("a", {"a": "b", "b": "c"}), "c")
|
||||
self.assertIn(mine.canon("a", {"a": "b", "b": "a"}), ("a", "b"))
|
||||
self.assertEqual(mine.canon("x", {}), "x")
|
||||
|
||||
def test_consolidation_validates_and_merges(self):
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)), \
|
||||
mock.patch.object(config, "AUDITS", Path(d) / "a.jsonl"):
|
||||
self._audits(d)
|
||||
reply = {"merge": {
|
||||
"fixed_sleep_before_ready": "app_ready_blind_wait", # 同机制、同重放型 → 并
|
||||
"frida_retry": "app_ready_blind_wait", # 重放型不同 → 拒
|
||||
"app_ready_blind_wait": "made_up_name", # 规范名不存在 → 拒
|
||||
}}
|
||||
with mock.patch.object(llm, "ask_json", return_value=reply):
|
||||
r = mine.consolidate_clusters()
|
||||
self.assertEqual(r["merged"], 1)
|
||||
names = {k["cluster"]: k["count"] for k in mine.known_clusters()}
|
||||
self.assertEqual(names, {"app_ready_blind_wait": 2, "frida_retry": 1})
|
||||
|
||||
def test_audit_prompt_lists_known_clusters(self):
|
||||
known = [{"cluster": "app_ready_blind_wait", "clusterTitle": "盲等就绪", "classification": "decision_error", "replayType": "T2_wait", "count": 5}]
|
||||
p = mine._audit_prompt({"stage": "bug.run", "label": "wall:x", "n_fail": 2, "first_call": 1, "last_call": 3}, "时间线", known)
|
||||
self.assertIn("app_ready_blind_wait", p)
|
||||
self.assertIn("必须复用", p)
|
||||
|
||||
|
||||
class LabModeTests(unittest.TestCase):
|
||||
def test_lab_run_sessions_are_not_mined(self):
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)), \
|
||||
mock.patch.object(config, "EPISODES", Path(d) / "e.jsonl"), mock.patch.object(config, "POINTS", Path(d) / "p.jsonl"), \
|
||||
mock.patch.object(mine, "transcript_files", return_value=[(Path(d) / "x.jsonl", {"workspaceKey": "rsi-lab-abc", "node": "n", "file": "x.jsonl"})]), \
|
||||
mock.patch.object(mine, "load_rows", side_effect=AssertionError("复跑会话不该被读")):
|
||||
r = mine.mine_local()
|
||||
self.assertEqual(r["episodes"], 0)
|
||||
|
||||
def test_only_submits_bug_run_with_lab_node(self):
|
||||
now = 2_000_000_000_000
|
||||
clusters = [
|
||||
{"stage": "feature.run.first", "platform": "android", "cluster": "f", "replayType": "T2_wait"},
|
||||
{"stage": "bug.run", "platform": "ios", "cluster": "no_lab_node", "replayType": "T1_repeat"},
|
||||
{"stage": "bug.run", "platform": "android", "cluster": "ok", "replayType": "T2_wait"},
|
||||
]
|
||||
state = {"labNodes": {"android": "evaluator-android-01.shiro0.icu"}}
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)):
|
||||
self.assertEqual(_eligible_cluster(clusters, state, now, point_count=lambda c: 30)["cluster"], "ok")
|
||||
# 没配实验机就一个都不提
|
||||
self.assertIsNone(_eligible_cluster(clusters, {}, now, point_count=lambda c: 30))
|
||||
|
||||
def test_lab_verdicts_get_conclusions(self):
|
||||
self.assertIn("lab_passed", runner.TERMINAL)
|
||||
self.assertIn("lab_failed", runner.TERMINAL)
|
||||
self.assertIn("declined", runner.TERMINAL)
|
||||
|
||||
|
||||
class PlatformTests(unittest.TestCase):
|
||||
def test_live_experiment_only_blocks_its_own_platform(self):
|
||||
now = 2_000_000_000_000
|
||||
clusters = [
|
||||
{"stage": "bug.run", "platform": "android", "cluster": "a", "replayType": "T2_wait"},
|
||||
{"stage": "bug.run", "platform": "ios", "cluster": "b", "replayType": "T2_wait"},
|
||||
]
|
||||
state = {"labNodes": LAB_NODES, "live": [{"stage": "bug.run", "platform": "android"}]}
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)):
|
||||
self.assertEqual(_eligible_cluster(clusters, state, now, point_count=lambda c: 30)["cluster"], "b")
|
||||
# 不带平台的旧实验对所有平台生效
|
||||
legacy = {"labNodes": LAB_NODES, "live": [{"stage": "bug.run", "platform": None}]}
|
||||
self.assertIsNone(_eligible_cluster(clusters, legacy, now, point_count=lambda c: 30))
|
||||
|
||||
def test_replay_points_stay_on_cluster_platform(self):
|
||||
pts = [
|
||||
{"id": "a", "stage": "bug.run", "platform": "android", "ptype": "T2_wait"},
|
||||
{"id": "i", "stage": "bug.run", "platform": "ios", "ptype": "T2_wait"},
|
||||
]
|
||||
with mock.patch.object(mine, "_read_jsonl", return_value=pts):
|
||||
got = mine.replay_points({"stage": "bug.run", "platform": "ios", "replayType": "T2_wait"})
|
||||
self.assertEqual([p["id"] for p in got], ["i"])
|
||||
|
||||
def test_node_without_transcripts_is_not_an_error(self):
|
||||
import urllib.error
|
||||
|
||||
def fake_get(path, timeout=60):
|
||||
if path == "/nodes":
|
||||
return {"nodes": [{"id": "on", "platform": "android"}, {"id": "off", "platform": "ios"}]}
|
||||
if path.startswith("/nodes/off/"):
|
||||
raise urllib.error.HTTPError(path, 404, "not found", {}, None)
|
||||
return {"files": []}
|
||||
|
||||
with tempfile.TemporaryDirectory() as d, \
|
||||
mock.patch.object(config, "DATA", Path(d)), mock.patch.object(config, "TX_DIR", Path(d) / "tx"), \
|
||||
mock.patch.object(config, "RUNS", Path(d) / "runs"), mock.patch.object(config, "STATE", Path(d) / "state.json"), \
|
||||
mock.patch.object(config, "NODES", []), mock.patch("rsi_loop.api.get", side_effect=fake_get):
|
||||
stats = mine.sync_transcripts(2_000_000_000_000)
|
||||
self.assertEqual(stats["nodes"], 1)
|
||||
self.assertEqual(stats["notEnabled"], ["off"])
|
||||
self.assertEqual(stats["errors"], [])
|
||||
|
||||
|
||||
class SyncTests(unittest.TestCase):
|
||||
def test_one_broken_download_does_not_stop_the_rest(self):
|
||||
files = [{"file": f"{i:08d}-0000-0000-0000-000000000000.jsonl", "size": 10, "mtime": 2_000_000_000_000} for i in range(3)]
|
||||
calls = {"n": 0}
|
||||
|
||||
def fake_download(path, dest):
|
||||
calls["n"] += 1
|
||||
if "00000001-" in path:
|
||||
raise IOError("IncompleteRead(542248 bytes read)")
|
||||
dest.write_text("{}")
|
||||
return 10
|
||||
|
||||
with tempfile.TemporaryDirectory() as d, \
|
||||
mock.patch.object(config, "DATA", Path(d)), mock.patch.object(config, "TX_DIR", Path(d) / "tx"), \
|
||||
mock.patch.object(config, "RUNS", Path(d) / "runs"), mock.patch.object(config, "STATE", Path(d) / "state.json"), \
|
||||
mock.patch.object(config, "NODES", ["n1"]), \
|
||||
mock.patch("rsi_loop.api.get", return_value={"files": files}), \
|
||||
mock.patch("rsi_loop.api.download", side_effect=fake_download), \
|
||||
mock.patch("rsi_loop.mine.time.sleep"):
|
||||
stats = mine.sync_transcripts(2_000_000_000_000)
|
||||
self.assertEqual(stats["downloaded"], 2)
|
||||
self.assertEqual(len(stats["errors"]), 1)
|
||||
self.assertIn("IncompleteRead", stats["errors"][0])
|
||||
|
||||
|
||||
class DryRunTests(unittest.TestCase):
|
||||
def test_dry_run_post_never_touches_network(self):
|
||||
from rsi_loop import api
|
||||
with tempfile.TemporaryDirectory() as d, mock.patch.object(config, "DATA", Path(d)), \
|
||||
mock.patch.object(config, "DRY_RUN", True), \
|
||||
mock.patch("urllib.request.urlopen", side_effect=AssertionError("dry-run 不该联网")):
|
||||
got = api.post("/experiments", {"title": "t", "stage": "bug.run"})
|
||||
self.assertEqual(got["experiment"]["status"], "dry_run")
|
||||
self.assertTrue(api.event("运行摘要"))
|
||||
lines = (Path(d) / "dry_posts.jsonl").read_text().splitlines()
|
||||
self.assertEqual([json.loads(l)["path"] for l in lines], ["/experiments", "/events"])
|
||||
runner._record_skip({"stage": "bug.run", "cluster": "x"}, "anchor", 1)
|
||||
self.assertFalse((Path(d) / "skips.json").exists())
|
||||
|
||||
|
||||
class FirstTurnTests(unittest.TestCase):
|
||||
def test_text_then_tool_use_is_one_turn(self):
|
||||
lines = [
|
||||
json.dumps({"type": "system", "subtype": "init"}),
|
||||
json.dumps({"type": "assistant", "message": {"id": "m1", "content": [{"type": "text", "text": "先冷启动看登录态。"}]}}),
|
||||
json.dumps({"type": "assistant", "message": {"id": "m1", "content": [
|
||||
{"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "adb shell monkey -p x 1"}}]}}),
|
||||
json.dumps({"type": "result", "is_error": False}),
|
||||
]
|
||||
first, err = replay.first_turn(lines)
|
||||
self.assertIsNone(err)
|
||||
self.assertEqual(scoring.action_of({"first": first})["input"]["command"], "adb shell monkey -p x 1")
|
||||
|
||||
def test_error_result_is_reported(self):
|
||||
first, err = replay.first_turn([json.dumps({"type": "result", "is_error": True, "result": "Failed to authenticate"})])
|
||||
self.assertIsNone(first)
|
||||
self.assertIn("authenticate", err)
|
||||
|
||||
|
||||
class SamplingTests(unittest.TestCase):
|
||||
def _points(self):
|
||||
mk = lambda i, stage, ptype, ep: {"id": f"p{i}", "stage": stage, "ptype": ptype, "episodeId": ep, # noqa: E731
|
||||
"node": "n", "file": f"f{i}", "point_row": 1}
|
||||
target = [mk(i, "bug.run", "T1_repeat", f"t{i}") for i in range(20)]
|
||||
others = [
|
||||
mk(100, "bug.run", "T2_wait", None), # 同 stage 等待点:可进保留集
|
||||
mk(101, "bug.run", "T1_repeat", "real"), # 同 stage 审计过的真失败:可进
|
||||
mk(102, "bug.run", "T1_repeat", "unaudited"), # 没审计过(可能是误报):不进
|
||||
mk(103, "feature.run.first", "T2_wait", None), # 其它 stage:不进
|
||||
]
|
||||
return target, others
|
||||
|
||||
def test_holdout_is_same_stage_and_audited_and_anchor_checked(self):
|
||||
target, others = self._points()
|
||||
cluster = {"stage": "bug.run", "cluster": "x", "replayType": "T1_repeat"}
|
||||
with mock.patch.object(mine, "replay_points", side_effect=lambda c=None: target if c else target + others), \
|
||||
mock.patch.object(mine, "real_episode_ids", return_value={"real"}), \
|
||||
mock.patch.object(mine, "load_rows_for_point", side_effect=lambda p: p["id"]), \
|
||||
mock.patch.object(replay, "candidate_applies", side_effect=lambda rows, ins: rows != "p3"):
|
||||
t, h, seeds, skipped = runner._sample_points(cluster, {"afterLine": "a", "content": "b"})
|
||||
self.assertEqual({p["id"] for p in h}, {"p100", "p101"})
|
||||
self.assertNotIn("p3", {p["id"] for p in t})
|
||||
self.assertEqual(skipped["targetAnchorMissing"], 1)
|
||||
self.assertGreaterEqual(seeds, 2)
|
||||
|
||||
def test_high_error_rate_aborts_without_offline_result(self):
|
||||
target, _ = self._points()
|
||||
cluster = {"stage": "bug.run", "cluster": "x", "replayType": "T1_repeat"}
|
||||
with tempfile.TemporaryDirectory() as d, \
|
||||
mock.patch.object(config, "RUNS", Path(d)), \
|
||||
mock.patch.object(mine, "replay_points", side_effect=lambda c=None: target), \
|
||||
mock.patch.object(mine, "real_episode_ids", return_value=set()), \
|
||||
mock.patch.object(mine, "load_rows_for_point", return_value=[]), \
|
||||
mock.patch.object(replay, "candidate_applies", return_value=True), \
|
||||
mock.patch.object(replay, "prepare"), \
|
||||
mock.patch.object(replay, "run_point", return_value={"first": None, "secs": 0, "error": "Failed to authenticate"}):
|
||||
with self.assertRaises(runner.ReplayAborted):
|
||||
runner.run_replays(cluster, {"afterLine": None, "content": "x"})
|
||||
|
||||
|
||||
class RuntimeProposalTests(unittest.TestCase):
|
||||
KNOBS = {
|
||||
"effort": {"label": "思考强度", "values": ["low", "medium", "high", "xhigh", "max"], "doc": "d"},
|
||||
"ui_settle": {"label": "等界面稳定", "values": ["on"], "doc": "d", "platforms": ["ios"]},
|
||||
}
|
||||
|
||||
def _exp(self, platform, profile):
|
||||
return {"kind": "runtime", "platform": platform, "candidateProfile": json.dumps(profile)}
|
||||
|
||||
def test_skips_tried_baseline_and_off_platform(self):
|
||||
exps = [self._exp("android", {"effort": "medium"}), self._exp("ios", {"ui_settle": "on"})]
|
||||
with mock.patch.object(config, "LAB_BASELINE", {"android": {"effort": "xhigh"}}):
|
||||
# medium 试过、xhigh 是实验机现行 → high;ui_settle 不适用 Android
|
||||
self.assertEqual(runtime.next_candidate("android", self.KNOBS, exps), ("effort", "high"))
|
||||
# iOS:effort 一个都没试过,从 medium 开始
|
||||
self.assertEqual(runtime.next_candidate("ios", self.KNOBS, exps), ("effort", "medium"))
|
||||
|
||||
def test_exhausted_catalog_proposes_nothing(self):
|
||||
exps = [self._exp("ios", {"effort": v}) for v in ["low", "medium", "high", "xhigh", "max"]]
|
||||
exps.append(self._exp("ios", {"ui_settle": "on"}))
|
||||
self.assertIsNone(runtime.next_candidate("ios", self.KNOBS, exps))
|
||||
|
||||
def test_free_lab_nodes_each_get_one(self):
|
||||
state = {"runtimeKnobs": self.KNOBS, "labNodes": LAB_NODES}
|
||||
posted = []
|
||||
with mock.patch.object(runner.api, "post", side_effect=lambda path, body, timeout=120: posted.append(body) or {"experiment": {"id": "x", "title": body["title"], "status": "lab"}}), \
|
||||
mock.patch.object(config, "LAB_BASELINE", {}):
|
||||
got = runner._submit_runtime_experiments(state, [], busy={"android"})
|
||||
self.assertEqual([b["platform"] for b in posted], ["ios"])
|
||||
self.assertEqual(posted[0]["candidateProfile"], {"effort": "medium"})
|
||||
self.assertEqual(got[0]["status"], "lab")
|
||||
|
||||
|
||||
class IosWaitPointTests(unittest.TestCase):
|
||||
def test_catuse_probe_counts_as_ui_wait(self):
|
||||
self.assertTrue(episodes.UI_LIKE.search("sleep 20; catuse apps 2>&1 | head -20"))
|
||||
self.assertTrue(episodes._is_wait("sleep 20; catuse apps 2>&1 | head -20"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,82 @@
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from rsi_loop import config, review_mining
|
||||
|
||||
# 2026-11-02 是周一(北京时间),2026-11-03 是周二
|
||||
MON = 1793548800000 + 3600_000 * 4 # 2026-11-02 04:00 北京
|
||||
TUE = MON + 86_400_000
|
||||
|
||||
|
||||
class LabelTest(unittest.TestCase):
|
||||
def test_batches_posted_and_failures_isolated(self):
|
||||
pending = {"total": 3, "batches": [{"keys": ["a"], "prompt": "p1"}, {"keys": ["b"], "prompt": "p2"}]}
|
||||
answers = [RuntimeError("boom"), {"labels": [{"key": "b", "cat": "ops"}]}]
|
||||
posted = []
|
||||
|
||||
def ask_json(prompt, timeout=0):
|
||||
a = answers.pop(0)
|
||||
if isinstance(a, Exception):
|
||||
raise a
|
||||
return a
|
||||
|
||||
with mock.patch.object(review_mining.api, "get", return_value=pending), \
|
||||
mock.patch.object(review_mining.llm, "ask_json", side_effect=ask_json), \
|
||||
mock.patch.object(review_mining.api, "post", side_effect=lambda path, body, **k: posted.append((path, body)) or {"saved": 1, "dropped": 0}):
|
||||
out = review_mining.label_pending()
|
||||
self.assertEqual(out["failedBatches"], 1)
|
||||
self.assertEqual(out["saved"], 1)
|
||||
self.assertEqual(posted[0][0], "/review-mining/labels")
|
||||
self.assertEqual(posted[0][1]["labeler"], review_mining.LABELER)
|
||||
|
||||
def test_days_query(self):
|
||||
with mock.patch.object(review_mining.api, "get", return_value={"batches": []}) as g:
|
||||
review_mining.label_pending(days=180)
|
||||
g.assert_called_with("/review-mining/pending?days=180", timeout=300)
|
||||
|
||||
|
||||
class ClusterTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.marker = Path(self.tmp.name) / "last"
|
||||
self.patch = mock.patch.object(review_mining, "LAST_CLUSTER", self.marker)
|
||||
self.patch.start()
|
||||
|
||||
def tearDown(self):
|
||||
self.patch.stop()
|
||||
self.tmp.cleanup()
|
||||
|
||||
def test_only_on_monday_once(self):
|
||||
self.assertFalse(review_mining._cluster_due(TUE))
|
||||
self.assertTrue(review_mining._cluster_due(MON))
|
||||
inp = {"rows": 2, "prompt": "p", "window": {"from": 1, "to": 2}}
|
||||
with mock.patch.object(review_mining.api, "get", return_value=inp), \
|
||||
mock.patch.object(review_mining.llm, "ask_json", return_value={"candidates": [{"family": "x"}]}), \
|
||||
mock.patch.object(review_mining.api, "post", return_value={"saved": 1, "rejected": []}) as post, \
|
||||
mock.patch.object(config, "DRY_RUN", False):
|
||||
out = review_mining.cluster(MON)
|
||||
self.assertEqual(out["saved"], 1)
|
||||
self.assertEqual(post.call_args[0][1]["window"], {"from": 1, "to": 2})
|
||||
self.assertFalse(review_mining._cluster_due(MON)) # 同一天不再跑
|
||||
|
||||
def test_force_and_empty_rows(self):
|
||||
with mock.patch.object(review_mining.api, "get", return_value={"rows": 0, "window": {}}), \
|
||||
mock.patch.object(review_mining.llm, "ask_json") as ask, \
|
||||
mock.patch.object(config, "DRY_RUN", True):
|
||||
out = review_mining.cluster(TUE, force=True)
|
||||
ask.assert_not_called()
|
||||
self.assertEqual(out["rows"], 0)
|
||||
self.assertFalse(self.marker.exists()) # 试运行不落标记
|
||||
|
||||
def test_run_isolates_errors(self):
|
||||
with mock.patch.object(review_mining, "label_pending", side_effect=RuntimeError("down")), \
|
||||
mock.patch.object(review_mining, "cluster", return_value=None):
|
||||
out = review_mining.run(TUE)
|
||||
self.assertIn("error", out["label"])
|
||||
self.assertIn("打标签失败", review_mining.summary_line(out))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Keep-alive supervisor for the satellite process.
|
||||
#
|
||||
# Bare `nohup bun satellite/dist/satellite.js` cannot come back after the
|
||||
# process exits, so a hot-deploy that swaps the bundle and exits would leave the
|
||||
# node dead. This loop relaunches the satellite whenever it exits, which is
|
||||
# exactly how a deploy restarts onto new code: the /sat/admin/deploy handler
|
||||
# swaps dist/satellite.js then calls process.exit(0), and this loop re-launches
|
||||
# it on the fresh bundle.
|
||||
#
|
||||
# Rollout (one-time, per node):
|
||||
# 1. Sync the repo (or at least scripts/ + satellite/dist/) to the node.
|
||||
# 2. Stop the old bare-nohup satellite: pkill -f 'satellite/dist/satellite.js' || true
|
||||
# 3. Start under this loop: nohup ./scripts/run-satellite.sh > ~/satellite.log 2>&1 &
|
||||
#
|
||||
# Set SATELLITE_ENTRY to run from source instead of the bundle (dev nodes):
|
||||
# SATELLITE_ENTRY=satellite/src/index.ts ./scripts/run-satellite.sh
|
||||
#
|
||||
set -uo pipefail
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
export PATH="/opt/homebrew/bin:$HOME/.bun/bin:$PATH"
|
||||
set -a; [ -f .env ] && source .env; set +a
|
||||
|
||||
# Hold a display power assertion for as long as this supervisor lives.
|
||||
#
|
||||
# The satellite drives a real macOS GUI app and captures evidence with
|
||||
# `catuse getscreenshot`, so a screen saver turns every screenshot into a picture
|
||||
# of the screen saver. Setting the screensaver idleTime to 0 does NOT work on
|
||||
# these nodes: MDM ships /Library/Managed Preferences/com.apple.screensaver.plist
|
||||
# (idleTime=300, askForPassword=true) and a managed preference outranks the
|
||||
# user's ByHost value, so loginwindow keeps reading 300 no matter what
|
||||
# `defaults -currentHost write` puts on disk. A power assertion sidesteps the
|
||||
# preference layer entirely — it is the same mechanism that keeps the screen
|
||||
# saver away while a video player is in the foreground.
|
||||
#
|
||||
# `-w $$` releases the assertion when this supervisor exits, so a stopped node
|
||||
# never leaves one behind. Re-armed per loop iteration in case it ever dies.
|
||||
CAFFEINATE_PID=""
|
||||
ensure_awake() {
|
||||
# `kill -0 0` targets the whole process group and always succeeds, so an empty
|
||||
# CAFFEINATE_PID must be tested separately or the assertion is never taken.
|
||||
if [ -z "${CAFFEINATE_PID}" ] || ! kill -0 "${CAFFEINATE_PID}" 2>/dev/null; then
|
||||
caffeinate -dims -w $$ &
|
||||
CAFFEINATE_PID=$!
|
||||
echo "[run-satellite] display assertion held by caffeinate ${CAFFEINATE_PID}"
|
||||
fi
|
||||
}
|
||||
ensure_awake
|
||||
|
||||
ENTRY="${SATELLITE_ENTRY:-satellite/dist/satellite.js}"
|
||||
BACKOFF="${SATELLITE_RESTART_SECONDS:-2}"
|
||||
|
||||
echo "[run-satellite] supervising ${ENTRY} (restart backoff ${BACKOFF}s)"
|
||||
while true; do
|
||||
ensure_awake
|
||||
bun "${ENTRY}"
|
||||
code=$?
|
||||
echo "[run-satellite] satellite exited (code ${code}); restarting in ${BACKOFF}s"
|
||||
sleep "${BACKOFF}"
|
||||
done
|
||||
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Send status-change preview cards through the REAL Streamline bot — the same
|
||||
* `@larksuiteoapi/node-sdk` client and LARK_APP_ID/LARK_APP_SECRET that
|
||||
* `notify.ts` uses in production. What lands in Lark is then byte-identical to
|
||||
* what a real status change will send: same sender identity, same app, same
|
||||
* rendering path. (Previewing through some other Lark app only proves the JSON
|
||||
* parses — it doesn't prove *this* app can send it.)
|
||||
*
|
||||
* Deliberately DM-only: it refuses `oc_…` chat ids so a design preview can never
|
||||
* be blasted into the shared report group.
|
||||
*
|
||||
* Usage, from repo root (bun auto-loads .env):
|
||||
* bun scripts/send-notify-preview.ts someone@bytedance.com
|
||||
* bun scripts/send-notify-preview.ts someone@bytedance.com bug:manual_review feature:dispatched
|
||||
* bun scripts/send-notify-preview.ts someone@bytedance.com real:bug:<uuid>
|
||||
*
|
||||
* With no card names it sends one representative card per category. Card names
|
||||
* are the same `<category>:<kind>` ids that `preview-notify-cards.ts` prints.
|
||||
*
|
||||
* `real:<category>:<id>` pulls the ACTUAL row from the internal read-only API
|
||||
* and renders the card the notifier would really send for its current status.
|
||||
* Use it whenever the question is "how long/ugly does this get with production
|
||||
* text?" — the built-in samples are hand-written fixtures, so their plans and
|
||||
* failure reasons are far shorter and tidier than the real thing.
|
||||
*/
|
||||
import { larkEnabled, sendCard } from "../control-server/src/lark";
|
||||
import type { Task, Feature, Security } from "../control-server/src/types";
|
||||
import { extractPlainSummary } from "../control-server/src/lark_card_media";
|
||||
|
||||
/**
|
||||
* Previews must render the PRODUCTION link, whatever the local `.env` says.
|
||||
*
|
||||
* A developer's `.env` points APP_BASE_URL at their own host, so a preview built
|
||||
* with it carries a domain no recipient can open — and the reviewer of the
|
||||
* preview has no way to tell that apart from a genuine bug in the card. Set
|
||||
* before any card module loads. Override with PREVIEW_BASE_URL when you really
|
||||
* do want to preview against a local build.
|
||||
*/
|
||||
const PREVIEW_BASE_URL = process.env.PREVIEW_BASE_URL || "https://streamline.bytedance.net";
|
||||
process.env.APP_BASE_URL = PREVIEW_BASE_URL;
|
||||
|
||||
const {
|
||||
buildCard, buildFeatureCard, buildSecurityCard, meta, featureMeta, securityMeta,
|
||||
} = await import("../control-server/src/notify_cards");
|
||||
|
||||
const READONLY_API = (process.env.STREAMLINE_READONLY_API
|
||||
|| "https://streamline-internal-api.bytedance.net").replace(/\/+$/, "");
|
||||
|
||||
/**
|
||||
* The 大白话 a real manual_review card would carry, read through the read-only
|
||||
* API. Worth the extra request: the fixtures' summary is hand-written, and the
|
||||
* summary is now the only content on a review card.
|
||||
*/
|
||||
async function realPlain(path: string, id: string): Promise<string | null> {
|
||||
try {
|
||||
const r = await fetch(`${READONLY_API}/api/${path}/${encodeURIComponent(id)}/readable-report`);
|
||||
if (!r.ok) return null;
|
||||
const rr = await r.json() as { html?: string | null };
|
||||
return extractPlainSummary(rr?.html ?? null);
|
||||
} catch { return null; }
|
||||
}
|
||||
|
||||
/** Fetch one live row and render its card for whatever status it's currently in. */
|
||||
async function realCard(category: string, id: string): Promise<unknown> {
|
||||
const path = category === "feature" ? "features" : category === "security" ? "security" : "tasks";
|
||||
const res = await fetch(`${READONLY_API}/api/${path}/${encodeURIComponent(id)}`);
|
||||
if (!res.ok) throw new Error(`GET /api/${path}/${id} → ${res.status}`);
|
||||
const row = await res.json() as Record<string, unknown>;
|
||||
const status = String(row.status);
|
||||
const extras: { plain?: string | null } = {};
|
||||
if (status === "manual_review") {
|
||||
extras.plain = await realPlain(path, id);
|
||||
console.log(` ${id}: 大白话=${extras.plain ? "有" : "无"}`);
|
||||
}
|
||||
if (category === "feature") return buildFeatureCard(row as unknown as Feature, featureMeta(status as never), extras);
|
||||
if (category === "security") return buildSecurityCard(row as unknown as Security, securityMeta(status as never), extras);
|
||||
return buildCard(row as unknown as Task, meta(status as never), extras);
|
||||
}
|
||||
|
||||
const [target, ...names] = process.argv.slice(2);
|
||||
|
||||
if (!target) {
|
||||
console.error("usage: bun scripts/send-notify-preview.ts <email|ou_open_id> [card ...]");
|
||||
process.exit(1);
|
||||
}
|
||||
if (target.startsWith("oc_")) {
|
||||
console.error("refusing to send previews to a chat id — DM a person instead (email or ou_…).");
|
||||
process.exit(1);
|
||||
}
|
||||
if (!larkEnabled()) {
|
||||
console.error("LARK_APP_ID / LARK_APP_SECRET are unset — nothing to send with (see .env.example).");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
const picked = names.length
|
||||
? names
|
||||
: ["bug:pending_confirmation", "bug:manual_review", "bug:fix_failed", "feature:manual_review", "security:dispatched"];
|
||||
|
||||
// Dynamic import: the card modules read APP_BASE_URL at build time, so the
|
||||
// override above has to be in place before they load.
|
||||
const { CARDS } = await import("./_notify-preview-cards");
|
||||
|
||||
const unknown = picked.filter((n) => !CARDS[n] && !n.startsWith("real:"));
|
||||
if (unknown.length) {
|
||||
console.error(`unknown card(s): ${unknown.join(", ")}`);
|
||||
console.error(`available: ${Object.keys(CARDS).join(", ")}`);
|
||||
console.error(`or: real:<bug|feature|security>:<id>`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
|
||||
console.log(`links point at ${PREVIEW_BASE_URL}`);
|
||||
const receiveIdType = target.includes("@") ? "email" : "open_id";
|
||||
for (const name of picked) {
|
||||
try {
|
||||
const card = name.startsWith("real:")
|
||||
? await realCard(name.split(":")[1]!, name.split(":").slice(2).join(":"))
|
||||
: CARDS[name]!();
|
||||
const ok = await sendCard(receiveIdType, target, card);
|
||||
console.log(`${ok ? "sent" : "FAILED"} ${name} → ${target}`);
|
||||
} catch (err) {
|
||||
console.log(`FAILED ${name} — ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import * as Lark from "@larksuiteoapi/node-sdk";
|
||||
|
||||
const APP_ID = process.env.LARK_APP_ID || "";
|
||||
const APP_SECRET = process.env.LARK_APP_SECRET || "";
|
||||
const DOMAIN = process.env.LARK_DOMAIN || Lark.Domain.Feishu;
|
||||
const BASE = process.env.APP_BASE_URL || "";
|
||||
const TARGET = "jiaopengwen@bytedance.com";
|
||||
|
||||
if (!APP_ID || !APP_SECRET) { console.error("LARK creds missing"); process.exit(1); }
|
||||
const client = new Lark.Client({ appId: APP_ID, appSecret: APP_SECRET, domain: DOMAIN });
|
||||
|
||||
const div = (content: string) => ({ tag: "div", text: { tag: "lark_md", content } });
|
||||
const hr = { tag: "hr" };
|
||||
|
||||
const card = {
|
||||
config: { wide_screen_mode: true },
|
||||
header: {
|
||||
template: "blue",
|
||||
title: { tag: "plain_text", content: "🤖 Streamline 自动修复执行报告" },
|
||||
},
|
||||
elements: [
|
||||
div("截至 2026-06-29 | 全量任务 **186**(Bug 163 / Feature 23)"),
|
||||
hr,
|
||||
div(
|
||||
"**✅ 完成 82(44.1%)**,已 merge 落地 **61**\n" +
|
||||
"Bug 69(42.3%)| Feature 13(56.5%)"
|
||||
),
|
||||
div(
|
||||
"**⚡ 一次通过率 79.3%**,两轮内 89.0%\n" +
|
||||
"Android **84.8%** | iOS **56.3%**"
|
||||
),
|
||||
div(
|
||||
"**🔍 无法复现 31**,自动判定 **87.1% 已确认**\n" +
|
||||
"可高准确率自动标识,替代人工初筛"
|
||||
),
|
||||
div(
|
||||
"**⏱️ 单任务中位耗时 1.1h**(端到端)\n" +
|
||||
"Android 1.07h | iOS 1.39h"
|
||||
),
|
||||
{
|
||||
tag: "action",
|
||||
actions: [
|
||||
BASE ? { tag: "button", text: { tag: "plain_text", content: "打开看板" }, url: BASE, type: "primary" } : null,
|
||||
].filter(Boolean),
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
async function send() {
|
||||
const r = await client.im.v1.message.create({
|
||||
params: { receive_id_type: "email" },
|
||||
data: { receive_id: TARGET, msg_type: "interactive", content: JSON.stringify(card) },
|
||||
});
|
||||
console.log("send result code:", r?.code, "msg:", r?.msg, "message_id:", r?.data?.message_id);
|
||||
}
|
||||
send().catch((e) => { console.error("send failed:", e?.message || e); process.exit(1); });
|
||||
@@ -0,0 +1,140 @@
|
||||
// Standalone faithful simulation of the SECURITY eval phase for task
|
||||
// 4f097de5-8c3b-443e-b49e-2e8d7c4384a5. Reproduces exactly what
|
||||
// satellite/src/security_evaluator.ts:evaluateSecurity() spawns — same pty
|
||||
// wrapper, same cwd, same flags, same prompt — but writes to a temp log and
|
||||
// does NOT touch the DB or task status.
|
||||
import { spawn } from "bun";
|
||||
import { mkdirSync, openSync, closeSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { NEEDS_INPUT_OPEN, NEEDS_INPUT_CLOSE, needsInput as NI } from "../shared/src/protocol";
|
||||
import { CLAUDE_LAUNCH, AGENT_MODE } from "../satellite/src/claude_launch";
|
||||
|
||||
const AI_WORKSPACE = process.env.AI_WORKSPACE || path.join(process.env.HOME!, "ai_workspace");
|
||||
const workspaceDir = "/Users/fei/bug_workspace/security-4f097de5-8c3b-443e-b49e-2e8d7c4384a5";
|
||||
const security = {
|
||||
title: "[cluster=default] GIP投稿的BC toggle样式优化 // Optimization of BC toggle style for GIP videos",
|
||||
platform: "android",
|
||||
targetRef: "001fcedc74e7109e1daf6aa97035fc6b9d481fd3",
|
||||
remark: null as string | null,
|
||||
lang: undefined as string | undefined,
|
||||
};
|
||||
|
||||
// ── copied verbatim from security_evaluator.ts ──────────────────────────────
|
||||
function securityTemplateRules(targetRef: string | null): string {
|
||||
const refLine = targetRef
|
||||
? `构建基线:**已指定 \`${targetRef}\`(分支或 commit),必须以此为准检出后构建**。`
|
||||
: `构建基线:未指定,默认 \`develop\` 最新;若 repro spec 内指定了分支/commit/MR,则以其为准。`;
|
||||
return `你只负责产出 SecuritySpec,也就是下游搜证 agent 要执行的 TestSpec.md。不要执行复现、不要构建、不要安装 App、不要采集证据。
|
||||
|
||||
**事实边界(贯穿全文,违反即视为不合格)**:
|
||||
- 搜证方案的每一条复现路径、入口、接口、配置、文件路径和判定标准,都必须来自 review_spec.md / machine_readable_repro_spec.json 或你对代码仓的实际调研。
|
||||
- 严禁编造复现路径、接口、模块名、实验开关、日志 tag、网络端点或 UI 文案。
|
||||
- 对局部步骤不确定时,不要把整单判为 NEEDS_INFO;在对应位置原样写一行 NEEDS_INPUT 哨兵:
|
||||
\`${NEEDS_INPUT_OPEN} <一句话说明缺什么、需要补充什么> ${NEEDS_INPUT_CLOSE}\`
|
||||
- 哨兵会被工具识别为结构性缺失。能从输入或代码确定的内容必须正常写出,不要滥用哨兵。
|
||||
|
||||
**输出纪律**:
|
||||
- 最终 SecuritySpec 里只允许出现:你确定的真实内容,或 NEEDS_INPUT 哨兵。
|
||||
- 严禁把本模板的说明文字、括号提示、示例占位、省略号抄进结果。
|
||||
- 不要新增模板未列出的章节;章节标题、顺序、\`//\` 英文副标题必须保持原样。
|
||||
|
||||
按下面结构产出:
|
||||
|
||||
## 证据目标 // Evidence Goal
|
||||
- 一句话:本次要搜集/证明什么(对应 review_spec 里的安全关注点)。
|
||||
- 判定成功的标志:拿到哪类证据即视为搜证完成。
|
||||
|
||||
## 复现前置 // Repro Preconditions
|
||||
- ${refLine}
|
||||
- 目标平台 / 设备要求;若 review_spec 未给出平台,用哨兵。
|
||||
- 触达目标场景前需要的账号 / 数据 / 开关 / region / 实验配置等前置条件(基于 repro spec 和代码调研)。
|
||||
|
||||
## 复现路径 // Repro Path
|
||||
(基于 machine_readable_repro_spec.json + 代码调研给出到达目标场景的可执行步骤,一般 3 到 7 步;每一步必须能被下游 agent 在真机上执行。不确定的关键步骤用哨兵。)
|
||||
1. ...
|
||||
|
||||
涉及的关键文件 / 模块 / 入口(基于代码调研列出真实路径,不要编):
|
||||
- ...
|
||||
|
||||
## 搜证方法 // Evidence Method
|
||||
(到达场景后如何取证:要抓什么(日志 / 截图 / 抓包 / 调试器或 Frida hook 输出 / 状态快照),落到 evidence/ 的哪些文件;若需要临时改代码或用 Frida 挂钩来观测,写明挂在哪、观测什么。这些手段都是一次性的,不得提交。)
|
||||
- ...
|
||||
|
||||
## 判定标准 // Verdict Criteria
|
||||
(可验证的命题:拿到什么证据判 EVIDENCE_READY;什么情况判无法复现;什么情况判证据不足。每条写成"当...时,应..."。)
|
||||
1. ...
|
||||
|
||||
## 风险与前置缺失 // Risks & Gaps
|
||||
(基于真实事实 / 代码调研写风险、环境依赖和不可控点;结构性缺失用哨兵。)
|
||||
- ...`;
|
||||
}
|
||||
function langDirective(s: typeof security): string {
|
||||
return s.lang === "en"
|
||||
? "Generate the SecuritySpec and the REASON line in English.\n\n"
|
||||
: "请使用中文生成搜证方案(SecuritySpec)以及 REASON 行的内容。\n\n";
|
||||
}
|
||||
function buildSecurityEvalPrompt(s: typeof security, wd: string): string {
|
||||
return `${langDirective(s)}You are the evaluation agent for a SECURITY evidence-gathering task. Your job is to decide whether the request has enough reproduction information to run, then draft a grounded SecuritySpec for a downstream runner. You may inspect the repository under your cwd, but you must not execute the reproduction, build the app, install anything, or collect evidence.
|
||||
|
||||
Read both input files before deciding:
|
||||
- ${wd}/review_spec.md <- human-readable security review request
|
||||
- ${wd}/machine_readable_repro_spec.json <- machine-readable reproduction spec
|
||||
|
||||
Context:
|
||||
- Title: ${s.title}
|
||||
- Platform: ${s.platform}
|
||||
- Target build ref: ${s.targetRef || "(none — default develop, or as specified in the repro spec)"}
|
||||
${s.remark ? `\nReporter remark / supplemental info:\n${s.remark}\n` : ""}
|
||||
|
||||
Decision policy:
|
||||
- Return NEEDS_INFO only when the repro spec is empty or contains no usable reproduction information at all.
|
||||
- Missing or fuzzy local details are not a hard blocker. Draft the SecuritySpec and place NEEDS_INPUT sentinels exactly where the downstream runner needs clarification.
|
||||
- The SecuritySpec must be executable by another agent on a real build. Prefer concrete paths, entry points, settings keys, logs, commands, and evidence names over generic advice.
|
||||
|
||||
Output contract. Use exactly this wrapper format, with no preamble and no trailing prose:
|
||||
|
||||
ASSESSMENT: <READY | NEEDS_INFO>
|
||||
REASON: <one-sentence rationale; if NEEDS_INFO, name the specific structural gap>
|
||||
|
||||
If and only if ASSESSMENT is READY, emit the SecuritySpec between the markers below, following every rule.
|
||||
|
||||
<PLAN>
|
||||
${securityTemplateRules(s.targetRef)}
|
||||
</PLAN>
|
||||
|
||||
The filled SecuritySpec goes between <PLAN> and </PLAN>. Do not write anything after </PLAN>.`;
|
||||
}
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
const logFile = path.join("/tmp", `sim-eval-4f097de5-${Date.now()}.log`);
|
||||
const prompt = buildSecurityEvalPrompt(security, workspaceDir);
|
||||
mkdirSync(AI_WORKSPACE, { recursive: true });
|
||||
const fd = openSync(logFile, "w");
|
||||
|
||||
console.log(`[sim] AGENT_MODE=${AGENT_MODE}`);
|
||||
console.log(`[sim] CLAUDE_LAUNCH=${JSON.stringify(CLAUDE_LAUNCH)}`);
|
||||
console.log(`[sim] cwd=${AI_WORKSPACE}`);
|
||||
console.log(`[sim] log=${logFile}`);
|
||||
console.log(`[sim] prompt bytes=${prompt.length}`);
|
||||
const t0 = Date.now();
|
||||
|
||||
const proc = spawn({
|
||||
cmd: [...CLAUDE_LAUNCH, "-p", prompt, "--output-format", "stream-json", "--verbose", "--dangerously-skip-permissions"],
|
||||
cwd: AI_WORKSPACE,
|
||||
stdout: fd,
|
||||
stderr: fd,
|
||||
env: { ...process.env, CLAUDE_PTY_DEBUG: "1" }, // surface wrapper decisions into the log
|
||||
});
|
||||
|
||||
// Satellite's own watchdog is EVAL_TIMEOUT_MS; give this sim a generous cap.
|
||||
const CAP_MS = Number(process.env.SIM_CAP_MS || 900_000);
|
||||
const timer = setTimeout(() => {
|
||||
console.log(`[sim] cap ${CAP_MS}ms reached — killing`);
|
||||
try { proc.kill(); } catch {}
|
||||
}, CAP_MS);
|
||||
|
||||
await proc.exited;
|
||||
clearTimeout(timer);
|
||||
try { closeSync(fd); } catch {}
|
||||
console.log(`[sim] exited after ${((Date.now() - t0) / 1000).toFixed(1)}s; exitCode=${proc.exitCode}`);
|
||||
console.log(`[sim] log written to ${logFile}`);
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* 把 shared/src/code_conventions.ts 的现行规范块写进三份执行模板的源码。
|
||||
*
|
||||
* bun scripts/sync-code-conventions.ts # 改写 shared/src/run_prompt.ts
|
||||
* bun scripts/sync-code-conventions.ts --check # 只检查,不一致时退出码 1
|
||||
*
|
||||
* 为什么不用 `${}` 插值:RSI 批准上线时由 rsi-ship 直接改写这几个模板常量的源码
|
||||
* (shared/src/rsi_ship.ts::replaceLiteralConst),它只认纯字面量。所以规范块的原文抄在模板里,
|
||||
* code_conventions.ts 是唯一的来源,这个脚本负责抄,control-server/test/review_mining.test.ts 守住两边一致。
|
||||
* 改完记得重抓提示词夹具(satellite/test/run_prompt_parity.test.ts 头注释)。
|
||||
*/
|
||||
import { readFileSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { replaceConventionsBlock } from "../shared/src/code_conventions";
|
||||
import { replaceLiteralConst } from "../shared/src/rsi_ship";
|
||||
import * as runPrompt from "../shared/src/run_prompt";
|
||||
|
||||
const SYMBOLS = ["DEFAULT_BUG_RUN_TEMPLATE", "DEFAULT_FEATURE_RUN_FIRST_TEMPLATE", "DEFAULT_FEATURE_RUN_SECOND_TEMPLATE"] as const;
|
||||
const file = join(import.meta.dir, "../shared/src/run_prompt.ts");
|
||||
let src = readFileSync(file, "utf-8");
|
||||
const stale: string[] = [];
|
||||
for (const sym of SYMBOLS) {
|
||||
const cur = (runPrompt as Record<string, string>)[sym];
|
||||
const next = replaceConventionsBlock(cur);
|
||||
if (next === cur) continue;
|
||||
stale.push(sym);
|
||||
const r = replaceLiteralConst(src, sym, next);
|
||||
if ("error" in r) { console.error(r.error); process.exit(2); }
|
||||
src = r.source;
|
||||
}
|
||||
if (process.argv.includes("--check")) {
|
||||
if (stale.length) { console.error(`规范块与 code_conventions.ts 不一致:${stale.join(", ")} —— 跑 bun scripts/sync-code-conventions.ts`); process.exit(1); }
|
||||
console.log("规范块一致");
|
||||
} else if (stale.length) {
|
||||
writeFileSync(file, src);
|
||||
console.log(`已更新:${stale.join(", ")}`);
|
||||
} else {
|
||||
console.log("无需更新");
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// One-off: send a sample status-change notification to verify the Lark link.
|
||||
// Run from repo root: `bun scripts/test-notify.ts`
|
||||
import { larkEnabled, sendCard } from "../control-server/src/lark";
|
||||
import { notifyStatusChange } from "../control-server/src/notify";
|
||||
import type { Task } from "@bbbug/shared/types";
|
||||
|
||||
const now = Date.now();
|
||||
const sample: Task = {
|
||||
id: "test-" + now,
|
||||
meegoId: "TEST-0001",
|
||||
meegoUrl: "https://meego.feishu.cn/story/detail/0001",
|
||||
title: "[测试] 状态变更通知链路验证",
|
||||
description: "", reproSteps: "",
|
||||
bugType: "ui", platform: "android", difficulty: "medium", stepsClarity: "clear",
|
||||
remark: null, reviewFeedback: null, executionPlan: null, regenContext: null,
|
||||
workspacePath: null, pinned: 0, status: "pending_confirmation", priorityKey: now,
|
||||
creatorEmail: null, creatorLarkId: null,
|
||||
createdAt: now, startedAt: null, finishedAt: null, durationMs: null, totalDurationMs: null,
|
||||
branchName: null, mrUrl: null, logPath: null, beforeEvidencePath: null, afterEvidencePath: null,
|
||||
failureReason: null, attempts: 0,
|
||||
};
|
||||
|
||||
console.log("larkEnabled:", larkEnabled());
|
||||
console.log("LARK_NOTIFY_CHAT_ID:", process.env.LARK_NOTIFY_CHAT_ID || "(unset)");
|
||||
|
||||
// Direct send for a clear pass/fail signal (notifyStatusChange swallows errors).
|
||||
const ok = await sendCard("chat_id", process.env.LARK_NOTIFY_CHAT_ID || "", {
|
||||
config: { wide_screen_mode: true },
|
||||
header: { template: "turquoise", title: { tag: "plain_text", content: "✅ 通知链路自检" } },
|
||||
elements: [{ tag: "div", text: { tag: "lark_md", content: "这是一条来自 Streamline 的测试消息,用于验证状态变更通知是否能送达本群。" } }],
|
||||
});
|
||||
console.log("direct sendCard ok:", ok);
|
||||
|
||||
// Also exercise the real notification path (pending_confirmation card).
|
||||
await notifyStatusChange(sample, "pending_confirmation");
|
||||
console.log("notifyStatusChange(pending_confirmation) dispatched");
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env bun
|
||||
/**
|
||||
* Drive the server-side Bits MR pipeline end-to-end against a real Meego ticket.
|
||||
* Usage:
|
||||
* bun scripts/test_bits_mr.ts <meegoId> [sourceBranch]
|
||||
*
|
||||
* Looks up the task in ~/.streamline/data.sqlite when sourceBranch is omitted.
|
||||
* Prints every Bits API step + reviewer roster so partial failures are visible.
|
||||
*/
|
||||
|
||||
import { Database } from "bun:sqlite";
|
||||
import { homedir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { fetchMeegoUserContacts } from "../control-server/src/meego";
|
||||
import { submitMrForFix, composeMrTitle, emailPrefix, pickBitsUser, firstEmailWatcher, BITS_FIXED_OPERATOR } from "../control-server/src/bits";
|
||||
import type { Platform } from "../control-server/src/types";
|
||||
|
||||
function arg(i: number): string | undefined {
|
||||
return Bun.argv[i + 2];
|
||||
}
|
||||
|
||||
const meegoId = arg(0) || "7321935078";
|
||||
let sourceBranch = arg(1);
|
||||
let title: string | null = null;
|
||||
let meegoUrl = `https://meego.larkoffice.com/tiktok/issue/detail/${meegoId}`;
|
||||
let platform: Platform = "android";
|
||||
let creatorEmail: string | null = null;
|
||||
let watchers: string[] = [];
|
||||
|
||||
if (!sourceBranch) {
|
||||
const dbPath = path.join(homedir(), ".streamline", "data.sqlite");
|
||||
const db = new Database(dbPath, { readonly: true });
|
||||
const row = db
|
||||
.query("SELECT meegoUrl, title, branchName, platform, creatorEmail, watchers FROM tasks WHERE meegoId = $m")
|
||||
.get({ $m: meegoId }) as { meegoUrl: string; title: string; branchName: string; platform: Platform; creatorEmail: string | null; watchers: string | null } | null;
|
||||
db.close();
|
||||
if (!row?.branchName) {
|
||||
console.error(`No branchName found for meegoId=${meegoId} in ${dbPath}`);
|
||||
process.exit(2);
|
||||
}
|
||||
sourceBranch = row.branchName;
|
||||
title = row.title;
|
||||
meegoUrl = row.meegoUrl;
|
||||
platform = row.platform;
|
||||
creatorEmail = row.creatorEmail;
|
||||
try { watchers = JSON.parse(row.watchers || "[]"); } catch { watchers = []; }
|
||||
}
|
||||
|
||||
const composedTitle = composeMrTitle(title || `Meego ${meegoId}`);
|
||||
|
||||
console.log("=== Inputs ===");
|
||||
console.log(JSON.stringify({ meegoId, meegoUrl, sourceBranch, title, composedTitle, fixedOperator: BITS_FIXED_OPERATOR }, null, 2));
|
||||
|
||||
console.log("\n=== fetchMeegoUserContacts ===");
|
||||
const contacts = await fetchMeegoUserContacts(meegoId, meegoUrl);
|
||||
console.log(JSON.stringify({
|
||||
...contacts,
|
||||
assigneePrefix: emailPrefix(contacts.assignee),
|
||||
reporterPrefix: emailPrefix(contacts.reporter),
|
||||
}, null, 2));
|
||||
|
||||
const bitsUser = pickBitsUser([contacts.assignee, creatorEmail, firstEmailWatcher(watchers)]);
|
||||
console.log(`bits-user (MR owner): ${bitsUser}`);
|
||||
|
||||
async function runOnce(label: string) {
|
||||
console.log(`\n=== ${label} ===`);
|
||||
const result = await submitMrForFix({
|
||||
platform,
|
||||
sourceBranch: sourceBranch!,
|
||||
targetBranch: "develop",
|
||||
title: composedTitle,
|
||||
mrType: "bug",
|
||||
meegoTaskId: meegoId,
|
||||
assigneeEmail: contacts.assignee,
|
||||
reporterEmail: contacts.reporter,
|
||||
bitsUser,
|
||||
});
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
return result;
|
||||
}
|
||||
|
||||
const first = await runOnce("submitMrForFix · attempt #1");
|
||||
const second = await runOnce("submitMrForFix · attempt #2 (duplicate path)");
|
||||
|
||||
console.log("\n=== Summary ===");
|
||||
console.table([
|
||||
{ attempt: 1, ok: first.ok, mrId: first.mrId, failed: first.steps.filter((s) => !s.ok).map((s) => s.step).join(",") || "-" },
|
||||
{ attempt: 2, ok: second.ok, mrId: second.mrId, failed: second.steps.filter((s) => !s.ok).map((s) => s.step).join(",") || "-" },
|
||||
]);
|
||||
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Read-only probe for the 「Business line」parsing in meego.ts: fetches real Meego
|
||||
* tickets and prints the 一级/二级/三级业务线 cascade the parser extracted.
|
||||
* Touches nothing but Meego's read API.
|
||||
*
|
||||
* Nothing routes on this field any more — the Messaging → im-sg cluster override
|
||||
* was dropped on 2026-10-15 (see importer.ts) — so this is purely a parser check.
|
||||
*
|
||||
* bun scripts/test_biz_line_cluster.ts [<meego url or id> ...]
|
||||
*/
|
||||
import { fetchMeegoIssue, parseMeegoLink } from "../control-server/src/meego";
|
||||
|
||||
const links = process.argv.slice(2).length
|
||||
? process.argv.slice(2)
|
||||
: ["https://meego.larkoffice.com/tiktok/issue/detail/7362008168"];
|
||||
|
||||
for (const link of links) {
|
||||
const parsed = parseMeegoLink(link);
|
||||
if (!parsed) { console.log(`${link} → unparseable`); continue; }
|
||||
const issue = await fetchMeegoIssue(parsed.id, parsed.url);
|
||||
console.log(`${parsed.id} ${issue.title}`);
|
||||
console.log(` stack : ${issue.stack}`);
|
||||
console.log(` bizLine : ${issue.bizLine}`);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
* Ad-hoc test for the feature Meego-link expansion (createFeature's fetch step).
|
||||
* Runs the REAL expandMeegoLinks against a description that mixes:
|
||||
* - two real past-bug Meego work-item links (different projects)
|
||||
* - one Lark doc link (must pass through untouched — handled by the agent skill)
|
||||
* - some prose + a bare number (must be ignored)
|
||||
* Prints the appended description + the combined attachment manifest.
|
||||
*
|
||||
* Run: bun scripts/test_feature_meego_parse.ts
|
||||
*/
|
||||
import { expandMeegoLinks } from "../control-server/src/feature_importer";
|
||||
|
||||
const MEEGO_A = "https://meego.larkoffice.com/tiktok/issue/detail/7357180292";
|
||||
const MEEGO_B = "https://meego.larkoffice.com/tiktok_live/issue/detail/7356610293";
|
||||
const LARK = "https://bytedance.us.larkoffice.com/docx/XFLEdHT7BoS0dCxSvG0ukKj9sbe";
|
||||
|
||||
const description = [
|
||||
"在收件箱顶部增加「说句悄悄话」快捷心情入口,让用户一键发布预设状态。",
|
||||
"",
|
||||
`需求详情见 Meego: ${MEEGO_A}`,
|
||||
`关联的直播侧异常单: ${MEEGO_B}`,
|
||||
`PRD 设计文档(飞书): ${LARK}`,
|
||||
"验收编号 12345 仅为业务序号,不是工单。",
|
||||
].join("\n");
|
||||
|
||||
console.log("═══════════ INPUT description ═══════════");
|
||||
console.log(description);
|
||||
|
||||
const res = await expandMeegoLinks(description);
|
||||
|
||||
console.log("\n═══════════ OUTPUT description (original + appended) ═══════════");
|
||||
console.log(res.description);
|
||||
|
||||
console.log("\n═══════════ attachments manifest ═══════════");
|
||||
console.log(JSON.stringify(res.attachments, null, 2));
|
||||
|
||||
console.log("\n═══════════ assertions ═══════════");
|
||||
const outStr = res.description;
|
||||
const check = (name: string, ok: boolean) => console.log(`${ok ? "✅" : "❌"} ${name}`);
|
||||
check("original description preserved verbatim at head", outStr.startsWith(description));
|
||||
check("Meego A expanded (fence present)", outStr.includes(`===${MEEGO_A} 实际内容===`));
|
||||
check("Meego B expanded (fence present)", outStr.includes(`===${MEEGO_B} 实际内容===`));
|
||||
check("Lark link NOT expanded (no fence)", !outStr.includes(`===${LARK} 实际内容===`));
|
||||
check("Lark link still present as plain text", outStr.includes(LARK));
|
||||
check("no stub leaked into output", !/\[stub\]|Cannot derive|Unparseable/.test(outStr));
|
||||
if (res.attachments) {
|
||||
const all = [...res.attachments.inlineImages, ...res.attachments.attachments];
|
||||
check("every attachment entry carries a source link", all.every((e) => !!e.link));
|
||||
console.log(` inlineImages=${res.attachments.inlineImages.length} attachments=${res.attachments.attachments.length}`);
|
||||
}
|
||||
|
||||
process.exit(0);
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* TOS S3 upload load test — drives the real `tos.ts` module (same code path the
|
||||
* control plane uses) and reports latency percentiles + error breakdown.
|
||||
*
|
||||
* bun scripts/tos_loadtest.ts [--conc 16] [--total 400] [--size 64k] [--secs 0]
|
||||
*/
|
||||
import { uploadImageToTos, uploadEvidenceToTos, isTosConfigured, evidenceContentType } from "../control-server/src/tos";
|
||||
|
||||
function arg(name: string, dflt: string): string {
|
||||
const i = process.argv.indexOf(`--${name}`);
|
||||
return i > 0 && process.argv[i + 1] ? process.argv[i + 1]! : dflt;
|
||||
}
|
||||
function bytes(spec: string): number {
|
||||
const m = /^(\d+)([kKmM]?)$/.exec(spec);
|
||||
if (!m) throw new Error(`bad size: ${spec}`);
|
||||
return Number(m[1]) * (m[2]?.toLowerCase() === "k" ? 1024 : m[2]?.toLowerCase() === "m" ? 1024 * 1024 : 1);
|
||||
}
|
||||
|
||||
const CONC = Number(arg("conc", "16"));
|
||||
const TOTAL = Number(arg("total", "400"));
|
||||
const SIZE = bytes(arg("size", "64k"));
|
||||
const SECS = Number(arg("secs", "0")); // >0 → run for a duration instead of a fixed count
|
||||
|
||||
if (!isTosConfigured()) { console.error("TOS not configured (need TOS_ACCESS_KEY + TOS_SECRET_KEY)"); process.exit(1); }
|
||||
|
||||
const payload = new Uint8Array(SIZE);
|
||||
for (let i = 0; i < SIZE; i++) payload[i] = i & 0xff;
|
||||
|
||||
const lat: number[] = [];
|
||||
const errors = new Map<string, number>();
|
||||
let done = 0, ok = 0, fail = 0;
|
||||
const started = Date.now();
|
||||
const deadline = SECS > 0 ? started + SECS * 1000 : Infinity;
|
||||
|
||||
function more(): boolean {
|
||||
return SECS > 0 ? Date.now() < deadline : done < TOTAL;
|
||||
}
|
||||
|
||||
async function worker(id: number): Promise<void> {
|
||||
for (let n = 0; more(); n++) {
|
||||
done++;
|
||||
const t0 = performance.now();
|
||||
try {
|
||||
await uploadImageToTos(`_loadtest/w${id}_${n}.bin`, payload, "application/octet-stream");
|
||||
lat.push(performance.now() - t0);
|
||||
ok++;
|
||||
} catch (err) {
|
||||
fail++;
|
||||
const msg = (err as Error).message.slice(0, 120);
|
||||
errors.set(msg, (errors.get(msg) ?? 0) + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function pct(sorted: number[], p: number): number {
|
||||
if (!sorted.length) return NaN;
|
||||
return sorted[Math.min(sorted.length - 1, Math.floor((p / 100) * sorted.length))]!;
|
||||
}
|
||||
|
||||
await Promise.all(Array.from({ length: CONC }, (_, i) => worker(i)));
|
||||
|
||||
const wall = (Date.now() - started) / 1000;
|
||||
const s = [...lat].sort((a, b) => a - b);
|
||||
const f = (n: number) => (Number.isNaN(n) ? "-" : n.toFixed(1));
|
||||
console.log(`\n=== TOS S3 PUT load test ===`);
|
||||
console.log(`conc=${CONC} size=${(SIZE / 1024).toFixed(0)}KB requests=${ok + fail} wall=${wall.toFixed(1)}s`);
|
||||
console.log(`ok=${ok} fail=${fail} error_rate=${((fail / (ok + fail)) * 100).toFixed(2)}%`);
|
||||
console.log(`throughput=${((ok + fail) / wall).toFixed(1)} req/s ${(((ok * SIZE) / 1024 / 1024) / wall).toFixed(1)} MB/s`);
|
||||
console.log(`latency ms: min=${f(s[0]!)} p50=${f(pct(s, 50))} p90=${f(pct(s, 90))} p99=${f(pct(s, 99))} max=${f(s[s.length - 1]!)}`);
|
||||
if (errors.size) {
|
||||
console.log(`\nerrors:`);
|
||||
for (const [msg, n] of [...errors].sort((a, b) => b[1] - a[1])) console.log(` ${n}x ${msg}`);
|
||||
}
|
||||
process.exit(fail > 0 ? 1 : 0);
|
||||
@@ -0,0 +1,41 @@
|
||||
// Integration smoke: drive the real tos.ts helpers, then read every uploaded
|
||||
// object back through the public voffline URL to prove the key encoding used
|
||||
// for signing matches what actually landed in the bucket.
|
||||
import { uploadImageToTos, uploadEvidenceToTos, isTosConfigured, evidenceContentType } from "../control-server/src/tos";
|
||||
|
||||
console.log("isTosConfigured:", isTosConfigured());
|
||||
|
||||
const cases: Array<[string, () => Promise<string>, string]> = [
|
||||
["image / plain uuid name", () => {
|
||||
const b = new TextEncoder().encode("PNGDATA-plain");
|
||||
return uploadImageToTos("_smoke-plain.png", b, "image/png");
|
||||
}, "PNGDATA-plain"],
|
||||
["evidence / spaces + unicode + parens", () => {
|
||||
const b = new TextEncoder().encode("EVIDENCE-weird");
|
||||
const n = "屏幕截图 (1)!'*.png";
|
||||
return uploadEvidenceToTos("bug", "_smoke-owner", n, b, evidenceContentType(n));
|
||||
}, "EVIDENCE-weird"],
|
||||
["evidence / mp4 content-type", () => {
|
||||
const b = new TextEncoder().encode("MP4BYTES");
|
||||
const n = "demo video.mp4";
|
||||
return uploadEvidenceToTos("feature", "_smoke-owner", n, b, evidenceContentType(n));
|
||||
}, "MP4BYTES"],
|
||||
];
|
||||
|
||||
let bad = 0;
|
||||
for (const [label, run, expect] of cases) {
|
||||
try {
|
||||
const url = await run();
|
||||
const res = await fetch(url);
|
||||
const body = await res.text();
|
||||
const good = res.ok && body === expect;
|
||||
if (!good) bad++;
|
||||
console.log(`${good ? "PASS" : "FAIL"} ${label}`);
|
||||
console.log(` url=${url}`);
|
||||
console.log(` voffline=${res.status} ct=${res.headers.get("content-type")} body=${JSON.stringify(body.slice(0, 40))}`);
|
||||
} catch (err) {
|
||||
bad++;
|
||||
console.log(`FAIL ${label}: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
process.exit(bad ? 1 : 0);
|
||||
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 校验执行期提示词与夹具逐字节相同。
|
||||
*
|
||||
* bun scripts/verify_prompt_fixtures.ts # 退出码 0 = 一致
|
||||
*
|
||||
* 为什么它是一个独立进程,而不是直接写在测试里:被比对的构造函数读的是**模块作用域**
|
||||
* 常量(`claude_launch.ts` 的 `RECORD_EVIDENCE_CMD` 在 import 那一刻就由环境变量定死
|
||||
* 了)。只要同一个 bun 进程里有任何别的测试文件先 import 过这些模块,常量就已经用别的
|
||||
* 环境算完了 —— 于是这套断言会随「今天跑了哪些测试文件」时红时绿,而那种测试比没有
|
||||
* 测试更糟。
|
||||
*
|
||||
* 独占一个进程,环境在任何 import 之前设好,结果就只取决于代码本身。
|
||||
*/
|
||||
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import {
|
||||
PROMPT_FIXTURE_ENV, PROMPT_FIXTURE_EXEC_PATH, promptFixtureCases, FIXTURE_DIR,
|
||||
} from "../satellite/test/prompt_fixture_cases";
|
||||
|
||||
for (const [k, v] of Object.entries(PROMPT_FIXTURE_ENV)) process.env[k] = v;
|
||||
Object.defineProperty(process, "execPath", { value: PROMPT_FIXTURE_EXEC_PATH, configurable: true });
|
||||
|
||||
const dir = path.resolve(import.meta.dir, "..", FIXTURE_DIR);
|
||||
if (!existsSync(dir)) {
|
||||
console.error(`缺少夹具目录 ${FIXTURE_DIR} — 先跑 bun scripts/capture_prompt_fixtures.ts`);
|
||||
process.exit(2);
|
||||
}
|
||||
|
||||
const cases = await promptFixtureCases();
|
||||
const problems: string[] = [];
|
||||
const rendered = new Map<string, string>();
|
||||
|
||||
for (const c of cases) {
|
||||
const file = path.join(dir, `${c.name}.txt`);
|
||||
const got = c.render();
|
||||
const prev = rendered.get(got);
|
||||
// Two cases rendering the same text means that branch is not actually covered;
|
||||
// the fixture count would then overstate what is pinned.
|
||||
if (prev) problems.push(`用例未区分分支:${prev} 与 ${c.name} 渲染结果相同`);
|
||||
else rendered.set(got, c.name);
|
||||
|
||||
if (!existsSync(file)) { problems.push(`${c.name}: 没有对应夹具`); continue; }
|
||||
const want = readFileSync(file, "utf8");
|
||||
if (got === want) continue;
|
||||
let i = 0;
|
||||
while (i < Math.min(got.length, want.length) && got[i] === want[i]) i++;
|
||||
const around = (s: string) => JSON.stringify(s.slice(Math.max(0, i - 60), i + 60));
|
||||
problems.push(`${c.name}: 第 ${i} 字符起不同\n 夹具: ${around(want)}\n 当前: ${around(got)}`);
|
||||
}
|
||||
|
||||
const names = new Set(cases.map((c) => c.name));
|
||||
for (const f of readdirSync(dir).filter((x) => x.endsWith(".txt"))) {
|
||||
const n = f.replace(/\.txt$/, "");
|
||||
if (!names.has(n)) problems.push(`孤儿夹具 ${n}:没有对应用例,请删掉或补回用例`);
|
||||
}
|
||||
|
||||
if (problems.length) {
|
||||
console.error(`[parity] ${problems.length} 处不一致:\n\n${problems.join("\n\n")}`);
|
||||
console.error(`\n[parity] 若这些措辞改动是有意的:bun scripts/capture_prompt_fixtures.ts 重抓夹具。`);
|
||||
process.exit(1);
|
||||
}
|
||||
console.log(`[parity] ${cases.length} 个用例与夹具逐字节一致 ✓`);
|
||||
@@ -0,0 +1,33 @@
|
||||
#!/bin/zsh
|
||||
# Weekly Streamline metrics card → Lark group.
|
||||
#
|
||||
# The machine clock is NOT Beijing, so we cannot rely on cron's local time. This
|
||||
# wrapper is invoked hourly by cron and self-gates on Beijing (Asia/Shanghai)
|
||||
# time: it only fires the report at Friday 11:00 北京时间. Because the gate uses
|
||||
# TZ=Asia/Shanghai explicitly, it stays correct across DST changes and even if
|
||||
# the machine's timezone is later changed.
|
||||
#
|
||||
# Args:
|
||||
# (none) cron mode — gate on Beijing Friday 11:00, window = last 7 days.
|
||||
# --test bypass the gate, window = [last Beijing Friday 11:00, now].
|
||||
# --force bypass the gate, normal 7-day window.
|
||||
|
||||
set -e
|
||||
REPO=/Users/fei/BBBBBBug
|
||||
BUN=/opt/homebrew/bin/bun
|
||||
LOG=$HOME/.streamline/weekly_report.log
|
||||
|
||||
cd "$REPO"
|
||||
|
||||
ARG="${1:-}"
|
||||
if [[ "$ARG" != "--test" && "$ARG" != "--force" ]]; then
|
||||
# Gate: only at Beijing Friday (weekday 5) 11:00.
|
||||
read DOW HOUR <<< "$(TZ=Asia/Shanghai date +'%u %H')"
|
||||
if [[ "$DOW" != "5" || "$HOUR" != "11" ]]; then
|
||||
exit 0
|
||||
fi
|
||||
ARG=""
|
||||
fi
|
||||
|
||||
echo "===== $(TZ=Asia/Shanghai date '+%Y-%m-%d %H:%M:%S %Z') run arg='${ARG}' =====" >> "$LOG"
|
||||
"$BUN" run control-server/src/weekly_report.ts $ARG >> "$LOG" 2>&1
|
||||
Reference in New Issue
Block a user