Skip to content

armgit / atas

Public Imported from GitHub
0

Any Time, Any Space: A World-State-Centric Framework for Agentic Video Generation

5.5 KB
Raw Blame
from __future__ import annotations

import json
from pathlib import Path


def story_root(hot_root: Path, run_id: str, story_id: str) -> Path:
    return hot_root / run_id / story_id


def l0_dir(hot_root: Path, run_id: str, story_id: str, shot_id: str) -> Path:
    from atas_geometry.world.world_state import committed_observation
    committed = committed_observation(story_root(hot_root, run_id, story_id), shot_id)
    return committed or raw_l0_dir(hot_root, run_id, story_id, shot_id)


def raw_l0_dir(hot_root: Path, run_id: str, story_id: str, shot_id: str) -> Path:
    return story_root(hot_root, run_id, story_id) / "shots" / shot_id / "l0"


def l1_path(hot_root: Path, run_id: str, story_id: str, scene_id: str) -> Path:
    from atas_geometry.world.world_state import current_l1
    return current_l1(story_root(hot_root, run_id, story_id) / "scenes" / scene_id)


def scene_key(run_id: str, story_id: str, scene_id: str) -> str:
    return f"{run_id}/{story_id}/{scene_id}"


def scene_dir(hot_root: Path, run_id: str, story_id: str, scene_id: str) -> Path:
    return story_root(hot_root, run_id, story_id) / "scenes" / scene_id


def home_shot_marker(hot_root: Path, run_id: str, story_id: str, scene_id: str) -> Path:
    return scene_dir(hot_root, run_id, story_id, scene_id) / "home_shot_id.txt"


def list_shot_l0(hot_root: Path, run_id: str, story_id: str) -> list[Path]:
    shots = story_root(hot_root, run_id, story_id) / "shots"
    if not shots.is_dir():
        return []
    return sorted(
        path
        for path in shots.iterdir()
        if (path / "l0" / "cameras.npz").is_file()
    )


def remember_home_shot(
    hot_root: Path, run_id: str, story_id: str, scene_id: str, shot_id: str
) -> None:
    marker = home_shot_marker(hot_root, run_id, story_id, scene_id)
    if marker.is_file():
        return
    marker.parent.mkdir(parents=True, exist_ok=True)
    marker.write_text(str(shot_id).strip() + "\n", encoding="utf-8")


def remember_scene_shot(scene: Path, shot_id: str) -> None:
    """Append this shot to the scene's recon order (idempotent).

    Fusion aligns against the last entry in this list, not the previous clip
    on the story timeline, so a palace revisit does not SIFT against a bedroom.
    """
    sid = str(shot_id).strip()
    if not sid:
        return
    scene.mkdir(parents=True, exist_ok=True)
    log = scene / "shot_ids.txt"
    ids: list[str] = []
    if log.is_file():
        ids = [
            line.strip()
            for line in log.read_text(encoding="utf-8").splitlines()
            if line.strip()
        ]
    if sid not in ids:
        ids.append(sid)
        log.write_text("\n".join(ids) + "\n", encoding="utf-8")


def resolve_home_shot(
    hot_root: Path,
    run_id: str,
    story_id: str,
    scene_id: str,
    *,
    requested: str = "",
) -> str:
    """First successful recon of this scene. Never a later continue L0."""
    del requested
    marker = home_shot_marker(hot_root, run_id, story_id, scene_id)
    if marker.is_file():
        home = marker.read_text(encoding="utf-8").strip()
        if home and (l0_dir(hot_root, run_id, story_id, home) / "cameras.npz").is_file():
            return home
    # The committed world is the authority: its first observation is the Home shot.
    # (Covers a crash between the world commit and writing the marker.)
    state_path = scene_dir(hot_root, run_id, story_id, scene_id) / "world_state.json"
    if state_path.is_file():
        observations = list(json.loads(state_path.read_text()).get("observations", {}))
        if observations:
            remember_home_shot(hot_root, run_id, story_id, scene_id, observations[0])
            return observations[0]
    existing = list_shot_l0(hot_root, run_id, story_id)
    if existing:
        home = existing[0].name
        remember_home_shot(hot_root, run_id, story_id, scene_id, home)
        return home
    raise RuntimeError(f"no Home L0 for scene {scene_id} under {run_id}/{story_id}")


def render_dir(hot_root: Path, run_id: str, story_id: str, shot_id: str) -> Path:
    return story_root(hot_root, run_id, story_id) / "shots" / shot_id / "render"


def last_camera_path(hot_root: Path, run_id: str, story_id: str, shot_id: str) -> Path:
    """Last-frame camera of our own L1 render for this shot.

    Written after every continue render. The pose is generated by us in the
    scene L1 frame, so chaining off it needs no recon alignment at all.
    """
    return render_dir(hot_root, run_id, story_id, shot_id) / "last_camera.json"


def resolve_start_shot(
    hot_root: Path,
    run_id: str,
    story_id: str,
    scene_id: str,
    *,
    requested: str,
    home: str,
    rendering_shot_id: str = "",
) -> tuple[str, str, bool]:
    """Chain-first start: the shot id the client sent, else this scene's Home.

    Returns (shot_id, role, fell_back). ``requested`` is whatever the client
    put in ``previous_shot_id`` — for continue that is the previous clip, for
    a scene revisit it is the last rendered shot **of this room**, not the
    previous clip on the story timeline. We read that id's ``last_camera.json``
    as-is and never walk the timeline ourselves. Missing render falls back to
    this scene's Home so a palace revisit cannot inherit a bedroom pose.
    """
    if requested and requested != home and requested != rendering_shot_id:
        if last_camera_path(hot_root, run_id, story_id, requested).is_file():
            return requested, "previous_shot", False
        return home, "scene_home", True
    return home, "scene_home", False