refactor(rectification): split runV9AgentTurn into prepare / attempt / retry / finish

Move-only (TASK-rectification-code-split-20260926 T3/T4). No behavior,
receipt, billing, Skill or scoring change.

- agent-run.ts: 1400 -> 135 lines; runV9AgentTurn 1046 -> 25 lines, no
  nested functions. It keeps the public types, the budget constants and the
  phase order: agent-run-prepare.ts (reliability, session, year re-ask, Skill
  identity, delivered guard, reserve, turn row / replay),
  agent-run-retry.ts (attempt loop), agent-run-attempt.ts (the old nested
  streamAttempt), agent-run-finish.ts (billing settle, receipts, interview,
  finalize), agent-run-support.ts (outcome types, retry classification, turn
  receipt writers), agent-run-messages.ts (buildAgentMessages /
  buildOpeningBrief, re-exported).
- One token changed with the move: the attempt passes its own
  `previousErrorCode` argument to buildAgentMessages instead of reading the
  enclosing `lastAttemptError`; the loop passes that same value (clears the
  old unused-parameter warning).
- Tests: whole-source contracts read tests/rectification-agent-run-surface.ts;
  behavioral runV9AgentTurn tests unchanged. agent-run growth caps added.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017eEAG8HD3mm8gsKXgk8uU8
This commit is contained in:
Jesse_Chen
2026-09-26 19:52:51 +08:00
co-authored by Claude Opus 5.5
parent eb773f1299
commit a00c40d4b8
25 changed files with 1665 additions and 1332 deletions
@@ -0,0 +1,29 @@
import { readFileSync } from "node:fs";
/**
* The V10 rectification turn runner as source text: agent-run.ts (public
* types and the order of the phases) plus the phase modules it was split into
* (TASK-rectification-code-split-20260926), in the order the code ran inside
* the old runV9AgentTurn: preparation, the retry loop, the finish, one
* attempt, then the shared pieces and the model messages. A whole-source
* contract that held on agent-run.ts before the split holds on this
* concatenation after it. Slices that no longer sit in one file are
* rewritten per test.
*/
export const rectificationAgentRunFiles = [
"../src/lib/rectification-agentic/v9/agent-run.ts",
"../src/lib/rectification-agentic/v9/agent-run-prepare.ts",
"../src/lib/rectification-agentic/v9/agent-run-retry.ts",
"../src/lib/rectification-agentic/v9/agent-run-finish.ts",
"../src/lib/rectification-agentic/v9/agent-run-attempt.ts",
"../src/lib/rectification-agentic/v9/agent-run-support.ts",
"../src/lib/rectification-agentic/v9/agent-run-messages.ts",
] as const;
export function readRectificationAgentRunFile(relativePath: (typeof rectificationAgentRunFiles)[number]): string {
return readFileSync(new URL(relativePath, import.meta.url), "utf8");
}
export const rectificationAgentRunSurface = rectificationAgentRunFiles
.map((relativePath) => readRectificationAgentRunFile(relativePath))
.join("\n");