Skip to main content

lvz_context/
lib.rs

1//! `lvz-context` — the token-efficiency engine (§6.1).
2//!
3//! This crate holds the largest token levers, all usable offline and independent of any
4//! provider:
5//!
6//! - [`skeleton`] — tree-sitter file-skeleton extraction: keep signatures + types + docs,
7//!   elide bodies (multi-language: Rust, Python, JavaScript, TypeScript).
8//! - [`symbols`] — recursive symbol-dependency graph; drives the skeleton-radius knob `N`
9//!   via [`symbols::skeleton_with_radius`].
10//! - [`anchor`] — hash-anchored edits: address lines by a short content hash so edits don't
11//!   require resending the file, and stale edits are rejected rather than misapplied.
12//! - [`diff`] — token-efficient unified diffs: emit only changed hunks, never full rewrites.
13//! - [`tokens`] / [`budget`] — deterministic token estimate + the §6.5 budget-fixture loop
14//!   that gates skeleton-radius regressions in CI.
15//!
16//! These are the primitives the agent uses to read less and write less; the skeleton-radius
17//! knob `N` (§6.5) drives [`skeleton::skeletonize`]'s `keep_bodies` set.
18
19pub mod anchor;
20pub mod budget;
21pub mod diff;
22mod lang;
23pub mod skeleton;
24pub mod symbols;
25pub mod tokens;
26
27pub use lang::Lang;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use std::collections::HashSet;
33
34    /// End-to-end: skeleton a file to read it cheaply, then edit it by anchor, then diff.
35    #[test]
36    fn skeleton_then_anchored_edit_then_diff() {
37        let src = "\
38/// Doubles n.
39fn double(n: i32) -> i32 {
40    n * 2
41}
42";
43        // 1. Skeleton keeps the signature, drops the body.
44        let skel = skeleton::skeletonize(src, Lang::Rust, &HashSet::new());
45        assert!(skel.contains("fn double(n: i32) -> i32"));
46        assert!(!skel.contains("n * 2"));
47
48        // 2. Anchored edit changes the body line without resending the file.
49        let anchor = anchor::anchor_of("    n * 2");
50        let edited =
51            anchor::apply_edits(src, &[anchor::Edit::replace(anchor, "    n * 3")]).unwrap();
52        assert!(edited.contains("n * 3"));
53
54        // 3. A minimal diff captures exactly the change.
55        let d = diff::unified_diff(src, &edited, 0);
56        assert!(d.contains("-    n * 2"));
57        assert!(d.contains("+    n * 3"));
58    }
59}