Skip to content

fix(stage_planning): _camera_presence_by_episode reads check_runs instead of check_runs_latest, dropping Stage.MEDIA after errored retries #698

Description

@shobhitagnihotri69

Summary

In src/hflow/stage_planning.py, _camera_presence_by_episode queries the historical check_runs table directly rather than check_runs_latest.

When camera_frame_stats is re-run and encounters an error or worker timeout (status = 'error'), _camera_presence_by_episode bypasses the latest errored run, matches an older historical run from check_runs, and returns camera_presence_by_episode[episode_id] = False.

Consequently, plan_outstanding_stages evaluates camera_presence_by_episode.get(episode_id) is False and drops Stage.MEDIA from the execution plan, directly violating the documented safe fallback invariant:

"If that exact default check is not registered or its result is missing, the planner preserves the safe fallback and schedules media rather than risk omitting a contact sheet." (stage_planning.py:284-286)


Root Cause

In src/hflow/stage_planning.py:229-246:

    rows = connection.execute(
        f"""
        SELECT runs.episode_id, count(measurements.key) > 0
        FROM check_runs AS runs            # <-- Reads all historical check_runs instead of check_runs_latest
        LEFT JOIN measurements
          ON measurements.episode_id = runs.episode_id
         AND measurements.run_fingerprint = runs.run_fingerprint
         AND measurements.check_name = runs.check_name
         AND measurements.check_version = runs.check_version
         AND ends_with(measurements.key, '/message_count')
        WHERE runs.status IN ({statuses})
          AND runs.episode_id IN ({episode_placeholders})
          AND runs.check_name = ?
          AND runs.check_version = ?
        GROUP BY runs.episode_id
        """,
        [*episode_ids, check_name, check_version],
    ).fetchall()

Activity

  1. kstonekuan commented on Oct 7, 2026

    @kstonekuan
    Contributor

    Thanks, but this is the camera case discussed on #659. episode_id is a content hash, so an earlier successful camera_frame_stats run at the same version measured the same camera streams. A later crash withdraws nothing about which cameras the content has, so the planner can keep using that answer. The fallback in stage_planning.py covers the case where no such run exists at all.

  2. shobhitagnihotri69 commented on Oct 7, 2026

    @shobhitagnihotri69
    ContributorAuthor

    Thanks for the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions