summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/db/model.rs23
-rw-r--r--src/db/queries.rs12
-rw-r--r--src/recipe.rs37
-rw-r--r--src/review/risk.rs2
-rw-r--r--src/watch/digests.rs31
-rw-r--r--src/watch/mod.rs2
-rw-r--r--src/watch/upstream.rs28
-rw-r--r--tests/layers.rs94
8 files changed, 187 insertions, 42 deletions
diff --git a/src/db/model.rs b/src/db/model.rs
index e962d6f..dfc1f8b 100644
--- a/src/db/model.rs
+++ b/src/db/model.rs
@@ -21,6 +21,29 @@ pub struct UpstreamPin {
pub commit: String,
}
+/// A row of the Stage B cache (§5.2): the version that resolution produced,
+/// with the pins it was computed from.
+///
+/// This lives with the schema because the table lives here — the persistence
+/// layer must not name a type from the module that owns VCS probing, which is
+/// the dependency pointing the wrong way (§6 M-6). `watch::upstream::StageB`
+/// is this plus `cached`, and that extra field is the reason the two are not
+/// the same type: "was this answer a cache hit?" is a fact about the resolution
+/// that just happened, not a column.
+#[derive(Debug, Clone)]
+pub struct StageBCacheRow {
+ pub epoch: String,
+ pub pkgver: String,
+ pub pkgrel: String,
+ pub pins: Vec<UpstreamPin>,
+}
+
+impl StageBCacheRow {
+ pub fn version(&self) -> String {
+ crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel)
+ }
+}
+
/// A source entry with the digest *we* computed, regardless of what the
/// PKGBUILD declares (§5.3).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
diff --git a/src/db/queries.rs b/src/db/queries.rs
index ffb2520..b768f83 100644
--- a/src/db/queries.rs
+++ b/src/db/queries.rs
@@ -957,7 +957,7 @@ pub fn insert_artifact_in(tx: &rusqlite::Transaction<'_>, a: &NewArtifact<'_>) -
// --------------------------------------------------------- stage B cache --
/// Exact cache hit: same recipe, same upstream tips (§5.2).
-pub fn stage_b_cache_get(db: &Db, pkgbase_id: i64, recipe_hash: &str, tip_hash: &str) -> Result<Option<crate::watch::upstream::StageB>> {
+pub fn stage_b_cache_get(db: &Db, pkgbase_id: i64, recipe_hash: &str, tip_hash: &str) -> Result<Option<StageBCacheRow>> {
let (rh, th) = (recipe_hash.to_string(), tip_hash.to_string());
db.read(move |c| {
use rusqlite::OptionalExtension;
@@ -975,18 +975,17 @@ pub fn stage_b_cache_get(db: &Db, pkgbase_id: i64, recipe_hash: &str, tip_hash:
},
)
.optional()?
- .map(|(epoch, pkgver, pkgrel, pins_json)| crate::watch::upstream::StageB {
+ .map(|(epoch, pkgver, pkgrel, pins_json)| StageBCacheRow {
epoch,
pkgver,
pkgrel,
pins: serde_json::from_str(&pins_json).unwrap_or_default(),
- cached: true,
}))
})
}
/// The most recent answer for this pkgbase, if it is inside the cooldown window.
-pub fn stage_b_recent(db: &Db, pkgbase_id: i64, cooldown_secs: i64) -> Result<Option<crate::watch::upstream::StageB>> {
+pub fn stage_b_recent(db: &Db, pkgbase_id: i64, cooldown_secs: i64) -> Result<Option<StageBCacheRow>> {
let cutoff = crate::time::secs_ago(cooldown_secs);
db.read(move |c| {
use rusqlite::OptionalExtension;
@@ -1005,12 +1004,11 @@ pub fn stage_b_recent(db: &Db, pkgbase_id: i64, cooldown_secs: i64) -> Result<Op
},
)
.optional()?
- .map(|(epoch, pkgver, pkgrel, pins_json)| crate::watch::upstream::StageB {
+ .map(|(epoch, pkgver, pkgrel, pins_json)| StageBCacheRow {
epoch,
pkgver,
pkgrel,
pins: serde_json::from_str(&pins_json).unwrap_or_default(),
- cached: true,
}))
})
}
@@ -1020,7 +1018,7 @@ pub fn stage_b_cache_put(
pkgbase_id: i64,
recipe_hash: &str,
tip_hash: &str,
- r: &crate::watch::upstream::StageB,
+ r: &StageBCacheRow,
) -> Result<()> {
let (rh, th) = (recipe_hash.to_string(), tip_hash.to_string());
let (epoch, pkgver, pkgrel) = (r.epoch.clone(), r.pkgver.clone(), r.pkgrel.clone());
diff --git a/src/recipe.rs b/src/recipe.rs
index a43059d..6c6666c 100644
--- a/src/recipe.rs
+++ b/src/recipe.rs
@@ -354,6 +354,43 @@ pub fn is_diffable(path: &str) -> bool {
)
}
+/// `sha256sums=('SKIP')`: the recipe declining to verify a source (§5.3).
+pub const SKIP: &str = "SKIP";
+
+/// The checksum a recipe declares for source entry `index`, `sha256sums` first
+/// and `b2sums` second; `None` when the recipe declares nothing for it.
+pub fn declared_sums(recipe: &Recipe, index: usize) -> Option<String> {
+ recipe
+ .sha256sums
+ .get(index)
+ .or_else(|| recipe.b2sums.get(index))
+ .map(|s| s.trim().to_string())
+ .filter(|s| !s.is_empty())
+}
+
+/// True when a declared checksum is an actual hex digest of the length we
+/// expect, rather than `SKIP` or a typo a maintainer left behind.
+pub fn is_hex_digest(s: &str) -> bool {
+ (s.len() == 64 || s.len() == 128) && s.chars().all(|c| c.is_ascii_hexdigit())
+}
+
+/// The non-VCS sources the recipe refuses to verify (§5.3): `SKIP`, a malformed
+/// digest, or no digest at all. A question about the *recipe*, so it lives with
+/// the recipe — the watcher and the risk scorer both ask it, and neither should
+/// own the answer.
+pub fn skip_on_non_vcs(recipe: &Recipe) -> Vec<usize> {
+ recipe
+ .source_entries
+ .iter()
+ .filter(|e| !e.is_vcs())
+ .filter(|e| match declared_sums(recipe, e.index) {
+ Some(d) => d == SKIP || !is_hex_digest(&d),
+ None => true,
+ })
+ .map(|e| e.index)
+ .collect()
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/review/risk.rs b/src/review/risk.rs
index b05604d..0c09841 100644
--- a/src/review/risk.rs
+++ b/src/review/risk.rs
@@ -71,7 +71,7 @@ pub fn assess(
if new.skipgpcheck {
s.add("skippgpcheck", level::HIGH);
}
- for idx in crate::watch::digests::skip_on_non_vcs(new) {
+ for idx in crate::recipe::skip_on_non_vcs(new) {
s.add(&format!("skip-checksum:source-{idx}"), level::HIGH);
}
for md in pkgver::dep_names(&new.makedepends) {
diff --git a/src/watch/digests.rs b/src/watch/digests.rs
index cb7bdbc..c67d60e 100644
--- a/src/watch/digests.rs
+++ b/src/watch/digests.rs
@@ -13,23 +13,9 @@
use crate::config::Config;
use crate::db::{self, *};
use crate::error::Result;
+use crate::recipe::{declared_sums, is_hex_digest, SKIP};
use crate::watch::mirror::Tree;
-const SKIP: &str = "SKIP";
-
-fn declared_sums(recipe: &Recipe, index: usize) -> Option<String> {
- recipe
- .sha256sums
- .get(index)
- .or_else(|| recipe.b2sums.get(index))
- .map(|s| s.trim().to_string())
- .filter(|s| !s.is_empty())
-}
-
-fn is_hex_digest(s: &str) -> bool {
- (s.len() == 64 || s.len() == 128) && s.chars().all(|c| c.is_ascii_hexdigit())
-}
-
/// Resolve the digest of every `source=()` entry.
pub fn resolve_sources(
cfg: &Config,
@@ -157,18 +143,3 @@ pub fn cached_path(db: &Db, url: &str, digest: &str) -> Option<std::path::PathBu
.map(std::path::PathBuf::from)
.filter(|p| p.exists())
}
-
-/// True when a `SKIP` on a non-VCS source means the recipe permanently refuses
-/// to verify its input — itself a risk signal (§5.3).
-pub fn skip_on_non_vcs(recipe: &Recipe) -> Vec<usize> {
- recipe
- .source_entries
- .iter()
- .filter(|e| !e.is_vcs())
- .filter(|e| match declared_sums(recipe, e.index) {
- Some(d) => d == SKIP || !is_hex_digest(&d),
- None => true,
- })
- .map(|e| e.index)
- .collect()
-}
diff --git a/src/watch/mod.rs b/src/watch/mod.rs
index f469f86..87df3f3 100644
--- a/src/watch/mod.rs
+++ b/src/watch/mod.rs
@@ -9,6 +9,6 @@ pub mod staleness;
pub mod upstream;
pub use detect::{check_pkgbase, CheckReport, Outcome};
-pub use digests::{cached_path, download_and_cache, resolve_sources, skip_on_non_vcs};
+pub use digests::{cached_path, download_and_cache, resolve_sources};
pub use mirror::{read_tree, sync_mirror, Tree};
pub use scheduler::spawn_scheduler;
diff --git a/src/watch/upstream.rs b/src/watch/upstream.rs
index 442647c..8d7fa4f 100644
--- a/src/watch/upstream.rs
+++ b/src/watch/upstream.rs
@@ -124,6 +124,28 @@ impl StageB {
pub fn version(&self) -> String {
crate::pkgver::full_version(&self.epoch, &self.pkgver, &self.pkgrel)
}
+
+ /// Rebuild an answer from its cache row. The `cached` flag is ours to set:
+ /// the row says nothing about how this call got its answer (§6 M-6).
+ fn from_cache(row: StageBCacheRow) -> Self {
+ StageB {
+ epoch: row.epoch,
+ pkgver: row.pkgver,
+ pkgrel: row.pkgrel,
+ pins: row.pins,
+ cached: true,
+ }
+ }
+
+ /// The row this answer occupies in `stage_b_cache`.
+ fn as_row(&self) -> StageBCacheRow {
+ StageBCacheRow {
+ epoch: self.epoch.clone(),
+ pkgver: self.pkgver.clone(),
+ pkgrel: self.pkgrel.clone(),
+ pins: self.pins.clone(),
+ }
+ }
}
/// A process-wide cap on concurrent Stage B resolutions (§5.2 "a global
@@ -180,7 +202,7 @@ pub fn resolve(
// Cache hit: same recipe, same upstream tips. Keyed on
// (pkgbase, recipe_hash, upstream_tip) exactly as §5.2 asks.
if let Some(hit) = db::stage_b_cache_get(db, pb.id, recipe_hash, &tip_hash)? {
- return Ok(Some(hit));
+ return Ok(Some(StageB::from_cache(hit)));
}
// Per-pkgbase cooldown: even if the tips moved again, we do not re-resolve
@@ -188,12 +210,12 @@ pub fn resolve(
// is reused, so a chatty upstream cannot turn detection into a build loop.
let cooldown = db::setting_i64(db, "stage_b_cooldown", 86400);
if let Some(recent) = db::stage_b_recent(db, pb.id, cooldown)? {
- return Ok(Some(recent));
+ return Ok(Some(StageB::from_cache(recent)));
}
let _slot = StageBSlot::acquire(db::setting_i64(db, "stage_b_concurrency", 1) as usize);
let resolved = run(cfg, db, pb, recipe, recipe_hash, &tip_hash, stage_a)?;
- db::stage_b_cache_put(db, pb.id, recipe_hash, &tip_hash, &resolved)?;
+ db::stage_b_cache_put(db, pb.id, recipe_hash, &tip_hash, &resolved.as_row())?;
Ok(Some(resolved))
}
diff --git a/tests/layers.rs b/tests/layers.rs
new file mode 100644
index 0000000..548f475
--- /dev/null
+++ b/tests/layers.rs
@@ -0,0 +1,94 @@
+//! The dependency direction between modules (§6 M-6, M-7).
+//!
+//! These are not style rules. The review found the persistence layer naming a
+//! type from the VCS watcher, and the reviewer's risk scorer calling back into
+//! the watcher for a recipe question — two edges pointing up the stack, which
+//! makes `db` and `watch` mutually dependent and means any change to one is a
+//! change to the other. A layering rule that is only written down gets violated
+//! by the next reasonable-looking commit, so it is a test here.
+
+use std::path::{Path, PathBuf};
+
+fn src_root() -> PathBuf {
+ Path::new(env!("CARGO_MANIFEST_DIR")).join("src")
+}
+
+fn rust_files(dir: &Path) -> Vec<PathBuf> {
+ let mut out = Vec::new();
+ let mut stack = vec![dir.to_path_buf()];
+ while let Some(d) = stack.pop() {
+ for entry in std::fs::read_dir(&d).expect("read dir") {
+ let path = entry.expect("entry").path();
+ if path.is_dir() {
+ stack.push(path);
+ } else if path.extension().is_some_and(|e| e == "rs") {
+ out.push(path);
+ }
+ }
+ }
+ out.sort();
+ out
+}
+
+/// Every `crate::X` reference from `dir` into any of `forbidden`, as
+/// "file:line: text" so a failure names the exact edge.
+fn edges(dir: &str, forbidden: &[&str]) -> Vec<String> {
+ let mut hits = Vec::new();
+ for file in rust_files(&src_root().join(dir)) {
+ let text = std::fs::read_to_string(&file).expect("read file");
+ for (n, line) in text.lines().enumerate() {
+ let line = line.trim();
+ if line.starts_with("//") {
+ continue;
+ }
+ for m in forbidden {
+ if line.contains(&format!("crate::{m}")) {
+ hits.push(format!(
+ "{}:{}: {line}",
+ file.strip_prefix(src_root()).unwrap().display(),
+ n + 1
+ ));
+ }
+ }
+ }
+ }
+ hits
+}
+
+#[test]
+fn persistence_names_nothing_above_itself() {
+ // M-6: `db::stage_b_cache_get` used to return `watch::upstream::StageB`, so
+ // the storage layer could not be understood without the watcher. The row
+ // type is `db::model::StageBCacheRow` now and the watcher converts.
+ let hits = edges("db", &["watch", "review", "pipeline", "http", "ops", "shims"]);
+ assert!(hits.is_empty(), "src/db depends upward on the modules above it:\n{hits:#?}");
+}
+
+#[test]
+fn review_does_not_call_back_into_the_watcher() {
+ // M-7: `review::risk` called `watch::digests::skip_on_non_vcs` while
+ // `watch::detect` called `review::candidates` — a cycle. Whether a recipe
+ // skips verification is a recipe question, so `recipe::skip_on_non_vcs` is
+ // where it lives and both sides look down, not sideways.
+ let hits = edges("review", &["watch", "pipeline", "http"]);
+ assert!(hits.is_empty(), "src/review depends on modules beside or above it:\n{hits:#?}");
+}
+
+#[test]
+fn the_watcher_does_not_reach_into_the_pipeline_or_the_ui() {
+ let hits = edges("watch", &["pipeline", "http", "shims"]);
+ assert!(hits.is_empty(), "src/watch depends on modules above it:\n{hits:#?}");
+}
+
+#[test]
+fn the_recipe_leaf_depends_on_nothing_above_it() {
+ // `recipe` is a leaf: parsing and recipe-shaped questions. Everything else
+ // is allowed to ask it; it may ask nothing.
+ let text = std::fs::read_to_string(src_root().join("recipe.rs")).expect("read recipe.rs");
+ for m in ["watch", "review", "pipeline", "http", "ops", "shims"] {
+ assert!(
+ !text.contains(&format!("crate::{m}")),
+ "src/recipe.rs must not name crate::{m}"
+ );
+ }
+}