File size: 25,548 Bytes
39ff632
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
 
39ff632
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
39ff632
 
 
 
 
 
 
37e3d5a
39ff632
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
39ff632
 
bf1fb5f
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
39ff632
 
 
 
 
 
bf1fb5f
 
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
39ff632
 
 
 
 
 
 
 
 
37e3d5a
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
39ff632
37e3d5a
 
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
39ff632
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf1fb5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
39ff632
 
 
 
37e3d5a
 
 
 
 
 
39ff632
 
 
 
 
bf1fb5f
 
 
 
37e3d5a
 
 
 
 
 
39ff632
 
37e3d5a
 
 
bf1fb5f
 
 
 
 
39ff632
 
 
 
 
 
37e3d5a
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
39ff632
 
 
 
 
37e3d5a
39ff632
 
 
 
 
 
bf1fb5f
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf1fb5f
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
37e3d5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39ff632
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""FastAPI application for the img2threejs Hugging Face Docker Space.

Routes
  GET  /                                  SPA (static/index.html)
  GET  /static/*                          static assets
  GET  /health                            liveness + readiness signal
  GET  /api/config                        public, credential-free config view
  POST /api/jobs                          start a conversion job (multipart image)
  GET  /api/jobs/{job_id}                 job status snapshot
  GET  /api/jobs/{job_id}/events          SSE stream of pipeline events
  GET  /api/jobs/{job_id}/artifacts/{n}   whitelisted per-job artifacts
  GET  /api/gallery                       persistent community gallery
  GET  /api/gallery/{item_id}             gallery item detail
  GET  /api/gallery/{item_id}/artifacts/* immutable gallery artifacts

Binding: 0.0.0.0 on $PORT (default 7860) per the HF Docker Space contract.
One uvicorn worker: the job registry is in-memory by design.
"""

from __future__ import annotations

import os

# Keep every third-party cache in writable Space storage. These must be set
# before importing the web stack or any optional model/runtime dependency.
os.environ.setdefault("HF_HOME", "/tmp/.cache/huggingface")
os.environ.setdefault("HF_MODULES_CACHE", "/tmp/hf_modules")
os.environ.setdefault("MPLCONFIGDIR", "/tmp/matplotlib")

import asyncio
import json
import logging
import threading
import time
from pathlib import Path

from fastapi import FastAPI, Query, Request
from fastapi.responses import (FileResponse, JSONResponse, Response,
                               StreamingResponse)
from fastapi.staticfiles import StaticFiles
from starlette.datastructures import UploadFile
from starlette.types import ASGIApp, Receive, Scope, Send

from .config import load_settings
from .gallery import (
    GALLERY_ARTIFACT_NAMES,
    GalleryError,
    GalleryStore,
    valid_item_id,
)
from .image_guard import ImageRejected
from .llm import LLMClient
from .pipeline import ARTIFACT_NAMES, JobRegistry, fail_job_timeout, run_job
from .ratelimit import RateLimiter, client_key

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s %(message)s",
)
logger = logging.getLogger("img2threejs")

REPO_ROOT = Path(__file__).resolve().parents[1]
STATIC_DIR = Path(__file__).resolve().parent / "static"

settings = load_settings()
registry = JobRegistry(Path(settings.runs_dir))
gallery_store = GalleryStore(Path(settings.gallery_dir))
limiter = RateLimiter(settings.rate_limit_jobs_per_hour)
job_semaphore = asyncio.Semaphore(settings.max_concurrent_jobs)

# Multipart boundary + the small ``hint`` field. The image itself still has
# the tighter ``max_upload_bytes`` limit below.
MULTIPART_OVERHEAD_BYTES = 256 * 1024
QUEUE_PROGRESS_INTERVAL_S = 25.0
SUCCESS_ONLY_JOB_ARTIFACTS = frozenset({
    "compile-spec.json",
    "factory.ts",
    "model.bundle.js",
    "standalone.html",
})


class _RequestBodyTooLarge(Exception):
    pass


class UploadBodyLimitMiddleware:
    """Cap the /api/jobs request stream before multipart parsing.

    ``UploadFile`` avoids retaining large files in RAM, but without an ASGI
    stream cap a caller could still force the multipart parser to spool an
    unbounded request. Content-Length is a cheap early rejection; counting
    receive chunks enforces the same limit when that header is absent or
    false.
    """

    def __init__(self, app: ASGIApp) -> None:
        self.app = app

    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        if (
            scope["type"] != "http"
            or scope.get("method") != "POST"
            or scope.get("path") != "/api/jobs"
        ):
            await self.app(scope, receive, send)
            return

        body_limit = settings.max_upload_bytes + MULTIPART_OVERHEAD_BYTES
        declared_length: int | None = None
        for name, value in scope.get("headers", []):
            if name.lower() == b"content-length":
                try:
                    declared_length = int(value)
                except (TypeError, ValueError):
                    declared_length = None
                break
        if declared_length is not None and declared_length > body_limit:
            await self._reject(scope, receive, send, body_limit)
            return

        received = 0

        async def limited_receive():
            nonlocal received
            message = await receive()
            if message.get("type") == "http.request":
                received += len(message.get("body", b""))
                if received > body_limit:
                    raise _RequestBodyTooLarge
            return message

        try:
            await self.app(scope, limited_receive, send)
        except _RequestBodyTooLarge:
            await self._reject(scope, receive, send, body_limit)

    @staticmethod
    async def _reject(
        scope: Scope, receive: Receive, send: Send, body_limit: int
    ) -> None:
        response = JSONResponse(
            status_code=413,
            content={
                "error": "request_too_large",
                "detail": (
                    "The multipart request exceeds the configured upload "
                    f"boundary ({body_limit} bytes including form overhead)."
                ),
            },
        )
        await response(scope, receive, send)


# A reservation covers the period between the cheap admission check and
# JobRegistry.create(). It closes the race where many multipart bodies could
# all pass the queue check while awaiting parsing.
_queue_guard = threading.Lock()
_pending_uploads = 0


def _reserve_job_slot() -> tuple[bool, int]:
    global _pending_uploads
    with _queue_guard:
        in_flight = sum(1 for job in registry.jobs.values() if job.status == "running")
        occupied = in_flight + _pending_uploads
        if occupied >= settings.max_in_flight_jobs:
            return False, occupied
        _pending_uploads += 1
        return True, occupied


def _release_job_slot() -> None:
    global _pending_uploads
    with _queue_guard:
        _pending_uploads = max(0, _pending_uploads - 1)


def _create_reserved_job():
    """Atomically convert one upload reservation into a registry job."""
    global _pending_uploads
    with _queue_guard:
        job = registry.create()
        _pending_uploads = max(0, _pending_uploads - 1)
        return job


app = FastAPI(title="img2threejs", docs_url=None, redoc_url=None, openapi_url=None)
app.add_middleware(UploadBodyLimitMiddleware)
# Factory for the per-job LLM client; tests substitute a mock.
app.state.llm_factory = lambda s: LLMClient(s)  # noqa: E731

CSP = (
    "default-src 'self'; script-src 'self' blob:; style-src 'self' 'unsafe-inline'; "
    "img-src 'self' data: blob:; connect-src 'self'; frame-src 'self'; "
    "object-src 'none'; base-uri 'none'; "
    "frame-ancestors 'self' https://huggingface.co"
)

# The sandboxed viewer shell runs LLM-influenced code inside an inline module
# script and blob: imports — its own policy. Sending the app-wide CSP on this
# path would intersect with the document's meta policy and block the shell
# (multiple CSPs intersect; 'self' never matches an opaque origin anyway).
VIEWER_CSP = (
    "default-src 'none'; script-src 'unsafe-inline' blob:; "
    "style-src 'unsafe-inline'; img-src blob: data:; worker-src blob:; "
    "frame-ancestors 'self' https://huggingface.co; sandbox allow-scripts"
)
VIEWER_PATH = "/static/viewer.html"


@app.middleware("http")
async def security_headers(request: Request, call_next):
    response = await call_next(request)
    if request.url.path == VIEWER_PATH:
        response.headers["Content-Security-Policy"] = VIEWER_CSP
    else:
        response.headers.setdefault("Content-Security-Policy", CSP)
    response.headers.setdefault("X-Content-Type-Options", "nosniff")
    response.headers.setdefault("Referrer-Policy", "no-referrer")
    return response


@app.on_event("startup")
async def startup() -> None:
    Path(settings.runs_dir).mkdir(parents=True, exist_ok=True)
    try:
        gallery_store.ensure_ready()
    except GalleryError as exc:
        # Gallery storage is an optional post-generation destination. A
        # transient Bucket mount outage must not take down image conversion;
        # gallery APIs report 503 and publish attempts surface a warning until
        # the mount recovers.
        logger.warning("startup: community gallery unavailable: %s", exc)
    logger.info(
        "startup: llm_configured=%s port=%s space=%s",
        settings.llm_configured, settings.port, settings.space_id or "(local)",
    )

    async def reaper() -> None:
        while True:
            await asyncio.sleep(600)
            doomed = registry.reap(settings.job_ttl_s)
            if doomed:
                logger.info("reaper: evicted %d expired jobs", len(doomed))

    asyncio.create_task(reaper())


# ---------------------------------------------------------------------------
# Health + config
# ---------------------------------------------------------------------------

@app.get("/health")
async def health() -> JSONResponse:
    """Liveness + lightweight readiness. The LLM is deliberately NOT probed:
    a failing provider must not mark the container down — it is surfaced in
    the UI instead."""
    return JSONResponse({
        "status": "ok",
        "llm_configured": settings.llm_configured,
        "space_id": settings.space_id,
        "time": int(time.time()),
    })


@app.get("/api/config")
async def config() -> JSONResponse:
    """Public config view. Never includes the API key or any secret value."""
    return JSONResponse({
        "llm_configured": settings.llm_configured,
        "missing_llm_vars": settings.missing_llm_vars,
        "model": settings.llm_model if settings.llm_configured else None,
        "max_upload_bytes": settings.max_upload_bytes,
        "rate_limit_jobs_per_hour": settings.rate_limit_jobs_per_hour,
        "space_host": settings.space_host,
        "community_gallery": True,
        "share_default": True,
    })


# ---------------------------------------------------------------------------
# Jobs
# ---------------------------------------------------------------------------

_TRUE_FORM_VALUES = frozenset({"1", "true", "yes", "on"})
_FALSE_FORM_VALUES = frozenset({"0", "false", "no", "off"})


def _parse_share_preference(value: object) -> bool | None:
    """Parse the optional multipart share field.

    Missing means the intentionally public-by-default behaviour. ``None`` is
    reserved for malformed values so callers never silently invert a choice.
    """
    if value is None:
        return True
    if not isinstance(value, str):
        return None
    normalized = value.strip().lower()
    if normalized in _TRUE_FORM_VALUES:
        return True
    if normalized in _FALSE_FORM_VALUES:
        return False
    return None


async def _acquire_job_worker(job) -> None:
    """Wait for one worker with truthful elapsed feedback and a hard deadline."""
    deadline = job.created + settings.job_timeout_s
    while True:
        remaining = deadline - time.time()
        if remaining <= 0:
            raise TimeoutError
        interval = min(QUEUE_PROGRESS_INTERVAL_S, remaining)
        try:
            await asyncio.wait_for(job_semaphore.acquire(), timeout=interval)
            return
        except TimeoutError:
            if time.time() >= deadline:
                raise
            elapsed = max(1, round(time.time() - job.created))
            job.emit(
                "queued",
                "progress",
                f"Still queued: waiting {elapsed}s for a conversion worker.",
                elapsedSeconds=elapsed,
                shareRequested=bool(
                    job.events[0].get("data", {}).get("shareRequested", True)
                ),
            )


@app.post("/api/jobs", status_code=202)
async def create_job(request: Request) -> JSONResponse:
    if not settings.llm_configured:
        missing = ", ".join(settings.missing_llm_vars)
        return JSONResponse(status_code=503, content={
            "error": "llm_not_configured",
            "detail": (
                f"This Space needs vision-LLM credentials to author the sculpt spec: set "
                f"{missing} and LLM_BASE_URL as Space Secrets (Settings → Secrets), then "
                "restart the Space. No model was generated — results are never fabricated."
            ),
        })

    # Reserve capacity *before* parsing multipart data. Without this
    # reservation, concurrent requests can all retain/spool their complete
    # images while waiting to create an unbounded number of tasks.
    reserved, occupied = _reserve_job_slot()
    if not reserved:
        return JSONResponse(
            status_code=503,
            headers={"Retry-After": "60"},
            content={
                "error": "queue_full",
                "detail": (
                    f"The Space is busy ({occupied} jobs in flight, max "
                    f"{settings.max_in_flight_jobs}). Try again shortly."
                ),
            },
        )

    reservation_active = True
    try:
        # Uvicorn resolves forwarding headers only from configured trusted
        # proxies; do not reinterpret raw X-Forwarded-For in application code.
        key = client_key(request.client.host if request.client else None)
        retry_after = limiter.check(key)
        if retry_after is not None:
            return JSONResponse(
                status_code=429,
                headers={"Retry-After": str(retry_after)},
                content={
                    "error": "rate_limited",
                    "detail": (
                        f"At most {settings.rate_limit_jobs_per_hour} jobs per hour "
                        f"per client. Try again in {retry_after}s."
                    ),
                },
            )

        request_content_type = request.headers.get("content-type", "").lower()
        if request_content_type and not request_content_type.startswith("multipart/form-data"):
            return JSONResponse(status_code=415, content={
                "error": "unsupported_media_type",
                "detail": "Expected multipart form data containing an image file.",
            })

        # Parse only after admission. max_files/max_fields constrain multipart
        # metadata; UploadBodyLimitMiddleware constrains the complete stream.
        async with request.form(max_files=1, max_fields=3, max_part_size=64 * 1024) as form:
            file = form.get("file")
            if not isinstance(file, UploadFile):
                return JSONResponse(status_code=422, content={
                    "error": "file_required",
                    "detail": "A multipart image field named 'file' is required.",
                })
            declared = (file.content_type or "").lower()
            if declared and not declared.startswith("image/"):
                return JSONResponse(status_code=415, content={
                    "error": "unsupported_media_type",
                    "detail": "Expected an image upload (PNG, JPEG, WebP, GIF or BMP).",
                })
            raw = await file.read(settings.max_upload_bytes + 1)
            if len(raw) > settings.max_upload_bytes:
                return JSONResponse(status_code=413, content={
                    "error": "file_too_large",
                    "detail": (
                        "The upload exceeds the "
                        f"{settings.max_upload_bytes // (1024 * 1024)} MiB limit."
                    ),
                })
            hint_value = form.get("hint")
            hint = hint_value if isinstance(hint_value, str) else None
            share = _parse_share_preference(form.get("share"))
            if share is None:
                return JSONResponse(status_code=422, content={
                    "error": "invalid_share_preference",
                    "detail": (
                        "The optional multipart 'share' field must be true or false."
                    ),
                })

        job = _create_reserved_job()
        reservation_active = False
        object_hint = (hint or "").strip()[:80] or None
        job.emit(
            "queued",
            "started",
            "Job accepted and queued for the next available conversion worker.",
            shareRequested=share,
        )
    finally:
        if reservation_active:
            _release_job_slot()

    async def runner() -> None:
        acquired = False
        try:
            await _acquire_job_worker(job)
            acquired = True
            job.emit(
                "queued",
                "done",
                "Conversion worker acquired; starting image intake.",
                shareRequested=share,
            )
            await run_job(job, raw_upload=raw, object_hint=object_hint,
                          settings=settings,
                          llm=app.state.llm_factory(settings),
                          share=share,
                          gallery_store=gallery_store)
        except TimeoutError:
            fail_job_timeout(job, settings.job_timeout_s)
        finally:
            if acquired:
                job_semaphore.release()

    asyncio.create_task(runner())
    return JSONResponse(status_code=202, content={
        "job_id": job.id,
        "events_url": f"/api/jobs/{job.id}/events",
        "status_url": f"/api/jobs/{job.id}",
        "share_requested": share,
    }, headers={"Cache-Control": "no-store"})


@app.get("/api/jobs/{job_id}")
async def job_status(job_id: str) -> JSONResponse:
    job = registry.get(job_id)
    if job is None:
        return JSONResponse(
            status_code=404,
            content={"error": "job_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    return JSONResponse({
        "job_id": job.id,
        "status": job.status,
        "stage": job.stage,
        "result": job.result,
        "error": job.error,
    }, headers={"Cache-Control": "no-store"})


def _is_terminal(event: dict) -> bool:
    """An event ends the SSE stream: any error, or the final done/done."""
    return event.get("status") == "error" or (
        event.get("status") == "done" and event.get("stage") == "done"
    )


def _sse_resume_seq(request: Request, current_seq: int) -> int:
    """Parse a standard Last-Event-ID or explicit cursor without exceptions."""
    raw = request.headers.get("last-event-id")
    if raw is None:
        raw = request.query_params.get("cursor")
    try:
        parsed = int(raw) if raw is not None else 0
    except (TypeError, ValueError):
        return 0
    if parsed < 0:
        return 0
    return min(parsed, current_seq)


@app.get("/api/jobs/{job_id}/events", response_model=None)
async def job_events(job_id: str, request: Request) -> Response:
    job = registry.get(job_id)
    if job is None:
        return JSONResponse(
            status_code=404,
            content={"error": "job_not_found"},
            headers={"Cache-Control": "no-store"},
        )

    async def stream():
        last_seq = _sse_resume_seq(request, job.seq)
        cursor = next(
            (
                index for index, event in enumerate(job.events)
                if int(event.get("seq", 0)) > last_seq
            ),
            len(job.events),
        )
        while True:
            # Replay any events not yet sent (covers reconnects).
            while cursor < len(job.events):
                event = job.events[cursor]
                cursor += 1
                yield f"id: {event['seq']}\ndata: {json.dumps(event)}\n\n"
                if _is_terminal(event):
                    return
            if job.status != "running":
                return
            if await request.is_disconnected():
                return
            # Clear before the sequence recheck: an event emitted before this
            # clear is observed by the cursor check, while one emitted after it
            # sets the waiter. This avoids the old set+immediate-clear race.
            job.waiter.clear()
            if cursor < len(job.events):
                continue
            try:
                await asyncio.wait_for(job.waiter.wait(), timeout=15)
            except asyncio.TimeoutError:
                yield ": heartbeat\n\n"

    return StreamingResponse(
        stream(),
        media_type="text/event-stream",
        headers={"Cache-Control": "no-store", "X-Accel-Buffering": "no"},
    )


@app.get("/api/jobs/{job_id}/artifacts/{name}")
async def job_artifact(job_id: str, name: str, download: bool = False) -> Response:
    job = registry.get(job_id)
    if job is None:
        return JSONResponse(
            status_code=404,
            content={"error": "job_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    if name not in ARTIFACT_NAMES:
        return JSONResponse(
            status_code=404,
            content={"error": "artifact_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    if job.status != "done" and name in SUCCESS_ONLY_JOB_ARTIFACTS:
        return JSONResponse(
            status_code=404,
            content={"error": "artifact_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    path = (job.dir / name).resolve()
    # Containment: the artifact must live inside this job's directory.
    if not path.is_file() or path.parent != job.dir.resolve():
        return JSONResponse(
            status_code=404,
            content={"error": "artifact_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    headers = {"Cache-Control": "no-store"}
    if download or name in {"factory.ts", "standalone.html"}:
        headers["Content-Disposition"] = f'attachment; filename="{name}"'
    media_type = ARTIFACT_NAMES[name].split(";")[0]
    return FileResponse(path, media_type=media_type, headers=headers)


# ---------------------------------------------------------------------------
# Persistent community gallery
# ---------------------------------------------------------------------------

@app.get("/api/gallery")
def gallery_index(
    offset: int = Query(default=0, ge=0, le=1_000_000),
    limit: int = Query(default=24, ge=1, le=100),
) -> JSONResponse:
    try:
        payload = gallery_store.list_items(offset=offset, limit=limit)
    except GalleryError:
        logger.exception("gallery listing failed")
        return JSONResponse(
            status_code=503,
            content={"error": "gallery_unavailable"},
            headers={"Cache-Control": "no-store"},
        )
    return JSONResponse(payload, headers={"Cache-Control": "no-store"})


@app.get("/api/gallery/{item_id}")
def gallery_detail(item_id: str) -> JSONResponse:
    item = gallery_store.get(item_id)
    if item is None:
        return JSONResponse(
            status_code=404,
            content={"error": "gallery_item_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    return JSONResponse(item, headers={"Cache-Control": "public, max-age=60"})


@app.get("/api/gallery/{item_id}/artifacts/{name}")
def gallery_artifact(
    item_id: str,
    name: str,
    download: bool = False,
) -> Response:
    path = gallery_store.artifact_path(item_id, name)
    if path is None:
        return JSONResponse(
            status_code=404,
            content={"error": "gallery_artifact_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    headers = {"Cache-Control": "public, max-age=31536000, immutable"}
    if download or name in {"factory.ts", "standalone.html"}:
        headers["Content-Disposition"] = f'attachment; filename="{name}"'
    media_type = GALLERY_ARTIFACT_NAMES[name].split(";")[0]
    return FileResponse(path, media_type=media_type, headers=headers)


# ---------------------------------------------------------------------------
# Static SPA
# ---------------------------------------------------------------------------

@app.get("/", include_in_schema=False)
async def index() -> FileResponse:
    return FileResponse(STATIC_DIR / "index.html")


@app.get("/gallery", include_in_schema=False)
async def gallery_page() -> FileResponse:
    """Friendly SPA entry for browsing the community gallery."""
    return FileResponse(STATIC_DIR / "index.html")


@app.get("/gallery/{item_id}", include_in_schema=False, response_model=None)
async def gallery_item_page(item_id: str) -> Response:
    """Friendly SPA deep link; reject malformed ids before serving the shell."""
    if not valid_item_id(item_id):
        return JSONResponse(
            status_code=404,
            content={"error": "gallery_item_not_found"},
            headers={"Cache-Control": "no-store"},
        )
    return FileResponse(STATIC_DIR / "index.html")


app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")


@app.exception_handler(ImageRejected)
async def image_rejected_handler(_: Request, exc: ImageRejected) -> JSONResponse:
    return JSONResponse(status_code=422, content={"error": exc.code, "detail": exc.reason})


def main() -> None:
    """Console entry: python -m app.main"""
    import uvicorn

    uvicorn.run(
        "app.main:app",
        host="0.0.0.0",
        port=settings.port,
        workers=1,
        proxy_headers=True,
        log_level="info",
    )


if __name__ == "__main__":
    main()