use std::io::{Cursor, Read};
use std::path::{Path, PathBuf};
use std::sync::{Arc, OnceLock};
use anyhow::{Context, Result, bail};
use sha2::{Digest, Sha256};
use crate::pack::PackRuntime;
pub const EXTENSIONS_PREFIX: &str = "extensions/";
pub const ARCHIVE_SUFFIX: &str = ".gtxpack";
pub const OPT_OUT_ENV: &str = "GREENTIC_AW_PACK_EXTENSIONS";
const COMPLETE_MARKER: &str = ".ext-staged";
const EXTENSION_SUBDIR: &str = "ext";
#[must_use]
pub fn is_extension_archive_entry(entry: &str) -> bool {
let Some(name) = entry.strip_prefix(EXTENSIONS_PREFIX) else {
return false;
};
if name.contains('/') {
return false;
}
let Some(stem) = name.strip_suffix(ARCHIVE_SUFFIX) else {
return false;
};
!stem.is_empty()
}
#[derive(Debug, Clone)]
pub(crate) struct StagedExtension {
pub(crate) entry_name: String,
pub(crate) dir: PathBuf,
pub(crate) extension_id: Option<String>,
pub(crate) unpacked_bytes: u64,
}
#[derive(Debug, Clone)]
pub(crate) struct StagingFailure {
pub(crate) entry_name: String,
pub(crate) reason: String,
}
#[derive(Debug, Default)]
pub(crate) struct PackExtensions {
pub(crate) staged: Vec<StagedExtension>,
pub(crate) failures: Vec<StagingFailure>,
}
static STAGING_ROOT: OnceLock<Option<tempfile::TempDir>> = OnceLock::new();
fn staging_root() -> Option<&'static Path> {
STAGING_ROOT
.get_or_init(|| {
match tempfile::Builder::new()
.prefix("greentic-pack-extensions-")
.tempdir()
{
Ok(dir) => Some(dir),
Err(error) => {
tracing::warn!(
error = %error,
"cannot create a staging directory; pack-carried extensions are unavailable"
);
None
}
}
})
.as_ref()
.map(tempfile::TempDir::path)
}
const MAX_EXT_ENTRY_BYTES: u64 = 64 * 1024 * 1024;
const MAX_EXT_TOTAL_BYTES: u64 = 256 * 1024 * 1024;
const MAX_EXT_ENTRIES: usize = 4096;
#[cfg(test)]
fn unpack_archive(bytes: &[u8], dest: &Path) -> Result<u64> {
unpack_archive_within(bytes, dest, MAX_EXT_TOTAL_BYTES)
}
fn unpack_archive_within(bytes: &[u8], dest: &Path, max_total_bytes: u64) -> Result<u64> {
unpack_archive_with_limits(
bytes,
dest,
ExtractLimits {
max_entry_bytes: MAX_EXT_ENTRY_BYTES,
max_total_bytes: max_total_bytes.min(MAX_EXT_TOTAL_BYTES),
max_entries: MAX_EXT_ENTRIES,
},
)
}
#[derive(Debug, Clone, Copy)]
struct ExtractLimits {
max_entry_bytes: u64,
max_total_bytes: u64,
max_entries: usize,
}
fn unpack_archive_with_limits(bytes: &[u8], dest: &Path, limits: ExtractLimits) -> Result<u64> {
let mut archive =
zip::ZipArchive::new(Cursor::new(bytes)).context("read the extension archive")?;
if archive.len() > limits.max_entries {
bail!(
"extension archive has too many entries ({}, limit {})",
archive.len(),
limits.max_entries
);
}
let mut total_bytes = 0u64;
for index in 0..archive.len() {
let mut entry = archive.by_index(index)?;
let raw_name = entry.name().to_string();
let Some(relative) = entry.enclosed_name() else {
bail!("archive entry escapes the staging directory: {raw_name}");
};
let out_path = dest.join(&relative);
if entry.is_dir() {
std::fs::create_dir_all(&out_path)?;
continue;
}
if entry.size() > limits.max_entry_bytes {
bail!(
"extension archive entry `{raw_name}` is too large ({} bytes, limit {})",
entry.size(),
limits.max_entry_bytes
);
}
if let Some(parent) = out_path.parent() {
std::fs::create_dir_all(parent)?;
}
let mut outfile = std::fs::File::create(&out_path)
.with_context(|| format!("create {}", out_path.display()))?;
let mut limited = (&mut entry).take(limits.max_entry_bytes.saturating_add(1));
let copied = std::io::copy(&mut limited, &mut outfile)
.with_context(|| format!("write {}", out_path.display()))?;
if copied > limits.max_entry_bytes {
bail!(
"extension archive entry `{raw_name}` exceeds the size limit ({})",
limits.max_entry_bytes
);
}
total_bytes = total_bytes.saturating_add(copied);
if total_bytes > limits.max_total_bytes {
bail!(
"extension archive exceeds the total size limit ({} bytes)",
limits.max_total_bytes
);
}
}
Ok(total_bytes)
}
fn staged_extension_id(dir: &Path) -> Option<String> {
let bytes = std::fs::read(dir.join("describe.json")).ok()?;
let value: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
value
.get("metadata")?
.get("id")?
.as_str()
.map(str::to_string)
}
fn stage_one(entry_name: &str, bytes: &[u8], unpack_budget: u64) -> Result<StagedExtension> {
let root = staging_root().context("no staging directory is available")?;
stage_one_budgeted(root, entry_name, bytes, unpack_budget)
}
#[cfg(test)]
fn stage_one_in(root: &Path, entry_name: &str, bytes: &[u8]) -> Result<StagedExtension> {
stage_one_budgeted(root, entry_name, bytes, MAX_EXT_TOTAL_BYTES)
}
fn stage_one_budgeted(
root: &Path,
entry_name: &str,
bytes: &[u8],
unpack_budget: u64,
) -> Result<StagedExtension> {
static STAGING_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
let _guard = STAGING_LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let digest = hex::encode(Sha256::digest(bytes));
let slot = root.join(&digest[..32]);
let dir = slot.join(EXTENSION_SUBDIR);
let marker = slot.join(COMPLETE_MARKER);
if !marker.exists() {
if slot.exists() {
std::fs::remove_dir_all(&slot)
.with_context(|| format!("clear partial staging at {}", slot.display()))?;
}
std::fs::create_dir_all(&dir)
.with_context(|| format!("create staging dir {}", dir.display()))?;
let unpacked = match unpack_archive_within(bytes, &dir, unpack_budget) {
Ok(unpacked) => unpacked,
Err(error) => {
if let Err(clean) = std::fs::remove_dir_all(&slot) {
tracing::debug!(error = %clean, "cannot remove refused staging slot");
}
return Err(error);
}
};
std::fs::write(&marker, unpacked.to_string())
.with_context(|| format!("mark {} staged", dir.display()))?;
}
let unpacked_bytes = recorded_unpacked_bytes(&slot, &dir);
if unpacked_bytes > unpack_budget {
bail!(
"extension archive unpacks to {unpacked_bytes} bytes, over the {unpack_budget} bytes left for this pass"
);
}
let extension_id = staged_extension_id(&dir);
record_slot_use(&slot, extension_id.as_deref());
sweep_stale_slots(root, &slot, SUPERSEDED_GRACE);
Ok(StagedExtension {
entry_name: entry_name.to_string(),
dir,
extension_id,
unpacked_bytes,
})
}
fn recorded_unpacked_bytes(slot: &Path, dir: &Path) -> u64 {
std::fs::read_to_string(slot.join(COMPLETE_MARKER))
.ok()
.and_then(|text| text.trim().parse::<u64>().ok())
.unwrap_or_else(|| dir_size(dir))
}
fn dir_size(dir: &Path) -> u64 {
let Ok(read_dir) = std::fs::read_dir(dir) else {
return 0;
};
read_dir
.flatten()
.map(|entry| match entry.metadata() {
Ok(meta) if meta.is_dir() => dir_size(&entry.path()),
Ok(meta) => meta.len(),
Err(_) => 0,
})
.fold(0u64, u64::saturating_add)
}
const SLOT_ID_FILE: &str = ".ext-id";
const SUPERSEDED_GRACE: std::time::Duration = std::time::Duration::from_secs(60 * 60);
fn record_slot_use(slot: &Path, extension_id: Option<&str>) {
if let Some(id) = extension_id
&& let Err(error) = std::fs::write(slot.join(SLOT_ID_FILE), id)
{
tracing::debug!(error = %error, "cannot record extension id for staging slot");
}
let touched = std::fs::File::options()
.write(true)
.open(slot.join(COMPLETE_MARKER))
.and_then(|file| file.set_modified(std::time::SystemTime::now()));
if let Err(error) = touched {
tracing::debug!(error = %error, "cannot refresh staging slot use time");
}
}
fn sweep_stale_slots(root: &Path, keep: &Path, grace: std::time::Duration) {
let keep_id = std::fs::read_to_string(keep.join(SLOT_ID_FILE)).ok();
let Ok(read_dir) = std::fs::read_dir(root) else {
return;
};
let now = std::time::SystemTime::now();
for candidate in read_dir.flatten() {
let path = candidate.path();
if path == keep || !path.is_dir() {
continue;
}
let last_used = match std::fs::metadata(path.join(COMPLETE_MARKER)) {
Ok(meta) => {
let same_extension = keep_id.is_some()
&& std::fs::read_to_string(path.join(SLOT_ID_FILE)).ok() == keep_id;
if !same_extension {
continue;
}
meta.modified().ok()
}
Err(_) => std::fs::metadata(&path).and_then(|m| m.modified()).ok(),
};
let aged = last_used
.and_then(|t| now.duration_since(t).ok())
.is_some_and(|age| age >= grace);
if aged && let Err(error) = std::fs::remove_dir_all(&path) {
tracing::debug!(
error = %error,
slot = %path.display(),
"cannot remove stale extension staging slot"
);
}
}
}
pub(crate) fn stage_from_packs(packs: &[Arc<PackRuntime>]) -> PackExtensions {
let mut seen = std::collections::HashSet::new();
let mut candidates: Vec<(String, &Arc<PackRuntime>)> = Vec::new();
for pack in packs {
for entry_name in pack.extension_archive_entries() {
if seen.insert(entry_name.clone()) {
candidates.push((entry_name, pack));
}
}
}
stage_bounded(
candidates,
|name, pack, limit| pack.read_pack_file_capped(name, limit),
MAX_PACK_EXT_ENTRIES,
MAX_PACK_EXT_TOTAL_BYTES,
MAX_PACK_EXT_UNPACKED_BYTES,
)
}
const MAX_PACK_EXT_ENTRIES: usize = 64;
const MAX_PACK_EXT_TOTAL_BYTES: u64 = 512 * 1024 * 1024;
const MAX_PACK_EXT_UNPACKED_BYTES: u64 = 1024 * 1024 * 1024;
fn stage_bounded<T>(
candidates: Vec<(String, T)>,
read: impl Fn(&str, &T, u64) -> Option<Vec<u8>>,
max_entries: usize,
max_total_bytes: u64,
max_unpacked_bytes: u64,
) -> PackExtensions {
let mut out = PackExtensions::default();
let mut total: u64 = 0;
let mut unpacked_total: u64 = 0;
for (index, (entry_name, source)) in candidates.into_iter().enumerate() {
if index >= max_entries {
out.failures.push(StagingFailure {
entry_name,
reason: format!("more than {max_entries} extension archives in the loaded packs"),
});
continue;
}
let remaining = max_total_bytes.saturating_sub(total);
let Some(bytes) = read(&entry_name, &source, remaining) else {
out.failures.push(StagingFailure {
entry_name,
reason: format!(
"the pack lists this entry but it could not be read within the {remaining} bytes left for this pass"
),
});
continue;
};
let len = bytes.len() as u64;
if total.saturating_add(len) > max_total_bytes {
out.failures.push(StagingFailure {
entry_name,
reason: format!(
"extension archives from the loaded packs exceed {max_total_bytes} bytes in total"
),
});
continue;
}
total += len;
let unpack_budget = max_unpacked_bytes.saturating_sub(unpacked_total);
match stage_one(&entry_name, &bytes, unpack_budget) {
Ok(staged) => {
unpacked_total = unpacked_total.saturating_add(staged.unpacked_bytes);
out.staged.push(staged);
}
Err(error) => out.failures.push(StagingFailure {
entry_name,
reason: format!("{error:#}"),
}),
}
}
out
}
#[cfg(feature = "agentic-worker")]
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct PackExtensionLoad {
pub(crate) loaded: usize,
pub(crate) shadowed: usize,
pub(crate) failed: usize,
}
#[cfg(feature = "agentic-worker")]
fn is_shadowed(staged: &StagedExtension, on_disk: &std::collections::HashSet<String>) -> bool {
staged
.extension_id
.as_deref()
.is_some_and(|id| on_disk.contains(id))
}
#[cfg(feature = "agentic-worker")]
pub(crate) fn register_from_packs(
runtime: &mut greentic_ext_runtime::ExtensionRuntime,
packs: &[Arc<PackRuntime>],
) -> PackExtensionLoad {
let mut report = PackExtensionLoad::default();
if packs.is_empty() {
return report;
}
if std::env::var(OPT_OUT_ENV).ok().as_deref() == Some("0") {
tracing::info!("{OPT_OUT_ENV}=0; pack-carried design extensions disabled");
return report;
}
let PackExtensions { staged, failures } = stage_from_packs(packs);
for failure in &failures {
report.failed += 1;
tracing::warn!(
entry = %failure.entry_name,
reason = %failure.reason,
"pack-carried extension could not be staged; its tools will not resolve"
);
}
let on_disk: std::collections::HashSet<String> = runtime
.loaded()
.keys()
.map(|id| id.as_str().to_string())
.collect();
for extension in staged {
if is_shadowed(&extension, &on_disk) {
report.shadowed += 1;
tracing::info!(
entry = %extension.entry_name,
extension_id = extension.extension_id.as_deref().unwrap_or_default(),
"pack-carried extension is already installed on disk; keeping the installed copy"
);
continue;
}
match runtime.register_loaded_from_dir(&extension.dir) {
Ok(()) => {
report.loaded += 1;
tracing::info!(
entry = %extension.entry_name,
extension_id = extension.extension_id.as_deref().unwrap_or_default(),
"loaded design extension from the pack"
);
}
Err(error) => {
report.failed += 1;
tracing::warn!(
entry = %extension.entry_name,
extension_id = extension.extension_id.as_deref().unwrap_or_default(),
error = %error,
"pack-carried extension failed to load; its tools will not resolve"
);
}
}
}
report
}
#[cfg(test)]
mod tests {
use std::io::Write;
use super::*;
fn zip_bytes(entries: &[(&str, &[u8])]) -> Vec<u8> {
let mut writer = zip::ZipWriter::new(Cursor::new(Vec::new()));
let options: zip::write::FileOptions<'_, ()> =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
for (name, bytes) in entries {
writer.start_file(*name, options).expect("start entry");
writer.write_all(bytes).expect("write entry");
}
writer.finish().expect("finish zip").into_inner()
}
fn stage_archives<I>(entries: I) -> PackExtensions
where
I: IntoIterator<Item = (String, Vec<u8>)>,
{
stage_bounded(
entries.into_iter().collect(),
|_, bytes, limit| (bytes.len() as u64 <= limit).then(|| bytes.clone()),
usize::MAX,
u64::MAX,
u64::MAX,
)
}
fn describe_bytes(id: &str) -> Vec<u8> {
serde_json::to_vec(&serde_json::json!({
"apiVersion": "v2",
"kind": "design",
"metadata": { "id": id, "version": "0.1.0" }
}))
.expect("serialise describe")
}
fn extension_archive(id: &str) -> Vec<u8> {
zip_bytes(&[
("describe.json", describe_bytes(id).as_slice()),
("extension.wasm", b"not-a-real-component".as_slice()),
])
}
#[test]
fn only_gtxpack_entries_at_one_flat_level_are_extensions() {
assert!(is_extension_archive_entry("extensions/acme.tool.gtxpack"));
assert!(!is_extension_archive_entry(
"extensions/wizard-answers.json"
));
assert!(!is_extension_archive_entry(
"extensions/design/acme.gtxpack"
));
assert!(!is_extension_archive_entry("extensions/.gtxpack"));
assert!(!is_extension_archive_entry("assets/acme.gtxpack"));
assert!(!is_extension_archive_entry("extensions/acme.gtxpack.bak"));
}
#[test]
fn a_staged_archive_is_unpacked_whole_and_carries_its_extension_id() {
let staged = stage_archives([(
"extensions/acme.tool.gtxpack".to_string(),
extension_archive("acme.tool"),
)]);
assert!(staged.failures.is_empty(), "{:?}", staged.failures);
assert_eq!(staged.staged.len(), 1);
let extension = &staged.staged[0];
assert_eq!(extension.extension_id.as_deref(), Some("acme.tool"));
assert_eq!(
std::fs::read(extension.dir.join("describe.json")).expect("staged describe"),
describe_bytes("acme.tool"),
"the staged describe must be the archive's own bytes"
);
assert_eq!(
std::fs::read(extension.dir.join("extension.wasm")).expect("staged wasm"),
b"not-a-real-component",
);
}
#[test]
fn a_corrupt_archive_is_reported_and_a_sibling_still_stages() {
let staged = stage_archives([
(
"extensions/broken.gtxpack".to_string(),
b"this is not a zip".to_vec(),
),
(
"extensions/good.gtxpack".to_string(),
extension_archive("acme.good"),
),
]);
assert_eq!(staged.failures.len(), 1);
assert_eq!(staged.failures[0].entry_name, "extensions/broken.gtxpack");
assert!(
!staged.failures[0].reason.is_empty(),
"a refused archive must say why"
);
assert_eq!(
staged
.staged
.iter()
.map(|e| e.entry_name.as_str())
.collect::<Vec<_>>(),
vec!["extensions/good.gtxpack"],
"one broken archive must not cost the operator the others"
);
}
#[test]
fn an_archive_entry_that_escapes_the_staging_dir_is_refused() {
let hostile = zip_bytes(&[("../escaped.txt", b"pwned".as_slice())]);
let staged = stage_archives([("extensions/hostile.gtxpack".to_string(), hostile)]);
assert!(
staged.staged.is_empty(),
"a traversing archive must not be presented to the loader"
);
assert_eq!(staged.failures.len(), 1);
assert!(
staged.failures[0].reason.contains("escapes"),
"reason was {:?}",
staged.failures[0].reason
);
let root = staging_root().expect("staging root");
assert!(
!root.join("escaped.txt").exists() && !root.join("../escaped.txt").exists(),
"nothing may be written outside the staged directory"
);
}
#[test]
fn a_describe_without_an_id_still_stages_for_the_loader_to_refuse() {
let archive = zip_bytes(&[("describe.json", b"{}".as_slice())]);
let staged = stage_archives([("extensions/anonymous.gtxpack".to_string(), archive)]);
assert!(staged.failures.is_empty());
assert_eq!(staged.staged.len(), 1);
assert_eq!(staged.staged[0].extension_id, None);
}
#[test]
fn a_pack_carried_extension_is_staged_when_the_extensions_directory_is_empty() {
use std::io::Write;
let empty_dir = tempfile::tempdir().expect("an empty extensions directory");
assert_eq!(
std::fs::read_dir(empty_dir.path())
.expect("read the empty dir")
.count(),
0,
"the on-disk source must be empty for this test to mean anything"
);
let holder = tempfile::tempdir().expect("tempdir");
let pack_path = holder.path().join("worker.gtpack");
let mut writer =
zip::ZipWriter::new(std::fs::File::create(&pack_path).expect("create pack"));
let options: zip::write::FileOptions<'_, ()> =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
writer
.start_file("extensions/acme.tool.gtxpack", options)
.expect("start entry");
writer
.write_all(&extension_archive("acme.tool"))
.expect("write entry");
writer
.start_file("extensions/wizard-answers.json", options)
.expect("start sidecar");
writer.write_all(b"{}").expect("write sidecar");
writer.finish().expect("finish pack");
let pack = Arc::new(crate::pack::tests::pack_runtime_for_dir(&pack_path));
let staged = stage_from_packs(&[pack]);
assert!(staged.failures.is_empty(), "{:?}", staged.failures);
assert_eq!(
staged.staged.len(),
1,
"the `.json` sidecar must not be staged as an extension"
);
assert_eq!(staged.staged[0].entry_name, "extensions/acme.tool.gtxpack");
assert_eq!(
staged.staged[0].extension_id.as_deref(),
Some("acme.tool"),
"a staged extension must be identifiable, or precedence cannot be decided"
);
assert!(staged.staged[0].dir.join("extension.wasm").is_file());
}
#[test]
fn staging_caps_the_entry_count_and_never_reads_past_it() {
use std::cell::Cell;
let reads = Cell::new(0usize);
let candidates: Vec<(String, String)> = (0..5)
.map(|n| (format!("extensions/e{n}.gtxpack"), format!("acme.e{n}")))
.collect();
let out = stage_bounded(
candidates,
|_, id, _| {
reads.set(reads.get() + 1);
Some(extension_archive(id))
},
3,
u64::MAX,
u64::MAX,
);
assert_eq!(out.staged.len(), 3);
assert_eq!(out.failures.len(), 2, "over-cap entries are reported");
assert_eq!(reads.get(), 3, "over-cap entries are never read");
}
#[test]
fn staging_stops_at_the_aggregate_byte_budget() {
let archive = extension_archive("acme.big");
let one = archive.len() as u64;
let candidates: Vec<(String, ())> = (0..3)
.map(|n| (format!("extensions/b{n}.gtxpack"), ()))
.collect();
let out = stage_bounded(
candidates,
|_, (), _| Some(archive.clone()),
10,
one * 2,
u64::MAX,
);
assert_eq!(out.staged.len(), 2);
assert_eq!(out.failures.len(), 1);
assert!(out.failures[0].reason.contains("in total"));
}
#[test]
fn the_reader_is_given_the_remaining_budget_not_the_whole_one() {
use std::cell::RefCell;
let archive = extension_archive("acme.budget");
let one = archive.len() as u64;
let seen = RefCell::new(Vec::new());
let candidates: Vec<(String, ())> = (0..3)
.map(|n| (format!("extensions/r{n}.gtxpack"), ()))
.collect();
let out = stage_bounded(
candidates,
|_, (), limit| {
seen.borrow_mut().push(limit);
(archive.len() as u64 <= limit).then(|| archive.clone())
},
10,
one * 2 + 1,
u64::MAX,
);
assert_eq!(*seen.borrow(), vec![one * 2 + 1, one + 1, 1]);
assert_eq!(out.staged.len(), 2);
assert_eq!(out.failures.len(), 1, "the over-budget read is refused");
}
#[test]
fn an_over_budget_pack_entry_is_refused_while_it_is_read() {
let dir = tempfile::tempdir().expect("tmp");
let extensions = dir.path().join("extensions");
std::fs::create_dir_all(&extensions).expect("mkdir");
std::fs::write(extensions.join("big.gtxpack"), vec![0u8; 4096]).expect("write");
let pack = crate::pack::tests::pack_runtime_for_dir(dir.path());
assert_eq!(
pack.read_pack_file_capped("extensions/big.gtxpack", 4096)
.map(|bytes| bytes.len()),
Some(4096)
);
assert_eq!(
pack.read_pack_file_capped("extensions/big.gtxpack", 4095),
None,
"one byte over the limit must not be returned"
);
}
#[test]
fn an_unreadable_listed_entry_is_a_failure_not_a_gap() {
let out = stage_bounded(
vec![("extensions/gone.gtxpack".to_string(), ())],
|_, (), _| None,
10,
u64::MAX,
u64::MAX,
);
assert!(out.staged.is_empty());
assert_eq!(out.failures.len(), 1);
}
#[cfg(feature = "agentic-worker")]
#[test]
fn an_unverifiable_pack_carried_extension_is_refused_and_counted() {
use std::io::Write;
use greentic_ext_runtime::{DiscoveryPaths, ExtensionRuntime, RuntimeConfig};
let empty_root = tempfile::tempdir().expect("an empty discovery root");
let config =
RuntimeConfig::from_paths(DiscoveryPaths::new(empty_root.path().to_path_buf()));
let mut runtime = ExtensionRuntime::new(config).expect("build an extension runtime");
assert!(runtime.loaded().is_empty(), "nothing is installed on disk");
let holder = tempfile::tempdir().expect("tempdir");
let pack_path = holder.path().join("worker.gtpack");
let mut writer =
zip::ZipWriter::new(std::fs::File::create(&pack_path).expect("create pack"));
let options: zip::write::FileOptions<'_, ()> =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
writer
.start_file("extensions/acme.tool.gtxpack", options)
.expect("start entry");
writer
.write_all(&extension_archive("acme.tool"))
.expect("write entry");
writer.finish().expect("finish pack");
let pack = Arc::new(crate::pack::tests::pack_runtime_for_dir(&pack_path));
let report = register_from_packs(&mut runtime, &[pack]);
assert_eq!(
report,
PackExtensionLoad {
loaded: 0,
shadowed: 0,
failed: 1,
},
"an unsigned, unverifiable archive must be refused and reported"
);
assert!(
runtime.loaded().is_empty(),
"a refused archive must not reach the runtime"
);
}
#[cfg(feature = "agentic-worker")]
#[test]
fn an_extension_already_loaded_from_disk_is_not_replaced_by_the_pack() {
let staged = StagedExtension {
entry_name: "extensions/acme.tool.gtxpack".to_string(),
dir: PathBuf::from("/nonexistent"),
extension_id: Some("acme.tool".to_string()),
unpacked_bytes: 0,
};
let on_disk: std::collections::HashSet<String> =
["acme.tool".to_string()].into_iter().collect();
assert!(is_shadowed(&staged, &on_disk));
let other: std::collections::HashSet<String> =
["acme.other".to_string()].into_iter().collect();
assert!(!is_shadowed(&staged, &other));
let anonymous = StagedExtension {
extension_id: None,
..staged
};
assert!(
!is_shadowed(&anonymous, &on_disk),
"an unreadable id must reach the loader, not be assumed shadowed"
);
}
fn files_under(dir: &Path) -> Vec<String> {
fn walk(base: &Path, current: &Path, out: &mut Vec<String>) {
for entry in std::fs::read_dir(current).expect("read staged dir") {
let path = entry.expect("dir entry").path();
if path.is_dir() {
walk(base, &path, out);
} else {
let rel = path.strip_prefix(base).expect("under base");
out.push(rel.to_string_lossy().replace('\\', "/"));
}
}
}
let mut out = Vec::new();
walk(dir, dir, &mut out);
out.sort();
out
}
#[test]
fn the_loader_directory_holds_only_the_archives_files_and_the_marker_sits_beside_it() {
let root = tempfile::tempdir().expect("staging root");
let archive = zip_bytes(&[
("describe.json", describe_bytes("acme.tool").as_slice()),
("manifest.json", b"{}".as_slice()),
("extension.wasm", b"wasm".as_slice()),
("assets/icon.svg", b"<svg/>".as_slice()),
]);
let staged =
stage_one_in(root.path(), "extensions/acme.tool.gtxpack", &archive).expect("stage");
assert_eq!(
files_under(&staged.dir),
vec![
"assets/icon.svg",
"describe.json",
"extension.wasm",
"manifest.json",
],
"nothing but the archive's own entries may appear in the loader's directory"
);
let slot = staged.dir.parent().expect("slot");
assert_eq!(slot.parent(), Some(root.path()));
assert!(
slot.join(COMPLETE_MARKER).is_file(),
"the completion marker must live beside the extension directory"
);
assert!(!staged.dir.join(COMPLETE_MARKER).exists());
assert!(!staged.dir.join(".staged").exists());
}
#[test]
fn a_completed_stage_is_reused_and_not_unpacked_again() {
let root = tempfile::tempdir().expect("staging root");
let archive = extension_archive("acme.tool");
let first = stage_one_in(root.path(), "extensions/a.gtxpack", &archive).expect("first");
let sentinel = first.dir.parent().expect("slot").join("sentinel");
std::fs::write(&sentinel, b"kept").expect("write sentinel");
let second = stage_one_in(root.path(), "extensions/a.gtxpack", &archive).expect("second");
assert_eq!(first.dir, second.dir);
assert!(sentinel.exists(), "a completed stage must be reused as-is");
assert_eq!(second.extension_id.as_deref(), Some("acme.tool"));
}
fn small_limits() -> ExtractLimits {
ExtractLimits {
max_entry_bytes: 1024,
max_total_bytes: 3 * 1024,
max_entries: 4,
}
}
fn lie_about_size(mut bytes: Vec<u8>, declared: u32) -> Vec<u8> {
let patch = |bytes: &mut Vec<u8>, sig: &[u8; 4], offset: usize| {
let mut at = 0;
while at + 4 <= bytes.len() {
if &bytes[at..at + 4] == sig && at + offset + 4 <= bytes.len() {
bytes[at + offset..at + offset + 4].copy_from_slice(&declared.to_le_bytes());
at += 4;
} else {
at += 1;
}
}
};
patch(&mut bytes, b"PK\x01\x02", 24);
bytes
}
#[test]
fn a_normal_archive_unpacks_under_the_limits() {
let dir = tempfile::tempdir().expect("dest");
let archive = extension_archive("acme.tool");
unpack_archive_with_limits(&archive, dir.path(), small_limits()).expect("unpack");
assert!(dir.path().join("describe.json").is_file());
assert!(dir.path().join("extension.wasm").is_file());
let dir = tempfile::tempdir().expect("dest");
unpack_archive(&archive, dir.path()).expect("unpack with default limits");
}
#[test]
fn an_entry_declared_over_the_cap_is_refused() {
let dir = tempfile::tempdir().expect("dest");
let payload = vec![0u8; 2048];
let archive = zip_bytes(&[("big.bin", payload.as_slice())]);
let err = unpack_archive_with_limits(&archive, dir.path(), small_limits())
.expect_err("oversized entry must be refused");
assert!(err.to_string().contains("too large"), "{err}");
}
#[test]
fn an_entry_that_lies_about_its_size_is_stopped_by_the_streaming_cap() {
let dir = tempfile::tempdir().expect("dest");
let payload = vec![0u8; 4096];
let archive = lie_about_size(zip_bytes(&[("big.bin", payload.as_slice())]), 10);
let err = unpack_archive_with_limits(&archive, dir.path(), small_limits())
.expect_err("a lying declared size must not bypass the cap");
let written = std::fs::metadata(dir.path().join("big.bin"))
.map(|m| m.len())
.unwrap_or(0);
assert!(written <= small_limits().max_entry_bytes + 1, "{err}");
}
#[test]
fn the_total_size_cap_is_enforced_across_entries() {
let dir = tempfile::tempdir().expect("dest");
let chunk = vec![1u8; 1000];
let archive = zip_bytes(&[
("a.bin", chunk.as_slice()),
("b.bin", chunk.as_slice()),
("c.bin", chunk.as_slice()),
("d.bin", chunk.as_slice()),
]);
let limits = ExtractLimits {
max_entry_bytes: 1024,
max_total_bytes: 2500,
max_entries: 10,
};
let err = unpack_archive_with_limits(&archive, dir.path(), limits)
.expect_err("total cap must trip");
assert!(err.to_string().contains("total size"), "{err}");
}
#[test]
fn the_entry_count_cap_is_enforced() {
let dir = tempfile::tempdir().expect("dest");
let names: Vec<String> = (0..5).map(|i| format!("f{i}.txt")).collect();
let entries: Vec<(&str, &[u8])> = names.iter().map(|n| (n.as_str(), &b"x"[..])).collect();
let archive = zip_bytes(&entries);
let err = unpack_archive_with_limits(&archive, dir.path(), small_limits())
.expect_err("too many entries must be refused");
assert!(err.to_string().contains("too many entries"), "{err}");
}
fn age_slot(slot: &Path, age: std::time::Duration) {
let past = std::time::SystemTime::now() - age;
let marker = std::fs::File::options()
.write(true)
.open(slot.join(COMPLETE_MARKER))
.expect("open marker");
marker.set_modified(past).expect("age marker");
}
#[test]
fn a_superseded_digest_is_swept_after_the_grace_but_not_before() {
let root = tempfile::tempdir().expect("staging root");
let old = stage_one_in(
root.path(),
"extensions/a.gtxpack",
&zip_bytes(&[
("describe.json", describe_bytes("acme.tool").as_slice()),
("v", b"1".as_slice()),
]),
)
.expect("old");
let old_slot = old.dir.parent().expect("slot").to_path_buf();
let other = stage_one_in(
root.path(),
"extensions/b.gtxpack",
&extension_archive("other.tool"),
)
.expect("other");
let other_slot = other.dir.parent().expect("slot").to_path_buf();
let new = stage_one_in(
root.path(),
"extensions/a.gtxpack",
&zip_bytes(&[
("describe.json", describe_bytes("acme.tool").as_slice()),
("v", b"2".as_slice()),
]),
)
.expect("new");
let new_slot = new.dir.parent().expect("slot").to_path_buf();
assert!(old_slot.exists(), "inside the grace the old slot stays");
age_slot(
&old_slot,
SUPERSEDED_GRACE + std::time::Duration::from_secs(60),
);
age_slot(
&other_slot,
SUPERSEDED_GRACE + std::time::Duration::from_secs(60),
);
sweep_stale_slots(root.path(), &new_slot, SUPERSEDED_GRACE);
assert!(!old_slot.exists(), "an aged superseded slot is removed");
assert!(new_slot.exists(), "the current slot is kept");
assert!(
other_slot.exists(),
"another extension's slot is never evicted"
);
}
#[test]
fn a_complete_slot_of_another_extension_idle_past_the_grace_is_not_swept() {
let root = tempfile::tempdir().expect("staging root");
let other = stage_one_in(
root.path(),
"extensions/b.gtxpack",
&extension_archive("other.tool"),
)
.expect("other");
let other_slot = other.dir.parent().expect("slot").to_path_buf();
let anonymous = stage_one_in(
root.path(),
"extensions/c.gtxpack",
&zip_bytes(&[("extension.wasm", b"no describe".as_slice())]),
)
.expect("anonymous");
let anonymous_slot = anonymous.dir.parent().expect("slot").to_path_buf();
let current = stage_one_in(
root.path(),
"extensions/a.gtxpack",
&extension_archive("acme.tool"),
)
.expect("current");
let current_slot = current.dir.parent().expect("slot").to_path_buf();
for slot in [&other_slot, &anonymous_slot] {
age_slot(slot, SUPERSEDED_GRACE + std::time::Duration::from_secs(60));
}
sweep_stale_slots(root.path(), ¤t_slot, SUPERSEDED_GRACE);
assert!(
other_slot.exists() && anonymous_slot.exists(),
"a runtime that is not rebuilt still points into these complete slots"
);
assert!(current_slot.exists());
}
#[test]
fn the_unpacked_byte_budget_bounds_a_pass_and_cleans_up() {
let big = zip_bytes(&[
("describe.json", describe_bytes("acme.big").as_slice()),
("blob.bin", vec![7u8; 4000].as_slice()),
]);
let small = extension_archive("acme.small");
let candidates = vec![
("extensions/big.gtxpack".to_string(), big.clone()),
("extensions/small.gtxpack".to_string(), small.clone()),
];
let out = stage_bounded(
candidates,
|_, bytes, _| Some(bytes.clone()),
10,
u64::MAX,
1000,
);
assert_eq!(out.staged.len(), 1, "{:?}", out.failures);
assert_eq!(out.staged[0].entry_name, "extensions/small.gtxpack");
assert_eq!(out.failures.len(), 1);
assert_eq!(out.failures[0].entry_name, "extensions/big.gtxpack");
assert!(
out.failures[0].reason.contains("total size limit"),
"{:?}",
out.failures
);
}
#[test]
fn the_unpacked_budget_is_spent_across_entries() {
let a = extension_archive("acme.a");
let b = extension_archive("acme.b");
let one = out_unpacked(&a);
let out = stage_bounded(
vec![
("extensions/a.gtxpack".to_string(), a),
("extensions/b.gtxpack".to_string(), b),
],
|_, bytes, _| Some(bytes.clone()),
10,
u64::MAX,
one + one / 2,
);
assert_eq!(out.staged.len(), 1, "{:?}", out.failures);
assert_eq!(out.failures.len(), 1);
}
fn out_unpacked(archive: &[u8]) -> u64 {
let dir = tempfile::tempdir().expect("dest");
unpack_archive(archive, dir.path()).expect("unpack")
}
#[test]
fn a_staged_slot_records_its_unpacked_size_and_is_charged_again_on_reuse() {
let root = tempfile::tempdir().expect("staging root");
let archive = extension_archive("acme.tool");
let expected = out_unpacked(&archive);
let first = stage_one_in(root.path(), "extensions/a.gtxpack", &archive).expect("first");
assert_eq!(first.unpacked_bytes, expected);
let again = stage_one_in(root.path(), "extensions/a.gtxpack", &archive).expect("again");
assert_eq!(again.unpacked_bytes, expected);
let err = stage_one_budgeted(root.path(), "extensions/a.gtxpack", &archive, expected - 1)
.expect_err("over budget");
assert!(err.to_string().contains("over the"), "{err}");
}
#[test]
fn an_aged_incomplete_slot_is_swept() {
let root = tempfile::tempdir().expect("staging root");
let orphan = root.path().join("deadbeefdeadbeefdeadbeefdeadbeef");
std::fs::create_dir_all(orphan.join(EXTENSION_SUBDIR)).expect("orphan");
let staged = stage_one_in(
root.path(),
"extensions/a.gtxpack",
&extension_archive("acme.tool"),
)
.expect("stage");
let keep = staged.dir.parent().expect("slot").to_path_buf();
sweep_stale_slots(root.path(), &keep, SUPERSEDED_GRACE);
assert!(orphan.exists(), "a fresh incomplete slot may be mid-unpack");
sweep_stale_slots(root.path(), &keep, std::time::Duration::ZERO);
assert!(!orphan.exists());
assert!(keep.exists());
}
#[test]
fn a_partial_unpack_without_the_marker_is_redone() {
let root = tempfile::tempdir().expect("staging root");
let archive = extension_archive("acme.tool");
let digest = hex::encode(Sha256::digest(&archive));
let slot = root.path().join(&digest[..32]);
let dir = slot.join(EXTENSION_SUBDIR);
std::fs::create_dir_all(&dir).expect("partial dir");
std::fs::write(dir.join("describe.json"), b"trunc").expect("truncated describe");
std::fs::write(dir.join("stale.bin"), b"left behind").expect("stale file");
let staged = stage_one_in(root.path(), "extensions/a.gtxpack", &archive).expect("stage");
assert_eq!(
files_under(&staged.dir),
vec!["describe.json", "extension.wasm"]
);
assert_eq!(
std::fs::read(staged.dir.join("describe.json")).expect("describe"),
describe_bytes("acme.tool"),
);
assert!(slot.join(COMPLETE_MARKER).is_file());
}
#[test]
fn a_slot_left_by_the_old_layout_is_restaged_into_the_new_one() {
let root = tempfile::tempdir().expect("staging root");
let archive = extension_archive("acme.tool");
let digest = hex::encode(Sha256::digest(&archive));
let slot = root.path().join(&digest[..32]);
unpack_archive(&archive, &{
std::fs::create_dir_all(&slot).expect("old slot");
slot.clone()
})
.expect("old-layout unpack");
std::fs::write(slot.join(".staged"), b"").expect("old marker");
let staged = stage_one_in(root.path(), "extensions/a.gtxpack", &archive).expect("stage");
assert_eq!(staged.dir, slot.join(EXTENSION_SUBDIR));
assert_eq!(
files_under(&staged.dir),
vec!["describe.json", "extension.wasm"]
);
assert_eq!(
files_under(&slot),
vec![
SLOT_ID_FILE.to_string(),
COMPLETE_MARKER.to_string(),
format!("{EXTENSION_SUBDIR}/describe.json"),
format!("{EXTENSION_SUBDIR}/extension.wasm"),
],
"the old-layout files and marker must be gone"
);
}
#[cfg(feature = "agentic-worker")]
#[test]
fn a_signed_pack_carried_extension_is_staged_and_registers_through_the_loader() {
use std::io::Write;
use greentic_ext_runtime::{DiscoveryPaths, ExtensionRuntime, RuntimeConfig};
let archive = signed_extension_archive("acme.signed-tool");
let empty_root = tempfile::tempdir().expect("an empty discovery root");
let trust = tempfile::tempdir().expect("a temp trust root");
let config =
RuntimeConfig::from_paths(DiscoveryPaths::new(empty_root.path().to_path_buf()))
.with_trust_root(trust.path().to_path_buf());
let mut runtime = ExtensionRuntime::new(config).expect("build an extension runtime");
assert!(runtime.loaded().is_empty(), "nothing is installed on disk");
let holder = tempfile::tempdir().expect("tempdir");
let pack_path = holder.path().join("worker.gtpack");
let mut writer =
zip::ZipWriter::new(std::fs::File::create(&pack_path).expect("create pack"));
let options: zip::write::FileOptions<'_, ()> =
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
writer
.start_file("extensions/acme.signed-tool.gtxpack", options)
.expect("start entry");
writer.write_all(&archive).expect("write entry");
writer.finish().expect("finish pack");
let pack = Arc::new(crate::pack::tests::pack_runtime_for_dir(&pack_path));
let report = register_from_packs(&mut runtime, &[pack]);
assert_eq!(
report,
PackExtensionLoad {
loaded: 1,
shadowed: 0,
failed: 0,
},
"a correctly signed pack-carried extension must pass the load gate"
);
assert!(
runtime
.loaded()
.keys()
.any(|id| id.as_str() == "acme.signed-tool"),
"the extension must be registered in the runtime"
);
}
#[cfg(feature = "agentic-worker")]
fn signed_extension_archive(id: &str) -> Vec<u8> {
use greentic_extension_sdk_contract::{
DescribeJson, artifact_sha256, bind_manifest, build_manifest, sign_describe,
};
let wasm: Vec<u8> = vec![0x00, 0x61, 0x73, 0x6d, 0x0d, 0x00, 0x01, 0x00];
let wasm_sha = artifact_sha256(&wasm);
let mut describe: DescribeJson = serde_json::from_value(serde_json::json!({
"apiVersion": "greentic.ai/v2",
"kind": "DesignExtension",
"compat": {
"min_designer_version": ">=1.0.0",
"min_runner_version": "^0.12.0",
"contract_version": "1.2.0"
},
"metadata": {
"id": id,
"name": id,
"version": "0.1.0",
"summary": "test",
"author": { "name": "test" },
"license": "MIT"
},
"engine": { "greenticDesigner": "*", "extRuntime": "*" },
"capabilities": {
"offered": [{ "id": "greentic:test/ping", "version": "1.0.0" }],
"required": []
},
"runtime": {
"memoryLimitMB": 64,
"permissions": {},
"components": {
"stub": {
"gtpack": {
"file": "extension.wasm",
"sha256": wasm_sha,
"pack_id": id,
"component_version": "0.1.0"
},
"sha256": wasm_sha,
"world": "greentic:component/stub@0.1.0"
}
}
},
"contributions": {}
}))
.expect("a valid describe");
let manifest = build_manifest(vec![("extension.wasm", wasm.as_slice())]);
let manifest_json = serde_json::to_vec(&manifest).expect("serialise manifest");
bind_manifest(&mut describe, &manifest_json);
let key = ed25519_dalek::SigningKey::from_bytes(&[7u8; 32]);
sign_describe(&mut describe, &key).expect("sign describe");
let describe_json = serde_json::to_vec_pretty(&describe).expect("serialise describe");
zip_bytes(&[
("describe.json", describe_json.as_slice()),
("manifest.json", manifest_json.as_slice()),
("extension.wasm", wasm.as_slice()),
])
}
}