Skip to content

feat(memory): tiered context compaction — soft at 70%, hard at 90% (#1338)#1720

Merged
bug-ops merged 3 commits intomainfrom
tiered-context-compaction-soft
Mar 14, 2026
Merged

feat(memory): tiered context compaction — soft at 70%, hard at 90% (#1338)#1720
bug-ops merged 3 commits intomainfrom
tiered-context-compaction-soft

Conversation

@bug-ops
Copy link
Copy Markdown
Owner

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

Summary

Implements tiered context compaction (issue #1338):

  • Soft tier (70%): prune tool outputs + apply deferred summaries — no LLM call, does not set compacted_this_turn, allows Hard tier to fire in the same turn
  • Hard tier (90%): full LLM summarization (existing behavior, now triggered at 90% instead of 80%)
  • New CompactionTier enum (None / Soft / Hard) replaces the separate should_compact() and should_apply_deferred() predicates

Config changes

[memory]
soft_compaction_threshold = 0.70   # new — soft tier
hard_compaction_threshold = 0.90   # renamed from compaction_threshold
# deferred_apply_threshold = 0.70  # preserved (count-pressure trigger)

Old compaction_threshold still accepted via #[serde(alias)]. ZEPH_MEMORY_COMPACTION_THRESHOLD env var remapped to hard threshold.

Validation

  • All 7 critic findings (design + implementation) verified as addressed
  • Security: threshold invariant (soft < hard) enforced in config.validate(); no sensitive data leaks
  • Perf: compaction_tier() is O(1), allocation-free; soft tier has zero await points
  • Tests: 5364 passed, 12 skipped, 0 failed

Follow-up

Closes #1338

@github-actions github-actions bot added documentation Improvements or additions to documentation llm zeph-llm crate (Ollama, Claude) rust Rust code changes core zeph-core crate config Configuration file changes enhancement New feature or request size/XL Extra large PR (500+ lines) labels Mar 14, 2026
bug-ops added 3 commits March 14, 2026 01:46
…#1338)

Replace single compaction_threshold (0.80) with two-tier compaction:
- Soft tier (soft_compaction_threshold, default 0.70): apply deferred tool
  summaries + prune tool outputs without LLM call. Does not set
  compacted_this_turn, allowing the hard tier to fire in the same turn.
- Hard tier (hard_compaction_threshold, default 0.90): apply deferred
  summaries, prune tool outputs, fall back to full LLM summarization.
  Sets compacted_this_turn to prevent double LLM calls per turn.

Add CompactionTier enum (None/Soft/Hard) replacing should_compact() and
should_apply_deferred() methods. Preserve maybe_apply_deferred_summaries()
for tool execution loop call sites (legacy.rs:166, native.rs:202, mod.rs:2428)
with both token-pressure (via compaction_tier()) and count-pressure triggers.

Config backward compat: compaction_threshold serde alias maps to
hard_compaction_threshold. ZEPH_MEMORY_COMPACTION_THRESHOLD env var
also remapped to hard threshold.

BREAKING: deferred_apply_threshold config field removed (silently ignored
by serde). soft_compaction_threshold replaces it. Update config.toml to
use new field names.
- Add ZEPH_MEMORY_SOFT_COMPACTION_THRESHOLD to ENV_KEYS cleanup array
  in config/tests.rs (REV-SUGGEST-02): count 52 → 53 so clear_env()
  covers the new env var
- Update env var table in docs/src/advanced/context.md (REV-SUGGEST-01):
  replace stale ZEPH_MEMORY_DEFERRED_APPLY_THRESHOLD row with
  ZEPH_MEMORY_SOFT_COMPACTION_THRESHOLD; update ZEPH_MEMORY_COMPACTION_THRESHOLD
  description and default (0.75 → 0.90, now hard threshold backward compat alias)

Follow-up filed: #1719 (wizard hard_compaction_threshold prompt)
@bug-ops bug-ops force-pushed the tiered-context-compaction-soft branch from 1092c35 to 88b7c85 Compare March 14, 2026 00:46
@bug-ops bug-ops enabled auto-merge (squash) March 14, 2026 00:46
@bug-ops bug-ops merged commit 20ab06f into main Mar 14, 2026
19 checks passed
@bug-ops bug-ops deleted the tiered-context-compaction-soft branch March 14, 2026 00:54
bug-ops added a commit that referenced this pull request Mar 14, 2026
…tion (#1720)

Resolve conflicts with feat(core): tiered compaction (soft 70% / hard 90%).
Integrate the three compaction guards from #1708 into the new tiered
architecture: cooldown skips Hard-tier LLM summarization only (Soft pruning
still runs), counterproductive and exhaustion guards apply within the Hard
tier after LLM summarization.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

config Configuration file changes core zeph-core crate documentation Improvements or additions to documentation enhancement New feature or request llm zeph-llm crate (Ollama, Claude) rust Rust code changes size/XL Extra large PR (500+ lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tiered context compaction: soft at 70%, hard at 90%

1 participant