Skip to content

Align CBM serialized form with declared schema#1065

Open
GiggleLiu wants to merge 2 commits intomainfrom
fix/cbm-serde-wire-format
Open

Align CBM serialized form with declared schema#1065
GiggleLiu wants to merge 2 commits intomainfrom
fix/cbm-serde-wire-format

Conversation

@GiggleLiu
Copy link
Copy Markdown
Contributor

Summary

ConsecutiveBlockMinimization's ProblemSchemaEntry::fields declares only matrix and bound, but the model's serde was inconsistent with that declaration:

  • Serialize wrote 4 fields (matrix, num_rows, num_cols, bound) via the default field-by-field derive
  • Deserialize (via try_from = ConsecutiveBlockMinimizationDef) required num_rows and num_cols to be present, even though they're fully derivable from matrix

That inconsistency broke the schema-driven pred create path: it builds JSON from the declared schema fields (matrix + bound), then validates by deserializing — which failed with Schema-driven factory rejected generated data for ConsecutiveBlockMinimization: missing field 'num_rows'. Reproducer:

$ pred create CBM --matrix '[[true,false,true],[false,true,true]]' --bound-k 2
Error: Schema-driven factory rejected generated data for ConsecutiveBlockMinimization: missing field `num_rows`

Fix

num_rows and num_cols are just matrix.len() and matrix[0].len() — they're cached for fast getter access but they're not independent data. Drop them from the wire format entirely by adding serde(into = ConsecutiveBlockMinimizationDef) so both directions round-trip through the minimal {matrix, bound} form. try_new already recomputes the cached dimensions on construction.

The previous validation that "if the JSON contains a num_rows that disagrees with the matrix, reject" was guarding against malformed input we shouldn't have been producing in the first place; the real input validation that survives is ragged-matrix rejection (in validate_matrix_dimensions), which is now what the test suite exercises.

Verification

$ pred create CBM --matrix '[[true,false,true],[false,true,true]]' --bound-k 2 \
    | pred solve - --solver brute-force
{
  "evaluation": "Or(true)",
  "problem": "ConsecutiveBlockMinimization",
  "solution": [0, 2, 1],
  "solver": "brute-force"
}

Test plan

  • cargo test --lib — 4958 passed (9 CBM-specific tests pass)
  • cargo test --test main — 75 integration tests passed
  • cargo fmt --all -- --check
  • cargo clippy --all-targets --workspace -- -D warnings
  • Manual end-to-end: pred create CBM ... | pred solve returns Or(true) with witness [0, 2, 1]

Related

🤖 Generated with Claude Code

GiggleLiu and others added 2 commits April 28, 2026 22:03
`ConsecutiveBlockMinimization`'s `ProblemSchemaEntry::fields` declares
`matrix` and `bound`, but its serialized form also wrote `num_rows` and
`num_cols`, and its `try_from` rejected JSON that lacked them.  This made
the schema-driven `pred create` path fail with "missing field `num_rows`"
for any caller that built JSON from the declared schema fields.

`num_rows` and `num_cols` are fully derived from `matrix` (just
`matrix.len()` and `matrix[0].len()`), so they don't belong in the wire
format.  Switch to symmetric `serde(try_from / into = Def)` so both
serialize and deserialize round-trip through the minimal `{matrix, bound}`
form, and let `try_new` recompute the cached dimensions on the way in.
The cached fields stay on the in-memory struct for fast getter access.

Replace the now-unreachable inconsistent-dimensions test with one that
pins the minimal wire format and one that exercises ragged-matrix
rejection — the actual remaining input validation.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
The CI Test job for #1065 failed because cli_tests.rs:445 still asserted
the old "num_cols must match matrix column count" error. Under the new
serde definition, extra num_rows/num_cols fields in the JSON are silently
ignored (serde's default behavior for unknown fields when not using
deny_unknown_fields), so the previous JSON now deserializes successfully
and the CLI no longer rejects it.

Switch the test to exercise the actual remaining input validation —
ragged-matrix rejection — which is what `validate_matrix_dimensions`
guards against and what the in-tree unit test now also covers. Rename
the test accordingly.

Co-Authored-By: Claude Opus 4.7 <[email protected]>
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.92%. Comparing base (cbfad84) to head (61db3c7).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1065   +/-   ##
=======================================
  Coverage   97.92%   97.92%           
=======================================
  Files         966      966           
  Lines      100043   100044    +1     
=======================================
+ Hits        97967    97971    +4     
+ Misses       2076     2073    -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant