use std::path::Path;
use crate::port::{Git, Workspace};
use crate::types::project::{ProjectFile, retag_project};
use crate::types::report::{Diagnostic, Diagnostics, Location};
use crate::types::resolve::{FetchError, SourceKey, UpdateEntry, UpdateOutcome, UpdateQuery};
const PROJECT_FILE: &str = "agentstacks.toml";
pub fn update<W: Workspace, G: Git>(
workspace: &W,
git: &G,
) -> Result<Vec<UpdateEntry>, Diagnostics> {
let text = workspace
.project_text()
.map_err(|error| single(Diagnostic::new(Location::file(PROJECT_FILE), error)))?;
let project = ProjectFile::parse(Path::new(PROJECT_FILE), &text)?;
let packs = project
.packs()
.iter()
.map(|(name, entry)| (SourceKey::Pack(name.clone()), entry.source()));
let tools = project
.tools()
.iter()
.map(|(name, source)| (SourceKey::Tool(name.clone()), source));
let mut entries = Vec::new();
let mut refusals = Diagnostics::new();
let mut without_git = Vec::new();
for (key, source) in packs.chain(tools) {
let query = match UpdateQuery::for_source(key, source) {
None => continue,
Some(Err(entry)) => {
entries.push(entry);
continue;
}
Some(Ok(query)) => query,
};
match git.list_tags(&query.url, &query.prefix) {
Ok(tags) => entries.push(query.finish(&tags)),
Err(FetchError::GitNotFound { .. }) => without_git.push(query.key.to_string()),
Err(error) => refusals.push(located(&query.key, error)),
}
}
if !without_git.is_empty() {
let error = FetchError::GitNotFound {
needed_by: without_git,
};
refusals.push(Diagnostic::new(Location::file(PROJECT_FILE), error));
}
refusals.into_result(entries)
}
pub fn retag<W: Workspace>(workspace: &W, entries: &[UpdateEntry]) -> Result<bool, Diagnostics> {
let changes: Vec<_> = entries
.iter()
.filter_map(|entry| match (&entry.outcome, &entry.current) {
(UpdateOutcome::NewTag(new), Some(current)) => {
Some((entry.key.clone(), current.clone(), new.clone()))
}
_ => None,
})
.collect();
if changes.is_empty() {
return Ok(false);
}
let at_file =
|error: std::io::Error| single(Diagnostic::new(Location::file(PROJECT_FILE), error));
let text = workspace.project_text().map_err(at_file)?;
ProjectFile::parse(Path::new(PROJECT_FILE), &text)?;
let edited = retag_project(&text, &changes).map_err(single)?;
workspace.write_project(&edited).map_err(at_file)?;
Ok(true)
}
fn located(key: &SourceKey, error: FetchError) -> Diagnostic {
let location = match &error {
FetchError::CacheIo { path, .. } => Location::file(path.clone()),
_ => key.location(),
};
Diagnostic::new(location, error)
}
fn single(diagnostic: Diagnostic) -> Diagnostics {
let mut refusals = Diagnostics::new();
refusals.push(diagnostic);
refusals
}
#[cfg(test)]
mod tests {
use super::*;
use crate::port::{FakeGit, FakeWorkspace, GitCall, WorkspaceCall};
use crate::types::name::PackName;
use crate::types::project::{CommitId, GitTag, GitUrl};
use crate::types::resolve::{RemoteTag, UpdateNote, UpdateOutcome};
type TestResult = Result<(), Box<dyn std::error::Error>>;
const CORE_URL: &str = "https://github.com/airsstack/core";
const LINT_URL: &str = "https://github.com/airsstack/lint";
const TOOL_URL: &str = "https://github.com/airsstack/claude";
const REV: &str = "0123456789abcdef0123456789abcdef01234567";
fn tagged(url: &str, tag: &str) -> String {
format!("git = \"{url}\"\ntag = \"{tag}\"\n")
}
fn pinned(url: &str) -> String {
format!("git = \"{url}\"\nrev = \"{REV}\"\n")
}
fn project(packs: &[(&str, String)], tools: &[(&str, String)]) -> String {
let packs = packs
.iter()
.map(|(name, body)| format!("[packs.{name}]\n{body}"));
let tools = tools
.iter()
.map(|(name, body)| format!("[tools.{name}]\n{body}"));
std::iter::once("api = 0\n".to_owned())
.chain(packs)
.chain(tools)
.collect()
}
fn url(text: &str) -> Result<GitUrl, Box<dyn std::error::Error>> {
Ok(GitUrl::new(text)?)
}
fn remote(tag: &str) -> Result<RemoteTag, Box<dyn std::error::Error>> {
Ok(RemoteTag {
rev: CommitId::new(REV)?,
tag: GitTag::new(tag)?,
})
}
fn remotes(tags: &[&str]) -> Result<Vec<RemoteTag>, Box<dyn std::error::Error>> {
tags.iter().map(|tag| remote(tag)).collect()
}
fn core() -> Result<SourceKey, Box<dyn std::error::Error>> {
Ok(SourceKey::Pack(PackName::new("core")?))
}
fn refusals(
result: Result<Vec<UpdateEntry>, Diagnostics>,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
let diagnostics = result.err().ok_or("update succeeded")?;
Ok(diagnostics
.into_sorted()
.iter()
.map(ToString::to_string)
.collect())
}
fn entries(
result: Result<Vec<UpdateEntry>, Diagnostics>,
) -> Result<Vec<UpdateEntry>, Box<dyn std::error::Error>> {
result.map_err(|d| {
let shown: Vec<String> = d.into_sorted().iter().map(ToString::to_string).collect();
format!("update refused: {shown:?}").into()
})
}
fn note(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_reports_a_missing_project_file() -> TestResult {
let workspace = FakeWorkspace::new();
let git = FakeGit::new();
let missing = workspace.project_text().err().ok_or("a project")?;
let found = refusals(update(&workspace, &git))?;
assert_eq!(found, [format!("agentstacks.toml: {missing}")]);
assert!(git.calls().is_empty());
Ok(())
}
#[test]
fn update_reports_an_unreadable_project_file() -> TestResult {
let workspace = FakeWorkspace::new().fail(WorkspaceCall::ProjectText);
let found = refusals(update(&workspace, &FakeGit::new()))?;
assert_eq!(
found,
[format!("agentstacks.toml: {}", FakeWorkspace::denied())]
);
Ok(())
}
#[test]
fn update_passes_project_refusals_through() -> TestResult {
let workspace = FakeWorkspace::new().project("api = 0\n");
let git = FakeGit::new();
let found = refusals(update(&workspace, &git))?;
assert_eq!(found, ["agentstacks.toml: nothing to install"]);
assert!(git.calls().is_empty());
Ok(())
}
#[test]
fn update_gives_no_entry_for_a_path_source() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", "path = \"packs/core\"\n".to_owned())],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let git = FakeGit::new();
assert!(entries(update(&workspace, &git))?.is_empty());
assert!(git.calls().is_empty());
Ok(())
}
#[test]
fn update_notes_a_rev_pin() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", pinned(CORE_URL))],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let git = FakeGit::new();
let found = entries(update(&workspace, &git))?;
assert_eq!(found.len(), 1);
assert_eq!(found[0].key, core()?);
assert_eq!(
note(&found[0])?,
format!("agentstacks.toml: packs.core: pinned to rev {REV}, not updated")
);
assert!(git.calls().is_empty());
Ok(())
}
#[test]
fn update_notes_a_tag_with_no_version() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "main-latest"))],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let git = FakeGit::new();
let found = entries(update(&workspace, &git))?;
assert_eq!(found.len(), 1);
assert_eq!(
note(&found[0])?,
"agentstacks.toml: packs.core: tag \"main-latest\" has no version to update from"
);
assert!(git.calls().is_empty());
Ok(())
}
#[test]
fn update_orders_entries_by_key() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("lint", pinned(LINT_URL)), ("core", pinned(CORE_URL))],
&[("claude", pinned(TOOL_URL))],
));
let found = entries(update(&workspace, &FakeGit::new()))?;
let keys: Vec<String> = found.iter().map(|entry| entry.key.to_string()).collect();
assert_eq!(keys, ["packs.core", "packs.lint", "tools.claude"]);
Ok(())
}
#[test]
fn update_asks_git_with_the_tag_prefix() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "packs-v0.1.0"))],
&[("claude", tagged(TOOL_URL, "x-1.2.3-4.5.6"))],
));
let git = FakeGit::new()
.tags(&url(CORE_URL)?, Vec::new())
.tags(&url(TOOL_URL)?, Vec::new());
entries(update(&workspace, &git))?;
assert_eq!(
git.calls(),
[
GitCall::ListTags {
url: url(CORE_URL)?,
prefix: "packs-v".to_owned()
},
GitCall::ListTags {
url: url(TOOL_URL)?,
prefix: "x-".to_owned()
},
]
);
Ok(())
}
fn one_core(
tag: &str,
listed: &[&str],
) -> Result<Vec<UpdateEntry>, Box<dyn std::error::Error>> {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, tag))],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let git = FakeGit::new().tags(&url(CORE_URL)?, remotes(listed)?);
entries(update(&workspace, &git))
}
#[test]
fn update_offers_the_highest_released_tag() -> TestResult {
let found = one_core(
"packs-v0.1.0",
&[
"packs-v0.1.0",
"packs-v0.10.0",
"packs-v0.2.0",
"packs-v1.0.0-rc.1",
"packs-vnext",
],
)?;
assert_eq!(
found,
[UpdateEntry {
key: core()?,
current: Some(GitTag::new("packs-v0.1.0")?),
outcome: UpdateOutcome::NewTag(GitTag::new("packs-v0.10.0")?),
}]
);
Ok(())
}
#[test]
fn update_notes_no_newer_tag() -> TestResult {
let found = one_core("packs-v0.1.0", &["packs-v0.1.0"])?;
assert_eq!(found.len(), 1);
assert_eq!(
found[0].outcome,
UpdateOutcome::Note(UpdateNote::NoNewer {
key: core()?,
tag: GitTag::new("packs-v0.1.0")?,
})
);
assert_eq!(
note(&found[0])?,
"agentstacks.toml: packs.core: no newer tag than packs-v0.1.0"
);
Ok(())
}
#[test]
fn update_notes_no_newer_tag_when_nothing_matches() -> TestResult {
let found = one_core("packs-v0.1.0", &[])?;
assert_eq!(found.len(), 1);
assert_eq!(
note(&found[0])?,
"agentstacks.toml: packs.core: no newer tag than packs-v0.1.0"
);
Ok(())
}
#[test]
fn update_leaves_the_project_untouched() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "packs-v0.1.0"))],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let git = FakeGit::new().tags(&url(CORE_URL)?, remotes(&["packs-v0.2.0"])?);
let found = entries(update(&workspace, &git))?;
assert!(matches!(found[0].outcome, UpdateOutcome::NewTag(_)));
assert_eq!(workspace.calls(), [WorkspaceCall::ProjectText]);
Ok(())
}
#[test]
fn update_passes_a_git_failure_through() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "packs-v0.1.0"))],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let stderr =
"fatal: could not read Username for 'https://github.com': terminal prompts disabled";
let git = FakeGit::new().fail(
&url(CORE_URL)?,
FetchError::GitFailed {
subcommand: "ls-remote".to_owned(),
stderr: stderr.to_owned(),
},
);
assert_eq!(
refusals(update(&workspace, &git))?,
[format!(
"agentstacks.toml: packs.core: git ls-remote failed: {stderr}"
)]
);
Ok(())
}
#[test]
fn update_passes_a_git_spawn_failure_and_unreadable_output_through() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[
("core", tagged(CORE_URL, "packs-v0.1.0")),
("lint", tagged(LINT_URL, "packs-v0.1.0")),
],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let git = FakeGit::new()
.fail(
&url(CORE_URL)?,
FetchError::GitSpawn(FakeWorkspace::denied()),
)
.fail(
&url(LINT_URL)?,
FetchError::GitUnreadable {
subcommand: "ls-remote".to_owned(),
first_line: "garbage".to_owned(),
},
);
assert_eq!(
refusals(update(&workspace, &git))?,
[
format!("agentstacks.toml: packs.core: git: {}", FakeWorkspace::denied()),
"agentstacks.toml: packs.lint: git ls-remote printed output agentstacks cannot read: \"garbage\"".to_owned(),
]
);
Ok(())
}
#[test]
fn update_reports_missing_git_once() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "packs-v0.1.0"))],
&[("claude", tagged(TOOL_URL, "x-1.2.3"))],
));
let git = FakeGit::new().git_missing();
assert_eq!(
refusals(update(&workspace, &git))?,
["agentstacks.toml: git not found on PATH (needed by packs.core, tools.claude)"]
);
Ok(())
}
#[test]
fn update_reports_missing_git_only_for_sources_that_asked() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "packs-v0.1.0"))],
&[("claude", pinned(TOOL_URL))],
));
let git = FakeGit::new().git_missing();
assert_eq!(
refusals(update(&workspace, &git))?,
["agentstacks.toml: git not found on PATH (needed by packs.core)"]
);
Ok(())
}
#[test]
fn update_collects_every_git_failure() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[
("core", tagged(CORE_URL, "packs-v0.1.0")),
("lint", tagged(LINT_URL, "packs-v0.1.0")),
],
&[("claude", "path = \"extensions/claude\"\n".to_owned())],
));
let failed = |stderr: &str| FetchError::GitFailed {
subcommand: "ls-remote".to_owned(),
stderr: stderr.to_owned(),
};
let git = FakeGit::new()
.fail(&url(CORE_URL)?, failed("first"))
.fail(&url(LINT_URL)?, failed("second"));
assert_eq!(
refusals(update(&workspace, &git))?,
[
"agentstacks.toml: packs.core: git ls-remote failed: first",
"agentstacks.toml: packs.lint: git ls-remote failed: second",
]
);
Ok(())
}
#[test]
fn update_names_a_tool_in_its_refusal() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", pinned(CORE_URL))],
&[("claude", tagged(TOOL_URL, "x-1.2.3"))],
));
let git = FakeGit::new().fail(
&url(TOOL_URL)?,
FetchError::GitFailed {
subcommand: "ls-remote".to_owned(),
stderr: "no".to_owned(),
},
);
assert_eq!(
refusals(update(&workspace, &git))?,
["agentstacks.toml: tools.claude: git ls-remote failed: no"]
);
Ok(())
}
fn newer(
key: SourceKey,
current: &str,
new: &str,
) -> Result<UpdateEntry, Box<dyn std::error::Error>> {
Ok(UpdateEntry {
key,
current: Some(GitTag::new(current)?),
outcome: UpdateOutcome::NewTag(GitTag::new(new)?),
})
}
fn claude_path() -> (&'static str, String) {
("claude", "path = \"extensions/claude\"\n".to_owned())
}
fn lint() -> Result<SourceKey, Box<dyn std::error::Error>> {
Ok(SourceKey::Pack(PackName::new("lint")?))
}
fn two_packs() -> String {
project(
&[
("core", tagged(CORE_URL, "packs-v0.1.0")),
("lint", tagged(LINT_URL, "packs-v0.1.0")),
],
&[claude_path()],
)
}
#[test]
fn retag_writes_every_newer_tag_once() -> TestResult {
let workspace = FakeWorkspace::new().project(&two_packs());
let entries = [
newer(core()?, "packs-v0.1.0", "packs-v0.2.0")?,
newer(lint()?, "packs-v0.1.0", "packs-v0.3.0")?,
];
assert!(retag(&workspace, &entries).map_err(|d| format!("{d:?}"))?);
let written = workspace.project_written().ok_or("a write")?;
assert_eq!(
written,
two_packs()
.replacen("packs-v0.1.0", "packs-v0.2.0", 1)
.replacen("packs-v0.1.0", "packs-v0.3.0", 1)
);
let writes = workspace
.calls()
.iter()
.filter(|call| **call == WorkspaceCall::WriteProject)
.count();
assert_eq!(writes, 1);
Ok(())
}
#[test]
fn retag_with_no_newer_tag_writes_nothing() -> TestResult {
let workspace = FakeWorkspace::new().project(&two_packs());
let no_newer = UpdateEntry {
key: core()?,
current: Some(GitTag::new("packs-v0.1.0")?),
outcome: UpdateOutcome::Note(UpdateNote::NoNewer {
key: core()?,
tag: GitTag::new("packs-v0.1.0")?,
}),
};
assert!(!retag(&workspace, &[no_newer]).map_err(|d| format!("{d:?}"))?);
assert!(!retag(&workspace, &[]).map_err(|d| format!("{d:?}"))?);
assert_eq!(workspace.project_written(), None);
assert!(workspace.calls().is_empty());
Ok(())
}
#[test]
fn retag_refuses_text_that_no_longer_parses() -> TestResult {
let workspace = FakeWorkspace::new().project("[packs.core\n");
let entries = [newer(core()?, "packs-v0.1.0", "packs-v0.2.0")?];
let shown = refused(retag(&workspace, &entries))?;
assert_eq!(shown, ["agentstacks.toml:1: unclosed table, expected `]`"]);
assert_eq!(workspace.project_written(), None);
Ok(())
}
#[test]
fn retag_refuses_a_tag_changed_at_re_read() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("core", tagged(CORE_URL, "packs-v0.1.5"))],
&[claude_path()],
));
let entries = [newer(core()?, "packs-v0.1.0", "packs-v0.2.0")?];
assert_eq!(
refused(retag(&workspace, &entries))?,
["agentstacks.toml: packs.core: tag changed during update (run update again)"]
);
assert_eq!(workspace.project_written(), None);
Ok(())
}
#[test]
fn retag_reports_a_failed_write() -> TestResult {
let workspace = FakeWorkspace::new()
.project(&two_packs())
.fail(WorkspaceCall::WriteProject);
let entries = [newer(core()?, "packs-v0.1.0", "packs-v0.2.0")?];
assert_eq!(
refused(retag(&workspace, &entries))?,
[format!("agentstacks.toml: {}", FakeWorkspace::denied())]
);
Ok(())
}
#[test]
fn retag_reports_a_failed_re_read() -> TestResult {
let workspace = FakeWorkspace::new()
.project(&two_packs())
.fail(WorkspaceCall::ProjectText);
let entries = [newer(core()?, "packs-v0.1.0", "packs-v0.2.0")?];
assert_eq!(
refused(retag(&workspace, &entries))?,
[format!("agentstacks.toml: {}", FakeWorkspace::denied())]
);
assert_eq!(workspace.project_written(), None);
Ok(())
}
#[test]
fn retag_refuses_a_key_missing_at_re_read() -> TestResult {
let workspace = FakeWorkspace::new().project(&project(
&[("lint", tagged(LINT_URL, "packs-v0.1.0"))],
&[claude_path()],
));
let entries = [newer(core()?, "packs-v0.1.0", "packs-v0.2.0")?];
assert_eq!(
refused(retag(&workspace, &entries))?,
["agentstacks.toml: packs.core: no tag to update (run update again)"]
);
assert_eq!(workspace.project_written(), None);
Ok(())
}
fn refused(
result: Result<bool, Diagnostics>,
) -> Result<Vec<String>, Box<dyn std::error::Error>> {
Ok(result
.err()
.ok_or("retag succeeded")?
.into_sorted()
.iter()
.map(ToString::to_string)
.collect())
}
}