use std::fmt;
use crate::types::manifest::PackVersion;
use crate::types::project::CommitId;
use crate::types::project::GitTag;
use crate::types::project::GitUrl;
use crate::types::project::Pin;
use crate::types::project::Source;
use crate::types::resolve::RemoteTag;
use crate::types::resolve::SourceKey;
use crate::types::resolve::TagVersion;
use crate::types::resolve::latest_tag;
use crate::types::resolve::tag::precedence;
use crate::types::shorthand::official_major;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UpdateNote {
NoVersion {
key: SourceKey,
tag: GitTag,
},
NoNewer {
key: SourceKey,
tag: GitTag,
},
PinnedRev {
key: SourceKey,
rev: CommitId,
},
}
impl fmt::Display for UpdateNote {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoVersion { key, tag } => write!(
f,
"{}: tag \"{tag}\" has no version to update from",
key.location()
),
Self::NoNewer { key, tag } => {
write!(f, "{}: no newer tag than {tag}", key.location())
}
Self::PinnedRev { key, rev } => {
write!(f, "{}: pinned to rev {rev}, not updated", key.location())
}
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UpdateOutcome {
NewTag(GitTag),
Note(UpdateNote),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateEntry {
pub key: SourceKey,
pub current: Option<GitTag>,
pub outcome: UpdateOutcome,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UpdateQuery {
pub key: SourceKey,
pub url: GitUrl,
pub prefix: String,
pub tag: GitTag,
pub version: PackVersion,
}
impl UpdateQuery {
#[must_use]
pub fn for_source(key: SourceKey, source: &Source) -> Option<Result<Self, UpdateEntry>> {
let Source::Git { url, pin, .. } = source else {
return None;
};
let tag = match pin {
Pin::Rev(rev) => {
let note = UpdateNote::PinnedRev {
key: key.clone(),
rev: rev.clone(),
};
return Some(Err(UpdateEntry {
key,
current: None,
outcome: UpdateOutcome::Note(note),
}));
}
Pin::Tag(tag) => tag,
};
let Some(TagVersion { prefix, version }) = TagVersion::split(tag) else {
let note = UpdateNote::NoVersion {
key: key.clone(),
tag: tag.clone(),
};
return Some(Err(UpdateEntry {
key,
current: Some(tag.clone()),
outcome: UpdateOutcome::Note(note),
}));
};
Some(Ok(Self {
key,
url: url.clone(),
prefix,
tag: tag.clone(),
version,
}))
}
#[must_use]
pub fn finish(self, tags: &[RemoteTag]) -> UpdateEntry {
let major = official_major(&self.url, &self.prefix);
let newer = latest_tag(tags, &self.prefix, major)
.filter(|(_, version)| precedence(version, &self.version).is_gt())
.map(|(tag, _)| tag);
let outcome = newer.map_or_else(
|| {
UpdateOutcome::Note(UpdateNote::NoNewer {
key: self.key.clone(),
tag: self.tag.clone(),
})
},
UpdateOutcome::NewTag,
);
UpdateEntry {
key: self.key,
current: Some(self.tag),
outcome,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::name::PackName;
use crate::types::name::ToolName;
use crate::types::project::LocalPath;
type TestResult = Result<(), Box<dyn std::error::Error>>;
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
const OLD: &str = "aec92b4596aebd6fbc0d2ab92532bbe3c6e0bdcd";
const NEW: &str = "90e2a535db371753890122ed088c81bf3867be7a";
fn core() -> Result<SourceKey, Box<dyn std::error::Error>> {
Ok(SourceKey::Pack(PackName::new("core")?))
}
fn source_at(url: &str, tag: &str) -> Result<Source, Box<dyn std::error::Error>> {
Ok(Source::Git {
url: GitUrl::new(url)?,
pin: Pin::Tag(GitTag::new(tag)?),
subdir: None,
})
}
fn tag_source(tag: &str) -> Result<Source, Box<dyn std::error::Error>> {
source_at("https://github.com/acme/packs", tag)
}
fn remotes(names: &[&str]) -> Result<Vec<RemoteTag>, Box<dyn std::error::Error>> {
names
.iter()
.map(|name| {
Ok(RemoteTag {
rev: CommitId::new(NEW)?,
tag: GitTag::new(name)?,
})
})
.collect()
}
fn outcome_at(
url: &str,
current: &str,
listed: &[&str],
) -> Result<UpdateOutcome, Box<dyn std::error::Error>> {
let query = UpdateQuery::for_source(core()?, &source_at(url, current)?)
.ok_or("a git source")?
.map_err(|entry| format!("{entry:?}"))?;
Ok(query.finish(&remotes(listed)?).outcome)
}
fn g1() -> Result<Vec<RemoteTag>, Box<dyn std::error::Error>> {
[
(OLD, "packs-v0.1.0"),
(NEW, "packs-v0.10.0"),
(NEW, "packs-v0.2.0"),
(NEW, "packs-v0.9.0"),
]
.iter()
.map(|(rev, tag)| {
Ok(RemoteTag {
rev: CommitId::new(rev)?,
tag: GitTag::new(tag)?,
})
})
.collect()
}
fn query(tag: &str) -> Result<UpdateQuery, Box<dyn std::error::Error>> {
UpdateQuery::for_source(core()?, &tag_source(tag)?)
.ok_or("a git source")?
.map_err(|entry| format!("{entry:?}").into())
}
fn note_of(entry: &UpdateEntry) -> Result<String, Box<dyn std::error::Error>> {
match &entry.outcome {
UpdateOutcome::Note(note) => Ok(note.to_string()),
UpdateOutcome::NewTag(tag) => Err(format!("a new tag {tag}").into()),
}
}
#[test]
fn update_note_texts_match_the_spec() -> TestResult {
let no_version = UpdateNote::NoVersion {
key: core()?,
tag: GitTag::new("main-latest")?,
};
assert_eq!(
no_version.to_string(),
"agentstacks.toml: packs.core: tag \"main-latest\" has no version to update from"
);
let no_newer = UpdateNote::NoNewer {
key: core()?,
tag: GitTag::new("packs-v0.1.0")?,
};
assert_eq!(
no_newer.to_string(),
"agentstacks.toml: packs.core: no newer tag than packs-v0.1.0"
);
let pinned = UpdateNote::PinnedRev {
key: core()?,
rev: CommitId::new(REV)?,
};
assert_eq!(
pinned.to_string(),
format!("agentstacks.toml: packs.core: pinned to rev {REV}, not updated")
);
Ok(())
}
#[test]
fn update_skips_a_path_source() -> TestResult {
let source = Source::Path(LocalPath::new("packs/core")?);
assert_eq!(UpdateQuery::for_source(core()?, &source), None);
Ok(())
}
#[test]
fn update_notes_a_rev_pin() -> TestResult {
let source = Source::Git {
url: GitUrl::new("https://github.com/airsstack/agentstacks")?,
pin: Pin::Rev(CommitId::new(REV)?),
subdir: None,
};
let entry = UpdateQuery::for_source(core()?, &source)
.ok_or("a git source")?
.err()
.ok_or("expected a note")?;
assert_eq!(entry.current, None);
assert_eq!(
note_of(&entry)?,
format!("agentstacks.toml: packs.core: pinned to rev {REV}, not updated")
);
Ok(())
}
#[test]
fn update_notes_a_tag_without_version() -> TestResult {
let entry = UpdateQuery::for_source(core()?, &tag_source("main-latest")?)
.ok_or("a git source")?
.err()
.ok_or("expected a note")?;
assert_eq!(entry.current, Some(GitTag::new("main-latest")?));
assert_eq!(
note_of(&entry)?,
"agentstacks.toml: packs.core: tag \"main-latest\" has no version to update from"
);
Ok(())
}
#[test]
fn update_finds_a_newer_tag() -> TestResult {
let query = query("packs-v0.1.0")?;
assert_eq!(query.prefix, "packs-v");
let entry = query.finish(&g1()?);
assert_eq!(entry.current, Some(GitTag::new("packs-v0.1.0")?));
assert_eq!(
entry.outcome,
UpdateOutcome::NewTag(GitTag::new("packs-v0.10.0")?)
);
Ok(())
}
#[test]
fn update_notes_nothing_newer() -> TestResult {
let expected = "agentstacks.toml: packs.core: no newer tag than packs-v0.10.0";
let entry = query("packs-v0.10.0")?.finish(&g1()?);
assert_eq!(entry.current, Some(GitTag::new("packs-v0.10.0")?));
assert_eq!(note_of(&entry)?, expected);
let entry = query("packs-v0.10.0")?.finish(&[]);
assert_eq!(note_of(&entry)?, expected);
Ok(())
}
#[test]
fn official_packs_tag_keeps_api_major() -> TestResult {
let outcome = outcome_at(
"https://github.com/airsstack/agentstacks",
"packs-v0.2.0",
&[
"packs-v0.2.0",
"packs-v1.0.0",
"packs-v1.4.2",
"packs-v2.0.0",
],
)?;
assert_eq!(outcome, UpdateOutcome::NewTag(GitTag::new("packs-v1.4.2")?));
Ok(())
}
#[test]
fn official_source_never_moves_past_the_api_major() -> TestResult {
let outcome = outcome_at(
"https://github.com/airsstack/agentstacks",
"packs-v1.0.0",
&["packs-v1.0.0", "packs-v2.0.0"],
)?;
assert!(matches!(
outcome,
UpdateOutcome::Note(UpdateNote::NoNewer { .. })
));
Ok(())
}
#[test]
fn third_party_tags_unchanged() -> TestResult {
let outcome = outcome_at(
"https://github.com/acme/packs",
"packs-v0.2.0",
&["packs-v0.2.0", "packs-v1.0.0", "packs-v2.0.0"],
)?;
assert_eq!(outcome, UpdateOutcome::NewTag(GitTag::new("packs-v2.0.0")?));
Ok(())
}
#[test]
fn dot_git_url_is_third_party() -> TestResult {
let outcome = outcome_at(
"https://github.com/airsstack/agentstacks.git",
"packs-v0.2.0",
&["packs-v0.2.0", "packs-v1.0.0", "packs-v2.0.0"],
)?;
assert_eq!(outcome, UpdateOutcome::NewTag(GitTag::new("packs-v2.0.0")?));
Ok(())
}
#[test]
fn official_url_with_another_prefix_keeps_every_major() -> TestResult {
let outcome = outcome_at(
"https://github.com/airsstack/agentstacks",
"rel-0.2.0",
&["rel-0.2.0", "rel-2.0.0"],
)?;
assert_eq!(outcome, UpdateOutcome::NewTag(GitTag::new("rel-2.0.0")?));
Ok(())
}
#[test]
fn update_entry_for_a_tool_uses_its_key() -> TestResult {
let key = SourceKey::Tool(ToolName::new("claude")?);
let entry = UpdateQuery::for_source(key, &tag_source("main-latest")?)
.ok_or("a git source")?
.err()
.ok_or("expected a note")?;
assert!(note_of(&entry)?.starts_with("agentstacks.toml: tools.claude: "));
Ok(())
}
}