style: pi-lens 对 flow/vocab/tests 的引号与换行格式重排(无行为变化;check 与 477 条测试通过)

This commit is contained in:
2026-10-05 00:56:37 -04:00
parent 3f7de96db3
commit fdda4f009b
4 changed files with 1767 additions and 332 deletions
+458 -95
View File
@@ -1,9 +1,30 @@
import type { RoundStepId, StudyStepId } from "../config/index.ts";
import type { Content, LedgerRecord } from "../ledger/index.ts";
import { Board, CELL_NOT_RUN, cellId, DECISION_KINDS, experimenterId, factorKey, FORWARD_NOT_RUN, ORIGINAL_BASELINE, researcherId, splitCell, verifierId } from "../materials/index.ts";
import { compareIds, listCells, runBalance, sharedSettings } from "../rules/index.ts";
import {
Board,
CELL_NOT_RUN,
cellId,
DECISION_KINDS,
experimenterId,
factorKey,
FORWARD_NOT_RUN,
ORIGINAL_BASELINE,
researcherId,
splitCell,
verifierId,
} from "../materials/index.ts";
import {
compareIds,
listCells,
runBalance,
sharedSettings,
} from "../rules/index.ts";
import { ablationOutcome, verificationOutcome } from "../submissions/index.ts";
import { SUBMISSION_KINDS, type Stage, type SubmissionKind } from "../vocab/index.ts";
import {
SUBMISSION_KINDS,
type Stage,
type SubmissionKind,
} from "../vocab/index.ts";
import { readFrozen, type Frozen } from "./frozen.ts";
// "下一步做什么"(docs/结构.md 第二部分"几条边界"):步骤的先后只写在这里。
@@ -19,14 +40,20 @@ const ROUND_ZERO_STAGES = {
discussion1: { stage: "discussion", kind: "discussion" },
discussion2: { stage: "discussion", kind: "discussion" },
claim: { stage: "claim", kind: "claim" },
} as const satisfies Partial<Record<StudyStepId, { stage: Stage; kind: string }>>;
} as const satisfies Partial<
Record<StudyStepId, { stage: Stage; kind: string }>
>;
type RoundZeroStep = keyof typeof ROUND_ZERO_STAGES;
/** 第 r 轮的研究者阶段:步骤名就是阶段名(vocab STAGES)。 */
type RoundStageStep = Extract<RoundStepId, "proposal" | "firstPrediction" | "critique" | "response" | "secondPrediction">;
type RoundStageStep = Extract<
RoundStepId,
"proposal" | "firstPrediction" | "critique" | "response" | "secondPrediction"
>;
/** 阶段编号由 flow 起:r<轮>-<步骤>,一个步骤一个阶段。第 r 轮按配置跳过的步骤也用这个编号记"按配置跳过"。 */
export const stageIdOf = (round: number, step: string): string => `r${round}-${step}`;
export const stageIdOf = (round: number, step: string): string =>
`r${round}-${step}`;
/** 基线运行编号:没启动就起下一个编号重跑(docs/结构.md 第四部分)。 */
export const baselineRunId = (n: number): string => `base-${n}`;
@@ -41,14 +68,24 @@ export const EVENTS = {
} as const;
/** 一个任务:一个人对一个对象交一种提交;提案和第零轮任务没有对象。 */
export type TaskRef = { actor: string; kind: SubmissionKind; object: string | null };
export type TaskRef = {
actor: string;
kind: SubmissionKind;
object: string | null;
};
export type Next =
| { do: "freezeConfig" }
| { do: "saveBaseline" }
| { do: "baselineRun"; run: string }
| { do: "skip"; step: string; reason: string }
| { do: "openStage"; stageId: string; stage: Stage; round: number; roster: string[] }
| {
do: "openStage";
stageId: string;
stage: Stage;
round: number;
roster: string[];
}
| { do: "closeStage"; stageId: string }
| { do: "dispatch"; stageId: string; tasks: TaskRef[] }
| { do: "assignEntries" }
@@ -72,7 +109,11 @@ export function nextStep(records: readonly LedgerRecord[]): Next {
const view = new View(records);
for (const step of frozen.steps.study) {
const next =
step.status === "skip" ? skipNext(step.id, step.reason, view) : step.id === "rounds" ? roundsNext(view, frozen) : studyStepNext(step.id, view, frozen);
step.status === "skip"
? skipNext(step.id, step.reason, view)
: step.id === "rounds"
? roundsNext(view, frozen)
: studyStepNext(step.id, view, frozen);
if (next !== null) return next;
}
throw new Error("步骤清单走完了却没有停下:export 一步应当停下");
@@ -80,11 +121,17 @@ export function nextStep(records: readonly LedgerRecord[]): Next {
/** 按配置跳过的步骤 → 一条"按配置跳过"事件,写明步骤和原因。 */
function skipNext(step: string, reason: string, view: View): Next | null {
return view.events(EVENTS.skipped).some((e) => e.objects[0] === step) ? null : { do: "skip", step, reason };
return view.events(EVENTS.skipped).some((e) => e.objects[0] === step)
? null
: { do: "skip", step, reason };
}
/** 整份研究的一个要执行的步骤还要做什么;做完了交回 null。 */
function studyStepNext(step: Exclude<StudyStepId, "rounds">, view: View, frozen: Frozen): Next | null {
function studyStepNext(
step: Exclude<StudyStepId, "rounds">,
view: View,
frozen: Frozen,
): Next | null {
switch (step) {
case "freezeConfig":
return null;
@@ -95,11 +142,21 @@ function studyStepNext(step: Exclude<StudyStepId, "rounds">, view: View, frozen:
case "discussion2":
case "claim": {
const { stage, kind } = ROUND_ZERO_STAGES[step];
return stageNext(stageIdOf(0, step), stage, 0, (roster) => roster.map((actor) => ({ actor, kind, object: null })), view, frozen, researchers(frozen));
return stageNext(
stageIdOf(0, step),
stage,
0,
(roster) => roster.map((actor) => ({ actor, kind, object: null })),
view,
frozen,
researchers(frozen),
);
}
case "assignEntries":
// 分配 → 第 0 轮的入口分配决定(decision,种类 entries)。
return view.board.decision("entries", 0) === undefined ? { do: "assignEntries" } : null;
return view.board.decision("entries", 0) === undefined
? { do: "assignEntries" }
: null;
case "export":
// 余额用完、一轮接一轮走完后走到这里:导出还没写,停下(任务书 17 第 2.1 节)。
return { do: "halt", reason: "导出还没写", objects: [] };
@@ -116,9 +173,16 @@ function roundsNext(view: View, frozen: Frozen): Next | null {
// 余额为 0 不开【下一轮】;已经开始的轮次照常走完——最后一次正向用掉全部余额时,这一轮的采纳、
// 消融(各格记“没测:剩余次数不够”)、收尾与解封照样做(方法.md 第七部分“什么时候开下一轮”)。
// 一轮在账本里留下的第一处痕迹是阶段记录(或按配置跳过事件、决定记录),此前这一轮还不算开始。
if (!roundStarted(view, round) && runBalance(frozen.config.runs, view.board.runStates()).balance === 0) return null;
if (
!roundStarted(view, round) &&
runBalance(frozen.config.runs, view.board.runStates()).balance === 0
)
return null;
for (const step of frozen.steps.round) {
const next = step.status === "skip" ? skipNext(stageIdOf(round, step.id), step.reason, view) : roundStepNext(step.id, round, view, frozen);
const next =
step.status === "skip"
? skipNext(stageIdOf(round, step.id), step.reason, view)
: roundStepNext(step.id, round, view, frozen);
if (next !== null) return next;
}
}
@@ -127,13 +191,29 @@ function roundsNext(view: View, frozen: Frozen): Next | null {
/** 第 round 轮开始了没有:看账本里有没有这一轮的阶段、跳过事件或决定。 */
function roundStarted(view: View, round: number): boolean {
const prefix = `r${round}-`;
if ([...view.board.stages.keys()].some((id) => id.startsWith(prefix))) return true;
if (DECISION_KINDS.some((kind) => view.board.decision(kind, round) !== undefined)) return true;
return view.events(EVENTS.skipped).some((e) => typeof e.objects[0] === "string" && e.objects[0].startsWith(prefix));
if ([...view.board.stages.keys()].some((id) => id.startsWith(prefix)))
return true;
if (
DECISION_KINDS.some(
(kind) => view.board.decision(kind, round) !== undefined,
)
)
return true;
return view
.events(EVENTS.skipped)
.some(
(e) =>
typeof e.objects[0] === "string" && e.objects[0].startsWith(prefix),
);
}
/** 第 round 轮的一个要执行的步骤还要做什么;做完了交回 null。 */
function roundStepNext(step: RoundStepId, round: number, view: View, frozen: Frozen): Next | null {
function roundStepNext(
step: RoundStepId,
round: number,
view: View,
frozen: Frozen,
): Next | null {
switch (step) {
case "openRound":
case "carryOverPool":
@@ -144,25 +224,55 @@ function roundStepNext(step: RoundStepId, round: number, view: View, frozen: Fro
case "critique":
case "response":
case "secondPrediction":
return stageNext(stageIdOf(round, step), step, round, (roster) => roundTasks(step, round, roster, view.board), view, frozen, researchers(frozen));
return stageNext(
stageIdOf(round, step),
step,
round,
(roster) => roundTasks(step, round, roster, view.board),
view,
frozen,
researchers(frozen),
);
case "dedupe":
// 判重合并 → 本轮的判重合并决定(decision,种类 dedup)。
return view.board.decision("dedup", round) === undefined ? { do: "dedupe", round } : null;
return view.board.decision("dedup", round) === undefined
? { do: "dedupe", round }
: null;
case "freezeProtocol": {
// 冻结协议 → 本轮每个新候选都有冻结协议记录(protocol)。全部候选在同一个事务里写;
// 这一步排在回应阶段关闭之后,materials 把冻结协议的出现当作"已公开",早写会泄露。
if (frozen.steps.round.some((s) => s.id === "response" && s.status === "skip")) {
return { do: "halt", reason: "配置关掉了挑刺与回应时怎么冻结协议还没写:submissions 的 protocolBody 只收回应和统一", objects: [] };
if (
frozen.steps.round.some(
(s) => s.id === "response" && s.status === "skip",
)
) {
return {
do: "halt",
reason:
"配置关掉了挑刺与回应时怎么冻结协议还没写:submissions 的 protocolBody 只收回应和统一",
objects: [],
};
}
const frozenIds = new Set(view.board.protocols().map((p) => p.candidate));
return freshOf(view.board, round).every((c) => frozenIds.has(c.candidate)) ? null : { do: "freezeProtocols", round };
return freshOf(view.board, round).every((c) => frozenIds.has(c.candidate))
? null
: { do: "freezeProtocols", round };
}
case "selectBatch":
// 选批 → 本轮的选批决定(decision,种类 batch)。
return view.board.decision("batch", round) === undefined ? { do: "selectBatch", round } : null;
return view.board.decision("batch", round) === undefined
? { do: "selectBatch", round }
: null;
case "implementation": {
const candidates = view.board.decision("batch", round)!.output.chosen;
return assignedStage("implementation", round, "implementation", candidates, view, frozen);
return assignedStage(
"implementation",
round,
"implementation",
candidates,
view,
frozen,
);
}
case "verification":
case "repair":
@@ -174,42 +284,94 @@ function roundStepNext(step: RoundStepId, round: number, view: View, frozen: Fro
// 不另写记录:整批运行已结束。采纳决定使结果整批公开,信息账从预测和结果现算。
return null;
case "adopt":
return view.board.decision("adopt", round) === undefined ? { do: "adopt", round } : null;
return view.board.decision("adopt", round) === undefined
? { do: "adopt", round }
: null;
case "listCells":
// 不写记录:格从采纳决定和冻结协议现算(方法.md 4.3 ⑫),格编号用 materials 的 cellId。
return null;
case "explanation": {
// ⑭:这一轮有消融(采纳了、且被采纳改法有两个以上因子)才开解释阶段;否则直接到收尾(方法.md 4.3 ⑫)。
// 解释 → 这一轮解释阶段的关闭记录。
const adopted = view.board.decision("adopt", round)?.output.adopted ?? null;
if (adopted === null || view.board.protocol(adopted).factors.length < 2) return null;
if (frozen.steps.round.some((s) => s.id === "listCells" && s.status === "skip")) {
return { do: "halt", reason: "配置关掉了消融、又采纳了多因子改法:不交三个数的解释还没写(cells 只按完整流程全开实现,做步骤开关时再补)", objects: [] };
const adopted =
view.board.decision("adopt", round)?.output.adopted ?? null;
if (adopted === null || view.board.protocol(adopted).factors.length < 2)
return null;
if (
frozen.steps.round.some(
(s) => s.id === "listCells" && s.status === "skip",
)
) {
return {
do: "halt",
reason:
"配置关掉了消融、又采纳了多因子改法:不交三个数的解释还没写(cells 只按完整流程全开实现,做步骤开关时再补)",
objects: [],
};
}
return stageNext(stageIdOf(round, "explanation"), "explanation", round, (roster) => roster.map((actor) => ({ actor, kind: "explanation" as const, object: null })), view, frozen, researchers(frozen));
return stageNext(
stageIdOf(round, "explanation"),
"explanation",
round,
(roster) =>
roster.map((actor) => ({
actor,
kind: "explanation" as const,
object: null,
})),
view,
frozen,
researchers(frozen),
);
}
case "selectCells":
// ⑮ → 本轮的选格决定(decision,种类 cells)。没选中的格不另写事件:原因都在这条决定里。
if (ablationOf(view.board, round) === null) return null;
return view.board.decision("cells", round) === undefined ? { do: "selectCells", round } : null;
return view.board.decision("cells", round) === undefined
? { do: "selectCells", round }
: null;
case "ablationImplementation":
case "ablationVerification":
// ⑯ → 每个选中的格:交了"跑不起来",或最近一次核验通过,或重做次数用完、有"不跑"事件。
return ablationOf(view.board, round) === null ? null : ablationCellsNext(round, view, frozen);
return ablationOf(view.board, round) === null
? null
: ablationCellsNext(round, view, frozen);
case "ablationRun":
// ⑯ → 每个通过核验的格,最近一次消融运行有结果,而且不是"没启动"。
return ablationOf(view.board, round) === null ? null : ablationRunNext(round, view);
return ablationOf(view.board, round) === null
? null
: ablationRunNext(round, view);
case "settle":
// ⑰ → 本轮的结算记录(settlement),以及同一事务里的押的因子决定(decision,种类 bet)。
if (ablationOf(view.board, round) === null) return null;
return view.board.settlement(round) === undefined || view.board.decision("bet", round) === undefined ? { do: "settle", round } : null;
return view.board.settlement(round) === undefined ||
view.board.decision("bet", round) === undefined
? { do: "settle", round }
: null;
case "wrapUp": {
// ⑱ → 收尾阶段的关闭记录,以及本轮选中的每个候选都有解封记录。
const stage = stageNext(stageIdOf(round, "wrapUp"), "wrapUp", round, (roster) => roster.map((actor) => ({ actor, kind: "wrapUp" as const, object: null })), view, frozen, researchers(frozen));
const stage = stageNext(
stageIdOf(round, "wrapUp"),
"wrapUp",
round,
(roster) =>
roster.map((actor) => ({
actor,
kind: "wrapUp" as const,
object: null,
})),
view,
frozen,
researchers(frozen),
);
if (stage !== null) return stage;
// 轮末解封(方法.md 4.3 ⑱):只在收尾阶段关闭之后写,提前写会泄露。本轮全部解封写在同一个事务里。
const chosen = view.board.decision("batch", round)!.output.chosen;
return chosen.every((c) => view.of("unseal").some((u) => u.candidate === c)) ? null : { do: "unsealChosen", round };
return chosen.every((c) =>
view.of("unseal").some((u) => u.candidate === c),
)
? null
: { do: "unsealChosen", round };
}
default:
throw new Error(`第 ${round} 轮的 ${step} 没有对应的下一步写法`);
@@ -217,13 +379,19 @@ function roundStepNext(step: RoundStepId, round: number, view: View, frozen: Fro
}
/** 第 round 轮判重合并出的新候选,以及各合并了哪几条原提案。 */
export function freshOf(board: Board, round: number): Array<{ candidate: string; proposals: string[] }> {
export function freshOf(
board: Board,
round: number,
): Array<{ candidate: string; proposals: string[] }> {
return board.decision("dedup", round)!.output.fresh;
}
/** 第 round 轮的候选池:新候选加留池候选(方法.md 4.3 ①⑦)。第二次预测和选批都用这一份。 */
export function poolOf(board: Board, round: number): string[] {
return [...freshOf(board, round).map((c) => c.candidate), ...board.carried(round)];
return [
...freshOf(board, round).map((c) => c.candidate),
...board.carried(round),
];
}
/**
@@ -234,17 +402,34 @@ export function poolOf(board: Board, round: number): string[] {
* - 回应与统一:每个新候选一个任务,一条原提案的由作者交回应,几条合成的由其中编号最小的作者交统一(方法.md 4.3 ③⑥);
* - 第二次预测:每人 × 候选池里每个候选。
*/
function roundTasks(step: RoundStageStep, round: number, roster: readonly string[], board: Board): TaskRef[] {
function roundTasks(
step: RoundStageStep,
round: number,
roster: readonly string[],
board: Board,
): TaskRef[] {
const each = (kind: TaskRef["kind"], objects: readonly string[]): TaskRef[] =>
roster.flatMap((actor) => objects.map((object) => ({ actor, kind, object })));
roster.flatMap((actor) =>
objects.map((object) => ({ actor, kind, object })),
);
switch (step) {
case "proposal":
return roster.map((actor) => ({ actor, kind: "proposal", object: null }));
case "firstPrediction":
return each("firstPrediction", freshOf(board, round).map((c) => c.candidate));
return each(
"firstPrediction",
freshOf(board, round).map((c) => c.candidate),
);
case "critique": {
const proposals = board.published("proposal").filter((p) => p.round === round).sort((a, b) => compareIds(a.item!, b.item!));
return roster.flatMap((actor) => proposals.filter((p) => p.author !== actor).map((p) => ({ actor, kind: "critique" as const, object: p.item! })));
const proposals = board
.published("proposal")
.filter((p) => p.round === round)
.sort((a, b) => compareIds(a.item!, b.item!));
return roster.flatMap((actor) =>
proposals
.filter((p) => p.author !== actor)
.map((p) => ({ actor, kind: "critique" as const, object: p.item! })),
);
}
case "response":
return freshOf(board, round).map((c) => ({
@@ -262,16 +447,28 @@ function roundTasks(step: RoundStageStep, round: number, roster: readonly string
* 没启动的起新编号重跑;判为不算数的停下(方法.md 4.1)。
*/
function baselineNext(view: View): Next | null {
if (!view.of("code").some((c) => c.object === ORIGINAL_BASELINE)) return { do: "saveBaseline" };
if (!view.of("code").some((c) => c.object === ORIGINAL_BASELINE))
return { do: "saveBaseline" };
const plans = view.of("runPlanned").filter((p) => p.purpose === "baseline");
const last = plans.at(-1);
if (last === undefined) return { do: "baselineRun", run: baselineRunId(1) };
const result = view.of("runResult").find((r) => r.run === last.run);
if (result === undefined) throw new Error(`基线运行 ${last.run} 登记了还没有结果:恢复入口应当已经核对过`);
if (result.process.ending === "notStarted") return { do: "baselineRun", run: baselineRunId(plans.length + 1) };
if (result === undefined)
throw new Error(
`基线运行 ${last.run} 登记了还没有结果:恢复入口应当已经核对过`,
);
if (result.process.ending === "notStarted")
return { do: "baselineRun", run: baselineRunId(plans.length + 1) };
if (result.judged?.verdict === "valid") return null;
const why = result.judged === null ? result.process.ending : result.judged.verdictReason;
return { do: "halt", reason: `基线运行 ${last.run} 判为不算数(${why}),不能当基线`, objects: [last.run] };
const why =
result.judged === null
? result.process.ending
: result.judged.verdictReason;
return {
do: "halt",
reason: `基线运行 ${last.run} 判为不算数(${why}),不能当基线`,
objects: [last.run],
};
}
/**
@@ -300,7 +497,8 @@ function stageNext(
for (const t of todo) {
const last = view.lastCall(stageId, t);
// 没有调用、被用户停下、出错(含恢复时补写的"进程中断")都派;没交的只在开发入口开着时派(方法.md 第九部分"开发入口")。
if (last?.status === "notSubmitted" && !frozen.config.devEntry) stuck.push({ call: last.call, reason: last.reason });
if (last?.status === "notSubmitted" && !frozen.config.devEntry)
stuck.push({ call: last.call, reason: last.reason });
else tasks.push(t);
}
if (tasks.length > 0) return { do: "dispatch", stageId, tasks };
@@ -312,44 +510,95 @@ function stageNext(
}
/** 研究者阶段的冻结名单。实验员、核验员阶段则按本阶段的任务列名单。 */
const researchers = (frozen: Frozen): string[] => frozen.config.models.researchers.map((_, i) => researcherId(i));
const researchers = (frozen: Frozen): string[] =>
frozen.config.models.researchers.map((_, i) => researcherId(i));
/** 正向的实现、修复、核验共用阶段开关和派发规则,每个候选固定一个人。 */
function assignedStage(
step: string,
round: number,
kind: "implementation" | "repair" | "verification" | "ablationImplementation" | "ablationVerification",
kind:
| "implementation"
| "repair"
| "verification"
| "ablationImplementation"
| "ablationVerification",
candidates: readonly string[],
view: View,
frozen: Frozen,
): Next | null {
const actorOf = SUBMISSION_KINDS[kind].role === "verifier" ? verifierId : experimenterId;
const tasks = candidates.map((object) => ({ actor: actorOf(object), kind, object }));
return stageNext(stageIdOf(round, step), SUBMISSION_KINDS[kind].stage, round, () => tasks, view, frozen, tasks.map((t) => t.actor));
const actorOf =
SUBMISSION_KINDS[kind].role === "verifier" ? verifierId : experimenterId;
const tasks = candidates.map((object) => ({
actor: actorOf(object),
kind,
object,
}));
return stageNext(
stageIdOf(round, step),
SUBMISSION_KINDS[kind].stage,
round,
() => tasks,
view,
frozen,
tasks.map((t) => t.actor),
);
}
/**
* ⑨:实现全部关闭后开核验;核验全部关闭后,失败的才修复。每次只重验上次修复的候选。
* 第几次由已开关的阶段推出来;次数用完先写不跑事件,不登记运行(方法.md 4.3 ⑨)。
*/
function verificationNext(round: number, view: View, frozen: Frozen): Next | null {
function verificationNext(
round: number,
view: View,
frozen: Frozen,
): Next | null {
let candidates = view.board.decision("batch", round)!.output.chosen;
for (let n = 1; candidates.length > 0; n++) {
const verificationId = stageIdOf(round, `verification-${n}`);
const verify = assignedStage(`verification-${n}`, round, "verification", candidates, view, frozen);
const verify = assignedStage(
`verification-${n}`,
round,
"verification",
candidates,
view,
frozen,
);
if (verify !== null) return verify;
const failed = view.board.published("verification")
.filter((s) => s.stageId === verificationId && !verificationOutcome(s.content).passed);
const failed = view.board
.published("verification")
.filter(
(s) =>
s.stageId === verificationId &&
!verificationOutcome(s.content).passed,
);
if (n > frozen.config.maxRepairs) {
for (const s of failed) {
if (view.events(FORWARD_NOT_RUN).some((e) => e.objects.includes(s.target!))) continue;
return { do: "forwardNotRun", candidate: s.target!, reason: `第 ${n} 次核验仍不通过,修复次数上限 ${frozen.config.maxRepairs} 已用完:${JSON.stringify(verificationOutcome(s.content))};${s.content.body}` };
if (
view
.events(FORWARD_NOT_RUN)
.some((e) => e.objects.includes(s.target!))
)
continue;
return {
do: "forwardNotRun",
candidate: s.target!,
reason: `第 ${n} 次核验仍不通过,修复次数上限 ${frozen.config.maxRepairs} 已用完:${JSON.stringify(verificationOutcome(s.content))};${s.content.body}`,
};
}
return null;
}
candidates = failed.map((s) => s.target!);
if (candidates.length === 0) return null;
const repair = assignedStage(`repair-${n}`, round, "repair", candidates, view, frozen);
const repair = assignedStage(
`repair-${n}`,
round,
"repair",
candidates,
view,
frozen,
);
if (repair !== null) return repair;
}
return null;
@@ -358,12 +607,29 @@ function verificationNext(round: number, view: View, frozen: Frozen): Next | nul
/** ⑩:按选批顺序一次只交回一个运行;没启动的下次启动起新编号,不会把半批提前公开。 */
function forwardNext(round: number, view: View): Next | null {
for (const candidate of view.board.decision("batch", round)!.output.chosen) {
if (view.events(FORWARD_NOT_RUN).some((e) => e.objects.includes(candidate))) continue;
const runs = view.board.runs().filter((r) => r.plan.purpose === "forward" && r.plan.round === round && r.plan.object === candidate);
if (view.events(FORWARD_NOT_RUN).some((e) => e.objects.includes(candidate)))
continue;
const runs = view.board
.runs()
.filter(
(r) =>
r.plan.purpose === "forward" &&
r.plan.round === round &&
r.plan.object === candidate,
);
const last = runs.at(-1);
if (last !== undefined && last.result === null) throw new Error(`正向运行 ${last.plan.run} 登记了还没有结果:恢复入口应当已经核对过`);
if (last !== undefined && last.result!.process.ending !== "notStarted") continue;
return { do: "forwardRun", round, candidate, run: `r${round}-forward-${candidate}-${runs.length + 1}` };
if (last !== undefined && last.result === null)
throw new Error(
`正向运行 ${last.plan.run} 登记了还没有结果:恢复入口应当已经核对过`,
);
if (last !== undefined && last.result!.process.ending !== "notStarted")
continue;
return {
do: "forwardRun",
round,
candidate,
run: `r${round}-forward-${candidate}-${runs.length + 1}`,
};
}
return null;
}
@@ -372,40 +638,74 @@ function forwardNext(round: number, view: View): Next | null {
* 这一轮做不做消融、消融哪个候选的哪几个因子(方法.md 4.3 ⑫):采纳了、且被采纳改法有两个以上因子。
* 没采纳或只采纳了单因子改法时 ⑬–⑰ 都不做:这几步不写记录,从采纳决定和冻结协议就算得出。
*/
export function ablationOf(board: Board, round: number): { candidate: string; keys: string[] } | null {
export function ablationOf(
board: Board,
round: number,
): { candidate: string; keys: string[] } | null {
const adopted = board.decision("adopt", round)?.output.adopted ?? null;
if (adopted === null) return null;
const keys = listCells(board.protocol(adopted).factors.map((_, i) => factorKey(i)));
const keys = listCells(
board.protocol(adopted).factors.map((_, i) => factorKey(i)),
);
return keys.length < 2 ? null : { candidate: adopted, keys };
}
/** 选格决定选中要测的格(对象编号用 materials 的 cellId),按因子编号排。 */
export function chosenCells(board: Board, round: number): string[] {
const { candidate } = ablationOf(board, round)!;
return board.decision("cells", round)!.output.chosen.map((key) => cellId(candidate, key));
return board
.decision("cells", round)!
.output.chosen.map((key) => cellId(candidate, key));
}
/** 一格最近一次已公开的消融实现提交(阶段关闭即公开);没有为 undefined。 */
export function lastAblationImpl(board: Board, cell: string) {
return board.published("ablationImplementation").filter((s) => s.target === cell).at(-1);
return board
.published("ablationImplementation")
.filter((s) => s.target === cell)
.at(-1);
}
/** 选中的格里"活着"的(没交"跑不起来"、没有"不跑"事件),以及每格实际关掉的全部因子(指定的加实验员报的一起关的)。 */
export function aliveCells(board: Board, events: readonly Content<"event">[], round: number): Array<{ cell: string; off: string[] }> {
export function aliveCells(
board: Board,
events: readonly Content<"event">[],
round: number,
): Array<{ cell: string; off: string[] }> {
return chosenCells(board, round)
.map((cell) => ({ cell, impl: lastAblationImpl(board, cell) }))
.filter((x) => x.impl !== undefined && x.impl.content.cannotRun === null && !events.some((e) => e.event === CELL_NOT_RUN && e.objects.includes(x.cell)))
.map((x) => ({ cell: x.cell, off: [splitCell(x.cell).key, ...x.impl!.content.alsoOff] }));
.filter(
(x) =>
x.impl !== undefined &&
x.impl.content.cannotRun === null &&
!events.some(
(e) => e.event === CELL_NOT_RUN && e.objects.includes(x.cell),
),
)
.map((x) => ({
cell: x.cell,
off: [splitCell(x.cell).key, ...x.impl!.content.alsoOff],
}));
}
/**
* 要跑消融运行的格:活着的格里同设置的第一格(方法.md 6.6"一起关":几格实际关掉的因子完全相同就是同一种设置,
* 沿用第一格的结果,不重跑、不重复扣次、不重复记账)。
*/
export function cellsToRun(board: Board, events: readonly Content<"event">[], round: number): string[] {
export function cellsToRun(
board: Board,
events: readonly Content<"event">[],
round: number,
): string[] {
const alive = aliveCells(board, events, round);
const sameAs = new Map(sharedSettings(alive.map((x) => ({ factor: splitCell(x.cell).key, off: x.off }))).map((x) => [x.factor, x.sameAs]));
return alive.filter((x) => sameAs.get(splitCell(x.cell).key) === null).map((x) => x.cell);
const sameAs = new Map(
sharedSettings(
alive.map((x) => ({ factor: splitCell(x.cell).key, off: x.off })),
).map((x) => [x.factor, x.sameAs]),
);
return alive
.filter((x) => sameAs.get(splitCell(x.cell).key) === null)
.map((x) => x.cell);
}
/**
@@ -413,26 +713,60 @@ export function cellsToRun(board: Board, events: readonly Content<"event">[], ro
* 从它上一次交的这一格代码接着改,每次只重验刚重做的格。阶段编号按"第几次"排,第几次从已开关的阶段数出来,不另存。
* 交了"跑不起来"的格不核验、不运行、不扣次;核验不通过、次数用完写"不跑"事件,不登记运行(方法.md 4.3 ⑯、6.6)。
*/
function ablationCellsNext(round: number, view: View, frozen: Frozen): Next | null {
let cells = chosenCells(view.board, round).filter((cell) => !view.events(CELL_NOT_RUN).some((e) => e.objects.includes(cell)));
function ablationCellsNext(
round: number,
view: View,
frozen: Frozen,
): Next | null {
let cells = chosenCells(view.board, round).filter(
(cell) => !view.events(CELL_NOT_RUN).some((e) => e.objects.includes(cell)),
);
for (let n = 1; cells.length > 0; n++) {
const impl = assignedStage(`ablationImplementation-${n}`, round, "ablationImplementation", cells, view, frozen);
const impl = assignedStage(
`ablationImplementation-${n}`,
round,
"ablationImplementation",
cells,
view,
frozen,
);
if (impl !== null) return impl;
const runnable = cells.filter((cell) => lastAblationImpl(view.board, cell)!.content.cannotRun === null);
const runnable = cells.filter(
(cell) => lastAblationImpl(view.board, cell)!.content.cannotRun === null,
);
const failed: Array<{ cell: string; reason: string }> = [];
if (runnable.length > 0) {
const ver = assignedStage(`ablationVerification-${n}`, round, "ablationVerification", runnable, view, frozen);
const ver = assignedStage(
`ablationVerification-${n}`,
round,
"ablationVerification",
runnable,
view,
frozen,
);
if (ver !== null) return ver;
for (const s of view.board.published("ablationVerification").filter((x) => x.stageId === stageIdOf(round, `ablationVerification-${n}`))) {
const outcome = ablationOutcome(s.content, splitCell(s.target!).key, lastAblationImpl(view.board, s.target!)!.content.alsoOff);
for (const s of view.board
.published("ablationVerification")
.filter(
(x) => x.stageId === stageIdOf(round, `ablationVerification-${n}`),
)) {
const outcome = ablationOutcome(
s.content,
splitCell(s.target!).key,
lastAblationImpl(view.board, s.target!)!.content.alsoOff,
);
if (!outcome.passed) {
failed.push({ cell: s.target!, reason: `第 ${n} 次核验仍不通过,修复次数上限 ${frozen.config.maxRepairs} 已用完:${JSON.stringify({ leftOn: outcome.leftOn, wronglyOff: outcome.wronglyOff })}` });
failed.push({
cell: s.target!,
reason: `第 ${n} 次核验仍不通过,修复次数上限 ${frozen.config.maxRepairs} 已用完:${JSON.stringify({ leftOn: outcome.leftOn, wronglyOff: outcome.wronglyOff })}`,
});
}
}
}
if (n > frozen.config.maxRepairs) {
for (const f of failed) {
if (view.events(CELL_NOT_RUN).some((e) => e.objects.includes(f.cell))) continue;
if (view.events(CELL_NOT_RUN).some((e) => e.objects.includes(f.cell)))
continue;
return { do: "cellNotRun", cell: f.cell, reason: f.reason };
}
return null;
@@ -446,11 +780,27 @@ function ablationCellsNext(round: number, view: View, frozen: Frozen): Next | nu
/** ⑯ 消融运行:按格编号顺序一次只交回一个运行(一份研究里的消融运行一个接一个跑,同正向);没启动的下次启动起新编号。 */
function ablationRunNext(round: number, view: View): Next | null {
for (const cell of cellsToRun(view.board, view.of("event"), round)) {
const runs = view.board.runs().filter((r) => r.plan.purpose === "ablation" && r.plan.round === round && r.plan.object === cell);
const runs = view.board
.runs()
.filter(
(r) =>
r.plan.purpose === "ablation" &&
r.plan.round === round &&
r.plan.object === cell,
);
const last = runs.at(-1);
if (last !== undefined && last.result === null) throw new Error(`消融运行 ${last.plan.run} 登记了还没有结果:恢复入口应当已经核对过`);
if (last !== undefined && last.result!.process.ending !== "notStarted") continue;
return { do: "ablationRun", round, cell, run: `r${round}-ablation-${cell}-${runs.length + 1}` };
if (last !== undefined && last.result === null)
throw new Error(
`消融运行 ${last.plan.run} 登记了还没有结果:恢复入口应当已经核对过`,
);
if (last !== undefined && last.result!.process.ending !== "notStarted")
continue;
return {
do: "ablationRun",
round,
cell,
run: `r${round}-ablation-${cell}-${runs.length + 1}`,
};
}
return null;
}
@@ -466,7 +816,9 @@ class View {
}
of<K extends LedgerRecord["kind"]>(kind: K): Array<Content<K>> {
return (this.#records.filter((r) => r.kind === kind) as Array<LedgerRecord<K>>).map((r) => r.content);
return (
this.#records.filter((r) => r.kind === kind) as Array<LedgerRecord<K>>
).map((r) => r.content);
}
events(event: string): Array<Content<"event">> {
@@ -475,7 +827,10 @@ class View {
/** 一个任务做完了:这个人在这个阶段对这个对象有一份被收下的提交(任务书 14 第 2.3 节)。 */
done(stageId: string, t: TaskRef): boolean {
return this.of("submission").some((s) => s.stageId === stageId && s.author === t.actor && s.target === t.object);
return this.of("submission").some(
(s) =>
s.stageId === stageId && s.author === t.actor && s.target === t.object,
);
}
/**
@@ -484,11 +839,19 @@ class View {
*/
lastCall(stageId: string, t: TaskRef): Content<"callEnded"> | null {
const started = this.of("callStarted")
.filter((c) => c.stageId === stageId && c.actor === t.actor && (c.materials as { object: string | null }).object === t.object)
.filter(
(c) =>
c.stageId === stageId &&
c.actor === t.actor &&
(c.materials as { object: string | null }).object === t.object,
)
.at(-1);
if (started === undefined) return null;
const ended = this.of("callEnded").find((e) => e.call === started.call);
if (ended === undefined) throw new Error(`模型调用 ${started.call} 开始了还没有结束记录:恢复入口应当已经补写`);
if (ended === undefined)
throw new Error(
`模型调用 ${started.call} 开始了还没有结束记录:恢复入口应当已经补写`,
);
return ended;
}
}
+103 -21
View File
@@ -1,5 +1,13 @@
import type { Content } from "../../ledger/index.ts";
import { Board, cellId, CELL_NOT_RUN, explanationId, protocolMetrics, splitCell, validScore } from "../../materials/index.ts";
import {
Board,
cellId,
CELL_NOT_RUN,
explanationId,
protocolMetrics,
splitCell,
validScore,
} from "../../materials/index.ts";
import {
chooseBet,
decisionSeed,
@@ -30,7 +38,11 @@ import type { Study } from "../study.ts";
export function selectCells(s: Study, round: number): void {
const board = new Board(s.ledger.records());
const { config, steps } = s.frozen;
const seed = decisionSeed({ studyId: config.studyId, round, version: SEED_VERSIONS.cells });
const seed = decisionSeed({
studyId: config.studyId,
round,
version: SEED_VERSIONS.cells,
});
const input: CellRuleInput = {
...modelInput(board, s, round),
balance: runBalance(config.runs, board.runStates()).balance,
@@ -39,7 +51,8 @@ export function selectCells(s: Study, round: number): void {
decay: config.flow.decay,
seed,
};
const rule = steps.round.find((x) => x.id === "selectCells")!.rule!.name as CellRuleName;
const rule = steps.round.find((x) => x.id === "selectCells")!.rule!
.name as CellRuleName;
const { rule: used, ...output } = runCellRule(rule, input);
// SAFETY: rules 是纯计算,输入输出都是纯 Json 值;账本在写入时校验 Json。
s.ledger.append("decision", {
@@ -55,14 +68,30 @@ export function selectCells(s: Study, round: number): void {
}
/** ⑯ 消融运行:用核验过的这一格快照,复用正式运行的登记、扣次、判分入口。完整代码和基线的分数取本批已有的运行结果,不重跑。 */
export function ablationRun(s: Study, round: number, cell: string, run: string): Promise<RunOutcome> {
export function ablationRun(
s: Study,
round: number,
cell: string,
run: string,
): Promise<RunOutcome> {
const board = new Board(s.ledger.records());
return formalRun(s, { run, purpose: "ablation", round, object: cell }, board.code(cell).version, ablationMetrics(board, splitCell(cell).candidate));
return formalRun(
s,
{ run, purpose: "ablation", round, object: cell },
board.code(cell).version,
ablationMetrics(board, splitCell(cell).candidate),
);
}
/** 消融格正式运行登记的指标:照被采纳候选的冻结协议取;消融格里假设专用指标缺失一律只记缺失、不作废这一格(方法.md 第五部分"指标")。 */
export function ablationMetrics(board: Board, candidate: string): HypothesisMetric[] {
return protocolMetrics(board, board.protocol(candidate)).map((m) => ({ ...m, missing: "recordMissing" }));
export function ablationMetrics(
board: Board,
candidate: string,
): HypothesisMetric[] {
return protocolMetrics(board, board.protocol(candidate)).map((m) => ({
...m,
missing: "recordMissing",
}));
}
/**
@@ -73,21 +102,37 @@ export function ablationMetrics(board: Board, candidate: string): HypothesisMetr
export function settle(s: Study, round: number): void {
const board = new Board(s.ledger.records());
const { candidate, keys } = ablationOf(board, round)!;
const events = s.ledger.records().flatMap((r) => (r.kind === "event" ? [r.content] : []));
const events = s.ledger
.records()
.flatMap((r) => (r.kind === "event" ? [r.content] : []));
const model = modelInput(board, s, round);
const input: SettleInput = { ...model, cells: keys.map((key) => ({ factor: key, outcome: outcomeOf(board, events, round, candidate, key) })) };
const input: SettleInput = {
...model,
cells: keys.map((key) => ({
factor: key,
outcome: outcomeOf(board, events, round, candidate, key),
})),
};
const output = settleAblation(input);
const betInput = { ...model, cells: input.cells };
const bet = chooseBet(betInput);
// SAFETY: rules 是纯计算,结算和决定的输入输出都是纯 Json 值;账本在写入时校验 Json。
s.ledger.transaction(() => {
s.ledger.append("settlement", { round, result: { candidate, input, output } as unknown as Json });
s.ledger.append("settlement", {
round,
result: { candidate, input, output } as unknown as Json,
});
s.ledger.append("decision", {
decision: "bet",
round,
rule: "chooseBet",
ruleVersion: 1,
params: { direction: model.direction, epsilon: model.epsilon, knownBest: model.knownBest, lambda: model.lambda },
params: {
direction: model.direction,
epsilon: model.epsilon,
knownBest: model.knownBest,
lambda: model.lambda,
},
seed: null,
input: betInput as unknown as Json,
output: bet as unknown as Json,
@@ -96,19 +141,40 @@ export function settle(s: Study, round: number): void {
}
/** 一格的结局(方法.md 6.6"一格的几种结局"):没测、跑不起来、证据不足、一起关、有效分数。 */
function outcomeOf(board: Board, events: readonly Content<"event">[], round: number, candidate: string, key: string): CellOutcome {
if (!board.decision("cells", round)!.output.chosen.includes(key)) return { kind: "untested" };
function outcomeOf(
board: Board,
events: readonly Content<"event">[],
round: number,
candidate: string,
key: string,
): CellOutcome {
if (!board.decision("cells", round)!.output.chosen.includes(key))
return { kind: "untested" };
const cell = cellId(candidate, key);
const impl = lastAblationImpl(board, cell)!;
if (impl.content.cannotRun !== null) return { kind: "cannotRun" };
if (events.some((e) => e.event === CELL_NOT_RUN && e.objects.includes(cell))) return { kind: "insufficient" };
if (events.some((e) => e.event === CELL_NOT_RUN && e.objects.includes(cell)))
return { kind: "insufficient" };
const off = [key, ...impl.content.alsoOff];
// 几格实际关掉的因子完全相同时,沿用同设置第一格的运行结果(不重跑,方法.md 6.6"一起关")。
const alive = aliveCells(board, events, round);
const sameAs = new Map(sharedSettings(alive.map((x) => ({ factor: splitCell(x.cell).key, off: x.off }))).map((x) => [x.factor, x.sameAs]));
const sameAs = new Map(
sharedSettings(
alive.map((x) => ({ factor: splitCell(x.cell).key, off: x.off })),
).map((x) => [x.factor, x.sameAs]),
);
const first = sameAs.get(key);
const ranCell = first === null || first === undefined ? cell : cellId(candidate, first);
const run = board.runs().filter((r) => r.plan.purpose === "ablation" && r.plan.round === round && r.plan.object === ranCell).at(-1)!;
const ranCell =
first === null || first === undefined ? cell : cellId(candidate, first);
const run = board
.runs()
.filter(
(r) =>
r.plan.purpose === "ablation" &&
r.plan.round === round &&
r.plan.object === ranCell,
)
.at(-1)!;
const score = validScore(run.result!.judged);
if (off.length > 1) return { kind: "joint", off, score };
return score === null ? { kind: "insufficient" } : { kind: "valid", score };
@@ -125,11 +191,21 @@ export function unsealChosen(s: Study, round: number): void {
const chosen = board.decision("batch", round)!.output.chosen;
s.ledger.transaction(() => {
for (const candidate of chosen) {
if (records.some((r) => r.kind === "unseal" && r.content.candidate === candidate)) continue;
if (
records.some(
(r) => r.kind === "unseal" && r.content.candidate === candidate,
)
)
continue;
const seals = board.proposalsOf(candidate).map((id) => {
const seq = board.proposal(id).seq;
const seal = records.find((r) => r.kind === "seal" && r.content.proposal === seq);
if (seal === undefined) throw new Error(`提案 ${id}(记录 ${seq})没有封存记录:提案的作者自评在写入提案时一并封存`);
const seal = records.find(
(r) => r.kind === "seal" && r.content.proposal === seq,
);
if (seal === undefined)
throw new Error(
`提案 ${id}(记录 ${seq})没有封存记录:提案的作者自评在写入提案时一并封存`,
);
return seal.seq;
});
s.ledger.append("unseal", { candidate, seals });
@@ -158,5 +234,11 @@ function explanationsOf(board: Board, round: number): AblationExplanation[] {
return board
.published("explanation")
.filter((x) => x.round === round)
.flatMap((x) => x.content.explanations.map((e, i) => ({ id: explanationId(round, x.author, i + 1), author: x.author, cells: e.cells! })));
.flatMap((x) =>
x.content.explanations.map((e, i) => ({
id: explanationId(round, x.author, i + 1),
author: x.author,
cells: e.cells!,
})),
);
}