Skip to content

daemon: the auto-compaction toggle reaches the running session - #3986

Open
snimu wants to merge 1 commit into
mainfrom
sebastian/auto-compaction-live-2026-11-07
Open

snimu wants to merge 1 commit into
mainfrom
sebastian/auto-compaction-live-2026-11-07

Conversation

@snimu

@snimu snimu commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Turning auto-compaction on or off in a running daemon session had no effect on the session itself: it only took hold after the session was rebuilt (e.g. on restart), while the UI already showed the new value. The toggle now reaches the built session immediately and applies from the next turn.

Cause

set_auto_compaction (the command the TUI/clients use) wrote settings.json and the worker flag SessionCore.auto_compaction_enabled, but that flag is only read for the session summary. The threshold and overflow compaction arms read the core session's CompactionSettings.enabled, which was set once when the session was built. So a running session kept the old value until it was rebuilt. RPC mode did not have the bug: rpc/commands.rs already updated the core flag.

Fix

  • engine.rs: new SessionEngine::set_auto_compaction_enabled trait method; a default no-op, documented as blocking (call off the runtime).
  • agent_engine/session_engine_impl.rs: AgentSessionEngine implements it by forwarding to the built core session (pa_core wiring set_auto_compaction_enabled) under the session mutex via blocking_lock.
  • compaction.rs: CompactionManager::set_auto_compaction is now async; it still writes the worker flag, then calls the engine through spawn_blocking.
  • setting_switches.rs: handle_set_auto_compaction becomes async and awaits the compaction manager (worker/commands.rs call site updated). An unbuilt session reads the persisted setting when it builds.

Test

pa-daemon setting_switches::tests::set_auto_compaction_reaches_the_built_session: a faux-engine worker whose settings start with compaction disabled and a reserve that puts the threshold at 1616 tokens. Turn 1 (off) does not threshold-compact, toggling on makes turn 2 threshold-compact, toggling off makes turn 3 not compact again. It counts compaction_start events with reason threshold.

Fails on main (main 74591cd + only the test):

thread 'setting_switches::tests::set_auto_compaction_reaches_the_built_session' panicked at crates/pa-daemon/src/setting_switches.rs:1248:13:
assertion `left == right` failed: the second turn's threshold compaction (toggle Some(true))
  left: false
 right: true

Notes

  • The worker flag SessionCore.auto_compaction_enabled is kept on purpose: the session summary is composed under the worker's std core lock on async threads, where the session's tokio mutex cannot be taken. The same command writes both flags.
  • Gates (Prime sandbox, toolchain 1.98.1): cargo fmt --all --check, cargo clippy --workspace --all-targets -D warnings, and cargo test -p pa-daemon --no-fail-fast all pass.
  • .changes/auto-compaction-live-2026-11-07.md added.
  • Overlap: open PR pa-tui: hide Decision API children from the user-facing UI #3901 also touches worker/session_core.rs and summary.rs (not changed here). No other open PR touches setting_switches.rs or the daemon's compaction.rs.

Note

Medium Risk
Changes live compaction behavior on every turn boundary and adds async/blocking coordination on a frequently toggled setting; scope is confined to daemon compaction wiring with new regression coverage.

Overview
Auto-compaction on/off now affects already-built daemon sessions from the next turn, not only after a rebuild. Previously set_auto_compaction updated settings.json and the worker connection-state flag, while threshold/overflow compaction still read the value frozen at session build.

The fix threads a live update through the engine: new SessionEngine::set_auto_compaction_enabled (blocking, off-runtime), implemented on AgentSessionEngine by calling pa_core's session toggle under the session lock. CompactionManager::set_auto_compaction is async and invokes that hook via spawn_blocking after updating the worker flag; handle_set_auto_compaction awaits it, and the worker command dispatch does too.

A faux-engine test toggles compaction between turns and asserts threshold compaction_start events match the switch. A short changelog note documents the behavior change.

Reviewed by Cursor Bugbot for commit eae93df. Bugbot is set up for automated code reviews on this repo. Configure here.

Note

Make the auto-compaction toggle affect a running daemon session

Previously, changing the auto-compaction setting only updated connection state; a built session kept its old behavior until rebuilt. Now a successful set_auto_compaction also forwards the toggle to the live session.

  • Adds a SessionEngine.set_auto_compaction_enabled trait method with a no-op default, and implements it in AgentSessionEngine to forward the value to the built session (engine.rs, session_engine_impl.rs)
  • Makes CompactionManager.set_auto_compaction async: it updates connection state, then invokes the engine setter on Tokio's blocking thread pool and awaits it (compaction.rs)
  • Makes Worker.handle_set_auto_compaction async and awaits it in the dispatcher; it still saves the setting first and leaves runtime state unchanged if the save fails (setting_switches.rs, commands.rs)
  • Adds an end-to-end test set_auto_compaction_reaches_the_built_session verifying threshold compaction events appear and disappear on later turns after toggling
  • Behavioral Change: set_auto_compaction is now async, so dispatch waits for the live-session update before returning; engine setters that may block should be invoked off the runtime per the new trait docs
📊 Macroscope summarized eae93df. 6 files reviewed, 1 issue evaluated, 0 issues filtered, 1 comment posted

🗂️ Filtered Issues

The worker's set_auto_compaction wrote settings.json and the worker's
connection-state flag, but never the live core session's compaction
settings. The threshold and overflow arms read the core flag, which kept
its build-time value: a running session kept auto-compacting after the
user turned it off (and never started after turning it on) until the
session was rebuilt.

The command now also forwards the toggle to the built core session
through SessionEngine::set_auto_compaction_enabled, as RPC mode already
does. An unbuilt session reads the saved setting at its build.
Comment thread crates/pa-daemon/src/compaction.rs Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant