Skip to content

refactor(#1507): split runIssueWithLogging (1,620 lines) into stage functions of ≤ 200 counted lines, behaviour-preserving - #1578

Merged
admarble merged 2 commits into
mainfrom
feature/1507-refactor-workflow-split-runissuewithlogging-1-620-
Oct 23, 2026
Merged

admarble merged 2 commits into
mainfrom
feature/1507-refactor-workflow-split-runissuewithlogging-1-620-

Conversation

@admarble

@admarble admarble commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

The 1,072-counted-line function in src/lib/workflow/batch-executor.ts is now a short series of stage calls. The stages are private functions in the same file, so every vi.mock in the tests still resolves and the file's exports are the same. They pass two objects explicitly, with no module-level state:

  • IssueRunEnv is read-only and built once: the context, worktree path and branch, start time, auto-wait ledger, log/warn, the per-issue config and the relay activation.
  • IssueRunState is mutable and created per call: phase results, the resume handle, the phase list, the halt flags, the divergence bundle and the ladder state.

The stages:

  • Setup: initIssueRun.
  • Spec: dispatchSpecPhase, recordSpecOutcome, resolvePhasesFromSpec.
  • Phase plan: finalizePhasePlan.
  • Quality loop: runQualityLoop, runPhaseStep, dispatchLoopPhase, recordLoopPhaseOutcome, logLoopPhaseResult, handlePhaseFailure, runLoopFixPhase, assessIterationProgress.
  • After the phases: settleIssueStatus and deliverIssueBranch.
  • The old inner closures openOrUpdatePR, makeWaitTransition and buildBundle are now module-level functions.

Control flow: every old break or early return is now a value the caller checks on the next line:

  • runPhaseStep returns "next" or "break".
  • dispatchLoopPhase returns null for the top-of-ladder halt.
  • The setup and spec stages return the IssueResult that halts the issue.

All 26 noteSuppressed labels moved with their catches, unchanged.

Shared helpers: only code whose output was already byte-identical in both places is shared: buildPhaseErrorContext, phaseLogStatus and persistResumeHandle. Spec's run-log entry still has no diff stats, commit hash or verdict.

AC verification

AC Status Evidence
AC-1 ✅ The eslint max-lines-per-function command → 0
AC-2 ✅ The git diff --numstat test-file count → 0
AC-3 ✅ batch-executor.test.ts plus batch-executor.suppressed.integration.test.ts: 154/154 passed
AC-4 ✅ 1 1
AC-5 ✅ (runner) The golden run on ad-motion #285 prints identical; see "Golden run" below
AC-6 ✅ The baseline jq check → true; health-report --json shows over200 6, over400 1, longest RunOrchestrator.run (649)
AC-7 (derived) ✅ 15 test files call the function, 5 more than the spec counted (3 under __tests__/, 2 integration). All 15 passed: 368 tests
AC-8 (derived) ✅ The diff of ^export lines against origin/main → same
AC-9 (derived) ✅ npm run build, npm run lint, tsc and npm run lint:health are clean. knip reports nothing in batch-executor.ts; its other findings were already on main

Files changed

  • src/lib/workflow/batch-executor.ts: the split, plus three type-only imports (AutoWaitLedger, LadderState, LoopProgressSnapshot).
  • scripts/qa/health-baseline.json: I edited only the functions keys by hand. A full --write-baseline also moves testRatio28d (a rolling 28-day number this change doesn't affect) and the generatedAt/commit stamps, so I left those as they were, per the runner note.
  • docs/adr/0020-issue-run-stage-functions.md and the docs/adr/README.md index: the ADR is outside the issue's "May touch" list. The spec's Open Question 1 recommended including it.
  • CHANGELOG.md: one [Unreleased] → ### Changed line.

Checks

  • Affected tests:
    • npx vitest run --changed origin/main selected 47 files, run as --shard=1/3, 2/3 and 3/3: 16 / 16 / 15 files passed (274, 537 and 271 tests).
    • I ran the extra set in a separate call, because passing file filters together with --changed runs only files on both lists. It covered the 15 caller files plus 8 tests that read a changed file (adr-convention, health-report, ground-check, markdown-only-ci, merge-check-extended, assess-collision-detect, merger-release-checkout-lock, qa-markdown-on (runner: the generated body is cut here, at the 4000-character exec-summary cap in capImportedText`)

Golden run (AC-5, runner, 2026-10-22)

The testbed is ad-motion #285, a fixture issue with one fixed change (docs/golden-run.md). Both runs used sequant run 285 --phases spec,exec,qa --force --full-qa through the npm-linked dev dist/. Before each run I removed the fixture's branch, worktree, comments and planned label, and restored ad-motion's .sequant/ from one snapshot.

Build Run log (ad-motion .sequant/logs/) PR created spec / exec / qa
BEFORE main df795ca64 run-2026-10-22T03-01-05-41f479a0-07c2-4456-a928-be468b53f9bb.json #286 40 s / 61 s / 104 s
AFTER this PR 0672b1526 run-2026-10-22T03-31-46-05a8fddb-5845-4707-9f79-64d8eb2c1150.json #287 30 s / 25 s / 62 s

The AC-5 projection is the same for both runs, and diff prints identical:

[{"issue":285,"phases":[{"phase":"spec","status":"success"},{"phase":"exec","status":"success"},{"phase":"qa","status":"success"}],"status":"success"}]

Beyond the AC, the two runs also match on these points:

  • Both run logs record suppressed {total: 12, byScope: {other: 12}}.
  • Both runs created a PR that changes only docs/golden-run.md.
  • Spec added the planned label both times.
  • Each run posted 2 comments.

Both PRs are closed unmerged.

A superseded first AFTER run. The first AFTER run (run-2026-10-22T03-27-14-e724a5e7-…json) kept #285's original title. The branch name comes from the title, so it found the closed PR #286 by branch name and took the existing-PR path, not PR creation. Its spec also reused BEFORE's plan comment. Its projection matched too, which shows that the existing-PR path behaves the same on this build. It is not the AC-5 evidence. For the AFTER run above, I added "(run 2)" to the title and deleted the earlier comments, so it started from the same conditions as BEFORE.

What the runner traced in the diff. Git's moved-code detection, allowing indentation changes, finds 2,070 moved lines, 491 added lines and 207 removed lines that are not moves. The removed lines are locals that moved onto the IssueRunState/IssueRunEnv objects, plus closures that became functions. I checked these by hand against main:

  • finalizePhasePlan keeps main's order: resume filter, testgen, security-review, onPhasePlan.
  • In runPhaseStep, "break" happens exactly where the inline loop broke, including when a /loop pass succeeds.
  • TOP_OF_LADDER (dispatchLoopPhase returns null) matches main's halt.
  • The relay-deactivation warning text is unchanged.

I did not read recordSpecOutcome, recordLoopPhaseOutcome, runLoopFixPhase, settleIssueStatus or deliverIssueBranch line by line. The unchanged suite (154 tests in AC-3's two files) and the golden run cover them.

Fixes #1507

QA verdict: NEEDS_VERIFICATION — acceptance criteria met, pending external verification (e.g. CI or a manual test). See the QA review comment.

Follow-ups


🤖 Generated by sequant run

… most 200 counted lines

Same-file, module-private stages share an explicit IssueRunEnv/IssueRunState pair. Every former break or return is a returned signal the caller checks. No behaviour change.

Entire-Checkpoint: d5fbe297736e
…seline, ADR-0020, CHANGELOG

Entire-Checkpoint: 01d4af48ff35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(workflow): split runIssueWithLogging (1,620 lines) into stage functions of ≤ 200 counted lines, behaviour-preserving

1 participant