| 1 | # Porting Notes: `last30days-core` |
| 2 | |
| 3 | Follows the `rust-port` and `rust-best-practices` skills and lints from |
| 4 | https://github.com/morganlinton/BeautifulRust. |
| 5 | |
| 6 | ## Architecture |
| 7 | |
| 8 | End users never install Rust or Cargo. `rust/` compiles to a single standalone |
| 9 | native executable (`last30days-core`) linked only to standard OS libraries. |
| 10 | `skills/last30days/scripts/lib/rust_accel.py` spawns it lazily as a persistent |
| 11 | newline-delimited JSON stdio worker when present, and falls back transparently |
| 12 | to the pure-Python modules when absent or when `LAST30DAYS_ENGINE=python` is set. |
| 13 | |
| 14 | ## API Mapping |
| 15 | |
| 16 | | Python (`skills/last30days/scripts/lib/`) | Rust (`last30days-core`) | |
| 17 | |---|---| |
| 18 | | `cjk.has_cjk`, `cjk.segment` (bigram path) | `cjk::has_cjk`, `cjk::segment` | |
| 19 | | `dedupe.normalize_text`, `get_ngrams`, `jaccard_similarity`, `token_jaccard`, `hybrid_similarity`, `dedupe_items` (pass 2) | `dedupe::normalize_text`, `ngrams`, `jaccard_similarity`, `token_jaccard`, `hybrid_similarity`, `dedupe_kept_indices` | |
| 20 | | `relevance.tokenize`, `PreparedQuery`, `token_overlap_relevance` | `relevance::tokenize`, `PreparedQuery`, `token_overlap_relevance` | |
| 21 | | `dates.recency_score`, `signals.source_quality`, `engagement_raw`, `engagement_score`, `freshness`, `local_relevance`, `annotate_stream` | `signals::recency_score_from_days`, `source_quality`, `engagement_raw`, `engagement_score`, `freshness_from_days`, `local_relevance`, `annotate_stream` | |
| 22 | | `entity_extract.has_anchor_signal`, `extract_text_entities`, `entity_overlap` | `entity::has_anchor_signal`, `extract_text_entities`, `entity_overlap` | |
| 23 | | `cluster.cluster_candidates`, `_mmr_representatives`, `_merge_entity_clusters` | `cluster::cluster_candidates` | |
| 24 | | `library.has_private_corpus`, `is_unbounded_private_brief`, `public_markdown`, `slugify`, `parse_markdown_file` | `markdown::has_private_corpus`, `is_unbounded_private_brief`, `public_markdown`, `slugify`, `parse_markdown_meta`, `sanitize_markdown_batch`, `scan_markdown_files_batch` | |
| 25 | | `library_index._without_library_context`, `_fingerprint` | `markdown::without_library_context`, `indexable_fingerprint_from_public` | |
| 26 | |
| 27 | ## Test Mapping |
| 28 | |
| 29 | | Python test file | Rust test in `crates/last30days-core/tests/parity_tests.rs` | |
| 30 | |---|---| |
| 31 | | `tests/test_cjk.py` | `cjk_has_cjk_matches_python`, `cjk_segment_ascii_chinese_mixed_and_single`, `cjk_chinese_relevance_and_phrase_bonus`, `cjk_chinese_dedupe` | |
| 32 | | `tests/test_dedupe_v3.py` | `dedupe_normalize_text`, `dedupe_ngrams`, `dedupe_jaccard_and_token_jaccard`, `dedupe_kept_indices_behavior` | |
| 33 | | `tests/test_relevance.py` | `relevance_tokenize_and_synonyms`, `relevance_scoring_and_hashtags` | |
| 34 | | `tests/test_signals_v3.py` | `signals_engagement_formulas`, `signals_annotate_stream_orders_relevant_above_noise` | |
| 35 | | `tests/test_cluster_v3.py`, `tests/test_entity_extract.py` | `entity_anchor_and_overlap`, `cluster_cross_source_merge_and_polymarket_isolation`, `cluster_stale_candidate_demoted_below_fresh` | |
| 36 | | `tests/test_library_feed.py`, `tests/test_library_search.py` | `markdown_redacts_private_blocks_and_drills_respecting_fences`, `markdown_unbounded_private_brief_excluded`, `markdown_batch_and_meta_parse` | |
| 37 | |
| 38 | ## Deliberate Differences |
| 39 | |
| 40 | - **Set fingerprinting in `PreparedText`**: Instead of allocating a `String` per |
| 41 | character trigram and token, `PreparedText` stores sorted, deduplicated 64-bit |
| 42 | FNV-1a + SplitMix64 fingerprints (`Vec<u64>`) with exact cardinality and |
| 43 | remaining-element early-exit bounds in the two-pointer merge. Results match |
| 44 | Python Jaccard ratios while eliminating per-trigram heap churn. |
| 45 | - **Optional `jieba` segmentation**: `last30days-core` implements the default |
| 46 | dictionary-free CJK bigram path. When optional `jieba` is installed in Python |
| 47 | and input text contains CJK characters, the Python bridge automatically stays |
| 48 | on Python so `jieba` behavior is never altered. |
| 49 | - **Exact-URL dedupe and `SourceItem` metadata merging**: Pass 1 of |
| 50 | `dedupe.dedupe_items` stays in Python so source-specific metadata merges |
| 51 | (Reddit provenance, Polymarket match bundles, comment unions) remain lossless; |
| 52 | pass 2 (the quadratic fuzzy similarity loop) runs in Rust. |
| 53 | - **No `unsafe` code**: `unsafe_code = "forbid"` is enforced at workspace level. |
| 54 | POSIX `O_NOFOLLOW` file open uses `std::os::unix::fs::OpenOptionsExt` without |
| 55 | any `unsafe` blocks. |
| 56 | - **Parallel library scans**: `scan_markdown_files_batch` and |
| 57 | `sanitize_markdown_batch` use `rayon` to sanitize and fingerprint saved |
| 58 | Markdown reports across CPU cores in a single pass instead of running |
| 59 | `public_markdown` twice per file on one thread. |
| 60 | |
| 61 | ## Preserved Quirks |
| 62 | |
| 63 | - Python banker's rounding (`round_ties_even`) is used for `token_overlap_relevance` |
| 64 | (2 decimals) and `engagement_score` (nearest integer) so boundary values match. |
| 65 | - Python `min()` / `max()` first-wins tie breaking is preserved in |
| 66 | `cluster::mmr_representatives` and `cluster::cluster_candidates` (stable sort). |
| 67 | - `dedupe_items` keeps `UrlOnly` items (jobs and matched Polymarket events) and |
| 68 | empty-text items without inserting them into the fuzzy comparison pool. |
| 69 | - `library.public_markdown` preserves the exact CommonMark backtick/tilde code |
| 70 | fence tracking rules, marker checks, and private-block trailing-marker newline |
| 71 | insertion (`\n\n` when preceded by a public marker). |
| 72 | |
| 73 | ## Verification |
| 74 | |
| 75 | Run from `rust/`: |
| 76 | |
| 77 | ```bash |
| 78 | cargo fmt --check |
| 79 | cargo clippy --all-targets --all-features -- -D warnings |
| 80 | cargo test |
| 81 | RUSTDOCFLAGS="-D warnings" cargo doc --no-deps |
| 82 | cargo build --release |
| 83 | ``` |
| 84 |