Repository navigation
fix!: round-18/19/20 deep-review hardening (LSP encoding, ShiftSpans trait, FFI v2) - #1247
Conversation
There was a problem hiding this comment.
Pull request overview
Lands rounds 18–20 of deep-review hardening from #1244 that were in the working tree but missed the squash: a ShiftSpans trait that enforces span-shifting discipline at the type level in rustledger-core, encoding-correct Position/Range emission across all LSP handlers via threaded PositionEncoding, BOM tolerance and exhaustive ParseError sampling in the parser, and a breaking FFI bump to API v2.0 for the ParseErrorEntry wire shape.
Changes:
- Core:
ShiftSpanstrait +impl_shift_spans_noop!macro with per-variant binding patterns, making any newSpanned<T>field a compile-time error until its shift impl is added. - LSP:
LineIndex::new(source, encoding)andbyte_in_line_to_positionthreaded through ~30 handlers; typedDispatchError; duplicate-initializerejection; BOMCodeActiondedup;ranges_overlapsemantics aligned to LSP half-open. - FFI:
API_VERSION1.0 → 2.0 withParseErrorEntry[]structurederror.data.errors;path_securityopt-out onledger.loadFile;format_source_to_responseshared byformat.source/format.file; documented-32000vs-32700separation.
Reviewed changes
Copilot reviewed 77 out of 79 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| supply-chain/{imports.lock,config.toml} | Promote either from exemption to audited via Google/Mozilla deltas. |
| Cargo.toml, Cargo.lock | Pin ropey to cr_lines-only (match similar line semantics); add similar workspace dep; tempfile dev-dep for ffi-wasi tests. |
| crates/rustledger/src/cmd/{format,extract_cmd,add_cmd}/*.rs | Use new Alignment/format_directives/format_source path; -c becomes optional with auto-default. |
| crates/rustledger/tests/format_compat.rs + fixtures | Golden + idempotence + live bean-format parity tests for #1242. |
| crates/rustledger-core/src/lib.rs | Re-export Alignment, FormatLine, format_directives, ShiftSpans, etc. |
| crates/rustledger-core/src/span.rs | New ShiftSpans trait, blanket impls, impl_shift_spans_noop! macro, and rationale for not marking Span/Spanned non_exhaustive. |
| crates/rustledger-core/src/format/{transaction,directives}.rs | Two-phase formatter: *_lines render phase + render_lines align phase. |
| crates/rustledger-core/src/synthetic/edge_cases.rs, tests/synthetic_generation.rs | Update synthetic generation/tests to new format API. |
| crates/rustledger-loader/src/phase.rs | Replace clippy #[allow] with explanatory comment for non-const into_inner. |
| crates/rustledger-parser/src/{lib,error,bom}.rs, CHANGELOG.md | New bom module, has_leading_bom, BomInDirectiveBody variant, exhaustive every_kind_sample, #[non_exhaustive] on result/error types. |
| crates/rustledger-parser/tests/parser_integration_test.rs | Use format_directives in round-trip test. |
| crates/rustledger-lsp/Cargo.toml | Add similar dep for formatting line-diff. |
| crates/rustledger-lsp/src/server.rs | Thread PositionEncoding from negotiation into main loop. |
| crates/rustledger-lsp/src/handlers/*.rs | Thread encoding into ~30 handlers; replace raw byte-col emission with byte_in_line_to_position; share ranges_overlap via utils. |
| crates/rustledger-lsp/tests/encoding_coverage.rs | New end-to-end round-trip test exercising handlers under both UTF-8 and UTF-16. |
| crates/rustledger-wasm/src/{api,parsed_ledger,types}.rs | Use format_source for whole-file formatting; doc fix for TypedValueJson. |
| crates/rustledger-wasm/bindings/{types.py,index.schema.json} | Sync regenerated bindings doc-string. |
| crates/rustledger-ffi-wasi/src/lib.rs, CHANGELOG.md, README.md, openrpc.json | API_VERSION 1.0 → 2.0; document ParseErrorEntry shape; path_security schema. |
| crates/rustledger-ffi-wasi/src/jsonrpc/{router,response,request,error,mod}.rs | Shared format_source_to_response; beancount_parse_error (-32000) vs parse_error (-32700) split; stdin IO failure now internal_error; path_security param plumbed to Loader::with_path_security. |
| crates/rustledger-ffi-wasi/Cargo.toml + tests/{openrpc_kind_code_sync,load_file_path_security}.rs | New tests pin openrpc.json ↔ ParseErrorKind sync and path_security opt-in/opt-out behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…rge) Captures the round-18/19/20 deep-review work that was in the working tree of the #1244 PR branch but never committed, so it did not land in the squash-merge. Preserved on its own branch as a starting point for a follow-up PR. Scope (~47 files, +4382/-1084): rustledger-core: - ShiftSpans trait with per-variant binding discipline - HashMap<K, V, S> blanket impl, replacing open-coded shift_metadata rustledger-lsp: - LineIndex with negotiated PositionEncoding (UTF-8/UTF-16) and lazy ropey::Rope (OnceCell, never built under UTF-8) - byte_in_line_to_position helper with strict overshoot rejection - 30+ handlers migrated for encoding-aware positions - DispatchError typed enum replacing prefix-string routing - Duplicate initialize rejection per LSP 3.17 §Lifecycle - BOM CodeAction dedup via BTreeSet<usize> - Currency/account collectors walk parser currency_occurrences index rustledger-parser: - Mid-file BOM tolerated as lexer error preserving following indent - ParseError::every_kind_sample exhaustive via variant_index match rustledger-ffi-wasi: - API_VERSION 1.0 -> 2.0 wire-shape bump (breaking) Verified pre-stash: cargo test --all-features (workspace, zero failures), clippy -D warnings clean, fmt clean, rustdoc clean. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
b336388 to
cb3fb73
Compare
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
📊 Benchmark Results
vs main branch (2026-06-02)
What do these numbers mean?
|
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Summary
Follow-up to #1244. The original PR was a one-shot fix for
rledger formatvsbean-formatbyte divergence (issue #1242) but accumulated 20 rounds of iterative deep review. The first 17 rounds landed in #1244. Rounds 18, 19, and 20 were in the working tree at merge time but uncommitted, so they did not make the squash. This PR lands them.All findings here originated from deep-review passes that exercised the same areas #1244 touched (LSP, parser, FFI, core span machinery). They are the architectural hardening that the format-compat work surfaced.
Changes by area
rustledger-core:ShiftSpanstrait with compile-time disciplineReplaces the prior open-coded
shift_metadatafree function plus wildcardSelf::X(_) | Self::Y(_) => {}no-op arms with aShiftSpanstrait, per-variant binding patterns (Self::Variant(v) => v.shift_spans(shift)), and aHashMap<K, V, S>blanket impl. A newSpanned<T>field on any variant now compiles only after its impl shifts the span. The discipline is enforced at the type level.rustledger-lsp: encoding-correct positions across every handlerLineIndexwith negotiatedPositionEncoding. Every handler that emits aPositionorRangenow threads UTF-8 / UTF-16 negotiation through a single encoding-aware index (offset_to_position,position_to_offset,byte_in_line_to_position). Lazyropey::Ropefor UTF-16 (never built under UTF-8 negotiation).byte_in_line_to_positionhelper. Collapses the recurringline.find(needle)thenemit Positionpattern that previously emitted raw byte offsets as columns (wrong under UTF-16 on non-ASCII). Strictly rejects byte offsets that overshoot the addressed line, instead of silently routing to the next line via the source-len clamp.DispatchErrorenum. Request dispatch now routes viaMethodNotFound/DuplicateInitialize/Handlervariants instead ofmsg.starts_with(...)prefix-string matching.initializerejection. Per LSP 3.17 §Lifecycle, a secondinitializeis a protocol violation; previously the server silently rebuilt a strippedServerCapabilities(downgrading the session). Now returnsInvalidRequest.CodeActiondedup. MultipleBomInDirectiveBodyerrors at the same offset collapse into one quick-fix viaBTreeSet<usize>.currency_occurrencesindex. Replaces bespoke string-search that mutated currency-substrings inside payee strings and account names (linked-edit, rename, and highlight were corrupting unrelated text as the user typed).rustledger-parser: BOM handling and exhaustivenessParseError::every_kind_sampleexhaustive. Gated by a compile-time-exhaustivevariant_indexmatch so new variants can't silently slip out of the sample list.rustledger-ffi-wasi: API v2.0 (breaking)API_VERSIONbumped from1.0to2.0to mark theParseErrorEntry.error.data.errorswire-shape break. CHANGELOG, README, OpenRPC schema, and tests updated.Breaking change
FFI-WASI
API_VERSIONbumped to2.0.ParseErrorEntry.error.data.errorsis now an object array; consumers pinned to the1.xstring-array shape must update.How to test
cargo test --all-features: full workspace, 2925 tests, zero failurescargo test -p rustledger-lsp --all-features: 192 LSP tests includingtest_byte_in_line_to_position_strict_overshoot,dispatch_error_codes,dispatch_error_display, and the round-trip encoding coverage suite under both UTF-8 and UTF-16cargo test -p rustledger-core --all-features: pinsmetadata_shift_spans_dispatches_via_value_implfor the trait wiringTest plan
cargo test --all-features: workspace-wide, 2925 passed / 0 failedcargo clippy --all-features --all-targets -- -D warnings: cleancargo fmt --all -- --check: cleanRUSTDOCFLAGS=-Dwarnings cargo doc --workspace --no-deps --all-features: cleanFollow-up to #1244.
🤖 Generated with Claude Code