{{ message }}
feat(aimds): aidefence-core Rust crate over the shared AIDefence pattern packs - #102
Merged
Conversation
ruvnet
force-pushed
the
feat/aidefence-core-rust
branch
from
September 4, 2026 21:14
d69beb3 to
eb1160c
Compare
ruvnet
force-pushed
the
feat/aidefence-core-rust
branch
2 times, most recently
from
September 4, 2026 21:18
546468d to
98877c9
Compare
…pattern packs
Adds AIMDS/crates/aidefence-core to the AIMDS workspace: a deterministic
detection core so Rust services (cognitum-one Slack bot, cognitum-cogs)
run the same detection as the aidefence npm package.
- Loads the shared AIMDS/patterns/*.json packs (core = the 25 patterns of
@claude-flow/aidefence 3.0.2, plus tool_invocation, exfil_url,
encoded_instruction, slack_markup_forgery, instruction_override_i18n):
include_str! at build time and Registry::from_dir at runtime. No regex
is hand-copied into Rust.
- Mirrors AIMDS/src/detection/engine.ts: normalisation, separator/leet/
compact variants, bounded base64/hex/url/rot13/reverse decoding with one
rescan, per-id dedupe, variant confidence penalties.
- JS-to-regex-crate translation (ASCII \b \d \w, inline flags) with an
explicit divergence ledger: CORE-005's trailing (?!...) becomes a
not_followed_by post-check; {n,2048} repeats in EX-001/002/004 and
SL-004 are relaxed to {n,}. Skipped or failing patterns are reported,
never silently dropped.
- tests/parity.rs: id-for-id, variant, decoding and confidence parity
with the TS engine on the shared 85-case corpus (snapshot in
tests/fixtures/ts-expected.json generated from the TS engine).
- API: detect / is_safe / sanitize (idempotent, PII-masking, never
truncates) / normalize / decode_and_rescan, Detector + Config (pack
overrides, 100 KB truncation, unsafe threshold), PII via patterns/pii.json.
- 45 tests: unit, examples, parity, proptest, runtime loader, 100 KB
latency gate (25 ms dev with regex crates at opt-level 3, 10 ms release).
wasm feature checked on wasm32-unknown-unknown.
Learning/HNSW/behavioural layers are intentionally not ported;
aimds-detection is untouched (unification recommended as follow-up).
Co-Authored-By: RuFlo <ruv@ruv.net>
Claude-Session: https://claude.ai/code/session_013PKv3picsfzoQJDKLLW7Rt
ruvnet
force-pushed
the
feat/aidefence-core-rust
branch
from
September 5, 2026 13:34
98877c9 to
a5e8c6c
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
Adds
AIMDS/crates/aidefence-core, a Rust detection core in the AIMDS Cargo workspace, so Rust services (the cognitum-one Slack bot, cognitum-cogs) run the same AIDefence detection as the npm package.Stacked on
feat/aidefence-injection-packs(the pattern-pack PR #105, #105, head2bb5f27): the base of this PR is that branch, so the diff here is the crate only. Merge that PR first, then retarget this PR tomain(gh pr edit 102 --base main) before the pack branch is deleted.AIMDS/patterns/*.jsonat build time (include_str!) and can load the same directory at runtime (Registry::from_dir). Thecorepack is the 25-pattern set of@claude-flow/aidefence3.0.2 (ruflov3/@claude-flow/aidefence/src/domain/services/threat-detection-service.ts, the code behindaidefence_scan); the other five packs cover the misses measured 2026-09-04. Note: midstreamAIMDS/srcand the publishedaidefence@2.3.0carry no detection regexes of their own.AIMDS/src/detection/engine.ts: normalisation (NFKC, invisibles, the same confusables table, newlines kept), separator / leet / compact variants with\s+-free compact regexes, bounded base64 / hex / url / rot13 / reverse decoding with one rescan, per-id dedupe, variant confidence penalties.Registry::divergences, asserted in tests):CORE-005's trailing(?!going|about|ready)becomes anot_followed_bypost-check (one documented gap: JS would still matchyou are now goingwith two spaces). Counted repeats with a bound of 1024+ would be relaxed to unbounded and listed too, but the pack PR already replaced its{n,2048}bounds with+/*?, so today: 1 rewritten, 0 skipped, 0 errors across 53 patterns. Mid-pattern lookaround or backreferences are load errors;"engine": "js"patterns that fail becomeSkipped, never silent drops.tests/parity.rsrunsAIMDS/tests/fixtures/injection-corpus.json(85 cases) and requires, per case, the same pattern ids, matching variant, decoding and confidence as the TS engine (tests/fixtures/ts-expected.json, generated withtsxfrom the TS engine at2bb5f27; regeneration one-liner in the README). The TS unit assertions are mirrored one for one (known FPsF13/F27= exactlyCORE-018, leet case, decoded-from, no recursion, candidate cap, core-only 3.0.2 verdicts).detect,is_safe,sanitize(idempotent, PII-masking, never truncates),normalize,decode_and_rescan,Detector+Config(pack overrides honouringenabledByDefault, variants/decode toggles, 100 KB truncation, unsafe threshold). PII (6 patterns) lives in the crate'spatterns/pii.jsonbecause the shared packs carry none; the TS email class[A-Z|a-z]typo is fixed there.wasmfeature (wasm-bindgen exports) iscargo checked onwasm32-unknown-unknown; no wasm-pack build or JS smoke test.AIMDS/Cargo.toml: crate added tomembers; regex crates set toopt-level = 3in the dev profile (the packs' heavy regexes made the 100 KB latency gate ~290 ms unoptimised).Not ported (stated in README): ThreatLearningService (ReasoningBank-style learning, HNSW similarity, mitigation tracking), behavioural analysis, policy verification,
inputHash,quickScan/getStats, and 3.0.2's confidence arithmetic (both engines use the pack model instead).Recommendation.
AIMDS/crates/aimds-detectionstill carries its own older 10-literal + 5-regex matcher and PII sanitizer; it is untouched here and should be unified onto these packs in a follow-up so the workspace has one detection engine.Test plan
rust-ci.ymlruns the root workspace only and AIMDS is a separate workspace with a gitignoredCargo.lock, so these gates ran locally (rustc 1.97;rust-version = "1.81"declared, not verified). The crate embeds files outside its package root, so it is a git dependency, not publishable to crates.io as-is.🤖 Generated with RuFlo
https://claude.ai/code/session_013PKv3picsfzoQJDKLLW7Rt