Terraphim Architecture Improvement Plan
Date: 2026-02-12
Related report: docs/architecture-review-report.md
Goal
Improve architectural reuse and reduce dependency surface while preserving existing functionality and release velocity.
Guiding Principles
- Default-minimal build first (opt-in heavy features).
- Stable reuse interfaces (traits + thin public APIs).
- Strict dependency direction (core -> domain -> app -> adapters).
- Incremental, reversible steps (small PRs, clear acceptance criteria).
Phase 0 β Baseline & Governance (1 week)
Deliverables
- ADR for architecture layering and dependency direction.
- ADR for workspace dependency policy.
- Dependency baseline snapshot (duplicate crates + compile timings for key binaries).
Actions
- Define allowed shared dependencies that must be
workspace = true. - Add CI guard: fail when core shared deps are version-pinned in member crates.
- Establish βminimal profileβ commands as quality gate.
Exit criteria
- Policy documented + enforced in CI.
- Baseline metrics stored in docs for before/after comparison.
Phase 1 β Boundary Extraction for Reuse (2β3 weeks)
Deliverables
- Extracted runtime bootstrap module/crate for rolegraph + indexing initialization.
- Shared router builder to remove duplicated route registrations.
Actions
- Extract server startup indexing logic from
terraphim_server/src/lib.rsinto a reusable initializer component. - Create a single route composition function used by both production and test router constructors.
- Keep
main.rsand server entrypoints as thin composition roots.
Exit criteria
- No duplicate route lists in server code.
- Startup/indexing orchestration reusable from tests and other adapters.
Phase 2 β Service Decomposition (3β4 weeks)
Deliverables
terraphim_servicesplit by capability modules with clear traits:- search service
- KG/thesaurus service
- document service
- LLM/chat service
- Reduced size/complexity of monolithic
lib.rs.
Actions
- Move long branching workflows (e.g., thesaurus load/build fallback chains) into strategy objects.
- Introduce typed service interfaces for easier integration testing and reuse.
- Keep crate facade stable; evolve internals behind module boundaries.
Exit criteria
lib.rsacts mostly as facade/wiring.- Each service capability has isolated tests and explicit dependencies.
Phase 3 β Dependency Surface Reduction (2β3 weeks)
Deliverables
- All common dependencies unified via workspace policy.
- Feature-gated heavy integrations isolated by default.
- Core-model dependency trim proposal implemented (as feasible).
Actions
- Normalize shared deps (
tokio,serde,serde_json,reqwest,chrono,uuid, etc.) to workspace definitions. - Audit and isolate heavy/optional paths (VM execution, multi-agent, schema generation, desktop-only behaviors).
- Review
terraphim_typesandterraphim_configfor feature/profile splits:types-core/types-schemastyle split (or equivalent feature split)- pure config-model vs runtime bootstrap separation
Exit criteria
- Minimal server/CLI build avoids linking optional heavy stacks.
- Duplicate dependency count decreased from baseline.
Phase 4 β CLI Product-Line Consolidation (2 weeks)
Deliverables
- ADR for
terraphim_agent/terraphim_cli/terraphim_replstrategy. - Shared command runtime crate used by all retained binaries.
Actions
- Decide between:
- single primary binary with feature modes, or
- multiple binaries sharing one command execution core.
- Remove duplicate runtime wiring and duplicated command logic.
Exit criteria
- Clear ownership and purpose per binary.
- Reduced maintenance and compile duplication.
Quality Gates per Phase
For each phase/PR:
cargo build --workspacecargo clippycargo test --workspace(or targeted crates when scoped)ubs <changed-files>- dependency diff evidence attached in PR notes
Risk Register
-
Risk: Refactoring disrupts existing API behavior.
Mitigation: preserve public facades; add compatibility tests before moving internals. -
Risk: Feature gating breaks downstream packaging.
Mitigation: validate minimal + full feature matrices in CI. -
Risk: Large PRs slow reviews.
Mitigation: enforce phase-based, small, single-purpose PRs.
Success Metrics
- Fewer duplicated crates in
cargo tree -dfor core targets. - Reduced compile times for default server and CLI builds.
- Reduced LOC in composition roots (
main.rs, serverlib.rs) and monolithic services. - Clearer module ownership documented via ADRs and crate READMEs.
Immediate Next Steps (this week)
- Approve ADR templates and create ADR-001/002 (layering + dependency policy).
- Implement CI guard for workspace dependency consistency.
- Open issue set for Phase 1 extraction work (router unification + bootstrap extraction).
- Track baseline metrics in
docs/for objective before/after comparison.