Score share metrics — the v10 distribution share: each species’ share of its global range, per km² against the national average

Published

2026-11-06

v10 adds one scoring stage to the v8 build and changes nothing upstream of it. The published (“within ecoregion”) metrics of v8 — extrisk_{cat} → ecoregional min–max → equal-weight composite — are kept in cell_metric / zone_metric unchanged and are the control: with SHARE_METRICS=0 this notebook writes nothing and only proves that the five scoring tables still digest to v8’s checkpoints. With SHARE_METRICS=1 it adds the “across the nation” family of the method page (Sensitivity Across Regions, 2026-09-11, rev 14; plan 2026-10-29 §2), computed by msens::share_run() — the rule lives in msens::share_sql() and is asserted by test-share.R:

Three denominators are computed as columns (plan D2; SHARE_DENOM names the headline): global (the species’ whole range — the headline), usa (its US cells: Tim White’s 2026-09-11 brief, the same score with species weight w instead of w · f_us), pooled (no per-species normalization, the RESA-style sum). The floored turtle cells are kept by default and stripped as a sensitivity column (D4). Nothing here touches the v8 rows: the gate below digests them before and after.

1 Design

Code
flowchart LR
  v8[("sdm.duckdb (clone of v8)<br/>model_cell · taxon · cell · zone · zone_cell<br/>cell_metric · zone_metric (within ecoregion)")] --> sr
  td[("taxon_denom<br/>(build_taxon_global_total)")] --> sr
  sr["msens::share_run()<br/>denom = global | usa | pooled<br/>kappa, floor_strip"] --> zm[("zone_metric<br/>extrisk_{cat}_v10_{denom} · _share_ · _pct_world ·<br/>_pct_nation · _coverage · _n_species · primprod_v10 ·<br/>score_v10_equalweights_{denom}")]
  sr --> cm[("cell_metric<br/>extrisk_{cat}_share_global · extrisk_{cat}_v10_global")]
  sr --> zt[("zone_taxon + share_global · share_usa · share_v10_contrib")]
  sr --> rm[("release_method")]
  v8 --> gate["digests of v8's rows == checkpoints_v8<br/>share rows == data/manifests/score_share_metrics.json"]
  zm --> gate
flowchart LR
  v8[("sdm.duckdb (clone of v8)<br/>model_cell · taxon · cell · zone · zone_cell<br/>cell_metric · zone_metric (within ecoregion)")] --> sr
  td[("taxon_denom<br/>(build_taxon_global_total)")] --> sr
  sr["msens::share_run()<br/>denom = global | usa | pooled<br/>kappa, floor_strip"] --> zm[("zone_metric<br/>extrisk_{cat}_v10_{denom} · _share_ · _pct_world ·<br/>_pct_nation · _coverage · _n_species · primprod_v10 ·<br/>score_v10_equalweights_{denom}")]
  sr --> cm[("cell_metric<br/>extrisk_{cat}_share_global · extrisk_{cat}_v10_global")]
  sr --> zt[("zone_taxon + share_global · share_usa · share_v10_contrib")]
  sr --> rm[("release_method")]
  v8 --> gate["digests of v8's rows == checkpoints_v8<br/>share rows == data/manifests/score_share_metrics.json"]
  zm --> gate
Figure 1: v8’s scoring tables (the control, untouched) + taxon_denom → msens::share_run() per denominator → share metrics beside them; reference digests gate every run

2 Setup + flags

Code
librarian::shelf(DBI, dplyr, duckdb, fs, glue, here, jsonlite, logger, purrr, readr, tibble, tidyr,
                 MarineSensitivity/msens, quiet = T)
source(here("libs/paths.R")); source(here("libs/vars.R")); source(here("libs/duckdb_budget.R"))
options(readr.show_col_types = FALSE)
manifest <- here("data/manifests/score_share_metrics.json"); dir_create(path_dir(manifest))
stopifnot(
  "run build_cell_grid / bootstrap first (sdm.duckdb missing)" = file_exists(sdm_db),
  "msens 0.60.0 or newer is required (share_sql / share_run / denom_sql)" = packageVersion("msens") >= "0.60.0")

# method flags -- CONTROL VALUE IS OFF: SHARE_METRICS=0 writes nothing and proves the v8 rows ----
# SHARE_METRICS     : compute the share family (0 = control)
# SHARE_DENOM       : the headline denominator (global | usa | pooled); the other two are written beside it
# SHARE_KAPPA       : the reportability floor as a fraction of the zone's area (0.05)
# SHARE_FLOOR_STRIP : 1 = the headline strips the floored (val = 1) cells of premultiplied taxa; the
#                     other treatment is always written as the `_strip` / `_keep` sensitivity column
# SHARE_CELL_DENOMS : which denominators also get per-cell rasters (default the headline only)
share_metrics  <- .flag("SHARE_METRICS")
share_denom    <- match.arg(Sys.getenv("SHARE_DENOM", "global"), c("global", "usa", "pooled"))
share_kappa    <- as.numeric(Sys.getenv("SHARE_KAPPA", "0.05"))
share_strip    <- .flag("SHARE_FLOOR_STRIP")
cell_denoms    <- strsplit(Sys.getenv("SHARE_CELL_DENOMS", share_denom), ",")[[1]]
denoms         <- c("global", "usa", "pooled")
stopifnot("msens >= 0.61.1 (components_registry)" = packageVersion("msens") >= "0.61.1")
sp_cats        <- msens::components_registry() |> dplyr::filter(kind == "species", key != "other") |> dplyr::pull(key)
stopifnot(!is.na(share_kappa), share_kappa >= 0, share_kappa <= 1, all(cell_denoms %in% denoms))
log_info("SHARE_METRICS={share_metrics} denom={share_denom} kappa={share_kappa} floor_strip={share_strip} cell_denoms={paste(cell_denoms, collapse=',')}")

con <- dbConnect(duckdb(sdm_db))
duckdb_tune(con, path.expand(glue("{dir_big_v}/_tmp")))
val_col <- msens::sdm_val_col(con, "cell_metric")
stopifnot("cell_metric must use `val` (v8+ schema)" = val_col == "val")

# the five tables the control digests, DOUBLEs as FLOAT (build_v7c: parallel SUM() reassociates,
# so raw-double digests legitimately differ between two correct builds)
digest_tables <- function(con) list(
  cell_metric = msens::hash_query(con, "SELECT m.metric_key, cm.cell_id, CAST(cm.val AS FLOAT) AS val
                                        FROM cell_metric cm JOIN metric m USING (metric_seq)"),
  zone_metric = msens::hash_query(con, "SELECT z.fld, z.val AS zone_val, m.metric_key, CAST(zm.val AS FLOAT) AS val
                                        FROM zone_metric zm JOIN zone z USING (zone_seq) JOIN metric m USING (metric_seq)"),
  metric      = msens::hash_query(con, "SELECT metric_key FROM metric"),
  taxon       = msens::hash_query(con, "SELECT taxon_id, ms_merge_key, sp_cat, er_score, is_valid_usa, is_marine, is_er_spatial FROM taxon"),
  zone_taxon  = msens::hash_query(con, "SELECT zone_fld, zone_value, mdl_key, CAST(area_km2 AS FLOAT) AS area_km2,
                                        CAST(avg_suit AS FLOAT) AS avg_suit, CAST(suit_er_area AS FLOAT) AS suit_er_area FROM zone_taxon"))

3 The control: v8’s rows are v8’s, before anything is written

The clone verified at bootstrap hashes the whole tables (checkpoints_v8.json). This stage adds rows to three of them, so its own gate is on the v8 subset — the metric keys and zone_taxon columns v8 wrote — digested the same way before and after the share rows land. The reference values are recorded on the first run and asserted on every later one; BUILD_REFERENCE=1 is the only way to move them. Zone-level surfaces are digested as FLOAT (as build_v7c does); the per-cell shares at four significant digits, because their parallel sums differ in the last double bits between two correct runs.

Code
v8_keys <- dbGetQuery(con, "SELECT metric_key FROM metric WHERE metric_key NOT LIKE '%\\_v10%' ESCAPE '\\'
  AND metric_key NOT LIKE 'extrisk\\_%\\_share\\_%' ESCAPE '\\' AND metric_key NOT LIKE '%\\_pct\\_world' ESCAPE '\\'
  AND metric_key NOT LIKE '%\\_pct\\_nation%' ESCAPE '\\' AND metric_key NOT LIKE '%\\_coverage' ESCAPE '\\'
  AND metric_key NOT LIKE '%\\_n\\_species' ESCAPE '\\' AND metric_key NOT LIKE '%\\_mean\\_share%' ESCAPE '\\'
  AND metric_key NOT LIKE 'primprod\\_v10%' ESCAPE '\\' AND metric_key NOT LIKE 'score\\_v10%' ESCAPE '\\'")$metric_key
keys_sql <- paste(sprintf("'%s'", v8_keys), collapse = ", ")
digest_v8 <- function(con) list(
  cell_metric = msens::hash_query(con, glue("SELECT m.metric_key, cm.cell_id, CAST(cm.val AS FLOAT) AS val
    FROM cell_metric cm JOIN metric m USING (metric_seq) WHERE m.metric_key IN ({keys_sql})")),
  zone_metric = msens::hash_query(con, glue("SELECT z.fld, z.val AS zone_val, m.metric_key, CAST(zm.val AS FLOAT) AS val
    FROM zone_metric zm JOIN zone z USING (zone_seq) JOIN metric m USING (metric_seq) WHERE m.metric_key IN ({keys_sql})")),
  metric      = msens::hash_query(con, glue("SELECT metric_key FROM metric WHERE metric_key IN ({keys_sql})")),
  taxon       = digest_tables(con)$taxon,
  zone_taxon  = digest_tables(con)$zone_taxon)
before <- digest_v8(con)
ck_prev <- fromJSON(here(glue("data/manifests/checkpoints_{ver_prev}.json")))$targets
# the whole-table checkpoints of ver_prev hold as long as nothing has been added: the FIRST run of
# this stage (no share rows yet) must reproduce them exactly, which ties this gate to the clone's
n_share_keys <- dbGetQuery(con, "SELECT count(*) n FROM metric")$n - length(v8_keys)
# the ver_prev gate is a CONTROL gate: it holds only when every v11 method flag sits at its control value (the
# v8 scoring family is then ver_prev's by construction). A method run (any flag on) changes those rows on
# purpose; its evidence is the attribution harness (snapshot_scores + compare_versions), not this gate.
v11_control <- isTRUE(v11_flags_off)
if (n_share_keys == 0 && !v11_control) log_info("v11 method flags on ({paste(v11_flags_on, collapse = ', ')}): the ver_prev scoring gate does not apply")
if (n_share_keys == 0 && v11_control) {
  whole <- list(cell_metric = msens::hash_query(con, "cell_metric"), zone_metric = msens::hash_query(con, "zone_metric"))
  exact <- identical(whole$cell_metric, ck_prev$score_cell_metrics$content_hash) &&
           identical(whole$zone_metric, ck_prev$score_zone_metrics$content_hash)
  if (exact) {
    log_info("GATE ver_prev checkpoints reproduced on the untouched tables: cell_metric {whole$cell_metric}, zone_metric {whole$zone_metric}")
  } else {
    # a version SCORED FROM SCRATCH on another machine (v11's control on the Mac mini, 2026-10-30) reproduces v8's
    # merge bit-for-bit but not its cell_metric digest: the raw extrisk_* metrics are rounded to two decimals from
    # parallel sums, and ~0.1 % of cells sit on a rounding tie that reassociation flips by exactly 0.01 (zone
    # metrics then differ by < 1e-4). The gate for the scoring family is therefore a TOLERANCE against ver_prev's
    # own tables: identical row sets, raw cell |delta| <= 0.01 on <= 0.5 % of rows, zone |delta| <= 1e-3.
    prev_sdm <- path.expand(glue("{dir_big}/{ver_prev}/sdm.duckdb"))
    stopifnot("cell_metric / zone_metric do not hash to ver_prev's checkpoints and ver_prev's sdm.duckdb is not here to compare against" = file_exists(prev_sdm))
    dbExecute(con, glue("ATTACH '{prev_sdm}' AS prev (READ_ONLY)"))
    cmp <- dbGetQuery(con, glue("
      WITH a AS (SELECT m.metric_key, cm.cell_id, cm.val FROM cell_metric cm JOIN metric m USING (metric_seq) WHERE m.metric_key IN ({keys_sql})),
           b AS (SELECT m.metric_key, cm.cell_id, cm.val FROM prev.cell_metric cm JOIN prev.metric m USING (metric_seq) WHERE m.metric_key IN ({keys_sql}))
      SELECT count(*) AS n, count(*) FILTER (WHERE a.val IS DISTINCT FROM b.val) AS n_diff,
             max(abs(a.val - b.val)) AS max_abs,
             count(*) FILTER (WHERE a.val IS NULL OR b.val IS NULL) AS n_oneside,
             count(*) FILTER (WHERE a.val IS DISTINCT FROM b.val AND m2.metric_key NOT LIKE '%rescaled%' AND m2.metric_key NOT LIKE 'score%') AS n_diff_raw,
             max(abs(a.val - b.val)) FILTER (WHERE m2.metric_key NOT LIKE '%rescaled%' AND m2.metric_key NOT LIKE 'score%') AS max_abs_raw
      FROM a FULL OUTER JOIN b USING (metric_key, cell_id) JOIN metric m2 ON m2.metric_key = coalesce(a.metric_key, b.metric_key)"))
    cmz <- dbGetQuery(con, glue("
      WITH a AS (SELECT z.fld, z.val AS zone_val, m.metric_key, zm.val FROM zone_metric zm JOIN zone z USING (zone_seq) JOIN metric m USING (metric_seq) WHERE m.metric_key IN ({keys_sql})),
           b AS (SELECT z.fld, z.val AS zone_val, m.metric_key, zm.val FROM prev.zone_metric zm JOIN prev.zone z USING (zone_seq) JOIN prev.metric m USING (metric_seq) WHERE m.metric_key IN ({keys_sql}))
      SELECT count(*) AS n, count(*) FILTER (WHERE a.val IS DISTINCT FROM b.val) AS n_diff, max(abs(a.val - b.val)) AS max_abs,
             count(*) FILTER (WHERE a.val IS NULL OR b.val IS NULL) AS n_oneside
      FROM a FULL OUTER JOIN b USING (fld, zone_val, metric_key)"))
    dbExecute(con, "DETACH prev")
    log_warn("GATE (tolerance): ver_prev's scoring digests not reproduced bit-for-bit (cell_metric {whole$cell_metric} vs {ck_prev$score_cell_metrics$content_hash}); ",
             "vs {ver_prev}'s tables: cell rows {cmp$n}, {cmp$n_diff} differ (raw {cmp$n_diff_raw}, max |delta| raw {signif(cmp$max_abs_raw, 3)}, any {signif(cmp$max_abs, 3)}), ",
             "{cmp$n_oneside} one-sided; zone rows {cmz$n}, {cmz$n_diff} differ, max |delta| {signif(cmz$max_abs, 3)}, {cmz$n_oneside} one-sided")
    stopifnot(
      "cell_metric row set differs from ver_prev's (a cell or metric present on one side only)" = cmp$n_oneside == 0,
      "zone_metric row set differs from ver_prev's" = cmz$n_oneside == 0,
      "raw cell metrics differ from ver_prev's by more than a 0.01 rounding tie" = cmp$max_abs_raw <= 0.01 + 1e-9,
      "more than 0.5 % of raw cell metrics differ from ver_prev's" = cmp$n_diff_raw <= 0.005 * cmp$n,
      "zone metrics differ from ver_prev's by more than 1e-3" = cmz$max_abs <= 1e-3)
    log_info("GATE ver_prev's scoring reproduced within tolerance (floating-point rounding ties only)")
  }
}
knitr::kable(tibble(table = names(before), digest_v8_rows = unlist(before)), caption = glue("digests of {ver_prev}'s rows before this run ({length(v8_keys)} metric keys)"))
digests of v10’s rows before this run (41 metric keys)
table digest_v8_rows
cell_metric a4fb678b97c4d9df
zone_metric 28968defb82fb049
metric 9d9d35364e9ad460
taxon 9471172023a7895e
zone_taxon c575b6a5ddadaae1

4 The share family (SHARE_METRICS=1)

msens::share_run() is called once per denominator (and once more for the floor-strip sensitivity under the global denominator); each call returns the zone table, the per-cell table, the per-zone per-species table and the composite, and asserts the rule’s own invariants (a positive denominator for every species, cell shares summing to 1 / f_us, the national pseudo-zone present). Inputs: taxon_denom (from build_taxon_global_total), a temporary taxon shadow carrying er_mode for the v8 schema, an empty temporary model_cell_er, and primprod_cell from v8’s own primprod metric. The national reference is the study area (cell.in_usa), not the USA pseudo-zone (see ?msens::share_sql).

Code
# idempotent metric_seq: fetch or insert the key, then wipe the prior rows of this key at the ONE level
# being written. A metric key is shared by cell_metric and zone_metric by design (v8's
# extrisk_{cat}_ecoregion_rescaled lives at both levels), so a writer must never clear the other
# level: the first render of this stage deleted the zone-level headline rows while writing the
# per-cell rasters of the same keys.
mseq <- function(key, desc = "", level = c("zone", "cell")) {
  level <- match.arg(level)
  ex <- dbGetQuery(con, glue("SELECT metric_seq FROM metric WHERE metric_key = '{key}'"))
  s  <- if (nrow(ex)) ex$metric_seq[1] else {
    s0 <- dbGetQuery(con, "SELECT coalesce(max(metric_seq),0)+1 s FROM metric")$s
    dbExecute(con, glue("INSERT INTO metric VALUES ({s0}, '{key}', '{gsub(\"'\",\"\",desc)}')")); s0 }
  dbExecute(con, glue("DELETE FROM {level}_metric WHERE metric_seq = {s}"))
  s
}
written <- list()   # key -> rows written per level, re-read from the database at the end
write_zone <- function(d, key, desc, col = "val") {
  s <- mseq(key, desc, "zone")
  d <- d |> filter(!is.na(.data[[col]])) |> transmute(zone_seq, metric_seq = s, val = .data[[col]])
  if (nrow(d)) { duckdb_register(con, "tmp_zm", d); dbExecute(con, glue("INSERT INTO zone_metric SELECT * FROM tmp_zm")); duckdb_unregister(con, "tmp_zm") }
  written[[paste0("zone:", key)]] <<- nrow(d)
  nrow(d)
}
write_cell <- function(d, key, desc, col = "val") {
  s <- mseq(key, desc, "cell")
  d <- d |> filter(!is.na(.data[[col]])) |> transmute(cell_id, metric_seq = s, val = .data[[col]])
  duckdb_register(con, "tmp_cm", d); dbExecute(con, glue("INSERT INTO cell_metric SELECT * FROM tmp_cm")); duckdb_unregister(con, "tmp_cm")
  written[[paste0("cell:", key)]] <<- nrow(d)
  nrow(d)
}
LAB <- c(global = "share of the species' global range", usa = "share of the species' US distribution (Tim White 2026-09-11)",
         pooled = "pooled value-area, no per-species normalization (RESA-style)")

results <- list(); n_written <- 0L
if (share_metrics) {
  stopifnot("run build_taxon_global_total first (taxon_denom missing)" = "taxon_denom" %in% dbListTables(con))
  # v8's taxon has no er_mode and no model_cell_er: a temporary shadow carries them (main.taxon untouched,
  # as in build_taxon_global_total); is_er_spatial marks exactly the premultiplied sea turtles
  if (!"er_mode" %in% dbListFields(con, "taxon"))
    dbExecute(con, "CREATE TEMP TABLE taxon AS SELECT *, CASE WHEN is_er_spatial THEN 'premultiplied' ELSE 'taxon' END AS er_mode FROM main.taxon")
  if (!"model_cell_er" %in% dbListTables(con))
    dbExecute(con, "CREATE TEMP TABLE model_cell_er (mdl_key VARCHAR, cell_id INTEGER, er DOUBLE)")
  dbExecute(con, "CREATE OR REPLACE TEMP TABLE primprod_cell AS
    SELECT cm.cell_id, cm.val AS npp FROM cell_metric cm JOIN metric m USING (metric_seq) WHERE m.metric_key = 'primprod'")
  zones <- dbGetQuery(con, "SELECT zone_seq, fld, val AS zone_key FROM zone")

  runs <- c(setNames(as.list(denoms), denoms), list(global_alt = "global"))
  for (nm in names(runs)) {
    d <- runs[[nm]]; strip <- if (nm == "global_alt") !share_strip else share_strip
    t0 <- Sys.time()
    r <- share_run(con, cats = sp_cats, denom = d, kappa = share_kappa, floor_strip = strip)
    results[[nm]] <- r
    log_info("share_run {nm} (denom {d}, floor_strip {strip}): {nrow(r$zone)} zone x category rows, {nrow(r$cell)} cell rows, {round(as.numeric(difftime(Sys.time(), t0, units = 'mins')), 1)} min")
    sfx <- if (nm == "global_alt") glue("global_{if (strip) 'strip' else 'keep'}") else d
    # zone metrics (every zone set) ----
    for (cat in sp_cats) {
      z <- r$zone |> filter(sp_cat == cat)
      n_written <- n_written +
        write_zone(z, glue("extrisk_{cat}_v10_{sfx}"), glue("v10 across the nation, {cat}: 100 = the national average concentration of risk-weighted {LAB[[d]]}; absent where coverage < kappa"), "v10") +
        write_zone(z, glue("extrisk_{cat}_share_{sfx}"), glue("zone total of risk-weighted {LAB[[d]]}, {cat}"), "share") +
        write_zone(z, glue("extrisk_{cat}_pct_nation_{sfx}"), glue("percent of the nation's risk-weighted {cat} importance in the zone ({d} denominator)"), "pct_nation") +
        write_zone(z, glue("extrisk_{cat}_mean_share_{sfx}"), glue("zone total divided by the number of {cat} species present ({d} denominator)"), "mean_share")
      if (nm == "global") n_written <- n_written +
        write_zone(z, glue("extrisk_{cat}_pct_world"), glue("percent of the world's risk-weighted {cat} importance in the zone"), "pct_world") +
        write_zone(z, glue("extrisk_{cat}_coverage"), glue("area-weighted fraction of the zone's cells carrying a scored {cat} species (reportable iff >= kappa)"), "coverage") +
        write_zone(z, glue("extrisk_{cat}_n_species"), glue("number of scored {cat} species with cells in the zone"), "n_species")
    }
    if (nm == "global") n_written <- n_written +
      write_zone(r$primprod, "primprod_v10", "v10 across the nation, primary productivity: 100 x the zone's area-weighted mean npp over the national mean; absent where coverage < kappa", "v10") +
      write_zone(r$primprod, "primprod_coverage", "area-weighted fraction of the zone's cells with a productivity value", "coverage")
    n_written <- n_written +
      write_zone(r$composite, glue("score_v10_equalweights_{sfx}"), glue("v10 composite: equal-weight mean of the reportable components ({LAB[[d]]})"), "score") +
      write_zone(r$composite, glue("score_v10_n_components_{sfx}"), "number of reportable components in the composite", "n_components")
    # cell rasters (headline denominators only) ----
    if (nm %in% cell_denoms) {
      for (cat in sp_cats) {
        cc <- r$cell |> filter(sp_cat == cat)
        n_written <- n_written +
          write_cell(cc, glue("extrisk_{cat}_share_{sfx}"), glue("per-cell sum of risk-weighted {LAB[[d]]}, {cat}"), "share") +
          write_cell(cc, glue("extrisk_{cat}_v10_{sfx}"), glue("v10 per cell, {cat}: 100 = the national average cell ({d} denominator)"), "v10")
      }
      # per-cell composite: the equal-weight mean of the components present in the cell, with the
      # productivity quotient as one more component (no coverage floor at cell level)
      comp_cell <- dbGetQuery(con, "SELECT cell_id, npp FROM primprod_cell") |>
        transmute(cell_id, v10 = 100 * npp / r$primprod$mean_usa[1]) |>
        bind_rows(r$cell |> select(cell_id, v10)) |>
        group_by(cell_id) |> summarise(val = mean(v10), .groups = "drop")
      n_written <- n_written + write_cell(comp_cell, glue("score_v10_equalweights_{sfx}"), glue("v10 composite per cell: equal-weight mean of the components present ({LAB[[d]]})"), "val")
    }
  }
  # zone_taxon: the per-species shares that explain a zone's score (treemap leaf = w x share) ----
  for (col in c("share_global", "share_usa", "share_v10_contrib"))
    if (!col %in% dbListFields(con, "zone_taxon")) dbExecute(con, glue("ALTER TABLE zone_taxon ADD COLUMN {col} DOUBLE"))
  zt <- results[[share_denom]]$zone_taxon |> inner_join(zones, by = "zone_seq") |>
    transmute(zone_fld = fld, zone_value = zone_key, mdl_key = ms_merge_key, share_global, share_usa, share_v10_contrib = contrib)
  duckdb_register(con, "tmp_zt", zt)
  dbExecute(con, "UPDATE zone_taxon SET share_global = NULL, share_usa = NULL, share_v10_contrib = NULL")
  n_zt <- dbExecute(con, "UPDATE zone_taxon z SET share_global = t.share_global, share_usa = t.share_usa, share_v10_contrib = t.share_v10_contrib
    FROM tmp_zt t WHERE t.zone_fld = z.zone_fld AND t.zone_value = z.zone_value AND t.mdl_key = z.mdl_key")
  duckdb_unregister(con, "tmp_zt")
  log_info("zone_taxon: share columns set on {n_zt} rows ({share_denom} headline)")
  log_info("share family: {n_written} metric rows written")
  # what was written is what the database holds: every key re-read at its level
  held <- dbGetQuery(con, "SELECT 'zone:' || m.metric_key AS k, count(*) n FROM zone_metric zm JOIN metric m USING (metric_seq) GROUP BY 1
    UNION ALL SELECT 'cell:' || m.metric_key, count(*) FROM cell_metric cm JOIN metric m USING (metric_seq) GROUP BY 1")
  chk <- tibble(k = names(written), n_written = unlist(written)) |> left_join(held, by = "k") |> mutate(n = coalesce(n, 0L), ok = n == n_written)
  stopifnot("a written metric does not hold the rows written (a writer cleared another level's rows)" = all(chk$ok))
  stopifnot("the headline composite has no Program-Area rows" = held$n[held$k == glue("zone:score_v10_equalweights_{share_denom}")] >= 20)
  log_info("re-read check: {nrow(chk)} key x level pairs hold exactly the rows written")
} else log_info("SHARE_METRICS unset — control run: nothing written")

5 The methods as data (release_method)

Code
onoff <- function(x) if (isTRUE(x)) "on" else "off"
bs_rec <- tryCatch(fromJSON(here("data/manifests/bootstrap_version.json")), error = function(e) NULL)
merge_is_clone <- !is.null(bs_rec) && identical(bs_rec$ver, ver) && isTRUE(bs_rec$clone_merge) && isTRUE(bs_rec$merge_verified)
rm_rows <- tibble(
  method_key = c("share_metrics", "share_denom", "share_kappa_pct", "share_floor_strip", "share_components", "components_labels", "prev"),
  value = c(onoff(share_metrics), share_denom, as.character(share_kappa * 100), onoff(share_strip), paste(sp_cats, collapse = ","), if (components_v11) "v11" else "v10", ver_prev),
  description = c(
    "The across-the-nation score family (each species' risk-weighted share of its range per km2 against the national average = 100) computed beside the within-ecoregion metrics (v8: off)",
    "The headline denominator of each species' share: global (its whole modelled range), usa (its US distribution), pooled (no per-species normalization); the other two are written beside it",
    "A zone component is reportable only if its scored cells cover at least this percent of the zone's area (area-weighted); below it there is no score, never a zero",
    "Whether the floored (val = 1) cells of premultiplied taxa (the v4b-v10 sea-turtle rule; none under TURTLE_RULE=dps) are zeroed on both sides of the share (off = kept, the surface stays continuous)",
    "The scored components; v8's taxonomy-based categories (no 'other'); primary productivity joins as primprod_v10",
    "Which component names the manifest shows: v10 (Primary Producers, Primary productivity) or v11 (Flora, Productivity; data/components.csv, msens::component_label())",
    if (isTRUE(merge_is_clone)) "The release whose merge and within-ecoregion scoring this version keeps unchanged" else
      "The release this version is compared against; the merge was re-run under the method flags recorded in the rows that follow"))
# the v11 method flags (libs/vars.R `v11_flags`) as data, so the docs state the rules from the release rather than
# from prose: every flag with its value as this run saw it (a flag at its default = v10's rule). Recorded for every
# version from v10 on -- v10's rows all read their control values, which is itself the statement that v10 is v8's merge.
flag_desc <- c(
  VGPM_INTEGRATED  = "Primary productivity as the year-integrated VGPM (polar night = 0) instead of the mean of available months (off: v8/v10)",
  TURTLE_SUIT      = "Sea-turtle suitability source: am = AquaMaps, ogm = the project's own occurrence-based models",
  TURTLE_RULE      = "Sea-turtle merge rule: turtle = multiplicative er x suit (v4b-v10), dps = suitability with the per-cell ER beside (dps_sql)",
  OCC_RULE         = "Zero-record occurrence gate: a species is excluded from an ecoregion with no OBIS record (0.5-degree blocks; protected, narrow-range and unsearched overrides; msens::occurrence_rule)",
  REGION_WEIGHT    = "Encounter weights per species x ecoregion (msens::encounter_weight)",
  FOLD_DENSITY     = "Survey density datasets (gm, nc, cce) replace suitability inside their domain",
  FOLD_DENSITY_CAL = "Calibration of the folded-in survey: none, mass (the control's value basis) or suit (the suitability prior where modelled)",
  WORMS_SOURCE     = "WoRMS taxonomy source: download or obisindicators",
  COMPONENTS_V11   = "Component labels Flora / Productivity (data/components.csv)",
  NC_ATL_SOURCE    = "NCCOS Atlantic seabird set: 2018 (Winship et al.) or 2023 (MDAT v3 / BOEM 2023-060)",
  SHARE_BASIS      = "Merged value basis: taxon = max(er, suit) with the governing ER multiplied in again at scoring; cell = suitability alone, ER beside (single counting)",
  RANGE_FILL       = "Value of a range cell no suitability model covers under the cell basis: er (the governing score), presence (100) or mean (the taxon's modelled-cell mean)")
if (exists("v11_flags")) {
  fl <- v11_flags[names(flag_desc)]
  rm_rows <- bind_rows(rm_rows, tibble(
    method_key  = tolower(names(fl)),
    value       = vapply(fl, function(x) if (is.logical(x)) onoff(x) else as.character(x), ""),
    description = unname(flag_desc[names(fl)])))
}
if (share_metrics) dbWriteTable(con, "release_method", rm_rows, overwrite = TRUE)
knitr::kable(rm_rows)
method_key value description
share_metrics on The across-the-nation score family (each species’ risk-weighted share of its range per km2 against the national average = 100) computed beside the within-ecoregion metrics (v8: off)
share_denom global The headline denominator of each species’ share: global (its whole modelled range), usa (its US distribution), pooled (no per-species normalization); the other two are written beside it
share_kappa_pct 5 A zone component is reportable only if its scored cells cover at least this percent of the zone’s area (area-weighted); below it there is no score, never a zero
share_floor_strip off Whether the floored (val = 1) cells of premultiplied taxa (the v4b-v10 sea-turtle rule; none under TURTLE_RULE=dps) are zeroed on both sides of the share (off = kept, the surface stays continuous)
share_components bird,coral,fish,invertebrate,mammal,primary_producer,turtle The scored components; v8’s taxonomy-based categories (no ‘other’); primary productivity joins as primprod_v10
components_labels v11 Which component names the manifest shows: v10 (Primary Producers, Primary productivity) or v11 (Flora, Productivity; data/components.csv, msens::component_label())
prev v10 The release this version is compared against; the merge was re-run under the method flags recorded in the rows that follow
vgpm_integrated on Primary productivity as the year-integrated VGPM (polar night = 0) instead of the mean of available months (off: v8/v10)
turtle_suit ogm Sea-turtle suitability source: am = AquaMaps, ogm = the project’s own occurrence-based models
turtle_rule dps Sea-turtle merge rule: turtle = multiplicative er x suit (v4b-v10), dps = suitability with the per-cell ER beside (dps_sql)
occ_rule on Zero-record occurrence gate: a species is excluded from an ecoregion with no OBIS record (0.5-degree blocks; protected, narrow-range and unsearched overrides; msens::occurrence_rule)
region_weight on Encounter weights per species x ecoregion (msens::encounter_weight)
fold_density on Survey density datasets (gm, nc, cce) replace suitability inside their domain
fold_density_cal suit Calibration of the folded-in survey: none, mass (the control’s value basis) or suit (the suitability prior where modelled)
worms_source download WoRMS taxonomy source: download or obisindicators
components_v11 on Component labels Flora / Productivity (data/components.csv)
nc_atl_source 2023 NCCOS Atlantic seabird set: 2018 (Winship et al.) or 2023 (MDAT v3 / BOEM 2023-060)
share_basis cell Merged value basis: taxon = max(er, suit) with the governing ER multiplied in again at scoring; cell = suitability alone, ER beside (single counting)
range_fill mean Value of a range cell no suitability model covers under the cell basis: er (the governing score), presence (100) or mean (the taxon’s modelled-cell mean)

6 Gates: the v8 rows are untouched, the share rows reproduce the reference

Code
after <- digest_v8(con)
d_ctl <- tibble(table = names(before), before = unlist(before), after = unlist(after)) |> mutate(same = before == after)
knitr::kable(d_ctl, caption = glue("{ver_prev}'s rows before vs after this run"))
v10’s rows before vs after this run
table before after same
cell_metric a4fb678b97c4d9df a4fb678b97c4d9df TRUE
zone_metric 28968defb82fb049 28968defb82fb049 TRUE
metric 9d9d35364e9ad460 9d9d35364e9ad460 TRUE
taxon 9471172023a7895e 9471172023a7895e TRUE
zone_taxon c575b6a5ddadaae1 c575b6a5ddadaae1 TRUE
Code
stopifnot("this stage changed a v8 row: the control is broken" = all(d_ctl$same))

# the share rows' digests: recorded by BUILD_REFERENCE=1 (or the first run), asserted on every
# same-flag run after; a run with different flags is a control render and is not compared
h_share <- if (share_metrics) list(
  zone_share = msens::hash_query(con, glue("SELECT z.fld, z.val AS zone_val, m.metric_key, CAST(zm.val AS FLOAT) AS val
    FROM zone_metric zm JOIN zone z USING (zone_seq) JOIN metric m USING (metric_seq) WHERE m.metric_key NOT IN ({keys_sql})")),
  # the per-cell shares are sums over thousands of species whose parallel summation order differs
  # between two correct runs (measured: 4,822 of 6,217 GAA species sums differ in the last bits,
  # max relative 1e-13), and over 3.9 M cells x 15 keys a FLOAT cast still flips somewhere (the first
  # same-flag rerun failed on this surface alone). Four significant digits leave an expected
  # 0.01 flips per run; the zone-level surfaces reproduce at FLOAT precision and keep it.
  cell_share = msens::hash_query(con, glue("SELECT m.metric_key, cm.cell_id, printf('%.3e', cm.val) AS val
    FROM cell_metric cm JOIN metric m USING (metric_seq) WHERE m.metric_key NOT IN ({keys_sql})")),
  zone_taxon_share = msens::hash_query(con, "SELECT zone_fld, zone_value, mdl_key, CAST(share_global AS FLOAT) AS a,
    CAST(share_usa AS FLOAT) AS b, CAST(share_v10_contrib AS FLOAT) AS c FROM zone_taxon WHERE share_global IS NOT NULL")) else list()
ref <- if (file_exists(manifest)) fromJSON(manifest) else NULL
is_ref <- nzchar(Sys.getenv("BUILD_REFERENCE"))
flags <- list(share_metrics = share_metrics, share_denom = share_denom, share_kappa = share_kappa, share_floor_strip = share_strip,
              cell_denoms = paste(cell_denoms, collapse = ","))
# the committed reference is the CONTROL's (every v11 method flag at its control value, == v10): a method run
# (snapshot_scores records its variant) is never compared to it and never overwrites it
same_flags <- !is.null(ref) && all(vapply(names(flags), \(k) isTRUE(all.equal(ref[[k]], flags[[k]])), TRUE)) && isTRUE(v11_flags_off)
if (share_metrics) {
  d_ref <- tibble(surface = names(h_share), digest = unlist(h_share),
                  reference = vapply(names(h_share), \(k) ref[[glue("digest_{k}")]] %||% NA_character_, ""))
  knitr::kable(d_ref, caption = "share-row digests vs the committed reference")
  if (is_ref || is.null(ref) || !isTRUE(ref$share_metrics)) log_info("BUILD_REFERENCE / no reference yet: these digests BECOME the reference")
  else if (!same_flags) log_info("flags differ from the committed reference's ({if (!isTRUE(v11_flags_off)) paste('v11 method run:', paste(v11_flags_on, collapse = ', ')) else 'a share-flag sensitivity render'}) — reference not compared")
  # zone_taxon_share: v8's zone_taxon (cloned into v10) was STALE for the subregion zone set -- no Atlantic (AT)
  # rows at all, 3,605 of 11,631 Pacific rows, 9,632 of 17,103 USA rows -- while its zone_cell and zone_metric
  # tables had every subregion. A version scored from scratch (v11, 2026-10-30) rebuilds zone_taxon in full, so
  # its reference digest for that surface was re-recorded (BUILD_REFERENCE=1) after proving zone_share and
  # cell_share reproduce v10's bit-for-bit and every zone_taxon row common to v10 matches at FLOAT precision.
  # v10 itself (2026-11-05): REDO_ZONE_TAXON rebuilt the clone in full (115,700 -> 138,728 rows; the 23,028 added
  # rows are all subregion_key) and this gate stopped on zone_taxon_share alone -- zone_share and cell_share
  # reproduced the reference bit for bit, and the rebuilt rows restricted to v8's stale key set hashed EXACTLY to
  # the old reference (9e6bebde1fe69de3). The digest moved by row coverage, not by value, so the reference was
  # re-recorded (BUILD_REFERENCE=1, authorized by Ben) on the complete table.
  else stopifnot("the share rows differ from the committed reference (same flags): a correct build reproduces them on any machine" = all(d_ref$digest == d_ref$reference))
}

7 Plausibility: the method’s own validation targets (plan 2026-10-29 §2)

The page’s v9 numbers are the reference shape, not the target: on v8’s models the Gulf of America Program Areas must hold the nation’s turtle concentration, the Aleutian Arc almost none, St. George Basin’s turtles must not be reportable, Kemp’s ridley must carry the Gulf’s turtle column, the Arctic must keep its mammal column through the bowhead, walrus and ice seals, and Alaska as a whole must hold under 1 % of the nation’s turtle importance under the global weighting.

Code
if (share_metrics) {
  r  <- results[[share_denom]]
  pa <- r$zone |> inner_join(zones, by = "zone_seq") |> filter(fld == "programarea_key")
  v10_of <- function(key, cat) { x <- pa |> filter(zone_key == key, sp_cat == cat); if (nrow(x)) x$v10 else NA_real_ }
  rep_of <- function(key, cat) { x <- pa |> filter(zone_key == key, sp_cat == cat); nrow(x) > 0 && isTRUE(x$reportable) }
  turt <- pa |> filter(sp_cat == "turtle") |> select(zone_key, share, n_species, coverage, reportable, pct_world, pct_nation, v10) |> arrange(desc(v10))
  msens::report_table(turt, caption = glue("sea turtles by Program Area ({share_denom} denominator)"))
  ak_keys <- dbGetQuery(con, "SELECT DISTINCT pa.val AS pa FROM zone pa JOIN zone_cell zc USING (zone_seq)
    JOIN zone_cell zs ON zs.cell_id = zc.cell_id JOIN zone s ON s.zone_seq = zs.zone_seq
    WHERE pa.fld = 'programarea_key' AND s.fld = 'subregion_key' AND s.val = 'AK'")$pa
  ak_turtle_pct <- sum(turt$pct_nation[turt$zone_key %in% ak_keys], na.rm = TRUE)
  # top contributors per Program Area and category (what the treemap shows)
  top <- r$zone_taxon |> inner_join(zones, by = "zone_seq") |> filter(fld == "programarea_key") |>
    inner_join(dbGetQuery(con, "SELECT ms_merge_key, common_name, scientific_name FROM taxon"), by = "ms_merge_key") |>
    group_by(zone_key, sp_cat) |> slice_max(contrib, n = 1, with_ties = FALSE) |> ungroup() |>
    transmute(zone_key, sp_cat, top_species = coalesce(common_name, scientific_name), contrib = signif(contrib, 3))
  gulf_turtle_top <- top |> filter(zone_key %in% c("GAA", "GAB"), sp_cat == "turtle")
  arctic_mammal <- top |> filter(zone_key %in% c("BFT", "CHU", "HOP", "NOR"), sp_cat == "mammal")
  msens::report_table(bind_rows(gulf_turtle_top, arctic_mammal), caption = "top contributor: Gulf turtles, Arctic mammals")
  checks <- tibble(
    check  = c("GAA turtles >> 100", "GAB turtles >> 100", "ALA turtles << 100", "GEO turtles not reportable",
               "Kemp's ridley is the top turtle of GAA (GAB: green turtle on v8)", "Arctic Program Areas keep a mammal column",
               "Alaska holds < 2 % of the nation's turtle importance (global weighting; v8 preview 1.05 %)"),
    value  = c(v10_of("GAA", "turtle"), v10_of("GAB", "turtle"), v10_of("ALA", "turtle"), as.numeric(rep_of("GEO", "turtle")),
               NA, nrow(arctic_mammal), ak_turtle_pct),
    pass   = c(isTRUE(v10_of("GAA", "turtle") > 200), isTRUE(v10_of("GAB", "turtle") > 200), isTRUE(v10_of("ALA", "turtle") < 50) || is.na(v10_of("ALA", "turtle")),
               !rep_of("GEO", "turtle"), any(grepl("Kemp", gulf_turtle_top$top_species[gulf_turtle_top$zone_key == "GAA"])), nrow(arctic_mammal) >= 2,
               share_denom != "global" || ak_turtle_pct < 2))   # the page measured 0.7 % on v9 (AquaX surfaces); v8 gives ~1.05 %
  knitr::kable(checks)
  # Spearman between the within-ecoregion (v8) and across-the-nation (v10) composite rankings
  v8c <- dbGetQuery(con, "SELECT z.val AS zone_key, zm.val AS score_v8 FROM zone_metric zm JOIN zone z USING (zone_seq) JOIN metric m USING (metric_seq)
    WHERE z.fld = 'programarea_key' AND m.metric_key = 'score_extriskspcat_primprod_ecoregionrescaled_equalweights'")
  cmp <- r$composite |> inner_join(zones, by = "zone_seq") |> filter(fld == "programarea_key") |> inner_join(v8c, by = "zone_key")
  rho <- cor(cmp$score, cmp$score_v8, method = "spearman")
  msens::report_table(cmp |> transmute(zone_key, score_v8 = round(score_v8, 1), score_v10 = round(score, 1), n_components) |> arrange(desc(score_v10)),
                      caption = glue("Program-Area composite: within ecoregion (v8) vs across the nation (v10, {share_denom}); Spearman rho = {round(rho, 3)}"))
  # the plausibility targets are the CONTROL's expectations (v10: the Alaska turtles below 2 %, Kemp's ridley topping
  # the Gulf, ...). A method variant may legitimately move them -- `no_occ_rule` puts the turtles back into Alaska,
  # which is the point of the variant -- so a method run WARNS and the attribution report is its gate; the control
  # (every v11 flag at its control value) still stops.
  if (isTRUE(v11_flags_off)) {
    stopifnot("a plausibility target failed — inspect the tables above before trusting the family" = all(checks$pass))
  } else if (!all(checks$pass)) {
    log_warn("plausibility target(s) failed in a v11 method run ({paste(v11_flags_on, collapse = ', ')}): {paste(checks$check[!checks$pass], collapse = ' | ')} — expected to move for some variants; the attribution report judges it")
  } else log_info("plausibility targets all pass")
} else log_info("control run: plausibility checks skipped")

8 Manifest

Code
n_metric <- dbGetQuery(con, "SELECT count(*) n FROM metric")$n
dbDisconnect(con, shutdown = TRUE)
# the reference is the share run's: a control render writes the manifest only when none exists yet,
# never over a share reference; a share run writes it under BUILD_REFERENCE, when no share reference
# exists, or when its flags equal the reference's (a sensitivity render never moves it)
write_mf <- if (share_metrics) ((is_ref || is.null(ref) || !isTRUE(ref$share_metrics) || same_flags) && isTRUE(v11_flags_off)) else is.null(ref)
if (!write_mf) log_info("manifest not rewritten (the committed reference stays the control's); this run's digests: {paste(names(h_share), unlist(h_share), sep = '=', collapse = ' ')}")
if (write_mf) msens::write_manifest(
  manifest, target = "score_share_metrics",
  content_hash = if (share_metrics) msens::hash_query(dbConnect(duckdb()), glue("SELECT '{paste(unlist(h_share), collapse = '')}' AS a")) else paste(unlist(after), collapse = ""),
  stats = c(flags, list(ver = ver, ver_prev = ver_prev, n_metric_keys = n_metric, n_v8_keys = length(v8_keys), n_rows_written = n_written),
            setNames(as.list(unlist(after)), paste0("digest_v8_", names(after))),
            if (share_metrics) setNames(as.list(unlist(h_share)), paste0("digest_", names(h_share))) else list()),
  force = msens::force_target("score_share_metrics"))