mod cli_audit;
mod cli_catalogs;
mod cli_dependencies;
mod cli_hash;
mod cli_install;
mod cli_repair;
mod cli_search;
mod cli_source;
use anyhow::{Context, Result, ensure};
use clap::{Args, Parser, Subcommand};
use haps::comments::{self, Comments, Scope};
use haps::{
discovery::{Announcement, Discovery},
install::{Installation, ensure_public_key, load_keys},
model::*,
repository::{Repository, Snapshot},
trust::{Trust, attest, attest_at, attestation_time_ms, parse_attestation, warn, warn_at},
};
use nostr::{Event, EventBuilder, EventId, Keys, Kind, Tag, Timestamp};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, BTreeSet},
fs,
path::{Path, PathBuf},
process::ExitCode,
sync::Arc,
};
#[derive(Parser)]
#[command(version, about = "Haps — Hashtree Package System")]
struct Cli {
#[arg(long, global = true, env = "HAPS_HOME")]
home: Option<PathBuf>,
#[arg(long, global = true, env = "HAPS_NO_DEFAULTS")]
no_defaults: bool,
#[arg(long, global = true, env = "HAPS_NON_INTERACTIVE")]
non_interactive: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Args)]
struct ClaimArgs {
release: String,
#[arg(long)]
version: Option<semver::Version>,
#[arg(long, requires = "version")]
target: Option<String>,
#[arg(long)]
note: String,
#[arg(long, requires = "provenance", conflicts_with = "revoke")]
audited: bool,
#[arg(long, requires = "audited")]
provenance: Option<String>,
#[arg(long)]
revoke: bool,
#[arg(long)]
out: Option<PathBuf>,
#[arg(long)]
json: bool,
}
#[derive(Args, Clone, Default)]
struct BuildExecutionArgs {
#[arg(long, conflicts_with = "allow_host_build")]
allow_build_network: bool,
#[arg(long)]
allow_host_build: bool,
}
impl BuildExecutionArgs {
fn execution(&self) -> haps::build::sandbox::Execution {
haps::build::sandbox::Execution {
allow_host: self.allow_host_build,
allow_network: self.allow_build_network,
}
}
}
#[derive(Args, Clone, Default)]
struct AuditReviewArgs {
#[arg(long)]
audit_agent: Option<String>,
#[arg(long)]
audit_model: Option<String>,
#[arg(long, num_args = 0..=1, default_missing_value = "true")]
publish_audit: Option<bool>,
#[command(flatten)]
build_execution: BuildExecutionArgs,
}
#[derive(Subcommand)]
enum AuditAction {
Repair {
#[command(subcommand)]
action: RepairAction,
},
Prepare {
package: String,
#[arg(long)]
version: Option<semver::Version>,
},
Finish {
session: String,
#[arg(long)]
note: String,
#[arg(long, default_value = "manual")]
reviewer: String,
#[arg(long)]
model: Option<String>,
#[arg(long, num_args = 0..=1, default_missing_value = "true")]
publish: Option<bool>,
#[command(flatten)]
build_execution: BuildExecutionArgs,
},
Settings {
#[arg(long)]
agent: Option<String>,
#[arg(long)]
model: Option<String>,
#[arg(long)]
publish: Option<bool>,
},
}
#[derive(Subcommand)]
enum RepairAction {
Prepare {
failed: PathBuf,
},
Build {
session: String,
#[arg(long)]
repository: String,
#[arg(long)]
rev: String,
#[arg(long)]
note: String,
#[arg(long)]
audit_agent: Option<String>,
#[arg(long)]
audit_model: Option<String>,
#[command(flatten)]
build_execution: BuildExecutionArgs,
},
Publish {
session: String,
#[arg(long)]
name: String,
},
}
#[derive(Subcommand)]
enum Command {
Audit {
#[command(subcommand)]
action: AuditAction,
},
StartingPoint {
public_key: Option<String>,
#[arg(long, conflicts_with = "public_key")]
clear: bool,
},
Identity {
#[command(subcommand)]
action: IdentityAction,
},
Alias {
#[command(subcommand)]
action: AliasAction,
},
Target,
Init(haps::init::InitArgs),
Add {
package: String,
#[arg(long)]
payload: Option<PathBuf>,
#[arg(long, default_value = "default")]
catalog: String,
},
Catalog {
#[command(subcommand)]
action: CatalogAction,
},
Pack {
manifest: PathBuf,
#[arg(long)]
package: Option<String>,
#[arg(long)]
target: Option<String>,
#[arg(long)]
payload: PathBuf,
#[arg(long)]
out: PathBuf,
#[arg(long)]
key_file: Option<PathBuf>,
},
Manifest {
path: PathBuf,
#[arg(long)]
package: Option<String>,
#[arg(long)]
target: Option<String>,
},
ImportRelease(haps::release::ReleaseArgs),
Publish {
#[arg(value_name = "DIRECTORY")]
catalog: PathBuf,
#[arg(long)]
name: String,
#[arg(long)]
key_file: Option<PathBuf>,
},
Sync,
#[command(hide = true)]
Index {
#[command(subcommand)]
action: IndexAction,
},
Build {
repository: String,
#[arg(long)]
rev: String,
#[arg(long)]
recipe: Option<String>,
#[arg(long, conflicts_with = "recipe")]
recipe_file: Option<PathBuf>,
#[arg(long)]
package: Option<String>,
#[arg(long)]
execute: bool,
#[arg(long, requires = "execute")]
install: bool,
#[command(flatten)]
audit_review: AuditReviewArgs,
#[arg(long, requires = "install")]
attest_scan: bool,
},
#[command(hide = true)]
Source {
#[command(subcommand)]
action: SourceAction,
},
Search {
query: String,
#[arg(long)]
json: bool,
#[arg(long, requires = "json")]
stream: bool,
},
Info {
package: String,
#[arg(long)]
version: Option<semver::Version>,
#[arg(long)]
json: bool,
},
Install {
package: String,
#[arg(long = "select", value_name = "PUBLISHER/NAME")]
select_package: Option<String>,
#[arg(long, conflicts_with = "allow_unaudited")]
from_source: bool,
#[arg(long, requires = "from_source")]
recipe_file: Option<PathBuf>,
#[arg(long)]
version: Option<semver::Version>,
#[arg(long = "release")]
release_id: Option<EventId>,
#[arg(long)]
allow_untrusted: bool,
#[arg(long)]
allow_warnings: bool,
#[arg(long, default_value_t = 1)]
require_attestations: usize,
#[arg(long, conflicts_with_all = ["audit", "audit_agent", "audit_model", "publish_audit"])]
allow_unaudited: bool,
#[arg(long)]
audit: bool,
#[command(flatten)]
audit_review: AuditReviewArgs,
#[arg(long)]
json: bool,
},
Update {
package: String,
#[arg(long)]
allow_untrusted: bool,
#[arg(long)]
allow_warnings: bool,
#[arg(long, conflicts_with_all = ["audit", "audit_agent", "audit_model", "publish_audit"])]
allow_unaudited: bool,
#[arg(long)]
audit: bool,
#[command(flatten)]
audit_review: AuditReviewArgs,
#[arg(long)]
json: bool,
},
Link {
package: String,
#[arg(long)]
bin_dir: Option<PathBuf>,
},
Security {
#[arg(long, conflicts_with = "disable")]
rules: Option<PathBuf>,
#[arg(long, requires = "rules")]
scanner: Option<PathBuf>,
#[arg(long)]
disable: bool,
},
Run {
package: String,
#[arg(long)]
command: Option<String>,
#[arg(last = true)]
args: Vec<String>,
},
Path { package: String },
#[cfg(target_os = "linux")]
Omarchy {
#[arg(long)]
remove: bool,
#[arg(long, conflicts_with = "remove")]
menu: bool,
},
List {
#[arg(long)]
json: bool,
},
Rollback {
package: String,
#[arg(long)]
allow_unaudited: bool,
},
Remove { package: String },
Follow {
public_key: String,
#[arg(long)]
export: Option<PathBuf>,
},
Import { events: PathBuf },
Attest(ClaimArgs),
Warn(ClaimArgs),
Comment {
package: String,
text: String,
#[arg(long)]
release: Option<String>,
#[arg(long)]
reply_to: Option<String>,
#[arg(long)]
out: Option<PathBuf>,
},
Comments {
package: String,
#[arg(long)]
release: Option<String>,
},
}
impl Command {
fn json(&self) -> bool {
match self {
Self::Init(args) => args.json,
Self::Search { json, .. }
| Self::Info { json, .. }
| Self::Install { json, .. }
| Self::Update { json, .. }
| Self::List { json } => *json,
Self::Attest(args) | Self::Warn(args) => args.json,
_ => false,
}
}
}
#[derive(Subcommand)]
enum IdentityAction {
Init,
Use {
public_key: String,
},
Show,
}
#[derive(Subcommand)]
enum CatalogAction {
List,
Show {
#[arg(default_value = "default")]
name: String,
},
Remove {
package: String,
#[arg(long, default_value = "default")]
catalog: String,
},
Add {
name: String,
location: String,
#[arg(long)]
author: String,
},
Discover,
Publish {
#[arg(default_value = "default")]
name: String,
#[arg(long, env = "HAPS_KEY_FILE")]
key_file: Option<PathBuf>,
},
}
#[derive(Subcommand)]
enum SourceAction {
Add {
name: String,
location: String,
#[arg(long)]
author: String,
},
List,
Remove {
name: String,
},
}
#[derive(Subcommand)]
enum IndexAction {
Add {
name: String,
location: String,
#[arg(long)]
author: String,
},
List,
Remove {
name: String,
},
Build {
#[arg(long)]
out: PathBuf,
#[arg(long)]
key_file: Option<PathBuf>,
},
}
#[derive(Subcommand)]
enum AliasAction {
Add { name: String, public_key: String },
List,
Remove { name: String },
}
#[derive(Default, Serialize, Deserialize)]
struct Config {
identity: Option<String>,
sources: BTreeMap<String, Source>,
#[serde(default)]
indexes: BTreeMap<String, Source>,
#[serde(skip)]
aliases: BTreeMap<String, String>,
#[serde(default)]
starting_point: Option<String>,
#[serde(default)]
discovery_defaults_version: u32,
#[serde(default)]
audit: AuditSettings,
}
#[derive(Default, Serialize, Deserialize)]
struct AuditSettings {
agent: Option<String>,
model: Option<String>,
publish: Option<bool>,
}
const DISCOVERY_DEFAULTS_VERSION: u32 = 2;
const LEGACY_INDEX_LOCATION: &str =
"htree://npub1q6g6t3yk0m2ppp5mrqsze4xg6uqhw5p29kjutet5m3uk637xjfaqac3p2a/package-index";
const LEGACY_INDEX_AUTHOR: &str =
"731fd6f74667cac0e86b7b4f7cd2c828996db866c3044368a2f26d87cb571ad0";
fn discovery_presets(config: &mut Config) -> Result<()> {
let event: Event = serde_json::from_str(include_str!("../packages/catalog-root.json"))?;
let announcement = haps::event_catalog::IndexAnnouncement::verify(event)?;
config
.indexes
.entry("haps".into())
.or_insert_with(|| Source {
location: announcement.location,
author: announcement.event.pubkey.to_hex(),
sequence: 0,
event_id: announcement.event.id.to_hex(),
});
Ok(())
}
fn fresh_config(no_defaults: bool) -> Result<Config> {
let mut config = Config {
discovery_defaults_version: DISCOVERY_DEFAULTS_VERSION,
..Default::default()
};
if !no_defaults {
discovery_presets(&mut config)?;
let npub = hashtree_config::DEFAULT_SOCIALGRAPH_ENTRYPOINT_NPUB;
let key = ensure_public_key(npub)?;
config.starting_point = Some(key.clone());
}
Ok(config)
}
fn resolve_key(config: &Config, key: &str) -> Result<String> {
ensure_public_key(config.aliases.get(key).map(String::as_str).unwrap_or(key))
}
fn resolve_package(config: &Config, package: &str) -> Result<String> {
if let Some((key, name)) = package.split_once('/') {
safe_name(name)?;
return Ok(format!("{}/{}", resolve_key(config, key)?, name));
}
Ok(package.into())
}
fn publisher_label(aliases: &BTreeMap<String, String>, author: &str) -> String {
use nostr::nips::nip19::ToBech32;
aliases
.iter()
.find(|(_, key)| key.as_str() == author)
.map(|(alias, _)| alias.clone())
.unwrap_or_else(|| {
nostr::PublicKey::parse(author)
.map(|p| p.to_bech32().unwrap())
.unwrap_or_else(|_| author.into())
})
}
#[derive(Serialize, Deserialize)]
struct Source {
location: String,
author: String,
sequence: u64,
event_id: String,
}
#[derive(Clone)]
struct Candidate {
repository: Arc<Repository>,
package_card: Option<String>,
release: Release,
}
fn save_config(home: &Path, config: &Config) -> Result<()> {
atomic_write(
&home.join("config.json"),
&serde_json::to_vec_pretty(config)?,
)
}
fn load_trust(home: &Path, config: &Config) -> Result<Trust> {
let mut trust = Trust::new(
config
.identity
.clone()
.unwrap_or_else(|| "unconfigured".into()),
);
let file = home.join("social.json");
if file.exists() {
let events: Vec<Event> = read_json(&file)?;
ensure!(events.len() <= 10_000, "social event limit exceeded");
for event in events {
trust.ingest(event)?;
}
}
trust.set_starting_point(config.starting_point.clone())?;
Ok(trust)
}
fn save_trust(home: &Path, trust: &Trust) -> Result<()> {
atomic_write(
&home.join("social.json"),
&serde_json::to_vec_pretty(&trust.events())?,
)
}
fn audit_agent(selected: Option<&str>, interactive: bool) -> Result<String> {
if let Some(agent) = selected {
ensure!(
matches!(agent, "codex" | "claude" | "manual"),
"supported audit agents: codex, claude, manual"
);
return Ok(agent.into());
}
ensure!(
interactive,
"choose --audit-agent codex or claude for a non-interactive audit; install and sign in to the chosen agent first"
);
let agents = haps::audit::agents().unwrap_or_else(|_| {
eprintln!("Agent detection needs Python 3; manual review is still available.");
vec!["manual".into()]
});
let labels: Vec<_> = agents
.iter()
.map(|agent| {
if agent == "manual" {
"Review it myself".to_owned()
} else {
format!("Review with {agent}")
}
})
.collect();
let choice = dialoguer::Select::new()
.with_prompt("Choose how to audit")
.items(&labels)
.default(0)
.interact_opt()?
.context("audit cancelled")?;
Ok(agents[choice].clone())
}
async fn record_audit(
home: &Path,
config: &mut Config,
trust: &mut Trust,
keys: &Keys,
release: &Release,
report: (&haps::audit::Evidence, String),
publication: (Option<bool>, bool),
) -> Result<()> {
let (evidence, note) = report;
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_millis();
let mut at = u64::try_from(now)?;
for event in trust.events().iter().filter(|event| {
event.pubkey == keys.public_key()
&& parse_attestation(event)
.is_ok_and(|claim| claim.release == release.event.id.to_hex())
}) {
at = at.max(
(attestation_time_ms(event)? / 1000)
.checked_add(1)
.and_then(|t| t.checked_mul(1000))
.context("audit timestamp overflow")?,
);
}
let event = haps::trust::attest_audit_at(keys, release.event.id, note, evidence, at)?;
trust.ingest(event.clone())?;
save_trust(home, trust)?;
atomic_write(
&home.join("audits").join(format!("{}.json", event.id)),
&serde_json::to_vec_pretty(&event)?,
)?;
eprintln!(
"Audit passed. Full report: {}",
home.join("audits")
.join(format!("{}.json", event.id))
.display()
);
let publish = if let Some(value) = publication.0.or(config.audit.publish) {
value
} else if publication.1 {
let value = dialoguer::Confirm::new()
.with_prompt("Publish this and future audits (signed note, provenance, agent/model) to your configured relays?")
.default(false).interact()?;
config.audit.publish = Some(value);
save_config(home, config)?;
value
} else {
false
};
if publish {
publish_feedback(home, config, &event).await?;
eprintln!("Audit saved and queued for publication.");
} else {
eprintln!(
"Audit saved locally. Change the default with haps audit settings --publish true."
);
}
Ok(())
}
#[derive(Clone, Copy)]
struct AuditOptions<'a> {
requested: bool,
review: &'a AuditReviewArgs,
non_interactive: bool,
allow_untrusted: bool,
allow_warnings: bool,
allow_unaudited: bool,
minimum: usize,
}
async fn ensure_audited(
home: &Path,
config: &mut Config,
trust: &mut Trust,
candidate: &Candidate,
options: AuditOptions<'_>,
) -> Result<()> {
use std::io::IsTerminal;
let release = &candidate.release;
trust.authorize_with_policy(release, options.allow_untrusted, 0, options.allow_warnings)?;
if options.allow_unaudited {
eprintln!("Warning: bypassing the audit requirement for this operation only.");
return Ok(());
}
let enough = trust.audits(release).len() >= options.minimum.max(1);
let requested = options.requested
|| options.review.audit_agent.is_some()
|| options.review.audit_model.is_some();
ensure!(
requested || options.review.publish_audit.is_none(),
"--publish-audit requires --audit or --audit-agent"
);
if enough && !requested {
return Ok(());
}
if release.data.package.source.is_none() {
ensure!(
!requested,
"Audit and install requires pinned source. A completed manual audit can be recorded with its provenance"
);
return trust.authorize_install(release, options.allow_untrusted, options.minimum, options.allow_warnings)
.context("Audit and install requires pinned source. A completed manual audit can be recorded with its provenance");
}
let interactive = !options.non_interactive
&& std::io::stdin().is_terminal()
&& std::io::stderr().is_terminal();
if !requested
&& (!interactive
|| !dialoguer::Confirm::new()
.with_prompt("This release needs an audit. Audit and install?")
.default(false)
.interact()?)
{
return trust.authorize_install(
release,
options.allow_untrusted,
options.minimum,
options.allow_warnings,
);
}
let agent = audit_agent(
options
.review
.audit_agent
.as_deref()
.or(config.audit.agent.as_deref()),
interactive,
)?;
let model = options.review.audit_model.clone().or_else(|| {
(agent != "manual")
.then(|| config.audit.model.clone())
.flatten()
});
let keys = publishing_keys(home, config)?;
*trust = load_trust(home, config)?;
let other_audits = trust
.audits(release)
.iter()
.filter(|e| e.pubkey != keys.public_key())
.count();
ensure!(
other_audits + 1 >= options.minimum.max(1),
"a self-audit adds one reviewer; more independent audits are required by this installation's policy"
);
let result = haps::audit::review_and_rebuild(
home,
release,
&candidate.repository,
&agent,
model.as_deref(),
interactive,
options.review.build_execution.execution(),
)
.await;
let (evidence, note) = match result {
Ok(report) => report,
Err(error) => {
if let Err(publication_error) =
cli_audit::offer_warning(home, config, trust, release, &error, interactive).await
{
eprintln!("Warning publication did not complete: {publication_error:#}");
}
return Err(error);
}
};
record_audit(
home,
config,
trust,
&keys,
release,
(&evidence, note),
(options.review.publish_audit, interactive),
)
.await?;
trust.authorize_install(
release,
options.allow_untrusted,
options.minimum,
options.allow_warnings,
)
}
fn load_comments(home: &Path) -> Result<Comments> {
let mut comments = Comments::default();
let path = home.join("comments.json");
if path.exists() {
for event in read_json::<Vec<Event>>(&path)? {
comments.ingest(event)?;
}
}
Ok(comments)
}
fn save_comments(home: &Path, comments: &Comments) -> Result<()> {
let bytes = serde_json::to_vec_pretty(&comments.events())?;
ensure!(
bytes.len() <= MAX_METADATA,
"local comments exceed metadata limit"
);
atomic_write(&home.join("comments.json"), &bytes)
}
fn select_discussion(
candidates: Vec<Candidate>,
package: &str,
release: Option<&str>,
) -> Result<Candidate> {
if let Some(id) = release {
let id = EventId::from_hex(id)?;
let filtered: Vec<_> = candidates
.into_iter()
.filter(|c| c.release.event.id == id)
.collect();
let normalized = if let Some((key, name)) = package.split_once('/') {
format!("{}/{}", ensure_public_key(key)?, name)
} else {
package.into()
};
let candidate = filtered
.into_iter()
.find(|c| {
c.release.identity() == normalized || c.release.data.package.name == normalized
})
.context("release does not belong to this package or is unavailable")?;
return Ok(candidate);
}
let normalized = if let Some((key, name)) = package.split_once('/') {
format!("{}/{}", ensure_public_key(key)?, name)
} else {
package.into()
};
let mut filtered: Vec<_> = candidates
.into_iter()
.filter(|c| c.release.identity() == normalized || c.release.data.package.name == normalized)
.collect();
let authors: BTreeSet<_> = filtered.iter().map(|c| c.release.author()).collect();
ensure!(
authors.len() == 1,
"package missing or ambiguous; use publisher/name"
);
filtered.sort_by(|a, b| {
b.release
.data
.package
.version
.cmp(&a.release.data.package.version)
});
Ok(filtered.remove(0))
}
fn own_keys(home: &Path, config: &Config) -> Result<Keys> {
let keys = load_keys(&home.join("identity.key"))?;
ensure!(
config.identity.as_deref() == Some(keys.public_key().to_hex().as_str()),
"active identity does not match local signing key"
);
Ok(keys)
}
fn publishing_keys(home: &Path, config: &mut Config) -> Result<Keys> {
if config.identity.is_none() && !home.join("identity.key").exists() {
use std::io::Write;
let keys = Keys::generate();
let mut options = fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options.open(home.join("identity.key"))?;
file.write_all(keys.secret_key().to_secret_hex().as_bytes())?;
file.sync_all()?;
config.identity = Some(keys.public_key().to_hex());
save_config(home, config)?;
}
own_keys(home, config)
}
fn check_checkpoint(source: &mut Source, snapshot: &Snapshot) -> Result<()> {
ensure!(
snapshot.head.sequence >= source.sequence,
"catalog rollback detected"
);
ensure!(
source.event_id.is_empty()
|| snapshot.head.sequence != source.sequence
|| snapshot.event.id.to_hex() == source.event_id,
"catalog changed at the same sequence"
);
source.sequence = snapshot.head.sequence;
source.event_id = snapshot.event.id.to_hex();
Ok(())
}
async fn query_metadata<T>(read: impl std::future::Future<Output = Result<T>>) -> Result<T> {
query_metadata_until(
tokio::time::Instant::now() + std::time::Duration::from_secs(3),
read,
)
.await
}
async fn query_metadata_until<T>(
deadline: tokio::time::Instant,
read: impl std::future::Future<Output = Result<T>>,
) -> Result<T> {
tokio::time::timeout_at(deadline, read)
.await
.context("package metadata response deadline elapsed")?
}
#[derive(Clone, Copy)]
struct PackageSelection<'a> {
identity: &'a str,
version: Option<&'a semver::Version>,
target: &'a str,
}
#[derive(Default)]
struct ObservedLatest(std::sync::Mutex<Option<semver::Version>>);
impl ObservedLatest {
fn record(&self, version: &semver::Version) {
let mut observed = self.0.lock().expect("observed version lock");
if observed.as_ref().is_none_or(|old| version > old) {
*observed = Some(version.clone());
}
}
fn require_available(&self, candidates: &[Candidate]) -> Result<()> {
if let Some(version) = self.0.lock().expect("observed version lock").as_ref() {
ensure!(
candidates
.iter()
.any(|candidate| &candidate.release.data.package.version >= version),
"newest observed release {version} is unavailable or incomplete; refusing an older release"
);
}
Ok(())
}
}
async fn candidates_for_version(
home: &Path,
config: &mut Config,
package: &str,
version: Option<&semver::Version>,
target: &str,
) -> Result<Vec<Candidate>> {
let selection = (package.contains('/') || version.is_some()).then_some(PackageSelection {
identity: package,
version,
target,
});
candidates_matching(home, config, None, Some(package), selection).await
}
async fn direct_candidates(
home: &Path,
query: Option<&str>,
package: Option<&str>,
deadline: tokio::time::Instant,
selection: Option<PackageSelection<'_>>,
observed: &ObservedLatest,
) -> Result<Vec<Candidate>> {
let discovery = Discovery::open(home)?;
let announcements = discovery.announcements(None).await?;
let cached: BTreeMap<_, _> = discovery
.events(vec![nostr::Filter::new().kind(APP_KIND)])
.await?
.into_iter()
.map(|event| (event.id.to_hex(), event))
.collect();
drop(discovery);
let mut reads = Vec::new();
for announcement in announcements {
let Some(ref head) = announcement.head else {
continue;
};
let author = announcement.author();
let identity = format!("{author}/{}", announcement.name);
if package.is_some_and(|p| p != identity && p != announcement.name) {
continue;
}
if let Some(query) = query {
let text = format!("{} {}", announcement.name, head.description).to_lowercase();
if !query
.split_whitespace()
.all(|term| text.contains(&term.to_lowercase()))
{
continue;
}
}
reads.push((identity, author, announcement));
}
use futures::StreamExt;
let mut responses = futures::stream::iter(reads)
.map(|(identity, author, announcement)| {
let cached = &cached;
async move {
let head = announcement.head.expect("direct announcement");
let selected_version = selection.and_then(|selection| {
selection.version.or_else(|| {
head.releases
.iter()
.filter(|pointer| {
pointer.target == selection.target && pointer.version.pre.is_empty()
})
.map(|pointer| &pointer.version)
.max()
})
});
if selection.is_some_and(|selection| selection.version.is_none())
&& let Some(version) = selected_version
{
observed.record(version);
}
let result = query_metadata_until(deadline, async {
let repo = Arc::new(Repository::open(&head.payload, &home.join("cache"))?);
let mut releases = Vec::new();
if let Some(selection) = selection
&& let Some(version) = selection.version
&& !head.releases.iter().any(|pointer| {
pointer.version == *version && pointer.target == selection.target
})
{
let snapshot = repo.catalog(&author).await?;
if let Some(release) = repo
.exact_release(&snapshot, &identity, version, selection.target)
.await?
{
releases.push(Candidate {
repository: repo.clone(),
package_card: Some(head.card.clone()),
release,
});
}
return Ok(releases);
}
for pointer in &head.releases {
if selection.is_some_and(|selection| {
selected_version != Some(&pointer.version)
|| pointer.target != selection.target
}) {
continue;
}
let event: Event = match cached.get(&pointer.id) {
Some(event) => event.clone(),
None => repo.json(&pointer.cid).await?,
};
let release =
head.verify_release(pointer, event, &author, &announcement.name)?;
releases.push(Candidate {
repository: repo.clone(),
package_card: Some(head.card.clone()),
release,
});
}
Ok(releases)
})
.await;
(identity, result)
}
})
.buffer_unordered(8);
let mut found = Vec::new();
while let Ok(Some((identity, result))) =
tokio::time::timeout_at(deadline, responses.next()).await
{
match result {
Ok(releases) => found.extend(releases),
Err(error) => eprintln!(
"Package {identity} unavailable or invalid; results may be incomplete: {error:#}"
),
}
}
Ok(found)
}
async fn candidates(
home: &Path,
config: &mut Config,
query: Option<&str>,
package: Option<&str>,
) -> Result<Vec<Candidate>> {
candidates_matching(home, config, query, package, None).await
}
async fn candidates_matching(
home: &Path,
config: &mut Config,
query: Option<&str>,
package: Option<&str>,
selection: Option<PackageSelection<'_>>,
) -> Result<Vec<Candidate>> {
let budget = if selection.is_some() { 10 } else { 3 };
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(budget);
let observed = ObservedLatest::default();
let mut found = direct_candidates(home, query, package, deadline, selection, &observed).await?;
let mut available = usize::from(!found.is_empty());
let mut seen: BTreeMap<_, _> = found
.iter()
.map(|c| (c.release.coordinate(), c.release.event.id))
.collect();
let direct_authors: BTreeSet<_> = found.iter().map(|c| c.release.author()).collect();
use futures::StreamExt;
let sources: Vec<_> = config
.sources
.iter()
.filter(|(_, source)| {
selection.is_some_and(|selection| selection.version.is_none())
|| !(package.is_some() && direct_authors.contains(&source.author))
})
.map(|(name, source)| (name.clone(), source.location.clone(), source.author.clone()))
.collect();
let mut responses = futures::stream::iter(sources)
.map(|(name, location, author)| {
let observed = &observed;
async move {
let result = query_metadata_until(deadline, async {
let repo = Arc::new(Repository::open(&location, &home.join("cache"))?);
let snapshot = repo.catalog(&author).await?;
let releases = if let Some(selection) = selection {
let release = if let Some(version) = selection.version {
let identity = if selection.identity.contains('/') {
selection.identity.to_owned()
} else {
format!("{author}/{}", selection.identity)
};
repo.exact_release(&snapshot, &identity, version, selection.target)
.await?
} else {
repo.latest_release(
&snapshot,
selection.identity,
selection.target,
|version| observed.record(version),
)
.await?
};
release.into_iter().collect()
} else if let Some(query) = query {
repo.search(&snapshot, query).await?
} else {
repo.releases(&snapshot).await?
};
Ok((repo, snapshot, releases))
})
.await;
(name, result)
}
})
.buffer_unordered(8);
while let Ok(Some((name, result))) = tokio::time::timeout_at(deadline, responses.next()).await {
let (repo, snapshot, releases) = match result {
Ok(response) => response,
Err(error) => {
eprintln!("Package source {name} unavailable or invalid: {error:#}");
continue;
}
};
check_checkpoint(
config
.sources
.get_mut(&name)
.expect("source remains configured"),
&snapshot,
)?;
available += 1;
for release in releases {
if let Some(id) = seen.insert(release.coordinate(), release.event.id) {
ensure!(
id == release.event.id,
"conflicting signed releases at {}",
release.coordinate()
);
continue;
}
found.push(Candidate {
repository: repo.clone(),
package_card: snapshot.catalog.packages.get(&release.identity()).cloned(),
release,
});
}
}
observed.require_available(&found)?;
save_config(home, config)?;
ensure!(
available > 0,
"package discovery is unavailable or incomplete. Check your connection and try haps sync"
);
Ok(found)
}
struct Selection {
non_interactive: bool,
include_untrusted: bool,
confirm_install: bool,
}
fn select(
mut candidates: Vec<Candidate>,
package: &str,
version: Option<&semver::Version>,
aliases: &BTreeMap<String, String>,
trust: &Trust,
target_filter: &str,
selection: Selection,
) -> Result<Candidate> {
use std::io::IsTerminal;
let interactive = !selection.non_interactive
&& std::io::stdin().is_terminal()
&& std::io::stdout().is_terminal()
&& std::io::stderr().is_terminal()
&& !std::env::var("TERM").is_ok_and(|term| term == "dumb");
let package = if let Some((key, name)) = package.split_once('/') {
format!("{}/{}", ensure_public_key(key)?, name)
} else {
package.to_string()
};
candidates.retain(|c| {
(c.release.identity() == package || c.release.data.package.name == package)
&& c.release.data.package.target == target_filter
&& version.is_none_or(|v| &c.release.data.package.version == v)
&& (version.is_some() || c.release.data.package.version.pre.is_empty())
});
let mut latest = BTreeMap::new();
for candidate in &candidates {
let version = &candidate.release.data.package.version;
let current = latest.entry(candidate.release.author()).or_insert(version);
*current = (*current).max(version);
}
let latest: BTreeMap<_, _> = latest.into_iter().map(|(k, v)| (k, v.clone())).collect();
candidates.retain(|c| latest[&c.release.author()] == c.release.data.package.version);
if !selection.include_untrusted && !package.contains('/') {
candidates.retain(|c| trust.socially_trusted(&c.release));
}
let authors: BTreeSet<_> = candidates.iter().map(|c| c.release.author()).collect();
let confirm_single = selection.confirm_install && !package.contains('/') && authors.len() == 1;
if candidates.is_empty() {
let requested = version.map(|v| format!(" version {v}")).unwrap_or_default();
if package.contains('/') {
anyhow::bail!(
"no matching release for {package}{requested} on {target_filter} was observed; discovery may be incomplete"
);
}
anyhow::bail!(
"no matching release for {target_filter} in the selected scope was observed; discovery may be incomplete. Packages outside your social graph require an explicit npub/package"
);
}
if authors.len() > 1 {
let mut choices: Vec<_> = authors.into_iter().collect();
let approvals = |author: &str| {
candidates
.iter()
.filter(|c| c.release.author() == author)
.max_by(|a, b| {
a.release
.data
.package
.version
.cmp(&b.release.data.package.version)
})
.map_or(0, |c| trust.attesters(&c.release).len())
};
choices.sort_by_key(|author| {
(
trust.muted(author),
trust.distance(author).unwrap_or(u32::MAX),
std::cmp::Reverse(approvals(author)),
publisher_label(aliases, author),
)
});
let releases: Vec<_> = choices
.iter()
.map(|author| {
&candidates
.iter()
.filter(|c| c.release.author() == *author)
.max_by_key(|c| &c.release.data.package.version)
.unwrap()
.release
})
.collect();
let options: Vec<_> = releases
.iter()
.map(|release| {
let author = release.author();
format!(
"{} {} · {}{}\n {}",
release_label(release, aliases),
release.data.package.version,
relationship(trust, &author, aliases),
if trust.muted(&author) {
" · muted"
} else {
""
},
attestation_summary(release, trust, aliases),
)
})
.collect();
if !interactive {
return Err(AmbiguousPublishers {
candidates: releases
.iter()
.map(|r| release_json(r, trust, aliases))
.collect(),
}
.into());
}
eprintln!("Several publishers match. Choose one:");
eprintln!("↑/↓ move · Enter select · Esc cancel");
let theme = dialoguer::theme::ColorfulTheme {
active_item_style: dialoguer::console::Style::new().for_stderr().green(),
..Default::default()
};
let index = dialoguer::Select::with_theme(&theme)
.items(&options)
.default(0)
.max_length(5)
.interact_opt()?
.context("cancelled; no publisher selected")?;
candidates.retain(|c| c.release.author() == choices[index]);
}
candidates.sort_by(|a, b| {
b.release
.data
.package
.version
.cmp(&a.release.data.package.version)
});
let candidate = candidates.remove(0);
if confirm_single {
let release = &candidate.release;
if !interactive {
return Err(PublisherConfirmationRequired {
candidate: release_json(release, trust, aliases),
}
.into());
}
use nostr::nips::nip19::ToBech32;
eprintln!(
"Found {} {} · {}",
release_label(release, aliases),
release.data.package.version,
relationship(trust, &release.author(), aliases),
);
eprintln!("Publisher: {}", release.event.pubkey.to_bech32()?);
eprintln!(" {}", attestation_summary(release, trust, aliases));
ensure!(
dialoguer::Confirm::new()
.with_prompt("Install this package?")
.default(false)
.interact()?,
"cancelled; nothing was installed"
);
}
Ok(candidate)
}
fn terminal_text(value: &str) -> String {
value
.chars()
.flat_map(|c| {
if c.is_control() || matches!(c, '\u{202a}'..='\u{202e}' | '\u{2066}'..='\u{2069}') {
c.escape_default().collect::<Vec<_>>()
} else {
vec![c]
}
})
.collect()
}
fn release_label(release: &Release, aliases: &BTreeMap<String, String>) -> String {
format!(
"{}/{}",
terminal_text(&publisher_label(aliases, &release.author())),
release.data.package.name
)
}
fn relationship(trust: &Trust, author: &str, aliases: &BTreeMap<String, String>) -> String {
if trust.distance(author) == Some(0) {
return "You".into();
}
let mut names: Vec<_> = trust
.followed_by_friends(author)
.iter()
.map(|key| terminal_text(&publisher_label(aliases, key)))
.collect();
names.sort();
if !names.is_empty() {
let remaining = names.len().saturating_sub(3);
names.truncate(3);
let suffix = match remaining {
0 => String::new(),
1 => " and 1 other you follow".into(),
n => format!(" and {n} others you follow"),
};
return format!("Followed by {}{suffix}", names.join(", "));
}
match trust.distance(author) {
Some(1) => "Followed by you".into(),
Some(_) => "In your extended graph".into(),
None => "outside your graph".into(),
}
}
fn attestation_summary(
release: &Release,
trust: &Trust,
aliases: &BTreeMap<String, String>,
) -> String {
let mut names: Vec<_> = trust
.attesters(release)
.iter()
.map(|key| terminal_text(&publisher_label(aliases, key)))
.collect();
names.sort();
let mut summary = if names.is_empty() {
"No trusted attestations".into()
} else {
format!("Attested by {}", names.join(", "))
};
summary.push_str(&format!(
"; {} trusted audit(s)",
trust.audits(release).len()
));
let mut warning_names: Vec<_> = trust
.warnings(release)
.iter()
.map(|event| terminal_text(&publisher_label(aliases, &event.pubkey.to_hex())))
.collect();
warning_names.sort();
if !warning_names.is_empty() {
summary.push_str(&format!("; Warning from {}", warning_names.join(", ")));
}
summary
}
fn release_json(
release: &Release,
trust: &Trust,
aliases: &BTreeMap<String, String>,
) -> serde_json::Value {
let attestations: Vec<_> = trust
.attestations(release)
.iter()
.map(|event| {
serde_json::json!({"signer": event.pubkey.to_hex(),
"label": publisher_label(aliases, &event.pubkey.to_hex()), "event": event})
})
.collect();
let warnings: Vec<_> = trust
.warnings(release)
.iter()
.map(|event| {
serde_json::json!({"signer": event.pubkey.to_hex(),
"label": publisher_label(aliases, &event.pubkey.to_hex()), "event": event})
})
.collect();
let audits: Vec<_> = trust
.audits(release)
.iter()
.map(|event| {
serde_json::json!({"signer": event.pubkey.to_hex(),
"label": publisher_label(aliases, &event.pubkey.to_hex()),
"evidence": haps::trust::parse_audit(event).expect("validated audit"), "event": event})
})
.collect();
let source_reviews: Vec<_> = trust.source_reviews(release).iter().map(|event| {
serde_json::json!({"signer": event.pubkey.to_hex(),
"label": publisher_label(aliases, &event.pubkey.to_hex()),
"evidence": haps::trust::parse_source_review(event).expect("validated source review"), "event": event})
}).collect();
serde_json::json!({
"identity": release.identity(),
"label": release_label(release, aliases),
"package": release.data.package, "publisher": release.author(),
"release_id": release.event.id.to_hex(), "manifest": release.data.manifest,
"follow_distance": trust.distance(&release.author()), "muted": trust.muted(&release.author()),
"overmuted": trust.overmuted(&release.author()), "socially_trusted": trust.socially_trusted(release),
"followed_by": trust.followed_by_friends(&release.author()).iter().map(|key| serde_json::json!({"pubkey": key, "label": publisher_label(aliases, key)})).collect::<Vec<_>>(),
"attesters": trust.attesters(release), "attestations": attestations,
"warnings": warnings, "audits": audits, "source_reviews": source_reviews, "minimum_audits": 1,
})
}
#[derive(Debug)]
struct AmbiguousPublishers {
candidates: Vec<serde_json::Value>,
}
impl std::fmt::Display for AmbiguousPublishers {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(
f,
"multiple publishers use this name; choose an explicit publisher/name:"
)?;
for candidate in &self.candidates {
writeln!(
f,
" {} ({})",
candidate["label"].as_str().unwrap(),
candidate["identity"].as_str().unwrap()
)?;
}
Ok(())
}
}
impl std::error::Error for AmbiguousPublishers {}
#[derive(Debug)]
struct PublisherConfirmationRequired {
candidate: serde_json::Value,
}
impl std::fmt::Display for PublisherConfirmationRequired {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"bare-name installation needs publisher confirmation; use an interactive terminal or specify publisher/name:\n haps install {}",
self.candidate["identity"].as_str().unwrap()
)
}
}
impl std::error::Error for PublisherConfirmationRequired {}
fn print_release(
release: &Release,
trust: &Trust,
aliases: &BTreeMap<String, String>,
) -> Result<()> {
println!(
"{}",
serde_json::to_string_pretty(&release_json(release, trust, aliases))?
);
Ok(())
}
fn print_install_start(release: &Release, trust: &Trust, aliases: &BTreeMap<String, String>) {
eprintln!(
"Installing {} {}",
release_label(release, aliases),
release.data.package.version
);
eprintln!(" Signature verified");
eprintln!(" {}", attestation_summary(release, trust, aliases));
for event in trust
.attestations(release)
.into_iter()
.chain(trust.warnings(release))
{
if let Ok(claim) = parse_attestation(event) {
eprintln!(
" {}: {}",
terminal_text(&publisher_label(aliases, &event.pubkey.to_hex())),
terminal_text(&claim.note)
);
if let Ok(evidence) = haps::trust::parse_audit(event) {
eprintln!(
" Binary match: {}. {}",
if evidence.binary_match {
"verified by auditor"
} else {
"unverified"
},
terminal_text(&evidence.provenance)
);
if let Some(reviewer) = evidence.reviewer {
eprintln!(
" {} · requested model: {} · reported model: {}",
terminal_text(&reviewer.agent),
terminal_text(reviewer.requested_model.as_deref().unwrap_or("default")),
terminal_text(reviewer.reported_model.as_deref().unwrap_or("not reported"))
);
}
}
}
}
}
fn print_installed(
release: &Release,
trust: &Trust,
aliases: &BTreeMap<String, String>,
json: bool,
) -> Result<()> {
if json {
println!(
"{}",
serde_json::json!({"status": "installed", "release": release_json(release, trust, aliases)})
);
} else {
println!(
"Installed {} {}",
release_label(release, aliases),
release.data.package.version
);
if release.data.package.app.is_some() || !release.data.package.commands.is_empty() {
println!("Run with: haps run {}", release_label(release, aliases));
}
}
Ok(())
}
#[tokio::main]
async fn main() -> ExitCode {
let cli = Cli::parse();
let json = cli.command.json();
match Box::pin(execute(cli)).await {
Ok(code) => ExitCode::from(code),
Err(error) => {
if json {
let mut value = serde_json::json!({"code": "operation_failed", "message": format!("{error:#}")});
if let Some(ambiguous) = error.downcast_ref::<AmbiguousPublishers>() {
value["code"] = "ambiguous_package".into();
value["candidates"] = serde_json::json!(ambiguous.candidates);
}
if let Some(unconfirmed) = error.downcast_ref::<PublisherConfirmationRequired>() {
value["code"] = "publisher_confirmation_required".into();
value["candidate"] = unconfirmed.candidate.clone();
}
println!("{}", serde_json::json!({"error": value}));
} else {
eprintln!("haps: {error:#}");
}
ExitCode::FAILURE
}
}
}
async fn execute(mut cli: Cli) -> Result<u8> {
let non_interactive = cli.non_interactive || cli.command.json();
haps::progress::Progress::plain_output(non_interactive);
let bootstrap = if matches!(&cli.command, Command::Update { package, .. } if package == "haps")
{
let installer = haps::bootstrap::Installer::current()?;
if installer.is_none() && haps::bootstrap::cargo_installation()? {
anyhow::bail!(
"This Haps installation is managed by Cargo. To update it, run `cargo install haps --locked`. No changes made."
);
}
installer
} else {
None
};
if bootstrap.is_some()
&& let Command::Update { package, .. } = &mut cli.command
{
*package = haps::bootstrap::package()?;
}
let (progress, _reporter) = haps::progress::Progress::stderr(
!cli.command.json()
&& matches!(
cli.command,
Command::Install { .. } | Command::Update { .. }
),
)?;
match &cli.command {
Command::Install { package, .. } => {
progress.stage(format!("Preparing to install {}", terminal_text(package)));
}
Command::Update { package, .. } => {
progress.stage(format!(
"Checking for updates to {}",
terminal_text(package)
));
}
_ => {}
}
if let Command::Init(args) = &cli.command {
let spec = haps::init::create(args)?;
if args.json {
println!(
"{}",
serde_json::to_string(
&serde_json::json!({"status": "created", "manifest": args.out, "package": spec})
)?
);
} else {
println!("Created {}", args.out.display());
println!(
"Check the metadata, then pack it with `haps pack <manifest> --payload <directory> --out <catalog>`."
);
}
return Ok(0);
}
if matches!(cli.command, Command::Target) {
println!("{}", target());
return Ok(0);
}
let home = cli
.home
.or_else(|| dirs::data_local_dir().map(|p| p.join("haps")))
.context("set HAPS_HOME or --home")?;
fs::create_dir_all(&home)?;
let home = home.canonicalize()?;
let guard = lock_with_wait(&home.join(".cli.lock"), || {
progress.stage("Waiting for another Haps command");
})?;
progress.stage("Loading saved social graph");
let config_file = home.join("config.json");
let mut config: Config = if config_file.exists() {
read_json(&config_file)?
} else {
fresh_config(cli.no_defaults)?
};
if config.discovery_defaults_version < DISCOVERY_DEFAULTS_VERSION {
let legacy_index = config.indexes.get("haps").is_some_and(|source| {
source.location == LEGACY_INDEX_LOCATION && source.author == LEGACY_INDEX_AUTHOR
});
let original_defaults = config.discovery_defaults_version == 0
&& (config.starting_point.is_some() || config.sources.contains_key("iris"));
if !cli.no_defaults && (legacy_index || original_defaults) {
if legacy_index {
config.indexes.remove("haps");
}
discovery_presets(&mut config)?;
let npub = hashtree_config::DEFAULT_SOCIALGRAPH_ENTRYPOINT_NPUB;
let author = ensure_public_key(npub)?;
if config.sources.get("iris").is_some_and(|source| {
source.location == format!("htree://{npub}/haps-packages")
&& source.author == author
}) {
config.sources.remove("iris");
}
}
config.discovery_defaults_version = DISCOVERY_DEFAULTS_VERSION;
save_config(&home, &config)?;
}
config.aliases = haps::aliases::read()?;
if !config_file.exists() {
if config.starting_point.is_some() {
let name = hashtree_config::DEFAULT_SOCIALGRAPH_ENTRYPOINT_ALIAS;
if !config.aliases.contains_key(name) {
haps::aliases::add(name, hashtree_config::DEFAULT_SOCIALGRAPH_ENTRYPOINT_NPUB)?;
config.aliases = haps::aliases::read()?;
}
eprintln!(
"Starting point: Sirius Business Ltd (maintainer). Use `haps starting-point` to inspect or change it; `haps starting-point --clear` disables it."
);
}
save_config(&home, &config)?;
}
let mut trust = load_trust(&home, &config)?;
match &mut cli.command {
Command::Install { package, .. } if cli_hash::reference(package)?.is_some() => {}
Command::Install { package, .. }
| Command::Info { package, .. }
| Command::Update { package, .. }
| Command::Run { package, .. }
| Command::Path { package }
| Command::Rollback { package, .. }
| Command::Remove { package }
| Command::Comment { package, .. }
| Command::Comments { package, .. } => {
*package = resolve_package(&config, package)?;
}
_ => {}
}
match &cli.command {
Command::Install { package, .. } if cli_hash::reference(package)?.is_some() => {}
Command::Install { package, .. }
| Command::Info { package, .. }
| Command::Update { package, .. }
| Command::Comment { package, .. }
| Command::Comments { package, .. } => {
let publisher = package
.split_once('/')
.map(|(key, _)| nostr::PublicKey::parse(key))
.transpose()?;
progress.stage("Looking up packages and social trust");
refresh_discovery(
&home,
&mut config,
&mut trust,
publisher,
Some(package.rsplit('/').next().unwrap()),
None,
)
.await?;
}
_ => {}
}
let installation = Installation::new(home.clone())?.with_progress(progress.clone());
let is_warning = matches!(&cli.command, Command::Warn(_));
match cli.command {
Command::Audit { action } => {
Box::pin(cli_audit::run(
&home,
&mut config,
&mut trust,
action,
non_interactive,
))
.await?;
}
Command::Target | Command::Init(_) => unreachable!(),
Command::Add {
package,
payload,
catalog,
} => {
let directory = haps::event_catalog::directory(&home, &catalog)?;
if let Some(payload) = payload {
let spec =
haps::build::manifest::Manifest::parse(&fs::read_to_string(&package)?)?.package;
let keys = publishing_keys(&home, &mut config)?;
let repo = Repository::local(directory.join("packages"))?;
let release = repo.publish(&keys, spec, &payload).await?;
Discovery::open(&directory)?.ingest([release.event]).await?;
} else {
let package = resolve_package(&config, &package)?;
let publisher = package
.split_once('/')
.map(|(key, _)| nostr::PublicKey::parse(key))
.transpose()?;
refresh_discovery(
&home,
&mut config,
&mut trust,
publisher,
Some(package.rsplit('/').next().unwrap()),
None,
)
.await?;
haps::event_catalog::add_package(&home, &catalog, &package).await?;
}
println!("Added to catalog {catalog}. Publish with: haps catalog publish {catalog}");
}
Command::Catalog { action } => {
Box::pin(cli_catalogs::run(&home, &mut config, &mut trust, action)).await?;
}
Command::StartingPoint { public_key, clear } => {
if clear {
config.starting_point = None;
save_config(&home, &config)?;
println!("Starting point disabled");
} else if let Some(key) = public_key {
config.starting_point = Some(resolve_key(&config, &key)?);
save_config(&home, &config)?;
}
println!("{}", config.starting_point.as_deref().unwrap_or("none"));
}
Command::Alias { action } => match action {
AliasAction::Add { name, public_key } => {
let key = ensure_public_key(&public_key)?;
haps::aliases::add(&name, &key)?;
println!("{name}: {key}");
}
AliasAction::List => println!("{}", serde_json::to_string_pretty(&config.aliases)?),
AliasAction::Remove { name } => {
haps::aliases::remove(&name)?;
}
},
Command::Identity { action } => match action {
IdentityAction::Init => {
let path = home.join("identity.key");
ensure!(
!path.exists(),
"identity already exists; use `haps identity show`"
);
let keys = Keys::generate();
use std::io::Write;
let mut options = fs::OpenOptions::new();
options.write(true).create_new(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o600);
}
let mut file = options.open(path)?;
file.write_all(keys.secret_key().to_secret_hex().as_bytes())?;
file.sync_all()?;
config.identity = Some(keys.public_key().to_hex());
save_config(&home, &config)?;
println!("{}", keys.public_key().to_hex());
}
IdentityAction::Use { public_key } => {
config.identity = Some(ensure_public_key(&public_key)?);
save_config(&home, &config)?;
println!("{}", config.identity.unwrap());
}
IdentityAction::Show => {
println!("{}", config.identity.context("no identity configured")?)
}
},
Command::Pack {
manifest,
package,
target: selected_target,
payload,
out,
key_file,
} => {
let spec = haps::build::manifest::Manifest::select(
&fs::read_to_string(manifest)?,
selected_target.as_deref().unwrap_or(target()),
package.as_deref(),
)?
.package;
let keys = load_keys(&key_file.unwrap_or_else(|| home.join("identity.key")))?;
let repo = Repository::local(out)?;
let release = repo.publish(&keys, spec, &payload).await?;
println!("{}", serde_json::to_string_pretty(&release.event)?);
}
Command::Manifest {
path,
package,
target: selected_target,
} => {
ensure!(
fs::metadata(&path)?.len() <= MAX_METADATA as u64,
"manifest is too large"
);
let manifest = haps::build::manifest::Manifest::select(
&fs::read_to_string(path)?,
selected_target.as_deref().unwrap_or(target()),
package.as_deref(),
)?;
manifest.package.validate()?;
println!("{}", serde_json::to_string_pretty(&manifest)?);
}
Command::Link { package, bin_dir } => {
let package = resolve_package(&config, &package)?;
let directory = installation.link(&package, bin_dir.as_deref())?;
println!(
"Linked commands in {}. Add this directory to PATH before other installations.",
directory.display()
);
}
Command::Security {
rules,
scanner,
disable,
} => {
haps::security::configure(&home, rules.as_deref(), scanner.as_deref(), disable)?;
}
Command::ImportRelease(args) => {
if let Some(prepared) = haps::release::prepare(&args, &home).await? {
let haps::release::PreparedRelease {
directory: path,
catalog: name,
keys,
releases,
packages,
} = prepared;
if args.publish {
let mut events = haps::event_catalog::package_events_from(
&home,
&path,
&keys,
None,
args.config
.canonicalize()?
.parent()
.context("release config has no parent")?,
)
.await?;
let location = haps::event_catalog::upload_index(&home, &events)?;
let discovery = Discovery::open(&home)?;
let previous = discovery.events(vec![nostr::Filter::new()]).await?;
events.push(haps::event_catalog::advance(
&keys,
haps::event_catalog::IndexAnnouncement::sign(&keys, &name, &location)?,
&previous,
)?);
let required: Vec<_> = events
.iter()
.filter(|event| {
releases.contains(&event.id)
|| (event.kind == haps::discovery::SOFTWARE_KIND
&& haps::model::tag_value(event, "d")
.is_ok_and(|name| packages.contains(name)))
|| event.kind == haps::event_catalog::INDEX_KIND
})
.cloned()
.collect();
discovery.queue(&required)?;
discovery.ingest(events).await?;
let bus = haps::discovery::relay_bus(&home).await?;
ensure!(
discovery.flush_events(&bus, &required).await? == 0,
"release announcements remain queued; run haps sync before considering publication complete"
);
println!("Release announcements acknowledged by a relay");
let historical = discovery.pending()?;
if historical > 0 {
eprintln!(
"{historical} unrelated announcements remain queued; retry with haps sync"
);
}
} else {
println!("Prepared catalog {name}; publish with haps catalog publish {name}");
}
}
}
Command::Publish {
catalog,
name,
key_file,
} => {
let path = catalog
.canonicalize()
.context("catalog directory is missing")?;
let keys = load_keys(&key_file.unwrap_or_else(|| home.join("identity.key")))?;
let events =
haps::event_catalog::package_events(&home, &path, &keys, Some(&name)).await?;
let discovery = Discovery::open(&home)?;
discovery.queue(&events)?;
discovery.ingest(events.clone()).await?;
flush_discovery(&home, &discovery, &events).await?;
}
Command::Sync => {
let discovery = Discovery::open(&home)?;
let bus = haps::discovery::relay_bus(&home).await?;
let pending = discovery.flush(&bus).await?;
ensure!(
pending == 0,
"{pending} announcements remain queued; retry with haps sync"
);
println!("Announcements acknowledged by a relay");
drop(discovery);
refresh_discovery(&home, &mut config, &mut trust, None, None, None).await?;
}
Command::Index { action } => match action {
IndexAction::Add {
name,
location,
author,
} => {
safe_name(&name)?;
let author = resolve_key(&config, &author)?;
Discovery::open(&home)?
.import_index(&location, &author)
.await?;
config.indexes.insert(
name,
Source {
location,
author,
sequence: 0,
event_id: String::new(),
},
);
save_config(&home, &config)?;
}
IndexAction::Remove { name } => {
ensure!(config.indexes.remove(&name).is_some(), "index not found");
save_config(&home, &config)?;
}
IndexAction::List => println!("{}", serde_json::to_string_pretty(&config.indexes)?),
IndexAction::Build { out, key_file } => {
refresh_discovery(&home, &mut config, &mut trust, None, None, None).await?;
let keys = load_keys(&key_file.unwrap_or_else(|| home.join("identity.key")))?;
Discovery::open(&home)?.export_index(&out, &keys).await?;
println!(
"Built signed index in {}. Publish with htree add <directory> --publish <name>.",
out.display()
);
}
},
command @ Command::Build { .. } => {
Box::pin(cli_source::execute_build(
&home,
&mut config,
&mut trust,
&installation,
command,
non_interactive,
))
.await?;
}
Command::Source { action } => match action {
SourceAction::Add {
name,
location,
author,
} => {
safe_name(&name)?;
ensure!(
!config.sources.contains_key(&name),
"source already exists; remove it explicitly before replacing its publisher pin"
);
let author = resolve_key(&config, &author)?;
let location = if location.starts_with("https://")
|| location.starts_with("http://")
|| location.starts_with("htree://")
{
location
} else {
Path::new(&location)
.canonicalize()?
.to_str()
.context("non-UTF8 source path")?
.to_string()
};
let repo = Repository::open(&location, &home.join("cache"))?;
let snapshot = repo.catalog(&author).await?;
config.sources.insert(
name.clone(),
Source {
location,
author,
sequence: snapshot.head.sequence,
event_id: snapshot.event.id.to_hex(),
},
);
save_config(&home, &config)?;
println!("Added {name}");
}
SourceAction::List => println!("{}", serde_json::to_string_pretty(&config.sources)?),
SourceAction::Remove { name } => {
ensure!(config.sources.remove(&name).is_some(), "source not found");
save_config(&home, &config)?;
}
},
Command::Search {
query,
json,
stream,
} => {
Box::pin(cli_search::run(
&home,
&mut config,
&mut trust,
&query,
json,
stream,
))
.await?;
}
Command::Info {
package, version, ..
} => {
let choices =
candidates_for_version(&home, &mut config, &package, version.as_ref(), target())
.await?;
let releases: Vec<_> = choices.iter().map(|c| c.release.clone()).collect();
refresh_feedback(&home, &config, &mut trust, &releases).await?;
let candidate = select(
choices,
&package,
version.as_ref(),
&config.aliases,
&trust,
target(),
Selection {
non_interactive,
include_untrusted: false,
confirm_install: false,
},
)?;
print_release(&candidate.release, &trust, &config.aliases)?;
}
command @ Command::Install { .. } => {
Box::pin(cli_install::install(
&home,
&mut config,
&mut trust,
&installation,
&progress,
command,
non_interactive,
))
.await?;
}
Command::Update {
package,
allow_untrusted,
allow_warnings,
allow_unaudited,
audit,
audit_review,
json,
} => {
let (identity, minimum_attestations) = if bootstrap.is_some() {
let receipts = installation.receipts()?;
(
package.clone(),
receipts
.get(&package)
.map_or(1, |r| r.minimum_attestations.max(1)),
)
} else {
let receipt = installation.receipt(&package)?;
(
Release::verify(receipt.current)?.identity(),
receipt.minimum_attestations.max(1),
)
};
progress.stage("Reading package catalogs");
let choices =
candidates_for_version(&home, &mut config, &identity, None, target()).await?;
let releases: Vec<_> = choices.iter().map(|c| c.release.clone()).collect();
progress.stage("Checking release attestations and warnings");
refresh_feedback(&home, &config, &mut trust, &releases).await?;
progress.clear();
let candidate = select(
choices,
&identity,
None,
&config.aliases,
&trust,
target(),
Selection {
non_interactive,
include_untrusted: true,
confirm_install: false,
},
)?;
if let Some(installer) = &bootstrap {
installer.validate_release(&candidate.release)?;
}
ensure_audited(
&home,
&mut config,
&mut trust,
&candidate,
AuditOptions {
requested: audit,
review: &audit_review,
non_interactive,
allow_untrusted: allow_untrusted || package.contains('/'),
allow_warnings,
allow_unaudited,
minimum: minimum_attestations,
},
)
.await?;
if !trust.socially_trusted(&candidate.release) {
eprintln!(
"Warning: {} is not authored or vouched for by your social graph (or its author/vouchers are overmuted); proceeding with your explicit choice.",
release_label(&candidate.release, &config.aliases)
);
}
if !json {
print_install_start(&candidate.release, &trust, &config.aliases);
}
cli_dependencies::install(
&home,
&mut config,
&mut trust,
&installation,
&candidate,
AuditOptions {
requested: audit,
review: &audit_review,
non_interactive,
allow_untrusted: allow_untrusted || package.contains('/'),
allow_warnings,
allow_unaudited,
minimum: minimum_attestations,
},
allow_untrusted,
&[],
)
.await?;
if let Some(installer) = &bootstrap {
progress.stage("Switching to Haps-managed updates");
installation.adopt_installer(&identity, installer)?;
}
progress.clear();
print_installed(&candidate.release, &trust, &config.aliases, json)?;
}
Command::Run {
package,
command,
args,
} => {
#[cfg(target_os = "macos")]
if command.is_none() {
let release = Release::verify(installation.receipt(&package)?.current)?;
if let Some(app) = release.data.package.app {
let path = installation.path(&package)?.join(safe_path(&app)?);
let mut process = std::process::Command::new("open");
if let Some(runtime) = installation.runtime_path(&package)? {
let mut value = std::ffi::OsString::from("PATH=");
value.push(runtime);
process.arg("--env").arg(value);
}
drop(guard);
let status = process
.arg("-a")
.arg(path)
.arg("--args")
.args(args)
.status()?;
return Ok(if status.success() { 0 } else { 1 });
}
}
let executable = installation.command(&package, command.as_deref())?;
let mut process = std::process::Command::new(executable);
if let Some(path) = installation.runtime_path(&package)? {
process.env("PATH", path);
}
process.env("HAPS_HOME", &home);
#[cfg(target_os = "linux")]
process.env(
"XDG_DATA_DIRS",
haps::launch::linux_data_dirs(
&installation.path(&package)?,
std::env::var_os("XDG_DATA_DIRS").as_deref(),
)?,
);
drop(guard);
let status = process.args(args).status()?;
return Ok(status
.code()
.and_then(|c| u8::try_from(c).ok())
.unwrap_or(1));
}
Command::Path { package } => println!("{}", installation.path(&package)?.display()),
#[cfg(target_os = "linux")]
Command::Omarchy { remove, menu } => {
let mut process = std::process::Command::new("python3");
process
.arg("-c")
.arg(include_str!("integrations/omarchy.py"))
.arg(std::env::current_exe()?)
.arg(&home);
if remove {
process.arg("--remove");
} else if menu {
process.arg("--menu");
}
drop(guard);
ensure!(
process.status()?.success(),
"Omarchy menu integration failed"
);
}
Command::List { json } => {
let mut releases = Vec::new();
for (id, receipt) in installation.receipts()? {
let release = Release::verify(receipt.current)?;
if json {
releases.push(release_json(&release, &trust, &config.aliases));
} else {
println!(
"{id}\t{}\t{}",
release.data.package.version, release.event.id
);
}
}
if json {
println!("{}", serde_json::to_string(&releases)?);
}
}
Command::Rollback {
package,
allow_unaudited,
} => {
let plan = installation.rollback_plan(&package)?;
refresh_feedback(&home, &config, &mut trust, &plan).await?;
for release in &plan {
let receipt = installation.receipt(&release.identity())?;
if allow_unaudited {
trust.authorize_with_policy(release, true, 0, false)?;
} else {
trust.authorize_install(release, true, receipt.minimum_attestations, false)?;
}
}
if allow_unaudited {
eprintln!("Warning: bypassing the audit requirement for this operation only.");
}
installation.rollback_checked(&package, &plan)?;
println!("Rolled back {package}");
}
Command::Remove { package } => {
installation.remove(&package)?;
println!("Removed {package}");
}
Command::Follow { public_key, export } => {
let keys = own_keys(&home, &config)?;
let public_key = nostr::PublicKey::parse(&resolve_key(&config, &public_key)?)?;
let old = trust
.events()
.into_iter()
.find(|e| e.kind == Kind::ContactList && e.pubkey == keys.public_key());
let mut tags: Vec<Tag> = old
.as_ref()
.map(|e| e.tags.iter().cloned().collect())
.unwrap_or_default();
if !tags.iter().any(|t| {
t.as_slice().first().is_some_and(|v| v == "p")
&& t.as_slice().get(1) == Some(&public_key.to_hex())
}) {
tags.push(Tag::public_key(public_key));
}
let timestamp = Timestamp::from(
Timestamp::now().as_secs().max(
old.as_ref()
.map(|e| e.created_at.as_secs() + 1)
.unwrap_or(0),
),
);
let event = EventBuilder::new(
Kind::ContactList,
old.as_ref().map(|e| e.content.as_str()).unwrap_or(""),
)
.tags(tags)
.custom_created_at(timestamp)
.sign_with_keys(&keys)?;
trust.ingest(event.clone())?;
save_trust(&home, &trust)?;
if let Some(path) = export {
atomic_write(&path, &serde_json::to_vec_pretty(&event)?)?;
}
println!("Following {} locally", public_key.to_hex());
}
Command::Import { events } => {
let value: serde_json::Value = read_json(&events)?;
let events: Vec<Event> = if value.is_array() {
serde_json::from_value(value)?
} else {
vec![serde_json::from_value(value)?]
};
ensure!(events.len() <= 10_000, "too many events");
let count = events.len();
let mut comments = load_comments(&home)?;
for event in events {
if event.kind == Kind::Comment {
comments.ingest(event)?;
} else {
trust.ingest(event)?;
}
}
save_trust(&home, &trust)?;
save_comments(&home, &comments)?;
println!("Imported {count} signed events");
}
Command::Attest(args) | Command::Warn(args) => {
let ClaimArgs {
release,
version,
target: requested_target,
note,
audited,
provenance,
revoke,
out,
json,
} = args;
let keys = own_keys(&home, &config)?;
ensure!(
!is_warning || !audited,
"--audited is only valid for an approval"
);
let audit_evidence = provenance.map(haps::audit::Evidence::manual).transpose()?;
ensure!(
!note.trim().is_empty(),
"describe your finding or checked work with --note"
);
let release_id = if let Ok(id) = EventId::from_hex(&release) {
ensure!(
version.is_none(),
"--version is only used with a package name"
);
id
} else {
let version = version.context(
"use publisher/package --version VERSION, or an exact release event ID",
)?;
let package = resolve_package(&config, &release)?;
let candidate = select(
candidates_for_version(
&home,
&mut config,
&package,
Some(&version),
requested_target.as_deref().unwrap_or(target()),
)
.await?,
&package,
Some(&version),
&config.aliases,
&trust,
requested_target.as_deref().unwrap_or(target()),
Selection {
non_interactive,
include_untrusted: false,
confirm_install: false,
},
)?;
if !json {
eprintln!(
"{} {} {} ({})",
if revoke {
"Withdrawing claim for"
} else if is_warning {
"Warning about"
} else {
"Attesting to"
},
release_label(&candidate.release, &config.aliases),
version,
candidate.release.data.package.target
);
eprintln!(" Release: {}", candidate.release.event.id);
eprintln!(" Claim: {}", terminal_text(¬e));
}
candidate.release.event.id
};
let mut event = if is_warning {
warn(&keys, release_id, !revoke, note)?
} else {
attest(&keys, release_id, !revoke, note)?
};
let previous = trust
.events()
.into_iter()
.filter(|e| {
e.pubkey == keys.public_key()
&& parse_attestation(e).is_ok_and(|a| a.release == release_id.to_hex())
})
.map(|e| attestation_time_ms(&e))
.collect::<Result<Vec<_>>>()?
.into_iter()
.max();
if let Some(previous) = previous
&& previous / 1000 >= event.created_at.as_secs()
{
let claim = parse_attestation(&event)?;
let build = if is_warning { warn_at } else { attest_at };
event = build(
&keys,
release_id,
if is_warning {
claim.warning
} else {
claim.approved
},
claim.note,
(previous / 1000)
.checked_add(1)
.and_then(|seconds| seconds.checked_mul(1000))
.context("attestation timestamp overflow")?,
)?;
}
if let Some(evidence) = audit_evidence {
event = haps::trust::attest_audit_at(
&keys,
release_id,
parse_attestation(&event)?.note,
&evidence,
attestation_time_ms(&event)?,
)?;
}
trust.ingest(event.clone())?;
save_trust(&home, &trust)?;
let out =
out.unwrap_or_else(|| home.join("attestations").join(format!("{}.json", event.id)));
atomic_write(&out, &serde_json::to_vec_pretty(&event)?)?;
publish_feedback(&home, &config, &event).await?;
if json {
println!("{}", serde_json::to_string(&event)?);
} else {
println!("{}", event.id);
eprintln!("Saved signed claim: {}", out.display());
}
}
Command::Comment {
package,
text,
release,
reply_to,
out,
} => {
let candidate = select_discussion(
candidates(&home, &mut config, None, Some(&package)).await?,
&package,
release.as_deref(),
)?;
refresh_feedback(
&home,
&config,
&mut trust,
std::slice::from_ref(&candidate.release),
)
.await?;
let mut comments = load_comments(&home)?;
let parent = reply_to.as_deref().map(EventId::from_hex).transpose()?;
let parent = parent
.map(|id| {
comments
.get(&id)
.context("reply target is unavailable; try haps comments first")
})
.transpose()?;
let root = if release.is_some() {
candidate.release.event.clone()
} else {
let cid = candidate
.package_card
.as_ref()
.context("package card is missing")?;
let event: Event = candidate.repository.json(cid).await?;
ensure!(
event.pubkey == candidate.release.event.pubkey
&& tag_value(&event, "d")?
== format!("haps/package/{}", candidate.release.data.package.name),
"package card publisher/name mismatch"
);
event
};
let event = comments::create(
&own_keys(&home, &config)?,
&root,
release.is_some(),
parent,
text,
)?;
comments.ingest(event.clone())?;
save_comments(&home, &comments)?;
if let Some(out) = out {
atomic_write(&out, &serde_json::to_vec_pretty(&event)?)?;
}
publish_feedback(&home, &config, &event).await?;
println!("{}", event.id);
}
Command::Comments { package, release } => {
let candidate = select_discussion(
candidates(&home, &mut config, None, Some(&package)).await?,
&package,
release.as_deref(),
)?;
refresh_feedback(
&home,
&config,
&mut trust,
std::slice::from_ref(&candidate.release),
)
.await?;
let scope = if release.is_some() {
Scope::release(&candidate.release)
} else {
Scope::package(&candidate.release)
};
let comments = load_comments(&home)?;
for event in comments.thread(&scope) {
if trust.muted(&event.pubkey.to_hex()) {
continue;
}
println!(
"{}",
serde_json::to_string_pretty(&serde_json::json!({
"event": event, "follow_distance": trust.distance(&event.pubkey.to_hex()),
}))?
);
}
}
}
Ok(0)
}
fn networking_enabled(config: &Config) -> bool {
config.starting_point.is_some() || std::env::var_os("NOSTR_RELAYS").is_some()
}
async fn publish_scan(home: &Path, config: &Config, event: Event) -> Result<()> {
let mut comments = load_comments(home)?;
comments.ingest(event.clone())?;
save_comments(home, &comments)?;
publish_feedback(home, config, &event).await?;
eprintln!(
"Signed source scan: {} (evidence only; no release endorsement)",
event.id
);
Ok(())
}
async fn publish_feedback(home: &Path, config: &Config, event: &Event) -> Result<()> {
let discovery = Discovery::open(home)?;
discovery.queue(std::slice::from_ref(event))?;
if networking_enabled(config) {
let result = async {
let bus = haps::discovery::relay_bus(home).await?;
discovery.flush(&bus).await
}
.await;
match result {
Ok(0) => eprintln!("Signed feedback acknowledged by a relay"),
Ok(_) => eprintln!("Signed feedback queued; retry with haps sync"),
Err(error) => eprintln!("Signed feedback queued: {error:#}. Retry with haps sync"),
}
}
Ok(())
}
async fn refresh_feedback(
home: &Path,
config: &Config,
trust: &mut Trust,
releases: &[Release],
) -> Result<()> {
if releases.is_empty() {
return Ok(());
}
let result = async {
let relay = if networking_enabled(config) {
match haps::discovery::relay_bus(home).await {
Ok(bus) => Some(Arc::new(bus)),
Err(error) => {
eprintln!("Relay feedback unavailable; using indexes: {error:#}");
None
}
}
} else {
None
};
let indexes: Vec<_> = config
.indexes
.values()
.map(|source| (source.location.clone(), source.author.clone()))
.collect();
let lookup = Discovery::open(home)?
.event_lookup(relay, &indexes, std::time::Duration::from_secs(3))
.await?;
let events: Vec<Event> = futures::future::try_join_all(
releases
.chunks(128)
.map(|batch| haps::feedback::refresh(&lookup, batch)),
)
.await?
.into_iter()
.flatten()
.collect();
Ok::<_, anyhow::Error>(events)
}
.await;
let events = match result {
Ok(events) => events,
Err(error) => {
eprintln!("Feedback refresh unavailable; cached findings may be incomplete: {error:#}");
return Ok(());
}
};
let mut comments = load_comments(home)?;
for event in events {
if event.kind == Kind::Comment {
comments.ingest(event)?;
} else {
trust.ingest(event)?;
}
}
save_trust(home, trust)?;
save_comments(home, &comments)
}
async fn flush_discovery(home: &Path, discovery: &Discovery, events: &[Event]) -> Result<()> {
let bus = haps::discovery::relay_bus(home).await?;
let pending = discovery.flush_events(&bus, events).await?;
ensure!(
pending == 0,
"{pending} publication announcements remain queued; retry with haps sync"
);
println!("Announcements acknowledged by a relay");
let historical = discovery.pending()?;
if historical > 0 {
eprintln!("{historical} unrelated announcements remain queued; retry with haps sync");
}
Ok(())
}
async fn discover_social_indexes(
home: &Path,
config: &mut Config,
trust: &mut Trust,
discovery: &Discovery,
relay: Option<&Arc<nostr_pubsub_relay::RelayEventBus>>,
) -> Result<()> {
use haps::event_catalog::{INDEX_KIND, IndexAnnouncement};
let indexes: Vec<_> = config
.indexes
.values()
.map(|source| (source.location.clone(), source.author.clone()))
.collect();
let lookup = discovery
.event_lookup(relay.cloned(), &indexes, std::time::Duration::from_secs(2))
.await?;
let mut queried = BTreeSet::new();
for _ in 0..2 {
let authors: Vec<_> = trust
.discovery_authors(1)
.into_iter()
.filter(|key| queried.insert(*key))
.collect();
if authors.is_empty() {
break;
}
let events = lookup
.query(vec![
nostr::Filter::new()
.kinds([Kind::ContactList, Kind::MuteList])
.authors(authors)
.limit(512),
])
.await?;
for event in events {
if let Err(error) = trust.ingest(event) {
eprintln!("Invalid social event: {error:#}");
}
}
save_trust(home, trust)?;
}
let authors = trust.discovery_authors(2);
if !authors.is_empty() {
let events = lookup
.query(vec![
nostr::Filter::new()
.kinds([INDEX_KIND, APP_KIND])
.custom_tag(
nostr::SingleLetterTag::lowercase(nostr::Alphabet::L),
"hashtree",
)
.authors(authors)
.limit(256),
])
.await?;
discovery.ingest(events).await?;
}
config.indexes.retain(|name, source| {
!name.starts_with("social-")
|| (!trust.overmuted(&source.author)
&& trust.distance(&source.author).is_some_and(|d| d <= 2))
});
for event in discovery
.events(vec![nostr::Filter::new().kinds([INDEX_KIND, APP_KIND])])
.await?
{
let Ok(announcement) = IndexAnnouncement::verify(event) else {
continue;
};
let author = announcement.event.pubkey.to_hex();
if trust.overmuted(&author) || !trust.distance(&author).is_some_and(|d| d <= 2) {
continue;
}
let name = format!(
"social-{author}-{}",
hex::encode(tag_value(&announcement.event, "d")?)
);
config.indexes.insert(
name,
Source {
location: announcement.location,
author,
sequence: 0,
event_id: String::new(),
},
);
}
Ok(())
}
async fn refresh_discovery(
home: &Path,
config: &mut Config,
trust: &mut Trust,
publisher: Option<nostr::PublicKey>,
package: Option<&str>,
query: Option<&str>,
) -> Result<()> {
let discovery = Discovery::open(home)?;
let relay = if networking_enabled(config) {
match haps::discovery::relay_bus(home).await {
Ok(bus) => Some(Arc::new(bus)),
Err(error) => {
eprintln!("Relay discovery unavailable; using indexes: {error:#}");
None
}
}
} else {
None
};
discover_social_indexes(home, config, trust, &discovery, relay.as_ref()).await?;
let indexes: Vec<_> = config
.indexes
.values()
.map(|source| (source.location.clone(), source.author.clone()))
.collect();
discovery
.lookup_sources(
relay,
&indexes,
publisher,
package,
query,
std::time::Duration::from_secs(3),
)
.await?;
for announcement in discovery.announcements(publisher).await? {
if announcement.head.is_some() {
continue;
}
if let Some(query) = query {
let text =
format!("{} {}", announcement.name, announcement.event.content).to_lowercase();
if !query
.split_whitespace()
.any(|term| text.contains(&term.to_lowercase()))
{
continue;
}
}
let author = announcement.author();
if config
.sources
.values()
.any(|s| s.author == author && s.location == announcement.location)
{
continue;
}
let name = format!("discovered-{}-{}", author, announcement.name);
let source = config.sources.entry(name).or_insert_with(|| Source {
location: announcement.location.clone(),
author,
sequence: 0,
event_id: String::new(),
});
source.location = announcement.location;
}
save_config(home, config)
}
#[cfg(test)]
mod stack_tests {
use super::*;
#[test]
fn missing_explicit_release_reports_incomplete_observation_without_social_hint() {
let keys = Keys::generate();
let package = format!("{}/hello", keys.public_key().to_hex());
let version = "2.0.0".parse().unwrap();
let error = select(
vec![],
&package,
Some(&version),
&BTreeMap::new(),
&Trust::new(keys.public_key().to_hex()),
target(),
Selection {
non_interactive: true,
include_untrusted: false,
confirm_install: false,
},
)
.err()
.expect("no observed release")
.to_string();
assert!(error.contains("version 2.0.0"));
assert!(error.contains("discovery may be incomplete"));
assert!(!error.contains("require an explicit"));
}
#[test]
fn command_dispatch_runs_on_a_small_stack() {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let cli = Cli::parse_from(["haps", "--no-defaults", "target"]);
let code = std::thread::Builder::new()
.stack_size(768 * 1024)
.spawn(move || runtime.block_on(Box::pin(execute(cli))).unwrap())
.unwrap()
.join()
.unwrap();
assert_eq!(code, 0);
}
}