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 
 
 
 
 
 
 
 
 
 
 
 
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
import asyncio
import json
from pathlib import Path
from types import SimpleNamespace

import httpx
import pytest
from pydantic import ValidationError

from agent.config import Config
from agent.core.session import Event, Session
from agent.messaging.gateway import NotificationGateway
from agent.messaging.models import NotificationRequest, NotificationResult
from agent.messaging.slack import SlackProvider, _format_slack_mrkdwn
from agent.tools.notify_tool import notify_handler
from backend.session_manager import AgentSession, SessionManager


class DummyToolRouter:
    def get_tool_specs_for_llm(self) -> list[dict]:
        return []


class RecordingGateway:
    def __init__(self):
        self.enqueued: list[NotificationRequest] = []
        self.sent: list[NotificationRequest] = []

    async def enqueue(self, request: NotificationRequest) -> bool:
        self.enqueued.append(request)
        return True

    async def send_many(
        self, requests: list[NotificationRequest]
    ) -> list[NotificationResult]:
        self.sent.extend(requests)
        return [
            NotificationResult(
                destination=request.destination,
                ok=True,
                provider="test",
            )
            for request in requests
        ]


def _config_with_messaging(**destination_overrides) -> Config:
    destination = {
        "provider": "slack",
        "token": "xoxb-test",
        "channel": "C123",
        **destination_overrides,
    }
    return Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "destinations": {
                    "slack.ops": destination,
                },
            },
        }
    )


def _test_session(config: Config, gateway, session_id: str = "session-test") -> Session:
    return Session(
        asyncio.Queue(),
        config=config,
        tool_router=DummyToolRouter(),
        context_manager=SimpleNamespace(items=[]),
        notification_gateway=gateway,
        session_id=session_id,
    )


def test_messaging_config_validates_destination_names():
    with pytest.raises(ValidationError):
        Config.model_validate(
            {
                "model_name": "moonshotai/Kimi-K2.6",
                "messaging": {
                    "enabled": True,
                    "destinations": {
                        "Slack Ops": {
                            "provider": "slack",
                            "token": "x",
                            "channel": "C123",
                        }
                    },
                },
            }
        )

    config = _config_with_messaging(allow_agent_tool=True, allow_auto_events=True)
    assert config.messaging.can_agent_tool_send("slack.ops")
    assert config.messaging.can_auto_send("slack.ops")


def test_messaging_config_default_auto_destinations_only_returns_auto_enabled():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    },
                    "slack.tool": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C999",
                        "allow_agent_tool": True,
                    },
                },
            },
        }
    )

    assert config.messaging.default_auto_destinations() == ["slack.ops"]


def test_messaging_config_default_auto_destinations_empty_when_disabled():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": False,
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    },
                },
            },
        }
    )

    assert config.messaging.default_auto_destinations() == []


def test_slack_mrkdwn_formatter_converts_common_markdown():
    formatted = _format_slack_mrkdwn(
        "# Result\n"
        "**Done** with *details* and ~~old text~~.\n"
        "See [PR](https://github.com/huggingface/ml-intern/pull/116).\n"
        "Keep `**literal**` and ```python\nx < 3\n``` untouched.\n"
        "Escape <raw> & text."
    )

    assert "*Result*" in formatted
    assert "*Done*" in formatted
    assert "_details_" in formatted
    assert "~old text~" in formatted
    assert "<https://github.com/huggingface/ml-intern/pull/116|PR>" in formatted
    assert "`**literal**`" in formatted
    assert "```python\nx < 3\n```" in formatted
    assert "Escape &lt;raw&gt; &amp; text." in formatted


@pytest.mark.asyncio
async def test_slack_provider_formats_and_sends_payload():
    seen: dict[str, object] = {}

    def handler(request: httpx.Request) -> httpx.Response:
        seen["auth"] = request.headers["Authorization"]
        seen["content_type"] = request.headers["Content-Type"]
        seen["json"] = request.read().decode("utf-8")
        return httpx.Response(200, json={"ok": True, "ts": "123.456"})

    async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
        provider = SlackProvider()
        result = await provider.send(
            client,
            "slack.ops",
            _config_with_messaging().messaging.destinations["slack.ops"],
            NotificationRequest(
                destination="slack.ops",
                title="Approval required",
                message="A **run** is waiting. See [details](https://example.com).",
                severity="warning",
                metadata={"session_id": "sess-1"},
            ),
        )

    assert result.ok
    assert result.external_id == "123.456"
    assert seen["auth"] == "Bearer xoxb-test"
    assert seen["content_type"].startswith("application/json")
    payload = json.loads(str(seen["json"]))
    assert payload["channel"] == "C123"
    assert payload["mrkdwn"] is True
    assert payload["text"] == (
        "[WARNING] Approval required\n"
        "A *run* is waiting. See <https://example.com|details>.\n"
        "session_id: sess-1"
    )


@pytest.mark.asyncio
async def test_notification_gateway_retries_transient_failures(monkeypatch):
    attempts = {"count": 0}

    def handler(_request: httpx.Request) -> httpx.Response:
        attempts["count"] += 1
        if attempts["count"] == 1:
            return httpx.Response(503, json={"ok": False})
        return httpx.Response(200, json={"ok": True, "ts": "999.1"})

    async def fake_sleep(_delay: float) -> None:
        return None

    monkeypatch.setattr("agent.messaging.gateway.asyncio.sleep", fake_sleep)

    config = _config_with_messaging(allow_agent_tool=True)
    gateway = NotificationGateway(config.messaging)
    async with httpx.AsyncClient(transport=httpx.MockTransport(handler)) as client:
        gateway._client = client
        result = await gateway.send(
            NotificationRequest(
                destination="slack.ops",
                message="hello",
            )
        )
        gateway._client = None

    assert attempts["count"] == 2
    assert result.ok


@pytest.mark.asyncio
async def test_notify_tool_rejects_non_allowlisted_destinations():
    config = _config_with_messaging(allow_agent_tool=False)
    gateway = RecordingGateway()
    session = _test_session(config, gateway)

    output, ok = await notify_handler(
        {"destinations": ["slack.ops"], "message": "done"},
        session=session,
    )

    assert not ok
    assert "unavailable for the notify tool" in output
    assert gateway.sent == []


@pytest.mark.asyncio
async def test_notify_tool_sends_to_allowlisted_destinations():
    config = _config_with_messaging(allow_agent_tool=True)
    gateway = RecordingGateway()
    session = _test_session(config, gateway, session_id="sess-42")

    output, ok = await notify_handler(
        {
            "destinations": ["slack.ops"],
            "title": "Training complete",
            "message": "The run finished successfully.",
            "severity": "success",
        },
        session=session,
    )

    assert ok
    assert output == "slack.ops: sent"
    assert len(gateway.sent) == 1
    sent = gateway.sent[0]
    assert sent.metadata["session_id"] == "sess-42"
    assert sent.metadata["model"] == "moonshotai/Kimi-K2.6"


@pytest.mark.asyncio
async def test_session_auto_notifications_only_send_opted_in_auto_destinations():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    },
                    "slack.tool": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C999",
                        "allow_agent_tool": True,
                    },
                },
            },
        }
    )
    gateway = RecordingGateway()
    session = _test_session(config, gateway, session_id="sess-auto")
    session.set_notification_destinations(["slack.ops", "slack.tool"])

    await session.send_event(
        Event(
            event_type="approval_required",
            data={"tools": [{"tool": "hf_jobs", "tool_call_id": "tc-1"}]},
        )
    )
    await session.send_event(
        Event(event_type="assistant_message", data={"content": "normal message"})
    )

    assert len(gateway.enqueued) == 1
    request = gateway.enqueued[0]
    assert request.destination == "slack.ops"
    assert request.severity == "warning"
    assert request.event_type == "approval_required"
    assert "hf_jobs" in request.message


@pytest.mark.asyncio
async def test_turn_complete_auto_notification_includes_final_response_summary():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    }
                },
            },
        }
    )
    gateway = RecordingGateway()
    session = _test_session(config, gateway, session_id="sess-done")
    session.set_notification_destinations(["slack.ops"])

    await session.send_event(
        Event(
            event_type="turn_complete",
            data={
                "history_size": 12,
                "final_response": "Evaluation finished. Accuracy: 84.2% on the validation split.",
            },
        )
    )

    assert len(gateway.enqueued) == 1
    request = gateway.enqueued[0]
    assert request.destination == "slack.ops"
    assert request.severity == "success"
    assert request.event_type == "turn_complete"
    assert "completed successfully" in request.message
    assert "Accuracy: 84.2%" in request.message


@pytest.mark.asyncio
async def test_turn_complete_auto_notification_supports_longer_summary():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    }
                },
            },
        }
    )
    gateway = RecordingGateway()
    session = _test_session(config, gateway, session_id="sess-long")
    session.set_notification_destinations(["slack.ops"])

    long_summary = "A" * 1200 + " END"
    await session.send_event(
        Event(
            event_type="turn_complete",
            data={
                "history_size": 12,
                "final_response": long_summary,
            },
        )
    )

    assert len(gateway.enqueued) == 1
    request = gateway.enqueued[0]
    assert request.event_type == "turn_complete"
    assert "A" * 1200 in request.message
    assert request.message.endswith("END")


@pytest.mark.asyncio
async def test_turn_complete_auto_notification_can_be_deferred():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    }
                },
            },
        }
    )
    gateway = RecordingGateway()
    session = Session(
        asyncio.Queue(),
        config=config,
        tool_router=DummyToolRouter(),
        context_manager=SimpleNamespace(items=[]),
        notification_gateway=gateway,
        notification_destinations=["slack.ops"],
        defer_turn_complete_notification=True,
        session_id="sess-deferred",
    )
    event = Event(
        event_type="turn_complete",
        data={"final_response": "Finished after the CLI drained the stream."},
    )

    await session.send_event(event)
    assert gateway.enqueued == []

    await session.send_deferred_turn_complete_notification(event)

    assert len(gateway.enqueued) == 1
    request = gateway.enqueued[0]
    assert request.destination == "slack.ops"
    assert request.event_type == "turn_complete"
    assert "Finished after the CLI drained the stream." in request.message


@pytest.mark.asyncio
async def test_turn_complete_can_be_disabled_by_custom_auto_event_config():
    config = Config.model_validate(
        {
            "model_name": "moonshotai/Kimi-K2.6",
            "messaging": {
                "enabled": True,
                "auto_event_types": ["error"],
                "destinations": {
                    "slack.ops": {
                        "provider": "slack",
                        "token": "xoxb-test",
                        "channel": "C123",
                        "allow_auto_events": True,
                    }
                },
            },
        }
    )
    gateway = RecordingGateway()
    session = _test_session(config, gateway, session_id="sess-optout")
    session.set_notification_destinations(["slack.ops"])

    await session.send_event(
        Event(
            event_type="turn_complete",
            data={"final_response": "This should not notify."},
        )
    )

    assert gateway.enqueued == []


def test_session_manager_updates_notification_destinations_in_session_info():
    config = _config_with_messaging(allow_auto_events=True)
    manager = SessionManager(
        str(Path(__file__).resolve().parents[2] / "configs" / "cli_agent_config.json")
    )
    manager.config = config
    manager.sessions = {}

    session = _test_session(config, RecordingGateway(), session_id="sess-manager")
    manager.sessions["sess-manager"] = AgentSession(
        session_id="sess-manager",
        session=session,
        tool_router=DummyToolRouter(),
        submission_queue=asyncio.Queue(),
    )

    updated = manager.set_notification_destinations(
        "sess-manager",
        ["slack.ops", "slack.ops"],
    )

    assert updated == ["slack.ops"]
    info = manager.get_session_info("sess-manager")
    assert info is not None
    assert info["notification_destinations"] == ["slack.ops"]

    with pytest.raises(ValueError):
        manager.set_notification_destinations("sess-manager", ["slack.unknown"])