Skip to content

refactor: extract magic strings, decompose long functions, standardize error display#628

Merged
Wirasm merged 3 commits into
mainfrom
kild/refactor-cleanup
Mar 17, 2026
Merged

refactor: extract magic strings, decompose long functions, standardize error display#628
Wirasm merged 3 commits into
mainfrom
kild/refactor-cleanup

Conversation

@Wirasm
Copy link
Copy Markdown
Owner

@Wirasm Wirasm commented Mar 4, 2026

Summary

  • Extract magic strings into constants: WORKTREE_ADMIN_PREFIX, SHIM_VERSION, and use kild_branch_name() / KILD_BRANCH_PREFIX consistently instead of raw format!("kild/...") literals
  • Decompose 3 long functions: kill_tracked_agents() and sweep_ui_daemon_sessions() from destroy_session(), resolve_resume_args() from open_session()
  • Standardize CLI error display with display_operation_error() helper and consistent color::error() usage across 9 command handlers

Test plan

  • cargo fmt --check passes
  • cargo clippy --all -- -D warnings passes
  • cargo test --all passes (3 pre-existing env-dependent test failures unrelated to this change)
  • No behavioral changes — pure refactor

Closes #438

Wirasm added 2 commits March 4, 2026 17:26
…e error display

- Add WORKTREE_ADMIN_PREFIX constant to naming.rs alongside KILD_BRANCH_PREFIX
- Use kild_branch_name() instead of raw format!("kild/{}") in pr.rs, detail_view.rs
- Use KILD_BRANCH_PREFIX in kild_branch_name() function body
- Add SHIM_VERSION constant for tmux version string in shim commands

- Extract kill_tracked_agents() from destroy_session() (127 lines → helper)
- Extract sweep_ui_daemon_sessions() from destroy_session() (55 lines → helper)
- Extract resolve_resume_args() from open_session() (46 lines → helper)

- Add display_operation_error() helper for consistent CLI error formatting
- Standardize error display across open, hide, focus, diff, health, stats,
  sync, commits, and teammates commands to use color::error() consistently

Closes #438
…t helpers

- Restore non-fatal comment on daemon cleanup in kill_tracked_agents()
- Change WORKTREE_ADMIN_PREFIX to pub(crate) — no external callers
- Replace raw format!("kild/...") in cleanup/handler.rs and overlaps.rs
  test helpers with kild_branch_name() / kild_worktree_admin_name()
@Wirasm
Copy link
Copy Markdown
Owner Author

Wirasm commented Mar 17, 2026

PR Review Summary

Reviewed by: code-reviewer, docs-impact-agent, comment-analyzer, code-simplifier

Critical Issues (0 found)

None. Code review confirms the refactoring is behavior-preserving across all 18 changed files.

Important Issues (3 found)

Agent Issue Location
comment-analyzer kill_tracked_agents docstring implies force controls daemon error suppression, but daemon errors are always non-fatal regardless of force crates/kild-core/src/sessions/destroy.rs:37-40
comment-analyzer resolve_resume_args docstring omits is_bare_shell parameter entirely and hides both error return paths (ResumeUnsupported, ResumeNoSessionId) — describes 2 branches when there are 3 crates/kild-core/src/sessions/open.rs:39-46
docs-impact kild-git crate is absent from CLAUDE.md workspace structure list despite being a real workspace member used by kild-core, kild-ui, and kild-tmux-shim. PR #628 adds WORKTREE_ADMIN_PREFIX there, making the gap more conspicuous CLAUDE.md workspace structure section

Suggestions (5 found)

Agent Suggestion Location
simplifier .unwrap() in kill_tracked_agents on kill_errors.first() — logically safe but violates the "no .unwrap() in production" rule. Use &kill_errors[0] or let-else crates/kild-core/src/sessions/destroy.rs:142
simplifier display_operation_error takes &dyn std::fmt::Display — prefer impl std::fmt::Display for monomorphization (all callers pass concrete types) crates/kild/src/commands/helpers.rs:19
simplifier WORKTREE_ADMIN_PREFIX is pub(crate) with a single use site inside kild_worktree_admin_name() — adds indirection without external callers to justify it. Consider inlining crates/kild-git/src/naming.rs:18
simplifier pr.rs calls kild_branch_name(branch) in no_pr_found path when kild_branch variable is already in scope — reuse the variable crates/kild/src/commands/pr.rs
simplifier teammates.rs uses crate::color::error when color is already imported — use color::error crates/kild/src/commands/teammates.rs:51

Strengths

  • Function decomposition is clean — kill_tracked_agents, sweep_ui_daemon_sessions, resolve_resume_args have good names and well-scoped responsibilities
  • display_operation_error helper standardizes CLI error output with a clear format contract
  • Magic string extraction to kild_branch_name()/kild_worktree_admin_name() eliminates scattered format!("kild/...") calls
  • sweep_ui_daemon_sessions docstring precisely documents the {kild_id}_ui_shell_{counter} naming convention — excellent load-bearing context

Documentation Issues

  • CLAUDE.md workspace structure: add kild-git entry
  • CLAUDE.md git/ module description: note that naming utilities now live in separate kild-git crate

Verdict

READY TO MERGE — no behavioral issues. The important issues are docstring accuracy improvements and a CLAUDE.md gap that should be addressed (in this PR or a follow-up).

- Fix kill_tracked_agents docstring: clarify daemon errors are always
  non-fatal, not gated on force flag
- Fix resolve_resume_args docstring: document is_bare_shell parameter
  and error return paths
- Remove .unwrap() in kill_tracked_agents, use indexing instead
- Change display_operation_error to use impl Display over &dyn Display
- Inline WORKTREE_ADMIN_PREFIX constant (single use site)
- Reuse kild_branch variable in pr.rs no_pr_found path
- Use imported color module instead of crate::color in teammates.rs
- Add kild-git crate to CLAUDE.md workspace structure
@Wirasm Wirasm merged commit 8aa9c5c into main Mar 17, 2026
6 checks passed
@Wirasm Wirasm deleted the kild/refactor-cleanup branch March 17, 2026 13:20
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.

General refactoring: magic strings, long functions, inconsistent error display

1 participant