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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
513
"""Human-in-the-loop Space helpers for staging training artifacts."""

from __future__ import annotations

import json
import os
from datetime import UTC, datetime
from pathlib import Path
from typing import Any
from uuid import uuid4

from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator

from maris_core.data.quality import (
    DatasetQualityGateConfig,
    apply_quality_gate_to_records,
    build_dataset_quality_report,
)
from maris_core.training.preferences import (
    PreferenceExample,
    build_blind_side_by_side_artifact,
    build_human_eval_summary,
    summarize_preference_dataset,
)
from maris_core.training.space_ui import SAFE_OUTPUT_SEGMENT_RE
from maris_core.utils.env import validate_hf_model, validate_hf_repo_id

HUMAN_TRAINING_STAGE_DIRNAME = "human-training-staging"
HUMAN_TRAINING_REPO_PREFIX = "human-training"
HUMAN_TRAINING_MIN_TEXT_CHARS = 24
HUMAN_TRAINING_BLIND_REVIEW_SEED = 7


def _timestamp() -> str:
    return datetime.now(UTC).replace(microsecond=0).isoformat()


def _new_run_id() -> str:
    timestamp = datetime.now(UTC).strftime("%Y%m%dT%H%M%SZ")
    return f"{timestamp}-{uuid4().hex[:8]}"


def _validate_output_subdir(value: str) -> str:
    normalized = value.strip().strip("/")
    if not normalized:
        raise ValueError("Output apakšdirektorija nedrīkst būt tukša.")
    parts = Path(normalized).parts
    if ".." in parts or not SAFE_OUTPUT_SEGMENT_RE.fullmatch(normalized):
        raise ValueError("Output apakšdirektorijā drīkst būt tikai droši ceļa segmenti.")
    return normalized


def _normalize_lines(values: list[str]) -> list[str]:
    normalized: list[str] = []
    seen: set[str] = set()
    for value in values:
        text = str(value or "").strip()
        if not text:
            continue
        signature = text.casefold()
        if signature in seen:
            continue
        seen.add(signature)
        normalized.append(text)
    return normalized


class HumanTrainingConversationExample(BaseModel):
    model_config = ConfigDict(str_strip_whitespace=True)

    user: str = Field(min_length=1)
    assistant: str = Field(min_length=1)
    context: str = ""


class HumanTrainingPreferencePair(BaseModel):
    model_config = ConfigDict(str_strip_whitespace=True)

    prompt: str = Field(min_length=1)
    chosen: str = Field(min_length=1)
    rejected: str = Field(min_length=1)
    context: str = ""
    confidence: float | None = Field(default=None, ge=0.0, le=1.0)


class HumanTrainingEvalExample(BaseModel):
    model_config = ConfigDict(str_strip_whitespace=True)

    prompt: str = Field(min_length=1)
    completion: str = Field(min_length=1)
    context: str = ""


class HumanTrainingRequest(BaseModel):
    """Request payload for building staged human-training artifacts."""

    model_config = ConfigDict(str_strip_whitespace=True)

    dataset_repo: str
    model_repo: str = ""
    hub_model_id: str = ""
    model_preset: str = "balanced"
    model_name: str = ""
    num_epochs: int = Field(default=3, ge=1, le=100)
    all_branches: bool = False
    push_to_hub: bool = True
    output_subdir: str = "maris-human-training"
    continue_from_latest_artifact: bool = True
    continue_model_path: str = ""
    profile_facts: list[str] = Field(default_factory=list)
    profile_preferences: list[str] = Field(default_factory=list)
    response_instructions: list[str] = Field(default_factory=list)
    conversation_examples: list[HumanTrainingConversationExample] = Field(default_factory=list)
    preference_pairs: list[HumanTrainingPreferencePair] = Field(default_factory=list)
    eval_examples: list[HumanTrainingEvalExample] = Field(default_factory=list)

    @field_validator("dataset_repo")
    @classmethod
    def validate_dataset_repo(cls, value: str) -> str:
        try:
            return validate_hf_repo_id(value, "dataset_repo", label="dataset repo")
        except RuntimeError as exc:
            raise ValueError(str(exc)) from exc

    @field_validator("model_repo")
    @classmethod
    def validate_model_repo(cls, value: str) -> str:
        normalized = value.strip()
        if not normalized:
            return ""
        try:
            return validate_hf_model(normalized, "model_repo")
        except RuntimeError as exc:
            raise ValueError(str(exc)) from exc

    @field_validator("hub_model_id")
    @classmethod
    def validate_hub_model_id(cls, value: str) -> str:
        normalized = value.strip()
        if not normalized:
            return ""
        try:
            return validate_hf_model(normalized, "hub_model_id")
        except RuntimeError as exc:
            raise ValueError(str(exc)) from exc

    @field_validator("model_name")
    @classmethod
    def validate_model_name(cls, value: str) -> str:
        normalized = value.strip()
        if not normalized:
            return ""
        try:
            return validate_hf_model(normalized, "model_name")
        except RuntimeError as exc:
            raise ValueError(str(exc)) from exc

    @field_validator("output_subdir")
    @classmethod
    def validate_output_subdir(cls, value: str) -> str:
        return _validate_output_subdir(value)

    @field_validator("continue_model_path")
    @classmethod
    def validate_continue_model_path(cls, value: str) -> str:
        normalized = value.strip()
        if not normalized:
            return ""
        return _validate_output_subdir(normalized)

    @field_validator("profile_facts", "profile_preferences", "response_instructions")
    @classmethod
    def dedupe_lines(cls, value: list[str]) -> list[str]:
        return _normalize_lines(value)

    @model_validator(mode="after")
    def validate_has_training_signal(self) -> HumanTrainingRequest:
        resolved_model_repo = self.hub_model_id or self.model_repo
        if not resolved_model_repo:
            raise ValueError("Jānorāda hub_model_id vai model_repo.")
        self.hub_model_id = resolved_model_repo
        self.model_repo = resolved_model_repo
        if not any(
            (
                self.profile_facts,
                self.profile_preferences,
                self.response_instructions,
                self.conversation_examples,
                self.preference_pairs,
                self.eval_examples,
            )
        ):
            raise ValueError("Human training pieprasījumā jābūt vismaz vienam ievades blokam.")
        if not self.model_name and not self.model_preset:
            self.model_preset = "balanced"
        return self


class HumanTrainingExecuteRequest(BaseModel):
    model_config = ConfigDict(str_strip_whitespace=True)

    run_id: str = Field(min_length=6)
    publish_artifacts: bool = True
    start_training: bool = False

    @model_validator(mode="after")
    def validate_execution_flags(self) -> HumanTrainingExecuteRequest:
        if self.start_training and not self.publish_artifacts:
            raise ValueError(
                "Lai sāktu treniņu, artefakti vispirms jāpublicē dataset repozitorijā."
            )
        return self


class HumanTrainingLaunchSpec(BaseModel):
    model_config = ConfigDict(str_strip_whitespace=True)

    dataset_repo: str
    model_repo: str
    hub_model_id: str = ""
    model_preset: str = ""
    model_name: str = ""
    num_epochs: int
    all_branches: bool
    push_to_hub: bool
    output_subdir: str
    continue_from_latest_artifact: bool = True
    continue_model_path: str = ""

    @model_validator(mode="after")
    def validate_model_target(self) -> HumanTrainingLaunchSpec:
        resolved_model_repo = self.hub_model_id or self.model_repo
        if not resolved_model_repo:
            raise ValueError("Jānorāda hub_model_id vai model_repo.")
        self.hub_model_id = resolved_model_repo
        self.model_repo = resolved_model_repo
        return self


def resolve_human_training_stage_dir(persistent_dir: str, run_id: str) -> Path:
    root = Path(persistent_dir).expanduser().resolve()
    target = (root / HUMAN_TRAINING_STAGE_DIRNAME / run_id).resolve()
    if os.path.commonpath([str(root), str(target)]) != str(root):
        raise ValueError(
            "Human training staging direktorijai jāatrodas persistent storage ietvaros."
        )
    return target


def stage_human_training_artifacts(
    request: HumanTrainingRequest,
    *,
    persistent_dir: str,
) -> dict[str, Any]:
    run_id = _new_run_id()
    stage_dir = resolve_human_training_stage_dir(persistent_dir, run_id)
    stage_dir.mkdir(parents=True, exist_ok=True)

    train_records = _build_train_records(request)
    eval_records = _build_eval_records(request)
    preference_dataset = _build_preference_dataset(request)

    quality_config = DatasetQualityGateConfig(min_text_chars=HUMAN_TRAINING_MIN_TEXT_CHARS)
    filtered_train, train_report = apply_quality_gate_to_records(
        train_records,
        split_name="train",
        config=quality_config,
    )
    filtered_eval, eval_report = (
        apply_quality_gate_to_records(
            eval_records,
            split_name="eval",
            config=quality_config,
        )
        if eval_records
        else ([], None)
    )
    quality_report = build_dataset_quality_report(
        config=quality_config,
        train_report=train_report,
        eval_report=eval_report,
    ).to_dict()

    preference_examples = [PreferenceExample(**item) for item in preference_dataset["preferences"]]
    preference_summary = (
        summarize_preference_dataset(preference_examples) if preference_examples else None
    )
    blind_review = (
        build_blind_side_by_side_artifact(
            preference_examples,
            seed=HUMAN_TRAINING_BLIND_REVIEW_SEED,
        )
        if preference_examples
        else None
    )
    human_eval_summary = (
        build_human_eval_summary(preference_examples) if preference_examples else None
    )

    artifact_specs: dict[str, dict[str, Any]] = {
        "train_dataset": {
            "repo_path": f"data/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/train.json",
            "payload": filtered_train,
            "record_count": len(filtered_train),
        },
        "dataset_quality_report": {
            "repo_path": f"artifacts/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/dataset-quality-report.json",
            "payload": quality_report,
            "record_count": quality_report["kept_records"],
        },
    }
    if filtered_eval:
        artifact_specs["eval_dataset"] = {
            "repo_path": f"artifacts/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/eval.json",
            "payload": filtered_eval,
            "record_count": len(filtered_eval),
        }
    if preference_dataset["preferences"]:
        artifact_specs["preference_dataset"] = {
            "repo_path": f"artifacts/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/preferences.json",
            "payload": preference_dataset,
            "record_count": len(preference_dataset["preferences"]),
        }
    if preference_summary is not None:
        artifact_specs["preference_summary"] = {
            "repo_path": f"artifacts/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/preference-summary.json",
            "payload": preference_summary,
            "record_count": preference_summary["total_examples"],
        }
    if blind_review is not None:
        artifact_specs["blind_review"] = {
            "repo_path": f"artifacts/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/blind-review.json",
            "payload": blind_review,
            "record_count": blind_review["total_pairs"],
        }
    if human_eval_summary is not None:
        artifact_specs["human_eval_summary"] = {
            "repo_path": f"artifacts/{HUMAN_TRAINING_REPO_PREFIX}/{run_id}/human-eval-summary.json",
            "payload": human_eval_summary,
            "record_count": human_eval_summary["total_examples"],
        }

    artifacts: dict[str, dict[str, Any]] = {}
    for name, spec in artifact_specs.items():
        local_path = stage_dir / Path(spec["repo_path"]).name
        _write_json(local_path, spec["payload"])
        artifacts[name] = {
            "local_path": str(local_path),
            "repo_path": spec["repo_path"],
            "record_count": spec["record_count"],
        }

    launch_spec = HumanTrainingLaunchSpec(
        dataset_repo=request.dataset_repo,
        model_repo=request.model_repo,
        hub_model_id=request.hub_model_id,
        model_preset="" if request.model_name else request.model_preset,
        model_name=request.model_name,
        num_epochs=request.num_epochs,
        all_branches=request.all_branches,
        push_to_hub=request.push_to_hub,
        output_subdir=request.output_subdir,
        continue_from_latest_artifact=request.continue_from_latest_artifact,
        continue_model_path=request.continue_model_path,
    )
    manifest = {
        "artifact_type": "human-training-manifest",
        "run_id": run_id,
        "staged_at": _timestamp(),
        "stage_dir": str(stage_dir),
        "dataset_repo": request.dataset_repo,
        "model_repo": request.model_repo,
        "input_summary": {
            "profile_facts": len(request.profile_facts),
            "profile_preferences": len(request.profile_preferences),
            "response_instructions": len(request.response_instructions),
            "conversation_examples": len(request.conversation_examples),
            "preference_pairs": len(request.preference_pairs),
            "eval_examples": len(request.eval_examples),
        },
        "quality_report": quality_report,
        "artifacts": artifacts,
        "training_request": launch_spec.model_dump(),
        "ready_for_review": True,
        "ready_for_training": bool(filtered_train),
    }
    _write_json(stage_dir / "manifest.json", manifest)
    return manifest


def load_human_training_manifest(persistent_dir: str, run_id: str) -> dict[str, Any]:
    manifest_path = resolve_human_training_stage_dir(persistent_dir, run_id) / "manifest.json"
    if not manifest_path.is_file():
        raise FileNotFoundError(f"Human training staging ieraksts {run_id} nav atrasts.")
    return json.loads(manifest_path.read_text(encoding="utf-8"))


def publish_human_training_artifacts(
    manifest: dict[str, Any],
    *,
    save_file: Any,
) -> list[dict[str, Any]]:
    published: list[dict[str, Any]] = []
    dataset_repo = str(manifest["dataset_repo"])
    run_id = str(manifest["run_id"])
    for artifact_name, artifact in manifest.get("artifacts", {}).items():
        repo_path = artifact.get("repo_path")
        local_path = artifact.get("local_path")
        if not isinstance(repo_path, str) or not isinstance(local_path, str):
            continue
        content = Path(local_path).read_text(encoding="utf-8")
        result = save_file(
            repo_id=dataset_repo,
            repo_type="dataset",
            path_in_repo=repo_path,
            content=content,
            commit_message=f"Add human training artifacts for {run_id}",
        )
        published.append({"artifact": artifact_name, **result})
    return published


def build_human_training_launch_spec(manifest: dict[str, Any]) -> HumanTrainingLaunchSpec:
    return HumanTrainingLaunchSpec.model_validate(manifest["training_request"])


def _build_train_records(request: HumanTrainingRequest) -> list[dict[str, Any]]:
    records: list[dict[str, Any]] = []
    profile_record = _build_profile_record(request)
    if profile_record is not None:
        records.append(profile_record)
    for example in request.conversation_examples:
        record = {
            "user": example.user,
            "assistant": example.assistant,
            "metadata": {"source": "human_training_space"},
        }
        if example.context:
            record["context"] = example.context
        records.append(record)
    return records


def _build_profile_record(request: HumanTrainingRequest) -> dict[str, Any] | None:
    sections: list[str] = []
    if request.profile_facts:
        sections.append(
            "Fakti par lietotāju:\n" + "\n".join(f"- {item}" for item in request.profile_facts)
        )
    if request.profile_preferences:
        sections.append(
            "Lietotāja preferences:\n"
            + "\n".join(f"- {item}" for item in request.profile_preferences)
        )
    if request.response_instructions:
        sections.append(
            "Atbildēšanas instrukcijas:\n"
            + "\n".join(f"- {item}" for item in request.response_instructions)
        )
    if not sections:
        return None
    return {
        "prompt": "Iegaumē šo lietotāja profilu un atbildē saskaņā ar to turpmākajās sarunās.",
        "completion": "\n\n".join(sections),
        "metadata": {
            "source": "human_training_space",
            "artifact": "profile_memory",
        },
    }


def _build_eval_records(request: HumanTrainingRequest) -> list[dict[str, Any]]:
    records: list[dict[str, Any]] = []
    for example in request.eval_examples:
        record = {
            "prompt": example.prompt,
            "completion": example.completion,
            "metadata": {"source": "human_training_space", "artifact": "eval"},
        }
        if example.context:
            record["context"] = example.context
        records.append(record)
    return records


def _build_preference_dataset(request: HumanTrainingRequest) -> dict[str, Any]:
    preferences: list[dict[str, Any]] = []
    for index, pair in enumerate(request.preference_pairs, start=1):
        item = {
            "prompt": pair.prompt,
            "chosen": pair.chosen,
            "rejected": pair.rejected,
            "context": pair.context,
            "source": "human_training_space",
            "source_type": "real_reviewer",
            "reviewer_segment": "self-training",
            "preference_outcome": "chosen",
            "confidence": pair.confidence,
            "pair_id": f"human-training-{index:04d}",
            "blind": True,
            "production_like": True,
            "tags": ["human-training"],
        }
        preferences.append(item)
    return {
        "artifact_type": "human-training-preferences",
        "preferences": preferences,
    }


def _write_json(path: Path, payload: Any) -> None:
    path.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")