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

@@ -4,6 +4,8 @@ use anyhow::{Context, Result, bail};
use chrono::{Local, TimeZone};
use clap::{Parser, Subcommand, ValueEnum};
use lune_core::config::{Config, VaultConfig};
use lune_core::guard;
use lune_core::secrets;
use lune_core::runner::{self, Timing};
use lune_core::state::{self, SyncState};
use lune_core::tidy::{self, Keep};
@@ -51,7 +53,14 @@ enum Command {
Tidy {
#[command(subcommand)]
action: Option<TidyCmd>,
#[arg(long)]
#[arg(long, global = true)]
vault: Option<String>,
},
/// What's held back on this device (secrets, big changes) and deciding about it
Held {
#[command(subcommand)]
action: Option<HeldCmd>,
#[arg(long, global = true)]
vault: Option<String>,
},
/// Recent history entries and which device made them
@@ -85,6 +94,30 @@ enum ServiceCmd {
enum TidyCmd {
/// Keep one version (mine = this device's), or both as separate notes
Keep { choice: KeepArg, file: PathBuf },
/// Compare the two kept versions line by line
Show { file: PathBuf },
}
#[derive(Subcommand)]
enum HeldCmd {
/// The big change was intended: share it anyway
Share,
/// The big change was a mistake: bring back everything it removed or rewrote
Undo,
/// Replace the secret in a note with a pointer to where it lives
Pointer {
file: PathBuf,
/// Where to get it, e.g. "search prod-deploy in Secrets Manager"
pointer: String,
#[arg(long)]
line: Option<usize>,
},
/// It isn't a secret; don't hold that exact line again on this device
NotSecret {
file: PathBuf,
#[arg(long)]
line: Option<usize>,
},
}
#[derive(Clone, Copy, ValueEnum)]
@@ -177,6 +210,44 @@ fn run_cli(cli: Cli) -> Result<()> {
}
share_now(&v);
}
Command::Tidy { action: Some(TidyCmd::Show { file }), vault } => {
let (cfg, rel) = locate(&config, vault.as_deref(), &cwd, &file)?;
let v = Vault::open(cfg, &config.device_label())?;
print!("{}", tidy::show(&v, &rel)?);
println!("Pick one with `lunesync tidy keep mine|theirs|both {rel}`.");
}
Command::Held { action: None, vault } => {
let list: Vec<&VaultConfig> = match vault {
Some(n) => vec![config.pick(Some(&n), &cwd)?],
None => config.vaults.iter().collect(),
};
let mut any = false;
for cfg in list {
let v = Vault::open(cfg, &config.device_label())?;
for h in guard::load(&v.git_dir) {
any = true;
println!(
"{}: big change held — \"{}\" removes {} and rewrites {} of {} notes.",
v.name, h.message, h.removed, h.rewritten, h.total
);
println!(" Send it: `lunesync held share` Bring everything back: `lunesync held undo`");
}
for n in v.secret_holds()? {
any = true;
for h in &n.hits {
println!("{}: {} line {} has a possible {}: {}", v.name, n.path, h.line, h.rule, h.hint);
}
println!(
" Replace it: `lunesync held pointer {} \"where to get it\"` Not a secret: `lunesync held not-secret {}`",
n.path, n.path
);
}
}
if !any {
println!("Nothing held back.");
}
}
Command::Held { action: Some(cmd), vault } => held(&config, &cwd, vault.as_deref(), cmd)?,
Command::History { vault, count } => {
let cfg = config.pick(vault.as_deref(), &cwd)?;
let v = Vault::open(cfg, &config.device_label())?;
@@ -203,6 +274,80 @@ fn run_cli(cli: Cli) -> Result<()> {
Ok(())
}
fn held(config: &Config, cwd: &Path, vault: Option<&str>, cmd: HeldCmd) -> Result<()> {
let label = config.device_label();
match cmd {
HeldCmd::Share | HeldCmd::Undo => {
let cfg = config.pick(vault, cwd)?;
let v = Vault::open(cfg, &label)?;
let _op = state::op_lock(&v.git_dir)?;
if guard::load(&v.git_dir).is_empty() {
println!("No big change is held in {}.", v.name);
return Ok(());
}
if matches!(cmd, HeldCmd::Share) {
for h in v.share_held()? {
println!("Sharing \"{}\".", h.message);
}
} else {
let kept = v.undo_held()?;
println!("Brought everything back in {}.", v.name);
for (note, copy) in kept {
println!(" {note} was edited elsewhere meanwhile — kept both, the other version is \"{copy}\".");
}
}
share_now(&v);
}
HeldCmd::Pointer { file, pointer, line } => {
let (cfg, rel) = locate(config, vault, cwd, &file)?;
let v = Vault::open(cfg, &label)?;
let _op = state::op_lock(&v.git_dir)?;
let hits = held_hits(&v, &rel, line)?;
let path = v.root.join(&rel);
let text = std::fs::read_to_string(&path)?;
std::fs::write(&path, secrets::replace_with_pointer(&text, &hits, line, &pointer))?;
println!("Replaced {} secret{} in {rel} with \"{pointer}\".", hits.len(), if hits.len() == 1 { "" } else { "s" });
save_and_share(&v)?;
}
HeldCmd::NotSecret { file, line } => {
let (cfg, rel) = locate(config, vault, cwd, &file)?;
let v = Vault::open(cfg, &label)?;
let _op = state::op_lock(&v.git_dir)?;
for h in held_hits(&v, &rel, line)? {
secrets::allow(&v.git_dir, &h, &rel)?;
println!("OK — line {} of {rel} won't be held again on this device.", h.line);
}
save_and_share(&v)?;
}
}
Ok(())
}
fn held_hits(v: &Vault, rel: &str, line: Option<usize>) -> Result<Vec<secrets::Hit>> {
let note = v.secret_holds()?.into_iter().find(|n| n.path == rel);
let hits: Vec<secrets::Hit> = note
.map(|n| n.hits)
.unwrap_or_default()
.into_iter()
.filter(|h| line.is_none_or(|l| h.line == l))
.collect();
if hits.is_empty() {
bail!("nothing is held in {rel}{}", line.map(|l| format!(" on line {l}")).unwrap_or_default());
}
Ok(hits)
}
fn save_and_share(v: &Vault) -> Result<()> {
if v.save_changes(None, &Default::default())?.is_some_and(|s| s.held.is_some()) {
println!("That change is big enough to be held too — see `lunesync held`.");
}
if !v.secret_holds()?.is_empty() {
println!("Something else in this vault is still held — see `lunesync held`.");
}
share_now(v);
Ok(())
}
fn add(config: &mut Config, name: &str, folder: &Path, remote: &str) -> Result<()> {
if config.find(name).is_some() {
bail!("there's already a vault named \"{name}\"");
@@ -293,6 +438,16 @@ fn print_status(cfg: &VaultConfig, label: &str) {
if let Some(t) = st.last_synced {
println!(" Last sync: {} ({})", Local.timestamp_opt(t, 0).unwrap().format("%Y-%m-%d %H:%M:%S"), ago(t));
}
for h in guard::load(&v.git_dir) {
println!(" Holding back a big change: \"{}\" — nothing from here is shared until you decide (`lunesync held`)", h.message);
}
let secret_notes = v.secret_holds().map(|l| l.len()).unwrap_or(0);
if secret_notes > 0 {
println!(
" {secret_notes} note{} not shared: looks like a secret (`lunesync held`)",
if secret_notes == 1 { "" } else { "s" }
);
}
let to_tidy = tidy::list(&v).map(|l| l.len()).unwrap_or(0);
if to_tidy > 0 {
println!(" {to_tidy} note{} to tidy — see `lunesync tidy`", if to_tidy == 1 { "" } else { "s" });