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(addnotescraft-textdiff = { path = "crates/textdiff" }in[workspace.dependencies], next tonotescraft-tasks),xtask/src/layers.rs(add("textdiff", Class::Layer(0)),in the L0 block ofTABLE; run the xtask tests in case one counts the table)
FILES TO OPEN
specs/plugins-parity/version-diff/spec.mdD1crates/tasks/Cargo.tomlandcrates/tasks/src/lib.rs(manifest and crate header to copy)xtask/src/layers.rslines 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.