refactor(compression): tidy in-place compaction path (simplify pass)
Parallel 3-reviewer cleanup of the in-place compaction code. Findings applied:
- perf: in-place mode no longer pre-flushes current-turn messages. The flush
ran INSERTs that the immediately-following replace_messages(compressed)
DELETE+reinsert discarded -- pure wasted writes per compaction. The
current-turn tail survives via the compressor's compressed output
(protect_last_n), not the flush. Verified no data loss; rotation still
pre-flushes (its old session row is preserved, so the flush is real there).
- quality: hoist the two shared post-write steps (update_system_prompt +
_last_flushed_db_idx = 0) below the if/else -- they ran in both branches
against agent.session_id. Removes the easiest divergence bug.
- quality: compute the compaction-boundary locals (_old_sid, _is_boundary,
_boundary_parent) ONCE instead of recomputing locals().get('old_session_id')
and the "_old_sid or agent.session_id or ''" chain three times.
- quality: initialize compacted_in_place up front and assign
agent._last_compaction_in_place directly, dropping the fragile
locals().get('compacted_in_place') reflection.
- reuse: parse the in_place config flag with utils.is_truthy_value (the
project's canonical truthy coerce) instead of a hand-rolled
str().lower() in {...} (agent_init already imports from utils).
Dropped as false positives / out of scope: gateway getattr of agent internals
(established session_id pattern), dual result-dict carry (mirrors history_offset
etc.), stringly-typed "compression" (codebase-wide convention, no constant).
Behavior-preserving: 7 in-place tests (incl. 2 new flush-guard tests) + 26
rotation/boundary/persistence/command tests green; mutation check confirms the
durable-replace guard still binds (removing replace_messages fails the test);
ruff clean. Added test_in_place_skips_redundant_preflush /
test_rotation_still_preflushes to guard the perf change.
This commit is contained in:
parent
1fbf48d4ad
commit
4f9485a95d
3 changed files with 93 additions and 47 deletions
|
|
@ -124,6 +124,48 @@ class TestInPlaceCompaction:
|
|||
roles = [m["role"] for m in compressed if m.get("role") != "system"]
|
||||
assert all(roles[i] != roles[i + 1] for i in range(len(roles) - 1))
|
||||
|
||||
def test_in_place_skips_redundant_preflush(self):
|
||||
"""In-place must NOT pre-flush current-turn messages: replace_messages
|
||||
rewrites the whole row, so a flush would INSERT rows it immediately
|
||||
deletes (wasted writes). The current-turn tail survives via the
|
||||
compressor's `compressed` output, not the flush."""
|
||||
from hermes_state import SessionDB
|
||||
from agent.conversation_compression import compress_context
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
db = SessionDB(db_path=Path(tmp) / "t.db")
|
||||
_seed(db, "ip_flush", "f")
|
||||
agent = _make_agent(db, "ip_flush", in_place=True)
|
||||
calls = {"n": 0}
|
||||
agent._flush_messages_to_session_db = lambda *a, **k: calls.__setitem__(
|
||||
"n", calls["n"] + 1
|
||||
)
|
||||
compress_context(
|
||||
agent, [{"role": "user", "content": "x"}] * 8,
|
||||
approx_tokens=100_000, system_message="sys",
|
||||
)
|
||||
assert calls["n"] == 0
|
||||
|
||||
def test_rotation_still_preflushes(self):
|
||||
"""Rotation MUST pre-flush so current-turn messages survive in the
|
||||
preserved old (parent) session before it is ended (#47202)."""
|
||||
from hermes_state import SessionDB
|
||||
from agent.conversation_compression import compress_context
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
db = SessionDB(db_path=Path(tmp) / "t.db")
|
||||
_seed(db, "rot_flush", "f")
|
||||
agent = _make_agent(db, "rot_flush", in_place=False)
|
||||
calls = {"n": 0}
|
||||
agent._flush_messages_to_session_db = lambda *a, **k: calls.__setitem__(
|
||||
"n", calls["n"] + 1
|
||||
)
|
||||
compress_context(
|
||||
agent, [{"role": "user", "content": "x"}] * 8,
|
||||
approx_tokens=100_000, system_message="sys",
|
||||
)
|
||||
assert calls["n"] == 1
|
||||
|
||||
|
||||
class TestRotationStillDefault:
|
||||
def test_rotation_when_flag_off(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue