Skip to content

refactor: apply OOP design principles to core design#8

Merged
shenxiangzhuang merged 2 commits intomasterfrom
copilot/refactor-design-principles
Mar 10, 2026
Merged

refactor: apply OOP design principles to core design#8
shenxiangzhuang merged 2 commits intomasterfrom
copilot/refactor-design-principles

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

Applies SRP, DRY, and Delegation principles to reduce cognitive load in three high-traffic areas: Castle.create(), Session, and agent_loop().

Changes

Castle.create() — SRP decomposition (castle.py)

A 70-line monolithic factory handling dirs, skill discovery, provider setup, channels, and session/model wiring. Extracted three focused private helpers:

cls._ensure_dirs(config)
skill_manager = cls._build_skill_manager(config)
channels = cls._build_channels(config, session_id=..., continue_latest=..., daemon=...)

create() is now a readable orchestrator; each helper carries a single responsibility.

Session.create() / Session.resume() — DRY fix (session/session.py)

Both classmethods ended with identical code: calling agent_factory(trace) and constructing Session. Extracted into _assemble():

return cls._assemble(session_dir=session_dir, meta=meta,
                     trace=trace, trace_manager=trace_manager,
                     agent_factory=agent_factory)

agent_loop() — trace recording extraction (kagent/loop.py)

The TurnEnd handler mixed hook dispatch, trace appending, and usage accumulation. Extracted _record_turn_end(state, event, *, run_id, turn_index) -> TokenUsage | None to own the trace concern, keeping the loop focused on orchestration.


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

- SRP: decompose Castle.create() into _ensure_dirs(), _build_skill_manager(),
  and _build_channels() private helpers — each with a single responsibility
- DRY + Delegation: extract Session._assemble() classmethod shared by
  Session.create() and Session.resume(), eliminating duplicated setup code
- SRP + Delegation: extract _record_turn_end() helper from agent_loop() so
  trace recording is isolated from hook dispatch and loop orchestration

Co-authored-by: shenxiangzhuang <17157965+shenxiangzhuang@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor code to align with design principles refactor: apply OOP design principles to core design Mar 10, 2026
@shenxiangzhuang shenxiangzhuang marked this pull request as ready for review March 10, 2026 07:47
@shenxiangzhuang shenxiangzhuang requested a review from Copilot March 10, 2026 07:47
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors three frequently used orchestration paths to better separate responsibilities and remove duplicated assembly logic, improving readability without changing expected behavior.

Changes:

  • Refactors Castle.create() into a small orchestrator by extracting directory creation, skill manager construction, and channel construction helpers.
  • Removes duplicated Session.create() / Session.resume() wiring by extracting shared construction into Session._assemble().
  • Extracts trace-recording from agent_loop() into _record_turn_end() to keep the main loop focused on control flow and hooks.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/kcastle/src/kcastle/session/session.py Introduces Session._assemble() to DRY up session construction in create() and resume().
packages/kcastle/src/kcastle/castle.py Decomposes Castle.create() into _ensure_dirs, _build_skill_manager, and _build_channels helpers.
packages/kagent/src/kagent/loop.py Extracts per-turn trace appends and usage extraction into _record_turn_end() and simplifies usage accumulation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@shenxiangzhuang shenxiangzhuang merged commit d606d99 into master Mar 10, 2026
14 checks passed
@shenxiangzhuang shenxiangzhuang deleted the copilot/refactor-design-principles branch March 10, 2026 07:51
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.

3 participants