Files
lucisv/docs/features/refactor-correctness-r1

refactor-correctness-r1

Goal

Fix five correctness, panic, and determinism issues in vlog_refactor:

  1. UTF-8 char-boundary panic in skip_string & callers (rename/home.rs, rename/carry.rs): advancing past a backslash escape in "\\é" or "\\中" with i += 2 lands mid-codepoint and panics subsequent string slices.
  2. SipHash nondeterminism and cycle-memoization bug in carry.rs: HashMap/HashSet iteration order combined with memoizing cycle-tainted false results in Walk::is_binder (and order-sensitive recursion in Walk::formals_of) produces non-deterministic carrier sets for mutually recursive macros.
  3. Duplicate-unit member rename ambiguity (#53) (rename/mod.rs): renameable_here and Scope::declaration previously checked index.units(&name).len() > 1 only for file_scope symbols; renaming a member/port/block declared inside a file-scope unit (module/interface/package/class/typedef) whose unit name appears more than once in index.units (matching kind) must refuse unambiguously with ambiguous(unit_name).
  4. Cross-module same-file .port(...) false body-use hit (#38) (ports/remove.rs): body_uses_in previously treated all semantics.references_of(id) in the file as body uses of the target module, including .port(...) connections on instances in another module in the same file.
  5. CRLF preservation in extract (#39) (extract/render.rs, extract/mod.rs): preserve \r\n vs \n from the source file across the extracted module, replacement instantiation, and \r\n\r\n same-file insertion.

Assumptions

  • Semantics::file_scope() owns all units indexed by WorkspaceIndex::units(name) (Module, Interface, Package, Typedef, Class), matching vlog_semantic/src/workspace/record.rs::is_unit.
  • Unit-kind matching treats Module/Interface (is_instantiable()) as sharing the instantiable namespace (matching ports/find.rs:52) while keeping Package, Class, and Typedef distinct by SymbolKind, so a package pk and module pk in the same workspace (top_rooted_paths::qa_f1_*) are not treated as duplicate declarations of one unit, and two non-duplicated packages pkg1/pkg2 with identically named classes/members remain renameable.
  • Source files using CRLF consistently contain \r\n; mixed line-ending files default to \r\n when any \r\n is present (matching rename/layout.rs), and instantiation line endings are normalized in extract/render.rs because instantiate::InstantiationPlan::render_with is outside this task's file ownership.

Handshakes

  • rename/preflight.rs (declared_here) (implemented in Round 1 Lane 1A QA-fix pass):
    • Shares super::enclosing_unit with Scope::declaration (rename/mod.rs) so declared_here defers (Ok(())) before already_named whenever enclosing_unit(semantics, declaration).is_some() (both file-scope declarations and declarations inside file-scope units). This keeps rename_preflight one-directionally consistent with rename on duplicate-unit members (qa_correctness_r1::qa_preflight_same_name_inside_duplicate_unit_matches_rename). crates/vlog_refactor/tests/preflight_oracle.rs::renaming_a_symbol_to_the_name_it_already_has_is_refused_from_the_buffer was updated to test buffer-only already_named outside file-scope units (a $unit function local) and deferral inside file-scope units (module sub).
  • lucisv_server/src/same_file.rs (renamed_here) (assigned to Lane 1B — untouched here):
    • same_file.rs:179-197 treats symbols with symbol.scope != file_scope() as eligible for single-file fast-path prepareRename when the workspace index is not consulted. Lane 1B owns updating same_file.rs for QA finding F2b so single-file prepareRename defers members inside file-scope units to match settled prepare_rename_here.
  • lucisv_mcp/src/tools/find.rs (engine_refusal):
    • Translates "which declaration this rename means" to "which declaration this position means". Reusing ambiguous(enclosing_unit) preserves exact parity across rename::prepare_rename, rename::prepare_rename_here, Scope::declaration, and lucisv_mcp's find_references.
  • crates/vlog_refactor/tests/qa_rename_implicit.rs (qa_a_port_rename_into_a_module_declared_twice_is_refused):
    • Previously, renaming a port inside duplicated module leaf passed Scope::declaration and failed downstream in connections.rs with "`u_leaf` at top.sv:2 connects with `.*` to `leaf`, which lucisv cannot read...". With #53 fixed, Scope::declaration now refuses upfront with "`leaf` is declared in more than one indexed file; lucisv will not guess which declaration this rename means."; the test assertion was updated accordingly (while rename_implicit::a_wildcard_into_a_module_declared_twice_is_refused, which renames in top.sv rather than leaf.sv, still exercises the u_leaf wildcard refusal unchanged).
  • crates/vlog_refactor/src/instantiate.rs:
    • InstantiationPlan::render_with emits \n. extract/render.rs::instantiation converts emitted \n to \r\n when the source uses CRLF so instantiate.rs stays untouched.

Status / TODO

  • Fix UTF-8 char-boundary handling in rename/home.rs and rename/carry.rs
  • Replace HashMap/HashSet and cycle-tainted memoization in rename/carry.rs with BTreeMap/BTreeSet + monotonic fixed-point computation
  • Refuse renames inside duplicated file-scope units in rename/mod.rs (renameable_here and Scope::declaration)
  • Filter ports/remove.rs::body_uses_in references to module_span(module)
  • Preserve source line endings (\r\n vs \n) in extract/render.rs and extract/mod.rs
  • Add/update regression tests in crates/vlog_refactor/tests/ and inline unit tests
  • Fix QA F1 (ports/remove.rs, ports/offer.rs): pass find_module's full module ... endmodule span into body_uses_in so empty-body modules check header port default expressions
  • Fix QA F2a (rename/preflight.rs, rename/mod.rs): share enclosing_unit in declared_here so same-name renames inside file-scope units defer to the ambiguity check
  • Pass formatting, clippy, full test suite, and author-stamp check

Work log

  • Changed rename/home.rs::skip_string to accept &str and step past escaped multi-byte UTF-8 characters (1 + char::len_utf8), and guarded string/slice indexing in home.rs (includes_in, macro_names, skip_string) and carry.rs (invocations, split_arguments) with .get(..) and UTF-8 character stepping.
  • Changed carry::Defs, Carriers::formals, and Carriers::binders from HashMap/HashSet to BTreeMap/BTreeSet; replaced Walk::is_binder and Walk::formals_of with order-independent monotonic fixed-point loops (find_binders least fixed point and compute_formals greatest fixed point so cycles remain carried/true).
  • Added enclosing_unit and is_duplicate_unit in rename/mod.rs, shared between renameable_here and Scope::declaration, refusing members/ports/blocks inside duplicated same-kind file-scope units with ambiguous(unit).
  • Filtered ports/remove.rs::body_uses_in references to module_span(module) so .port(...) connections in other modules within the same file are ignored.
  • Threaded line_ending(text) (\r\n vs \n) through extract/mod.rs (ensure_newline, module_text, instantiation, and {eol}{eol}{module_text} same-file insertion) and extract/render.rs (new_module, instantiation).
  • Added regression unit and integration tests in rename/home.rs, rename/carry.rs, tests/rename_macro_carry.rs, tests/rename.rs, tests/ports.rs, and tests/extract.rs, and updated tests/qa_rename_implicit.rs.
  • Production non-blank non-comment line counts (<= 600 limit):
    • crates/vlog_refactor/src/rename/home.rs: 211 lines
    • crates/vlog_refactor/src/rename/carry.rs: 233 lines
    • crates/vlog_refactor/src/rename/mod.rs: 495 lines
    • crates/vlog_refactor/src/ports/remove.rs: 288 lines
    • crates/vlog_refactor/src/extract/render.rs: 322 lines
    • crates/vlog_refactor/src/extract/mod.rs: 409 lines

2026-11-26 — Round 1 Lane 1A QA-fix pass (F1 & F2a)

  • QA F1 (crates/vlog_refactor/src/ports/remove.rs, crates/vlog_refactor/src/ports/offer.rs): Removed module_span(module) and updated body_uses_in to take module_range: TextRange directly from find_module's item.span (which covers the entire module ... endmodule declaration including header ports and default expressions while excluding sibling modules in the same file). Updated both plan_remove (ports/remove.rs) and removal (ports/offer.rs) to pass that range.
  • QA F2a (crates/vlog_refactor/src/rename/mod.rs, crates/vlog_refactor/src/rename/preflight.rs): Promoted enclosing_unit to pub(super) fn in rename/mod.rs and updated declared_here in rename/preflight.rs to defer (Ok(())) whenever enclosing_unit(semantics, declaration).is_some(), matching Scope::declaration's ambiguity-before-already_named ordering without duplicating logic.
  • Updated crates/vlog_refactor/tests/preflight_oracle.rs::renaming_a_symbol_to_the_name_it_already_has_is_refused_from_the_buffer to verify already_named still refuses from buffer outside file-scope units ($unit function local) and defers for declarations inside file-scope units (module sub).
  • Un-ignored and expanded crates/vlog_refactor/tests/qa_correctness_r1.rs::qa_remove_port_empty_body_module_with_default_expr_using_removed_port and qa_preflight_same_name_inside_duplicate_unit_matches_rename.
  • Updated production non-blank non-comment line counts (<= 600 limit):
    • crates/vlog_refactor/src/rename/home.rs: 211 lines
    • crates/vlog_refactor/src/rename/carry.rs: 233 lines
    • crates/vlog_refactor/src/rename/mod.rs: 495 lines
    • crates/vlog_refactor/src/rename/preflight.rs: 33 lines
    • crates/vlog_refactor/src/ports/remove.rs: 283 lines
    • crates/vlog_refactor/src/ports/offer.rs: 286 lines
    • crates/vlog_refactor/src/extract/render.rs: 322 lines
    • crates/vlog_refactor/src/extract/mod.rs: 409 lines

QA log

2026-11-26 — QA agent: FAIL — core fixes 1/2/5 hold, two concrete findings on #38 (module_span) and #53 (preflight / same_file handshakes)

Verdict: FAIL. Two concrete findings (F1 inside owned ports/remove.rs, F2 in the #53 handshakes noted as untouched by DEV). Fixes 1 (UTF-8 skip_string), 2 (carry.rs determinism), and 5 (extract CRLF) survived every adversarial attack.

Findings

  • F1 (BLOCKING — crates/vlog_refactor/src/ports/remove.rs:248-251): module_span(module) computes end = module.items.last().map_or(module.name_span.end, |item| item.span.end) and returns TextRange::new(module.name_span.start, end.max(module.name_span.end)). When module.items is empty (e.g., module leaf (\n input logic a,\n input logic b = a\n);\nendmodule\n), module_span covers only leaf (name_span), excluding the port header. As a result, body_uses_in (ports/remove.rs:223-227) fails to find port a (module_range.contains(s.name_span.start) is false) and returns Vec::new():

    • port_offers (crates/vlog_refactor/src/ports/offer.rs:302) falsely offers "lucisv: remove port a (no instantiation in scope)" despite b = a using a.
    • remove_port bypasses the "is still used inside ..." refusal and fails later in verify_splice with "removing port afromleafwould change whata means in leaf.sv; no edits were made." instead of "`a` is still used inside `leaf` at leaf.sv:3; remove those uses first.".
    • Reproduction: crates/vlog_refactor/tests/qa_correctness_r1.rs::qa_remove_port_empty_body_module_with_default_expr_using_removed_port (#[ignore = "QA finding 1..."]; find_module at ports/find.rs:95 already returns the full item.span covering module ... endmodule, which ports/remove.rs:99 and ports/offer.rs:298 currently discard).
  • F2 (BLOCKING — #53 parity breaks in crates/vlog_refactor/src/rename/preflight.rs:79-81 and crates/lucisv_server/src/same_file.rs:196): Both files were left untouched by DEV under strict file ownership and documented in ## Handshakes, but #53 breaks their invariants with concrete user-visible mismatches:

    • F2a (crates/vlog_refactor/src/rename/preflight.rs:79-81): declared_here only skips already_named when declaration.scope == semantics.file_scope(). With Scope::declaration now rejecting members inside duplicated file-scope units before already_named, renaming a member sig to "sig" inside duplicated module dup returns Err("sig is the name it already has.") from rename_preflight while rename returns Err("dup is declared in more than one indexed file; lucisv will not guess which declaration this rename means."). Since crates/lucisv_server/src/rename.rs:85 calls rename_preflight first, the LSP server returns the wrong refusal.
      • Reproduction: crates/vlog_refactor/tests/qa_correctness_r1.rs::qa_preflight_same_name_inside_duplicate_unit_matches_rename (#[ignore = "QA finding 2..."]).
    • F2b (crates/lucisv_server/src/same_file.rs:196): renamed_here only defers to the workspace index when symbol.scope == doc.semantics.file_scope(). With prepare_rename_here now reading index.units(...) for members inside file-scope units, an early/mid-scan textDocument/prepareRename (ahead.rs) on logic d; in dup_b.sv (module dup; logic d; endmodule) answers Ok({"placeholder": "d", ...}) while the settled server refuses with "lucisv: dup is declared in more than one indexed file; lucisv will not guess which declaration this rename means.".
      • Reproduction: Adding "dup_b.sv" (already written in shapes at crates/lucisv_server/src/server/tests/fast_actions_differential.rs:245) to the loop at line 272 fails early_answers_equal_settled_over_the_locality_shapes with differed=3 on dup_b.sv line 1 cols 8–9 (lucisv_server was immediately reverted so no unowned files remain modified).

Verification Summary

  • Diff / Ownership / Standards:
    • Modified/added files confined to crates/vlog_refactor/ (6 prod files, 5 existing test files, new tests/qa_correctness_r1.rs) and docs/features/refactor-correctness-r1/README.md.
    • Production non-blank, non-comment line counts before mod tests: home.rs 211, carry.rs 233, rename/mod.rs 495, ports/remove.rs 288, extract/render.rs 322, extract/mod.rs 409 (all <= 600).
    • Zero unwrap(), expect(), panic!, or unsafe in modified production code.
    • python3 tools/stamp_author.py --check: pass (would stamp 0 files; left 249 untouched).
  • Adversarial Checks (crates/vlog_refactor/src/rename/{home,carry}.rs + crates/vlog_refactor/tests/qa_correctness_r1.rs):
    • UTF-8 skip_string (home.rs, carry.rs): tested "\\\\", "\\\\\\", "\\é", "\\🚀", "\\ at EOF, multi-line strings in invocations/split_arguments, and non-ASCII comments/identifiers near strings — all passed without panic.
    • Determinism (carry.rs): tested 3-cycles (M1 -> M2 -> M3 -> M1 with binder and cross-position forwarding), reversed key names (Z3/Z2/Z1), and reversed definition order across 10 iterations — identical deterministic carrier/warning behavior.
    • Duplicate-unit member rename (rename/mod.rs): same-named module m + package m do not collide; two interface bus_if files refuse ports/signals/modports; module bus + interface bus collide; nested generate/for/if inside dup refuses; non-duplicated unique_m in the same file renames cleanly; duplicate file-scope typedef struct and class refuse; byte-for-byte prepare_rename vs prepare_rename_here parity holds on mixed.sv.
    • #38 (ports/remove.rs): same-file .p(sig) in another module + body use of p still refuses; same-file positional refuses; same-file .* warns; non-ANSI port and nested same-named port succeed; fails F1 on empty-body module with b = a port default.
    • #39 (extract/render.rs, extract/mod.rs): CRLF with imports + params + ports + // and /* */ comments + no trailing EOL (SameFile & NewFile) and zero-port/zero-param CRLF emit zero bare \n; LF control emits zero \r.
  • Gates (post-final-edit):
    • cargo fmt -p vlog_refactor -- --check: pass.
    • ./tools/guard.sh -m 6 -t 600 -- cargo clippy -p vlog_refactor -p lucisv_server -p lucisv_mcp --all-targets -- -D warnings: pass.
    • ./tools/guard.sh -m 6 -t 600 -- cargo test -p vlog_refactor --all-targets: pass (qa_correctness_r1: 7 passed, 0 failed, 2 ignored for F1/F2a; full vlog_refactor suite green).
    • ./tools/guard.sh -m 6 -t 600 -- cargo test -p lucisv_mcp --all-targets: pass.
    • ./tools/guard.sh -m 6 -t 600 -- cargo test -p lucisv_server --all-targets: pass.

2026-11-26 — QA re-check (post DEV fix pass): PASS (Lane 1A F1 & F2a resolved; F2b remains with Lane 1B)

Verdict: PASS. Re-verified Round 1, Lane 1A after the DEV fix pass. Both Lane 1A findings (F1 in ports/remove.rs & ports/offer.rs, F2a in rename/preflight.rs & rename/mod.rs) are fixed and survived expanded adversarial attacks; F2b (crates/lucisv_server/src/same_file.rs) remains tracked under Lane 1B per ## Handshakes.

Re-Verification Summary

  • Diff / Ownership / Standards:
    • git status / git diff: only crates/vlog_refactor/ (8 modified prod files, 6 modified test files, tests/qa_correctness_r1.rs) and docs/features/refactor-correctness-r1/README.md are modified/untracked.
    • Non-blank, non-comment prod lines before mod tests (<= 600): extract/mod.rs 409, extract/render.rs 322, ports/offer.rs 286, ports/remove.rs 283, rename/carry.rs 234, rename/home.rs 212, rename/mod.rs 495, rename/preflight.rs 33.
    • Zero unwrap(), expect(), panic!, or unsafe in modified production code.
    • python3 tools/stamp_author.py --check: exit 0 (would stamp 0 files; left 249 untouched).
  • F1 Attack (crates/vlog_refactor/src/ports/{remove,offer}.rs):
    • qa_correctness_r1::qa_remove_port_empty_body_module_with_default_expr_using_removed_port runs un-ignored and passes.
    • Added qa_correctness_r1::qa_remove_port_non_ansi_param_default_and_sibling_before_target_edge_cases: passes across sibling module top (input logic a) placed before leaf with parameter defaults P = 8, Q = P + 1 and b = a (refuses a in remove_port & empty port_offers, offers & removes b while ignoring sibling .b(y) in body checks), non-ANSI (a, b); input logic a; input logic b = a; + sibling .a(x), .b(y), shared non-ANSI input logic a, b; with port_offers (PortOfferKind::Remove), and sibling parameter a alongside target parameter default Q = P.
  • F2a Attack (crates/vlog_refactor/src/rename/{preflight,mod}.rs):
    • qa_correctness_r1::qa_preflight_same_name_inside_duplicate_unit_matches_rename runs un-ignored and passes; all 11 tests in tests/preflight_oracle.rs pass.
    • Added qa_correctness_r1::qa_preflight_parity_sweep_unit_local_vs_duplicate_module_nested_members: passes for $unit function local/arg renamed to itself (immediate already_named), duplicated module dup nested generate/function/class members renamed to themselves (rename_preflight defers with Ok(()) to ambiguous("dup")), and full byte-by-byte parity sweeps for prepare_rename vs prepare_rename_here and rename_preflight vs rename on both shapes.
  • Guarded Gates (post-final-code-edit):
    • cargo fmt -p vlog_refactor -- --check: pass.
    • ./tools/guard.sh -m 6 -t 600 -- cargo clippy -p vlog_refactor -p lucisv_server -p lucisv_mcp --all-targets -- -D warnings: pass (0 warnings in crates).
    • ./tools/guard.sh -m 6 -t 600 -- cargo test -p vlog_refactor --all-targets: pass (qa_correctness_r1: 11 passed, 0 failed, 0 ignored; preflight_oracle: 11 passed, 0 failed; total 765 passed, 0 failed, 6 pre-existing ignored across corpus/opentitan_top_rooted/qa_rename_implicit/rename_bind).
    • ./tools/guard.sh -m 6 -t 600 -- cargo test -p lucisv_mcp --all-targets: pass (388 passed, 0 failed, 4 pre-existing ignored).
    • ./tools/guard.sh -m 6 -t 600 -- cargo test -p lucisv_server --all-targets: pass (2139 passed, 0 failed, 52 pre-existing ignored).

Review

2026-11-26 — Lead review: PASS (merged with Lane 1B in Round 1)

  • Inspected combined diff across vlog_refactor and lucisv_server; cross-lane handshake F2b (same_file::renamed_here + dup_b.sv differential check) landed cleanly in Lane 1B and passes together with Lane 1A on trunk.
  • Removed stray duplicate // Author: line at line 3 of this README.
  • Handshakes closed in docs/topo.md: #38, #39, #53 (engine half). Full tools/gates.sh verified green on merged master.