-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathclient.py
More file actions
725 lines (654 loc) · 25 KB
/
Copy pathclient.py
File metadata and controls
725 lines (654 loc) · 25 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
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
"""
HTTP client for the PAW API.
Handles compilation, program download, and authentication.
"""
from __future__ import annotations
import json
import os
import shutil
import stat
import tempfile
import time
import zipfile
from dataclasses import dataclass
from pathlib import Path, PurePosixPath
from typing import Optional, TypedDict, cast
import httpx
from . import config
from ._output import ProgressCallback, report_progress
MAX_PAW_ARCHIVE_BYTES = 256 * 1024 * 1024
MAX_PAW_ARCHIVE_MEMBERS = 256
MAX_PAW_EXPANDED_BYTES = 512 * 1024 * 1024
@dataclass
class Program:
"""Result of a compilation."""
id: str
status: str
slug: Optional[str] = None
compiler_snapshot: Optional[str] = None
compiler_kind: Optional[str] = None
pseudo_program_strategy: Optional[str] = None
runtime_id: Optional[str] = None
runtime_manifest_version: Optional[int] = None
timings: Optional[dict] = None
error: Optional[str] = None
version: Optional[int] = None
version_action: Optional[str] = None
class CompilePrecheck(TypedDict):
"""Result returned by :meth:`PAWClient.precheck_compile`."""
cached: bool
program_id: str | None
compiler_snapshot: str | None
compiler_kind: str | None
queue_length: int | None
estimated_wait_s: float | None
class CompileJob(TypedDict):
"""Queued compile returned by :meth:`PAWClient.compile_async`."""
job_id: str
status: str
program_id: str | None
compiler_snapshot: str | None
compiler_kind: str | None
pseudo_program: str | None
pseudo_program_strategy: str | None
runtime_id: str | None
runtime_manifest_version: int | None
queue_position: int | None
estimated_wait_s: float | None
timings: dict | None
error: str | None
cached: bool
slug: str | None
version: int | None
version_action: str | None
class CompileStatus(TypedDict):
"""Current state returned by :meth:`PAWClient.get_compile_status`."""
job_id: str
status: str
program_id: str | None
compiler_snapshot: str | None
compiler_kind: str | None
error: str | None
created_at: str | None
completed_at: str | None
percent: float | None
pseudo_program: str | None
pseudo_program_strategy: str | None
runtime_id: str | None
runtime_manifest_version: int | None
cached: bool
queue_length: int | None
estimated_wait_s: float | None
slug: str | None
version: int | None
version_action: str | None
class CompileCancellation(TypedDict):
"""Cancellation acknowledgement returned by the compile API."""
job_id: str
status: str
class PAWClient:
"""HTTP client for the PAW API."""
def __init__(self, api_url: str | None = None, api_key: str | None = None):
self._api_url = (api_url or config.get_api_url()).rstrip("/")
self._api_key = api_key or config.get_api_key()
def _headers(self) -> dict:
h = {"Content-Type": "application/json"}
if self._api_key:
h["X-API-Key"] = self._api_key
return h
@staticmethod
def _compile_body(
spec: str,
compiler: str | None = None,
name: str | None = None,
tags: list[str] | None = None,
public: bool = True,
slug: str | None = None,
ephemeral: bool = False,
) -> dict:
"""Build the shared request body for sync and async compilation."""
body: dict = {"spec": spec, "public": public}
if compiler:
body["compiler"] = compiler
if ephemeral:
body["ephemeral"] = True
if name:
body["name"] = name
if tags:
body["tags"] = tags
if slug:
body["slug"] = slug
return body
def compile(
self,
spec: str,
compiler: str | None = None,
name: str | None = None,
tags: list[str] | None = None,
public: bool = True,
slug: str | None = None,
ephemeral: bool = False,
) -> Program:
"""Compile a spec into a neural program on the server.
Args:
spec: Natural language specification. Include examples in the text.
compiler: Compiler name (alias or snapshot).
name: Human-readable program name (display title).
tags: Tags for hub discovery.
public: Whether to list on the public hub.
slug: URL-safe handle (e.g. 'message-classifier'). Creates a
``username/slug`` alias for easy reference. Requires auth.
Returns:
Program with id, slug, status, and timings.
Raises:
httpx.HTTPStatusError: On API errors (422 for validation, 429 for rate limit).
"""
body = self._compile_body(
spec,
compiler=compiler,
name=name,
tags=tags,
public=public,
slug=slug,
ephemeral=ephemeral,
)
resp = httpx.post(
f"{self._api_url}/api/v1/compile",
json=body,
headers=self._headers(),
# Preserve the compatibility sync path: a finetune may use its
# 1900s provider budget plus 330s of artifact finalization.
# New long-running callers should prefer compile_async().
timeout=2400.0,
)
resp.raise_for_status()
data = resp.json()
return Program(
id=data.get("program_id", ""),
status=data.get("status", "unknown"),
slug=data.get("slug"),
compiler_snapshot=data.get("compiler_snapshot"),
compiler_kind=data.get("compiler_kind"),
pseudo_program_strategy=data.get("pseudo_program_strategy"),
runtime_id=data.get("runtime_id"),
runtime_manifest_version=data.get("runtime_manifest_version"),
timings=data.get("timings"),
error=data.get("error"),
version=data.get("version"),
version_action=data.get("version_action"),
)
def precheck_compile(
self,
spec: str,
compiler: str | None = None,
) -> CompilePrecheck:
"""Check cache and queue state without submitting a compile."""
body: dict = {"spec": spec}
if compiler:
body["compiler"] = compiler
resp = httpx.post(
f"{self._api_url}/api/v1/compile/precheck",
json=body,
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return cast(CompilePrecheck, resp.json())
def compile_async(
self,
spec: str,
compiler: str,
name: str | None = None,
tags: list[str] | None = None,
public: bool = True,
slug: str | None = None,
ephemeral: bool = False,
) -> CompileJob:
"""Queue a long-running compile and return its job metadata.
This is a synchronous HTTP call that submits work to the asynchronous
compile endpoint. Poll :meth:`get_compile_status` for progress.
``compiler`` is required because this endpoint only accepts explicit
finetune compilers.
"""
if not isinstance(compiler, str) or not compiler.strip():
raise ValueError(
"compile_async requires an explicit finetune compiler."
)
body = self._compile_body(
spec,
compiler=compiler,
name=name,
tags=tags,
public=public,
slug=slug,
ephemeral=ephemeral,
)
resp = httpx.post(
f"{self._api_url}/api/v1/compile/async",
json=body,
headers=self._headers(),
timeout=30.0,
)
resp.raise_for_status()
return cast(CompileJob, resp.json())
def get_compile_status(self, job_id: str) -> CompileStatus:
"""Get live status for an asynchronous compile job."""
resp = httpx.get(
f"{self._api_url}/api/v1/compile/{job_id}",
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return cast(CompileStatus, resp.json())
def cancel_compile(self, job_id: str) -> CompileCancellation:
"""Cancel a queued compile job.
The server returns HTTP 409 if the job has already started.
"""
resp = httpx.delete(
f"{self._api_url}/api/v1/compile/{job_id}",
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return cast(CompileCancellation, resp.json())
def resolve_slug(self, slug: str) -> str:
"""Resolve a human-readable slug to a program ID."""
resp = httpx.get(
f"{self._api_url}/api/v1/programs/resolve/{slug}",
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return resp.json()["program_id"]
def download_paw(
self,
program_id: str,
progress: ProgressCallback | None = None,
) -> Path:
"""Download a .paw bundle to the local cache.
Returns the path to the extracted program directory.
Handles 202 Accepted (assets still generating) and 302 redirects (HF CDN).
"""
from . import cache
if not isinstance(program_id, str) or not cache.is_program_id(program_id):
raise ValueError(f"Invalid program ID: {program_id!r}")
program_dir = config.get_programs_dir() / program_id
if cache.has_valid_program_assets(program_id):
report_progress(
progress,
{
"stage": "program",
"status": "cached",
"program_id": program_id,
"path": str(program_dir),
},
)
return program_dir
with cache.program_cache_lock(program_id):
if cache.has_valid_program_assets(program_id):
report_progress(
progress,
{
"stage": "program",
"status": "cached",
"program_id": program_id,
"path": str(program_dir),
},
)
return program_dir
report_progress(
progress,
{
"stage": "program",
"status": "downloading",
"program_id": program_id,
"path": str(program_dir),
},
f"Downloading program {program_id[:12]}...",
)
max_wait = 60
elapsed = 0
waiting_logged = False
programs_dir = config.get_programs_dir()
staging_parent = programs_dir / ".staging"
staging_parent.mkdir(parents=True, exist_ok=True)
staging_root = None
try:
while elapsed < max_wait:
retry_after = None
downloaded = False
with httpx.stream(
"GET",
f"{self._api_url}/api/v1/programs/{program_id}/download",
headers=self._headers(),
timeout=60.0,
follow_redirects=True,
) as resp:
if resp.status_code == 202:
if not waiting_logged:
report_progress(
progress,
{
"stage": "program",
"status": "waiting",
"program_id": program_id,
"path": str(program_dir),
},
"Waiting for program to be ready...",
)
waiting_logged = True
retry_after = max(
1,
int(resp.headers.get("Retry-After", "3")),
)
elif resp.status_code == 404:
try:
resp.read()
detail = resp.json().get("detail", "")
except Exception:
detail = getattr(resp, "text", "")
if "not found" in str(detail).lower():
raise RuntimeError(
f"Program {program_id} not found on server."
)
if elapsed < max_wait - 3:
if not waiting_logged:
report_progress(
progress,
{
"stage": "program",
"status": "waiting",
"program_id": program_id,
"path": str(program_dir),
},
"Waiting for program to be ready...",
)
waiting_logged = True
retry_after = 3
else:
resp.raise_for_status()
else:
resp.raise_for_status()
staging_root = Path(
tempfile.mkdtemp(
prefix=f"{program_id}.",
dir=str(staging_parent),
)
)
paw_path = staging_root / f"{program_id}.paw"
self._stream_response_to_file(resp, paw_path)
downloaded = True
if downloaded:
break
if retry_after is not None:
time.sleep(retry_after)
elapsed += retry_after
continue
raise RuntimeError(
f"Program {program_id} download did not return assets."
)
else:
raise RuntimeError(
f"Program {program_id} assets not ready after {max_wait}s. "
"The program may still be generating. Try again shortly."
)
if staging_root is None:
raise RuntimeError(
f"Program {program_id} download produced no archive."
)
paw_path = staging_root / f"{program_id}.paw"
extracted_dir = staging_root / "extracted"
extracted_dir.mkdir()
self._safe_extract_paw(paw_path, extracted_dir)
if not cache.validate_program_assets_dir(
extracted_dir,
program_id,
):
raise RuntimeError(
f"Downloaded program {program_id} has missing, "
"malformed, or mismatched staged assets."
)
self._hydrate_runtime_manifest(extracted_dir)
if not cache.validate_program_assets_dir(
extracted_dir,
program_id,
):
raise RuntimeError(
f"Downloaded program {program_id} became invalid while "
"hydrating its runtime metadata."
)
self._install_staged_program(
extracted_dir,
program_dir,
program_id,
)
finally:
if staging_root is not None:
shutil.rmtree(staging_root, ignore_errors=True)
if not cache.has_valid_program_assets(program_id):
raise RuntimeError(
f"Installed program {program_id} failed strict asset "
"validation."
)
report_progress(
progress,
{
"stage": "program",
"status": "ready",
"program_id": program_id,
"path": str(program_dir),
},
)
return program_dir
@staticmethod
def _validate_stream_content_length(resp) -> int | None:
declared = resp.headers.get("content-length")
if declared is None:
declared = resp.headers.get("Content-Length")
if declared is None:
return None
try:
declared_size = int(declared)
except (TypeError, ValueError) as exc:
raise ValueError(
f"Invalid program archive Content-Length: {declared!r}"
) from exc
if declared_size < 0 or declared_size > MAX_PAW_ARCHIVE_BYTES:
raise ValueError(
f"Program archive is too large: {declared_size} bytes "
f"(limit {MAX_PAW_ARCHIVE_BYTES})."
)
return declared_size
@staticmethod
def _stream_response_to_file(resp, destination: Path) -> int:
PAWClient._validate_stream_content_length(resp)
downloaded = 0
with open(destination, "xb") as output:
for chunk in resp.iter_bytes(chunk_size=64 * 1024):
if not chunk:
continue
next_size = downloaded + len(chunk)
if next_size > MAX_PAW_ARCHIVE_BYTES:
raise ValueError(
f"Program archive is too large: more than "
f"{MAX_PAW_ARCHIVE_BYTES} bytes."
)
output.write(chunk)
downloaded = next_size
output.flush()
os.fsync(output.fileno())
return downloaded
@staticmethod
def _safe_extract_paw(paw_path: Path, destination: Path) -> None:
"""Extract regular archive members without traversal or symlinks."""
if paw_path.stat().st_size > MAX_PAW_ARCHIVE_BYTES:
raise ValueError(
f"Program archive exceeds {MAX_PAW_ARCHIVE_BYTES} bytes."
)
seen: set[str] = set()
try:
archive = zipfile.ZipFile(paw_path)
except (OSError, zipfile.BadZipFile) as exc:
raise RuntimeError(f"Invalid .paw archive: {exc}") from exc
with archive:
members = archive.infolist()
if len(members) > MAX_PAW_ARCHIVE_MEMBERS:
raise ValueError(
f"Program archive has {len(members)} members "
f"(limit {MAX_PAW_ARCHIVE_MEMBERS})."
)
expanded_size = sum(info.file_size for info in members)
if expanded_size > MAX_PAW_EXPANDED_BYTES:
raise ValueError(
f"Program archive expands to {expanded_size} bytes "
f"(limit {MAX_PAW_EXPANDED_BYTES})."
)
for info in members:
member = info.filename
pure_path = PurePosixPath(member)
parts = pure_path.parts
if (
not member
or "\\" in member
or pure_path.is_absolute()
or not parts
or ":" in parts[0]
or any(part in ("", ".", "..") for part in parts)
):
raise ValueError(
f"Unsafe path in .paw archive: {member!r}"
)
normalized = "/".join(parts)
if normalized in seen:
raise ValueError(
f"Duplicate path in .paw archive: {member!r}"
)
seen.add(normalized)
unix_mode = (info.external_attr >> 16) & 0xFFFF
file_type = stat.S_IFMT(unix_mode)
if file_type and not (
stat.S_ISREG(unix_mode) or stat.S_ISDIR(unix_mode)
):
raise ValueError(
f"Non-regular path in .paw archive: {member!r}"
)
target = destination.joinpath(*parts)
try:
target.relative_to(destination)
except ValueError as exc:
raise ValueError(
f"Unsafe path in .paw archive: {member!r}"
) from exc
if info.is_dir():
target.mkdir(parents=True, exist_ok=False)
continue
target.parent.mkdir(parents=True, exist_ok=True)
with archive.open(info, "r") as source, open(
target,
"xb",
) as output:
shutil.copyfileobj(source, output)
@staticmethod
def _install_staged_program(
staged_dir: Path,
program_dir: Path,
program_id: str,
) -> None:
"""Atomically publish a complete staged directory under its ID."""
parent = program_dir.parent
backup = parent / (
f".{program_id}.replaced.{os.getpid()}.{time.time_ns()}"
)
moved_old = False
try:
if program_dir.exists() or program_dir.is_symlink():
os.replace(str(program_dir), str(backup))
moved_old = True
os.replace(str(staged_dir), str(program_dir))
except BaseException:
if moved_old and not program_dir.exists() and backup.exists():
os.replace(str(backup), str(program_dir))
raise
else:
if moved_old:
if backup.is_dir() and not backup.is_symlink():
shutil.rmtree(backup, ignore_errors=True)
else:
backup.unlink(missing_ok=True)
def _hydrate_runtime_manifest(self, program_dir: Path) -> None:
from . import cache
meta_path = program_dir / "meta.json"
if not meta_path.is_file() or meta_path.is_symlink():
return
try:
meta = json.loads(meta_path.read_text(encoding="utf-8"))
except (json.JSONDecodeError, OSError):
return
if not isinstance(meta, dict):
return
runtime_manifest = cache.resolve_runtime_manifest(
meta,
api_url=self._api_url,
api_key=self._api_key,
offline=False,
)
if runtime_manifest is None:
return
meta["runtime"] = runtime_manifest
meta.setdefault(
"runtime_id",
runtime_manifest.get("runtime_id"),
)
meta.setdefault(
"runtime_manifest_version",
runtime_manifest.get("manifest_version"),
)
cache._atomic_write_json(meta_path, meta)
def get_program_meta(self, program_id: str) -> dict:
"""Get program metadata from the server."""
resp = httpx.get(
f"{self._api_url}/api/v1/programs/{program_id}",
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return resp.json()
def get_runtime_manifest(self, runtime_id: str) -> dict:
"""Fetch a runtime manifest from the server and cache it locally."""
from . import cache
cached = cache.get_cached_runtime_manifest(runtime_id)
if cached:
return cached
return cache.fetch_runtime_manifest(
runtime_id,
api_url=self._api_url,
api_key=self._api_key,
)
def list_compilers(self) -> list[dict]:
"""List available compilers from the server."""
resp = httpx.get(
f"{self._api_url}/api/v1/models/compilers",
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return resp.json()["compilers"]
def list_slug_versions(self, slug: str) -> dict:
"""List all versions of a slug. Slug format: 'username/slug-name' or bare 'slug-name'."""
resp = httpx.get(
f"{self._api_url}/api/v1/programs/{slug}/versions",
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return resp.json()
def list_programs(self, sort: str = "recent", per_page: int = 20, page: int = 1) -> dict:
"""List programs for the authenticated user."""
resp = httpx.get(
f"{self._api_url}/api/v1/programs",
params={"mine": "true", "sort": sort, "per_page": per_page, "page": page},
headers=self._headers(),
timeout=10.0,
)
resp.raise_for_status()
return resp.json()