File size: 27,106 Bytes
f440f03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
"""Tests Maris AI projektu aģenta helperiem."""

from __future__ import annotations

import json
from pathlib import Path

import pytest

from maris_core.space_agent import (
    SPACE_AGENT_MODEL_DEFAULT,
    SPACE_AGENT_SPACE_REPO_DEFAULT,
    SpaceAgentChatRequest,
    SpaceAgentToolCall,
    build_space_agent_messages,
    execute_space_agent_tool,
    generate_space_agent_reply,
    get_space_agent_runtime_info,
    resolve_space_agent_models,
)


def test_space_agent_runtime_defaults_are_stable(monkeypatch) -> None:
    monkeypatch.delenv("HF_SPACE_ASSISTANT_MODEL", raising=False)
    monkeypatch.delenv("HF_SPACE_REPO", raising=False)
    monkeypatch.delenv("HF_SPACE_ASSISTANT_MODELS", raising=False)

    runtime = get_space_agent_runtime_info()

    assert runtime.model == SPACE_AGENT_MODEL_DEFAULT
    assert runtime.default_model == SPACE_AGENT_MODEL_DEFAULT
    assert runtime.space_repo == SPACE_AGENT_SPACE_REPO_DEFAULT
    assert SPACE_AGENT_MODEL_DEFAULT in runtime.available_models
    assert "model_dataset_playbook" in runtime.tool_names
    assert "browser_capabilities" in runtime.tool_names
    assert "persona_catalog" in runtime.tool_names
    assert "read_workspace_file" in runtime.tool_names
    assert "write_workspace_file" in runtime.tool_names
    assert "workspace_command_catalog" in runtime.tool_names
    assert any(item["title"] == "Model & dataset fixer" for item in runtime.capabilities)
    assert runtime.command_presets
    assert runtime.command_presets[0]["items"]


def test_build_space_agent_messages_includes_system_prompt_and_history() -> None:
    request = SpaceAgentChatRequest(
        message="Palīdzi ar manu Maris darba telpu",
        model="MarisUK/Codex",
        task_mode="code",
        history=[
            {"role": "user", "content": "Sveiks"},
            {"role": "assistant", "content": "Sveiks!"},
        ],
    )

    messages = build_space_agent_messages(request)

    assert messages[0]["role"] == "system"
    assert "MarisUK/Codex" in messages[0]["content"]
    assert "Maris AI Project Operator" in messages[0]["content"]
    assert "model_dataset_playbook" in messages[0]["content"]
    assert "audit → validate → evaluate → fix → train → sync" in messages[0]["content"]
    assert "`code`" in messages[0]["content"]
    assert messages[-1] == {"role": "user", "content": "Palīdzi ar manu Maris darba telpu"}
    assert messages[1]["content"] == "Sveiks"


class _DummyClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token
        self.models: list[str] = []

    def chat_completion(self, **kwargs: object) -> dict[str, object]:
        self.models.append(str(kwargs["model"]))
        return {
            "choices": [
                {
                    "message": {
                        "content": "Profesionāla atbilde par Maris darba telpas konfigurāciju.",
                    }
                }
            ]
        }


def test_generate_space_agent_reply_uses_hf_client_when_available() -> None:
    request = SpaceAgentChatRequest(
        message="Sakārto manu Space",
        model="MarisUK/Codex",
    )

    response = generate_space_agent_reply(request, client_factory=_DummyClient, token="hf_test")

    assert response.model == "MarisUK/Codex"
    assert response.used_fallback is False
    assert "Profesionāla atbilde" in response.response
    assert response.task_mode == "chat"


def test_generate_space_agent_reply_uses_hf_inference_space_runtime_config(
    monkeypatch,
) -> None:
    captured_kwargs: dict[str, object] = {}

    class _ConfiguredClient:
        def __init__(self, **kwargs: object) -> None:
            captured_kwargs.update(kwargs)

        def chat_completion(self, **kwargs: object) -> dict[str, object]:
            return {
                "choices": [
                    {
                        "message": {
                            "content": f"Atbilde no {kwargs['model']}",
                        }
                    }
                ]
            }

    monkeypatch.setenv("HF_INFERENCE_API_KEY", "hf_inference_secret")

    response = generate_space_agent_reply(
        SpaceAgentChatRequest(message="Sakārto manu Space"),
        client_factory=_ConfiguredClient,
    )

    assert response.model == SPACE_AGENT_MODEL_DEFAULT
    assert captured_kwargs == {
        "provider": "hf-inference",
        "base_url": "https://api-inference.huggingface.co",
        "token": "hf_inference_secret",
    }


class _ToolCallingClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token
        self.calls = 0

    def chat_completion(self, **_: object) -> dict[str, object]:
        self.calls += 1
        if self.calls == 1:
            return {
                "choices": [
                    {
                        "message": {
                            "content": (
                                '{"mode":"tool","tool_calls":['
                                '{"name":"project_runtime","arguments":{}},'
                                '{"name":"sync_commands","arguments":{}}'
                                "]}"
                            )
                        }
                    }
                ]
            }
        return {
            "choices": [
                {
                    "message": {
                        "content": '{"mode":"final","response":"Izmanto sync komandu un publicē Space profesionāli."}'
                    }
                }
            ]
        }


class _MultiStepToolCallingClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token
        self.calls = 0

    def chat_completion(self, **_: object) -> dict[str, object]:
        self.calls += 1
        if self.calls == 1:
            return {
                "choices": [
                    {
                        "message": {
                            "content": (
                                '{"mode":"tool","tool_calls":['
                                '{"name":"list_huggingface_repo_files","arguments":'
                                '{"repo_id":"MarisUK/maris-ai-master","repo_type":"model"}}'
                                "]}"
                            )
                        }
                    }
                ]
            }
        if self.calls == 2:
            return {
                "choices": [
                    {
                        "message": {
                            "content": (
                                '{"mode":"tool","tool_calls":['
                                '{"name":"read_huggingface_repo_file","arguments":'
                                '{"repo_id":"MarisUK/maris-ai-master","repo_type":"model","path":"README.md"}},'
                                '{"name":"write_huggingface_repo_file","arguments":'
                                '{"repo_id":"MarisUK/maris-ai-master","repo_type":"model","path":"README.md",'
                                '"content":"salabots modelis","commit_message":"Fix model README"}}'
                                "]}"
                            )
                        }
                    }
                ]
            }
        return {
            "choices": [
                {
                    "message": {
                        "content": (
                            '{"mode":"final","response":"Pārbaudīju modeli, salaboju README un saglabāju izmaiņas."}'
                        )
                    }
                }
            ]
        }


def test_execute_space_agent_tool_returns_runtime_payload() -> None:
    result = execute_space_agent_tool(SpaceAgentToolCall(name="project_runtime", arguments={}))

    assert result["model"] == SPACE_AGENT_MODEL_DEFAULT
    assert result["space_repo"] == SPACE_AGENT_SPACE_REPO_DEFAULT


def test_execute_space_agent_tool_returns_model_dataset_playbook() -> None:
    result = execute_space_agent_tool(
        SpaceAgentToolCall(name="model_dataset_playbook", arguments={})
    )

    assert result["dataset_repo"]
    assert result["model_repo"]
    assert result["space_repo"]
    assert "latest_agent_principles" in result
    assert "recommended_loop" in result
    assert "validate_dataset" in result["repo_commands"]
    assert any(
        "HF_TOKEN" in item or "MARIS_REPO_TOKEN" in item for item in result["required_setup"]
    )


def test_execute_space_agent_tool_returns_browser_capabilities() -> None:
    result = execute_space_agent_tool(SpaceAgentToolCall(name="browser_capabilities", arguments={}))

    assert result["provider"] == "playwright"
    assert "extract_text" in result["supported_actions"]


def test_execute_space_agent_tool_returns_workspace_command_catalog() -> None:
    result = execute_space_agent_tool(
        SpaceAgentToolCall(name="workspace_command_catalog", arguments={})
    )

    assert result["presets"]
    assert any(group["category"] == "python" for group in result["presets"])
    assert any(
        item["id"] == "frontend-build" for group in result["presets"] for item in group["items"]
    )


def test_execute_space_agent_tool_returns_persona_catalog() -> None:
    result = execute_space_agent_tool(SpaceAgentToolCall(name="persona_catalog", arguments={}))

    assert result["default_persona_id"] == "assistant"
    assert any(persona["id"] == "teacher" for persona in result["personas"])


def test_execute_space_agent_tool_can_list_huggingface_repos(monkeypatch) -> None:
    class FakeRepo:
        def __init__(self, repo_id: str) -> None:
            self.id = repo_id

    class FakeApi:
        def list_models(self, **_: object):
            return [FakeRepo("MarisUK/maris-ai-master")]

        def list_datasets(self, **_: object):
            return []

        def list_spaces(self, **_: object):
            return [FakeRepo("MarisUK/maris.ai.agent")]

    monkeypatch.setattr("maris_core.space_agent._get_hf_api_client", lambda: FakeApi())

    result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="list_huggingface_repos", arguments={"repo_type": "all", "limit": 5}
        )
    )

    assert result["owner"] == "MarisUK"
    assert any(entry["id"] == "MarisUK/maris-ai-master" for entry in result["entries"])
    assert any(entry["repo_type"] == "space" for entry in result["entries"])


def test_execute_space_agent_tool_can_read_and_write_huggingface_repo_files(
    monkeypatch, tmp_path: Path
) -> None:
    uploaded: list[dict[str, object]] = []
    downloaded = tmp_path / "README.md"
    downloaded.write_text("hf saturs", encoding="utf-8")

    class FakeApi:
        def upload_file(self, **kwargs: object) -> None:
            uploaded.append(kwargs)

        def list_repo_files(self, **_: object):
            return ["README.md", "app.py"]

    monkeypatch.setattr("maris_core.space_agent._get_hf_api_client", lambda: FakeApi())
    monkeypatch.setattr(
        "maris_core.space_agent._download_hf_repo_file",
        lambda **_: str(downloaded),
    )

    listing = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="list_huggingface_repo_files",
            arguments={"repo_id": "MarisUK/maris.ai.agent", "repo_type": "space"},
        )
    )
    assert "README.md" in listing["entries"]

    read_result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="read_huggingface_repo_file",
            arguments={
                "repo_id": "MarisUK/maris.ai.agent",
                "repo_type": "space",
                "path": "README.md",
            },
        )
    )
    assert read_result["content"] == "hf saturs"

    write_result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="write_huggingface_repo_file",
            arguments={
                "repo_id": "MarisUK/maris.ai.agent",
                "repo_type": "space",
                "path": "README.md",
                "content": "jauns hf saturs",
                "commit_message": "Update README",
            },
        )
    )
    assert write_result["saved"] is True
    assert uploaded[0]["repo_id"] == "MarisUK/maris.ai.agent"
    assert uploaded[0]["path_in_repo"] == "README.md"


def test_execute_space_agent_tool_can_list_read_and_write_workspace_files(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    existing_file = workspace / "notes.txt"
    existing_file.write_text("sākotnējais saturs", encoding="utf-8")

    listing = execute_space_agent_tool(
        SpaceAgentToolCall(name="list_workspace", arguments={"path": "."}),
        context={"workspace_root": str(workspace)},
    )
    assert listing["path"] == "."
    assert any(entry["path"] == "notes.txt" for entry in listing["entries"])

    read_result = execute_space_agent_tool(
        SpaceAgentToolCall(name="read_workspace_file", arguments={"path": "notes.txt"}),
        context={"workspace_root": str(workspace)},
    )
    assert read_result["content"] == "sākotnējais saturs"

    write_result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="write_workspace_file",
            arguments={"path": "nested/todo.txt", "content": "jauns saturs"},
        ),
        context={"workspace_root": str(workspace)},
    )
    assert write_result["saved"] is True
    assert write_result["operation"] == "create"
    assert "+++ b/nested/todo.txt" in write_result["diff"]
    assert (workspace / "nested" / "todo.txt").read_text(encoding="utf-8") == "jauns saturs"


def test_execute_space_agent_tool_stages_workspace_write_when_approval_required(
    tmp_path: Path,
) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()
    staged_payloads: list[dict[str, object]] = []

    result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="write_workspace_file",
            arguments={"path": "notes.txt", "content": "jauns saturs"},
        ),
        context={
            "workspace_root": str(workspace),
            "require_workspace_approval": True,
            "task_mode": "code",
            "stage_workspace_write": lambda payload: (
                staged_payloads.append(payload)
                or {"proposal_id": "workspace-1", "status": "pending", "diff": "draft diff"}
            ),
        },
    )

    assert result["requires_approval"] is True
    assert result["saved"] is False
    assert result["saved_to_draft"] is True
    assert result["proposal_id"] == "workspace-1"
    assert result["diff"] == "draft diff"
    assert (workspace / "notes.txt").read_text(encoding="utf-8") == "jauns saturs"
    assert staged_payloads[0]["task_mode"] == "code"


def test_execute_space_agent_tool_stages_huggingface_write_when_approval_required() -> None:
    staged_payloads: list[dict[str, object]] = []

    result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="write_huggingface_repo_file",
            arguments={
                "repo_id": "MarisUK/maris.ai.agent",
                "repo_type": "space",
                "path": "README.md",
                "content": "jauns saturs",
                "commit_message": "Update README",
            },
        ),
        context={
            "require_publish_approval": True,
            "task_mode": "design",
            "stage_hf_write": lambda payload: (
                staged_payloads.append(payload)
                or {"proposal_id": "proposal-1", "status": "pending"}
            ),
        },
    )

    assert result["requires_approval"] is True
    assert result["saved"] is False
    assert result["proposal_id"] == "proposal-1"
    assert result["status"] == "pending"
    assert staged_payloads[0]["task_mode"] == "design"


def test_execute_space_agent_tool_runs_workspace_command_with_context_runner() -> None:
    result = execute_space_agent_tool(
        SpaceAgentToolCall(
            name="run_workspace_command",
            arguments={"command": "python -m pytest tests/test_space_agent.py"},
        ),
        context={
            "workspace_command_runner": lambda arguments: {
                "ok": True,
                "command_display": arguments["command"],
                "exit_code": 0,
            }
        },
    )

    assert result["ok"] is True
    assert result["command_display"] == "python -m pytest tests/test_space_agent.py"
    assert result["exit_code"] == 0


def test_execute_space_agent_tool_blocks_paths_outside_workspace(tmp_path: Path) -> None:
    workspace = tmp_path / "workspace"
    workspace.mkdir()

    with pytest.raises(ValueError):
        execute_space_agent_tool(
            SpaceAgentToolCall(name="read_workspace_file", arguments={"path": "../secret.txt"}),
            context={"workspace_root": str(workspace)},
        )


def test_generate_space_agent_reply_supports_tool_calling_roundtrip() -> None:
    request = SpaceAgentChatRequest(message="Kā man syncot Space?", tool_calling=True)

    response = generate_space_agent_reply(
        request,
        client_factory=_ToolCallingClient,
        tool_context={"training_status": {"running": False}},
    )

    assert response.used_fallback is False
    assert response.response == "Izmanto sync komandu un publicē Space profesionāli."
    assert response.task_mode == "chat"
    assert [tool_call.name for tool_call in response.tool_calls] == [
        "project_runtime",
        "sync_commands",
    ]
    assert any(event["type"] == "tool_call" for event in response.events)
    assert response.events[-1]["type"] == "final"


def test_generate_space_agent_reply_supports_multi_step_model_fix(
    monkeypatch, tmp_path: Path
) -> None:
    downloaded = tmp_path / "README.md"
    downloaded.write_text("vecs saturs", encoding="utf-8")
    staged: list[dict[str, object]] = []

    class FakeApi:
        def list_repo_files(self, **_: object):
            return ["README.md", "config.json"]

    monkeypatch.setattr("maris_core.space_agent._get_hf_api_client", lambda: FakeApi())
    monkeypatch.setattr(
        "maris_core.space_agent._download_hf_repo_file",
        lambda **_: str(downloaded),
    )

    response = generate_space_agent_reply(
        SpaceAgentChatRequest(message="Pārbaudi manu modeli un salabo to."),
        client_factory=_MultiStepToolCallingClient,
        tool_context={
            "require_publish_approval": True,
            "task_mode": "improve",
            "stage_hf_write": lambda payload: (
                staged.append(payload) or {"proposal_id": "approval-1", "status": "pending"}
            ),
        },
    )

    assert response.response == "Pārbaudīju modeli, salaboju README un saglabāju izmaiņas."
    assert [tool_call.name for tool_call in response.tool_calls] == [
        "list_huggingface_repo_files",
        "read_huggingface_repo_file",
        "write_huggingface_repo_file",
    ]
    assert staged[0]["repo_id"] == "MarisUK/maris-ai-master"
    assert staged[0]["path"] == "README.md"
    assert staged[0]["commit_message"] == "Fix model README"
    assert staged[0]["content"] == "salabots modelis"
    assert any(
        event["type"] == "tool_result" and event["tool_name"] == "write_huggingface_repo_file"
        for event in response.events
    )
    assert response.change_previews[0]["requires_approval"] is True


class _FailingClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token

    def chat_completion(self, **_: object) -> dict[str, object]:
        raise OSError("offline")


class _StopIterationChoices:
    def __bool__(self) -> bool:
        return True

    def __getitem__(self, index: int) -> object:
        raise StopIteration(index)

    def __iter__(self):
        return iter(())


class _MalformedChoicesClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token

    def chat_completion(self, **_: object) -> dict[str, object]:
        return {"choices": _StopIterationChoices()}


class _RetryAcrossModelsClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token
        self.chat_models: list[str] = []

    def chat_completion(self, **kwargs: object) -> dict[str, object]:
        model = str(kwargs["model"])
        self.chat_models.append(model)
        if model == "broken/model":
            raise OSError("broken")
        return {
            "choices": [
                {
                    "message": {
                        "content": "Profesionāli pārbaudīju MarisUK saturu un varu turpināt ar labojumiem."
                    }
                }
            ]
        }


class _MissingChatCompletionClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token


class _RuntimeErrorChatClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token

    def chat_completion(self, **_: object) -> dict[str, object]:
        raise RuntimeError("provider rejected chat completion")


class _TextModelCompatibilityClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token
        self.captured_messages: list[list[dict[str, str]]] = []

    def chat_completion(self, **kwargs: object) -> dict[str, object]:
        self.captured_messages.append(list(kwargs["messages"]))  # type: ignore[arg-type]
        return {
            "choices": [
                {
                    "message": {
                        "content": json.dumps(
                            {
                                "mode": "final",
                                "response": "Strādāju tiešā teksta režīmā ar Maris modeli.",
                            },
                            ensure_ascii=False,
                        ),
                    }
                }
            ]
        }


class _ToolFailureRecoveryClient:
    def __init__(self, token: str | None = None) -> None:
        self.token = token
        self.calls = 0

    def chat_completion(self, **_: object) -> dict[str, object]:
        self.calls += 1
        if self.calls == 1:
            return {
                "choices": [
                    {
                        "message": {
                            "content": (
                                '{"mode":"tool","tool_calls":['
                                '{"name":"read_workspace_file","arguments":{"path":"README.md"}}'
                                "]}"
                            )
                        }
                    }
                ]
            }
        return {
            "choices": [
                {
                    "message": {
                        "content": (
                            '{"mode":"final","response":"Pārbaudīju rīka kļūdu un turpinu bez iekšējas kļūdas."}'
                        )
                    }
                }
            ]
        }


def test_space_agent_accepts_external_hf_model_ids() -> None:
    request = SpaceAgentChatRequest(message="Sakārto manu Space", model="MarisUK/maris-ai-master")

    assert request.model == "MarisUK/maris-ai-master"


def test_space_agent_rejects_malformed_model_ids() -> None:
    with pytest.raises(ValueError):
        SpaceAgentChatRequest(message="Sakārto manu Space", model="bad model")


def test_resolve_space_agent_models_uses_only_explicit_request_model(monkeypatch) -> None:
    monkeypatch.setenv("MARIS_AGENT_MODEL", "MarisUK/maris-ai-master")
    monkeypatch.setenv("MARIS_AGENT_MODELS", "meta-llama/Llama-3.3-70B-Instruct")

    models = resolve_space_agent_models("deepseek-ai/DeepSeek-V3.2")

    assert models == ("deepseek-ai/DeepSeek-V3.2",)


def test_generate_space_agent_reply_raises_when_chat_completion_returns_malformed_choices() -> None:
    request = SpaceAgentChatRequest(message="Pārbaudi manu Space konfigurāciju")

    with pytest.raises(RuntimeError, match="nevarēja pieslēgties modelim"):
        generate_space_agent_reply(request, client_factory=_MalformedChoicesClient)


def test_generate_space_agent_reply_uses_requested_model_without_hidden_retry() -> None:
    response = generate_space_agent_reply(
        SpaceAgentChatRequest(
            message="Auditē manu MarisUK Space un sagatavo profesionālu atbildi.",
            model="Qwen/Qwen3-Coder-480B-A35B-Instruct",
        ),
        client_factory=_RetryAcrossModelsClient,
    )

    assert response.model == "Qwen/Qwen3-Coder-480B-A35B-Instruct"
    assert response.used_fallback is False
    assert "Profesionāli pārbaudīju" in response.response


def test_generate_space_agent_reply_raises_when_chat_completion_is_missing() -> None:
    with pytest.raises(RuntimeError, match="nevarēja pieslēgties modelim"):
        generate_space_agent_reply(
            SpaceAgentChatRequest(message="Pārbaudi manu Space konfigurāciju"),
            client_factory=_MissingChatCompletionClient,
        )


def test_generate_space_agent_reply_raises_after_runtime_error_chat_completion() -> None:
    with pytest.raises(RuntimeError, match="provider rejected chat completion"):
        generate_space_agent_reply(
            SpaceAgentChatRequest(message="Pārbaudi manu Space konfigurāciju"),
            client_factory=_RuntimeErrorChatClient,
        )


def test_generate_space_agent_reply_surfaces_tool_errors_without_internal_failure() -> None:
    response = generate_space_agent_reply(
        SpaceAgentChatRequest(message="Nolasi README un pasaki ko redzi."),
        client_factory=_ToolFailureRecoveryClient,
    )

    assert response.response == "Pārbaudīju rīka kļūdu un turpinu bez iekšējas kļūdas."
    assert any(event["type"] == "tool_error" for event in response.events)
    assert any(tool_call.name == "read_workspace_file" for tool_call in response.tool_calls)


def test_maris_text_model_uses_simple_mode() -> None:
    client = _TextModelCompatibilityClient()
    response = generate_space_agent_reply(
        SpaceAgentChatRequest(
            message="Palīdzi man saprast Space konfigurāciju.",
            model="MarisUK/maris-ai-text",
            tool_calling=True,
        ),
        client_factory=lambda token=None: client,
    )

    assert response.response == "Strādāju tiešā teksta režīmā ar Maris modeli."
    assert response.tool_calls == []
    assert response.used_fallback is False
    assert any("vienkāršotu tiešās atbildes ceļu" in event["message"] for event in response.events)
    assert client.captured_messages
    assert "Atbildi tikai ar JSON" not in client.captured_messages[0][0]["content"]
    assert "Domā kā senior programmētājs" not in client.captured_messages[0][0]["content"]


def test_generate_space_agent_reply_raises_direct_model_error_on_failure() -> None:
    request = SpaceAgentChatRequest(message="Deploy manu agent space")

    with pytest.raises(RuntimeError, match="offline"):
        generate_space_agent_reply(
            request,
            client_factory=_FailingClient,
            tool_context={"training_status": {"progress": {"label": "Gaida startu"}}},
        )