Skip to content

The agent that grows with you

fix(plugins): post_compaction survives concurrent sessions and a failed projection

Tekniuma31975222e18
diff --git a/gateway/session_mutations.py b/gateway/session_mutations.py
index 8a84bf0dbb4..3eaf712558e 100644
--- a/gateway/session_mutations.py
+++ b/gateway/session_mutations.py
@@ -224,10 +224,14 @@ async def write_mutation(authority, ref, params, editor_servers, *, principal_id
# state.db or a contended writer lock never stalls other clients (main 1769024ca3b).
result = await asyncio.to_thread(mutate_runtime_session, authority.db, **fields,
_prepared=prepared, _authorize_write=authorize_write)
- await _project_result(authority, ref, live, params, result, editor_servers, applied=applied)
- if operation == 'compress' and applied and (prepared or {}).get('post_compaction'):
- from gateway.session_mutation_compress import fire_post_compaction_hook
- await fire_post_compaction_hook(authority, prepared, result)
+ try:
+ await _project_result(authority, ref, live, params, result, editor_servers, applied=applied)
+ finally:
+ # The receipt is committed: a failed projection must not swallow the hook, and an exact
+ # retry (applied=False) never refires it.
+ if operation == 'compress' and applied and (prepared or {}).get('post_compaction'):
+ from gateway.session_mutation_compress import fire_post_compaction_hook
+ await fire_post_compaction_hook(authority, prepared, result)
if operation == 'delete':
# After the synchronous projections (no yield before retirement), off the loop: the
# retired-media owner check is a messages-table pass (exact retries repeat it too).
diff --git a/hermes_cli/plugins_dispatch.py b/hermes_cli/plugins_dispatch.py
index e8cd9282249..accb9c5ddc1 100644
--- a/hermes_cli/plugins_dispatch.py
+++ b/hermes_cli/plugins_dispatch.py
@@ -52,7 +52,9 @@ _HOOK_TIMEOUT_BOUNDED_HOOKS: set[str] = {
}
# Hooks whose calls are told apart by another payload field: one session's commands are one
# sequence, so a still-running callback skips that session's next command, not another session's.
-_HOOK_CALL_IDENTITY_FIELDS: dict[str, tuple[str, ...]] = {"pre_command": ("session_key",)}
+_HOOK_CALL_IDENTITY_FIELDS: dict[str, tuple[str, ...]] = {
+ "pre_command": ("session_key",), "post_compaction": ("session_id",),
+}
# Policy hooks: timeout / still-running must fail closed (block the tool).
_HOOK_TIMEOUT_FAIL_CLOSED_HOOKS: set[str] = {"pre_tool_call"}
diff --git a/tests/agent/test_post_compaction_hook.py b/tests/agent/test_post_compaction_hook.py
index c11a38b360b..c73ee6be887 100644
--- a/tests/agent/test_post_compaction_hook.py
+++ b/tests/agent/test_post_compaction_hook.py
@@ -3,6 +3,8 @@ and a raising plugin never changes what the compaction returns (#118382)."""
from unittest.mock import patch
+import threading
+
import pytest
from hermes_cli import plugins as plugins_mod
@@ -44,3 +46,26 @@ def test_post_compaction_fires_once_per_committed_compaction(ctx, summary, commi
)
assert kw["messages_removed"] == len(_messages()) - len(compressed) > 0
assert kw["before_tokens"] > kw["after_tokens"] > 0
+
+
+def test_a_slow_callback_for_one_session_does_not_skip_another_sessions_compaction(ctx):
+ """The bounded dispatcher skips a callback still running for the same call; two sessions are two calls."""
+ entered, release, seen = threading.Event(), threading.Event(), []
+
+ def slow(session_id, **_kw):
+ seen.append(session_id)
+ if session_id == "a":
+ entered.set()
+ release.wait(10)
+
+ ctx.register_hook("post_compaction", slow)
+ first = threading.Thread(target=ctx._manager.invoke_hook, args=("post_compaction",), kwargs={"session_id": "a"})
+ first.start()
+ assert entered.wait(10)
+ try:
+ ctx._manager.invoke_hook("post_compaction", session_id="b")
+ finally:
+ release.set()
+ first.join(10)
+
+ assert sorted(seen) == ["a", "b"]