Milestone 2: secret check and runaway guard

Notes with a likely secret on a new line are held on the device (own
short rule list, per SPEC §8); `lunesync held pointer|not-secret`
resolves them. Bursts removing or rewriting a big part of the vault are
saved but not shared until `lunesync held share|undo`. Incoming edits
to a held note land in place with the held version kept beside it.
Adds `tidy show` and SPEC §6 detail for M2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 00:08:02 +07:00
parent 25b8be5bbd
commit 62bc3db416
11 changed files with 963 additions and 55 deletions

View File

@@ -125,7 +125,7 @@ fn a_burst_is_one_history_entry_labeled_with_the_device() {
a.sync().unwrap();
let head = a.head_commit().unwrap().unwrap();
assert_eq!(head.author().name().unwrap(), "Hermes");
assert_eq!(head.summary().unwrap().unwrap(), "Updated 14 notes");
assert_eq!(head.summary().unwrap().unwrap(), "Added 14 notes");
}
#[test]
@@ -361,3 +361,157 @@ fn names_differing_only_by_case_are_renamed_not_overwritten() {
assert_eq!(read(&a, "Notes.md"), b"upper\n");
assert_eq!(read(&a, "notes (name clash).md"), b"lower\n");
}
// ---- Milestone 2: secrets and the runaway guard ----
use lune_core::secrets;
const FAKE_KEY: &str = "aws_access_key_id = AKIAIOSFODNN7EXAMPLE\n";
#[test]
fn a_note_with_a_secret_stays_home_while_the_rest_is_shared() {
let w = World::new();
let a = w.device("Codex");
let b = w.device("Hermes");
write(&a, "aws.md", format!("# AWS\n{FAKE_KEY}"));
write(&a, "other.md", "fine\n");
save_and_sync(&a);
b.sync().unwrap();
assert_eq!(read(&b, "other.md"), b"fine\n");
assert!(!b.root.join("aws.md").exists());
let held = a.secret_holds().unwrap();
assert_eq!(held.len(), 1);
assert_eq!((held[0].path.as_str(), held[0].hits[0].rule, held[0].hits[0].line), ("aws.md", "AWS access key", 2));
let text = String::from_utf8(read(&a, "aws.md")).unwrap();
write(&a, "aws.md", secrets::replace_with_pointer(&text, &held[0].hits, None, "(search prod-deploy in Secrets Manager)"));
save_and_sync(&a);
b.sync().unwrap();
assert_eq!(read(&b, "aws.md"), b"# AWS\naws_access_key_id = (search prod-deploy in Secrets Manager)\n");
}
#[test]
fn not_a_secret_is_remembered_for_that_line() {
let w = World::new();
let a = w.device("Codex");
let b = w.device("Hermes");
write(&a, "example.md", "The docs use AKIAIOSFODNN7EXAMPLE as a sample.\n");
save_and_sync(&a);
let held = a.secret_holds().unwrap();
secrets::allow(&a.git_dir, &held[0].hits[0], "example.md").unwrap();
assert!(a.secret_holds().unwrap().is_empty());
save_and_sync(&a);
b.sync().unwrap();
assert!(b.root.join("example.md").exists());
}
#[test]
fn incoming_changes_to_a_held_note_land_and_the_held_version_moves_beside() {
let w = World::new();
let a = w.device("Codex");
write(&a, "ops.md", "ops\n");
save_and_sync(&a);
let b = w.device("Hermes");
write(&a, "ops.md", format!("ops\n{FAKE_KEY}"));
save_and_sync(&a);
write(&b, "ops.md", "ops\nnew runbook link\n");
save_and_sync(&b);
let report = a.sync().unwrap();
assert!(!report.waiting_for_local_edits);
assert_eq!(report.set_aside, vec![("ops.md".into(), "ops (Codex's version).md".into())]);
assert_eq!(read(&a, "ops.md"), b"ops\nnew runbook link\n");
assert_eq!(read(&a, "ops (Codex's version).md"), format!("ops\n{FAKE_KEY}").as_bytes());
let held: Vec<String> = a.secret_holds().unwrap().into_iter().map(|n| n.path).collect();
assert_eq!(held, ["ops (Codex's version).md"]);
}
fn vault_of_20(w: &World) -> (Vault, Vault) {
let a = w.device("Codex");
for i in 0..20 {
write(&a, &format!("notes/{i:02}.md"), format!("note {i}\nline two\n"));
}
save_and_sync(&a);
let b = w.device("Hermes");
(a, b)
}
#[test]
fn deleting_half_the_vault_is_held_and_undo_brings_back_every_byte() {
let w = World::new();
let (a, b) = vault_of_20(&w);
let before = snapshot(&a.root);
for i in 0..10 {
fs::remove_file(a.root.join(format!("notes/{i:02}.md"))).unwrap();
}
write(&a, "notes/03-new.md", "unrelated new note\n");
let saved = a.save_changes(None, &HashSet::new()).unwrap().unwrap();
let d = saved.held.expect("should be held");
assert_eq!((d.removed, d.total), (10, 20));
let report = a.sync().unwrap();
assert!(report.holding);
b.sync().unwrap();
assert_eq!(snapshot(&b.root), before, "nothing reached the other device");
a.undo_held().unwrap();
let mut expected = before.clone();
expected.insert("notes/03-new.md".into(), b"unrelated new note\n".to_vec());
assert_eq!(snapshot(&a.root), expected);
a.sync().unwrap();
b.sync().unwrap();
assert_eq!(snapshot(&b.root), expected);
}
#[test]
fn share_anyway_sends_the_big_change() {
let w = World::new();
let (a, b) = vault_of_20(&w);
for i in 0..10 {
fs::remove_file(a.root.join(format!("notes/{i:02}.md"))).unwrap();
}
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_some());
a.sync().unwrap();
a.share_held().unwrap();
let report = a.sync().unwrap();
assert!(!report.holding);
b.sync().unwrap();
assert_eq!(snapshot(&b.root).len(), 10);
}
#[test]
fn undo_keeps_both_when_a_removed_note_was_edited_elsewhere() {
let w = World::new();
let (a, b) = vault_of_20(&w);
for i in 0..10 {
fs::remove_file(a.root.join(format!("notes/{i:02}.md"))).unwrap();
}
a.save_changes(None, &HashSet::new()).unwrap();
write(&b, "notes/03.md", "note 3\nline two\nedited on hermes\n");
save_and_sync(&b);
a.sync().unwrap();
a.undo_held().unwrap();
let all: Vec<Vec<u8>> = snapshot(&a.root).into_values().collect();
assert!(all.contains(&b"note 3\nline two\n".to_vec()));
assert!(all.contains(&b"note 3\nline two\nedited on hermes\n".to_vec()));
}
#[test]
fn moves_and_appends_are_not_runaway() {
let w = World::new();
let (a, _b) = vault_of_20(&w);
for i in 0..20 {
fs::rename(a.root.join(format!("notes/{i:02}.md")), a.root.join(format!("{i:02}.md"))).unwrap();
}
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_none());
for i in 0..20 {
write(&a, &format!("{i:02}.md"), format!("note {i}\nline two\nappended\n"));
}
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_none());
for i in 0..20 {
write(&a, &format!("{i:02}.md"), "wiped\n");
}
assert!(a.save_changes(None, &HashSet::new()).unwrap().unwrap().held.is_some());
}