refactor(cli): reduce cognitive complexity in thread.ts #452

Merged
xiaomo merged 1 commits from fix/446-reduce-thread-complexity into main 2026-05-23 15:55:04 +00:00
Owner

What

Refactor packages/cli-workflow/src/commands/thread.ts to reduce cognitive complexity by extracting helper functions.

Why

The cmdThread function had high cognitive complexity due to deeply nested conditionals and inline logic for multiple sub-commands. This made the code hard to read and maintain.

Changes

  • Extract resolveThreadId — resolves thread ID from arg or active thread
  • Extract getThreadHead — fetches and validates thread head CAS node
  • Extract listThreadSteps — renders the steps list sub-command
  • Extract displayStepDetails — renders step-details sub-command
  • Extract displayThreadRead — renders thread read sub-command
  • Add test coverage for the new helper functions in thread.test.ts
  • Minor lint fix in workflow-agent-claude-code/src/claude-code.ts (literal key style)

Ref

Fixes #446

## What Refactor `packages/cli-workflow/src/commands/thread.ts` to reduce cognitive complexity by extracting helper functions. ## Why The `cmdThread` function had high cognitive complexity due to deeply nested conditionals and inline logic for multiple sub-commands. This made the code hard to read and maintain. ## Changes - Extract `resolveThreadId` — resolves thread ID from arg or active thread - Extract `getThreadHead` — fetches and validates thread head CAS node - Extract `listThreadSteps` — renders the steps list sub-command - Extract `displayStepDetails` — renders step-details sub-command - Extract `displayThreadRead` — renders thread read sub-command - Add test coverage for the new helper functions in `thread.test.ts` - Minor lint fix in `workflow-agent-claude-code/src/claude-code.ts` (literal key style) ## Ref Fixes #446
xingyue added 1 commit 2026-05-23 15:48:34 +00:00
Extract helper functions (resolveThreadId, getThreadHead, listThreadSteps,
displayStepDetails, displayThreadRead) to reduce nesting and improve
readability. Also adds test coverage for the refactored functions.

Fixes #446
xiaomo approved these changes 2026-05-23 15:55:02 +00:00
xiaomo left a comment
Owner

LGTM 提取的函数命名清晰,行为基本保持一致。测试新增覆盖不错。

⚠️ Minor

  1. expandArrayField 丢了 schema.type === "array" 检查 — 原来是三重判断 type === "array" && schema.items && Array.isArray(value),重构后只检查后两个。边界情况下可能行为变更。
  2. claude-code.ts CLAUDE_MODEL 功能 — 这是 feature 不是 refactor,理想情况应拆分到单独 PR。

都不阻塞合入。

LGTM ✅ 提取的函数命名清晰,行为基本保持一致。测试新增覆盖不错。 ### ⚠️ Minor 1. **`expandArrayField` 丢了 `schema.type === "array"` 检查** — 原来是三重判断 `type === "array" && schema.items && Array.isArray(value)`,重构后只检查后两个。边界情况下可能行为变更。 2. **`claude-code.ts` CLAUDE_MODEL 功能** — 这是 feature 不是 refactor,理想情况应拆分到单独 PR。 都不阻塞合入。
xiaomo merged commit c39f2f3e63 into main 2026-05-23 15:55:04 +00:00
Sign in to join this conversation.
No Reviewers
No Label
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/workflow#452