Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Enforce per-group chunk time order in doctor (#239)
  • Loading branch information
mhaye9545 committed Aug 29, 2026
commit 5cb88a589bffd49175d4ac25a26961e30468b3c1
3 changes: 2 additions & 1 deletion docs/FORMAT.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ A file claiming this convention must satisfy, in increasing strictness:
one report each in argument order; its aggregate result follows the
[exit code rules](#exit-codes). It validates the container, summary, indexes,
stamps, chunk purity (against the file's own group map, or a video-versus-state
approximation when it has none), per-topic time order, and the H.264
approximation when it has none), per-topic time order, per-group chunk time order, and the H.264
access-unit properties listed below. It does not currently classify H.264 picture coding types to detect
B-frames, so a clean report is not proof of the unchecked no-B-frame
constraint. The doctor also does not reject non-VCL NAL units before the first
Expand All @@ -179,6 +179,7 @@ automation. An `error` breaks the canonical convention (or the MCAP spec); a
| `no-chunk-indexes` | error | The summary has no `ChunkIndex` records. |
| `chunk-missing-message-indexes` | error | A chunk index has no per-channel `MessageIndex` offsets. |
| `chunk-mixes-groups` | warning | One chunk contains channels assigned to different groups; custom grouping can make this intentional. |
| group-chunks-out-of-order | warning | A group's chunks are not strictly ascending by message_start_time. |
| `chunk-mixes-video-and-state` | warning | One chunk contains both video and state channels, reported only when the file has no group map; custom grouping can make this intentional. |
| `missing-provenance` | error | The `provenance/v1` metadata record is absent. |
| `provenance-missing-key` | error | `provenance/v1` lacks `schema_version` or `pipeline_version`. |
Expand Down
18 changes: 18 additions & 0 deletions src/hflow/doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ def diagnose(path: Path | str) -> DoctorReport:
"no-chunk-indexes",
"no ChunkIndex records: the file is unchunked or unindexed",
)
last_chunk_start_by_group = {}
out_of_order_groups = set()

for chunk_number, chunk_index in enumerate(summary.chunk_indexes):
chunk_channel_ids = set(chunk_index.message_index_offsets.keys())
if not chunk_channel_ids:
Expand All @@ -248,6 +251,14 @@ def diagnose(path: Path | str) -> DoctorReport:
if topic in group_by_topic:
chunk_groups.add(group_by_topic[topic])

for group in chunk_groups:
if (
group in last_chunk_start_by_group
and chunk_index.message_start_time < last_chunk_start_by_group[group]
):
out_of_order_groups.add(group)
last_chunk_start_by_group[group] = chunk_index.message_start_time

if len(chunk_groups) > 1:
mixed_topics = sorted(
topics_by_channel_id[channel_id] for channel_id in chunk_channel_ids
Expand Down Expand Up @@ -276,6 +287,13 @@ def diagnose(path: Path | str) -> DoctorReport:
"the default convention separates them",
)

for group in sorted(out_of_order_groups):
collector.add(
DiagnosticLevel.WARNING,
"group-chunks-out-of-order",
f"chunks for group {group!r} are not time-ordered",
)

if provenance is None:
collector.add(
DiagnosticLevel.ERROR,
Expand Down
106 changes: 106 additions & 0 deletions tests/test_doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,109 @@ def test_chunk_mix_without_map(tmp_path: Path) -> None:
codes = {finding.code for finding in report.findings}
assert "chunk-mixes-video-and-state" in codes
assert "chunk-mixes-groups" not in codes


def test_group_chunks_out_of_order(tmp_path: Path) -> None:
path = tmp_path / "descending.mcap"
with path.open("wb") as stream:
writer = StockWriter(stream, chunk_size=1)
writer.start(profile="", library="test")
writer.add_metadata(
name="provenance/v1",
data={
"group//alpha": "state",
"schema_version": "1",
"pipeline_version": "1",
},
)
schema_id = writer.register_schema(name="dummy", encoding="json", data=b"{}")
ch1 = writer.register_channel(topic="/alpha", message_encoding="json", schema_id=schema_id)

# force a chunk boundary by writing and flushing?
# we can just write 2000 bytes since chunk_size=1024
writer.add_message(ch1, log_time=1000, data=b"{" + b"a" * 2000 + b"}", publish_time=1000)
writer.add_message(ch1, log_time=500, data=b"{}", publish_time=500)
writer.finish()

report = diagnose(path)
codes = {finding.code for finding in report.findings}
assert "group-chunks-out-of-order" in codes


def test_group_chunks_ascending_is_clean(tmp_path: Path) -> None:
path = tmp_path / "ascending.mcap"
with path.open("wb") as stream:
writer = StockWriter(stream, chunk_size=1)
writer.start(profile="", library="test")
writer.add_metadata(
name="provenance/v1",
data={
"group//alpha": "state",
"schema_version": "1",
"pipeline_version": "1",
},
)
schema_id = writer.register_schema(name="dummy", encoding="json", data=b"{}")
ch1 = writer.register_channel(topic="/alpha", message_encoding="json", schema_id=schema_id)
writer.add_message(ch1, log_time=500, data=b"x" * 2000, publish_time=500)
writer.add_message(ch1, log_time=1000, data=b"x" * 2000, publish_time=1000)
writer.finish()

report = diagnose(path)
codes = {finding.code for finding in report.findings}
assert "group-chunks-out-of-order" not in codes


def test_group_chunks_interleaved_different_groups_is_clean(tmp_path: Path) -> None:
path = tmp_path / "interleaved.mcap"
with path.open("wb") as stream:
writer = StockWriter(stream, chunk_size=1)
writer.start(profile="", library="test")
writer.add_metadata(
name="provenance/v1",
data={
"group//alpha": "state1",
"group//beta": "state2",
"schema_version": "1",
"pipeline_version": "1",
},
)
schema_id = writer.register_schema(name="dummy", encoding="json", data=b"{}")
ch1 = writer.register_channel(topic="/alpha", message_encoding="json", schema_id=schema_id)
ch2 = writer.register_channel(topic="/beta", message_encoding="json", schema_id=schema_id)

# alpha chunk 1: t=1000
writer.add_message(ch1, log_time=1000, data=b"x" * 2000, publish_time=1000)
# beta chunk 1: t=500
writer.add_message(ch2, log_time=500, data=b"x" * 2000, publish_time=500)
# alpha chunk 2: t=2000 (ascending for alpha)
writer.add_message(ch1, log_time=2000, data=b"x" * 2000, publish_time=2000)
writer.finish()

report = diagnose(path)
codes = {finding.code for finding in report.findings}
assert "group-chunks-out-of-order" not in codes


def test_descending_without_map_is_clean(tmp_path: Path) -> None:
path = tmp_path / "descending_nomap.mcap"
with path.open("wb") as stream:
writer = StockWriter(stream, chunk_size=1)
writer.start(profile="", library="test")
writer.add_metadata(
name="provenance/v1",
data={
"schema_version": "1",
"pipeline_version": "1",
},
)
schema_id = writer.register_schema(name="dummy", encoding="json", data=b"{}")
ch1 = writer.register_channel(topic="/alpha", message_encoding="json", schema_id=schema_id)

writer.add_message(ch1, log_time=1000, data=b"x" * 2000, publish_time=1000)
writer.add_message(ch1, log_time=500, data=b"x" * 2000, publish_time=500)
writer.finish()

report = diagnose(path)
codes = {finding.code for finding in report.findings}
assert "group-chunks-out-of-order" not in codes
Loading