Skip to content

refactor(epic-1974): improve type safety - CompactionState, BoxFuture, schema helper#2014

Merged
bug-ops merged 1 commit intomainfrom
issue-1974-type-safety
Mar 19, 2026
Merged

refactor(epic-1974): improve type safety - CompactionState, BoxFuture, schema helper#2014
bug-ops merged 1 commit intomainfrom
issue-1974-type-safety

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

@bug-ops bug-ops commented Mar 19, 2026

Summary

Implement type safety improvements from epic #1974 audit. Three changes improve maintainability and leverage Rust's type system:

  • TYPE-02: Consolidate BoxFuture from 3 definitions → 1 shared pub(crate) alias (-25 LOC)
  • TYPE-06: Replace 4 boolean fields with CompactionState enum (invalid states unrepresentable by type)
  • TYPE-03: Extract schema helper for 3 LLM providers (DRY, -1 LOC)

Validation

  • ✅ 5427/5427 tests passing (+15 new state transition tests)
  • ✅ Clippy clean (0 warnings)
  • ✅ Format compliant
  • ✅ Security audit: APPROVED (no unsafe code, no security issues)
  • ✅ Performance: NEUTRAL (enum smaller than original booleans)

Key Changes

TYPE-02: BoxFuture Consolidation

  • vector_store.rs: Define pub(crate) type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>
  • sqlite_vector_store.rs: Remove duplicate, import from vector_store.rs
  • qdrant_ops.rs: Replace 8 inline Pin<Box<dyn Future<...>>> with alias

TYPE-06: CompactionState Enum

  • Replace 4 fields: compacted_this_turn, compaction_exhausted, exhaustion_warned, compaction_turns_since
  • 4 variants: Ready, CompactedThisTurn { cooldown }, Cooling { turns_remaining }, Exhausted { warned }
  • All 12 mutation sites converted with explicit transition logic
  • 14 new unit tests for state transitions
  • Addresses critic findings:
    • S1: advance_turn() ordering documented and tested
    • S2: Transition map confirms Cooling → Exhausted impossible
    • M5: Proactive compression included in state map

TYPE-03: Schema Helper

  • Extract short_type_name<T>() in provider.rs
  • Replace 3 duplicate patterns in openai/mod.rs and claude/mod.rs
  • 3 unit tests for schema generation

Files Changed

  • crates/zeph-core/src/agent/context/summarization.rs
  • crates/zeph-core/src/agent/context/tests.rs
  • crates/zeph-core/src/agent/context_manager.rs
  • crates/zeph-core/src/agent/mod.rs
  • crates/zeph-core/src/agent/tool_execution/native.rs
  • crates/zeph-llm/src/claude/mod.rs
  • crates/zeph-llm/src/openai/mod.rs
  • crates/zeph-llm/src/provider.rs
  • crates/zeph-memory/src/qdrant_ops.rs
  • crates/zeph-memory/src/sqlite_vector_store.rs
  • crates/zeph-memory/src/vector_store.rs

Related Issues

Checklist

  • All tests passing (5427/5427)
  • Clippy clean
  • Format compliant
  • Security audit APPROVED
  • Compiler warnings: 0
  • Unsafe code: 0 new blocks
  • Breaking changes: none
  • Documentation: state transitions documented in code

…oxFuture consolidation, and schema helper

- TYPE-02: Consolidate BoxFuture type alias from 3 files into single pub(crate) definition in vector_store.rs
  - Remove duplicate from sqlite_vector_store.rs
  - Replace 8 inline Pin<Box<dyn Future>> in qdrant_ops.rs with alias
  - Net: -25 LOC

- TYPE-06: Replace 4 boolean/u8 fields with CompactionState enum in ContextManager
  - Enum variants: Ready, CompactedThisTurn{cooldown}, Cooling{turns_remaining}, Exhausted{warned}
  - Makes invalid state combinations unrepresentable by type system
  - All 12 mutation sites converted with explicit transition rules
  - advance_turn() ordering documented and tested (S1 from critic)
  - Transition map confirms Cooling→Exhausted impossible (S2 from critic)
  - Proactive compression included in map (M5 from critic)
  - 14 new unit tests for state transitions

- TYPE-03: Extract schema_type_name helper in LlmProvider
  - Consolidate 3 duplicate type_name() patterns across providers
  - New unit tests for schema generation

Test results:
- 5427/5427 tests passing (+15 new tests for state transitions)
- Clippy: clean
- Format: compliant
- Security audit: APPROVED (no unsafe code, no security issues)
- Performance: NEUTRAL (enum smaller than original booleans)

Closes #1974 (partial - TYPE-02, TYPE-03, TYPE-06 complete; TYPE-01 deferred)
@github-actions github-actions bot added llm zeph-llm crate (Ollama, Claude) memory zeph-memory crate (SQLite) rust Rust code changes core zeph-core crate refactor Code refactoring without functional changes size/XL Extra large PR (500+ lines) labels Mar 19, 2026
@bug-ops bug-ops enabled auto-merge (squash) March 19, 2026 13:23
@bug-ops bug-ops merged commit 4da43c6 into main Mar 19, 2026
22 checks passed
@bug-ops bug-ops deleted the issue-1974-type-safety branch March 19, 2026 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core zeph-core crate llm zeph-llm crate (Ollama, Claude) memory zeph-memory crate (SQLite) refactor Code refactoring without functional changes rust Rust code changes size/XL Extra large PR (500+ lines)

Projects

None yet

1 participant