refactor: moderator uses dual limits — max coder rounds (20) + max total rejections (10)

Either limit triggers END. Simple, no per-rejector budgets.
This commit is contained in:
小橘 2026-04-28 14:27:40 +00:00
parent bbcaf1eba5
commit 3d9f239230
3 changed files with 55 additions and 36 deletions

View File

@ -18,16 +18,23 @@ export type WorkflowMeta = {
"pr-publisher": PrPublisherMeta; "pr-publisher": PrPublisherMeta;
}; };
const MAX_IMPLEMENTER_SELF_ITERATIONS = 5; const MAX_IMPLEMENTER_ROUNDS = 20;
const MAX_REVIEWER_REJECTIONS = 3; const MAX_TOTAL_REJECTIONS = 10;
const MAX_TESTER_REJECTIONS = 3;
function countRejections(steps: { role: string; meta: unknown }[], rejector: string, metaKey: string): number { function implementerRounds(steps: { role: string }[]): number {
return steps.filter((s) => s.role === rejector && !(s.meta as Record<string, boolean>)[metaKey]).length; return steps.filter((s) => s.role === "implementer").length;
} }
function countImplementerSelfIterations(steps: { role: string; meta: unknown }[]): number { function totalRejections(steps: { role: string; meta: unknown }[]): number {
return steps.filter((s) => s.role === "implementer" && !(s.meta as Record<string, boolean>).implementationOk).length; return steps.filter((s) => {
if (s.role === "reviewer") return !(s.meta as Record<string, boolean>).approved;
if (s.role === "tester") return !(s.meta as Record<string, boolean>).passed;
return false;
}).length;
}
function canRetryImplementer(steps: { role: string; meta: unknown }[]): boolean {
return implementerRounds(steps) < MAX_IMPLEMENTER_ROUNDS && totalRejections(steps) < MAX_TOTAL_REJECTIONS;
} }
export const moderator: Moderator<WorkflowMeta> = (context) => { export const moderator: Moderator<WorkflowMeta> = (context) => {
@ -55,18 +62,18 @@ export const moderator: Moderator<WorkflowMeta> = (context) => {
if (last.role === "implementer") { if (last.role === "implementer") {
const meta = last.meta as WorkflowMeta["implementer"]; const meta = last.meta as WorkflowMeta["implementer"];
if (meta.implementationOk) return "reviewer"; if (meta.implementationOk) return "reviewer";
return countImplementerSelfIterations(context.steps) < MAX_IMPLEMENTER_SELF_ITERATIONS ? "implementer" : END; return canRetryImplementer(context.steps) ? "implementer" : END;
} }
if (last.role === "reviewer") { if (last.role === "reviewer") {
if (last.meta.approved) return "tester"; if (last.meta.approved) return "tester";
return countRejections(context.steps, "reviewer", "approved") < MAX_REVIEWER_REJECTIONS ? "implementer" : END; return canRetryImplementer(context.steps) ? "implementer" : END;
} }
if (last.role === "tester") { if (last.role === "tester") {
const meta = last.meta as WorkflowMeta["tester"]; const meta = last.meta as WorkflowMeta["tester"];
if (meta.passed) return "pr-publisher"; if (meta.passed) return "pr-publisher";
return countRejections(context.steps, "tester", "passed") < MAX_TESTER_REJECTIONS ? "implementer" : END; return canRetryImplementer(context.steps) ? "implementer" : END;
} }
if (last.role === "pr-publisher") { if (last.role === "pr-publisher") {

View File

@ -14,17 +14,24 @@ export type SenseMeta = {
committer: CommitterMeta; committer: CommitterMeta;
}; };
const MAX_CODER_SELF_ITERATIONS = 5; const MAX_CODER_ROUNDS = 20;
const MAX_REVIEWER_REJECTIONS = 3; const MAX_TOTAL_REJECTIONS = 10;
const MAX_TESTER_REJECTIONS = 3;
const MAX_COMMITTER_REJECTIONS = 2;
function countRejections(steps: { role: string; meta: unknown }[], rejector: string, metaKey: string): number { function coderRounds(steps: { role: string }[]): number {
return steps.filter((s) => s.role === rejector && !(s.meta as Record<string, boolean>)[metaKey]).length; return steps.filter((s) => s.role === "coder").length;
} }
function countCoderSelfIterations(steps: { role: string; meta: unknown }[]): number { function totalRejections(steps: { role: string; meta: unknown }[]): number {
return steps.filter((s) => s.role === "coder" && !(s.meta as Record<string, boolean>).done).length; return steps.filter((s) => {
if (s.role === "reviewer") return !(s.meta as Record<string, boolean>).approved;
if (s.role === "tester") return !(s.meta as Record<string, boolean>).passed;
if (s.role === "committer") return !(s.meta as Record<string, boolean>).success;
return false;
}).length;
}
function canRetryCoder(steps: { role: string; meta: unknown }[]): boolean {
return coderRounds(steps) < MAX_CODER_ROUNDS && totalRejections(steps) < MAX_TOTAL_REJECTIONS;
} }
export const moderator: Moderator<SenseMeta> = (context) => { export const moderator: Moderator<SenseMeta> = (context) => {
@ -36,22 +43,22 @@ export const moderator: Moderator<SenseMeta> = (context) => {
if (last.role === "coder") { if (last.role === "coder") {
if (last.meta.done) return "reviewer"; if (last.meta.done) return "reviewer";
return countCoderSelfIterations(context.steps) < MAX_CODER_SELF_ITERATIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
if (last.role === "reviewer") { if (last.role === "reviewer") {
if (last.meta.approved) return "tester"; if (last.meta.approved) return "tester";
return countRejections(context.steps, "reviewer", "approved") < MAX_REVIEWER_REJECTIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
if (last.role === "tester") { if (last.role === "tester") {
if (last.meta.passed) return "committer"; if (last.meta.passed) return "committer";
return countRejections(context.steps, "tester", "passed") < MAX_TESTER_REJECTIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
if (last.role === "committer") { if (last.role === "committer") {
if (last.meta.success) return END; if (last.meta.success) return END;
return countRejections(context.steps, "committer", "success") < MAX_COMMITTER_REJECTIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
return END; return END;

View File

@ -14,18 +14,24 @@ export type WorkflowMeta = {
committer: CommitterMeta; committer: CommitterMeta;
}; };
const MAX_CODER_SELF_ITERATIONS = 5; const MAX_CODER_ROUNDS = 20;
const MAX_REVIEWER_REJECTIONS = 3; const MAX_TOTAL_REJECTIONS = 10;
const MAX_TESTER_REJECTIONS = 3;
const MAX_COMMITTER_REJECTIONS = 2;
function countRejections(steps: { role: string; meta: unknown }[], rejector: string, metaKey: string): number { function coderRounds(steps: { role: string }[]): number {
return steps.filter((s) => s.role === rejector && !(s.meta as Record<string, boolean>)[metaKey]).length; return steps.filter((s) => s.role === "coder").length;
} }
function countCoderSelfIterations(steps: { role: string; meta: unknown }[]): number { function totalRejections(steps: { role: string; meta: unknown }[]): number {
// coder rounds where done=false (self-iteration, not a rejection retry) return steps.filter((s) => {
return steps.filter((s) => s.role === "coder" && !(s.meta as Record<string, boolean>).done).length; if (s.role === "reviewer") return !(s.meta as Record<string, boolean>).approved;
if (s.role === "tester") return !(s.meta as Record<string, boolean>).passed;
if (s.role === "committer") return !(s.meta as Record<string, boolean>).success;
return false;
}).length;
}
function canRetryCoder(steps: { role: string; meta: unknown }[]): boolean {
return coderRounds(steps) < MAX_CODER_ROUNDS && totalRejections(steps) < MAX_TOTAL_REJECTIONS;
} }
export const moderator: Moderator<WorkflowMeta> = (context) => { export const moderator: Moderator<WorkflowMeta> = (context) => {
@ -39,23 +45,22 @@ export const moderator: Moderator<WorkflowMeta> = (context) => {
if (last.role === "coder") { if (last.role === "coder") {
if (last.meta.done) return "reviewer"; if (last.meta.done) return "reviewer";
// coder says not done yet — allow self-iteration return canRetryCoder(context.steps) ? "coder" : END;
return countCoderSelfIterations(context.steps) < MAX_CODER_SELF_ITERATIONS ? "coder" : END;
} }
if (last.role === "reviewer") { if (last.role === "reviewer") {
if (last.meta.approved) return "tester"; if (last.meta.approved) return "tester";
return countRejections(context.steps, "reviewer", "approved") < MAX_REVIEWER_REJECTIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
if (last.role === "tester") { if (last.role === "tester") {
if (last.meta.passed) return "committer"; if (last.meta.passed) return "committer";
return countRejections(context.steps, "tester", "passed") < MAX_TESTER_REJECTIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
if (last.role === "committer") { if (last.role === "committer") {
if (last.meta.success) return END; if (last.meta.success) return END;
return countRejections(context.steps, "committer", "success") < MAX_COMMITTER_REJECTIONS ? "coder" : END; return canRetryCoder(context.steps) ? "coder" : END;
} }
return END; return END;