Skip to content

feat: native Rust implementation of the Lore CLI (parity port) - #71

Open
Ian-stetsenko wants to merge 12 commits into
mainfrom
feat/rust-rewrite
Open

Ian-stetsenko wants to merge 12 commits into
mainfrom
feat/rust-rewrite

Conversation

@Ian-stetsenko

@Ian-stetsenko Ian-stetsenko commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

A native Rust implementation of the whole Lore CLI under rust/, as a parity port of the TypeScript sources: same 16 commands and flags, same text and JSON output (key names and order), same exit codes, same .lore/config.toml schema and merge rules, same commit-msg hook script.

This PR is additive. The npm package, src/, tests/ and the existing CI are untouched, so open PRs and the 0.6.0 release path are unaffected. The plan for retiring the TypeScript sources is in rust/README.md.

Why

  • Startup. The commit-msg hook runs lore validate on every commit and agents call lore in loops. Measured on this machine (20 runs each, same harness):

    command Node (dist/main.js) Rust (target/release/lore)
    lore --version 336 ms 60 ms
    lore log --json (this repo) 366 ms 131 ms

    Most of the remaining Rust time is process spawning on Windows plus the git calls themselves.

  • Distribution. A 3.4 MB static binary with no runtime dependency; hooks stop breaking on missing or mismatched Node.

  • Headroom. In-process git (gitoxide) and a persistent atom index become possible later. Deliberately not in this PR: the port shells out to git exactly like GitClient does today so behaviour stays byte-comparable.

What is in the box

rust/
  Cargo.toml            crate `lore-cli`, binary `lore`, MSRV 1.85
  src/main.rs           composition root (the only place concrete types are wired)
  src/cli.rs            clap definitions for every command and flag
  src/commands/         one module per command + shared path-query pipeline
  src/formatters/       TextFormatter (chalk-compatible ANSI), JsonFormatter
  src/services/         1:1 with src/services/*.ts (atom_repository.rs, validator.rs, ...)
  src/interfaces/       traits mirroring src/interfaces/*.ts
  src/types/ src/util/  domain model, config schema, constants, error -> exit-code mapping
  src/test_support/     MockGitClient, MockPrompt, fixtures (test builds only)
  tests/                git integration tests + 34 end-to-end CLI scenarios
.github/workflows/rust.yml   fmt + clippy, tests on Linux/macOS/Windows, MSRV check

The layering rules from CLAUDE.md hold (commands -> services -> interfaces/types; nothing in services/ imports commands/ or formatters/). Module names map 1:1 onto the TypeScript files so behaviour can be diffed side by side.

Tests

suite count
unit tests (every tests/unit/**/*.test.ts case ported case-for-case, plus Rust-only edge cases) 629
end-to-end: built binary against throwaway git repos (exit codes, text/JSON output, amend, hooks, validate modes, stale, squash, doctor) 34
git integration (port of tests/integration/git-client.integration.test.ts + more) 9

cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo +1.85 check are clean. All suites pass on Windows (this machine); CI covers Linux and macOS.

Deliberate behaviour differences

All documented in code comments and in rust/README.md:

  • Update notifier: every opt-out (--no-update-notifier, LORE_NO_UPDATE_CHECK, NO_UPDATE_NOTIFIER, CI, JSON, [cli] update_check) is honoured, but no network check runs until there is a release channel for binaries (the Node package checked npm).
  • Usage errors exit 1 like Commander; --help/--version exit 0 and --version prints the bare version.
  • Formatter is built once up front (clap parses global options eagerly) instead of the lazy getFormatter() factory.
  • Unparseable author dates become the Unix epoch instead of crashing on Invalid Date.
  • Lore-id index caches only a successful build (TS cached a rejected promise).
  • Lengths counted in Unicode scalars or UTF-16 units where the TS tests pin one or the other; identical for all BMP text.

Follow-ups (not in this PR)

  1. Release workflow publishing prebuilt binaries from tags, and an npm shim so npm install -g lore-protocol keeps working.
  2. Decide gitoxide vs shelling out before any index work (Enhancement: Query result pagination with cached result sets #29).
  3. Retire src/ and move rust/ to the repository root once a release has been exercised by the hook and by agents.

Ian-stetsenko and others added 12 commits October 29, 2026 13:38
…ler parser, test doubles

Lays the foundation for the Rust implementation of the Lore CLI under rust/:
domain types, config schema, query/output shapes, the trait ports that
mirror the TypeScript interfaces, protocol constants and error/exit-code
mapping, clap definitions for every command, the TrailerParser, and shared
test doubles (MockGitClient, MockPrompt, fixtures). Service, formatter and
command modules are placeholders filled in by the following commits.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…s for the Rust port

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Port JsonFormatter and TextFormatter from the TypeScript implementation
with case-for-case test ports. Key order, nesting and literal strings
match the TS output exactly; the text formatter reproduces chalk level-1
ANSI sequences (including dim.strikethrough nesting and per-line
re-styling of multi-line text) without a colour crate.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…lter, squash merger

Port slice A of the Rust parity work:
- PathResolver (parse_target / to_git_log_args / to_git_blame_args, BlameArgs
  uses Option<u32> instead of the TS -1 end-of-file sentinel)
- RandomLoreIdGenerator implementing the LoreIdGenerator port via rand
- SupersessionResolver (direct + transitive chains, cycle-safe)
- SearchFilter (all lore search filters plus has/text helpers)
- SquashMerger with SquashOptions; empty input returns LoreError
- Case-for-case ports of the vitest suites for CustomTrailerCollection and
  TrailerParser; new unit tests for SearchFilter (no TS test file exists)
- TrailerParser: use slice::contains for the custom-key check (clippy)

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…reIdReader, TomlConfigLoader

Parity-first port of the query/validation/staleness/config services with
their vitest suites ported case-for-case. Also fixes a pre-existing
manual_contains clippy lint in trailer_parser.rs that blocked -D warnings.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…input readers and collectors

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Wires all sixteen commands (context, constraints, rejected, directives,
tested/coverage, why, search, log, stale, trace, commit, validate, squash,
doctor, hooks, init) with clap, ports the command unit tests, and adds an
end-to-end suite that drives the built binary against throwaway git
repositories (34 scenarios: exit codes, text and JSON output, hooks, amend).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

@TommyC81 TommyC81 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes on exactly one point; everything else here is exemplary.

What I verified (with file evidence):

  • Layering holds: no commands/formatters imports under services/; main.rs is the sole composition root (concretes wired at main.rs:81-111, error-to-exit mapping at :55-70,271-295).
  • Exit codes 1/2/3 message-identical to src/util/errors.ts (rust/src/util/errors.rs:69-73).
  • JSON output key order preserved (serde_json preserve_order, Cargo.toml:29); log/trace key sequences identical to the TS formatters (json_formatter.rs:126-164,219-245).
  • Config defaults, merge precedence, and aliases identical; hook script line-for-line identical (hook_script_generator.rs:20-108); amend preserves the Lore-id the same way (commit.rs:67-87).
  • Test claims exact: 629 unit / 34 e2e / 9 integration; the e2e suite asserts exit codes and JSON shape with a hermetic git environment and zero hardcoded temp paths.
  • Zero unsafe, zero #[allow], no production unwrap, a small dependency tree, and rust.yml runs fmt + clippy -D warnings + tests on all three OSes + MSRV. Genuinely nice work.

The blocker: the JSON commit-input path ports the silent-drop bug, and its tests enshrine it.

rust/src/services/readers/json_input_reader.rs mirrors the 0.6.0 TS reader: unknown top-level keys are never inspected; non-string intent becomes "" (json_input_reader.rs:26-28); non-string body becomes None (:29-31); non-object trailers is dropped (:33-35); and the tests assert that leniency (should_default_intent_to_empty_string_when_not_a_string, json_input_reader/tests.rs:85; trailers_that_are_not_an_object_are_ignored, :222). In the TS CLI this is a live bug: a flat schema ({"intent": "...", "Constraint": [...]}) commits with exit 0 and every trailer dropped, and lore validate passes the barren atom.

Strict top-level validation is ready as #72 (commit a648bce, Lore-id cd854cfb): reject unknown top-level keys (naming them and the allowed schema), wrong-typed intent/body/trailers, non-object payloads, and invalid JSON, all as exit-1 errors. If this PR ships the lenient behavior, the two CLIs diverge the moment #72 merges. Recommend porting the strict validation instead - landing #72 first gives the Rust side an exact spec to port against, tests included.

Nits (none blocking):

  • The description says nothing outside rust/ plus one workflow file changes; the PR also adds .gitattributes and edits README.md (native-build paragraph) and CLAUDE.md (Rust section). All fine by me - just amend the description.
  • Two micro-divergences worth a line on the rust/README.md deltas list: Rust drops non-string elements in config arrays (config_loader.rs:283-295) where TS keeps them (config-loader.ts:168-169); hook chmod is #[cfg(unix)] (hook_installer.rs:56-62) versus unconditional in TS (a no-op difference on Windows either way).
  • One e2e test asserting JSON key order (not just shape) would pin the preserve_order guarantee the parity claim rests on.

The documented deltas (update-check opt-outs without a network check, usage errors exit 1, eager formatter, epoch fallback for unparseable dates, id-index caching successful builds only, Unicode length counting) all read as reasonable and are properly documented.

With the blocker addressed (or sequenced behind #72): happy to approve.

@TommyC81

Copy link
Copy Markdown
Collaborator

One heads-up since my review above: main has two more changes in flight that touch the port's parity surface.

#74 is the one to watch - it changes Lore-id lookup semantics to HEAD-reachable-first (with an --all fallback on miss), so without the same two-pass logic the port would re-introduce the #57 side-branch shadowing in Rust. #73 is smaller: it adds validate --from with a --since deprecation path (removal in 0.7).

Happy to re-review once you push.

This branch has not been deployed

No deployments
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.

2 participants