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
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
"""Persistent, append-only storage for community gallery results.

Each published result is committed as one immutable directory::

    <gallery_dir>/<32-hex item id>/
        item.json
        reference.png
        model.bundle.js
        ...

Writers first build a hidden staging directory and atomically rename it to
the public item id. Readers ignore staging directories and rebuild their view
from validated metadata on every request, so a process restart requires no
in-memory index and a concurrent publish is never observed half-written.
"""

from __future__ import annotations

import json
import math
import os
import re
import shutil
import threading
import time
import uuid
from datetime import datetime, timezone
from pathlib import Path
from typing import Any


ITEM_ID_RE = re.compile(r"^[0-9a-f]{32}$")
METADATA_NAME = "item.json"
METADATA_SCHEMA_VERSION = 1
MAX_METADATA_BYTES = 64 * 1024

# This is deliberately narrower than the per-job artifact set. In particular,
# events.jsonl is operational history rather than a gallery artifact and is
# never made permanent or public.
GALLERY_ARTIFACT_NAMES = {
    "reference.png": "image/png",
    "probe.json": "application/json",
    "spec.json": "application/json",
    "compile-spec.json": "application/json",
    "validation.json": "application/json",
    "factory.ts": "text/plain; charset=utf-8",
    "model.bundle.js": "text/javascript; charset=utf-8",
    "standalone.html": "text/html; charset=utf-8",
}
REQUIRED_GALLERY_ARTIFACTS = frozenset({
    "reference.png",
    "spec.json",
    "factory.ts",
    "model.bundle.js",
    "standalone.html",
})


class GalleryError(RuntimeError):
    """A gallery item could not be committed or storage is unavailable."""


def valid_item_id(item_id: str) -> bool:
    return bool(ITEM_ID_RE.fullmatch(item_id or ""))


def _iso_utc(epoch: float) -> str:
    return (
        datetime.fromtimestamp(epoch, timezone.utc)
        .isoformat(timespec="milliseconds")
        .replace("+00:00", "Z")
    )


def _safe_int(value: Any, default: int = 0) -> int:
    if isinstance(value, bool):
        return default
    try:
        parsed = int(value)
    except (TypeError, ValueError, OverflowError):
        return default
    return max(0, parsed)


def _safe_float(value: Any, default: float = 0.0) -> float:
    if isinstance(value, bool):
        return default
    try:
        parsed = float(value)
    except (TypeError, ValueError, OverflowError):
        return default
    return max(0.0, parsed) if math.isfinite(parsed) else default


class GalleryStore:
    """Concurrency-safe persistent gallery backed by immutable directories."""

    def __init__(self, root: Path) -> None:
        self.root = Path(root)
        self._lock = threading.RLock()

    def ensure_ready(self) -> None:
        try:
            self.root.mkdir(parents=True, exist_ok=True)
            if not self.root.is_dir():
                raise OSError("gallery path is not a directory")
        except OSError as exc:
            raise GalleryError(
                f"Community gallery storage is unavailable: {type(exc).__name__}."
            ) from exc

    def publish(
        self,
        *,
        item_id: str,
        job_dir: Path,
        result: dict[str, Any],
        created_at: float | None = None,
        elapsed_started_at: float | None = None,
        staging_token: str | None = None,
    ) -> dict[str, Any]:
        """Atomically copy one successful job into permanent gallery storage.

        The item id is normally the job id. Publishing the same completed item
        twice is idempotent; a corrupt pre-existing destination is refused.
        """
        if not valid_item_id(item_id):
            raise GalleryError("Gallery item id must be 32 lowercase hex characters.")

        self.ensure_ready()
        root = self.root.resolve()
        source_root = Path(job_dir).resolve()
        if not source_root.is_dir():
            raise GalleryError("Completed job artifacts are unavailable.")

        epoch = time.time() if created_at is None else _safe_float(created_at, time.time())
        metadata = self._metadata_from_result(
            item_id=item_id,
            result=result,
            created_at=epoch,
        )
        final_dir = root / item_id
        if staging_token is None:
            staging_token = uuid.uuid4().hex
        if not re.fullmatch(r"[0-9a-f]{32}", staging_token):
            raise GalleryError("Gallery staging token must be 32 lowercase hex characters.")
        staging = root / f".publishing-{item_id}-{staging_token}"

        with self._lock:
            existing = self.get(item_id)
            if existing is not None:
                return existing
            if final_dir.exists():
                raise GalleryError("A corrupt gallery item already uses this id.")

            copied_names: list[str] = []
            committed = False
            try:
                staging.mkdir(mode=0o750)
                for name in GALLERY_ARTIFACT_NAMES:
                    source = (source_root / name).resolve()
                    if not source.is_file() or source.parent != source_root:
                        continue
                    destination = staging / name
                    with source.open("rb") as reader, destination.open("xb") as writer:
                        shutil.copyfileobj(reader, writer, length=1024 * 1024)
                        writer.flush()
                        try:
                            os.fsync(writer.fileno())
                        except OSError:
                            # Some mounted object stores do not implement fsync.
                            pass
                    copied_names.append(name)

                missing = REQUIRED_GALLERY_ARTIFACTS.difference(copied_names)
                if missing:
                    raise GalleryError(
                        "Completed result is missing required gallery artifacts: "
                        + ", ".join(sorted(missing))
                    )

                metadata["artifactNames"] = copied_names
                if elapsed_started_at is not None:
                    elapsed_origin = _safe_float(
                        elapsed_started_at, default=time.time()
                    )
                    metadata["elapsedSeconds"] = round(
                        max(0.0, time.time() - elapsed_origin),
                        1,
                    )
                # Build the response before the filesystem commit. Once the
                # rename succeeds, no fallible transformation remains that
                # could make the pipeline report an error for a visible item.
                public_item = self._public_item(metadata)
                metadata_path = staging / METADATA_NAME
                with metadata_path.open("x", encoding="utf-8") as writer:
                    json.dump(metadata, writer, ensure_ascii=False, separators=(",", ":"))
                    writer.write("\n")
                    writer.flush()
                    try:
                        os.fsync(writer.fileno())
                    except OSError:
                        pass

                # The rename is the commit point. A reader sees either no item
                # directory or the complete immutable directory.
                staging.rename(final_dir)
                committed = True
                return public_item
            except GalleryError:
                raise
            except OSError as exc:
                # A second process may have won an extremely unlikely
                # same-id race. Treat a valid committed item as idempotent.
                existing = self.get(item_id)
                if existing is not None:
                    return existing
                raise GalleryError(
                    f"Could not publish the community gallery item: {type(exc).__name__}."
                ) from exc
            finally:
                if not committed and staging.exists():
                    shutil.rmtree(staging, ignore_errors=True)

    def list_items(self, *, offset: int = 0, limit: int = 24) -> dict[str, Any]:
        """Return validated items newest first with offset pagination."""
        self.ensure_ready()
        offset = max(0, int(offset))
        limit = max(1, min(100, int(limit)))
        items: list[dict[str, Any]] = []
        try:
            children = list(self.root.iterdir())
        except OSError as exc:
            raise GalleryError(
                f"Could not read community gallery storage: {type(exc).__name__}."
            ) from exc

        for child in children:
            if not child.is_dir() or not valid_item_id(child.name):
                continue
            item = self.get(child.name)
            if item is not None:
                items.append(item)
        items.sort(key=lambda item: (item["createdAt"], item["id"]), reverse=True)
        total = len(items)
        page = items[offset:offset + limit]
        return {
            "items": page,
            "offset": offset,
            "limit": limit,
            "total": total,
            "hasMore": offset + len(page) < total,
        }

    def get(self, item_id: str) -> dict[str, Any] | None:
        """Read and validate one complete item directly from persistent disk."""
        if not valid_item_id(item_id):
            return None
        try:
            root = self.root.resolve()
            item_dir = (root / item_id).resolve()
            if item_dir.parent != root or not item_dir.is_dir():
                return None
            metadata_path = (item_dir / METADATA_NAME).resolve()
            if (
                metadata_path.parent != item_dir
                or not metadata_path.is_file()
                or metadata_path.stat().st_size > MAX_METADATA_BYTES
            ):
                return None
            raw = json.loads(metadata_path.read_text(encoding="utf-8"))
        except (OSError, UnicodeError, json.JSONDecodeError, TypeError, ValueError):
            return None
        metadata = self._validated_metadata(raw, expected_id=item_id, item_dir=item_dir)
        return self._public_item(metadata) if metadata is not None else None

    def artifact_path(self, item_id: str, name: str) -> Path | None:
        """Resolve one allowlisted artifact with metadata and path containment."""
        if name not in GALLERY_ARTIFACT_NAMES:
            return None
        item = self.get(item_id)
        if item is None or name not in item["artifacts"]:
            return None
        root = self.root.resolve()
        item_dir = (root / item_id).resolve()
        path = (item_dir / name).resolve()
        if path.parent != item_dir or not path.is_file():
            return None
        return path

    @staticmethod
    def _metadata_from_result(
        *,
        item_id: str,
        result: dict[str, Any],
        created_at: float,
    ) -> dict[str, Any]:
        target_name = result.get("targetName")
        if not isinstance(target_name, str) or not target_name.strip():
            target_name = "Object"
        return {
            "schemaVersion": METADATA_SCHEMA_VERSION,
            "id": item_id,
            "jobId": item_id,
            "createdAtEpoch": created_at,
            "targetName": target_name.strip()[:80],
            "components": _safe_int(result.get("components")),
            "materials": _safe_int(result.get("materials")),
            "elapsedSeconds": _safe_float(result.get("elapsedSeconds")),
            "generationMode": str(result.get("generationMode") or "unknown")[:80],
            "generatedPass": str(result.get("generatedPass") or "unknown")[:80],
            "reviewStatus": str(result.get("reviewStatus") or "unreviewed")[:80],
            "artifactNames": [],
        }

    @staticmethod
    def _validated_metadata(
        raw: Any,
        *,
        expected_id: str,
        item_dir: Path,
    ) -> dict[str, Any] | None:
        if not isinstance(raw, dict):
            return None
        if raw.get("schemaVersion") != METADATA_SCHEMA_VERSION:
            return None
        if raw.get("id") != expected_id or raw.get("jobId") != expected_id:
            return None
        target_name = raw.get("targetName")
        if not isinstance(target_name, str) or not target_name.strip() or len(target_name) > 80:
            return None
        epoch = raw.get("createdAtEpoch")
        if isinstance(epoch, bool) or not isinstance(epoch, (int, float)):
            return None
        epoch = float(epoch)
        if not math.isfinite(epoch) or epoch < 0:
            return None
        names = raw.get("artifactNames")
        if not isinstance(names, list) or any(not isinstance(name, str) for name in names):
            return None
        artifact_names: list[str] = []
        for name in GALLERY_ARTIFACT_NAMES:
            if name not in names:
                continue
            path = (item_dir / name).resolve()
            if path.parent != item_dir or not path.is_file():
                return None
            artifact_names.append(name)
        if not REQUIRED_GALLERY_ARTIFACTS.issubset(artifact_names):
            return None
        return {
            "schemaVersion": METADATA_SCHEMA_VERSION,
            "id": expected_id,
            "jobId": expected_id,
            "createdAtEpoch": epoch,
            "targetName": target_name.strip(),
            "components": _safe_int(raw.get("components")),
            "materials": _safe_int(raw.get("materials")),
            "elapsedSeconds": _safe_float(raw.get("elapsedSeconds")),
            "generationMode": str(raw.get("generationMode") or "unknown")[:80],
            "generatedPass": str(raw.get("generatedPass") or "unknown")[:80],
            "reviewStatus": str(raw.get("reviewStatus") or "unreviewed")[:80],
            "artifactNames": artifact_names,
        }

    @staticmethod
    def _public_item(metadata: dict[str, Any]) -> dict[str, Any]:
        item_id = metadata["id"]
        artifacts = {
            name: f"/api/gallery/{item_id}/artifacts/{name}"
            for name in GALLERY_ARTIFACT_NAMES
            if name in metadata["artifactNames"]
        }
        return {
            "id": item_id,
            "jobId": metadata["jobId"],
            "targetName": metadata["targetName"],
            "createdAt": _iso_utc(metadata["createdAtEpoch"]),
            "thumbnailUrl": artifacts["reference.png"],
            "detailUrl": f"/api/gallery/{item_id}",
            "artifacts": artifacts,
            "stats": {
                "components": metadata["components"],
                "materials": metadata["materials"],
                "elapsedSeconds": metadata["elapsedSeconds"],
            },
            "generation": {
                "mode": metadata["generationMode"],
                "generatedPass": metadata["generatedPass"],
                "reviewStatus": metadata["reviewStatus"],
            },
        }