Skip to content

Magika 2.0: custom rules, and one fast pipeline for the CLI, Python and C #1537

Description

@ebursztein

This issue tracks the work that puts Magika's rules and speed in the library, where every front end gets them, instead of in the CLI alone. Each PR below is self-contained and says Part of this issue.

Design

  • The low-level library API stays the real API:

    • Builder/Runtime;
    • FeaturesOrRuled::extract_file / extract_content;
    • Session::identify_features_batch.

    Integrators build their own pipelines on it.

  • Custom rules live in Options (Options::custom_rules, a cheaply cloned Rules), so extraction applies them in any pipeline.

    • Order: custom rules, then built-in rules, then the model, each when enabled.
    • Veto: custom rules veto the model the same way built-in rules do since Veto ML output when rules have no false negatives #1529. A content type whose enforced rules are all class = "full" is vetoed when none of them matched.
  • Shared orchestration for our front ends lives in magika::pipeline, behind a pipeline feature that is off by default, and uses the public API only.

    • Engine: identifies from any thread, on the CPU at once and on the GPU once it's ready.
    • Pipeline: parallel walk, read, batch, infer and reorder.
    • Front ends: the CLI, Python and C use these helpers instead of their own copies.

PRs in progress

PR What Status
#1529 Veto ML output when rules have no false negatives (@ia0); also reads the zip tail In review (Yanick); prerequisite of #1530
#1530 Custom rules before the built-in rules: library Options::custom_rules, CLI --rules-file / --rules-check, Python rules= / rules_files=, C magika_rules_new; includes two fixes to #1529 (GPU tests; veto when features were extracted without rules) Ready, stacked on #1529
#1531 Engine and Pipeline in magika::pipeline (feature off by default); CLI, Python and C use them; C magika_engine_identify_paths Ready, stacked on #1530
#1532 Prepare CPU plans beyond the smallest on first use; fixes #1497 ✅ Merged
#1533 python: accept raw binary streams in identify_stream; fixes #1403, carries #1426 forward with its author Ready
#1476 python: reject a bare str or bytes in identify_paths (@kaluli123123); fixes #1472 Ready, verified on main
#1475 python: propagate the real error for unseekable streams (@kaluli123123); fixes #1473 Ready, verified on main

Merged earlier, from splitting #1447

PR What
#1462 Add the magika-rules crate
#1463 Detect recursive directory cycles in the CLI
#1464 Harden the CLI against bad limits, special files and pipeline errors
#1465 Read small files once during feature extraction
#1466 Fix tract convolution padding and check every GPU batch plan at startup
#1468 Restore the reference conformance tests and qualify GPU decisions
#1470 Link the C library on macOS
#1471 Add 62 content types to the knowledge base
#1484 Look up content types by label
#1486 Fix release scripts on macOS and stop persisting CI checkout tokens
#1487 Identify files with format rules in the library and CLI
#1489 Start the tract runtime without parsing NNEF
#1490 Identify on the CPU while the GPU is prepared
#1491 Update the CLI README with BSD sed too
#1492 Compile the bundled rules when the crate is built
#1494 Evaluate zip and PE facts in magika-rules
#1496 Build a rules regex only where a match can start

Closed

PR Why
#1447 The reference PR for the split; every lane is merged or listed here
#1495 Its labels landed with #1494, and its zip tail read lands with #1529
#1501 Implemented #1497 from outside the maintainers; #1532 landed instead
#1426, #1412 Same fix for #1403; carried forward in #1533
#1407 The Python symlink race no longer exists since symlinks are handled in Rust
#1379, #1362, #1317 They targeted the Python fallback CLI, removed with #1483 (#1243 stays open)

Next

Rules: preparation and packaging

  • Authoring: authors edit rust/rules/rulesets/{full,partial,notworking}/*.yar.
  • Generation: rust/sync.sh regenerates the committed src/bundled.rs and the rule labels, which rust/gen checks against the knowledge base YAML.
  • Freshness: a test fails if bundled.rs is stale.
  • Shipping: the rules are compiled into the CLI, the Python extension and the C library, so no rule file is read at runtime.
  • Custom rules: they use the same validation, and are compiled once when the options are built.

Testing

  • In every PR: rust/test.sh (library, CLI, rules, C, generator), rust/changelog.sh, and the Python suite against the PyO3 build.
  • Rules PRs: the rules dataset gate (0 false positives).
  • Performance: front-end changes report interleaved before/after timings for 1 to 3,000 files.

🤖 Generated with Claude Code

Activity

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