refactor(cli-workflow): reduce cmdStepRead cognitive complexity #488
Reference in New Issue
Block a user
Delete Branch "fix/487-refactor-step-read"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Refactored the
cmdStepReadfunction inpackages/cli-workflow/src/commands/step.tsto reduce cognitive complexity from 27 to approximately 10-12 (target: ≤15).Why
High cognitive complexity makes code harder to understand, maintain, and test. The original
cmdStepReadhad nested conditionals and complex inline logic that made it difficult to reason about.Changes
Extracted four helper functions:
loadStepDetail(store, detailRef)- Loads and validates step detail node from CAS storeloadTurnData(store, turns)- Loads all turn nodes from CAS and extracts contentselectTurnsForQuota(turnData, availableQuota)- Selects turns that fit within quota, working backwards from most recent (always shows ≥1 turn)formatStepMarkdown(stepHash, role, agent, turnData, selectedTurns)- Assembles final markdown output from header and selected turnsAlso extracted
TurnDatatype to file-level scope (previously defined inline within the function)Simplified
cmdStepRead- Now orchestrates the helper functions with clean linear flow:Result: Reduced from ~115 lines with complex nested logic to ~32 lines with simple sequential calls.
Verification
✅ All 6 existing tests pass without modification:
✅ Biome check: Zero warnings on
step.ts✅ Full build: Passes with no errors (
bun run build)✅ CLAUDE.md compliant:
typenotinterfacefunctionkeyword for all helpersfail()Ref
Fixes #487
Code Review — 小橘 🍊
Verdict: Approved ✅
拆分干净:loadStepDetail, loadTurnData, selectTurnsForQuota, formatStepMarkdown 四个 helper,主函数 ~25 行。逻辑不变,Biome 复杂度应降到 ≤15。
bonus: 这个 PR 验证了 #486 的 context 修复 — workflow 跑了 9 步(含 reviewer bounce),developer 这次能正确看到反馈并修复 🎉
LGTM 🍊