refactor(evaluator): split mod.rs into focused submodules#153
Conversation
This commit adds a new module `engine.rs` that implements the core logic for evaluating magic rules against file buffers. Key functionalities include: - `evaluate_single_rule`: Evaluates an individual magic rule, resolving offsets and applying operators. - `evaluate_rules`: Handles hierarchical evaluation of rules, managing context and error handling gracefully. Additionally, the `mod.rs` file has been updated to expose these new functions, and the previous test suite has been removed as part of the refactor. This lays the groundwork for improved rule evaluation and extensibility in the magic file identification process. Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
|
Caution Review failedFailed to post review comments Summary by CodeRabbit
WalkthroughPublic evaluator refactor: core evaluation logic moved into a new Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as Client
participant Engine as evaluator::engine
participant Ctx as EvaluationContext
participant Buf as Buffer
participant Rules as MagicRules
Caller->>Engine: evaluate_rules_with_config(rules, buffer, config)
Engine->>Ctx: create EvaluationContext(config)
Engine->>Rules: iterate rules
Engine->>Buf: read bytes at resolved offset
Engine->>Rules: apply operator & compare
Engine->>Ctx: increment/decrement recursion_depth
Engine-->>Caller: Vec<RuleMatch> (matches or error)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
Merge ProtectionsYour pull request matches the following merge protections and will not be merged until they are valid. 🟢 CI must passWonderful, this rule succeeded.All CI checks must pass. Release-plz PRs are exempt because they only bump versions and changelogs (code was already tested on main), and GITHUB_TOKEN-triggered force-pushes suppress CI.
🟢 Do not merge outdated PRsWonderful, this rule succeeded.Make sure PRs are within 10 commits of the base branch before merging
|
|
Related Documentation 5 document(s) may need updating based on files changed in this PR: libMagic-rs ARCHITECTURE
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
Pull request overview
Refactors the evaluator module to reduce src/evaluator/mod.rs size by extracting the core rule-evaluation engine into a new src/evaluator/engine.rs module, relocating most evaluator tests alongside the engine implementation.
Changes:
- Added
src/evaluator/engine.rscontainingevaluate_single_rule,evaluate_rules, andevaluate_rules_with_config, plus most evaluator tests. - Slimmed
src/evaluator/mod.rsto focus on the public API types (EvaluationContext,RuleMatch) and re-exports. - Removed the old
src/evaluator/tests.rsfile (tests relocated into modules).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| src/evaluator/tests.rs | Deleted; evaluator tests moved into other evaluator modules. |
| src/evaluator/mod.rs | Introduces engine submodule and re-exports; keeps context/result types and their tests. |
| src/evaluator/engine.rs | New engine module with core evaluation functions and most former evaluator tests. |
🧪 CI InsightsHere's what we observed from your CI run for e879b4e. 🟢 All jobs passed!But CI Insights is watching 👀 |
On error paths in evaluate_rules, decrement_recursion_depth() was called with ? which could mask the original error (Timeout, RecursionLimit) if the depth was already 0. Use `let _ =` for best-effort cleanup instead. Also fix timeout propagation to bind the original timeout_ms from the caught error rather than reconstructing it with unwrap_or(0), which would report "timed out after 0ms" if timeout_ms was None. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
Restore # Examples, # Arguments, and # Returns sections on all three public engine functions (evaluate_single_rule, evaluate_rules, evaluate_rules_with_config) that were dropped during the extraction to engine.rs. This restores 3 doc tests (142 -> 145). Also fix: - evaluate_single_rule doc described 3 steps but code has 4 (add coercion step) - mod.rs module doc now reflects its role as types + re-exports - RuleMatch doc no longer claims to contain "the rule that matched" (it contains extracted fields) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
Reflect the refactoring from issue #59: remove deleted tests.rs, add new engine.rs, and update mod.rs description from "Main evaluation engine" to its actual role as public interface and re-exports. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
Move the ~1880-line #[cfg(test)] block from engine.rs into engine/tests.rs, bringing engine/mod.rs down to 332 lines (within the 500-600 line guideline). The engine module is now a directory with mod.rs for production code and tests.rs for unit tests. Also rename test functions with unclear "debug" naming: - test_debug_error_recovery -> test_evaluate_rules_skips_out_of_bounds_rule - test_debug_mixed_rules -> test_mixed_valid_and_invalid_rules_yield_valid_matches Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
Remove `pub use offset::*`, `pub use operators::*`, etc. that were introduced in the refactor. On main these were `pub mod` only, so the glob re-exports expanded the public API surface unintentionally. Also fix RecursionLimitExceeded propagation to bind and return the original error instead of reconstructing it with a potentially under-reported depth after decrementing. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
Reflect that engine is now a directory submodule (engine/mod.rs + engine/tests.rs) instead of a single engine.rs file. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.
Comments suppressed due to low confidence (2)
docs/ARCHITECTURE.md:94
- The architecture tree lists
engine.rs,offset.rs, andoperators.rs, but the current source layout uses anengine/submodule directory andoffset/+operators/submodules (directories). Updating this tree will keep the top-level architecture doc aligned with the code.
│ ├── evaluator/ # Rule evaluation engine
│ │ ├── mod.rs # Public API surface with re-exports, EvaluationContext, RuleMatch
│ │ ├── engine.rs # Core evaluation logic (evaluate_single_rule, evaluate_rules, evaluate_rules_with_config)
│ │ ├── offset.rs # Offset resolution
│ │ ├── types.rs # Type reading with bounds checking
│ │ ├── operators.rs # Comparison operations
│ │ └── strength.rs # Strength calculation and sorting
src/evaluator/engine/tests.rs:299
src/evaluator/engine/tests.rsis very large (~1,883 lines) and exceeds the repo’s 500–600 line source-file guideline. Since this PR is specifically aiming to split monolithic modules, consider splitting this test file into multiple focused test modules/files (e.g.,evaluate_single_rule_*,evaluate_rules_*, error recovery, end-to-end parsing) to keep navigation and future edits manageable.
Move the ~418-line #[cfg(test)] block from mod.rs into tests.rs, bringing mod.rs down to 250 lines (well within the 500-600 line guideline). Also replace brittle string-based error assertions with pattern matching on concrete error variants: - RecursionLimitExceeded: match variant and assert depth - InternalError: match variant directly Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
Update evaluator.md, architecture.md, and AGENTS.md to reflect the actual directory structure: engine/ is a directory (not engine.rs), offset/ and operators/ are directories (not flat files), and mod.rs now has a companion tests.rs file. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: UncleSp1d3r <unclesp1d3r@evilbitlabs.io>
There was a problem hiding this comment.
Stale comment
Security review completed for PR #153.
Findings (priority-ordered)
- No high-confidence vulnerabilities found in the diff.
What I validated
- Buffer safety: no new unchecked indexing on untrusted buffers in production evaluator code; rule evaluation still routes through offset/type readers with error handling.
- Integer/offset safety: no new risky integer casts or offset arithmetic paths introduced by the refactor; timeout comparison remains bounded and uses widening conversion.
- DoS/resource controls: recursion depth and timeout controls are still enforced; this PR preserves and slightly improves critical-error propagation for timeout/recursion-limit paths.
- Unsafe/panics: no new
unsafe,unwrap(), orexpect()in library runtime paths introduced by this PR.- Dependency risk: no dependency-manifest changes in this PR.
- Info disclosure: no new error-message paths that expose buffer contents or internal paths.
Security result
- Status: pass (no security blocker identified in this PR).
Residual risk note: this PR is largely a module split/refactor, so baseline evaluator complexity risks (large rule sets until timeout) remain project-level concerns rather than newly introduced issues here.
There was a problem hiding this comment.
Security Review Result: PASS
No high-confidence vulnerabilities found in this PR.
What I reviewed
- Buffer safety and offset arithmetic in
src/evaluator/engine/mod.rs(new location of core evaluator logic) - Integer/overflow behavior in rule counting, timeout checks, and recursion-depth handling
- DoS/resource controls (timeout path, recursion-depth limit behavior)
- Forbidden patterns (
unsafe, raw pointers,unwrap/expectin library code) - Dependency/supply-chain risk impact from the diff
Findings (prioritized)
- No confirmed vulnerabilities introduced by this diff.
Notes
- The production-code changes are a refactor/split of evaluator logic into
engineplus test/docs moves; no new dependency files were changed (Cargo.toml/Cargo.lockunchanged). cargo auditcompleted successfully (no advisories reported for the current lockfile).cargo deny checkpassed (advisories ok, bans ok, licenses ok, sources ok).
Residual risk
- As with prior behavior, pathological rule sets can still consume CPU up to configured timeout/recursion limits; this PR does not appear to weaken those protections.
There was a problem hiding this comment.
PR Risk Assessment
Risk level: Medium-High
Code review required: Yes
Evidence-based assessment (from diff)
- Core runtime evaluator logic was moved from
src/evaluator/mod.rsinto newsrc/evaluator/engine/mod.rsand re-exported. - This touches a shared, performance-sensitive library codepath (
evaluate_single_rule,evaluate_rules,evaluate_rules_with_config) used broadly by file-type detection. - The refactor is large (
~4kLOC churn including tests/docs), increasing regression surface even when mostly structural. - There are behavioral-path changes in error propagation inside recursive child evaluation (timeout/recursion error handling), so this is not purely formatting/mechanical.
- Prompt/instruction documentation was also updated in
AGENTS.md(non-runtime, but still behavior-relevant for automation guidance).
Reviewer assignment outcome
- Checked existing assigned reviewers: none.
- Attempted to request a non-bot reviewer (
param-jasani) based on recent contribution history. - GitHub rejected assignment because there are no eligible non-author collaborators available for this repository.
Approval decision
- Per policy,
Medium-Highrisk PRs are not auto-approved. - Also noted an existing approval from another bot reviewer; no additional approval was added by this automation.
If this PR is updated later, risk will be re-evaluated and any prior automation approval will be dismissed if risk increases.
## 🤖 New release * `libmagic-rs`: 0.4.0 -> 0.4.1 (✓ API compatible changes) <details><summary><i><b>Changelog</b></i></summary><p> <blockquote> ## [0.4.1] - 2026-03-06 ### Refactor - **evaluator**: Split mod.rs into focused submodules ([#153](#153)) <!-- generated by git-cliff --> </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/release-plz/release-plz/). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>


Summary
Closes #59
evaluate_single_rule,evaluate_rules,evaluate_rules_with_config) into newevaluator/engine.rsmodule (~2,096 lines)evaluator/mod.rsto public API surface only (EvaluationContext,RuleMatch, re-exports) -- now ~720 linesmod.rsinto the engine module where the logic livesDetails
evaluator/mod.rswas 2,638 lines (4.4x the 500-600 line guideline). This split separates concerns ahead of v0.2.0 work (comparison operators, indirect/relative offsets) which will add more evaluation logic.No public API changes -- all existing exports are preserved via re-exports in
mod.rs.Test plan
cargo test)evaluator/mod.rsis under 600 lines target (currently ~720 -- within acceptable range)Generated with Claude Code