fake_worker.py python
77 lines 2.5 KB
Raw
sha256:2313b6a6353294a0ca08ff579624198da250b99163a86215aed31a905912e360 Land Scooling Lab T0/T2 training contract and seven-tier tests Human minor ⚠ breaking 45 days ago
1 """In-process fake worker for the T2 training API contract."""
2
3 from __future__ import annotations
4
5 import hashlib
6 import importlib.resources
7 import json
8
9 from scooling_lab.contracts import TrainingJobStatus
10 from scooling_lab.store import ArtifactMetadata, TrainingJobRecord, TrainingJobStore, utc_now_iso
11
12 FIXTURE_DATASET_RESOURCE = "synthetic_training_dataset.jsonl"
13
14
15 def fixture_dataset_bytes() -> bytes:
16 """Read the committed synthetic fixture dataset from package resources."""
17
18 return (
19 importlib.resources.files("scooling_lab.fixtures")
20 .joinpath(FIXTURE_DATASET_RESOURCE)
21 .read_bytes()
22 )
23
24
25 def fixture_dataset_hash() -> str:
26 """Return the stable SHA-256 hash for the synthetic fixture dataset."""
27
28 return hashlib.sha256(fixture_dataset_bytes()).hexdigest()
29
30
31 def placeholder_artifact_hash(job: TrainingJobRecord, dataset_hash: str) -> str:
32 """Return a stable hash for the placeholder artifact metadata."""
33
34 payload = json.dumps(
35 {
36 "datasetHash": dataset_hash,
37 "jobId": job.id,
38 "modelId": job.request.model_id,
39 "placeholder": "scooling-lab-fake-artifact-v1",
40 "trainingParameters": dict(sorted(job.request.training_parameters.items())),
41 },
42 separators=(",", ":"),
43 sort_keys=True,
44 )
45 return hashlib.sha256(payload.encode("utf-8")).hexdigest()
46
47
48 class FakeTrainingWorker:
49 """Synchronous fake worker that performs no real training and writes no models."""
50
51 def __init__(self, store: TrainingJobStore) -> None:
52 """Bind the worker to a server-owned job store."""
53
54 self._store = store
55
56 def run_job(self, job_id: str) -> TrainingJobRecord:
57 """Move a queued job through running to succeeded and register metadata."""
58
59 job = self._store.get(job_id)
60 if job.status == TrainingJobStatus.SUCCEEDED:
61 return job
62 self._store.update_status(job_id, TrainingJobStatus.RUNNING)
63 running_job = self._store.get(job_id)
64 dataset_hash = fixture_dataset_hash()
65 artifact_hash = placeholder_artifact_hash(running_job, dataset_hash)
66 artifact_id = f"artifact_{artifact_hash[:24]}"
67 self._store.register_artifact(
68 job_id,
69 ArtifactMetadata(
70 id=artifact_id,
71 job_id=job_id,
72 dataset_hash=dataset_hash,
73 artifact_hash=artifact_hash,
74 created_at=utc_now_iso(),
75 ),
76 )
77 return self._store.update_status(job_id, TrainingJobStatus.SUCCEEDED)
File History 1 commit
sha256:2313b6a6353294a0ca08ff579624198da250b99163a86215aed31a905912e360 Land Scooling Lab T0/T2 training contract and seven-tier tests Human minor 45 days ago