Skip to content

refactor(context): complete zeph-agent-context migration (assembly + summarization)#3525

Merged
bug-ops merged 11 commits intomainfrom
3523-migrate-agent-context
Apr 27, 2026
Merged

refactor(context): complete zeph-agent-context migration (assembly + summarization)#3525
bug-ops merged 11 commits intomainfrom
3523-migrate-agent-context

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

@bug-ops bug-ops commented Apr 27, 2026

Summary

Completes the migration of context-assembly and summarization logic from zeph-core into zeph-agent-context, continuing from #3522 which scaffolded the crate and moved assembler_helpers.rs.

  • Relocate CompactionOutcome and BudgetHint from zeph-core to zeph-agent-context
  • Expand MessageWindowView and ContextSummarizationView from placeholders to real borrow-lens fields
  • Implement all ContextService stub methods: prepare_context, inject_semantic_recall, inject_cross_session_context, inject_summaries, trim_messages_to_budget, 13 remove_* mutators, disambiguate_skills, maybe_compact, maybe_summarize_tool_pair, apply_deferred_summaries, flush_deferred_summaries, maybe_soft_compact_mid_iteration, maybe_proactive_compress, maybe_refresh_task_goal, maybe_refresh_subgoal, reset_conversation
  • Create zeph-agent-context::summarization sub-module with compaction, deferred, pruning, scheduling
  • Replace Agent<C> method bodies in assembly.rs, scheduling.rs, deferred.rs with thin delegation to ContextService
  • Isolation invariant: cargo tree -p zeph-agent-context | grep zeph-core produces no output

Known scope exclusion: Agent<C>::compact_context (called from native.rs and agent_access_impl.rs) retains its original body — full delegation requires extending ContextSummarizationView with probe validation, compression guidelines, and Qdrant session-summary fields. Documented with TODO(review) in compaction.rs.

Test plan

  • cargo clippy --workspace --features "desktop,ide,server,chat,pdf,scheduler" -- -D warnings — clean
  • cargo +nightly fmt --check — clean
  • RUSTDOCFLAGS="--deny rustdoc::broken_intra_doc_links" cargo doc --no-deps -p zeph-agent-context --all-features — clean
  • cargo nextest run --workspace --lib --bins — 8653 passed (baseline at merge point: 8644)
  • cargo tree -p zeph-agent-context | grep zeph-core — empty

Closes #3517 (phase 2 follow-up work)

bug-ops added 10 commits April 27, 2026 21:16
…agent-context

Move CompactionOutcome from zeph-core::agent::context (private) to
zeph-agent-context::state (pub). Move BudgetHint from assembly.rs (private)
to zeph-agent-context::helpers (pub) with full doc and tests.

zeph-core now imports both types from zeph-agent-context — no local copies remain.
Isolation invariant holds: `cargo tree -p zeph-agent-context | grep zeph-core` is empty.
…ors to ContextService

Expand MessageWindowView with cached_prompt_tokens, token_counter, and
completed_tool_ids fields (PR0b view expansion).

Migrate 15 message-window mutator methods from Agent<C> assembly.rs to
ContextService: clear_history, 13 remove_* methods, and trim_messages_to_budget.
Each Agent<C> shim creates a ZST ContextService instance and delegates via
message_window_view(). All original logic deleted from zeph-core.

Move message prefix constants (CORRECTIONS_PREFIX, CODE_CONTEXT_PREFIX,
SESSION_DIGEST_PREFIX, LSP_NOTE_PREFIX, DOCUMENT_RAG_PREFIX) to
zeph-agent-context::helpers, updating all callers including test modules.

1345 tests pass. Isolation: zeph-agent-context has no dependency on zeph-core.
…gs from context migration

Seven compilation errors in zeph-core caused by partially-migrated prepare_context:
- Fix unresolved imports in retrieved.rs (prefix consts from zeph-agent-context::helpers)
- Fix SECURITY_EVENT_CAP path (crate::metrics, not crate::agent::utils)
- Fix sanitizer borrow (&ContentSanitizer, not Arc<...>)
- Fix quarantine_summarizer: .as_ref() instead of .clone() (no Clone impl)
- Fix channel_skills: .allowed field (&[String], not ChannelSkillsConfig)
- Remove ref binding in Edition 2024 spawn_outgoing_digest pattern
- Fix borrow conflict in prepare_context: snapshot cached_prompt_tokens and call
  self.providers() before constructing view struct literals with mutable borrows

Twenty clippy warnings resolved:
- zeph-agent-context: needless_borrow, clone_on_copy, doc_markdown (x4), ref pattern
- zeph-core: unused imports, unexpected cfg (index feature absent in zeph-core),
  dead_code (14 test-only shims moved to #[cfg(test)] impl block), TxStatus removed,
  as_index_access removed, SESSION_DIGEST_PREFIX unused import, too_many_lines allow,
  items_after_statements (SecuritySink moved to module scope)

Isolation invariant verified: cargo tree -p zeph-agent-context | grep zeph-core is empty.
All 8998 unit tests pass.

Closes #3523 (tasks #7 and #8)
…tion types (PR0b)

- Replace PhantomData placeholder in ContextSummarizationView with 20 real
  borrow-lens fields covering the message window, context manager, runtime
  handles, memory persistence, and compaction state
- Move SubgoalRegistry, SubgoalExtractionResult, BlockScore, ContentDensity,
  SubgoalId, SubgoalState, and all scoring/compaction functions from
  zeph-core::agent::compaction_strategy to zeph-agent-context::compaction
- Delete zeph-core/src/agent/compaction_strategy.rs; all callers updated to
  import from zeph_agent_context
- Update CompressionState in zeph-core::agent::state to reference
  zeph_agent_context types
- Migrate run_focus_auto_consolidation async tests to zeph-agent-context
- Invariant holds: zeph-agent-context has no dependency on zeph-core
- Add `summarization_view()` method on `Agent<C>` in `assembly.rs` that
  constructs a `ContextSummarizationView` borrow-lens from disjoint field
  projections: message window, context manager, runtime handles, memory
  persistence, and all compression-state fields
- Expose `build_summarization_deps` as `pub(super)` so `assembly.rs` (in
  the same `context` module) can call it when building the view
- The shim is the integration point for subsequent migration PRs (PR4
  deferred summaries, PR7 proactive compression, PR8 compaction); suppressed
  with `#[allow(dead_code)]` until the first call site lands in PR4
Move all summarization logic from zeph-core to zeph-agent-context/src/summarization/:
- compaction.rs: LLM-based compact_context engine (partition → summarize → drain → reinsert)
- deferred.rs: tool-pair deferred summarization (count, find, apply, flush, maybe_apply)
- pruning.rs: five-strategy tool-output eviction (Reactive, TaskAware, MIG, Subgoal, SubgoalMIG)
- scheduling.rs: Soft/Hard/mid-iteration compaction dispatch + background goal/subgoal extraction

Implement all service.rs stubs: maybe_compact, maybe_summarize_tool_pair,
apply_deferred_summaries, flush_deferred_summaries, maybe_apply_deferred_summaries,
maybe_soft_compact_mid_iteration, maybe_proactive_compress, maybe_refresh_task_goal,
maybe_refresh_subgoal, reset_conversation.

Isolation invariant holds: cargo tree -p zeph-agent-context | grep zeph-core produces no output.
44 unit tests pass, 8658 workspace tests pass.
…ervice

Replace old Agent<C> summarization implementations in zeph-core/summarization/
with thin delegation shims to ContextService:

- scheduling.rs: replace maybe_compact (~400 LOC), do_soft_compaction, do_hard_compaction,
  maybe_proactive_compress, maybe_soft_compact_mid_iteration with service delegations.
  Add CollectStatusSink to bridge service status emissions to channel.send_status.
  Track context_compactions and compaction_hard_count metrics post-delegation since
  MetricsCallback is not available in ContextSummarizationView.

- deferred.rs: replace maybe_summarize_tool_pair, apply_deferred_summaries,
  flush_deferred_summaries, maybe_apply_deferred_summaries with service delegations.
  Move test-only helpers (count_unsummarized_pairs, find_oldest_unsummarized_pair,
  count_deferred_summaries) to cfg(test) impl block.

- pruning.rs: mark all Agent<C> pruning methods as test-only (dead_code allowed).
  Production pruning now goes through ContextService via ContextSummarizationView.

- compaction.rs: add test-only compact_context_with_budget delegation shim for
  integration tests exercising orphan-pair detection and tool-pair boundary adjustment.

Isolation invariant holds: cargo tree -p zeph-agent-context | grep zeph-core is empty.
8653/8653 workspace tests pass.
…t compact_context blocker

pruning.rs: remove #[allow(dead_code)] from the impl block that mixed live and dead
methods. Move the test-only Agent<C> pruning methods (prune_tool_outputs,
prune_tool_outputs_oldest_first, prune_tool_outputs_scored, prune_tool_outputs_mig,
prune_tool_outputs_subgoal, prune_tool_outputs_subgoal_mig, evict_sorted_blocks,
prune_protection_boundary) into a #[cfg(test)] impl block so the compiler enforces
their test-only status. Keep prune_stale_tool_outputs in the production impl (called
from native.rs). Gate dump_pruning_scores and dump_subgoal_registry in DebugDumper
with #[cfg(test)] since they are now only called from the test-only pruning impl.

compaction.rs: add TODO(review) comment on compact_context explaining why the simple
ContextService::compact_context delegation is not yet possible — the service lacks
probe validation, tool output archiving, Qdrant session-summary write, and returns
usize instead of CompactionOutcome. The existing full implementation is kept intact.
compact_context_command already delegates to compact_context and needs no changes.
@bug-ops bug-ops merged commit 09453ca into main Apr 27, 2026
32 checks passed
@bug-ops bug-ops deleted the 3523-migrate-agent-context branch April 27, 2026 19:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core zeph-core crate documentation Improvements or additions to documentation refactor Code refactoring without functional changes rust Rust code changes size/XL Extra large PR (500+ lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(core): extract zeph-agent-context crate from zeph-core (Phase 2)

1 participant