Skip to content

refactor: decouple main process modules via interfaces and constructor injection#1402

Merged
IceyLiu merged 33 commits intomainfrom
zynx/refactor/main-process-decouple
Mar 18, 2026
Merged

refactor: decouple main process modules via interfaces and constructor injection#1402
IceyLiu merged 33 commits intomainfrom
zynx/refactor/main-process-decouple

Conversation

@piorpua
Copy link
Copy Markdown
Contributor

@piorpua piorpua commented Mar 18, 2026

Summary

  • Define TypeScript interfaces for each main-process layer: IConversationRepository, IConversationService, IAgentManager, IAgentFactory, IWorkerTaskManager, IAgentEventEmitter, WorkerProtocol
  • Implement concrete classes with constructor injection: SqliteConversationRepository, ConversationServiceImpl, IpcAgentEventEmitter, AgentFactory, WorkerTaskManager
  • Replace legacy WorkerManage (switch-based dispatch) with WorkerTaskManager + AgentFactory
  • Inject IAgentEventEmitter into BaseAgentManager; remove direct ipcBridge calls from agent layer
  • Clean up conversationBridge: remove task.type guards from stop and sendMessage handlers, unify dispatch via IAgentManager interface
  • Annotate ForkTask.postMessage with WorkerProtocol types

Architecture

Each layer now depends only on an interface, not a concrete class. Dependencies are passed via constructor injection (no DI framework).

conversationBridge (thin router)
  └── IWorkerTaskManager  ←  WorkerTaskManager
        └── IAgentFactory  ←  AgentFactory
              └── IAgentManager  ←  BaseAgentManager / concrete managers
                    └── IAgentEventEmitter  ←  IpcAgentEventEmitter

conversationBridge
  └── IConversationService  ←  ConversationServiceImpl
        └── IConversationRepository  ←  SqliteConversationRepository

Test Plan

  • bun run test passes (691 tests)
  • bunx tsc --noEmit passes
  • bun run lint:fix produces no errors on changed files
  • ConversationServiceImpl can be unit-tested by mocking IConversationRepository
  • WorkerTaskManager can be unit-tested by mocking IAgentFactory
  • BaseAgentManager can be unit-tested by mocking IAgentEventEmitter
  • No direct getDatabase() calls remain outside SqliteConversationRepository
  • No direct ipcBridge calls remain inside BaseAgentManager
  • grep -rn "WorkerManage" src/ returns no results

zynx added 28 commits March 18, 2026 12:00
- Remove legacy fallback paths for create, createWithConversation,
  remove, and update handlers; conversationService is always provided
- Route all WorkerManage.* calls through workerTaskManager (getTask,
  getOrBuildTask, kill, clear)
- Route getDatabase().getConversation() calls through
  conversationService.getConversation() in get, getRuntime, remove,
  update, getSlashCommands, and getAssociateConversation handlers
- Remove unused imports: WorkerManage, ConversationService, cronService,
  uuid
- Make initConversationBridge parameters non-optional
- Add unknown intermediate cast for IAgentManager -> concrete manager
  type narrowing (required by TypeScript strict mode)

Note: getAssociateConversation retains one direct getDatabase() call for
getUserConversations() pagination query; IConversationService does not
yet expose a list/query method. Flagged for future migration.
zynx added 4 commits March 18, 2026 15:43
…e diagram

- Add phase2-design.md: 6-PR plan covering WorkerTaskManager, bridge
  layer injection, CronService decoupling, channel repository, database/
  extensions bridge refactor, and cleanup
- Add phase2-decouple-design.html: visual dependency-injection diagram
  showing all bridge, service, and agent layers after Phase 2 completion
@IceyLiu IceyLiu merged commit d7bc128 into main Mar 18, 2026
15 of 16 checks passed
@piorpua piorpua deleted the zynx/refactor/main-process-decouple branch March 18, 2026 11:15
piorpua pushed a commit that referenced this pull request Mar 18, 2026
- Mark PR #1402 as merged in header metadata
- Update branch name to phase2-specific branch
- Add two missing coupling entries: applicationBridge and conversationBridge line 125
- PR-A: add listAllConversations to IConversationRepository interface; fix conversationBridge line 125
- PR-B: add applicationBridge.ts decoupling with same injection pattern
- PR-F: document channels layer migration targets (SystemActions, ActionExecutor)
- Testing: add Test Quality Rules section with behavior-first, risk-first guidelines
- Testing: annotate all failure paths with [failure path], reorder CronService tests by risk
- Testing: add conversationBridge and applicationBridge to coverage targets
kaizhou-lab pushed a commit that referenced this pull request Mar 18, 2026
- Mark PR #1402 as merged in header metadata
- Update branch name to phase2-specific branch
- Add two missing coupling entries: applicationBridge and conversationBridge line 125
- PR-A: add listAllConversations to IConversationRepository interface; fix conversationBridge line 125
- PR-B: add applicationBridge.ts decoupling with same injection pattern
- PR-F: document channels layer migration targets (SystemActions, ActionExecutor)
- Testing: add Test Quality Rules section with behavior-first, risk-first guidelines
- Testing: annotate all failure paths with [failure path], reorder CronService tests by risk
- Testing: add conversationBridge and applicationBridge to coverage targets
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.

4 participants