Skip to content

[MEDIUM] Refactor context.rs (173KB), lib.rs (75KB), documents.rs (53KB) into submodules #40

@matthewod11-stack

Description

@matthewod11-stack

Description

Three Rust files have grown past the point of effective code review. At these sizes, bugs hide in plain sight and the LLM-era grep-and-edit workflow degrades (navigating a 4,700-LOC file is slow for humans and for agents).

Current State

  • src-tauri/src/context.rs — 173 KB / 4,733 LOC — mixes query intent, employee retrieval, system prompt, org aggregates, verification, token budgeting.
  • src-tauri/src/lib.rs — 75 KB / 2,193 LOC — every #[tauri::command] registered in one file.
  • src-tauri/src/documents.rs — 53 KB / 1,534 LOC — discovery, parsing, chunking, FTS, watcher, commands.

Suggested Fix

context.rs → context/

lib.rs → commands/

  • commands/api_key.rs, commands/providers.rs, commands/license.rs, commands/chat.rs, commands/employees.rs, commands/documents.rs, commands/company.rs, commands/settings.rs, etc.
  • Each exports a generate_handler!-compatible bundle; lib.rs composes them.

documents.rs → documents/

  • documents/discovery.rs, documents/chunker.rs, documents/ingest.rs, documents/watcher.rs, documents/commands.rs.

Do NOT change behavior — pure split. Do each file in its own PR.

Verification

  • cargo test --manifest-path src-tauri/Cargo.toml passes with zero diff in passing tests.
  • cargo check clean.
  • git diff --stat shows the refactor is nearly 1:1 (line counts preserved, structure only moved).

Automation Hints

scope: src-tauri/src/context.rs (or documents.rs, or lib.rs) → split per PR
do-not-touch: business logic
approach: extract-and-move
risk: medium-high (large diff, many imports to update)
max-files-changed: 15 per PR
blocked-by: none
bail-if: public API changes or tests break

Priority

Medium — compounding maintenance debt; do in patches over next 2-3 weeks.

Notes

needs-design-decision candidate — confirm the split before starting each one. Do lib.rs first (lowest-risk; most files but least coupled logic), context.rs last (most integration tests to verify).

Metadata

Metadata

Assignees

No one assigned

    Labels

    hardeningReliability or defense-in-depth improvementtech-debtEligible for automated overnight fixing

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions