Round 5 found `hierarchy up` + `peek` cut to 20 of 77 instantiations by the 300-line cap. Same-day base controls put read-only mode at parity with file tools (a +8 %, d −8 %) and show the base arm itself moved 22–29 % in six days. Co-Authored-By: Claude Fable 5.1 <[email protected]>
138 lines
5.9 KiB
Rust
138 lines
5.9 KiB
Rust
//! The `hierarchy` cap counts instances, never `peek` lines (census round 5,
|
|
//! 2026-09-29): an agent asking for every instantiation of a module with
|
|
//! `peek` gets every one up to 100 (300 without `peek`), each with its
|
|
//! statement, not a quarter of them. Each test writes a generated design into a scratch directory and
|
|
//! talks to the real binary over it.
|
|
|
|
use std::fs;
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
use std::process::{Command, Stdio};
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
use serde_json::{json, Value};
|
|
|
|
static NEXT_DIR: AtomicUsize = AtomicUsize::new(0);
|
|
|
|
/// A scratch design, removed on drop.
|
|
struct Tree(PathBuf);
|
|
|
|
impl Tree {
|
|
fn with(files: &[(&str, String)]) -> Tree {
|
|
let unique = NEXT_DIR.fetch_add(1, Ordering::SeqCst);
|
|
let dir = std::env::temp_dir()
|
|
.join(format!("lucisv-mcp-hierarchy-cap-{}-{unique}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).unwrap();
|
|
for (rel, text) in files {
|
|
fs::write(dir.join(rel), text).unwrap();
|
|
}
|
|
Tree(fs::canonicalize(dir).unwrap())
|
|
}
|
|
}
|
|
|
|
impl Drop for Tree {
|
|
fn drop(&mut self) {
|
|
let _ = fs::remove_dir_all(&self.0);
|
|
}
|
|
}
|
|
|
|
/// One `hierarchy` call over `tree` that must succeed: its text.
|
|
fn hierarchy(tree: &Tree, arguments: Value) -> String {
|
|
let mut child = Command::new(env!("CARGO_BIN_EXE_lucisv-mcp"))
|
|
.args(["--root", tree.0.to_str().unwrap()])
|
|
.stdin(Stdio::piped())
|
|
.stdout(Stdio::piped())
|
|
.stderr(Stdio::null())
|
|
.spawn()
|
|
.unwrap();
|
|
let init = json!({"jsonrpc": "2.0", "id": 0, "method": "initialize", "params": {
|
|
"protocolVersion": "2025-06-18", "capabilities": {},
|
|
"clientInfo": {"name": "hierarchy_cap", "version": "0"}}});
|
|
let params = json!({"name": "hierarchy", "arguments": arguments});
|
|
let call = json!({"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": params});
|
|
let mut stdin = child.stdin.take().unwrap();
|
|
stdin.write_all(format!("{init}\n{call}\n").as_bytes()).unwrap();
|
|
drop(stdin);
|
|
let output = child.wait_with_output().unwrap();
|
|
let stdout = String::from_utf8(output.stdout).unwrap();
|
|
let reply: Value = stdout
|
|
.lines()
|
|
.map(|line| serde_json::from_str::<Value>(line).unwrap())
|
|
.find(|reply| reply["id"] == 1)
|
|
.expect("the call is answered");
|
|
assert_eq!(reply["result"]["isError"], false, "{reply}");
|
|
reply["result"]["content"][0]["text"].as_str().unwrap().to_string()
|
|
}
|
|
|
|
/// `count` instantiations of `sub` in `top`, each written over `ports + 2`
|
|
/// lines: the header, one line per port, the closer.
|
|
fn crowded(count: usize, ports: usize) -> Tree {
|
|
let names: Vec<String> = (0..ports).map(|p| format!("p{p}")).collect();
|
|
let declared: Vec<String> = names.iter().map(|n| format!("input logic {n}")).collect();
|
|
let sub = format!("module sub #(parameter int W = 1) ({});\nendmodule\n", declared.join(", "));
|
|
let mut top = format!("module top;\n logic {};\n", names.join(", "));
|
|
for i in 0..count {
|
|
top.push_str(&format!(" sub #(.W({i})) u_{i:03} (\n"));
|
|
for (at, name) in names.iter().enumerate() {
|
|
let comma = if at + 1 < names.len() { "," } else { "" };
|
|
top.push_str(&format!(" .{name}({name}){comma}\n"));
|
|
}
|
|
top.push_str(" );\n");
|
|
}
|
|
top.push_str("endmodule\n");
|
|
Tree::with(&[("sub.sv", sub), ("top.sv", top)])
|
|
}
|
|
|
|
/// The tree lines of an answer: every line that is not a peeked statement.
|
|
fn node_lines(text: &str, statement_indent: &str) -> Vec<String> {
|
|
text.lines().filter(|l| !l.starts_with(statement_indent)).map(str::to_string).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn up_with_peek_lists_every_instantiation_whatever_the_statements_cost() {
|
|
// 30 instantiations of 14 lines each: peek shows 12 lines and `…` per
|
|
// statement, 420 lines in all — every one is still there.
|
|
let tree = crowded(30, 12);
|
|
let text = hierarchy(&tree, json!({"root": "sub", "up": true, "peek": true}));
|
|
let nodes = node_lines(&text, " ");
|
|
let expected: Vec<String> =
|
|
(0..30).map(|i| format!("top.u_{i:03} top.sv:{}", 3 + i * 14)).collect();
|
|
assert_eq!(nodes, expected, "{text}");
|
|
assert!(!text.contains("… and "), "{text}");
|
|
assert_eq!(text.lines().count(), 30 * 14);
|
|
// Each statement: its first 12 lines as written, then `…`.
|
|
let first: Vec<&str> = text.lines().skip(1).take(13).collect();
|
|
assert_eq!(first[0], " sub #(.W(0)) u_000 (");
|
|
assert_eq!(first[11], " .p10(p10),");
|
|
assert_eq!(first[12], " …");
|
|
}
|
|
|
|
#[test]
|
|
fn over_100_instances_with_peek_shows_100_and_over_300_without_shows_300() {
|
|
let tree = crowded(350, 1);
|
|
// Up with peek: 350 instantiations, 100 shown, each with its 3-line statement.
|
|
let up = hierarchy(&tree, json!({"root": "sub", "up": true, "peek": true}));
|
|
let lines: Vec<&str> = up.lines().collect();
|
|
assert_eq!(lines.len(), 100 * 4 + 1, "{up}");
|
|
assert_eq!(*lines.last().unwrap(), "… and 250 more");
|
|
let nodes = node_lines(&up, " ");
|
|
assert_eq!(nodes.len(), 101);
|
|
assert_eq!(nodes[99], "top.u_099 top.sv:300");
|
|
assert_eq!(lines[lines.len() - 2], " );", "the last statement is whole");
|
|
// Down with peek: the root line and 99 instances make 100 nodes; 251 are left.
|
|
let down = hierarchy(&tree, json!({"root": "top", "peek": true}));
|
|
let nodes = node_lines(&down, " ");
|
|
assert_eq!(nodes.len(), 101, "{down}");
|
|
assert_eq!(nodes[0], "top top.sv:1");
|
|
assert_eq!(nodes[99], " u_098: sub top.sv:297");
|
|
assert_eq!(nodes[100], "… and 251 more");
|
|
// Without peek: 300 lines, then the count.
|
|
let plain = hierarchy(&tree, json!({"root": "sub", "up": true}));
|
|
assert_eq!(plain.lines().count(), 301);
|
|
assert!(plain.ends_with("\n… and 50 more\n"), "{plain}");
|
|
let plain = hierarchy(&tree, json!({"root": "top"}));
|
|
assert_eq!(plain.lines().count(), 301);
|
|
assert!(plain.ends_with("\n… and 51 more\n"), "{plain}");
|
|
}
|