Files
Marcos Paulo b32650767c
ci / ci (macos-latest) (push) Has been cancelled
ci / ci (ubuntu-latest) (push) Has been cancelled
ci / ci (windows-latest) (push) Has been cancelled
docs: specs and briefs for smart folders, version diff, media timestamps, clean paste, link cards, spaced repetition
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-21 12:18:00 -03:00

2.8 KiB

TASK textdiff-crate: word-level diff library notescraft-textdiff (Note visual diff)

Runs through OpenCode (specs/agents/opencode-runbook.md is attached automatically). Task 1 of specs/plugins-parity/version-diff/tasks.md. Needs owner answer Q2 (own code vs similar): if the answer is not in the spec, build your own as below.

OWNS

  • crates/textdiff/Cargo.toml, crates/textdiff/src/lib.rs, crates/textdiff/src/tokens.rs, crates/textdiff/src/myers.rs, crates/textdiff/tests/word_diff.rs
  • registration (this task only): root Cargo.toml (add notescraft-textdiff = { path = "crates/textdiff" } in [workspace.dependencies], next to notescraft-tasks), xtask/src/layers.rs (add ("textdiff", Class::Layer(0)), in the L0 block of TABLE; run the xtask tests in case one counts the table)

FILES TO OPEN

  • specs/plugins-parity/version-diff/spec.md D1
  • crates/tasks/Cargo.toml and crates/tasks/src/lib.rs (manifest and crate header to copy)
  • xtask/src/layers.rs lines 36-60

GOAL / API

Write crates/textdiff/src/lib.rs in your SECOND tool call (stub + one failing test).

pub enum Op { Same, Add, Del }
pub struct Span { pub op: Op, pub text: String }
pub struct Limits { pub max_bytes: usize, pub max_tokens: usize, pub max_cells: usize }   // Default: 2 MiB, 40_000, 16_000_000 (named consts)
pub fn word_diff(old: &str, new: &str, limits: &Limits) -> Result<Vec<Span>>;
pub fn stats(spans: &[Span]) -> (usize, usize);

Error::TooLarge (thiserror) when a limit is exceeded; never truncate silently. Tokens: word-char runs, whitespace runs, single other chars (char::is_alphanumeric or _ = word char). Strip common prefix and suffix tokens, then Myers (or an LCS table bounded by max_cells) on the middle; merge adjacent same-op spans. Same+Del == old and Same+Add == new always. Crate header #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)], [lints] workspace = true, rustdoc on every public item, //! on every module, only thiserror as dependency. No [i], no [a..b] (use get, split_at_checked, iterators).

TESTS

Reproduction property over 1 000 pairs from a fixed-seed LCG you write in the test; one changed word in a long paragraph gives one Del + one Add; insert at start and end; empty old; empty new; both empty; CRLF; emoji and combining marks; over-limit bytes, tokens, cells each return TooLarge; #[ignore] perf: two 2 MiB texts < 500 ms or TooLarge.

VERIFY

export PATH="$HOME/.cargo/bin:$PATH"
cargo test -p notescraft-textdiff
cargo clippy -p notescraft-textdiff --all-targets -- -D warnings
rustfmt --edition 2024 crates/textdiff/src/*.rs crates/textdiff/tests/word_diff.rs
cargo xtask layers

DO NOT

Add any dependency but thiserror; edit other crates; touch engine or UI (tasks 2-4); cargo fmt --all.