Files
romm/backend/handler/streaming/imports.py
Georges-Antoine Assi 20bff0830a fix(streaming): merge the webstation exit delta over the restored save archive (#5673)
* fix(streaming): merge the webstation exit delta over the restored save archive

A webstation exit export holds only the files changed since launch, but
RomM filed it as a standalone .saves.zip and the next launch restored only
the newest zip. A session that exited without an in-game save therefore
filed an archive with no save data, and the next Play booted without it.

The launch now records which native archive it restored on the session
record, and the exit pull lays the delta over that archive before filing
it, so every stored archive is a full snapshot.

Fixes #5544

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(streaming): type the stored exit archive as bytes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): skip the exit merge when the restored archive was replaced

PUT /saves/{id} rewrites a save in place under the same id, so the exit
merge could combine the session's delta with save data it never loaded.
Record the restored archive's content hash at launch next to its id, and
merge only while the base still has it; otherwise file the delta alone.

Addresses Greptile's review on #5673, including folding the
_over_restored_archive docstring into one sentence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(streaming): drop ensure_zipfile_writable from the archive overlay

#5678 applies the zipfile_inflate64 fix where it is imported, and removed the helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: code-review

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: simplify

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: review-polish

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* pr-ready: prose (code-review additions)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(streaming): keep imported states, folders and the entry cap in the exit merge

A launch that imported a state left the base archive's states off the
container, so the exit merge now leaves them out too. The merge keeps
both archives' folders and refuses past the archive entry limit, so a
merged archive always gets a content hash. The webstation hydrate now
uploads the save its caller resolved instead of looking it up again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(db): query a platform id no sequence can reach

Postgres keeps id sequences across tests, so a worker that has created
about a thousand platforms hands id 999 to the fixture's own platform.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 08:40:47 -05:00

288 lines
10 KiB
Python

"""Folding a foreign-emulator save or state pick into the one archive a
launch's `activate` call sends, under `.import/<kind>/` for the broker to place.
"""
import asyncio
import io
import json
import time
import zipfile
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, Literal, NamedTuple
from handler.filesystem import fs_asset_handler
from handler.streaming import archive, broker, saves, states, webstation
from handler.streaming.config import ResolvedContainer, emulator_labels
from logger.logger import log
from models.assets import Save, State
from models.rom import Rom
# Charged on each entry's expanded size, so a small, highly compressible
# upload cannot inflate past the ceiling any stored save archive has.
_MAX_EXPANDED_BYTES = broker.SAVE_FILE_MAX_BYTES
_MAX_MEMBERS = 2000
@dataclass(frozen=True)
class ForeignMember:
"""One foreign pick's bytes, waiting to be folded into the import archive."""
kind: Literal["save", "state"]
name: str
content: bytes
origin: str
def origin_of(emulator: str | None, origin_device_id: str | None) -> str:
"""The advisory manifest origin of a pick; EmulatorJS records its core's name as `emulator`."""
if origin_device_id is not None:
return "hardware"
if not emulator:
return "unknown"
return "standalone" if emulator.lower() in emulator_labels() else "emulatorjs"
def _safe_name(name: str) -> str | None:
"""None when this zip member is junk that must never ride in an import
archive (a dotfile, or macOS's resource-fork sidecar folder)."""
normalized = name.replace("\\", "/")
parts = [p for p in normalized.split("/") if p]
if not parts or parts[0] == "__MACOSX" or any(p.startswith(".") for p in parts):
return None
return "/".join(parts)
def _manifest_files(zf: zipfile.ZipFile) -> dict[str, dict[str, Any]]:
"""A broker archive's manifest entries by path, empty when it has none."""
return archive.manifest_files(archive.read_manifest(zf))
_Staged = dict[str, tuple[bytes, dict[str, Any], zipfile.ZipInfo | None]]
@dataclass
class _ArchiveBudget:
"""Expanded-size and member-count budget for one archive build."""
bytes_left: int = _MAX_EXPANDED_BYTES
members_left: int = _MAX_MEMBERS
def charge(self, size: int) -> None:
self.bytes_left -= size
self.members_left -= 1
if self.bytes_left < 0 or self.members_left < 0:
raise ValueError(
"import archive exceeds the expanded-size or member-count limit"
)
def _stage_zip(
zf: zipfile.ZipFile,
staged: _Staged,
budget: _ArchiveBudget,
*,
skip_state: bool,
place: Callable[[str, dict[str, Any] | None], tuple[str, dict[str, Any]]],
) -> None:
"""Stage each non-junk entry of `zf` at the path and manifest entry `place` picks.
A later write at the same path replaces an earlier one, as a zip reader
resolves duplicate entries.
"""
files = _manifest_files(zf)
for info in zf.infolist():
name = _safe_name(info.filename)
if info.is_dir() or name is None:
continue
entry = files.get(info.filename) or files.get(name)
if skip_state and entry is not None and entry.get("kind") == "state":
continue
budget.charge(info.file_size)
path, manifest_entry = place(name, entry)
staged[path] = (archive.read_member(zf, info), manifest_entry, info)
def _write_member(
member: ForeignMember, staged: _Staged, budget: _ArchiveBudget
) -> None:
"""Stage a foreign save archive's inner entries, or the member itself as one file."""
def place(name: str, _entry: dict[str, Any] | None) -> tuple[str, dict[str, Any]]:
path = f".import/{member.kind}/{name}"
return path, {"path": path, "kind": member.kind, "origin": member.origin}
# A state is one emulator file even when it is zip-shaped (a PCSX2 .p2s).
inner = None
if member.kind == "save":
try:
inner = zipfile.ZipFile(io.BytesIO(member.content))
except zipfile.BadZipFile:
pass
if inner is None:
budget.charge(len(member.content))
path, entry = place(_safe_name(member.name) or "data", None)
staged[path] = (member.content, entry, None)
return
with inner:
# Another emulator's save archive also carries its exit state.
_stage_zip(inner, staged, budget, skip_state=True, place=place)
def build_import_archive(
rom_id: int,
base: tuple[str, bytes] | None,
members: list[ForeignMember],
) -> tuple[bytes, list[dict[str, Any]]]:
"""Build the single v2 zip `activate`'s `save.archive` wants: the base plus `.import/` members.
Returns:
The archive bytes, and the manifest's `files` list actually written.
"""
staged: _Staged = {}
budget = _ArchiveBudget()
if base is not None:
base_name, base_content = base
try:
base_zf = zipfile.ZipFile(io.BytesIO(base_content))
except zipfile.BadZipFile:
log.warning("base save archive is not a valid zip, %s", base_name)
else:
with base_zf:
_stage_zip(
base_zf,
staged,
budget,
skip_state=any(m.kind == "state" for m in members),
place=lambda name, entry: (
name,
(
{**entry, "path": name}
if entry is not None
else {"path": name, "kind": "save"}
),
),
)
for member in members:
_write_member(member, staged, budget)
carried = [entry for _content, entry, _source in staged.values()]
out = io.BytesIO()
with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as zf:
for path, (content, _entry, source) in staged.items():
zf.writestr(archive.utf8_zipinfo(path, source), content)
zf.writestr(
archive.MANIFEST_NAME,
json.dumps(
{
"version": 2,
"created_at": time.time(),
"import": {"source": "romm", "rom_id": rom_id},
"files": carried,
}
),
)
return out.getvalue(), carried
async def _read_asset(file_path: str, file_name: str) -> bytes | None:
try:
return await fs_asset_handler.read_file(f"{file_path}/{file_name}")
except FileNotFoundError:
log.warning("import member missing on disk, %s", file_name)
return None
class ImportHydration(NamedTuple):
"""What `hydrate_import_archive` actually got onto the container."""
path: str | None = None
state_imported: bool = False
# The archive is a default save alone, so a refused import boots the newest archive.
native_fallback: bool = False
# The native save archive the upload is built on, and its content hash at that time.
restored_save_id: int | None = None
restored_save_hash: str | None = None
# The import left the base's states off the container for the one it carries.
restored_without_state: bool = False
async def hydrate_import_archive(
user_id: int,
rom: Rom,
container: ResolvedContainer,
*,
save: Save | None,
save_is_foreign: bool,
state: State | None,
native_fallback: bool = False,
) -> ImportHydration:
"""Build this launch's one archive (native base plus `.import/` members) and upload it.
Args:
native_fallback: a foreign save that doesn't make it in leaves the
newest native save as the base, as a launch that picked none gets.
Returns:
The container path `activate`'s `save.archive` wants (None when
there is nothing to send through the import path), whether
`state` specifically made it into the uploaded archive, and the
native archive it carries as its base.
"""
members: list[ForeignMember] = []
if save is not None and save_is_foreign:
picked = await saves.read_restorable_archive(save)
if picked is not None:
members.append(
ForeignMember(
kind="save",
name=save.file_name,
content=picked[1],
origin=origin_of(save.emulator, save.origin_device_id),
)
)
if state is not None:
content = await _read_asset(state.file_path, state.file_name)
if content is not None:
members.append(
ForeignMember(
kind="state",
name=states.container_state_filename(state.file_name),
content=content,
origin=origin_of(state.emulator, None),
)
)
# Nothing foreign made it in, so the caller falls back to ordinary hydration.
if not members:
return ImportHydration()
base: tuple[str, bytes] | None = None
base_save: Save | None = None
save_lost = save_is_foreign and not any(m.kind == "save" for m in members)
if not save_is_foreign or (native_fallback and save_lost):
native = (None if save_is_foreign else save) or await asyncio.to_thread(
saves.newest_restorable, user_id, rom.id, container.emulator
)
if native is not None:
base = await saves.read_restorable_archive(native)
base_save = native if base is not None else None
archive_bytes, carried = await asyncio.to_thread(
build_import_archive, rom.id, base, members
)
path = await asyncio.to_thread(
webstation.upload_archive, container, f"rom-{rom.id}.zip", archive_bytes
)
# A member whose inner zip held only junk never lands in `carried`.
state_included = path is not None and any(f.get("kind") == "state" for f in carried)
restored_id, restored_hash = saves.restored_ref(base_save, path)
return ImportHydration(
path,
state_included,
restored_save_id=restored_id,
restored_save_hash=restored_hash,
restored_without_state=any(m.kind == "state" for m in members),
)