|
9 | 9 | import pytest |
10 | 10 |
|
11 | 11 | from mempalace.hooks_cli import ( |
12 | | - MAX_PRECOMPACT_BLOCK_ATTEMPTS, |
13 | 12 | SAVE_INTERVAL, |
14 | 13 | _count_human_messages, |
15 | 14 | _extract_recent_messages, |
@@ -60,85 +59,6 @@ def test_sanitize_empty_returns_unknown(): |
60 | 59 | assert _sanitize_session_id("!!!") == "unknown" |
61 | 60 |
|
62 | 61 |
|
63 | | -# --- hook_precompact attempt cap (regression for #955 deadlock fix) --- |
64 | | - |
65 | | - |
66 | | -def _call_precompact(session_id: str) -> dict: |
67 | | - """Invoke hook_precompact with a deterministic session_id, capture stdout. |
68 | | -
|
69 | | - Returns the parsed JSON decision emitted by the hook. |
70 | | - """ |
71 | | - stdout = io.StringIO() |
72 | | - with contextlib.redirect_stdout(stdout): |
73 | | - hook_precompact({"session_id": session_id}, "claude-code") |
74 | | - raw = stdout.getvalue().strip() |
75 | | - return json.loads(raw) if raw else {} |
76 | | - |
77 | | - |
78 | | -def test_precompact_first_two_attempts_block(tmp_path, monkeypatch): |
79 | | - """First MAX_PRECOMPACT_BLOCK_ATTEMPTS calls must block with a reason.""" |
80 | | - monkeypatch.setenv("HOME", str(tmp_path)) |
81 | | - monkeypatch.delenv("MEMPAL_DIR", raising=False) |
82 | | - |
83 | | - import mempalace.hooks_cli as hooks_cli |
84 | | - monkeypatch.setattr( |
85 | | - hooks_cli, "STATE_DIR", tmp_path / "hook_state", raising=False |
86 | | - ) |
87 | | - |
88 | | - sid = "test-session-block" |
89 | | - for i in range(MAX_PRECOMPACT_BLOCK_ATTEMPTS): |
90 | | - decision = _call_precompact(sid) |
91 | | - assert decision.get("decision") == "block", ( |
92 | | - f"attempt {i + 1}/{MAX_PRECOMPACT_BLOCK_ATTEMPTS}: expected block, " |
93 | | - f"got {decision}" |
94 | | - ) |
95 | | - assert decision.get("reason") == PRECOMPACT_BLOCK_REASON |
96 | | - |
97 | | - |
98 | | -def test_precompact_passes_through_after_cap(tmp_path, monkeypatch): |
99 | | - """After the cap is reached, the hook must stop blocking (fix for #955).""" |
100 | | - monkeypatch.setenv("HOME", str(tmp_path)) |
101 | | - monkeypatch.delenv("MEMPAL_DIR", raising=False) |
102 | | - |
103 | | - import mempalace.hooks_cli as hooks_cli |
104 | | - monkeypatch.setattr( |
105 | | - hooks_cli, "STATE_DIR", tmp_path / "hook_state", raising=False |
106 | | - ) |
107 | | - |
108 | | - sid = "test-session-passthrough" |
109 | | - for _ in range(MAX_PRECOMPACT_BLOCK_ATTEMPTS): |
110 | | - _call_precompact(sid) # exhaust the budget |
111 | | - |
112 | | - # Next call must pass through (empty JSON decision) |
113 | | - decision = _call_precompact(sid) |
114 | | - assert decision == {}, ( |
115 | | - f"after {MAX_PRECOMPACT_BLOCK_ATTEMPTS} attempts, hook must pass " |
116 | | - f"through to avoid deadlock; got {decision}" |
117 | | - ) |
118 | | - |
119 | | - |
120 | | -def test_precompact_counter_is_per_session(tmp_path, monkeypatch): |
121 | | - """A fresh session_id must get a fresh attempt budget.""" |
122 | | - monkeypatch.setenv("HOME", str(tmp_path)) |
123 | | - monkeypatch.delenv("MEMPAL_DIR", raising=False) |
124 | | - |
125 | | - import mempalace.hooks_cli as hooks_cli |
126 | | - monkeypatch.setattr( |
127 | | - hooks_cli, "STATE_DIR", tmp_path / "hook_state", raising=False |
128 | | - ) |
129 | | - |
130 | | - sid_a = "session-a" |
131 | | - sid_b = "session-b" |
132 | | - |
133 | | - # Exhaust session A |
134 | | - for _ in range(MAX_PRECOMPACT_BLOCK_ATTEMPTS): |
135 | | - _call_precompact(sid_a) |
136 | | - assert _call_precompact(sid_a) == {} # A is done blocking |
137 | | - |
138 | | - # Session B must still block on its first call — isolation between sessions |
139 | | - assert _call_precompact(sid_b).get("decision") == "block" |
140 | | - |
141 | | - |
142 | 62 | # --- _count_human_messages --- |
143 | 63 |
|
144 | 64 |
|
|
0 commit comments