-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_tools_kitchen_gate.py
More file actions
417 lines (320 loc) · 15.4 KB
/
Copy pathtest_tools_kitchen_gate.py
File metadata and controls
417 lines (320 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
"""Tests for tools_kitchen.py: gate toggle, review gate cleanup, kitchen_id, misc."""
from __future__ import annotations
import json
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from autoskillit.hooks.formatters._fmt_primitives import _HOOK_CONFIG_PATH_COMPONENTS
from tests.server.conftest import _make_mock_ctx
pytestmark = [pytest.mark.layer("server"), pytest.mark.small]
# T2a
@pytest.mark.anyio
async def test_open_kitchen_enables_gate(tmp_path, monkeypatch):
"""After _open_kitchen_handler(), gate is enabled."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
with patch(
"autoskillit.server.tools.tools_kitchen._prime_quota_cache", new=AsyncMock()
):
with patch("autoskillit.server.tools.tools_kitchen._write_hook_config"):
from autoskillit.server.tools.tools_kitchen import _open_kitchen_handler
await _open_kitchen_handler()
mock_ctx.gate.enable.assert_called_once()
# T2b
def test_close_kitchen_disables_gate(tmp_path, monkeypatch):
"""After _close_kitchen_handler(), gate is disabled."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler()
mock_ctx.gate.disable.assert_called_once()
# T2c
def test_close_kitchen_no_file_no_error(tmp_path, monkeypatch):
"""_close_kitchen_handler() doesn't raise when no gate file exists."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler() # Should not raise
# Gate file was never created — confirm it still does not exist
assert not tmp_path.joinpath(*_HOOK_CONFIG_PATH_COMPONENTS).exists()
# ---------------------------------------------------------------------------
# Group J — T7, T8, T5, alwaysLoad MCP meta
# ---------------------------------------------------------------------------
def test_kitchen_failure_envelope_hint_says_install_not_reinstall() -> None:
from autoskillit.server.tools.tools_kitchen import _kitchen_failure_envelope
result = json.loads(_kitchen_failure_envelope(exc=RuntimeError("x"), stage="test"))
msg = result["user_visible_message"]
assert "autoskillit install" in msg
assert "reinstall" not in msg
def test_display_categories_omits_fleet_when_disabled() -> None:
"""Fleet category must not appear in iter_display_categories output when fleet is disabled."""
from autoskillit.config import iter_display_categories
cfg_features: dict[str, bool] = {"fleet": False}
categories = [name for name, _ in iter_display_categories(cfg_features)]
assert "Fleet" not in categories
def test_display_categories_includes_fleet_when_enabled() -> None:
"""Fleet category must appear in iter_display_categories output when fleet is enabled."""
from autoskillit.config import iter_display_categories
cfg_features: dict[str, bool] = {"fleet": True}
categories = [name for name, _ in iter_display_categories(cfg_features)]
assert "Fleet" in categories
# ---------------------------------------------------------------------------
# T5: close_kitchen cleans up review_gate_state.json
# ---------------------------------------------------------------------------
_REVIEW_GATE_STATE_RELPATH = (".autoskillit", "temp", "review_gate_state.json")
# T5-1
def test_close_kitchen_preserves_review_gate_when_loop_active(tmp_path, monkeypatch):
"""Preserve review_gate_state.json when an active review loop is in progress."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
state_path = tmp_path.joinpath(*_REVIEW_GATE_STATE_RELPATH)
state_path.parent.mkdir(parents=True, exist_ok=True)
state_path.write_text(
json.dumps(
{
"gate": "LOOP_REQUIRED",
"review_verdict": "changes_requested",
"check_review_loop_called": False,
"pr_number": "1290",
"set_at": "2026-04-26T04:30:00+00:00",
}
)
)
assert state_path.exists(), "State file must exist before close_kitchen"
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler()
assert state_path.exists(), "Active review loop state must survive close_kitchen"
# T5-2
def test_close_kitchen_no_review_gate_state_no_error(tmp_path, monkeypatch):
"""_close_kitchen_handler() must not raise when review_gate_state.json is absent."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler() # Must not raise
assert not tmp_path.joinpath(*_REVIEW_GATE_STATE_RELPATH).exists()
# T5-3
def test_close_kitchen_removes_review_gate_when_loop_complete(tmp_path, monkeypatch):
"""Remove review_gate_state.json when check_review_loop_called is True."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
state_path = tmp_path.joinpath(*_REVIEW_GATE_STATE_RELPATH)
state_path.parent.mkdir(parents=True, exist_ok=True)
state_path.write_text(
json.dumps(
{
"gate": "LOOP_REQUIRED",
"review_verdict": "changes_requested",
"check_review_loop_called": True,
"pr_number": "1290",
"set_at": "2026-04-26T04:30:00+00:00",
}
)
)
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler()
assert not state_path.exists(), "Completed loop state must be cleaned up on close"
# T5-4
def test_close_kitchen_removes_review_gate_when_gate_not_loop_required(tmp_path, monkeypatch):
"""_close_kitchen_handler() must remove review_gate_state.json when gate != LOOP_REQUIRED."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
state_path = tmp_path.joinpath(*_REVIEW_GATE_STATE_RELPATH)
state_path.parent.mkdir(parents=True, exist_ok=True)
state_path.write_text(
json.dumps(
{
"gate": "CLEAR",
"check_review_loop_called": False,
"pr_number": "1290",
"set_at": "2026-04-26T04:30:00+00:00",
}
)
)
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler()
assert not state_path.exists(), "Non-LOOP_REQUIRED gate state must be cleaned up on close"
# T5-5
def test_close_kitchen_removes_review_gate_on_corrupt_json(tmp_path, monkeypatch):
"""Delete review_gate_state.json when JSON is malformed (fail-safe)."""
monkeypatch.chdir(tmp_path)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
state_path = tmp_path.joinpath(*_REVIEW_GATE_STATE_RELPATH)
state_path.parent.mkdir(parents=True, exist_ok=True)
state_path.write_text("{not valid json")
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler()
assert not state_path.exists(), "Corrupt gate state must be deleted (fail-safe)"
@pytest.mark.anyio
async def test_open_kitchen_has_always_load_meta() -> None:
"""open_kitchen must carry anthropic/alwaysLoad: true in its MCP meta.
alwaysLoad ensures open_kitchen is always in the initial tool context for
direct 'claude' sessions (where ToolSearch is enabled). Defense-in-depth
against the MCP first-call race for non-order/cook sessions.
"""
from autoskillit.server import mcp
tool = await mcp.get_tool("open_kitchen")
assert tool is not None
assert tool.meta is not None and tool.meta.get("anthropic/alwaysLoad") is True, (
"open_kitchen missing anthropic/alwaysLoad:true — add to @mcp.tool(meta={...})"
)
# ---------------------------------------------------------------------------
# Triple-ID unification: kitchen_id inherits AUTOSKILLIT_CAMPAIGN_ID
# ---------------------------------------------------------------------------
@pytest.mark.anyio
async def test_open_kitchen_inherits_campaign_id_from_env(tmp_path, monkeypatch):
"""When AUTOSKILLIT_CAMPAIGN_ID is in env, open_kitchen uses it as kitchen_id."""
monkeypatch.chdir(tmp_path)
monkeypatch.setenv("AUTOSKILLIT_CAMPAIGN_ID", "abc123def456")
mock_ctx = _make_mock_ctx()
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
with patch(
"autoskillit.server.tools.tools_kitchen._prime_quota_cache", new=AsyncMock()
):
with patch("autoskillit.server.tools.tools_kitchen._write_hook_config"):
from autoskillit.server.tools.tools_kitchen import _open_kitchen_handler
await _open_kitchen_handler()
assert mock_ctx.kitchen_id == "abc123def456"
@pytest.mark.anyio
async def test_open_kitchen_generates_uuid_without_campaign_env(tmp_path, monkeypatch):
"""Without AUTOSKILLIT_CAMPAIGN_ID, open_kitchen generates a fresh UUID."""
monkeypatch.chdir(tmp_path)
monkeypatch.delenv("AUTOSKILLIT_CAMPAIGN_ID", raising=False)
mock_ctx = _make_mock_ctx()
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
with patch(
"autoskillit.server.tools.tools_kitchen._prime_quota_cache", new=AsyncMock()
):
with patch("autoskillit.server.tools.tools_kitchen._write_hook_config"):
from autoskillit.server.tools.tools_kitchen import _open_kitchen_handler
await _open_kitchen_handler()
kitchen_id = mock_ctx.kitchen_id
assert len(kitchen_id) == 36 and kitchen_id.count("-") == 4
# ---------------------------------------------------------------------------
# Kitchen-close orphan drain
# ---------------------------------------------------------------------------
@pytest.mark.anyio
async def test_close_kitchen_drains_orphaned_github_api_entries(tmp_path, monkeypatch):
"""Entries accumulated after last run_skill are flushed to disk at close."""
import json
from autoskillit.pipeline.github_api_log import DefaultGitHubApiLog
monkeypatch.chdir(tmp_path)
log = DefaultGitHubApiLog()
await log.record_httpx(
method="GET",
path="/repos/o/r/issues/1",
status_code=200,
latency_ms=10.0,
rate_limit_remaining=4999,
rate_limit_used=1,
rate_limit_reset=0,
timestamp="2026-04-27T10:00:00Z",
)
mock_ctx = _make_mock_ctx()
mock_ctx.project_dir = tmp_path
mock_ctx.github_api_log = log
mock_ctx.kitchen_id = "test-kitchen-123"
log_dir = tmp_path / "logs"
log_dir.mkdir()
mock_ctx.config.linux_tracing.log_dir = str(log_dir)
with patch("autoskillit.server._get_ctx", return_value=mock_ctx):
with patch("autoskillit.server.logger"):
with patch(
"autoskillit.server.tools.tools_kitchen.resolve_log_dir", return_value=log_dir
):
from autoskillit.server.tools.tools_kitchen import _close_kitchen_handler
_close_kitchen_handler()
orphan_path = log_dir / "github_api_usage_orchestrator.json"
assert orphan_path.exists()
data = json.loads(orphan_path.read_text())
assert data["total_requests"] == 1
assert not log._entries
@pytest.mark.anyio
async def test_open_kitchen_fail_closed_empty_content(monkeypatch, tmp_path):
"""open_kitchen returns fail-closed when content is empty (caught at validity gate)."""
monkeypatch.chdir(tmp_path)
ctx = _make_mock_ctx()
ctx.gate.enabled = True
ctx.gate_infrastructure_ready = True
_load_result = {"valid": True, "content": "", "dispatch_feasible": True}
ctx.recipes.load_and_validate.return_value = _load_result
ctx.recipes.find.return_value = MagicMock(path=tmp_path / "r.yaml")
ctx.config.providers = MagicMock()
ctx.backend = MagicMock()
ctx.backend.name = "test"
with (
patch("autoskillit.server._get_ctx", return_value=ctx),
patch("autoskillit.server.logger"),
patch(
"autoskillit.server.tools.tools_kitchen._apply_triage_gate",
new=AsyncMock(return_value=_load_result),
),
patch(
"autoskillit.server.tools.tools_kitchen._prime_quota_cache",
new_callable=AsyncMock,
),
patch("autoskillit.server.tools.tools_kitchen._write_hook_config"),
):
from autoskillit.server.tools.tools_kitchen import open_kitchen
raw = await open_kitchen(name="test-recipe")
parsed = json.loads(raw)
assert parsed["success"] is False
assert parsed.get("kitchen") == "failed"
assert "error" in parsed
assert "failed validation" in parsed["error"]
@pytest.mark.anyio
async def test_open_kitchen_fail_closed_missing_content(monkeypatch, tmp_path):
"""open_kitchen returns fail-closed when content key is missing (caught at validity gate)."""
monkeypatch.chdir(tmp_path)
ctx = _make_mock_ctx()
ctx.gate.enabled = True
ctx.gate_infrastructure_ready = True
_load_result = {"valid": True, "dispatch_feasible": True}
ctx.recipes.load_and_validate.return_value = _load_result
ctx.recipes.find.return_value = MagicMock(path=tmp_path / "r.yaml")
ctx.config.providers = MagicMock()
ctx.backend = MagicMock()
ctx.backend.name = "test"
with (
patch("autoskillit.server._get_ctx", return_value=ctx),
patch("autoskillit.server.logger"),
patch(
"autoskillit.server.tools.tools_kitchen._apply_triage_gate",
new=AsyncMock(return_value=_load_result),
),
patch(
"autoskillit.server.tools.tools_kitchen._prime_quota_cache",
new_callable=AsyncMock,
),
patch("autoskillit.server.tools.tools_kitchen._write_hook_config"),
):
from autoskillit.server.tools.tools_kitchen import open_kitchen
raw = await open_kitchen(name="test-recipe")
parsed = json.loads(raw)
assert parsed["success"] is False
assert parsed.get("kitchen") == "failed"
assert "error" in parsed
assert "failed validation" in parsed["error"]