Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Refactoring Plan

Structural refactoring for design pattern alignment, extensibility, and scalability. The full checklist lives in docs/plans/comprehensive-refactoring-plan.md.

Strategic themes

  1. Type safety over stringly-typed – replace magic strings (event kinds, job kinds, tool names) with compile-time-safe constants and validated registries
  2. GoF patterns as Rust idioms – Builder (typestate), Strategy (generics), Observer (typed channels), Decorator (tower layers), Command (typed enums), Visitor (AST traversal)
  3. Scalability substrate – saga orchestration, association graphs, self-improvement loops, Tower Service composability

What is NOT changing: hexagonal port/adapter boundary, DepartmentApp trait, composition root, single-binary constraint, SQLite WAL, or any ADR decisions.

Current patterns (verified)

PatternWhereQuality
StrategyPort traits (LlmPort, AgentPort, etc.)Excellent
Abstract FactoryComposition root (rusvel-app)Excellent
FacadeAgentRuntime over LLM+Tool+MemoryExcellent
AdapterEvery rusvel-* crateExcellent
ObserverAgentEvent over mpsc, EventPort broadcastGood
CommandJobKind dispatch, ToolHandler closuresGood
BuilderDepartmentManifest::new()Partial
ProxyScopedToolRegistryGood

Gaps to address

PatternGapSprint
Typestate BuilderNo compile-time field enforcement2
Decorator (Tower)No composable middleware on ports3
Visitorcode-engine has no visitor trait4
Typed ObserverEvent subscriptions use string matching4
State (Typestate)Job/flow status transitions unchecked4
CompositeFlow nodes are flat (no nesting)8
MediatorNo cross-department agent delegation9
SagaNo compensation logic in multi-step flows6

Sprint overview

SprintThemeKey Deliverables
1Type safetyEvent kind constants, tool collision detection, job registry
2Builders & factoriesTypestate DepartmentManifest, PlatformFactory trait, TestFactory
3StructuralAppState decomposition (25 fields -> 5 sub-states), Tower layers on LlmPort
4BehavioralTypedEvent system, JobCommand trait, SymbolVisitor, DelegateAgentTool
5DDDAggregateRoot trait, ObjectStore associations, value object enforcement
6EventsSaga compensation, lightweight projections, event replay
7PluginsManifest validation, port requirement checks, capability tokens
8WorkflowsCheckpoint/resume, SubFlow/Parallel/Loop nodes, per-node retry
9AgentsHierarchical delegation, supervisor pattern, blackboard cross-engine
10ScalabilityMagic number extraction, graceful shutdown, data-driven CLI
11FrontendTyped API client, command/query stores, error boundaries
12Self-improvementFailure reflection loop, skill accumulation, experience replay

Structural issues being fixed

IssueSeveritySprint
Monolithic AppState (~25 fields)High3
Tool name collisions silently overwriteHigh1
Event kinds are string literalsMedium1
Job results stored in untyped metadataMedium4
No association graph between domain objectsMedium5
Magic numbers scattered in codeLow10
Job worker has no graceful shutdownMedium10
CLI department variants repeated 9xLow10