feat: flow 第 1 部分(骨架、研究开始、第零轮)与第零轮检验

按任务书 12 实现 src/flow:从账本现算下一步的纯函数、主循环、恢复入口、派模型任务和跑正式运行两个共用动作、冻结配置、基线、第零轮三个研究者阶段和入口分配,走到第 1 轮时停下。materials 导出 finalEntries、加 researcherId 和 StudyRecord。

审阅采纳两条:研究编号本机已用过时冻结前报错;docker run 没启动时写完结果就停下,不在本进程里无限起新编号。

eval/ 新增第零轮检验:起研究命令行、回放假模型、检验用 Jev 客户端、六问质量评估、16-real-1 回放、杀进程与两份同时跑。tsc 与 depcruise 也检查 eval/,新增 eval 只经 index.ts 用正式代码的规则。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 10:58:52 -04:00
parent 8491191a2b
commit 75d7cffd27
36 changed files with 2456 additions and 14 deletions
+99
View File
@@ -0,0 +1,99 @@
import { mkdirSync } from "node:fs";
import { resolve } from "node:path";
import type { ResearchConfig } from "../config/index.ts";
import { Ledger } from "../ledger/index.ts";
import { Runner } from "../runner/index.ts";
import { fullConfig, readFrozen } from "./frozen.ts";
import { EVENTS, nextStep, type Next } from "./next-step.ts";
import { ledgerFile } from "./paths.ts";
import { recover } from "./recover.ts";
import { baselineRun, saveBaseline } from "./steps/baseline.ts";
import { assignEntries, dispatchStage } from "./steps/round-zero.ts";
import { freezeConfig, halt, openStudy, USER_STOPPED, type Study, type StudyEnding } from "./study.ts";
// 主循环:读账本 → 问下一步 → 做这一步 → 回到开头(任务书 12 第 2.4 节)。每做完一步都重新读账本。
// 每次启动走同一条路:新研究先冻结配置;之后把研究接上(核对镜像和提示词)、核对未完成的动作,再进入主循环。
export type StudyInput = {
/** 研究目录:账本、代码仓库、每次运行和每次模型调用的目录都在这里(任务书 12 第 2.1 节)。 */
studyDir: string;
/** 新研究交 checkConfig 检查过的配置;接着跑一份已有的研究写 null,以账本里冻结的那份为准。 */
config: ResearchConfig | null;
/** 用户停下研究:正在跑的模型会话立即停,正在跑的容器不管它,下次启动时由恢复入口接着等。 */
stop: AbortSignal;
};
export async function runStudy(input: StudyInput): Promise<StudyEnding> {
const dir = resolve(input.studyDir);
const config = input.config === null ? null : fullConfig(input.config);
mkdirSync(dir, { recursive: true });
const ledger = Ledger.open(ledgerFile(dir));
try {
const frozen = readFrozen(ledger.records()) !== null;
if (!frozen && config === null) throw new Error(`研究目录 ${dir} 里还没有研究,配置不能写 null`);
if (frozen && config !== null) throw new Error(`研究目录 ${dir} 里已经有冻结的配置:接着跑时配置写 null,以账本里冻结的那份为准`);
// Docker 守护进程的地址交给 docker 命令按自己的环境变量和 context 决定。
const runner = new Runner(null);
// 恢复入口:把研究接上,核对未完成的动作,之后才问下一步(任务书 12 第 2.5 节)。新研究冻结配置之后走同一条路。
const begin = async (): Promise<Study | StudyEnding> => {
const opened = await openStudy(dir, ledger, runner, input.stop);
if ("halt" in opened) return halt(ledger, opened.halt, []);
const recovered = await recover(opened);
if (recovered === "stopped") return USER_STOPPED;
if (recovered !== null) return { status: "halted", reason: recovered.halt };
return opened;
};
let study: Study | null = null;
if (frozen) {
const begun = await begin();
if ("status" in begun) return begun;
study = begun;
}
for (;;) {
if (input.stop.aborted) return USER_STOPPED;
const next = nextStep(ledger.records());
if (next.do === "freezeConfig") {
await freezeConfig(dir, ledger, runner, config!);
const begun = await begin();
if ("status" in begun) return begun;
study = begun;
continue;
}
const ended = await doStep(study!, next);
if (ended !== null) return ended;
}
} finally {
ledger.close();
}
}
/** 做一步。交回 null 表示接着走下一步。 */
async function doStep(s: Study, next: Exclude<Next, { do: "freezeConfig" }>): Promise<StudyEnding | null> {
switch (next.do) {
case "saveBaseline":
saveBaseline(s);
return null;
case "baselineRun": {
const ran = await baselineRun(s, next.run);
if (ran === "stopped") return USER_STOPPED;
if (ran === "done") return null;
return halt(s.ledger, `运行 ${next.run} 没有启动,名额已退还;本进程不重跑,下次启动时起新编号重跑。Docker 的报错:${ran.notStarted}`, [next.run]);
}
case "skip":
s.ledger.append("event", { event: EVENTS.skipped, reason: next.reason, objects: [next.step] });
return null;
case "openStage":
s.ledger.append("stageOpen", { stageId: next.stageId, stage: next.stage, round: 0, roster: next.roster });
return null;
case "closeStage":
s.ledger.append("stageClose", { stageId: next.stageId });
return null;
case "dispatch":
return dispatchStage(s, next);
case "assignEntries":
assignEntries(s);
return null;
case "halt":
return halt(s.ledger, next.reason, next.objects);
}
}